UNPKG

2.49 kBJavaScriptView Raw
1import { inflateRaw } from 'node:zlib';
2import { LAUNCHERS } from "./launchers.mjs";
3
4/**
5 * HTML encode.
6 *
7 * @param s Raw strings.
8 * @param dq Double quotes.
9 * @param sq Single quotes.
10 * @returns Encoded strings.
11 */
12export function htmlEncode(s, dq = false, sq = false) {
13 s = s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
14 if (dq) {
15 s = s.replace(/"/g, '&quot;');
16 }
17 if (sq) {
18 s = s.replace(/'/g, '&#39;');
19 }
20 return s;
21}
22
23/**
24 * Trim dot slash from head of path.
25 *
26 * @param path Path string.
27 * @returns Trimmed path.
28 */
29export function trimDotSlash(path) {
30 return path.replace(/^(\.\/)+/, '');
31}
32
33/**
34 * Find path relative from base, if base matches.
35 *
36 * @param path Path to match against.
37 * @param start Search start.
38 * @param nocase Match case-insensitive.
39 * @returns Returns path, or null.
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 if (p === s) {
45 return '';
46 }
47 if (p.startsWith(`${s}/`)) {
48 return path.substring(s.length + 1);
49 }
50 return null;
51}
52
53/**
54 * Same as pathRelativeBase, but retuns true on a match, else false.
55 *
56 * @param path Path to match against.
57 * @param start Search start.
58 * @param nocase Match case-insensitive.
59 * @returns Returns true on match, else false.
60 */
61export function pathRelativeBaseMatch(path, start, nocase = false) {
62 return pathRelativeBase(path, start, nocase) !== null;
63}
64
65/**
66 * Trim a file extenion.
67 *
68 * @param path File path.
69 * @param ext File extension.
70 * @param nocase Match case-insensitive.
71 * @returns Path without file extension.
72 */
73export function trimExtension(path, ext, nocase = false) {
74 const p = nocase ? path.toLowerCase() : path;
75 const e = nocase ? ext.toLowerCase() : ext;
76 return p.endsWith(e) ? path.substring(0, p.length - e.length) : path;
77}
78
79/**
80 * Get launcher data for an ID.
81 *
82 * @param id Laucher ID.
83 * @returns Launcher data.
84 */
85export async function launcher(id) {
86 const b64 = LAUNCHERS[id];
87 if (typeof b64 !== 'string') {
88 throw new Error(`Invalid launcher id: ${id}`);
89 }
90 return new Promise((resolve, reject) => {
91 inflateRaw(Buffer.from(b64, 'base64'), (err, data) => {
92 if (err) {
93 reject(err);
94 return;
95 }
96 resolve(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
97 });
98 });
99}
100//# sourceMappingURL=util.mjs.map
\No newline at end of file