Skip to content
File

Blob: src/node/internal/internal_zlib_constants.ts

typescript58 lines
1// Copyright (c) 2026 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5import { default as zlibUtil } from 'node-internal:zlib';
6 
7const {
8 CONST_Z_OK,
9 CONST_Z_STREAM_END,
10 CONST_Z_NEED_DICT,
11 CONST_Z_ERRNO,
12 CONST_Z_STREAM_ERROR,
13 CONST_Z_DATA_ERROR,
14 CONST_Z_MEM_ERROR,
15 CONST_Z_BUF_ERROR,
16 CONST_Z_VERSION_ERROR,
17} = zlibUtil;
18 
19const constPrefix = 'CONST_';
20export const constants: Record<string, number> = {};
21 
22Object.defineProperties(
23 constants,
24 Object.fromEntries(
25 // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
26 Object.entries(Object.getPrototypeOf(zlibUtil))
27 .filter(([k]) => k.startsWith(constPrefix))
28 .map(([k, v]) => [
29 k.slice(constPrefix.length),
30 {
31 value: v,
32 writable: false,
33 configurable: false,
34 enumerable: true,
35 },
36 ])
37 )
38);
39 
40// Translation table for return codes.
41const rawCodes: Record<string, number | string> = {
42 Z_OK: CONST_Z_OK,
43 Z_STREAM_END: CONST_Z_STREAM_END,
44 Z_NEED_DICT: CONST_Z_NEED_DICT,
45 Z_ERRNO: CONST_Z_ERRNO,
46 Z_STREAM_ERROR: CONST_Z_STREAM_ERROR,
47 Z_DATA_ERROR: CONST_Z_DATA_ERROR,
48 Z_MEM_ERROR: CONST_Z_MEM_ERROR,
49 Z_BUF_ERROR: CONST_Z_BUF_ERROR,
50 Z_VERSION_ERROR: CONST_Z_VERSION_ERROR,
51};
52 
53for (const key of Object.keys(rawCodes)) {
54 rawCodes[rawCodes[key] as number] = key;
55}
56 
57export const codes = Object.freeze(rawCodes);