Skip to content

Commit

Permalink
chore: sync up Node.js test files for v20.11.1 (denoland#24066)
Browse files Browse the repository at this point in the history
Co-authored-by: Yoshiya Hinosawa <[email protected]>
  • Loading branch information
bartlomieju and kt3k committed Jun 11, 2024
1 parent 3d41b48 commit 6a356af
Show file tree
Hide file tree
Showing 89 changed files with 937 additions and 703 deletions.
4 changes: 0 additions & 4 deletions ext/node/polyfills/_fs/_fs_read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ export function read(
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",
Expand Down
6 changes: 1 addition & 5 deletions ext/node/polyfills/_fs/_fs_write.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import * as io from "ext:deno_io/12_io.js";
import * as fs from "ext:deno_fs/30_fs.js";
import {
getValidatedFd,
showStringCoercionDeprecation,
validateOffsetLengthWrite,
validateStringAfterArrayBufferView,
} from "ext:deno_node/internal/fs/utils.mjs";
Expand Down Expand Up @@ -114,9 +113,6 @@ export function write(fd, buffer, offset, length, position, callback) {
// `fs.write(fd, string[, position[, encoding]], callback)`

validateStringAfterArrayBufferView(buffer, "buffer");
if (typeof buffer !== "string") {
showStringCoercionDeprecation();
}

if (typeof position !== "function") {
if (typeof offset === "function") {
Expand All @@ -128,7 +124,7 @@ export function write(fd, buffer, offset, length, position, callback) {
length = "utf-8";
}

const str = String(buffer);
const str = buffer;
validateEncoding(str, length);
callback = maybeCallback(position);
buffer = Buffer.from(str, length);
Expand Down
20 changes: 5 additions & 15 deletions ext/node/polyfills/_fs/_fs_writeFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
denoErrorToNodeError,
} from "ext:deno_node/internal/errors.ts";
import {
showStringCoercionDeprecation,
validateStringAfterArrayBufferView,
} from "ext:deno_node/internal/fs/utils.mjs";
import { promisify } from "ext:deno_node/internal/util.mjs";
Expand All @@ -32,8 +31,7 @@ interface Writer {

export function writeFile(
pathOrRid: string | number | URL,
// deno-lint-ignore ban-types
data: string | Uint8Array | Object,
data: string | Uint8Array,
optOrCallback: Encodings | CallbackWithError | WriteFileOptions | undefined,
callback?: CallbackWithError,
) {
Expand Down Expand Up @@ -61,10 +59,7 @@ export function writeFile(

if (!ArrayBuffer.isView(data)) {
validateStringAfterArrayBufferView(data, "data");
if (typeof data !== "string") {
showStringCoercionDeprecation();
}
data = Buffer.from(String(data), encoding);
data = Buffer.from(data, encoding);
}

const isRid = typeof pathOrRid === "number";
Expand Down Expand Up @@ -101,15 +96,13 @@ export function writeFile(

export const writeFilePromise = promisify(writeFile) as (
pathOrRid: string | number | URL,
// deno-lint-ignore ban-types
data: string | Uint8Array | Object,
data: string | Uint8Array,
options?: Encodings | WriteFileOptions,
) => Promise<void>;

export function writeFileSync(
pathOrRid: string | number | URL,
// deno-lint-ignore ban-types
data: string | Uint8Array | Object,
data: string | Uint8Array,
options?: Encodings | WriteFileOptions,
) {
pathOrRid = pathOrRid instanceof URL ? pathFromURL(pathOrRid) : pathOrRid;
Expand All @@ -127,10 +120,7 @@ export function writeFileSync(

if (!ArrayBuffer.isView(data)) {
validateStringAfterArrayBufferView(data, "data");
if (typeof data !== "string") {
showStringCoercionDeprecation();
}
data = Buffer.from(String(data), encoding);
data = Buffer.from(data, encoding);
}

const isRid = typeof pathOrRid === "number";
Expand Down
23 changes: 17 additions & 6 deletions ext/node/polyfills/_stream.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3754,7 +3754,14 @@ var require_writable = __commonJS({
this.destroyed = false;
const noDecode = !!(options && options.decodeStrings === false);
this.decodeStrings = !noDecode;
this.defaultEncoding = options && options.defaultEncoding || "utf8";
const defaultEncoding = options?.defaultEncoding;
if (defaultEncoding == null) {
this.defaultEncoding = 'utf8';
} else if (Buffer2.isEncoding(defaultEncoding)) {
this.defaultEncoding = defaultEncoding;
} else {
throw new ERR_UNKNOWN_ENCODING(defaultEncoding);
}
this.length = 0;
this.writing = false;
this.corked = 0;
Expand Down Expand Up @@ -3845,10 +3852,12 @@ var require_writable = __commonJS({
const state = stream._writableState;
if (typeof encoding === "function") {
cb = encoding;
encoding = state.defaultEncoding;
// Simulates https://github.com/nodejs/node/commit/dbed0319ac438dcbd6e92483f3280b1dc6767e00
encoding = state.objectMode ? undefined : state.defaultEncoding;
} else {
if (!encoding) {
encoding = state.defaultEncoding;
// Simulates https://github.com/nodejs/node/commit/dbed0319ac438dcbd6e92483f3280b1dc6767e00
encoding = state.objectMode ? undefined : state.defaultEncoding;
} else if (encoding !== "buffer" && !Buffer2.isEncoding(encoding)) {
throw new ERR_UNKNOWN_ENCODING(encoding);
}
Expand Down Expand Up @@ -4031,7 +4040,7 @@ var require_writable = __commonJS({
}
while (count-- > 0) {
state.pendingcb--;
cb();
cb(null);
}
if (state.destroyed) {
errorBuffer(state);
Expand Down Expand Up @@ -4158,8 +4167,10 @@ var require_writable = __commonJS({
err = new ERR_STREAM_DESTROYED("end");
}
if (typeof cb === "function") {
if (err || state.finished) {
if (err) {
process.nextTick(cb, err);
} else if (state.finished) {
process.nextTick(cb, null);
} else {
state[kOnFinished].push(cb);
}
Expand Down Expand Up @@ -4246,7 +4257,7 @@ var require_writable = __commonJS({
state.finished = true;
const onfinishCallbacks = state[kOnFinished].splice(0);
for (let i = 0; i < onfinishCallbacks.length; i++) {
onfinishCallbacks[i]();
onfinishCallbacks[i](null);
}
stream.emit("finish");
if (state.autoDestroy) {
Expand Down
31 changes: 24 additions & 7 deletions ext/node/polyfills/internal/buffer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
import { normalizeEncoding } from "ext:deno_node/internal/util.mjs";
import { validateBuffer } from "ext:deno_node/internal/validators.mjs";
import { isUint8Array } from "ext:deno_node/internal/util/types.ts";
import { ERR_INVALID_STATE } from "ext:deno_node/internal/errors.ts";
import { ERR_INVALID_STATE, NodeError } from "ext:deno_node/internal/errors.ts";
import {
forgivingBase64Encode,
forgivingBase64UrlEncode,
Expand Down Expand Up @@ -167,10 +167,7 @@ Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype);
Object.setPrototypeOf(Buffer, Uint8Array);

function assertSize(size) {
validateNumber(size, "size");
if (!(size >= 0 && size <= kMaxLength)) {
throw new codes.ERR_INVALID_ARG_VALUE.RangeError("size", size);
}
validateNumber(size, "size", 0, kMaxLength);
}

function _alloc(size, fill, encoding) {
Expand Down Expand Up @@ -852,7 +849,14 @@ function _base64Slice(buf, start, end) {
const decoder = new TextDecoder();

function _utf8Slice(buf, start, end) {
return decoder.decode(buf.slice(start, end));
try {
return decoder.decode(buf.slice(start, end));
} catch (err) {
if (err instanceof TypeError) {
throw new NodeError("ERR_STRING_TOO_LONG", "String too long");
}
throw err;
}
}

function _latin1Slice(buf, start, end) {
Expand Down Expand Up @@ -2297,10 +2301,23 @@ export function boundsError(value, length, type) {
);
}

export function validateNumber(value, name) {
export function validateNumber(value, name, min = undefined, max) {
if (typeof value !== "number") {
throw new codes.ERR_INVALID_ARG_TYPE(name, "number", value);
}

if (
(min != null && value < min) || (max != null && value > max) ||
((min != null || max != null) && Number.isNaN(value))
) {
throw new codes.ERR_OUT_OF_RANGE(
name,
`${min != null ? `>= ${min}` : ""}${
min != null && max != null ? " && " : ""
}${max != null ? `<= ${max}` : ""}`,
value,
);
}
}

function checkInt(value, min, max, buf, offset, byteLength) {
Expand Down
5 changes: 2 additions & 3 deletions ext/node/polyfills/internal/cli_table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ const renderRow = (row: string[], columnWidths: number[]) => {
for (let i = 0; i < row.length; i++) {
const cell = row[i];
const len = getStringWidth(cell);
const needed = (columnWidths[i] - len) / 2;
const needed = columnWidths[i] - len;
// round(needed) + ceil(needed) will always add up to the amount
// of spaces we need while also left justifying the output.
out += " ".repeat(needed) + cell +
" ".repeat(Math.ceil(needed));
out += cell + " ".repeat(Math.ceil(needed));
if (i !== row.length - 1) {
out += tableChars.middle;
}
Expand Down
31 changes: 6 additions & 25 deletions ext/node/polyfills/internal/fs/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
isUint8Array,
} from "ext:deno_node/internal/util/types.ts";
import { once } from "ext:deno_node/internal/util.mjs";
import { deprecate } from "node:util";
import { toPathIfFileURL } from "ext:deno_node/internal/url.ts";
import {
validateAbortSignal,
Expand Down Expand Up @@ -959,24 +958,13 @@ export const getValidMode = hideStackFrames((mode, type) => {

export const validateStringAfterArrayBufferView = hideStackFrames(
(buffer, name) => {
if (typeof buffer === "string") {
return;
}

if (
typeof buffer === "object" &&
buffer !== null &&
typeof buffer.toString === "function" &&
Object.prototype.hasOwnProperty.call(buffer, "toString")
) {
return;
if (typeof buffer !== "string") {
throw new ERR_INVALID_ARG_TYPE(
name,
["string", "Buffer", "TypedArray", "DataView"],
buffer,
);
}

throw new ERR_INVALID_ARG_TYPE(
name,
["string", "Buffer", "TypedArray", "DataView"],
buffer,
);
},
);

Expand Down Expand Up @@ -1005,12 +993,6 @@ export const constants = {
kWriteFileMaxChunkSize,
};

export const showStringCoercionDeprecation = deprecate(
() => {},
"Implicit coercion of objects with own toString property is deprecated.",
"DEP0162",
);

export default {
constants,
assertEncoding,
Expand All @@ -1030,7 +1012,6 @@ export default {
preprocessSymlinkDestination,
realpathCacheKey,
getStatsFromBinding,
showStringCoercionDeprecation,
stringToFlags,
stringToSymlinkType,
Stats,
Expand Down
15 changes: 14 additions & 1 deletion ext/node/polyfills/internal/validators.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,23 @@ function validateString(value, name) {
* @param {unknown} value
* @param {string} name
*/
function validateNumber(value, name) {
function validateNumber(value, name, min = undefined, max) {
if (typeof value !== "number") {
throw new codes.ERR_INVALID_ARG_TYPE(name, "number", value);
}

if (
(min != null && value < min) || (max != null && value > max) ||
((min != null || max != null) && Number.isNaN(value))
) {
throw new codes.ERR_OUT_OF_RANGE(
name,
`${min != null ? `>= ${min}` : ""}${
min != null && max != null ? " && " : ""
}${max != null ? `<= ${max}` : ""}`,
value,
);
}
}

/**
Expand Down
6 changes: 5 additions & 1 deletion ext/node/polyfills/path/_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,17 @@ export function normalizeString(
return res;
}

function formatExt(ext) {
return ext ? `${ext[0] === "." ? "" : "."}${ext}` : "";
}

export function _format(
sep: string,
pathObject: FormatInputPathObject,
): string {
const dir: string | undefined = pathObject.dir || pathObject.root;
const base: string = pathObject.base ||
(pathObject.name || "") + (pathObject.ext || "");
(pathObject.name || "") + formatExt(pathObject.ext);
if (!dir) return base;
if (dir === pathObject.root) return dir + base;
return dir + sep + base;
Expand Down

0 comments on commit 6a356af

Please sign in to comment.