UNPKG

2.6 kBJavaScriptView Raw
1import zlib from 'zlib';
2import { launchers } from "./launchers.mjs";
3/**
4 * Create return value once.
5 *
6 * @param create Create function.
7 * @returns Returned value.
8 */
9
10export function once(create) {
11 let called = false;
12 let value;
13 return () => {
14 if (!called) {
15 value = create();
16 called = true;
17 }
18
19 return value;
20 };
21}
22/**
23 * Trim dot slash from head of path.
24 *
25 * @param path Path string.
26 * @returns Trimmed path.
27 */
28
29export function trimDotSlash(path) {
30 return path.replace(/^(\.\/)+/, '');
31}
32/**
33 * Find path relative from base, if base matches.
34 *
35 * @param path Path to match against.
36 * @param start Search start.
37 * @param nocase Match case-insensitive.
38 * @returns Returns path, or null.
39 */
40
41export function pathRelativeBase(path, start, nocase = false) {
42 const p = trimDotSlash(nocase ? path.toLowerCase() : path);
43 const s = trimDotSlash(nocase ? start.toLowerCase() : start);
44
45 if (p === s) {
46 return '';
47 }
48
49 if (p.startsWith(`${s}/`)) {
50 return path.substr(s.length + 1);
51 }
52
53 return null;
54}
55/**
56 * Same as pathRelativeBase, but retuns true on a match, else false.
57 *
58 * @param path Path to match against.
59 * @param start Search start.
60 * @param nocase Match case-insensitive.
61 * @returns Returns true on match, else false.
62 */
63
64export function pathRelativeBaseMatch(path, start, nocase = false) {
65 return pathRelativeBase(path, start, nocase) !== null;
66}
67/**
68 * Trim a file extenion.
69 *
70 * @param path File path.
71 * @param ext File extension.
72 * @param nocase Match case-insensitive.
73 * @returns Path without file extension.
74 */
75
76export function trimExtension(path, ext, nocase = false) {
77 const p = nocase ? path.toLowerCase() : path;
78 const e = nocase ? ext.toLowerCase() : ext;
79 return p.endsWith(e) ? path.substr(0, p.length - e.length) : path;
80}
81/**
82 * Get ArrayBuffer from Buffer.
83 *
84 * @param buffer Buffer instance.
85 * @returns ArrayBuffer copy.
86 */
87
88export function bufferToArrayBuffer(buffer) {
89 const {
90 byteOffset,
91 byteLength
92 } = buffer;
93 return buffer.buffer.slice(byteOffset, byteOffset + byteLength);
94}
95/**
96 * Get launcher data for an ID.
97 *
98 * @param id Laucher ID.
99 * @returns Launcher data.
100 */
101
102export async function launcher(id) {
103 const b64 = launchers()[id];
104
105 if (typeof b64 !== 'string') {
106 throw new Error(`Invalid launcher id: ${id}`);
107 }
108
109 return new Promise((resolve, reject) => {
110 zlib.inflateRaw(Buffer.from(b64, 'base64'), (err, data) => {
111 if (err) {
112 reject(err);
113 return;
114 }
115
116 resolve(data);
117 });
118 });
119}
120//# sourceMappingURL=util.mjs.map