-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
69 lines (61 loc) · 1.44 KB
/
utils.ts
File metadata and controls
69 lines (61 loc) · 1.44 KB
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
// deno-cgi
//
// Copyright © Gapotchenko and Contributors
//
// File introduced by: Oleksiy Gapotchenko
// Year of introduction: 2025
export function startsWithU8(
haystack: Uint8Array,
needle: Uint8Array,
): boolean {
if (needle.length > haystack.length) return false;
for (let i = 0; i < needle.length; i++) {
if (haystack[i] !== needle[i]) return false;
}
return true;
}
export function getPrefixedStream(
prefix: Uint8Array,
source: ReadableStream<Uint8Array>,
): ReadableStream<Uint8Array> {
const reader = source.getReader();
return new ReadableStream<Uint8Array>({
start(controller) {
if (prefix.length) controller.enqueue(prefix);
},
async pull(controller) {
try {
const { value, done } = await reader.read();
if (done) {
controller.close();
reader.releaseLock();
return;
}
if (value?.length) controller.enqueue(value);
} catch (err) {
controller.error(err);
reader.releaseLock();
}
},
cancel(reason) {
return reader.cancel(reason).catch(() => {});
},
});
}
export function equalsOrStartsWith(
haystack: string,
needles: Set<string>,
needlePrefixes: string[],
): boolean {
// Exact matches
if (needles.has(haystack)) {
return true;
}
// Prefix-based checks
for (const prefix of needlePrefixes) {
if (haystack.startsWith(prefix)) {
return true;
}
}
return false;
}