@vite-hub/queue 0.0.4 → 0.3.1

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 (63) hide show
  1. package/README.md +170 -31
  2. package/dist/client-Cj50hQYp.js +180 -0
  3. package/dist/cloudflare-client-DYsAjM8B.js +140 -0
  4. package/dist/{cloudflare-resource-name-R8zYQiZb.js → cloudflare-resource-name-BJHYRCMQ.js} +3 -2
  5. package/dist/config-BcTDeQIP.js +105 -0
  6. package/dist/durability.d.ts +13 -0
  7. package/dist/durability.js +47 -0
  8. package/dist/error-diagnostics-BRartC1z.js +181 -0
  9. package/dist/error-diagnostics-DSkCJC_L.js +266 -0
  10. package/dist/errors-DL3pcNO4.js +77 -0
  11. package/dist/handler-response-By0Fpspv.js +61 -0
  12. package/dist/index.d.ts +29 -83
  13. package/dist/index.js +24 -11
  14. package/dist/inspect-PeuckaC3.js +30 -0
  15. package/dist/internal/runtime/cloudflare-client.d.ts +3 -5
  16. package/dist/internal/runtime/cloudflare-client.js +1 -1
  17. package/dist/internal/runtime/cloudflare-vite.d.ts +30 -2
  18. package/dist/internal/runtime/cloudflare-vite.js +111 -1
  19. package/dist/internal/runtime/local-client.d.ts +17 -0
  20. package/dist/internal/runtime/local-client.js +71 -0
  21. package/dist/internal/runtime/postgres-client.d.ts +46 -0
  22. package/dist/internal/runtime/postgres-client.js +560 -0
  23. package/dist/internal/runtime/state.d.ts +23 -15
  24. package/dist/internal/runtime/state.js +2 -2
  25. package/dist/internal/runtime/vercel-client.d.ts +3 -5
  26. package/dist/internal/runtime/vercel-client.js +120 -63
  27. package/dist/internal/runtime/worker-client.d.ts +6 -0
  28. package/dist/internal/runtime/worker-client.js +11 -0
  29. package/dist/invariant-D8_xBqmZ.js +22 -0
  30. package/dist/local-CGckW5nr.js +540 -0
  31. package/dist/local-CVVRVWeR.d.ts +22 -0
  32. package/dist/object-D8pRdprI.js +15 -0
  33. package/dist/operation-definition-8gOe-ubD.d.ts +234 -0
  34. package/dist/operations-Crwjca91.js +124 -0
  35. package/dist/operations.d.ts +69 -0
  36. package/dist/operations.js +1344 -0
  37. package/dist/runtime/hosted.d.ts +5 -9
  38. package/dist/runtime/hosted.js +21 -23
  39. package/dist/state-mVA2X_wa.js +196 -0
  40. package/dist/types-BD-8U9zS.d.ts +420 -0
  41. package/dist/types-DS_rqu9l.d.ts +10 -0
  42. package/dist/vercel-region-D1CivgdA.js +26 -0
  43. package/dist/virtual.d.ts +4 -0
  44. package/dist/vite.d.ts +655 -40
  45. package/dist/vite.js +2071 -2
  46. package/package.json +66 -30
  47. package/dist/_app-D29AwUdD.d.ts +0 -11
  48. package/dist/app-BZpq2cwC.js +0 -10
  49. package/dist/client-DG9fweET.js +0 -93
  50. package/dist/cloudflare-client-w5vphoNn.js +0 -99
  51. package/dist/cloudflare-vite-BlQfDVZD.js +0 -97
  52. package/dist/cloudflare-vite-nFPor46-.d.ts +0 -24
  53. package/dist/config-CNgAeIND.js +0 -53
  54. package/dist/delivery-error-glgYWC9g.js +0 -45
  55. package/dist/errors-CjuG_qr-.js +0 -108
  56. package/dist/internal/runtime/vercel-vite.d.ts +0 -20
  57. package/dist/internal/runtime/vercel-vite.js +0 -49
  58. package/dist/nuxt.d.ts +0 -19
  59. package/dist/nuxt.js +0 -30
  60. package/dist/state-Cno9O7YS.js +0 -110
  61. package/dist/types-CwvoMATy.d.ts +0 -169
  62. package/dist/vercel-runtime-package-copy-CEToJtCu.js +0 -116
  63. package/dist/vite-DOuoen0w.js +0 -4414
@@ -1,45 +1,86 @@
1
- import { i as isQueueBoundaryIdentity, l as normalizeQueueEnqueueInput, n as createQueueError, s as runQueueProviderOperation } from "../../errors-CjuG_qr-.js";
2
- import { a as getQueueRuntimeEvent } from "../../state-Cno9O7YS.js";
1
+ import { t as queueErrorDiagnostics } from "../../error-diagnostics-BRartC1z.js";
2
+ import { c as createQueueMessageId, l as normalizeQueueEnqueueInput, o as queueProviderResult, s as queueUnsupportedOptionsError } from "../../errors-DL3pcNO4.js";
3
+ import { t as resolveVercelQueueRegion } from "../../vercel-region-D1CivgdA.js";
3
4
  //#region src/providers/vercel.ts
