Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(vite-node-miniflare): replace hattip with h3 for internal middleware #135

Closed
wants to merge 9 commits into from

Conversation

hi-ogawa
Copy link
Owner

@hi-ogawa hi-ogawa commented Dec 15, 2023

It's a little ergonomically nicer to just install h3 (instead of multiple @hattip/.... packages), but it seem proxying request.body is not working with miniflare?

error log
# test POST API on React Router SSR demo
$ pnpm dev-react-router
..
[h3] [unhandled] TypeError: fetch failed
    at fetch (/home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/[email protected]/node_modules/undici/index.js:112:15)
    at async fetch2 (/home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/[email protected]/node_modules/miniflare/dist/src/index.js:4584:20)
    at async Miniflare.dispatchFetch (/home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/[email protected]/node_modules/miniflare/dist/src/index.js:9021:22)
    at async Object.handler (file:///home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:1675:19)
    at async toNodeHandle (file:///home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:1885:7) {
  cause: RequestContentLengthMismatchError: Request body length does not match content-length header
      at AsyncWriter.end (/home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/[email protected]/node_modules/undici/lib/client.js:2246:15)
      at writeIterable (/home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/[email protected]/node_modules/undici/lib/client.js:2129:12)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
    code: 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH'
  },
  statusCode: 500,
  fatal: false,
  unhandled: true,
  statusMessage: undefined,
  data: undefined
}
[vite-node-miniflare] invalidateDepTree: { invalidatedModules: [], invalidatedTree: Set(0) {} }
[mf:inf] POST /hello 200 OK (3ms)
workerd/server/server.c++:2838: error: Uncaught exception: kj/async-io-unix.c++:186: disconnected: remote.worker_do_not_log; Request failed due to internal error
stack: /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@3a68f8f /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@3a69ab1 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@28c3b1b /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@3a464f0 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@1fbb1e7 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@209f370 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@1f3d350 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@1f3e0d5 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@1deebf0 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@1def160 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@1df0040 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@1df0670 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@18a3f70 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@28b295b /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@28b1e50 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@189d530 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@1f3d610 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@1f3da44 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@1df0670 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@18bf78b /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@28c4d8c /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@28c5b93 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@28c6194 /home/hiroshi/code/personal/vite-plugins/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workerd-linux-64/bin/workerd@288b0b0

Also, response headers aren't propagated properly, which breaks data request convention using x-loader-response header?

curl examples
# before
$ curl -X GET -I "http://localhost:5173/loader-data?loader-route-id=%2Floader-data"
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
content-length: 26
content-type: application/json; charset=utf-8
x-loader-response: 1
Date: Fri, 15 Dec 2023 00:57:41 GMT
Connection: keep-alive
Keep-Alive: timeout=5

# after
curl -X GET -I "http://localhost:5173/loader-data?loader-route-id=%2Floader-data"
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
content-type: default
Date: Fri, 15 Dec 2023 00:56:21 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 26

@hi-ogawa hi-ogawa changed the title refactor(vite-node-miniflare): replace h3 with hattip for middleware refactor(vite-node-miniflare): replace hattip with h3 for internal middleware Dec 15, 2023
const app = h3.createApp().use([
h3.eventHandler((event) =>
viteNodeServerRpc.requestHandler({
request: h3.toWebRequest(event),
Copy link
Owner Author

@hi-ogawa hi-ogawa Dec 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be due to calling toWebRequest twice in custom handler?

EDIT: temporary workaround? fa72b7c

@hi-ogawa hi-ogawa mentioned this pull request Dec 15, 2023
19 tasks
Comment on lines 78 to 79
const request = h3.toWebRequest(event) as any as MiniflareRequest;
return miniflare.dispatchFetch(request.url, {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another blocker... Miniflare probably uses its own Request/Response polyfills and it breaks h3's isWebResponse check https://github.com/unjs/h3/blob/53703dc860f1ff6fe7ce71d543deff1cfa810b11/src/utils/response.ts#L209-L211

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by 65258b0

@hi-ogawa hi-ogawa marked this pull request as ready for review December 18, 2023 06:54
@hi-ogawa
Copy link
Owner Author

Closing as it's obsolete

@hi-ogawa hi-ogawa closed this Jun 10, 2024
@hi-ogawa hi-ogawa deleted the refactor-vite-node-middleware-hattip-to-h3 branch June 10, 2024 01:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant