render-result.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. class RenderResult {
  7. constructor(response, { contentType } = {}){
  8. this._result = response;
  9. this._contentType = contentType;
  10. }
  11. contentType() {
  12. return this._contentType;
  13. }
  14. toUnchunkedString() {
  15. if (typeof this._result !== "string") {
  16. throw new Error("invariant: dynamic responses cannot be unchunked. This is a bug in Next.js");
  17. }
  18. return this._result;
  19. }
  20. pipe(res) {
  21. if (typeof this._result === "string") {
  22. throw new Error("invariant: static responses cannot be piped. This is a bug in Next.js");
  23. }
  24. const response = this._result;
  25. const flush = typeof res.flush === "function" ? ()=>res.flush() : ()=>{};
  26. return (async ()=>{
  27. const reader = response.getReader();
  28. let fatalError = false;
  29. try {
  30. while(true){
  31. const { done , value } = await reader.read();
  32. if (done) {
  33. res.end();
  34. return;
  35. }
  36. fatalError = true;
  37. res.write(value);
  38. flush();
  39. }
  40. } catch (err) {
  41. if (fatalError) {
  42. res.destroy(err);
  43. }
  44. throw err;
  45. }
  46. })();
  47. }
  48. isDynamic() {
  49. return typeof this._result !== "string";
  50. }
  51. static fromStatic(value) {
  52. return new RenderResult(value);
  53. }
  54. static empty = RenderResult.fromStatic("");
  55. }
  56. exports.default = RenderResult;
  57. //# sourceMappingURL=render-result.js.map