Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate to ESM #12

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
"jest": true
},
"parser": "@typescript-eslint/parser",
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
"prettier"
],
"plugins": [
"import"
],
"parserOptions": {
"project": "tsconfig.json",
"sourceType": "module"
},
"plugins": [
"import"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"rules": {
"linebreak-style": ["error", "unix"],
"no-empty": 1,
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ npm install
# build the dist
npm run build
# run the repl (this allows you to import from ./src)
npm run ts-node
npm run tsx
# run the tests
npm run test
# lint the source code
Expand Down
29 changes: 20 additions & 9 deletions benches/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
#!/usr/bin/env ts-node

import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';
import si from 'systeminformation';
import workerManager from './worker_manager';
import { benchesPath } from './utils/utils.js';
import workerManager from './worker_manager.js';

async function main(): Promise<void> {
await fs.promises.mkdir(path.join(__dirname, 'results'), { recursive: true });
await fs.promises.mkdir(path.join(benchesPath, 'results'), {
recursive: true,
});
await workerManager();
const resultFilenames = await fs.promises.readdir(
path.join(__dirname, 'results'),
path.join(benchesPath, 'results'),
);
const metricsFile = await fs.promises.open(
path.join(__dirname, 'results', 'metrics.txt'),
path.join(benchesPath, 'results', 'metrics.txt'),
'w',
);
let concatenating = false;
for (const resultFilename of resultFilenames) {
if (/.+_metrics\.txt$/.test(resultFilename)) {
const metricsData = await fs.promises.readFile(
path.join(__dirname, 'results', resultFilename),
path.join(benchesPath, 'results', resultFilename),
);
if (concatenating) {
await metricsFile.write('\n');
Expand All @@ -35,9 +39,16 @@ async function main(): Promise<void> {
system: 'model, manufacturer',
});
await fs.promises.writeFile(
path.join(__dirname, 'results', 'system.json'),
path.join(benchesPath, 'results', 'system.json'),
JSON.stringify(systemData, null, 2),
);
}

void main();
if (import.meta.url.startsWith('file:')) {
const modulePath = url.fileURLToPath(import.meta.url);
if (process.argv[1] === modulePath) {
void main();
}
}

export default main;
2 changes: 1 addition & 1 deletion benches/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './utils';
export * from './utils.js';
21 changes: 13 additions & 8 deletions benches/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';
import b from 'benny';
import { codeBlock } from 'common-tags';
import packageJson from '../../package.json';
import packageJson from '../../package.json' assert { type: 'json' };

const benchesPath = path.dirname(
path.dirname(url.fileURLToPath(import.meta.url)),
);

const suiteCommon = [
b.cycle(),
b.complete(),
b.save({
file: (summary) => summary.name,
folder: path.join(__dirname, '../results'),
folder: path.join(benchesPath, 'results'),
version: packageJson.version,
details: true,
}),
b.save({
file: (summary) => summary.name,
folder: path.join(__dirname, '../results'),
folder: path.join(benchesPath, 'results'),
version: packageJson.version,
format: 'chart.html',
}),
b.complete((summary) => {
const filePath = path.join(
__dirname,
'../results',
benchesPath,
'results',
summary.name + '_metrics.txt',
);
fs.writeFileSync(
Expand Down Expand Up @@ -58,4 +63,4 @@ const suiteCommon = [
}),
];

export { suiteCommon };
export { benchesPath, suiteCommon };
22 changes: 14 additions & 8 deletions benches/worker_manager.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { WorkerModule } from '@/worker';
import path from 'path';
import crypto from 'crypto';
import type { WorkerModule } from '#worker.js';
import path from 'node:path';
import url from 'node:url';
import crypto from 'node:crypto';
import b from 'benny';
import { spawn, Worker, Transfer } from 'threads';
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import WorkerManager from '@/WorkerManager';
import { suiteCommon } from './utils';
import { suiteCommon } from './utils/index.js';
import WorkerManager from '#WorkerManager.js';

const filePath = url.fileURLToPath(import.meta.url);

const logger = new Logger('WorkerManager Bench', LogLevel.WARN, [
new StreamHandler(),
Expand All @@ -22,7 +25,7 @@ async function main() {
// 1 KiB of data is still too small
const bytes = crypto.randomBytes(1024 * 1024);
const summary = await b.suite(
path.basename(__filename, path.extname(__filename)),
path.basename(filePath, path.extname(filePath)),
b.add('call overhead', async () => {
// This calls a noop, this will show the overhead costs
// All parallelised operation can never be faster than this
Expand Down Expand Up @@ -104,8 +107,11 @@ async function main() {
return summary;
}

if (require.main === module) {
void main();
if (import.meta.url.startsWith('file:')) {
const modulePath = url.fileURLToPath(import.meta.url);
if (process.argv[1] === modulePath) {
void main();
}
}

export default main;
27 changes: 15 additions & 12 deletions jest.config.js → jest.config.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
const path = require('path');
const { pathsToModuleNameMapper } = require('ts-jest');
const { compilerOptions } = require('./tsconfig');
import path from 'node:path';
import url from 'node:url';
import tsconfigJSON from './tsconfig.json' assert { type: "json" };

const moduleNameMapper = pathsToModuleNameMapper(compilerOptions.paths, {
prefix: '<rootDir>/src/',
});
const projectPath = path.dirname(url.fileURLToPath(import.meta.url));

// Global variables that are shared across the jest worker pool
// These variables must be static and serializable
const globals = {
// Absolute directory to the project root
projectDir: __dirname,
projectDir: projectPath,
// Absolute directory to the test root
testDir: path.join(__dirname, 'tests'),
testDir: path.join(projectPath, 'tests'),
// Default asynchronous test timeout
defaultTimeout: 20000,
// Timeouts rely on setTimeout which takes 32 bit numbers
Expand All @@ -24,7 +22,7 @@ const globals = {
// They can however receive the process environment
// Use `process.env` to set variables

module.exports = {
const config = {
testEnvironment: 'node',
verbose: true,
collectCoverage: false,
Expand All @@ -40,10 +38,10 @@ module.exports = {
parser: {
syntax: "typescript",
tsx: true,
decorators: compilerOptions.experimentalDecorators,
decorators: tsconfigJSON.compilerOptions.experimentalDecorators,
dynamicImport: true,
},
target: compilerOptions.target.toLowerCase(),
target: tsconfigJSON.compilerOptions.target.toLowerCase(),
keepClassNames: true,
},
}
Expand Down Expand Up @@ -77,5 +75,10 @@ module.exports = {
'jest-extended/all',
'<rootDir>/tests/setupAfterEnv.ts'
],
moduleNameMapper: moduleNameMapper,
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
extensionsToTreatAsEsm: ['.ts', '.tsx', '.mts'],
};

export default config;
Loading