summaryrefslogtreecommitdiffstats
path: root/devtools/shared/transport/worker-transport.js
blob: 903fd69cf4e050ec279daa6d75a2569a5e9b13b5 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

// Each worker debugger supports only a single connection to the main thread.
// However, its theoretically possible for multiple servers to connect to the
// same worker. Consequently, each transport has a connection id, to allow
// messages from multiple connections to be multiplexed on a single channel.

/**
 * A transport that uses a WorkerDebugger to send packets from the main
 * thread to a worker thread.
 */
class MainThreadWorkerDebuggerTransport {
  constructor(dbg, id) {
    this._dbg = dbg;
    this._id = id;

    this._dbgListener = {
      onMessage: this._onMessage.bind(this),
    };
  }

  ready() {
    this._dbg.addListener(this._dbgListener);
  }

  close() {
    if (this._dbgListener) {
      this._dbg.removeListener(this._dbgListener);
    }
    this._dbgListener = null;
    this.hooks?.onTransportClosed();
  }

  send(packet) {
    this._dbg.postMessage(
      JSON.stringify({
        type: "message",
        id: this._id,
        message: packet,
      })
    );
  }

  startBulkSend() {
    throw new Error("Can't send bulk data from worker threads!");
  }

  _onMessage(message) {
    const packet = JSON.parse(message);
    if (packet.type !== "message" || packet.id !== this._id || !this.hooks) {
      return;
    }

    this.hooks.onPacket(packet.message);
  }
}

exports.MainThreadWorkerDebuggerTransport = MainThreadWorkerDebuggerTransport;

/**
 * A transport that uses a WorkerDebuggerGlobalScope to send packets from a
 * worker thread to the main thread.
 */
function WorkerThreadWorkerDebuggerTransport(scope, id) {
  this._scope = scope;
  this._id = id;
  this._onMessage = this._onMessage.bind(this);
}

WorkerThreadWorkerDebuggerTransport.prototype = {
  constructor: WorkerThreadWorkerDebuggerTransport,

  ready() {
    this._scope.addEventListener("message", this._onMessage);
  },

  close() {
    this._scope.removeEventListener("message", this._onMessage);
    this.hooks?.onTransportClosed();
  },

  send(packet) {
    this._scope.postMessage(
      JSON.stringify({
        type: "message",
        id: this._id,
        message: packet,
      })
    );
  },

  startBulkSend() {
    throw new Error("Can't send bulk data from worker threads!");
  },

  _onMessage(event) {
    const packet = JSON.parse(event.data);
    if (packet.type !== "message" || packet.id !== this._id) {
      return;
    }

    if (this.hooks) {
      this.hooks.onPacket(packet.message);
    }
  },
};

exports.WorkerThreadWorkerDebuggerTransport =
  WorkerThreadWorkerDebuggerTransport;