fix fs-non-number-arguments-throw.test.js (#14312)

This commit is contained in:
Meghan Denny
2024-10-10 22:07:41 -07:00
committed by GitHub
parent 874c9dbb24
commit 170fafbca9
3 changed files with 86 additions and 1 deletions

View File

@@ -300,7 +300,7 @@ WTF::String ERR_OUT_OF_RANGE(JSC::ThrowScope& scope, JSC::JSGlobalObject* global
auto input = JSValueToStringSafe(globalObject, val_input);
RETURN_IF_EXCEPTION(scope, {});
return makeString("The value of \""_s, arg_name, "\" is out of range. It must be "_s, range, ". Received: \""_s, input, '"');
return makeString("The value of \""_s, arg_name, "\" is out of range. It must be "_s, range, ". Received: "_s, input);
}
}

View File

@@ -5,6 +5,9 @@ const promises = require("node:fs/promises");
const Stream = require("node:stream");
const types = require("node:util/types");
const { ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE } = require("internal/errors");
const { validateInteger } = require("internal/validators");
const NumberIsFinite = Number.isFinite;
const DateNow = Date.now;
const DatePrototypeGetTime = Date.prototype.getTime;
@@ -830,6 +833,18 @@ function ReadStream(this: typeof ReadStream, pathOrFd, options) {
// Get the stream controller
// We need the pointer to the underlying stream controller for the NativeReadable
if (start !== undefined) {
validateInteger(start, "start", 0);
}
if (end === undefined) {
end = Infinity;
} else if (end !== Infinity) {
validateInteger(end, "end", 0);
if (start !== undefined && start > end) {
throw new ERR_OUT_OF_RANGE("start", `<= "end" (here: ${end})`, start);
}
}
const stream = blobToStreamWithOffset.$apply(fileRef, [start]);
var ptr = stream.$bunNativePtr;
if (!ptr) {
@@ -1068,6 +1083,11 @@ var WriteStreamClass = (WriteStream = function WriteStream(path, options = defau
pos = defaultWriteStreamOptions.pos,
} = options;
if (start !== undefined) {
validateInteger(start, "start", 0);
options.pos = start;
}
var tempThis = {};
var handle = null;
if (fd != null) {

View File

@@ -0,0 +1,65 @@
//#FILE: test-fs-non-number-arguments-throw.js
//#SHA1: 65db5c653216831bc16d38c5d659fbffa296d3d8
//-----------------
'use strict';
const fs = require('fs');
const path = require('path');
const os = require('os');
const tmpdir = path.join(os.tmpdir(), 'test-fs-non-number-arguments-throw');
const tempFile = path.join(tmpdir, 'fs-non-number-arguments-throw');
beforeAll(() => {
if (fs.existsSync(tmpdir)) {
fs.rmSync(tmpdir, { recursive: true, force: true });
}
fs.mkdirSync(tmpdir, { recursive: true });
fs.writeFileSync(tempFile, 'abc\ndef');
});
afterAll(() => {
fs.rmSync(tmpdir, { recursive: true, force: true });
});
test('createReadStream with valid number arguments', (done) => {
const sanity = 'def';
const saneEmitter = fs.createReadStream(tempFile, { start: 4, end: 6 });
saneEmitter.on('data', (data) => {
expect(data.toString('utf8')).toBe(sanity);
done();
});
});
test('createReadStream throws with string start argument', () => {
expect(() => {
fs.createReadStream(tempFile, { start: '4', end: 6 });
}).toThrow(expect.objectContaining({
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: expect.any(String)
}));
});
test('createReadStream throws with string end argument', () => {
expect(() => {
fs.createReadStream(tempFile, { start: 4, end: '6' });
}).toThrow(expect.objectContaining({
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: expect.any(String)
}));
});
test('createWriteStream throws with string start argument', () => {
expect(() => {
fs.createWriteStream(tempFile, { start: '4' });
}).toThrow(expect.objectContaining({
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: expect.any(String)
}));
});
//<#END_FILE: test-fs-non-number-arguments-throw.js