11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst { 41cb0ef41Sopenharmony_ci ObjectDefineProperties, 51cb0ef41Sopenharmony_ci Symbol, 61cb0ef41Sopenharmony_ci} = primordials; 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst { 91cb0ef41Sopenharmony_ci codes: { 101cb0ef41Sopenharmony_ci ERR_INVALID_ARG_VALUE, 111cb0ef41Sopenharmony_ci ERR_INVALID_THIS, 121cb0ef41Sopenharmony_ci }, 131cb0ef41Sopenharmony_ci} = require('internal/errors'); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciconst { 161cb0ef41Sopenharmony_ci newReadableWritablePairFromDuplex, 171cb0ef41Sopenharmony_ci} = require('internal/webstreams/adapters'); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciconst { customInspect } = require('internal/webstreams/util'); 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ciconst { 221cb0ef41Sopenharmony_ci customInspectSymbol: kInspect, 231cb0ef41Sopenharmony_ci kEnumerableProperty, 241cb0ef41Sopenharmony_ci} = require('internal/util'); 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_cilet zlib; 271cb0ef41Sopenharmony_cifunction lazyZlib() { 281cb0ef41Sopenharmony_ci zlib ??= require('zlib'); 291cb0ef41Sopenharmony_ci return zlib; 301cb0ef41Sopenharmony_ci} 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciconst kHandle = Symbol('kHandle'); 331cb0ef41Sopenharmony_ciconst kTransform = Symbol('kTransform'); 341cb0ef41Sopenharmony_ciconst kType = Symbol('kType'); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci/** 371cb0ef41Sopenharmony_ci * @typedef {import('./readablestream').ReadableStream} ReadableStream 381cb0ef41Sopenharmony_ci * @typedef {import('./writablestream').WritableStream} WritableStream 391cb0ef41Sopenharmony_ci */ 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_cifunction isCompressionStream(value) { 421cb0ef41Sopenharmony_ci return typeof value?.[kHandle] === 'object' && 431cb0ef41Sopenharmony_ci value?.[kType] === 'CompressionStream'; 441cb0ef41Sopenharmony_ci} 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_cifunction isDecompressionStream(value) { 471cb0ef41Sopenharmony_ci return typeof value?.[kHandle] === 'object' && 481cb0ef41Sopenharmony_ci value?.[kType] === 'DecompressionStream'; 491cb0ef41Sopenharmony_ci} 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ciclass CompressionStream { 521cb0ef41Sopenharmony_ci /** 531cb0ef41Sopenharmony_ci * @param {'deflate'|'gzip'} format 541cb0ef41Sopenharmony_ci */ 551cb0ef41Sopenharmony_ci constructor(format) { 561cb0ef41Sopenharmony_ci this[kType] = 'CompressionStream'; 571cb0ef41Sopenharmony_ci switch (format) { 581cb0ef41Sopenharmony_ci case 'deflate': 591cb0ef41Sopenharmony_ci this[kHandle] = lazyZlib().createDeflate(); 601cb0ef41Sopenharmony_ci break; 611cb0ef41Sopenharmony_ci case 'gzip': 621cb0ef41Sopenharmony_ci this[kHandle] = lazyZlib().createGzip(); 631cb0ef41Sopenharmony_ci break; 641cb0ef41Sopenharmony_ci default: 651cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_VALUE('format', format); 661cb0ef41Sopenharmony_ci } 671cb0ef41Sopenharmony_ci this[kTransform] = newReadableWritablePairFromDuplex(this[kHandle]); 681cb0ef41Sopenharmony_ci } 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci /** 711cb0ef41Sopenharmony_ci * @readonly 721cb0ef41Sopenharmony_ci * @type {ReadableStream} 731cb0ef41Sopenharmony_ci */ 741cb0ef41Sopenharmony_ci get readable() { 751cb0ef41Sopenharmony_ci if (!isCompressionStream(this)) 761cb0ef41Sopenharmony_ci throw new ERR_INVALID_THIS('CompressionStream'); 771cb0ef41Sopenharmony_ci return this[kTransform].readable; 781cb0ef41Sopenharmony_ci } 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci /** 811cb0ef41Sopenharmony_ci * @readonly 821cb0ef41Sopenharmony_ci * @type {WritableStream} 831cb0ef41Sopenharmony_ci */ 841cb0ef41Sopenharmony_ci get writable() { 851cb0ef41Sopenharmony_ci if (!isCompressionStream(this)) 861cb0ef41Sopenharmony_ci throw new ERR_INVALID_THIS('CompressionStream'); 871cb0ef41Sopenharmony_ci return this[kTransform].writable; 881cb0ef41Sopenharmony_ci } 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci [kInspect](depth, options) { 911cb0ef41Sopenharmony_ci if (!isCompressionStream(this)) 921cb0ef41Sopenharmony_ci throw new ERR_INVALID_THIS('CompressionStream'); 931cb0ef41Sopenharmony_ci customInspect(depth, options, 'CompressionStream', { 941cb0ef41Sopenharmony_ci readable: this[kTransform].readable, 951cb0ef41Sopenharmony_ci writable: this[kTransform].writable, 961cb0ef41Sopenharmony_ci }); 971cb0ef41Sopenharmony_ci } 981cb0ef41Sopenharmony_ci} 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ciclass DecompressionStream { 1011cb0ef41Sopenharmony_ci /** 1021cb0ef41Sopenharmony_ci * @param {'deflate'|'gzip'} format 1031cb0ef41Sopenharmony_ci */ 1041cb0ef41Sopenharmony_ci constructor(format) { 1051cb0ef41Sopenharmony_ci this[kType] = 'DecompressionStream'; 1061cb0ef41Sopenharmony_ci switch (format) { 1071cb0ef41Sopenharmony_ci case 'deflate': 1081cb0ef41Sopenharmony_ci this[kHandle] = lazyZlib().createInflate(); 1091cb0ef41Sopenharmony_ci break; 1101cb0ef41Sopenharmony_ci case 'gzip': 1111cb0ef41Sopenharmony_ci this[kHandle] = lazyZlib().createGunzip(); 1121cb0ef41Sopenharmony_ci break; 1131cb0ef41Sopenharmony_ci default: 1141cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_VALUE('format', format); 1151cb0ef41Sopenharmony_ci } 1161cb0ef41Sopenharmony_ci this[kTransform] = newReadableWritablePairFromDuplex(this[kHandle]); 1171cb0ef41Sopenharmony_ci } 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci /** 1201cb0ef41Sopenharmony_ci * @readonly 1211cb0ef41Sopenharmony_ci * @type {ReadableStream} 1221cb0ef41Sopenharmony_ci */ 1231cb0ef41Sopenharmony_ci get readable() { 1241cb0ef41Sopenharmony_ci if (!isDecompressionStream(this)) 1251cb0ef41Sopenharmony_ci throw new ERR_INVALID_THIS('DecompressionStream'); 1261cb0ef41Sopenharmony_ci return this[kTransform].readable; 1271cb0ef41Sopenharmony_ci } 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci /** 1301cb0ef41Sopenharmony_ci * @readonly 1311cb0ef41Sopenharmony_ci * @type {WritableStream} 1321cb0ef41Sopenharmony_ci */ 1331cb0ef41Sopenharmony_ci get writable() { 1341cb0ef41Sopenharmony_ci if (!isDecompressionStream(this)) 1351cb0ef41Sopenharmony_ci throw new ERR_INVALID_THIS('DecompressionStream'); 1361cb0ef41Sopenharmony_ci return this[kTransform].writable; 1371cb0ef41Sopenharmony_ci } 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ci [kInspect](depth, options) { 1401cb0ef41Sopenharmony_ci if (!isDecompressionStream(this)) 1411cb0ef41Sopenharmony_ci throw new ERR_INVALID_THIS('DecompressionStream'); 1421cb0ef41Sopenharmony_ci customInspect(depth, options, 'DecompressionStream', { 1431cb0ef41Sopenharmony_ci readable: this[kTransform].readable, 1441cb0ef41Sopenharmony_ci writable: this[kTransform].writable, 1451cb0ef41Sopenharmony_ci }); 1461cb0ef41Sopenharmony_ci } 1471cb0ef41Sopenharmony_ci} 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ciObjectDefineProperties(CompressionStream.prototype, { 1501cb0ef41Sopenharmony_ci readable: kEnumerableProperty, 1511cb0ef41Sopenharmony_ci writable: kEnumerableProperty, 1521cb0ef41Sopenharmony_ci}); 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ciObjectDefineProperties(DecompressionStream.prototype, { 1551cb0ef41Sopenharmony_ci readable: kEnumerableProperty, 1561cb0ef41Sopenharmony_ci writable: kEnumerableProperty, 1571cb0ef41Sopenharmony_ci}); 1581cb0ef41Sopenharmony_ci 1591cb0ef41Sopenharmony_cimodule.exports = { 1601cb0ef41Sopenharmony_ci CompressionStream, 1611cb0ef41Sopenharmony_ci DecompressionStream, 1621cb0ef41Sopenharmony_ci}; 163