@hadoku/service-kernel 0.1.228 → 0.1.230

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.
@@ -1,2 +1,13 @@
1
- /** Write JSON to `target` atomically: a temp file beside it, synced, then renamed over it. */
2
- export declare function atomicWriteJson(target: string, data: unknown): Promise<void>;
1
+ /**
2
+ * Writing a file atomically: a temp file beside the target (this process's own, never shared with
3
+ * another write), synced, then renamed over it — a reader, or a restart after a crash mid-write,
4
+ * sees the old file or the new one, never half of either.
5
+ */
6
+ export interface JsonWriteOptions {
7
+ /** One line, no indent: a file only code reads. Default: two-space indent. */
8
+ compact?: boolean;
9
+ }
10
+ export declare function atomicWriteFile(target: string, text: string): Promise<void>;
11
+ export declare function atomicWriteJson(target: string, data: unknown, opts?: JsonWriteOptions): Promise<void>;
12
+ /** {@link atomicWriteJson} for a caller that cannot wait: blocks until the file is in place. */
13
+ export declare function atomicWriteJsonSync(target: string, data: unknown, opts?: JsonWriteOptions): void;
@@ -1,13 +1,20 @@
1
- /** Write JSON to `target` atomically: a temp file beside it, synced, then renamed over it. */
1
+ /**
2
+ * Writing a file atomically: a temp file beside the target (this process's own, never shared with
3
+ * another write), synced, then renamed over it — a reader, or a restart after a crash mid-write,
4
+ * sees the old file or the new one, never half of either.
5
+ */
2
6
  import { randomUUID } from 'node:crypto';
7
+ import { closeSync, fsyncSync, mkdirSync, openSync, renameSync, writeFileSync } from 'node:fs';
3
8
  import { mkdir, open, rename } from 'node:fs/promises';
4
- import { join } from 'node:path';
5
- export async function atomicWriteJson(target, data) {
6
- await mkdir(join(target, '..'), { recursive: true });
7
- const tmp = `${target}.${process.pid}.${randomUUID()}.tmp`;
9
+ import { dirname } from 'node:path';
10
+ const tempOf = (target) => `${target}.${process.pid}.${randomUUID()}.tmp`;
11
+ const jsonOf = (data, opts) => (opts.compact === true ? JSON.stringify(data) : JSON.stringify(data, null, 2));
12
+ export async function atomicWriteFile(target, text) {
13
+ await mkdir(dirname(target), { recursive: true });
14
+ const tmp = tempOf(target);
8
15
  const handle = await open(tmp, 'w');
9
16
  try {
10
- await handle.writeFile(JSON.stringify(data, null, 2));
17
+ await handle.writeFile(text);
11
18
  await handle.sync();
12
19
  }
13
20
  finally {
@@ -15,3 +22,20 @@ export async function atomicWriteJson(target, data) {
15
22
  }
16
23
  await rename(tmp, target);
17
24
  }
25
+ export function atomicWriteJson(target, data, opts = {}) {
26
+ return atomicWriteFile(target, jsonOf(data, opts));
27
+ }
28
+ /** {@link atomicWriteJson} for a caller that cannot wait: blocks until the file is in place. */
29
+ export function atomicWriteJsonSync(target, data, opts = {}) {
30
+ mkdirSync(dirname(target), { recursive: true });
31
+ const tmp = tempOf(target);
32
+ const fd = openSync(tmp, 'w');
33
+ try {
34
+ writeFileSync(fd, jsonOf(data, opts));
35
+ fsyncSync(fd);
36
+ }
37
+ finally {
38
+ closeSync(fd);
39
+ }
40
+ renameSync(tmp, target);
41
+ }
@@ -1 +1 @@
1
- export { atomicWriteJson } from './atomic-write.js';
1
+ export { atomicWriteFile, atomicWriteJson, atomicWriteJsonSync, type JsonWriteOptions } from './atomic-write.js';
@@ -1 +1 @@
1
- export { atomicWriteJson } from "./atomic-write.js";
1
+ export { atomicWriteFile, atomicWriteJson, atomicWriteJsonSync } from "./atomic-write.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/service-kernel",
3
- "version": "0.1.228",
4
- "gitHead": "43473a09e2f2d07dd684b2aa45ac5c250e145d50",
3
+ "version": "0.1.230",
4
+ "gitHead": "20683bcbf5bb6a1f9afba2273dae40f2964aaec9",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "description": "The server parts any game keeps: edge auth and the tier gate, base path and bundle check, health and the busy probe, lifecycle, the JSON logger, the state directory, telemetry and capacity, per-caller limits, the browser relays, the erase registry, and createGameServer, which boots a game from its config and plugins. Knows no game; the app binds its service id, prefix and env names. MIT; imports only node built-ins, express, socket.io, platform-wire and @wolffm/*.",
@@ -56,7 +56,7 @@
56
56
  }
57
57
  },
58
58
  "dependencies": {
59
- "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.195",
59
+ "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.197",
60
60
  "express": "^5.1.0",
61
61
  "socket.io": "^4.8.3"
62
62
  },