@ricsam/r5d-worker 0.0.47 → 0.0.49

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,5 +1,5 @@
1
1
  {
2
2
  "name": "@ricsam/r5d-worker",
3
- "version": "0.0.47",
3
+ "version": "0.0.49",
4
4
  "type": "commonjs"
5
5
  }
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var port_forward_client_exports = {};
30
+ __export(port_forward_client_exports, {
31
+ openWorkerPortForwardRelay: () => openWorkerPortForwardRelay
32
+ });
33
+ module.exports = __toCommonJS(port_forward_client_exports);
34
+ var import_node_net = __toESM(require("node:net"), 1);
35
+ var import_ws = __toESM(require("ws"), 1);
36
+ const RELAY_FRAME_BYTES = 64 * 1024;
37
+ const RELAY_PAUSE_BYTES = 1024 * 1024;
38
+ const RELAY_RESUME_BYTES = 256 * 1024;
39
+ function relayUrl(baseUrl, label, forwardId, relayConnectionId) {
40
+ const url = new URL("/worker/port-forward/ws", baseUrl);
41
+ url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
42
+ url.searchParams.set("label", label);
43
+ url.searchParams.set("forward", forwardId);
44
+ url.searchParams.set("connection", relayConnectionId);
45
+ return url.toString();
46
+ }
47
+ function rawDataBuffer(data) {
48
+ if (Buffer.isBuffer(data)) return data;
49
+ if (Array.isArray(data)) return Buffer.concat(data);
50
+ return Buffer.from(data);
51
+ }
52
+ function openWorkerPortForwardRelay(options) {
53
+ const socket = import_node_net.default.createConnection({ host: "127.0.0.1", port: options.workerPort, allowHalfOpen: true });
54
+ socket.pause();
55
+ socket.setNoDelay(true);
56
+ const relay = new import_ws.default(relayUrl(options.baseUrl, options.label, options.forwardId, options.relayConnectionId), {
57
+ headers: { Authorization: `Bearer ${options.token}` },
58
+ perMessageDeflate: false
59
+ });
60
+ let relayReady = false;
61
+ let targetReady = false;
62
+ const pendingChunks = [];
63
+ let pendingBytes = 0;
64
+ let closing = false;
65
+ let resumeTimer;
66
+ const maybeResume = () => {
67
+ if (relayReady && targetReady && !resumeTimer) socket.resume();
68
+ };
69
+ const cleanup = () => {
70
+ if (closing) return;
71
+ closing = true;
72
+ if (resumeTimer) clearInterval(resumeTimer);
73
+ socket.destroy();
74
+ if (relay.readyState === import_ws.default.OPEN || relay.readyState === import_ws.default.CONNECTING) relay.close();
75
+ };
76
+ const sendError = (message) => {
77
+ if (relay.readyState === import_ws.default.OPEN) relay.send(JSON.stringify({ type: "error", error: message }));
78
+ cleanup();
79
+ };
80
+ const waitForRelayDrain = () => {
81
+ if (resumeTimer || relay.bufferedAmount <= RELAY_PAUSE_BYTES) return;
82
+ socket.pause();
83
+ resumeTimer = setInterval(() => {
84
+ if (relay.readyState !== import_ws.default.OPEN) return cleanup();
85
+ if (relay.bufferedAmount > RELAY_RESUME_BYTES) return;
86
+ clearInterval(resumeTimer);
87
+ resumeTimer = void 0;
88
+ maybeResume();
89
+ }, 10);
90
+ };
91
+ const sendChunk = (chunk) => {
92
+ for (let offset = 0; offset < chunk.byteLength; offset += RELAY_FRAME_BYTES) {
93
+ relay.send(chunk.subarray(offset, Math.min(chunk.byteLength, offset + RELAY_FRAME_BYTES)), { binary: true });
94
+ }
95
+ waitForRelayDrain();
96
+ };
97
+ socket.once("connect", () => {
98
+ targetReady = true;
99
+ maybeResume();
100
+ });
101
+ socket.on("data", (chunk) => {
102
+ if (!relayReady || relay.readyState !== import_ws.default.OPEN) {
103
+ pendingBytes += chunk.byteLength;
104
+ if (pendingBytes > RELAY_PAUSE_BYTES) return cleanup();
105
+ pendingChunks.push(Buffer.from(chunk));
106
+ socket.pause();
107
+ return;
108
+ }
109
+ sendChunk(chunk);
110
+ });
111
+ socket.on("end", () => {
112
+ if (relay.readyState === import_ws.default.OPEN) relay.send(JSON.stringify({ type: "end" }));
113
+ });
114
+ socket.on("error", (error) => sendError(`Could not connect to 127.0.0.1:${options.workerPort}: ${error.message}`));
115
+ socket.on("close", cleanup);
116
+ relay.on("message", (data, isBinary) => {
117
+ if (!isBinary) {
118
+ let control;
119
+ try {
120
+ control = JSON.parse(rawDataBuffer(data).toString("utf8"));
121
+ } catch {
122
+ return cleanup();
123
+ }
124
+ if (control.type === "ready") {
125
+ relayReady = true;
126
+ for (const chunk of pendingChunks.splice(0)) sendChunk(chunk);
127
+ pendingBytes = 0;
128
+ maybeResume();
129
+ } else if (control.type === "end") {
130
+ socket.end();
131
+ } else if (control.type === "error") {
132
+ socket.destroy(new Error(control.error || "Port-forward relay failed."));
133
+ }
134
+ return;
135
+ }
136
+ if (!socket.write(rawDataBuffer(data))) {
137
+ relay.pause();
138
+ socket.once("drain", () => relay.resume());
139
+ }
140
+ });
141
+ relay.once("error", cleanup);
142
+ relay.once("close", cleanup);
143
+ socket.pause();
144
+ }
145
+ // Annotate the CommonJS export names for ESM import in node:
146
+ 0 && (module.exports = {
147
+ openWorkerPortForwardRelay
148
+ });
@@ -30,6 +30,7 @@ var workspace_sync_exports = {};
30
30
  __export(workspace_sync_exports, {
31
31
  MAX_WORKSPACE_SYNC_DIFF_BYTES: () => MAX_WORKSPACE_SYNC_DIFF_BYTES,
32
32
  WORKSPACE_BRANCH: () => WORKSPACE_BRANCH,
33
+ WORKSPACE_NATIVE_PLANS_RELATIVE_PATH: () => WORKSPACE_NATIVE_PLANS_RELATIVE_PATH,
33
34
  WORKSPACE_PERIODIC_SCAN_INTERVAL_MS: () => WORKSPACE_PERIODIC_SCAN_INTERVAL_MS,
34
35
  WorkspaceSyncSingleFlight: () => WorkspaceSyncSingleFlight,
35
36
  assertCanonicalCheckoutIndexMatchesWorktree: () => assertCanonicalCheckoutIndexMatchesWorktree,
@@ -130,12 +131,16 @@ function workspaceProjectBranchRelativePath(projectId, branchName) {
130
131
  function workspacePlansRelativePath(projectId, branchName) {
131
132
  return import_node_path.default.posix.join("plans", projectId, encodeWorkspaceBranch(branchName));
132
133
  }
134
+ const WORKSPACE_NATIVE_PLANS_RELATIVE_PATH = "workspace-plans";
133
135
  function visibleProjectBranchPath(projectsRoot, manifest, branchName) {
134
136
  return (0, import_managed_paths.managedBranchPath)(projectsRoot, manifest.checkoutPathSegments, branchName);
135
137
  }
136
138
  function localPlansBranchPath(plansRoot, projectId, branchName) {
137
139
  return import_node_path.default.join(plansRoot, projectId, branchName);
138
140
  }
141
+ function localWorkspacePlansPath(plansRoot) {
142
+ return import_node_path.default.join(plansRoot, "workspace");
143
+ }
139
144
  function assertInside(root, candidate, label) {
140
145
  const resolvedRoot = import_node_path.default.resolve(root);
141
146
  const resolvedCandidate = import_node_path.default.resolve(candidate);
@@ -713,12 +718,43 @@ function mirrorShadowPlansToLocal(input, manifest, branchName) {
713
718
  listFilesRecursively(targetRoot, planFilter)
714
719
  );
715
720
  }
721
+ function mirrorLocalWorkspacePlansToShadow(input) {
722
+ if (input.trigger.canonicalCheckoutOnly) return;
723
+ const sourceRoot = localWorkspacePlansPath(input.plansRoot);
724
+ if (!import_node_fs.default.existsSync(sourceRoot)) return;
725
+ const targetRoot = import_node_path.default.join(input.shadowRoot, WORKSPACE_NATIVE_PLANS_RELATIVE_PATH);
726
+ const planFilter = (relativePath) => relativePath.endsWith(".plan.md");
727
+ mirrorFileSet(sourceRoot, targetRoot, listFilesRecursively(sourceRoot, planFilter), listFilesRecursively(targetRoot, planFilter));
728
+ }
729
+ function mirrorShadowWorkspacePlansToLocal(input) {
730
+ if (input.trigger.canonicalCheckoutOnly) return;
731
+ const sourceRoot = import_node_path.default.join(input.shadowRoot, WORKSPACE_NATIVE_PLANS_RELATIVE_PATH);
732
+ const targetRoot = localWorkspacePlansPath(input.plansRoot);
733
+ const planFilter = (relativePath) => relativePath.endsWith(".plan.md");
734
+ mirrorFileSet(
735
+ sourceRoot,
736
+ targetRoot,
737
+ listShadowTrackedFiles(input, WORKSPACE_NATIVE_PLANS_RELATIVE_PATH).filter(planFilter),
738
+ listFilesRecursively(targetRoot, planFilter)
739
+ );
740
+ }
741
+ function reconcileWorkspaceNativePlans(input) {
742
+ if (input.trigger.canonicalCheckoutOnly) return;
743
+ if (import_node_fs.default.existsSync(localWorkspacePlansPath(input.plansRoot))) {
744
+ mirrorLocalWorkspacePlansToShadow(input);
745
+ return;
746
+ }
747
+ if (listShadowTrackedFiles(input, WORKSPACE_NATIVE_PLANS_RELATIVE_PATH).length > 0 || restoreShadowRootFromRemote(input, WORKSPACE_NATIVE_PLANS_RELATIVE_PATH)) {
748
+ mirrorShadowWorkspacePlansToLocal(input);
749
+ }
750
+ }
716
751
  function mergeGitlinkProjection(target, source) {
717
752
  target.entries.push(...source.entries);
718
753
  target.opaqueRoots.push(...source.opaqueRoots);
719
754
  }
720
755
  function mirrorVisibleWorkspaceToShadow(input, excludedCheckouts = /* @__PURE__ */ new Set()) {
721
756
  const projection = { entries: [], opaqueRoots: [] };
757
+ reconcileWorkspaceNativePlans(input);
722
758
  for (const manifest of [...input.projects].sort((left, right) => left.projectId.localeCompare(right.projectId))) {
723
759
  for (const branchName of [...new Set(manifest.branches)].sort()) {
724
760
  if (excludedCheckouts.has(`${manifest.projectId}\0${branchName}`)) continue;
@@ -754,6 +790,7 @@ function reconcileNewVisibleCheckouts(input) {
754
790
  return projection;
755
791
  }
756
792
  function mirrorShadowWorkspaceToVisible(input, opaqueWorkspaceRoots = []) {
793
+ mirrorShadowWorkspacePlansToLocal(input);
757
794
  for (const manifest of [...input.projects].sort((left, right) => left.projectId.localeCompare(right.projectId))) {
758
795
  for (const branchName of [...new Set(manifest.branches)].sort()) {
759
796
  const relativeRoot = workspaceProjectBranchRelativePath(manifest.projectId, branchName);
@@ -1338,6 +1375,7 @@ class WorkspaceSyncSingleFlight {
1338
1375
  0 && (module.exports = {
1339
1376
  MAX_WORKSPACE_SYNC_DIFF_BYTES,
1340
1377
  WORKSPACE_BRANCH,
1378
+ WORKSPACE_NATIVE_PLANS_RELATIVE_PATH,
1341
1379
  WORKSPACE_PERIODIC_SCAN_INTERVAL_MS,
1342
1380
  WorkspaceSyncSingleFlight,
1343
1381
  assertCanonicalCheckoutIndexMatchesWorktree,