@vite-hub/queue 0.6.1 → 0.7.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 (43) hide show
  1. package/README.md +17 -12
  2. package/dist/{client-CwoCdKct.js → client-B8QVg1mA.js} +46 -15
  3. package/dist/{cloudflare-client-xWteD-su.js → cloudflare-client-BoaOlFzD.js} +13 -8
  4. package/dist/{cloudflare-resource-name-Bv8Z7nDU.js → cloudflare-resource-name-Coz5CPmn.js} +1 -1
  5. package/dist/{config-Bzp1xmdh.js → config-ByKUipob.js} +4 -2
  6. package/dist/{definition-CIiFfst2.js → definition-Dorliu47.js} +9 -2
  7. package/dist/{duration-C5w_-Rf4.js → duration-CcLSF94z.js} +20 -5
  8. package/dist/{error-diagnostics-kKXseq8d.js → error-diagnostics-CsiNo_oI.js} +3 -3
  9. package/dist/{error-diagnostics-Dv6nKZQW.js → error-diagnostics-Yj8XCiya.js} +30 -6
  10. package/dist/{handler-response-CchTXVjm.js → handler-response-GFXYd6mF.js} +6 -2
  11. package/dist/index.d.ts +13 -8
  12. package/dist/index.js +4 -4
  13. package/dist/internal/runtime/cloudflare-client.d.ts +1 -1
  14. package/dist/internal/runtime/cloudflare-client.js +1 -1
  15. package/dist/internal/runtime/cloudflare-vite.d.ts +1 -1
  16. package/dist/internal/runtime/cloudflare-vite.js +9 -8
  17. package/dist/internal/runtime/local-client.d.ts +3 -3
  18. package/dist/internal/runtime/local-client.js +4 -4
  19. package/dist/internal/runtime/postgres-client.d.ts +8 -6
  20. package/dist/internal/runtime/postgres-client.js +174 -36
  21. package/dist/internal/runtime/state.d.ts +5 -3
  22. package/dist/internal/runtime/state.js +1 -1
  23. package/dist/internal/runtime/vercel-client.d.ts +1 -1
  24. package/dist/internal/runtime/vercel-client.js +24 -5
  25. package/dist/internal/runtime/worker-client.d.ts +2 -2
  26. package/dist/internal/runtime/worker-client.js +1 -1
  27. package/dist/{local-RX10a_Sq.js → local-BR4W57HU.js} +146 -97
  28. package/dist/{local-B72WRWkS.d.ts → local-DUh8sGn-.d.ts} +1 -1
  29. package/dist/{operations-CoEXxfUW.js → operations-COCpSoua.js} +11 -6
  30. package/dist/{operation-definition-CqZkx1Rm.d.ts → operations-DSslh_vK.d.ts} +169 -1
  31. package/dist/operations.d.ts +13 -4
  32. package/dist/operations.js +98 -37
  33. package/dist/retries-BSAZru9n.js +27 -0
  34. package/dist/run-handler-B_RVXMs5.js +61 -0
  35. package/dist/runtime/hosted.d.ts +1 -1
  36. package/dist/runtime/hosted.js +32 -20
  37. package/dist/{state-DqxeV11k.js → state-C-t99I4Q.js} +9 -3
  38. package/dist/{types-BevEYrUV.d.ts → types-BPs6OsRv.d.ts} +1 -1
  39. package/dist/{types-BzPGPV9z.d.ts → types-rZHAtkK0.d.ts} +74 -12
  40. package/dist/{vercel-region-DEsR1YJQ.js → vercel-region-Bp3hwRBr.js} +1 -1
  41. package/dist/vite.d.ts +2 -155
  42. package/dist/vite.js +460 -68
  43. package/package.json +3 -3
@@ -16,6 +16,7 @@ interface CloudflareQueueSendBatchMessage<T = unknown> extends CloudflareQueueSe
16
16
  /** One job in `sendBatch()` of a Cloudflare QueueClient. */
