-
Notifications
You must be signed in to change notification settings - Fork 160
/
ReadableByteStreamController-impl.js
110 lines (88 loc) · 3.1 KB
/
ReadableByteStreamController-impl.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
'use strict';
const assert = require('assert');
const { CancelSteps, PullSteps, ReleaseSteps } = require('./abstract-ops/internal-methods.js');
const { ResetQueue } = require('./abstract-ops/queue-with-sizes.js');
const aos = require('./abstract-ops/readable-streams.js');
exports.implementation = class ReadableByteStreamControllerImpl {
get byobRequest() {
return aos.ReadableByteStreamControllerGetBYOBRequest(this);
}
get desiredSize() {
return aos.ReadableByteStreamControllerGetDesiredSize(this);
}
close() {
if (this._closeRequested === true) {
throw new TypeError('The stream has already been closed; do not close it again!');
}
const state = this._stream._state;
if (state !== 'readable') {
throw new TypeError(`The stream (in ${state} state) is not in the readable state and cannot be closed`);
}
aos.ReadableByteStreamControllerClose(this);
}
enqueue(chunk) {
if (chunk.byteLength === 0) {
throw new TypeError('chunk must have non-zero byteLength');
}
if (chunk.buffer.byteLength === 0) {
throw new TypeError('chunk\'s buffer must have non-zero byteLength');
}
if (this._closeRequested === true) {
throw new TypeError('stream is closed or draining');
}
const state = this._stream._state;
if (state !== 'readable') {
throw new TypeError(`The stream (in ${state} state) is not in the readable state and cannot be enqueued to`);
}
aos.ReadableByteStreamControllerEnqueue(this, chunk);
}
error(e) {
aos.ReadableByteStreamControllerError(this, e);
}
[CancelSteps](reason) {
aos.ReadableByteStreamControllerClearPendingPullIntos(this);
ResetQueue(this);
const result = this._cancelAlgorithm(reason);
aos.ReadableByteStreamControllerClearAlgorithms(this);
return result;
}
[PullSteps](readRequest) {
const stream = this._stream;
assert(aos.ReadableStreamHasDefaultReader(stream) === true);
if (this._queueTotalSize > 0) {
assert(aos.ReadableStreamGetNumReadRequests(stream) === 0);
aos.ReadableByteStreamControllerFillReadRequestFromQueue(this, readRequest);
return;
}
const autoAllocateChunkSize = this._autoAllocateChunkSize;
if (autoAllocateChunkSize !== undefined) {
let buffer;
try {
buffer = new ArrayBuffer(autoAllocateChunkSize);
} catch (bufferE) {
readRequest.errorSteps(bufferE);
return;
}
const pullIntoDescriptor = {
buffer,
bufferByteLength: autoAllocateChunkSize,
byteOffset: 0,
byteLength: autoAllocateChunkSize,
bytesFilled: 0,
elementSize: 1,
viewConstructor: Uint8Array,
readerType: 'default'
};
this._pendingPullIntos.push(pullIntoDescriptor);
}
aos.ReadableStreamAddReadRequest(stream, readRequest);
aos.ReadableByteStreamControllerCallPullIfNeeded(this);
}
[ReleaseSteps]() {
if (this._pendingPullIntos.length > 0) {
const firstPullInto = this._pendingPullIntos[0];
firstPullInto.readerType = 'none';
this._pendingPullIntos = [firstPullInto];
}
}
};