@steve31415/baselib 3.74.1 → 3.76.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.
@@ -1,6 +1,8 @@
1
1
  import type { Runner, ServiceHealth } from './types.js';
2
2
  /** A gate's output shows its test browser process crashed (e.g. a segfault). */
3
3
  export declare function testBrowserCrashed(output: string): boolean;
4
+ /** A Cloud Build log shows a registry refused a pull for request quota. */
5
+ export declare function registryRateLimited(buildLog: string): boolean;
4
6
  /**
5
7
  * The `--startup-probe` value every `services update` asserts: Cloud Run
6
8
  * sends `GET <health path>` to each new instance and lets it take traffic
@@ -23,6 +23,19 @@ const BROWSER_CRASH = /^\s*\[pid=\d+\]\[err\] Received signal \d+ /m;
23
23
  export function testBrowserCrashed(output) {
24
24
  return BROWSER_CRASH.test(output);
25
25
  }
26
+ /** The Cloud Build id `gcloud builds submit` prints when it creates a build. */
27
+ const CLOUD_BUILD_ID = /\/builds\/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\b/;
28
+ /** A registry refusing a pull for request quota (Docker's error code). */
29
+ const REGISTRY_RATE_LIMIT = /\btoomanyrequests\b|429 Too Many Requests/i;
30
+ /**
31
+ * Pause before resubmitting a rate-limited build: the quota that refused it
32
+ * is counted per minute.
33
+ */
34
+ const RATE_LIMIT_PAUSE_MS = 60_000;
35
+ /** A Cloud Build log shows a registry refused a pull for request quota. */
36
+ export function registryRateLimited(buildLog) {
37
+ return REGISTRY_RATE_LIMIT.test(buildLog);
38
+ }
26
39
  /**
27
40
  * The `--startup-probe` value every `services update` asserts: Cloud Run
28
41
  * sends `GET <health path>` to each new instance and lets it take traffic
@@ -226,13 +239,35 @@ export async function runDeploy(options) {
226
239
  log('reusing existing immutable commit image');
227
240
  }
228
241
  else {
229
- const submit = await must(runner, 'gcloud', [
242
+ const submitArgs = [
230
243
  'builds', 'submit', contextDir,
231
244
  '--config', join(contextDir, 'cloudbuild.yaml'),
232
245
  '--region', config.region, ...gcloudBase,
233
246
  '--substitutions', `_IMAGE=${imageUri}`,
234
247
  '--suppress-logs', '--format=json',
235
- ], { timeoutMs: 15 * 60_000 });
248
+ ];
249
+ const submitBuild = () => runner('gcloud', submitArgs, { timeoutMs: 15 * 60_000 });
250
+ let submit = await submitBuild();
251
+ const buildId = CLOUD_BUILD_ID.exec(submit.stderr + submit.stdout)?.[1];
252
+ if (submit.code !== 0 && buildId) {
253
+ // The registry refusing a base-image pull for its per-minute request
254
+ // quota is outside anything the commit controls: resubmit once after
255
+ // a pause. Any other build failure fails at once. backup3 lost a
256
+ // deploy to mirror.gcr.io's "toomanyrequests" on 2026-11-30 (Cloud
257
+ // Build 6fb71ed9, Coder task 66468cf5) — 1 of ~1,140 builds since
258
+ // 2026-10-01.
259
+ const buildLog = await runner('gcloud', ['builds', 'log', buildId, '--region', config.region, ...gcloudBase]);
260
+ if (registryRateLimited(buildLog.stdout + buildLog.stderr)) {
261
+ await evidence.save('cloud-build-rate-limited.log', buildLog.stdout + buildLog.stderr);
262
+ log(`image build ${buildId} failed on a registry rate limit; resubmitting once in ${RATE_LIMIT_PAUSE_MS / 1000} s`);
263
+ await sleep(RATE_LIMIT_PAUSE_MS);
264
+ submit = await submitBuild();
265
+ }
266
+ }
267
+ if (submit.code !== 0) {
268
+ const tail = (submit.stderr.trim() || submit.stdout.trim()).slice(-2000);
269
+ throw new CommandFailure(`gcloud ${submitArgs.join(' ')} exited ${submit.code}\n${tail}`, submit);
270
+ }
236
271
  await evidence.save('cloud-build.json', submit.stdout);
237
272
  }
238
273
  const digest = (await describeImage()).image_summary?.fully_qualified_digest ?? '';
@@ -85,7 +85,9 @@ export interface SyncClientOptions<B, C extends EventBody, S extends EventBody,
85
85
  fetchBootstrap(signal: AbortSignal): Promise<B>;
86
86
  fetchCatchup(afterSeq: number, signal: AbortSignal): Promise<CatchupResult<S>>;
87
87
  beaconUrl: string;
88
- socketUrl(): string;
88
+ /** The app's own sync socket. Needed only without `hubUrl`: an app whose
89
+ * server no longer serves one omits it (since 3.75.0). */
90
+ socketUrl?(): string;
89
91
  runtime: SyncBrowserRuntime;
