2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-04-01 15:09:59 -04:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2019-09-02 17:07:11 -04:00
|
|
|
import * as dispatch from "./dispatch.ts";
|
|
|
|
import { sendAsync, sendSync } from "./dispatch_json.ts";
|
2020-01-21 03:49:47 -05:00
|
|
|
import { log } from "./util.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
import { TextDecoder, TextEncoder } from "./text_encoding.ts";
|
2020-01-21 03:49:47 -05:00
|
|
|
/*
|
2019-09-02 17:07:11 -04:00
|
|
|
import { blobURLMap } from "./url.ts";
|
|
|
|
import { blobBytesWeakMap } from "./blob.ts";
|
2020-01-21 03:49:47 -05:00
|
|
|
*/
|
2020-01-20 09:30:30 -05:00
|
|
|
import { Event } from "./event.ts";
|
2020-01-17 18:43:53 -05:00
|
|
|
import { EventTarget } from "./event_target.ts";
|
2019-01-08 14:44:06 -05:00
|
|
|
|
2019-04-01 15:09:59 -04:00
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
2020-01-21 03:49:47 -05:00
|
|
|
function encodeMessage(data: any): Uint8Array {
|
2019-04-01 15:09:59 -04:00
|
|
|
const dataJson = JSON.stringify(data);
|
|
|
|
return encoder.encode(dataJson);
|
|
|
|
}
|
|
|
|
|
2020-01-21 03:49:47 -05:00
|
|
|
function decodeMessage(dataIntArray: Uint8Array): any {
|
2019-04-01 15:09:59 -04:00
|
|
|
const dataJson = decoder.decode(dataIntArray);
|
|
|
|
return JSON.parse(dataJson);
|
|
|
|
}
|
|
|
|
|
2019-08-05 07:23:41 -04:00
|
|
|
function createWorker(
|
|
|
|
specifier: string,
|
2019-08-06 09:22:11 -04:00
|
|
|
hasSourceCode: boolean,
|
2020-01-29 12:54:23 -05:00
|
|
|
sourceCode: Uint8Array,
|
|
|
|
name?: string
|
2020-01-17 18:43:53 -05:00
|
|
|
): { id: number; loaded: boolean } {
|
2019-08-26 08:50:21 -04:00
|
|
|
return sendSync(dispatch.OP_CREATE_WORKER, {
|
|
|
|
specifier,
|
2019-08-06 09:22:11 -04:00
|
|
|
hasSourceCode,
|
2020-01-29 12:54:23 -05:00
|
|
|
sourceCode: new TextDecoder().decode(sourceCode),
|
|
|
|
name
|
2019-08-26 08:50:21 -04:00
|
|
|
});
|
2019-04-01 15:09:59 -04:00
|
|
|
}
|
|
|
|
|
2020-01-17 18:43:53 -05:00
|
|
|
async function hostGetWorkerLoaded(id: number): Promise<any> {
|
|
|
|
return await sendAsync(dispatch.OP_HOST_GET_WORKER_LOADED, { id });
|
|
|
|
}
|
|
|
|
|
|
|
|
async function hostPollWorker(id: number): Promise<any> {
|
|
|
|
return await sendAsync(dispatch.OP_HOST_POLL_WORKER, { id });
|
|
|
|
}
|
|
|
|
|
|
|
|
function hostCloseWorker(id: number): void {
|
|
|
|
sendSync(dispatch.OP_HOST_CLOSE_WORKER, { id });
|
|
|
|
}
|
|
|
|
|
|
|
|
function hostResumeWorker(id: number): void {
|
|
|
|
sendSync(dispatch.OP_HOST_RESUME_WORKER, { id });
|
2019-04-01 15:09:59 -04:00
|
|
|
}
|
|
|
|
|
2019-11-09 15:07:14 -05:00
|
|
|
function hostPostMessage(id: number, data: any): void {
|
2019-04-01 15:09:59 -04:00
|
|
|
const dataIntArray = encodeMessage(data);
|
2019-11-09 15:07:14 -05:00
|
|
|
sendSync(dispatch.OP_HOST_POST_MESSAGE, { id }, dataIntArray);
|
2019-04-01 15:09:59 -04:00
|
|
|
}
|
|
|
|
|
2019-11-09 15:07:14 -05:00
|
|
|
async function hostGetMessage(id: number): Promise<any> {
|
|
|
|
const res = await sendAsync(dispatch.OP_HOST_GET_MESSAGE, { id });
|
2019-08-26 08:50:21 -04:00
|
|
|
|
|
|
|
if (res.data != null) {
|
|
|
|
return decodeMessage(new Uint8Array(res.data));
|
2019-04-01 15:09:59 -04:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Worker {
|
2020-01-17 18:43:53 -05:00
|
|
|
onerror?: (e: any) => void;
|
2019-04-01 15:09:59 -04:00
|
|
|
onmessage?: (e: { data: any }) => void;
|
|
|
|
onmessageerror?: () => void;
|
|
|
|
postMessage(data: any): void;
|
2020-01-29 12:54:23 -05:00
|
|
|
terminate(): void;
|
2019-04-01 15:09:59 -04:00
|
|
|
}
|
|
|
|
|
2020-01-21 03:49:47 -05:00
|
|
|
export interface WorkerOptions {
|
|
|
|
type?: "classic" | "module";
|
2020-01-29 12:54:23 -05:00
|
|
|
name?: string;
|
2019-08-05 07:23:41 -04:00
|
|
|
}
|
|
|
|
|
2020-01-17 18:43:53 -05:00
|
|
|
export class WorkerImpl extends EventTarget implements Worker {
|
2019-11-09 15:07:14 -05:00
|
|
|
private readonly id: number;
|
2019-09-07 12:27:18 -04:00
|
|
|
private isClosing = false;
|
2020-01-17 18:43:53 -05:00
|
|
|
private messageBuffer: any[] = [];
|
|
|
|
private ready = false;
|
|
|
|
public onerror?: (e: any) => void;
|
2019-04-01 15:09:59 -04:00
|
|
|
public onmessage?: (data: any) => void;
|
|
|
|
public onmessageerror?: () => void;
|
|
|
|
|
2020-01-21 03:49:47 -05:00
|
|
|
constructor(specifier: string, options?: WorkerOptions) {
|
2020-01-17 18:43:53 -05:00
|
|
|
super();
|
2019-08-06 09:22:11 -04:00
|
|
|
|
2020-01-21 03:49:47 -05:00
|
|
|
let type = "classic";
|
|
|
|
|
|
|
|
if (options?.type) {
|
|
|
|
type = options.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type !== "module") {
|
|
|
|
throw new Error(
|
|
|
|
'Not yet implemented: only "module" type workers are supported'
|
|
|
|
);
|
2019-08-05 07:23:41 -04:00
|
|
|
}
|
2020-01-21 03:49:47 -05:00
|
|
|
|
|
|
|
const hasSourceCode = false;
|
|
|
|
const sourceCode = new Uint8Array();
|
|
|
|
|
|
|
|
/* TODO(bartlomieju):
|
2019-08-06 09:22:11 -04:00
|
|
|
// Handle blob URL.
|
|
|
|
if (specifier.startsWith("blob:")) {
|
|
|
|
hasSourceCode = true;
|
|
|
|
const b = blobURLMap.get(specifier);
|
|
|
|
if (!b) {
|
|
|
|
throw new Error("No Blob associated with the given URL is found");
|
|
|
|
}
|
|
|
|
const blobBytes = blobBytesWeakMap.get(b!);
|
|
|
|
if (!blobBytes) {
|
|
|
|
throw new Error("Invalid Blob");
|
|
|
|
}
|
|
|
|
sourceCode = blobBytes!;
|
|
|
|
}
|
2020-01-21 03:49:47 -05:00
|
|
|
*/
|
2019-08-06 09:22:11 -04:00
|
|
|
|
2020-01-29 12:54:23 -05:00
|
|
|
const { id, loaded } = createWorker(
|
|
|
|
specifier,
|
|
|
|
hasSourceCode,
|
|
|
|
sourceCode,
|
|
|
|
options?.name
|
|
|
|
);
|
2020-01-17 18:43:53 -05:00
|
|
|
this.id = id;
|
|
|
|
this.ready = loaded;
|
|
|
|
this.poll();
|
2019-04-01 15:09:59 -04:00
|
|
|
}
|
|
|
|
|
2020-01-17 18:43:53 -05:00
|
|
|
private handleError(e: any): boolean {
|
2020-01-20 09:30:30 -05:00
|
|
|
// TODO: this is being handled in a type unsafe way, it should be type safe
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const event = new Event("error", { cancelable: true }) as any;
|
2020-01-17 18:43:53 -05:00
|
|
|
event.message = e.message;
|
|
|
|
event.lineNumber = e.lineNumber ? e.lineNumber + 1 : null;
|
|
|
|
event.columnNumber = e.columnNumber ? e.columnNumber + 1 : null;
|
|
|
|
event.fileName = e.fileName;
|
|
|
|
event.error = null;
|
|
|
|
|
|
|
|
let handled = false;
|
|
|
|
if (this.onerror) {
|
|
|
|
this.onerror(event);
|
|
|
|
if (event.defaultPrevented) {
|
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
}
|
|
|
|
|
|
|
|
async poll(): Promise<void> {
|
|
|
|
// If worker has not been immediately executed
|
|
|
|
// then let's await it's readiness
|
|
|
|
if (!this.ready) {
|
|
|
|
const result = await hostGetWorkerLoaded(this.id);
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
if (!this.handleError(result.error)) {
|
|
|
|
throw new Error(result.error.message);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// drain messages
|
|
|
|
for (const data of this.messageBuffer) {
|
|
|
|
hostPostMessage(this.id, data);
|
|
|
|
}
|
|
|
|
this.messageBuffer = [];
|
|
|
|
this.ready = true;
|
|
|
|
this.run();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const result = await hostPollWorker(this.id);
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
if (!this.handleError(result.error)) {
|
|
|
|
throw Error(result.error.message);
|
|
|
|
} else {
|
|
|
|
hostResumeWorker(this.id);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.isClosing = true;
|
|
|
|
hostCloseWorker(this.id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 15:09:59 -04:00
|
|
|
postMessage(data: any): void {
|
2020-01-17 18:43:53 -05:00
|
|
|
if (!this.ready) {
|
|
|
|
this.messageBuffer.push(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-09 15:07:14 -05:00
|
|
|
hostPostMessage(this.id, data);
|
2019-04-01 15:09:59 -04:00
|
|
|
}
|
|
|
|
|
2020-01-29 12:54:23 -05:00
|
|
|
terminate(): void {
|
|
|
|
throw new Error("Not yet implemented");
|
|
|
|
}
|
|
|
|
|
2019-04-01 15:09:59 -04:00
|
|
|
private async run(): Promise<void> {
|
|
|
|
while (!this.isClosing) {
|
2019-11-09 15:07:14 -05:00
|
|
|
const data = await hostGetMessage(this.id);
|
2019-04-01 15:09:59 -04:00
|
|
|
if (data == null) {
|
|
|
|
log("worker got null message. quitting.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (this.onmessage) {
|
|
|
|
const event = { data };
|
|
|
|
this.onmessage(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|