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

Body mixins throw TypeError: unusable #1208

Closed
shellscape opened this issue Feb 6, 2022 · 4 comments
Closed

Body mixins throw TypeError: unusable #1208

shellscape opened this issue Feb 6, 2022 · 4 comments
Labels
Docs Changes related to the documentation good first issue Good for newcomers

Comments

@shellscape
Copy link
Contributor

Bug Description

Calling await body.text() immediately following await body.json() throws TypeError: unusable.

I'm not entirely sure this is a bug. I tried to navigate the fetch spec on this, but maybe I'm just foggy today and didn't grok.

Reproducible By

When using request and making a successful request with 200 response, I'm performing an await body.json(). If for some reason the resulting object is falsy or doesn't contain properties I expect, I'm attempting to call await body.text() so I can get an unfettered raw body to pump out to logs for later examination.

Expected Behavior

I was expecting the body in plain text.

Logs & Screenshots

Here's the code I've been trying to run, of course missing additional context from the encompassing file (which I cannot share).

      const registryUrl = `https://skimdb.npmjs.com/registry/${encodeURIComponent(change.id)}`;
      const { body, statusCode } = await request(registryUrl, options as any);

      if (statusCode !== 200 && statusCode !== 404) {
        throw new AssertionError({
          message: `Registry Request Error → url: ${registryUrl}, statusCode: ${statusCode}`
        });
      }

      const pkg = (await body.json()) as NpmPackage;

      if (!pkg || !pkg.name) {
        const text = await body.text();
        log.warn('Package or Package Name empty. Skipping. Message body:', text);
        continue;
      }

Environment

MacOS/AWS Linux
Node 14@latest

Additional context

@shellscape shellscape added the bug Something isn't working label Feb 6, 2022
@ronag
Copy link
Member

ronag commented Feb 6, 2022

This is expected behavior. You cannot consume the body twice.

@mcollina mcollina closed this as completed Feb 6, 2022
@shellscape
Copy link
Contributor Author

@mcollina a bit quick on the trigger there. for the uninitiated, I would have enjoyed some more conversation about this before closing.

@ronag what's the preferred method here in order to enable that? is there a reset? should we pipe into another stream? should we not rely on the JSON mixin and only use the text mixin?

there are a lot of folks out there that are not intimately familiar with the spec and how things should work, additionally that's not documented at all in this repository. so that's leaving people in the dark, and some thorough discussion around that would absolutely help the community

@mcollina mcollina reopened this Feb 6, 2022
@mcollina mcollina added Docs Changes related to the documentation good first issue Good for newcomers and removed bug Something isn't working labels Feb 6, 2022
@mcollina
Copy link
Member

mcollina commented Feb 6, 2022

@mcollina a bit quick on the trigger there. for the uninitiated, I would have enjoyed some more conversation about this before closing.

You are right, sorry about this. We should document it better.

@ronag what's the preferred method here in order to enable that? is there a reset? should we pipe into another stream? should we not rely on the JSON mixin and only use the text mixin?

I would call .text() and the call JSON.parse() on it.

@shellscape
Copy link
Contributor Author

@mcollina thanks, much appreciated. I had a few extra minutes this morning after the daycare run and put together a small section in the README and accompanying PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Changes related to the documentation good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants