nitro-queue 0.0.4 → 0.0.5

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 CHANGED
@@ -1,6 +1,6 @@
1
+ import { stat, readdir, mkdir, writeFile } from 'node:fs/promises';
1
2
  import { fileURLToPath } from 'node:url';
2
3
  import { join, relative, dirname } from 'pathe';
3
- import { stat, readdir, mkdir, writeFile } from 'node:fs/promises';
4
4
 
5
5
  function defineQueue(endpoint) {
6
6
  return {
@@ -63,7 +63,7 @@ function initQueue() {
63
63
  const queues = await scanQueues(queuesDir);
64
64
  await generateQueueTypes(queues, outputPath);
65
65
  }
66
- async function generateVirtualHandlers() {
66
+ async function generateHandlersFile() {
67
67
  const queues = await scanQueues(queuesDir);
68
68
  const imports = [];
69
69
  const handlerEntries = [];
@@ -72,16 +72,21 @@ function initQueue() {
72
72
  imports.push(`import ${varName} from '${queue.filePath}'`);
73
73
  handlerEntries.push(` '${queue.name}': ${varName}`);
74
74
  });
75
- return `${imports.join("\n")}
75
+ const content = `${imports.join("\n")}
76
76
 
77
77
  export const handlers = {
78
78
  ${handlerEntries.join(",\n")}
79
79
  }`;
80
+ const handlersPath2 = join(nitro.options.rootDir, "node_modules/.nitro/server/nitro-queue-handlers.js");
81
+ await mkdir(dirname(handlersPath2), { recursive: true });
82
+ await writeFile(handlersPath2, content);
83
+ return handlersPath2;
80
84
  }
81
85
  nitro.hooks.hook("build:before", generateTypes);
82
- const virtualContent = await generateVirtualHandlers();
83
- nitro.options.virtual["#queue-handlers"] = virtualContent;
84
- nitro.options.plugins.push(fileURLToPath(new URL("./runtime/plugin", import.meta.url)));
86
+ const handlersPath = await generateHandlersFile();
87
+ nitro.options.alias["#nitro-queue-handlers"] = handlersPath;
88
+ const packageRoot = fileURLToPath(new URL("..", import.meta.url));
89
+ nitro.options.plugins.push(join(packageRoot, "src/runtime/plugin.ts"));
85
90
  }
86
91
  };
87
92
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nitro-queue",
3
3
  "type": "module",
4
- "version": "0.0.4",
4
+ "version": "0.0.5",
5
5
  "private": false,
6
6
  "description": "Queue module for Nitro",
7
7
  "license": "MIT",
@@ -18,7 +18,8 @@
18
18
  "nitro-queue": "./dist/cli.mjs"
19
19
  },
20
20
  "files": [
21
- "dist"
21
+ "dist",
22
+ "src/runtime"
22
23
  ],
23
24
  "peerDependencies": {
24
25
  "nitro": "^3.0.1-alpha.2"
@@ -0,0 +1,23 @@
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
+ })
@@ -1,3 +0,0 @@
1
- declare const _default: any;
2
-
3
- export { _default as default };
@@ -1,3 +0,0 @@
1
- declare const _default: any;
2
-
3
- export { _default as default };
@@ -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 };