Skip to content

fix: vendor deno dependencies #2302

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

Merged
merged 14 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ lib
demos
packages/runtime/src/templates/edge
packages/runtime/src/templates/edge-shared
packages/runtime/src/templates/vendor
packages/runtime/src/templates/vendor.ts
packages/runtime/lib
packages/runtime/dist-types
jestSetup.js
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/cypress-canary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ jobs:
with:
node-version: '16'

- uses: denoland/setup-deno@v1
with:
deno-version: v1.x

- run: npm install

- name: Cypress run
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/cypress-demo-nx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ jobs:
with:
node-version: '16'

- uses: denoland/setup-deno@v1
with:
deno-version: v1.x

- run: npm install

- name: Cypress run
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/cypress-demo-static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ jobs:
with:
node-version: '16'

- uses: denoland/setup-deno@v1
with:
deno-version: v1.x

- run: npm install

- name: Cypress run
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/cypress-demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ jobs:
with:
node-version: '16'

- uses: denoland/setup-deno@v1
with:
deno-version: v1.x

- run: npm install

- name: Cypress run
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/cypress-middleware.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ jobs:
with:
node-version: '16'

- uses: denoland/setup-deno@v1
with:
deno-version: v1.x

- run: npm install

- name: Cypress run
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/e2e-appdir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ jobs:
test-files: ${{ steps['set-test-files'].outputs['test-files'] }}
steps:
- uses: actions/checkout@v3
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- run: npm install
- id: set-test-files
name: Get test files
Expand All @@ -42,6 +45,9 @@ jobs:
with:
node-version: '16'
cache: 'npm'
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- run: npm install
- name: Install Netlify CLI
run: npm install -g netlify-cli
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/e2e-next.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ jobs:
with:
node-version: '16'
cache: 'npm'
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- run: npm install
- name: Install Netlify CLI
run: npm install -g netlify-cli
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/pre-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ jobs:
cache: 'npm'
check-latest: true
registry-url: 'https://registry.npmjs.org'
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: Install core dependencies
run: npm ci --no-audit
- name: Extract tag and version
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ jobs:
check-latest: true
registry-url: 'https://registry.npmjs.org'
if: ${{ steps.release.outputs.releases_created }}
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
if: ${{ steps.release.outputs.releases_created }}
- name: Install dependencies
run: CI=1 npm ci
if: ${{ steps.release.outputs.releases_created }}
Expand Down
21 changes: 0 additions & 21 deletions .github/workflows/test-deno.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .github/workflows/test-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ jobs:
with:
node-version: 18
check-latest: true
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: Install netlify-cli and npm
run: npm install -g netlify-cli npm
- name: NPM Install
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ jobs:
with:
node-version: 16
check-latest: true
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: NPM Install
run: npm install
- name: Linting
run: npm run format:ci
- name: Run tests against next@latest
run: npm test
- name: Run Deno tests
run: npm run test:deno
canary:
name: Unit tests (Canary)
runs-on: ${{ matrix.os }}
Expand All @@ -54,6 +59,9 @@ jobs:
with:
node-version: 16
check-latest: true
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: NPM Install
run: npm install
- name: Install Next.js Canary
Expand Down
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ test/e2e

**/CHANGELOG.md
packages/runtime/lib
packages/runtime/dist-types
packages/runtime/dist-types
packages/runtime/src/templates/vendor
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"deno.enablePaths": [
"packages/runtime/src/templates/edge",
"packages/runtime/src/templates/edge-shared",
"packages/runtime/src/templates/vendor",
"packages/runtime/src/templates/vendor.ts",
"demos/middleware/.netlify/edge-functions",
"demos/server-components/.netlify/edge-functions",
],
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/templates/vendor
4 changes: 4 additions & 0 deletions packages/runtime/html_rewriter.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
56c56
< fetch(new URL("./vendor/html_rewriter_bg.wasm", import.meta.url).href)
---
Copy link
Contributor Author

@Skn0tt Skn0tt Sep 29, 2023

Choose a reason for hiding this comment

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

This looks like we're replacing the from-disk fetch with a HTTP request at runtime here. This is required because deno vendor doesn't download the WASM onto disk (for whatever reason), so fetching this from disk would fail. This was already the case before this PR, but before this file was imported via https://, meaning that import.meta.url was something like https://deno.land/x/... - now that we're accessing this file from disk, it's more like file://.netlify/edge-functions/vendor.... Before, with the HTTPS address, fetch would fall back to an HTTP request anyways.

So TL;DR: This looks like we're replacing a solid disk-read with a brittle network fetch. In reality, it's always been a brittle network fetch, so this is fine-ish!

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we download that wasm file ourselves and manually vendor it? i.e. manually pull from https://github.com/worker-tools/html-rewriter/blob/v0.1.0-pre.17/vendor/html_rewriter_bg.wasm and avoid doing a patch (it's bit icky to me, but manual download is icky as well, so I guess - pick your poison). This way at least we would avoid this brittle network fetch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried that, but the problem is not only that deno vendor doesn't download WASM files - Deno's eszip tool (that we use for bundling) also isn't able to detect the import, so it doesn't bundle the file. This will probably change once https://github.com/tc39/proposal-source-phase-imports lands, but until then the HTTP fetch is the only thing we can do :/

