-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBacktraceFileAttachment.ts
More file actions
37 lines (30 loc) · 1.19 KB
/
BacktraceFileAttachment.ts
File metadata and controls
37 lines (30 loc) · 1.19 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
import { BacktraceAttachment } from '@backtrace/sdk-core';
import nodeFs from 'fs';
import path from 'path';
import { Readable } from 'stream';
import { NodeFs } from '../storage/nodeFs.js';
export class BacktraceFileAttachment implements BacktraceAttachment<Readable> {
public readonly name: string;
constructor(
public readonly filePath: string,
name?: string,
private readonly _fs: Pick<NodeFs, 'existsSync' | 'createReadStream'> = nodeFs,
) {
this.name = name ?? path.basename(this.filePath);
}
public get(): Readable | undefined {
if (!this._fs.existsSync(this.filePath)) {
return undefined;
}
return this._fs.createReadStream(this.filePath);
}
}
export interface BacktraceFileAttachmentFactory {
create(filePath: string, name?: string): BacktraceFileAttachment;
}
export class NodeFsBacktraceFileAttachmentFactory implements BacktraceFileAttachmentFactory {
constructor(private readonly _fs: Pick<NodeFs, 'existsSync' | 'createReadStream'> = nodeFs) {}
public create(filePath: string, name?: string): BacktraceFileAttachment {
return new BacktraceFileAttachment(filePath, name, this._fs);
}
}