-
Notifications
You must be signed in to change notification settings - Fork 161
/
thin-byob-byte-stream-reader.js
228 lines (188 loc) · 6.46 KB
/
thin-byob-byte-stream-reader.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
export class ThinByobByteStreamReader {
constructor(source) {
this._source = source;
// Stores a tuple of the following data for reader.read() calls which were made after the reader is closed or
// errored until the underlying source finishes returning all the buffers given to the underlying source:
// - the Uint8Array provided by the user of the reader on the reader.read() call
// - resolution/rejection function of the promise returned to the user of the reader on the reader.read() call
this._pendingReads = [];
// Stores a tuple of the following data for _source.read() calls which are not yet fulfilled:
// - the Uint8Array given to the source
// - resolution/rejection function of the promise returned to the user of the reader on the corresponding
// reader.read() call
this._pendingSourceReads = [];
// True when closed but the source may still return the given buffers using done().
this._closed = false;
// True when errored but the source may still return the given buffers using done().
this._errored = false;
this._errorReason = undefined;
// True when errored and the source cannot return the given buffers.
this._fatalErrored = false;
this._closedPromise = new Promise((resolve, reject) => {
this._resolveClosedPromise = resolve;
this._rejectClosedPromise = reject;
});
if (this._source.start !== undefined) {
this._source.start(this._close.bind(this), this._error.bind(this), this._fatalError.bind(this));
}
}
get closed() {
return this._closedPromise;
}
_processPendingReads() {
while (this._pendingReads.length > 0) {
const entry = this._pendingReads.shift();
if (this._fatalErrored) {
entry.reject({reason: this._errorReason, view: undefined});
} else if (this._errored) {
entry.reject({reason: this._errorReason, view: entry.view.subarray(0, 0)});
} else if (this._closed) {
entry.resolve({view: entry.view.subarray(0, 0), done: true});
} else if (this._source === undefined) {
entry.reject({reason: new TypeError('already released'), view: entry.view.subarray(0, 0)});
} else {
// Not reached
}
}
}
_handleFatalError(reason) {
this._fatalErrored = true;
this._errorReason = reason;
while (this._pendingSourceReads.length > 0) {
const entry = this._pendingSourceReads.shift();
entry.reject({reason, view: undefined});
}
this._processPendingReads();
}
// Tell the stream that the underlying source has done or aborted writing to the oldest pending view.
_done(bytesWritten) {
if (this._fatalErrored) {
throw new TypeError('already fatal-errored');
}
if (this._pendingSourceReads.length === 0) {
throw new TypeError('no pending read');
}
const entry = this._pendingSourceReads.shift();
// TODO: Detach entry.view
if (this._errored) {
entry.reject({reason: this._errorReason, view: entry.view.subarray(0, 0)});
if (this._pendingSourceReads.length === 0) {
this._processPendingReads();
}
return;
}
if (this._closed) {
entry.resolve({value: entry.view.subarray(0, 0), done: true});
if (this._pendingSourceReads.length === 0) {
this._processPendingReads();
}
return;
}
if (bytesWritten === undefined) {
throw new TypeError('bytesWritten is undefined');
}
if (entry.view.byteLength < bytesWritten) {
throw new RangeError('bytesWritten is bigger than the given view');
}
entry.resolve({value: entry.view.subarray(0, bytesWritten), done: false});
}
_markClosed() {
this._closed = true;
this._resolveClosedPromise();
this._resolveClosedPromise = undefined;
this._rejectClosedPromise = undefined;
}
_close() {
if (this._fatalErrored) {
throw new TypeError('already fatal-errored');
}
if (this._errored) {
throw new TypeError('already errored');
}
if (this._closed) {
throw new TypeError('already closed');
}
this._markClosed();
}
_markErrored(reason) {
this._errored = true;
this._errorReason = reason;
this._rejectClosedPromise(reason);
this._resolveClosedPromise = undefined;
this._rejectClosedPromise = undefined;
}
_error(reason) {
if (this._fatalErrored) {
throw new TypeError('already fatal-errored');
}
if (this._errored) {
throw new TypeError('already errored');
}
if (this._closed) {
throw new TypeError('already closed');
}
this._markErrored(reason);
}
_fatalError(reason) {
if (this._fatalErrored) {
throw new TypeError('already fatal-errored');
}
this._handleFatalError(reason);
}
read(view) {
if (this._fatalErrored) {
return Promise.reject({reason: this._errorReason, view: undefined});
}
return new Promise((resolve, reject) => {
if (this._errored || this._closed || this._source === undefined) {
if (this._pendingSourceReads.length > 0) {
this._pendingReads.push({view, resolve, reject});
} else {
if (this._errored) {
reject({reason: this._errorReason, view: view.subarray(0, 0)});
} else if (this._closed) {
resolve({value: view.subarray(0, 0), done: true});
} else {
reject({reason: new TypeError('already released'), view: view.subarray(0, 0)});
}
}
return;
}
// TODO: Detach view
this._pendingSourceReads.push({view, resolve, reject});
try {
this._source.read(
view,
this._done.bind(this),
this._close.bind(this),
this._error.bind(this),
this._fatalError.bind(this));
} catch (e) {
if (!(this._fatalErrored || this._errored || this._closed)) {
this._markErrored(e);
}
}
});
}
cancel(reason) {
if (this._source === undefined) {
return Promise.reject(new TypeError('already released'));
}
if (this._fatalErrored || this._errored) {
return Promise.reject(this._errorReason);
}
if (this._closed) {
return Promise.reject(new TypeError('already closed'));
}
this._markClosed();
return new Promise((resolve, reject) => {
resolve(this._source.cancel(reason));
}).then(r => undefined);
}
releaseLock() {
if (this._source === undefined) {
throw new TypeError('already released');
}
this._source = undefined;
}
}