From 52a568d2b283fed25ab79340e6bb483121504323 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Tue, 24 Dec 2024 02:27:07 -0800 Subject: [PATCH] Fix flaky zlib dictionary test (#15976) --- src/bun.js/api/zlib.classes.ts | 2 +- src/bun.js/node/node_zlib_binding.zig | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/bun.js/api/zlib.classes.ts b/src/bun.js/api/zlib.classes.ts index d907e3c838a665..3a0b9b59b5d6b8 100644 --- a/src/bun.js/api/zlib.classes.ts +++ b/src/bun.js/api/zlib.classes.ts @@ -10,7 +10,7 @@ function generate(name: string) { estimatedSize: true, klass: {}, JSType: "0b11101110", - values: ["writeCallback", "errorCallback"], + values: ["writeCallback", "errorCallback", "dictionary"], proto: { init: { fn: "init" }, diff --git a/src/bun.js/node/node_zlib_binding.zig b/src/bun.js/node/node_zlib_binding.zig index d892844e4a28fb..cd233554c775b3 100644 --- a/src/bun.js/node/node_zlib_binding.zig +++ b/src/bun.js/node/node_zlib_binding.zig @@ -358,6 +358,7 @@ pub const SNativeZlib = struct { pub fn init(this: *SNativeZlib, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.argumentsUndef(7).slice(); + const this_value = callframe.this(); if (arguments.len != 7) { return globalThis.ERR_MISSING_ARGS("init(windowBits, level, memLevel, strategy, writeResult, writeCallback, dictionary)", .{}).throw(); @@ -373,7 +374,12 @@ pub const SNativeZlib = struct { const dictionary = if (arguments[6].isUndefined()) null else arguments[6].asArrayBuffer(globalThis).?.byteSlice(); this.write_result = writeResult; - SNativeZlib.writeCallbackSetCached(callframe.this(), globalThis, writeCallback); + SNativeZlib.writeCallbackSetCached(this_value, globalThis, writeCallback); + + // Keep the dictionary alive by keeping a reference to it in the JS object. + if (dictionary != null) { + SNativeZlib.dictionarySetCached(this_value, globalThis, arguments[6]); + } this.stream.init(level, windowBits, memLevel, strategy, dictionary); @@ -723,7 +729,7 @@ pub const SNativeBrotli = struct { }; } - pub fn init(this: *@This(), globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { + pub fn init(this: *SNativeBrotli, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { const arguments = callframe.argumentsUndef(3).slice(); const this_value = callframe.this(); if (arguments.len != 3) {