deno.land / std@0.160.0 / node / _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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { deferred } from "../async/mod.ts";import { assert, assertStringIncludes, fail } from "../testing/asserts.ts";import { readAll } from "../streams/conversion.ts";import { errorMap } from "./internal_binding/uv.ts";import { codes } from "./internal/error_codes.ts";
export type BinaryEncodings = "binary";
export type TextEncodings = | "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "hex";
export type Encodings = BinaryEncodings | TextEncodings;
export function notImplemented(msg: string): never { const message = msg ? `Not implemented: ${msg}` : "Not implemented"; throw new Error(message);}
export function warnNotImplemented(msg?: string) { const message = msg ? `Warning: Not implemented: ${msg}` : "Warning: Not implemented"; console.warn(message);}
export type _TextDecoder = typeof TextDecoder.prototype;export const _TextDecoder = TextDecoder;
export type _TextEncoder = typeof TextEncoder.prototype;export const _TextEncoder = TextEncoder;
// API helpers
export type MaybeNull<T> = T | null;export type MaybeDefined<T> = T | undefined;export type MaybeEmpty<T> = T | null | undefined;
export function intoCallbackAPI<T>( // deno-lint-ignore no-explicit-any func: (...args: any[]) => Promise<T>, cb: MaybeEmpty<(err: MaybeNull<Error>, value?: MaybeEmpty<T>) => void>, // deno-lint-ignore no-explicit-any ...args: any[]) { func(...args).then( (value) => cb && cb(null, value), (err) => cb && cb(err), );}
export function intoCallbackAPIWithIntercept<T1, T2>( // deno-lint-ignore no-explicit-any func: (...args: any[]) => Promise<T1>, interceptor: (v: T1) => T2, cb: MaybeEmpty<(err: MaybeNull<Error>, value?: MaybeEmpty<T2>) => void>, // deno-lint-ignore no-explicit-any ...args: any[]) { func(...args).then( (value) => cb && cb(null, interceptor(value)), (err) => cb && cb(err), );}
export function spliceOne(list: string[], index: number) { for (; index + 1 < list.length; index++) list[index] = list[index + 1]; list.pop();}
// Taken from: https://github.com/nodejs/node/blob/ba684805b6c0eded76e5cd89ee00328ac7a59365/lib/internal/util.js#L125// Return undefined if there is no match.// Move the "slow cases" to a separate function to make sure this function gets// inlined properly. That prioritizes the common case.export function normalizeEncoding( enc: string | null,): TextEncodings | undefined { if (enc == null || enc === "utf8" || enc === "utf-8") return "utf8"; return slowCases(enc);}
// https://github.com/nodejs/node/blob/ba684805b6c0eded76e5cd89ee00328ac7a59365/lib/internal/util.js#L130function slowCases(enc: string): TextEncodings | undefined { switch (enc.length) { case 4: if (enc === "UTF8") return "utf8"; if (enc === "ucs2" || enc === "UCS2") return "utf16le"; enc = `${enc}`.toLowerCase(); if (enc === "utf8") return "utf8"; if (enc === "ucs2") return "utf16le"; break; case 3: if (enc === "hex" || enc === "HEX" || `${enc}`.toLowerCase() === "hex") { return "hex"; } break; case 5: if (enc === "ascii") return "ascii"; if (enc === "ucs-2") return "utf16le"; if (enc === "UTF-8") return "utf8"; if (enc === "ASCII") return "ascii"; if (enc === "UCS-2") return "utf16le"; enc = `${enc}`.toLowerCase(); if (enc === "utf-8") return "utf8"; if (enc === "ascii") return "ascii"; if (enc === "ucs-2") return "utf16le"; break; case 6: if (enc === "base64") return "base64"; if (enc === "latin1" || enc === "binary") return "latin1"; if (enc === "BASE64") return "base64"; if (enc === "LATIN1" || enc === "BINARY") return "latin1"; enc = `${enc}`.toLowerCase(); if (enc === "base64") return "base64"; if (enc === "latin1" || enc === "binary") return "latin1"; break; case 7: if ( enc === "utf16le" || enc === "UTF16LE" || `${enc}`.toLowerCase() === "utf16le" ) { return "utf16le"; } break; case 8: if ( enc === "utf-16le" || enc === "UTF-16LE" || `${enc}`.toLowerCase() === "utf-16le" ) { return "utf16le"; } break; default: if (enc === "") return "utf8"; }}
export function validateIntegerRange( value: number, name: string, min = -2147483648, max = 2147483647,) { // The defaults for min and max correspond to the limits of 32-bit integers. if (!Number.isInteger(value)) { throw new Error(`${name} must be 'an integer' but was ${value}`); }
if (value < min || value > max) { throw new Error( `${name} must be >= ${min} && <= ${max}. Value was ${value}`, ); }}
type OptionalSpread<T> = T extends undefined ? [] : [T];
export function once<T = undefined>( callback: (...args: OptionalSpread<T>) => void,) { let called = false; return function (this: unknown, ...args: OptionalSpread<T>) { if (called) return; called = true; callback.apply(this, args); };}
/** * @param [expectedExecutions = 1] * @param [timeout = 1000] Milliseconds to wait before the promise is forcefully exited */export function mustCall<T extends unknown[]>( fn: (...args: T) => void = () => {}, expectedExecutions = 1, timeout = 1000,): [Promise<void>, (...args: T) => void] { if (expectedExecutions < 1) { throw new Error("Expected executions can't be lower than 1"); } let timesExecuted = 0; const completed = deferred();
const abort = setTimeout(() => completed.reject(), timeout);
function callback(this: unknown, ...args: T) { timesExecuted++; if (timesExecuted === expectedExecutions) { completed.resolve(); } fn.apply(this, args); }
const result = completed .then(() => clearTimeout(abort)) .catch(() => fail( `Async operation not completed: Expected ${expectedExecutions}, executed ${timesExecuted}`, ) );
return [ result, callback, ];}/** Asserts that an error thrown in a callback will not be wrongly caught. */export async function assertCallbackErrorUncaught( { prelude, invocation, cleanup }: { /** Any code which needs to run before the actual invocation (notably, any import statements). */ prelude?: string; /** * The start of the invocation of the function, e.g. `open("foo.txt", `. * The callback will be added after it. */ invocation: string; /** Called after the subprocess is finished but before running the assertions, e.g. to clean up created files. */ cleanup?: () => Promise<void> | void; },) { // Since the error has to be uncaught, and that will kill the Deno process, // the only way to test this is to spawn a subprocess. const p = Deno.run({ cmd: [ Deno.execPath(), "eval", "--no-check", // Running TSC for every one of these tests would take way too long "--unstable", `${prelude ?? ""}
${invocation}(err) => { // If the bug is present and the callback is called again with an error, // don't throw another error, so if the subprocess fails we know it had the correct behaviour. if (!err) throw new Error("success"); });`, ], stderr: "piped", }); const status = await p.status(); const stderr = new TextDecoder().decode(await readAll(p.stderr)); p.close(); p.stderr.close(); await cleanup?.(); assert(!status.success); assertStringIncludes(stderr, "Error: success");}
export function makeMethodsEnumerable(klass: { new (): unknown }) { const proto = klass.prototype; for (const key of Object.getOwnPropertyNames(proto)) { const value = proto[key]; if (typeof value === "function") { const desc = Reflect.getOwnPropertyDescriptor(proto, key); if (desc) { desc.enumerable = true; Object.defineProperty(proto, key, desc); } } }}
const NumberIsSafeInteger = Number.isSafeInteger;
/** * Returns a system error name from an error code number. * @param code error code number */export function getSystemErrorName(code: number): string | undefined { if (typeof code !== "number") { throw new codes.ERR_INVALID_ARG_TYPE("err", "number", code); } if (code >= 0 || !NumberIsSafeInteger(code)) { throw new codes.ERR_OUT_OF_RANGE("err", "a negative integer", code); } return errorMap.get(code)?.[0];}
std

Version Info

Tagged at
a year ago