@powerduck/conf-patch 0.3.1 → 0.3.3
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/README.md +203 -255
- package/dist/{core-CDrue4z-.d.mts → core-ymvYwGVG.d.mts} +3 -3
- package/dist/{core-CDrue4z-.d.ts → core-ymvYwGVG.d.ts} +3 -3
- package/dist/core.d.mts +1 -1
- package/dist/core.d.ts +1 -1
- package/dist/index.d.mts +7 -8
- package/dist/index.d.ts +7 -8
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,348 +1,296 @@
|
|
|
1
|
-
# @
|
|
1
|
+
# @powerduck/conf-patch
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Production-grade configuration file editor with a clean two-layer architecture: a browser-safe core layer for patching JSON/JSONC/YAML strings, and a Node.js/Electron file layer with atomic writes and cross-process file locking.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/@powerduck/conf-patch)
|
|
6
|
+
[](https://github.com/PowerDuckie/conf-patch/blob/main/LICENSE)
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
## Links
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
- [Official Website](https://www.powerduck.com/opensource/conf-patch.html)
|
|
11
|
+
- [Documentation](https://www.powerduck.com/docs/conf-patch/introduction)
|
|
12
|
+
- [Live Demo](https://www.powerduck.com/demo/conf-patch.html)
|
|
13
|
+
- [GitHub](https://github.com/PowerDuckie/conf-patch)
|
|
14
|
+
- [npm](https://www.npmjs.com/package/@powerduck/conf-patch)
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
11
19
|
|
|
12
|
-
###
|
|
13
|
-
- **`patchContent`** — Apply RFC 6902 patch operations to a configuration string
|
|
14
|
-
- **`setContentValue`** — Set or create a nested value
|
|
15
|
-
- **`deleteContentValue`** — Remove a key or array element
|
|
16
|
-
- **No filesystem access** — Works in browsers, IndexedDB, localStorage, or any storage layer
|
|
17
|
-
|
|
18
|
-
### File Layer (Node.js / Electron)
|
|
19
|
-
- **`readConfigFile`** — Read a configuration file
|
|
20
|
-
- **`writeConfigFile`** — Write a file with atomic writes and optional locking
|
|
21
|
-
- **`patchConfigFile`** — Read-patch-write transaction inside a file lock
|
|
22
|
-
- **`setConfigValue`** — Set a value in a file
|
|
23
|
-
- **`deleteConfigValue`** — Delete a value from a file
|
|
24
|
-
- **Atomic writes** — Temp file + rename pattern, no partial writes on crash
|
|
25
|
-
- **Cross-process file locking** — Exclusive locks with ownership tokens, exponential backoff, stale-lock recovery
|
|
26
|
-
|
|
27
|
-
### Format Support
|
|
28
|
-
- **JSON** — Standard JSON
|
|
29
|
-
- **JSONC** — JSON with comments and trailing commas (preserved)
|
|
30
|
-
- **YAML** — YAML 1.2 with comments and indentation preservation
|
|
31
|
-
|
|
32
|
-
### OpenAPI Validation
|
|
33
|
-
- **`validateOpenAPISpec`** — Validate raw OpenAPI/Swagger content (JSON or YAML)
|
|
34
|
-
- **`validateOpenAPIFile`** — Validate from a file path
|
|
35
|
-
- Delegates to `@powerduck/openapi-parser` with secure input handling (size limits, path sandboxing, deadlines, structural complexity guards)
|
|
36
|
-
|
|
37
|
-
## Installation
|
|
20
|
+
### Install
|
|
38
21
|
|
|
39
22
|
```bash
|
|
40
|
-
npm install @
|
|
41
|
-
pnpm add @powerduckie/confedit
|
|
42
|
-
yarn add @powerduckie/confedit
|
|
23
|
+
npm install @powerduck/conf-patch
|
|
43
24
|
```
|
|
44
25
|
|
|
45
|
-
|
|
26
|
+
### Patch a JSON string (browser-safe, no filesystem)
|
|
46
27
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
### CommonJS (Node.js / Electron main process)
|
|
28
|
+
```typescript
|
|
29
|
+
import { patchContent } from "@powerduck/conf-patch/core";
|
|
50
30
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
31
|
+
const updated = patchContent(
|
|
32
|
+
'{"name": "app", "version": "1.0.0"}',
|
|
33
|
+
[
|
|
34
|
+
{ op: "replace", path: ["version"], value: "2.0.0" },
|
|
35
|
+
{ op: "add", path: ["description"], value: "My application" },
|
|
36
|
+
],
|
|
37
|
+
"json",
|
|
38
|
+
);
|
|
54
39
|
|
|
55
|
-
|
|
56
|
-
|
|
40
|
+
console.log(updated);
|
|
41
|
+
// {
|
|
42
|
+
// "name": "app",
|
|
43
|
+
// "version": "2.0.0",
|
|
44
|
+
// "description": "My application"
|
|
45
|
+
// }
|
|
57
46
|
```
|
|
58
47
|
|
|
59
|
-
###
|
|
48
|
+
### Set a value in a file (Node.js / Electron)
|
|
60
49
|
|
|
61
50
|
```typescript
|
|
62
|
-
|
|
63
|
-
import { setConfigValue, readConfigFile } from "@powerduckie/confedit";
|
|
51
|
+
import { setConfigValue, readConfigFile } from "@powerduck/conf-patch";
|
|
64
52
|
|
|
65
|
-
//
|
|
66
|
-
|
|
53
|
+
// Atomic write with file locking
|
|
54
|
+
await setConfigValue("config.yaml", ["database", "port"], 5432);
|
|
55
|
+
|
|
56
|
+
const content = await readConfigFile("config.yaml");
|
|
57
|
+
console.log(content);
|
|
67
58
|
```
|
|
68
59
|
|
|
69
|
-
###
|
|
60
|
+
### Validate an OpenAPI document
|
|
70
61
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
| Main | `dist/index.js` (26KB) | `dist/index.mjs` (16KB) | `dist/index.d.ts` |
|
|
74
|
-
| Core | `dist/core.js` (9.5KB) | `dist/core.mjs` (1KB) | `dist/core.d.ts` |
|
|
62
|
+
```typescript
|
|
63
|
+
import { validateOpenAPISpec, OpenApiValidationError } from "@powerduck/conf-patch";
|
|
75
64
|
|
|
76
|
-
|
|
65
|
+
try {
|
|
66
|
+
const doc = await validateOpenAPISpec(`
|
|
67
|
+
openapi: 3.1.0
|
|
68
|
+
info:
|
|
69
|
+
title: Demo API
|
|
70
|
+
version: 1.0.0
|
|
71
|
+
paths: {}
|
|
72
|
+
`);
|
|
73
|
+
console.log("Valid. openapi =", doc.openapi);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
if (error instanceof OpenApiValidationError) {
|
|
76
|
+
console.error(`[${error.code}]`, error.message);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
77
80
|
|
|
78
|
-
|
|
81
|
+
---
|
|
79
82
|
|
|
80
|
-
|
|
83
|
+
## Features
|
|
81
84
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
+
- **Two-layer architecture** — core layer runs in browsers, Edge Functions, and IndexedDB; file layer adds atomic writes and locking for Node.js/Electron
|
|
86
|
+
- **JSON, JSONC, and YAML support** — patch all three formats with one API
|
|
87
|
+
- **Comment-preserving edits** — JSONC edits are range-based, so comments and trailing commas around touched lines are preserved
|
|
88
|
+
- **RFC 6902 JSON Patch** — `add`, `replace`, and `remove` operations with strict/non-strict modes
|
|
89
|
+
- **Atomic writes** — temp file + rename, so a crash never leaves a half-written file
|
|
90
|
+
- **Cross-process file locking** — ownership tokens, exponential backoff, and stale-lock recovery
|
|
91
|
+
- **OpenAPI validation** — built on `@powerduck/openapi-parser` with secure input handling, size limits, and DoS guards
|
|
92
|
+
- **Format auto-detection** — file extension detection for `.json`, `.jsonc`, `.yaml`, `.yml`
|
|
93
|
+
- **Dual ESM/CJS builds** — works with `import` and `require`, with bundled TypeScript declarations
|
|
85
94
|
|
|
86
|
-
|
|
87
|
-
import { patchContent, setContentValue, deleteContentValue } from "@powerduckie/confedit/core";
|
|
95
|
+
---
|
|
88
96
|
|
|
89
|
-
|
|
90
|
-
const updated = patchContent(
|
|
91
|
-
'{"name": "app"}',
|
|
92
|
-
[{ op: "add", path: ["version"], value: "1.0.0" }],
|
|
93
|
-
"json",
|
|
94
|
-
);
|
|
97
|
+
## Architecture
|
|
95
98
|
|
|
96
|
-
|
|
97
|
-
|
|
99
|
+
```
|
|
100
|
+
┌──────────────────────────────────────────────────────────────┐
|
|
101
|
+
│ Application code │
|
|
102
|
+
├──────────────────────────────────────────────────────────────┤
|
|
103
|
+
│ File layer (Node.js / Electron only) │
|
|
104
|
+
│ ┌───────────────┐ ┌────────────────┐ ┌──────────────────┐ │
|
|
105
|
+
│ │ readConfigFile │ │ writeConfigFile│ │ patchConfigFile │ │
|
|
106
|
+
│ │ setConfigValue │ │ deleteConfig… │ │ withFileLock │ │
|
|
107
|
+
│ └───────┬───────┘ └───────┬────────┘ └────────┬─────────┘ │
|
|
108
|
+
├──────────┼───────────────────┼─────────────────────┼──────────┤
|
|
109
|
+
│ Core layer (browser-safe, no filesystem) │
|
|
110
|
+
│ ┌───────────────┐ ┌────────────────┐ ┌──────────────────┐ │
|
|
111
|
+
│ │ patchContent │ │ setContentValue│ │ deleteContentValue│ │
|
|
112
|
+
│ └───────────────┘ └────────────────┘ └──────────────────┘ │
|
|
113
|
+
└──────────────────────────────────────────────────────────────┘
|
|
114
|
+
```
|
|
98
115
|
|
|
99
|
-
|
|
100
|
-
const cleaned = deleteContentValue(updated, ["legacy"], "json");
|
|
116
|
+
---
|
|
101
117
|
|
|
102
|
-
|
|
103
|
-
localStorage.setItem("config", updated);
|
|
104
|
-
```
|
|
118
|
+
## Core Layer (Browser-Safe)
|
|
105
119
|
|
|
106
|
-
|
|
120
|
+
Import from `@powerduck/conf-patch/core` for pure string operations with no filesystem access.
|
|
121
|
+
|
|
122
|
+
### `patchContent(content, ops, format, options?)`
|
|
107
123
|
|
|
108
124
|
```typescript
|
|
109
|
-
|
|
125
|
+
function patchContent(
|
|
126
|
+
content: string,
|
|
127
|
+
ops: JsonPatchOp[],
|
|
128
|
+
format: ConfigFormat,
|
|
129
|
+
options?: PatchContentOptions,
|
|
130
|
+
): string;
|
|
131
|
+
```
|
|
110
132
|
|
|
111
|
-
|
|
112
|
-
await setConfigValue("config.yaml", ["database", "port"], 5432);
|
|
133
|
+
Applies an array of RFC 6902 patch operations. Returns the patched content.
|
|
113
134
|
|
|
114
|
-
|
|
115
|
-
|
|
135
|
+
| Parameter | Type | Description |
|
|
136
|
+
|---|---|---|
|
|
137
|
+
| `content` | `string` | The raw configuration content |
|
|
138
|
+
| `ops` | `JsonPatchOp[]` | Operations to apply in order |
|
|
139
|
+
| `format` | `ConfigFormat` | `"json" \| "jsonc" \| "yaml"` |
|
|
140
|
+
| `options.strict` | `boolean` | When `true` (default), failed operations throw. When `false`, skipped with a warning. |
|
|
116
141
|
|
|
117
|
-
|
|
118
|
-
await patchConfigFile("config.jsonc", [
|
|
119
|
-
{ op: "replace", path: ["server", "host"], value: "0.0.0.0" },
|
|
120
|
-
{ op: "add", path: ["server", "ssl"], value: true },
|
|
121
|
-
{ op: "remove", path: ["legacySection"] },
|
|
122
|
-
]);
|
|
123
|
-
```
|
|
142
|
+
### `setContentValue(content, path, value, format)`
|
|
124
143
|
|
|
125
|
-
|
|
144
|
+
Sets or creates a single value. Internally calls `patchContent` with one `add` operation.
|
|
126
145
|
|
|
127
|
-
|
|
128
|
-
// In the Electron main process (CommonJS)
|
|
129
|
-
const { setConfigValue, readConfigFile } = require("@powerduckie/confedit");
|
|
146
|
+
### `deleteContentValue(content, path, format)`
|
|
130
147
|
|
|
131
|
-
|
|
132
|
-
try {
|
|
133
|
-
await setConfigValue(filePath, path, value);
|
|
134
|
-
return { success: true };
|
|
135
|
-
} catch (error) {
|
|
136
|
-
return { success: false, error: error.message };
|
|
137
|
-
}
|
|
138
|
-
});
|
|
148
|
+
Removes a key or array element. The path must exist.
|
|
139
149
|
|
|
140
|
-
|
|
141
|
-
try {
|
|
142
|
-
const data = await readConfigFile(filePath);
|
|
143
|
-
return { success: true, data };
|
|
144
|
-
} catch (error) {
|
|
145
|
-
return { success: false, error: error.message };
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
```
|
|
150
|
+
---
|
|
149
151
|
|
|
150
|
-
##
|
|
152
|
+
## File Layer (Node.js / Electron)
|
|
151
153
|
|
|
152
|
-
|
|
154
|
+
Import from the main entry `@powerduck/conf-patch`.
|
|
153
155
|
|
|
154
|
-
|
|
156
|
+
### `readConfigFile(filePath)`
|
|
155
157
|
|
|
156
|
-
|
|
158
|
+
Reads UTF-8 text from a local file path or `file://` URL.
|
|
157
159
|
|
|
158
|
-
|
|
159
|
-
|---|---|---|
|
|
160
|
-
| `content` | `string` | The raw configuration content |
|
|
161
|
-
| `ops` | `JsonPatchOp[]` | Array of patch operations |
|
|
162
|
-
| `format` | `"json" \| "jsonc" \| "yaml"` | The configuration format |
|
|
163
|
-
| `options.strict` | `boolean` | When true, failed operations throw. Default: `true` |
|
|
160
|
+
### `writeConfigFile(filePath, content, options?)`
|
|
164
161
|
|
|
165
|
-
|
|
162
|
+
Writes content using an atomic write (temp file + rename) and exclusive file locking.
|
|
166
163
|
|
|
167
|
-
|
|
164
|
+
### `patchConfigFile(filePath, ops, options?)`
|
|
168
165
|
|
|
169
|
-
|
|
166
|
+
The primary file-layer transaction: read the file inside a lock, apply `patchContent`, and write back atomically only if the content changed.
|
|
170
167
|
|
|
171
|
-
|
|
168
|
+
### `setConfigValue(filePath, path, value, options?)`
|
|
172
169
|
|
|
173
|
-
|
|
170
|
+
Adds or replaces a nested value in a file.
|
|
174
171
|
|
|
175
|
-
###
|
|
172
|
+
### `deleteConfigValue(filePath, path, options?)`
|
|
176
173
|
|
|
177
|
-
|
|
174
|
+
Removes an existing nested value from a file.
|
|
178
175
|
|
|
179
|
-
|
|
176
|
+
### `withFileLock(filePath, callback, options?)`
|
|
180
177
|
|
|
181
|
-
|
|
178
|
+
Runs an async callback under a process-local queue and an exclusive lock file.
|
|
182
179
|
|
|
183
|
-
|
|
180
|
+
---
|
|
184
181
|
|
|
185
|
-
|
|
186
|
-
|---|---|---|
|
|
187
|
-
| `lock` | `boolean` | Enable file locking. Default: `true` |
|
|
188
|
-
| `lockTimeoutMs` | `number` | Maximum time to wait for a lock |
|
|
189
|
-
| `lockRetryDelayMs` | `number` | Initial retry delay (exponential backoff) |
|
|
190
|
-
| `lockStaleThresholdMs` | `number` | Lock age after which recovery is allowed |
|
|
191
|
-
| `allowStaleRecovery` | `boolean` | Allow automatic stale-lock recovery |
|
|
182
|
+
## OpenAPI Validation
|
|
192
183
|
|
|
193
|
-
|
|
184
|
+
### `validateOpenAPISpec(input, options?)`
|
|
194
185
|
|
|
195
|
-
|
|
186
|
+
Validates raw OpenAPI/Swagger content (JSON or YAML). Returns the validated document on success, throws `OpenApiValidationError` on failure.
|
|
196
187
|
|
|
197
|
-
|
|
198
|
-
|---|---|---|
|
|
199
|
-
| `format` | `"json" \| "jsonc" \| "yaml"` | Explicit format (auto-detected from extension if omitted) |
|
|
200
|
-
| `strict` | `boolean` | When true, failed operations throw. Default: `true` |
|
|
201
|
-
| `lock` | `boolean` | Enable file locking. Default: `true` |
|
|
202
|
-
| `lockTimeoutMs` | `number` | Maximum time to wait for a lock |
|
|
203
|
-
| `lockRetryDelayMs` | `number` | Initial retry delay |
|
|
204
|
-
| `lockStaleThresholdMs` | `number` | Lock age after which recovery is allowed |
|
|
205
|
-
| `allowStaleRecovery` | `boolean` | Allow automatic stale-lock recovery |
|
|
188
|
+
### `validateOpenAPIFile(filePath, options?)`
|
|
206
189
|
|
|
207
|
-
|
|
190
|
+
Convenience wrapper that validates from a file path. Requires `allowedRootDirectory` for path sandboxing.
|
|
208
191
|
|
|
209
|
-
|
|
192
|
+
### `OpenApiValidationError`
|
|
210
193
|
|
|
211
|
-
|
|
194
|
+
Structured error with a machine-readable `code` property:
|
|
212
195
|
|
|
213
|
-
|
|
196
|
+
| Code | Meaning |
|
|
197
|
+
|---|---|
|
|
198
|
+
| `INVALID_OPTION` | Invalid option value provided |
|
|
199
|
+
| `INPUT_TOO_LARGE` | Raw content exceeds `maxInputBytes` |
|
|
200
|
+
| `FILE_INPUT_FORBIDDEN` | File input used without `allowedRootDirectory` |
|
|
201
|
+
| `PATH_OUTSIDE_ROOT` | File path resolves outside `allowedRootDirectory` |
|
|
202
|
+
| `PARSE_ERROR` | Failed to parse JSON/YAML content |
|
|
203
|
+
| `UNSUPPORTED_VERSION` | Document does not declare a supported OpenAPI/Swagger version |
|
|
204
|
+
| `SPEC_VALIDATION_FAILED` | The spec failed OpenAPI validation |
|
|
205
|
+
| `OPERATION_TIMEOUT` | Operation exceeded `timeoutMs` |
|
|
206
|
+
| `OPERATION_ABORTED` | Operation was aborted via `signal` |
|
|
207
|
+
|
|
208
|
+
---
|
|
214
209
|
|
|
215
|
-
|
|
210
|
+
## Types
|
|
216
211
|
|
|
217
212
|
```typescript
|
|
218
213
|
type ConfigFormat = "json" | "jsonc" | "yaml";
|
|
219
|
-
type JsonPathSegment = string | number;
|
|
220
214
|
|
|
221
215
|
interface JsonPatchOp {
|
|
222
216
|
op: "add" | "replace" | "remove";
|
|
223
|
-
path:
|
|
224
|
-
value?: unknown; //
|
|
217
|
+
path: (string | number)[];
|
|
218
|
+
value?: unknown; // Required for "add" and "replace"
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
interface PatchContentOptions {
|
|
222
|
+
strict?: boolean; // default: true
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
interface PatchConfigOptions {
|
|
226
|
+
format?: ConfigFormat;
|
|
227
|
+
strict?: boolean;
|
|
228
|
+
lock?: boolean;
|
|
229
|
+
lockTimeoutMs?: number;
|
|
230
|
+
lockRetryDelayMs?: number;
|
|
231
|
+
lockStaleThresholdMs?: number;
|
|
232
|
+
allowStaleRecovery?: boolean;
|
|
225
233
|
}
|
|
226
234
|
```
|
|
227
235
|
|
|
228
|
-
|
|
236
|
+
---
|
|
229
237
|
|
|
230
|
-
|
|
231
|
-
import { validateOpenAPISpec, validateOpenAPIFile, OpenApiValidationError } from "@powerduckie/confedit";
|
|
238
|
+
## Electron IPC Example
|
|
232
239
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
+
```javascript
|
|
241
|
+
// Electron main process (CommonJS)
|
|
242
|
+
const { setConfigValue, readConfigFile } = require("@powerduck/conf-patch");
|
|
243
|
+
|
|
244
|
+
ipcMain.handle("config:set", async (_event, { filePath, path, value }) => {
|
|
245
|
+
try {
|
|
246
|
+
await setConfigValue(filePath, path, value);
|
|
247
|
+
return { success: true };
|
|
248
|
+
} catch (error) {
|
|
249
|
+
return { success: false, error: error.message };
|
|
240
250
|
}
|
|
241
|
-
}
|
|
251
|
+
});
|
|
242
252
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
253
|
+
ipcMain.handle("config:read", async (_event, { filePath }) => {
|
|
254
|
+
try {
|
|
255
|
+
const data = await readConfigFile(filePath);
|
|
256
|
+
return { success: true, data };
|
|
257
|
+
} catch (error) {
|
|
258
|
+
return { success: false, error: error.message };
|
|
259
|
+
}
|
|
246
260
|
});
|
|
247
261
|
```
|
|
248
262
|
|
|
249
|
-
|
|
263
|
+
---
|
|
250
264
|
|
|
251
|
-
|
|
265
|
+
## Development
|
|
252
266
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
| `baseFilePath` | `string` | - | Base path for reference resolution context |
|
|
257
|
-
| `allowedRootDirectory` | `string` | - | Root directory for file input (required for file mode) |
|
|
258
|
-
| `timeoutMs` | `number` | `15000` | Operation deadline in milliseconds |
|
|
259
|
-
| `maxInputBytes` | `number` | `5242880` (5MB) | Maximum raw input size in bytes |
|
|
260
|
-
| `maxDocumentNodes` | `number` | `100000` | Maximum nodes in parsed document (DoS protection) |
|
|
261
|
-
| `maxDocumentDepth` | `number` | `100` | Maximum nesting depth (DoS protection) |
|
|
262
|
-
| `maxValidationErrors` | `number` | `50` | Maximum validation errors included in error message |
|
|
263
|
-
| `maxErrorMessageLength` | `number` | `1000` | Maximum characters per validation error |
|
|
264
|
-
| `signal` | `AbortSignal` | - | Optional cancellation signal |
|
|
267
|
+
```bash
|
|
268
|
+
# Install dependencies
|
|
269
|
+
npm install
|
|
265
270
|
|
|
266
|
-
|
|
271
|
+
# Type check
|
|
272
|
+
npm run typecheck
|
|
267
273
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
| `INVALID_OPTION` | Invalid option value provided |
|
|
271
|
-
| `INPUT_TOO_LARGE` | Input exceeds maximum allowed size |
|
|
272
|
-
| `INPUT_FILE_TOO_LARGE` | File exceeds maximum allowed size |
|
|
273
|
-
| `FILE_INPUT_FORBIDDEN` | File input used without `allowedRootDirectory` |
|
|
274
|
-
| `PATH_OUTSIDE_ROOT` | File path is outside the allowed root directory |
|
|
275
|
-
| `PARSE_ERROR` | Failed to parse JSON/YAML content |
|
|
276
|
-
| `INVALID_DOCUMENT_SHAPE` | Document is not a JSON object |
|
|
277
|
-
| `UNSUPPORTED_VERSION` | Document does not declare a supported OpenAPI/Swagger version |
|
|
278
|
-
| `DOCUMENT_TOO_DEEP` | Document exceeds maximum nesting depth |
|
|
279
|
-
| `DOCUMENT_TOO_LARGE` | Document exceeds maximum node count |
|
|
280
|
-
| `SPEC_VALIDATION_FAILED` | OpenAPI specification validation failed |
|
|
281
|
-
| `OPERATION_TIMEOUT` | Operation timed out |
|
|
282
|
-
| `OPERATION_ABORTED` | Operation was aborted via signal |
|
|
283
|
-
| `UNKNOWN_ERROR` | Unexpected error occurred |
|
|
284
|
-
|
|
285
|
-
## Behavior Notes
|
|
286
|
-
|
|
287
|
-
### Array Operations
|
|
288
|
-
- `add` at an array index **inserts** the element (RFC 6902 behavior)
|
|
289
|
-
- `replace` at an array index **replaces** the element
|
|
290
|
-
- To replace an existing array element, use `patchConfigFile` with `op: "replace"`
|
|
291
|
-
|
|
292
|
-
### Nested Paths
|
|
293
|
-
- `add` requires the parent path to exist. To create a nested object, first create the parent, then the child.
|
|
294
|
-
- `replace` and `remove` require the target path to exist.
|
|
295
|
-
|
|
296
|
-
### Format Detection
|
|
297
|
-
- `.json` → JSON
|
|
298
|
-
- `.jsonc` → JSON with comments
|
|
299
|
-
- `.yaml` / `.yml` → YAML
|
|
300
|
-
- Other extensions → throw unless `format` is explicitly set
|
|
274
|
+
# Build (ESM + CJS + type declarations)
|
|
275
|
+
npm run build
|
|
301
276
|
|
|
302
|
-
|
|
277
|
+
# Run tests
|
|
278
|
+
npm test
|
|
303
279
|
|
|
280
|
+
# Watch mode
|
|
281
|
+
npx vitest watch
|
|
304
282
|
```
|
|
305
|
-
┌─────────────────────────────────────────────────────────┐
|
|
306
|
-
│ Application Code │
|
|
307
|
-
├─────────────────────────────────────────────────────────┤
|
|
308
|
-
│ File Layer (Node.js / Electron only) │
|
|
309
|
-
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
|
|
310
|
-
│ │ readConfigFile│ │writeConfigFile│ │patchConfigFile│ │
|
|
311
|
-
│ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ │
|
|
312
|
-
│ │ │ │ │
|
|
313
|
-
│ ┌──────▼───────────────────▼───────────────────▼───────┐│
|
|
314
|
-
│ │ Core Layer (Browser-Safe) ││
|
|
315
|
-
│ │ patchContent setContentValue deleteContentValue ││
|
|
316
|
-
│ └────────────────────────────────────────────────────────┘│
|
|
317
|
-
└───────────────────────────────────────────────────────────┘
|
|
318
|
-
```
|
|
319
|
-
|
|
320
|
-
The core layer has zero dependencies on `node:fs`, `node:path`, or any Node.js-specific APIs. It can be used in browsers, Edge Functions, or any JavaScript environment.
|
|
321
283
|
|
|
322
|
-
|
|
284
|
+
---
|
|
323
285
|
|
|
324
|
-
##
|
|
286
|
+
## Related Packages
|
|
325
287
|
|
|
326
|
-
-
|
|
327
|
-
-
|
|
328
|
-
-
|
|
329
|
-
- **Process-local queue**: Same-file operations are serialized in-process to avoid lock contention.
|
|
330
|
-
- **Bundle size**: Core layer ~15KB gzipped. Full library ~30KB gzipped.
|
|
288
|
+
- [`@powerduck/openapi-parser`](https://www.npmjs.com/package/@powerduck/openapi-parser) — OpenAPI 3.2 parser, validator, and upgrader
|
|
289
|
+
- [`@powerduck/openapi-request`](https://www.npmjs.com/package/@powerduck/openapi-request) — Execute OpenAPI operations with full parameter serialization
|
|
290
|
+
- [`@powerduck/openapi-codegen`](https://www.npmjs.com/package/@powerduck/openapi-codegen) — Generate runnable request examples in 21 languages
|
|
331
291
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
```bash
|
|
335
|
-
npm test # Run all tests (280 tests)
|
|
336
|
-
npm run typecheck # TypeScript type checking
|
|
337
|
-
npm run build # Build CJS + ESM + type declarations
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
Test coverage: 280 tests across 12 test files, covering all public APIs, edge cases, concurrency, error handling, browser compatibility, dual-module (CJS/ESM) support, and OpenAPI validation security controls.
|
|
292
|
+
---
|
|
341
293
|
|
|
342
294
|
## License
|
|
343
295
|
|
|
344
|
-
MIT
|
|
345
|
-
|
|
346
|
-
## Repository
|
|
347
|
-
|
|
348
|
-
[https://github.com/PowerDuckie/confedit](https://github.com/PowerDuckie/confedit)
|
|
296
|
+
MIT
|
|
@@ -54,7 +54,7 @@ interface PatchContentOptions {
|
|
|
54
54
|
*
|
|
55
55
|
* @example
|
|
56
56
|
* ```typescript
|
|
57
|
-
* import { patchContent } from "@
|
|
57
|
+
* import { patchContent } from "@powerduck/conf-patch";
|
|
58
58
|
*
|
|
59
59
|
* const updated = patchContent(
|
|
60
60
|
* '{"name": "app"}',
|
|
@@ -81,7 +81,7 @@ declare function patchContent(content: string, ops: JsonPatchOp[], format: Confi
|
|
|
81
81
|
*
|
|
82
82
|
* @example
|
|
83
83
|
* ```typescript
|
|
84
|
-
* import { setContentValue } from "@
|
|
84
|
+
* import { setContentValue } from "@powerduck/conf-patch";
|
|
85
85
|
*
|
|
86
86
|
* const updated = setContentValue(
|
|
87
87
|
* "name: app\n",
|
|
@@ -104,7 +104,7 @@ declare function setContentValue(content: string, path: readonly (string | numbe
|
|
|
104
104
|
*
|
|
105
105
|
* @example
|
|
106
106
|
* ```typescript
|
|
107
|
-
* import { deleteContentValue } from "@
|
|
107
|
+
* import { deleteContentValue } from "@powerduck/conf-patch";
|
|
108
108
|
*
|
|
109
109
|
* const updated = deleteContentValue(
|
|
110
110
|
* '{"name": "app", "legacy": true}',
|
|
@@ -54,7 +54,7 @@ interface PatchContentOptions {
|
|
|
54
54
|
*
|
|
55
55
|
* @example
|
|
56
56
|
* ```typescript
|
|
57
|
-
* import { patchContent } from "@
|
|
57
|
+
* import { patchContent } from "@powerduck/conf-patch";
|
|
58
58
|
*
|
|
59
59
|
* const updated = patchContent(
|
|
60
60
|
* '{"name": "app"}',
|
|
@@ -81,7 +81,7 @@ declare function patchContent(content: string, ops: JsonPatchOp[], format: Confi
|
|
|
81
81
|
*
|
|
82
82
|
* @example
|
|
83
83
|
* ```typescript
|
|
84
|
-
* import { setContentValue } from "@
|
|
84
|
+
* import { setContentValue } from "@powerduck/conf-patch";
|
|
85
85
|
*
|
|
86
86
|
* const updated = setContentValue(
|
|
87
87
|
* "name: app\n",
|
|
@@ -104,7 +104,7 @@ declare function setContentValue(content: string, path: readonly (string | numbe
|
|
|
104
104
|
*
|
|
105
105
|
* @example
|
|
106
106
|
* ```typescript
|
|
107
|
-
* import { deleteContentValue } from "@
|
|
107
|
+
* import { deleteContentValue } from "@powerduck/conf-patch";
|
|
108
108
|
*
|
|
109
109
|
* const updated = deleteContentValue(
|
|
110
110
|
* '{"name": "app", "legacy": true}',
|
package/dist/core.d.mts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { C as ConfigFormat, J as JsonPatchOp, a as JsonPathSegment, b as PatchContentOptions, c as assertConfigFormat, d as assertNonEmptyString, e as assertPatchOperations, f as assertPatchPath, g as createError, h as deleteContentValue, i as getErrorMessage, p as patchContent, s as setContentValue } from './core-
|
|
1
|
+
export { C as ConfigFormat, J as JsonPatchOp, a as JsonPathSegment, b as PatchContentOptions, c as assertConfigFormat, d as assertNonEmptyString, e as assertPatchOperations, f as assertPatchPath, g as createError, h as deleteContentValue, i as getErrorMessage, p as patchContent, s as setContentValue } from './core-ymvYwGVG.mjs';
|
package/dist/core.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { C as ConfigFormat, J as JsonPatchOp, a as JsonPathSegment, b as PatchContentOptions, c as assertConfigFormat, d as assertNonEmptyString, e as assertPatchOperations, f as assertPatchPath, g as createError, h as deleteContentValue, i as getErrorMessage, p as patchContent, s as setContentValue } from './core-
|
|
1
|
+
export { C as ConfigFormat, J as JsonPatchOp, a as JsonPathSegment, b as PatchContentOptions, c as assertConfigFormat, d as assertNonEmptyString, e as assertPatchOperations, f as assertPatchPath, g as createError, h as deleteContentValue, i as getErrorMessage, p as patchContent, s as setContentValue } from './core-ymvYwGVG.js';
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { P as PatchConfigOptions, J as JsonPatchOp, C as ConfigFormat } from './core-
|
|
2
|
-
export { F as FileLockOptions, a as JsonPathSegment, b as PatchContentOptions, c as assertConfigFormat, d as assertNonEmptyString, e as assertPatchOperations, f as assertPatchPath, g as createError, h as deleteContentValue, i as getErrorMessage, p as patchContent, s as setContentValue } from './core-
|
|
1
|
+
import { P as PatchConfigOptions, J as JsonPatchOp, C as ConfigFormat } from './core-ymvYwGVG.mjs';
|
|
2
|
+
export { F as FileLockOptions, a as JsonPathSegment, b as PatchContentOptions, c as assertConfigFormat, d as assertNonEmptyString, e as assertPatchOperations, f as assertPatchPath, g as createError, h as deleteContentValue, i as getErrorMessage, p as patchContent, s as setContentValue } from './core-ymvYwGVG.mjs';
|
|
3
3
|
import { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -44,7 +44,7 @@ interface WriteConfigOptions {
|
|
|
44
44
|
*
|
|
45
45
|
* @example
|
|
46
46
|
* ```typescript
|
|
47
|
-
* import { writeConfigFile } from "@
|
|
47
|
+
* import { writeConfigFile } from "@powerduck/conf-patch";
|
|
48
48
|
*
|
|
49
49
|
* await writeConfigFile("config.json", '{"name": "app"}');
|
|
50
50
|
* ```
|
|
@@ -63,7 +63,7 @@ declare function writeConfigFile(filePath: string, content: string, options?: Wr
|
|
|
63
63
|
*
|
|
64
64
|
* @example
|
|
65
65
|
* ```typescript
|
|
66
|
-
* import { patchConfigFile } from "@
|
|
66
|
+
* import { patchConfigFile } from "@powerduck/conf-patch";
|
|
67
67
|
*
|
|
68
68
|
* await patchConfigFile("config.json", [
|
|
69
69
|
* { op: "replace", path: ["server", "host"], value: "0.0.0.0" },
|
|
@@ -88,7 +88,7 @@ declare function patchConfigFile(filePath: string, ops: JsonPatchOp[], options?:
|
|
|
88
88
|
*
|
|
89
89
|
* @example
|
|
90
90
|
* ```typescript
|
|
91
|
-
* import { setConfigValue } from "@
|
|
91
|
+
* import { setConfigValue } from "@powerduck/conf-patch";
|
|
92
92
|
*
|
|
93
93
|
* await setConfigValue("config.yaml", ["database", "port"], 5432);
|
|
94
94
|
* ```
|
|
@@ -105,7 +105,7 @@ declare function setConfigValue(filePath: string, path: readonly (string | numbe
|
|
|
105
105
|
*
|
|
106
106
|
* @example
|
|
107
107
|
* ```typescript
|
|
108
|
-
* import { deleteConfigValue } from "@
|
|
108
|
+
* import { deleteConfigValue } from "@powerduck/conf-patch";
|
|
109
109
|
*
|
|
110
110
|
* await deleteConfigValue("config.json", ["features", "betaPreview"]);
|
|
111
111
|
* ```
|
|
@@ -183,8 +183,7 @@ declare class OpenApiValidationError extends Error {
|
|
|
183
183
|
* Input is treated as content by default. File-path interpretation is only
|
|
184
184
|
* enabled by explicitly setting inputKind to "file".
|
|
185
185
|
*
|
|
186
|
-
* Validation is delegated to @powerduck/openapi-parser
|
|
187
|
-
* @scalar/openapi-parser). This module adds secure input handling: size
|
|
186
|
+
* Validation is delegated to @powerduck/openapi-parser. This module adds secure input handling: size
|
|
188
187
|
* limits, path sandboxing, structural complexity guards, and deadlines.
|
|
189
188
|
*/
|
|
190
189
|
declare function validateOpenAPISpec(input: string, options?: ValidateOpenApiOptions): Promise<AnyOpenAPIDocument>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { P as PatchConfigOptions, J as JsonPatchOp, C as ConfigFormat } from './core-
|
|
2
|
-
export { F as FileLockOptions, a as JsonPathSegment, b as PatchContentOptions, c as assertConfigFormat, d as assertNonEmptyString, e as assertPatchOperations, f as assertPatchPath, g as createError, h as deleteContentValue, i as getErrorMessage, p as patchContent, s as setContentValue } from './core-
|
|
1
|
+
import { P as PatchConfigOptions, J as JsonPatchOp, C as ConfigFormat } from './core-ymvYwGVG.js';
|
|
2
|
+
export { F as FileLockOptions, a as JsonPathSegment, b as PatchContentOptions, c as assertConfigFormat, d as assertNonEmptyString, e as assertPatchOperations, f as assertPatchPath, g as createError, h as deleteContentValue, i as getErrorMessage, p as patchContent, s as setContentValue } from './core-ymvYwGVG.js';
|
|
3
3
|
import { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -44,7 +44,7 @@ interface WriteConfigOptions {
|
|
|
44
44
|
*
|
|
45
45
|
* @example
|
|
46
46
|
* ```typescript
|
|
47
|
-
* import { writeConfigFile } from "@
|
|
47
|
+
* import { writeConfigFile } from "@powerduck/conf-patch";
|
|
48
48
|
*
|
|
49
49
|
* await writeConfigFile("config.json", '{"name": "app"}');
|
|
50
50
|
* ```
|
|
@@ -63,7 +63,7 @@ declare function writeConfigFile(filePath: string, content: string, options?: Wr
|
|
|
63
63
|
*
|
|
64
64
|
* @example
|
|
65
65
|
* ```typescript
|
|
66
|
-
* import { patchConfigFile } from "@
|
|
66
|
+
* import { patchConfigFile } from "@powerduck/conf-patch";
|
|
67
67
|
*
|
|
68
68
|
* await patchConfigFile("config.json", [
|
|
69
69
|
* { op: "replace", path: ["server", "host"], value: "0.0.0.0" },
|
|
@@ -88,7 +88,7 @@ declare function patchConfigFile(filePath: string, ops: JsonPatchOp[], options?:
|
|
|
88
88
|
*
|
|
89
89
|
* @example
|
|
90
90
|
* ```typescript
|
|
91
|
-
* import { setConfigValue } from "@
|
|
91
|
+
* import { setConfigValue } from "@powerduck/conf-patch";
|
|
92
92
|
*
|
|
93
93
|
* await setConfigValue("config.yaml", ["database", "port"], 5432);
|
|
94
94
|
* ```
|
|
@@ -105,7 +105,7 @@ declare function setConfigValue(filePath: string, path: readonly (string | numbe
|
|
|
105
105
|
*
|
|
106
106
|
* @example
|
|
107
107
|
* ```typescript
|
|
108
|
-
* import { deleteConfigValue } from "@
|
|
108
|
+
* import { deleteConfigValue } from "@powerduck/conf-patch";
|
|
109
109
|
*
|
|
110
110
|
* await deleteConfigValue("config.json", ["features", "betaPreview"]);
|
|
111
111
|
* ```
|
|
@@ -183,8 +183,7 @@ declare class OpenApiValidationError extends Error {
|
|
|
183
183
|
* Input is treated as content by default. File-path interpretation is only
|
|
184
184
|
* enabled by explicitly setting inputKind to "file".
|
|
185
185
|
*
|
|
186
|
-
* Validation is delegated to @powerduck/openapi-parser
|
|
187
|
-
* @scalar/openapi-parser). This module adds secure input handling: size
|
|
186
|
+
* Validation is delegated to @powerduck/openapi-parser. This module adds secure input handling: size
|
|
188
187
|
* limits, path sandboxing, structural complexity guards, and deadlines.
|
|
189
188
|
*/
|
|
190
189
|
declare function validateOpenAPISpec(input: string, options?: ValidateOpenApiOptions): Promise<AnyOpenAPIDocument>;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.create;var e,t=Object.defineProperty,r=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,n=(Object.getPrototypeOf,Object.prototype.hasOwnProperty),__copyProps=(e,a,i,s)=>{if(a&&"object"==typeof a||"function"==typeof a)for(let c of o(a))n.call(e,c)||c===i||t(e,c,{get:()=>a[c],enumerable:!(s=r(a,c))||s.enumerable});return e},a={};((e,r)=>{for(var o in r)t(e,o,{get:r[o],enumerable:!0})})(a,{OpenApiValidationError:()=>I,assertConfigFormat:()=>assertConfigFormat,assertNonEmptyString:()=>assertNonEmptyString,assertPatchOperations:()=>assertPatchOperations,assertPatchPath:()=>assertPatchPath,createError:()=>createError2,deleteConfigValue:()=>deleteConfigValue,deleteContentValue:()=>deleteContentValue,detectFormat:()=>detectFormat,getErrorMessage:()=>getErrorMessage2,normalizeFilePath:()=>normalizeFilePath,patchConfigFile:()=>patchConfigFile,patchContent:()=>patchContent,readConfigFile:()=>readConfigFile,releaseAllLocalLocks:()=>releaseAllLocalLocks,setConfigValue:()=>setConfigValue,setContentValue:()=>setContentValue,validateOpenAPIFile:()=>validateOpenAPIFile,validateOpenAPISpec:()=>validateOpenAPISpec,withFileLock:()=>withFileLock,writeConfigFile:()=>writeConfigFile}),module.exports=(e=a,__copyProps(t({},"__esModule",{value:!0}),e));var i=require("jsonc-parser"),s={insertSpaces:!0,tabSize:2,eol:"\n"};function validateOperation(e){if(null===e||"object"!=typeof e)throw new TypeError("Patch operation must be an object.");if(!Array.isArray(e.path)||0===e.path.length)throw new Error("Patch operation path must be a non-empty array.");if("add"!==e.op&&"replace"!==e.op&&"remove"!==e.op)throw new Error(`Unsupported JSON patch operation: ${String(e.op)}`);if(("add"===e.op||"replace"===e.op)&&!Object.prototype.hasOwnProperty.call(e,"value"))throw new Error(`Patch operation "${e.op}" requires a value.`)}function normalizePath(e){return e.map(e=>{if("number"==typeof e){if(!Number.isSafeInteger(e)||e<0)throw new Error(`Invalid array index: ${e}`);return e}if("string"!=typeof e)throw new TypeError(`Patch path segments must be strings or numbers; received ${typeof e}.`);return e})}function applyAdd(e,t,r){const o=function(e,t){if(1===t.length){if("object"!==e.type&&"array"!==e.type)throw new Error("Cannot add a root child to a scalar JSON value.");return e}const r=t.slice(0,-1),o=(0,i.findNodeAtLocation)(e,r);if(void 0===o)throw new Error(`Cannot add value because its parent does not exist at path: ${formatPath(r)}`);if("object"!==o.type&&"array"!==o.type)throw new Error(`Cannot add value because its parent is not an object or array at path: ${formatPath(r)}`);return o}(getTree(e),t),n=t[t.length-1];if("array"===o.type){const a=function(e,t){if("number"!=typeof e)throw new Error(`Array index must be a number at path: ${formatPath(t)}`);if(!Number.isSafeInteger(e)||e<0)throw new Error(`Invalid array index ${e} at path: ${formatPath(t)}`);return e}(n,t),i=o.children?.length??0;if(a>i)throw new Error(`Cannot add at array index ${a}; array length is ${i} at path: ${formatPath(t.slice(0,-1))}`);return applyModify(e,t,r,!0)}if("object"===o.type){if("string"!=typeof n)throw new Error(`Object property path segment must be a string at path: ${formatPath(t)}`);return applyModify(e,t,r,!1)}throw new Error(`Cannot add a child to non-container value at path: ${formatPath(t.slice(0,-1))}`)}function applyReplace(e,t,r){const o=getTree(e);if(void 0===(0,i.findNodeAtLocation)(o,t))throw new Error(`Cannot replace a value that does not exist at path: ${formatPath(t)}`);return applyModify(e,t,r,!1)}function applyRemove(e,t){const r=getTree(e);if(void 0===(0,i.findNodeAtLocation)(r,t))throw new Error(`Cannot remove a value that does not exist at path: ${formatPath(t)}`);return applyModify(e,t,void 0,!1)}function applyModify(e,t,r,o){const n=(0,i.modify)(e,t,r,{formattingOptions:s,isArrayInsertion:o});if(0===n.length)throw new Error(`No JSONC edit was generated for path: ${formatPath(t)}`);return(0,i.applyEdits)(e,n)}function getTree(e){const t=[],r=(0,i.parseTree)(e,t,{allowTrailingComma:!0,disallowComments:!1});if(void 0===r||t.length>0)throw new Error(`The source text is not valid JSON or JSONC${function(e){if(0===e.length)return"";return` (parse errors: ${e.map(e=>`code=${e.error}, offset=${e.offset}`).join("; ")})`}(t)}.`);return r}function formatPath(e){return`[${e.map(e=>JSON.stringify(e)).join(", ")}]`}function safeStringify(e){try{return JSON.stringify(e)}catch{return"[unserializable patch operation]"}}function getErrorMessage(e){return e instanceof Error?e.message:String(e)}function createError(e,t){const r=new Error(e);try{Object.defineProperty(r,"cause",{configurable:!0,enumerable:!1,value:t,writable:!0})}catch{}return r}var c=require("yaml");function applyYamlOperation(e,t){!function(e){if(!Array.isArray(e.path)||0===e.path.length)throw new Error("[confedit] Patch path must be a non-empty array.");if("add"!==e.op&&"replace"!==e.op&&"remove"!==e.op)throw new Error(`[confedit] Unsupported patch operation: ${String(e.op)}.`)}(t);const r=t.path,o=function(e,t){if(0===t.length)throw new Error("[confedit] Replacing the YAML document root is not supported.");if(1===t.length){if(null===e.contents)throw new Error("[confedit] Cannot patch an empty YAML document without a root container.");return e.contents}const r=t.slice(0,-1),o=e.getIn(r,!0);if(null==o)throw new Error(`[confedit] Missing parent path: ${formatPath2(r)}.`);if(!(0,c.isMap)(o)&&!(0,c.isSeq)(o))throw new Error(`[confedit] Parent at ${formatPath2(r)} is not a YAML map or sequence.`);return o}(e,r),n=r[r.length-1];if((0,c.isMap)(o))!function(e,t,r,o){if("string"!=typeof r)throw new Error(`[confedit] YAML map keys must be strings at ${formatPath2(o.path)}.`);const n=t.has(r);switch(o.op){case"add":return void t.set(r,e.createNode(o.value));case"replace":if(!n)throw new Error(`[confedit] Cannot replace missing value at ${formatPath2(o.path)}.`);return void t.set(r,e.createNode(o.value));case"remove":if(!n)throw new Error(`[confedit] Cannot remove missing value at ${formatPath2(o.path)}.`);return void t.delete(r);default:throw new Error(`[confedit] Unsupported patch operation: ${String(o.op)}.`)}}(e,o,n,t);else{if(!(0,c.isSeq)(o))throw new Error(`[confedit] Cannot apply patch at ${formatPath2(r)} because its parent is not a YAML map or sequence.`);!function(e,t,r,o){if("number"!=typeof r||!Number.isSafeInteger(r)||r<0)throw new Error(`[confedit] YAML sequence indexes must be non-negative integers at ${formatPath2(o.path)}.`);const n=t.items.length;switch(o.op){case"add":if(r>n)throw new Error(`[confedit] Cannot insert at index ${r}; sequence length is ${n}.`);return void t.items.splice(r,0,e.createNode(o.value));case"replace":if(r>=n)throw new Error(`[confedit] Cannot replace index ${r}; sequence length is ${n}.`);return void(t.items[r]=e.createNode(o.value));case"remove":if(r>=n)throw new Error(`[confedit] Cannot remove index ${r}; sequence length is ${n}.`);return void t.items.splice(r,1);default:throw new Error(`[confedit] Unsupported patch operation: ${String(o.op)}.`)}}(e,o,n,t)}}function formatPath2(e){return`[${e.map(e=>JSON.stringify(e)).join(", ")}]`}function assertNonEmptyString(e,t){if("string"!=typeof e||0===e.length)throw new TypeError(`[confedit] ${t} must be a non-empty string.`)}function assertPatchPath(e,t="path"){if(!Array.isArray(e)||0===e.length)throw new TypeError(`[confedit] ${t} must be a non-empty array.`);for(const[r,o]of e.entries())if("string"!=typeof o){if("number"!=typeof o||!Number.isSafeInteger(o)||o<0)throw new TypeError(`[confedit] ${t}[${r}] must be a non-empty string or a non-negative integer.`)}else if(0===o.length)throw new TypeError(`[confedit] ${t}[${r}] must not be an empty string.`)}function assertPatchOperations(e){if(!Array.isArray(e))throw new TypeError("[confedit] ops must be an array.");for(const[t,r]of e.entries()){if(null===r||"object"!=typeof r)throw new TypeError(`[confedit] Patch operation at index ${t} must be an object.`);if("add"!==r.op&&"replace"!==r.op&&"remove"!==r.op)throw new Error(`[confedit] Unsupported patch operation at index ${t}: ${String(r.op)}.`);if(assertPatchPath(r.path,`ops[${t}].path`),("add"===r.op||"replace"===r.op)&&!Object.prototype.hasOwnProperty.call(r,"value"))throw new TypeError(`[confedit] Patch operation at index ${t} requires a value.`)}}function assertConfigFormat(e){if("json"!==e&&"jsonc"!==e&&"yaml"!==e)throw new Error(`[confedit] Unsupported configuration format: ${String(e)}.`)}function getErrorMessage2(e){return e instanceof Error?e.message:String(e)}function createError2(e,t){const r=new Error(e);try{Object.defineProperty(r,"cause",{configurable:!0,enumerable:!1,value:t,writable:!0})}catch{}return r}function patchContent(e,t,r,o={}){if("string"!=typeof e)throw new TypeError("[confedit] content must be a string.");if(assertPatchOperations(t),assertConfigFormat(r),0===t.length)return e;const n=o.strict??!0;switch(r){case"json":case"jsonc":return function(e,t,r=!0){if("string"!=typeof e)throw new TypeError("[confedit] sourceText must be a string.");if(!Array.isArray(t))throw new TypeError("[confedit] ops must be an array.");let o=e;for(const e of t)try{validateOperation(e);const t=normalizePath(e.path);switch(e.op){case"add":o=applyAdd(o,t,e.value);break;case"replace":o=applyReplace(o,t,e.value);break;case"remove":o=applyRemove(o,t);break;default:throw new Error(`[confedit] Unsupported JSON patch operation: ${String(e.op)}`)}}catch(t){const o=`[confedit] Failed to apply JSON patch ${safeStringify(e)}: ${getErrorMessage(t)}`;if(r)throw createError(o,t);console.warn(o)}return o}(e,t,n);case"yaml":return function(e,t,r=!0){const o=(0,c.parseDocument)(e,{prettyErrors:!0,strict:!0});if(o.errors.length>0)throw new Error(`[confedit] Invalid YAML source: ${o.errors.map(e=>e.message).join("; ")}`);for(const e of t)try{applyYamlOperation(o,e)}catch(t){if(r)throw t;console.warn(`[confedit yaml patch warn] Skip operation ${JSON.stringify(e)}`,t)}return o.toString()}(e,t,n)}}function setContentValue(e,t,r,o){return assertPatchPath(t),patchContent(e,[{op:"add",path:[...t],value:r}],o)}function deleteContentValue(e,t,r){return assertPatchPath(t),patchContent(e,[{op:"remove",path:[...t]}],r)}var l=require("fs/promises"),u=require("path"),f=require("url");function normalizeFilePath(e){if("string"!=typeof e||0===e.length)throw new TypeError("[confedit] filePath must be a non-empty string.");if(e.startsWith("file:"))try{return(0,f.fileURLToPath)(e)}catch(t){throw function(e,t){const r=new Error(e);try{Object.defineProperty(r,"cause",{configurable:!0,enumerable:!1,value:t,writable:!0})}catch{}return r}(`[confedit] Invalid file URL "${e}": ${function(e){return e instanceof Error?e.message:String(e)}(t)}`,t)}return(0,u.isAbsolute)(e)?e:(0,u.resolve)(e)}async function readConfigFile(e){assertNonEmptyString(e,"filePath");const t=normalizeFilePath(e);try{return await(0,l.readFile)(t,"utf8")}catch(t){throw createError2(`[confedit] Failed to read "${e}": ${getErrorMessage2(t)}`,t)}}var p=require("fs/promises"),h=require("path"),d=require("crypto"),m=require("fs/promises"),w=require("path");async function atomicWrite(e,t){if(function(e,t){if("string"!=typeof e||0===e.length)throw new TypeError(`[confedit] ${t} must be a non-empty string.`)}(e,"filePath"),"string"!=typeof t)throw new TypeError("[confedit] content must be a string.");const r=normalizeFilePath(e),o=(0,w.dirname)(r),n=(0,w.basename)(r),a=`${process.pid}.${Date.now()}.${(0,d.randomBytes)(12).toString("hex")}`,i=(0,w.join)(o,`.${n}.${a}.tmp`),s=(0,w.join)(o,`.${n}.${a}.bak`);let c,l=!1,u=!1;try{await(0,m.mkdir)(o,{recursive:!0});const e=await async function(e){try{return 511&(await(0,m.stat)(e)).mode}catch(e){if("ENOENT"===getErrorCode(e))return;throw e}}(r);c=await(0,m.open)(i,"wx",e??384);try{await c.writeFile(t,"utf8"),await c.sync()}finally{await c.close(),c=void 0}void 0!==e&&await(0,m.chmod)(i,e);try{await(0,m.rename)(i,r),u=!0}catch(e){if(!function(e){if("win32"!==process.platform)return!1;const t=getErrorCode(e);return"EEXIST"===t||"EPERM"===t||"EACCES"===t}(e))throw e;await(0,m.rename)(r,s),l=!0;try{await(0,m.rename)(i,r),u=!0}catch(e){const t=await async function(e,t){try{return await(0,m.unlink)(e).catch(e=>{if("ENOENT"!==getErrorCode(e))throw e}),await(0,m.rename)(t,e),!0}catch{return!1}}(r,s);throw t&&(l=!1),createError4(t?`[confedit] Failed to replace "${r}", but the original file was restored.`:`[confedit] Failed to replace "${r}". The backup was retained at "${s}".`,e)}}await syncDirectory(o),l&&(await(0,m.unlink)(s),l=!1,await syncDirectory(o))}catch(t){throw void 0!==c&&await c.close().catch(()=>{}),createError4(`[confedit] Failed to atomically write "${e}": ${function(e){return e instanceof Error?e.message:String(e)}(t)}`,t)}finally{await(0,m.unlink)(i).catch(()=>{}),u&&l&&await(0,m.unlink)(s).catch(()=>{})}}async function syncDirectory(e){let t;try{t=await(0,m.open)(e,"r"),await t.sync()}catch(e){const t=getErrorCode(e);if("EINVAL"!==t&&"EPERM"!==t&&"EISDIR"!==t&&"ENOSYS"!==t&&"ENOTSUP"!==t)throw e}finally{await(t?.close().catch(()=>{}))}}function getErrorCode(e){if(null!==e&&"object"==typeof e&&"code"in e&&"string"==typeof e.code)return e.code}function createError4(e,t){const r=new Error(e);try{Object.defineProperty(r,"cause",{configurable:!0,enumerable:!1,value:t,writable:!0})}catch{}return r}var y=require("crypto"),g=require("fs/promises"),E=require("os"),b=1e4,v=25,P=1e3,O=6e4,$=new Map,M=new Set;async function withFileLock(e,t,r={}){const o=normalizeFilePath(e);return function(e){if(void 0!==e.timeoutMs&&(!Number.isFinite(e.timeoutMs)||e.timeoutMs<0))throw new TypeError("[confedit] lock timeoutMs must be a non-negative finite number.");if(void 0!==e.retryDelayMs&&(!Number.isFinite(e.retryDelayMs)||e.retryDelayMs<=0))throw new TypeError("[confedit] lock retryDelayMs must be a positive finite number.");if(void 0!==e.staleThresholdMs&&(!Number.isFinite(e.staleThresholdMs)||e.staleThresholdMs<=0))throw new TypeError("[confedit] lock staleThresholdMs must be a positive finite number.")}(r),function(e,t){const r=$.get(e)??Promise.resolve();let o;const n=new Promise(e=>{o=e}),a=r.catch(()=>{}).then(()=>n);return $.set(e,a),r.catch(()=>{}).then(t).finally(()=>{o(),$.get(e)===a&&$.delete(e)})}(o,async()=>{const e=await async function(e,t){const r=t.timeoutMs??b,o=t.retryDelayMs??v,n=t.staleThresholdMs??Math.max(2*r,O),a=t.allowStaleRecovery??!1,i=`${e}.confedit.lock`,s=Date.now();let c=o;const l=createToken(),u={version:1,pid:process.pid,hostname:(0,E.hostname)(),createdAt:(new Date).toISOString(),token:l};for(;;)try{const e=await(0,g.open)(i,"wx",384);try{await(0,g.writeFile)(e,`${JSON.stringify(u)}\n`,"utf8"),await e.sync()}finally{await e.close()}return M.add(i),createReleaseHandler(i,l)}catch(t){if("EEXIST"!==getErrorCode2(t))throw createError5(`[confedit] Failed to acquire lock "${i}": ${getErrorMessage5(t)}`,t);if(a&&await tryRecoverStaleLock(i,n),Date.now()-s>=r)throw new Error(`[confedit] Timed out waiting for lock on "${e}" after ${r}ms.`);await sleep(c),c=Math.min(Math.ceil(1.5*c),P)}}(o,r);try{return await t()}finally{await e()}})}async function releaseAllLocalLocks(){const e=Array.from(M).map(e=>(0,g.unlink)(e).catch(()=>{}));await Promise.allSettled(e),M.clear()}function createReleaseHandler(e,t){return async()=>{try{const r=await readLockData(e);r?.token===t&&(await(0,g.unlink)(e).catch(()=>{}),M.delete(e))}catch{}}}async function tryRecoverStaleLock(e,t){const r=await readLockData(e);if(void 0===r||!isExpiredLock(r,t))return;const o=`${e}.stale.${createToken()}`;try{await(0,g.rename)(e,o)}catch(e){return void getErrorCode2(e)}try{const n=await readLockData(o);if(n?.token===r.token&&isExpiredLock(n,t))return void await(0,g.unlink)(o).catch(()=>{});await(0,g.rename)(o,e).catch(()=>{})}catch{}}function isExpiredLock(e,t){const r=Date.parse(e.createdAt);return Number.isFinite(r)&&Date.now()-r>t}async function readLockData(e){try{return function(e){try{const t=JSON.parse(e);if(1!==t.version||"number"!=typeof t.pid||!Number.isSafeInteger(t.pid)||t.pid<=0||"string"!=typeof t.hostname||"string"!=typeof t.createdAt||!Number.isFinite(Date.parse(t.createdAt))||"string"!=typeof t.token||t.token.length<16)return;return{version:1,pid:t.pid,hostname:t.hostname,createdAt:t.createdAt,token:t.token}}catch{return}}(await(0,g.readFile)(e,"utf8"))}catch{return}}function createToken(){return`${process.pid}-${Date.now()}-${(0,y.randomBytes)(16).toString("hex")}`}function sleep(e){return new Promise(t=>setTimeout(t,e))}function getErrorCode2(e){if(null!==e&&"object"==typeof e&&"code"in e&&"string"==typeof e.code)return e.code}function getErrorMessage5(e){return e instanceof Error?e.message:String(e)}function createError5(e,t){const r=new Error(e);try{Object.defineProperty(r,"cause",{configurable:!0,enumerable:!1,value:t,writable:!0})}catch{}return r}async function writeConfigFile(e,t,r={}){if(assertNonEmptyString(e,"filePath"),"string"!=typeof t)throw new TypeError("[confedit] content must be a string.");const o=normalizeFilePath(e),writeTransaction=async()=>{try{await atomicWrite(o,t)}catch(t){throw createError2(`[confedit] Failed to write "${e}": ${getErrorMessage2(t)}`,t)}};r.lock??!0?(await(0,p.mkdir)((0,h.dirname)(o),{recursive:!0}),await withFileLock(o,writeTransaction,{timeoutMs:r.lockTimeoutMs,retryDelayMs:r.lockRetryDelayMs,staleThresholdMs:r.lockStaleThresholdMs,allowStaleRecovery:r.allowStaleRecovery})):await writeTransaction()}var T=require("path");function detectFormat(e){const t=normalizeFilePath(e);switch((0,T.extname)(t).toLowerCase()){case".json":return"json";case".jsonc":return"jsonc";case".yaml":case".yml":return"yaml";default:throw new Error(`[confedit] Cannot detect a supported configuration format for "${e}". Expected .json, .jsonc, .yaml, or .yml.`)}}async function patchConfigFile(e,t,r={}){if("string"!=typeof e||0===e.length)throw new TypeError("[confedit] filePath must be a non-empty string.");if(assertPatchOperations(t),0===t.length)return;const o=normalizeFilePath(e),n=r.format??detectFormat(o);assertConfigFormat(n),function(e){if(void 0!==e.lockTimeoutMs&&(!Number.isFinite(e.lockTimeoutMs)||e.lockTimeoutMs<0))throw new TypeError("[confedit] lockTimeoutMs must be a non-negative finite number.");if(void 0!==e.lockRetryDelayMs&&(!Number.isFinite(e.lockRetryDelayMs)||e.lockRetryDelayMs<=0))throw new TypeError("[confedit] lockRetryDelayMs must be a positive finite number.");if(void 0!==e.lockStaleThresholdMs&&(!Number.isFinite(e.lockStaleThresholdMs)||e.lockStaleThresholdMs<=0))throw new TypeError("[confedit] lockStaleThresholdMs must be a positive finite number.")}(r);const a=r.strict??!0,patchTransaction=async()=>{const r=await readConfigFile(o),i=patchContent(r,t,n,{strict:a});if(i!==r)try{await atomicWrite(o,i)}catch(t){throw createError2(`[confedit] Failed to write "${e}": ${getErrorMessage2(t)}`,t)}};r.lock??!0?await withFileLock(o,patchTransaction,{timeoutMs:r.lockTimeoutMs,retryDelayMs:r.lockRetryDelayMs,staleThresholdMs:r.lockStaleThresholdMs,allowStaleRecovery:r.allowStaleRecovery}):await patchTransaction()}async function setConfigValue(e,t,r,o={}){await patchConfigFile(e,[{op:"add",path:[...t],value:r}],o)}async function deleteConfigValue(e,t,r={}){await patchConfigFile(e,[{op:"remove",path:[...t]}],r)}var D,N=require("fs/promises"),S=require("path"),A=require("yaml"),I=class extends Error{constructor(e,t,r){super(t),this.name="OpenApiValidationError",this.code=e,this.cause=r}},F={inputKind:"content",timeoutMs:15e3,maxInputBytes:5242880,maxDocumentNodes:1e5,maxDocumentDepth:100,maxValidationErrors:50,maxErrorMessageLength:1e3};async function validateOpenAPISpec(e,t={}){!function(e,t){if("string"!=typeof e||0===e.length)throw new TypeError(`[confedit] ${t} must be a non-empty string.`)}(e,"input");const r=function(e){const t={inputKind:e.inputKind??F.inputKind,baseFilePath:e.baseFilePath,allowedRootDirectory:e.allowedRootDirectory,timeoutMs:e.timeoutMs??F.timeoutMs,maxInputBytes:e.maxInputBytes??F.maxInputBytes,maxDocumentNodes:e.maxDocumentNodes??F.maxDocumentNodes,maxDocumentDepth:e.maxDocumentDepth??F.maxDocumentDepth,maxValidationErrors:e.maxValidationErrors??F.maxValidationErrors,maxErrorMessageLength:e.maxErrorMessageLength??F.maxErrorMessageLength,signal:e.signal};if("content"!==t.inputKind&&"file"!==t.inputKind)throw new I("INVALID_OPTION",`inputKind must be "content" or "file", got: ${String(t.inputKind)}.`);return assertPositiveFiniteNumber(t.timeoutMs,"timeoutMs"),assertPositiveFiniteNumber(t.maxInputBytes,"maxInputBytes"),assertPositiveFiniteNumber(t.maxDocumentNodes,"maxDocumentNodes"),assertPositiveFiniteNumber(t.maxDocumentDepth,"maxDocumentDepth"),assertPositiveFiniteNumber(t.maxValidationErrors,"maxValidationErrors"),assertPositiveFiniteNumber(t.maxErrorMessageLength,"maxErrorMessageLength"),t}(t),o=function(e){const t=new AbortController;if(e){if(!e.aborted){const onAbort=()=>t.abort();return e.addEventListener("abort",onAbort,{once:!0}),{controller:t,dispose:()=>e.removeEventListener("abort",onAbort)}}t.abort()}return{controller:t,dispose:()=>{}}}(r.signal),n={options:r,controller:o.controller,deadline:Date.now()+r.timeoutMs};try{return await runWithDeadline(n,async()=>{if("file"===r.inputKind){const t=await async function(e,t){const r=normalizeFilePath(e);if(!t.options.allowedRootDirectory)throw new I("FILE_INPUT_FORBIDDEN","File input requires allowedRootDirectory.");return async function(e,t){const r=(0,S.isAbsolute)(e)?e:(0,S.resolve)(t,e),o=await(0,N.realpath)(r),n=(0,S.relative)(t,o);if(n.startsWith("..")||""===n||"\\"===S.sep&&/^[a-zA-Z]:/.test(n))throw new I("PATH_OUTSIDE_ROOT",`File path is outside the allowed root directory: ${e}.`);return o}(r,await async function(e){if(e.rootDirectory)return e.rootDirectory;const t=e.options.allowedRootDirectory;if(!t)throw new I("INVALID_OPTION","allowedRootDirectory is required for file operations.");const r=(0,S.resolve)(t),o=await(0,N.realpath)(r);if(!(await(0,N.stat)(o)).isDirectory())throw new I("INVALID_OPTION",`allowedRootDirectory is not a directory: ${t}.`);return e.rootDirectory=o,o}(t))}(e,n),o=await async function(e,t,r,o){throwIfCancelled(r);const n=await(0,N.open)(e,"r");try{const r=await n.stat();if(r.size>t)throw new I(o,`File exceeds maximum allowed size of ${t} bytes: ${e}.`);const a=Buffer.alloc(r.size);return await n.read(a,0,r.size,0),a.toString("utf8")}finally{await n.close()}}(t,r.maxInputBytes,n,"INPUT_FILE_TOO_LARGE");return validateRawContent(o,{...n,options:{...r,baseFilePath:t}})}return assertByteLength(e,r.maxInputBytes,"INPUT_TOO_LARGE"),validateRawContent(e,n)})}catch(e){throw function(e){if(e instanceof I)return e;return new I("UNKNOWN_ERROR",`OpenAPI validation failed: ${getErrorMessage6(e)}.`,e)}(e)}finally{o.dispose(),n.controller.abort()}}async function validateOpenAPIFile(e,t={}){return validateOpenAPISpec(e,{...t,inputKind:"file",baseFilePath:e})}async function validateRawContent(e,t){throwIfCancelled(t),assertByteLength(e,t.options.maxInputBytes,"INPUT_TOO_LARGE");const r=function(e){try{const t=(0,A.parse)(e);if(null===t||"object"!=typeof t||Array.isArray(t))throw new I("INVALID_DOCUMENT_SHAPE","OpenAPI document must be a JSON object.");return t}catch(e){if(e instanceof I)throw e;throw new I("PARSE_ERROR",`Failed to parse OpenAPI document: ${getErrorMessage6(e)}.`,e)}}(e);!function(e,t){let r=0;const o=[{value:e,depth:0}];for(;o.length>0;){const{value:e,depth:n}=o.pop();if(n>t.maxDocumentDepth)throw new I("DOCUMENT_TOO_DEEP",`Document exceeds maximum nesting depth of ${t.maxDocumentDepth}.`);if(r++,r>t.maxDocumentNodes)throw new I("DOCUMENT_TOO_LARGE",`Document exceeds maximum node count of ${t.maxDocumentNodes}.`);if(Array.isArray(e))for(const t of e)o.push({value:t,depth:n+1});else if(null!==e&&"object"==typeof e)for(const t of Object.values(e))o.push({value:t,depth:n+1})}}(r,t.options),function(e){const t=e.openapi??e.swagger;if("string"!=typeof t||0===t.length)throw new I("UNSUPPORTED_VERSION","Document must declare an openapi or swagger version.");const r=Number.parseInt(t.split(".")[0]??"",10);if(!Number.isFinite(r)||2!==r&&3!==r)throw new I("UNSUPPORTED_VERSION",`Unsupported OpenAPI/Swagger version: ${t}. Supported: 2.x, 3.x.`)}(r);const o=await runWithDeadline(t,()=>async function(){return D??=import("@powerduck/openapi-parser").then(({validate:e})=>e),D}()),n=await runWithDeadline(t,()=>o(r,{throwOnError:!1}));if(!n.valid)throw new I("SPEC_VALIDATION_FAILED",`OpenAPI validation failed: ${function(e,t,r){const o=e.slice(0,t).map(e=>{const t=e.instancePath??"",o=e.message??"Unknown error",n=t?`${t}: ${o}`:o;return n.length>r?`${n.slice(0,r)}...`:n});e.length>t&&o.push(`... and ${e.length-t} more errors`);return o.join("; ")}(n.errors??[],t.options.maxValidationErrors,t.options.maxErrorMessageLength)}`);return n.specification}function assertPositiveFiniteNumber(e,t){if(!Number.isFinite(e)||e<=0)throw new I("INVALID_OPTION",`${t} must be a positive finite number, got: ${String(e)}.`)}async function runWithDeadline(e,t){throwIfCancelled(e);const r=await Promise.race([t(),createDeadlinePromise(e)]);return throwIfCancelled(e),r}function createDeadlinePromise(e){return new Promise((t,r)=>{const o=e.deadline-Date.now(),n=Math.max(0,Math.min(o,2147483647)),a=setTimeout(()=>{r(new I("OPERATION_TIMEOUT",`OpenAPI validation operation timed out after ${e.options.timeoutMs}ms.`))},n);e.controller.signal.addEventListener("abort",()=>{clearTimeout(a),r(new I("OPERATION_ABORTED","OpenAPI validation operation was aborted."))},{once:!0})})}function throwIfCancelled(e){if(e.controller.signal.aborted)throw new I("OPERATION_ABORTED","OpenAPI validation operation was aborted.");if(Date.now()>e.deadline)throw new I("OPERATION_TIMEOUT",`OpenAPI validation operation timed out after ${e.options.timeoutMs}ms.`)}function assertByteLength(e,t,r){const o=Buffer.byteLength(e,"utf8");if(o>t)throw new I(r,`Input exceeds maximum allowed size of ${t} bytes (actual: ${o} bytes).`)}function getErrorMessage6(e){return e instanceof Error?e.message:String(e)}
|
|
1
|
+
Object.create;var e,t=Object.defineProperty,r=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,n=(Object.getPrototypeOf,Object.prototype.hasOwnProperty),__copyProps=(e,a,i,s)=>{if(a&&"object"==typeof a||"function"==typeof a)for(let c of o(a))n.call(e,c)||c===i||t(e,c,{get:()=>a[c],enumerable:!(s=r(a,c))||s.enumerable});return e},a={};((e,r)=>{for(var o in r)t(e,o,{get:r[o],enumerable:!0})})(a,{OpenApiValidationError:()=>I,assertConfigFormat:()=>assertConfigFormat,assertNonEmptyString:()=>assertNonEmptyString,assertPatchOperations:()=>assertPatchOperations,assertPatchPath:()=>assertPatchPath,createError:()=>createError2,deleteConfigValue:()=>deleteConfigValue,deleteContentValue:()=>deleteContentValue,detectFormat:()=>detectFormat,getErrorMessage:()=>getErrorMessage2,normalizeFilePath:()=>normalizeFilePath,patchConfigFile:()=>patchConfigFile,patchContent:()=>patchContent,readConfigFile:()=>readConfigFile,releaseAllLocalLocks:()=>releaseAllLocalLocks,setConfigValue:()=>setConfigValue,setContentValue:()=>setContentValue,validateOpenAPIFile:()=>validateOpenAPIFile,validateOpenAPISpec:()=>validateOpenAPISpec,withFileLock:()=>withFileLock,writeConfigFile:()=>writeConfigFile}),module.exports=(e=a,__copyProps(t({},"__esModule",{value:!0}),e));var i=require("jsonc-parser"),s={insertSpaces:!0,tabSize:2,eol:"\n"};function validateOperation(e){if(null===e||"object"!=typeof e)throw new TypeError("Patch operation must be an object.");if(!Array.isArray(e.path)||0===e.path.length)throw new Error("Patch operation path must be a non-empty array.");if("add"!==e.op&&"replace"!==e.op&&"remove"!==e.op)throw new Error(`Unsupported JSON patch operation: ${String(e.op)}`);if(("add"===e.op||"replace"===e.op)&&!Object.prototype.hasOwnProperty.call(e,"value"))throw new Error(`Patch operation "${e.op}" requires a value.`)}function normalizePath(e){return e.map(e=>{if("number"==typeof e){if(!Number.isSafeInteger(e)||e<0)throw new Error(`Invalid array index: ${e}`);return e}if("string"!=typeof e)throw new TypeError(`Patch path segments must be strings or numbers; received ${typeof e}.`);return e})}function applyAdd(e,t,r){const o=function(e,t){if(1===t.length){if("object"!==e.type&&"array"!==e.type)throw new Error("Cannot add a root child to a scalar JSON value.");return e}const r=t.slice(0,-1),o=(0,i.findNodeAtLocation)(e,r);if(void 0===o)throw new Error(`Cannot add value because its parent does not exist at path: ${formatPath(r)}`);if("object"!==o.type&&"array"!==o.type)throw new Error(`Cannot add value because its parent is not an object or array at path: ${formatPath(r)}`);return o}(getTree(e),t),n=t[t.length-1];if("array"===o.type){const a=function(e,t){if("number"!=typeof e)throw new Error(`Array index must be a number at path: ${formatPath(t)}`);if(!Number.isSafeInteger(e)||e<0)throw new Error(`Invalid array index ${e} at path: ${formatPath(t)}`);return e}(n,t),i=o.children?.length??0;if(a>i)throw new Error(`Cannot add at array index ${a}; array length is ${i} at path: ${formatPath(t.slice(0,-1))}`);return applyModify(e,t,r,!0)}if("object"===o.type){if("string"!=typeof n)throw new Error(`Object property path segment must be a string at path: ${formatPath(t)}`);return applyModify(e,t,r,!1)}throw new Error(`Cannot add a child to non-container value at path: ${formatPath(t.slice(0,-1))}`)}function applyReplace(e,t,r){const o=getTree(e);if(void 0===(0,i.findNodeAtLocation)(o,t))throw new Error(`Cannot replace a value that does not exist at path: ${formatPath(t)}`);return applyModify(e,t,r,!1)}function applyRemove(e,t){const r=getTree(e);if(void 0===(0,i.findNodeAtLocation)(r,t))throw new Error(`Cannot remove a value that does not exist at path: ${formatPath(t)}`);return applyModify(e,t,void 0,!1)}function applyModify(e,t,r,o){const n=(0,i.modify)(e,t,r,{formattingOptions:s,isArrayInsertion:o});if(0===n.length)throw new Error(`No JSONC edit was generated for path: ${formatPath(t)}`);return(0,i.applyEdits)(e,n)}function getTree(e){const t=[],r=(0,i.parseTree)(e,t,{allowTrailingComma:!0,disallowComments:!1});if(void 0===r||t.length>0)throw new Error(`The source text is not valid JSON or JSONC${function(e){if(0===e.length)return"";return` (parse errors: ${e.map(e=>`code=${e.error}, offset=${e.offset}`).join("; ")})`}(t)}.`);return r}function formatPath(e){return`[${e.map(e=>JSON.stringify(e)).join(", ")}]`}function safeStringify(e){try{return JSON.stringify(e)}catch{return"[unserializable patch operation]"}}function getErrorMessage(e){return e instanceof Error?e.message:String(e)}function createError(e,t){const r=new Error(e);try{Object.defineProperty(r,"cause",{configurable:!0,enumerable:!1,value:t,writable:!0})}catch{}return r}var c=require("yaml");function applyYamlOperation(e,t){!function(e){if(!Array.isArray(e.path)||0===e.path.length)throw new Error("[confedit] Patch path must be a non-empty array.");if("add"!==e.op&&"replace"!==e.op&&"remove"!==e.op)throw new Error(`[confedit] Unsupported patch operation: ${String(e.op)}.`)}(t);const r=t.path,o=function(e,t){if(0===t.length)throw new Error("[confedit] Replacing the YAML document root is not supported.");if(1===t.length){if(null===e.contents)throw new Error("[confedit] Cannot patch an empty YAML document without a root container.");return e.contents}const r=t.slice(0,-1),o=e.getIn(r,!0);if(null==o)throw new Error(`[confedit] Missing parent path: ${formatPath2(r)}.`);if(!(0,c.isMap)(o)&&!(0,c.isSeq)(o))throw new Error(`[confedit] Parent at ${formatPath2(r)} is not a YAML map or sequence.`);return o}(e,r),n=r[r.length-1];if((0,c.isMap)(o))!function(e,t,r,o){if("string"!=typeof r)throw new Error(`[confedit] YAML map keys must be strings at ${formatPath2(o.path)}.`);const n=t.has(r);switch(o.op){case"add":return void t.set(r,e.createNode(o.value));case"replace":if(!n)throw new Error(`[confedit] Cannot replace missing value at ${formatPath2(o.path)}.`);return void t.set(r,e.createNode(o.value));case"remove":if(!n)throw new Error(`[confedit] Cannot remove missing value at ${formatPath2(o.path)}.`);return void t.delete(r);default:throw new Error(`[confedit] Unsupported patch operation: ${String(o.op)}.`)}}(e,o,n,t);else{if(!(0,c.isSeq)(o))throw new Error(`[confedit] Cannot apply patch at ${formatPath2(r)} because its parent is not a YAML map or sequence.`);!function(e,t,r,o){if("number"!=typeof r||!Number.isSafeInteger(r)||r<0)throw new Error(`[confedit] YAML sequence indexes must be non-negative integers at ${formatPath2(o.path)}.`);const n=t.items.length;switch(o.op){case"add":if(r>n)throw new Error(`[confedit] Cannot insert at index ${r}; sequence length is ${n}.`);return void t.items.splice(r,0,e.createNode(o.value));case"replace":if(r>=n)throw new Error(`[confedit] Cannot replace index ${r}; sequence length is ${n}.`);return void(t.items[r]=e.createNode(o.value));case"remove":if(r>=n)throw new Error(`[confedit] Cannot remove index ${r}; sequence length is ${n}.`);return void t.items.splice(r,1);default:throw new Error(`[confedit] Unsupported patch operation: ${String(o.op)}.`)}}(e,o,n,t)}}function formatPath2(e){return`[${e.map(e=>JSON.stringify(e)).join(", ")}]`}function assertNonEmptyString(e,t){if("string"!=typeof e||0===e.length)throw new TypeError(`[confedit] ${t} must be a non-empty string.`)}function assertPatchPath(e,t="path"){if(!Array.isArray(e)||0===e.length)throw new TypeError(`[confedit] ${t} must be a non-empty array.`);for(const[r,o]of e.entries())if("string"!=typeof o){if("number"!=typeof o||!Number.isSafeInteger(o)||o<0)throw new TypeError(`[confedit] ${t}[${r}] must be a non-empty string or a non-negative integer.`)}else if(0===o.length)throw new TypeError(`[confedit] ${t}[${r}] must not be an empty string.`)}function assertPatchOperations(e){if(!Array.isArray(e))throw new TypeError("[confedit] ops must be an array.");for(const[t,r]of e.entries()){if(null===r||"object"!=typeof r)throw new TypeError(`[confedit] Patch operation at index ${t} must be an object.`);if("add"!==r.op&&"replace"!==r.op&&"remove"!==r.op)throw new Error(`[confedit] Unsupported patch operation at index ${t}: ${String(r.op)}.`);if(assertPatchPath(r.path,`ops[${t}].path`),("add"===r.op||"replace"===r.op)&&!Object.prototype.hasOwnProperty.call(r,"value"))throw new TypeError(`[confedit] Patch operation at index ${t} requires a value.`)}}function assertConfigFormat(e){if("json"!==e&&"jsonc"!==e&&"yaml"!==e)throw new Error(`[confedit] Unsupported configuration format: ${String(e)}.`)}function getErrorMessage2(e){return e instanceof Error?e.message:String(e)}function createError2(e,t){const r=new Error(e);try{Object.defineProperty(r,"cause",{configurable:!0,enumerable:!1,value:t,writable:!0})}catch{}return r}function patchContent(e,t,r,o={}){if("string"!=typeof e)throw new TypeError("[confedit] content must be a string.");if(assertPatchOperations(t),assertConfigFormat(r),0===t.length)return e;const n=o.strict??!0;switch(r){case"json":case"jsonc":return function(e,t,r=!0){if("string"!=typeof e)throw new TypeError("[confedit] sourceText must be a string.");if(!Array.isArray(t))throw new TypeError("[confedit] ops must be an array.");let o=e;for(const e of t)try{validateOperation(e);const t=normalizePath(e.path);switch(e.op){case"add":o=applyAdd(o,t,e.value);break;case"replace":o=applyReplace(o,t,e.value);break;case"remove":o=applyRemove(o,t);break;default:throw new Error(`[confedit] Unsupported JSON patch operation: ${String(e.op)}`)}}catch(t){const o=`[confedit] Failed to apply JSON patch ${safeStringify(e)}: ${getErrorMessage(t)}`;if(r)throw createError(o,t);console.warn(o)}return o}(e,t,n);case"yaml":return function(e,t,r=!0){const o=(0,c.parseDocument)(e,{prettyErrors:!0,strict:!0});if(o.errors.length>0)throw new Error(`[confedit] Invalid YAML source: ${o.errors.map(e=>e.message).join("; ")}`);for(const e of t)try{applyYamlOperation(o,e)}catch(t){if(r)throw t;console.warn(`[confedit yaml patch warn] Skip operation ${JSON.stringify(e)}`,t)}return o.toString()}(e,t,n)}}function setContentValue(e,t,r,o){return assertPatchPath(t),patchContent(e,[{op:"add",path:[...t],value:r}],o)}function deleteContentValue(e,t,r){return assertPatchPath(t),patchContent(e,[{op:"remove",path:[...t]}],r)}var l=require("fs/promises"),u=require("path"),f=require("url");function normalizeFilePath(e){if("string"!=typeof e||0===e.length)throw new TypeError("[confedit] filePath must be a non-empty string.");if(e.startsWith("file:"))try{return(0,f.fileURLToPath)(e)}catch(t){throw function(e,t){const r=new Error(e);try{Object.defineProperty(r,"cause",{configurable:!0,enumerable:!1,value:t,writable:!0})}catch{}return r}(`[confedit] Invalid file URL "${e}": ${function(e){return e instanceof Error?e.message:String(e)}(t)}`,t)}return(0,u.isAbsolute)(e)?e:(0,u.resolve)(e)}async function readConfigFile(e){assertNonEmptyString(e,"filePath");const t=normalizeFilePath(e);try{return await(0,l.readFile)(t,"utf8")}catch(t){throw createError2(`[confedit] Failed to read "${e}": ${getErrorMessage2(t)}`,t)}}var p=require("fs/promises"),h=require("path"),d=require("crypto"),m=require("fs/promises"),w=require("path");async function atomicWrite(e,t){if(function(e,t){if("string"!=typeof e||0===e.length)throw new TypeError(`[confedit] ${t} must be a non-empty string.`)}(e,"filePath"),"string"!=typeof t)throw new TypeError("[confedit] content must be a string.");const r=normalizeFilePath(e),o=(0,w.dirname)(r),n=(0,w.basename)(r),a=`${process.pid}.${Date.now()}.${(0,d.randomBytes)(12).toString("hex")}`,i=(0,w.join)(o,`.${n}.${a}.tmp`),s=(0,w.join)(o,`.${n}.${a}.bak`);let c,l=!1,u=!1;try{await(0,m.mkdir)(o,{recursive:!0});const e=await async function(e){try{return 511&(await(0,m.stat)(e)).mode}catch(e){if("ENOENT"===getErrorCode(e))return;throw e}}(r);c=await(0,m.open)(i,"wx",e??384);try{await c.writeFile(t,"utf8"),await c.sync()}finally{await c.close(),c=void 0}void 0!==e&&await(0,m.chmod)(i,e);try{await(0,m.rename)(i,r),u=!0}catch(e){if(!function(e){if("win32"!==process.platform)return!1;const t=getErrorCode(e);return"EEXIST"===t||"EPERM"===t||"EACCES"===t}(e))throw e;await(0,m.rename)(r,s),l=!0;try{await(0,m.rename)(i,r),u=!0}catch(e){const t=await async function(e,t){try{return await(0,m.unlink)(e).catch(e=>{if("ENOENT"!==getErrorCode(e))throw e}),await(0,m.rename)(t,e),!0}catch{return!1}}(r,s);throw t&&(l=!1),createError4(t?`[confedit] Failed to replace "${r}", but the original file was restored.`:`[confedit] Failed to replace "${r}". The backup was retained at "${s}".`,e)}}await syncDirectory(o),l&&(await(0,m.unlink)(s),l=!1,await syncDirectory(o))}catch(t){throw void 0!==c&&await c.close().catch(()=>{}),createError4(`[confedit] Failed to atomically write "${e}": ${function(e){return e instanceof Error?e.message:String(e)}(t)}`,t)}finally{await(0,m.unlink)(i).catch(()=>{}),u&&l&&await(0,m.unlink)(s).catch(()=>{})}}async function syncDirectory(e){let t;try{t=await(0,m.open)(e,"r"),await t.sync()}catch(e){const t=getErrorCode(e);if("EINVAL"!==t&&"EPERM"!==t&&"EISDIR"!==t&&"ENOSYS"!==t&&"ENOTSUP"!==t)throw e}finally{await(t?.close().catch(()=>{}))}}function getErrorCode(e){if(null!==e&&"object"==typeof e&&"code"in e&&"string"==typeof e.code)return e.code}function createError4(e,t){const r=new Error(e);try{Object.defineProperty(r,"cause",{configurable:!0,enumerable:!1,value:t,writable:!0})}catch{}return r}var y=require("crypto"),g=require("fs/promises"),E=require("os"),b=1e4,v=25,P=1e3,O=6e4,$=new Map,M=new Set;async function withFileLock(e,t,r={}){const o=normalizeFilePath(e);return function(e){if(void 0!==e.timeoutMs&&(!Number.isFinite(e.timeoutMs)||e.timeoutMs<0))throw new TypeError("[confedit] lock timeoutMs must be a non-negative finite number.");if(void 0!==e.retryDelayMs&&(!Number.isFinite(e.retryDelayMs)||e.retryDelayMs<=0))throw new TypeError("[confedit] lock retryDelayMs must be a positive finite number.");if(void 0!==e.staleThresholdMs&&(!Number.isFinite(e.staleThresholdMs)||e.staleThresholdMs<=0))throw new TypeError("[confedit] lock staleThresholdMs must be a positive finite number.")}(r),function(e,t){const r=$.get(e)??Promise.resolve();let o;const n=new Promise(e=>{o=e}),a=r.catch(()=>{}).then(()=>n);return $.set(e,a),r.catch(()=>{}).then(t).finally(()=>{o(),$.get(e)===a&&$.delete(e)})}(o,async()=>{const e=await async function(e,t){const r=t.timeoutMs??b,o=t.retryDelayMs??v,n=t.staleThresholdMs??Math.max(2*r,O),a=t.allowStaleRecovery??!1,i=`${e}.confedit.lock`,s=Date.now();let c=o;const l=createToken(),u={version:1,pid:process.pid,hostname:(0,E.hostname)(),createdAt:(new Date).toISOString(),token:l};for(;;)try{const e=await(0,g.open)(i,"wx",384);try{await(0,g.writeFile)(e,`${JSON.stringify(u)}\n`,"utf8"),await e.sync()}finally{await e.close()}return M.add(i),createReleaseHandler(i,l)}catch(t){if("EEXIST"!==getErrorCode2(t))throw createError5(`[confedit] Failed to acquire lock "${i}": ${getErrorMessage5(t)}`,t);if(a&&await tryRecoverStaleLock(i,n),Date.now()-s>=r)throw new Error(`[confedit] Timed out waiting for lock on "${e}" after ${r}ms.`);await sleep(c),c=Math.min(Math.ceil(1.5*c),P)}}(o,r);try{return await t()}finally{await e()}})}async function releaseAllLocalLocks(){const e=Array.from(M).map(e=>(0,g.unlink)(e).catch(()=>{}));await Promise.allSettled(e),M.clear()}function createReleaseHandler(e,t){return async()=>{try{const r=await readLockData(e);r?.token===t&&(await(0,g.unlink)(e).catch(()=>{}),M.delete(e))}catch{}}}async function tryRecoverStaleLock(e,t){const r=await readLockData(e);if(void 0===r||!isExpiredLock(r,t))return;const o=`${e}.stale.${createToken()}`;try{await(0,g.rename)(e,o)}catch(e){return void getErrorCode2(e)}try{const n=await readLockData(o);if(n?.token===r.token&&isExpiredLock(n,t))return void await(0,g.unlink)(o).catch(()=>{});await(0,g.rename)(o,e).catch(()=>{})}catch{}}function isExpiredLock(e,t){const r=Date.parse(e.createdAt);return Number.isFinite(r)&&Date.now()-r>t}async function readLockData(e){try{return function(e){try{const t=JSON.parse(e);if(1!==t.version||"number"!=typeof t.pid||!Number.isSafeInteger(t.pid)||t.pid<=0||"string"!=typeof t.hostname||"string"!=typeof t.createdAt||!Number.isFinite(Date.parse(t.createdAt))||"string"!=typeof t.token||t.token.length<16)return;return{version:1,pid:t.pid,hostname:t.hostname,createdAt:t.createdAt,token:t.token}}catch{return}}(await(0,g.readFile)(e,"utf8"))}catch{return}}function createToken(){return`${process.pid}-${Date.now()}-${(0,y.randomBytes)(16).toString("hex")}`}function sleep(e){return new Promise(t=>setTimeout(t,e))}function getErrorCode2(e){if(null!==e&&"object"==typeof e&&"code"in e&&"string"==typeof e.code)return e.code}function getErrorMessage5(e){return e instanceof Error?e.message:String(e)}function createError5(e,t){const r=new Error(e);try{Object.defineProperty(r,"cause",{configurable:!0,enumerable:!1,value:t,writable:!0})}catch{}return r}async function writeConfigFile(e,t,r={}){if(assertNonEmptyString(e,"filePath"),"string"!=typeof t)throw new TypeError("[confedit] content must be a string.");const o=normalizeFilePath(e),writeTransaction=async()=>{try{await atomicWrite(o,t)}catch(t){throw createError2(`[confedit] Failed to write "${e}": ${getErrorMessage2(t)}`,t)}};r.lock??!0?(await(0,p.mkdir)((0,h.dirname)(o),{recursive:!0}),await withFileLock(o,writeTransaction,{timeoutMs:r.lockTimeoutMs,retryDelayMs:r.lockRetryDelayMs,staleThresholdMs:r.lockStaleThresholdMs,allowStaleRecovery:r.allowStaleRecovery})):await writeTransaction()}var T=require("path");function detectFormat(e){const t=normalizeFilePath(e);switch((0,T.extname)(t).toLowerCase()){case".json":return"json";case".jsonc":return"jsonc";case".yaml":case".yml":return"yaml";default:throw new Error(`[confedit] Cannot detect a supported configuration format for "${e}". Expected .json, .jsonc, .yaml, or .yml.`)}}async function patchConfigFile(e,t,r={}){if("string"!=typeof e||0===e.length)throw new TypeError("[confedit] filePath must be a non-empty string.");if(assertPatchOperations(t),0===t.length)return;const o=normalizeFilePath(e),n=r.format??detectFormat(o);assertConfigFormat(n),function(e){if(void 0!==e.lockTimeoutMs&&(!Number.isFinite(e.lockTimeoutMs)||e.lockTimeoutMs<0))throw new TypeError("[confedit] lockTimeoutMs must be a non-negative finite number.");if(void 0!==e.lockRetryDelayMs&&(!Number.isFinite(e.lockRetryDelayMs)||e.lockRetryDelayMs<=0))throw new TypeError("[confedit] lockRetryDelayMs must be a positive finite number.");if(void 0!==e.lockStaleThresholdMs&&(!Number.isFinite(e.lockStaleThresholdMs)||e.lockStaleThresholdMs<=0))throw new TypeError("[confedit] lockStaleThresholdMs must be a positive finite number.")}(r);const a=r.strict??!0,patchTransaction=async()=>{const r=await readConfigFile(o),i=patchContent(r,t,n,{strict:a});if(i!==r)try{await atomicWrite(o,i)}catch(t){throw createError2(`[confedit] Failed to write "${e}": ${getErrorMessage2(t)}`,t)}};r.lock??!0?await withFileLock(o,patchTransaction,{timeoutMs:r.lockTimeoutMs,retryDelayMs:r.lockRetryDelayMs,staleThresholdMs:r.lockStaleThresholdMs,allowStaleRecovery:r.allowStaleRecovery}):await patchTransaction()}async function setConfigValue(e,t,r,o={}){await patchConfigFile(e,[{op:"add",path:[...t],value:r}],o)}async function deleteConfigValue(e,t,r={}){await patchConfigFile(e,[{op:"remove",path:[...t]}],r)}var D,N=require("fs/promises"),S=require("path"),A=require("yaml"),I=class extends Error{constructor(e,t,r){super(t),this.name="OpenApiValidationError",this.code=e,this.cause=r}},F={inputKind:"content",timeoutMs:15e3,maxInputBytes:5242880,maxDocumentNodes:1e5,maxDocumentDepth:100,maxValidationErrors:50,maxErrorMessageLength:1e3};async function validateOpenAPISpec(e,t={}){!function(e,t){if("string"!=typeof e||0===e.length)throw new TypeError(`[confedit] ${t} must be a non-empty string.`)}(e,"input");const r=function(e){const t={inputKind:e.inputKind??F.inputKind,baseFilePath:e.baseFilePath,allowedRootDirectory:e.allowedRootDirectory,timeoutMs:e.timeoutMs??F.timeoutMs,maxInputBytes:e.maxInputBytes??F.maxInputBytes,maxDocumentNodes:e.maxDocumentNodes??F.maxDocumentNodes,maxDocumentDepth:e.maxDocumentDepth??F.maxDocumentDepth,maxValidationErrors:e.maxValidationErrors??F.maxValidationErrors,maxErrorMessageLength:e.maxErrorMessageLength??F.maxErrorMessageLength,signal:e.signal};if("content"!==t.inputKind&&"file"!==t.inputKind)throw new I("INVALID_OPTION",`inputKind must be "content" or "file", got: ${String(t.inputKind)}.`);return assertPositiveFiniteNumber(t.timeoutMs,"timeoutMs"),assertPositiveFiniteNumber(t.maxInputBytes,"maxInputBytes"),assertPositiveFiniteNumber(t.maxDocumentNodes,"maxDocumentNodes"),assertPositiveFiniteNumber(t.maxDocumentDepth,"maxDocumentDepth"),assertPositiveFiniteNumber(t.maxValidationErrors,"maxValidationErrors"),assertPositiveFiniteNumber(t.maxErrorMessageLength,"maxErrorMessageLength"),t}(t),o=function(e){const t=new AbortController;if(e){if(!e.aborted){const onAbort=()=>t.abort();return e.addEventListener("abort",onAbort,{once:!0}),{controller:t,dispose:()=>e.removeEventListener("abort",onAbort)}}t.abort()}return{controller:t,dispose:()=>{}}}(r.signal),n={options:r,controller:o.controller,deadline:Date.now()+r.timeoutMs};try{return await runWithDeadline(n,async()=>{if("file"===r.inputKind){const t=await async function(e,t){const r=normalizeFilePath(e);if(!t.options.allowedRootDirectory)throw new I("FILE_INPUT_FORBIDDEN","File input requires allowedRootDirectory.");return async function(e,t){const r=(0,S.isAbsolute)(e)?e:(0,S.resolve)(t,e),o=await(0,N.realpath)(r),n=(0,S.relative)(t,o);if(n.startsWith("..")||""===n||"\\"===S.sep&&/^[a-zA-Z]:/.test(n))throw new I("PATH_OUTSIDE_ROOT",`File path is outside the allowed root directory: ${e}.`);return o}(r,await async function(e){if(e.rootDirectory)return e.rootDirectory;const t=e.options.allowedRootDirectory;if(!t)throw new I("INVALID_OPTION","allowedRootDirectory is required for file operations.");const r=(0,S.resolve)(t),o=await(0,N.realpath)(r);if(!(await(0,N.stat)(o)).isDirectory())throw new I("INVALID_OPTION",`allowedRootDirectory is not a directory: ${t}.`);return e.rootDirectory=o,o}(t))}(e,n),o=await async function(e,t,r,o){throwIfCancelled(r);const n=await(0,N.open)(e,"r");try{const r=await n.stat();if(r.size>t)throw new I(o,`File exceeds maximum allowed size of ${t} bytes: ${e}.`);const a=Buffer.alloc(r.size);return await n.read(a,0,r.size,0),a.toString("utf8")}finally{await n.close()}}(t,r.maxInputBytes,n,"INPUT_FILE_TOO_LARGE");return validateRawContent(o,{...n,options:{...r,baseFilePath:t}})}return assertByteLength(e,r.maxInputBytes,"INPUT_TOO_LARGE"),validateRawContent(e,n)})}catch(e){throw function(e){if(e instanceof I)return e;return new I("UNKNOWN_ERROR",`OpenAPI validation failed: ${getErrorMessage6(e)}.`,e)}(e)}finally{o.dispose(),n.controller.abort()}}async function validateOpenAPIFile(e,t={}){return validateOpenAPISpec(e,{...t,inputKind:"file",baseFilePath:e})}async function validateRawContent(e,t){throwIfCancelled(t),assertByteLength(e,t.options.maxInputBytes,"INPUT_TOO_LARGE");const r=function(e){try{const t=(0,A.parse)(e);if(null===t||"object"!=typeof t||Array.isArray(t))throw new I("INVALID_DOCUMENT_SHAPE","OpenAPI document must be a JSON object.");return t}catch(e){if(e instanceof I)throw e;throw new I("PARSE_ERROR",`Failed to parse OpenAPI document: ${getErrorMessage6(e)}.`,e)}}(e);!function(e,t){let r=0;const o=[{value:e,depth:0}];for(;o.length>0;){const{value:e,depth:n}=o.pop();if(n>t.maxDocumentDepth)throw new I("DOCUMENT_TOO_DEEP",`Document exceeds maximum nesting depth of ${t.maxDocumentDepth}.`);if(r++,r>t.maxDocumentNodes)throw new I("DOCUMENT_TOO_LARGE",`Document exceeds maximum node count of ${t.maxDocumentNodes}.`);if(Array.isArray(e))for(const t of e)o.push({value:t,depth:n+1});else if(null!==e&&"object"==typeof e)for(const t of Object.values(e))o.push({value:t,depth:n+1})}}(r,t.options),function(e){const t=e.openapi??e.swagger;if("string"!=typeof t||0===t.length)throw new I("UNSUPPORTED_VERSION","Document must declare an openapi or swagger version.");const r=Number.parseInt(t.split(".")[0]??"",10);if(!Number.isFinite(r)||2!==r&&3!==r)throw new I("UNSUPPORTED_VERSION",`Unsupported OpenAPI/Swagger version: ${t}. Supported: 2.x, 3.x.`)}(r);const o=await runWithDeadline(t,()=>async function(){return D??=import("@powerduck/openapi-parser").then(e=>e.validate),D}()),n=await runWithDeadline(t,()=>o(r,{throwOnError:!1}));if(!n.valid)throw new I("SPEC_VALIDATION_FAILED",`OpenAPI validation failed: ${function(e,t,r){const o=e.slice(0,t).map(e=>{const t=e.instancePath??"",o=e.message??"Unknown error",n=t?`${t}: ${o}`:o;return n.length>r?`${n.slice(0,r)}...`:n});e.length>t&&o.push(`... and ${e.length-t} more errors`);return o.join("; ")}(n.errors??[],t.options.maxValidationErrors,t.options.maxErrorMessageLength)}`);return n.specification}function assertPositiveFiniteNumber(e,t){if(!Number.isFinite(e)||e<=0)throw new I("INVALID_OPTION",`${t} must be a positive finite number, got: ${String(e)}.`)}async function runWithDeadline(e,t){throwIfCancelled(e);const r=await Promise.race([t(),createDeadlinePromise(e)]);return throwIfCancelled(e),r}function createDeadlinePromise(e){return new Promise((t,r)=>{const o=e.deadline-Date.now(),n=Math.max(0,Math.min(o,2147483647)),a=setTimeout(()=>{r(new I("OPERATION_TIMEOUT",`OpenAPI validation operation timed out after ${e.options.timeoutMs}ms.`))},n);e.controller.signal.addEventListener("abort",()=>{clearTimeout(a),r(new I("OPERATION_ABORTED","OpenAPI validation operation was aborted."))},{once:!0})})}function throwIfCancelled(e){if(e.controller.signal.aborted)throw new I("OPERATION_ABORTED","OpenAPI validation operation was aborted.");if(Date.now()>e.deadline)throw new I("OPERATION_TIMEOUT",`OpenAPI validation operation timed out after ${e.options.timeoutMs}ms.`)}function assertByteLength(e,t,r){const o=Buffer.byteLength(e,"utf8");if(o>t)throw new I(r,`Input exceeds maximum allowed size of ${t} bytes (actual: ${o} bytes).`)}function getErrorMessage6(e){return e instanceof Error?e.message:String(e)}
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{assertConfigFormat as t,assertNonEmptyString as e,assertPatchOperations as o,assertPatchPath as r,createError as n,deleteContentValue as i,getErrorMessage as a,patchContent as s,setContentValue as c}from"./chunk-YWMVVRYM.mjs";import{readFile as l}from"fs/promises";import{isAbsolute as u,resolve as f}from"path";import{fileURLToPath as m}from"url";function normalizeFilePath(t){if("string"!=typeof t||0===t.length)throw new TypeError("[confedit] filePath must be a non-empty string.");if(t.startsWith("file:"))try{return m(t)}catch(e){throw function(t,e){const o=new Error(t);try{Object.defineProperty(o,"cause",{configurable:!0,enumerable:!1,value:e,writable:!0})}catch{}return o}(`[confedit] Invalid file URL "${t}": ${function(t){return t instanceof Error?t.message:String(t)}(e)}`,e)}return u(t)?t:f(t)}async function readConfigFile(t){e(t,"filePath");const o=normalizeFilePath(t);try{return await l(o,"utf8")}catch(e){throw n(`[confedit] Failed to read "${t}": ${a(e)}`,e)}}import{mkdir as d}from"fs/promises";import{dirname as w}from"path";import{randomBytes as h}from"crypto";import{chmod as p,mkdir as y,open as g,rename as E,stat as D,unlink as b}from"fs/promises";import{basename as T,dirname as v,join as M}from"path";async function atomicWrite(t,e){if(function(t,e){if("string"!=typeof t||0===t.length)throw new TypeError(`[confedit] ${e} must be a non-empty string.`)}(t,"filePath"),"string"!=typeof e)throw new TypeError("[confedit] content must be a string.");const o=normalizeFilePath(t),r=v(o),n=T(o),i=`${process.pid}.${Date.now()}.${h(12).toString("hex")}`,a=M(r,`.${n}.${i}.tmp`),s=M(r,`.${n}.${i}.bak`);let c,l=!1,u=!1;try{await y(r,{recursive:!0});const t=await async function(t){try{return 511&(await D(t)).mode}catch(t){if("ENOENT"===getErrorCode(t))return;throw t}}(o);c=await g(a,"wx",t??384);try{await c.writeFile(e,"utf8"),await c.sync()}finally{await c.close(),c=void 0}void 0!==t&&await p(a,t);try{await E(a,o),u=!0}catch(t){if(!function(t){if("win32"!==process.platform)return!1;const e=getErrorCode(t);return"EEXIST"===e||"EPERM"===e||"EACCES"===e}(t))throw t;await E(o,s),l=!0;try{await E(a,o),u=!0}catch(t){const e=await async function(t,e){try{return await b(t).catch(t=>{if("ENOENT"!==getErrorCode(t))throw t}),await E(e,t),!0}catch{return!1}}(o,s);throw e&&(l=!1),createError3(e?`[confedit] Failed to replace "${o}", but the original file was restored.`:`[confedit] Failed to replace "${o}". The backup was retained at "${s}".`,t)}}await syncDirectory(r),l&&(await b(s),l=!1,await syncDirectory(r))}catch(e){throw void 0!==c&&await c.close().catch(()=>{}),createError3(`[confedit] Failed to atomically write "${t}": ${function(t){return t instanceof Error?t.message:String(t)}(e)}`,e)}finally{await b(a).catch(()=>{}),u&&l&&await b(s).catch(()=>{})}}async function syncDirectory(t){let e;try{e=await g(t,"r"),await e.sync()}catch(t){const e=getErrorCode(t);if("EINVAL"!==e&&"EPERM"!==e&&"EISDIR"!==e&&"ENOSYS"!==e&&"ENOTSUP"!==e)throw t}finally{await(e?.close().catch(()=>{}))}}function getErrorCode(t){if(null!==t&&"object"==typeof t&&"code"in t&&"string"==typeof t.code)return t.code}function createError3(t,e){const o=new Error(t);try{Object.defineProperty(o,"cause",{configurable:!0,enumerable:!1,value:e,writable:!0})}catch{}return o}import{randomBytes as P}from"crypto";import{open as O,readFile as I,rename as N,unlink as F,writeFile as x}from"fs/promises";import{hostname as k}from"os";var $=new Map,A=new Set;async function withFileLock(t,e,o={}){const r=normalizeFilePath(t);return function(t){if(void 0!==t.timeoutMs&&(!Number.isFinite(t.timeoutMs)||t.timeoutMs<0))throw new TypeError("[confedit] lock timeoutMs must be a non-negative finite number.");if(void 0!==t.retryDelayMs&&(!Number.isFinite(t.retryDelayMs)||t.retryDelayMs<=0))throw new TypeError("[confedit] lock retryDelayMs must be a positive finite number.");if(void 0!==t.staleThresholdMs&&(!Number.isFinite(t.staleThresholdMs)||t.staleThresholdMs<=0))throw new TypeError("[confedit] lock staleThresholdMs must be a positive finite number.")}(o),function(t,e){const o=$.get(t)??Promise.resolve();let r;const n=new Promise(t=>{r=t}),i=o.catch(()=>{}).then(()=>n);return $.set(t,i),o.catch(()=>{}).then(e).finally(()=>{r(),$.get(t)===i&&$.delete(t)})}(r,async()=>{const t=await async function(t,e){const o=e.timeoutMs??1e4,r=e.retryDelayMs??25,n=e.staleThresholdMs??Math.max(2*o,6e4),i=e.allowStaleRecovery??!1,a=`${t}.confedit.lock`,s=Date.now();let c=r;const l=createToken(),u={version:1,pid:process.pid,hostname:k(),createdAt:(new Date).toISOString(),token:l};for(;;)try{const t=await O(a,"wx",384);try{await x(t,`${JSON.stringify(u)}\n`,"utf8"),await t.sync()}finally{await t.close()}return A.add(a),createReleaseHandler(a,l)}catch(e){if("EEXIST"!==getErrorCode2(e))throw createError4(`[confedit] Failed to acquire lock "${a}": ${getErrorMessage4(e)}`,e);if(i&&await tryRecoverStaleLock(a,n),Date.now()-s>=o)throw new Error(`[confedit] Timed out waiting for lock on "${t}" after ${o}ms.`);await sleep(c),c=Math.min(Math.ceil(1.5*c),1e3)}}(r,o);try{return await e()}finally{await t()}})}async function releaseAllLocalLocks(){const t=Array.from(A).map(t=>F(t).catch(()=>{}));await Promise.allSettled(t),A.clear()}function createReleaseHandler(t,e){return async()=>{try{const o=await readLockData(t);o?.token===e&&(await F(t).catch(()=>{}),A.delete(t))}catch{}}}async function tryRecoverStaleLock(t,e){const o=await readLockData(t);if(void 0===o||!isExpiredLock(o,e))return;const r=`${t}.stale.${createToken()}`;try{await N(t,r)}catch(t){return void getErrorCode2(t)}try{const n=await readLockData(r);if(n?.token===o.token&&isExpiredLock(n,e))return void await F(r).catch(()=>{});await N(r,t).catch(()=>{})}catch{}}function isExpiredLock(t,e){const o=Date.parse(t.createdAt);return Number.isFinite(o)&&Date.now()-o>e}async function readLockData(t){try{return function(t){try{const e=JSON.parse(t);if(1!==e.version||"number"!=typeof e.pid||!Number.isSafeInteger(e.pid)||e.pid<=0||"string"!=typeof e.hostname||"string"!=typeof e.createdAt||!Number.isFinite(Date.parse(e.createdAt))||"string"!=typeof e.token||e.token.length<16)return;return{version:1,pid:e.pid,hostname:e.hostname,createdAt:e.createdAt,token:e.token}}catch{return}}(await I(t,"utf8"))}catch{return}}function createToken(){return`${process.pid}-${Date.now()}-${P(16).toString("hex")}`}function sleep(t){return new Promise(e=>setTimeout(e,t))}function getErrorCode2(t){if(null!==t&&"object"==typeof t&&"code"in t&&"string"==typeof t.code)return t.code}function getErrorMessage4(t){return t instanceof Error?t.message:String(t)}function createError4(t,e){const o=new Error(t);try{Object.defineProperty(o,"cause",{configurable:!0,enumerable:!1,value:e,writable:!0})}catch{}return o}async function writeConfigFile(t,o,r={}){if(e(t,"filePath"),"string"!=typeof o)throw new TypeError("[confedit] content must be a string.");const i=normalizeFilePath(t),writeTransaction=async()=>{try{await atomicWrite(i,o)}catch(e){throw n(`[confedit] Failed to write "${t}": ${a(e)}`,e)}};r.lock??!0?(await d(w(i),{recursive:!0}),await withFileLock(i,writeTransaction,{timeoutMs:r.lockTimeoutMs,retryDelayMs:r.lockRetryDelayMs,staleThresholdMs:r.lockStaleThresholdMs,allowStaleRecovery:r.allowStaleRecovery})):await writeTransaction()}import{extname as R}from"path";function detectFormat(t){const e=normalizeFilePath(t);switch(R(e).toLowerCase()){case".json":return"json";case".jsonc":return"jsonc";case".yaml":case".yml":return"yaml";default:throw new Error(`[confedit] Cannot detect a supported configuration format for "${t}". Expected .json, .jsonc, .yaml, or .yml.`)}}async function patchConfigFile(e,r,i={}){if("string"!=typeof e||0===e.length)throw new TypeError("[confedit] filePath must be a non-empty string.");if(o(r),0===r.length)return;const c=normalizeFilePath(e),l=i.format??detectFormat(c);t(l),function(t){if(void 0!==t.lockTimeoutMs&&(!Number.isFinite(t.lockTimeoutMs)||t.lockTimeoutMs<0))throw new TypeError("[confedit] lockTimeoutMs must be a non-negative finite number.");if(void 0!==t.lockRetryDelayMs&&(!Number.isFinite(t.lockRetryDelayMs)||t.lockRetryDelayMs<=0))throw new TypeError("[confedit] lockRetryDelayMs must be a positive finite number.");if(void 0!==t.lockStaleThresholdMs&&(!Number.isFinite(t.lockStaleThresholdMs)||t.lockStaleThresholdMs<=0))throw new TypeError("[confedit] lockStaleThresholdMs must be a positive finite number.")}(i);const u=i.strict??!0,patchTransaction=async()=>{const t=await readConfigFile(c),o=s(t,r,l,{strict:u});if(o!==t)try{await atomicWrite(c,o)}catch(t){throw n(`[confedit] Failed to write "${e}": ${a(t)}`,t)}};i.lock??!0?await withFileLock(c,patchTransaction,{timeoutMs:i.lockTimeoutMs,retryDelayMs:i.lockRetryDelayMs,staleThresholdMs:i.lockStaleThresholdMs,allowStaleRecovery:i.allowStaleRecovery}):await patchTransaction()}async function setConfigValue(t,e,o,r={}){await patchConfigFile(t,[{op:"add",path:[...e],value:o}],r)}async function deleteConfigValue(t,e,o={}){await patchConfigFile(t,[{op:"remove",path:[...e]}],o)}import{open as S,realpath as L,stat as C}from"fs/promises";import{isAbsolute as _,relative as V,resolve as U,sep as B}from"path";import{parse as j}from"yaml";var z,K=class extends Error{constructor(t,e,o){super(e),this.name="OpenApiValidationError",this.code=t,this.cause=o}},W={inputKind:"content",timeoutMs:15e3,maxInputBytes:5242880,maxDocumentNodes:1e5,maxDocumentDepth:100,maxValidationErrors:50,maxErrorMessageLength:1e3};async function validateOpenAPISpec(t,e={}){!function(t,e){if("string"!=typeof t||0===t.length)throw new TypeError(`[confedit] ${e} must be a non-empty string.`)}(t,"input");const o=function(t){const e={inputKind:t.inputKind??W.inputKind,baseFilePath:t.baseFilePath,allowedRootDirectory:t.allowedRootDirectory,timeoutMs:t.timeoutMs??W.timeoutMs,maxInputBytes:t.maxInputBytes??W.maxInputBytes,maxDocumentNodes:t.maxDocumentNodes??W.maxDocumentNodes,maxDocumentDepth:t.maxDocumentDepth??W.maxDocumentDepth,maxValidationErrors:t.maxValidationErrors??W.maxValidationErrors,maxErrorMessageLength:t.maxErrorMessageLength??W.maxErrorMessageLength,signal:t.signal};if("content"!==e.inputKind&&"file"!==e.inputKind)throw new K("INVALID_OPTION",`inputKind must be "content" or "file", got: ${String(e.inputKind)}.`);return assertPositiveFiniteNumber(e.timeoutMs,"timeoutMs"),assertPositiveFiniteNumber(e.maxInputBytes,"maxInputBytes"),assertPositiveFiniteNumber(e.maxDocumentNodes,"maxDocumentNodes"),assertPositiveFiniteNumber(e.maxDocumentDepth,"maxDocumentDepth"),assertPositiveFiniteNumber(e.maxValidationErrors,"maxValidationErrors"),assertPositiveFiniteNumber(e.maxErrorMessageLength,"maxErrorMessageLength"),e}(e),r=function(t){const e=new AbortController;if(t){if(!t.aborted){const onAbort=()=>e.abort();return t.addEventListener("abort",onAbort,{once:!0}),{controller:e,dispose:()=>t.removeEventListener("abort",onAbort)}}e.abort()}return{controller:e,dispose:()=>{}}}(o.signal),n={options:o,controller:r.controller,deadline:Date.now()+o.timeoutMs};try{return await runWithDeadline(n,async()=>{if("file"===o.inputKind){const e=await async function(t,e){const o=normalizeFilePath(t);if(!e.options.allowedRootDirectory)throw new K("FILE_INPUT_FORBIDDEN","File input requires allowedRootDirectory.");return async function(t,e){const o=_(t)?t:U(e,t),r=await L(o),n=V(e,r);if(n.startsWith("..")||""===n||"\\"===B&&/^[a-zA-Z]:/.test(n))throw new K("PATH_OUTSIDE_ROOT",`File path is outside the allowed root directory: ${t}.`);return r}(o,await async function(t){if(t.rootDirectory)return t.rootDirectory;const e=t.options.allowedRootDirectory;if(!e)throw new K("INVALID_OPTION","allowedRootDirectory is required for file operations.");const o=U(e),r=await L(o);if(!(await C(r)).isDirectory())throw new K("INVALID_OPTION",`allowedRootDirectory is not a directory: ${e}.`);return t.rootDirectory=r,r}(e))}(t,n),r=await async function(t,e,o,r){throwIfCancelled(o);const n=await S(t,"r");try{const o=await n.stat();if(o.size>e)throw new K(r,`File exceeds maximum allowed size of ${e} bytes: ${t}.`);const i=Buffer.alloc(o.size);return await n.read(i,0,o.size,0),i.toString("utf8")}finally{await n.close()}}(e,o.maxInputBytes,n,"INPUT_FILE_TOO_LARGE");return validateRawContent(r,{...n,options:{...o,baseFilePath:e}})}return assertByteLength(t,o.maxInputBytes,"INPUT_TOO_LARGE"),validateRawContent(t,n)})}catch(t){throw function(t){if(t instanceof K)return t;return new K("UNKNOWN_ERROR",`OpenAPI validation failed: ${getErrorMessage5(t)}.`,t)}(t)}finally{r.dispose(),n.controller.abort()}}async function validateOpenAPIFile(t,e={}){return validateOpenAPISpec(t,{...e,inputKind:"file",baseFilePath:t})}async function validateRawContent(t,e){throwIfCancelled(e),assertByteLength(t,e.options.maxInputBytes,"INPUT_TOO_LARGE");const o=function(t){try{const e=j(t);if(null===e||"object"!=typeof e||Array.isArray(e))throw new K("INVALID_DOCUMENT_SHAPE","OpenAPI document must be a JSON object.");return e}catch(t){if(t instanceof K)throw t;throw new K("PARSE_ERROR",`Failed to parse OpenAPI document: ${getErrorMessage5(t)}.`,t)}}(t);!function(t,e){let o=0;const r=[{value:t,depth:0}];for(;r.length>0;){const{value:t,depth:n}=r.pop();if(n>e.maxDocumentDepth)throw new K("DOCUMENT_TOO_DEEP",`Document exceeds maximum nesting depth of ${e.maxDocumentDepth}.`);if(o++,o>e.maxDocumentNodes)throw new K("DOCUMENT_TOO_LARGE",`Document exceeds maximum node count of ${e.maxDocumentNodes}.`);if(Array.isArray(t))for(const e of t)r.push({value:e,depth:n+1});else if(null!==t&&"object"==typeof t)for(const e of Object.values(t))r.push({value:e,depth:n+1})}}(o,e.options),function(t){const e=t.openapi??t.swagger;if("string"!=typeof e||0===e.length)throw new K("UNSUPPORTED_VERSION","Document must declare an openapi or swagger version.");const o=Number.parseInt(e.split(".")[0]??"",10);if(!Number.isFinite(o)||2!==o&&3!==o)throw new K("UNSUPPORTED_VERSION",`Unsupported OpenAPI/Swagger version: ${e}. Supported: 2.x, 3.x.`)}(o);const r=await runWithDeadline(e,()=>async function(){return z??=import("@powerduck/openapi-parser").then(({validate:t})=>t),z}()),n=await runWithDeadline(e,()=>r(o,{throwOnError:!1}));if(!n.valid)throw new K("SPEC_VALIDATION_FAILED",`OpenAPI validation failed: ${function(t,e,o){const r=t.slice(0,e).map(t=>{const e=t.instancePath??"",r=t.message??"Unknown error",n=e?`${e}: ${r}`:r;return n.length>o?`${n.slice(0,o)}...`:n});t.length>e&&r.push(`... and ${t.length-e} more errors`);return r.join("; ")}(n.errors??[],e.options.maxValidationErrors,e.options.maxErrorMessageLength)}`);return n.specification}function assertPositiveFiniteNumber(t,e){if(!Number.isFinite(t)||t<=0)throw new K("INVALID_OPTION",`${e} must be a positive finite number, got: ${String(t)}.`)}async function runWithDeadline(t,e){throwIfCancelled(t);const o=await Promise.race([e(),createDeadlinePromise(t)]);return throwIfCancelled(t),o}function createDeadlinePromise(t){return new Promise((e,o)=>{const r=t.deadline-Date.now(),n=Math.max(0,Math.min(r,2147483647)),i=setTimeout(()=>{o(new K("OPERATION_TIMEOUT",`OpenAPI validation operation timed out after ${t.options.timeoutMs}ms.`))},n);t.controller.signal.addEventListener("abort",()=>{clearTimeout(i),o(new K("OPERATION_ABORTED","OpenAPI validation operation was aborted."))},{once:!0})})}function throwIfCancelled(t){if(t.controller.signal.aborted)throw new K("OPERATION_ABORTED","OpenAPI validation operation was aborted.");if(Date.now()>t.deadline)throw new K("OPERATION_TIMEOUT",`OpenAPI validation operation timed out after ${t.options.timeoutMs}ms.`)}function assertByteLength(t,e,o){const r=Buffer.byteLength(t,"utf8");if(r>e)throw new K(o,`Input exceeds maximum allowed size of ${e} bytes (actual: ${r} bytes).`)}function getErrorMessage5(t){return t instanceof Error?t.message:String(t)}export{K as OpenApiValidationError,t as assertConfigFormat,e as assertNonEmptyString,o as assertPatchOperations,r as assertPatchPath,n as createError,deleteConfigValue,i as deleteContentValue,detectFormat,a as getErrorMessage,normalizeFilePath,patchConfigFile,s as patchContent,readConfigFile,releaseAllLocalLocks,setConfigValue,c as setContentValue,validateOpenAPIFile,validateOpenAPISpec,withFileLock,writeConfigFile};
|
|
1
|
+
import{assertConfigFormat as t,assertNonEmptyString as e,assertPatchOperations as o,assertPatchPath as r,createError as n,deleteContentValue as i,getErrorMessage as a,patchContent as s,setContentValue as c}from"./chunk-YWMVVRYM.mjs";import{readFile as l}from"fs/promises";import{isAbsolute as u,resolve as f}from"path";import{fileURLToPath as m}from"url";function normalizeFilePath(t){if("string"!=typeof t||0===t.length)throw new TypeError("[confedit] filePath must be a non-empty string.");if(t.startsWith("file:"))try{return m(t)}catch(e){throw function(t,e){const o=new Error(t);try{Object.defineProperty(o,"cause",{configurable:!0,enumerable:!1,value:e,writable:!0})}catch{}return o}(`[confedit] Invalid file URL "${t}": ${function(t){return t instanceof Error?t.message:String(t)}(e)}`,e)}return u(t)?t:f(t)}async function readConfigFile(t){e(t,"filePath");const o=normalizeFilePath(t);try{return await l(o,"utf8")}catch(e){throw n(`[confedit] Failed to read "${t}": ${a(e)}`,e)}}import{mkdir as d}from"fs/promises";import{dirname as w}from"path";import{randomBytes as h}from"crypto";import{chmod as p,mkdir as y,open as g,rename as E,stat as D,unlink as b}from"fs/promises";import{basename as T,dirname as v,join as M}from"path";async function atomicWrite(t,e){if(function(t,e){if("string"!=typeof t||0===t.length)throw new TypeError(`[confedit] ${e} must be a non-empty string.`)}(t,"filePath"),"string"!=typeof e)throw new TypeError("[confedit] content must be a string.");const o=normalizeFilePath(t),r=v(o),n=T(o),i=`${process.pid}.${Date.now()}.${h(12).toString("hex")}`,a=M(r,`.${n}.${i}.tmp`),s=M(r,`.${n}.${i}.bak`);let c,l=!1,u=!1;try{await y(r,{recursive:!0});const t=await async function(t){try{return 511&(await D(t)).mode}catch(t){if("ENOENT"===getErrorCode(t))return;throw t}}(o);c=await g(a,"wx",t??384);try{await c.writeFile(e,"utf8"),await c.sync()}finally{await c.close(),c=void 0}void 0!==t&&await p(a,t);try{await E(a,o),u=!0}catch(t){if(!function(t){if("win32"!==process.platform)return!1;const e=getErrorCode(t);return"EEXIST"===e||"EPERM"===e||"EACCES"===e}(t))throw t;await E(o,s),l=!0;try{await E(a,o),u=!0}catch(t){const e=await async function(t,e){try{return await b(t).catch(t=>{if("ENOENT"!==getErrorCode(t))throw t}),await E(e,t),!0}catch{return!1}}(o,s);throw e&&(l=!1),createError3(e?`[confedit] Failed to replace "${o}", but the original file was restored.`:`[confedit] Failed to replace "${o}". The backup was retained at "${s}".`,t)}}await syncDirectory(r),l&&(await b(s),l=!1,await syncDirectory(r))}catch(e){throw void 0!==c&&await c.close().catch(()=>{}),createError3(`[confedit] Failed to atomically write "${t}": ${function(t){return t instanceof Error?t.message:String(t)}(e)}`,e)}finally{await b(a).catch(()=>{}),u&&l&&await b(s).catch(()=>{})}}async function syncDirectory(t){let e;try{e=await g(t,"r"),await e.sync()}catch(t){const e=getErrorCode(t);if("EINVAL"!==e&&"EPERM"!==e&&"EISDIR"!==e&&"ENOSYS"!==e&&"ENOTSUP"!==e)throw t}finally{await(e?.close().catch(()=>{}))}}function getErrorCode(t){if(null!==t&&"object"==typeof t&&"code"in t&&"string"==typeof t.code)return t.code}function createError3(t,e){const o=new Error(t);try{Object.defineProperty(o,"cause",{configurable:!0,enumerable:!1,value:e,writable:!0})}catch{}return o}import{randomBytes as P}from"crypto";import{open as O,readFile as I,rename as N,unlink as F,writeFile as x}from"fs/promises";import{hostname as k}from"os";var $=new Map,A=new Set;async function withFileLock(t,e,o={}){const r=normalizeFilePath(t);return function(t){if(void 0!==t.timeoutMs&&(!Number.isFinite(t.timeoutMs)||t.timeoutMs<0))throw new TypeError("[confedit] lock timeoutMs must be a non-negative finite number.");if(void 0!==t.retryDelayMs&&(!Number.isFinite(t.retryDelayMs)||t.retryDelayMs<=0))throw new TypeError("[confedit] lock retryDelayMs must be a positive finite number.");if(void 0!==t.staleThresholdMs&&(!Number.isFinite(t.staleThresholdMs)||t.staleThresholdMs<=0))throw new TypeError("[confedit] lock staleThresholdMs must be a positive finite number.")}(o),function(t,e){const o=$.get(t)??Promise.resolve();let r;const n=new Promise(t=>{r=t}),i=o.catch(()=>{}).then(()=>n);return $.set(t,i),o.catch(()=>{}).then(e).finally(()=>{r(),$.get(t)===i&&$.delete(t)})}(r,async()=>{const t=await async function(t,e){const o=e.timeoutMs??1e4,r=e.retryDelayMs??25,n=e.staleThresholdMs??Math.max(2*o,6e4),i=e.allowStaleRecovery??!1,a=`${t}.confedit.lock`,s=Date.now();let c=r;const l=createToken(),u={version:1,pid:process.pid,hostname:k(),createdAt:(new Date).toISOString(),token:l};for(;;)try{const t=await O(a,"wx",384);try{await x(t,`${JSON.stringify(u)}\n`,"utf8"),await t.sync()}finally{await t.close()}return A.add(a),createReleaseHandler(a,l)}catch(e){if("EEXIST"!==getErrorCode2(e))throw createError4(`[confedit] Failed to acquire lock "${a}": ${getErrorMessage4(e)}`,e);if(i&&await tryRecoverStaleLock(a,n),Date.now()-s>=o)throw new Error(`[confedit] Timed out waiting for lock on "${t}" after ${o}ms.`);await sleep(c),c=Math.min(Math.ceil(1.5*c),1e3)}}(r,o);try{return await e()}finally{await t()}})}async function releaseAllLocalLocks(){const t=Array.from(A).map(t=>F(t).catch(()=>{}));await Promise.allSettled(t),A.clear()}function createReleaseHandler(t,e){return async()=>{try{const o=await readLockData(t);o?.token===e&&(await F(t).catch(()=>{}),A.delete(t))}catch{}}}async function tryRecoverStaleLock(t,e){const o=await readLockData(t);if(void 0===o||!isExpiredLock(o,e))return;const r=`${t}.stale.${createToken()}`;try{await N(t,r)}catch(t){return void getErrorCode2(t)}try{const n=await readLockData(r);if(n?.token===o.token&&isExpiredLock(n,e))return void await F(r).catch(()=>{});await N(r,t).catch(()=>{})}catch{}}function isExpiredLock(t,e){const o=Date.parse(t.createdAt);return Number.isFinite(o)&&Date.now()-o>e}async function readLockData(t){try{return function(t){try{const e=JSON.parse(t);if(1!==e.version||"number"!=typeof e.pid||!Number.isSafeInteger(e.pid)||e.pid<=0||"string"!=typeof e.hostname||"string"!=typeof e.createdAt||!Number.isFinite(Date.parse(e.createdAt))||"string"!=typeof e.token||e.token.length<16)return;return{version:1,pid:e.pid,hostname:e.hostname,createdAt:e.createdAt,token:e.token}}catch{return}}(await I(t,"utf8"))}catch{return}}function createToken(){return`${process.pid}-${Date.now()}-${P(16).toString("hex")}`}function sleep(t){return new Promise(e=>setTimeout(e,t))}function getErrorCode2(t){if(null!==t&&"object"==typeof t&&"code"in t&&"string"==typeof t.code)return t.code}function getErrorMessage4(t){return t instanceof Error?t.message:String(t)}function createError4(t,e){const o=new Error(t);try{Object.defineProperty(o,"cause",{configurable:!0,enumerable:!1,value:e,writable:!0})}catch{}return o}async function writeConfigFile(t,o,r={}){if(e(t,"filePath"),"string"!=typeof o)throw new TypeError("[confedit] content must be a string.");const i=normalizeFilePath(t),writeTransaction=async()=>{try{await atomicWrite(i,o)}catch(e){throw n(`[confedit] Failed to write "${t}": ${a(e)}`,e)}};r.lock??!0?(await d(w(i),{recursive:!0}),await withFileLock(i,writeTransaction,{timeoutMs:r.lockTimeoutMs,retryDelayMs:r.lockRetryDelayMs,staleThresholdMs:r.lockStaleThresholdMs,allowStaleRecovery:r.allowStaleRecovery})):await writeTransaction()}import{extname as R}from"path";function detectFormat(t){const e=normalizeFilePath(t);switch(R(e).toLowerCase()){case".json":return"json";case".jsonc":return"jsonc";case".yaml":case".yml":return"yaml";default:throw new Error(`[confedit] Cannot detect a supported configuration format for "${t}". Expected .json, .jsonc, .yaml, or .yml.`)}}async function patchConfigFile(e,r,i={}){if("string"!=typeof e||0===e.length)throw new TypeError("[confedit] filePath must be a non-empty string.");if(o(r),0===r.length)return;const c=normalizeFilePath(e),l=i.format??detectFormat(c);t(l),function(t){if(void 0!==t.lockTimeoutMs&&(!Number.isFinite(t.lockTimeoutMs)||t.lockTimeoutMs<0))throw new TypeError("[confedit] lockTimeoutMs must be a non-negative finite number.");if(void 0!==t.lockRetryDelayMs&&(!Number.isFinite(t.lockRetryDelayMs)||t.lockRetryDelayMs<=0))throw new TypeError("[confedit] lockRetryDelayMs must be a positive finite number.");if(void 0!==t.lockStaleThresholdMs&&(!Number.isFinite(t.lockStaleThresholdMs)||t.lockStaleThresholdMs<=0))throw new TypeError("[confedit] lockStaleThresholdMs must be a positive finite number.")}(i);const u=i.strict??!0,patchTransaction=async()=>{const t=await readConfigFile(c),o=s(t,r,l,{strict:u});if(o!==t)try{await atomicWrite(c,o)}catch(t){throw n(`[confedit] Failed to write "${e}": ${a(t)}`,t)}};i.lock??!0?await withFileLock(c,patchTransaction,{timeoutMs:i.lockTimeoutMs,retryDelayMs:i.lockRetryDelayMs,staleThresholdMs:i.lockStaleThresholdMs,allowStaleRecovery:i.allowStaleRecovery}):await patchTransaction()}async function setConfigValue(t,e,o,r={}){await patchConfigFile(t,[{op:"add",path:[...e],value:o}],r)}async function deleteConfigValue(t,e,o={}){await patchConfigFile(t,[{op:"remove",path:[...e]}],o)}import{open as S,realpath as L,stat as C}from"fs/promises";import{isAbsolute as _,relative as V,resolve as U,sep as B}from"path";import{parse as j}from"yaml";var z,K=class extends Error{constructor(t,e,o){super(e),this.name="OpenApiValidationError",this.code=t,this.cause=o}},W={inputKind:"content",timeoutMs:15e3,maxInputBytes:5242880,maxDocumentNodes:1e5,maxDocumentDepth:100,maxValidationErrors:50,maxErrorMessageLength:1e3};async function validateOpenAPISpec(t,e={}){!function(t,e){if("string"!=typeof t||0===t.length)throw new TypeError(`[confedit] ${e} must be a non-empty string.`)}(t,"input");const o=function(t){const e={inputKind:t.inputKind??W.inputKind,baseFilePath:t.baseFilePath,allowedRootDirectory:t.allowedRootDirectory,timeoutMs:t.timeoutMs??W.timeoutMs,maxInputBytes:t.maxInputBytes??W.maxInputBytes,maxDocumentNodes:t.maxDocumentNodes??W.maxDocumentNodes,maxDocumentDepth:t.maxDocumentDepth??W.maxDocumentDepth,maxValidationErrors:t.maxValidationErrors??W.maxValidationErrors,maxErrorMessageLength:t.maxErrorMessageLength??W.maxErrorMessageLength,signal:t.signal};if("content"!==e.inputKind&&"file"!==e.inputKind)throw new K("INVALID_OPTION",`inputKind must be "content" or "file", got: ${String(e.inputKind)}.`);return assertPositiveFiniteNumber(e.timeoutMs,"timeoutMs"),assertPositiveFiniteNumber(e.maxInputBytes,"maxInputBytes"),assertPositiveFiniteNumber(e.maxDocumentNodes,"maxDocumentNodes"),assertPositiveFiniteNumber(e.maxDocumentDepth,"maxDocumentDepth"),assertPositiveFiniteNumber(e.maxValidationErrors,"maxValidationErrors"),assertPositiveFiniteNumber(e.maxErrorMessageLength,"maxErrorMessageLength"),e}(e),r=function(t){const e=new AbortController;if(t){if(!t.aborted){const onAbort=()=>e.abort();return t.addEventListener("abort",onAbort,{once:!0}),{controller:e,dispose:()=>t.removeEventListener("abort",onAbort)}}e.abort()}return{controller:e,dispose:()=>{}}}(o.signal),n={options:o,controller:r.controller,deadline:Date.now()+o.timeoutMs};try{return await runWithDeadline(n,async()=>{if("file"===o.inputKind){const e=await async function(t,e){const o=normalizeFilePath(t);if(!e.options.allowedRootDirectory)throw new K("FILE_INPUT_FORBIDDEN","File input requires allowedRootDirectory.");return async function(t,e){const o=_(t)?t:U(e,t),r=await L(o),n=V(e,r);if(n.startsWith("..")||""===n||"\\"===B&&/^[a-zA-Z]:/.test(n))throw new K("PATH_OUTSIDE_ROOT",`File path is outside the allowed root directory: ${t}.`);return r}(o,await async function(t){if(t.rootDirectory)return t.rootDirectory;const e=t.options.allowedRootDirectory;if(!e)throw new K("INVALID_OPTION","allowedRootDirectory is required for file operations.");const o=U(e),r=await L(o);if(!(await C(r)).isDirectory())throw new K("INVALID_OPTION",`allowedRootDirectory is not a directory: ${e}.`);return t.rootDirectory=r,r}(e))}(t,n),r=await async function(t,e,o,r){throwIfCancelled(o);const n=await S(t,"r");try{const o=await n.stat();if(o.size>e)throw new K(r,`File exceeds maximum allowed size of ${e} bytes: ${t}.`);const i=Buffer.alloc(o.size);return await n.read(i,0,o.size,0),i.toString("utf8")}finally{await n.close()}}(e,o.maxInputBytes,n,"INPUT_FILE_TOO_LARGE");return validateRawContent(r,{...n,options:{...o,baseFilePath:e}})}return assertByteLength(t,o.maxInputBytes,"INPUT_TOO_LARGE"),validateRawContent(t,n)})}catch(t){throw function(t){if(t instanceof K)return t;return new K("UNKNOWN_ERROR",`OpenAPI validation failed: ${getErrorMessage5(t)}.`,t)}(t)}finally{r.dispose(),n.controller.abort()}}async function validateOpenAPIFile(t,e={}){return validateOpenAPISpec(t,{...e,inputKind:"file",baseFilePath:t})}async function validateRawContent(t,e){throwIfCancelled(e),assertByteLength(t,e.options.maxInputBytes,"INPUT_TOO_LARGE");const o=function(t){try{const e=j(t);if(null===e||"object"!=typeof e||Array.isArray(e))throw new K("INVALID_DOCUMENT_SHAPE","OpenAPI document must be a JSON object.");return e}catch(t){if(t instanceof K)throw t;throw new K("PARSE_ERROR",`Failed to parse OpenAPI document: ${getErrorMessage5(t)}.`,t)}}(t);!function(t,e){let o=0;const r=[{value:t,depth:0}];for(;r.length>0;){const{value:t,depth:n}=r.pop();if(n>e.maxDocumentDepth)throw new K("DOCUMENT_TOO_DEEP",`Document exceeds maximum nesting depth of ${e.maxDocumentDepth}.`);if(o++,o>e.maxDocumentNodes)throw new K("DOCUMENT_TOO_LARGE",`Document exceeds maximum node count of ${e.maxDocumentNodes}.`);if(Array.isArray(t))for(const e of t)r.push({value:e,depth:n+1});else if(null!==t&&"object"==typeof t)for(const e of Object.values(t))r.push({value:e,depth:n+1})}}(o,e.options),function(t){const e=t.openapi??t.swagger;if("string"!=typeof e||0===e.length)throw new K("UNSUPPORTED_VERSION","Document must declare an openapi or swagger version.");const o=Number.parseInt(e.split(".")[0]??"",10);if(!Number.isFinite(o)||2!==o&&3!==o)throw new K("UNSUPPORTED_VERSION",`Unsupported OpenAPI/Swagger version: ${e}. Supported: 2.x, 3.x.`)}(o);const r=await runWithDeadline(e,()=>async function(){return z??=import("@powerduck/openapi-parser").then(t=>t.validate),z}()),n=await runWithDeadline(e,()=>r(o,{throwOnError:!1}));if(!n.valid)throw new K("SPEC_VALIDATION_FAILED",`OpenAPI validation failed: ${function(t,e,o){const r=t.slice(0,e).map(t=>{const e=t.instancePath??"",r=t.message??"Unknown error",n=e?`${e}: ${r}`:r;return n.length>o?`${n.slice(0,o)}...`:n});t.length>e&&r.push(`... and ${t.length-e} more errors`);return r.join("; ")}(n.errors??[],e.options.maxValidationErrors,e.options.maxErrorMessageLength)}`);return n.specification}function assertPositiveFiniteNumber(t,e){if(!Number.isFinite(t)||t<=0)throw new K("INVALID_OPTION",`${e} must be a positive finite number, got: ${String(t)}.`)}async function runWithDeadline(t,e){throwIfCancelled(t);const o=await Promise.race([e(),createDeadlinePromise(t)]);return throwIfCancelled(t),o}function createDeadlinePromise(t){return new Promise((e,o)=>{const r=t.deadline-Date.now(),n=Math.max(0,Math.min(r,2147483647)),i=setTimeout(()=>{o(new K("OPERATION_TIMEOUT",`OpenAPI validation operation timed out after ${t.options.timeoutMs}ms.`))},n);t.controller.signal.addEventListener("abort",()=>{clearTimeout(i),o(new K("OPERATION_ABORTED","OpenAPI validation operation was aborted."))},{once:!0})})}function throwIfCancelled(t){if(t.controller.signal.aborted)throw new K("OPERATION_ABORTED","OpenAPI validation operation was aborted.");if(Date.now()>t.deadline)throw new K("OPERATION_TIMEOUT",`OpenAPI validation operation timed out after ${t.options.timeoutMs}ms.`)}function assertByteLength(t,e,o){const r=Buffer.byteLength(t,"utf8");if(r>e)throw new K(o,`Input exceeds maximum allowed size of ${e} bytes (actual: ${r} bytes).`)}function getErrorMessage5(t){return t instanceof Error?t.message:String(t)}export{K as OpenApiValidationError,t as assertConfigFormat,e as assertNonEmptyString,o as assertPatchOperations,r as assertPatchPath,n as createError,deleteConfigValue,i as deleteContentValue,detectFormat,a as getErrorMessage,normalizeFilePath,patchConfigFile,s as patchContent,readConfigFile,releaseAllLocalLocks,setConfigValue,c as setContentValue,validateOpenAPIFile,validateOpenAPISpec,withFileLock,writeConfigFile};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powerduck/conf-patch",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "Two-layer configuration editor: pure core for patching JSON/JSONC/YAML strings (browser-safe), plus file layer with atomic writes and locking for Node.js/Electron. RFC 6902 JSON Patch, comment and formatting preservation, OpenAPI validation.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|