-
Notifications
You must be signed in to change notification settings - Fork 999
Expand file tree
/
Copy pathfile-system.ts
More file actions
78 lines (66 loc) · 2.27 KB
/
file-system.ts
File metadata and controls
78 lines (66 loc) · 2.27 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { Config, Logger, decrypt, encrypt } from '@browserless.io/browserless';
import { readFile, writeFile } from 'fs/promises';
import { EventEmitter } from 'events';
export class FileSystem extends EventEmitter {
protected fsMap: Map<string, string[]> = new Map();
protected currentAESKey: Buffer;
protected logger = new Logger('file-system');
constructor(protected config: Config) {
super();
this.currentAESKey = config.getAESKey();
}
/**
* Appends contents to a file-path for persistance. File contents are
* encrypted before being saved to disk. Reads happen via the in-memory
* lookup of the internal map.
*
* @param path The filepath to persist contents to
* @param newContent A string of new content to add to the file
* @returns void
*/
public async append(
path: string,
newContent: string,
shouldEncode: boolean,
): Promise<void> {
const contents = await this.read(path, shouldEncode);
contents.push(newContent);
this.fsMap.set(path, contents);
const encoded = shouldEncode
? await encrypt(contents.join('\n'), this.currentAESKey)
: contents.join('\n');
return writeFile(path, encoded.toString());
}
/**
* Reads contents from the local map, if any exist, or loads
* from the file system and hydrates the cache for the particular filepath
*
* @param path The filepath of the contents to read
* @returns Promise of the contents separated by newlines
*/
public async read(path: string, encoded: boolean): Promise<string[]> {
const hasKey = this.fsMap.has(path);
if (hasKey) {
return this.fsMap.get(path) as string[];
}
const contents = (await readFile(path).catch(() => '')).toString();
const decoded =
encoded && contents.length
? await decrypt(contents, this.currentAESKey)
: contents;
const splitContents = decoded.length ? decoded.split('\n') : [];
this.fsMap.set(path, splitContents);
return splitContents;
}
/**
* Implement any browserless-core-specific shutdown logic here.
* Calls the empty-SDK stop method for downstream implementations.
*/
public async shutdown() {
return await this.stop();
}
/**
* Left blank for downstream SDK modules to optionally implement.
*/
public stop() {}
}