1 | import zlib from 'zlib';
|
2 | import { launchers } from "./launchers.mjs";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | export 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 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | export function trimDotSlash(path) {
|
30 | return path.replace(/^(\.\/)+/, '');
|
31 | }
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | export 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 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | export function pathRelativeBaseMatch(path, start, nocase = false) {
|
65 | return pathRelativeBase(path, start, nocase) !== null;
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | export 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 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | export function bufferToArrayBuffer(buffer) {
|
89 | const {
|
90 | byteOffset,
|
91 | byteLength
|
92 | } = buffer;
|
93 | return buffer.buffer.slice(byteOffset, byteOffset + byteLength);
|
94 | }
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | export 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 |
|