Compare commits

...

2 Commits

Author SHA1 Message Date
Meghan Denny
2ea386de78 Merge branch 'main' into nektro-patch-40770 2025-11-28 18:11:21 -08:00
Meghan Denny
2580c4b352 node: fix test-fs-internal-assertencoding.js 2025-06-25 17:02:40 -07:00
2 changed files with 47 additions and 4 deletions

View File

@@ -3,7 +3,7 @@ import type { Dirent as DirentType, PathLike, Stats as StatsType } from "fs";
const EventEmitter = require("node:events");
const promises = require("node:fs/promises");
const types = require("node:util/types");
const { validateFunction, validateInteger } = require("internal/validators");
const { validateAbortSignal, validateFunction, validateInteger } = require("internal/validators");
const kEmptyObject = Object.freeze(Object.create(null));
@@ -648,6 +648,33 @@ function getValidatedPath(p: any) {
if (typeof p !== "string") throw $ERR_INVALID_ARG_TYPE("path", "string or URL", p);
return require("node:path").resolve(p);
}
function getOptions(options, defaultOptions = kEmptyObject) {
if (options == null || typeof options === "function") {
return defaultOptions;
}
if (typeof options === "string") {
defaultOptions = { ...defaultOptions };
defaultOptions.encoding = options;
options = defaultOptions;
} else if (typeof options !== "object") {
throw $ERR_INVALID_ARG_TYPE("options", ["string", "object"], options);
}
if (options.encoding !== "buffer") assertEncoding(options.encoding);
if (options.signal !== undefined) {
validateAbortSignal(options.signal, "options.signal");
}
return options;
}
function assertEncoding(encoding) {
if (encoding && !Buffer.isEncoding(encoding)) {
const reason = "is invalid encoding";
throw $ERR_INVALID_ARG_VALUE("encoding", encoding, reason);
}
}
function watchFile(filename, options, listener) {
filename = getValidatedPath(filename);
@@ -1040,9 +1067,10 @@ function _toUnixTimestamp(time: any, name = "time") {
}
function opendirSync(path, options) {
// TODO: validatePath
// validateString(path, "path");
return new Dir(1, path, options);
const handle = 1;
path = getValidatedPath(path);
options = getOptions(options, { encoding: "utf8" });
return new Dir(handle, path, options);
}
class Dir {

View File

@@ -0,0 +1,15 @@
'use strict';
// Tests to verify that a correctly formatted invalid encoding error
// is thrown. Originally the internal assertEncoding utility was
// reversing the arguments when constructing the ERR_INVALID_ARG_VALUE
// error.
require('../common');
const { opendirSync } = require('node:fs');
const { throws } = require('node:assert');
throws(() => opendirSync('.', { encoding: 'no' }), {
code: 'ERR_INVALID_ARG_VALUE',
message: 'The argument \'encoding\' is invalid encoding. Received \'no\'',
});