@wrongstack/persistence 0.316.2 → 0.317.2

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.
Files changed (2) hide show
  1. package/dist/index.js +40 -16
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -35,8 +35,8 @@ function createPersistencePrimitives(options = {}) {
35
35
  let mode;
36
36
  let existing;
37
37
  try {
38
- const stat2 = await fs.stat(targetPath);
39
- existing = stat2.mode & 511;
38
+ const stat3 = await fs.stat(targetPath);
39
+ existing = stat3.mode & 511;
40
40
  } catch {
41
41
  existing = void 0;
42
42
  }
@@ -139,10 +139,10 @@ function createPersistencePrimitives(options = {}) {
139
139
  throw error;
140
140
  }
141
141
  try {
142
- const stat2 = await fs.stat(lockPath);
143
- if (Date.now() - stat2.mtimeMs > staleMs) {
142
+ const stat3 = await fs.stat(lockPath);
143
+ if (Date.now() - stat3.mtimeMs > staleMs) {
144
144
  const recheck = await fs.stat(lockPath).catch(() => void 0);
145
- if (recheck && recheck.mtimeMs === stat2.mtimeMs && Date.now() - recheck.mtimeMs > staleMs) {
145
+ if (recheck && recheck.mtimeMs === stat3.mtimeMs && Date.now() - recheck.mtimeMs > staleMs) {
146
146
  await fs.unlink(lockPath).catch(() => void 0);
147
147
  }
148
148
  await retryAfterBackoff();
@@ -258,7 +258,19 @@ var ensureDir = defaultPrimitives.ensureDir;
258
258
  var withFileLock = defaultPrimitives.withFileLock;
259
259
 
260
260
  // src/file-permissions.ts
261
- import { chmod as chmod2 } from "node:fs/promises";
261
+ import * as childProcess from "node:child_process";
262
+ import { chmod as chmod2, stat as stat2 } from "node:fs/promises";
263
+ import { promisify } from "node:util";
264
+ var _execFileAsync;
265
+ function getExecFileAsync() {
266
+ if (!_execFileAsync) {
267
+ const fn = childProcess.execFile;
268
+ if (typeof fn === "function") {
269
+ _execFileAsync = promisify(fn);
270
+ }
271
+ }
272
+ return _execFileAsync;
273
+ }
262
274
  var SECRET_FILE_MODE = 384;
263
275
  var SECRET_DIR_MODE = 448;
264
276
  async function restrictFilePermissions(filePath, opts) {
@@ -266,9 +278,6 @@ async function restrictFilePermissions(filePath, opts) {
266
278
  const warn = opts?.warn ?? ((msg) => console.warn(msg));
267
279
  if (process.platform === "win32") {
268
280
  try {
269
- const { execFile } = await import("node:child_process");
270
- const { promisify } = await import("node:util");
271
- const execFileAsync = promisify(execFile);
272
281
  const user = windowsAccountName();
273
282
  if (!user) {
274
283
  warn(
@@ -276,12 +285,19 @@ async function restrictFilePermissions(filePath, opts) {
276
285
  );
277
286
  return;
278
287
  }
279
- await execFileAsync("icacls", [filePath, "/inheritance:r", "/grant:r", `${user}:(F)`], {
288
+ const execFn = getExecFileAsync();
289
+ if (!execFn) {
290
+ warn(`[${label}] child_process.execFile unavailable on this platform.`);
291
+ return;
292
+ }
293
+ await execFn("icacls", [filePath, "/inheritance:r", "/grant:r", `${user}:(F)`], {
280
294
  windowsHide: true
281
295
  });
282
- } catch {
296
+ } catch (error) {
297
+ if (await pathIsGone(filePath)) return;
298
+ const reason = error instanceof Error ? error.message : String(error);
283
299
  warn(
284
- `[${label}] Could not restrict permissions on ${filePath} \u2014 it may be readable by other users on this system.`
300
+ `[${label}] Could not restrict permissions on ${filePath} (${reason}) \u2014 it may be readable by other users on this system.`
285
301
  );
286
302
  }
287
303
  } else {
@@ -291,6 +307,14 @@ async function restrictFilePermissions(filePath, opts) {
291
307
  }
292
308
  }
293
309
  }
310
+ async function pathIsGone(filePath) {
311
+ try {
312
+ await stat2(filePath);
313
+ return false;
314
+ } catch {
315
+ return true;
316
+ }
317
+ }
294
318
  async function restrictDirPermissions(dirPath, opts) {
295
319
  if (process.platform === "win32") {
296
320
  await restrictFilePermissions(dirPath, opts);
@@ -302,9 +326,9 @@ async function restrictDirPermissions(dirPath, opts) {
302
326
  }
303
327
  }
304
328
  function windowsAccountName() {
305
- const username = process.env.USERNAME || process.env.USER;
329
+ const username = process.env["USERNAME"] ?? process.env["USER"];
306
330
  if (!username || username.includes("\0")) return void 0;
307
- const domain = process.env.USERDOMAIN;
331
+ const domain = process.env["USERDOMAIN"];
308
332
  if (domain && !domain.includes("\0")) return `${domain}\\${username}`;
309
333
  return username;
310
334
  }
@@ -320,10 +344,10 @@ function unixSocketPathLimit(platform = process.platform) {
320
344
  return 107;
321
345
  }
322
346
  function checkUnixSocketPath(socketPath, platform = process.platform) {
323
- const byteLength = Buffer.byteLength(socketPath, "utf8");
324
347
  if (platform === "win32") {
325
- return { ok: true, byteLength, maxBytes: Number.MAX_SAFE_INTEGER };
348
+ return { ok: true, byteLength: socketPath.length, maxBytes: Number.MAX_SAFE_INTEGER };
326
349
  }
350
+ const byteLength = Buffer.byteLength(socketPath, "utf8");
327
351
  const maxBytes = unixSocketPathLimit(platform);
328
352
  return { ok: byteLength <= maxBytes, byteLength, maxBytes };
329
353
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wrongstack/persistence",
3
- "version": "0.316.2",
3
+ "version": "0.317.2",
4
4
  "license": "MIT",
5
5
  "description": "Dependency-free filesystem persistence primitives shared across WrongStack packages.",
6
6
  "repository": {