forked from puppeteer/puppeteer
-
Notifications
You must be signed in to change notification settings - Fork 4
/
FirefoxTransport.js
126 lines (116 loc) · 3.43 KB
/
FirefoxTransport.js
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
/**
* Copyright 2018 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const {Socket} = require('net');
/**
* @implements {!Puppeteer.ConnectionTransport}
* @internal
*/
class FirefoxTransport {
/**
* @param {number} port
* @return {!Promise<!FirefoxTransport>}
*/
static async create(port) {
const socket = new Socket();
try {
await new Promise((resolve, reject) => {
socket.once('connect', resolve);
socket.once('error', reject);
socket.connect({
port,
host: 'localhost'
});
});
} catch (e) {
socket.destroy();
throw e;
}
return new FirefoxTransport(socket);
}
/**
* @param {!Socket} socket
*/
constructor(socket) {
this._socket = socket;
this._socket.once('close', had_error => {
if (this.onclose)
this.onclose.call(null);
});
this._dispatchQueue = new DispatchQueue(this);
let buffer = Buffer.from('');
socket.on('data', async data => {
buffer = Buffer.concat([buffer, data]);
while (true) {
const bufferString = buffer.toString();
const seperatorIndex = bufferString.indexOf(':');
if (seperatorIndex === -1)
return;
const length = parseInt(bufferString.substring(0, seperatorIndex), 10);
if (buffer.length < length + seperatorIndex)
return;
const message = buffer.slice(seperatorIndex + 1, seperatorIndex + 1 + length).toString();
buffer = buffer.slice(seperatorIndex + 1 + length);
this._dispatchQueue.enqueue(message);
}
});
// Silently ignore all errors - we don't know what to do with them.
this._socket.on('error', () => {});
this.onmessage = null;
this.onclose = null;
}
/**
* @param {string} message
*/
send(message) {
this._socket.write(Buffer.byteLength(message) + ':' + message);
}
close() {
this._socket.destroy();
}
}
// We want to dispatch all "message" events in separate tasks
// to make sure all message-related promises are resolved first
// before dispatching next message.
//
// We cannot just use setTimeout() in Node.js here like we would
// do in Browser - see https://github.com/nodejs/node/issues/23773
// Thus implement a dispatch queue that enforces new tasks manually.
/**
* @internal
*/
class DispatchQueue {
constructor(transport) {
this._transport = transport;
this._timeoutId = null;
this._queue = [];
this._dispatch = this._dispatch.bind(this);
}
enqueue(message) {
this._queue.push(message);
if (!this._timeoutId)
this._timeoutId = setTimeout(this._dispatch, 0);
}
_dispatch() {
const message = this._queue.shift();
if (this._queue.length)
this._timeoutId = setTimeout(this._dispatch, 0)
else
this._timeoutId = null;
if (this._transport.onmessage)
this._transport.onmessage.call(null, message);
}
}
module.exports = FirefoxTransport;