-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.ts
422 lines (409 loc) · 13.2 KB
/
io.ts
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//This file contains functions for performing I/O;
//specifically, reads and writes of types and values and HTTP responses
import * as http from 'http'
import {Readable, Writable} from 'stream'
import {CustomPromisifySymbol, promisify} from 'util'
import * as zlib from 'zlib'
import * as accepts from 'accepts'
import {AppendableStream} from './lib/appendable-stream'
import * as assert from './lib/assert'
import {toArrayBuffer} from './lib/growable-buffer'
import * as r from './read'
import AbstractType from './types/abstract'
import type {Type} from './types'
type ArrayBufferCallback = (err: Error | null, buffer: ArrayBuffer) => void
function concatStream(stream: Readable, callback: ArrayBufferCallback) {
const segments: Buffer[] = []
stream
.on('data', chunk =>
segments.push(chunk instanceof Buffer ? chunk : Buffer.from(chunk))
)
.on('error', err => {
stream.destroy()
//eslint-disable-next-line @typescript-eslint/no-non-null-assertion
callback(err, null!)
})
.on('end', () =>
callback(null, toArrayBuffer(Buffer.concat(segments)))
)
}
export interface WriteParams<E> {
type: Type<E>
outStream: Writable
}
export interface WriteTypeValueParams<E> extends WriteParams<E> {
value: E
}
export interface ReadValueParams<E> {
type: Type<unknown, E>
inStream: Readable
}
export interface TypeAndValue<E> {
type: Type<E>
value: E
}
export interface HttpParams<E> {
req: http.IncomingMessage
res: http.OutgoingMessage
type: Type<E>
value: E
}
/**
* A callback that receives only a possible error
*/
export type ErrCallback = (err: Error | null) => void
/**
* A callback that receives a possible error and,
* if no error occurred, a read type
*/
export type TypeCallback<E> = (err: Error | null, type: Type<E>) => void
/**
* A callback that receives a possible error and,
* if no error occurred, a read value
*/
export type ValueCallback<E> = (err: Error | null, value: E) => void
/**
* A callback that receives a possible error and,
* if no error occurred, a read type and value
*/
export type TypeAndValueCallback<E> = (err: Error | null, type: Type<E>, value: E) => void
/**
* Writes the contents of `type.toBuffer()` ([[Type.toBuffer]])
* to a writable stream and then closes the stream.
* Calls `callback` when done.
*
* Example:
* ````javascript
* //With a callback:
* sb.writeType({
* type: new sb.StructType({
* abc: new sb.ArrayType(new sb.StringType),
* def: new sb.DateType
* }),
* outStream: fs.createWriteStream('out.sbt')
* }, err => {
* if (err) throw err
* console.log('Done')
* })
*
* //As a Promise:
* util.promisify(sb.writeType)({
* type: new sb.StructType({
* abc: new sb.ArrayType(new sb.StringType),
* def: new sb.DateType
* }),
* outStream: fs.createWriteStream('out.sbt')
* })
* .then(_ => console.log('Done'))
* ````
*
* @param type The type to write
* @param outStream The stream to write to
* @param callback The optional callback to call when write ends
* @return `outStream`
*/
export function writeType({type, outStream}: WriteParams<unknown>, callback?: ErrCallback): Writable {
assert.instanceOf(type, AbstractType)
if (callback === undefined) callback = () => {}
assert.instanceOf(callback, Function)
let error: Error | null = null
outStream
.on('error', callback)
//eslint-disable-next-line @typescript-eslint/no-non-null-assertion
.on('finish', () => callback!(error))
const typeStream = new AppendableStream(outStream)
try { type.addToBuffer(typeStream) }
catch (err) { error = err }
outStream.end()
return outStream
}
/**
* Writes the contents of `type.valueBuffer(value)` ([[Type.valueBuffer]])
* to a writable stream and then closes the stream.
* Calls `callback` when done.
*
* Example:
* ````javascript
* //With a callback:
* sb.writeValue({
* type: new sb.FlexUnsignedIntType,
* value: 1000,
* outStream: fs.createWriteStream('out.sbv')
* }, err => {
* if (err) throw err
* console.log('Done')
* })
*
* //As a Promise:
* util.promisify(sb.writeValue)({
* type: new sb.FlexUnsignedIntType,
* value: 1000,
* outStream: fs.createWriteStream('out.sbv')
* })
* .then(_ => console.log('Done'))
* ````
*
* @param E The type of value being written
* @param type The type to use to write the value
* @param value The value to write
* @param outStream The stream to write to
* @param callback The optional callback to call when write ends
* @return `outStream`
*/
export function writeValue<E>({type, value, outStream}: WriteTypeValueParams<E>, callback?: ErrCallback): Writable {
assert.instanceOf(type, AbstractType)
if (callback === undefined) callback = () => {}
assert.instanceOf(callback, Function)
let error: Error | null = null
outStream
.on('error', callback)
//eslint-disable-next-line @typescript-eslint/no-non-null-assertion
.on('finish', () => callback!(error))
const valueStream = new AppendableStream(outStream)
try { type.writeValue(valueStream, value) }
catch (err) { error = err }
outStream.end()
return outStream
}
/**
* Writes the contents of `type.toBuffer()` ([[Type.toBuffer]]),
* followed by the contents of `type.valueBuffer(value)` ([[Type.valueBuffer]]),
* to a writable stream and then closes the stream.
* Calls `callback` when done.
*
* Example:
* ````javascript
* //With a callback:
* sb.writeTypeAndValue({
* type: new sb.FlexUnsignedIntType,
* value: 1000,
* outStream: fs.createWriteStream('out.sbtv')
* }, err => {
* if (err) throw err
* console.log('Done')
* })
*
* //As a Promise:
* util.promisify(sb.writeTypeAndValue)({
* type: new sb.FlexUnsignedIntType,
* value: 1000,
* outStream: fs.createWriteStream('out.sbtv')
* })
* .then(_ => console.log('Done'))
* ````
*
* @param E The type of value being written
* @param type The type to write and to use to write the value
* @param value The value to write
* @param outStream The stream to write to
* @param callback The optional callback to call when write ends
* @return `outStream`
*/
export function writeTypeAndValue<E>({type, value, outStream}: WriteTypeValueParams<E>, callback?: ErrCallback): Writable {
assert.instanceOf(type, AbstractType)
if (callback === undefined) callback = () => {}
assert.instanceOf(callback, Function)
let error: Error | null = null
outStream
.on('error', callback)
//eslint-disable-next-line @typescript-eslint/no-non-null-assertion
.on('finish', () => callback!(error))
const typeValueStream = new AppendableStream(outStream)
try {
type.addToBuffer(typeValueStream)
type.writeValue(typeValueStream, value)
}
catch (err) { error = err }
outStream.end()
return outStream
}
/**
* Reads a type from a readable stream.
* This should be used when reading from sources
* written to by [[writeType]].
* Calls `callback` with the type when done.
*
* Example:
* ````javascript
* //With a callback:
* sb.readType(fs.createReadStream('out.sbt'), (err, type) => {
* if (err) throw err
* console.log(type)
* })
*
* //As a Promise:
* util.promisify(sb.readType)(fs.createReadStream('out.sbt'))
* .then(type => console.log(type))
* ````
*
* @param inStream The stream to read from
* @param callback The callback to call with the read result
*/
export function readType<E>(inStream: Readable, callback: TypeCallback<E>): void {
assert.instanceOf(inStream, Readable)
assert.instanceOf(callback, Function)
concatStream(inStream, (err, buffer) => {
//eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (err) return callback(err, null!)
let type: Type<E>
try { type = r.type(buffer, false) as Type<E> }
//eslint-disable-next-line @typescript-eslint/no-non-null-assertion
catch (e) { return callback(e, null!) }
callback(null, type)
})
}
/**
* Reads a value from a readable stream.
* The [[Type]] used to write the value bytes must be known.
* This should be used when reading from sources
* written to by [[writeValue]].
* Calls `callback` with the value when done.
*
* Example:
* ````javascript
* //With a callback:
* sb.readValue({
* type: new sb.FlexUnsignedIntType,
* inStream: fs.createReadStream('out.sbv')
* }, (err, value) => {
* if (err) throw err
* console.log(value)
* })
*
* //As a Promise:
* util.promisify(sb.readValue)({
* type: new sb.FlexUnsignedIntType,
* inStream: fs.createReadStream('out.sbv')
* })
* .then(value => console.log(value))
* ````
*
* @param E The type of value being read
* (must match the `VALUE` parameter of `type`)
* @param type The [[Type]] (or an equivalent one) that wrote the value bytes
* @param inStream The stream to read from
* @param callback The callback to call with the read result
*/
export function readValue<E>({type, inStream}: ReadValueParams<E>, callback: ValueCallback<E>): void {
assert.instanceOf(inStream, Readable)
assert.instanceOf(callback, Function)
concatStream(inStream, (err, buffer) => {
//eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (err) return callback(err, null!)
let value: E
try { value = type.readValue(buffer) }
//eslint-disable-next-line @typescript-eslint/no-non-null-assertion
catch (e) { return callback(e, null!) }
callback(null, value)
})
}
/**
* Reads a type and a value from a readable stream.
* This should be used when reading from sources
* written to by [[writeTypeAndValue]].
* Calls `callback` with the type and value when done.
*
* Example:
* ````javascript
* //With a callback:
* sb.readTypeAndValue(fs.createReadStream('out.sbtv'), (err, type, value) => {
* if (err) throw err
* console.log(type, value)
* })
*
* //As a Promise:
* util.promisify(sb.readTypeAndValue)(fs.createReadStream('out.sbtv'))
* .then(({type, value}) => console.log(type, value))
* ````
*
* @param inStream The stream to read from
* @param callback The callback to call with the read result
*/
export const readTypeAndValue = (
<E>(inStream: Readable, callback: TypeAndValueCallback<E>): void => {
assert.instanceOf(inStream, Readable)
assert.instanceOf(callback, Function)
concatStream(inStream, (err, buffer) => {
//eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (err) return callback(err, null!, null!)
const bufferOffset = {buffer, offset: 0}
let type: Type<E>
//Using consumeType() in order to get the length of the type (the start of the value)
try { type = r.consumeType(bufferOffset) as Type<E> }
//eslint-disable-next-line @typescript-eslint/no-non-null-assertion
catch (e) { return callback(e, null!, null!) }
let value: E
try { value = type.readValue(buffer, bufferOffset.offset) }
//eslint-disable-next-line @typescript-eslint/no-non-null-assertion
catch (e) { return callback(e, null!, null!) }
callback(null, type, value)
})
}
) as (<E>(inStream: Readable, callback: TypeAndValueCallback<E>) => void) &
CustomPromisifySymbol<<E>(inStream: Readable) => Promise<TypeAndValue<E>>>
//Custom promisifiy function because Promise cannot resolve to 2 values
readTypeAndValue[promisify.custom] = <E>(inStream: Readable) =>
new Promise<TypeAndValue<E>>((resolve, reject) =>
readTypeAndValue<E>(inStream, (err, type, value) => {
if (err) reject(err)
else resolve({type, value})
})
)
/**
* Responds to an HTTP(S) request for a value.
* Will send both type and value if the `sig` header
* doesn't match the type's signature.
* Will only send the value if the signatures match.
* Response is gzipped to decrease size, if client allows.
* Calls `callback` when done.
*
* Example:
* ````javascript
* sb.httpRespond({
* req,
* res,
* type: new sb.DateType
* value: new Date
* })
* ````
*
* @param req The client request
* @param res The server response
* @param type The [[Type]] to use to write the value
* @param value The value to send
* @param callback The optional callback to call when response ends
*/
export function httpRespond<E>({req, res, type, value}: HttpParams<E>, callback?: ErrCallback): void {
assert.instanceOf(type, AbstractType)
if (callback === undefined) callback = () => {}
assert.instanceOf(callback, Function)
assert.instanceOf(req, http.IncomingMessage)
assert.instanceOf(res, http.OutgoingMessage)
try {
res.setHeader('Content-Type', 'application/octet-stream')
res.setHeader('sig', type.getSignature())
const acceptsGzip = accepts(req).encoding(['gzip'])
let outStream: Writable
if (acceptsGzip) {
res.setHeader('Content-Encoding', 'gzip')
outStream = zlib.createGzip() //pipe into a zip stream to decrease size of response
}
else outStream = res
const writeEndCallback = (err: Error | null) => {
//eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (err) callback!(err)
//eslint-disable-next-line @typescript-eslint/no-non-null-assertion
else if (!acceptsGzip) callback!(null)
}
if (req.headers.sig && req.headers.sig === type.getSignature()) { //if client already has type, only value needs to be sent
writeValue({type, value, outStream}, writeEndCallback)
}
else writeTypeAndValue({type, value, outStream}, writeEndCallback) //otherwise, type and value need to be sent
if (acceptsGzip) { //don't pipe until writing begins
outStream.pipe(res)
//eslint-disable-next-line @typescript-eslint/no-non-null-assertion
.on('finish', () => callback!(null))
}
}
catch (err) { callback(err) }
}