@socketsecurity/lib 5.19.1 → 5.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +105 -74
- package/dist/archives.js +13 -0
- package/dist/cacache.js +6 -8
- package/dist/cache-with-ttl.d.ts +7 -0
- package/dist/cache-with-ttl.js +27 -8
- package/dist/constants/socket.js +1 -1
- package/dist/dlx/detect.js +25 -8
- package/dist/dlx/lockfile.js +4 -1
- package/dist/dlx/manifest.d.ts +10 -4
- package/dist/dlx/package.d.ts +1 -1
- package/dist/dlx/package.js +19 -3
- package/dist/external/@npmcli/package-json/lib/read-package.js +40 -32
- package/dist/external/@npmcli/package-json/lib/sort.js +104 -92
- package/dist/external/@npmcli/package-json.js +9 -3968
- package/dist/external/@sinclair/typebox/value.js +9007 -0
- package/dist/external/@sinclair/typebox.js +7891 -0
- package/dist/external/debug.js +162 -328
- package/dist/external/npm-pack.js +13935 -33342
- package/dist/fs.js +8 -2
- package/dist/globs.js +5 -1
- package/dist/http-request.d.ts +0 -25
- package/dist/http-request.js +6 -5
- package/dist/ipc.js +43 -10
- package/dist/json/edit.d.ts +1 -1
- package/dist/json/parse.d.ts +47 -2
- package/dist/json/parse.js +40 -2
- package/dist/json/types.d.ts +49 -0
- package/dist/memoization.d.ts +4 -23
- package/dist/memoization.js +15 -49
- package/dist/packages/specs.js +9 -2
- package/dist/paths/packages.js +6 -2
- package/dist/process-lock.js +1 -6
- package/dist/promise-queue.d.ts +9 -4
- package/dist/promise-queue.js +10 -8
- package/dist/promises.d.ts +41 -0
- package/dist/promises.js +19 -2
- package/dist/regexps.d.ts +4 -13
- package/dist/regexps.js +60 -3
- package/dist/schema/parse.d.ts +26 -0
- package/dist/{zod.js → schema/parse.js} +14 -6
- package/dist/schema/types.d.ts +121 -0
- package/dist/schema/validate.d.ts +35 -0
- package/dist/schema/validate.js +98 -0
- package/dist/stdio/progress.js +1 -1
- package/dist/suppress-warnings.js +0 -2
- package/dist/tables.js +2 -3
- package/dist/url.js +5 -1
- package/dist/versions.js +2 -2
- package/dist/words.js +4 -7
- package/package.json +15 -14
- package/dist/external/zod.js +0 -15223
- package/dist/validation/json-parser.d.ts +0 -58
- package/dist/validation/json-parser.js +0 -63
- package/dist/validation/types.d.ts +0 -118
- package/dist/zod.d.ts +0 -5
- /package/dist/{validation → schema}/types.js +0 -0
package/dist/promise-queue.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview Bounded concurrency promise queue.
|
|
3
3
|
* Exports the `PromiseQueue` class, which limits how many async tasks run
|
|
4
|
-
* simultaneously, supports an optional max queue length (
|
|
5
|
-
*
|
|
4
|
+
* simultaneously, supports an optional max queue length (new tasks beyond
|
|
5
|
+
* the cap are rejected with "Task dropped: queue length exceeded"), and
|
|
6
|
+
* exposes an idle-wait helper.
|
|
6
7
|
*/
|
|
7
8
|
export declare class PromiseQueue {
|
|
8
9
|
private queue;
|
|
@@ -13,13 +14,17 @@ export declare class PromiseQueue {
|
|
|
13
14
|
/**
|
|
14
15
|
* Creates a new PromiseQueue
|
|
15
16
|
* @param maxConcurrency - Maximum number of promises that can run concurrently
|
|
16
|
-
* @param maxQueueLength - Maximum queue size
|
|
17
|
+
* @param maxQueueLength - Maximum queue size; submissions past the cap
|
|
18
|
+
* reject with "Task dropped: queue length exceeded" instead of evicting
|
|
19
|
+
* a caller that has been waiting patiently. Callers must handle this
|
|
20
|
+
* rejection or they'll see an unhandled rejection.
|
|
17
21
|
*/
|
|
18
22
|
constructor(maxConcurrency: number, maxQueueLength?: number | undefined);
|
|
19
23
|
/**
|
|
20
24
|
* Add a task to the queue
|
|
21
25
|
* @param fn - Async function to execute
|
|
22
|
-
* @returns Promise that resolves with the function's result
|
|
26
|
+
* @returns Promise that resolves with the function's result, or rejects
|
|
27
|
+
* with "Task dropped: queue length exceeded" if the queue is full.
|
|
23
28
|
*/
|
|
24
29
|
add<T>(fn: () => Promise<T>): Promise<T>;
|
|
25
30
|
private runNext;
|
package/dist/promise-queue.js
CHANGED
|
@@ -32,7 +32,10 @@ class PromiseQueue {
|
|
|
32
32
|
/**
|
|
33
33
|
* Creates a new PromiseQueue
|
|
34
34
|
* @param maxConcurrency - Maximum number of promises that can run concurrently
|
|
35
|
-
* @param maxQueueLength - Maximum queue size
|
|
35
|
+
* @param maxQueueLength - Maximum queue size; submissions past the cap
|
|
36
|
+
* reject with "Task dropped: queue length exceeded" instead of evicting
|
|
37
|
+
* a caller that has been waiting patiently. Callers must handle this
|
|
38
|
+
* rejection or they'll see an unhandled rejection.
|
|
36
39
|
*/
|
|
37
40
|
constructor(maxConcurrency, maxQueueLength) {
|
|
38
41
|
this.maxConcurrency = maxConcurrency;
|
|
@@ -44,17 +47,16 @@ class PromiseQueue {
|
|
|
44
47
|
/**
|
|
45
48
|
* Add a task to the queue
|
|
46
49
|
* @param fn - Async function to execute
|
|
47
|
-
* @returns Promise that resolves with the function's result
|
|
50
|
+
* @returns Promise that resolves with the function's result, or rejects
|
|
51
|
+
* with "Task dropped: queue length exceeded" if the queue is full.
|
|
48
52
|
*/
|
|
49
53
|
async add(fn) {
|
|
50
54
|
return await new Promise((resolve, reject) => {
|
|
51
|
-
const task = { fn, resolve, reject };
|
|
52
55
|
if (this.maxQueueLength !== void 0 && this.queue.length >= this.maxQueueLength) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
droppedTask.reject(new Error("Task dropped: queue length exceeded"));
|
|
56
|
-
}
|
|
56
|
+
reject(new Error("Task dropped: queue length exceeded"));
|
|
57
|
+
return;
|
|
57
58
|
}
|
|
59
|
+
const task = { fn, resolve, reject };
|
|
58
60
|
this.queue.push(task);
|
|
59
61
|
this.runNext();
|
|
60
62
|
});
|
|
@@ -69,7 +71,7 @@ class PromiseQueue {
|
|
|
69
71
|
return;
|
|
70
72
|
}
|
|
71
73
|
this.running++;
|
|
72
|
-
task.fn().then(task.resolve).catch(task.reject).finally(() => {
|
|
74
|
+
Promise.resolve().then(() => task.fn()).then(task.resolve).catch(task.reject).finally(() => {
|
|
73
75
|
this.running--;
|
|
74
76
|
this.runNext();
|
|
75
77
|
});
|
package/dist/promises.d.ts
CHANGED
|
@@ -440,3 +440,44 @@ export declare function pRetry<T>(callbackFn: (...args: unknown[]) => Promise<T>
|
|
|
440
440
|
* // => { retries: 5, minTimeout: 200, maxTimeout: 5000, factor: 2 }
|
|
441
441
|
*/
|
|
442
442
|
export declare function resolveRetryOptions(options?: number | RetryOptions | undefined): RetryOptions;
|
|
443
|
+
/**
|
|
444
|
+
* Shape returned by {@link withResolvers}: a fresh pending promise plus
|
|
445
|
+
* the `resolve` / `reject` handles that settle it.
|
|
446
|
+
*
|
|
447
|
+
* Matches the spec return-shape exactly
|
|
448
|
+
* ([ECMA-262 §27.2.4.9](https://tc39.es/ecma262/#sec-promise.withResolvers)).
|
|
449
|
+
*/
|
|
450
|
+
export interface PromiseWithResolvers<T> {
|
|
451
|
+
/** The pending promise. */
|
|
452
|
+
promise: Promise<T>;
|
|
453
|
+
/** Resolves {@link promise} with the given value (or thenable). */
|
|
454
|
+
resolve: (value: T | PromiseLike<T>) => void;
|
|
455
|
+
/** Rejects {@link promise} with the given reason. */
|
|
456
|
+
reject: (reason?: unknown) => void;
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Create a pending promise together with its `resolve` and `reject`
|
|
460
|
+
* handles as first-class values, per
|
|
461
|
+
* [ECMA-262 §27.2.4.9](https://tc39.es/ecma262/#sec-promise.withResolvers).
|
|
462
|
+
*
|
|
463
|
+
* Bound to native `Promise.withResolvers` when available (Node 20.12+ /
|
|
464
|
+
* 21+ / 22+; V8 ≥ 12.0); otherwise falls back to a spec-equivalent
|
|
465
|
+
* `new Promise(executor)` implementation that captures the handles via
|
|
466
|
+
* closure. The returned object always has own data properties `promise`,
|
|
467
|
+
* `resolve`, `reject` on `Object.prototype` — writable, enumerable, and
|
|
468
|
+
* configurable — matching the spec's `CreateDataPropertyOrThrow` steps.
|
|
469
|
+
*
|
|
470
|
+
* Use this instead of the manual
|
|
471
|
+
* `let resolve; const p = new Promise(r => { resolve = r })` dance for
|
|
472
|
+
* deferred-resolution patterns (event-driven bridges, adapter layers,
|
|
473
|
+
* handshake signaling) where the settle path lives outside the executor.
|
|
474
|
+
*
|
|
475
|
+
* @example
|
|
476
|
+
* ```typescript
|
|
477
|
+
* const { promise, resolve, reject } = withResolvers<string>()
|
|
478
|
+
* emitter.once('ready', () => resolve('ok'))
|
|
479
|
+
* emitter.once('error', err => reject(err))
|
|
480
|
+
* const result = await promise
|
|
481
|
+
* ```
|
|
482
|
+
*/
|
|
483
|
+
export declare const withResolvers: <T>() => PromiseWithResolvers<T>;
|
package/dist/promises.js
CHANGED
|
@@ -27,7 +27,8 @@ __export(promises_exports, {
|
|
|
27
27
|
pFilter: () => pFilter,
|
|
28
28
|
pFilterChunk: () => pFilterChunk,
|
|
29
29
|
pRetry: () => pRetry,
|
|
30
|
-
resolveRetryOptions: () => resolveRetryOptions
|
|
30
|
+
resolveRetryOptions: () => resolveRetryOptions,
|
|
31
|
+
withResolvers: () => withResolvers
|
|
31
32
|
});
|
|
32
33
|
module.exports = __toCommonJS(promises_exports);
|
|
33
34
|
var import_arrays = require("./arrays");
|
|
@@ -258,6 +259,21 @@ function resolveRetryOptions(options) {
|
|
|
258
259
|
}
|
|
259
260
|
return options ? { ...defaults, ...options } : defaults;
|
|
260
261
|
}
|
|
262
|
+
const maybeNativeWithResolvers = Promise.withResolvers;
|
|
263
|
+
const withResolvers = typeof maybeNativeWithResolvers === "function" ? (
|
|
264
|
+
// Bind so callers who destructure the export don't lose `this`.
|
|
265
|
+
maybeNativeWithResolvers.bind(
|
|
266
|
+
Promise
|
|
267
|
+
)
|
|
268
|
+
) : () => {
|
|
269
|
+
let resolve;
|
|
270
|
+
let reject;
|
|
271
|
+
const promise = new Promise((res, rej) => {
|
|
272
|
+
resolve = res;
|
|
273
|
+
reject = rej;
|
|
274
|
+
});
|
|
275
|
+
return { promise, resolve, reject };
|
|
276
|
+
};
|
|
261
277
|
// Annotate the CommonJS export names for ESM import in node:
|
|
262
278
|
0 && (module.exports = {
|
|
263
279
|
normalizeIterationOptions,
|
|
@@ -267,5 +283,6 @@ function resolveRetryOptions(options) {
|
|
|
267
283
|
pFilter,
|
|
268
284
|
pFilterChunk,
|
|
269
285
|
pRetry,
|
|
270
|
-
resolveRetryOptions
|
|
286
|
+
resolveRetryOptions,
|
|
287
|
+
withResolvers
|
|
271
288
|
});
|
package/dist/regexps.d.ts
CHANGED
|
@@ -1,15 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @fileoverview Regular expression utilities including
|
|
3
|
-
* Provides regex escaping and pattern matching
|
|
2
|
+
* @fileoverview Regular expression utilities including a spec-compliant
|
|
3
|
+
* `RegExp.escape` fallback. Provides regex escaping and pattern matching
|
|
4
|
+
* helpers.
|
|
4
5
|
*/
|
|
5
|
-
|
|
6
|
-
* Escape special characters in a string for use in a regular expression.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* escapeRegExp('foo.bar') // 'foo\\.bar'
|
|
11
|
-
* escapeRegExp('a+b*c?') // 'a\\+b\\*c\\?'
|
|
12
|
-
* new RegExp(escapeRegExp('[test]')) // /\[test\]/
|
|
13
|
-
* ```
|
|
14
|
-
*/
|
|
15
|
-
export declare function escapeRegExp(str: string): string;
|
|
6
|
+
export declare const escapeRegExp: (str: string) => string;
|
package/dist/regexps.js
CHANGED
|
@@ -23,10 +23,67 @@ __export(regexps_exports, {
|
|
|
23
23
|
escapeRegExp: () => escapeRegExp
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(regexps_exports);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
const SYNTAX_CHARACTERS = new Set("^$\\.*+?()[]{}|/");
|
|
27
|
+
const CONTROL_ESCAPES = /* @__PURE__ */ new Map([
|
|
28
|
+
[9, "\\t"],
|
|
29
|
+
[10, "\\n"],
|
|
30
|
+
[11, "\\v"],
|
|
31
|
+
[12, "\\f"],
|
|
32
|
+
[13, "\\r"]
|
|
33
|
+
]);
|
|
34
|
+
const OTHER_PUNCTUATORS = new Set(",-=<>#&!%:;@~'`\"");
|
|
35
|
+
function isSpecHexEscapeCp(cp) {
|
|
36
|
+
if (OTHER_PUNCTUATORS.has(String.fromCodePoint(cp))) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
if (cp === 10 || cp === 13 || cp === 8232 || cp === 8233) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
if (cp === 9 || cp === 11 || cp === 12 || cp === 32 || cp === 160 || cp === 65279) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
if (cp >= 55296 && cp <= 57343) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
function hex2(n) {
|
|
51
|
+
return n.toString(16).padStart(2, "0");
|
|
52
|
+
}
|
|
53
|
+
function hex4(n) {
|
|
54
|
+
return n.toString(16).padStart(4, "0");
|
|
55
|
+
}
|
|
56
|
+
function escapeRegExpFallback(str) {
|
|
57
|
+
let out = "";
|
|
58
|
+
let isFirst = true;
|
|
59
|
+
for (const char of str) {
|
|
60
|
+
const cp = char.codePointAt(0);
|
|
61
|
+
if (isFirst && (cp >= 48 && cp <= 57 || cp >= 65 && cp <= 90 || cp >= 97 && cp <= 122)) {
|
|
62
|
+
out += "\\x" + hex2(cp);
|
|
63
|
+
} else if (SYNTAX_CHARACTERS.has(char)) {
|
|
64
|
+
out += "\\" + char;
|
|
65
|
+
} else {
|
|
66
|
+
const ctrl = CONTROL_ESCAPES.get(cp);
|
|
67
|
+
if (ctrl !== void 0) {
|
|
68
|
+
out += ctrl;
|
|
69
|
+
} else if (isSpecHexEscapeCp(cp)) {
|
|
70
|
+
if (cp <= 255) {
|
|
71
|
+
out += "\\x" + hex2(cp);
|
|
72
|
+
} else {
|
|
73
|
+
for (let i = 0; i < char.length; i++) {
|
|
74
|
+
out += "\\u" + hex4(char.charCodeAt(i));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
out += char;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
isFirst = false;
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
29
84
|
}
|
|
85
|
+
const maybeNativeEscape = RegExp.escape;
|
|
86
|
+
const escapeRegExp = typeof maybeNativeEscape === "function" ? maybeNativeEscape : escapeRegExpFallback;
|
|
30
87
|
// Annotate the CommonJS export names for ESM import in node:
|
|
31
88
|
0 && (module.exports = {
|
|
32
89
|
escapeRegExp
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Throwing twin of `validateSchema`.
|
|
3
|
+
*
|
|
4
|
+
* Use `parseSchema(schema, data)` for fail-fast trust boundaries (app
|
|
5
|
+
* startup, config files, internal assertions). Use the non-throwing
|
|
6
|
+
* `validateSchema` for recoverable input (form fields, API request bodies,
|
|
7
|
+
* anywhere errors need to surface to a user).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { z } from 'zod'
|
|
12
|
+
* import { parseSchema } from '@socketsecurity/lib/schema/parse'
|
|
13
|
+
*
|
|
14
|
+
* const Config = z.object({ host: z.string(), port: z.number() })
|
|
15
|
+
* const config = parseSchema(Config, json) // throws on invalid
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
import type { Infer } from './types';
|
|
19
|
+
/**
|
|
20
|
+
* Parse `data` against `schema` and return the validated value.
|
|
21
|
+
*
|
|
22
|
+
* @throws {Error} When validation fails. The message lists all issues as
|
|
23
|
+
* `path: message, path: message, ...`. Use `validateSchema` if you need
|
|
24
|
+
* structured access to the error list.
|
|
25
|
+
*/
|
|
26
|
+
export declare function parseSchema<S>(schema: S, data: unknown): Infer<S>;
|
|
@@ -18,13 +18,21 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
20
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
-
var
|
|
22
|
-
__export(
|
|
23
|
-
|
|
21
|
+
var parse_exports = {};
|
|
22
|
+
__export(parse_exports, {
|
|
23
|
+
parseSchema: () => parseSchema
|
|
24
24
|
});
|
|
25
|
-
module.exports = __toCommonJS(
|
|
26
|
-
var
|
|
25
|
+
module.exports = __toCommonJS(parse_exports);
|
|
26
|
+
var import_validate = require("./validate");
|
|
27
|
+
function parseSchema(schema, data) {
|
|
28
|
+
const result = (0, import_validate.validateSchema)(schema, data);
|
|
29
|
+
if (result.ok) {
|
|
30
|
+
return result.value;
|
|
31
|
+
}
|
|
32
|
+
const summary = result.errors.map((e) => `${e.path.join(".") || "(root)"}: ${e.message}`).join(", ");
|
|
33
|
+
throw new Error(`Validation failed: ${summary}`);
|
|
34
|
+
}
|
|
27
35
|
// Annotate the CommonJS export names for ESM import in node:
|
|
28
36
|
0 && (module.exports = {
|
|
29
|
-
|
|
37
|
+
parseSchema
|
|
30
38
|
});
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Shared types for schema validation.
|
|
3
|
+
*
|
|
4
|
+
* `Schema<T>` is the Zod-shaped duck-type contract — any validator with
|
|
5
|
+
* a `.safeParse(data)` method returning `{ success, data?, error? }`
|
|
6
|
+
* satisfies it. socket-lib detects Zod (v3 and v4) structurally via this
|
|
7
|
+
* interface; consumers bring their own Zod.
|
|
8
|
+
*
|
|
9
|
+
* `ValidateResult<T>` / `ValidationIssue` / `Infer<S>` / `AnySchema` are
|
|
10
|
+
* the normalized shapes produced by `@socketsecurity/lib/schema/validate`
|
|
11
|
+
* and `@socketsecurity/lib/schema/parse`.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Result of a Zod-shaped schema's `.safeParse()` call.
|
|
15
|
+
*
|
|
16
|
+
* @template T - The expected type of the parsed data
|
|
17
|
+
*/
|
|
18
|
+
export interface ParseResult<T> {
|
|
19
|
+
/** Indicates whether parsing was successful */
|
|
20
|
+
success: boolean;
|
|
21
|
+
/** Parsed and validated data (only present when `success` is `true`) */
|
|
22
|
+
data?: T | undefined;
|
|
23
|
+
/** Error information (only present when `success` is `false`) */
|
|
24
|
+
error?: unknown;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Zod-shaped duck-type for any validator exposing `safeParse` / `parse`.
|
|
28
|
+
*
|
|
29
|
+
* @template T - The expected output type after validation
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { z } from 'zod'
|
|
34
|
+
*
|
|
35
|
+
* const userSchema = z.object({ name: z.string(), age: z.number() })
|
|
36
|
+
*
|
|
37
|
+
* // Schema satisfies this interface
|
|
38
|
+
* const schema: Schema<User> = userSchema
|
|
39
|
+
* const result = schema.safeParse({ name: 'Alice', age: 30 })
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export interface Schema<T = unknown> {
|
|
43
|
+
/** Non-throwing parse. */
|
|
44
|
+
safeParse(data: unknown): ParseResult<T>;
|
|
45
|
+
/** Throwing parse. */
|
|
46
|
+
parse(data: unknown): T;
|
|
47
|
+
/** Optional schema name for debugging. */
|
|
48
|
+
_name?: string | undefined;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Internal structural shape of a Zod v4 schema — carries the inferred
|
|
52
|
+
* output type on `_zod.output`. Used for type-only detection in `Infer<S>`.
|
|
53
|
+
*
|
|
54
|
+
* @internal
|
|
55
|
+
*/
|
|
56
|
+
interface ZodV4LikeSchema<O = unknown> {
|
|
57
|
+
_zod: {
|
|
58
|
+
output: O;
|
|
59
|
+
};
|
|
60
|
+
safeParse(data: unknown): unknown;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Internal structural shape of a Zod v3 schema — carries the inferred
|
|
64
|
+
* output type on `_output`. Used for type-only detection in `Infer<S>`.
|
|
65
|
+
*
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
interface ZodV3LikeSchema<O = unknown> {
|
|
69
|
+
_output: O;
|
|
70
|
+
safeParse(data: unknown): unknown;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Internal structural shape of a TypeBox `TSchema` — carries the inferred
|
|
74
|
+
* output type on the phantom `static` field. Only used inside socket-lib
|
|
75
|
+
* for type-only detection in `Infer<S>`; external callers should pass
|
|
76
|
+
* Zod schemas.
|
|
77
|
+
*
|
|
78
|
+
* @internal
|
|
79
|
+
*/
|
|
80
|
+
interface TypeBoxLikeSchema {
|
|
81
|
+
static: unknown;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Any schema kind the validators accept.
|
|
85
|
+
*/
|
|
86
|
+
export type AnySchema = ZodV4LikeSchema<unknown> | ZodV3LikeSchema<unknown> | TypeBoxLikeSchema | Schema<unknown>;
|
|
87
|
+
/**
|
|
88
|
+
* Infer the validated output type from any supported schema kind.
|
|
89
|
+
*
|
|
90
|
+
* Order matters: TypeBox schemas carry a phantom `static` field, so we
|
|
91
|
+
* check for TypeBox before falling through to Zod and the duck-type.
|
|
92
|
+
*/
|
|
93
|
+
export type Infer<S> = S extends {
|
|
94
|
+
static: infer Static;
|
|
95
|
+
} ? Static : S extends {
|
|
96
|
+
_zod: {
|
|
97
|
+
output: infer O;
|
|
98
|
+
};
|
|
99
|
+
} ? O : S extends {
|
|
100
|
+
_output: infer O;
|
|
101
|
+
} ? O : S extends Schema<infer T> ? T : unknown;
|
|
102
|
+
/**
|
|
103
|
+
* A single normalized validation error.
|
|
104
|
+
*/
|
|
105
|
+
export interface ValidationIssue {
|
|
106
|
+
/** Array path into the value (e.g. `['user', 'age']`). */
|
|
107
|
+
path: Array<string | number>;
|
|
108
|
+
/** Human-readable description of the failure. */
|
|
109
|
+
message: string;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Tagged-union result of `validateSchema`. Callers narrow on `ok`.
|
|
113
|
+
*/
|
|
114
|
+
export type ValidateResult<T> = {
|
|
115
|
+
ok: true;
|
|
116
|
+
value: T;
|
|
117
|
+
} | {
|
|
118
|
+
ok: false;
|
|
119
|
+
errors: ValidationIssue[];
|
|
120
|
+
};
|
|
121
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Universal schema validator — non-throwing.
|
|
3
|
+
*
|
|
4
|
+
* Accepts any Zod-shaped schema (`.safeParse`-exposing) and returns a tagged
|
|
5
|
+
* result `{ ok: true, value } | { ok: false, errors }` with normalized
|
|
6
|
+
* `{ path, message }` issues. No runtime dependency on `zod` — detection
|
|
7
|
+
* is purely structural.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
* socket-lib additionally recognizes TypeBox schemas for its own internal
|
|
11
|
+
* use (e.g. `src/ipc.ts`'s stub-file validation). That path is not a
|
|
12
|
+
* supported consumer API.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { z } from 'zod'
|
|
17
|
+
* import { validateSchema } from '@socketsecurity/lib/schema/validate'
|
|
18
|
+
*
|
|
19
|
+
* const User = z.object({ name: z.string() })
|
|
20
|
+
* const r = validateSchema(User, data)
|
|
21
|
+
* if (r.ok) r.value.name // string
|
|
22
|
+
* else r.errors // ValidationIssue[]
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
import type { Infer, ValidateResult } from './types';
|
|
26
|
+
/**
|
|
27
|
+
* Validate `data` against a Zod-style `schema`. Non-throwing.
|
|
28
|
+
*
|
|
29
|
+
* The return type narrows `value` to `Infer<S>`, so callers get
|
|
30
|
+
* `z.infer<typeof S>` with no casts. Errors are normalized to
|
|
31
|
+
* `{ path, message }` regardless of the underlying validator.
|
|
32
|
+
*
|
|
33
|
+
* @throws {TypeError} When `schema` is not a recognized validator kind.
|
|
34
|
+
*/
|
|
35
|
+
export declare function validateSchema<S>(schema: S, data: unknown): ValidateResult<Infer<S>>;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* Socket Lib - Built with esbuild */
|
|
3
|
+
"use strict";
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
+
var validate_exports = {};
|
|
22
|
+
__export(validate_exports, {
|
|
23
|
+
validateSchema: () => validateSchema
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(validate_exports);
|
|
26
|
+
function isTypeBoxSchema(schema) {
|
|
27
|
+
if (schema === null || typeof schema !== "object") {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
for (const sym of Object.getOwnPropertySymbols(schema)) {
|
|
31
|
+
if (sym.description === "TypeBox.Kind") {
|
|
32
|
+
return typeof schema[sym] === "string";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
function normalizeTypeBoxErrors(errors) {
|
|
38
|
+
const out = [];
|
|
39
|
+
for (const err of errors) {
|
|
40
|
+
const segs = err.path.split("/").filter(Boolean);
|
|
41
|
+
out.push({
|
|
42
|
+
path: segs.map((s) => {
|
|
43
|
+
const n = Number(s);
|
|
44
|
+
return Number.isInteger(n) && String(n) === s ? n : s;
|
|
45
|
+
}),
|
|
46
|
+
message: err.message
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
function normalizeZodError(err) {
|
|
52
|
+
if (err === null || typeof err !== "object") {
|
|
53
|
+
return [{ path: [], message: String(err) }];
|
|
54
|
+
}
|
|
55
|
+
const issues = err.issues;
|
|
56
|
+
if (!Array.isArray(issues)) {
|
|
57
|
+
return [{ path: [], message: "Unknown validation error" }];
|
|
58
|
+
}
|
|
59
|
+
return issues.map((issue) => {
|
|
60
|
+
const i = issue;
|
|
61
|
+
return {
|
|
62
|
+
path: Array.isArray(i.path) ? i.path : [],
|
|
63
|
+
message: typeof i.message === "string" ? i.message : "Invalid value"
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
function validateSchema(schema, data) {
|
|
68
|
+
if (isTypeBoxSchema(schema)) {
|
|
69
|
+
const { Value } = require("../external/@sinclair/typebox/value");
|
|
70
|
+
if (Value.Check(schema, data)) {
|
|
71
|
+
return { ok: true, value: data };
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
ok: false,
|
|
75
|
+
errors: normalizeTypeBoxErrors(Value.Errors(schema, data))
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
if (schema !== null && typeof schema === "object" && typeof schema.safeParse === "function") {
|
|
79
|
+
const result = schema.safeParse(data);
|
|
80
|
+
if (result.success === true) {
|
|
81
|
+
return {
|
|
82
|
+
ok: true,
|
|
83
|
+
value: result.data
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
ok: false,
|
|
88
|
+
errors: normalizeZodError(result.error)
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
throw new TypeError(
|
|
92
|
+
"validateSchema: unsupported schema kind. Expected a Zod schema or an object with a safeParse method."
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
96
|
+
0 && (module.exports = {
|
|
97
|
+
validateSchema
|
|
98
|
+
});
|
package/dist/stdio/progress.js
CHANGED
|
@@ -95,7 +95,6 @@ function suppressWarningType(warningType) {
|
|
|
95
95
|
}
|
|
96
96
|
async function withSuppressedWarnings(warningType, callback) {
|
|
97
97
|
const wasAlreadySuppressed = suppressedWarnings.has(warningType);
|
|
98
|
-
const original = import_node_process.default.emitWarning;
|
|
99
98
|
suppressWarningType(warningType);
|
|
100
99
|
try {
|
|
101
100
|
return await callback();
|
|
@@ -103,7 +102,6 @@ async function withSuppressedWarnings(warningType, callback) {
|
|
|
103
102
|
if (!wasAlreadySuppressed) {
|
|
104
103
|
suppressedWarnings.delete(warningType);
|
|
105
104
|
}
|
|
106
|
-
import_node_process.default.emitWarning = original;
|
|
107
105
|
}
|
|
108
106
|
}
|
|
109
107
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/tables.js
CHANGED
|
@@ -37,11 +37,10 @@ module.exports = __toCommonJS(tables_exports);
|
|
|
37
37
|
var import_yoctocolors_cjs = __toESM(require("./external/yoctocolors-cjs"));
|
|
38
38
|
var import_strings = require("./strings");
|
|
39
39
|
function displayWidth(text) {
|
|
40
|
-
return (0, import_strings.
|
|
40
|
+
return (0, import_strings.stringWidth)(text);
|
|
41
41
|
}
|
|
42
42
|
function padText(text, width, align = "left") {
|
|
43
|
-
const
|
|
44
|
-
const textWidth = stripped.length;
|
|
43
|
+
const textWidth = displayWidth(text);
|
|
45
44
|
const padding = Math.max(0, width - textWidth);
|
|
46
45
|
switch (align) {
|
|
47
46
|
case "right":
|
package/dist/url.js
CHANGED
|
@@ -73,7 +73,11 @@ function urlSearchParamAsBoolean(value, options) {
|
|
|
73
73
|
};
|
|
74
74
|
if (typeof value === "string") {
|
|
75
75
|
const trimmed = value.trim();
|
|
76
|
-
|
|
76
|
+
if (trimmed === "") {
|
|
77
|
+
return !!defaultValue;
|
|
78
|
+
}
|
|
79
|
+
const lowered = trimmed.toLowerCase();
|
|
80
|
+
return lowered === "1" || lowered === "true" || lowered === "yes" || lowered === "on";
|
|
77
81
|
}
|
|
78
82
|
if (value === null || value === void 0) {
|
|
79
83
|
return !!defaultValue;
|
package/dist/versions.js
CHANGED
|
@@ -111,11 +111,11 @@ function isValidVersion(version) {
|
|
|
111
111
|
}
|
|
112
112
|
function maxVersion(versions) {
|
|
113
113
|
const semver = getSemver();
|
|
114
|
-
return semver.maxSatisfying(versions, "*") || void 0;
|
|
114
|
+
return semver.maxSatisfying(versions, "*", { includePrerelease: true }) || void 0;
|
|
115
115
|
}
|
|
116
116
|
function minVersion(versions) {
|
|
117
117
|
const semver = getSemver();
|
|
118
|
-
return semver.minSatisfying(versions, "*") || void 0;
|
|
118
|
+
return semver.minSatisfying(versions, "*", { includePrerelease: true }) || void 0;
|
|
119
119
|
}
|
|
120
120
|
function parseVersion(version) {
|
|
121
121
|
const semver = getSemver();
|
package/dist/words.js
CHANGED
|
@@ -27,18 +27,15 @@ __export(words_exports, {
|
|
|
27
27
|
module.exports = __toCommonJS(words_exports);
|
|
28
28
|
// @__NO_SIDE_EFFECTS__
|
|
29
29
|
function capitalize(word) {
|
|
30
|
-
|
|
31
|
-
if (length === 0) {
|
|
30
|
+
if (word.length === 0) {
|
|
32
31
|
return word;
|
|
33
32
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
return `${word.charAt(0).toUpperCase()}${word.slice(1).toLowerCase()}`;
|
|
33
|
+
const [first, ...rest] = [...word];
|
|
34
|
+
return (first ?? "").toUpperCase() + rest.join("").toLowerCase();
|
|
38
35
|
}
|
|
39
36
|
// @__NO_SIDE_EFFECTS__
|
|
40
37
|
function determineArticle(word) {
|
|
41
|
-
return /^[aeiou]
|
|
38
|
+
return /^[aeiou]/i.test(word) ? "an" : "a";
|
|
42
39
|
}
|
|
43
40
|
// @__NO_SIDE_EFFECTS__
|
|
44
41
|
function pluralize(word, options) {
|