Skip to content

Commit

Permalink
fix fs-non-number-arguments-throw.test.js (#14312)
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro authored Oct 11, 2024
1 parent 874c9db commit 170fafb
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/bun.js/bindings/ErrorCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Expand Down
20 changes: 20 additions & 0 deletions src/js/node/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
65 changes: 65 additions & 0 deletions test/js/node/test/parallel/fs-non-number-arguments-throw.test.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 170fafb

Please sign in to comment.