-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from tsxper/v2-improvements
V2 improvements
- Loading branch information
Showing
16 changed files
with
877 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
dist | ||
e2e | ||
.github | ||
**/*.js | ||
__tests__ | ||
*.spec.ts |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"root": true, | ||
"parser": "@typescript-eslint/parser", | ||
"plugins": [ | ||
"@typescript-eslint" | ||
], | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/eslint-recommended", | ||
"plugin:@typescript-eslint/recommended" | ||
], | ||
"rules": { | ||
"no-console": 2 | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
node_modules | ||
/dist | ||
.DS_STORE | ||
.vscode | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<p>Calculating crc32 for text "hello crc32": <span id="placeholder"></span></p> | ||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"@tsxper/crc32": "https://www.unpkg.com/@tsxper/crc32@1.0.5/esm/CRC32.js" | ||
} | ||
} | ||
</script> | ||
<script type="module"> | ||
import { CRC32 } from "@tsxper/crc32"; | ||
const crc32 = new CRC32(); | ||
const crc = crc32.calculate("hello crc32"); | ||
document.getElementById('placeholder').innerText = crc; | ||
</script> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/sh | ||
|
||
if [[ "$OSTYPE" == "darwin"* ]]; then | ||
find ./dist/esm -name "*.js" -exec sed -i '' -E "s#export (.*) from '\.(.*)';#export \1 from '.\2\.js';#g" {} +; | ||
find ./dist/esm -name "*.js" -exec sed -i '' -E "s#import (.*) from '\.(.*)';#import \1 from '.\2\.js';#g" {} +; | ||
else | ||
find ./dist/esm -name "*.js" -exec sed -i -E "s#export (.*) from '\.(.*)';#export \1 from '.\2\.js';#g" {} +; | ||
find ./dist/esm -name "*.js" -exec sed -i -E "s#import (.*) from '\.(.*)';#import \1 from '.\2\.js';#g" {} +; | ||
fi |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import fs from 'fs'; | ||
import stream from 'stream'; | ||
import { CRC32 } from './CRC32'; | ||
|
||
export class CRC32Streams extends CRC32 { | ||
public async forFile(filePath: string): Promise<number> { | ||
const readableStream = fs.createReadStream(filePath); | ||
const sum = await this.forReadableStream(readableStream); | ||
return sum; | ||
} | ||
|
||
public async forReadableStream(readableStream: stream.Readable): Promise<number> { | ||
const p: Promise<number> = new Promise((resolve, reject) => { | ||
let sum: number; | ||
readableStream.on('error', (err: Error) => { | ||
reject(err); | ||
}); | ||
readableStream.on('data', (chunk: Buffer) => { | ||
sum = this.forBytes(chunk, sum); | ||
}); | ||
readableStream.on('close', () => { | ||
resolve(sum); | ||
}); | ||
}); | ||
const checksum = await p; | ||
return checksum; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { CRC32 } from '../CRC32'; | ||
|
||
describe('Test CRC32', () => { | ||
const checksums: [string, number][] = [ | ||
['', 0], | ||
['getting-started', 3434040051], | ||
['npm test', 3233492827], | ||
['Running from command line', 1207362252], | ||
['actions/checkout@v4', 1785145639], | ||
['hello crc32', 2560021400], | ||
]; | ||
|
||
it.each(checksums)('Calculate crc32 for "%s"', (input: string, expected: number) => { | ||
const crc32 = new CRC32(); | ||
expect(crc32.calculate(input)).toBe(expected); | ||
}); | ||
|
||
it.each(checksums)('Calculate crc32 with forBytes("%s")', (input: string, expected: number) => { | ||
const crc32 = new CRC32(); | ||
const bytes = new TextEncoder().encode(input); | ||
expect(crc32.forBytes(bytes)).toBe(expected); | ||
}); | ||
|
||
it('Calculate crc32 for chunks', () => { | ||
const crc32 = new CRC32(); | ||
const chunks = [new TextEncoder().encode('text1 text2')]; | ||
let crc = 0; | ||
for (const chunk of chunks) { | ||
crc = crc32.forBytes(chunk, crc); | ||
} | ||
expect(crc32.forString('text1 text2')).toBe(crc); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import path from 'path'; | ||
import { CRC32Streams } from '../CRC32Streams'; | ||
|
||
describe('Test CRC32Streams', () => { | ||
it('Calculate crc32 of a file', async () => { | ||
const expected = 3908598516; | ||
const file = path.join(__dirname, 'file.txt'); | ||
const crc32 = new CRC32Streams(); | ||
const checksum = await crc32.forFile(file); | ||
expect(checksum).toBe(expected); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Test file with some text. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { CRC32 } from './CRC32'; | ||
export { CRC32Streams } from './CRC32Streams'; |
Oops, something went wrong.