@socketsecurity/lib 5.18.2 → 5.19.1
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 +57 -0
- package/README.md +6 -5
- package/dist/abort.d.ts +3 -0
- package/dist/abort.js +1 -0
- package/dist/agent.d.ts +66 -49
- package/dist/agent.js +51 -50
- package/dist/ansi.d.ts +2 -5
- package/dist/ansi.js +3 -2
- package/dist/archives.d.ts +25 -21
- package/dist/archives.js +35 -34
- package/dist/argv/flags.d.ts +100 -91
- package/dist/argv/flags.js +88 -87
- package/dist/argv/parse.d.ts +30 -40
- package/dist/argv/parse.js +33 -32
- package/dist/arrays.d.ts +4 -8
- package/dist/arrays.js +1 -0
- package/dist/bin.d.ts +7 -5
- package/dist/bin.js +52 -29
- package/dist/cacache.d.ts +13 -12
- package/dist/cacache.js +9 -8
- package/dist/cache-with-ttl.d.ts +53 -35
- package/dist/cache-with-ttl.js +1 -0
- package/dist/colors.d.ts +5 -5
- package/dist/colors.js +1 -0
- package/dist/constants/agents.d.ts +5 -9
- package/dist/constants/agents.js +3 -2
- package/dist/constants/core.d.ts +5 -10
- package/dist/constants/core.js +2 -1
- package/dist/constants/encoding.d.ts +3 -3
- package/dist/constants/encoding.js +1 -0
- package/dist/constants/github.d.ts +3 -3
- package/dist/constants/github.js +1 -0
- package/dist/constants/licenses.d.ts +9 -2
- package/dist/constants/licenses.js +1 -0
- package/dist/constants/lifecycle-script-names.js +1 -0
- package/dist/constants/maintained-node-versions.d.ts +3 -0
- package/dist/constants/maintained-node-versions.js +1 -0
- package/dist/constants/node.d.ts +125 -16
- package/dist/constants/node.js +74 -73
- package/dist/constants/package-default-node-range.d.ts +3 -1
- package/dist/constants/package-default-node-range.js +2 -1
- package/dist/constants/package-default-socket-categories.d.ts +0 -1
- package/dist/constants/package-default-socket-categories.js +1 -0
- package/dist/constants/packages.d.ts +12 -17
- package/dist/constants/packages.js +31 -16
- package/dist/constants/platform.d.ts +1 -2
- package/dist/constants/platform.js +2 -1
- package/dist/constants/process.d.ts +18 -0
- package/dist/constants/process.js +1 -0
- package/dist/constants/socket.d.ts +4 -10
- package/dist/constants/socket.js +2 -1
- package/dist/constants/testing.d.ts +3 -3
- package/dist/constants/testing.js +1 -0
- package/dist/constants/time.d.ts +3 -4
- package/dist/constants/time.js +1 -0
- package/dist/constants/typescript.d.ts +10 -3
- package/dist/constants/typescript.js +5 -4
- package/dist/cover/code.d.ts +3 -0
- package/dist/cover/code.js +12 -12
- package/dist/cover/formatters.d.ts +12 -9
- package/dist/cover/formatters.js +19 -18
- package/dist/cover/type.d.ts +3 -0
- package/dist/cover/type.js +1 -0
- package/dist/cover/types.js +1 -0
- package/dist/debug.d.ts +32 -47
- package/dist/debug.js +97 -96
- package/dist/dlx/arborist.d.ts +134 -0
- package/dist/dlx/arborist.js +177 -0
- package/dist/dlx/binary.d.ts +25 -9
- package/dist/dlx/binary.js +34 -12
- package/dist/dlx/cache.d.ts +1 -0
- package/dist/dlx/cache.js +2 -1
- package/dist/dlx/detect.d.ts +15 -0
- package/dist/dlx/detect.js +28 -20
- package/dist/dlx/dir.d.ts +1 -11
- package/dist/dlx/dir.js +2 -12
- package/dist/dlx/integrity.d.ts +86 -0
- package/dist/dlx/integrity.js +112 -0
- package/dist/dlx/lockfile.d.ts +115 -0
- package/dist/dlx/lockfile.js +139 -0
- package/dist/dlx/manifest.d.ts +26 -1
- package/dist/dlx/manifest.js +9 -6
- package/dist/dlx/package.d.ts +58 -3
- package/dist/dlx/package.js +101 -74
- package/dist/dlx/packages.d.ts +1 -11
- package/dist/dlx/packages.js +3 -14
- package/dist/dlx/paths.d.ts +1 -0
- package/dist/dlx/paths.js +2 -1
- package/dist/effects/pulse-frames.js +1 -0
- package/dist/effects/text-shimmer.d.ts +13 -2
- package/dist/effects/text-shimmer.js +50 -46
- package/dist/effects/types.js +1 -0
- package/dist/effects/ultra.js +1 -0
- package/dist/env/ci.d.ts +6 -1
- package/dist/env/ci.js +1 -0
- package/dist/env/debug.d.ts +5 -1
- package/dist/env/debug.js +1 -0
- package/dist/env/github.d.ts +19 -23
- package/dist/env/github.js +5 -4
- package/dist/env/helpers.d.ts +21 -9
- package/dist/env/helpers.js +5 -11
- package/dist/env/home.d.ts +17 -4
- package/dist/env/home.js +2 -1
- package/dist/env/locale.d.ts +4 -3
- package/dist/env/locale.js +1 -0
- package/dist/env/node-auth-token.d.ts +5 -1
- package/dist/env/node-auth-token.js +1 -0
- package/dist/env/node-env.d.ts +5 -1
- package/dist/env/node-env.js +1 -0
- package/dist/env/npm.d.ts +4 -5
- package/dist/env/npm.js +1 -0
- package/dist/env/package-manager.d.ts +4 -3
- package/dist/env/package-manager.js +2 -1
- package/dist/env/path.d.ts +5 -1
- package/dist/env/path.js +1 -0
- package/dist/env/pre-commit.d.ts +5 -1
- package/dist/env/pre-commit.js +1 -0
- package/dist/env/rewire.d.ts +10 -0
- package/dist/env/rewire.js +7 -5
- package/dist/env/shell.d.ts +5 -1
- package/dist/env/shell.js +1 -0
- package/dist/env/socket-cli-shadow.d.ts +4 -5
- package/dist/env/socket-cli-shadow.js +1 -0
- package/dist/env/socket-cli.d.ts +4 -14
- package/dist/env/socket-cli.js +1 -0
- package/dist/env/socket.d.ts +6 -18
- package/dist/env/socket.js +4 -3
- package/dist/env/temp-dir.d.ts +4 -3
- package/dist/env/temp-dir.js +1 -0
- package/dist/env/term.d.ts +5 -1
- package/dist/env/term.js +1 -0
- package/dist/env/test.d.ts +4 -3
- package/dist/env/test.js +1 -0
- package/dist/env/windows.d.ts +4 -4
- package/dist/env/windows.js +1 -0
- package/dist/env/xdg.d.ts +4 -3
- package/dist/env/xdg.js +1 -0
- package/dist/env.d.ts +83 -23
- package/dist/env.js +112 -80
- package/dist/errors.js +1 -0
- package/dist/external/@socketregistry/packageurl-js.js +2 -2
- package/dist/external/external-pack.js +1562 -1673
- package/dist/external/npm-pack.js +5 -1
- package/dist/fs.d.ts +92 -113
- package/dist/fs.js +35 -35
- package/dist/git.d.ts +170 -164
- package/dist/git.js +113 -87
- package/dist/github.d.ts +249 -227
- package/dist/github.js +82 -81
- package/dist/globs.d.ts +31 -17
- package/dist/globs.js +51 -58
- package/dist/http-request.d.ts +99 -99
- package/dist/http-request.js +182 -156
- package/dist/ipc-cli.d.ts +28 -0
- package/dist/{utils/get-ipc.js → ipc-cli.js} +4 -3
- package/dist/ipc.d.ts +9 -299
- package/dist/ipc.js +18 -204
- package/dist/json/edit.d.ts +3 -1
- package/dist/json/edit.js +30 -30
- package/dist/json/format.d.ts +36 -36
- package/dist/json/format.js +22 -21
- package/dist/json/parse.d.ts +4 -2
- package/dist/json/parse.js +1 -0
- package/dist/json/types.js +1 -0
- package/dist/{links/index.d.ts → links.d.ts} +6 -2
- package/dist/{links/index.js → links.js} +5 -4
- package/dist/logger.d.ts +4 -6
- package/dist/logger.js +61 -53
- package/dist/memoization.d.ts +52 -49
- package/dist/memoization.js +55 -33
- package/dist/objects.d.ts +25 -44
- package/dist/objects.js +2 -1
- package/dist/package-extensions.d.ts +6 -0
- package/dist/package-extensions.js +2 -1
- package/dist/packages/edit.d.ts +3 -5
- package/dist/packages/edit.js +4 -3
- package/dist/packages/exports.d.ts +11 -14
- package/dist/packages/exports.js +11 -10
- package/dist/packages/isolation.d.ts +4 -0
- package/dist/packages/isolation.js +20 -19
- package/dist/packages/licenses.d.ts +3 -10
- package/dist/packages/licenses.js +2 -1
- package/dist/packages/manifest.d.ts +3 -3
- package/dist/packages/manifest.js +1 -0
- package/dist/packages/normalize.d.ts +3 -4
- package/dist/packages/normalize.js +1 -0
- package/dist/packages/operations.d.ts +3 -10
- package/dist/packages/operations.js +1 -0
- package/dist/packages/provenance.d.ts +10 -8
- package/dist/packages/provenance.js +55 -54
- package/dist/packages/specs.d.ts +3 -5
- package/dist/packages/specs.js +1 -0
- package/dist/packages/validation.d.ts +3 -3
- package/dist/packages/validation.js +1 -0
- package/dist/packages.d.ts +2 -17
- package/dist/packages.js +2 -1
- package/dist/paths/dirnames.d.ts +0 -2
- package/dist/paths/dirnames.js +1 -0
- package/dist/paths/exts.d.ts +0 -1
- package/dist/paths/exts.js +1 -0
- package/dist/paths/filenames.d.ts +0 -1
- package/dist/paths/filenames.js +1 -0
- package/dist/paths/globs.d.ts +0 -1
- package/dist/paths/globs.js +1 -0
- package/dist/paths/normalize.d.ts +115 -123
- package/dist/paths/normalize.js +128 -127
- package/dist/paths/packages.d.ts +3 -2
- package/dist/paths/packages.js +2 -1
- package/dist/paths/rewire.d.ts +9 -0
- package/dist/paths/rewire.js +5 -3
- package/dist/paths/socket.d.ts +37 -23
- package/dist/paths/socket.js +26 -25
- package/dist/performance.d.ts +87 -84
- package/dist/performance.js +97 -96
- package/dist/process-lock.d.ts +41 -1
- package/dist/process-lock.js +24 -17
- package/dist/promise-queue.d.ts +6 -0
- package/dist/promise-queue.js +1 -0
- package/dist/promises.d.ts +40 -55
- package/dist/promises.js +12 -11
- package/dist/regexps.d.ts +0 -5
- package/dist/regexps.js +1 -0
- package/dist/releases/github.d.ts +59 -63
- package/dist/releases/github.js +95 -100
- package/dist/releases/socket-btm.d.ts +9 -2
- package/dist/releases/socket-btm.js +32 -25
- package/dist/sea.d.ts +5 -0
- package/dist/sea.js +1 -0
- package/dist/shadow.d.ts +4 -0
- package/dist/shadow.js +1 -0
- package/dist/signal-exit.d.ts +7 -7
- package/dist/signal-exit.js +46 -45
- package/dist/sorts.d.ts +5 -7
- package/dist/sorts.js +11 -10
- package/dist/spawn.d.ts +96 -77
- package/dist/spawn.js +39 -38
- package/dist/spinner.d.ts +21 -22
- package/dist/spinner.js +15 -11
- package/dist/ssri.d.ts +31 -36
- package/dist/ssri.js +15 -14
- package/dist/stdio/_stream.d.ts +31 -0
- package/dist/stdio/_stream.js +57 -0
- package/dist/stdio/clear.d.ts +38 -15
- package/dist/stdio/clear.js +5 -4
- package/dist/stdio/divider.d.ts +40 -36
- package/dist/stdio/divider.js +10 -9
- package/dist/stdio/footer.d.ts +20 -0
- package/dist/stdio/footer.js +12 -2
- package/dist/stdio/header.d.ts +4 -16
- package/dist/stdio/header.js +1 -9
- package/dist/stdio/progress.d.ts +4 -0
- package/dist/stdio/progress.js +13 -8
- package/dist/stdio/prompts.d.ts +24 -23
- package/dist/stdio/prompts.js +10 -9
- package/dist/stdio/stderr.d.ts +51 -39
- package/dist/stdio/stderr.js +19 -22
- package/dist/stdio/stdout.d.ts +54 -52
- package/dist/stdio/stdout.js +35 -33
- package/dist/streams.d.ts +4 -3
- package/dist/streams.js +1 -0
- package/dist/strings.d.ts +31 -85
- package/dist/strings.js +5 -49
- package/dist/suppress-warnings.d.ts +32 -15
- package/dist/suppress-warnings.js +14 -13
- package/dist/tables.d.ts +30 -26
- package/dist/tables.js +24 -23
- package/dist/temporary-executor.d.ts +4 -0
- package/dist/temporary-executor.js +1 -0
- package/dist/themes/context.d.ts +30 -26
- package/dist/themes/context.js +19 -18
- package/dist/themes/index.d.ts +0 -4
- package/dist/themes/index.js +1 -0
- package/dist/themes/themes.d.ts +1 -1
- package/dist/themes/themes.js +1 -0
- package/dist/themes/types.js +1 -0
- package/dist/themes/utils.d.ts +46 -43
- package/dist/themes/utils.js +45 -44
- package/dist/types.d.ts +48 -52
- package/dist/types.js +35 -35
- package/dist/url.d.ts +48 -53
- package/dist/url.js +38 -37
- package/dist/validation/json-parser.d.ts +19 -190
- package/dist/validation/json-parser.js +5 -69
- package/dist/validation/types.d.ts +9 -33
- package/dist/validation/types.js +1 -0
- package/dist/versions.d.ts +1 -0
- package/dist/versions.js +2 -1
- package/dist/words.d.ts +3 -6
- package/dist/words.js +1 -0
- package/dist/zod.js +1 -0
- package/package.json +40 -54
- package/dist/functions.d.ts +0 -57
- package/dist/functions.js +0 -70
- package/dist/stdio/mask.d.ts +0 -151
- package/dist/stdio/mask.js +0 -224
- package/dist/utils/get-ipc.d.ts +0 -15
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview Safe JSON parsing with validation and security controls.
|
|
3
|
-
* Provides protection against prototype pollution, size limits, and schema
|
|
3
|
+
* Provides protection against prototype pollution, size limits, and schema
|
|
4
|
+
* validation.
|
|
4
5
|
*
|
|
5
6
|
* Key Features:
|
|
6
|
-
* - Prototype pollution protection: Blocks `__proto__`, `constructor`, and
|
|
7
|
-
*
|
|
8
|
-
* -
|
|
9
|
-
* -
|
|
10
|
-
* - Memory safety: Prevents memory exhaustion attacks
|
|
7
|
+
* - Prototype pollution protection: Blocks `__proto__`, `constructor`, and
|
|
8
|
+
* `prototype` keys via JSON.parse reviver at any depth.
|
|
9
|
+
* - Size limits: Configurable maximum JSON string size (default 10MB).
|
|
10
|
+
* - Schema validation: Optional Zod-compatible schema validation.
|
|
11
|
+
* - Memory safety: Prevents memory exhaustion attacks.
|
|
11
12
|
*/
|
|
12
|
-
import type {
|
|
13
|
+
import type { SafeJsonParseOptions, Schema } from './types';
|
|
13
14
|
/**
|
|
14
15
|
* Safely parse JSON with optional schema validation and security controls.
|
|
15
16
|
* Throws errors on parse failures, validation failures, or security violations.
|
|
16
17
|
*
|
|
17
|
-
* This is the recommended method for parsing untrusted JSON input as it
|
|
18
|
-
* multiple layers of security including prototype pollution
|
|
18
|
+
* This is the recommended method for parsing untrusted JSON input as it
|
|
19
|
+
* provides multiple layers of security including prototype pollution
|
|
20
|
+
* protection and size limits.
|
|
19
21
|
*
|
|
20
22
|
* @template T - The expected type of the parsed data
|
|
21
23
|
* @param jsonString - The JSON string to parse
|
|
@@ -23,10 +25,11 @@ import type { JsonParseOptions, JsonParseResult, Schema } from './types';
|
|
|
23
25
|
* @param options - Parsing options for security and behavior control
|
|
24
26
|
* @returns The parsed and validated data
|
|
25
27
|
*
|
|
26
|
-
* @throws {Error} When JSON string exceeds `maxSize
|
|
27
|
-
* @throws {Error} When JSON parsing fails
|
|
28
|
-
* @throws {Error} When prototype pollution keys are detected (unless
|
|
29
|
-
*
|
|
28
|
+
* @throws {Error} When JSON string exceeds `maxSize`.
|
|
29
|
+
* @throws {Error} When JSON parsing fails.
|
|
30
|
+
* @throws {Error} When prototype pollution keys are detected (unless
|
|
31
|
+
* `allowPrototype` is `true`).
|
|
32
|
+
* @throws {Error} When schema validation fails.
|
|
30
33
|
*
|
|
31
34
|
* @example
|
|
32
35
|
* ```ts
|
|
@@ -46,184 +49,10 @@ import type { JsonParseOptions, JsonParseResult, Schema } from './types';
|
|
|
46
49
|
* maxSize: 1024 * 1024 // 1MB
|
|
47
50
|
* })
|
|
48
51
|
*
|
|
49
|
-
* // Allow prototype keys (
|
|
50
|
-
* const data = safeJsonParse(
|
|
52
|
+
* // Allow prototype keys (DANGEROUS — only for trusted sources)
|
|
53
|
+
* const data = safeJsonParse('{"__proto__": {}}', undefined, {
|
|
51
54
|
* allowPrototype: true
|
|
52
55
|
* })
|
|
53
56
|
* ```
|
|
54
57
|
*/
|
|
55
|
-
export declare function safeJsonParse<T = unknown>(jsonString: string, schema?: Schema<T> | undefined, options?:
|
|
56
|
-
/**
|
|
57
|
-
* Attempt to parse JSON, returning `undefined` on any error.
|
|
58
|
-
* This is a non-throwing wrapper around `safeJsonParse` for cases where
|
|
59
|
-
* you want to gracefully handle parse failures without try-catch blocks.
|
|
60
|
-
*
|
|
61
|
-
* Use this when parsing is optional or you have a fallback strategy.
|
|
62
|
-
* For critical parsing where you need error details, use `safeJsonParse` or `parseJsonWithResult`.
|
|
63
|
-
*
|
|
64
|
-
* @template T - The expected type of the parsed data
|
|
65
|
-
* @param jsonString - The JSON string to parse
|
|
66
|
-
* @param schema - Optional Zod-compatible schema for validation
|
|
67
|
-
* @param options - Parsing options for security and behavior control
|
|
68
|
-
* @returns The parsed data on success, or `undefined` on any error
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
71
|
-
* ```ts
|
|
72
|
-
* // Graceful fallback to default
|
|
73
|
-
* const config = tryJsonParse<Config>(jsonString) ?? defaultConfig
|
|
74
|
-
*
|
|
75
|
-
* // Optional parsing
|
|
76
|
-
* const data = tryJsonParse(possiblyInvalidJson)
|
|
77
|
-
* if (data) {
|
|
78
|
-
* console.log('Parsed successfully:', data)
|
|
79
|
-
* }
|
|
80
|
-
*
|
|
81
|
-
* // With schema validation
|
|
82
|
-
* const user = tryJsonParse(jsonString, userSchema)
|
|
83
|
-
* ```
|
|
84
|
-
*/
|
|
85
|
-
export declare function tryJsonParse<T = unknown>(jsonString: string, schema?: Schema<T> | undefined, options?: JsonParseOptions | undefined): T | undefined;
|
|
86
|
-
/**
|
|
87
|
-
* Parse JSON and return a discriminated union result.
|
|
88
|
-
* Never throws - always returns a result object with success/failure information.
|
|
89
|
-
*
|
|
90
|
-
* This is ideal when you need detailed error messages and type-safe result handling.
|
|
91
|
-
* The discriminated union allows TypeScript to narrow types based on the `success` flag.
|
|
92
|
-
*
|
|
93
|
-
* @template T - The expected type of the parsed data
|
|
94
|
-
* @param jsonString - The JSON string to parse
|
|
95
|
-
* @param schema - Optional Zod-compatible schema for validation
|
|
96
|
-
* @param options - Parsing options for security and behavior control
|
|
97
|
-
* @returns Result object with either `{success: true, data}` or `{success: false, error}`
|
|
98
|
-
*
|
|
99
|
-
* @example
|
|
100
|
-
* ```ts
|
|
101
|
-
* // Type-safe error handling
|
|
102
|
-
* const result = parseJsonWithResult<User>(jsonString, userSchema)
|
|
103
|
-
*
|
|
104
|
-
* if (result.success) {
|
|
105
|
-
* // TypeScript knows result.data is available
|
|
106
|
-
* console.log(`User: ${result.data.name}`)
|
|
107
|
-
* } else {
|
|
108
|
-
* // TypeScript knows result.error is available
|
|
109
|
-
* console.error(`Parse failed: ${result.error}`)
|
|
110
|
-
* }
|
|
111
|
-
*
|
|
112
|
-
* // Early return pattern
|
|
113
|
-
* const result = parseJsonWithResult(jsonString)
|
|
114
|
-
* if (!result.success) {
|
|
115
|
-
* logger.error(result.error)
|
|
116
|
-
* return
|
|
117
|
-
* }
|
|
118
|
-
* processData(result.data)
|
|
119
|
-
* ```
|
|
120
|
-
*/
|
|
121
|
-
export declare function parseJsonWithResult<T = unknown>(jsonString: string, schema?: Schema<T> | undefined, options?: JsonParseOptions | undefined): JsonParseResult<T>;
|
|
122
|
-
/**
|
|
123
|
-
* Create a reusable JSON parser with pre-configured schema and options.
|
|
124
|
-
* Useful for parsing multiple JSON strings with the same validation rules.
|
|
125
|
-
*
|
|
126
|
-
* The returned parser function can accept per-call options that override the defaults.
|
|
127
|
-
* This factory pattern reduces repetition when parsing many similar JSON payloads.
|
|
128
|
-
*
|
|
129
|
-
* @template T - The expected type of the parsed data
|
|
130
|
-
* @param schema - Optional Zod-compatible schema for validation
|
|
131
|
-
* @param defaultOptions - Default parsing options applied to all parse calls
|
|
132
|
-
* @returns A parser function that accepts a JSON string and optional per-call options
|
|
133
|
-
*
|
|
134
|
-
* @example
|
|
135
|
-
* ```ts
|
|
136
|
-
* // Create a parser for API responses
|
|
137
|
-
* import { z } from 'zod'
|
|
138
|
-
* const apiResponseSchema = z.object({
|
|
139
|
-
* status: z.string(),
|
|
140
|
-
* data: z.unknown()
|
|
141
|
-
* })
|
|
142
|
-
*
|
|
143
|
-
* const parseApiResponse = createJsonParser(apiResponseSchema, {
|
|
144
|
-
* maxSize: 5 * 1024 * 1024 // 5MB limit for API responses
|
|
145
|
-
* })
|
|
146
|
-
*
|
|
147
|
-
* // Use the parser multiple times
|
|
148
|
-
* const response1 = parseApiResponse(json1)
|
|
149
|
-
* const response2 = parseApiResponse(json2)
|
|
150
|
-
*
|
|
151
|
-
* // Override options for specific calls
|
|
152
|
-
* const response3 = parseApiResponse(json3, { maxSize: 10 * 1024 * 1024 })
|
|
153
|
-
* ```
|
|
154
|
-
*/
|
|
155
|
-
export declare function createJsonParser<T = unknown>(schema?: Schema<T> | undefined, defaultOptions?: JsonParseOptions | undefined): (jsonString: string, options?: JsonParseOptions) => T;
|
|
156
|
-
/**
|
|
157
|
-
* Parse newline-delimited JSON (NDJSON) into an array.
|
|
158
|
-
* Each line is treated as a separate JSON object. Empty lines are skipped.
|
|
159
|
-
*
|
|
160
|
-
* NDJSON format is commonly used for streaming logs, bulk data transfers,
|
|
161
|
-
* and event streams where each line represents a complete JSON document.
|
|
162
|
-
*
|
|
163
|
-
* @template T - The expected type of each parsed JSON object
|
|
164
|
-
* @param ndjson - Newline-delimited JSON string (supports both `\n` and `\r\n`)
|
|
165
|
-
* @param schema - Optional Zod-compatible schema for validation of each line
|
|
166
|
-
* @param options - Parsing options applied to each line
|
|
167
|
-
* @returns Array of parsed objects, one per non-empty line
|
|
168
|
-
*
|
|
169
|
-
* @throws {Error} When any line fails to parse (includes line number in error message)
|
|
170
|
-
*
|
|
171
|
-
* @example
|
|
172
|
-
* ```ts
|
|
173
|
-
* // Parse NDJSON logs
|
|
174
|
-
* const ndjsonString = `
|
|
175
|
-
* {"level":"info","message":"Server started"}
|
|
176
|
-
* {"level":"error","message":"Connection failed"}
|
|
177
|
-
* {"level":"info","message":"Retrying..."}
|
|
178
|
-
* `
|
|
179
|
-
* const logs = parseNdjson<LogEntry>(ndjsonString, logSchema)
|
|
180
|
-
* console.log(logs.length) // 3
|
|
181
|
-
*
|
|
182
|
-
* // Parse with size limits per line
|
|
183
|
-
* const entries = parseNdjson(ndjson, undefined, { maxSize: 1024 })
|
|
184
|
-
*
|
|
185
|
-
* // Empty lines are automatically skipped
|
|
186
|
-
* const data = parseNdjson('{"a":1}\n\n{"b":2}\n') // 2 objects
|
|
187
|
-
* ```
|
|
188
|
-
*/
|
|
189
|
-
export declare function parseNdjson<T = unknown>(ndjson: string, schema?: Schema<T> | undefined, options?: JsonParseOptions | undefined): T[];
|
|
190
|
-
/**
|
|
191
|
-
* Stream-parse newline-delimited JSON (NDJSON) using a generator.
|
|
192
|
-
* Yields one parsed object at a time, enabling memory-efficient processing of large NDJSON files.
|
|
193
|
-
*
|
|
194
|
-
* Unlike `parseNdjson` which loads all results into memory, this generator allows
|
|
195
|
-
* processing each line individually, making it ideal for large datasets or streaming scenarios.
|
|
196
|
-
*
|
|
197
|
-
* @template T - The expected type of each parsed JSON object
|
|
198
|
-
* @param ndjson - Newline-delimited JSON string (supports both `\n` and `\r\n`)
|
|
199
|
-
* @param schema - Optional Zod-compatible schema for validation of each line
|
|
200
|
-
* @param options - Parsing options applied to each line
|
|
201
|
-
* @yields Parsed objects one at a time as the generator iterates
|
|
202
|
-
*
|
|
203
|
-
* @throws {Error} When any line fails to parse (includes line number in error message)
|
|
204
|
-
*
|
|
205
|
-
* @example
|
|
206
|
-
* ```ts
|
|
207
|
-
* // Memory-efficient processing of large NDJSON files
|
|
208
|
-
* const ndjsonString = readLargeFile('logs.ndjson')
|
|
209
|
-
*
|
|
210
|
-
* for (const log of streamNdjson<LogEntry>(ndjsonString, logSchema)) {
|
|
211
|
-
* if (log.level === 'error') {
|
|
212
|
-
* console.error('Error found:', log.message)
|
|
213
|
-
* }
|
|
214
|
-
* }
|
|
215
|
-
*
|
|
216
|
-
* // Collect filtered results without loading everything
|
|
217
|
-
* const errors = [...streamNdjson(ndjson)]
|
|
218
|
-
* .filter(log => log.level === 'error')
|
|
219
|
-
*
|
|
220
|
-
* // Early termination when condition is met
|
|
221
|
-
* for (const entry of streamNdjson(ndjson)) {
|
|
222
|
-
* if (entry.id === targetId) {
|
|
223
|
-
* processEntry(entry)
|
|
224
|
-
* break // Stop processing remaining lines
|
|
225
|
-
* }
|
|
226
|
-
* }
|
|
227
|
-
* ```
|
|
228
|
-
*/
|
|
229
|
-
export declare function streamNdjson<T = unknown>(ndjson: string, schema?: Schema<T> | undefined, options?: JsonParseOptions | undefined): Generator<T, void, unknown>;
|
|
58
|
+
export declare function safeJsonParse<T = unknown>(jsonString: string, schema?: Schema<T> | undefined, options?: SafeJsonParseOptions): T;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/* Socket Lib - Built with esbuild */
|
|
3
|
+
"use strict";
|
|
3
4
|
var __defProp = Object.defineProperty;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
@@ -19,12 +20,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
19
20
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
21
|
var json_parser_exports = {};
|
|
21
22
|
__export(json_parser_exports, {
|
|
22
|
-
|
|
23
|
-
parseJsonWithResult: () => parseJsonWithResult,
|
|
24
|
-
parseNdjson: () => parseNdjson,
|
|
25
|
-
safeJsonParse: () => safeJsonParse,
|
|
26
|
-
streamNdjson: () => streamNdjson,
|
|
27
|
-
tryJsonParse: () => tryJsonParse
|
|
23
|
+
safeJsonParse: () => safeJsonParse
|
|
28
24
|
});
|
|
29
25
|
module.exports = __toCommonJS(json_parser_exports);
|
|
30
26
|
const DANGEROUS_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
@@ -53,75 +49,15 @@ function safeJsonParse(jsonString, schema, options = {}) {
|
|
|
53
49
|
if (schema) {
|
|
54
50
|
const result = schema.safeParse(parsed);
|
|
55
51
|
if (!result.success) {
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
).join(", ");
|
|
52
|
+
const error = result.error;
|
|
53
|
+
const errors = error.issues.map((issue) => `${issue.path.join(".")}: ${issue.message}`).join(", ");
|
|
59
54
|
throw new Error(`Validation failed: ${errors}`);
|
|
60
55
|
}
|
|
61
56
|
return result.data;
|
|
62
57
|
}
|
|
63
58
|
return parsed;
|
|
64
59
|
}
|
|
65
|
-
function tryJsonParse(jsonString, schema, options) {
|
|
66
|
-
try {
|
|
67
|
-
return safeJsonParse(jsonString, schema, options);
|
|
68
|
-
} catch {
|
|
69
|
-
return void 0;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
function parseJsonWithResult(jsonString, schema, options) {
|
|
73
|
-
try {
|
|
74
|
-
const data = safeJsonParse(jsonString, schema, options);
|
|
75
|
-
return { success: true, data };
|
|
76
|
-
} catch (error) {
|
|
77
|
-
const message = error instanceof Error ? error.message : "Unknown error";
|
|
78
|
-
return { success: false, error: message };
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
function createJsonParser(schema, defaultOptions) {
|
|
82
|
-
return (jsonString, options) => {
|
|
83
|
-
return safeJsonParse(jsonString, schema, { ...defaultOptions, ...options });
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
function parseNdjson(ndjson, schema, options) {
|
|
87
|
-
const results = [];
|
|
88
|
-
const lines = ndjson.split(/\r?\n/);
|
|
89
|
-
for (let i = 0; i < lines.length; i++) {
|
|
90
|
-
const line = lines[i]?.trim();
|
|
91
|
-
if (!line || line === "") {
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
try {
|
|
95
|
-
const parsed = safeJsonParse(line, schema, options);
|
|
96
|
-
results.push(parsed);
|
|
97
|
-
} catch (error) {
|
|
98
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
99
|
-
throw new Error(`Failed to parse NDJSON at line ${i + 1}: ${message}`);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return results;
|
|
103
|
-
}
|
|
104
|
-
function* streamNdjson(ndjson, schema, options) {
|
|
105
|
-
const lines = ndjson.split(/\r?\n/);
|
|
106
|
-
for (let i = 0; i < lines.length; i++) {
|
|
107
|
-
const line = lines[i]?.trim();
|
|
108
|
-
if (!line || line === "") {
|
|
109
|
-
continue;
|
|
110
|
-
}
|
|
111
|
-
try {
|
|
112
|
-
yield safeJsonParse(line, schema, options);
|
|
113
|
-
} catch (error) {
|
|
114
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
115
|
-
throw new Error(`Failed to parse NDJSON at line ${i + 1}: ${message}`);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
60
|
// Annotate the CommonJS export names for ESM import in node:
|
|
120
61
|
0 && (module.exports = {
|
|
121
|
-
|
|
122
|
-
parseJsonWithResult,
|
|
123
|
-
parseNdjson,
|
|
124
|
-
safeJsonParse,
|
|
125
|
-
streamNdjson,
|
|
126
|
-
tryJsonParse
|
|
62
|
+
safeJsonParse
|
|
127
63
|
});
|
|
@@ -24,7 +24,7 @@ export interface ParseResult<T> {
|
|
|
24
24
|
/** Parsed and validated data (only present when `success` is `true`) */
|
|
25
25
|
data?: T | undefined;
|
|
26
26
|
/** Error information (only present when `success` is `false`) */
|
|
27
|
-
error?:
|
|
27
|
+
error?: unknown;
|
|
28
28
|
}
|
|
29
29
|
/**
|
|
30
30
|
* Base schema interface compatible with Zod and similar validation libraries.
|
|
@@ -46,7 +46,7 @@ export interface ParseResult<T> {
|
|
|
46
46
|
* const result = schema.safeParse({ name: 'Alice', age: 30 })
|
|
47
47
|
* ```
|
|
48
48
|
*/
|
|
49
|
-
export interface Schema<T =
|
|
49
|
+
export interface Schema<T = unknown> {
|
|
50
50
|
/**
|
|
51
51
|
* Safely parse data without throwing errors.
|
|
52
52
|
* Returns a result object indicating success or failure.
|
|
@@ -54,7 +54,7 @@ export interface Schema<T = any> {
|
|
|
54
54
|
* @param data - The data to validate
|
|
55
55
|
* @returns Parse result with success flag and data or error
|
|
56
56
|
*/
|
|
57
|
-
safeParse(data:
|
|
57
|
+
safeParse(data: unknown): ParseResult<T>;
|
|
58
58
|
/**
|
|
59
59
|
* Parse data and throw an error if validation fails.
|
|
60
60
|
* Use this when you want to fail fast on invalid data.
|
|
@@ -63,7 +63,7 @@ export interface Schema<T = any> {
|
|
|
63
63
|
* @returns The validated and parsed data
|
|
64
64
|
* @throws {Error} When validation fails
|
|
65
65
|
*/
|
|
66
|
-
parse(data:
|
|
66
|
+
parse(data: unknown): T;
|
|
67
67
|
/**
|
|
68
68
|
* Optional schema name for debugging and error messages.
|
|
69
69
|
* Useful for identifying which schema failed in complex validation chains.
|
|
@@ -71,17 +71,19 @@ export interface Schema<T = any> {
|
|
|
71
71
|
_name?: string | undefined;
|
|
72
72
|
}
|
|
73
73
|
/**
|
|
74
|
-
* Options for configuring JSON parsing
|
|
74
|
+
* Options for configuring safe JSON parsing with security controls.
|
|
75
|
+
* Distinct from `JsonParseOptions` in `@socketsecurity/lib/json/types`
|
|
76
|
+
* which is scoped to reviver/error-handling for fs-oriented JSON reads.
|
|
75
77
|
*
|
|
76
78
|
* @example
|
|
77
79
|
* ```ts
|
|
78
|
-
* const options:
|
|
80
|
+
* const options: SafeJsonParseOptions = {
|
|
79
81
|
* maxSize: 1024 * 1024, // 1MB limit
|
|
80
82
|
* allowPrototype: false // Block prototype pollution
|
|
81
83
|
* }
|
|
82
84
|
* ```
|
|
83
85
|
*/
|
|
84
|
-
export interface
|
|
86
|
+
export interface SafeJsonParseOptions {
|
|
85
87
|
/**
|
|
86
88
|
* Allow dangerous prototype pollution keys (`__proto__`, `constructor`, `prototype`).
|
|
87
89
|
* Set to `true` only if you trust the JSON source completely.
|
|
@@ -114,29 +116,3 @@ export interface JsonParseOptions {
|
|
|
114
116
|
*/
|
|
115
117
|
maxSize?: number | undefined;
|
|
116
118
|
}
|
|
117
|
-
/**
|
|
118
|
-
* Discriminated union type for JSON parsing results.
|
|
119
|
-
* Enables type-safe handling of success and failure cases.
|
|
120
|
-
*
|
|
121
|
-
* @template T - The expected type of the parsed data
|
|
122
|
-
*
|
|
123
|
-
* @example
|
|
124
|
-
* ```ts
|
|
125
|
-
* const result: JsonParseResult<User> = parseJsonWithResult(jsonString)
|
|
126
|
-
*
|
|
127
|
-
* if (result.success) {
|
|
128
|
-
* // TypeScript knows result.data is available
|
|
129
|
-
* console.log(result.data.name)
|
|
130
|
-
* } else {
|
|
131
|
-
* // TypeScript knows result.error is available
|
|
132
|
-
* console.error(result.error)
|
|
133
|
-
* }
|
|
134
|
-
* ```
|
|
135
|
-
*/
|
|
136
|
-
export type JsonParseResult<T> = {
|
|
137
|
-
success: true;
|
|
138
|
-
data: T;
|
|
139
|
-
} | {
|
|
140
|
-
success: false;
|
|
141
|
-
error: string;
|
|
142
|
-
};
|
package/dist/validation/types.js
CHANGED
package/dist/versions.d.ts
CHANGED
package/dist/versions.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/* Socket Lib - Built with esbuild */
|
|
3
|
+
"use strict";
|
|
3
4
|
var __defProp = Object.defineProperty;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
@@ -44,7 +45,7 @@ module.exports = __toCommonJS(versions_exports);
|
|
|
44
45
|
let _semver;
|
|
45
46
|
function getSemver() {
|
|
46
47
|
if (_semver === void 0) {
|
|
47
|
-
_semver = require("./external/semver
|
|
48
|
+
_semver = require("./external/semver");
|
|
48
49
|
}
|
|
49
50
|
return _semver;
|
|
50
51
|
}
|
package/dist/words.d.ts
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
* @fileoverview Word manipulation utilities for capitalization and formatting.
|
|
3
3
|
* Provides text transformation functions for consistent word processing.
|
|
4
4
|
*/
|
|
5
|
+
export interface PluralizeOptions {
|
|
6
|
+
count?: number;
|
|
7
|
+
}
|
|
5
8
|
/**
|
|
6
9
|
* Capitalize the first letter of a word.
|
|
7
10
|
*
|
|
@@ -12,7 +15,6 @@
|
|
|
12
15
|
* capitalize('') // ''
|
|
13
16
|
* ```
|
|
14
17
|
*/
|
|
15
|
-
/*@__NO_SIDE_EFFECTS__*/
|
|
16
18
|
export declare function capitalize(word: string): string;
|
|
17
19
|
/**
|
|
18
20
|
* Determine the appropriate article (a/an) for a word.
|
|
@@ -23,11 +25,7 @@ export declare function capitalize(word: string): string;
|
|
|
23
25
|
* determineArticle('banana') // 'a'
|
|
24
26
|
* ```
|
|
25
27
|
*/
|
|
26
|
-
/*@__NO_SIDE_EFFECTS__*/
|
|
27
28
|
export declare function determineArticle(word: string): string;
|
|
28
|
-
export interface PluralizeOptions {
|
|
29
|
-
count?: number;
|
|
30
|
-
}
|
|
31
29
|
/**
|
|
32
30
|
* Pluralize a word based on count.
|
|
33
31
|
*
|
|
@@ -38,5 +36,4 @@ export interface PluralizeOptions {
|
|
|
38
36
|
* pluralize('file', { count: 0 }) // 'files'
|
|
39
37
|
* ```
|
|
40
38
|
*/
|
|
41
|
-
/*@__NO_SIDE_EFFECTS__*/
|
|
42
39
|
export declare function pluralize(word: string, options?: PluralizeOptions | undefined): string;
|
package/dist/words.js
CHANGED