summaryrefslogtreecommitdiff
path: root/cli/js/workers.ts
blob: 7e8219e19f4eae327fa7c8df3c8f94cde7c127c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as dispatch from "./dispatch.ts";
import { sendAsync, sendSync } from "./dispatch_json.ts";
import { log, createResolvable, Resolvable } from "./util.ts";
import { TextDecoder, TextEncoder } from "./text_encoding.ts";
import { blobURLMap } from "./url.ts";
import { blobBytesWeakMap } from "./blob.ts";
import { Event } from "./event.ts";
import { EventTarget } from "./event_target.ts";

const encoder = new TextEncoder();
const decoder = new TextDecoder();

export function encodeMessage(data: any): Uint8Array {
  const dataJson = JSON.stringify(data);
  return encoder.encode(dataJson);
}

export function decodeMessage(dataIntArray: Uint8Array): any {
  const dataJson = decoder.decode(dataIntArray);
  return JSON.parse(dataJson);
}

function createWorker(
  specifier: string,
  includeDenoNamespace: boolean,
  hasSourceCode: boolean,
  sourceCode: Uint8Array
): { id: number; loaded: boolean } {
  return sendSync(dispatch.OP_CREATE_WORKER, {
    specifier,
    includeDenoNamespace,
    hasSourceCode,
    sourceCode: new TextDecoder().decode(sourceCode)
  });
}

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 });
}

function hostPostMessage(id: number, data: any): void {
  const dataIntArray = encodeMessage(data);
  sendSync(dispatch.OP_HOST_POST_MESSAGE, { id }, dataIntArray);
}

async function hostGetMessage(id: number): Promise<any> {
  const res = await sendAsync(dispatch.OP_HOST_GET_MESSAGE, { id });

  if (res.data != null) {
    return decodeMessage(new Uint8Array(res.data));
  } else {
    return null;
  }
}

// Stuff for workers
export const onmessage: (e: { data: any }) => void = (): void => {};
export const onerror: (e: { data: any }) => void = (): void => {};

export function postMessage(data: any): void {
  const dataIntArray = encodeMessage(data);
  sendSync(dispatch.OP_WORKER_POST_MESSAGE, {}, dataIntArray);
}

export async function getMessage(): Promise<any> {
  log("getMessage");
  const res = await sendAsync(dispatch.OP_WORKER_GET_MESSAGE);
  if (res.data != null) {
    return decodeMessage(new Uint8Array(res.data));
  } else {
    return null;
  }
}

export let isClosing = false;

export function workerClose(): void {
  isClosing = true;
}

export async function workerMain(): Promise<void> {
  log("workerMain");

  while (!isClosing) {
    const data = await getMessage();
    if (data == null) {
      log("workerMain got null message. quitting.");
      break;
    }

    let result: void | Promise<void>;
    const event = { data };

    try {
      if (!globalThis["onmessage"]) {
        break;
      }
      result = globalThis.onmessage!(event);
      if (result && "then" in result) {
        await result;
      }
      if (!globalThis["onmessage"]) {
        break;
      }
    } catch (e) {
      if (globalThis["onerror"]) {
        const result = globalThis.onerror(
          e.message,
          e.fileName,
          e.lineNumber,
          e.columnNumber,
          e
        );
        if (result === true) {
          continue;
        }
      }
      throw e;
    }
  }
}

export interface Worker {
  onerror?: (e: any) => void;
  onmessage?: (e: { data: any }) => void;
  onmessageerror?: () => void;
  postMessage(data: any): void;
  // TODO(bartlomieju): remove this
  closed: Promise<void>;
}

// TODO(kevinkassimo): Maybe implement reasonable web worker options?
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface WorkerOptions {}

/** Extended Deno Worker initialization options.
 * `noDenoNamespace` hides global `globalThis.Deno` namespace for
 * spawned worker and nested workers spawned by it (default: false).
 */
export interface DenoWorkerOptions extends WorkerOptions {
  noDenoNamespace?: boolean;
}

export class WorkerImpl extends EventTarget implements Worker {
  private readonly id: number;
  private isClosing = false;
  private messageBuffer: any[] = [];
  private ready = false;
  private readonly isClosedPromise: Resolvable<void>;
  public onerror?: (e: any) => void;
  public onmessage?: (data: any) => void;
  public onmessageerror?: () => void;

  constructor(specifier: string, options?: DenoWorkerOptions) {
    super();
    let hasSourceCode = false;
    let sourceCode = new Uint8Array();

    let includeDenoNamespace = true;
    if (options && options.noDenoNamespace) {
      includeDenoNamespace = false;
    }
    // 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!;
    }

    const { id, loaded } = createWorker(
      specifier,
      includeDenoNamespace,
      hasSourceCode,
      sourceCode
    );
    this.id = id;
    this.ready = loaded;
    this.isClosedPromise = createResolvable();
    this.poll();
  }

  get closed(): Promise<void> {
    return this.isClosedPromise;
  }

  private handleError(e: any): boolean {
    // 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;
    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);
        this.isClosedPromise.resolve();
        break;
      }
    }
  }

  postMessage(data: any): void {
    if (!this.ready) {
      this.messageBuffer.push(data);
      return;
    }

    hostPostMessage(this.id, data);
  }

  private async run(): Promise<void> {
    while (!this.isClosing) {
      const data = await hostGetMessage(this.id);
      if (data == null) {
        log("worker got null message. quitting.");
        break;
      }
      if (this.onmessage) {
        const event = { data };
        this.onmessage(event);
      }
    }
  }
}