-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.ts
More file actions
102 lines (90 loc) · 2.54 KB
/
Copy pathutils.ts
File metadata and controls
102 lines (90 loc) · 2.54 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
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
/**
* Utility functions for the Streams API
*/
// Shared TextEncoder instance for string conversion
const encoder = new TextEncoder();
/**
* Convert a chunk (string or Uint8Array) to Uint8Array.
* Strings are UTF-8 encoded.
*/
export function toUint8Array(chunk: Uint8Array | string): Uint8Array {
if (typeof chunk === 'string') {
return encoder.encode(chunk);
}
return chunk;
}
/**
* Check if all chunks in an array are already Uint8Array (no strings).
* Short-circuits on the first string found.
*/
export function allUint8Array(chunks: (Uint8Array | string)[]): chunks is Uint8Array[] {
for (let i = 0; i < chunks.length; i++) {
if (typeof chunks[i] === 'string') return false;
}
return true;
}
/**
* Calculate total byte length of an array of chunks.
*/
function totalByteLength(chunks: Uint8Array[]): number {
let total = 0;
for (const chunk of chunks) {
total += chunk.byteLength;
}
return total;
}
/**
* Concatenate multiple Uint8Arrays into a single Uint8Array.
*/
export function concatBytes(chunks: Uint8Array[]): Uint8Array {
if (chunks.length === 0) {
return new Uint8Array(0);
}
if (chunks.length === 1) {
return chunks[0];
}
const total = totalByteLength(chunks);
const result = new Uint8Array(total);
let offset = 0;
// It turns out that this use of set is a bit of a performance bottleneck
// in V8, but it's still faster than manual copying.
for (const chunk of chunks) {
result.set(chunk, offset);
offset += chunk.byteLength;
}
return result;
}
// =============================================================================
// Options Parsing Helpers
// =============================================================================
import type { PullOptions } from './types.js';
/**
* Check if a value is PullOptions (object without transform or write property).
*/
export function isPullOptions(value: unknown): value is PullOptions {
return (
value !== null &&
typeof value === 'object' &&
!('transform' in value) &&
!('write' in value)
);
}
/**
* Parse variadic arguments for pull/pullSync.
* Returns { transforms, options }
*/
export function parsePullArgs<T, O extends PullOptions>(
args: (T | O)[]
): { transforms: T[]; options: O | undefined } {
if (args.length === 0) {
return { transforms: [], options: undefined };
}
const last = args[args.length - 1];
if (isPullOptions(last)) {
return {
transforms: args.slice(0, -1) as T[],
options: last as O,
};
}
return { transforms: args as T[], options: undefined };
}