pubblue 0.4.5 → 0.4.10

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,4 +1,12 @@
1
1
  // src/lib/tunnel-api.ts
2
+ var TunnelApiError = class extends Error {
3
+ constructor(message, status, retryAfterSeconds) {
4
+ super(message);
5
+ this.status = status;
6
+ this.retryAfterSeconds = retryAfterSeconds;
7
+ this.name = "TunnelApiError";
8
+ }
9
+ };
2
10
  var TunnelApiClient = class {
3
11
  constructor(baseUrl, apiKey) {
4
12
  this.baseUrl = baseUrl;
@@ -14,13 +22,26 @@ var TunnelApiClient = class {
14
22
  ...options.headers
15
23
  }
16
24
  });
25
+ const retryAfterHeader = res.headers.get("Retry-After");
26
+ const parsedRetryAfterSeconds = typeof retryAfterHeader === "string" ? Number.parseInt(retryAfterHeader, 10) : void 0;
27
+ const retryAfterSeconds = parsedRetryAfterSeconds !== void 0 && Number.isFinite(parsedRetryAfterSeconds) ? parsedRetryAfterSeconds : void 0;
17
28
  let data;
18
29
  try {
19
30
  data = await res.json();
20
31
  } catch {
21
32
  data = {};
22
33
  }
23
- if (!res.ok) throw new Error(data.error || `Request failed: ${res.status}`);
34
+ if (!res.ok) {
35
+ if (res.status === 429) {
36
+ const retrySuffix = retryAfterSeconds !== void 0 ? ` Retry after ${retryAfterSeconds}s.` : "";
37
+ throw new TunnelApiError(
38
+ `Rate limit exceeded.${retrySuffix}`,
39
+ res.status,
40
+ retryAfterSeconds
41
+ );
42
+ }
43
+ throw new TunnelApiError(data.error || `Request failed: ${res.status}`, res.status);
44
+ }
24
45
  return data;
25
46
  }
26
47
  async create(opts) {
@@ -53,5 +74,6 @@ var TunnelApiClient = class {
53
74
  };
54
75
 
55
76
  export {
77
+ TunnelApiError,
56
78
  TunnelApiClient
57
79
  };
@@ -0,0 +1,56 @@
1
+ // src/lib/tunnel-ipc.ts
2
+ import * as net from "net";
3
+ function getSocketPath(tunnelId) {
4
+ return `/tmp/pubblue-${tunnelId}.sock`;
5
+ }
6
+ async function ipcCall(socketPath, request) {
7
+ return new Promise((resolve, reject) => {
8
+ let settled = false;
9
+ let timeoutId = null;
10
+ const finish = (fn) => {
11
+ if (settled) return;
12
+ settled = true;
13
+ if (timeoutId) clearTimeout(timeoutId);
14
+ fn();
15
+ };
16
+ const client = net.createConnection(socketPath, () => {
17
+ client.write(`${JSON.stringify(request)}
18
+ `);
19
+ });
20
+ let data = "";
21
+ client.on("data", (chunk) => {
22
+ data += chunk.toString();
23
+ const newlineIdx = data.indexOf("\n");
24
+ if (newlineIdx !== -1) {
25
+ const line = data.slice(0, newlineIdx);
26
+ client.end();
27
+ try {
28
+ finish(() => resolve(JSON.parse(line)));
29
+ } catch {
30
+ finish(() => reject(new Error("Invalid response from daemon")));
31
+ }
32
+ }
33
+ });
34
+ client.on("error", (err) => {
35
+ if (err.code === "ECONNREFUSED" || err.code === "ENOENT") {
36
+ finish(() => reject(new Error("Daemon not running. Is the tunnel still active?")));
37
+ } else {
38
+ finish(() => reject(err));
39
+ }
40
+ });
41
+ client.on("end", () => {
42
+ if (!data.includes("\n")) {
43
+ finish(() => reject(new Error("Daemon closed connection unexpectedly")));
44
+ }
45
+ });
46
+ timeoutId = setTimeout(() => {
47
+ client.destroy();
48
+ finish(() => reject(new Error("Daemon request timed out")));
49
+ }, 1e4);
50
+ });
51
+ }
52
+
53
+ export {
54
+ getSocketPath,
55
+ ipcCall
56
+ };
@@ -485,13 +485,6 @@ async function startDaemon(config) {
485
485
  });
486
486
  });
487
487
  ipcServer.listen(socketPath);
488
- const infoDir = path.dirname(infoPath);
489
- if (!fs.existsSync(infoDir)) fs.mkdirSync(infoDir, { recursive: true });
490
- fs.writeFileSync(
491
- infoPath,
492
- JSON.stringify({ pid: process.pid, tunnelId, socketPath, startedAt: startTime })
493
- );
494
- scheduleNextPoll(0);
495
488
  try {
496
489
  await runNegotiationCycle();
497
490
  } catch (error) {
@@ -500,6 +493,13 @@ async function startDaemon(config) {
500
493
  await cleanup();
501
494
  throw new Error(`Failed to generate WebRTC offer: ${message}`);
502
495
  }
496
+ const infoDir = path.dirname(infoPath);
497
+ if (!fs.existsSync(infoDir)) fs.mkdirSync(infoDir, { recursive: true });
498
+ fs.writeFileSync(
499
+ infoPath,
500
+ JSON.stringify({ pid: process.pid, tunnelId, socketPath, startedAt: startTime })
501
+ );
502
+ scheduleNextPoll(0);
503
503
  async function cleanup() {
504
504
  if (stopped) return;
505
505
  stopped = true;
@@ -518,9 +518,6 @@ async function startDaemon(config) {
518
518
  } catch (error) {
519
519
  debugLog("failed to remove daemon info file during cleanup", error);
520
520
  }
521
- await apiClient.close(tunnelId).catch((error) => {
522
- markError("failed to close tunnel on API during cleanup", error);
523
- });
524
521
  }
525
522
  async function shutdown() {
526
523
  await cleanup();