-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.ts
359 lines (324 loc) · 9.14 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import { exists, existsSync } from "@std/fs/exists";
import { extname } from "@std/path";
import { initParser } from "deno_dom/wasm-noinit";
import { configure } from "zipjs/index.js";
import { MD5 } from "lifegpc-md5";
import { SHA1 } from "@lifegpc/sha1";
export function sleep(time: number): Promise<undefined> {
return new Promise((r) => {
setTimeout(() => {
r(undefined);
}, time);
});
}
export async function sure_dir(f = "./test") {
if (!await exists(f)) {
await Deno.mkdir(f, { "recursive": true });
}
}
export function sure_dir_sync(f = "./test") {
if (!existsSync(f)) {
Deno.mkdirSync(f, { "recursive": true });
}
}
export enum PromiseStatus {
Pending,
Fulfilled,
Rejected,
}
export type PromiseState<T> = {
status: PromiseStatus;
value: Awaited<T> | undefined;
reason: unknown;
};
export function promiseState<T>(p: Promise<T>): Promise<PromiseState<T>> {
const pe = { status: PromiseStatus.Pending };
return new Promise((resolve) => {
Promise.race([p, pe]).then((v) => {
v === pe ? resolve(pe as PromiseState<T>) : resolve(
{
status: PromiseStatus.Fulfilled,
value: v,
} as PromiseState<T>,
);
}).catch((e) => {
resolve(
{ status: PromiseStatus.Rejected, reason: e } as PromiseState<
T
>,
);
});
});
}
let inited_parser = false;
export async function initDOMParser() {
if (!inited_parser) {
await initParser();
inited_parser = true;
}
}
export function parse_bool(s: string) {
const l = s.toLowerCase();
return l === "true" || l === "yes";
}
export function try_remove_sync(
s: string,
o: Deno.RemoveOptions | undefined = undefined,
) {
try {
Deno.removeSync(s, o);
} catch (_) {
return;
}
}
const fail = Symbol();
export async function asyncFilter<T, V>(
arr: ArrayLike<T>,
callback: (
this: V | undefined,
element: T,
index: number,
array: ArrayLike<T>,
) => Promise<boolean>,
thisArg?: V,
): Promise<T[]> {
const t = <[T][]> (await Promise.all(
map(
arr,
async (item, index, array) =>
(await callback.apply(thisArg, [item, index, array]))
? [item]
: fail,
),
)).filter((i) => i !== fail);
return t.map((t) => t[0]);
}
export async function asyncForEach<T, V>(
arr: ArrayLike<T>,
callback: (
this: V | undefined,
element: T,
index: number,
array: ArrayLike<T>,
) => Promise<void>,
thisArg?: V,
) {
for (let i = 0; i < arr.length; i++) {
await callback.apply(thisArg, [arr[i], i, arr]);
}
}
export function addZero(n: string | number | bigint, len: number) {
let s = n.toString();
while (s.length < len) s = "0" + s;
return s;
}
export function filterFilename(p: string, maxLength = 256) {
// strip control chars
p = p.replace(/\p{C}/gu, "");
// normalize newline
p = p.replace(/[\n\r]/g, "");
p = p.replace(/\p{Zl}/gu, "");
p = p.replace(/\p{Zp}/gu, "");
// normalize space
p = p.replace(/\p{Zs}/gu, " ");
p = p.replace(/[\\/]/g, "_");
if (Deno.build.os == "windows") {
p = p.replace(/[:\*\?\"<>\|]/g, "_");
} else if (Deno.build.os == "linux") {
p = p.replace(/[!\$\"]/g, "_");
}
return limitFilename(p, maxLength);
}
export function limitFilename(p: string, maxLength: number) {
if (maxLength > 0 && p.length > maxLength) {
const ext = extname(p);
if (maxLength < ext.length) return ext;
return p.slice(0, maxLength - ext.length) + ext;
}
return p;
}
export type DiscriminatedUnion<
K extends PropertyKey,
T extends Record<PropertyKey, unknown>,
> = {
[P in keyof T]: ({ [Q in K]: P } & T[P]) extends infer U
? { [Q in keyof U]: U[Q] }
: never;
}[keyof T];
let zipjs_configured = false;
export function configureZipJs() {
if (zipjs_configured) return;
configure({ useWebWorkers: false });
zipjs_configured = true;
}
export function add_suffix_to_path(path: string, suffix?: string) {
if (suffix === undefined) {
suffix = Math.round(Math.random() * 100000).toString();
}
const ext = extname(path);
if (ext) {
return `${path.slice(0, path.length - ext.length)}-${suffix}${ext}`;
} else {
return `${path}-${suffix}`;
}
}
export async function calFileMd5(p: string | URL) {
const h = new MD5();
const f = await Deno.open(p);
try {
const buf = new Uint8Array(65536);
let readed: number | null = null;
do {
readed = await f.read(buf);
if (readed) {
h.update(buf, readed);
}
} while (readed !== null);
return h.digest_hex();
} finally {
f.close();
}
}
export async function calFileSha1(p: string | URL) {
const h = new SHA1();
const f = await Deno.open(p);
try {
const buf = new Uint8Array(65536);
let readed: number | null = null;
do {
readed = await f.read(buf);
if (readed) {
h.update(buf, readed);
}
} while (readed !== null);
return h.digest_hex();
} finally {
f.close();
}
}
export async function checkMapFile(p: string | URL, signal?: AbortSignal) {
if (!(await exists(p))) return false;
const map = JSON.parse(await Deno.readTextFile(p, { signal }));
if (
!(await asyncEvery(
Object.getOwnPropertyNames(map.inputs),
async (k) => {
const data = map.inputs[k];
const md5 = data.md5;
if (!md5) return false;
if (!(await exists(k))) return false;
const m = await calFileMd5(k);
return md5 === m;
},
))
) return false;
if (
!(await asyncEvery(
Object.getOwnPropertyNames(map.outputs),
async (k) => {
const data = map.outputs[k];
const md5 = data.md5;
if (!md5) return false;
if (!(await exists(k))) return false;
const m = await calFileMd5(k);
return md5 === m;
},
))
) return false;
return true;
}
export async function asyncEvery<T, V>(
arr: ArrayLike<T>,
callback: (
this: V | undefined,
element: T,
index: number,
array: ArrayLike<T>,
) => Promise<boolean>,
thisArg?: V,
) {
for (let i = 0; i < arr.length; i++) {
if (!await callback.apply(thisArg, [arr[i], i, arr])) return false;
}
return true;
}
export function map<T, S, V>(
arr: ArrayLike<T>,
callback: (
this: S | undefined,
element: T,
index: number,
array: ArrayLike<T>,
) => V,
thisArg?: S,
) {
const re: V[] = [];
for (let i = 0; i < arr.length; i++) {
re.push(callback.apply(thisArg, [arr[i], i, arr]));
}
return re;
}
export class RecoverableError extends Error {}
export class TimeoutError extends RecoverableError {
constructor() {
super("Timeout");
}
}
let _isDocker: boolean | undefined = undefined;
export function isDocker() {
if (_isDocker === undefined) {
_isDocker = parse_bool(Deno.env.get("DOCKER") ?? "false");
}
return _isDocker;
}
export function toJSON(obj: unknown) {
return JSON.stringify(
obj,
(_, value) => {
if (typeof value === "bigint") {
const s = value.toString();
const t = parseInt(s);
if (Number.isSafeInteger(t)) return t;
return s;
}
return value;
},
);
}
export function parseBigInt(str: string) {
const t = parseInt(str);
if (isNaN(t)) return t;
if (Number.isSafeInteger(t)) return t;
const m = str.match(/^(\+|-)?\d+/);
if (!m) return NaN;
return BigInt(m[0]);
}
export function isNumNaN(num: number | bigint) {
return typeof num === "number" ? isNaN(num) : false;
}
export function compareNum(num1: number | bigint, num2: number | bigint) {
return num1 == num2 ? 0 : num1 < num2 ? -1 : 1;
}
const HASH_PATTERN = /^\/h\/([0-9a-f]+)/;
const FHASH_PATTERN = /([0-9a-f]{40})-(\d+)-(\d+)-(\d+)-([^\/]+)/g;
export function getHashFromUrl(url: string | URL) {
const u = typeof url === "string" ? new URL(url) : url;
const m = u.pathname.match(HASH_PATTERN);
if (m) return m[1];
if (u.pathname.startsWith("/om/")) {
const ma = Array.from(u.pathname.matchAll(FHASH_PATTERN));
const comps = u.pathname.split("/");
if (ma.length && comps.length > 3) {
const width = comps[comps.length - 3];
if (width == "0") {
return ma[0][1];
}
for (const f of ma) {
if (f[3] == width) {
return f[1];
}
}
}
}
throw Error(`URL ${url} not contains hash info.`);
}