nitro-queue 0.0.5 → 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 +27 -12
- package/package.json +2 -3
- package/src/runtime/plugin.ts +0 -23
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { stat, readdir, mkdir, writeFile } from 'node:fs/promises';
|
|
2
|
-
import { fileURLToPath } from 'node:url';
|
|
3
2
|
import { join, relative, dirname } from 'pathe';
|
|
4
3
|
|
|
5
4
|
function defineQueue(endpoint) {
|
|
@@ -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,21 +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
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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");
|
|
84
101
|
}
|
|
85
102
|
nitro.hooks.hook("build:before", generateTypes);
|
|
86
|
-
const
|
|
87
|
-
nitro.options.
|
|
88
|
-
const packageRoot = fileURLToPath(new URL("..", import.meta.url));
|
|
89
|
-
nitro.options.plugins.push(join(packageRoot, "src/runtime/plugin.ts"));
|
|
103
|
+
const pluginPath = await generateQueueFiles();
|
|
104
|
+
nitro.options.plugins.push(pluginPath);
|
|
90
105
|
}
|
|
91
106
|
};
|
|
92
107
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nitro-queue",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.6",
|
|
5
5
|
"private": false,
|
|
6
6
|
"description": "Queue module for Nitro",
|
|
7
7
|
"license": "MIT",
|
|
@@ -18,8 +18,7 @@
|
|
|
18
18
|
"nitro-queue": "./dist/cli.mjs"
|
|
19
19
|
},
|
|
20
20
|
"files": [
|
|
21
|
-
"dist"
|
|
22
|
-
"src/runtime"
|
|
21
|
+
"dist"
|
|
23
22
|
],
|
|
24
23
|
"peerDependencies": {
|
|
25
24
|
"nitro": "^3.0.1-alpha.2"
|
package/src/runtime/plugin.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { Message } from '@cloudflare/workers-types'
|
|
2
|
-
import { handlers } from '#nitro-queue-handlers'
|
|
3
|
-
|
|
4
|
-
import { definePlugin } from 'nitro'
|
|
5
|
-
|
|
6
|
-
export default definePlugin((nitroApp) => {
|
|
7
|
-
nitroApp.hooks.hook('cloudflare:queue', async (queue) => {
|
|
8
|
-
for (const message of queue.batch.messages as unknown as Message[]) {
|
|
9
|
-
if (typeof message.body !== 'object' || message.body === null)
|
|
10
|
-
continue
|
|
11
|
-
if ('type' in message.body && typeof message.body.type === 'string') {
|
|
12
|
-
const body = message.body as { type: string, payload: unknown }
|
|
13
|
-
const handler = handlers[body.type as keyof typeof handlers]
|
|
14
|
-
|
|
15
|
-
if (!handler) {
|
|
16
|
-
throw new Error(`Unknown queue type: ${body.type}`)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
await handler.endpoint.run(body.payload as Message<typeof handler.$payload>, { queue, message })
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
})
|
|
23
|
-
})
|