node.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _apiUtils = require("../api-utils");
  6. var _node = require("../api-utils/node");
  7. var _requestMeta = require("../request-meta");
  8. var _index = require("./index");
  9. let _NEXT_REQUEST_META = _requestMeta.NEXT_REQUEST_META;
  10. class NodeNextRequest extends _index.BaseNextRequest {
  11. get originalRequest() {
  12. // Need to mimic these changes to the original req object for places where we use it:
  13. // render.tsx, api/ssg requests
  14. this._req[_requestMeta.NEXT_REQUEST_META] = this[_requestMeta.NEXT_REQUEST_META];
  15. this._req.url = this.url;
  16. this._req.cookies = this.cookies;
  17. return this._req;
  18. }
  19. set originalRequest(value) {
  20. this._req = value;
  21. }
  22. constructor(_req){
  23. super(_req.method.toUpperCase(), _req.url, _req);
  24. this._req = _req;
  25. this.headers = this._req.headers;
  26. this[_NEXT_REQUEST_META] = {};
  27. }
  28. async parseBody(limit) {
  29. return (0, _node).parseBody(this._req, limit);
  30. }
  31. }
  32. exports.NodeNextRequest = NodeNextRequest;
  33. class NodeNextResponse extends _index.BaseNextResponse {
  34. get originalResponse() {
  35. if (_apiUtils.SYMBOL_CLEARED_COOKIES in this) {
  36. this._res[_apiUtils.SYMBOL_CLEARED_COOKIES] = this[_apiUtils.SYMBOL_CLEARED_COOKIES];
  37. }
  38. return this._res;
  39. }
  40. constructor(_res){
  41. super(_res);
  42. this._res = _res;
  43. this.textBody = undefined;
  44. }
  45. get sent() {
  46. return this._res.finished || this._res.headersSent;
  47. }
  48. get statusCode() {
  49. return this._res.statusCode;
  50. }
  51. set statusCode(value) {
  52. this._res.statusCode = value;
  53. }
  54. get statusMessage() {
  55. return this._res.statusMessage;
  56. }
  57. set statusMessage(value) {
  58. this._res.statusMessage = value;
  59. }
  60. setHeader(name, value) {
  61. this._res.setHeader(name, value);
  62. return this;
  63. }
  64. getHeaderValues(name) {
  65. const values = this._res.getHeader(name);
  66. if (values === undefined) return undefined;
  67. return (Array.isArray(values) ? values : [
  68. values
  69. ]).map((value)=>value.toString());
  70. }
  71. hasHeader(name) {
  72. return this._res.hasHeader(name);
  73. }
  74. getHeader(name) {
  75. const values = this.getHeaderValues(name);
  76. return Array.isArray(values) ? values.join(",") : undefined;
  77. }
  78. appendHeader(name, value) {
  79. var ref;
  80. const currentValues = (ref = this.getHeaderValues(name)) != null ? ref : [];
  81. if (!currentValues.includes(value)) {
  82. this._res.setHeader(name, [
  83. ...currentValues,
  84. value
  85. ]);
  86. }
  87. return this;
  88. }
  89. body(value) {
  90. this.textBody = value;
  91. return this;
  92. }
  93. send() {
  94. this._res.end(this.textBody);
  95. }
  96. }
  97. exports.NodeNextResponse = NodeNextResponse;
  98. //# sourceMappingURL=node.js.map