nitro-queue 0.0.3 → 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/README.ko.md CHANGED
@@ -29,30 +29,29 @@ nitro-queue는 wrangler로 생성한 type을 프로젝트 queue 타입에 맞게
29
29
  "scripts": {
30
30
  "prepare": "pnpm types:cloudflare && pnpm types:cloudflare",
31
31
  "types:cloudflare": "wrangler types cloudflare.d.ts --config ./.output/server/wrangler.json",
32
- "types:queue": "npx nitro-queue types cloudflare.d.ts",
33
- },
32
+ "types:queue": "npx nitro-queue types cloudflare.d.ts"
33
+ }
34
34
  }
35
-
36
35
  ```
37
36
  # How to use
38
37
  ```ts
39
38
  // queues/user/remove.ts
40
39
  export default defineQueue<{
41
- userId: string,
40
+ userId: string
42
41
  }>({
43
- run(payload) {
44
- console.log(payload.userId)
45
- }
42
+ run(payload) {
43
+ console.log(payload.userId)
44
+ }
46
45
  })
47
46
 
48
47
  // routes/publish.ts
49
48
  export default defineHandler(async (e) => {
50
- e.runtime.cloudflare.env.MY_QUEUE.send({
51
- type: 'user:remove',
52
- payload: { userId }
53
- })
49
+ e.runtime.cloudflare.env.MY_QUEUE.send({
50
+ type: 'user:remove',
51
+ payload: { userId }
52
+ })
54
53
  })
55
54
  ```
56
55
 
57
56
  # 주의점
58
- - 테스트를 면밀히 거치지 않았습니다. 버그가 발생한다면 이슈를 통해 알려주시면 감사드리겠습니다.
57
+ - 테스트를 면밀히 거치지 않았습니다. 버그가 발생한다면 이슈를 통해 알려주시면 감사드리겠습니다.
package/README.md CHANGED
@@ -46,22 +46,22 @@ nitro-queue then modifies the types generated by wrangler to match your project'
46
46
  ```ts
47
47
  // queues/user/remove.ts
48
48
  export default defineQueue<{
49
- userId: string,
49
+ userId: string
50
50
  }>({
51
- run(payload) {
52
- console.log(payload.userId)
53
- }
51
+ run(payload) {
52
+ console.log(payload.userId)
53
+ }
54
54
  })
55
55
 
56
56
  // routes/publish.ts
57
57
  export default defineHandler(async (e) => {
58
- e.runtime.cloudflare.env.MY_QUEUE.send({
59
- type: 'user:remove',
60
- payload: { userId }
61
- })
58
+ e.runtime.cloudflare.env.MY_QUEUE.send({
59
+ type: 'user:remove',
60
+ payload: { userId }
61
+ })
62
62
  })
63
63
  ```
64
64
 
65
65
  # Note
66
66
 
67
- This module has not been thoroughly tested. If you encounter any bugs, please feel free to open an issue. Your feedback is greatly appreciated.
67
+ This module has not been thoroughly tested. If you encounter any bugs, please feel free to open an issue. Your feedback is greatly appreciated.
package/dist/cli.mjs CHANGED
@@ -19,7 +19,7 @@ if (command === "types") {
19
19
  replaceInFileSync({
20
20
  files: targetFile,
21
21
  from: /^/,
22
- to: `import type { QueueMessageBody } from './node_modules/.nitro/server/nitro-queue'
22
+ to: `import type { QueueMessageBody } from './node_modules/.nitro/types/nitro-queue'
23
23
  `
24
24
  });
25
25
  replaceInFileSync({
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 {
@@ -58,12 +58,12 @@ function initQueue() {
58
58
  async setup(nitro) {
59
59
  const serverDir = nitro.options.serverDir || nitro.options.rootDir;
60
60
  const queuesDir = join(serverDir, "queues");
61
- const outputPath = join(nitro.options.rootDir, "node_modules/.nitro/server/nitro-queue.d.ts");
61
+ const outputPath = join(nitro.options.rootDir, "node_modules/.nitro/types/nitro-queue.d.ts");
62
62
  async function generateTypes() {
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,13 +1,10 @@
1
1
  {
2
2
  "name": "nitro-queue",
3
3
  "type": "module",
4
+ "version": "0.0.5",
4
5
  "private": false,
5
- "version": "0.0.3",
6
6
  "description": "Queue module for Nitro",
7
7
  "license": "MIT",
8
- "bin": {
9
- "nitro-queue": "./dist/cli.mjs"
10
- },
11
8
  "exports": {
12
9
  ".": {
13
10
  "types": "./dist/index.d.ts",
@@ -17,8 +14,12 @@
17
14
  "main": "./dist/index.mjs",
18
15
  "module": "./dist/index.mjs",
19
16
  "types": "./dist/index.d.ts",
17
+ "bin": {
18
+ "nitro-queue": "./dist/cli.mjs"
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 };