sandbox.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.run = exports.getRuntimeContext = exports.ErrorSource = void 0;
  6. var _middleware = require("next/dist/compiled/@next/react-dev-overlay/dist/middleware");
  7. var _context = require("./context");
  8. var _bodyStreams = require("../../body-streams");
  9. const ErrorSource = Symbol("SandboxError");
  10. exports.ErrorSource = ErrorSource;
  11. const FORBIDDEN_HEADERS = [
  12. "content-length",
  13. "content-encoding",
  14. "transfer-encoding",
  15. ];
  16. /**
  17. * Decorates the runner function making sure all errors it can produce are
  18. * tagged with `edge-server` so they can properly be rendered in dev.
  19. */ function withTaggedErrors(fn) {
  20. return (params)=>{
  21. return fn(params).then((result)=>{
  22. var ref;
  23. return {
  24. ...result,
  25. waitUntil: result == null ? void 0 : (ref = result.waitUntil) == null ? void 0 : ref.catch((error)=>{
  26. // TODO: used COMPILER_NAMES.edgeServer instead. Verify that it does not increase the runtime size.
  27. throw (0, _middleware).getServerError(error, "edge-server");
  28. })
  29. };
  30. }).catch((error)=>{
  31. // TODO: used COMPILER_NAMES.edgeServer instead
  32. throw (0, _middleware).getServerError(error, "edge-server");
  33. });
  34. };
  35. }
  36. const getRuntimeContext = async (params)=>{
  37. var _onWarning;
  38. const { runtime , evaluateInContext } = await (0, _context).getModuleContext({
  39. moduleName: params.name,
  40. onWarning: (_onWarning = params.onWarning) != null ? _onWarning : ()=>{},
  41. useCache: params.useCache !== false,
  42. env: params.env,
  43. edgeFunctionEntry: params.edgeFunctionEntry,
  44. distDir: params.distDir
  45. });
  46. for (const paramPath of params.paths){
  47. evaluateInContext(paramPath);
  48. }
  49. return runtime;
  50. };
  51. exports.getRuntimeContext = getRuntimeContext;
  52. const run = withTaggedErrors(async (params)=>{
  53. var ref;
  54. const runtime = await getRuntimeContext(params);
  55. const subreq = params.request.headers[`x-middleware-subrequest`];
  56. const subrequests = typeof subreq === "string" ? subreq.split(":") : [];
  57. if (subrequests.includes(params.name)) {
  58. return {
  59. waitUntil: Promise.resolve(),
  60. response: new runtime.context.Response(null, {
  61. headers: {
  62. "x-middleware-next": "1"
  63. }
  64. })
  65. };
  66. }
  67. const edgeFunction = runtime.context._ENTRIES[`middleware_${params.name}`].default;
  68. const cloned = ![
  69. "HEAD",
  70. "GET"
  71. ].includes(params.request.method) ? (ref = params.request.body) == null ? void 0 : ref.cloneBodyStream() : undefined;
  72. try {
  73. const result = await edgeFunction({
  74. request: {
  75. ...params.request,
  76. body: cloned && (0, _bodyStreams).requestToBodyStream(runtime.context, cloned)
  77. }
  78. });
  79. for (const headerName of FORBIDDEN_HEADERS){
  80. result.response.headers.delete(headerName);
  81. }
  82. return result;
  83. } finally{
  84. var ref1;
  85. await ((ref1 = params.request.body) == null ? void 0 : ref1.finalize());
  86. }
  87. });
  88. exports.run = run;
  89. //# sourceMappingURL=sandbox.js.map