next-server.d.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /// <reference types="node" />
  2. /// <reference types="node" />
  3. /// <reference types="node" />
  4. /// <reference types="node" />
  5. /// <reference types="node" />
  6. import './node-polyfill-fetch';
  7. import './node-polyfill-web-streams';
  8. import type { Route } from './router';
  9. import { CacheFs } from '../shared/lib/utils';
  10. import type { MiddlewareManifest } from '../build/webpack/plugins/middleware-plugin';
  11. import type RenderResult from './render-result';
  12. import type { FetchEventResult } from './web/types';
  13. import type { PrerenderManifest } from '../build';
  14. import type { CustomRoutes } from '../lib/load-custom-routes';
  15. import type { BaseNextRequest, BaseNextResponse } from './base-http';
  16. import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin';
  17. import type { PayloadOptions } from './send-payload';
  18. import type { NextParsedUrlQuery, NextUrlWithParsedQuery } from './request-meta';
  19. import type { Params } from '../shared/lib/router/utils/route-matcher';
  20. import type { NextConfig } from './config-shared';
  21. import type { DynamicRoutes, PageChecker } from './router';
  22. import { IncomingMessage, ServerResponse } from 'http';
  23. import { UrlWithParsedQuery } from 'url';
  24. import { NodeNextRequest, NodeNextResponse } from './base-http/node';
  25. import { ParsedUrlQuery } from 'querystring';
  26. import { RenderOpts } from './render';
  27. import { ParsedUrl } from '../shared/lib/router/utils/parse-url';
  28. import BaseServer, { Options, FindComponentsResult, MiddlewareRoutingItem, RoutingItem, RequestContext } from './base-server';
  29. import { FontManifest } from './font-utils';
  30. import ResponseCache from './response-cache';
  31. export * from './base-server';
  32. export interface NodeRequestHandler {
  33. (req: IncomingMessage | BaseNextRequest, res: ServerResponse | BaseNextResponse, parsedUrl?: NextUrlWithParsedQuery | undefined): Promise<void>;
  34. }
  35. export default class NextNodeServer extends BaseServer {
  36. private imageResponseCache?;
  37. constructor(options: Options);
  38. private compression;
  39. protected loadEnvConfig({ dev, forceReload, }: {
  40. dev: boolean;
  41. forceReload?: boolean;
  42. }): void;
  43. protected getResponseCache({ dev }: {
  44. dev: boolean;
  45. }): ResponseCache;
  46. protected getPublicDir(): string;
  47. protected getHasStaticDir(): boolean;
  48. protected getPagesManifest(): PagesManifest | undefined;
  49. protected getAppPathsManifest(): PagesManifest | undefined;
  50. protected hasPage(pathname: string): Promise<boolean>;
  51. protected getBuildId(): string;
  52. protected getCustomRoutes(): CustomRoutes;
  53. protected generateImageRoutes(): Route[];
  54. protected generateStaticRoutes(): Route[];
  55. protected setImmutableAssetCacheControl(res: BaseNextResponse): void;
  56. protected generateFsStaticRoutes(): Route[];
  57. protected generatePublicRoutes(): Route[];
  58. private _validFilesystemPathSet;
  59. protected getFilesystemPaths(): Set<string>;
  60. protected sendRenderResult(req: NodeNextRequest, res: NodeNextResponse, options: {
  61. result: RenderResult;
  62. type: 'html' | 'json';
  63. generateEtags: boolean;
  64. poweredByHeader: boolean;
  65. options?: PayloadOptions | undefined;
  66. }): Promise<void>;
  67. protected sendStatic(req: NodeNextRequest, res: NodeNextResponse, path: string): Promise<void>;
  68. protected handleCompression(req: NodeNextRequest, res: NodeNextResponse): void;
  69. protected handleUpgrade(req: NodeNextRequest, socket: any, head: any): Promise<void>;
  70. protected proxyRequest(req: NodeNextRequest, res: NodeNextResponse, parsedUrl: ParsedUrl, upgradeHead?: any): Promise<{
  71. finished: boolean;
  72. }>;
  73. protected runApi(req: BaseNextRequest | NodeNextRequest, res: BaseNextResponse | NodeNextResponse, query: ParsedUrlQuery, params: Params | undefined, page: string, builtPagePath: string): Promise<boolean>;
  74. protected renderHTML(req: NodeNextRequest, res: NodeNextResponse, pathname: string, query: NextParsedUrlQuery, renderOpts: RenderOpts): Promise<RenderResult | null>;
  75. protected streamResponseChunk(res: NodeNextResponse, chunk: any): void;
  76. protected imageOptimizer(req: NodeNextRequest, res: NodeNextResponse, paramsResult: import('./image-optimizer').ImageParamsResult): Promise<{
  77. buffer: Buffer;
  78. contentType: string;
  79. maxAge: number;
  80. }>;
  81. protected getPagePath(pathname: string, locales?: string[]): string;
  82. protected renderPageComponent(ctx: RequestContext, bubbleNoFallback: boolean): Promise<false | {
  83. type: "html" | "json";
  84. body: RenderResult;
  85. revalidateOptions?: any;
  86. } | null>;
  87. protected findPageComponents({ pathname, query, params, isAppPath, }: {
  88. pathname: string;
  89. query: NextParsedUrlQuery;
  90. params: Params | null;
  91. isAppPath: boolean;
  92. }): Promise<FindComponentsResult | null>;
  93. protected getFontManifest(): FontManifest;
  94. protected getServerComponentManifest(): any;
  95. protected getServerCSSManifest(): any;
  96. protected getFallback(page: string): Promise<string>;
  97. protected generateRoutes(): {
  98. headers: Route[];
  99. rewrites: {
  100. beforeFiles: Route[];
  101. afterFiles: Route[];
  102. fallback: Route[];
  103. };
  104. fsRoutes: Route[];
  105. redirects: Route[];
  106. catchAllRoute: Route;
  107. catchAllMiddleware: Route[];
  108. pageChecker: PageChecker;
  109. useFileSystemPublicRoutes: boolean;
  110. dynamicRoutes: DynamicRoutes | undefined;
  111. nextConfig: NextConfig;
  112. };
  113. protected ensureApiPage(_pathname: string): Promise<void>;
  114. /**
  115. * Resolves `API` request, in development builds on demand
  116. * @param req http request
  117. * @param res http response
  118. * @param pathname path of request
  119. */
  120. protected handleApiRequest(req: BaseNextRequest, res: BaseNextResponse, pathname: string, query: ParsedUrlQuery): Promise<boolean>;
  121. protected getCacheFilesystem(): CacheFs;
  122. private normalizeReq;
  123. private normalizeRes;
  124. getRequestHandler(): NodeRequestHandler;
  125. render(req: BaseNextRequest | IncomingMessage, res: BaseNextResponse | ServerResponse, pathname: string, query?: NextParsedUrlQuery, parsedUrl?: NextUrlWithParsedQuery, internal?: boolean): Promise<void>;
  126. renderToHTML(req: BaseNextRequest | IncomingMessage, res: BaseNextResponse | ServerResponse, pathname: string, query?: ParsedUrlQuery): Promise<string | null>;
  127. renderError(err: Error | null, req: BaseNextRequest | IncomingMessage, res: BaseNextResponse | ServerResponse, pathname: string, query?: NextParsedUrlQuery, setHeaders?: boolean): Promise<void>;
  128. renderErrorToHTML(err: Error | null, req: BaseNextRequest | IncomingMessage, res: BaseNextResponse | ServerResponse, pathname: string, query?: ParsedUrlQuery): Promise<string | null>;
  129. render404(req: BaseNextRequest | IncomingMessage, res: BaseNextResponse | ServerResponse, parsedUrl?: NextUrlWithParsedQuery, setHeaders?: boolean): Promise<void>;
  130. serveStatic(req: BaseNextRequest | IncomingMessage, res: BaseNextResponse | ServerResponse, path: string, parsedUrl?: UrlWithParsedQuery): Promise<void>;
  131. protected getStaticRoutes(): Route[];
  132. protected isServeableUrl(untrustedFileUrl: string): boolean;
  133. protected generateRewrites({ restrictedRedirectPaths, }: {
  134. restrictedRedirectPaths: string[];
  135. }): {
  136. beforeFiles: Route[];
  137. afterFiles: Route[];
  138. fallback: Route[];
  139. };
  140. protected getMiddlewareManifest(): MiddlewareManifest | null;
  141. /** Returns the middleware routing item if there is one. */
  142. protected getMiddleware(): MiddlewareRoutingItem | undefined;
  143. protected getEdgeFunctions(): RoutingItem[];
  144. /**
  145. * Get information for the edge function located in the provided page
  146. * folder. If the edge function info can't be found it will throw
  147. * an error.
  148. */
  149. protected getEdgeFunctionInfo(params: {
  150. page: string;
  151. /** Whether we should look for a middleware or not */
  152. middleware: boolean;
  153. }): {
  154. name: string;
  155. paths: string[];
  156. env: string[];
  157. wasm: {
  158. filePath: string;
  159. name: string;
  160. }[];
  161. assets: {
  162. filePath: string;
  163. name: string;
  164. }[];
  165. } | null;
  166. /**
  167. * Checks if a middleware exists. This method is useful for the development
  168. * server where we need to check the filesystem. Here we just check the
  169. * middleware manifest.
  170. */
  171. protected hasMiddleware(pathname: string): Promise<boolean>;
  172. /**
  173. * A placeholder for a function to be defined in the development server.
  174. * It will make sure that the root middleware or an edge function has been compiled
  175. * so that we can run it.
  176. */
  177. protected ensureMiddleware(): Promise<void>;
  178. protected ensureEdgeFunction(_params: {
  179. page: string;
  180. appPaths: string[] | null;
  181. }): Promise<void>;
  182. /**
  183. * This method gets all middleware matchers and execute them when the request
  184. * matches. It will make sure that each middleware exists and is compiled and
  185. * ready to be invoked. The development server will decorate it to add warns
  186. * and errors with rich traces.
  187. */
  188. protected runMiddleware(params: {
  189. request: BaseNextRequest;
  190. response: BaseNextResponse;
  191. parsedUrl: ParsedUrl;
  192. parsed: UrlWithParsedQuery;
  193. onWarning?: (warning: Error) => void;
  194. }): Promise<FetchEventResult | {
  195. finished: boolean;
  196. }>;
  197. protected generateCatchAllMiddlewareRoute(devReady?: boolean): Route[];
  198. private _cachedPreviewManifest;
  199. protected getPrerenderManifest(): PrerenderManifest;
  200. protected getRoutesManifest(): any;
  201. protected attachRequestMeta(req: BaseNextRequest, parsedUrl: NextUrlWithParsedQuery): void;
  202. protected runEdgeFunction(params: {
  203. req: BaseNextRequest | NodeNextRequest;
  204. res: BaseNextResponse | NodeNextResponse;
  205. query: ParsedUrlQuery;
  206. params: Params | undefined;
  207. page: string;
  208. appPaths: string[] | null;
  209. onWarning?: (warning: Error) => void;
  210. }): Promise<FetchEventResult | null>;
  211. protected get _isLikeServerless(): boolean;
  212. protected get serverDistDir(): string;
  213. }