> fetch("https://deno.land/x/[email protected]/vendor/html_rewriter_bg.wasm")
5 changes: 4 additions & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"lib/**/*",
"src/templates/edge/*",
"src/templates/edge-shared/*",
"src/templates/vendor/*",
"index.js",
"manifest.yml"
],
Expand Down Expand Up @@ -52,7 +53,9 @@
"publish:install": "npm ci",
"publish:test": "cd .. && npm ci && npm test",
"clean": "rimraf lib dist-types",
"build": "tsc",
"build": "run-s build:*",
"build:vendor": "rimraf src/templates/vendor && deno vendor src/templates/vendor.ts --output=src/templates/vendor && patch src/templates/vendor/deno.land/x/[email protected]/index.ts html_rewriter.patch",
"build:tsc": "tsc",
"watch": "tsc --watch",
"prepare": "npm run build"
},
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/src/helpers/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ export const writeEdgeFunctions = async ({
const nextConfig = nextConfigFile.config
const usesAppDir = nextConfig.experimental?.appDir

await copy(getEdgeTemplatePath('../vendor'), join(edgeFunctionRoot, 'vendor'))
await copy(getEdgeTemplatePath('../edge-shared'), join(edgeFunctionRoot, 'edge-shared'))
await writeJSON(join(edgeFunctionRoot, 'edge-shared', 'nextConfig.json'), nextConfig)
await copy(join(publish, 'prerender-manifest.json'), join(edgeFunctionRoot, 'edge-shared', 'prerender-manifest.json'))
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime/src/templates/edge-shared/next-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
*/

// Deno imports
import type { Key } from 'https://deno.land/x/[email protected]/index.ts'
import type { Key } from '../vendor/deno.land/x/[email protected]/index.ts'

import { compile, pathToRegexp } from 'https://deno.land/x/[email protected]/index.ts'
import { getCookies } from 'https://deno.land/std@0.148.0/http/cookie.ts'
import { compile, pathToRegexp } from '../vendor/deno.land/x/[email protected]/index.ts'
import { getCookies } from '../vendor/deno.land/std@0.175.0/http/cookie.ts'

// Inlined/re-implemented types

Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/templates/edge-shared/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Context } from 'https://edge.netlify.com'
import { ElementHandlers, HTMLRewriter } from 'https://deno.land/x/[email protected]/index.ts'
import { ElementHandlers, HTMLRewriter } from '../vendor/deno.land/x/[email protected]/index.ts'

export interface FetchEventResult {
response: Response
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime/src/templates/edge/next-dev.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextRequest } from 'https://esm.sh/v91/[email protected]/deno/dist/server/web/spec-extension/request.js'
import { NextResponse } from 'https://esm.sh/v91/[email protected]/deno/dist/server/web/spec-extension/response.js'
import { fromFileUrl } from 'https://deno.land/std@0.151.0/path/mod.ts'
import { NextRequest } from '../vendor/esm.sh/v91/[email protected]/deno/dist/server/web/spec-extension/request.js'
import { NextResponse } from '../vendor/esm.sh/v91/[email protected]/deno/dist/server/web/spec-extension/response.js'
import { fromFileUrl } from '../vendor/deno.land/std@0.175.0/path/mod.ts'
import { buildResponse, isFunction } from '../edge-shared/utils.ts'

globalThis.NFRequestContextMap ||= new Map()
Expand Down
12 changes: 6 additions & 6 deletions packages/runtime/src/templates/edge/shims.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// @ts-check
// deno-lint-ignore-file prefer-const no-unused-vars
import { decode as _base64Decode } from 'https://deno.land/[email protected]/encoding/base64.ts'
import BufferCompat from 'https://deno.land/[email protected]/node/buffer.ts'
import EventsCompat from 'https://deno.land/[email protected]/node/events.ts'
import AsyncHooksCompat from 'https://deno.land/[email protected]/node/async_hooks.ts'
import AssertCompat from 'https://deno.land/[email protected]/node/assert.ts'
import UtilCompat from 'https://deno.land/[email protected]/node/util.ts'
import { decode as _base64Decode } from '../vendor/deno.land/[email protected]/encoding/base64.ts'
import BufferCompat from '../vendor/deno.land/[email protected]/node/buffer.ts'
import EventsCompat from '../vendor/deno.land/[email protected]/node/events.ts'
import AsyncHooksCompat from '../vendor/deno.land/[email protected]/node/async_hooks.ts'
import AssertCompat from '../vendor/deno.land/[email protected]/node/assert.ts'
import UtilCompat from '../vendor/deno.land/[email protected]/node/util.ts'

/**
* These are the shims, polyfills and other kludges to make Next.js work in standards-compliant runtime.
Expand Down
21 changes: 21 additions & 0 deletions packages/runtime/src/templates/vendor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// this file isn't meant to be imported.
// it's a list of all external modules that we use,
// and we vendor those into the `vendor/` directory
// for consumption in other files.
// Vendoring happens automatically as part of runtime `build` script.
// You can trigger just vendoring without full package build by running `build:vendor` script.

import 'https://deno.land/[email protected]/encoding/base64.ts'
import 'https://deno.land/[email protected]/http/cookie.ts'
import 'https://deno.land/[email protected]/node/buffer.ts'
import 'https://deno.land/[email protected]/node/events.ts'
import 'https://deno.land/[email protected]/node/async_hooks.ts'
import 'https://deno.land/[email protected]/node/assert.ts'
import 'https://deno.land/[email protected]/node/util.ts'
import 'https://deno.land/[email protected]/path/mod.ts'

import 'https://deno.land/x/[email protected]/index.ts'
import 'https://deno.land/x/[email protected]/index.ts'

import 'https://esm.sh/v91/[email protected]/deno/dist/server/web/spec-extension/request.js'
import 'https://esm.sh/v91/[email protected]/deno/dist/server/web/spec-extension/response.js'
3 changes: 2 additions & 1 deletion packages/runtime/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
],
"exclude": [
"src/templates/edge/*",
"src/templates/edge-shared/*"
"src/templates/edge-shared/*",
"src/templates/vendor/*"
]
}