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

Use JSDOM alternative for deploying to Vercel #95

Draft
wants to merge 62 commits into
base: main
Choose a base branch
from

Conversation

Josehower
Copy link
Contributor

@Josehower Josehower commented Jun 27, 2023

Handover TODO's

  • Solve Conflicts and update the branch
  • Research if the problem with js-dom has been solved or new workarounds have been found for the community
  • Verify the solution is still valid
  • Update PR based on research
  • Define if the PR is meant to be merged or maybe requires a different repo or just an independent branch than the main
  • Polish and send to Peer review

This branch has been deployed here:

https://vuln-examples-next-postgres-jose-7nzgag60s-josehower.vercel.app/


When trying to deploy to Vercel multiple issues appear when using JSDOM.

1. JSDOM makes the Serverless function exceed the maximum size

Failed to process build result for "example-6-cross-site-scripting/solution-2.rsc". Data: {"type":"Lambda"}.
Error: The Serverless Function "example-6-cross-site-scripting/solution-2" is 54.64mb which exceeds the maximum size limit of 50mb. Learn More: https://vercel.link/serverless-function-size
NOW_SANDBOX_WORKER_MAX_LAMBDA_SIZE: The Serverless Function "example-6-cross-site-scripting/solution-2" is 54.64mb which exceeds the maximum size limit of 50mb.

This problem was solved by using a lightweight version of JSDOM like https://www.npmjs.com/package/jsdom-light

2. JSDOM using canvas missing dependencies in serverless environments

Error: /lib64/libz.so.1: version `ZLIB_1.2.9' not found (required by /vercel/path0/node_modules/.pnpm/[email protected]/node_modules/canvas/build/Release/libpng16.so.16)
Error: /lib64/libc.so.6: version `GLIBC_2.28' not found (required by /vercel/path0/node_modules/canvas/build/Release/libglib-2.0.so.0)

The way we solve it this was by using a JSDOM solution that was implementing a canvas version that was compatible with serverless environments:

https://www.npmjs.com/package/jsdom-napi-rs-canvas (published from Miggogee/jsdom)

Related Links:

@Josehower Josehower changed the title Add setup to deploy to Vercel Use JSDOM alternative for deploying to Vercel Jun 27, 2023
ley.config.js Outdated Show resolved Hide resolved
database/connect.ts Outdated Show resolved Hide resolved
@@ -15,6 +14,7 @@ declare module globalThis {
function connectOneTimeToDatabase() {
if (!globalThis.postgresSqlClient) {
globalThis.postgresSqlClient = postgres({
ssl: !!process.env.POSTGRES_URL,
Copy link
Member

Choose a reason for hiding this comment

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

can we use Boolean() here for expressiveness?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

types/index.d.ts Outdated
Copy link
Member

Choose a reason for hiding this comment

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

  • I guess the contents of types/base.d.ts could be merged into this file?
  • the filename can be jsdom-napi-rs-canvas.d.ts
  • the file can be in the root of the project - until we have a more types

but not a big deal to make this super polished yet, we're not sure if we are staying with Vercel for this project

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@socket-security
Copy link

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Packages Version New capabilities Transitives Size Publisher
server-only 0.0.1 None +0 611 B sebmarkbage
jsdom-napi-rs-canvas 19.0.0 eval, network, filesystem, shell +18 6.06 MB miggog

🚮 Removed packages: @types/[email protected], [email protected], [email protected]

Copy link
Contributor Author

@Josehower Josehower left a comment

Choose a reason for hiding this comment

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

suggestions applied

@karlhorky karlhorky marked this pull request as ready for review June 30, 2023 13:30
@karlhorky karlhorky closed this Jun 30, 2023
@karlhorky karlhorky reopened this Jun 30, 2023
@karlhorky
Copy link
Member

karlhorky commented Aug 23, 2023

Edit: The link below has problems, I changed it to https://vuln-examples-next-postgres-jose.vercel.app/

Seems like the Railway link ( https://vuln-examples-next-postgres-jose-production.up.railway.app ) that was in the About section doesn't currently work, so I used the Vercel link above instead ( https://vuln-examples-next-postgres-jose-7nzgag60s-josehower.vercel.app/ )

The deployed version on Fly.io ( https://security-vulnerability-examples-next-js-postgres.fly.dev/ ) has been crashing up until this point, but this may improve in the future

@karlhorky
Copy link
Member

The deployed version on Fly.io ( security-vulnerability-examples-next-js-postgres.fly.dev ) has been crashing up until this point, but this may improve in the future

The version deployed on Fly.io is not crashing currently

https://security-vulnerability-examples-next-js-postgres.fly.dev/

@karlhorky
Copy link
Member

@Josehower the JSDOM + DOMPurify example doesn't seem to work on Vercel currently - so maybe the approach in this PR doesn't work?

https://vuln-examples-next-postgres-jose-7nzgag60s-josehower.vercel.app/example-6-cross-site-scripting/solution-2

Screenshot 2023-10-29 at 18 52 31

@karlhorky
Copy link
Member

Oh, it seems there is another deployment at https://vuln-examples-next-postgres-jose.vercel.app/

The Example 6, Solution 2 page here works fine:

https://vuln-examples-next-postgres-jose.vercel.app/example-6-cross-site-scripting/solution-2

Screenshot 2023-10-30 at 09 33 11

@Josehower
Copy link
Contributor Author

Josehower commented Nov 8, 2023

Handover Comment for the PR:

What work has been done

  • Update the content of the example to be deployed in Vercel
  • Use a configuration that replaces js-dom for a version that supports serverless environments

What work is still open

  • Handover TODO's section

who to take over

This can be handover by either @ProchaLu or @Eprince-hub

Where to start

A good place to start would be to get familiar with the changes in the PR and understand what is trying to be achieved. Get a conversation with @karlhorky to define if a PR to main is the best place to contain this deploy alternative or maybe it just needs another repo or just a branch.

Other details that you know that are not captured already in the issue / PR

Not really sure if this is a PR that wants to get merged to the main or have it as a backup in case fly.io doesn't work

This PR is an alternative to deploy the example on serverless environments. This was created since the version on fly.io was not working due to memory issues with Next.js

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

Successfully merging this pull request may close these issues.

2 participants