@stonyx/utils 0.2.3-alpha.24 → 0.2.3-alpha.25

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 CHANGED
@@ -75,10 +75,12 @@ Creates a file at the given path.
75
75
 
76
76
  #### `updateFile(filePath, data, options={})`
77
77
 
78
- Updates a file atomically by writing to a temporary file first.
78
+ Updates a file atomically by writing to a sibling swap file first, then renaming it over the target. Throws if the file does not exist.
79
79
 
80
80
  * `options.json` — boolean, serialize as JSON.
81
81
 
82
+ **Concurrency: last writer wins.** Concurrent `updateFile` calls on the same path are safe — each uses a swap file unique to its process and call, so neither caller can throw `ENOENT` or persist the other's bytes — but they are not serialized. The surviving value is whichever caller renames last.
83
+
82
84
  #### `copyFile(sourcePath, targetPath, options={})`
83
85
 
84
86
  Copies a file from source to target.
@@ -245,6 +247,8 @@ Prompts the user with `(y/N)` and resolves to `true` only if the answer is `"y"`
245
247
  * `options.input` — Readable stream (default: `process.stdin`).
246
248
  * `options.output` — Writable stream (default: `process.stdout`).
247
249
 
250
+ **TTY required:** Rejects with an error if `process.stdin` is not a TTY and no custom `input` stream is provided. For headless/container deployments, use `autoMigrate` config or provide a custom input stream.
251
+
248
252
  #### `prompt(question, options={})`
249
253
 
250
254
  Prompts the user with a question and resolves to the trimmed input string.
@@ -252,6 +256,8 @@ Prompts the user with a question and resolves to the trimmed input string.
252
256
  * `options.input` — Readable stream (default: `process.stdin`).
253
257
  * `options.output` — Writable stream (default: `process.stdout`).
254
258
 
