@danypops/vehicle-core 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,11 +18,20 @@
18
18
  * happens).
19
19
  */
20
20
  export interface AtomicJsonFsAdapter {
21
- writeFile(path: string, data: string): Promise<void>;
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
+ /** Appends a trailing "\n" -- the common POSIX text-file convention. Defaults to false (exact JSON.stringify output, unchanged). */
33
+ readonly trailingNewline?: boolean;
34
+ }
26
35
  export interface AtomicJsonWriterOptions {
27
36
  readonly fs: AtomicJsonFsAdapter;
28
37
  /** Defaults to Date.now. */
@@ -46,7 +55,7 @@ export interface AtomicJsonWriterOptions {
46
55
  }
47
56
  export interface AtomicJsonWriter {
48
57
  /** Serializes `value` to JSON and writes it to `filePath` atomically (temp file + rename). */
49
- write(filePath: string, value: unknown): Promise<void>;
58
+ write(filePath: string, value: unknown, options?: AtomicJsonWriteOptions): Promise<void>;
50
59
  /** Reads and JSON.parses `filePath`. Returns undefined if the file doesn't exist (fs.readFile throws ENOENT); rethrows any other error. */
51
60
  read(filePath: string): Promise<unknown | undefined>;
52
61
  }
@@ -61,19 +61,21 @@ 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 });
71
71
  }
72
72
  if (serialized === undefined)
73
73
  throw new Error(`atomic-json: value for ${filePath} is not JSON-serializable`);
74
+ if (options?.trailingNewline)
75
+ serialized += "\n";
74
76
  const { dir, base } = dirAndBase(filePath);
75
77
  const tempPath = `${dir}/.${base}.${pid()}.${now()}.${random()}.tmp`;
76
- await fsAdapter.writeFile(tempPath, serialized);
78
+ await fsAdapter.writeFile(tempPath, serialized, options?.mode);
77
79
  try {
78
80
  await renameWithRetry(tempPath, filePath);
79
81
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danypops/vehicle-core",
3
- "version": "0.5.0",
3
+ "version": "0.6.1",
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",
@@ -19,12 +19,22 @@
19
19
  */
20
20
 
21
21
  export interface AtomicJsonFsAdapter {
22
- writeFile(path: string, data: string): Promise<void>;
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
+ /** Appends a trailing "\n" -- the common POSIX text-file convention. Defaults to false (exact JSON.stringify output, unchanged). */
35
+ readonly trailingNewline?: boolean;
36
+ }
37
+
28
38
  export interface AtomicJsonWriterOptions {
29
39
  readonly fs: AtomicJsonFsAdapter;
30
40
  /** Defaults to Date.now. */
@@ -49,7 +59,7 @@ export interface AtomicJsonWriterOptions {
49
59
 
50
60
  export interface AtomicJsonWriter {
51
61
  /** Serializes `value` to JSON and writes it to `filePath` atomically (temp file + rename). */
52
- write(filePath: string, value: unknown): Promise<void>;
62
+ write(filePath: string, value: unknown, options?: AtomicJsonWriteOptions): Promise<void>;
53
63
  /** Reads and JSON.parses `filePath`. Returns undefined if the file doesn't exist (fs.readFile throws ENOENT); rethrows any other error. */
54
64
  read(filePath: string): Promise<unknown | undefined>;
55
65
  }
@@ -102,17 +112,18 @@ export function createAtomicJsonWriter(options: AtomicJsonWriterOptions): Atomic
102
112
  }
103
113
 
104
114
  return {
105
- async write(filePath, value) {
115
+ async write(filePath, value, options) {
106
116
  let serialized: string | undefined;
107
117
  try {
108
- serialized = JSON.stringify(value);
118
+ serialized = options?.pretty ? JSON.stringify(value, null, 2) : JSON.stringify(value);
109
119
  } catch (error) {
110
120
  throw new Error(`atomic-json: value for ${filePath} is not JSON-serializable`, { cause: error });
111
121
  }
112
122
  if (serialized === undefined) throw new Error(`atomic-json: value for ${filePath} is not JSON-serializable`);
123
+ if (options?.trailingNewline) serialized += "\n";
113
124
  const { dir, base } = dirAndBase(filePath);
114
125
  const tempPath = `${dir}/.${base}.${pid()}.${now()}.${random()}.tmp`;
115
- await fsAdapter.writeFile(tempPath, serialized);
126
+ await fsAdapter.writeFile(tempPath, serialized, options?.mode);
116
127
  try {
117
128
  await renameWithRetry(tempPath, filePath);
118
129
  } catch (error) {