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

Clean up addons injection script and fix bugs in library #227

Merged
merged 12 commits into from
Oct 29, 2024

Conversation

agjohnson
Copy link
Contributor

@agjohnson agjohnson commented Oct 23, 2024

There is a lot here to modernize this library and fix a number of bugs:

  • Sets this up as a package so we can use normal package management to define dependencies.
  • Adds a test framework to help catch bugs. I also found this necessary just to understand what the worker was doing and verify changes.
  • Reworks the docker container and image so that wrangler can gracefully catch file changes -- it's not necessary to constantly restart wrangler now.
  • Switch worker from event based to ESM layout -- don't use fetch event and instead use fetch() exported function
  • Alter how we are returning Response instances, and when this happens.

We should still figure out moving these files in

- #226
Copy link
Contributor Author

@agjohnson agjohnson Oct 23, 2024

Choose a reason for hiding this comment

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

@agjohnson
Copy link
Contributor Author

I've tested this against projects with large response pages and the exception is actually caught now, and the response falls back to the origin response. The next change will be trying to catch this through streaming and avoiding buffering the entire request.

If `Response.clone()` is used without `await` the resulting instance has
missing attributes, like `url = null`. The docs do not indicate that
`clone()` is async however, so it's not clear why this is required.

With this, and more careful response instance usage, most of the cases
of dangling, unused, cloned responses have been removed. Now, any
response that is used is cloned right as we use it and evaluate the
body. This is important, as `response.body` is ultimately what causes
the request to buffer.
packages/addons-inject/Dockerfile Show resolved Hide resolved
packages/addons-inject/index.js Show resolved Hide resolved
packages/addons-inject/index.js Outdated Show resolved Hide resolved
packages/addons-inject/index.js Outdated Show resolved Hide resolved
packages/addons-inject/index.js Show resolved Hide resolved
packages/addons-inject/index.js Outdated Show resolved Hide resolved
packages/addons-inject/package.json Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

tests 😍

Copy link
Member

@ericholscher ericholscher left a comment

Choose a reason for hiding this comment

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

I don't love all the explicit test logic in the actual code, but seems it's necessary from the comments around it. Overall this looks similar to what we had, and the logic seems the same. I'm not really knowledgable enough about Workers or JS patterns to review that stuff, but the flow seems reasonable, with a lot more async 😨

packages/addons-inject/index.js Outdated Show resolved Hide resolved
Copy link
Member

@humitos humitos left a comment

Choose a reason for hiding this comment

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

This looks really good to me. I'm happy we have tests 🧪 now and a pattern to keep adding more 💯

It seems the main change here is the usage of the lower-level API fetch and .passThroughOnException() that allow us to catch errors on a try/catch when transforming the clone()d response. I don't fully understand the details behind this, but I think I get the big picture here.

I left some suggestions and questions that will allow me to understand a little more some missing pieces.

I'm trying to test this locally, but it seems the paths for some of the Docker files are wrong. I'm getting this error when running inv docker.build:

=> ERROR [wrangler 2/6] COPY package.json /usr/src/app/packages/addons-inject/                                                                                                        0.0s
 => ERROR [wrangler 3/6] COPY index.js /usr/src/app/packages/addons-inject/                                                                                                            0.0s
 => ERROR [wrangler 4/6] COPY wrangler.toml /usr/src/app/packages/addons-inject/                                                                                                       0.0s
------                                                                                                                                                                                      
 > [wrangler 2/6] COPY package.json /usr/src/app/packages/addons-inject/:                                                                                                                   
------                                                                                                                                                                                      
------                                                                                                                                                                                      
 > [wrangler 3/6] COPY index.js /usr/src/app/packages/addons-inject/:                                                                                                                       
------                                                                                                                                                                                      
------
 > [wrangler 4/6] COPY wrangler.toml /usr/src/app/packages/addons-inject/:
------
failed to solve: failed to compute cache key: failed to calculate checksum of ref UPQU:UMYX:4FAE:CRS7:GXRC:2DRM:5B5J:CRJO:MLEW:4WNJ:5M75:ZC7N::p64btx7uytedu4ry4kp8bwbzq: "/wrangler.toml": not found

I think we need to use relative paths using readthedocs.org as working path.

dockerfiles/docker-compose.yml Show resolved Hide resolved
packages/addons-inject/package.json Show resolved Hide resolved
packages/addons-inject/Dockerfile Show resolved Hide resolved
packages/addons-inject/index.js Show resolved Hide resolved
packages/addons-inject/index.js Show resolved Hide resolved
packages/addons-inject/tests/wrangler.test.js Show resolved Hide resolved
packages/addons-inject/tests/wrangler.test.js Show resolved Hide resolved
packages/addons-inject/tests/wrangler.test.js Show resolved Hide resolved
packages/addons-inject/tests/wrangler.test.js Show resolved Hide resolved
packages/addons-inject/tests/wrangler.test.js Show resolved Hide resolved
@humitos
Copy link
Member

humitos commented Oct 28, 2024

After building the images properly I'm getting this error:

wrangler_1     | node:internal/modules/cjs/loader:1078
wrangler_1     |   throw err;
wrangler_1     |   ^
wrangler_1     | 
wrangler_1     | Error: Cannot find module '/usr/src/app/packages/addons-inject/wrangler'
wrangler_1     |     at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
wrangler_1     |     at Module._load (node:internal/modules/cjs/loader:920:27)
wrangler_1     |     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
wrangler_1     |     at node:internal/main/run_main_module:23:47 {
wrangler_1     |   code: 'MODULE_NOT_FOUND',
wrangler_1     |   requireStack: []
wrangler_1     | }
wrangler_1     | 
wrangler_1     | Node.js v18.15.0
wrangler_1 exited with code 1

@humitos
Copy link
Member

humitos commented Oct 28, 2024

Just a note that before merging this PR we should fix the wrangler Docker container so it works with the pattern we already have and builds correctly.

networks:
readthedocs:
working_dir: /usr/src/app/packages/addons-inject/
command: [
"wrangler",
Copy link
Member

Choose a reason for hiding this comment

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

We need this to be as follows to work properly when executing inv docker.up

Suggested change
"wrangler",
"node_modules/.bin/wrangler",

@humitos
Copy link
Member

humitos commented Oct 28, 2024

Just a note that before merging this PR we should fix the wrangler Docker container so it works with the pattern we already have and builds correctly.

I fixed this and left the solution as suggestions. I was able to build the Docker images and run it as usual with those changes.

@agjohnson
Copy link
Contributor Author

Review seems settled and the production test is looking much better:

image

I'm going to merge this and we can figure out the deploy next.

@agjohnson agjohnson merged commit 8291bac into main Oct 29, 2024
@agjohnson agjohnson deleted the agj/addons-package branch October 29, 2024 03:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants