Skip to content

Commit 0f7aed8

Browse files
kevinueharaaduh95
authored andcommittedJan 31, 2025
doc: fix the crc32 documentation
PR-URL: #55898 Fixes: #55800 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent ca6ed21 commit 0f7aed8

File tree

1 file changed

+61
-61
lines changed

1 file changed

+61
-61
lines changed
 

‎doc/api/zlib.md

+61-61
Original file line numberDiff line numberDiff line change
@@ -926,67 +926,6 @@ The `zlib.bytesWritten` property specifies the number of bytes written to
926926
the engine, before the bytes are processed (compressed or decompressed,
927927
as appropriate for the derived class).
928928

929-
### `zlib.crc32(data[, value])`
930-
931-
<!-- YAML
932-
added: v22.2.0
933-
-->
934-
935-
* `data` {string|Buffer|TypedArray|DataView} When `data` is a string,
936-
it will be encoded as UTF-8 before being used for computation.
937-
* `value` {integer} An optional starting value. It must be a 32-bit unsigned
938-
integer. **Default:** `0`
939-
* Returns: {integer} A 32-bit unsigned integer containing the checksum.
940-
941-
Computes a 32-bit [Cyclic Redundancy Check][] checksum of `data`. If
942-
`value` is specified, it is used as the starting value of the checksum,
943-
otherwise, 0 is used as the starting value.
944-
945-
The CRC algorithm is designed to compute checksums and to detect error
946-
in data transmission. It's not suitable for cryptographic authentication.
947-
948-
To be consistent with other APIs, if the `data` is a string, it will
949-
be encoded with UTF-8 before being used for computation. If users only
950-
use Node.js to compute and match the checksums, this works well with
951-
other APIs that uses the UTF-8 encoding by default.
952-
953-
Some third-party JavaScript libraries compute the checksum on a
954-
string based on `str.charCodeAt()` so that it can be run in browsers.
955-
If users want to match the checksum computed with this kind of library
956-
in the browser, it's better to use the same library in Node.js
957-
if it also runs in Node.js. If users have to use `zlib.crc32()` to
958-
match the checksum produced by such a third-party library:
959-
960-
1. If the library accepts `Uint8Array` as input, use `TextEncoder`
961-
in the browser to encode the string into a `Uint8Array` with UTF-8
962-
encoding, and compute the checksum based on the UTF-8 encoded string
963-
in the browser.
964-
2. If the library only takes a string and compute the data based on
965-
`str.charCodeAt()`, on the Node.js side, convert the string into
966-
a buffer using `Buffer.from(str, 'utf16le')`.
967-
968-
```mjs
969-
import zlib from 'node:zlib';
970-
import { Buffer } from 'node:buffer';
971-
972-
let crc = zlib.crc32('hello'); // 907060870
973-
crc = zlib.crc32('world', crc); // 4192936109
974-
975-
crc = zlib.crc32(Buffer.from('hello', 'utf16le')); // 1427272415
976-
crc = zlib.crc32(Buffer.from('world', 'utf16le'), crc); // 4150509955
977-
```
978-
979-
```cjs
980-
const zlib = require('node:zlib');
981-
const { Buffer } = require('node:buffer');
982-
983-
let crc = zlib.crc32('hello'); // 907060870
984-
crc = zlib.crc32('world', crc); // 4192936109
985-
986-
crc = zlib.crc32(Buffer.from('hello', 'utf16le')); // 1427272415
987-
crc = zlib.crc32(Buffer.from('world', 'utf16le'), crc); // 4150509955
988-
```
989-
990929
### `zlib.close([callback])`
991930

992931
<!-- YAML
@@ -1047,6 +986,67 @@ added: v7.0.0
1047986

1048987
Provides an object enumerating Zlib-related constants.
1049988

989+
## `zlib.crc32(data[, value])`
990+
991+
<!-- YAML
992+
added: v22.2.0
993+
-->
994+
995+
* `data` {string|Buffer|TypedArray|DataView} When `data` is a string,
996+
it will be encoded as UTF-8 before being used for computation.
997+
* `value` {integer} An optional starting value. It must be a 32-bit unsigned
998+
integer. **Default:** `0`
999+
* Returns: {integer} A 32-bit unsigned integer containing the checksum.
1000+
1001+
Computes a 32-bit [Cyclic Redundancy Check][] checksum of `data`. If
1002+
`value` is specified, it is used as the starting value of the checksum,
1003+
otherwise, 0 is used as the starting value.
1004+
1005+
The CRC algorithm is designed to compute checksums and to detect error
1006+
in data transmission. It's not suitable for cryptographic authentication.
1007+
1008+
To be consistent with other APIs, if the `data` is a string, it will
1009+
be encoded with UTF-8 before being used for computation. If users only
1010+
use Node.js to compute and match the checksums, this works well with
1011+
other APIs that uses the UTF-8 encoding by default.
1012+
1013+
Some third-party JavaScript libraries compute the checksum on a
1014+
string based on `str.charCodeAt()` so that it can be run in browsers.
1015+
If users want to match the checksum computed with this kind of library
1016+
in the browser, it's better to use the same library in Node.js
1017+
if it also runs in Node.js. If users have to use `zlib.crc32()` to
1018+
match the checksum produced by such a third-party library:
1019+
1020+
1. If the library accepts `Uint8Array` as input, use `TextEncoder`
1021+
in the browser to encode the string into a `Uint8Array` with UTF-8
1022+
encoding, and compute the checksum based on the UTF-8 encoded string
1023+
in the browser.
1024+
2. If the library only takes a string and compute the data based on
1025+
`str.charCodeAt()`, on the Node.js side, convert the string into
1026+
a buffer using `Buffer.from(str, 'utf16le')`.
1027+
1028+
```mjs
1029+
import zlib from 'node:zlib';
1030+
import { Buffer } from 'node:buffer';
1031+
1032+
let crc = zlib.crc32('hello'); // 907060870
1033+
crc = zlib.crc32('world', crc); // 4192936109
1034+
1035+
crc = zlib.crc32(Buffer.from('hello', 'utf16le')); // 1427272415
1036+
crc = zlib.crc32(Buffer.from('world', 'utf16le'), crc); // 4150509955
1037+
```
1038+
1039+
```cjs
1040+
const zlib = require('node:zlib');
1041+
const { Buffer } = require('node:buffer');
1042+
1043+
let crc = zlib.crc32('hello'); // 907060870
1044+
crc = zlib.crc32('world', crc); // 4192936109
1045+
1046+
crc = zlib.crc32(Buffer.from('hello', 'utf16le')); // 1427272415
1047+
crc = zlib.crc32(Buffer.from('world', 'utf16le'), crc); // 4150509955
1048+
```
1049+
10501050
## `zlib.createBrotliCompress([options])`
10511051

10521052
<!-- YAML

0 commit comments

Comments
 (0)
Please sign in to comment.