@sjawhar/pi-legion 19.33.0 → 19.35.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.
Files changed (2) hide show
  1. package/dist/legion.js +148 -35
  2. package/package.json +1 -1
package/dist/legion.js CHANGED
@@ -16067,6 +16067,7 @@ var SUBJECT_TOKEN_TYPE_GOOGLE = "urn:envoy:params:oauth:token-type:google-identi
16067
16067
  var SUBJECT_TOKEN_TYPE_PERSON = "urn:envoy:params:oauth:token-type:person-google-identity";
16068
16068
  var ACTOR_TOKEN_TYPE_MACHINE = "urn:envoy:params:oauth:token-type:machine-google-identity";
16069
16069
  var TOKEN_TYPE_DPOP = "DPoP";
16070
+ var POD_SESSION_NOT_REGISTERED = "pod_session_not_registered";
16070
16071
 
16071
16072
  // ../broker-client/src/metadata-identity.ts
16072
16073
  var METADATA_HOST_VAR = "GCE_METADATA_HOST";
@@ -17497,6 +17498,26 @@ var DEFAULT_RENEW_BEFORE_MS = 5 * 60000;
17497
17498
  var TOO_MANY_REQUESTS = 429;
17498
17499
  var INVALID_DPOP_PROOF = "invalid_dpop_proof";
17499
17500
  var USE_DPOP_NONCE = "use_dpop_nonce";
17501
+ var INVALID_GRANT = "invalid_grant";
17502
+ var UNREGISTERED_FIRST_WAIT_MS = 15000;
17503
+ var UNREGISTERED_MAX_WAIT_MS = 5 * 60000;
17504
+ var UNREGISTERED_LOG_EVERY_MS = 30 * 60000;
17505
+ function sleep(ms, signal) {
17506
+ const { promise, resolve } = Promise.withResolvers();
17507
+ if (signal?.aborted) {
17508
+ resolve();
17509
+ return promise;
17510
+ }
17511
+ const done = () => {
17512
+ clearTimeout(timer);
17513
+ signal?.removeEventListener("abort", done);
17514
+ resolve();
17515
+ };
17516
+ const timer = setTimeout(done, ms);
17517
+ timer.unref();
17518
+ signal?.addEventListener("abort", done, { once: true });
17519
+ return promise;
17520
+ }
17500
17521
 
17501
17522
  class BrokerRefusedError extends Error {
17502
17523
  status;
@@ -17510,7 +17531,19 @@ class BrokerRefusedError extends Error {
17510
17531
  this.name = "BrokerRefusedError";
17511
17532
  }
17512
17533
  get permanent() {
17513
- return this.status !== TOO_MANY_REQUESTS && this.code !== INVALID_DPOP_PROOF && this.code !== USE_DPOP_NONCE;
17534
+ return this.status !== TOO_MANY_REQUESTS && this.code !== INVALID_DPOP_PROOF && this.code !== USE_DPOP_NONCE && !this.podSessionUnregistered;
17535
+ }
17536
+ get podSessionUnregistered() {
17537
+ return this.code === INVALID_GRANT && this.description.startsWith(`${POD_SESSION_NOT_REGISTERED}:`);
17538
+ }
17539
+ }
17540
+
17541
+ class RegistrationPendingError extends Error {
17542
+ session;
17543
+ constructor(session, refusal) {
17544
+ super(`the secrets broker has no registration for this pod's session ${session} yet, and the trade is being retried in the background until it lands (${refusal.description})`, { cause: refusal });
17545
+ this.session = session;
17546
+ this.name = "RegistrationPendingError";
17514
17547
  }
17515
17548
  }
17516
17549
 
