-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98f2af7
commit d7afae3
Showing
6 changed files
with
225 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* @license LGPL-2.1-or-later | ||
* | ||
* vite-plugin-deno | ||
* | ||
* Copyright (C) 2024 Hans Schallmoser | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | ||
* USA or see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import type { AstResult, AstTask } from "./ast-worker.ts"; | ||
import { assert } from "@std/assert/assert"; | ||
|
||
export class WorkerPool { | ||
constructor(readonly concurrency: number) { | ||
assert(concurrency > 0); | ||
assert(concurrency % 1 === 0); // int | ||
for (let i = 0; i < concurrency; i++) { | ||
this.#pool.push(this.#spawn_worker()); | ||
} | ||
} | ||
#spawn_worker() { | ||
const worker = new Worker(new URL("./ast-worker.ts", import.meta.url), { | ||
type: "module", | ||
}); | ||
worker.addEventListener("message", (ev) => { | ||
const result = ev.data as AstResult; | ||
const listener = this.#listener.get(result.task_id); | ||
listener?.(result); | ||
this.#listener.delete(result.task_id); | ||
|
||
const waiting = this.#waiting.shift(); | ||
if (waiting) { | ||
waiting(worker); | ||
} else { | ||
this.#free_pool.push(worker); | ||
} | ||
}); | ||
this.#free_pool.push(worker); | ||
return worker; | ||
} | ||
#pool: Worker[] = []; | ||
#free_pool: Worker[] = []; | ||
#waiting: ((worker: Worker) => void)[] = []; | ||
#listener = new Map<number, (res: AstResult) => void>(); | ||
#run_task(task: AstTask, worker: Worker): Promise<AstResult> { | ||
return new Promise((resolve) => { | ||
this.#listener.set(task.task_id, resolve); | ||
worker.postMessage(task); | ||
}); | ||
} | ||
public run(task: AstTask): Promise<AstResult> { | ||
const worker = this.#free_pool.shift(); | ||
if (worker) { | ||
return this.#run_task(task, worker); | ||
} else { | ||
return new Promise((resolve) => { | ||
this.#waiting.push((worker) => { | ||
this.#run_task(task, worker).then(resolve); | ||
}); | ||
}); | ||
} | ||
} | ||
#task_id = 0; | ||
public get_task_id(): number { | ||
return this.#task_id++; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* @license LGPL-2.1-or-later | ||
* | ||
* vite-plugin-deno | ||
* | ||
* Copyright (C) 2024 Hans Schallmoser | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | ||
* USA or see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { transform } from "lebab"; | ||
import { parse } from "acorn"; | ||
import { simple as walk_simple } from "acorn-walk"; | ||
|
||
export interface AstTask { | ||
task_id: number; | ||
kind: "cjs-to-esm" | "default-exports"; | ||
id: string; | ||
raw_code: string; | ||
} | ||
|
||
export interface AstResult { | ||
task_id: number; | ||
code?: string; | ||
has_default_export?: boolean; | ||
} | ||
|
||
function run_task(task: AstTask): Omit<AstResult, "task_id"> { | ||
if (task.kind === "cjs-to-esm") { | ||
const { code, warnings } = transform( | ||
task.raw_code, | ||
["commonjs"], | ||
); | ||
for (const $ of warnings) { | ||
console.log($); | ||
} | ||
|
||
return { | ||
code, | ||
}; | ||
} else { | ||
const ast = parse(task.raw_code, { | ||
ecmaVersion: 2023, | ||
sourceType: "module", | ||
}); | ||
|
||
let has_default_export = false; | ||
|
||
walk_simple(ast, { | ||
ExportDefaultDeclaration(_node) { | ||
has_default_export = true; | ||
}, | ||
ExportNamedDeclaration(node) { | ||
if (node.specifiers) { | ||
for (const specifier of node.specifiers) { | ||
// @ts-ignore missing typedef | ||
if (specifier.type === "ExportSpecifier" && specifier.exported?.name === "default") { | ||
has_default_export = true; | ||
} | ||
} | ||
} | ||
}, | ||
}); | ||
|
||
return { | ||
has_default_export, | ||
}; | ||
} | ||
} | ||
|
||
self.onmessage = (e) => { | ||
const task = e.data as AstTask; | ||
|
||
const res: AstResult = { | ||
task_id: task.task_id, | ||
...run_task(task), | ||
}; | ||
|
||
self.postMessage(res); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters