-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreference-sink.js
59 lines (45 loc) · 1.31 KB
/
reference-sink.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
'use strict'
const { Buffer } = require('buffer')
const Status = require('./reference-status-enum')
class Sink {
constructor () {
this.source = null
this.exitCb = null
}
bindSource (source) {
// source MUST be a data source with a read(err, buffer) function
this.source = source
// Critically important
this.source.bindSink(this)
}
start (exitCb) {
// exitCb MUST be a function with an error argument
this.exitCb = exitCb
// start reading
// sink handles buffer allocation
const buffer = Buffer.alloc(0)
this.source.pull(null, buffer)
}
next (status, error, buffer, bytes) {
// status MUST be a valid status indicator (string currently)
// options are: Status.error, Status.continue, or Status.end
// error MUST be null or an error
// buffer MUST be a Buffer
// bytes MUST be the number of bytes read
if (error || status === Status.end) {
// cleanup
return this.exitCb(error)
}
// write or process buffer here
// if there was an error writing or processing the buffer...
const sinkError = new Error()
if (sinkError) {
return this.source.pull(error)
}
// pull again
// sink handles buffer allocation
const buf = Buffer.alloc(0)
this.source.pull(null, buf)
}
}
module.exports = Sink