Last active
July 11, 2024 13:43
-
-
Save segfault-bilibili/73086c88a6e67fba1f5329db4f835786 to your computer and use it in GitHub Desktop.
Calculate CRC-32 without zlib.crc32() binding in NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static crc32(data: fsPromises.FileHandle | Uint8Array): Promise<number> { | |
return new Promise((resolve, reject) => { | |
let inStream: stream.Readable; | |
try { | |
if (data instanceof Uint8Array) { | |
inStream = new stream.PassThrough().end(data); | |
} else { | |
inStream = data.createReadStream({ start: 0, autoClose: false, highWaterMark: this.chunkSize }); | |
} | |
} catch (e) { | |
reject(e); | |
return; | |
} | |
let gzip = zlib.createGzip({ level: 0, chunkSize: this.chunkSize }); | |
let lastChunk: Buffer; | |
let outStream = new stream.Writable({ | |
write: (chunk, encoding, callback) => { lastChunk = chunk; callback(); }, | |
writev: (chunks, callback) => { lastChunk = chunks[chunks.length - 1].chunk; callback(); }, | |
highWaterMark: this.chunkSize, | |
}); | |
outStream.on('close', () => { | |
let dv = new DataView(lastChunk.buffer, lastChunk.byteOffset, lastChunk.byteLength); | |
let crc32 = dv.getUint32(dv.byteLength - 8, true); | |
resolve(crc32); | |
}); | |
stream.pipeline(inStream, gzip, outStream, (err) => { reject(err); }); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment