UNPKG

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