UNPKG

11.4 kBTypeScriptView Raw
1/// <reference types="node" />
2/// <reference types="node" />
3import { PathLike } from 'fs';
4/**
5 * Ported from {@link https://github.com/google/crc32c/blob/21fc8ef30415a635e7351ffa0e5d5367943d4a94/src/crc32c_portable.cc#L16-L59 github.com/google/crc32c}
6 */
7declare const CRC32C_EXTENSIONS: readonly [0, 4067132163, 3778769143, 324072436, 3348797215, 904991772, 648144872, 3570033899, 2329499855, 2024987596, 1809983544, 2575936315, 1296289744, 3207089363, 2893594407, 1578318884, 274646895, 3795141740, 4049975192, 51262619, 3619967088, 632279923, 922689671, 3298075524, 2592579488, 1760304291, 2075979607, 2312596564, 1562183871, 2943781820, 3156637768, 1313733451, 549293790, 3537243613, 3246849577, 871202090, 3878099393, 357341890, 102525238, 4101499445, 2858735121, 1477399826, 1264559846, 3107202533, 1845379342, 2677391885, 2361733625, 2125378298, 820201905, 3263744690, 3520608582, 598981189, 4151959214, 85089709, 373468761, 3827903834, 3124367742, 1213305469, 1526817161, 2842354314, 2107672161, 2412447074, 2627466902, 1861252501, 1098587580, 3004210879, 2688576843, 1378610760, 2262928035, 1955203488, 1742404180, 2511436119, 3416409459, 969524848, 714683780, 3639785095, 205050476, 4266873199, 3976438427, 526918040, 1361435347, 2739821008, 2954799652, 1114974503, 2529119692, 1691668175, 2005155131, 2247081528, 3690758684, 697762079, 986182379, 3366744552, 476452099, 3993867776, 4250756596, 255256311, 1640403810, 2477592673, 2164122517, 1922457750, 2791048317, 1412925310, 1197962378, 3037525897, 3944729517, 427051182, 170179418, 4165941337, 746937522, 3740196785, 3451792453, 1070968646, 1905808397, 2213795598, 2426610938, 1657317369, 3053634322, 1147748369, 1463399397, 2773627110, 4215344322, 153784257, 444234805, 3893493558, 1021025245, 3467647198, 3722505002, 797665321, 2197175160, 1889384571, 1674398607, 2443626636, 1164749927, 3070701412, 2757221520, 1446797203, 137323447, 4198817972, 3910406976, 461344835, 3484808360, 1037989803, 781091935, 3705997148, 2460548119, 1623424788, 1939049696, 2180517859, 1429367560, 2807687179, 3020495871, 1180866812, 410100952, 3927582683, 4182430767, 186734380, 3756733383, 763408580, 1053836080, 3434856499, 2722870694, 1344288421, 1131464017, 2971354706, 1708204729, 2545590714, 2229949006, 1988219213, 680717673, 3673779818, 3383336350, 1002577565, 4010310262, 493091189, 238226049, 4233660802, 2987750089, 1082061258, 1395524158, 2705686845, 1972364758, 2279892693, 2494862625, 1725896226, 952904198, 3399985413, 3656866545, 731699698, 4283874585, 222117402, 510512622, 3959836397, 3280807620, 837199303, 582374963, 3504198960, 68661723, 4135334616, 3844915500, 390545967, 1230274059, 3141532936, 2825850620, 1510247935, 2395924756, 2091215383, 1878366691, 2644384480, 3553878443, 565732008, 854102364, 3229815391, 340358836, 3861050807, 4117890627, 119113024, 1493875044, 2875275879, 3090270611, 1247431312, 2660249211, 1828433272, 2141937292, 2378227087, 3811616794, 291187481, 34330861, 4032846830, 615137029, 3603020806, 3314634738, 939183345, 1776939221, 2609017814, 2295496738, 2058945313, 2926798794, 1545135305, 1330124605, 3173225534, 4084100981, 17165430, 307568514, 3762199681, 888469610, 3332340585, 3587147933, 665062302, 2042050490, 2346497209, 2559330125, 1793573966, 3190661285, 1279665062, 1595330642, 2910671697];
8declare const CRC32C_EXTENSION_TABLE: Int32Array;
9/** An interface for CRC32C hashing and validation */
10interface CRC32CValidator {
11 /**
12 * A method returning the CRC32C as a base64-encoded string.
13 *
14 * @example
15 * Hashing the string 'data' should return 'rth90Q=='
16 *
17 * ```js
18 * const buffer = Buffer.from('data');
19 * crc32c.update(buffer);
20 * crc32c.toString(); // 'rth90Q=='
21 * ```
22 **/
23 toString: () => string;
24 /**
25 * A method validating a base64-encoded CRC32C string.
26 *
27 * @example
28 * Should return `true` if the value matches, `false` otherwise
29 *
30 * ```js
31 * const buffer = Buffer.from('data');
32 * crc32c.update(buffer);
33 * crc32c.validate('DkjKuA=='); // false
34 * crc32c.validate('rth90Q=='); // true
35 * ```
36 */
37 validate: (value: string) => boolean;
38 /**
39 * A method for passing `Buffer`s for CRC32C generation.
40 *
41 * @example
42 * Hashing buffers from 'some ' and 'text\n'
43 *
44 * ```js
45 * const buffer1 = Buffer.from('some ');
46 * crc32c.update(buffer1);
47 *
48 * const buffer2 = Buffer.from('text\n');
49 * crc32c.update(buffer2);
50 *
51 * crc32c.toString(); // 'DkjKuA=='
52 * ```
53 */
54 update: (data: Buffer) => void;
55}
56/** A function that generates a CRC32C Validator */
57interface CRC32CValidatorGenerator {
58 /** Should return a new, ready-to-use `CRC32CValidator` */
59 (): CRC32CValidator;
60}
61declare const CRC32C_DEFAULT_VALIDATOR_GENERATOR: CRC32CValidatorGenerator;
62declare const CRC32C_EXCEPTION_MESSAGES: {
63 readonly INVALID_INIT_BASE64_RANGE: (l: number) => string;
64 readonly INVALID_INIT_BUFFER_LENGTH: (l: number) => string;
65 readonly INVALID_INIT_INTEGER: (l: number) => string;
66};
67declare class CRC32C implements CRC32CValidator {
68 #private;
69 /**
70 * Constructs a new `CRC32C` object.
71 *
72 * Reconstruction is recommended via the `CRC32C.from` static method.
73 *
74 * @param initialValue An initial CRC32C value - a signed 32-bit integer.
75 */
76 constructor(initialValue?: number);
77 /**
78 * Calculates a CRC32C from a provided buffer.
79 *
80 * Implementation inspired from:
81 * - {@link https://github.com/google/crc32c/blob/21fc8ef30415a635e7351ffa0e5d5367943d4a94/src/crc32c_portable.cc github.com/google/crc32c}
82 * - {@link https://github.com/googleapis/python-crc32c/blob/a595e758c08df445a99c3bf132ee8e80a3ec4308/src/google_crc32c/python.py github.com/googleapis/python-crc32c}
83 * - {@link https://github.com/googleapis/java-storage/pull/1376/files github.com/googleapis/java-storage}
84 *
85 * @param data The `Buffer` to generate the CRC32C from
86 */
87 update(data: Buffer): void;
88 /**
89 * Validates a provided input to the current CRC32C value.
90 *
91 * @param input A Buffer, `CRC32C`-compatible object, base64-encoded data (string), or signed 32-bit integer
92 */
93 validate(input: Buffer | CRC32CValidator | string | number): boolean;
94 /**
95 * Returns a `Buffer` representation of the CRC32C value
96 */
97 toBuffer(): Buffer;
98 /**
99 * Returns a JSON-compatible, base64-encoded representation of the CRC32C value.
100 *
101 * See {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify `JSON#stringify`}
102 */
103 toJSON(): string;
104 /**
105 * Returns a base64-encoded representation of the CRC32C value.
106 *
107 * See {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString `Object#toString`}
108 */
109 toString(): string;
110 /**
111 * Returns the `number` representation of the CRC32C value as a signed 32-bit integer
112 *
113 * See {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf `Object#valueOf`}
114 */
115 valueOf(): number;
116 static readonly CRC32C_EXTENSIONS: readonly [0, 4067132163, 3778769143, 324072436, 3348797215, 904991772, 648144872, 3570033899, 2329499855, 2024987596, 1809983544, 2575936315, 1296289744, 3207089363, 2893594407, 1578318884, 274646895, 3795141740, 4049975192, 51262619, 3619967088, 632279923, 922689671, 3298075524, 2592579488, 1760304291, 2075979607, 2312596564, 1562183871, 2943781820, 3156637768, 1313733451, 549293790, 3537243613, 3246849577, 871202090, 3878099393, 357341890, 102525238, 4101499445, 2858735121, 1477399826, 1264559846, 3107202533, 1845379342, 2677391885, 2361733625, 2125378298, 820201905, 3263744690, 3520608582, 598981189, 4151959214, 85089709, 373468761, 3827903834, 3124367742, 1213305469, 1526817161, 2842354314, 2107672161, 2412447074, 2627466902, 1861252501, 1098587580, 3004210879, 2688576843, 1378610760, 2262928035, 1955203488, 1742404180, 2511436119, 3416409459, 969524848, 714683780, 3639785095, 205050476, 4266873199, 3976438427, 526918040, 1361435347, 2739821008, 2954799652, 1114974503, 2529119692, 1691668175, 2005155131, 2247081528, 3690758684, 697762079, 986182379, 3366744552, 476452099, 3993867776, 4250756596, 255256311, 1640403810, 2477592673, 2164122517, 1922457750, 2791048317, 1412925310, 1197962378, 3037525897, 3944729517, 427051182, 170179418, 4165941337, 746937522, 3740196785, 3451792453, 1070968646, 1905808397, 2213795598, 2426610938, 1657317369, 3053634322, 1147748369, 1463399397, 2773627110, 4215344322, 153784257, 444234805, 3893493558, 1021025245, 3467647198, 3722505002, 797665321, 2197175160, 1889384571, 1674398607, 2443626636, 1164749927, 3070701412, 2757221520, 1446797203, 137323447, 4198817972, 3910406976, 461344835, 3484808360, 1037989803, 781091935, 3705997148, 2460548119, 1623424788, 1939049696, 2180517859, 1429367560, 2807687179, 3020495871, 1180866812, 410100952, 3927582683, 4182430767, 186734380, 3756733383, 763408580, 1053836080, 3434856499, 2722870694, 1344288421, 1131464017, 2971354706, 1708204729, 2545590714, 2229949006, 1988219213, 680717673, 3673779818, 3383336350, 1002577565, 4010310262, 493091189, 238226049, 4233660802, 2987750089, 1082061258, 1395524158, 2705686845, 1972364758, 2279892693, 2494862625, 1725896226, 952904198, 3399985413, 3656866545, 731699698, 4283874585, 222117402, 510512622, 3959836397, 3280807620, 837199303, 582374963, 3504198960, 68661723, 4135334616, 3844915500, 390545967, 1230274059, 3141532936, 2825850620, 1510247935, 2395924756, 2091215383, 1878366691, 2644384480, 3553878443, 565732008, 854102364, 3229815391, 340358836, 3861050807, 4117890627, 119113024, 1493875044, 2875275879, 3090270611, 1247431312, 2660249211, 1828433272, 2141937292, 2378227087, 3811616794, 291187481, 34330861, 4032846830, 615137029, 3603020806, 3314634738, 939183345, 1776939221, 2609017814, 2295496738, 2058945313, 2926798794, 1545135305, 1330124605, 3173225534, 4084100981, 17165430, 307568514, 3762199681, 888469610, 3332340585, 3587147933, 665062302, 2042050490, 2346497209, 2559330125, 1793573966, 3190661285, 1279665062, 1595330642, 2910671697];
117 static readonly CRC32C_EXTENSION_TABLE: Int32Array;
118 /**
119 * Generates a `CRC32C` from a compatible buffer format.
120 *
121 * @param value 4-byte `ArrayBufferView`/`Buffer`/`TypedArray`
122 */
123 private static fromBuffer;
124 static fromFile(file: PathLike): Promise<CRC32C>;
125 /**
126 * Generates a `CRC32C` from 4-byte base64-encoded data (string).
127 *
128 * @param value 4-byte base64-encoded data (string)
129 */
130 private static fromString;
131 /**
132 * Generates a `CRC32C` from a safe, unsigned 32-bit integer.
133 *
134 * @param value an unsigned 32-bit integer
135 */
136 private static fromNumber;
137 /**
138 * Generates a `CRC32C` from a variety of compatable types.
139 * Note: strings are treated as input, not as file paths to read from.
140 *
141 * @param value A number, 4-byte `ArrayBufferView`/`Buffer`/`TypedArray`, or 4-byte base64-encoded data (string)
142 */
143 static from(value: ArrayBuffer | ArrayBufferView | CRC32CValidator | string | number): CRC32C;
144}
145export { CRC32C, CRC32C_DEFAULT_VALIDATOR_GENERATOR, CRC32C_EXCEPTION_MESSAGES, CRC32C_EXTENSIONS, CRC32C_EXTENSION_TABLE, CRC32CValidator, CRC32CValidatorGenerator, };