forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
304 lines (277 loc) · 7.9 KB
/
Copy pathutils.js
File metadata and controls
304 lines (277 loc) · 7.9 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
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
'use strict';
const {
Array,
ArrayBufferPrototypeGetByteLength,
ArrayPrototypeSlice,
MathMax,
MathMin,
NumberMAX_SAFE_INTEGER,
PromiseResolve,
String,
TypedArrayPrototypeGetBuffer,
TypedArrayPrototypeGetByteLength,
TypedArrayPrototypeGetByteOffset,
TypedArrayPrototypeSet,
Uint8Array,
} = primordials;
const { TextEncoder } = require('internal/encoding');
const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_OPERATION_FAILED,
},
} = require('internal/errors');
const { isError } = require('internal/util');
const { isSharedArrayBuffer, isUint8Array } = require('internal/util/types');
const { validateOneOf } = require('internal/validators');
// Cached resolved promise to avoid allocating a new one on every sync fast-path.
const kResolvedPromise = PromiseResolve();
// Shared TextEncoder instance for string conversion.
const encoder = new TextEncoder();
// Default high water marks for push and multi-consumer streams. These values
// are somewhat arbitrary but have been tested across various workloads and
// appear to yield the best overall throughput/latency balance.
/** Default high water mark for push streams (single-consumer). */
const kPushDefaultHWM = 4;
/** Default high water mark for broadcast and share streams (multi-consumer). */
const kMultiConsumerDefaultHWM = 16;
/**
* Clamp a high water mark to [1, MAX_SAFE_INTEGER].
* @param {number} value
* @returns {number}
*/
function clampHWM(value) {
return MathMax(1, MathMin(NumberMAX_SAFE_INTEGER, value));
}
/**
* Register a handler for an AbortSignal, handling the already-aborted case.
* If the signal is already aborted, calls handler immediately.
* Otherwise, adds a one-time 'abort' listener.
* @param {AbortSignal} signal
* @param {Function} handler
*/
function onSignalAbort(signal, handler) {
if (signal.aborted) {
handler();
} else {
signal.addEventListener('abort', handler, { __proto__: null, once: true });
}
}
/**
* Compute the minimum cursor across a set of consumers and count how many
* consumers are at that cursor.
* @param {Set} consumers - Set of objects with a `cursor` property
* @param {number} fallback - Cursor to return when set is empty
* @returns {{ minCursor: number, minCursorConsumers: number }}
*/
function getMinCursor(consumers, fallback) {
let minCursor = fallback;
let minCursorConsumers = 0;
for (const consumer of consumers) {
if (consumer.cursor < minCursor) {
minCursor = consumer.cursor;
minCursorConsumers = 1;
} else if (consumer.cursor === minCursor) {
minCursorConsumers++;
}
}
return { __proto__: null, minCursor, minCursorConsumers };
}
/**
* Convert a chunk (string or Uint8Array) to Uint8Array.
* Strings are UTF-8 encoded.
* @param {Uint8Array|string} chunk
* @returns {Uint8Array}
*/
function toUint8Array(chunk) {
if (typeof chunk === 'string') {
return encoder.encode(chunk);
}
if (!isUint8Array(chunk)) {
throw new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Uint8Array'], chunk);
}
return chunk;
}
/**
* Check if all chunks in an array are already Uint8Array (no strings).
* Short-circuits on the first string found.
* @param {Array<Uint8Array|string>} chunks
* @returns {boolean}
*/
function allUint8Array(chunks) {
// Ok, well, kind of. This is more a check for "no strings"...
for (let i = 0; i < chunks.length; i++) {
if (typeof chunks[i] === 'string') return false;
}
return true;
}
/**
* Concatenate multiple Uint8Arrays into a single Uint8Array.
* @param {Uint8Array[]} chunks
* @returns {Uint8Array}
*/
function concatBytes(chunks) {
// Empty stream: return zero-length Uint8Array
if (chunks.length === 0) {
return new Uint8Array(0);
}
// Single chunk: return directly if it covers the entire backing buffer,
// otherwise return a copy
if (chunks.length === 1) {
const chunk = chunks[0];
// If non-zero offset, skip the remaining buffer checks.
if (TypedArrayPrototypeGetByteOffset(chunk) === 0) {
const buf = TypedArrayPrototypeGetBuffer(chunk);
// SharedArrayBuffer is not available in primordials, so use
// direct property access for its byteLength.
const bufByteLength = isSharedArrayBuffer(buf) ?
buf.byteLength :
ArrayBufferPrototypeGetByteLength(buf);
if (TypedArrayPrototypeGetByteLength(chunk) === bufByteLength) {
return chunk;
}
}
return new Uint8Array(chunk);
}
// Multiple chunks: concatenate
let totalByteLength = 0;
for (let i = 0; i < chunks.length; i++) {
totalByteLength += TypedArrayPrototypeGetByteLength(chunks[i]);
}
const concatenated = new Uint8Array(totalByteLength);
let offset = 0;
for (let i = 0; i < chunks.length; i++) {
TypedArrayPrototypeSet(concatenated, chunks[i], offset);
offset += TypedArrayPrototypeGetByteLength(chunks[i]);
}
return concatenated;
}
/**
* Convert an array of chunks (strings or Uint8Arrays) to a Uint8Array[].
* Always returns a fresh copy of the array.
* @param {Array<Uint8Array|string>} chunks
* @returns {Uint8Array[]}
*/
function convertChunks(chunks) {
if (allUint8Array(chunks)) {
return ArrayPrototypeSlice(chunks);
}
const len = chunks.length;
const result = new Array(len);
for (let i = 0; i < len; i++) {
result[i] = toUint8Array(chunks[i]);
}
return result;
}
/**
* Wrap a caught value as an Error, converting non-Error values.
* @param {unknown} error
* @returns {Error}
*/
function wrapError(error) {
return isError(error) ? error : new ERR_OPERATION_FAILED(String(error));
}
/**
* Check if a value implements a Symbol-keyed protocol (has a function
* at the given symbol key).
* @param {unknown} value
* @param {symbol} symbol
* @returns {boolean}
*/
function hasProtocol(value, symbol) {
return (
value !== null &&
typeof value === 'object' &&
symbol in value &&
typeof value[symbol] === 'function'
);
}
/**
* Check if a value is PullOptions (object without transform or write property).
* @param {unknown} value
* @returns {boolean}
*/
function isPullOptions(value) {
return (
value !== null &&
typeof value === 'object' &&
!('transform' in value) &&
!('write' in value)
);
}
/**
* Check if a value is a stateful transform object (has a transform method).
* @param {unknown} value
* @returns {boolean}
*/
function isTransformObject(value) {
return typeof value?.transform === 'function';
}
/**
* Check if a value is a valid transform (function or transform object).
* @param {unknown} value
* @returns {boolean}
*/
function isTransform(value) {
return typeof value === 'function' || isTransformObject(value);
}
/**
* Parse variadic arguments for pull/pullSync.
* Returns { transforms, options }
* @param {Array} args
* @returns {{ transforms: Array, options: object|undefined }}
*/
function parsePullArgs(args) {
if (args.length === 0) {
return { __proto__: null, transforms: [], options: undefined };
}
let transforms;
let options;
const last = args[args.length - 1];
if (isPullOptions(last)) {
transforms = ArrayPrototypeSlice(args, 0, -1);
options = last;
} else {
transforms = args;
options = undefined;
}
for (let i = 0; i < transforms.length; i++) {
if (!isTransform(transforms[i])) {
throw new ERR_INVALID_ARG_TYPE(
`transforms[${i}]`, ['Function', 'Object with transform()'],
transforms[i]);
}
}
return { __proto__: null, transforms, options };
}
/**
* Validate backpressure option value.
* @param {string} value
*/
function validateBackpressure(value) {
validateOneOf(value, 'options.backpressure', [
'strict',
'block',
'drop-oldest',
'drop-newest',
]);
}
module.exports = {
kMultiConsumerDefaultHWM,
kPushDefaultHWM,
kResolvedPromise,
allUint8Array,
clampHWM,
concatBytes,
convertChunks,
getMinCursor,
hasProtocol,
isPullOptions,
isTransform,
isTransformObject,
onSignalAbort,
parsePullArgs,
toUint8Array,
validateBackpressure,
wrapError,
};