nitro-queue 0.0.4 → 0.0.6
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.
- package/dist/index.mjs +28 -8
- package/package.json +1 -1
- package/dist/runtime/plugin.d.mts +0 -3
- package/dist/runtime/plugin.d.ts +0 -3
- package/dist/runtime/plugin.mjs +0 -21
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { fileURLToPath } from 'node:url';
|
|
2
|
-
import { join, relative, dirname } from 'pathe';
|
|
3
1
|
import { stat, readdir, mkdir, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { join, relative, dirname } from 'pathe';
|
|
4
3
|
|
|
5
4
|
function defineQueue(endpoint) {
|
|
6
5
|
return {
|
|
@@ -63,8 +62,10 @@ function initQueue() {
|
|
|
63
62
|
const queues = await scanQueues(queuesDir);
|
|
64
63
|
await generateQueueTypes(queues, outputPath);
|
|
65
64
|
}
|
|
66
|
-
async function
|
|
65
|
+
async function generateQueueFiles() {
|
|
67
66
|
const queues = await scanQueues(queuesDir);
|
|
67
|
+
const nitroDir = join(nitro.options.rootDir, "node_modules/.nitro/server");
|
|
68
|
+
await mkdir(nitroDir, { recursive: true });
|
|
68
69
|
const imports = [];
|
|
69
70
|
const handlerEntries = [];
|
|
70
71
|
queues.forEach((queue, index) => {
|
|
@@ -72,16 +73,35 @@ function initQueue() {
|
|
|
72
73
|
imports.push(`import ${varName} from '${queue.filePath}'`);
|
|
73
74
|
handlerEntries.push(` '${queue.name}': ${varName}`);
|
|
74
75
|
});
|
|
75
|
-
|
|
76
|
+
await writeFile(join(nitroDir, "queue-handlers.js"), `${imports.join("\n")}
|
|
76
77
|
|
|
77
78
|
export const handlers = {
|
|
78
79
|
${handlerEntries.join(",\n")}
|
|
79
|
-
}
|
|
80
|
+
}`);
|
|
81
|
+
await writeFile(join(nitroDir, "queue-plugin.js"), `import { handlers } from './queue-handlers.js'
|
|
82
|
+
import { definePlugin } from 'nitro'
|
|
83
|
+
|
|
84
|
+
export default definePlugin((nitroApp) => {
|
|
85
|
+
nitroApp.hooks.hook('cloudflare:queue', async (queue) => {
|
|
86
|
+
for (const message of queue.batch.messages) {
|
|
87
|
+
if (typeof message.body !== 'object' || message.body === null)
|
|
88
|
+
continue
|
|
89
|
+
if ('type' in message.body && typeof message.body.type === 'string') {
|
|
90
|
+
const body = message.body
|
|
91
|
+
const handler = handlers[body.type]
|
|
92
|
+
if (!handler) {
|
|
93
|
+
throw new Error(\`Unknown queue type: \${body.type}\`)
|
|
94
|
+
}
|
|
95
|
+
await handler.endpoint.run(body.payload, { queue, message })
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
})`);
|
|
100
|
+
return join(nitroDir, "queue-plugin.js");
|
|
80
101
|
}
|
|
81
102
|
nitro.hooks.hook("build:before", generateTypes);
|
|
82
|
-
const
|
|
83
|
-
nitro.options.
|
|
84
|
-
nitro.options.plugins.push(fileURLToPath(new URL("./runtime/plugin", import.meta.url)));
|
|
103
|
+
const pluginPath = await generateQueueFiles();
|
|
104
|
+
nitro.options.plugins.push(pluginPath);
|
|
85
105
|
}
|
|
86
106
|
};
|
|
87
107
|
}
|
package/package.json
CHANGED
package/dist/runtime/plugin.d.ts
DELETED
package/dist/runtime/plugin.mjs
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { handlers } from '#queue-handlers';
|
|
2
|
-
import { definePlugin } from 'nitro';
|
|
3
|
-
|
|
4
|
-
const plugin = definePlugin((nitroApp) => {
|
|
5
|
-
nitroApp.hooks.hook("cloudflare:queue", async (queue) => {
|
|
6
|
-
for (const message of queue.batch.messages) {
|
|
7
|
-
if (typeof message.body !== "object" || message.body === null)
|
|
8
|
-
continue;
|
|
9
|
-
if ("type" in message.body && typeof message.body.type === "string") {
|
|
10
|
-
const body = message.body;
|
|
11
|
-
const handler = handlers[body.type];
|
|
12
|
-
if (!handler) {
|
|
13
|
-
throw new Error(`Unknown queue type: ${body.type}`);
|
|
14
|
-
}
|
|
15
|
-
await handler.endpoint.run(body.payload, { queue, message });
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
export { plugin as default };
|