-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.js
169 lines (139 loc) · 3.98 KB
/
stream.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
const DEFAULT_WINDOW_SIZE = 256*1024;
//const DEFAULT_WINDOW_SIZE = 512*1024;
//const DEFAULT_WINDOW_SIZE = 6*1024*1024;
// TODO: Firefox and Deno both fail to notice if the chunk size is too big.
// The server side throws an error and closes the connection but the clients
// exit without any exceptions.
const MAX_CHUNK_SIZE = 64*1024;
class Stream {
constructor(streamId, writeCallback, closeCallback, windowCallback) {
this.syn = true;
this._streamId = streamId;
this._writeCallback = writeCallback;
this._closeCallback = closeCallback;
this._windowSize = DEFAULT_WINDOW_SIZE;
this._readClosed = false;
this._readableController = null;
this._writableController = null;
this._onWindowIncreaseCallback = null;
this._queue = [];
const stream = this;
this._readable = new ReadableStream(
{
start(controller) {
stream._readableController = controller;
},
pull(controller) {
if (stream._queue.length > 0) {
const chunk = stream._queue.shift()
controller.enqueue(chunk);
windowCallback(streamId, chunk.length);
}
if (stream._queue.length === 0) {
const promise = new Promise((resolve, reject) => {
stream._queueResolve = () => {
resolve();
};
});
return promise;
}
},
cancel() {
// TODO: should probably be doing something here...
//console.log("TODO: reader cancel signal", stream._streamId);
stream._readClosed = true;
},
},
//new CountQueuingStrategy({ highWaterMark: 100 })
);
this._writable = new WritableStream({
start(controller) {
stream._writableController = controller;
},
async write(chunk, controller) {
if (chunk.byteLength <= MAX_CHUNK_SIZE) {
return stream._attemptSend(chunk);
}
else {
const numChunks = chunk.byteLength / MAX_CHUNK_SIZE;
for (let i = 0; i < numChunks; i++) {
const offset = i*MAX_CHUNK_SIZE;
const subchunk = chunk.slice(offset, offset+MAX_CHUNK_SIZE);
await stream._attemptSend(subchunk);
}
}
},
close() {
stream._closeCallback(stream._streamId);
}
},
//new ByteLengthQueuingStrategy({
// highWaterMark: 256*1024
//})
);
}
get readable() {
return this._readable;
}
get writable() {
return this._writable;
}
_enqueueData(data) {
this._queue.push(data);
if (this._queueResolve) {
this._queueResolve();
this._queueResolve = null;
}
}
_windowIncrease(windowIncrease) {
this._windowSize += windowIncrease;
if (this._windowResolve) {
this._windowResolve();
}
if (this._onWindowIncreaseCallback) {
this._onWindowIncreaseCallback(windowIncrease);
}
}
closeRead() {
// Flush queue if necessary
while (this._queue.length > 0) {
const chunk = this._queue.shift();
this._readableController.enqueue(chunk);
}
if (!this._readClosed) {
this._readClosed = true;
return this._readableController.close();
}
}
_reset(errorCode) {
this._done = true;
this.closeRead();
if (this._writeReject) {
this._writeReject(new Error("Stream reset"));
}
}
async _attemptSend(data) {
if (this._done) {
// TODO: there's probably a way to simplify our logic by combining this
// with the promise below
return new Promise((resolve, reject) => {
reject();
});
}
if (data.length <= this._windowSize) {
this._writeCallback(this._streamId, data);
this._windowSize -= data.length;
this._windowResolve = null;
}
else {
await new Promise((resolve, reject) => {
this._windowResolve = resolve;
this._writeReject = reject;
});
return this._attemptSend(data);
}
}
}
export {
Stream,
};