Skip to content

Commit 1fc7e7d

Browse files
committed
feat: ESM migration
1 parent afe8cc8 commit 1fc7e7d

File tree

492 files changed

+6546
-5687
lines changed

Some content is hidden

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

492 files changed

+6546
-5687
lines changed

.eslintignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

.eslintrc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
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
],
15-
"plugins": [
16-
"import"
17-
],
18-
"parserOptions": {
19-
"project": "tsconfig.json",
20-
"sourceType": "module"
21-
},
2222
"rules": {
2323
"linebreak-style": ["error", "unix"],
2424
"no-empty": 1,

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ npm install
5858
# build the dist
5959
npm run build
6060
# run the repl (this allows you to import from ./src)
61-
npm run ts-node
61+
npm run tsx
6262
# run the tests
6363
npm run test
6464
# lint the source code

benches/suites/basic/buffer_encoding_decoding.ts renamed to benches/basic_buffer_encoding_decoding.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import url from 'node:url';
2+
import path from 'node:path';
13
import b from 'benny';
2-
import { summaryName, suiteCommon } from '../../utils';
4+
import { suiteCommon } from './utils/utils.js';
5+
6+
const filename = url.fileURLToPath(new URL(import.meta.url));
37

48
async function main() {
59
const buf = Buffer.allocUnsafe(64);
610
const summary = await b.suite(
7-
summaryName(__filename),
11+
path.basename(filename, path.extname(filename)),
812
b.add('JSON stringify and parse buffer', () => {
913
const bufJSON = JSON.stringify(buf);
1014
Buffer.from(JSON.parse(bufJSON));
@@ -22,8 +26,11 @@ async function main() {
2226
return summary;
2327
}
2428

25-
if (require.main === module) {
26-
void main();
29+
if (import.meta.url.startsWith('file:')) {
30+
const modulePath = url.fileURLToPath(import.meta.url);
31+
if (process.argv[1] === modulePath) {
32+
void main();
33+
}
2734
}
2835

2936
export default main;
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
import url from 'node:url';
2+
import path from 'node:path';
13
import b from 'benny';
2-
import { summaryName, suiteCommon } from '../../utils';
4+
import { suiteCommon } from './utils/utils.js';
5+
6+
const filename = url.fileURLToPath(new URL(import.meta.url));
37

48
async function main() {
59
let map = new Map();
610
let obj = {};
711
let arr: any = [];
812
let set = new Set();
913
const summary = await b.suite(
10-
summaryName(__filename),
14+
path.basename(filename, path.extname(filename)),
1115
b.add('map', async () => {
1216
map = new Map();
1317
return async () => {
@@ -83,8 +87,11 @@ async function main() {
8387
return summary;
8488
}
8589

86-
if (require.main === module) {
87-
void main();
90+
if (import.meta.url.startsWith('file:')) {
91+
const modulePath = url.fileURLToPath(import.meta.url);
92+
if (process.argv[1] === modulePath) {
93+
void main();
94+
}
8895
}
8996

9097
export default main;

benches/index.ts

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,76 @@
11
#!/usr/bin/env ts-node
22

3-
import type { Summary } from 'benny/lib/internal/common-types';
4-
import fs from 'fs';
5-
import path from 'path';
3+
import fs from 'node:fs';
4+
import path from 'node:path';
5+
import url from 'node:url';
66
import si from 'systeminformation';
7-
import { fsWalk, resultsPath, suitesPath } from './utils';
7+
import { benchesPath } from './utils/utils.js';
8+
import basicBufferEncodingDecoding from './basic_buffer_encoding_decoding.js';
9+
import gitGarbageCollection from './git_garbage_collection.js';
10+
import keysAsymmetricCrypto from './keys_asymmetric_crypto.js';
11+
import keysKeyGeneration from './keys_key_generation.js';
12+
import keysKeyringLifecycle from './keys_keyring_lifecycle.js';
13+
import keysPasswordHashing from './keys_password_hashing.js';
14+
import keysRandomBytes from './keys_random_bytes.js';
15+
import keysRecoveryCode from './keys_recovery_code.js';
16+
import keysSymmetricCrypto from './keys_symmetric_crypto.js';
17+
import keysX509 from './keys_x509.js';
18+
import workersKeys from './workers_keys.js';
19+
import workersOverhead from './workers_overhead.js';
820

921
async function main(): Promise<void> {
10-
await fs.promises.mkdir(path.join(__dirname, 'results'), { recursive: true });
11-
// Running all suites
12-
for await (const suitePath of fsWalk(suitesPath)) {
13-
// Skip over non-ts and non-js files
14-
const ext = path.extname(suitePath);
15-
if (ext !== '.ts' && ext !== '.js') {
16-
continue;
17-
}
18-
const suite: () => Promise<Summary> = (await import(suitePath)).default;
19-
// Skip default exports that are not functions and are not called "main"
20-
// They might be utility files
21-
if (typeof suite === 'function' && suite.name === 'main') {
22-
await suite();
23-
}
24-
}
25-
// Concatenating metrics
26-
const metricsPath = path.join(resultsPath, 'metrics.txt');
27-
await fs.promises.rm(metricsPath, { force: true });
22+
await fs.promises.mkdir(path.join(benchesPath, 'results'), {
23+
recursive: true,
24+
});
25+
await basicBufferEncodingDecoding();
26+
await gitGarbageCollection();
27+
await keysAsymmetricCrypto();
28+
await keysKeyGeneration();
29+
await keysKeyringLifecycle();
30+
await keysPasswordHashing();
31+
await keysRandomBytes();
32+
await keysRecoveryCode();
33+
await keysSymmetricCrypto();
34+
await keysX509();
35+
await workersKeys();
36+
await workersOverhead();
37+
const resultFilenames = await fs.promises.readdir(
38+
path.join(benchesPath, 'results'),
39+
);
40+
const metricsFile = await fs.promises.open(
41+
path.join(benchesPath, 'results', 'metrics.txt'),
42+
'w',
43+
);
2844
let concatenating = false;
29-
for await (const metricPath of fsWalk(resultsPath)) {
30-
// Skip over non-metrics files
31-
if (!metricPath.endsWith('_metrics.txt')) {
32-
continue;
45+
for (const resultFilename of resultFilenames) {
46+
if (/.+_metrics\.txt$/.test(resultFilename)) {
47+
const metricsData = await fs.promises.readFile(
48+
path.join(benchesPath, 'results', resultFilename),
49+
);
50+
if (concatenating) {
51+
await metricsFile.write('\n');
52+
}
53+
await metricsFile.write(metricsData);
54+
concatenating = true;
3355
}
34-
const metricData = await fs.promises.readFile(metricPath);
35-
if (concatenating) {
36-
await fs.promises.appendFile(metricsPath, '\n');
37-
}
38-
await fs.promises.appendFile(metricsPath, metricData);
39-
concatenating = true;
4056
}
57+
await metricsFile.close();
4158
const systemData = await si.get({
4259
cpu: '*',
4360
osInfo: 'platform, distro, release, kernel, arch',
4461
system: 'model, manufacturer',
4562
});
4663
await fs.promises.writeFile(
47-
path.join(__dirname, 'results', 'system.json'),
64+
path.join(benchesPath, 'results', 'system.json'),
4865
JSON.stringify(systemData, null, 2),
4966
);
5067
}
5168

52-
if (require.main === module) {
53-
void main();
69+
if (import.meta.url.startsWith('file:')) {
70+
const modulePath = url.fileURLToPath(import.meta.url);
71+
if (process.argv[1] === modulePath) {
72+
void main();
73+
}
5474
}
5575

5676
export default main;
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import url from 'node:url';
2+
import path from 'node:path';
13
import b from 'benny';
2-
import * as random from '@/keys/utils/random';
3-
import * as generate from '@/keys/utils/generate';
4-
import * as asymmetric from '@/keys/utils/asymmetric';
5-
import { summaryName, suiteCommon } from '../../utils';
4+
import { suiteCommon } from './utils/utils.js';
5+
import * as random from '#keys/utils/random.js';
6+
import * as generate from '#keys/utils/generate.js';
7+
import * as asymmetric from '#keys/utils/asymmetric.js';
8+
9+
const filename = url.fileURLToPath(new URL(import.meta.url));
610

711
async function main() {
812
const keyPair = generate.generateKeyPair();
@@ -34,7 +38,7 @@ async function main() {
3438
plain10KiB,
3539
);
3640
const summary = await b.suite(
37-
summaryName(__filename),
41+
path.basename(filename, path.extname(filename)),
3842
b.add('encrypt 512 B of data', () => {
3943
asymmetric.encryptWithPublicKey(keyPair.publicKey, plain512B);
4044
}),
@@ -88,8 +92,11 @@ async function main() {
8892
return summary;
8993
}
9094

91-
if (require.main === module) {
92-
void main();
95+
if (import.meta.url.startsWith('file:')) {
96+
const modulePath = url.fileURLToPath(import.meta.url);
97+
if (process.argv[1] === modulePath) {
98+
void main();
99+
}
93100
}
94101

95102
export default main;
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
import url from 'node:url';
2+
import path from 'node:path';
13
import b from 'benny';
2-
import * as generate from '@/keys/utils/generate';
3-
import * as recoveryCode from '@/keys/utils/recoveryCode';
4-
import { summaryName, suiteCommon } from '../../utils';
4+
import { suiteCommon } from './utils/utils.js';
5+
import * as generate from '#keys/utils/generate.js';
6+
import * as recoveryCode from '#keys/utils/recoveryCode.js';
7+
8+
const filename = url.fileURLToPath(new URL(import.meta.url));
59

610
async function main() {
711
const code = recoveryCode.generateRecoveryCode(24);
812
const summary = await b.suite(
9-
summaryName(__filename),
13+
path.basename(filename, path.extname(filename)),
1014
b.add('generate root asymmetric keypair', () => {
1115
generate.generateKeyPair();
1216
}),
@@ -21,8 +25,11 @@ async function main() {
2125
return summary;
2226
}
2327

24-
if (require.main === module) {
25-
void main();
28+
if (import.meta.url.startsWith('file:')) {
29+
const modulePath = url.fileURLToPath(import.meta.url);
30+
if (process.argv[1] === modulePath) {
31+
void main();
32+
}
2633
}
2734

2835
export default main;
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import fs from 'fs';
2-
import os from 'os';
3-
import path from 'path';
1+
import fs from 'node:fs';
2+
import os from 'node:os';
3+
import url from 'node:url';
4+
import path from 'node:path';
45
import b from 'benny';
56
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
6-
import KeyRing from '@/keys/KeyRing';
7-
import { summaryName, suiteCommon } from '../../utils';
7+
import { suiteCommon } from './utils/utils.js';
8+
import KeyRing from '#keys/KeyRing.js';
9+
10+
const filename = url.fileURLToPath(new URL(import.meta.url));
811

912
async function main() {
1013
const summary = await b.suite(
11-
summaryName(__filename),
14+
path.basename(filename, path.extname(filename)),
1215
b.add('KeyRing fresh creation', async () => {
1316
const dataDir = await fs.promises.mkdtemp(
1417
path.join(os.tmpdir(), 'polykey-bench-'),
@@ -52,8 +55,11 @@ async function main() {
5255
return summary;
5356
}
5457

55-
if (require.main === module) {
56-
void main();
58+
if (import.meta.url.startsWith('file:')) {
59+
const modulePath = url.fileURLToPath(import.meta.url);
60+
if (process.argv[1] === modulePath) {
61+
void main();
62+
}
5763
}
5864

5965
export default main;
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import url from 'node:url';
2+
import path from 'node:path';
13
import b from 'benny';
2-
import * as password from '@/keys/utils/password';
3-
import { summaryName, suiteCommon } from '../../utils';
4+
import { suiteCommon } from './utils/utils.js';
5+
import * as password from '#keys/utils/password.js';
6+
7+
const filename = url.fileURLToPath(new URL(import.meta.url));
48

59
async function main() {
610
const summary = await b.suite(
7-
summaryName(__filename),
11+
path.basename(filename, path.extname(filename)),
812
b.add('password hashing - min', () => {
913
password.hashPassword(
1014
'password',
@@ -42,8 +46,11 @@ async function main() {
4246
return summary;
4347
}
4448

45-
if (require.main === module) {
46-
void main();
49+
if (import.meta.url.startsWith('file:')) {
50+
const modulePath = url.fileURLToPath(import.meta.url);
51+
if (process.argv[1] === modulePath) {
52+
void main();
53+
}
4754
}
4855

4956
export default main;

0 commit comments

Comments
 (0)