@@ -17559,10 +17592,16 @@ class BrokerTokenClient {
17559
17592
  #launcherCredential;
17560
17593
  #renewBeforeMs;
17561
17594
  #now;
17595
+ #sleep;
17596
+ #log;
17562
17597
  #fetch;
17563
17598
  #nonce = "";
17564
17599
  #held = null;
17565
17600
  #trading = null;
17601
+ #waiters = new Set;
17602
+ #pending = null;
17603
+ #wake = null;
17604
+ #closed = new AbortController;
17566
17605
  constructor(options) {
17567
17606
  const brokerUrl = withoutTrailingSlashes(options.brokerUrl.trim());
17568
17607
  if (brokerUrl === "")
@@ -17576,6 +17615,8 @@ class BrokerTokenClient {
17576
17615
  this.#launcherCredential = options.launcherCredential ?? true;
17577
17616
  this.#renewBeforeMs = options.renewBeforeMs ?? DEFAULT_RENEW_BEFORE_MS;
17578
17617
  this.#now = options.now ?? Date.now;
17618
+ this.#sleep = options.sleep ?? sleep;
17619
+ this.#log = options.log ?? console.warn;
17579
17620
  this.#fetch = options.fetch ?? fetch;
17580
17621
  }
17581
17622
  get key() {
@@ -17588,31 +17629,95 @@ class BrokerTokenClient {
17588
17629
  const held = this.#held;
17589
17630
  if (held !== null && this.#now() < held.expiresAt.getTime() - this.#renewBeforeMs)
17590
17631
  return held;
17591
- if (this.#trading !== null)
17592
- return await this.#trading;
17593
- const trading = this.#renew(signal);
17594
- this.#trading = trading;
17595
- try {
17596
- return await trading;
17597
- } finally {
17632
+ if (this.#trading === null) {
17633
+ const started = this.#renew();
17634
+ this.#trading = started;
17635
+ started.then((token) => this.#settled(started, (waiter) => waiter.resolve(token)), (error) => this.#settled(started, (waiter) => waiter.reject(error)));
17636
+ } else {
17637
+ this.#wake?.();
17638
+ }
17639
+ return await this.#joined(signal);
17640
+ }
17641
+ #settled(started, tell) {
17642
+ if (this.#trading === started)
17598
17643
  this.#trading = null;
17644
+ const waiters = [...this.#waiters];
17645
+ this.#waiters.clear();
17646
+ for (const waiter of waiters)
17647
+ tell(waiter);
17648
+ }
17649
+ #joined(signal) {
17650
+ const { promise, resolve, reject } = Promise.withResolvers();
17651
+ const stop = () => {
17652
+ this.#waiters.delete(waiter);
17653
+ const pending = this.#pending;
17654
+ reject(pending === null ? signal?.reason : new RegistrationPendingError(this.#session, pending));
17655
+ };
17656
+ const waiter = {
17657
+ resolve: (token) => {
17658
+ signal?.removeEventListener("abort", stop);
17659
+ resolve(token);
17660
+ },
17661
+ reject: (error) => {
17662
+ signal?.removeEventListener("abort", stop);
17663
+ reject(error);
17664
+ }
17665
+ };
17666
+ if (signal?.aborted) {
17667
+ stop();
17668
+ return promise;
17599
17669
  }
17670
+ this.#waiters.add(waiter);
17671
+ signal?.addEventListener("abort", stop, { once: true });
17672
+ return promise;
17673
+ }
17674
+ close() {
17675
+ this.#closed.abort();
17600
17676
  }
17601
17677
  drop() {
17602
17678
  this.#held = null;
17603
17679
  }
17604
- async#renew(signal) {
17680
+ async#renew() {
17681
+ const closed = this.#closed.signal;
17682
+ let wait = UNREGISTERED_FIRST_WAIT_MS;
17683
+ let since = null;
17684
+ let logged = null;
17605
17685
  try {
17606
- const token = await this.#trade(signal);
17607
- this.#held = token;
17608
- return token;
17609
- } catch (error) {
17610
- const held = this.#held;
17611
- if (!isFinalRefusal(error) && held !== null && this.#now() < held.expiresAt.getTime()) {
17612
- return held;
17686
+ while (true) {
17687
+ try {
17688
+ const token = await this.#trade(closed);
17689
+ this.#held = token;
17690
+ return token;
17691
+ } catch (error) {
17692
+ const held = this.#held;
17693
+ if (!isFinalRefusal(error) && held !== null && this.#now() < held.expiresAt.getTime()) {
17694
+ return held;
17695
+ }
17696
+ this.#held = null;
17697
+ const unregistered = error instanceof BrokerRefusedError && error.podSessionUnregistered;
17698
+ if (!unregistered || closed.aborted)
17699
+ throw error;
17700
+ this.#pending = error;
17701
+ const now = this.#now();
17702
+ since ??= now;
17703
+ if (logged === null || now - logged >= UNREGISTERED_LOG_EVERY_MS) {
17704
+ logged = now;
17705
+ this.#log(`the secrets broker has no registration for this pod's session ${this.#session} yet (${Math.round((now - since) / 1000)} s so far); trading ${this.describe()} again until it lands`);
17706
+ }
17707
+ const woken = new AbortController;
17708
+ this.#wake = () => woken.abort();
17709
+ try {
17710
+ await this.#sleep(wait, AbortSignal.any([closed, woken.signal]));
17711
+ } finally {
17712
+ this.#wake = null;
17713
+ }
17714
+ if (closed.aborted)
17715
+ throw error;
17716
+ wait = Math.min(wait * 2, UNREGISTERED_MAX_WAIT_MS);
17717
+ }
17613
17718
  }
17614
- this.#held = null;
17615
- throw error;
17719
+ } finally {
17720
+ this.#pending = null;
17616
17721
  }
17617
17722
  }
17618
17723
  async#trade(signal) {
@@ -17718,7 +17823,7 @@ class BrokerTokenClient {
17718
17823
 
17719
17824
  // ../broker-client/src/person-identity.ts
17720
17825
  var ISSUER_TIMEOUT_SECONDS = 30;
17721
- var INVALID_GRANT = "invalid_grant";
17826
+ var INVALID_GRANT2 = "invalid_grant";
17722
17827
  function issuerSaid(error) {
17723
17828
  if (error instanceof ResponseBodyError) {
17724
17829
  const description = error.error_description ?? "";
@@ -17775,7 +17880,7 @@ function personIdentity(record, flow, options = {}) {
17775
17880
  config[customFetch2] = through(signal);
17776
17881
  answered = await refreshTokenGrant(config, refreshToken);
17777
17882
  } catch (error) {
17778
- if (error instanceof ResponseBodyError && error.error === INVALID_GRANT) {
17883
+ if (error instanceof ResponseBodyError && error.error === INVALID_GRANT2) {
17779
17884
  throw new SignInEndedError(flow.issuer, person, { cause: error });
17780
17885
  }
17781
17886
  throw new Error(`renew ${person}'s sign-in at ${flow.issuer}: ${issuerSaid(error)}`);
@@ -18117,7 +18222,7 @@ function brokerCredential(env, session) {
18117
18222
  const own = (acting) => (acting ?? "").trim() || sessionName(session);
18118
18223
  return {
18119
18224
  scheme: "DPoP",
18120
- async headers(method, url, acting) {
18225
+ async headers(method, url, acting, signal) {
18121
18226
  const { entry, made } = await clientFor(env, own(acting));
18122
18227
  const { client } = made;
18123
18228
  if (client === undefined) {
@@ -18125,7 +18230,7 @@ function brokerCredential(env, session) {
18125
18230
  }
18126
18231
  let token;
18127
18232
  try {
18128
- token = await client.token();
18233
+ token = await client.token(signal);
18129
18234
  } catch (error) {
18130
18235
  if (made.signIn !== "" && isFinalRefusal(error))
18131
18236
  entry.refused.set(made.signIn, error);
@@ -18321,7 +18426,7 @@ function legionControllerNoticeSubject(project) {
18321
18426
  var DISPATCH_TOPIC_PREFIX = "notifications.dispatch.";
18322
18427
  var DISPATCH_ISSUE_TOPIC_PREFIX = `${DISPATCH_TOPIC_PREFIX}issue.`;
18323
18428
  var DISPATCH_DOCUMENT_TOPIC_PREFIX = `${DISPATCH_TOPIC_PREFIX}document.`;
18324
- function dispatchIssueSubject(issueKey, type) {
18429
+ function legacyDispatchIssueSubject(issueKey, type) {
18325
18430
  return `${DISPATCH_ISSUE_TOPIC_PREFIX}${issueKey}.${type}`;
18326
18431
  }
18327
18432
  function dispatchDocumentSubject(project, slug, type) {
@@ -19203,7 +19308,7 @@ var MessageAcceptedEventPayloadSchema = strictObject({
19203
19308
  var NO_ROUTE_MESSAGE_PREFIX = "no route for ";
19204
19309
  var NO_ROUTE_RETRY_DELAYS_MS = [1000, 2000, 4000];
19205
19310
  var NO_ROUTE_ROLLING_RELEASE_MESSAGE = "The Dispatch process that answered does not offer this action yet, because a new release of " + "Dispatch is in progress. Try again in one minute.";
19206
- var sleep = (ms, signal) => {
19311
+ var sleep2 = (ms, signal) => {
19207
19312
  const { promise, resolve, reject } = Promise.withResolvers();
19208
19313
  if (signal?.aborted) {
19209
19314
  reject(signal.reason);
@@ -19224,7 +19329,7 @@ function jitteredDelay(ms) {
19224
19329
  return ms * (0.75 + Math.random() * 0.5);
19225
19330
  }
19226
19331
  async function retryNoRoute(attempt, isNoRoute, options) {
19227
- const wait = options.sleep ?? sleep;
19332
+ const wait = options.sleep ?? sleep2;
19228
19333
  for (const delay of [...NO_ROUTE_RETRY_DELAYS_MS, undefined]) {
19229
19334
  try {
19230
19335
  return await attempt();
@@ -20884,7 +20989,7 @@ function ownerTitle(owner) {
20884
20989
  return "";
20885
20990
  }
20886
20991
  function issueTopic(key) {
20887
- return { label: key, topic: dispatchIssueSubject(key, ">") };
20992
+ return { label: key, topic: legacyDispatchIssueSubject(key, ">") };
20888
20993
  }
20889
20994
  function documentTopicOf(project, slug) {
20890
20995
  return {
@@ -21322,7 +21427,7 @@ class DispatchClient {
21322
21427
  this.#baseUrl = withoutTrailingSlashes(baseUrl);
21323
21428
  this.#signal = requestSignal(options.signal);
21324
21429
  this.writeRetry = options.writeRetry ?? DISPATCH_WRITE_RETRY;
21325
- this.#sleep = options.sleep ?? sleep;
21430
+ this.#sleep = options.sleep ?? sleep2;
21326
21431
  this.#retryNoRoute = options.retryNoRoute ?? true;
21327
21432
  this.#madeAt = this.#now();
21328
21433
  }
@@ -21806,14 +21911,15 @@ class DispatchClient {
21806
21911
  }
21807
21912
  async#sendPresenting(method, url, body, accept, signal, key) {
21808
21913
  const headers = { Accept: accept };
21809
- let presented = await this.#credential.headers(method, url);
21914
+ const limit = signal === undefined ? this.#signal : AbortSignal.any([this.#signal, signal]);
21915
+ let presented = await this.#credential.headers(method, url, undefined, limit);
21810
21916
  if (body !== undefined)
21811
21917
  headers["Content-Type"] = body.type;
21812
21918
  if (key === undefined) {
21813
21919
  return this.fetchImpl(url, {
21814
21920
  method,
21815
21921
  headers: { ...headers, ...presented },
21816
- signal: signal === undefined ? this.#signal : AbortSignal.any([this.#signal, signal]),
21922
+ signal: limit,
21817
21923
  ...body === undefined ? {} : { body: body.content }
21818
21924
  });
21819
21925
  }
@@ -21833,7 +21939,7 @@ class DispatchClient {
21833
21939
  let thrown;
21834
21940
  try {
21835
21941
  if (attempt > 1)
21836
- presented = await this.#credential.headers(method, url);
21942
+ presented = await this.#credential.headers(method, url, undefined, limit);
21837
21943
  const sent = { ...headers, ...presented };
21838
21944
  answer = await writeAnswer(await this.#attempt(url, method, sent, body, silenceMs), key);
21839
21945
  if (answer.echoed)
@@ -21936,10 +22042,14 @@ class DispatchClient {
21936
22042
  async#probeKeyedLayer() {
21937
22043
  try {
21938
22044
  const probe = `${this.#baseUrl}/healthz`;
22045
+ const limit = AbortSignal.any([this.#signal, AbortSignal.timeout(KEY_LAYER_PROBE_MS)]);
21939
22046
  const response = await this.fetchImpl(probe, {
21940
22047
  method: "GET",
21941
- headers: { Accept: "application/json", ...await this.#credential.headers("GET", probe) },
21942
- signal: AbortSignal.any([this.#signal, AbortSignal.timeout(KEY_LAYER_PROBE_MS)])
22048
+ headers: {
22049
+ Accept: "application/json",
22050
+ ...await this.#credential.headers("GET", probe, undefined, limit)
22051
+ },
22052
+ signal: limit
21943
22053
  });
21944
22054
  if (!isJson(response))
21945
22055
  return false;
@@ -26559,21 +26669,24 @@ function createEnvoyClient(config) {
26559
26669
  const attemptOnce = async (url, init, deadlineMs, actingSession) => {
26560
26670
  let response;
26561
26671
  let unminted;
26672
+ const deadline = AbortSignal.timeout(deadlineMs);
26562
26673
  try {
26563
26674
  const headers = new Headers(init.headers);
26564
26675
  let presented;
26565
26676
  try {
26566
- const ready = credential?.headers(init.method ?? "GET", url, actingSession);
26677
+ const ready = credential?.headers(init.method ?? "GET", url, actingSession, deadline);
26567
26678
  presented = ready instanceof Promise ? await ready : ready;
26568
26679
  } catch (error) {
26569
26680
  unminted = error;
26570
26681
  }
26682
+ if (unminted !== undefined && deadline.aborted)
26683
+ return { unanswered: unminted };
26571
26684
  for (const [name, value] of Object.entries(presented ?? {}))
26572
26685
  headers.set(name, value);
26573
26686
  response = await config.fetch(url, {
26574
26687
  ...init,
26575
26688
  headers,
26576
- signal: AbortSignal.timeout(deadlineMs)
26689
+ signal: deadline
26577
26690
  });
26578
26691
  } catch (error) {
26579
26692
  return { unanswered: error };
@@ -29803,7 +29916,7 @@ import { logger as logger3 } from "@oh-my-pi/pi-utils";
29803
29916
  // package.json
29804
29917
  var package_default = {
29805
29918
  name: "@sjawhar/pi-legion",
29806
- version: "19.33.0",
29919
+ version: "19.35.0",
29807
29920
  license: "Apache-2.0",
29808
29921
  type: "module",
29809
29922
  omp: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sjawhar/pi-legion",
3
- "version": "19.33.0",
3
+ "version": "19.35.0",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "omp": {