Skip to content

Commit

Permalink
Merge pull request #128 from GordonSmith/NODEJS_WORKER
Browse files Browse the repository at this point in the history
chore:  Add specific NodeJS worker test
  • Loading branch information
GordonSmith authored Nov 6, 2022
2 parents f02277f + 0cac180 commit 1bcbbf8
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 39 deletions.
1 change: 1 addition & 0 deletions cpp/base91/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ TARGET_LINK_LIBRARIES(base91lib
)

INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/base91lib.wasm DESTINATION dist COMPONENT runtime)
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/base91lib.wasm DESTINATION dist-test COMPONENT runtime)

IF (CMAKE_BUILD_TYPE STREQUAL "Debug")
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/base91lib.wasm.map DESTINATION dist COMPONENT runtime)
Expand Down
1 change: 1 addition & 0 deletions cpp/expat/expatlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ TARGET_LINK_LIBRARIES(expatlib
)

INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/expatlib.wasm DESTINATION dist COMPONENT runtime)
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/expatlib.wasm DESTINATION dist-test COMPONENT runtime)

IF (CMAKE_BUILD_TYPE STREQUAL "Debug")
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/expatlib.wasm.map DESTINATION dist COMPONENT runtime)
Expand Down
1 change: 1 addition & 0 deletions cpp/graphviz/graphvizlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ TARGET_LINK_LIBRARIES(graphvizlib PRIVATE
# )

INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/graphvizlib.wasm DESTINATION dist COMPONENT runtime)
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/graphvizlib.wasm DESTINATION dist-test COMPONENT runtime)

IF (CMAKE_BUILD_TYPE STREQUAL "Debug")
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/graphvizlib.wasm.map DESTINATION dist COMPONENT runtime)
Expand Down
1 change: 1 addition & 0 deletions cpp/zstd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ TARGET_LINK_LIBRARIES(zstdlib
)

INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/zstdlib.wasm DESTINATION dist COMPONENT runtime)
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/zstdlib.wasm DESTINATION dist-test COMPONENT runtime)

IF (CMAKE_BUILD_TYPE STREQUAL "Debug")
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/zstdlib.wasm.map DESTINATION dist COMPONENT runtime)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,4 @@
"url": "https://github.com/hpcc-systems/hpcc-js-wasm/issues"
},
"homepage": "https://github.com/hpcc-systems/hpcc-js-wasm#readme"
}
}
7 changes: 4 additions & 3 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const nodeTpl = (input, cjsOutput, esOutput) => ({
include: ["lib-es6/__tests__/*.js"],
delimiters: ['', ''],
values: {
"../index": "../index-node"
"\"../index\"": "\"../index-node\""
}
}),
nodeResolve({
Expand Down Expand Up @@ -99,8 +99,9 @@ export default [
browserTpl("lib-es6/extract", "dist/extract", "dist/extract.es6"),

browserTpl("lib-es6/__tests__/index", "dist-test/index", "dist-test/index.es6"),
nodeTpl("lib-es6/__tests__/index", "dist-test/index.node", "dist-test/index.node.es6"),
browserTpl("lib-es6/__tests__/worker", "dist-test/worker", "dist-test/worker.es6"),
nodeTpl("lib-es6/__tests__/index-node", "dist-test/index.node", "dist-test/index.node.es6"),
browserTpl("lib-es6/__tests__/worker-browser", "dist-test/worker", "dist-test/worker.es6"),
nodeTpl("lib-es6/__tests__/worker-node", "dist-test/worker.node", "dist-test/worker.node.es6"),

binTpl("lib-es6/__bin__/dot-wasm", "bin/dot-wasm.mjs"),
binTpl("lib-es6/__bin__/sfx-wasm", "bin/sfx-wasm.mjs"),
Expand Down
28 changes: 0 additions & 28 deletions src/__tests__/base91.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,4 @@ describe("base91", function () {
const data2 = zstd.decompress(compressed_data2);
expect(data).to.deep.equal(data2);
});

if (globalThis.window?.Worker) {
it("worker", async function () {
const data = new Uint8Array(Array.from({ length: 1000 }, (_, i) => i % 256));

const value = await new Promise(resolve => {
const myWorker = new Worker("dist-test/worker.js");
myWorker.postMessage(data);
myWorker.onmessage = function (e) {
resolve(e.data);
};
});
expect(value).to.deep.equal(data);
});

it("worker-es6", async function () {
const data = new Uint8Array(Array.from({ length: 1000 }, (_, i) => i % 256));

const value = await new Promise(resolve => {
const myWorker = new Worker("dist-test/worker.es6.js");
myWorker.postMessage(data);
myWorker.onmessage = function (e) {
resolve(e.data);
};
});
expect(value).to.deep.equal(data);
});
}
});
29 changes: 29 additions & 0 deletions src/__tests__/browser-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect } from "chai";

describe("worker-browser", function () {
it("worker-umd", async function () {
const data = new Uint8Array(Array.from({ length: 1000 }, (_, i) => i % 256));

const value = await new Promise(resolve => {
const myWorker = new Worker("dist-test/worker.js");
myWorker.postMessage(data);
myWorker.onmessage = function (e) {
resolve(e.data);
};
});
expect(value).to.deep.equal(data);
});

it("worker-esm", async function () {
const data = new Uint8Array(Array.from({ length: 1000 }, (_, i) => i % 256));

const value = await new Promise(resolve => {
const myWorker = new Worker("dist-test/worker.es6.js");
myWorker.postMessage(data);
myWorker.onmessage = function (e) {
resolve(e.data);
};
});
expect(value).to.deep.equal(data);
});
});
7 changes: 7 additions & 0 deletions src/__tests__/index-common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { wasmFolder } from "../index";
wasmFolder("dist");

export * from "./base91";
export * from "./expat";
export * from "./graphviz";
export * from "./zstd";
2 changes: 2 additions & 0 deletions src/__tests__/index-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./index-common";
export * from "./node-tests";
9 changes: 2 additions & 7 deletions src/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
import { wasmFolder } from "../index";
wasmFolder("dist");

export * from "./base91";
export * from "./expat";
export * from "./graphviz";
export * from "./zstd";
export * from "./index-common";
export * from "./browser-tests";
30 changes: 30 additions & 0 deletions src/__tests__/node-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect } from "chai";
import { Worker } from "node:worker_threads";

describe("worker-node", function () {
it("worker-cjs", async function () {
const data = new Uint8Array(Array.from({ length: 1000 }, (_, i) => i % 256));

const value = await new Promise(resolve => {
const myWorker = new Worker("./dist-test/worker.node.js");
myWorker.postMessage(data);
myWorker.on("message", function (data) {
resolve(data);
});
});
expect(value).to.deep.equal(data);
});

it("worker-esm", async function () {
const data = new Uint8Array(Array.from({ length: 1000 }, (_, i) => i % 256));

const value = await new Promise(resolve => {
const myWorker = new Worker("./dist-test/worker.node.es6.mjs");
myWorker.postMessage(data);
myWorker.on("message", function (data) {
resolve(data);
});
});
expect(value).to.deep.equal(data);
});
});
File renamed without changes.
11 changes: 11 additions & 0 deletions src/__tests__/worker-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { parentPort } from "node:worker_threads";
import { Base91 } from "../index-node";

parentPort?.on("message", async function (data) {
const base91 = await Base91.load();
const base91Str = base91.encode(data);
const data2 = base91.decode(base91Str);
parentPort?.postMessage(data2);
process.exit(0);
});

0 comments on commit 1bcbbf8

Please sign in to comment.