@tool-bridge/server 0.26.2 → 0.28.0

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/dist/admin.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import {
3
3
  BootstrapStateStore,
4
4
  package_default
5
- } from "./chunk-KTWFYFDL.js";
5
+ } from "./chunk-A5VV3P2S.js";
6
6
  import {
7
7
  httpsReleaseUrl,
8
8
  pluginImageSchema
@@ -25,6 +25,11 @@ var stateSchema = z.strictObject({
25
25
  port: z.number().int().min(0).max(65535).default(8787),
26
26
  host: z.string().default("0.0.0.0")
27
27
  });
28
+ var BootstrapLockBusyError = class extends TBError {
29
+ constructor() {
30
+ super("conflict", "another local maintenance operation is running");
31
+ }
32
+ };
28
33
  var BootstrapStateStore = class {
29
34
  constructor(directory) {
30
35
  this.directory = directory;
@@ -41,7 +46,7 @@ var BootstrapStateStore = class {
41
46
  update: 1e4
42
47
  }).catch((error) => {
43
48
  if (error.code === "ELOCKED") {
44
- throw new TBError("conflict", "another local maintenance operation is running");
49
+ throw new BootstrapLockBusyError();
45
50
  }
46
51
  throw error;
47
52
  });
@@ -78,38 +83,45 @@ var BootstrapStateStore = class {
78
83
  return value === void 0 ? void 0 : stateSchema.parse(value);
79
84
  }
80
85
  async initialize() {
81
- return this.exclusive(async () => {
82
- const current = await this.state();
83
- if (current) {
84
- if (await this.read("initialized.json"))
85
- return { ...current, phase: "initialized" };
86
- return current;
87
- }
88
- if (await this.read("identity.json"))
89
- throw new TBError(
90
- "unavailable",
91
- "bootstrap state requires local recovery"
92
- );
93
- const state = {
94
- instanceId: randomUUID(),
95
- revision: 1,
96
- phase: "setup",
97
- port: 8787,
98
- host: "0.0.0.0"
99
- };
100
- await this.write("identity.json", { instanceId: state.instanceId });
101
- await this.write("keys.json", {
102
- keyring: createEncryptionKeyring(randomBytes(32).toString("base64url")),
103
- storeTokenKeyring: createEncryptionKeyring(
104
- randomBytes(32).toString("base64url")
105
- ),
106
- oauthKey: randomBytes(32).toString("base64url"),
107
- adminSk: `tb_sk_${randomBytes(32).toString("base64url")}`
108
- });
109
- await this.write("bootstrap.json", state);
110
- await this.pair(state.instanceId, "setup");
111
- return state;
112
- }, 5);
86
+ return this.exclusive(() => this.readOrInitialize(), 5);
87
+ }
88
+ /** Keep bootstrap reads, including the first initialization, locked through admission. */
89
+ async withStartupState(initializedInstanceId, admit) {
90
+ return this.exclusive(async () => admit(await this.readOrInitialize(initializedInstanceId)));
91
+ }
92
+ async readOrInitialize(initializedInstanceId) {
93
+ const current = await this.state();
94
+ if (initializedInstanceId && current?.instanceId !== initializedInstanceId)
95
+ throw new TBError("conflict", "bootstrap identity changed during handoff");
96
+ if (current) {
97
+ if (initializedInstanceId || await this.read("initialized.json"))
98
+ return { ...current, phase: "initialized" };
99
+ return current;
100
+ }
101
+ if (await this.read("identity.json"))
102
+ throw new TBError(
103
+ "unavailable",
104
+ "bootstrap state requires local recovery"
105
+ );
106
+ const state = {
107
+ instanceId: randomUUID(),
108
+ revision: 1,
109
+ phase: "setup",
110
+ port: 8787,
111
+ host: "0.0.0.0"
112
+ };
113
+ await this.write("identity.json", { instanceId: state.instanceId });
114
+ await this.write("keys.json", {
115
+ keyring: createEncryptionKeyring(randomBytes(32).toString("base64url")),
116
+ storeTokenKeyring: createEncryptionKeyring(
117
+ randomBytes(32).toString("base64url")
118
+ ),
119
+ oauthKey: randomBytes(32).toString("base64url"),
120
+ adminSk: `tb_sk_${randomBytes(32).toString("base64url")}`
121
+ });
122
+ await this.write("bootstrap.json", state);
123
+ await this.pair(state.instanceId, "setup");
124
+ return state;
113
125
  }
114
126
  async pair(instanceId, mode) {
115
127
  const token = randomBytes(32).toString("base64url");
@@ -216,7 +228,7 @@ var BootstrapStateStore = class {
216
228
  // package.json
217
229
  var package_default = {
218
230
  name: "@tool-bridge/server",
219
- version: "0.26.2",
231
+ version: "0.28.0",
220
232
  description: "Self-hosted Tool Bridge Node server with PostgreSQL, S3 object storage and WebSocket device channels",
221
233
  type: "module",
222
234
  license: "MIT",
@@ -299,6 +311,7 @@ var package_default = {
299
311
  };
300
312
 
301
313
  export {
314
+ BootstrapLockBusyError,
302
315
  BootstrapStateStore,
303
316
  package_default
304
317
  };