ebml.js 4.0.1 → 4.1.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ebml.js",
3
- "version": "4.0.1",
3
+ "version": "4.1.0",
4
4
  "description": "ebml parser",
5
5
  "main": "src/index.js",
6
6
  "maintainers": [
package/src/decoder.js CHANGED
@@ -1,301 +1,347 @@
1
- const { Transform } = require('stream');
2
- const tools = require('./tools');
3
- const schema = require('./schema');
4
- const { debugLog } = require('./debug-log');
5
-
6
- const debug = debugLog('ebml:decoder');
7
-
8
- const STATE_TAG = 1;
9
- const STATE_SIZE = 2;
10
- const STATE_CONTENT = 3;
11
-
12
- class EbmlDecoder extends Transform {
13
- /**
14
- * @property
15
- * @private
16
- * @type {Buffer}
17
- */
18
- mBuffer = null;
19
-
20
- /**
21
- * @private
22
- * @property
23
- * @readonly
24
- */
25
- mTagStack = [];
26
-
27
- /**
28
- * @property
29
- * @private
30
- * @type {Number}
31
- */
32
- mState = STATE_TAG;
33
-
34
- /**
35
- * @property
36
- * @private
37
- * @type {Number}
38
- */
39
- mCursor = 0;
40
-
41
- /**
42
- * @property
43
- * @private
44
- * @type {Number}
45
- */
46
- mTotal = 0;
47
-
48
- /**
49
- * @constructor
50
- * @param {Object} options The options to be passed along to the super class
51
- */
52
- constructor(options = {}) {
53
- super({ ...options, readableObjectMode: true });
54
- }
55
-
56
- get buffer() {
57
- return this.mBuffer;
58
- }
59
-
60
- get cursor() {
61
- return this.mCursor;
62
- }
63
-
64
- get state() {
65
- return this.mState;
66
- }
67
-
68
- get tagStack() {
69
- return this.mTagStack;
70
- }
71
-
72
- get total() {
73
- return this.mTotal;
74
- }
75
-
76
- set buffer(buffer) {
77
- this.mBuffer = buffer;
78
- }
79
-
80
- /**
81
- * @param {number} cursor
82
- */
83
- set cursor(cursor) {
84
- this.mCursor = cursor;
85
- }
86
-
87
- set state(state) {
88
- this.mState = state;
89
- }
90
-
91
- set total(total) {
92
- this.mTotal = total;
93
- }
94
-
95
- _transform(chunk, enc, done) {
96
- if (!this.buffer) {
97
- this.buffer = Buffer.from(chunk);
98
- } else {
99
- this.buffer = tools.concatenate(this.buffer, Buffer.from(chunk));
100
- }
101
-
102
- while (this.cursor < this.buffer.length) {
103
- if (this.state === STATE_TAG && !this.readTag()) {
104
- break;
105
- }
106
- if (this.state === STATE_SIZE && !this.readSize()) {
107
- break;
108
- }
109
- if (this.state === STATE_CONTENT && !this.readContent()) {
110
- break;
111
- }
112
- }
113
-
114
- done();
115
- }
116
-
117
- static getSchemaInfo(tag) {
118
- if (Number.isInteger(tag) && schema.has(tag)) {
119
- return schema.get(tag);
120
- }
121
- const tagStr = `0x${tag.toString(16).toUpperCase()}`
122
- const unknown = {
123
- type: null,
124
- name: `unknown-${tagStr}`,
125
- description: `${tagStr}`,
126
- level: -1,
127
- minver: -1,
128
- multiple: false,
129
- webm: false,
130
- };
131
- schema.set(tag, unknown)
132
- console.warn('[SCHEMA]', 'unknown tag:', tagStr)
133
- return unknown
134
- }
135
-
136
- readTag() {
137
- /* istanbul ignore if */
138
- if (debug.enabled) {
139
- debug('parsing tag');
140
- }
141
-
142
- if (this.cursor >= this.buffer.length) {
143
- /* istanbul ignore if */
144
- if (debug.enabled) {
145
- debug('waiting for more data');
146
- }
147
- return false;
148
- }
149
-
150
- const start = this.total;
151
- const tag = tools.readVint(this.buffer, this.cursor);
152
-
153
- if (tag == null) {
154
- /* istanbul ignore if */
155
- if (debug.enabled) {
156
- debug('waiting for more data');
157
- }
158
-
159
- return false;
160
- }
161
-
162
- const tagStr = tools.readHexString(
163
- this.buffer,
164
- this.cursor,
165
- this.cursor + tag.length,
166
- );
167
- const tagNum = Number.parseInt(tagStr, 16);
168
- this.cursor += tag.length;
169
- this.total += tag.length;
170
- this.state = STATE_SIZE;
171
-
172
- const tagObj = {
173
- tag: tag.value,
174
- tagStr,
175
- type: EbmlDecoder.getSchemaInfo(tagNum).type,
176
- name: EbmlDecoder.getSchemaInfo(tagNum).name,
177
- start,
178
- end: start + tag.length,
179
- };
180
-
181
- this.tagStack.push(tagObj);
182
- /* istanbul ignore if */
183
- if (debug.enabled) {
184
- debug(`read tag: ${tagStr}`);
185
- }
186
-
187
- return true;
188
- }
189
-
190
- readSize() {
191
- const tagObj = this.tagStack[this.tagStack.length - 1];
192
-
193
- /* istanbul ignore if */
194
- if (debug.enabled) {
195
- debug(`parsing size for tag: ${tagObj.tagStr}`);
196
- }
197
-
198
- if (this.cursor >= this.buffer.length) {
199
- /* istanbul ignore if */
200
- if (debug.enabled) {
201
- debug('waiting for more data');
202
- }
203
-
204
- return false;
205
- }
206
-
207
- const size = tools.readVint(this.buffer, this.cursor);
208
-
209
- if (size == null) {
210
- /* istanbul ignore if */
211
- if (debug.enabled) {
212
- debug('waiting for more data');
213
- }
214
-
215
- return false;
216
- }
217
-
218
- this.cursor += size.length;
219
- this.total += size.length;
220
- this.state = STATE_CONTENT;
221
- tagObj.dataSize = size.value;
222
-
223
- // unknown size
224
- if (size.value === -1) {
225
- tagObj.end = -1;
226
- } else {
227
- tagObj.end += size.value + size.length;
228
- }
229
- /* istanbul ignore if */
230
- if (debug.enabled) {
231
- debug(`read size: ${size.value}`);
232
- }
233
-
234
- return true;
235
- }
236
-
237
- readContent() {
238
- const { tagStr, type, dataSize, ...rest } = this.tagStack[this.tagStack.length - 1];
239
-
240
- /* istanbul ignore if */
241
- if (debug.enabled) {
242
- debug(`parsing content for tag: ${tagStr}`);
243
- }
244
-
245
- if (type === 'm') {
246
- /* istanbul ignore if */
247
- if (debug.enabled) {
248
- debug('content should be tags');
249
- }
250
- this.push(['start', { tagStr, type, dataSize, ...rest }]);
251
- this.state = STATE_TAG;
252
-
253
- return true;
254
- }
255
-
256
- if (this.buffer.length < this.cursor + dataSize) {
257
- /* istanbul ignore if */
258
- if (debug.enabled) {
259
- debug(`got: ${this.buffer.length}`);
260
- debug(`need: ${this.cursor + dataSize}`);
261
- debug('waiting for more data');
262
- }
263
-
264
- return false;
265
- }
266
-
267
- const data = this.buffer.subarray(this.cursor, this.cursor + dataSize);
268
- this.total += dataSize;
269
- this.state = STATE_TAG;
270
- this.buffer = this.buffer.subarray(this.cursor + dataSize);
271
- this.cursor = 0;
272
-
273
- this.tagStack.pop(); // remove the object from the stack
274
-
275
- this.push([
276
- 'tag',
277
- tools.readDataFromTag(
278
- { tagStr, type, dataSize, ...rest },
279
- Buffer.from(data),
280
- ),
281
- ]);
282
-
283
- while (this.tagStack.length > 0) {
284
- const topEle = this.tagStack[this.tagStack.length - 1];
285
- if (this.total < topEle.end) {
286
- break;
287
- }
288
- this.push(['end', topEle]);
289
- this.tagStack.pop();
290
- }
291
-
292
- /* istanbul ignore if */
293
- if (debug.enabled) {
294
- debug(`read data: ${data.toString('hex')}`);
295
- }
296
-
297
- return true;
298
- }
299
- }
300
-
301
- module.exports = EbmlDecoder
1
+ const { Transform } = require('stream');
2
+ const tools = require('./tools');
3
+ const schema = require('./schema');
4
+ const { debugLog } = require('./debug-log');
5
+
6
+ const debug = debugLog('ebml:decoder');
7
+
8
+ const STATE_TAG = 1;
9
+ const STATE_SIZE = 2;
10
+ const STATE_CONTENT = 3;
11
+
12
+ class EbmlDecoder extends Transform {
13
+ /**
14
+ * @property
15
+ * @private
16
+ * @type {Buffer}
17
+ */
18
+ mBuffer = null;
19
+
20
+ /**
21
+ * @private
22
+ * @property
23
+ * @readonly
24
+ */
25
+ mTagStack = [];
26
+
27
+ /**
28
+ * @property
29
+ * @private
30
+ * @type {Number}
31
+ */
32
+ mState = STATE_TAG;
33
+
34
+ /**
35
+ * @property
36
+ * @private
37
+ * @type {Number}
38
+ */
39
+ mCursor = 0;
40
+
41
+ /**
42
+ * @property
43
+ * @private
44
+ * @type {Number}
45
+ */
46
+ mTotal = 0;
47
+
48
+ /** @private */
49
+ mIsLive = false
50
+
51
+ /**
52
+ * @constructor
53
+ * @param {{ isLive?: boolean }} options The options to be passed along to the super class
54
+ */
55
+ constructor(options = {}) {
56
+ super({ ...options, readableObjectMode: true });
57
+ this.mIsLive = options ? options.isLive : false
58
+ }
59
+
60
+ get isLive() {
61
+ return this.mIsLive;
62
+ }
63
+
64
+ get buffer() {
65
+ return this.mBuffer;
66
+ }
67
+
68
+ get cursor() {
69
+ return this.mCursor;
70
+ }
71
+
72
+ get state() {
73
+ return this.mState;
74
+ }
75
+
76
+ get tagStack() {
77
+ return this.mTagStack;
78
+ }
79
+
80
+ get total() {
81
+ return this.mTotal;
82
+ }
83
+
84
+ set buffer(buffer) {
85
+ this.mBuffer = buffer;
86
+ }
87
+
88
+ /**
89
+ * @param {number} cursor
90
+ */
91
+ set cursor(cursor) {
92
+ this.mCursor = cursor;
93
+ }
94
+
95
+ set state(state) {
96
+ this.mState = state;
97
+ }
98
+
99
+ set total(total) {
100
+ this.mTotal = total;
101
+ }
102
+
103
+ _transform(chunk, enc, done) {
104
+ if (!this.buffer) {
105
+ this.buffer = Buffer.from(chunk);
106
+ } else {
107
+ this.buffer = tools.concatenate(this.buffer, Buffer.from(chunk));
108
+ }
109
+
110
+ while (this.cursor < this.buffer.length) {
111
+ if (this.state === STATE_TAG && !this.readTag()) {
112
+ break;
113
+ }
114
+ if (this.state === STATE_SIZE && !this.readSize()) {
115
+ break;
116
+ }
117
+ if (this.state === STATE_CONTENT && !this.readContent()) {
118
+ break;
119
+ }
120
+ }
121
+
122
+ done();
123
+ }
124
+
125
+ static getSchemaInfo(tag) {
126
+ if (Number.isInteger(tag) && schema.has(tag)) {
127
+ return schema.get(tag);
128
+ }
129
+ const tagStr = `0x${tag.toString(16).toUpperCase()}`
130
+ const unknown = {
131
+ type: null,
132
+ name: `unknown-${tagStr}`,
133
+ description: `${tagStr}`,
134
+ level: -1,
135
+ minver: -1,
136
+ multiple: false,
137
+ webm: false,
138
+ };
139
+ schema.set(tag, unknown)
140
+ console.warn('[SCHEMA]', 'unknown tag:', tagStr)
141
+ return unknown
142
+ }
143
+
144
+ readTag() {
145
+ /* istanbul ignore if */
146
+ if (debug.enabled) {
147
+ debug('parsing tag');
148
+ }
149
+
150
+ if (this.cursor >= this.buffer.length) {
151
+ /* istanbul ignore if */
152
+ if (debug.enabled) {
153
+ debug('waiting for more data');
154
+ }
155
+ return false;
156
+ }
157
+
158
+ if (this.isLive && !this.findTagStart()) {
159
+ return false
160
+ }
161
+
162
+ const start = this.total;
163
+ const tag = tools.readVint(this.buffer, this.cursor);
164
+
165
+ if (tag == null) {
166
+ /* istanbul ignore if */
167
+ if (debug.enabled) {
168
+ debug('waiting for more data');
169
+ }
170
+
171
+ return false;
172
+ }
173
+
174
+ const tagStr = tools.readHexString(
175
+ this.buffer,
176
+ this.cursor,
177
+ this.cursor + tag.length,
178
+ );
179
+ const tagNum = Number.parseInt(tagStr, 16);
180
+ this.cursor += tag.length;
181
+ this.total += tag.length;
182
+ this.state = STATE_SIZE;
183
+
184
+ const tagObj = {
185
+ tag: tag.value,
186
+ tagStr,
187
+ type: EbmlDecoder.getSchemaInfo(tagNum).type,
188
+ name: EbmlDecoder.getSchemaInfo(tagNum).name,
189
+ start,
190
+ end: start + tag.length,
191
+ };
192
+
193
+ this.tagStack.push(tagObj);
194
+ /* istanbul ignore if */
195
+ if (debug.enabled) {
196
+ debug(`read tag: ${tagStr}`);
197
+ }
198
+
199
+ return true;
200
+ }
201
+
202
+ /** @private */
203
+ findTagStart() {
204
+ while (this.cursor < this.buffer.length) {
205
+ try {
206
+ const tag = tools.readVint(this.buffer, this.cursor);
207
+ if (tag == null) {
208
+ return false
209
+ }
210
+ const tagStr = tools.readHexString(
211
+ this.buffer,
212
+ this.cursor,
213
+ this.cursor + tag.length,
214
+ );
215
+ const tagNum = Number.parseInt(tagStr, 16);
216
+ const tagName = EbmlDecoder.getSchemaInfo(tagNum).name
217
+ if (tagName && tagName.startsWith('unknown')) {
218
+ this.cursor += 1
219
+ continue
220
+ }
221
+ if (this.cursor > 0 && !this.total) {
222
+ this.buffer = this.buffer.subarray(this.cursor)
223
+ this.cursor = 0
224
+ }
225
+ return true
226
+ } catch (e) {
227
+ if (!e.message.startsWith('Unrepresentable length')) {
228
+ throw e
229
+ }
230
+ this.cursor += 1
231
+ }
232
+ }
233
+ return false
234
+ }
235
+
236
+ readSize() {
237
+ const tagObj = this.tagStack[this.tagStack.length - 1];
238
+
239
+ /* istanbul ignore if */
240
+ if (debug.enabled) {
241
+ debug(`parsing size for tag: ${tagObj.tagStr}`);
242
+ }
243
+
244
+ if (this.cursor >= this.buffer.length) {
245
+ /* istanbul ignore if */
246
+ if (debug.enabled) {
247
+ debug('waiting for more data');
248
+ }
249
+
250
+ return false;
251
+ }
252
+
253
+ const size = tools.readVint(this.buffer, this.cursor);
254
+
255
+ if (size == null) {
256
+ /* istanbul ignore if */
257
+ if (debug.enabled) {
258
+ debug('waiting for more data');
259
+ }
260
+
261
+ return false;
262
+ }
263
+
264
+ this.cursor += size.length;
265
+ this.total += size.length;
266
+ this.state = STATE_CONTENT;
267
+ tagObj.dataSize = size.value;
268
+
269
+ // unknown size
270
+ if (size.value === -1) {
271
+ tagObj.end = -1;
272
+ } else {
273
+ tagObj.end += size.value + size.length;
274
+ }
275
+ /* istanbul ignore if */
276
+ if (debug.enabled) {
277
+ debug(`read size: ${size.value}`);
278
+ }
279
+
280
+ return true;
281
+ }
282
+
283
+ readContent() {
284
+ const { tagStr, type, dataSize, ...rest } = this.tagStack[this.tagStack.length - 1];
285
+
286
+ /* istanbul ignore if */
287
+ if (debug.enabled) {
288
+ debug(`parsing content for tag: ${tagStr}`);
289
+ }
290
+
291
+ if (type === 'm') {
292
+ /* istanbul ignore if */
293
+ if (debug.enabled) {
294
+ debug('content should be tags');
295
+ }
296
+ this.push(['start', { tagStr, type, dataSize, ...rest }]);
297
+ this.state = STATE_TAG;
298
+
299
+ return true;
300
+ }
301
+
302
+ if (this.buffer.length < this.cursor + dataSize) {
303
+ /* istanbul ignore if */
304
+ if (debug.enabled) {
305
+ debug(`got: ${this.buffer.length}`);
306
+ debug(`need: ${this.cursor + dataSize}`);
307
+ debug('waiting for more data');
308
+ }
309
+
310
+ return false;
311
+ }
312
+
313
+ const data = this.buffer.subarray(this.cursor, this.cursor + dataSize);
314
+ this.total += dataSize;
315
+ this.state = STATE_TAG;
316
+ this.buffer = this.buffer.subarray(this.cursor + dataSize);
317
+ this.cursor = 0;
318
+
319
+ this.tagStack.pop(); // remove the object from the stack
320
+
321
+ this.push([
322
+ 'tag',
323
+ tools.readDataFromTag(
324
+ { tagStr, type, dataSize, ...rest },
325
+ Buffer.from(data),
326
+ ),
327
+ ]);
328
+
329
+ while (this.tagStack.length > 0) {
330
+ const topEle = this.tagStack[this.tagStack.length - 1];
331
+ if (this.total < topEle.end) {
332
+ break;
333
+ }
334
+ this.push(['end', topEle]);
335
+ this.tagStack.pop();
336
+ }
337
+
338
+ /* istanbul ignore if */
339
+ if (debug.enabled) {
340
+ debug(`read data: ${data.toString('hex')}`);
341
+ }
342
+
343
+ return true;
344
+ }
345
+ }
346
+
347
+ module.exports = EbmlDecoder