@danypops/vehicle-core 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/atomic-json.d.ts +9 -2
- package/dist/atomic-json.js +3 -3
- package/package.json +1 -1
- package/src/atomic-json.ts +13 -5
package/dist/atomic-json.d.ts
CHANGED
|
@@ -18,11 +18,18 @@
|
|
|
18
18
|
* happens).
|
|
19
19
|
*/
|
|
20
20
|
export interface AtomicJsonFsAdapter {
|
|
21
|
-
|
|
21
|
+
/** `mode` is a POSIX file-mode bitmask (e.g. 0o600); omitted means the platform/adapter's own default. */
|
|
22
|
+
writeFile(path: string, data: string, mode?: number): Promise<void>;
|
|
22
23
|
rename(oldPath: string, newPath: string): Promise<void>;
|
|
23
24
|
unlink(path: string): Promise<void>;
|
|
24
25
|
readFile(path: string): Promise<string>;
|
|
25
26
|
}
|
|
27
|
+
export interface AtomicJsonWriteOptions {
|
|
28
|
+
/** POSIX file-mode bitmask for the written file (e.g. 0o600 for a secret/credential-adjacent file). Omitted means the adapter's own default. */
|
|
29
|
+
readonly mode?: number;
|
|
30
|
+
/** Pretty-prints with 2-space indentation (matching JSON.stringify(value, null, 2)) for a human-editable file. Defaults to false (compact). */
|
|
31
|
+
readonly pretty?: boolean;
|
|
32
|
+
}
|
|
26
33
|
export interface AtomicJsonWriterOptions {
|
|
27
34
|
readonly fs: AtomicJsonFsAdapter;
|
|
28
35
|
/** Defaults to Date.now. */
|
|
@@ -46,7 +53,7 @@ export interface AtomicJsonWriterOptions {
|
|
|
46
53
|
}
|
|
47
54
|
export interface AtomicJsonWriter {
|
|
48
55
|
/** Serializes `value` to JSON and writes it to `filePath` atomically (temp file + rename). */
|
|
49
|
-
write(filePath: string, value: unknown): Promise<void>;
|
|
56
|
+
write(filePath: string, value: unknown, options?: AtomicJsonWriteOptions): Promise<void>;
|
|
50
57
|
/** Reads and JSON.parses `filePath`. Returns undefined if the file doesn't exist (fs.readFile throws ENOENT); rethrows any other error. */
|
|
51
58
|
read(filePath: string): Promise<unknown | undefined>;
|
|
52
59
|
}
|
package/dist/atomic-json.js
CHANGED
|
@@ -61,10 +61,10 @@ export function createAtomicJsonWriter(options) {
|
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
return {
|
|
64
|
-
async write(filePath, value) {
|
|
64
|
+
async write(filePath, value, options) {
|
|
65
65
|
let serialized;
|
|
66
66
|
try {
|
|
67
|
-
serialized = JSON.stringify(value);
|
|
67
|
+
serialized = options?.pretty ? JSON.stringify(value, null, 2) : JSON.stringify(value);
|
|
68
68
|
}
|
|
69
69
|
catch (error) {
|
|
70
70
|
throw new Error(`atomic-json: value for ${filePath} is not JSON-serializable`, { cause: error });
|
|
@@ -73,7 +73,7 @@ export function createAtomicJsonWriter(options) {
|
|
|
73
73
|
throw new Error(`atomic-json: value for ${filePath} is not JSON-serializable`);
|
|
74
74
|
const { dir, base } = dirAndBase(filePath);
|
|
75
75
|
const tempPath = `${dir}/.${base}.${pid()}.${now()}.${random()}.tmp`;
|
|
76
|
-
await fsAdapter.writeFile(tempPath, serialized);
|
|
76
|
+
await fsAdapter.writeFile(tempPath, serialized, options?.mode);
|
|
77
77
|
try {
|
|
78
78
|
await renameWithRetry(tempPath, filePath);
|
|
79
79
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@danypops/vehicle-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Vehicle's runtime-neutral wire contract: operation descriptors, schema codecs, failure shapes. Zero runtime dependencies, zero Bun-specific code -- the one thing every Vehicle client and server package depends on.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
package/src/atomic-json.ts
CHANGED
|
@@ -19,12 +19,20 @@
|
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
21
|
export interface AtomicJsonFsAdapter {
|
|
22
|
-
|
|
22
|
+
/** `mode` is a POSIX file-mode bitmask (e.g. 0o600); omitted means the platform/adapter's own default. */
|
|
23
|
+
writeFile(path: string, data: string, mode?: number): Promise<void>;
|
|
23
24
|
rename(oldPath: string, newPath: string): Promise<void>;
|
|
24
25
|
unlink(path: string): Promise<void>;
|
|
25
26
|
readFile(path: string): Promise<string>;
|
|
26
27
|
}
|
|
27
28
|
|
|
29
|
+
export interface AtomicJsonWriteOptions {
|
|
30
|
+
/** POSIX file-mode bitmask for the written file (e.g. 0o600 for a secret/credential-adjacent file). Omitted means the adapter's own default. */
|
|
31
|
+
readonly mode?: number;
|
|
32
|
+
/** Pretty-prints with 2-space indentation (matching JSON.stringify(value, null, 2)) for a human-editable file. Defaults to false (compact). */
|
|
33
|
+
readonly pretty?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
28
36
|
export interface AtomicJsonWriterOptions {
|
|
29
37
|
readonly fs: AtomicJsonFsAdapter;
|
|
30
38
|
/** Defaults to Date.now. */
|
|
@@ -49,7 +57,7 @@ export interface AtomicJsonWriterOptions {
|
|
|
49
57
|
|
|
50
58
|
export interface AtomicJsonWriter {
|
|
51
59
|
/** Serializes `value` to JSON and writes it to `filePath` atomically (temp file + rename). */
|
|
52
|
-
write(filePath: string, value: unknown): Promise<void>;
|
|
60
|
+
write(filePath: string, value: unknown, options?: AtomicJsonWriteOptions): Promise<void>;
|
|
53
61
|
/** Reads and JSON.parses `filePath`. Returns undefined if the file doesn't exist (fs.readFile throws ENOENT); rethrows any other error. */
|
|
54
62
|
read(filePath: string): Promise<unknown | undefined>;
|
|
55
63
|
}
|
|
@@ -102,17 +110,17 @@ export function createAtomicJsonWriter(options: AtomicJsonWriterOptions): Atomic
|
|
|
102
110
|
}
|
|
103
111
|
|
|
104
112
|
return {
|
|
105
|
-
async write(filePath, value) {
|
|
113
|
+
async write(filePath, value, options) {
|
|
106
114
|
let serialized: string | undefined;
|
|
107
115
|
try {
|
|
108
|
-
serialized = JSON.stringify(value);
|
|
116
|
+
serialized = options?.pretty ? JSON.stringify(value, null, 2) : JSON.stringify(value);
|
|
109
117
|
} catch (error) {
|
|
110
118
|
throw new Error(`atomic-json: value for ${filePath} is not JSON-serializable`, { cause: error });
|
|
111
119
|
}
|
|
112
120
|
if (serialized === undefined) throw new Error(`atomic-json: value for ${filePath} is not JSON-serializable`);
|
|
113
121
|
const { dir, base } = dirAndBase(filePath);
|
|
114
122
|
const tempPath = `${dir}/.${base}.${pid()}.${now()}.${random()}.tmp`;
|
|
115
|
-
await fsAdapter.writeFile(tempPath, serialized);
|
|
123
|
+
await fsAdapter.writeFile(tempPath, serialized, options?.mode);
|
|
116
124
|
try {
|
|
117
125
|
await renameWithRetry(tempPath, filePath);
|
|
118
126
|
} catch (error) {
|