Compare commits

...

5 Commits

Author SHA1 Message Date
Meghan Denny
26aae0f6f6 missed this file 2025-09-03 18:04:23 -07:00
autofix-ci[bot]
a2c37aa57a [autofix.ci] apply automated fixes (attempt 3/3) 2025-09-04 01:03:54 +00:00
autofix-ci[bot]
af3ac92570 [autofix.ci] apply automated fixes (attempt 2/3) 2025-09-04 01:02:23 +00:00
autofix-ci[bot]
b72fec72f3 [autofix.ci] apply automated fixes 2025-09-04 01:00:24 +00:00
Meghan Denny
f6e2bd4d6e node:tty: add ReadStream/WriteStream.ref/unref 2025-09-03 17:58:20 -07:00
7 changed files with 75 additions and 0 deletions

View File

@@ -249,9 +249,11 @@ src/bun.js/node/assert/myers_diff.zig
src/bun.js/node/buffer.zig
src/bun.js/node/dir_iterator.zig
src/bun.js/node/fs_events.zig
src/bun.js/node/misc/KeepAlive.zig
src/bun.js/node/net/BlockList.zig
src/bun.js/node/node_assert_binding.zig
src/bun.js/node/node_assert.zig
src/bun.js/node/node_binding.zig
src/bun.js/node/node_cluster_binding.zig
src/bun.js/node/node_crypto_binding.zig
src/bun.js/node/node_error_binding.zig

View File

@@ -51,6 +51,7 @@ pub const UDPSocket = @import("./api/bun/udp_socket.zig").UDPSocket;
pub const Valkey = @import("../valkey/js_valkey.zig").JSValkeyClient;
pub const BlockList = @import("./node/net/BlockList.zig");
pub const NativeZstd = @import("./node/zlib/NativeZstd.zig");
pub const KeepAlive = @import("./node/misc/KeepAlive.zig");
pub const napi = @import("../napi/napi.zig");
pub const node = @import("./node.zig");

View File

@@ -87,6 +87,7 @@ pub const Classes = struct {
pub const BlockList = api.BlockList;
pub const NativeZstd = api.NativeZstd;
pub const SourceMap = bun.sourcemap.JSSourceMap;
pub const KeepAlive = api.KeepAlive;
};
const bun = @import("bun");

View File

@@ -0,0 +1,30 @@
globalThis: *jsc.JSGlobalObject,
keep_alive: bun.Async.KeepAlive,
pub fn constructor(globalThis: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JSError!*@This() {
_ = callframe;
return bun.new(@This(), .{
.globalThis = globalThis,
.keep_alive = .{},
});
}
pub fn finalize(this: *@This()) void {
this.keep_alive.unref(this.globalThis.bunVM());
bun.destroy(this);
}
pub fn jsRef(this: *@This(), globalThis: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JSError!jsc.JSValue {
_ = callframe;
this.keep_alive.ref(globalThis.bunVM());
return .js_undefined;
}
pub fn jsUnref(this: *@This(), globalThis: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JSError!jsc.JSValue {
_ = callframe;
this.keep_alive.unref(globalThis.bunVM());
return .js_undefined;
}
const bun = @import("bun");
const jsc = bun.jsc;

View File

@@ -333,4 +333,17 @@ export default [
Stats: { getter: "getStats" },
},
}),
define({
name: "KeepAlive",
construct: true,
noConstructor: false,
finalize: true,
configurable: false,
klass: {},
JSType: "0b11101110",
proto: {
ref: { fn: "jsRef" },
unref: { fn: "jsUnref" },
},
}),
];

View File

@@ -0,0 +1,5 @@
const std = @import("std");
const bun = @import("bun");
const jsc = bun.jsc;
pub const KeepAlive = jsc.Codegen.JSKeepAlive.getConstructor;

View File

@@ -11,6 +11,9 @@ const {
const { validateInteger } = require("internal/validators");
const fs = require("internal/fs/streams");
const KeepAlive = $zig("node_binding.zig", "KeepAlive");
const kKeepAlive = Symbol("kKeepAlive");
function ReadStream(fd): void {
if (!(this instanceof ReadStream)) {
@@ -19,6 +22,7 @@ function ReadStream(fd): void {
fs.ReadStream.$apply(this, ["", { fd }]);
this.isRaw = false;
this.isTTY = true;
this[kKeepAlive] = new KeepAlive();
}
$toClass(ReadStream, "ReadStream", fs.ReadStream);
@@ -74,6 +78,15 @@ Object.defineProperty(ReadStream, "prototype", {
return this;
};
Prototype.ref = function () {
// in Bun ReadStream is not a net.Socket so these methods need to be added explicitly
this[kKeepAlive].ref();
};
Prototype.unref = function () {
// in Bun ReadStream is not a net.Socket so these methods need to be added explicitly
this[kKeepAlive].unref();
};
Object.defineProperty(ReadStream, "prototype", { value: Prototype });
return Prototype;
@@ -89,6 +102,7 @@ function WriteStream(fd): void {
stream.columns = undefined;
stream.rows = undefined;
stream.isTTY = isatty(stream.fd);
stream[kKeepAlive] = new KeepAlive();
if (stream.isTTY) {
const windowSizeArray = [0, 0];
@@ -167,6 +181,15 @@ Object.defineProperty(WriteStream, "prototype", {
})();
};
WriteStream.prototype.ref = function () {
// in Bun WriteStream is not a net.Socket so these methods need to be added explicitly
this[kKeepAlive].ref();
};
WriteStream.prototype.unref = function () {
// in Bun WriteStream is not a net.Socket so these methods need to be added explicitly
this[kKeepAlive].unref();
};
return Real;
},
enumerable: true,