-
Notifications
You must be signed in to change notification settings - Fork 32
/
test-node-stream.js
97 lines (83 loc) · 2.11 KB
/
test-node-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
import { EncoderStream, DecoderStream } from '../node-index.js'
import stream from 'stream'
import chai from 'chai'
import util from 'util'
import fs from 'fs'
const finished = util.promisify(stream.finished)
var assert = chai.assert
suite('cbor-x node stream tests', function(){
test('serialize/parse stream', () => {
const serializeStream = new EncoderStream({
})
const parseStream = new DecoderStream()
serializeStream.pipe(parseStream)
const received = []
parseStream.on('data', data => {
received.push(data)
})
const messages = [{
name: 'first'
}, {
name: 'second'
}, {
name: 'third'
}, {
name: 'third',
extra: [1, 3, { foo: 'hi'}, 'bye']
}]
for (const message of messages)
serializeStream.write(message)
return new Promise((resolve, reject) => {
setTimeout(() => {
assert.deepEqual(received, messages)
resolve()
}, 10)
})
})
test('stream from buffer', () => new Promise(async resolve => {
const parseStream = new DecoderStream()
let values = []
parseStream.on('data', (value) => {
values.push(value)
})
parseStream.on('end', () => {
assert.deepEqual(values, [1, 2])
resolve()
})
let bufferStream = new stream.Duplex()
bufferStream.pipe(parseStream)
bufferStream.push(new Uint8Array([1, 2]))
bufferStream.push(null)
}))
test('stream to/from file', (done) => {
const recordNum = 10000
const enc = new EncoderStream({
bundleStrings: true,
})
const read = () => {
console.time('READ')
const dec = new DecoderStream({
bundleStrings: true,
})
fs.createReadStream('test.cbor')
.on('data', (c) => console.log(c.length))
.pipe(dec)
.on('data', () => {})
.on('end', () => console.timeEnd('READ') || done())
}
enc.pipe(fs.createWriteStream('test.cbor'))
enc.on('end', () => console.timeEnd('GEN') || read())
console.log('Generating')
console.time('GEN')
const curr = Date.now()
for (let i = 0; i < recordNum; ++i) {
enc.write({ i, str: 'TEST_STR', ts: Date.now() })
}
enc.end()
})
teardown(function() {
try {
fs.unlinkSync('test.cbor')
}catch(error){}
})
})