17
17
  interface CloudflareQueueSendBatchItem<T = unknown> {
18
18
  body: T;
19
+ transaction?: never;
19
20
  contentType?: CloudflareQueueContentType;
20
21
  /** Time before delivery, such as `"1m"`. */
21
22
  delay?: Duration;
@@ -53,6 +54,8 @@ interface CloudflareQueueBatchHandlerOptions<T = unknown> {
53
54
  onMessage: (message: CloudflareQueueMessage<T>, batch: CloudflareQueueMessageBatch<T>) => unknown | Promise<unknown>;
54
55
  /** The Queue Definition name in delivery error reports. Defaults to the name that `batch.queue` encodes. */
55
56
  queue?: string;
57
+ /** The wait before a retry. The attempt limit is the consumer's `max_retries`, which the build sets from `retries.limit`. */
58
+ retries?: QueueRetries;
56
59
  }
57
60
  type VercelQueueRetryDirective = {
58
61
  acknowledge: true;
@@ -96,6 +99,7 @@ type VercelQueueBatchSendResult = {
96
99
  */
97
100
  interface VercelQueueSendBatchMessage<T = unknown> {
98
101
  body: T;
102
+ transaction?: never;
99
103
  /** Time before delivery, such as `"1m"`. */
100
104
  delay?: Duration;
101
105
  idempotencyKey?: string;
@@ -104,6 +108,7 @@ interface VercelQueueSendBatchMessage<T = unknown> {
104
108
  }
105
109
  /** Options that apply to every job of a Vercel `sendBatch()` unless the job sets its own. */
106
110
  interface VercelQueueSendBatchOptions {
111
+ transaction?: never;
107
112
  delay?: Duration;
108
113
  retention?: Duration | number;
109
114
  }
@@ -151,8 +156,8 @@ interface LocalQueueDriverOptions extends QueueSharedOptions {
151
156
  rules?: LocalQueueRules;
152
157
  }
153
158
  /**
154
- * The driver whose documented delivery rules the local driver follows: attempts, retry delay, the largest delay,
155
- * payload, and batch, and the enqueue options it accepts. In `vite dev`, a Cloudflare or Vercel app runs its jobs
159
+ * The driver whose documented limits the local driver follows: the largest delay, payload, and batch, and the
160
+ * enqueue options it accepts. Retries follow `retries` of the Queue Definition on every driver. In `vite dev`, a Cloudflare or Vercel app runs its jobs
156
161
  * with the local driver and the rules of its hosted driver.
157
162
  */
158
163
  type LocalQueueRules = QueueDriver;
@@ -200,8 +205,23 @@ interface QueueJob<TPayload = unknown> {
200
205
  /** Delivery metadata of the provider, when it gives some. */
201
206
  metadata?: unknown;
202
207
  payload: TPayload;
208
+ /** Aborts when ViteHub enforces the attempt's `timeout`. A host can stop the attempt first. Pass it to `fetch()` and other calls. */
209
+ signal: AbortSignal;
203
210
  }
204
211
  type QueueHandler<TPayload = unknown, TResult = unknown> = (job: QueueJob<TPayload>) => TResult;
212
+ /** How the wait between retries grows: `exponential` doubles it after each retry, `constant` keeps it. */
213
+ type QueueRetryBackoff = "constant" | "exponential";
214
+ /** When a failed job runs again. The defaults are `{ limit: 3, delay: '10 seconds', backoff: 'exponential' }`. */
215
+ interface QueueRetries {
216
+ backoff?: QueueRetryBackoff;
217
+ /** How long to wait before the first retry, such as `"30s"`. A wait is never longer than 12 hours. */
218
+ delay?: Duration;
219
+ /**
220
+ * How many times to run the job again after the first attempt fails, from 0 to 100. `0` runs it once. On Cloudflare,
221
+ * write it as a number literal, because the build reads it from the file to set the consumer's `max_retries`.
222
+ */
223
+ limit?: number;
224
+ }
205
225
  /** Options of `defineQueue()`, next to `handler`. */
206
226
  interface QueueDefinitionOptions {
207
227
  cache?: boolean;
@@ -216,6 +236,14 @@ interface QueueDefinitionOptions {
216
236
  name: string;
217
237
  }) => unknown | Promise<unknown>;
218
238
  onError?: CloudflareQueueBatchHandlerOptions["onError"];
239
+ /** When a failed job runs again. Every provider uses it. See `QueueRetries` for the defaults. */
240
+ retries?: QueueRetries;
241
+ /**
242
+ * ViteHub's time limit for one attempt, such as `"30s"` or `"5 minutes"`. Default `"15 minutes"`. When enforced,
243
+ * `job.signal` aborts and the attempt fails with `QUEUE_JOB_TIMEOUT`. A host can stop the attempt before either
244
+ * occurs. A handler that ignores the signal keeps running and can overlap its retry.
245
+ */
246
+ timeout?: Duration;
219
247
  }
220
248
  /** A Queue Definition: `defineQueue({ handler, ...options })`. */
221
249
  interface QueueDefinition<TPayload = unknown, TResult = unknown> extends QueueDefinitionOptions {
@@ -233,28 +261,46 @@ interface QueueEnqueueOptions {
233
261
  region?: string;
234
262
  /** How long the provider keeps the job: a duration such as `"1h"`, or a number of seconds. */
235
263
  retention?: Duration | number;
264
+ /**
265
+ * The postgres provider only: a Drizzle transaction of a Postgres Database, the `tx` of `db.transaction(async tx => ...)`.
266
+ * The job is saved in that transaction, so it exists only when the transaction commits, and it runs after the commit.
267
+ * The jobs table must be in the same database as the transaction, else `send()` returns
268
+ * `QUEUE_TRANSACTION_DATABASE_MISMATCH`. Other providers return `QUEUE_UNSUPPORTED_OPTION`.
269
+ */
270
+ transaction?: QueueTransaction;
271
+ }
272
+ /**
273
+ * A live Drizzle transaction of a ViteHub Postgres Database, on postgres.js or node-postgres. A Drizzle Database is not a
274
+ * transaction: it has no `rollback()`.
275
+ */
276
+ interface QueueTransaction {
277
+ readonly _: {
278
+ readonly session: object;
279
+ };
280
+ rollback: () => never;
236
281
  }
237
282
  type QueueSendResult = {
238
283
  messageId?: string;
239
284
  status: "queued";
240
285
  };
241
286
  /** Codes that Queue operations return in the `[error, data]` tuple. Config errors throw instead. */
242
- type QueueErrorCode = "QUEUE_PROVIDER_FAILED" | "QUEUE_PROVIDER_INVALID_RESPONSE" | "QUEUE_UNSUPPORTED_OPTION" | "RUNTIME_DURATION_INVALID";
287
+ type QueueErrorCode = "QUEUE_PROVIDER_FAILED" | "QUEUE_PROVIDER_INVALID_RESPONSE" | "QUEUE_TRANSACTION_DATABASE_MISMATCH" | "QUEUE_UNSUPPORTED_OPTION" | "RUNTIME_DURATION_INVALID";
243
288
  /** Codes that `send()` and `sendBatch()` of a Cloudflare QueueClient return. */
244
289
  type CloudflareQueueErrorCode = "QUEUE_PROVIDER_FAILED" | "QUEUE_UNSUPPORTED_OPTION" | "RUNTIME_DURATION_INVALID";
245
290
  /** Codes that `send()` and `sendBatch()` of a local QueueClient return. */
246
291
  type LocalQueueErrorCode = "QUEUE_PROVIDER_FAILED" | "QUEUE_UNSUPPORTED_OPTION" | "RUNTIME_DURATION_INVALID";
247
292
  /** Codes that `send()` and `sendBatch()` of a postgres QueueClient return. */
248
- type PostgresQueueErrorCode = "QUEUE_PROVIDER_FAILED" | "QUEUE_UNSUPPORTED_OPTION" | "RUNTIME_DURATION_INVALID";
293
+ type PostgresQueueErrorCode = "QUEUE_PROVIDER_FAILED" | "QUEUE_TRANSACTION_DATABASE_MISMATCH" | "QUEUE_UNSUPPORTED_OPTION" | "RUNTIME_DURATION_INVALID";
249
294
  /** One job in `sendBatch()` of a local or postgres QueueClient. */
250
295
  interface LocalQueueSendBatchMessage<T = unknown> {
251
296
  body: T;
297
+ transaction?: never;
252
298
  /** Time before delivery, such as `"1m"`. */
253
299
  delay?: Duration;
254
300
  idempotencyKey?: string;
255
301
  }
256
302
  /** Codes that `send()` and `sendBatch()` of a Vercel QueueClient return. */
257
- type VercelQueueErrorCode = QueueErrorCode;
303
+ type VercelQueueErrorCode = Exclude<QueueErrorCode, "QUEUE_TRANSACTION_DATABASE_MISMATCH">;
258
304
  interface QueueClientBase<P extends QueueDriver = QueueDriver> {
259
305
  readonly native: unknown;
260
306
  readonly provider: P;
@@ -266,7 +312,9 @@ interface CloudflareQueueClient<TPayload = unknown> extends QueueClientBase<"clo
266
312
  readonly binding: CloudflareQueueBinding;
267
313
  createBatchHandler: <TMessage = unknown>(options: CloudflareQueueBatchHandlerOptions<TMessage>) => (batch: CloudflareQueueMessageBatch<TMessage>) => Promise<void>;
268
314
  send: (payload: TPayload, options?: QueueEnqueueOptions) => Promise<Result<QueueSendResult, CloudflareQueueErrorCode>>;
269
- sendBatch: (items: CloudflareQueueSendBatchItem<TPayload>[], options?: QueueEnqueueOptions) => Promise<Result<QueueSendResult[], CloudflareQueueErrorCode>>;
315
+ sendBatch: (items: CloudflareQueueSendBatchItem<TPayload>[], options?: Omit<QueueEnqueueOptions, "transaction"> & {
316
+ transaction?: never;
317
+ }) => Promise<Result<QueueSendResult[], CloudflareQueueErrorCode>>;
270
318
  }
271
319
  /**
272
320
  * `send()` and `sendBatch()` return `[error, data]`. `error.code` is one of the codes in the method type.
@@ -292,6 +340,7 @@ interface LocalQueueClient<TPayload = unknown> extends QueueClientBase<"local">
292
340
  send: (payload: TPayload, options?: QueueEnqueueOptions) => Promise<Result<QueueSendResult, LocalQueueErrorCode>>;
293
341
  sendBatch: (items: LocalQueueSendBatchMessage<TPayload>[], options?: {
294
342
  delay?: Duration;
343
+ transaction?: never;
295
344
  }) => Promise<Result<QueueSendResult[], LocalQueueErrorCode>>;
296
345
  }
297
346
  /**
@@ -304,6 +353,7 @@ interface PostgresQueueClient<TPayload = unknown> extends QueueClientBase<"postg
304
353
  /** Inserts all jobs in one transaction. When one insert fails, no job of the batch is stored. */
305
354
  sendBatch: (items: LocalQueueSendBatchMessage<TPayload>[], options?: {
306
355
  delay?: Duration;
356
+ transaction?: never;
307
357
  }) => Promise<Result<QueueSendResult[], PostgresQueueErrorCode>>;
308
358
  }
309
359
  interface QueueClientMap<TPayload> {
@@ -313,6 +363,11 @@ interface QueueClientMap<TPayload> {
313
363
  vercel: VercelQueueClient<TPayload>;
314
364
  }
315
365
  type QueueClient<P extends QueueDriver = QueueDriver, TPayload = unknown> = QueueClientMap<TPayload>[P];
366
+ /**
367
+ * The client that `useQueue()` and `dynamicQueue.use()` return. `send()` and `sendBatch()` load the provider on the
368
+ * first call. `provider` is `local` in `vite dev` and in tests. Use `createQueueClient()` for the provider's `native` object.
369
+ */
370
+ type NamedQueueClient<P extends QueueDriver = QueueDriver, TPayload = unknown> = P extends QueueDriver ? Pick<QueueClientMap<TPayload>[P], "provider" | "send" | "sendBatch"> : never;
316
371
  /** Discovered Queue Definitions. Generated declarations extend this interface. */
317
372
  interface QueueRegistry {}
318
373
  type QueueName = Extract<keyof QueueRegistry, string>;
@@ -345,8 +400,6 @@ interface QueueDeliveryErrorDetails {
345
400
  }
346
401
  /** The delivery rules that the local engine applies. */
347
402
  interface LocalQueueDeliveryRules {
348
- /** Deliveries before a failing job becomes a dead job. `Infinity` retries until the job expires. */
349
- maxAttempts: number;
350
403
  /** Largest `delay`, in seconds, that a send accepts. */
351
404
  maxDelaySeconds?: number;
352
405
  /** Largest number of jobs in one `sendBatch()`. */
@@ -361,12 +414,10 @@ interface LocalQueueDeliveryRules {
361
414
  maxSeconds: number;
362
415
  minSeconds: number;
363
416
  };
364
- /** Milliseconds before the next delivery after `attempts` failed deliveries. */
365
- retryDelayMs: (attempts: number) => number;
366
417
  /** The provider whose documented rules these are. */
367
418
  rules: LocalQueueRules;
368
419
  /** Enqueue options that fail with `QUEUE_UNSUPPORTED_OPTION`. */
369
- unsupportedOptions: readonly ("contentType" | "idempotencyKey" | "region" | "retention")[];
420
+ unsupportedOptions: readonly ("contentType" | "idempotencyKey" | "region" | "retention" | "transaction")[];
370
421
  }
371
422
  interface LocalQueueStatus {
372
423
  /** Absolute directory of the local provider, or the Postgres table and database, without the password. */
@@ -418,6 +469,12 @@ interface LocalQueueClearFilter {
418
469
  queue?: string;
419
470
  state?: "dead" | "pending";
420
471
  }
472
+ /** What `retryDead()` did: how many dead jobs run again, and the message IDs of the dead jobs that it skipped. */
473
+ interface QueueRetryDeadResult {
474
+ retried: number;
475
+ /** Dead jobs whose `idempotencyKey` a live job or a newer dead job holds. They stay dead. */
476
+ skipped: string[];
477
+ }
421
478
  /**
422
479
  * The stored jobs of a provider that ViteHub can read: the local and the postgres provider. The Queue operations,
423
480
  * so `vitehub queue jobs` and the Console, read jobs through it.
@@ -438,7 +495,12 @@ interface QueueJobStore {
438
495
  * live job holds the `idempotencyKey` of the dead job.
439
496
  */
440
497
  retry: (id: string) => Promise<LocalQueueJob | "duplicate" | "missing" | "running">;
498
+ /**
499
+ * Gives every dead job of the queue a new set of attempts and runs it now, in batches of 500. A dead job is skipped
500
+ * when a live job or a newer dead job holds its `idempotencyKey`.
501
+ */
502
+ retryDead: (queue: string) => Promise<QueueRetryDeadResult>;
441
503
  readonly rules: LocalQueueDeliveryRules;
442
504
  status: () => Promise<LocalQueueStatus>;
443
505
  }
444
- export { QueueJobStore as A, VercelQueueCallbackOptions as B, QueueDefinitionRegistry as C, QueueErrorCode as D, QueueEnqueueOptions as E, QueueRegistry as F, VercelQueueSendBatchMessage as G, VercelQueueErrorCode as H, QueueSendResult as I, VercelQueueSendBatchOptions as K, ResolvedLocalQueueOptions as L, QueueName as M, QueueOptions as N, QueueHandler as O, QueuePayload as P, ResolvedQueueOptions as R, QueueDefinitionOptions as S, QueueDriverOptions as T, VercelQueueRetryDirective as U, VercelQueueClient as V, VercelQueueRetryHandler as W, PostgresQueueClient as _, CloudflareQueueMessageBatch as a, QueueClient as b, LocalQueueClient as c, LocalQueueJob as d, LocalQueueJobDetail as f, NodeQueueDriver as g, LocalQueueStatus as h, CloudflareQueueMessage as i, QueueModuleDriverOptions as j, QueueJob as k, LocalQueueDriverOptions as l, LocalQueueSendBatchMessage as m, CloudflareQueueClient as n, CloudflareQueueSendBatchItem as o, LocalQueueRules as p, VercelQueueSendResult as q, CloudflareQueueErrorCode as r, DiscoveredQueueDefinition as s, CloudflareQueueBatchHandlerOptions as t, LocalQueueErrorCode as u, PostgresQueueDriverOptions as v, QueueDriver as w, QueueDefinition as x, PostgresQueueErrorCode as y, VercelQueueBatchSendResult as z };
506
+ export { QueueJob as A, QueueSendResult as B, QueueDefinitionOptions as C, QueueEnqueueOptions as D, QueueDriverOptions as E, QueuePayload as F, VercelQueueCallbackOptions as G, ResolvedLocalQueueOptions as H, QueueRegistry as I, VercelQueueRetryDirective as J, VercelQueueClient as K, QueueRetries as L, QueueModuleDriverOptions as M, QueueName as N, QueueErrorCode as O, QueueOptions as P, VercelQueueSendResult as Q, QueueRetryBackoff as R, QueueDefinition as S, QueueDriver as T, ResolvedQueueOptions as U, QueueTransaction as V, VercelQueueBatchSendResult as W, VercelQueueSendBatchMessage as X, VercelQueueRetryHandler as Y, VercelQueueSendBatchOptions as Z, NodeQueueDriver as _, CloudflareQueueMessageBatch as a, PostgresQueueErrorCode as b, LocalQueueClient as c, LocalQueueJob as d, LocalQueueJobDetail as f, NamedQueueClient as g, LocalQueueStatus as h, CloudflareQueueMessage as i, QueueJobStore as j, QueueHandler as k, LocalQueueDriverOptions as l, LocalQueueSendBatchMessage as m, CloudflareQueueClient as n, CloudflareQueueSendBatchItem as o, LocalQueueRules as p, VercelQueueErrorCode as q, CloudflareQueueErrorCode as r, DiscoveredQueueDefinition as s, CloudflareQueueBatchHandlerOptions as t, LocalQueueErrorCode as u, PostgresQueueClient as v, QueueDefinitionRegistry as w, QueueClient as x, PostgresQueueDriverOptions as y, QueueRetryDeadResult as z };
@@ -1,4 +1,4 @@
1
- import { a as getQueueRuntimeEvent } from "./state-DqxeV11k.js";
1
+ import { a as getQueueRuntimeEvent } from "./state-C-t99I4Q.js";
2
2
  // src/internal/vercel-region.ts
3
3
  function readHeader(headers, name) {
4
4
  const get = headers?.get;
package/dist/vite.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- import { N as QueueOptions, s as DiscoveredQueueDefinition } from "./types-BzPGPV9z.js";
2
- import { n as OperationContributionEntry } from "./operation-definition-CqZkx1Rm.js";
3
- import "nostics";
1
+ import { P as QueueOptions, s as DiscoveredQueueDefinition } from "./types-rZHAtkK0.js";
2
+ import { n as OperationContributionEntry, r as ViteHubCliContributor } from "./operations-DSslh_vK.js";
4
3
  import { Plugin, UserConfig } from "vite";
5
4
  /** The host that ViteHub builds for: the `host` option of `vitehub()` and the value of `VITEHUB_HOST`. */
6
5
  type ViteHubHost = "cloudflare" | "deno" | "netlify" | "node" | "vercel";
@@ -112,158 +111,6 @@ interface DurabilityPluginMetadata {
112
111
  /** Called after Vite resolves the config. Return an empty list when the primitive is off. */
113
112
  durability?: () => readonly DurabilityContribution[];
114
113
  }
115
- interface HostCliRunResult {
116
- exitCode: number | null;
117
- stdout: string;
118
- stderr?: string;
119
- }
120
- /** Runs a host CLI with an argument array and no shell, and returns its stdout. */
121
- type HostCliRun = (command: string, args: readonly string[], options: {
122
- cwd?: string;
123
- env: NodeJS.ProcessEnv;
124
- signal?: AbortSignal;
125
- }) => Promise<HostCliRunResult>;
126
- type ProvisionProvider = "cloudflare" | "vercel";
127
- interface ProvisionLogger {
128
- log: (message: string) => void;
129
- warn: (message: string) => void;
130
- }
131
- interface ProvisionContext {
132
- env: Record<string, string | undefined>;
133
- fetch: typeof globalThis.fetch;
134
- logger: ProvisionLogger;
135
- markPlanUnchecked?: () => void;
136
- }
137
- interface ProvisionState {
138
- cloudflare?: Record<string, Record<string, string>>;
139
- vercel?: Record<string, Record<string, string>>;
140
- }
141
- interface ProvisionResult {
142
- ids?: ProvisionState;
143
- }
144
- interface ProvisionAction {
145
- kind: string;
146
- name: string;
147
- exists: boolean;
148
- pending?: boolean;
149
- /** Describes the change that `apply()` makes to an existing resource, for example `read replication: disabled -> auto`. */
150
- update?: string;
151
- apply: () => Promise<ProvisionResult>;
152
- }
153
- /** One Provision State category that a step records, with the keys that current Definitions use. */
154
- interface ProvisionStateCategory {
155
- category: string;
156
- declared: readonly string[];
157
- /** Provider resource identity for shared-resource checks. Return undefined when the recorded id lacks enough information. Defaults to the recorded id. */
158
- resourceIdentity?: (entry: {
159
- id: string;
160
- key: string;
161
- }) => string | undefined;
162
- /** Identity expected by current config for a declared key. Omit when it cannot be established without provider access. */
163
- declaredResourceIdentity?: (key: string) => string | undefined;
164
- /** Tells a developer how to delete the resource of a recorded entry that no Definition declares. ViteHub never runs it. */
165
- deleteHint: (entry: {
166
- id: string;
167
- key: string;
168
- }) => string;
169
- }
170
- interface ProvisionStep {
171
- id: string;
172
- provider: ProvisionProvider;
173
- plan: (context: ProvisionContext) => Promise<ProvisionAction[]>;
174
- /** Describes the Provision State this step records. Runs without network access, also without credentials. */
175
- recordedState?: (context: Pick<ProvisionContext, "env">) => ProvisionStateCategory[];
176
- }
177
- interface ViteHubCliStreams {
178
- stderr: {
179
- write: (chunk: string | Uint8Array, encoding?: BufferEncoding | StdoutWriteCallback, callback?: StdoutWriteCallback) => unknown;
180
- };
181
- stdout: {
182
- write: (chunk: string | Uint8Array) => unknown;
183
- };
184
- }
185
- interface ViteHubCliSpawnResult {
186
- exitCode: number | null;
187
- signal?: NodeJS.Signals | null;
188
- /** The output of the child when `stdout` is `pipe`. */
189
- stdout?: string;
190
- }
191
- interface ViteHubCliSpawnOptions {
192
- cwd?: string;
193
- env?: NodeJS.ProcessEnv;
194
- stderr?: "inherit" | "pipe";
195
- /** `ignore` gives the child no terminal input, so a tool that asks questions on a terminal does not ask. */
196
- stdin?: "ignore" | "inherit";
197
- /** `stderr` sends the child's stdout to the parent's stderr, so the parent's stdout keeps only its own output. */
198
- stdout?: "inherit" | "pipe" | "stderr";
199
- }
200
- type ViteHubCliSpawn = (command: string, args: string[], options?: ViteHubCliSpawnOptions) => Promise<ViteHubCliSpawnResult>;
201
- interface ViteHubCliContext extends ViteHubCliStreams {
202
- cwd: string;
203
- env: NodeJS.ProcessEnv;
204
- /** Replaces `fetch` for commands that call a running app, so tests need no server. */
205
- fetch?: typeof fetch;
206
- /**
207
- * Loads the project config for a build on `host` and returns the Provision Steps of its plugins, with the config root
208
- * that holds `.vitehub/provision.json`. Setup commands, such as `vitehub deploy`, call it only when they need the
209
- * steps, so they still run on a config that throws.
210
- */
211
- loadProvision?: (host: ProvisionProvider) => Promise<ViteHubCliProvision>;
212
- /** The deployed app's public origin from `vitehub({ publicUrl })`, when it is one string. */
213
- publicUrl?: string;
214
- rootDir: string;
215
- /** Replaces the host CLI runner, such as for `wrangler auth token`, so tests need no real host CLI. */
216
- runHostCli?: HostCliRun;
217
- spawn: ViteHubCliSpawn;
218
- }
219
- interface ViteHubCliProvision {
220
- rootDir: string;
221
- steps: readonly ProvisionStep[];
222
- }
223
- interface ViteHubCliFeature {
224
- description?: string;
225
- name: string;
226
- run: (args: string[], context: ViteHubCliContext) => Promise<number | void> | number | void;
227
- usage?: string;
228
- }
229
- interface ViteHubCliCommandNamespace {
230
- /** The feature that `vitehub <name>` runs when the next argument names no other feature. It gets every argument after `<name>`. */
231
- defaultFeature?: string;
232
- description?: string;
233
- features: ViteHubCliFeature[];
234
- name: string;
235
- }
236
- /** The flags of `vitehub seed` that every seed gets. */
237
- interface ViteHubCliSeedOptions {
238
- /** `--database <name>`: seed only this Database. Only the Database seed runs. */
239
- database?: string;
240
- /** The process environment with the env files of `--stage`. */
241
- env: NodeJS.ProcessEnv;
242
- /** `--remote`: write the deployed stores. Without it, a seed writes only local data. */
243
- remote: boolean;
244
- /** `--timeout <ms>` of each call to the app. */
245
- timeout?: number;
246
- /** The deployed app with `--remote`: `--url`, or `vitehub({ publicUrl })`. */
247
- url?: string;
248
- }
249
- /** The seed files of one primitive, which `vitehub seed` writes. */
250
- interface ViteHubCliSeed {
251
- /** Where the seed files are, such as `server/seed/kv/`. `vitehub seed` names it when it finds no seed files. */
252
- files: string;
253
- /** The primitive, such as `database` or `kv`. `vitehub seed <name>` runs only this seed. */
254
- name: string;
255
- /** Writes the seed files and returns the exit code, or `undefined` when the project has no seed files for this primitive. */
256
- run: (options: ViteHubCliSeedOptions, context: ViteHubCliContext) => Promise<number | undefined>;
257
- }
258
- interface ViteHubCliContributor {
259
- namespaces: ViteHubCliCommandNamespace[];
260
- provision?: ProvisionStep[];
261
- /** The seed files that `vitehub seed` writes. */
262
- seeds?: ViteHubCliSeed[];
263
- /** The deployed app's public origin. Commands receive it as `context.publicUrl`. */
264
- publicUrl?: string;
265
- }
266
- type StdoutWriteCallback = (error?: Error | null) => void;
267
114
  /** Module and export that hold a contributed Definition. */
268
115
  interface ContributionDefinition {
269
116
  /**