File size: 5,094 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import type { NextRequest } from './spec-extension/request'
import type {
  AppRouteRouteHandlerContext,
  AppRouteRouteModule,
} from '../route-modules/app-route/module'

import './globals'

import { adapter, type AdapterOptions } from './adapter'
import { IncrementalCache } from '../lib/incremental-cache'
import { RouteMatcher } from '../route-matchers/route-matcher'
import type { NextFetchEvent } from './spec-extension/fetch-event'
import { internal_getCurrentFunctionWaitUntil } from './internal-edge-wait-until'
import { getServerUtils } from '../server-utils'
import { searchParamsToUrlQuery } from '../../shared/lib/router/utils/querystring'
import { CloseController, trackStreamConsumed } from './web-on-close'
import { getEdgePreviewProps } from './get-edge-preview-props'
import type { NextConfigComplete } from '../config-shared'

export interface WrapOptions {
  nextConfig: NextConfigComplete
}

/**
 * EdgeRouteModuleWrapper is a wrapper around a route module.
 *
 * Note that this class should only be used in the edge runtime.
 */
export class EdgeRouteModuleWrapper {
  private readonly matcher: RouteMatcher

  /**
   * The constructor is wrapped with private to ensure that it can only be
   * constructed by the static wrap method.
   *
   * @param routeModule the route module to wrap
   */
  private constructor(
    private readonly routeModule: AppRouteRouteModule,
    private readonly nextConfig: NextConfigComplete
  ) {
    // TODO: (wyattjoh) possibly allow the module to define it's own matcher
    this.matcher = new RouteMatcher(routeModule.definition)
  }

  /**
   * This will wrap a module with the EdgeModuleWrapper and return a function
   * that can be used as a handler for the edge runtime.
   *
   * @param module the module to wrap
   * @param options any options that should be passed to the adapter and
   *                override the ones passed from the runtime
   * @returns a function that can be used as a handler for the edge runtime
   */
  public static wrap(routeModule: AppRouteRouteModule, options: WrapOptions) {
    // Create the module wrapper.
    const wrapper = new EdgeRouteModuleWrapper(routeModule, options.nextConfig)

    // Return the wrapping function.
    return (opts: AdapterOptions) => {
      return adapter({
        ...opts,
        IncrementalCache,
        // Bind the handler method to the wrapper so it still has context.
        handler: wrapper.handler.bind(wrapper),
      })
    }
  }

  private async handler(
    request: NextRequest,
    evt: NextFetchEvent
  ): Promise<Response> {
    const utils = getServerUtils({
      pageIsDynamic: this.matcher.isDynamic,
      page: this.matcher.definition.pathname,
      basePath: request.nextUrl.basePath,
      // We don't need the `handleRewrite` util, so can just pass an empty object
      rewrites: {},
      // only used for rewrites, so setting an arbitrary default value here
      caseSensitive: false,
    })

    const { params } = utils.normalizeDynamicRouteParams(
      searchParamsToUrlQuery(request.nextUrl.searchParams),
      false
    )

    const waitUntil = evt.waitUntil.bind(evt)
    const closeController = new CloseController()

    const previewProps = getEdgePreviewProps()

    // Create the context for the handler. This contains the params from the
    // match (if any).
    const context: AppRouteRouteHandlerContext = {
      params,
      prerenderManifest: {
        version: 4,
        routes: {},
        dynamicRoutes: {},
        preview: previewProps,
        notFoundRoutes: [],
      },
      renderOpts: {
        supportsDynamicResponse: true,
        waitUntil,
        onClose: closeController.onClose.bind(closeController),
        onAfterTaskError: undefined,
        experimental: {
          cacheComponents: !!process.env.__NEXT_CACHE_COMPONENTS,
          authInterrupts: !!process.env.__NEXT_EXPERIMENTAL_AUTH_INTERRUPTS,
        },
        cacheLifeProfiles: this.nextConfig.experimental.cacheLife,
      },
      sharedContext: {
        buildId: '', // TODO: Populate this properly.
      },
    }

    // Get the response from the handler.
    let res = await this.routeModule.handle(request, context)

    const waitUntilPromises = [internal_getCurrentFunctionWaitUntil()]
    if (context.renderOpts.pendingWaitUntil) {
      waitUntilPromises.push(context.renderOpts.pendingWaitUntil)
    }
    evt.waitUntil(Promise.all(waitUntilPromises))

    if (!res.body) {
      // we can delay running it until a bit later --
      // if it's needed, we'll have a `waitUntil` lock anyway.
      setTimeout(() => closeController.dispatchClose(), 0)
    } else {
      // NOTE: if this is a streaming response, onClose may be called later,
      // so we can't rely on `closeController.listeners` -- it might be 0 at this point.
      const trackedBody = trackStreamConsumed(res.body, () =>
        closeController.dispatchClose()
      )
      res = new Response(trackedBody, {
        status: res.status,
        statusText: res.statusText,
        headers: res.headers,
      })
    }

    return res
  }
}