90
92
  authState: {
91
93
  isBlocked(): boolean;
@@ -124,7 +126,8 @@ export interface SyncClientOptions<B, C extends EventBody, S extends EventBody,
124
126
  * `wss://<hub>/api/ws?app=<app>` and carries hints only; mutations go over
125
127
  * `postEvents` and catch-ups over `fetchCatchup`, against the app.
126
128
  * Unset or empty: the same-origin socket (`socketUrl`), exactly as before.
127
- * Requires `app` and `postEvents`. */
129
+ * Requires `app` and `postEvents`. One of `hubUrl` and `socketUrl` must be
130
+ * set: the constructor throws otherwise. */
128
131
  hubUrl?: string;
129
132
  /** This app's name on the hub socket (hub mode). */
130
133
  app?: string;
@@ -198,6 +201,8 @@ export declare class SyncClient<B, C extends EventBody, S extends EventBody, Sum
198
201
  private visibleSinceMono;
199
202
  private readonly counters;
200
203
  private readonly hub;
204
+ /** Where the live socket goes: the hub's URL, or the app's own. */
205
+ private readonly socketUrl;
201
206
  /** The live hub socket answered our `connect`. */
202
207
  private hubAcked;
203
208
  /** Counts acks: a catch-up establishes only if it began under the current one. */
@@ -386,7 +391,6 @@ export declare class SyncClient<B, C extends EventBody, S extends EventBody, Sum
386
391
  private cancelResyncFailureRetry;
387
392
  private sendConnect;
388
393
  private connectMessage;
389
- private socketUrl;
390
394
  private sendRaw;
391
395
  private trySend;
392
396
  private flushViaBeacon;
@@ -169,6 +169,8 @@ export class SyncClient {
169
169
  // fields above then hold the hub socket; the app's own reachability has
170
170
  // its own ladder here, independent of the socket's.
171
171
  hub;
172
+ /** Where the live socket goes: the hub's URL, or the app's own. */
173
+ socketUrl;
172
174
  /** The live hub socket answered our `connect`. */
173
175
  hubAcked = false;
174
176
  /** Counts acks: a catch-up establishes only if it began under the current one. */
@@ -201,12 +203,18 @@ export class SyncClient {
201
203
  constructor(options) {
202
204
  this.options = options;
203
205
  if (!options.hubUrl) {
206
+ const sameOrigin = options.socketUrl?.bind(options);
207
+ if (!sameOrigin)
208
+ throw new Error('sync needs hubUrl or socketUrl');
204
209
  this.hub = null;
210
+ this.socketUrl = () => sameOrigin();
205
211
  return;
206
212
  }
207
213
  if (!options.app || !options.postEvents)
208
214
  throw new Error('sync hub mode needs app and postEvents');
209
- this.hub = { socketUrl: hubSocketUrl(options.hubUrl, options.app), app: options.app, postEvents: options.postEvents };
215
+ const url = hubSocketUrl(options.hubUrl, options.app);
216
+ this.hub = { app: options.app, postEvents: options.postEvents };
217
+ this.socketUrl = () => url;
210
218
  }
211
219
  init() {
212
220
  if (this.disposed)
@@ -1900,9 +1908,6 @@ export class SyncClient {
1900
1908
  return { type: 'connect', clientGuid: this.options.clientGuid };
1901
1909
  return { type: 'connect', clientGuid: this.options.clientGuid, lastSeq: this.options.store.currentSeq() };
1902
1910
  }
1903
- socketUrl() {
1904
- return this.hub?.socketUrl ?? this.options.socketUrl();
1905
- }
1906
1911
  sendRaw(message) {
1907
1912
  if (this.socket?.readyState !== SOCKET_OPEN)
1908
1913
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steve31415/baselib",
3
- "version": "3.74.1",
3
+ "version": "3.76.0",
4
4
  "description": "Plasticine new-world shared platform library: logging, auth, service-to-service auth, db, HTTP, sync, app updates",
5
5
  "type": "module",
6
6
  "license": "MIT",