Skip to content

Commit 7bf7e71

Browse files
committed
feat: ESM migration
1 parent 352dde2 commit 7bf7e71

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+6290
-4045
lines changed

.eslintrc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
"jest": true
88
},
99
"parser": "@typescript-eslint/parser",
10+
"parserOptions": {
11+
"project": "tsconfig.json",
12+
"sourceType": "module"
13+
},
14+
"plugins": [
15+
"import"
16+
],
1017
"extends": [
1118
"eslint:recommended",
1219
"plugin:@typescript-eslint/recommended",
1320
"plugin:prettier/recommended",
1421
"prettier"
1522
],
16-
"plugins": [
17-
"import"
18-
],
19-
"parserOptions": {
20-
"project": "tsconfig.json",
21-
"sourceType": "module"
22-
},
2323
"rules": {
2424
"linebreak-style": ["error", "unix"],
2525
"no-empty": 1,
@@ -39,6 +39,7 @@
3939
"message": "Use `globalThis` instead"
4040
}
4141
],
42+
"prefer-rest-params": 0,
4243
"require-yield": 0,
4344
"eqeqeq": ["error", "smart"],
4445
"spaced-comment": [

.github/workflows/tag.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: "CI / Tag"
1+
npname: "CI / Tag"
22

33
on:
44
push:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ npm install
109109
# build the dist
110110
npm run build
111111
# run the repl (this allows you to import from ./src)
112-
npm run ts-node
112+
npm run tsx
113113
# run the tests
114114
npm run test
115115
# lint the source code

benches/crypto_100KiB.ts

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
import type { EFSWorkerModule } from '@/workers';
2-
import os from 'os';
3-
import path from 'path';
1+
import type { EFSWorker } from '#efsWorker.js';
2+
import os from 'node:os';
3+
import path from 'node:path';
4+
import url from 'node:url';
5+
import { Worker } from 'node:worker_threads';
46
import b from 'benny';
5-
import { spawn, Worker, Transfer } from 'threads';
67
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
78
import { WorkerManager } from '@matrixai/workers';
8-
import * as utils from '@/utils';
9-
import { suiteCommon } from './utils';
9+
import { suiteCommon } from './utils/index.js';
10+
import * as utils from '#utils.js';
11+
import efsWorker from '#efsWorker.js';
1012

1113
const logger = new Logger('crypto100KiB Bench', LogLevel.WARN, [
1214
new StreamHandler(),
1315
]);
16+
const filename = url.fileURLToPath(new URL(import.meta.url));
17+
const workerPath = path.join(filename, '../../dist/efsWorker');
1418

1519
async function main() {
1620
const cores = os.cpus().length;
1721
logger.warn(`Cores: ${cores}`);
18-
const workerManager =
19-
await WorkerManager.createWorkerManager<EFSWorkerModule>({
20-
workerFactory: () => spawn(new Worker('../src/workers/efsWorker')),
21-
cores: 1,
22-
logger,
23-
});
22+
const workerManager = await WorkerManager.createWorkerManager<EFSWorker>({
23+
workerFactory: () => new Worker(workerPath),
24+
manifest: efsWorker,
25+
cores: 1,
26+
logger,
27+
});
2428
const key = utils.generateKeySync(256);
2529
const plain100KiB = utils.getRandomBytesSync(1024 * 100);
2630
const cipher100KiB = await utils.encrypt(key, plain100KiB);
2731
const summary = await b.suite(
28-
path.basename(__filename, path.extname(__filename)),
32+
path.basename(filename, path.extname(filename)),
2933
b.add('encrypt 100 KiB of data', async () => {
3034
await utils.encrypt(key, plain100KiB);
3135
}),
@@ -35,25 +39,19 @@ async function main() {
3539
b.add('encrypt 100 KiB of data with workers', async () => {
3640
const keyAB = utils.toArrayBuffer(key);
3741
const plainTextAB = utils.toArrayBuffer(plain100KiB);
38-
const cipherTextAB = await workerManager.call(async (w) => {
39-
return await w.encrypt(
40-
Transfer(keyAB),
41-
// @ts-ignore: threads.js types are wrong
42-
Transfer(plainTextAB),
43-
);
44-
});
42+
const { data: cipherTextAB } = await workerManager.methods.encrypt(
43+
{ key: keyAB, plainText: plainTextAB },
44+
[keyAB, plainTextAB],
45+
);
4546
utils.fromArrayBuffer(cipherTextAB);
4647
}),
4748
b.add('decrypt 100 KiB of data with workers', async () => {
4849
const keyAB = utils.toArrayBuffer(key);
4950
const cipherTextAB = cipher100KiB.slice(0);
50-
const decrypted = await workerManager.call(async (w) => {
51-
return await w.decrypt(
52-
Transfer(keyAB),
53-
// @ts-ignore: threads.js types are wrong
54-
Transfer(cipherTextAB),
55-
);
56-
});
51+
const { data: decrypted } = await workerManager.methods.decrypt(
52+
{ key: keyAB, cipherText: cipherTextAB },
53+
[keyAB, cipherTextAB],
54+
);
5755
if (decrypted != null) {
5856
utils.fromArrayBuffer(decrypted);
5957
}
@@ -64,7 +62,7 @@ async function main() {
6462
return summary;
6563
}
6664

67-
if (require.main === module) {
65+
if (process.argv[1] === url.fileURLToPath(import.meta.url)) {
6866
void main();
6967
}
7068

benches/crypto_10KiB.ts

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
import type { EFSWorkerModule } from '@/workers';
2-
import os from 'os';
3-
import path from 'path';
1+
import type { EFSWorker } from '#efsWorker.js';
2+
import os from 'node:os';
3+
import path from 'node:path';
4+
import url from 'node:url';
5+
import { Worker } from 'node:worker_threads';
46
import b from 'benny';
5-
import { spawn, Worker, Transfer } from 'threads';
67
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
78
import { WorkerManager } from '@matrixai/workers';
8-
import * as utils from '@/utils';
9-
import { suiteCommon } from './utils';
9+
import { suiteCommon } from './utils/index.js';
10+
import * as utils from '#utils.js';
11+
import efsWorker from '#efsWorker.js';
1012

1113
const logger = new Logger('crypto10KiB Bench', LogLevel.WARN, [
1214
new StreamHandler(),
1315
]);
16+
const filename = url.fileURLToPath(new URL(import.meta.url));
17+
const workerPath = path.join(filename, '../../dist/efsWorker');
1418

1519
async function main() {
1620
const cores = os.cpus().length;
1721
logger.warn(`Cores: ${cores}`);
18-
const workerManager =
19-
await WorkerManager.createWorkerManager<EFSWorkerModule>({
20-
workerFactory: () => spawn(new Worker('../src/workers/efsWorker')),
21-
cores: 1,
22-
logger,
23-
});
22+
const workerManager = await WorkerManager.createWorkerManager<EFSWorker>({
23+
workerFactory: () => new Worker(workerPath),
24+
manifest: efsWorker,
25+
cores: 1,
26+
logger,
27+
});
2428
const key = utils.generateKeySync(256);
2529
const plain10KiB = utils.getRandomBytesSync(1024 * 10);
2630
const cipher10KiB = await utils.encrypt(key, plain10KiB);
2731
const summary = await b.suite(
28-
path.basename(__filename, path.extname(__filename)),
32+
path.basename(filename, path.extname(filename)),
2933
b.add('encrypt 10 KiB of data', async () => {
3034
await utils.encrypt(key, plain10KiB);
3135
}),
@@ -35,25 +39,19 @@ async function main() {
3539
b.add('encrypt 10 KiB of data with workers', async () => {
3640
const keyAB = utils.toArrayBuffer(key);
3741
const plainTextAB = utils.toArrayBuffer(plain10KiB);
38-
const cipherTextAB = await workerManager.call(async (w) => {
39-
return await w.encrypt(
40-
Transfer(keyAB),
41-
// @ts-ignore: threads.js types are wrong
42-
Transfer(plainTextAB),
43-
);
44-
});
42+
const { data: cipherTextAB } = await workerManager.methods.encrypt(
43+
{ key: keyAB, plainText: plainTextAB },
44+
[keyAB, plainTextAB],
45+
);
4546
utils.fromArrayBuffer(cipherTextAB);
4647
}),
4748
b.add('decrypt 10 KiB of data with workers', async () => {
4849
const keyAB = utils.toArrayBuffer(key);
4950
const cipherTextAB = cipher10KiB.slice(0);
50-
const decrypted = await workerManager.call(async (w) => {
51-
return await w.decrypt(
52-
Transfer(keyAB),
53-
// @ts-ignore: threads.js types are wrong
54-
Transfer(cipherTextAB),
55-
);
56-
});
51+
const { data: decrypted } = await workerManager.methods.decrypt(
52+
{ key: keyAB, cipherText: cipherTextAB },
53+
[keyAB, cipherTextAB],
54+
);
5755
if (decrypted != null) {
5856
utils.fromArrayBuffer(decrypted);
5957
}
@@ -64,7 +62,7 @@ async function main() {
6462
return summary;
6563
}
6664

67-
if (require.main === module) {
65+
if (process.argv[1] === url.fileURLToPath(import.meta.url)) {
6866
void main();
6967
}
7068

benches/crypto_16KiB.ts

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
import type { EFSWorkerModule } from '@/workers';
2-
import os from 'os';
3-
import path from 'path';
1+
import type { EFSWorker } from '#efsWorker.js';
2+
import os from 'node:os';
3+
import path from 'node:path';
4+
import url from 'node:url';
5+
import { Worker } from 'node:worker_threads';
46
import b from 'benny';
5-
import { spawn, Worker, Transfer } from 'threads';
67
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
78
import { WorkerManager } from '@matrixai/workers';
8-
import * as utils from '@/utils';
9-
import { suiteCommon } from './utils';
9+
import { suiteCommon } from './utils/index.js';
10+
import * as utils from '#utils.js';
11+
import efsWorker from '#efsWorker.js';
1012

1113
const logger = new Logger('crypto16KiB Bench', LogLevel.WARN, [
1214
new StreamHandler(),
1315
]);
16+
const filename = url.fileURLToPath(new URL(import.meta.url));
17+
const workerPath = path.join(filename, '../../dist/efsWorker');
1418

1519
async function main() {
1620
const cores = os.cpus().length;
1721
logger.warn(`Cores: ${cores}`);
18-
const workerManager =
19-
await WorkerManager.createWorkerManager<EFSWorkerModule>({
20-
workerFactory: () => spawn(new Worker('../src/workers/efsWorker')),
21-
cores: 1,
22-
logger,
23-
});
22+
const workerManager = await WorkerManager.createWorkerManager<EFSWorker>({
23+
workerFactory: () => new Worker(workerPath),
24+
manifest: efsWorker,
25+
cores: 1,
26+
logger,
27+
});
2428
const key = utils.generateKeySync(256);
2529
const plain16KiB = utils.getRandomBytesSync(1024 * 16);
2630
const cipher16KiB = await utils.encrypt(key, plain16KiB);
2731
const summary = await b.suite(
28-
path.basename(__filename, path.extname(__filename)),
32+
path.basename(filename, path.extname(filename)),
2933
b.add('encrypt 16 KiB of data', async () => {
3034
await utils.encrypt(key, plain16KiB);
3135
}),
@@ -35,25 +39,19 @@ async function main() {
3539
b.add('encrypt 16 KiB of data with workers', async () => {
3640
const keyAB = utils.toArrayBuffer(key);
3741
const plainTextAB = utils.toArrayBuffer(plain16KiB);
38-
const cipherTextAB = await workerManager.call(async (w) => {
39-
return await w.encrypt(
40-
Transfer(keyAB),
41-
// @ts-ignore: threads.js types are wrong
42-
Transfer(plainTextAB),
43-
);
44-
});
42+
const { data: cipherTextAB } = await workerManager.methods.encrypt(
43+
{ key: keyAB, plainText: plainTextAB },
44+
[keyAB, plainTextAB],
45+
);
4546
utils.fromArrayBuffer(cipherTextAB);
4647
}),
4748
b.add('decrypt 16 KiB of data with workers', async () => {
4849
const keyAB = utils.toArrayBuffer(key);
4950
const cipherTextAB = cipher16KiB.slice(0);
50-
const decrypted = await workerManager.call(async (w) => {
51-
return await w.decrypt(
52-
Transfer(keyAB),
53-
// @ts-ignore: threads.js types are wrong
54-
Transfer(cipherTextAB),
55-
);
56-
});
51+
const { data: decrypted } = await workerManager.methods.decrypt(
52+
{ key: keyAB, cipherText: cipherTextAB },
53+
[keyAB, cipherTextAB],
54+
);
5755
if (decrypted != null) {
5856
utils.fromArrayBuffer(decrypted);
5957
}
@@ -64,7 +62,7 @@ async function main() {
6462
return summary;
6563
}
6664

67-
if (require.main === module) {
65+
if (process.argv[1] === url.fileURLToPath(import.meta.url)) {
6866
void main();
6967
}
7068

0 commit comments

Comments
 (0)