-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathio.js
185 lines (174 loc) · 4.76 KB
/
io.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
let id = 0;
const symbolDispose = Symbol.dispose || Symbol.for('dispose');
const IoError = class Error {
constructor (msg) {
this.msg = msg;
}
toDebugString () {
return this.msg;
}
}
/**
* @typedef {{
* read?: (len: BigInt) => Uint8Array,
* blockingRead: (len: BigInt) => Uint8Array,
* skip?: (len: BigInt) => BigInt,
* blockingSkip?: (len: BigInt) => BigInt,
* subscribe: () => void,
* drop?: () => void,
* }} InputStreamHandler
*
* @typedef {{
* checkWrite?: () -> BigInt,
* write: (buf: Uint8Array) => BigInt,
* blockingWriteAndFlush?: (buf: Uint8Array) => void,
* flush?: () => void,
* blockingFlush: () => void,
* writeZeroes?: (len: BigInt) => void,
* blockingWriteZeroes?: (len: BigInt) => void,
* blockingWriteZeroesAndFlush?: (len: BigInt) => void,
* splice?: (src: InputStream, len: BigInt) => BigInt,
* blockingSplice?: (src: InputStream, len: BigInt) => BigInt,
* forward?: (src: InputStream) => void,
* subscribe?: () => void,
* drop?: () => void,
* }} OutputStreamHandler
*
**/
class InputStream {
/**
* @param {InputStreamHandler} handler
*/
constructor (handler) {
if (!handler)
console.trace('no handler');
this.id = ++id;
this.handler = handler;
}
read(len) {
if (this.handler.read)
return this.handler.read(len);
return this.handler.blockingRead.call(this, len);
}
blockingRead(len) {
return this.handler.blockingRead.call(this, len);
}
skip(len) {
if (this.handler.skip)
return this.handler.skip.call(this, len);
if (this.handler.read) {
const bytes = this.handler.read.call(this, len);
return BigInt(bytes.byteLength);
}
return this.blockingSkip.call(this, len);
}
blockingSkip(len) {
if (this.handler.blockingSkip)
return this.handler.blockingSkip.call(this, len);
const bytes = this.handler.blockingRead.call(this, len);
return BigInt(bytes.byteLength);
}
subscribe() {
console.log(`[streams] Subscribe to input stream ${this.id}`);
}
[symbolDispose] () {
if (this.handler.drop)
this.handler.drop.call(this);
}
}
class OutputStream {
/**
* @param {OutputStreamHandler} handler
*/
constructor (handler) {
if (!handler)
console.trace('no handler');
this.id = ++id;
this.open = true;
this.handler = handler;
}
checkWrite(len) {
if (!this.open)
return 0n;
if (this.handler.checkWrite)
return this.handler.checkWrite.call(this, len);
return 1_000_000n;
}
write(buf) {
this.handler.write.call(this, buf);
}
blockingWriteAndFlush(buf) {
/// Perform a write of up to 4096 bytes, and then flush the stream. Block
/// until all of these operations are complete, or an error occurs.
///
/// This is a convenience wrapper around the use of `check-write`,
/// `subscribe`, `write`, and `flush`, and is implemented with the
/// following pseudo-code:
///
/// ```text
/// let pollable = this.subscribe();
/// while !contents.is_empty() {
/// // Wait for the stream to become writable
/// poll-one(pollable);
/// let Ok(n) = this.check-write(); // eliding error handling
/// let len = min(n, contents.len());
/// let (chunk, rest) = contents.split_at(len);
/// this.write(chunk ); // eliding error handling
/// contents = rest;
/// }
/// this.flush();
/// // Wait for completion of `flush`
/// poll-one(pollable);
/// // Check for any errors that arose during `flush`
/// let _ = this.check-write(); // eliding error handling
/// ```
this.handler.write.call(this, buf);
}
flush() {
if (this.handler.flush)
this.handler.flush.call(this);
}
blockingFlush() {
this.open = true;
}
writeZeroes(len) {
this.write.call(this, new Uint8Array(Number(len)));
}
blockingWriteZeroes(len) {
this.blockingWrite.call(this, new Uint8Array(Number(len)));
}
blockingWriteZeroesAndFlush(len) {
this.blockingWriteAndFlush.call(this, new Uint8Array(Number(len)));
}
splice(src, len) {
const spliceLen = Math.min(len, this.checkWrite.call(this));
const bytes = src.read(spliceLen);
this.write.call(this, bytes);
return bytes.byteLength;
}
blockingSplice(_src, _len) {
console.log(`[streams] Blocking splice ${this.id}`);
}
forward(_src) {
console.log(`[streams] Forward ${this.id}`);
}
subscribe() {
console.log(`[streams] Subscribe to output stream ${this.id}`);
}
[symbolDispose]() {
}
}
export const error = { Error: IoError };
export const streams = { InputStream, OutputStream };
class Pollable {}
function pollList (_list) {
// TODO
}
function pollOne (_poll) {
// TODO
}
export const poll = {
Pollable,
pollList,
pollOne
};