basic.js
2.21 KB
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
const t = require('tap')
const MPS = require('../')
t.test('ok if size checks out', t => {
const mps = new MPS({ size: 4 })
mps.write(Buffer.from('a').toString('hex'), 'hex')
mps.write(Buffer.from('sd'))
mps.end('f')
return mps.concat().then(data => t.equal(data.toString(), 'asdf'))
})
t.test('error if size exceeded', t => {
const mps = new MPS({ size: 1 })
mps.on('error', er => {
t.match(er, {
message: 'Bad data size: expected 1 bytes, but got 4',
found: 4,
expect: 1,
code: 'EBADSIZE',
name: 'SizeError',
})
t.end()
})
mps.write('asdf')
})
t.test('error if size is not met', t => {
const mps = new MPS({ size: 999 })
t.throws(() => mps.end(), {
message: 'Bad data size: expected 999 bytes, but got 0',
found: 0,
name: 'SizeError',
expect: 999,
code: 'EBADSIZE',
})
t.end()
})
t.test('error if non-string/buffer is written', t => {
const mps = new MPS({size:1})
mps.on('error', er => {
t.match(er, {
message: 'MinipassSized streams only work with string and buffer data'
})
t.end()
})
mps.write({some:'object'})
})
t.test('projectiles', t => {
t.throws(() => new MPS(), {
message: 'invalid expected size: undefined'
}, 'size is required')
t.throws(() => new MPS({size: true}), {
message: 'invalid expected size: true'
}, 'size must be number')
t.throws(() => new MPS({size: NaN}), {
message: 'invalid expected size: NaN'
}, 'size must not be NaN')
t.throws(() => new MPS({size:1.2}), {
message: 'invalid expected size: 1.2'
}, 'size must be integer')
t.throws(() => new MPS({size: Infinity}), {
message: 'invalid expected size: Infinity'
}, 'size must be finite')
t.throws(() => new MPS({size: -1}), {
message: 'invalid expected size: -1'
}, 'size must be positive')
t.throws(() => new MPS({objectMode: true}), {
message: 'MinipassSized streams only work with string and buffer data'
}, 'no objectMode')
t.throws(() => new MPS({size: Number.MAX_SAFE_INTEGER + 1000000}), {
message: 'invalid expected size: 9007199255740992'
})
t.end()
})
t.test('exports SizeError class', t => {
t.isa(MPS.SizeError, 'function')
t.isa(MPS.SizeError.prototype, Error)
t.end()
})