ebml.js 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,206 @@
1
+ const assert = require('assert');
2
+ const unexpected = require('unexpected');
3
+ const unexpectedDate = require('unexpected-date');
4
+ const Encoder = require('./encoder');
5
+
6
+ const expect = unexpected.clone().use(unexpectedDate);
7
+
8
+ jest.dontMock('debug');
9
+ describe('EBML', () => {
10
+ describe('Encoder', () => {
11
+ function createEncoder(expected, done) {
12
+ const encoder = new Encoder();
13
+ encoder.on('data', chunk => {
14
+ expect(
15
+ chunk.toString('hex'),
16
+ 'to be',
17
+ Buffer.from(expected).toString('hex'),
18
+ );
19
+ encoder.on('finish', done);
20
+ done();
21
+ });
22
+ encoder.on('finish', done);
23
+ return encoder;
24
+ }
25
+
26
+ it('should write a single tag', done => {
27
+ const encoder = createEncoder([0x42, 0x86, 0x81, 0x01], done);
28
+ encoder.write([
29
+ 'tag',
30
+ {
31
+ name: 'EBMLVersion',
32
+ data: Buffer.from([0x01]),
33
+ },
34
+ ]);
35
+ encoder.end();
36
+ });
37
+ it('should write a tag with a single child', done => {
38
+ const encoder = createEncoder(
39
+ [0x1a, 0x45, 0xdf, 0xa3, 0x84, 0x42, 0x86, 0x81, 0x00],
40
+ done,
41
+ );
42
+ encoder.write(['start', { name: 'EBML' }]);
43
+ encoder.write([
44
+ 'tag',
45
+ {
46
+ name: 'EBMLVersion',
47
+ data: Buffer.from([0x00]),
48
+ },
49
+ ]);
50
+ encoder.write(['end', { name: 'EBML' }]);
51
+ // encoder.end();
52
+ });
53
+ describe('#cork and #uncork', () => {
54
+ /**
55
+ * @type Encoder
56
+ */
57
+ let encoder;
58
+ beforeEach(() => {
59
+ encoder = new Encoder();
60
+ });
61
+ it('should block flushing when corked', () => {
62
+ encoder.write(['start', { name: 'EBML' }]);
63
+ encoder.write([
64
+ 'tag',
65
+ {
66
+ name: 'EBMLVersion',
67
+ data: Buffer.from([0x00]),
68
+ },
69
+ ]);
70
+ encoder.cork();
71
+ encoder.write(['end', { name: 'EBML' }]);
72
+ encoder.flush();
73
+ // expect(
74
+ // encoder.buffer,
75
+ // 'to satisfy',
76
+ // Buffer.from([0x1a, 0x45, 0xdf, 0xa3, 0x84, 0x42, 0x86, 0x81, 0x00]),
77
+ // );
78
+ assert.ok(
79
+ encoder.buffer,
80
+ Buffer.from([0x1a, 0x45, 0xdf, 0xa3, 0x84, 0x42, 0x86, 0x81, 0x00]),
81
+ );
82
+ });
83
+ it('should not block flushing when uncorked', () => {
84
+ encoder.write(['start', { name: 'EBML' }]);
85
+ encoder.write([
86
+ 'tag',
87
+ {
88
+ name: 'EBMLVersion',
89
+ data: Buffer.from([0x00]),
90
+ },
91
+ ]);
92
+ encoder.cork();
93
+ encoder.write(['end', { name: 'EBML' }]);
94
+ encoder.flush();
95
+ // expect(
96
+ // encoder.buffer,
97
+ // 'to satisfy',
98
+ // Buffer.from([0x1a, 0x45, 0xdf, 0xa3, 0x84, 0x42, 0x86, 0x81, 0x00]),
99
+ // );
100
+ assert.ok(
101
+ encoder.buffer,
102
+ Buffer.from([0x1a, 0x45, 0xdf, 0xa3, 0x84, 0x42, 0x86, 0x81, 0x00]),
103
+ );
104
+ encoder.uncork();
105
+ encoder.flush();
106
+ expect(encoder.buffer, 'not to be a', Buffer);
107
+ });
108
+ });
109
+ describe('::getSchemaInfo', () => {
110
+ it('should return a valid number when a tag is found', () => {
111
+ assert.ok(Encoder.getSchemaInfo('EBMLVersion'), 0x4286);
112
+ });
113
+ it('should return null when not found', () => {
114
+ assert.strictEqual(Encoder.getSchemaInfo('404NotFound'), null);
115
+ });
116
+ });
117
+ describe('#writeTag', () => {
118
+ let encoder;
119
+ beforeAll(() => {
120
+ encoder = new Encoder();
121
+ });
122
+ it('does nothing with invalid tag data', () => {
123
+ encoder.writeTag('EBMLVersion', null);
124
+ expect(encoder.stack.length, 'to equal', 0);
125
+ });
126
+ it('throws with an invalid tag name', () => {
127
+ expect(
128
+ () => {
129
+ encoder.writeTag('404NotFound');
130
+ },
131
+ 'to throw',
132
+ /No schema entry found/,
133
+ );
134
+ });
135
+ });
136
+ describe('#startTag', () => {
137
+ let encoder;
138
+ beforeAll(() => {
139
+ encoder = new Encoder();
140
+ });
141
+ it('throws with an invalid tag name', () => {
142
+ expect(
143
+ () => {
144
+ encoder.startTag('404NotFound', { end: -1 });
145
+ },
146
+ 'to throw',
147
+ /No schema entry found/,
148
+ );
149
+ });
150
+ it('creates a valid tag when presented', () => {
151
+ encoder.startTag('ChapterTrackNumber', { end: -1 });
152
+ expect(encoder.stack, 'not to be empty')
153
+ .and('to have length', 1)
154
+ .and('to satisfy', [
155
+ {
156
+ data: expect.it('to be null'),
157
+ id: expect.it('to equal', 0x89),
158
+ name: expect.it('to equal', 'ChapterTrackNumber'),
159
+ children: expect.it('to be an array').and('to be empty'),
160
+ },
161
+ ]);
162
+ });
163
+ it('creates a valid tag when presented with a stack already present', () => {
164
+ encoder.stack = [
165
+ {
166
+ data: 1,
167
+ id: 0x89,
168
+ name: 'ChapterTrackNumber',
169
+ children: [],
170
+ },
171
+ ];
172
+ encoder.startTag('ChapterTimeStart', { end: 0x80 });
173
+ expect(encoder.stack[0].children, 'not to be empty').and(
174
+ 'to have length',
175
+ 1,
176
+ );
177
+ });
178
+ });
179
+ describe('#_transform', () => {
180
+ it('should do nothing on an invalid tag', () => {
181
+ const encoder = new Encoder();
182
+ encoder.write(['404NotFound', { name: 'EBML' }]);
183
+ expect(encoder.buffer, 'to be null');
184
+ });
185
+ });
186
+ describe('#_bufferAndFlush', () => {
187
+ /* eslint-disable no-underscore-dangle */
188
+ let encoder;
189
+ beforeEach(() => {
190
+ encoder = new Encoder();
191
+ });
192
+ it('should create a new buffer (but still be empty after eval) with an empty buffer', () => {
193
+ expect(encoder.buffer, 'to be null');
194
+ encoder._bufferAndFlush(Buffer.from([0x42, 0x86, 0x81, 0x01]));
195
+ expect(encoder.buffer, 'to be null');
196
+ });
197
+ it('should append to the buffer (and empty after eval) with an existing buffer', () => {
198
+ encoder.buffer = Buffer.from([0x42, 0x86, 0x81, 0x01]);
199
+ expect(encoder.buffer, 'to be a', Buffer);
200
+ encoder._bufferAndFlush(Buffer.from([0x42, 0x86, 0x81, 0x01]));
201
+ expect(encoder.buffer, 'to be null');
202
+ });
203
+ /* eslint-enable no-underscore-dangle */
204
+ });
205
+ });
206
+ });
package/src/index.js ADDED
@@ -0,0 +1,11 @@
1
+ const tools = require('./tools');
2
+ const schema = require('./schema');
3
+ const Decoder = require('./decoder');
4
+ const Encoder = require('./encoder');
5
+
6
+ module.exports = {
7
+ tools,
8
+ schema,
9
+ Decoder,
10
+ Encoder,
11
+ };