Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node/fs): Enable test-fs-read-zero-length.js and test-fs-read-type.js #2692

Merged
merged 13 commits into from
Sep 25, 2022
74 changes: 39 additions & 35 deletions node/_fs/_fs_read.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { Buffer } from "../buffer.ts";
import { assert } from "../../testing/asserts.ts";
import { ERR_INVALID_ARG_TYPE } from "../internal/errors.ts";
import {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
} from "../internal/errors.ts";
validateOffsetLengthRead,
validatePosition,
} from "../internal/fs/utils.mjs";
import { validateBuffer, validateInteger } from "../internal/validators.mjs";

type readOptions = {
buffer: Buffer | Uint8Array;
Expand Down Expand Up @@ -66,11 +67,10 @@ export function read(
cb = optOrBufferOrCb;
} else {
offset = offsetOrCallback as number;
validateInteger(offset, "offset", 0);
cb = callback;
}

if (!cb) throw new ERR_INVALID_ARG_TYPE("cb", "Callback", cb);

if (
optOrBufferOrCb instanceof Buffer || optOrBufferOrCb instanceof Uint8Array
) {
Expand All @@ -82,28 +82,32 @@ export function read(
position = null;
} else {
const opt = optOrBufferOrCb as readOptions;
if (
!(opt.buffer instanceof Buffer) && !(opt.buffer instanceof Uint8Array)
) {
if (opt.buffer === null) {
// @ts-ignore: Intentionally create TypeError for passing test-fs-read.js#L87
length = opt.buffer.byteLength;
}
throw new ERR_INVALID_ARG_TYPE("buffer", [
"Buffer",
"TypedArray",
"DataView",
], optOrBufferOrCb);
}
offset = opt.offset ?? 0;
buffer = opt.buffer ?? Buffer.alloc(16384);
length = opt.length ?? buffer.byteLength;
position = opt.position ?? null;
}

assert(offset >= 0, "offset should be greater or equal to 0");
assert(
offset + length <= buffer.byteLength,
`buffer doesn't have enough data: byteLength = ${buffer.byteLength}, offset + length = ${
offset + length
}`,
);

if (buffer.byteLength == 0) {
throw new ERR_INVALID_ARG_VALUE(
"buffer",
buffer,
"is empty and cannot be written",
);
if (position == null) {
position = -1;
}

validatePosition(position);
validateOffsetLengthRead(offset, length, buffer.byteLength);

let err: Error | null = null,
numberOfBytesRead: number | null = null;

Expand All @@ -123,6 +127,8 @@ export function read(
err = error instanceof Error ? error : new Error("[non-error thrown]");
}

if (!cb) throw new ERR_INVALID_ARG_TYPE("cb", "Callback", cb);

if (err) {
(callback as (err: Error) => void)(err);
} else {
Expand Down Expand Up @@ -154,34 +160,32 @@ export function readSync(
): number {
let offset = 0;

if (length == null) {
length = 0;
if (typeof fd !== "number") {
throw new ERR_INVALID_ARG_TYPE("fd", "number", fd);
}

if (buffer.byteLength == 0) {
throw new ERR_INVALID_ARG_VALUE(
"buffer",
buffer,
"is empty and cannot be written",
);
validateBuffer(buffer);

if (length == null) {
length = 0;
}

if (typeof offsetOrOpt === "number") {
offset = offsetOrOpt;
validateInteger(offset, "offset", 0);
} else {
const opt = offsetOrOpt as readSyncOptions;
offset = opt.offset ?? 0;
length = opt.length ?? buffer.byteLength;
position = opt.position ?? null;
}

assert(offset >= 0, "offset should be greater or equal to 0");
assert(
offset + length <= buffer.byteLength,
`buffer doesn't have enough data: byteLength = ${buffer.byteLength}, offset + length = ${
offset + length
}`,
);
if (position == null) {
position = -1;
}

validatePosition(position);
validateOffsetLengthRead(offset, length, buffer.byteLength);

let currentPosition = 0;
if (typeof position === "number" && position >= 0) {
Expand Down
3 changes: 2 additions & 1 deletion node/_tools/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"test-fs-append-file.js",
"test-fs-chmod-mask.js",
"test-fs-chmod.js",
"test-fs-read.js",
"test-fs-rmdir-recursive.js",
"test-fs-write-file.js",
"test-fs-write.js",
Expand Down Expand Up @@ -306,6 +305,8 @@
"test-fs-read-stream-resume.js",
"test-fs-read-stream-throw-type-error.js",
"test-fs-read-stream.js",
"test-fs-read-type.js",
"test-fs-read-zero-length.js",
"test-fs-read.js",
"test-fs-readdir-stack-overflow.js",
"test-fs-readdir.js",
Expand Down