next.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. require("./node-polyfill-fetch");
  7. var log = _interopRequireWildcard(require("../build/output/log"));
  8. var _config = _interopRequireDefault(require("./config"));
  9. var _path = require("path");
  10. var _constants = require("../lib/constants");
  11. var _constants1 = require("../shared/lib/constants");
  12. var _utils = require("./utils");
  13. function _interopRequireDefault(obj) {
  14. return obj && obj.__esModule ? obj : {
  15. default: obj
  16. };
  17. }
  18. function _getRequireWildcardCache() {
  19. if (typeof WeakMap !== "function") return null;
  20. var cache = new WeakMap();
  21. _getRequireWildcardCache = function() {
  22. return cache;
  23. };
  24. return cache;
  25. }
  26. function _interopRequireWildcard(obj) {
  27. if (obj && obj.__esModule) {
  28. return obj;
  29. }
  30. if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
  31. return {
  32. default: obj
  33. };
  34. }
  35. var cache = _getRequireWildcardCache();
  36. if (cache && cache.has(obj)) {
  37. return cache.get(obj);
  38. }
  39. var newObj = {};
  40. var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
  41. for(var key in obj){
  42. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  43. var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
  44. if (desc && (desc.get || desc.set)) {
  45. Object.defineProperty(newObj, key, desc);
  46. } else {
  47. newObj[key] = obj[key];
  48. }
  49. }
  50. }
  51. newObj.default = obj;
  52. if (cache) {
  53. cache.set(obj, newObj);
  54. }
  55. return newObj;
  56. }
  57. let ServerImpl;
  58. const getServerImpl = async ()=>{
  59. if (ServerImpl === undefined) ServerImpl = (await Promise.resolve(require("./next-server"))).default;
  60. return ServerImpl;
  61. };
  62. class NextServer {
  63. constructor(options){
  64. this.options = options;
  65. }
  66. get hostname() {
  67. return this.options.hostname;
  68. }
  69. get port() {
  70. return this.options.port;
  71. }
  72. getRequestHandler() {
  73. return async (req, res, parsedUrl)=>{
  74. const requestHandler = await this.getServerRequestHandler();
  75. return requestHandler(req, res, parsedUrl);
  76. };
  77. }
  78. getUpgradeHandler() {
  79. return async (req, socket, head)=>{
  80. const server = await this.getServer();
  81. // @ts-expect-error we mark this as protected so it
  82. // causes an error here
  83. return server.handleUpgrade.apply(server, [
  84. req,
  85. socket,
  86. head
  87. ]);
  88. };
  89. }
  90. setAssetPrefix(assetPrefix) {
  91. if (this.server) {
  92. this.server.setAssetPrefix(assetPrefix);
  93. } else {
  94. this.preparedAssetPrefix = assetPrefix;
  95. }
  96. }
  97. logError(...args) {
  98. if (this.server) {
  99. this.server.logError(...args);
  100. }
  101. }
  102. async render(...args) {
  103. const server = await this.getServer();
  104. return server.render(...args);
  105. }
  106. async renderToHTML(...args) {
  107. const server = await this.getServer();
  108. return server.renderToHTML(...args);
  109. }
  110. async renderError(...args) {
  111. const server = await this.getServer();
  112. return server.renderError(...args);
  113. }
  114. async renderErrorToHTML(...args) {
  115. const server = await this.getServer();
  116. return server.renderErrorToHTML(...args);
  117. }
  118. async render404(...args) {
  119. const server = await this.getServer();
  120. return server.render404(...args);
  121. }
  122. async serveStatic(...args) {
  123. const server = await this.getServer();
  124. return server.serveStatic(...args);
  125. }
  126. async prepare() {
  127. const server = await this.getServer();
  128. return server.prepare();
  129. }
  130. async close() {
  131. const server = await this.getServer();
  132. return server.close();
  133. }
  134. async createServer(options) {
  135. if (options.dev) {
  136. const DevServer = require("./dev/next-dev-server").default;
  137. return new DevServer(options);
  138. }
  139. const ServerImplementation = await getServerImpl();
  140. return new ServerImplementation(options);
  141. }
  142. async loadConfig() {
  143. return (0, _config).default(this.options.dev ? _constants1.PHASE_DEVELOPMENT_SERVER : _constants1.PHASE_PRODUCTION_SERVER, (0, _path).resolve(this.options.dir || "."), this.options.conf);
  144. }
  145. async getServer() {
  146. if (!this.serverPromise) {
  147. setTimeout(getServerImpl, 10);
  148. this.serverPromise = this.loadConfig().then(async (conf)=>{
  149. this.server = await this.createServer({
  150. ...this.options,
  151. conf
  152. });
  153. if (this.preparedAssetPrefix) {
  154. this.server.setAssetPrefix(this.preparedAssetPrefix);
  155. }
  156. return this.server;
  157. });
  158. }
  159. return this.serverPromise;
  160. }
  161. async getServerRequestHandler() {
  162. // Memoize request handler creation
  163. if (!this.reqHandlerPromise) {
  164. this.reqHandlerPromise = this.getServer().then((server)=>server.getRequestHandler().bind(server));
  165. }
  166. return this.reqHandlerPromise;
  167. }
  168. }
  169. exports.NextServer = NextServer;
  170. // This file is used for when users run `require('next')`
  171. function createServer(options) {
  172. if (options == null) {
  173. throw new Error("The server has not been instantiated properly. https://nextjs.org/docs/messages/invalid-server-options");
  174. }
  175. if (!("isNextDevCommand" in options) && process.env.NODE_ENV && ![
  176. "production",
  177. "development",
  178. "test"
  179. ].includes(process.env.NODE_ENV)) {
  180. log.warn(_constants.NON_STANDARD_NODE_ENV);
  181. }
  182. if (options.dev && typeof options.dev !== "boolean") {
  183. console.warn("Warning: 'dev' is not a boolean which could introduce unexpected behavior. https://nextjs.org/docs/messages/invalid-server-options");
  184. }
  185. if (_utils.shouldUseReactRoot) {
  186. process.env.__NEXT_REACT_ROOT = "true";
  187. }
  188. return new NextServer(options);
  189. }
  190. // Support commonjs `require('next')`
  191. module.exports = createServer;
  192. exports = module.exports;
  193. var _default = createServer;
  194. exports.default = _default;
  195. //# sourceMappingURL=next.js.map