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 2403974 commit daee459
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 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 { WasmBinding } from '../../../build/webpack/loaders/get-module-build-info'
import {
decorateServerError,
Expand All @@ -14,7 +13,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 @@ -68,7 +67,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 @@ -197,7 +201,7 @@ Learn More: https://nextjs.org/docs/messages/middleware-dynamic-wasm-compilation
}

const __fetch = context.fetch
context.fetch = (input: RequestInfo, init: RequestInit = {}) => {
context.fetch = (input, init = {}) => {
init.headers = new Headers(init.headers ?? {})
const prevs =
init.headers.get(`x-middleware-subrequest`)?.split(':') || []
Expand Down Expand Up @@ -236,10 +240,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 @@ -321,7 +328,7 @@ function createProcessPolyfill(
const warnedAlready = new Set<string>()

function addStub(
context: Primitives,
context: EdgeRuntime['context'],
name: string,
contextOptions: Pick<ModuleContextOptions, 'onWarning'>
) {
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 daee459

Please sign in to comment.