web.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _index = require("./index");
  6. class WebNextRequest extends _index.BaseNextRequest {
  7. constructor(request){
  8. const url = new URL(request.url);
  9. super(request.method, url.href.slice(url.origin.length), request.clone().body);
  10. this.request = request;
  11. this.headers = {};
  12. for (const [name, value] of request.headers.entries()){
  13. this.headers[name] = value;
  14. }
  15. }
  16. async parseBody(_limit) {
  17. throw new Error("parseBody is not implemented in the web runtime");
  18. }
  19. }
  20. exports.WebNextRequest = WebNextRequest;
  21. class WebNextResponse extends _index.BaseNextResponse {
  22. get sent() {
  23. return this._sent;
  24. }
  25. constructor(transformStream = new TransformStream()){
  26. super(transformStream.writable);
  27. this.transformStream = transformStream;
  28. this.headers = new Headers();
  29. this.textBody = undefined;
  30. this._sent = false;
  31. this.sendPromise = new Promise((resolve)=>{
  32. this.sendResolve = resolve;
  33. });
  34. this.response = this.sendPromise.then(()=>{
  35. var _textBody;
  36. return new Response((_textBody = this.textBody) != null ? _textBody : this.transformStream.readable, {
  37. headers: this.headers,
  38. status: this.statusCode,
  39. statusText: this.statusMessage
  40. });
  41. });
  42. }
  43. setHeader(name, value) {
  44. this.headers.delete(name);
  45. for (const val of Array.isArray(value) ? value : [
  46. value
  47. ]){
  48. this.headers.append(name, val);
  49. }
  50. return this;
  51. }
  52. getHeaderValues(name) {
  53. var ref;
  54. // https://developer.mozilla.org/en-US/docs/Web/API/Headers/get#example
  55. return (ref = this.getHeader(name)) == null ? void 0 : ref.split(",").map((v)=>v.trimStart());
  56. }
  57. getHeader(name) {
  58. var ref;
  59. return (ref = this.headers.get(name)) != null ? ref : undefined;
  60. }
  61. hasHeader(name) {
  62. return this.headers.has(name);
  63. }
  64. appendHeader(name, value) {
  65. this.headers.append(name, value);
  66. return this;
  67. }
  68. body(value) {
  69. this.textBody = value;
  70. return this;
  71. }
  72. send() {
  73. var _obj, ref;
  74. (ref = (_obj = this).sendResolve) == null ? void 0 : ref.call(_obj);
  75. this._sent = true;
  76. }
  77. toResponse() {
  78. return this.response;
  79. }
  80. }
  81. exports.WebNextResponse = WebNextResponse;
  82. //# sourceMappingURL=web.js.map