@abelxiaoxing/cadence 2.1.4 → 2.1.6

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.
@@ -70,6 +70,7 @@ export class HttpServiceTransport implements ServiceTransport {
70
70
  origin: string;
71
71
  /** Authenticated same-origin project routing; never an arbitrary URL. */
72
72
  project?: string;
73
+ /** Diagnostic metadata only; authentication never depends on a build. */
73
74
  build?: ClientBuild;
74
75
  fetch?: typeof fetch;
75
76
  /** Optional total bound for finite replies only, never streaming export. */
@@ -105,11 +106,12 @@ export class HttpServiceTransport implements ServiceTransport {
105
106
  workbenchCommand(request: WorkbenchCommand): Promise<unknown> {
106
107
  return this.request("workbenchCommand", request);
107
108
  }
108
- private headers(write: boolean): Record<string, string> {
109
+ private headers(write: boolean, session = false): Record<string, string> {
109
110
  const headers: Record<string, string> = { "Cache-Control": "no-store" };
110
111
  if (this.options.build)
111
112
  headers["X-Cadence-Build"] = canonicalJson(this.options.build);
112
- if (this.attachment) headers["X-Cadence-Attachment"] = this.attachment;
113
+ if (this.attachment && (write || session))
114
+ headers["X-Cadence-Attachment"] = this.attachment;
113
115
  if (this.options.localCapability)
114
116
  headers["X-Cadence-Local"] = this.options.localCapability;
115
117
  if (write) {
@@ -128,8 +130,8 @@ export class HttpServiceTransport implements ServiceTransport {
128
130
  const input = decodeHttpInput(operation, value),
129
131
  wire = wireRequest(operation, input),
130
132
  write = wire.method === "POST";
131
- // No automatic session/preflight refresh, changed command ID, mutation retry,
132
- // credential query or redirect follow after an uncertain HTTP outcome.
133
+ // No command replay, preflight renewal, changed command ID or redirect
134
+ // follow after an uncertain HTTP outcome.
133
135
  const scope =
134
136
  this.options.requestTimeoutMs === undefined
135
137
  ? undefined
@@ -137,27 +139,30 @@ export class HttpServiceTransport implements ServiceTransport {
137
139
  const requestSignal = scope?.signal ?? signal;
138
140
  try {
139
141
  if (requestSignal?.aborted) throw requestSignal.reason;
142
+ // One client-chosen observation ID survives a lost header/body. Repeating
143
+ // session reuses that slot and recovers CSRF, never a command or grant.
144
+ if (operation === "session") this.attachment ??= crypto.randomUUID();
140
145
  const response = await this.fetcher(
141
146
  `${this.origin}${this.options.project ? `/projects/${this.options.project}` : ""}${wire.path}`,
142
147
  {
143
148
  method: wire.method,
144
- headers: this.headers(write),
149
+ headers: this.headers(write, operation === "session"),
145
150
  credentials: "same-origin",
146
151
  redirect: "error",
147
152
  signal: requestSignal,
148
153
  ...(write ? { body: wire.body } : {}),
149
154
  },
150
155
  );
151
- // Keep a known allocation even if its original response body is lost or
152
- // malformed. Without a validated CSRF challenge detach may remain blocked;
153
- // never silently forget that slot and acquire 16 replacement sessions.
156
+ // Validate the reply's allocation without losing our original ID when
157
+ // its header or body is missing. A later session read can recover it.
154
158
  let sessionAttachment: string | undefined;
155
159
  try {
156
160
  if (operation === "session" && response.ok) {
157
161
  sessionAttachment = decodeId(
158
162
  response.headers.get("x-cadence-attachment"),
159
163
  );
160
- this.attachment = sessionAttachment;
164
+ if (sessionAttachment !== this.attachment)
165
+ throw new ContractError("invalid");
161
166
  }
162
167
  } catch (error) {
163
168
  await response.body?.cancel().catch(() => {});
@@ -276,19 +281,41 @@ export class HttpServiceTransport implements ServiceTransport {
276
281
  async detach(): Promise<unknown> {
277
282
  const attachment = this.attachment;
278
283
  if (!attachment) return;
284
+ // A lost session body may leave the exact allocation without its CSRF.
285
+ // Read that same ID again so departure/reconnection can release it instead
286
+ // of blocking forever or abandoning a slot. This cannot submit work.
287
+ if (!this.options.localCapability && !this.csrf) {
288
+ const session = decodeReply(await this.session(), decodeSessionBootstrap);
289
+ if (session.kind === "error") {
290
+ if (!["stale", "unauthenticated"].includes(session.error.code))
291
+ throw new ContractError(session.error.code);
292
+ if (this.attachment === attachment) this.attachment = undefined;
293
+ return session;
294
+ }
295
+ }
279
296
  const result = decodeReply(await this.request("detach", null), (value) => {
280
297
  if (value !== null) throw new ContractError("invalid");
281
298
  return null;
282
299
  });
283
300
  // A lost reply does not prove release. Retain the exact attachment for a
284
- // later explicit detach rather than allocating another of the 16 slots.
301
+ // later detach rather than allocating another of the 16 slots.
285
302
  // Stale means the server no longer owns this ID; rotation also revokes it.
286
303
  if (
287
304
  result.kind === "error" &&
288
305
  result.error.code !== "stale" &&
289
306
  result.error.code !== "unauthenticated"
290
- )
307
+ ) {
308
+ // A restarted direct owner can keep the access token but replace CSRF.
309
+ // The next detach recovers it by reading the same allocation, rather than
310
+ // repeatedly sending this refused challenge. No work command is retried.
311
+ if (
312
+ result.error.code === "forbidden" &&
313
+ !this.options.localCapability &&
314
+ this.attachment === attachment
315
+ )
316
+ this.csrf = null;
291
317
  throw new ContractError(result.error.code);
318
+ }
292
319
  if (this.attachment === attachment) {
293
320
  this.attachment = undefined;
294
321
  this.csrf = null;