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

[Bug] TypeError: Illegal invocation #9

Closed
togmund opened this issue Mar 22, 2021 · 4 comments
Closed

[Bug] TypeError: Illegal invocation #9

togmund opened this issue Mar 22, 2021 · 4 comments

Comments

@togmund
Copy link

togmund commented Mar 22, 2021

Hey!

I'm running into:

Uncaught (in promise)
TypeError: Illegal invocation
    at worker.js:1:5254
    at f (worker.js:1:3637)
    at Object.run (worker.js:1:4646)
    at worker.js:1:4769
Uncaught (in response)
TypeError: Illegal invocation

When trying to use req.body.text() and req.body.json() in a cloudflare worker.
I've wrapped the invocation in a try/catch and the error is empty.

With Worktop

import { Router } from 'worktop'
const API = new Router()

API.add('POST', '/webhook', async function (req, res) {
    const body = await req.body.text()
    console.log(body)
})

addEventListener('fetch', API.listen)
// TypeError: Illegal invocation

Without Worktop

async function handleRequest (request) {
  const body = await request.text()
  console.log(body)
}

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})
// ok!
@lukeed
Copy link
Owner

lukeed commented Mar 22, 2021

Hey, thanks! Fixing now – sorry about that :)

@lukeed lukeed closed this as completed in 693c15a Mar 22, 2021
@lukeed
Copy link
Owner

lukeed commented Mar 22, 2021

Your request should have the appropriate Content-Type header anyway, so until the next release goes out (in the next day or two!), you should be fine just using the "smart" req.body() helper instead:

API.add('POST', '/webhook', async function (req, res) {
    const body = await req.body()
    console.log(body) //=> will be text string
})

@togmund
Copy link
Author

togmund commented Mar 22, 2021

Appreciate it!

This is specifically for a Stripe Webhook, which signs the content as json, but will reject requests if you pull n a req.body() and pipe it through as JSON.stringify() (but will accept the raw request.text())

{
  cType: 'application/json; charset=utf-8',
  body: //{ big json object }
}

@lukeed
Copy link
Owner

lukeed commented Mar 23, 2021

Just in case you missed it, 0.2.0 went out yesterday w/ this fix :)

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

No branches or pull requests

2 participants