BREAKING CHANGE FileInfo.len renamed to FileName.size (#4338)

This commit is contained in:
dubiousjim 2020-03-14 22:57:42 -04:00 committed by GitHub
parent 0df9823cba
commit 6cc40b0865
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 22 additions and 26 deletions

View file

@ -3,7 +3,7 @@ import { StatResponse } from "./ops/fs/stat.ts";
import { build } from "./build.ts"; import { build } from "./build.ts";
export interface FileInfo { export interface FileInfo {
len: number; size: number;
modified: number | null; modified: number | null;
accessed: number | null; accessed: number | null;
created: number | null; created: number | null;
@ -26,7 +26,7 @@ export interface FileInfo {
export class FileInfoImpl implements FileInfo { export class FileInfoImpl implements FileInfo {
private readonly _isFile: boolean; private readonly _isFile: boolean;
private readonly _isSymlink: boolean; private readonly _isSymlink: boolean;
len: number; size: number;
modified: number | null; modified: number | null;
accessed: number | null; accessed: number | null;
created: number | null; created: number | null;
@ -64,7 +64,7 @@ export class FileInfoImpl implements FileInfo {
this._isFile = this._res.isFile; this._isFile = this._res.isFile;
this._isSymlink = this._res.isSymlink; this._isSymlink = this._res.isSymlink;
this.len = this._res.len; this.size = this._res.size;
this.modified = modified ? modified : null; this.modified = modified ? modified : null;
this.accessed = accessed ? accessed : null; this.accessed = accessed ? accessed : null;
this.created = created ? created : null; this.created = created ? created : null;

View file

@ -661,7 +661,7 @@ declare namespace Deno {
append?: boolean; append?: boolean;
/** Sets the option for truncating a previous file. If a file is /** Sets the option for truncating a previous file. If a file is
* successfully opened with this option set it will truncate the file to `0` * successfully opened with this option set it will truncate the file to `0`
* length if it already exists. The file must be opened with write access * size if it already exists. The file must be opened with write access
* for truncate to work. */ * for truncate to work. */
truncate?: boolean; truncate?: boolean;
/** Sets the option to allow creating a new file, if one doesn't already /** Sets the option to allow creating a new file, if one doesn't already
@ -1058,16 +1058,12 @@ declare namespace Deno {
// @url js/file_info.d.ts // @url js/file_info.d.ts
/** UNSTABLE: 'len' maybe should be 'length' or 'size'. /** A FileInfo describes a file and is returned by `stat`, `lstat`,
*
* A FileInfo describes a file and is returned by `stat`, `lstat`,
* `statSync`, `lstatSync`. A list of FileInfo is returned by `readdir`, * `statSync`, `lstatSync`. A list of FileInfo is returned by `readdir`,
* `readdirSync`. */ * `readdirSync`. */
export interface FileInfo { export interface FileInfo {
/** **UNSTABLE**: `.len` maybe should be `.length` or `.size`. /** The size of the file, in bytes. */
* size: number;
* The size of the file, in bytes. */
len: number;
/** The last modification time of the file. This corresponds to the `mtime` /** The last modification time of the file. This corresponds to the `mtime`
* field from `stat` on Linux/Mac OS and `ftLastWriteTime` on Windows. This * field from `stat` on Linux/Mac OS and `ftLastWriteTime` on Windows. This
* may not be available on all platforms. */ * may not be available on all platforms. */

View file

@ -5,7 +5,7 @@ import { FileInfo, FileInfoImpl } from "../../file_info.ts";
export interface StatResponse { export interface StatResponse {
isFile: boolean; isFile: boolean;
isSymlink: boolean; isSymlink: boolean;
len: number; size: number;
modified: number; modified: number;
accessed: number; accessed: number;
created: number; created: number;

View file

@ -19,7 +19,7 @@ unitTest({ perms: { read: true } }, async function filesCopyToStdout(): Promise<
const file = await Deno.open(filename); const file = await Deno.open(filename);
assert(file.rid > 2); assert(file.rid > 2);
const bytesWritten = await Deno.copy(Deno.stdout, file); const bytesWritten = await Deno.copy(Deno.stdout, file);
const fileSize = Deno.statSync(filename).len; const fileSize = Deno.statSync(filename).size;
assertEquals(bytesWritten, fileSize); assertEquals(bytesWritten, fileSize);
console.log("bytes written", bytesWritten); console.log("bytes written", bytesWritten);
file.close(); file.close();
@ -234,12 +234,12 @@ unitTest(
const f = await Deno.create(filename); const f = await Deno.create(filename);
let fileInfo = Deno.statSync(filename); let fileInfo = Deno.statSync(filename);
assert(fileInfo.isFile()); assert(fileInfo.isFile());
assert(fileInfo.len === 0); assert(fileInfo.size === 0);
const enc = new TextEncoder(); const enc = new TextEncoder();
const data = enc.encode("Hello"); const data = enc.encode("Hello");
await f.write(data); await f.write(data);
fileInfo = Deno.statSync(filename); fileInfo = Deno.statSync(filename);
assert(fileInfo.len === 5); assert(fileInfo.size === 5);
f.close(); f.close();
// TODO: test different modes // TODO: test different modes
@ -258,11 +258,11 @@ unitTest(
// assert file was created // assert file was created
let fileInfo = Deno.statSync(filename); let fileInfo = Deno.statSync(filename);
assert(fileInfo.isFile()); assert(fileInfo.isFile());
assertEquals(fileInfo.len, 0); assertEquals(fileInfo.size, 0);
// write some data // write some data
await file.write(data); await file.write(data);
fileInfo = Deno.statSync(filename); fileInfo = Deno.statSync(filename);
assertEquals(fileInfo.len, 13); assertEquals(fileInfo.size, 13);
// assert we can't read from file // assert we can't read from file
let thrown = false; let thrown = false;
try { try {
@ -277,7 +277,7 @@ unitTest(
// assert that existing file is truncated on open // assert that existing file is truncated on open
file = await Deno.open(filename, "w"); file = await Deno.open(filename, "w");
file.close(); file.close();
const fileSize = Deno.statSync(filename).len; const fileSize = Deno.statSync(filename).size;
assertEquals(fileSize, 0); assertEquals(fileSize, 0);
await Deno.remove(tempDir, { recursive: true }); await Deno.remove(tempDir, { recursive: true });
} }
@ -296,11 +296,11 @@ unitTest(
// assert file was created // assert file was created
let fileInfo = Deno.statSync(filename); let fileInfo = Deno.statSync(filename);
assert(fileInfo.isFile()); assert(fileInfo.isFile());
assertEquals(fileInfo.len, 0); assertEquals(fileInfo.size, 0);
// write some data // write some data
await file.write(data); await file.write(data);
fileInfo = Deno.statSync(filename); fileInfo = Deno.statSync(filename);
assertEquals(fileInfo.len, 13); assertEquals(fileInfo.size, 13);
const buf = new Uint8Array(20); const buf = new Uint8Array(20);
// seeking from beginning of a file // seeking from beginning of a file

View file

@ -431,7 +431,7 @@ fn op_copy_file(
return Err(OpError::not_found("File not found".to_string())); return Err(OpError::not_found("File not found".to_string()));
} }
// returns length of from as u64 (we ignore) // returns size of from as u64 (we ignore)
fs::copy(&from, &to)?; fs::copy(&from, &to)?;
Ok(json!({})) Ok(json!({}))
}) })
@ -469,7 +469,7 @@ fn get_stat_json(
let mut json_val = json!({ let mut json_val = json!({
"isFile": metadata.is_file(), "isFile": metadata.is_file(),
"isSymlink": metadata.file_type().is_symlink(), "isSymlink": metadata.file_type().is_symlink(),
"len": metadata.len(), "size": metadata.len(),
// In seconds. Available on both Unix or Windows. // In seconds. Available on both Unix or Windows.
"modified":to_seconds!(metadata.modified()), "modified":to_seconds!(metadata.modified()),
"accessed":to_seconds!(metadata.accessed()), "accessed":to_seconds!(metadata.accessed()),

View file

@ -334,7 +334,7 @@ export class Tar {
); );
} }
const fileSize = info?.len ?? opts.contentSize; const fileSize = info?.size ?? opts.contentSize;
assert(fileSize != null, "fileSize must be set"); assert(fileSize != null, "fileSize must be set");
const tarData: TarDataWithSource = { const tarData: TarDataWithSource = {
fileName, fileName,

View file

@ -102,7 +102,7 @@ async function serveFile(
): Promise<Response> { ): Promise<Response> {
const [file, fileInfo] = await Promise.all([open(filePath), stat(filePath)]); const [file, fileInfo] = await Promise.all([open(filePath), stat(filePath)]);
const headers = new Headers(); const headers = new Headers();
headers.set("content-length", fileInfo.len.toString()); headers.set("content-length", fileInfo.size.toString());
headers.set("content-type", "text/plain; charset=utf-8"); headers.set("content-type", "text/plain; charset=utf-8");
const res = { const res = {
@ -135,7 +135,7 @@ async function serveDir(
} catch (e) {} } catch (e) {}
listEntry.push({ listEntry.push({
mode: modeToString(fileInfo.isDirectory(), mode), mode: modeToString(fileInfo.isDirectory(), mode),
size: fileInfo.isFile() ? fileLenToString(fileInfo.len) : "", size: fileInfo.isFile() ? fileLenToString(fileInfo.size) : "",
name: fileInfo.name ?? "", name: fileInfo.name ?? "",
url: fileUrl url: fileUrl
}); });

View file

@ -3,7 +3,7 @@ import { assert, assertEquals, assertThrows } from "../../testing/asserts.ts";
import Dirent from "./_fs_dirent.ts"; import Dirent from "./_fs_dirent.ts";
class FileInfoMock implements Deno.FileInfo { class FileInfoMock implements Deno.FileInfo {
len = -1; size = -1;
modified = -1; modified = -1;
accessed = -1; accessed = -1;
created = -1; created = -1;