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

Support modules in web workers. #150

Open
wants to merge 2 commits into
base: main
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
10 changes: 10 additions & 0 deletions common/changes/c2pa/duggaraju-worker_module_2024-03-15-19-00.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "c2pa",
"comment": "Add support for worker thread to use modules",
"type": "patch"
}
],
"packageName": "c2pa"
}
4 changes: 3 additions & 1 deletion packages/c2pa/src/c2pa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ export async function createC2pa(config: C2paConfig): Promise<C2pa> {

const pool = await createPoolWrapper({
scriptSrc: config.workerSrc,
maxWorkers: navigator.hardwareConcurrency || 4,
maxWorkers:
config.poolOptions?.maxWorkers || navigator.hardwareConcurrency || 4,
type: config.poolOptions?.type,
});

const downloader = new Downloader(pool, config.downloaderOptions);
Expand Down
7 changes: 5 additions & 2 deletions packages/c2pa/src/lib/pool/workerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ export interface WorkerManager {
* @param scriptUrl URL to worker script
* @returns {WorkerManager}
*/
export function createWorkerManager(scriptUrl: string): WorkerManager {
const worker = new Worker(scriptUrl);
export function createWorkerManager(
scriptUrl: string,
options?: WorkerOptions,
): WorkerManager {
const worker = new Worker(scriptUrl, options);
let working = false;

const execute: WorkerManager['execute'] = async (request) => {
Expand Down
6 changes: 5 additions & 1 deletion packages/c2pa/src/lib/pool/workerPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { createWorkerManager, WorkerManager } from './workerManager';
export interface WorkerPoolConfig {
scriptSrc: string;
maxWorkers: number;
type?: WorkerType;
}

interface WorkerPool {
Expand Down Expand Up @@ -40,7 +41,10 @@ export function createWorkerPool(config: WorkerPoolConfig): WorkerPool {
if (!worker.isWorking()) return worker;
}
if (workers.length < config.maxWorkers) {
const newWorker = createWorkerManager(config.scriptSrc);
const options: WorkerOptions = {
type: config.type,
};
const newWorker = createWorkerManager(config.scriptSrc, options);
workers.push(newWorker);
return newWorker;
}
Expand Down
20 changes: 9 additions & 11 deletions packages/c2pa/src/lib/poolWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,20 @@ export interface SdkWorkerPool extends Worker {
export async function createPoolWrapper(
config: WorkerPoolConfig,
): Promise<SdkWorkerPool> {
const res = await fetch(config.scriptSrc);
const res = await fetch(config.scriptSrc, {
method: 'HEAD',
});

if (!res.ok) throw new InvalidWorkerSourceError(config.scriptSrc, res);

const src = await res.text();
// @TODO: check subresource integrity
dbg('Fetched worker from %s (%d bytes)', config.scriptSrc, src.length);

const workerBlob = new Blob([src], { type: 'application/javascript' });
const workerUrl = URL.createObjectURL(workerBlob);
dbg(
'Fetched worker from %s (%d bytes)',
config.scriptSrc,
res.headers.get('Content-Length'),
);

const workerPool = createWorkerPool({
...config,
scriptSrc: workerUrl,
});
const workerPool = createWorkerPool(config);

const pool: Worker = {
compileWasm: async (...args) => workerPool.execute('compileWasm', args),
Expand All @@ -48,7 +47,6 @@ export async function createPoolWrapper(
return {
...pool,
dispose: () => {
URL.revokeObjectURL(workerUrl);
return workerPool.terminate();
},
};
Expand Down
Loading