@bom-pi/core 0.1.0 → 0.1.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.
package/package.json CHANGED
@@ -1,21 +1,21 @@
1
1
  {
2
- "name": "@bom-pi/core",
3
- "version": "0.1.0",
4
- "description": "Core pairing machinery for Bom Pi: ceremonies, transports, durable stores",
5
- "type": "module",
6
- "license": "MIT",
7
- "dependencies": {
8
- "@bom-pi/protocol": "0.1.0"
9
- },
10
- "exports": {
11
- "./cloud-connector.ts": "./src/cloud-connector.ts",
12
- "./cloud-wss-transport.ts": "./src/cloud-wss-transport.ts",
13
- "./connector-store.ts": "./src/connector-store.ts",
14
- "./device-ceremony.ts": "./src/device-ceremony.ts",
15
- "./ingress-receiver.ts": "./src/ingress-receiver.ts",
16
- "./lan-pairing.ts": "./src/lan-pairing.ts",
17
- "./pairing-storage.ts": "./src/pairing-storage.ts",
18
- "./secure-transport.ts": "./src/secure-transport.ts",
19
- "./session-gateway.ts": "./src/session-gateway.ts"
20
- }
2
+ "name": "@bom-pi/core",
3
+ "version": "0.1.2",
4
+ "description": "Core pairing machinery for Bom Pi: ceremonies, transports, durable stores",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "dependencies": {
8
+ "@bom-pi/protocol": "0.1.0"
9
+ },
10
+ "exports": {
11
+ "./cloud-connector.ts": "./src/cloud-connector.ts",
12
+ "./cloud-wss-transport.ts": "./src/cloud-wss-transport.ts",
13
+ "./connector-store.ts": "./src/connector-store.ts",
14
+ "./device-ceremony.ts": "./src/device-ceremony.ts",
15
+ "./ingress-receiver.ts": "./src/ingress-receiver.ts",
16
+ "./lan-pairing.ts": "./src/lan-pairing.ts",
17
+ "./pairing-storage.ts": "./src/pairing-storage.ts",
18
+ "./secure-transport.ts": "./src/secure-transport.ts",
19
+ "./session-gateway.ts": "./src/session-gateway.ts"
20
+ }
21
21
  }
@@ -92,6 +92,15 @@ function ownerMismatch(stat: Stats): boolean {
92
92
  return process.getuid !== undefined && stat.uid !== process.getuid();
93
93
  }
94
94
 
95
+ /** POSIX mode bits are this store's privacy boundary on POSIX hosts. Windows stat carries no
96
+ * such bits (a writable file reads 0o666); there the boundary is the user profile's ACL, and
97
+ * the uid/ownership checks above still apply wherever uid exists. */
98
+ const POSIX_MODES = process.platform !== "win32";
99
+
100
+ function privateMode(stat: Stats, mode: number): boolean {
101
+ return !POSIX_MODES || (stat.mode & 0o777) === mode;
102
+ }
103
+
95
104
  /** Slot names are derived only from the validated fingerprint: exact, collision-free and free of
96
105
  * path metacharacters. A store instance is bound to exactly one pinned cloud forever. */
