@vercel/queue 0.0.0-alpha.32 → 0.0.0-alpha.34

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,196 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Standalone CLI script to discover queue handlers in development mode
5
- * This script is self-contained and doesn't import from the main package
6
- */
7
-
8
- const fs = require("fs");
9
- const path = require("path");
10
-
11
- function showHelp() {
12
- console.log(`
13
- @vercel/queue local-init - Initialize queue handlers in local development
14
-
15
- USAGE:
16
- npx vercel-queue-local-init [options]
17
-
18
- OPTIONS:
19
- --port <number> Port number for Next.js dev server (default: 3000)
20
- --config <path> Path to vercel.json file (default: ./vercel.json)
21
- --help, -h Show this help message
22
-
23
- EXAMPLES:
24
- npx vercel-queue-local-init
25
- npx vercel-queue-local-init --port 3001
26
- npx vercel-queue-local-init --config ./my-vercel.json
27
- `);
28
- }
29
-
30
- /**
31
- * Read vercel.json and extract function endpoints with queue triggers
32
- */
33
- function readVercelConfig(configPath = "./vercel.json") {
34
- try {
35
- const fullPath = path.resolve(configPath);
36
- const configContent = fs.readFileSync(fullPath, "utf-8");
37
- const config = JSON.parse(configContent);
38
-
39
- const routes = [];
40
-
41
- // Extract routes from functions with queue triggers
42
- if (config.functions) {
43
- for (const [functionPath, functionConfig] of Object.entries(
44
- config.functions,
45
- )) {
46
- if (
47
- functionConfig &&
48
- typeof functionConfig === "object" &&
49
- "experimentalTriggers" in functionConfig
50
- ) {
51
- const triggers = functionConfig.experimentalTriggers;
52
- if (Array.isArray(triggers)) {
53
- // Check if any trigger is a queue trigger
54
- const hasQueueTrigger = triggers.some(
55
- (trigger) =>
56
- trigger &&
57
- typeof trigger === "object" &&
58
- trigger.type === "queue/v1beta",
59
- );
60
-
61
- if (hasQueueTrigger) {
62
- // Convert file path to API route
63
- // Examples:
64
- // app/api/vm/queue/route.ts -> /api/vm/queue
65
- // app/(dashboard)/api/queue/route.ts -> /api/queue
66
- // app/(auth)/login/route.ts -> /login
67
- // src/app/api/queue/route.ts -> /api/queue
68
- // src/app/(admin)/settings/route.ts -> /settings
69
- // pages/api/queue.ts -> /api/queue
70
- // src/pages/api/queue.ts -> /api/queue
71
- // app/queue/route.ts -> /queue
72
- // src/app/webhooks/route.ts -> /webhooks
73
- let apiPath = functionPath;
74
-
75
- // Handle src/ prefix
76
- if (apiPath.startsWith("src/")) {
77
- apiPath = apiPath.replace("src/", "");
78
- }
79
-
80
- // Convert Next.js file paths to HTTP routes
81
- if (apiPath.startsWith("app/")) {
82
- // App Router: app/api/queue/route.ts -> /api/queue
83
- // App Router: app/queue/route.ts -> /queue
84
- apiPath = apiPath.replace("app/", "/");
85
- } else if (apiPath.startsWith("pages/api/")) {
86
- // Pages Router: pages/api/queue.ts -> /api/queue
87
- apiPath = apiPath.replace("pages/api/", "/api/");
88
- } else if (apiPath.startsWith("pages/")) {
89
- // Pages Router: pages/queue.ts -> /queue
90
- apiPath = apiPath.replace("pages/", "/");
91
- } else {
92
- // Fallback: assume it's a root-level route
93
- apiPath = "/" + apiPath;
94
- }
95
-
96
- // Remove Next.js route groups (folders wrapped in parentheses)
97
- // Route groups like (dashboard), (auth), (admin) are used for organization
98
- // but don't affect the URL structure
99
- apiPath = apiPath.replace(/\/\([^)]+\)/g, "");
100
-
101
- // Remove file extensions and Next.js-specific suffixes
102
- apiPath = apiPath.replace(
103
- /\/(route|index)\.(ts|js|tsx|jsx)$/,
104
- "",
105
- );
106
- apiPath = apiPath.replace(/\.(ts|js|tsx|jsx)$/, "");
107
-
108
- routes.push(apiPath);
109
- }
110
- }
111
- }
112
- }
113
- }
114
-
115
- return routes;
116
- } catch (error) {
117
- // vercel.json might not exist or be malformed
118
- return [];
119
- }
120
- }
121
-
122
- /**
123
- * Make OPTIONS requests to trigger Next.js lazy loading
124
- */
125
- async function discoverQueueHandlers(options = {}) {
126
- const baseUrl = options.baseUrl || "http://localhost:3000";
127
- const configPath = options.configPath || "./vercel.json";
128
-
129
- let routes = options.routes || [];
130
-
131
- // Auto-discover from vercel.json
132
- if (routes.length === 0) {
133
- const configRoutes = readVercelConfig(configPath);
134
- if (configRoutes.length > 0) {
135
- routes = configRoutes;
136
- console.log(
137
- `[Dev Mode] Found ${configRoutes.length} queue endpoints in vercel.json`,
138
- );
139
- } else {
140
- console.error("[Dev Mode] No queue endpoints found in vercel.json");
141
- process.exit(1);
142
- }
143
- }
144
-
145
- console.log(
146
- "[Dev Mode] Making OPTIONS requests to trigger module loading...",
147
- );
148
-
149
- for (const route of routes) {
150
- try {
151
- // Make an OPTIONS request to trigger module loading without processing
152
- const response = await fetch(`${baseUrl}${route}`, {
153
- method: "OPTIONS",
154
- // Add a header to identify this as a discovery request
155
- headers: { "x-queue-discovery": "true" },
156
- });
157
-
158
- // Any response means the route exists and module was loaded
159
- console.log(`[Dev Mode] ✓ ${route} (${response.status})`);
160
- } catch (error) {
161
- // Ignore errors - route might not exist or server not running
162
- console.log(`[Dev Mode] ✗ ${route} (server may not be running)`);
163
- }
164
- }
165
-
166
- console.log("[Dev Mode] Module loading complete.");
167
- }
168
-
169
- async function main() {
170
- const args = process.argv.slice(2);
171
- const options = {};
172
-
173
- // Parse simple command line arguments
174
- for (let i = 0; i < args.length; i++) {
175
- const arg = args[i];
176
- if (arg === "--help" || arg === "-h") {
177
- showHelp();
178
- return;
179
- } else if (arg === "--port" && args[i + 1]) {
180
- options.baseUrl = `http://localhost:${args[i + 1]}`;
181
- i++;
182
- } else if (arg === "--config" && args[i + 1]) {
183
- options.configPath = args[i + 1];
184
- i++;
185
- }
186
- }
187
-
188
- try {
189
- await discoverQueueHandlers(options);
190
- } catch (error) {
191
- console.error("Failed to discover queue handlers:", error.message);
192
- process.exit(1);
193
- }
194
- }
195
-
196
- main();