Skip to content

Commit

Permalink
Update types for new Edge Runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
javivelasco committed Jul 21, 2022
1 parent 53d9c77 commit fa977cb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
23 changes: 15 additions & 8 deletions packages/next/server/web/sandbox/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Primitives } from 'next/dist/compiled/@edge-runtime/primitives'
import type { AssetBinding } from '../../../build/webpack/loaders/get-module-build-info'
import {
decorateServerError,
Expand All @@ -16,7 +15,7 @@ const WEBPACK_HASH_REGEX =
/__webpack_require__\.h = function\(\) \{ return "[0-9a-f]+"; \}/g

interface ModuleContext {
runtime: EdgeRuntime<Primitives>
runtime: EdgeRuntime
paths: Map<string, string>
warnedEvals: Set<string>
}
Expand Down Expand Up @@ -71,7 +70,12 @@ function getModuleContextShared(options: ModuleContextOptions) {
* with a function that allows to run some code from a given
* filepath within the context.
*/
export async function getModuleContext(options: ModuleContextOptions) {
export async function getModuleContext(options: ModuleContextOptions): Promise<{
evaluateInContext: (filepath: string) => void
runtime: EdgeRuntime
paths: Map<string, string>
warnedEvals: Set<string>
}> {
let moduleContext = options.useCache
? moduleContexts.get(options.moduleName)
: await getModuleContextShared(options)
Expand Down Expand Up @@ -177,7 +181,7 @@ Learn More: https://nextjs.org/docs/messages/middleware-dynamic-wasm-compilation
}

const __fetch = context.fetch
context.fetch = async (input: RequestInfo, init: RequestInit = {}) => {
context.fetch = async (input, init = {}) => {
const assetResponse = await fetchInlineAsset({
input,
assets: options.edgeFunctionEntry.assets,
Expand Down Expand Up @@ -226,10 +230,13 @@ Learn More: https://nextjs.org/docs/messages/middleware-dynamic-wasm-compilation

const __Request = context.Request
context.Request = class extends __Request {
constructor(input: RequestInfo, init?: RequestInit | undefined) {
const url = typeof input === 'string' ? input : input.url
constructor(input: URL | RequestInfo, init?: RequestInit | undefined) {
const url =
typeof input !== 'string' && 'url' in input
? input.url
: String(input)
validateURL(url)
super(input, init)
super(url, init)
}
}

Expand Down Expand Up @@ -311,7 +318,7 @@ function createProcessPolyfill(
const warnedAlready = new Set<string>()

function addStub(
context: Primitives,
context: EdgeRuntime['context'],
name: string,
contextOptions: Pick<ModuleContextOptions, 'onWarning'>
) {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/web/sandbox/fetch-inline-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { resolve } from 'path'
* This allows to embed assets in Edge Runtime.
*/
export async function fetchInlineAsset(options: {
input: RequestInfo
input: RequestInfo | URL
distDir: string
assets: EdgeFunctionDefinition['assets']
context: EdgeRuntime['context']
Expand Down
7 changes: 4 additions & 3 deletions packages/next/server/web/spec-extension/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ export class NextRequest extends Request {
url: NextURL
}

constructor(input: Request | string, init: RequestInit = {}) {
const url = typeof input === 'string' ? input : input.url
constructor(input: URL | RequestInfo, init: RequestInit = {}) {
const url =
typeof input !== 'string' && 'url' in input ? input.url : String(input)
validateURL(url)
super(input, init)
super(url, init)
this[INTERNALS] = {
cookies: new NextCookies(this),
geo: init.geo || {},
Expand Down

0 comments on commit fa977cb

Please sign in to comment.