diff --git a/lib/internal/webstreams/compression.js b/lib/internal/webstreams/compression.js index 6dfddbce248b7a..692f64af005af9 100644 --- a/lib/internal/webstreams/compression.js +++ b/lib/internal/webstreams/compression.js @@ -25,12 +25,11 @@ const { customInspectSymbol: kInspect, } = require('internal/util'); -const { - createDeflate, - createInflate, - createGzip, - createGunzip, -} = require('zlib'); +let zlib; +function lazyZlib() { + zlib ??= require('zlib'); + return zlib; +} const kHandle = Symbol('kHandle'); const kTransform = Symbol('kTransform'); @@ -59,10 +58,10 @@ class CompressionStream { this[kType] = 'CompressionStream'; switch (format) { case 'deflate': - this[kHandle] = createDeflate(); + this[kHandle] = lazyZlib().createDeflate(); break; case 'gzip': - this[kHandle] = createGzip(); + this[kHandle] = lazyZlib().createGzip(); break; default: throw new ERR_INVALID_ARG_VALUE('format', format); @@ -108,10 +107,10 @@ class DecompressionStream { this[kType] = 'DecompressionStream'; switch (format) { case 'deflate': - this[kHandle] = createInflate(); + this[kHandle] = lazyZlib().createInflate(); break; case 'gzip': - this[kHandle] = createGunzip(); + this[kHandle] = lazyZlib().createGunzip(); break; default: throw new ERR_INVALID_ARG_VALUE('format', format);