index.react-server.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { createRequester } from "./_chunks/createRequester-VFTkDs_7.js";
  2. import parseHeaders from "parse-headers";
  3. var __accessCheck = (obj, member, msg) => {
  4. if (!member.has(obj))
  5. throw TypeError("Cannot " + msg);
  6. }, __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj)), __privateAdd = (obj, member, value) => {
  7. if (member.has(obj))
  8. throw TypeError("Cannot add the same private member more than once");
  9. member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
  10. }, __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value), _method, _url, _resHeaders, _headers, _controller, _init, _useAbortSignal;
  11. class FetchXhr {
  12. constructor() {
  13. this.readyState = 0, this.responseType = "", __privateAdd(this, _method, void 0), __privateAdd(this, _url, void 0), __privateAdd(this, _resHeaders, void 0), __privateAdd(this, _headers, {}), __privateAdd(this, _controller, void 0), __privateAdd(this, _init, {}), __privateAdd(this, _useAbortSignal, void 0);
  14. }
  15. // eslint-disable-next-line @typescript-eslint/no-unused-vars -- _async is only declared for typings compatibility
  16. open(method, url, _async) {
  17. __privateSet(this, _method, method), __privateSet(this, _url, url), __privateSet(this, _resHeaders, ""), this.readyState = 1, this.onreadystatechange(), __privateSet(this, _controller, void 0);
  18. }
  19. abort() {
  20. __privateGet(this, _controller) && __privateGet(this, _controller).abort();
  21. }
  22. getAllResponseHeaders() {
  23. return __privateGet(this, _resHeaders);
  24. }
  25. setRequestHeader(name, value) {
  26. __privateGet(this, _headers)[name] = value;
  27. }
  28. // Allow setting extra fetch init options, needed for runtimes such as Vercel Edge to set `cache` and other options in React Server Components
  29. setInit(init, useAbortSignal = !0) {
  30. __privateSet(this, _init, init), __privateSet(this, _useAbortSignal, useAbortSignal);
  31. }
  32. send(body) {
  33. const textBody = this.responseType !== "arraybuffer", options = {
  34. ...__privateGet(this, _init),
  35. method: __privateGet(this, _method),
  36. headers: __privateGet(this, _headers),
  37. body
  38. };
  39. typeof AbortController == "function" && __privateGet(this, _useAbortSignal) && (__privateSet(this, _controller, new AbortController()), typeof EventTarget < "u" && __privateGet(this, _controller).signal instanceof EventTarget && (options.signal = __privateGet(this, _controller).signal)), typeof document < "u" && (options.credentials = this.withCredentials ? "include" : "omit"), fetch(__privateGet(this, _url), options).then((res) => (res.headers.forEach((value, key) => {
  40. __privateSet(this, _resHeaders, __privateGet(this, _resHeaders) + `${key}: ${value}\r
  41. `);
  42. }), this.status = res.status, this.statusText = res.statusText, this.readyState = 3, textBody ? res.text() : res.arrayBuffer())).then((resBody) => {
  43. typeof resBody == "string" ? this.responseText = resBody : this.response = resBody, this.readyState = 4, this.onreadystatechange();
  44. }).catch((err) => {
  45. var _a;
  46. if (err.name === "AbortError") {
  47. this.onabort();
  48. return;
  49. }
  50. (_a = this.onerror) == null || _a.call(this, err);
  51. });
  52. }
  53. }
  54. _method = /* @__PURE__ */ new WeakMap(), _url = /* @__PURE__ */ new WeakMap(), _resHeaders = /* @__PURE__ */ new WeakMap(), _headers = /* @__PURE__ */ new WeakMap(), _controller = /* @__PURE__ */ new WeakMap(), _init = /* @__PURE__ */ new WeakMap(), _useAbortSignal = /* @__PURE__ */ new WeakMap();
  55. const adapter = typeof XMLHttpRequest == "function" ? "xhr" : "fetch", XmlHttpRequest = adapter === "xhr" ? XMLHttpRequest : FetchXhr, httpRequester = (context, callback) => {
  56. var _a;
  57. const opts = context.options, options = context.applyMiddleware("finalizeOptions", opts), timers = {}, injectedResponse = context.applyMiddleware("interceptRequest", void 0, {
  58. adapter,
  59. context
  60. });
  61. if (injectedResponse) {
  62. const cbTimer = setTimeout(callback, 0, null, injectedResponse);
  63. return { abort: () => clearTimeout(cbTimer) };
  64. }
  65. let xhr = new XmlHttpRequest();
  66. xhr instanceof FetchXhr && typeof options.fetch == "object" && xhr.setInit(options.fetch, (_a = options.useAbortSignal) != null ? _a : !0);
  67. const headers = options.headers, delays = options.timeout;
  68. let aborted = !1, loaded = !1, timedOut = !1;
  69. if (xhr.onerror = (event) => {
  70. onError(
  71. new Error(
  72. `Request error while attempting to reach ${options.url}${event.lengthComputable ? `(${event.loaded} of ${event.total} bytes transferred)` : ""}`
  73. )
  74. );
  75. }, xhr.ontimeout = (event) => {
  76. onError(
  77. new Error(
  78. `Request timeout while attempting to reach ${options.url}${event.lengthComputable ? `(${event.loaded} of ${event.total} bytes transferred)` : ""}`
  79. )
  80. );
  81. }, xhr.onabort = () => {
  82. stopTimers(!0), aborted = !0;
  83. }, xhr.onreadystatechange = () => {
  84. resetTimers(), !(aborted || xhr.readyState !== 4) && xhr.status !== 0 && onLoad();
  85. }, xhr.open(
  86. options.method,
  87. options.url,
  88. !0
  89. // Always async
  90. ), xhr.withCredentials = !!options.withCredentials, headers && xhr.setRequestHeader)
  91. for (const key in headers)
  92. headers.hasOwnProperty(key) && xhr.setRequestHeader(key, headers[key]);
  93. return options.rawBody && (xhr.responseType = "arraybuffer"), context.applyMiddleware("onRequest", { options, adapter, request: xhr, context }), xhr.send(options.body || null), delays && (timers.connect = setTimeout(() => timeoutRequest("ETIMEDOUT"), delays.connect)), { abort };
  94. function abort() {
  95. aborted = !0, xhr && xhr.abort();
  96. }
  97. function timeoutRequest(code) {
  98. timedOut = !0, xhr.abort();
  99. const error = new Error(
  100. code === "ESOCKETTIMEDOUT" ? `Socket timed out on request to ${options.url}` : `Connection timed out on request to ${options.url}`
  101. );
  102. error.code = code, context.channels.error.publish(error);
  103. }
  104. function resetTimers() {
  105. delays && (stopTimers(), timers.socket = setTimeout(() => timeoutRequest("ESOCKETTIMEDOUT"), delays.socket));
  106. }
  107. function stopTimers(force) {
  108. (force || aborted || xhr.readyState >= 2 && timers.connect) && clearTimeout(timers.connect), timers.socket && clearTimeout(timers.socket);
  109. }
  110. function onError(error) {
  111. if (loaded)
  112. return;
  113. stopTimers(!0), loaded = !0, xhr = null;
  114. const err = error || new Error(`Network error while attempting to reach ${options.url}`);
  115. err.isNetworkError = !0, err.request = options, callback(err);
  116. }
  117. function reduceResponse() {
  118. return {
  119. body: xhr.response || (xhr.responseType === "" || xhr.responseType === "text" ? xhr.responseText : ""),
  120. url: options.url,
  121. method: options.method,
  122. headers: parseHeaders(xhr.getAllResponseHeaders()),
  123. statusCode: xhr.status,
  124. statusMessage: xhr.statusText
  125. };
  126. }
  127. function onLoad() {
  128. if (!(aborted || loaded || timedOut)) {
  129. if (xhr.status === 0) {
  130. onError(new Error("Unknown XHR error"));
  131. return;
  132. }
  133. stopTimers(), loaded = !0, callback(null, reduceResponse());
  134. }
  135. }
  136. }, getIt = (initMiddleware = [], httpRequest = httpRequester) => createRequester(initMiddleware, httpRequest), environment = "react-server";
  137. export {
  138. adapter,
  139. environment,
  140. getIt
  141. };
  142. //# sourceMappingURL=index.react-server.js.map