Skip to content
Merged
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
114 changes: 0 additions & 114 deletions packages/rstack/THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,63 +273,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

## isoconcurrency

This package includes bundled code from
[isoconcurrency](https://github.com/fabiospampinato/isoconcurrency).

License: MIT

The MIT License (MIT)

Copyright (c) 2025-present Fabio Spampinato

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

## isotimer

This package includes bundled code from [isotimer](https://github.com/fabiospampinato/isotimer).

License: MIT

The MIT License (MIT)

Copyright (c) 2025-present Fabio Spampinato

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

## micromatch

This package includes bundled code from [micromatch](https://github.com/micromatch/micromatch).
Expand Down Expand Up @@ -577,60 +520,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

## webworker-shim

This package includes bundled code from
[webworker-shim](https://github.com/fabiospampinato/webworker-shim).

License: MIT

The MIT License (MIT)

Copyright (c) 2022-present Fabio Spampinato

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

## worktank

This package includes bundled code from [worktank](https://github.com/fabiospampinato/worktank).

License: MIT

The MIT License (MIT)

Copyright (c) 2021-present Fabio Spampinato

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
4 changes: 2 additions & 2 deletions packages/rstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@rslint/core": "catalog:",
"@rstest/core": "catalog:",
"prettier": "catalog:",
"tinypool": "catalog:",
"yuku-parser": "catalog:"
},
"devDependencies": {
Expand All @@ -78,8 +79,7 @@
"rslog": "catalog:",
"sort-package-json": "catalog:",
"tiny-readdir": "catalog:",
"typescript": "catalog:",
"worktank": "catalog:"
"typescript": "catalog:"
},
"peerDependencies": {
"@rspress/core": "^2.0.17"
Expand Down
2 changes: 1 addition & 1 deletion packages/rstack/src/fmt/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const runFmtFilesInWorkerPool = async (
);
return results.filter((result): result is FmtFileResult => result !== undefined);
} finally {
workerPool.terminate();
await workerPool.terminate();
}
};

Expand Down
13 changes: 9 additions & 4 deletions packages/rstack/src/fmt/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import type { FmtFileRequest } from './types.ts';
type PrettierPlugins = NonNullable<PrettierOptions['plugins']>;
type FormatFileResult = 'changed' | 'unchanged' | 'unsupported';

interface FormatFileTask {
file: FmtFileRequest;
shouldWrite: boolean;
}

const fileInfoOptions = {
ignorePath: [],
resolveConfig: false,
Expand All @@ -37,10 +42,10 @@ const resolveFmtParser = async (
* Use synchronous direct I/O inside the dedicated worker to avoid libuv
* scheduling overhead. This prioritizes throughput over crash-safe replacement.
*/
const formatFile = async (
{ path, options }: FmtFileRequest,
shouldWrite: boolean,
): Promise<FormatFileResult> => {
const formatFile = async ({
file: { path, options },
shouldWrite,
}: FormatFileTask): Promise<FormatFileResult> => {
const plugins = await getPrettierPlugins(options, path);
const parser = await resolveFmtParser(path, options, plugins);
if (!parser) {
Expand Down
35 changes: 18 additions & 17 deletions packages/rstack/src/fmt/workerPool.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Derived from @prettier/cli, see THIRD_PARTY_NOTICES.md

import { availableParallelism } from 'node:os';
import WorkTank from 'worktank';
import Tinypool from 'tinypool';
import type { FmtFileRequest } from './types.ts';

type FmtWorkerMethods = typeof import('./worker.ts');

interface FmtWorkerPool {
formatFile: FmtWorkerMethods['formatFile'];
terminate: () => void;
formatFile: (
file: FmtFileRequest,
shouldWrite: boolean,
) => ReturnType<FmtWorkerMethods['formatFile']>;
terminate: () => Promise<void>;
}

const getFmtWorkerCount = (fileCount: number, maxWorkers?: number): number =>
Expand All @@ -27,30 +31,27 @@ const createFmtWorkerPool = async (
maxWorkers?: number,
): Promise<FmtWorkerPool> => {
const workerCount = getFmtWorkerCount(fileCount, maxWorkers);
const pool = new WorkTank<FmtWorkerMethods>({
pool: {
name: 'rstack-fmt',
size: workerCount,
},
worker: {
autoInstantiate: true,
methods: getFmtWorkerUrl(),
},
const pool = new Tinypool({
filename: getFmtWorkerUrl().href,
name: 'initializeFmtWorker',
minThreads: workerCount,
maxThreads: workerCount,
});

try {
// Concurrent handshakes make WorkTank assign one task to every worker.
await Promise.all(
Array.from({ length: workerCount }, () => pool.exec('initializeFmtWorker', [])),
Array.from({ length: workerCount }, () =>
pool.run(undefined, { name: 'initializeFmtWorker' }),
),
);
} catch (error) {
pool.terminate();
await pool.destroy();
throw error;
}

return {
formatFile: (file, shouldWrite) => pool.exec('formatFile', [file, shouldWrite]),
terminate: pool.terminate,
formatFile: (file, shouldWrite) => pool.run({ file, shouldWrite }, { name: 'formatFile' }),
terminate: () => pool.destroy(),
};
};

Expand Down
1 change: 1 addition & 0 deletions packages/rstack/tests/fmt/runnerWriteFailure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rs.mock('../../src/fmt/workerPool.ts', () => ({
formatFile: () => Promise.reject(new Error('file write failed')),
terminate: () => {
mocks.terminateCalls++;
return Promise.resolve();
},
}),
}));
Expand Down
24 changes: 12 additions & 12 deletions packages/rstack/tests/fmt/worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ test('writes formatted files', async () => {
const filePath = writeProjectFile(rootPath, 'example.ts', 'const value=1');

await expect(
formatFile(
{
formatFile({
file: {
path: filePath,
options: {
parser: 'typescript',
},
},
true,
),
shouldWrite: true,
}),
).resolves.toBe('changed');

expect(readFileSync(filePath, 'utf8')).toBe('const value = 1;\n');
Expand All @@ -30,13 +30,13 @@ test('infers the parser for an explicitly provided node_modules file', async ()
const filePath = writeProjectFile(rootPath, 'node_modules/example/index.ts', source);

await expect(
formatFile(
{
formatFile({
file: {
path: filePath,
options: {},
},
false,
),
shouldWrite: false,
}),
).resolves.toBe('changed');

expect(readFileSync(filePath, 'utf8')).toBe(source);
Expand All @@ -48,13 +48,13 @@ test('skips unsupported files before reading them', async () => {
const filePath = path.join(rootPath, 'missing.unknown');

await expect(
formatFile(
{
formatFile({
file: {
path: filePath,
options: {},
},
true,
),
shouldWrite: true,
}),
).resolves.toBe('unsupported');
});
});
Loading