@allurereport/reader-api 3.0.0-beta.10

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/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # Reader API
2
+
3
+ [<img src="https://allurereport.org/public/img/allure-report.svg" height="85px" alt="Allure Report logo" align="right" />](https://allurereport.org "Allure Report")
4
+
5
+ - Learn more about Allure Report at https://allurereport.org
6
+ - 📚 [Documentation](https://allurereport.org/docs/) – discover official documentation for Allure Report
7
+ - ❓ [Questions and Support](https://github.com/orgs/allure-framework/discussions/categories/questions-support) – get help from the team and community
8
+ - 📢 [Official announcements](https://github.com/orgs/allure-framework/discussions/categories/announcements) – be in touch with the latest updates
9
+ - 💬 [General Discussion ](https://github.com/orgs/allure-framework/discussions/categories/general-discussion) – engage in casual conversations, share insights and ideas with the community
10
+
11
+ ---
12
+
13
+ ## Overview
14
+
15
+ The interfaces in the package describe the entities used for building Allure Readers.
16
+
17
+ ## Install
18
+
19
+ Use your favorite package manager to install the package:
20
+
21
+ ```shell
22
+ npm add @allurereport/reader-api
23
+ yarn add @allurereport/reader-api
24
+ pnpm add @allurereport/reader-api
25
+ ```
@@ -0,0 +1,2 @@
1
+ declare const detectContentType: (buffer: Uint8Array) => string | undefined;
2
+ export { detectContentType };
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 };
@@ -0,0 +1,5 @@
1
+ export type * from "./model.js";
2
+ export type * from "./reader.js";
3
+ export * from "./resultFile.js";
4
+ export { detectContentType } from "./detect.js";
5
+ export { extension, lookupContentType } from "./utils.js";
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./resultFile.js";
2
+ export { detectContentType } from "./detect.js";
3
+ export { extension, lookupContentType } from "./utils.js";
@@ -0,0 +1,92 @@
1
+ export type RawTestStatus = "failed" | "broken" | "passed" | "skipped" | "unknown";
2
+ export type RawMetadata = {
3
+ [key: string]: any;
4
+ };
5
+ export interface RawFixtureResult {
6
+ uuid?: string;
7
+ testResults?: string[];
8
+ type: "before" | "after";
9
+ name?: string;
10
+ start?: number;
11
+ stop?: number;
12
+ duration?: number;
13
+ status?: RawTestStatus;
14
+ message?: string;
15
+ trace?: string;
16
+ expected?: string;
17
+ actual?: string;
18
+ steps?: RawStep[];
19
+ }
20
+ export interface RawTestResult {
21
+ uuid?: string;
22
+ name?: string;
23
+ status?: RawTestStatus;
24
+ fullName?: string;
25
+ testId?: string;
26
+ testCaseName?: string;
27
+ historyId?: string;
28
+ description?: string;
29
+ descriptionHtml?: string;
30
+ precondition?: string;
31
+ preconditionHtml?: string;
32
+ expectedResult?: string;
33
+ expectedResultHtml?: string;
34
+ start?: number;
35
+ stop?: number;
36
+ duration?: number;
37
+ message?: string;
38
+ trace?: string;
39
+ expected?: string;
40
+ actual?: string;
41
+ flaky?: boolean;
42
+ muted?: boolean;
43
+ known?: boolean;
44
+ hostId?: string;
45
+ threadId?: string;
46
+ parameters?: RawTestParameter[];
47
+ steps?: RawStep[];
48
+ labels?: RawTestLabel[];
49
+ links?: RawTestLink[];
50
+ }
51
+ export interface RawTestLabel {
52
+ name?: string;
53
+ value?: string;
54
+ }
55
+ export interface RawTestLink {
56
+ name?: string;
57
+ url?: string;
58
+ type?: string;
59
+ }
60
+ export interface RawTestParameter {
61
+ name?: string;
62
+ value?: string;
63
+ hidden?: boolean;
64
+ excluded?: boolean;
65
+ masked?: boolean;
66
+ }
67
+ export type RawStep = RawTestStepResult | RawTestAttachment;
68
+ export interface RawTestStepResult {
69
+ name?: string;
70
+ status?: RawTestStatus;
71
+ message?: string;
72
+ trace?: string;
73
+ actual?: string;
74
+ expected?: string;
75
+ start?: number;
76
+ stop?: number;
77
+ duration?: number;
78
+ parameters?: RawTestParameter[];
79
+ steps?: RawStep[];
80
+ type: "step";
81
+ }
82
+ export interface RawTestAttachment {
83
+ name?: string;
84
+ originalFileName?: string;
85
+ contentType?: string;
86
+ contentLength?: number;
87
+ optional?: boolean;
88
+ start?: number;
89
+ stop?: number;
90
+ duration?: number;
91
+ type: "attachment";
92
+ }
package/dist/model.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ import type { ResultFile } from "@allurereport/plugin-api";
2
+ import type { RawFixtureResult, RawMetadata, RawTestResult } from "./model.js";
3
+ export interface ReaderContext {
4
+ readerId: string;
5
+ metadata?: RawMetadata;
6
+ }
7
+ export interface ResultsVisitor {
8
+ visitTestResult(result: RawTestResult, context: ReaderContext): Promise<void>;
9
+ visitTestFixtureResult(result: RawFixtureResult, context: ReaderContext): Promise<void>;
10
+ visitAttachmentFile(result: ResultFile, context: ReaderContext): Promise<void>;
11
+ visitMetadata(metadata: RawMetadata, context: ReaderContext): Promise<void>;
12
+ }
13
+ export interface ResultsReader {
14
+ read(visitor: ResultsVisitor, data: ResultFile): Promise<boolean>;
15
+ readerId(): string;
16
+ }
package/dist/reader.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,39 @@
1
+ import type { ResultFile } from "@allurereport/plugin-api";
2
+ import { ReadStream } from "node:fs";
3
+ import "node:fs/promises";
4
+ import type { Readable } from "node:stream";
5
+ export declare abstract class BaseResultFile implements ResultFile {
6
+ #private;
7
+ fileName: string;
8
+ extension: string | false;
9
+ contentType: string | undefined | false;
10
+ protected constructor(fileName: string);
11
+ protected abstract getContent(): ReadStream | undefined;
12
+ protected abstract readMagicHeader(): Uint8Array | undefined;
13
+ abstract getContentLength(): number | undefined;
14
+ getContentType(): string | undefined;
15
+ getOriginalFileName(): string;
16
+ getExtension(): string;
17
+ asJson<T>(): Promise<T | undefined>;
18
+ asUtf8String(): Promise<string | undefined>;
19
+ asBuffer(): Promise<Buffer | undefined>;
20
+ writeTo(path: string): Promise<void>;
21
+ readContent<T>(transform: (stream: ReadStream) => Promise<T | undefined>): Promise<T | undefined>;
22
+ }
23
+ export declare class BufferResultFile extends BaseResultFile {
24
+ buffer: Uint8Array;
25
+ constructor(buffer: Uint8Array, fileName: string);
26
+ protected getContent(): ReadStream;
27
+ protected readMagicHeader(): Uint8Array;
28
+ getContentLength(): number | undefined;
29
+ }
30
+ export declare class PathResultFile extends BaseResultFile {
31
+ path: string;
32
+ constructor(path: string, fileName?: string);
33
+ protected getContent(): ReadStream | undefined;
34
+ protected readMagicHeader(): Uint8Array | undefined;
35
+ getContentLength(): number | undefined;
36
+ }
37
+ export declare const readSteamToJson: <T>(stream: Readable) => Promise<T | undefined>;
38
+ export declare const readStreamToString: (stream: Readable) => Promise<string>;
39
+ export declare const readStreamToBuffer: (stream: Readable) => Promise<Buffer>;
@@ -0,0 +1,135 @@
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;
7
+ import { lookup } from "mime-types";
8
+ import { ReadStream, closeSync, createReadStream, createWriteStream, existsSync, openSync, readSync, statSync, } from "node:fs";
9
+ import "node:fs/promises";
10
+ import { basename } from "node:path";
11
+ import { pipeline } from "node:stream/promises";
12
+ import { detectContentType } from "./detect.js";
13
+ import { extension } from "./utils.js";
14
+ export class BaseResultFile {
15
+ constructor(fileName) {
16
+ _BaseResultFile_instances.add(this);
17
+ this.extension = false;
18
+ this.contentType = false;
19
+ this.fileName = fileName;
20
+ }
21
+ getContentType() {
22
+ if (this.contentType === false) {
23
+ this.contentType = __classPrivateFieldGet(this, _BaseResultFile_instances, "m", _BaseResultFile_detectContentType).call(this);
24
+ }
25
+ return this.contentType;
26
+ }
27
+ getOriginalFileName() {
28
+ return this.fileName;
29
+ }
30
+ getExtension() {
31
+ if (this.extension === false) {
32
+ this.extension = extension(this.getOriginalFileName()) ?? extension("", this.getContentType()) ?? "";
33
+ }
34
+ return this.extension;
35
+ }
36
+ async asJson() {
37
+ return await this.readContent(readSteamToJson);
38
+ }
39
+ async asUtf8String() {
40
+ return await this.readContent(readStreamToString);
41
+ }
42
+ async asBuffer() {
43
+ return await this.readContent(readStreamToBuffer);
44
+ }
45
+ async writeTo(path) {
46
+ await this.readContent(async (stream) => {
47
+ await pipeline(stream, createWriteStream(path));
48
+ });
49
+ }
50
+ async readContent(transform) {
51
+ const content = this.getContent();
52
+ return content ? await transform(content) : undefined;
53
+ }
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;
67
+ export class BufferResultFile extends BaseResultFile {
68
+ constructor(buffer, fileName) {
69
+ super(basename(fileName));
70
+ this.buffer = buffer;
71
+ }
72
+ getContent() {
73
+ return ReadStream.from(this.buffer);
74
+ }
75
+ readMagicHeader() {
76
+ return this.buffer.subarray(0, magicHeaderLength);
77
+ }
78
+ getContentLength() {
79
+ return this.buffer.length;
80
+ }
81
+ }
82
+ export class PathResultFile extends BaseResultFile {
83
+ constructor(path, fileName = basename(path)) {
84
+ super(fileName);
85
+ this.path = path;
86
+ }
87
+ getContent() {
88
+ if (existsSync(this.path)) {
89
+ return createReadStream(this.path);
90
+ }
91
+ else {
92
+ return undefined;
93
+ }
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
+ }
117
+ getContentLength() {
118
+ return statSync(this.path, { throwIfNoEntry: false })?.size;
119
+ }
120
+ }
121
+ export const readSteamToJson = async (stream) => {
122
+ const text = await readStreamToString(stream);
123
+ return JSON.parse(text);
124
+ };
125
+ export const readStreamToString = async (stream) => {
126
+ const res = await readStreamToBuffer(stream);
127
+ return res.toString("utf-8");
128
+ };
129
+ export const readStreamToBuffer = async (stream) => {
130
+ const chunks = [];
131
+ for await (const chunk of stream) {
132
+ chunks.push(Buffer.from(chunk));
133
+ }
134
+ return Buffer.concat(chunks);
135
+ };
@@ -0,0 +1,2 @@
1
+ export declare const extension: (fileName: string, contentType?: string) => string | undefined;
2
+ export declare const lookupContentType: (fileName: string) => string | undefined;
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 ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@allurereport/reader-api",
3
+ "version": "3.0.0-beta.10",
4
+ "description": "Allure Reader API",
5
+ "keywords": [
6
+ "allure",
7
+ "testing"
8
+ ],
9
+ "repository": "https://github.com/allure-framework/allure3",
10
+ "license": "Apache-2.0",
11
+ "author": "Qameta Software",
12
+ "type": "module",
13
+ "exports": {
14
+ ".": "./dist/index.js"
15
+ },
16
+ "main": "./dist/index.js",
17
+ "module": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "scripts": {
23
+ "build": "run clean && tsc --project ./tsconfig.json",
24
+ "clean": "rimraf ./dist",
25
+ "eslint": "eslint ./src/**/*.{js,jsx,ts,tsx}",
26
+ "eslint:format": "eslint --fix ./src/**/*.{js,jsx,ts,tsx}",
27
+ "test": "rimraf ./out && vitest run"
28
+ },
29
+ "dependencies": {
30
+ "@allurereport/core-api": "3.0.0-beta.10",
31
+ "@allurereport/plugin-api": "3.0.0-beta.10",
32
+ "mime-types": "^2.1.35"
33
+ },
34
+ "devDependencies": {
35
+ "@stylistic/eslint-plugin": "^2.6.1",
36
+ "@types/eslint": "^8.56.11",
37
+ "@types/mime-types": "^2.1.4",
38
+ "@types/node": "^20.17.9",
39
+ "@typescript-eslint/eslint-plugin": "^8.0.0",
40
+ "@typescript-eslint/parser": "^8.0.0",
41
+ "@vitest/runner": "^2.1.8",
42
+ "allure-vitest": "^3.0.9",
43
+ "eslint": "^8.57.0",
44
+ "eslint-config-prettier": "^9.1.0",
45
+ "eslint-plugin-import": "^2.29.1",
46
+ "eslint-plugin-jsdoc": "^50.0.0",
47
+ "eslint-plugin-n": "^17.10.1",
48
+ "eslint-plugin-no-null": "^1.0.2",
49
+ "eslint-plugin-prefer-arrow": "^1.2.3",
50
+ "rimraf": "^6.0.1",
51
+ "ts-node": "^10.9.2",
52
+ "tslib": "^2.7.0",
53
+ "typescript": "^5.6.3",
54
+ "vitest": "^2.1.8"
55
+ }
56
+ }