@allurereport/reader-api 3.0.0-beta.5 → 3.0.0-beta.7
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/dist/detect.d.ts +2 -0
- package/dist/detect.js +557 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/resultFile.d.ts +10 -3
- package/dist/resultFile.js +58 -5
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +23 -0
- package/package.json +3 -3
package/dist/detect.d.ts
ADDED
package/dist/detect.js
ADDED
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
const rawMagics = [
|
|
2
|
+
{
|
|
3
|
+
name: "video/mp4",
|
|
4
|
+
priority: 60,
|
|
5
|
+
clause: {
|
|
6
|
+
clause: "match",
|
|
7
|
+
value: "ftypmp42",
|
|
8
|
+
type: "string",
|
|
9
|
+
offset: "4",
|
|
10
|
+
},
|
|
11
|
+
extensions: [".mp4", ".mp4v", ".mpg4"],
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
name: "video/mp4",
|
|
15
|
+
priority: 60,
|
|
16
|
+
clause: {
|
|
17
|
+
clause: "match",
|
|
18
|
+
value: "ftypmp41",
|
|
19
|
+
type: "string",
|
|
20
|
+
offset: "4",
|
|
21
|
+
},
|
|
22
|
+
extensions: [".mp4", ".mp4v", ".mpg4"],
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: "image/svg+xml",
|
|
26
|
+
priority: 50,
|
|
27
|
+
clause: {
|
|
28
|
+
clause: "and",
|
|
29
|
+
nested: [
|
|
30
|
+
{
|
|
31
|
+
clause: "match",
|
|
32
|
+
value: "<svg",
|
|
33
|
+
type: "string",
|
|
34
|
+
offset: "0",
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
clause: "match",
|
|
38
|
+
value: "http://www.w3.org/2000/svg",
|
|
39
|
+
type: "string",
|
|
40
|
+
offset: "5:256",
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
extensions: [".svg", ".svgz"],
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: "image/png",
|
|
48
|
+
priority: 50,
|
|
49
|
+
clause: {
|
|
50
|
+
clause: "match",
|
|
51
|
+
value: "\\x89PNG\\x0d\\x0a\\x1a\\x0a",
|
|
52
|
+
type: "string",
|
|
53
|
+
offset: "0",
|
|
54
|
+
},
|
|
55
|
+
extensions: [".png"],
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: "application/x-gtar",
|
|
59
|
+
priority: 50,
|
|
60
|
+
clause: {
|
|
61
|
+
clause: "match",
|
|
62
|
+
value: "ustar \\0",
|
|
63
|
+
type: "string",
|
|
64
|
+
offset: "257",
|
|
65
|
+
},
|
|
66
|
+
extensions: [".gtar"],
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: "application/pdf",
|
|
70
|
+
priority: 50,
|
|
71
|
+
clause: {
|
|
72
|
+
clause: "match",
|
|
73
|
+
value: "\\xef\\xbb\\xbf%PDF-",
|
|
74
|
+
type: "string",
|
|
75
|
+
offset: "0",
|
|
76
|
+
},
|
|
77
|
+
extensions: [".pdf"],
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
name: "image/gif",
|
|
81
|
+
priority: 50,
|
|
82
|
+
clause: {
|
|
83
|
+
clause: "match",
|
|
84
|
+
value: "GIF89a",
|
|
85
|
+
type: "string",
|
|
86
|
+
offset: "0",
|
|
87
|
+
},
|
|
88
|
+
extensions: [".gif"],
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
name: "image/gif",
|
|
92
|
+
priority: 50,
|
|
93
|
+
clause: {
|
|
94
|
+
clause: "match",
|
|
95
|
+
value: "GIF87a",
|
|
96
|
+
type: "string",
|
|
97
|
+
offset: "0",
|
|
98
|
+
},
|
|
99
|
+
extensions: [".gif"],
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
name: "image/bmp",
|
|
103
|
+
priority: 50,
|
|
104
|
+
clause: {
|
|
105
|
+
clause: "and",
|
|
106
|
+
nested: [
|
|
107
|
+
{
|
|
108
|
+
clause: "match",
|
|
109
|
+
value: "BM",
|
|
110
|
+
type: "string",
|
|
111
|
+
offset: "0",
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
clause: "and",
|
|
115
|
+
nested: [
|
|
116
|
+
{
|
|
117
|
+
clause: "match",
|
|
118
|
+
value: "0x0100",
|
|
119
|
+
type: "string",
|
|
120
|
+
offset: "26",
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
clause: "or",
|
|
124
|
+
nested: [
|
|
125
|
+
{
|
|
126
|
+
clause: "match",
|
|
127
|
+
value: "0x0000",
|
|
128
|
+
type: "string",
|
|
129
|
+
offset: "28",
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
clause: "match",
|
|
133
|
+
value: "0x0100",
|
|
134
|
+
type: "string",
|
|
135
|
+
offset: "28",
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
clause: "match",
|
|
139
|
+
value: "0x0400",
|
|
140
|
+
type: "string",
|
|
141
|
+
offset: "28",
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
clause: "match",
|
|
145
|
+
value: "0x0800",
|
|
146
|
+
type: "string",
|
|
147
|
+
offset: "28",
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
clause: "match",
|
|
151
|
+
value: "0x1000",
|
|
152
|
+
type: "string",
|
|
153
|
+
offset: "28",
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
clause: "match",
|
|
157
|
+
value: "0x1800",
|
|
158
|
+
type: "string",
|
|
159
|
+
offset: "28",
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
clause: "match",
|
|
163
|
+
value: "0x2000",
|
|
164
|
+
type: "string",
|
|
165
|
+
offset: "28",
|
|
166
|
+
},
|
|
167
|
+
],
|
|
168
|
+
},
|
|
169
|
+
],
|
|
170
|
+
},
|
|
171
|
+
],
|
|
172
|
+
},
|
|
173
|
+
extensions: [".bmp", ".dib"],
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
name: "application/pdf",
|
|
177
|
+
priority: 50,
|
|
178
|
+
clause: {
|
|
179
|
+
clause: "match",
|
|
180
|
+
value: "%PDF-",
|
|
181
|
+
type: "string",
|
|
182
|
+
offset: "0",
|
|
183
|
+
},
|
|
184
|
+
extensions: [".pdf"],
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
name: "image/tiff",
|
|
188
|
+
priority: 50,
|
|
189
|
+
clause: {
|
|
190
|
+
clause: "match",
|
|
191
|
+
value: "MM\\x00\\x2b",
|
|
192
|
+
type: "string",
|
|
193
|
+
offset: "0",
|
|
194
|
+
},
|
|
195
|
+
extensions: [".tiff", ".tif"],
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
name: "image/tiff",
|
|
199
|
+
priority: 50,
|
|
200
|
+
clause: {
|
|
201
|
+
clause: "match",
|
|
202
|
+
value: "MM\\x00\\x2a",
|
|
203
|
+
type: "string",
|
|
204
|
+
offset: "0",
|
|
205
|
+
},
|
|
206
|
+
extensions: [".tiff", ".tif"],
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
name: "image/tiff",
|
|
210
|
+
priority: 50,
|
|
211
|
+
clause: {
|
|
212
|
+
clause: "match",
|
|
213
|
+
value: "II\\x2a\\x00",
|
|
214
|
+
type: "string",
|
|
215
|
+
offset: "0",
|
|
216
|
+
},
|
|
217
|
+
extensions: [".tiff", ".tif"],
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
name: "application/zip",
|
|
221
|
+
priority: 50,
|
|
222
|
+
clause: {
|
|
223
|
+
clause: "match",
|
|
224
|
+
value: "PK\\x07\\x08",
|
|
225
|
+
type: "string",
|
|
226
|
+
offset: "0",
|
|
227
|
+
},
|
|
228
|
+
extensions: [".zip"],
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
name: "application/zip",
|
|
232
|
+
priority: 50,
|
|
233
|
+
clause: {
|
|
234
|
+
clause: "match",
|
|
235
|
+
value: "PK\\005\\006",
|
|
236
|
+
type: "string",
|
|
237
|
+
offset: "0",
|
|
238
|
+
},
|
|
239
|
+
extensions: [".zip"],
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
name: "application/zip",
|
|
243
|
+
priority: 50,
|
|
244
|
+
clause: {
|
|
245
|
+
clause: "match",
|
|
246
|
+
value: "PK\\003\\004",
|
|
247
|
+
type: "string",
|
|
248
|
+
offset: "0",
|
|
249
|
+
},
|
|
250
|
+
extensions: [".zip"],
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
name: "image/jpeg",
|
|
254
|
+
priority: 50,
|
|
255
|
+
clause: {
|
|
256
|
+
clause: "match",
|
|
257
|
+
value: "0xffd8ff",
|
|
258
|
+
type: "string",
|
|
259
|
+
offset: "0",
|
|
260
|
+
},
|
|
261
|
+
extensions: [".jpg", ".jpeg", ".jpe", ".jif", ".jfif", ".jfi"],
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
name: "application/gzip",
|
|
265
|
+
priority: 45,
|
|
266
|
+
clause: {
|
|
267
|
+
clause: "match",
|
|
268
|
+
value: "\\x1f\\x8b",
|
|
269
|
+
type: "string",
|
|
270
|
+
offset: "0",
|
|
271
|
+
},
|
|
272
|
+
extensions: [".gz", ".tgz", "-gz"],
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
name: "application/gzip",
|
|
276
|
+
priority: 45,
|
|
277
|
+
clause: {
|
|
278
|
+
clause: "match",
|
|
279
|
+
value: "\\037\\213",
|
|
280
|
+
type: "string",
|
|
281
|
+
offset: "0",
|
|
282
|
+
},
|
|
283
|
+
extensions: [".gz", ".tgz", "-gz"],
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
name: "application/pdf",
|
|
287
|
+
priority: 40,
|
|
288
|
+
clause: {
|
|
289
|
+
clause: "and",
|
|
290
|
+
nested: [
|
|
291
|
+
{
|
|
292
|
+
clause: "match",
|
|
293
|
+
value: "%%",
|
|
294
|
+
type: "string",
|
|
295
|
+
offset: "0:128",
|
|
296
|
+
},
|
|
297
|
+
{
|
|
298
|
+
clause: "match",
|
|
299
|
+
value: "%PDF-2.",
|
|
300
|
+
type: "string",
|
|
301
|
+
offset: "1:512",
|
|
302
|
+
},
|
|
303
|
+
],
|
|
304
|
+
},
|
|
305
|
+
extensions: [".pdf"],
|
|
306
|
+
},
|
|
307
|
+
{
|
|
308
|
+
name: "application/pdf",
|
|
309
|
+
priority: 40,
|
|
310
|
+
clause: {
|
|
311
|
+
clause: "and",
|
|
312
|
+
nested: [
|
|
313
|
+
{
|
|
314
|
+
clause: "match",
|
|
315
|
+
value: "%%",
|
|
316
|
+
type: "string",
|
|
317
|
+
offset: "0:128",
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
clause: "match",
|
|
321
|
+
value: "%PDF-1.",
|
|
322
|
+
type: "string",
|
|
323
|
+
offset: "1:512",
|
|
324
|
+
},
|
|
325
|
+
],
|
|
326
|
+
},
|
|
327
|
+
extensions: [".pdf"],
|
|
328
|
+
},
|
|
329
|
+
{
|
|
330
|
+
name: "application/x-tar",
|
|
331
|
+
priority: 40,
|
|
332
|
+
clause: {
|
|
333
|
+
clause: "match",
|
|
334
|
+
value: "ustar\\0",
|
|
335
|
+
type: "string",
|
|
336
|
+
offset: "257",
|
|
337
|
+
},
|
|
338
|
+
extensions: [".tar"],
|
|
339
|
+
},
|
|
340
|
+
{
|
|
341
|
+
name: "application/pdf",
|
|
342
|
+
priority: 20,
|
|
343
|
+
clause: {
|
|
344
|
+
clause: "match",
|
|
345
|
+
value: "%PDF-2.",
|
|
346
|
+
type: "string",
|
|
347
|
+
offset: "1:512",
|
|
348
|
+
},
|
|
349
|
+
extensions: [".pdf"],
|
|
350
|
+
},
|
|
351
|
+
{
|
|
352
|
+
name: "application/pdf",
|
|
353
|
+
priority: 20,
|
|
354
|
+
clause: {
|
|
355
|
+
clause: "match",
|
|
356
|
+
value: "%PDF-1.",
|
|
357
|
+
type: "string",
|
|
358
|
+
offset: "1:512",
|
|
359
|
+
},
|
|
360
|
+
extensions: [".pdf"],
|
|
361
|
+
},
|
|
362
|
+
];
|
|
363
|
+
const decodeString = (value, type) => {
|
|
364
|
+
if (value.startsWith("0x")) {
|
|
365
|
+
const vals = new Uint8Array((value.length - 2) / 2);
|
|
366
|
+
for (let i = 0; i < vals.length; i++) {
|
|
367
|
+
vals[i] = parseInt(value.substring(2 + i * 2, 4 + i * 2), 16);
|
|
368
|
+
}
|
|
369
|
+
return vals;
|
|
370
|
+
}
|
|
371
|
+
const decoded = [];
|
|
372
|
+
for (let i = 0; i < value.length; i++) {
|
|
373
|
+
if (value[i] === "\\") {
|
|
374
|
+
if (value[i + 1] === "\\") {
|
|
375
|
+
decoded.push(92);
|
|
376
|
+
i++;
|
|
377
|
+
}
|
|
378
|
+
else if (value[i + 1] === "x") {
|
|
379
|
+
decoded.push(parseInt(value.substring(i + 2, i + 4), 16));
|
|
380
|
+
i += 3;
|
|
381
|
+
}
|
|
382
|
+
else if (value[i + 1] === "r") {
|
|
383
|
+
decoded.push(13);
|
|
384
|
+
i++;
|
|
385
|
+
}
|
|
386
|
+
else if (value[i + 1] === "n") {
|
|
387
|
+
decoded.push(10);
|
|
388
|
+
i++;
|
|
389
|
+
}
|
|
390
|
+
else {
|
|
391
|
+
let j = i + 1;
|
|
392
|
+
while (j < i + 4 && j < value.length && /\d/.test(value[j])) {
|
|
393
|
+
j++;
|
|
394
|
+
}
|
|
395
|
+
decoded.push(parseInt(`0${value.substring(i + 1, j)}`, 10));
|
|
396
|
+
i = j - 1;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
decoded.push(value.charCodeAt(i));
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
const chars = decoded;
|
|
404
|
+
let bytes;
|
|
405
|
+
if (type === "unicodeLE") {
|
|
406
|
+
bytes = new Uint8Array(chars.length * 2);
|
|
407
|
+
for (let i = 0; i < chars.length; i++) {
|
|
408
|
+
bytes[i * 2] = chars[i] & 0xff;
|
|
409
|
+
bytes[i * 2 + 1] = chars[i] >> 8;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
else if (type === "unicodeBE") {
|
|
413
|
+
bytes = new Uint8Array(chars.length * 2);
|
|
414
|
+
for (let i = 0; i < chars.length; i++) {
|
|
415
|
+
bytes[i * 2] = chars[i] >> 8;
|
|
416
|
+
bytes[i * 2 + 1] = chars[i] & 0xff;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
else {
|
|
420
|
+
bytes = new Uint8Array(chars.length);
|
|
421
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
422
|
+
bytes[i] = chars[i];
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return bytes;
|
|
426
|
+
};
|
|
427
|
+
class AndClause {
|
|
428
|
+
constructor(clauses) {
|
|
429
|
+
this.clauses = clauses;
|
|
430
|
+
}
|
|
431
|
+
eval(data) {
|
|
432
|
+
for (const clause of this.clauses) {
|
|
433
|
+
if (!clause.eval(data)) {
|
|
434
|
+
return false;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
return true;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
class OrClause {
|
|
441
|
+
constructor(clauses) {
|
|
442
|
+
this.clauses = clauses;
|
|
443
|
+
}
|
|
444
|
+
eval(data) {
|
|
445
|
+
for (const clause of this.clauses) {
|
|
446
|
+
if (!clause.eval(data)) {
|
|
447
|
+
return true;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return false;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
class MagicMatchClause {
|
|
454
|
+
constructor(pattern, mask, start = 0, end = start) {
|
|
455
|
+
this.pattern = pattern;
|
|
456
|
+
this.mask = mask;
|
|
457
|
+
this.start = start;
|
|
458
|
+
this.end = end;
|
|
459
|
+
this.eval = (data) => {
|
|
460
|
+
if (data.length < this.pattern.length + this.start) {
|
|
461
|
+
return false;
|
|
462
|
+
}
|
|
463
|
+
for (let i = this.start; i <= this.end; i++) {
|
|
464
|
+
let match = true;
|
|
465
|
+
for (let j = 0; match && j < this.pattern.length; j++) {
|
|
466
|
+
const masked = data[i + j] & this.mask[j];
|
|
467
|
+
match = masked === this.pattern[j];
|
|
468
|
+
}
|
|
469
|
+
if (match) {
|
|
470
|
+
return true;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return false;
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
const parseClause = (rawClause) => {
|
|
478
|
+
if (rawClause.clause === "and") {
|
|
479
|
+
return new AndClause(rawClause.nested.map((nested) => parseClause(nested)));
|
|
480
|
+
}
|
|
481
|
+
if (rawClause.clause === "or") {
|
|
482
|
+
return new OrClause(rawClause.nested.map((nested) => parseClause(nested)));
|
|
483
|
+
}
|
|
484
|
+
return createMagicMatch(rawClause.value, rawClause.mask, rawClause.offset, rawClause.type);
|
|
485
|
+
};
|
|
486
|
+
const parseMagics = (data) => {
|
|
487
|
+
return data.map((m) => {
|
|
488
|
+
return {
|
|
489
|
+
priority: m.priority,
|
|
490
|
+
type: m.name,
|
|
491
|
+
extensions: m.extensions,
|
|
492
|
+
clause: parseClause(m.clause),
|
|
493
|
+
};
|
|
494
|
+
});
|
|
495
|
+
};
|
|
496
|
+
const createMagicMatch = (value, mask, offset, type = "string") => {
|
|
497
|
+
const decodedValue = decodeString(value, type);
|
|
498
|
+
const decodedMask = mask ? decodeString(value, type) : undefined;
|
|
499
|
+
const patternLength = Math.max(decodedValue.length, decodedMask?.length ?? 0);
|
|
500
|
+
const resPattern = new Uint8Array(patternLength);
|
|
501
|
+
const resMask = new Uint8Array(patternLength);
|
|
502
|
+
for (let i = 0; i < patternLength; i++) {
|
|
503
|
+
if (decodedMask && i < decodedMask.length) {
|
|
504
|
+
resMask[i] = decodedMask[i];
|
|
505
|
+
}
|
|
506
|
+
else {
|
|
507
|
+
resMask[i] = -1;
|
|
508
|
+
}
|
|
509
|
+
if (i < decodedValue.length) {
|
|
510
|
+
resPattern[i] = decodedValue[i] & resMask[i];
|
|
511
|
+
}
|
|
512
|
+
else {
|
|
513
|
+
resPattern[i] = 0;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
let start = 0;
|
|
517
|
+
let end = 0;
|
|
518
|
+
if (offset) {
|
|
519
|
+
if (offset.indexOf(":") > 0) {
|
|
520
|
+
const [startRaw, endRaw] = offset.split(":");
|
|
521
|
+
start = +startRaw;
|
|
522
|
+
end = +endRaw;
|
|
523
|
+
}
|
|
524
|
+
else {
|
|
525
|
+
start = +offset;
|
|
526
|
+
end = start;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return new MagicMatchClause(resPattern, resMask, start, end);
|
|
530
|
+
};
|
|
531
|
+
const magics = parseMagics(rawMagics);
|
|
532
|
+
const detectContentType = (buffer) => {
|
|
533
|
+
if (buffer.length === 0) {
|
|
534
|
+
return undefined;
|
|
535
|
+
}
|
|
536
|
+
let result = [];
|
|
537
|
+
let currentPriority = -1;
|
|
538
|
+
for (const magic of magics) {
|
|
539
|
+
if (currentPriority > 0 && currentPriority > magic.priority) {
|
|
540
|
+
break;
|
|
541
|
+
}
|
|
542
|
+
if (magic.clause.eval(buffer)) {
|
|
543
|
+
if (currentPriority === magic.priority) {
|
|
544
|
+
result.push(magic.type);
|
|
545
|
+
}
|
|
546
|
+
else {
|
|
547
|
+
result = [magic.type];
|
|
548
|
+
currentPriority = magic.priority;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
if (result.length > 0) {
|
|
553
|
+
return result[0];
|
|
554
|
+
}
|
|
555
|
+
return undefined;
|
|
556
|
+
};
|
|
557
|
+
export { detectContentType };
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/resultFile.d.ts
CHANGED
|
@@ -3,12 +3,17 @@ import { ReadStream } from "node:fs";
|
|
|
3
3
|
import "node:fs/promises";
|
|
4
4
|
import type { Readable } from "node:stream";
|
|
5
5
|
export declare abstract class BaseResultFile implements ResultFile {
|
|
6
|
+
#private;
|
|
6
7
|
fileName: string;
|
|
8
|
+
extension: string | false;
|
|
9
|
+
contentType: string | undefined | false;
|
|
7
10
|
protected constructor(fileName: string);
|
|
8
11
|
protected abstract getContent(): ReadStream | undefined;
|
|
12
|
+
protected abstract readMagicHeader(): Uint8Array | undefined;
|
|
9
13
|
abstract getContentLength(): number | undefined;
|
|
10
14
|
getContentType(): string | undefined;
|
|
11
15
|
getOriginalFileName(): string;
|
|
16
|
+
getExtension(): string;
|
|
12
17
|
asJson<T>(): Promise<T | undefined>;
|
|
13
18
|
asUtf8String(): Promise<string | undefined>;
|
|
14
19
|
asBuffer(): Promise<Buffer | undefined>;
|
|
@@ -16,15 +21,17 @@ export declare abstract class BaseResultFile implements ResultFile {
|
|
|
16
21
|
readContent<T>(transform: (stream: ReadStream) => Promise<T | undefined>): Promise<T | undefined>;
|
|
17
22
|
}
|
|
18
23
|
export declare class BufferResultFile extends BaseResultFile {
|
|
19
|
-
buffer:
|
|
20
|
-
constructor(buffer:
|
|
21
|
-
protected getContent(): ReadStream
|
|
24
|
+
buffer: Uint8Array;
|
|
25
|
+
constructor(buffer: Uint8Array, fileName: string);
|
|
26
|
+
protected getContent(): ReadStream;
|
|
27
|
+
protected readMagicHeader(): Uint8Array;
|
|
22
28
|
getContentLength(): number | undefined;
|
|
23
29
|
}
|
|
24
30
|
export declare class PathResultFile extends BaseResultFile {
|
|
25
31
|
path: string;
|
|
26
32
|
constructor(path: string, fileName?: string);
|
|
27
33
|
protected getContent(): ReadStream | undefined;
|
|
34
|
+
protected readMagicHeader(): Uint8Array | undefined;
|
|
28
35
|
getContentLength(): number | undefined;
|
|
29
36
|
}
|
|
30
37
|
export declare const readSteamToJson: <T>(stream: Readable) => Promise<T | undefined>;
|
package/dist/resultFile.js
CHANGED
|
@@ -1,22 +1,38 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var _BaseResultFile_instances, _BaseResultFile_detectContentType;
|
|
1
7
|
import { lookup } from "mime-types";
|
|
2
|
-
import { ReadStream, createReadStream, createWriteStream, existsSync, statSync } from "node:fs";
|
|
8
|
+
import { ReadStream, closeSync, createReadStream, createWriteStream, existsSync, openSync, readSync, statSync, } from "node:fs";
|
|
3
9
|
import "node:fs/promises";
|
|
4
10
|
import { basename } from "node:path";
|
|
5
11
|
import { pipeline } from "node:stream/promises";
|
|
12
|
+
import { detectContentType } from "./detect.js";
|
|
13
|
+
import { extension } from "./utils.js";
|
|
6
14
|
export class BaseResultFile {
|
|
7
15
|
constructor(fileName) {
|
|
16
|
+
_BaseResultFile_instances.add(this);
|
|
17
|
+
this.extension = false;
|
|
18
|
+
this.contentType = false;
|
|
8
19
|
this.fileName = fileName;
|
|
9
20
|
}
|
|
10
21
|
getContentType() {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return undefined;
|
|
22
|
+
if (this.contentType === false) {
|
|
23
|
+
this.contentType = __classPrivateFieldGet(this, _BaseResultFile_instances, "m", _BaseResultFile_detectContentType).call(this);
|
|
14
24
|
}
|
|
15
|
-
return
|
|
25
|
+
return this.contentType;
|
|
16
26
|
}
|
|
17
27
|
getOriginalFileName() {
|
|
18
28
|
return this.fileName;
|
|
19
29
|
}
|
|
30
|
+
getExtension() {
|
|
31
|
+
if (this.extension === false) {
|
|
32
|
+
this.extension = extension(this.getOriginalFileName()) ?? extension("", this.getContentType()) ?? "";
|
|
33
|
+
}
|
|
34
|
+
return this.extension;
|
|
35
|
+
}
|
|
20
36
|
async asJson() {
|
|
21
37
|
return await this.readContent(readSteamToJson);
|
|
22
38
|
}
|
|
@@ -36,6 +52,18 @@ export class BaseResultFile {
|
|
|
36
52
|
return content ? await transform(content) : undefined;
|
|
37
53
|
}
|
|
38
54
|
}
|
|
55
|
+
_BaseResultFile_instances = new WeakSet(), _BaseResultFile_detectContentType = function _BaseResultFile_detectContentType() {
|
|
56
|
+
const res = lookup(this.getOriginalFileName());
|
|
57
|
+
if (res === false) {
|
|
58
|
+
const magicHeader = this.readMagicHeader();
|
|
59
|
+
if (!magicHeader) {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
return detectContentType(magicHeader);
|
|
63
|
+
}
|
|
64
|
+
return res;
|
|
65
|
+
};
|
|
66
|
+
const magicHeaderLength = 1024;
|
|
39
67
|
export class BufferResultFile extends BaseResultFile {
|
|
40
68
|
constructor(buffer, fileName) {
|
|
41
69
|
super(basename(fileName));
|
|
@@ -44,6 +72,9 @@ export class BufferResultFile extends BaseResultFile {
|
|
|
44
72
|
getContent() {
|
|
45
73
|
return ReadStream.from(this.buffer, { encoding: "utf8" });
|
|
46
74
|
}
|
|
75
|
+
readMagicHeader() {
|
|
76
|
+
return this.buffer.subarray(0, magicHeaderLength);
|
|
77
|
+
}
|
|
47
78
|
getContentLength() {
|
|
48
79
|
return this.buffer.length;
|
|
49
80
|
}
|
|
@@ -61,6 +92,28 @@ export class PathResultFile extends BaseResultFile {
|
|
|
61
92
|
return undefined;
|
|
62
93
|
}
|
|
63
94
|
}
|
|
95
|
+
readMagicHeader() {
|
|
96
|
+
if (existsSync(this.path)) {
|
|
97
|
+
const buf = new Uint8Array(magicHeaderLength);
|
|
98
|
+
const fd = openSync(this.path, "r");
|
|
99
|
+
try {
|
|
100
|
+
const size = readSync(fd, buf, 0, magicHeaderLength, null);
|
|
101
|
+
if (size === 0) {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
if (size < magicHeaderLength) {
|
|
105
|
+
return buf.subarray(0, size);
|
|
106
|
+
}
|
|
107
|
+
return buf;
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
closeSync(fd);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
64
117
|
getContentLength() {
|
|
65
118
|
return statSync(this.path, { throwIfNoEntry: false })?.size;
|
|
66
119
|
}
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { extension as extensionFromContentType, lookup } from "mime-types";
|
|
2
|
+
import { extname } from "node:path";
|
|
3
|
+
export const extension = (fileName, contentType) => {
|
|
4
|
+
const ext = extname(fileName);
|
|
5
|
+
if (ext !== "") {
|
|
6
|
+
return ext;
|
|
7
|
+
}
|
|
8
|
+
if (contentType) {
|
|
9
|
+
const result = extensionFromContentType(contentType);
|
|
10
|
+
if (result === false) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
return `.${result}`;
|
|
14
|
+
}
|
|
15
|
+
return undefined;
|
|
16
|
+
};
|
|
17
|
+
export const lookupContentType = (fileName) => {
|
|
18
|
+
const res = lookup(fileName);
|
|
19
|
+
if (res === false) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
return res;
|
|
23
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@allurereport/reader-api",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.7",
|
|
4
4
|
"description": "Allure Reader API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"allure",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"test": "rimraf ./out && vitest run"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@allurereport/core-api": "3.0.0-beta.
|
|
31
|
-
"@allurereport/plugin-api": "3.0.0-beta.
|
|
30
|
+
"@allurereport/core-api": "3.0.0-beta.7",
|
|
31
|
+
"@allurereport/plugin-api": "3.0.0-beta.7",
|
|
32
32
|
"mime-types": "^2.1.35"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|