Skip to content

Commit

Permalink
node:stream: fix setDefaultHighWaterMark (#14305)
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro authored Oct 3, 2024
1 parent 13ca454 commit 8e5255d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 50 deletions.
50 changes: 0 additions & 50 deletions src/js/node/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,14 +617,9 @@ var require_validators = __commonJS({
ArrayPrototypeIncludes,
ArrayPrototypeJoin,
ArrayPrototypeMap,
NumberIsInteger,
NumberMAX_SAFE_INTEGER,
NumberMIN_SAFE_INTEGER,
NumberParseInt,
RegExpPrototypeTest,
String: String2,
StringPrototypeToUpperCase,
StringPrototypeTrim,
} = require_primordials();
var {
hideStackFrames,
Expand Down Expand Up @@ -2121,49 +2116,6 @@ var require_from = __commonJS({
},
});

var require_state = __commonJS({
"node_modules/readable-stream/lib/internal/streams/state.js"(exports, module) {
"use strict";

const { MathFloor, NumberIsInteger } = require_primordials();
const { ERR_INVALID_ARG_VALUE } = require_errors().codes;
let defaultHighWaterMarkBytes = 16 * 1024;
let defaultHighWaterMarkObjectMode = 16;
function highWaterMarkFrom(options, isDuplex, duplexKey) {
return options.highWaterMark != null ? options.highWaterMark : isDuplex ? options[duplexKey] : null;
}
function getDefaultHighWaterMark(objectMode) {
return objectMode ? defaultHighWaterMarkObjectMode : defaultHighWaterMarkBytes;
}
function setDefaultHighWaterMark(objectMode, value) {
validateInteger(value, "value", 0);
if (objectMode) {
defaultHighWaterMarkObjectMode = value;
} else {
defaultHighWaterMarkBytes = value;
}
}
function getHighWaterMark(state, options, duplexKey, isDuplex) {
const hwm = highWaterMarkFrom(options, isDuplex, duplexKey);
if (hwm != null) {
if (!NumberIsInteger(hwm) || hwm < 0) {
const name = isDuplex ? `options.${duplexKey}` : "options.highWaterMark";
throw new ERR_INVALID_ARG_VALUE(name, hwm);
}
return MathFloor(hwm);
}

// Default value
return getDefaultHighWaterMark(state.objectMode);
}
module.exports = {
getHighWaterMark,
getDefaultHighWaterMark,
setDefaultHighWaterMark,
};
},
});

var _ReadableFromWeb;
var _ReadableFromWebForUndici;

Expand Down Expand Up @@ -2191,8 +2143,6 @@ var require_readable = __commonJS({

const BufferList = $cpp("JSBufferList.cpp", "getBufferList");

const { getHighWaterMark, getDefaultHighWaterMark } = require_state();

const { AbortError } = require_errors();

// TODO(benjamingr) it is likely slower to do it this way than with free functions
Expand Down
42 changes: 42 additions & 0 deletions test/js/node/test/parallel/stream-set-default-hwm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//#FILE: test-stream-set-default-hwm.js
//#SHA1: bc1189f9270a4b5463d8421ef234fc7baaad667f
//-----------------
"use strict";

const { setDefaultHighWaterMark, getDefaultHighWaterMark, Writable, Readable, Transform } = require("stream");

test("setDefaultHighWaterMark and getDefaultHighWaterMark for object mode", () => {
expect(getDefaultHighWaterMark(false)).not.toBe(32 * 1000);
setDefaultHighWaterMark(false, 32 * 1000);
expect(getDefaultHighWaterMark(false)).toBe(32 * 1000);
});

test("setDefaultHighWaterMark and getDefaultHighWaterMark for non-object mode", () => {
expect(getDefaultHighWaterMark(true)).not.toBe(32);
setDefaultHighWaterMark(true, 32);
expect(getDefaultHighWaterMark(true)).toBe(32);
});

test("Writable stream uses new default high water mark", () => {
const w = new Writable({
write() {},
});
expect(w.writableHighWaterMark).toBe(32 * 1000);
});

test("Readable stream uses new default high water mark", () => {
const r = new Readable({
read() {},
});
expect(r.readableHighWaterMark).toBe(32 * 1000);
});

test("Transform stream uses new default high water mark for both readable and writable", () => {
const t = new Transform({
transform() {},
});
expect(t.writableHighWaterMark).toBe(32 * 1000);
expect(t.readableHighWaterMark).toBe(32 * 1000);
});

//<#END_FILE: test-stream-set-default-hwm.js

0 comments on commit 8e5255d

Please sign in to comment.