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

Error: The first argument must be of type string or an instance of Buffer. Received an instance of Uint8Array #35

Closed
elmarw opened this issue Aug 22, 2021 · 13 comments

Comments

@elmarw
Copy link

elmarw commented Aug 22, 2021

Getting the error "The first argument must be of type string or an instance of Buffer. Received an instance of Uint8Array"

worker.js:
addEventListener("fetch", (event) => { event.respondWith(new Response("Hello Miniflare!")); });

running:
npx miniflare worker.js

@mrbbot
Copy link
Contributor

mrbbot commented Aug 22, 2021

Hello! 👋 Could you send the rest of the stack trace? Your code is working for me locally. 😕

@elmarw
Copy link
Author

elmarw commented Aug 22, 2021

Let me know if you need more details. Happy to provide.

[mf:err] GET /: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer. Received an instance of Uint8Array at write_ (_http_outgoing.js:661:11) at ServerResponse.write (_http_outgoing.js:629:15) at Miniflare._Miniflare_httpRequestListener (/Users/USERNAME/.npm/_npx/1485/lib/node_modules/miniflare/src/index.ts:418:18) at processTicksAndRejections (internal/process/task_queues.js:97:5)

@mrbbot
Copy link
Contributor

mrbbot commented Aug 22, 2021

Hmmm, interesting. I can see the problem there, but I'm confused why I'm not seeing this. Which version of Node are you using?

@mrbbot
Copy link
Contributor

mrbbot commented Aug 22, 2021

Looks like support for writing Uint8Arrays to writable streams was added in Node 8: https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback. I should definitely document this somewhere other than the package.json, but Miniflare requires at least Node 10.12.0. I'd suggest using a Node version manager like https://volta.sh/.

@elmarw
Copy link
Author

elmarw commented Aug 22, 2021 via email

@mrbbot
Copy link
Contributor

mrbbot commented Aug 22, 2021

😆 Ok, looks like this was actually fixed in Node 12.19.0: https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V12.md#2020-10-06-version-12190-erbium-lts-codebytere

http: don't throw on Uint8Arrays for http.ServerResponse#write

@elmarw
Copy link
Author

elmarw commented Aug 22, 2021

That was it! Thanks so much, for your help and the great tool.

@mrbbot
Copy link
Contributor

mrbbot commented Aug 22, 2021

No worries! Thank you! 😄

@chilts
Copy link

chilts commented Oct 18, 2021

I'm on node v14 and I also get this.

$ node --version
v14.4.0

$ npm --version
7.6.3

I have followed the instructions on https://miniflare.dev/cli.html, so here's the summary:

Installed with:

$ npm install -g miniflare

changed 122 packages, and audited 123 packages in 13s

10 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Then I added a worker.js exactly the same as https://miniflare.dev/cli.html#usage

$ cat worker.js 
addEventListener("fetch", (event) => {
  event.respondWith(new Response("Hello Miniflare!"));
});

Then run it with:

$ miniflare worker.js 
[mf:inf] Worker reloaded! (98B)
[mf:inf] Listening on :8787
[mf:inf] - http://127.0.0.1:8787
[mf:inf] - http://192.168.0.163:8787

All good so far.

When I hit http://127.0.0.1:8787/ I get this in the log, and the following image in the browser:

[mf:err] GET /: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer. Received an instance of Uint8Array
    at write_ (_http_outgoing.js:671:11)
    at ServerResponse.write (_http_outgoing.js:636:15)
    at Miniflare._Miniflare_httpRequestListener (/home/chilts/.nvm/versions/node/v14.4.0/lib/node_modules/miniflare/src/index.ts:426:18)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
GET / 200 OK (149.40ms)

Screenshot 2021-10-18 at 17-58-56 Screenshot

I'm fine playing with things or looking into it further for you but just thought I'd mention it here in case anyone else was having the same problem.

Btw, it's all exactly the same on v16. I'm using nvm to switch versions (not volta as recommended above) and whilst it should work exactly the same, I've seen things be a little different under different node version managers, though at the same time, that's not a smoking gun either. :)

@chilts
Copy link

chilts commented Oct 18, 2021

I've deleted this comment since it was incorrect. Summary: I said both v14 and v16 were still not working, but actually v16 is fine and just v14 is not working.

@chilts
Copy link

chilts commented Oct 18, 2021

I'll just edit my last comment. It seems v16 is fine, but v14 is still problematic. So, in conclusion, v16 is okay and I suspect that should be the version that goes into the docs, rather than v14 as previously suggested further up.

@mrbbot
Copy link
Contributor

mrbbot commented Oct 18, 2021

Hey! 👋 Thanks for looking into this. Miniflare 2 is going to have a hard requirement on Node 16 anyways for its built-in web standards (e.g. streams, crypto, ...) so this shouldn't be a problem once that's published.

@chilts
Copy link

chilts commented Oct 18, 2021

Ok, thanks. :)

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

3 participants