-
Notifications
You must be signed in to change notification settings - Fork 89
refactor: extract edge runtime shims into separate file #1916
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// deno-lint-ignore-file no-var prefer-const no-unused-vars no-explicit-any | ||
import { decode as _base64Decode } from 'https://deno.land/std@0.175.0/encoding/base64.ts' | ||
import { AsyncLocalStorage as ALSCompat } from 'https://deno.land/std@0.175.0/node/async_hooks.ts' | ||
|
||
/** | ||
* These are the shims, polyfills and other kludges to make Next.js work in standards-compliant runtime. | ||
* This file isn't imported, but is instead inlined along with other chunks into the edge bundle. | ||
*/ | ||
|
||
declare global { | ||
var process: { | ||
env: Record<string, string> | ||
} | ||
var EdgeRuntime: string | ||
var AsyncLocalStorage: typeof ALSCompat | ||
var _ASSETS: Record<string, string> | ||
ascorbic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Deno defines "window", but naughty libraries think this means it's a browser | ||
delete (globalThis as Omit<typeof globalThis, 'window'> & Pick<Partial<typeof globalThis>, 'window'>).window | ||
ascorbic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
globalThis.process = { | ||
env: { ...Deno.env.toObject(), NEXT_RUNTIME: 'edge', NEXT_PRIVATE_MINIMAL_MODE: '1' }, | ||
} | ||
globalThis.EdgeRuntime = 'netlify-edge' | ||
let _ENTRIES = {} | ||
|
||
// Next.js expects this as a global | ||
globalThis.AsyncLocalStorage = ALSCompat | ||
|
||
// Next.js uses this extension to the Headers API implemented by Cloudflare workerd | ||
if (!('getAll' in Headers.prototype)) { | ||
;(Headers as any).prototype.getAll = function getAll(name: string) { | ||
name = name.toLowerCase() | ||
if (name !== 'set-cookie') { | ||
throw new Error('Headers.getAll is only supported for Set-Cookie') | ||
} | ||
return [...this.entries()].filter(([key]) => key === name).map(([, value]) => value) | ||
} | ||
} | ||
// Next uses blob: urls to refer to local assets, so we need to intercept these | ||
const _fetch = globalThis.fetch | ||
const fetch: typeof globalThis.fetch = async (url, init) => { | ||
try { | ||
if (url instanceof URL && url.href?.startsWith('blob:')) { | ||
const key = url.href.slice(5) | ||
if (key in _ASSETS) { | ||
return new Response(_base64Decode(_ASSETS[key])) | ||
} | ||
} | ||
return await _fetch(url, init) | ||
} catch (error) { | ||
console.error(error) | ||
throw error | ||
} | ||
} | ||
|
||
// Next edge runtime uses "self" as a function-scoped global-like object, but some of the older polyfills expect it to equal globalThis | ||
// See https://nextjs.org/docs/basic-features/supported-browsers-features#polyfills | ||
const self = { ...globalThis, fetch } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just notice there’s the same function name twice in fs-extra but with different casings. Odd.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think that it's a legacy thing. Confusing, as I'm not sure which is "preferred"