Skip to content

Commit 1ca248b

Browse files
committed
feat: use async_hooks for retrieving event context
1 parent ccfde4a commit 1ca248b

File tree

8 files changed

+40
-15
lines changed

8 files changed

+40
-15
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ Checkout [the playground example](/playground).
5959

6060
## H3 Event
6161

62-
The h3 event is available as `this` for functions:
62+
The access the H3 event, you can use the `getEvent` function:
6363

6464
```ts
65+
import { getEvent } from 'nuxt-remote-fn/server'
6566
import { readBody } from 'h3'
66-
import type { H3Event } from 'h3'
6767

68-
export async function getTodos(this: H3Event, otherArg: any) {
69-
const body = await readBody(this)
68+
export async function getTodos() {
69+
const event = getEvent()
70+
const body = await readBody(event)
7071
// ...
7172
}
7273
```
@@ -92,7 +93,6 @@ Wouldn't it be nice if all of that was automatically handled and all you'd need
9293
## Development
9394

9495
- Run `cp playground/.env.example playground/.env`
95-
- Run `pnpm dev:prepare` to generate type stubs.
9696
- Use `pnpm dev` to start [playground](./playground) in development mode.
9797

9898
## Credits

client.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './dist/runtime/client'

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
"main": "./dist/module.cjs",
3030
"types": "./dist/types.d.ts",
3131
"files": [
32-
"dist"
32+
"dist",
33+
"*.d.ts"
3334
],
3435
"scripts": {
3536
"prepack": "nuxt-module-build",
36-
"dev": "nuxi dev playground",
37+
"dev": "pnpm prepack && nuxi dev playground",
3738
"dev:build": "nuxi build playground",
3839
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
3940
"lint": "eslint .",

playground/nuxt.config.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { defineNuxtConfig } from 'nuxt/config'
2-
import MyModule from '..'
32

43
export default defineNuxtConfig({
54
modules: [
6-
MyModule
5+
'nuxt-remote-fn'
76
]
87
})

playground/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"schema": "./prisma/schema.prisma"
1313
},
1414
"dependencies": {
15-
"@prisma/client": "^4.6.1"
15+
"@prisma/client": "^4.6.1",
16+
"nuxt-remote-fn": "workspace:*"
1617
}
1718
}

pnpm-lock.yaml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './dist/runtime/server'

src/runtime/server.ts

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
1-
import type { EventHandler } from 'h3'
1+
import { AsyncLocalStorage } from 'async_hooks'
2+
import type { EventHandler, H3Event } from 'h3'
23
import { eventHandler, isMethod, readBody, createError } from 'h3'
34

4-
export function createRemoteFnHandler<T> (functions: T): EventHandler<T> {
5-
return eventHandler(async (event) => {
5+
const DEFAULT_CONTEXT = {}
6+
7+
const asyncLocalStorage = new AsyncLocalStorage<H3Event>()
8+
9+
export function getEvent (): H3Event {
10+
return asyncLocalStorage.getStore() || DEFAULT_CONTEXT as H3Event
11+
}
12+
13+
function wrapEventHandler (handler: EventHandler): EventHandler {
14+
return eventHandler((event) => {
15+
const context = {
16+
node: event.node,
17+
context: event.context,
18+
path: event.path
19+
}
20+
return asyncLocalStorage.run(context as H3Event, () => handler(event))
21+
})
22+
}
23+
24+
export function createRemoteFnHandler<T> (functions: T): any {
25+
return wrapEventHandler(eventHandler(async (event) => {
626
if (!isMethod(event, 'POST')) {
727
throw createError({
828
statusCode: 405,
@@ -22,7 +42,7 @@ export function createRemoteFnHandler<T> (functions: T): EventHandler<T> {
2242
}
2343

2444
// @ts-ignore
25-
const result = await functions[moduleId][functionName].apply(event, input)
45+
const result = await functions[moduleId][functionName](...input)
2646
return result
27-
})
47+
}))
2848
}

0 commit comments

Comments
 (0)