259
+ **TTY required:** Rejects with an error if `process.stdin` is not a TTY and no custom `input` stream is provided.
260
+
255
261
  ```js
256
262
  import { confirm, prompt } from '@stonyx/utils/prompt';
257
263
 
package/dist/file.d.ts CHANGED
@@ -23,6 +23,22 @@ interface FileImportMeta {
23
23
  path: string;
24
24
  }
25
25
  export declare function createFile(filePath: string, data: string | Record<string, unknown>, options?: FileOptions): Promise<void>;
26
+ /**
27
+ * Atomically replace the contents of an existing file.
28
+ *
29
+ * Writes to a swap file that is a **sibling** of the target — so `rename` stays
30
+ * on one filesystem and therefore stays atomic — then renames it over the
31
+ * target. The swap name carries `process.pid` and a UUID rather than a
32
+ * timestamp: a whole-second token collided between concurrent callers, which
33
+ * made one caller throw `ENOENT` and the other silently persist bytes it had
34
+ * not written (abofs/stonyx-utils#44).
35
+ *
36
+ * Concurrency contract: **last writer wins**. Unique swap names remove the
37
+ * `ENOENT` and the byte-level clobber, but overlapping calls on one path still
38
+ * race on which value lands last. `updateFile` does not serialise, and callers
39
+ * needing a serialisation guarantee must supply their own — an in-process queue
40
+ * would not provide one across processes anyway.
41
+ */
26
42
  export declare function updateFile(filePath: string, data: string | Record<string, unknown>, options?: FileOptions): Promise<void>;
27
43
  interface CopyFileOptions {
28
44
  overwrite?: boolean;
package/dist/file.js CHANGED
@@ -1,9 +1,9 @@
1
- import { getTimestamp } from './date.js';
2
1
  import { kebabCaseToCamelCase } from './string.js';
3
2
  import { objToJson } from './object.js';
4
3
  import fs from 'fs';
5
4
  import { promises as fsp } from 'fs';
6
5
  import path from 'path';
6
+ import { randomUUID } from 'crypto';
7
7
  function isNodeError(error) {
8
8
  return error instanceof Error && 'code' in error;
9
9
  }
@@ -17,12 +17,39 @@ export async function createFile(filePath, data, options = {}) {
17
17
  throw error instanceof Error ? error : new Error(String(error));
18
18
  }
19
19
  }
20
+ /**
21
+ * Atomically replace the contents of an existing file.
22
+ *
23
+ * Writes to a swap file that is a **sibling** of the target — so `rename` stays
24
+ * on one filesystem and therefore stays atomic — then renames it over the
25
+ * target. The swap name carries `process.pid` and a UUID rather than a
26
+ * timestamp: a whole-second token collided between concurrent callers, which
27
+ * made one caller throw `ENOENT` and the other silently persist bytes it had
28
+ * not written (abofs/stonyx-utils#44).
29
+ *
30
+ * Concurrency contract: **last writer wins**. Unique swap names remove the
31
+ * `ENOENT` and the byte-level clobber, but overlapping calls on one path still
32
+ * race on which value lands last. `updateFile` does not serialise, and callers
33
+ * needing a serialisation guarantee must supply their own — an in-process queue
34
+ * would not provide one across processes anyway.
35
+ */
20
36
  export async function updateFile(filePath, data, options = {}) {
21
37
  try {
22
38
  await fsp.access(filePath);
23
- const swapFile = `${filePath}.temp-${getTimestamp()}`;
24
- await fsp.writeFile(swapFile, options.json ? objToJson(data) : String(data), 'utf8');
25
- await fsp.rename(swapFile, filePath);
39
+ // pid + UUID, never a timestamp: the swap name is a uniqueness token, and
40
+ // whole seconds cannot discriminate between concurrent callers.
41
+ const swapFile = `${filePath}.temp-${process.pid}-${randomUUID()}`;
42
+ // `wx` turns any residual name collision into a loud EEXIST rather than a
43
+ // silent overwrite of another caller's swap bytes.
44
+ await fsp.writeFile(swapFile, options.json ? objToJson(data) : String(data), { encoding: 'utf8', flag: 'wx' });
45
+ try {
46
+ await fsp.rename(swapFile, filePath);
47
+ }
48
+ catch (renameError) {
49
+ // Leave no orphan behind, and never let the cleanup mask the real error.
50
+ await fsp.unlink(swapFile).catch(() => { });
51
+ throw renameError;
52
+ }
26
53
  }
27
54
  catch (error) {
28
55
  throw error instanceof Error ? error : new Error(String(error));
package/dist/prompt.js CHANGED
@@ -1,5 +1,9 @@
1
1
  import { createInterface } from 'readline';
2
2
  export function confirm(question, { input, output } = {}) {
3
+ if (!input && !process.stdin.isTTY) {
4
+ return Promise.reject(new Error('Interactive confirm() requires a TTY on stdin. ' +
5
+ 'For headless/container deployments, use the autoMigrate config option instead.'));
6
+ }
3
7
  const rl = createInterface({
4
8
  input: input ?? process.stdin,
5
9
  output: output ?? process.stdout,
@@ -12,6 +16,10 @@ export function confirm(question, { input, output } = {}) {
12
16
  });
13
17
  }
14
18
  export function prompt(question, { input, output } = {}) {
19
+ if (!input && !process.stdin.isTTY) {
20
+ return Promise.reject(new Error('Interactive prompt() requires a TTY on stdin. ' +
21
+ 'For headless/container deployments, configure non-interactive alternatives instead.'));
22
+ }
15
23
  const rl = createInterface({
16
24
  input: input ?? process.stdin,
17
25
  output: output ?? process.stdout,
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "keywords": [
4
4
  "stonyx-module"
5
5
  ],
6
- "version": "0.2.3-alpha.24",
6
+ "version": "0.2.3-alpha.25",
7
7
  "description": "Utils module for Stonyx Framework",
8
8
  "repository": {
9
9
  "type": "git",