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: Received a malformed response from the API #1222

Closed
arggh opened this issue Jun 10, 2022 · 13 comments · Fixed by #1248 or #1249
Closed

🐛 BUG: Received a malformed response from the API #1222

arggh opened this issue Jun 10, 2022 · 13 comments · Fixed by #1248 or #1249
Assignees
Labels
blocked Blocked on other work bug Something that isn't working internal Requires support from the Cloudflare Platform

Comments

@arggh
Copy link

arggh commented Jun 10, 2022

What version of Wrangler are you using?

2.0.8

What operating system are you using?

Mac

Describe the Bug

I can't publish my project anymore (which I could publish yesterday just fine).

❯ wrangler publish --env staging
 ⛅️ wrangler 2.0.8
-------------------
Your worker has access to the following bindings:
- Vars:
  - ENVIRONMENT: "staging"

✘ [ERROR] Received a malformed response from the API

  {
    "result": {
      "id": "front-staging",
      "etag": "c2bc4c316edc5f156011be2cafb1a0ba8e2e6584c277... (length = 2809573)

  PUT /accounts/d7c7e62dcb0e66.......7b90d86b10/workers/scripts/front-staging -> 200 OK

  If you think this is a bug, please open an issue at:
  https://github.com/cloudflare/wrangler2/issues/new/choose

EDIT: Correction, it seems the project was published, even though Wrangler gives me an error.

@arggh arggh added the bug Something that isn't working label Jun 10, 2022
@arggh arggh changed the title 🐛 BUG: 🐛 BUG: Received a malformed response from the API Jun 10, 2022
@petebacondarwin petebacondarwin moved this to Untriaged in workers-sdk Jun 10, 2022
@0x15f
Copy link

0x15f commented Jun 10, 2022

I am encountering this issue as well. All was fine yesterday, attempting to republish (the same code that worked yesterday) yields a malformed response error. It does appear the publication is successful even with the error.

@petebacondarwin
Copy link
Contributor

Is the size of the Worker script particularly large?
It looks like the response is very large (~ 2809573) but it looks like it is valid JSON.
Perhaps we are failing to parse it because it is too large.
I wonder if the backend has changed to return the script contents in the response object?

Can you try running Wrangler in a debugger, pausing at the point this error is thrown and checking what the err value is?

The relevant code is:

https://github.com/cloudflare/wrangler2/blob/6b44822b1742e2b84e6b38c383e78043db922b67/packages/wrangler/src/cfetch/internal.ts#L46-L61

@0x15f
Copy link

0x15f commented Jun 12, 2022

Is the size of the Worker script particularly large? It looks like the response is very large (~ 2809573) but it looks like it is valid JSON. Perhaps we are failing to parse it because it is too large. I wonder if the backend has changed to return the script contents in the response object?

Can you try running Wrangler in a debugger, pausing at the point this error is thrown and checking what the err value is?

The relevant code is:

https://github.com/cloudflare/wrangler2/blob/6b44822b1742e2b84e6b38c383e78043db922b67/packages/wrangler/src/cfetch/internal.ts#L46-L61

The API is returning the entire script content, it is a parse error within the script property on the response payload.

Error:

ParseError: Unexpected token <

When saving the output to a JSON file, the script contents arent properly escaped. The rest of the payload is fine.

Below is some questionable regex I wrote for removing the script before parsing. It is working for me locally, however, it is not a solution to this issue.

let jsonText = await response.text();
jsonText = jsonText.replace(/"script":[\s\S]*",\n/igm, '');

@petebacondarwin
Copy link
Contributor

I wonder if this is related to #1219?
Can you say whether you have "orange clouded" the domain you are publishing to?

@petebacondarwin petebacondarwin moved this from Untriaged to Backlog in workers-sdk Jun 13, 2022
@0x15f
Copy link

0x15f commented Jun 13, 2022

I do have an orange cloud, this is a worker-only domain, not a worker proxy.

@petebacondarwin
Copy link
Contributor

@0x15f - when you say worker-only domains are you talking abut the new custom domain setup? I.e.

routes = [ { pattern: "abc.com", custom_domain: true } ]

@0x15f
Copy link

0x15f commented Jun 13, 2022

Yes, custom domains, my apologies for the miswording.

@petebacondarwin
Copy link
Contributor

When you say:

When saving the output to a JSON file, the script contents arent properly escaped.

can you describe what exactly is not being escaped correctly? For example is it the quotation marks or something?

@0x15f
Copy link

0x15f commented Jun 13, 2022

When you say:

When saving the output to a JSON file, the script contents arent properly escaped.

can you describe what exactly is not being escaped correctly? For example is it the quotation marks or something?

I cannot precisely tell what is not being escaped, the payload is too large for VSCode to syntax highlight it all. Below is the link to a gist with the response. When copying it into jsonformatter.org it yields

Parse error on line 15:
...   ],    "script": "(() => {\n  // dist
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

I think it may be in code comments causing the JSON to break. I am going to do more testing with our build to figure out exactly what.

Invalid escape character in string.json(261)

https://gist.github.com/0x15f/48014b9c09416ca2940d8275bfeb39ee

@petebacondarwin
Copy link
Contributor

petebacondarwin commented Jun 13, 2022

Thanks for providing that. It turns out that the problem is that there are three instances of \< in the JSON, which apparently is not a valid escape sequence. (I would have assumed that it would have just been mapped to <).

threepointone added a commit that referenced this issue Jun 13, 2022
When we upload a script bundle, we get the actual content of the script back in the response. Sometimes that script can be large (depending on whether the upload was large), and currently it may even be a badly escaped string. We can pass a queryparam `excludeScript` that, as it implies, exclude the script content in the response. This fix does that.

Fixes #1222
threepointone added a commit that referenced this issue Jun 13, 2022
When we upload a script bundle, we get the actual content of the script back in the response. Sometimes that script can be large (depending on whether the upload was large), and currently it may even be a badly escaped string. We can pass a queryparam `excludeScript` that, as it implies, exclude the script content in the response. This fix does that.

Fixes #1222
@threepointone
Copy link
Contributor

We have a fix rolling out on the api side (hopefully this week?), and a wrangler pr that leverages that fix (#1248) which we'll land soon after

threepointone added a commit that referenced this issue Jun 13, 2022
When we upload a script bundle, we get the actual content of the script back in the response. Sometimes that script can be large (depending on whether the upload was large), and currently it may even be a badly escaped string. We can pass a queryparam `excludeScript` that, as it implies, exclude the script content in the response. This fix does that.

Fixes #1222
@petebacondarwin petebacondarwin moved this from Backlog to In Progress in workers-sdk Jun 14, 2022
@petebacondarwin petebacondarwin added blocked Blocked on other work internal Requires support from the Cloudflare Platform labels Jun 14, 2022
threepointone added a commit that referenced this issue Jun 14, 2022
When we upload a script bundle, we get the actual content of the script back in the response. Sometimes that script can be large (depending on whether the upload was large), and currently it may even be a badly escaped string. We can pass a queryparam `excludeScript` that, as it implies, exclude the script content in the response. This fix does that.

Fixes #1222
Repository owner moved this from In Progress to Done in workers-sdk Jun 14, 2022
@threepointone threepointone reopened this Jun 14, 2022
Repository owner moved this from Done to In Progress in workers-sdk Jun 14, 2022
@threepointone
Copy link
Contributor

I'll keep this issue open until the api fix rolls out

Repository owner moved this from In Progress to Done in workers-sdk Jun 14, 2022
@threepointone threepointone reopened this Jun 14, 2022
Repository owner moved this from Done to In Progress in workers-sdk Jun 14, 2022
@threepointone
Copy link
Contributor

I believe the escaping bug has been fixed on the api end, and the additional fix to not include it given the excludeScript query params will be rolled out soon. So I'm going to close this. Thanks everyone for reporting!

Repository owner moved this from In Progress to Done in workers-sdk Jun 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Blocked on other work bug Something that isn't working internal Requires support from the Cloudflare Platform
Projects
None yet
4 participants