-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathnode.ts
More file actions
43 lines (36 loc) · 985 Bytes
/
node.ts
File metadata and controls
43 lines (36 loc) · 985 Bytes
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
// Import Node.js Dependencies
import fs from "node:fs";
import path from "node:path";
import * as consumers from "node:stream/consumers";
import { Readable } from "node:stream";
// Import Internal Dependencies
import { getFlags } from "./web.ts";
// CONSTANTS
const kFlagsPath = path.join(import.meta.dirname, "flags");
/**
* @description lazy read a flag file by getting a Node.js Readable Stream
*/
export function lazyFetchFlagFile(
name: string
): Readable {
if (typeof name !== "string") {
throw new TypeError("You should provide a flag name");
}
const flags = getFlags();
if (!flags.has(name)) {
throw new Error("There is no file associated with that name");
}
const fileName = path.extname(name) === ".html" ?
name :
`${name}.html`;
return fs.createReadStream(
path.join(kFlagsPath, fileName)
);
}
export function eagerFetchFlagFile(
name: string
): Promise<string> {
return consumers.text(
lazyFetchFlagFile(name)
);
}