-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.js
348 lines (289 loc) · 9.88 KB
/
pipe.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//
// Visualization of pipe buffer storage:
//
// [ write-offset | read-offset | ... byte-array ... ]
//
// The byte array is filled from read-offset to write-offset. It is possible
// for write-offset to be less than read-offset, which implies that we have
// wrapped around.
//
// If "X" represents a used byte, and "." represents an unused byte, we could
// have the following configurations:
//
// [ XXXXXXXXXX............. ]
//
// Or,
//
// [ ......XXXXXXXXXX....... ]
//
// Or,
//
// [ XXXXXX.............XXXX ]
//
// The write-offset is always one index beyond the last element of the used
// portion of the array. The read-offset indicates the index of the first
// used element.
//
class PipeBuffer {
constructor() {
this.sab_ = null;
this.int32_ = null;
}
initializeFromSAB(sab) {
this.sab_ = sab;
this.int32_ = new Int32Array(this.sab_);
}
initialize(size) {
this.initializeFromSAB(new SharedArrayBuffer(PipeBuffer.kHeaderSize + size + 4));
}
get sab() {
return this.sab_;
}
get numBytes() {
return this.computeNumBytes_(
Atomics.load(this.int32_, PipeBuffer.kWriteOffset),
Atomics.load(this.int32_, PipeBuffer.kReadOffset));
}
get maxBytes() {
return this.sab_.byteLength - PipeBuffer.kHeaderSize - 4;
}
hasData() {
return Atomics.load(this.int32_, PipeBuffer.kWriteOffset) !=
Atomics.load(this.int32_, PipeBuffer.kReadOffset);
}
waitForData() {
for (;;) {
var writeOffset = Atomics.load(this.int32_, PipeBuffer.kWriteOffset);
var readOffset = Atomics.load(this.int32_, PipeBuffer.kReadOffset);
if (writeOffset != readOffset)
return;
Atomics.wait(this.int32_, PipeBuffer.kWriteOffset, writeOffset);
}
}
hasSpace() {
return this.numBytes < this.maxBytes;
}
spaceAvailable() {
return this.maxBytes - this.numBytes;
}
waitForSpace() {
for (;;) {
var writeOffset = Atomics.load(this.int32_, PipeBuffer.kWriteOffset);
var readOffset = Atomics.load(this.int32_, PipeBuffer.kReadOffset);
var numBytes = this.computeNumBytes_(writeOffset, readOffset);
if (numBytes < this.maxBytes)
return;
Atomics.wait(this.int32_, PipeBuffer.kReadOffset, readOffset);
}
}
copyBytesOut() {
// Sample the write offset once. It may advance subsequently, but that's
// okay as we will only read up to the sampled point.
var writeOffset = Atomics.load(this.int32_, PipeBuffer.kWriteOffset);
var readOffset = Atomics.load(this.int32_, PipeBuffer.kReadOffset);
var num_bytes = this.computeNumBytes_(writeOffset, readOffset);
var result = new Int8Array(num_bytes);
if (writeOffset > readOffset) {
result.set(new Int8Array(this.sab_, PipeBuffer.kHeaderSize + readOffset, num_bytes));
} else {
var first_chunk_size = this.endOffset_ - readOffset;
var second_chunk_size = writeOffset;
if (first_chunk_size > 0)
result.set(new Int8Array(this.sab_, PipeBuffer.kHeaderSize + readOffset, first_chunk_size));
if (second_chunk_size > 0)
result.set(new Int8Array(this.sab_, PipeBuffer.kHeaderSize, second_chunk_size), first_chunk_size);
}
// Now we can set the read offset to the write offset as we have caught up.
Atomics.store(this.int32_, PipeBuffer.kReadOffset, writeOffset);
// Unblock waitForSpace().
(Atomics.notify ? Atomics.notify : Atomics.wake)(
this.int32_, PipeBuffer.kReadOffset, 1);
return result;
}
copyBytesIn(bytes) {
// Sample the read offset once. It may advance subsequently, but that's
// okay as we will only write up to the sampled point.
var writeOffset = Atomics.load(this.int32_, PipeBuffer.kWriteOffset);
var readOffset = Atomics.load(this.int32_, PipeBuffer.kReadOffset);
var num_bytes = this.computeNumBytes_(writeOffset, readOffset);
// The -1 here is to prevent write-offset from being incremented to equal
// read-offset.
var bytes_available = this.endOffset_ - num_bytes - 1;
var bytes_to_copy;
if (bytes_available < bytes.byteLength) {
bytes_to_copy = bytes_available;
} else {
bytes_to_copy = bytes.byteLength;
}
var int8 = new Int8Array(this.sab_, PipeBuffer.kHeaderSize, this.endOffset_);
if (readOffset > writeOffset || bytes_to_copy < (this.endOffset_ - writeOffset)) {
int8.set(new Int8Array(bytes.buffer, bytes.byteOffset, bytes_to_copy), writeOffset);
Atomics.store(this.int32_, PipeBuffer.kWriteOffset, writeOffset + bytes_to_copy);
} else {
var first_chunk_size = this.endOffset_ - writeOffset;
var second_chunk_size = bytes_to_copy - first_chunk_size;
if (first_chunk_size > 0)
int8.set(new Int8Array(bytes.buffer, bytes.byteOffset, first_chunk_size), writeOffset);
if (second_chunk_size > 0)
int8.set(new Int8Array(bytes.buffer, bytes.byteOffset + first_chunk_size, second_chunk_size), 0);
Atomics.store(this.int32_, PipeBuffer.kWriteOffset, second_chunk_size);
}
// Unblock waitForData().
(Atomics.notify ? Atomics.notify : Atomics.wake)(
this.int32_, PipeBuffer.kWriteOffset, 1);
return bytes_to_copy;
}
get endOffset_() {
return this.sab_.byteLength - PipeBuffer.kHeaderSize;
}
computeNumBytes_(writeOffset, readOffset) {
if (writeOffset >= readOffset)
return writeOffset - readOffset;
// Wrap around case.
return (this.endOffset_ - readOffset) + writeOffset;
}
}
PipeBuffer.kHeaderSize =
4 + // write offset
4; // read offset
PipeBuffer.kWriteOffset = 0;
PipeBuffer.kReadOffset = 1;
class PipeReader {
constructor(buffer) {
this.buffer_ = buffer;
}
read() { // returns Int8Array, blocking until available
this.buffer_.waitForData();
return this.buffer_.copyBytesOut();
}
tryRead() { // returns Int8Array or null
if (!this.buffer_.hasData())
return null;
return this.buffer_.copyBytesOut();
}
bytesAvailable() {
return this.numBytes;
}
}
class PipeWriter {
constructor(buffer) {
this.buffer_ = buffer;
}
write(bytes) { // Blocks until fully written.
for (;;) {
var bytes_written = this.tryWrite(bytes);
if (bytes_written == bytes.byteLength)
return;
var offset = bytes.byteOffset + bytes_written;
var length = bytes.byteLength - bytes_written;
bytes = new Int8Array(bytes.buffer, offset, length);
this.buffer_.waitForSpace();
}
}
tryWrite(bytes) { // Returns number of bytes written.
return this.buffer_.copyBytesIn(bytes);
}
spaceAvailable() {
return this.buffer_.spaceAvailable();
}
}
// MessagePipe{Reader,Writer} helps with sending framed messages.
class MessagePipeReader {
constructor(buffer) {
this.reader_ = new PipeReader(buffer);
this.messages_ = new Array();
this.partial_ = null;
}
read() { // returns Int8Array, blocking until available
if (this.messages_.length > 0)
return this.messages_.shift();
var result;
for (;;) {
var int8 = this.reader_.read();
this.ingest_(int8);
if (this.messages_.length > 0) {
result = this.messages_.shift();
break;
}
}
return result;
}
tryRead() { // returns Int8Array or null
if (this.messages_.length > 0)
return this.messages_.shift();
var int8 = this.reader_.tryRead();
if (!int8)
return null;
this.ingest_(int8);
if (this.messages_.length > 0)
return this.messages_.shift();
return null;
}
ingest_(input) {
var old_partial;
var int8;
if (this.partial_) {
int8 = new Int8Array(input.byteLength + this.partial_.byteLength);
int8.set(this.partial_);
int8.set(input, this.partial_.byteLength);
old_partial = this.partial_;
this.partial_ = null;
} else {
int8 = input;
}
var next_offset = 0;
for (;;) {
// Do we have enough to read the message size?
if ((int8.byteLength - next_offset) < 4)
break;
var int32 = new Int32Array(int8.buffer, int8.byteOffset + next_offset, 1);
var message_size = int32[0];
var aligned_message_size = this.computeAlignedLength_(message_size);
// Do we have a complete message (including alignment padding)?
if ((int8.byteLength - next_offset - 4) < aligned_message_size)
break;
var message = new Int8Array(int8.buffer, int8.byteOffset + next_offset + 4, message_size);
this.messages_.push(message);
next_offset += (4 + aligned_message_size);
}
if (int8.byteLength > next_offset)
this.partial_ = new Int8Array(int8.buffer, int8.byteOffset + next_offset);
}
computeAlignedLength_(length) {
return (length + 3) & ~3; // Align to multiples of 4
}
}
class MessagePipeWriter {
constructor(buffer) {
this.writer_ = new PipeWriter(buffer);
}
write(bytes) { // Blocks until fully written.
var int32 = new Int32Array(1);
int32[0] = bytes.byteLength;
this.writer_.write(new Int8Array(int32.buffer));
this.writer_.write(bytes);
var alignedLength = this.computeAlignedLength_(bytes.byteLength);
if (alignedLength > bytes.byteLength)
this.writer_.write(new Int8Array(bytes.byteLength - alignedLength));
}
tryWrite(bytes) { // Returns true iff message was written.
var alignedLength = this.computeAlignedLength_(bytes.byteLength);
if (this.writer_.spaceAvailable() < (4 + alignedLength))
return false;
var int32 = new Int32Array(1);
int32[0] = bytes.byteLength;
if (this.writer_.tryWrite(new Int8Array(int32.buffer)) != 4)
throw "oops";
if (this.writer_.tryWrite(bytes) != bytes.byteLength)
throw "oops";
if (alignedLength > bytes.byteLength) {
var paddingLength = alignedLength - bytes.byteLength;
if (this.writer_.tryWrite(new Int8Array(paddingLength)) != paddingLength)
throw "oops";
}
return true;
}
computeAlignedLength_(length) {
return (length + 3) & ~3; // Align to multiples of 4
}
}