4
- function readHeader(headers, name) {
5
- if (!headers) return;
6
- if (headers instanceof Headers) return headers.get(name) || void 0;
7
- const value = headers[name] ?? headers[name.toLowerCase()];
8
- if (typeof value === "string") return value;
9
- if (Array.isArray(value)) return typeof value[0] === "string" ? value[0] : void 0;
5
+ function invalidVercelSendResponse(cause) {
6
+ return [queueErrorDiagnostics.QUEUE_PROVIDER_INVALID_RESPONSE({ cause }), void 0];
10
7
  }
11
- function parseRegionFromVercelId(value) {
12
- if (!value) return;
13
- return value.match(/^([a-z0-9]+)::/i)?.[1]?.toLowerCase();
8
+ function isVercelQueueSDK(value) {
9
+ if (value === null || typeof value !== "object") return false;
10
+ try {
11
+ if (Array.isArray(value)) return false;
12
+ } catch {
13
+ return false;
14
+ }
15
+ const hasCallableMethod = (name) => {
16
+ let owner = value;
17
+ while (owner && owner !== Object.prototype) {
18
+ let descriptor;
19
+ try {
20
+ descriptor = Object.getOwnPropertyDescriptor(owner, name);
21
+ } catch {
22
+ return false;
23
+ }
24
+ if (descriptor) {
25
+ if (owner !== value) {
26
+ let constructor;
27
+ try {
28
+ constructor = Object.getOwnPropertyDescriptor(owner, "constructor");
29
+ if (!constructor || !("value" in constructor) || typeof constructor.value !== "function" || constructor.value.prototype !== owner) return false;
30
+ } catch {
31
+ return false;
32
+ }
33
+ }
34
+ if ("value" in descriptor) return typeof descriptor.value === "function";
35
+ try {
36
+ return typeof Reflect.get(value, name) === "function";
37
+ } catch {
38
+ return false;
39
+ }
40
+ }
41
+ try {
42
+ owner = Object.getPrototypeOf(owner);
43
+ } catch {
44
+ return false;
45
+ }
46
+ }
47
+ return false;
48
+ };
49
+ return hasCallableMethod("send") && hasCallableMethod("handleCallback");
14
50
  }
15
- function resolveVercelRegion(explicitRegion) {
16
- if (explicitRegion) return explicitRegion;
17
- if (typeof process.env.QUEUE_REGION === "string" && process.env.QUEUE_REGION) return process.env.QUEUE_REGION;
18
- if (typeof process.env.VERCEL_REGION === "string" && process.env.VERCEL_REGION) return process.env.VERCEL_REGION;
19
- const event = getQueueRuntimeEvent();
20
- const requestHeaders = event?.request instanceof Request ? event.request.headers : event?.req?.headers ?? event?.node?.req?.headers;
21
- return readHeader(requestHeaders, "ce-vqsregion") || parseRegionFromVercelId(readHeader(requestHeaders, "x-vercel-id"));
51
+ /** The cause of `QUEUE_PROVIDER_FAILED` when Vercel rejects jobs of a batch. `results` has each job's result in input order. */
52
+ function batchRejected(results) {
53
+ const failed = results.filter((result) => result.status === "failed").length;
54
+ return Object.assign(queueErrorDiagnostics.QUEUE_VERCEL_BATCH_REJECTED({
55
+ failed,
56
+ total: results.length
57
+ }), { results });
22
58
  }
23
- function invalidVercelSendResponse(cause) {
24
- throw createQueueError("QUEUE_PROVIDER_RESPONSE_INVALID", {
25
- cause,
26
- details: {
27
- operation: "send",
28
- provider: "vercel"
29
- }
30
- });
59
+ function parseVercelBatchResults(value, length) {
60
+ if (!Array.isArray(value) || value.length !== length) return invalidVercelSendResponse(value);
61
+ const results = [];
62
+ for (const entry of value) {
63
+ const [error, messageId] = parseVercelMessageId(entry);
64
+ if (error) return [error, void 0];
65
+ results.push({
66
+ status: "queued",
67
+ messageId
68
+ });
69
+ }
70
+ return [void 0, results];
31
71
  }
32
72
  function parseVercelMessageId(value) {
33
- if (!value || typeof value !== "object" || Array.isArray(value)) invalidVercelSendResponse(value);
73
+ if (!value || typeof value !== "object" || Array.isArray(value)) return invalidVercelSendResponse(value);
34
74
  let messageId;
35
75
  try {
76
+ if (!Object.prototype.hasOwnProperty.call(value, "messageId")) return invalidVercelSendResponse(value);
36
77
  messageId = Reflect.get(value, "messageId");
37
78
  } catch (cause) {
38
- invalidVercelSendResponse(cause);
79
+ return invalidVercelSendResponse(cause);
39
80
  }
40
- if (messageId === void 0 || messageId === null) return;
41
- if (typeof messageId !== "string" || !messageId || messageId.length > 128 || messageId.trim() !== messageId) invalidVercelSendResponse(value);
42
- return messageId;
81
+ if (messageId === null) return [void 0, void 0];
82
+ if (typeof messageId !== "string" || !messageId || messageId.length > 128 || messageId.trim() !== messageId) return invalidVercelSendResponse(value);
83
+ return [void 0, messageId];
43
84
  }
44
85
  async function loadVercelQueueClient(region) {
45
86
  let module;
@@ -56,50 +97,63 @@ async function loadVercelQueueClient(region) {
56
97
  module = await import(specifier);
57
98
  }
58
99
  }
59
- } catch (error) {
60
- if (isQueueBoundaryIdentity(error)) throw error;
61
- throw createQueueError("VERCEL_QUEUE_SDK_LOAD_FAILED", {
62
- cause: error,
63
- details: {
64
- operation: "load-sdk",
65
- provider: "vercel"
66
- }
67
- });
100
+ } catch (cause) {
101
+ throw queueErrorDiagnostics.QUEUE_VERCEL_SDK_UNAVAILABLE({ cause });
68
102
  }
69
- const resolvedRegion = resolveVercelRegion(region);
70
- if ("QueueClient" in module && typeof module.QueueClient === "function") {
71
- if (!resolvedRegion) throw createQueueError("VERCEL_QUEUE_REGION_REQUIRED", { details: { provider: "vercel" } });
103
+ const { region: resolvedRegion } = resolveVercelQueueRegion(region);
104
+ if (Object.hasOwn(module, "QueueClient") && typeof module.QueueClient === "function") {
105
+ if (!resolvedRegion) throw queueErrorDiagnostics.QUEUE_MISSING_REGION();
72
106
  return new module.QueueClient({ region: resolvedRegion });
73
107
  }
74
- if (typeof module.send === "function" && typeof module.handleCallback === "function") return {
108
+ if (Object.hasOwn(module, "send") && Object.hasOwn(module, "handleCallback") && typeof module.send === "function" && typeof module.handleCallback === "function") return {
75
109
  handleCallback: module.handleCallback,
76
- send: module.send
110
+ send: module.send,
111
+ ...Object.hasOwn(module, "sendBatch") && typeof module.sendBatch === "function" ? { sendBatch: module.sendBatch } : {}
77
112
  };
78
- throw createQueueError("VERCEL_QUEUE_SDK_INVALID", { details: { provider: "vercel" } });
113
+ throw queueErrorDiagnostics.QUEUE_VERCEL_SDK_INVALID();
79
114
  }
80
115
  async function createVercelQueueClient(provider) {
81
116
  const topic = provider.topic;
82
- if (!topic) throw createQueueError("VERCEL_TOPIC_RESOLUTION_REQUIRED", { details: { provider: "vercel" } });
83
- const client = provider.client || await loadVercelQueueClient(provider.region);
117
+ if (!topic) throw queueErrorDiagnostics.QUEUE_MISSING_TOPIC();
118
+ const client = provider.client === void 0 ? await loadVercelQueueClient(provider.region) : provider.client;
119
+ if (!isVercelQueueSDK(client)) throw queueErrorDiagnostics.QUEUE_VERCEL_SDK_INVALID();
84
120
  return {
85
121
  provider: "vercel",
86
122
  native: client,
87
123
  topic,
88
- async send(input) {
89
- const normalized = normalizeQueueEnqueueInput(input);
90
- if (normalized.options.contentType !== void 0) throw createQueueError("VERCEL_UNSUPPORTED_ENQUEUE_OPTIONS", { details: {
91
- provider: "vercel",
92
- unsupported: ["contentType"]
93
- } });
94
- return {
124
+ async send(payload, options) {
125
+ const normalized = normalizeQueueEnqueueInput(payload, options);
126
+ const optionsError = queueUnsupportedOptionsError(normalized.options, "vercel");
127
+ if (optionsError) return [optionsError, void 0];
128
+ const [error, response] = await queueProviderResult("vercel", "send", () => client.send(topic, normalized.payload, {
129
+ delaySeconds: normalized.options.delaySeconds,
130
+ idempotencyKey: normalized.options.idempotencyKey || normalized.id,
131
+ region: normalized.options.region ?? provider.region,
132
+ retentionSeconds: normalized.options.retentionSeconds
133
+ }));
134
+ if (error) return [error, void 0];
135
+ const [responseError, messageId] = parseVercelMessageId(response);
136
+ if (responseError) return [responseError, void 0];
137
+ return [void 0, {
95
138
  status: "queued",
96
- messageId: await runQueueProviderOperation("vercel", "send", async () => parseVercelMessageId(await client.send(topic, normalized.payload, {
97
- delaySeconds: normalized.options.delaySeconds,
98
- idempotencyKey: normalized.options.idempotencyKey || normalized.id,
99
- region: normalized.options.region ?? provider.region,
100
- retentionSeconds: normalized.options.retentionSeconds
101
- })))
102
- };
139
+ messageId
140
+ }];
141
+ },
142
+ async sendBatch(items, options) {
143
+ const messages = items.map((item) => ({
144
+ delaySeconds: item.delaySeconds ?? options?.delaySeconds,
145
+ idempotencyKey: item.idempotencyKey || createQueueMessageId(),
146
+ payload: item.body,
147
+ retentionSeconds: item.retentionSeconds ?? options?.retentionSeconds
148
+ }));
149
+ const [error, response] = await queueProviderResult("vercel", "send-batch", async () => {
150
+ if (typeof client.sendBatch !== "function") throw queueErrorDiagnostics.QUEUE_VERCEL_SEND_BATCH_UNAVAILABLE();
151
+ const results = await client.sendBatch(topic, messages);
152
+ if (Array.isArray(results) && results.some((result) => result?.status === "failed")) throw batchRejected(results);
153
+ return results;
154
+ });
155
+ if (error) return [error, void 0];
156
+ return parseVercelBatchResults(response, messages.length);
103
157
  },
104
158
  callback: client.handleCallback.bind(client)
105
159
  };
@@ -107,7 +161,10 @@ async function createVercelQueueClient(provider) {
107
161
  //#endregion
108
162
  //#region src/internal/runtime/vercel-client.ts
109
163
  async function createVercelQueueRuntimeClient(options) {
110
- if (options.provider !== "vercel") throw new TypeError("[vitehub] Vercel Queue runtime received a non-Vercel provider.");
164
+ if (options.driver !== "vercel") throw queueErrorDiagnostics.QUEUE_DRIVER_MISMATCH({
165
+ driver: options.driver,
166
+ expected: "vercel"
167
+ });
111
168
  return await createVercelQueueClient(options);
112
169
  }
113
170
  //#endregion
@@ -0,0 +1,6 @@
1
+ import { y as QueueClient } from "../../types-BD-8U9zS.js";
2
+ import { t as QueueRuntimeDriverOptions } from "../../types-DS_rqu9l.js";
3
+ //#region src/internal/runtime/worker-client.d.ts
4
+ /** Worker package conditions keep the Node driver and its optional peer outside the build graph. */
5
+ export declare function createPostgresQueueRuntimeClient(options: QueueRuntimeDriverOptions): Promise<QueueClient>;
6
+ //#endregion
@@ -0,0 +1,11 @@
1
+ import { t as queueErrorDiagnostics } from "../../error-diagnostics-BRartC1z.js";
2
+ //#region src/internal/runtime/worker-client.ts
3
+ /** Worker package conditions keep the Node driver and its optional peer outside the build graph. */
4
+ async function createPostgresQueueRuntimeClient(options) {
5
+ throw queueErrorDiagnostics.QUEUE_DRIVER_MISMATCH({
6
+ driver: options.driver,
7
+ expected: "cloudflare"
8
+ });
9
+ }
10
+ //#endregion
11
+ export { createPostgresQueueRuntimeClient };
@@ -0,0 +1,22 @@
1
+ import { defineDiagnostics } from "nostics";
2
+ //#region ../internal/src/invariant.ts
3
+ const invariantDiagnostics = /*#__PURE__*/ defineDiagnostics({
4
+ docsBase: "https://vitehub.dev/docs/development/diagnostics",
5
+ codes: { INTERNAL_INVARIANT_FAILED: {
6
+ why: ({ what }) => `ViteHub reached a state that it does not expect: ${what}`,
7
+ fix: "This is a ViteHub bug. Open an issue at https://github.com/vite-hub/vitehub/issues with this error message and its stack trace."
8
+ } }
9
+ });
10
+ /**
11
+ * The error for a state that only a ViteHub bug can reach, such as an impossible branch. `what` says which rule broke.
12
+ * Use a code from the package catalog when a developer can cause the error or fix it.
13
+ */
14
+ function invariantError(what) {
15
+ return invariantDiagnostics.INTERNAL_INVARIANT_FAILED({ what });
16
+ }
17
+ /** Throws `INTERNAL_INVARIANT_FAILED` when `condition` is falsy. */
18
+ function invariant(condition, what) {
19
+ if (!condition) throw invariantError(what);
20
+ }
21
+ //#endregion
22
+ export { invariant as t };