97
106
  function slotPaths(
@@ -282,7 +291,7 @@ function parseRecord(text: string, pinned: PinnedIdentity): ConnectorSnapshot {
282
291
  function assertRecordFile(stat: Stats): void {
283
292
  if (
284
293
  !stat.isFile() ||
285
- (stat.mode & 0o777) !== 0o600 ||
294
+ !privateMode(stat, 0o600) ||
286
295
  ownerMismatch(stat) ||
287
296
  stat.size > MAX_RECORD_BYTES
288
297
  ) {
@@ -293,7 +302,7 @@ function assertRecordFile(stat: Stats): void {
293
302
  async function checkDirectory(directory: string): Promise<void> {
294
303
  const stat = await lstat(directory).catch(() => fail("missing store directory"));
295
304
 
296
- if (!stat.isDirectory() || (stat.mode & 0o777) !== 0o700) {
305
+ if (!stat.isDirectory() || !privateMode(stat, 0o700)) {
297
306
  fail("store directory must be private (0700) and not a symlink");
298
307
  }
299
308
 
@@ -303,6 +312,12 @@ async function checkDirectory(directory: string): Promise<void> {
303
312
  }
304
313
 
305
314
  async function syncDirectory(directory: string): Promise<void> {
315
+ // Windows cannot open a directory for fsync (EPERM); there the atomic rename itself is the
316
+ // durability step, as in every other Windows-native atomic writer. POSIX keeps the fsync.
317
+ if (process.platform === "win32") {
318
+ return;
319
+ }
320
+
306
321
  const handle = await open(directory, "r");
307
322
 
308
323
  try {
@@ -385,7 +400,7 @@ async function assertLock(directory: string, lockPath: string, lock: LockLease):
385
400
  if (
386
401
  !owned.isFile() ||
387
402
  !present.isFile() ||
388
- (present.mode & 0o777) !== 0o600 ||
403
+ !privateMode(present, 0o600) ||
389
404
  owned.dev !== present.dev ||
390
405
  owned.ino !== present.ino ||
391
406
  owned.nlink !== 1 ||
@@ -529,6 +544,47 @@ export class FileConnectorStore implements ConnectorStore {
529
544
  return store;
530
545
  }
531
546
 
547
+ /** open() when a record exists, else create() — the one-shot enroll lane's provisioning. It
548
+ * tolerates exactly one leftover: a directory holding no record for this pin (an aborted
549
+ * create: mkdir ran, the record never landed — crash, power loss, or a platform check that
550
+ * refused mid-way). That orphan is removed and re-provisioned; a directory with any record,
551
+ * for this pin or another, is still never re-initialized over. */
552
+ static async openOrProvision(
553
+ directory: string,
554
+ pinned: PinnedCloud,
555
+ options?: FileConnectorStoreOptions,
556
+ ): Promise<FileConnectorStore> {
557
+ try {
558
+ return await FileConnectorStore.open(directory, pinned, options);
559
+ } catch (error) {
560
+ if (
561
+ !(error instanceof Error) ||
562
+ !/missing (?:store directory|record)/.test(error.message)
563
+ ) {
564
+ throw error;
565
+ }
566
+ }
567
+
568
+ try {
569
+ return await FileConnectorStore.create(directory, pinned, options);
570
+ } catch (error) {
571
+ if (!(error instanceof Error) || !/already exists/.test(error.message)) {
572
+ throw error;
573
+ }
574
+
575
+ const slot = slotPaths(directory, pinned);
576
+ const record = await lstat(slot.recordPath).catch(() => null);
577
+
578
+ if (record !== null) {
579
+ throw error;
580
+ }
581
+
582
+ await rm(directory, { recursive: true, force: true });
583
+
584
+ return await FileConnectorStore.create(directory, pinned, options);
585
+ }
586
+ }
587
+
532
588
  /** Opens an EXISTING store: the directory must pre-exist as a private 0700 directory owned by
533
589
  * the current user, and the record must be present, private (0600), structurally valid and
534
590
  * bound to exactly this pinned cloud. A missing record is refused (never silently treated as a
@@ -1893,7 +1893,8 @@ export class DevicePairBroker {
1893
1893
 
1894
1894
  record.state = decision === "approve" ? "approved" : "denied";
1895
1895
  record.decision = decision;
1896
- record.approvedExpiry = decision === "approve" ? approvedExpiryOf(input.decision) : null;
1896
+ record.approvedExpiry =
1897
+ decision === "approve" ? approvedExpiryOf(input.decision) : null;
1897
1898
  record.decisionReceipt = receipt;
1898
1899
  record.decisionBytes = Buffer.from(
1899
1900
  canonicalJson(input.decision as unknown as CanonicalValue),