@socketsecurity/lib 4.4.0 → 5.0.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 +67 -0
- package/dist/constants/node.js +1 -1
- package/dist/{package-default-node-range.js → constants/package-default-node-range.js} +1 -1
- package/dist/constants/packages.js +3 -3
- package/dist/{dlx-binary.d.ts → dlx/binary.d.ts} +2 -2
- package/dist/{dlx-binary.js → dlx/binary.js} +17 -17
- package/dist/dlx/cache.d.ts +25 -0
- package/dist/dlx/cache.js +32 -0
- package/dist/dlx/dir.d.ts +24 -0
- package/dist/dlx/dir.js +79 -0
- package/dist/{dlx-manifest.js → dlx/manifest.js} +7 -7
- package/dist/{dlx-package.d.ts → dlx/package.d.ts} +2 -2
- package/dist/{dlx-package.js → dlx/package.js} +16 -16
- package/dist/dlx/packages.d.ts +24 -0
- package/dist/dlx/packages.js +125 -0
- package/dist/dlx/paths.d.ts +31 -0
- package/dist/dlx/paths.js +75 -0
- package/dist/fs.d.ts +3 -3
- package/dist/fs.js +3 -3
- package/dist/json/edit.d.ts +16 -0
- package/dist/json/edit.js +217 -0
- package/dist/json/format.d.ts +140 -0
- package/dist/json/format.js +121 -0
- package/dist/json/parse.d.ts +76 -0
- package/dist/{json.js → json/parse.js} +4 -4
- package/dist/json/types.d.ts +229 -0
- package/dist/json/types.js +17 -0
- package/dist/packages/{editable.js → edit.js} +18 -32
- package/dist/packages/operations.js +3 -3
- package/dist/packages.d.ts +2 -2
- package/dist/packages.js +5 -5
- package/package.json +58 -34
- package/dist/dlx.d.ts +0 -104
- package/dist/dlx.js +0 -220
- package/dist/json.d.ts +0 -196
- /package/dist/{lifecycle-script-names.d.ts → constants/lifecycle-script-names.d.ts} +0 -0
- /package/dist/{lifecycle-script-names.js → constants/lifecycle-script-names.js} +0 -0
- /package/dist/{maintained-node-versions.d.ts → constants/maintained-node-versions.d.ts} +0 -0
- /package/dist/{maintained-node-versions.js → constants/maintained-node-versions.js} +0 -0
- /package/dist/{package-default-node-range.d.ts → constants/package-default-node-range.d.ts} +0 -0
- /package/dist/{package-default-socket-categories.d.ts → constants/package-default-socket-categories.d.ts} +0 -0
- /package/dist/{package-default-socket-categories.js → constants/package-default-socket-categories.js} +0 -0
- /package/dist/{dlx-manifest.d.ts → dlx/manifest.d.ts} +0 -0
- /package/dist/packages/{editable.d.ts → edit.d.ts} +0 -0
package/dist/dlx.d.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generate a cache directory name using npm/npx approach.
|
|
3
|
-
* Uses first 16 characters of SHA-512 hash (like npm/npx).
|
|
4
|
-
*
|
|
5
|
-
* Rationale for SHA-512 truncated (vs full SHA-256):
|
|
6
|
-
* - Matches npm/npx ecosystem behavior
|
|
7
|
-
* - Shorter paths for Windows MAX_PATH compatibility (260 chars)
|
|
8
|
-
* - 16 hex chars = 64 bits = acceptable collision risk for local cache
|
|
9
|
-
* - Collision probability ~1 in 18 quintillion with 1000 entries
|
|
10
|
-
*
|
|
11
|
-
* Input strategy (aligned with npx):
|
|
12
|
-
* - npx uses package spec strings (e.g., '@scope/pkg@1.0.0', 'prettier@3.0.0')
|
|
13
|
-
* - Caller provides complete spec string with version for accurate cache keying
|
|
14
|
-
* - For package installs: Use PURL-style spec with version
|
|
15
|
-
* Examples: 'npm:prettier@3.0.0', 'pypi:requests@2.31.0', 'gem:rails@7.0.0'
|
|
16
|
-
* Note: Socket uses shorthand format without 'pkg:' prefix
|
|
17
|
-
* (handled by @socketregistry/packageurl-js)
|
|
18
|
-
* - For binary downloads: Use URL:name for uniqueness
|
|
19
|
-
*
|
|
20
|
-
* Reference: npm/cli v11.6.2 libnpmexec/lib/index.js#L233-L244
|
|
21
|
-
* https://github.com/npm/cli/blob/v11.6.2/workspaces/libnpmexec/lib/index.js#L233-L244
|
|
22
|
-
* Implementation: packages.map().sort().join('\n') → SHA-512 → slice(0,16)
|
|
23
|
-
* npx hashes the package spec (name@version), not just name
|
|
24
|
-
*/
|
|
25
|
-
export declare function generateCacheKey(spec: string): string;
|
|
26
|
-
/**
|
|
27
|
-
* Clear all DLX package installations.
|
|
28
|
-
*/
|
|
29
|
-
export declare function clearDlx(): Promise<void>;
|
|
30
|
-
/**
|
|
31
|
-
* Clear all DLX package installations synchronously.
|
|
32
|
-
*/
|
|
33
|
-
export declare function clearDlxSync(): void;
|
|
34
|
-
/**
|
|
35
|
-
* Check if the DLX directory exists.
|
|
36
|
-
*/
|
|
37
|
-
export declare function dlxDirExists(): boolean;
|
|
38
|
-
/**
|
|
39
|
-
* Check if the DLX directory exists asynchronously.
|
|
40
|
-
*/
|
|
41
|
-
export declare function dlxDirExistsAsync(): Promise<boolean>;
|
|
42
|
-
/**
|
|
43
|
-
* Ensure the DLX directory exists, creating it if necessary.
|
|
44
|
-
*/
|
|
45
|
-
export declare function ensureDlxDir(): Promise<void>;
|
|
46
|
-
/**
|
|
47
|
-
* Ensure the DLX directory exists synchronously, creating it if necessary.
|
|
48
|
-
*/
|
|
49
|
-
export declare function ensureDlxDirSync(): void;
|
|
50
|
-
/**
|
|
51
|
-
* Get the installed package directory within DLX node_modules.
|
|
52
|
-
*/
|
|
53
|
-
export declare function getDlxInstalledPackageDir(packageName: string): string;
|
|
54
|
-
/**
|
|
55
|
-
* Get the DLX installation directory for a specific package.
|
|
56
|
-
*/
|
|
57
|
-
export declare function getDlxPackageDir(packageName: string): string;
|
|
58
|
-
/**
|
|
59
|
-
* Get the package.json path for a DLX installed package.
|
|
60
|
-
*/
|
|
61
|
-
export declare function getDlxPackageJsonPath(packageName: string): string;
|
|
62
|
-
/**
|
|
63
|
-
* Get the node_modules directory for a DLX package installation.
|
|
64
|
-
*/
|
|
65
|
-
export declare function getDlxPackageNodeModulesDir(packageName: string): string;
|
|
66
|
-
/**
|
|
67
|
-
* Check if a file path is within the Socket DLX directory.
|
|
68
|
-
* This is useful for determining if a binary or file is managed by Socket's DLX system.
|
|
69
|
-
*
|
|
70
|
-
* @param filePath - Absolute or relative path to check
|
|
71
|
-
* @returns true if the path is within ~/.socket/_dlx/, false otherwise
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
-
* ```typescript
|
|
75
|
-
* isInSocketDlx('/home/user/.socket/_dlx/abc123/bin/socket') // true
|
|
76
|
-
* isInSocketDlx('/usr/local/bin/socket') // false
|
|
77
|
-
* isInSocketDlx(process.argv[0]) // Check if current binary is in DLX
|
|
78
|
-
* ```
|
|
79
|
-
*/
|
|
80
|
-
export declare function isInSocketDlx(filePath: string): boolean;
|
|
81
|
-
/**
|
|
82
|
-
* Check if a package is installed in DLX.
|
|
83
|
-
*/
|
|
84
|
-
export declare function isDlxPackageInstalled(packageName: string): boolean;
|
|
85
|
-
/**
|
|
86
|
-
* Check if a package is installed in DLX asynchronously.
|
|
87
|
-
*/
|
|
88
|
-
export declare function isDlxPackageInstalledAsync(packageName: string): Promise<boolean>;
|
|
89
|
-
/**
|
|
90
|
-
* List all packages installed in DLX.
|
|
91
|
-
*/
|
|
92
|
-
export declare function listDlxPackages(): string[];
|
|
93
|
-
/**
|
|
94
|
-
* List all packages installed in DLX asynchronously.
|
|
95
|
-
*/
|
|
96
|
-
export declare function listDlxPackagesAsync(): Promise<string[]>;
|
|
97
|
-
/**
|
|
98
|
-
* Remove a DLX package installation.
|
|
99
|
-
*/
|
|
100
|
-
export declare function removeDlxPackage(packageName: string): Promise<void>;
|
|
101
|
-
/**
|
|
102
|
-
* Remove a DLX package installation synchronously.
|
|
103
|
-
*/
|
|
104
|
-
export declare function removeDlxPackageSync(packageName: string): void;
|
package/dist/dlx.js
DELETED
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/* Socket Lib - Built with esbuild */
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
-
var dlx_exports = {};
|
|
21
|
-
__export(dlx_exports, {
|
|
22
|
-
clearDlx: () => clearDlx,
|
|
23
|
-
clearDlxSync: () => clearDlxSync,
|
|
24
|
-
dlxDirExists: () => dlxDirExists,
|
|
25
|
-
dlxDirExistsAsync: () => dlxDirExistsAsync,
|
|
26
|
-
ensureDlxDir: () => ensureDlxDir,
|
|
27
|
-
ensureDlxDirSync: () => ensureDlxDirSync,
|
|
28
|
-
generateCacheKey: () => generateCacheKey,
|
|
29
|
-
getDlxInstalledPackageDir: () => getDlxInstalledPackageDir,
|
|
30
|
-
getDlxPackageDir: () => getDlxPackageDir,
|
|
31
|
-
getDlxPackageJsonPath: () => getDlxPackageJsonPath,
|
|
32
|
-
getDlxPackageNodeModulesDir: () => getDlxPackageNodeModulesDir,
|
|
33
|
-
isDlxPackageInstalled: () => isDlxPackageInstalled,
|
|
34
|
-
isDlxPackageInstalledAsync: () => isDlxPackageInstalledAsync,
|
|
35
|
-
isInSocketDlx: () => isInSocketDlx,
|
|
36
|
-
listDlxPackages: () => listDlxPackages,
|
|
37
|
-
listDlxPackagesAsync: () => listDlxPackagesAsync,
|
|
38
|
-
removeDlxPackage: () => removeDlxPackage,
|
|
39
|
-
removeDlxPackageSync: () => removeDlxPackageSync
|
|
40
|
-
});
|
|
41
|
-
module.exports = __toCommonJS(dlx_exports);
|
|
42
|
-
var import_crypto = require("crypto");
|
|
43
|
-
var import_fs = require("./fs");
|
|
44
|
-
var import_normalize = require("./paths/normalize");
|
|
45
|
-
var import_socket = require("./paths/socket");
|
|
46
|
-
var import_promises = require("./promises");
|
|
47
|
-
let _fs;
|
|
48
|
-
// @__NO_SIDE_EFFECTS__
|
|
49
|
-
function getFs() {
|
|
50
|
-
if (_fs === void 0) {
|
|
51
|
-
_fs = require("node:fs");
|
|
52
|
-
}
|
|
53
|
-
return _fs;
|
|
54
|
-
}
|
|
55
|
-
function generateCacheKey(spec) {
|
|
56
|
-
return (0, import_crypto.createHash)("sha512").update(spec).digest("hex").substring(0, 16);
|
|
57
|
-
}
|
|
58
|
-
let _path;
|
|
59
|
-
// @__NO_SIDE_EFFECTS__
|
|
60
|
-
function getPath() {
|
|
61
|
-
if (_path === void 0) {
|
|
62
|
-
_path = require("node:path");
|
|
63
|
-
}
|
|
64
|
-
return _path;
|
|
65
|
-
}
|
|
66
|
-
async function clearDlx() {
|
|
67
|
-
const packages = await listDlxPackagesAsync();
|
|
68
|
-
await (0, import_promises.pEach)(packages, (pkg) => removeDlxPackage(pkg));
|
|
69
|
-
}
|
|
70
|
-
function clearDlxSync() {
|
|
71
|
-
const packages = listDlxPackages();
|
|
72
|
-
for (const pkg of packages) {
|
|
73
|
-
removeDlxPackageSync(pkg);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
function dlxDirExists() {
|
|
77
|
-
const fs = /* @__PURE__ */ getFs();
|
|
78
|
-
return fs.existsSync((0, import_socket.getSocketDlxDir)());
|
|
79
|
-
}
|
|
80
|
-
async function dlxDirExistsAsync() {
|
|
81
|
-
const fs = /* @__PURE__ */ getFs();
|
|
82
|
-
try {
|
|
83
|
-
await fs.promises.access((0, import_socket.getSocketDlxDir)());
|
|
84
|
-
return true;
|
|
85
|
-
} catch {
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
async function ensureDlxDir() {
|
|
90
|
-
await (0, import_fs.safeMkdir)((0, import_socket.getSocketDlxDir)());
|
|
91
|
-
}
|
|
92
|
-
function ensureDlxDirSync() {
|
|
93
|
-
(0, import_fs.safeMkdirSync)((0, import_socket.getSocketDlxDir)());
|
|
94
|
-
}
|
|
95
|
-
function getDlxInstalledPackageDir(packageName) {
|
|
96
|
-
const path = /* @__PURE__ */ getPath();
|
|
97
|
-
return (0, import_normalize.normalizePath)(
|
|
98
|
-
path.join(getDlxPackageNodeModulesDir(packageName), packageName)
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
function getDlxPackageDir(packageName) {
|
|
102
|
-
const path = /* @__PURE__ */ getPath();
|
|
103
|
-
return (0, import_normalize.normalizePath)(path.join((0, import_socket.getSocketDlxDir)(), packageName));
|
|
104
|
-
}
|
|
105
|
-
function getDlxPackageJsonPath(packageName) {
|
|
106
|
-
const path = /* @__PURE__ */ getPath();
|
|
107
|
-
return (0, import_normalize.normalizePath)(
|
|
108
|
-
path.join(getDlxInstalledPackageDir(packageName), "package.json")
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
function getDlxPackageNodeModulesDir(packageName) {
|
|
112
|
-
const path = /* @__PURE__ */ getPath();
|
|
113
|
-
return (0, import_normalize.normalizePath)(path.join(getDlxPackageDir(packageName), "node_modules"));
|
|
114
|
-
}
|
|
115
|
-
function isInSocketDlx(filePath) {
|
|
116
|
-
if (!filePath) {
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
const path = /* @__PURE__ */ getPath();
|
|
120
|
-
const dlxDir = (0, import_socket.getSocketDlxDir)();
|
|
121
|
-
const absolutePath = (0, import_normalize.normalizePath)(path.resolve(filePath));
|
|
122
|
-
return absolutePath.startsWith(`${dlxDir}/`);
|
|
123
|
-
}
|
|
124
|
-
function isDlxPackageInstalled(packageName) {
|
|
125
|
-
const fs = /* @__PURE__ */ getFs();
|
|
126
|
-
return fs.existsSync(getDlxInstalledPackageDir(packageName));
|
|
127
|
-
}
|
|
128
|
-
async function isDlxPackageInstalledAsync(packageName) {
|
|
129
|
-
const fs = /* @__PURE__ */ getFs();
|
|
130
|
-
try {
|
|
131
|
-
await fs.promises.access(getDlxInstalledPackageDir(packageName));
|
|
132
|
-
return true;
|
|
133
|
-
} catch {
|
|
134
|
-
return false;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
function listDlxPackages() {
|
|
138
|
-
try {
|
|
139
|
-
return (0, import_fs.readDirNamesSync)((0, import_socket.getSocketDlxDir)(), { sort: true });
|
|
140
|
-
} catch {
|
|
141
|
-
return [];
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
async function listDlxPackagesAsync() {
|
|
145
|
-
const fs = /* @__PURE__ */ getFs();
|
|
146
|
-
try {
|
|
147
|
-
const entries = await fs.promises.readdir((0, import_socket.getSocketDlxDir)(), {
|
|
148
|
-
withFileTypes: true
|
|
149
|
-
});
|
|
150
|
-
return entries.filter((e) => e.isDirectory()).map((e) => e.name).sort();
|
|
151
|
-
} catch {
|
|
152
|
-
return [];
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
async function removeDlxPackage(packageName) {
|
|
156
|
-
const packageDir = getDlxPackageDir(packageName);
|
|
157
|
-
try {
|
|
158
|
-
await (0, import_fs.safeDelete)(packageDir, { recursive: true, force: true });
|
|
159
|
-
} catch (e) {
|
|
160
|
-
throw new Error(`Failed to remove DLX package "${packageName}"`, {
|
|
161
|
-
cause: e
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
function removeDlxPackageSync(packageName) {
|
|
166
|
-
const fs = /* @__PURE__ */ getFs();
|
|
167
|
-
const packageDir = getDlxPackageDir(packageName);
|
|
168
|
-
try {
|
|
169
|
-
fs.rmSync(packageDir, { recursive: true, force: true });
|
|
170
|
-
} catch (e) {
|
|
171
|
-
const code = e.code;
|
|
172
|
-
if (code === "EACCES" || code === "EPERM") {
|
|
173
|
-
throw new Error(
|
|
174
|
-
`Permission denied removing DLX package "${packageName}"
|
|
175
|
-
Directory: ${packageDir}
|
|
176
|
-
To resolve:
|
|
177
|
-
1. Check file/directory permissions
|
|
178
|
-
2. Close any programs using files in this directory
|
|
179
|
-
3. Try running with elevated privileges if necessary
|
|
180
|
-
4. Manually remove: rm -rf "${packageDir}"`,
|
|
181
|
-
{ cause: e }
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
if (code === "EROFS") {
|
|
185
|
-
throw new Error(
|
|
186
|
-
`Cannot remove DLX package "${packageName}" from read-only filesystem
|
|
187
|
-
Directory: ${packageDir}
|
|
188
|
-
The filesystem is mounted read-only.`,
|
|
189
|
-
{ cause: e }
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
throw new Error(
|
|
193
|
-
`Failed to remove DLX package "${packageName}"
|
|
194
|
-
Directory: ${packageDir}
|
|
195
|
-
Check permissions and ensure no programs are using this directory.`,
|
|
196
|
-
{ cause: e }
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
201
|
-
0 && (module.exports = {
|
|
202
|
-
clearDlx,
|
|
203
|
-
clearDlxSync,
|
|
204
|
-
dlxDirExists,
|
|
205
|
-
dlxDirExistsAsync,
|
|
206
|
-
ensureDlxDir,
|
|
207
|
-
ensureDlxDirSync,
|
|
208
|
-
generateCacheKey,
|
|
209
|
-
getDlxInstalledPackageDir,
|
|
210
|
-
getDlxPackageDir,
|
|
211
|
-
getDlxPackageJsonPath,
|
|
212
|
-
getDlxPackageNodeModulesDir,
|
|
213
|
-
isDlxPackageInstalled,
|
|
214
|
-
isDlxPackageInstalledAsync,
|
|
215
|
-
isInSocketDlx,
|
|
216
|
-
listDlxPackages,
|
|
217
|
-
listDlxPackagesAsync,
|
|
218
|
-
removeDlxPackage,
|
|
219
|
-
removeDlxPackageSync
|
|
220
|
-
});
|
package/dist/json.d.ts
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* JSON primitive types: `null`, `boolean`, `number`, or `string`.
|
|
3
|
-
*
|
|
4
|
-
* @example
|
|
5
|
-
* ```ts
|
|
6
|
-
* const primitives: JsonPrimitive[] = [null, true, 42, 'hello']
|
|
7
|
-
* ```
|
|
8
|
-
*/
|
|
9
|
-
export type JsonPrimitive = null | boolean | number | string;
|
|
10
|
-
/**
|
|
11
|
-
* Any valid JSON value: primitive, object, or array.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* const values: JsonValue[] = [
|
|
16
|
-
* null,
|
|
17
|
-
* true,
|
|
18
|
-
* 42,
|
|
19
|
-
* 'hello',
|
|
20
|
-
* { key: 'value' },
|
|
21
|
-
* [1, 2, 3]
|
|
22
|
-
* ]
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
export type JsonValue = JsonPrimitive | JsonObject | JsonArray;
|
|
26
|
-
/**
|
|
27
|
-
* A JSON object with string keys and JSON values.
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* ```ts
|
|
31
|
-
* const obj: JsonObject = {
|
|
32
|
-
* name: 'example',
|
|
33
|
-
* count: 42,
|
|
34
|
-
* active: true,
|
|
35
|
-
* nested: { key: 'value' }
|
|
36
|
-
* }
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
export interface JsonObject {
|
|
40
|
-
[key: string]: JsonValue;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* A JSON array containing JSON values.
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* ```ts
|
|
47
|
-
* const arr: JsonArray = [1, 'two', { three: 3 }, [4, 5]]
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
export interface JsonArray extends Array<JsonValue> {
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Reviver function for transforming parsed JSON values.
|
|
54
|
-
* Called for each key-value pair during parsing.
|
|
55
|
-
*
|
|
56
|
-
* @param key - The object key or array index being parsed
|
|
57
|
-
* @param value - The parsed value
|
|
58
|
-
* @returns The transformed value (or original if no transform needed)
|
|
59
|
-
*
|
|
60
|
-
* @example
|
|
61
|
-
* ```ts
|
|
62
|
-
* // Convert date strings to Date objects
|
|
63
|
-
* const reviver: JsonReviver = (key, value) => {
|
|
64
|
-
* if (typeof value === 'string' && /^\d{4}-\d{2}-\d{2}/.test(value)) {
|
|
65
|
-
* return new Date(value)
|
|
66
|
-
* }
|
|
67
|
-
* return value
|
|
68
|
-
* }
|
|
69
|
-
* ```
|
|
70
|
-
*/
|
|
71
|
-
export type JsonReviver = (key: string, value: unknown) => unknown;
|
|
72
|
-
/**
|
|
73
|
-
* Options for JSON parsing operations.
|
|
74
|
-
*/
|
|
75
|
-
export interface JsonParseOptions {
|
|
76
|
-
/**
|
|
77
|
-
* Optional filepath for improved error messages.
|
|
78
|
-
* When provided, errors will be prefixed with the filepath.
|
|
79
|
-
*
|
|
80
|
-
* @example
|
|
81
|
-
* ```ts
|
|
82
|
-
* // Error message will be: "config.json: Unexpected token } in JSON"
|
|
83
|
-
* jsonParse('invalid', { filepath: 'config.json' })
|
|
84
|
-
* ```
|
|
85
|
-
*/
|
|
86
|
-
filepath?: string | undefined;
|
|
87
|
-
/**
|
|
88
|
-
* Optional reviver function to transform parsed values.
|
|
89
|
-
* Called for each key-value pair during parsing.
|
|
90
|
-
*
|
|
91
|
-
* @example
|
|
92
|
-
* ```ts
|
|
93
|
-
* // Convert ISO date strings to Date objects
|
|
94
|
-
* const options = {
|
|
95
|
-
* reviver: (key, value) => {
|
|
96
|
-
* if (typeof value === 'string' && /^\d{4}-\d{2}-\d{2}/.test(value)) {
|
|
97
|
-
* return new Date(value)
|
|
98
|
-
* }
|
|
99
|
-
* return value
|
|
100
|
-
* }
|
|
101
|
-
* }
|
|
102
|
-
* ```
|
|
103
|
-
*/
|
|
104
|
-
reviver?: JsonReviver | undefined;
|
|
105
|
-
/**
|
|
106
|
-
* Whether to throw on parse errors.
|
|
107
|
-
* When `false`, returns `undefined` instead of throwing.
|
|
108
|
-
*
|
|
109
|
-
* @default true
|
|
110
|
-
*
|
|
111
|
-
* @example
|
|
112
|
-
* ```ts
|
|
113
|
-
* // Throws error
|
|
114
|
-
* jsonParse('invalid', { throws: true })
|
|
115
|
-
*
|
|
116
|
-
* // Returns undefined
|
|
117
|
-
* const result = jsonParse('invalid', { throws: false })
|
|
118
|
-
* ```
|
|
119
|
-
*/
|
|
120
|
-
throws?: boolean | undefined;
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Check if a value is a JSON primitive type.
|
|
124
|
-
* JSON primitives are: `null`, `boolean`, `number`, or `string`.
|
|
125
|
-
*
|
|
126
|
-
* @param value - Value to check
|
|
127
|
-
* @returns `true` if value is a JSON primitive, `false` otherwise
|
|
128
|
-
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```ts
|
|
131
|
-
* isJsonPrimitive(null) // => true
|
|
132
|
-
* isJsonPrimitive(true) // => true
|
|
133
|
-
* isJsonPrimitive(42) // => true
|
|
134
|
-
* isJsonPrimitive('hello') // => true
|
|
135
|
-
* isJsonPrimitive({}) // => false
|
|
136
|
-
* isJsonPrimitive([]) // => false
|
|
137
|
-
* isJsonPrimitive(undefined) // => false
|
|
138
|
-
* ```
|
|
139
|
-
*/
|
|
140
|
-
/*@__NO_SIDE_EFFECTS__*/
|
|
141
|
-
export declare function isJsonPrimitive(value: unknown): value is JsonPrimitive;
|
|
142
|
-
/**
|
|
143
|
-
* Parse JSON content with automatic Buffer handling and BOM stripping.
|
|
144
|
-
* Provides safer JSON parsing with helpful error messages and optional error suppression.
|
|
145
|
-
*
|
|
146
|
-
* Features:
|
|
147
|
-
* - Automatic UTF-8 Buffer conversion
|
|
148
|
-
* - BOM (Byte Order Mark) stripping for cross-platform compatibility
|
|
149
|
-
* - Enhanced error messages with filepath context
|
|
150
|
-
* - Optional error suppression (returns `undefined` instead of throwing)
|
|
151
|
-
* - Optional reviver for transforming parsed values
|
|
152
|
-
*
|
|
153
|
-
* @param content - JSON string or Buffer to parse
|
|
154
|
-
* @param options - Optional parsing configuration
|
|
155
|
-
* @returns Parsed JSON value, or `undefined` if parsing fails and `throws` is `false`
|
|
156
|
-
*
|
|
157
|
-
* @throws {SyntaxError} When JSON is invalid and `throws` is `true` (default)
|
|
158
|
-
*
|
|
159
|
-
* @example
|
|
160
|
-
* ```ts
|
|
161
|
-
* // Basic usage
|
|
162
|
-
* const data = jsonParse('{"name":"example"}')
|
|
163
|
-
* console.log(data.name) // => 'example'
|
|
164
|
-
*
|
|
165
|
-
* // Parse Buffer with UTF-8 BOM
|
|
166
|
-
* const buffer = Buffer.from('\uFEFF{"value":42}')
|
|
167
|
-
* const data = jsonParse(buffer)
|
|
168
|
-
* console.log(data.value) // => 42
|
|
169
|
-
*
|
|
170
|
-
* // Enhanced error messages with filepath
|
|
171
|
-
* try {
|
|
172
|
-
* jsonParse('invalid', { filepath: 'config.json' })
|
|
173
|
-
* } catch (err) {
|
|
174
|
-
* console.error(err.message)
|
|
175
|
-
* // => "config.json: Unexpected token i in JSON at position 0"
|
|
176
|
-
* }
|
|
177
|
-
*
|
|
178
|
-
* // Suppress errors
|
|
179
|
-
* const result = jsonParse('invalid', { throws: false })
|
|
180
|
-
* console.log(result) // => undefined
|
|
181
|
-
*
|
|
182
|
-
* // Transform values with reviver
|
|
183
|
-
* const json = '{"created":"2024-01-15T10:30:00Z"}'
|
|
184
|
-
* const data = jsonParse(json, {
|
|
185
|
-
* reviver: (key, value) => {
|
|
186
|
-
* if (key === 'created' && typeof value === 'string') {
|
|
187
|
-
* return new Date(value)
|
|
188
|
-
* }
|
|
189
|
-
* return value
|
|
190
|
-
* }
|
|
191
|
-
* })
|
|
192
|
-
* console.log(data.created instanceof Date) // => true
|
|
193
|
-
* ```
|
|
194
|
-
*/
|
|
195
|
-
/*@__NO_SIDE_EFFECTS__*/
|
|
196
|
-
export declare function jsonParse(content: string | Buffer, options?: JsonParseOptions | undefined): JsonValue | undefined;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|