Skip to content

Commit

Permalink
refactor: remove crc32 dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
xseman committed Aug 23, 2024
1 parent 19fb2df commit b8c8bba
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 19 deletions.
31 changes: 15 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"test:watch": "TS_NODE_TRANSPILE_ONLY=true node --test --watch --loader=ts-node/esm --no-warnings src/*.test.ts"
},
"dependencies": {
"crc-32": "~1.2.0",
"lzma1": "0.0.2",
"validator": "^13.12.0"
},
Expand Down
30 changes: 30 additions & 0 deletions src/crc32.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { strict as assert } from "node:assert";
import test, { describe } from "node:test";

import { crc32 } from "./crc32.js";

describe("crc32", () => {
test("empty string", () => {
const result = crc32("");
assert.equal(result, 0);
});

test('"123456789"', () => {
const result = crc32("123456789");
const expected = 0xCBF43926;
assert.equal(result, expected);
});

test('"Hello, World!"', () => {
const result = crc32("Hello, World!");
const expected = 0xEC4AC3D0;
assert.equal(result, expected);
});

test("a long string", () => {
const longString = "a".repeat(1000);
const result = crc32(longString);
const expected = 0x9A38DA03;
assert.equal(result, expected);
});
});
21 changes: 21 additions & 0 deletions src/crc32.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Precomputed CRC32 lookup table
const LOOKUP_TABLE = new Uint32Array(256);

for (let i = 0; i < LOOKUP_TABLE.length; i++) {
let crc = i;
for (let j = 0; j < 8; j++) {
crc = (crc >>> 1) ^ (0xEDB88320 * (crc & 1));
}
LOOKUP_TABLE[i] = crc;
}

export function crc32(data: string): number {
let crc = 0 ^ (-1);
const encoded = new TextEncoder().encode(data);

for (let i = 0; i < encoded.length; i++) {
crc = (crc >>> 8) ^ LOOKUP_TABLE[(crc ^ encoded[i]) & 0xFF];
}

return (crc ^ (-1)) >>> 0;
}
4 changes: 2 additions & 2 deletions src/encode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import crc32 from "crc-32";
import { compress } from "lzma1";

import * as base32hex from "./base32hex.js";
import { crc32 } from "./crc32.js";
import { deburr } from "./deburr.js";
import {
DataModel,
Expand Down Expand Up @@ -88,7 +88,7 @@ export function headerDataLength(length: number): Uint8Array {
*/
export function addChecksum(serialized: string): Uint8Array {
const checksum = new ArrayBuffer(4);
new DataView(checksum).setUint32(0, crc32.str(serialized), true);
new DataView(checksum).setUint32(0, crc32(serialized), true);

const byteArray = new TextEncoder().encode(serialized);

Expand Down

0 comments on commit b8c8bba

Please sign in to comment.