-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add js module usage to docs / worker example (#36)
- Loading branch information
Showing
8 changed files
with
104 additions
and
92 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
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 |
---|---|---|
|
@@ -24,5 +24,5 @@ npm install | |
Run: | ||
|
||
```bash | ||
node index.js | ||
node index.mjs | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as fs from "fs/promises"; | ||
import * as assert from "assert"; | ||
import * as path from "path"; | ||
import * as os from "os"; | ||
import { Piscina } from "piscina"; | ||
|
||
import { cases as testCases } from "../../dist/test/integration/data.js"; | ||
|
||
const numCpus = os.cpus().length - 1; | ||
|
||
const wasmagicWorkerPool = new Piscina({ | ||
filename: "./worker.mjs", | ||
minThreads: numCpus, | ||
maxThreads: numCpus, | ||
idleTimeout: 10000, | ||
}); | ||
|
||
// Fill Buffers | ||
for (const testCase of testCases) { | ||
const file = await fs.open(path.join("..", "..", testCase[0])); | ||
const stats = await file.stat(); | ||
// Copying directly into a SharedArrayBuffer is faster than copying | ||
// the buffer to the worker. | ||
// If you're loading from a file, you could just pass the file path | ||
// instead of the Buffer, and load the file from the worker thread. | ||
const sharedBuf = Buffer.from(new SharedArrayBuffer(stats.size)); | ||
const { bytesRead } = await file.read({ buffer: sharedBuf }); | ||
await file.close(); | ||
assert.equal(stats.size, bytesRead); | ||
testCase.push(sharedBuf); | ||
} | ||
|
||
// Kick off the workers | ||
for (const testCase of testCases) { | ||
testCase.push(wasmagicWorkerPool.run(testCase[3])); | ||
} | ||
|
||
// Check results | ||
for (const testCase of testCases) { | ||
const result = await testCase[4]; | ||
assert.equal(result, testCase[1]); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { WASMagic } from "../../dist/index.js"; | ||
const magic = await WASMagic.create(); | ||
|
||
export default (buf) => magic.getMime(buf); |