UNPKG

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