index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. // src/index.ts
  30. var src_exports = {};
  31. __export(src_exports, {
  32. RequestError: () => RequestError,
  33. createAdaptorServer: () => createAdaptorServer,
  34. getRequestListener: () => getRequestListener,
  35. serve: () => serve
  36. });
  37. module.exports = __toCommonJS(src_exports);
  38. // src/server.ts
  39. var import_node_http = require("http");
  40. // src/listener.ts
  41. var import_node_http22 = require("http2");
  42. // src/request.ts
  43. var import_node_http2 = require("http2");
  44. var import_node_stream = require("stream");
  45. var RequestError = class extends Error {
  46. constructor(message, options) {
  47. super(message, options);
  48. this.name = "RequestError";
  49. }
  50. };
  51. var toRequestError = (e) => {
  52. if (e instanceof RequestError) {
  53. return e;
  54. }
  55. return new RequestError(e.message, { cause: e });
  56. };
  57. var GlobalRequest = global.Request;
  58. var Request = class extends GlobalRequest {
  59. constructor(input, options) {
  60. if (typeof input === "object" && getRequestCache in input) {
  61. input = input[getRequestCache]();
  62. }
  63. if (typeof options?.body?.getReader !== "undefined") {
  64. ;
  65. options.duplex ??= "half";
  66. }
  67. super(input, options);
  68. }
  69. };
  70. var newHeadersFromIncoming = (incoming) => {
  71. const headerRecord = [];
  72. const rawHeaders = incoming.rawHeaders;
  73. for (let i = 0; i < rawHeaders.length; i += 2) {
  74. const { [i]: key, [i + 1]: value } = rawHeaders;
  75. if (key.charCodeAt(0) !== /*:*/
  76. 58) {
  77. headerRecord.push([key, value]);
  78. }
  79. }
  80. return new Headers(headerRecord);
  81. };
  82. var wrapBodyStream = Symbol("wrapBodyStream");
  83. var newRequestFromIncoming = (method, url, headers, incoming, abortController) => {
  84. const init = {
  85. method,
  86. headers,
  87. signal: abortController.signal
  88. };
  89. if (method === "TRACE") {
  90. init.method = "GET";
  91. const req = new Request(url, init);
  92. Object.defineProperty(req, "method", {
  93. get() {
  94. return "TRACE";
  95. }
  96. });
  97. return req;
  98. }
  99. if (!(method === "GET" || method === "HEAD")) {
  100. if ("rawBody" in incoming && incoming.rawBody instanceof Buffer) {
  101. init.body = new ReadableStream({
  102. start(controller) {
  103. controller.enqueue(incoming.rawBody);
  104. controller.close();
  105. }
  106. });
  107. } else if (incoming[wrapBodyStream]) {
  108. let reader;
  109. init.body = new ReadableStream({
  110. async pull(controller) {
  111. try {
  112. reader ||= import_node_stream.Readable.toWeb(incoming).getReader();
  113. const { done, value } = await reader.read();
  114. if (done) {
  115. controller.close();
  116. } else {
  117. controller.enqueue(value);
  118. }
  119. } catch (error) {
  120. controller.error(error);
  121. }
  122. }
  123. });
  124. } else {
  125. init.body = import_node_stream.Readable.toWeb(incoming);
  126. }
  127. }
  128. return new Request(url, init);
  129. };
  130. var getRequestCache = Symbol("getRequestCache");
  131. var requestCache = Symbol("requestCache");
  132. var incomingKey = Symbol("incomingKey");
  133. var urlKey = Symbol("urlKey");
  134. var headersKey = Symbol("headersKey");
  135. var abortControllerKey = Symbol("abortControllerKey");
  136. var getAbortController = Symbol("getAbortController");
  137. var requestPrototype = {
  138. get method() {
  139. return this[incomingKey].method || "GET";
  140. },
  141. get url() {
  142. return this[urlKey];
  143. },
  144. get headers() {
  145. return this[headersKey] ||= newHeadersFromIncoming(this[incomingKey]);
  146. },
  147. [getAbortController]() {
  148. this[getRequestCache]();
  149. return this[abortControllerKey];
  150. },
  151. [getRequestCache]() {
  152. this[abortControllerKey] ||= new AbortController();
  153. return this[requestCache] ||= newRequestFromIncoming(
  154. this.method,
  155. this[urlKey],
  156. this.headers,
  157. this[incomingKey],
  158. this[abortControllerKey]
  159. );
  160. }
  161. };
  162. [
  163. "body",
  164. "bodyUsed",
  165. "cache",
  166. "credentials",
  167. "destination",
  168. "integrity",
  169. "mode",
  170. "redirect",
  171. "referrer",
  172. "referrerPolicy",
  173. "signal",
  174. "keepalive"
  175. ].forEach((k) => {
  176. Object.defineProperty(requestPrototype, k, {
  177. get() {
  178. return this[getRequestCache]()[k];
  179. }
  180. });
  181. });
  182. ["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
  183. Object.defineProperty(requestPrototype, k, {
  184. value: function() {
  185. return this[getRequestCache]()[k]();
  186. }
  187. });
  188. });
  189. Object.setPrototypeOf(requestPrototype, Request.prototype);
  190. var newRequest = (incoming, defaultHostname) => {
  191. const req = Object.create(requestPrototype);
  192. req[incomingKey] = incoming;
  193. const incomingUrl = incoming.url || "";
  194. if (incomingUrl[0] !== "/" && // short-circuit for performance. most requests are relative URL.
  195. (incomingUrl.startsWith("http://") || incomingUrl.startsWith("https://"))) {
  196. if (incoming instanceof import_node_http2.Http2ServerRequest) {
  197. throw new RequestError("Absolute URL for :path is not allowed in HTTP/2");
  198. }
  199. try {
  200. const url2 = new URL(incomingUrl);
  201. req[urlKey] = url2.href;
  202. } catch (e) {
  203. throw new RequestError("Invalid absolute URL", { cause: e });
  204. }
  205. return req;
  206. }
  207. const host = (incoming instanceof import_node_http2.Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
  208. if (!host) {
  209. throw new RequestError("Missing host header");
  210. }
  211. let scheme;
  212. if (incoming instanceof import_node_http2.Http2ServerRequest) {
  213. scheme = incoming.scheme;
  214. if (!(scheme === "http" || scheme === "https")) {
  215. throw new RequestError("Unsupported scheme");
  216. }
  217. } else {
  218. scheme = incoming.socket && incoming.socket.encrypted ? "https" : "http";
  219. }
  220. const url = new URL(`${scheme}://${host}${incomingUrl}`);
  221. if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) {
  222. throw new RequestError("Invalid host header");
  223. }
  224. req[urlKey] = url.href;
  225. return req;
  226. };
  227. // src/response.ts
  228. var responseCache = Symbol("responseCache");
  229. var getResponseCache = Symbol("getResponseCache");
  230. var cacheKey = Symbol("cache");
  231. var GlobalResponse = global.Response;
  232. var Response2 = class _Response {
  233. #body;
  234. #init;
  235. [getResponseCache]() {
  236. delete this[cacheKey];
  237. return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
  238. }
  239. constructor(body, init) {
  240. let headers;
  241. this.#body = body;
  242. if (init instanceof _Response) {
  243. const cachedGlobalResponse = init[responseCache];
  244. if (cachedGlobalResponse) {
  245. this.#init = cachedGlobalResponse;
  246. this[getResponseCache]();
  247. return;
  248. } else {
  249. this.#init = init.#init;
  250. headers = new Headers(init.#init.headers);
  251. }
  252. } else {
  253. this.#init = init;
  254. }
  255. if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
  256. ;
  257. this[cacheKey] = [init?.status || 200, body, headers || init?.headers];
  258. }
  259. }
  260. get headers() {
  261. const cache = this[cacheKey];
  262. if (cache) {
  263. if (!(cache[2] instanceof Headers)) {
  264. cache[2] = new Headers(
  265. cache[2] || { "content-type": "text/plain; charset=UTF-8" }
  266. );
  267. }
  268. return cache[2];
  269. }
  270. return this[getResponseCache]().headers;
  271. }
  272. get status() {
  273. return this[cacheKey]?.[0] ?? this[getResponseCache]().status;
  274. }
  275. get ok() {
  276. const status = this.status;
  277. return status >= 200 && status < 300;
  278. }
  279. };
  280. ["body", "bodyUsed", "redirected", "statusText", "trailers", "type", "url"].forEach((k) => {
  281. Object.defineProperty(Response2.prototype, k, {
  282. get() {
  283. return this[getResponseCache]()[k];
  284. }
  285. });
  286. });
  287. ["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
  288. Object.defineProperty(Response2.prototype, k, {
  289. value: function() {
  290. return this[getResponseCache]()[k]();
  291. }
  292. });
  293. });
  294. Object.setPrototypeOf(Response2, GlobalResponse);
  295. Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
  296. // src/utils.ts
  297. async function readWithoutBlocking(readPromise) {
  298. return Promise.race([readPromise, Promise.resolve().then(() => Promise.resolve(void 0))]);
  299. }
  300. function writeFromReadableStreamDefaultReader(reader, writable, currentReadPromise) {
  301. const cancel = (error) => {
  302. reader.cancel(error).catch(() => {
  303. });
  304. };
  305. writable.on("close", cancel);
  306. writable.on("error", cancel);
  307. (currentReadPromise ?? reader.read()).then(flow, handleStreamError);
  308. return reader.closed.finally(() => {
  309. writable.off("close", cancel);
  310. writable.off("error", cancel);
  311. });
  312. function handleStreamError(error) {
  313. if (error) {
  314. writable.destroy(error);
  315. }
  316. }
  317. function onDrain() {
  318. reader.read().then(flow, handleStreamError);
  319. }
  320. function flow({ done, value }) {
  321. try {
  322. if (done) {
  323. writable.end();
  324. } else if (!writable.write(value)) {
  325. writable.once("drain", onDrain);
  326. } else {
  327. return reader.read().then(flow, handleStreamError);
  328. }
  329. } catch (e) {
  330. handleStreamError(e);
  331. }
  332. }
  333. }
  334. function writeFromReadableStream(stream, writable) {
  335. if (stream.locked) {
  336. throw new TypeError("ReadableStream is locked.");
  337. } else if (writable.destroyed) {
  338. return;
  339. }
  340. return writeFromReadableStreamDefaultReader(stream.getReader(), writable);
  341. }
  342. var buildOutgoingHttpHeaders = (headers) => {
  343. const res = {};
  344. if (!(headers instanceof Headers)) {
  345. headers = new Headers(headers ?? void 0);
  346. }
  347. const cookies = [];
  348. for (const [k, v] of headers) {
  349. if (k === "set-cookie") {
  350. cookies.push(v);
  351. } else {
  352. res[k] = v;
  353. }
  354. }
  355. if (cookies.length > 0) {
  356. res["set-cookie"] = cookies;
  357. }
  358. res["content-type"] ??= "text/plain; charset=UTF-8";
  359. return res;
  360. };
  361. // src/utils/response/constants.ts
  362. var X_ALREADY_SENT = "x-hono-already-sent";
  363. // src/globals.ts
  364. var import_node_crypto = __toESM(require("crypto"));
  365. if (typeof global.crypto === "undefined") {
  366. global.crypto = import_node_crypto.default;
  367. }
  368. // src/listener.ts
  369. var outgoingEnded = Symbol("outgoingEnded");
  370. var incomingDraining = Symbol("incomingDraining");
  371. var DRAIN_TIMEOUT_MS = 500;
  372. var MAX_DRAIN_BYTES = 64 * 1024 * 1024;
  373. var drainIncoming = (incoming) => {
  374. const incomingWithDrainState = incoming;
  375. if (incoming.destroyed || incomingWithDrainState[incomingDraining]) {
  376. return;
  377. }
  378. incomingWithDrainState[incomingDraining] = true;
  379. if (incoming instanceof import_node_http22.Http2ServerRequest) {
  380. try {
  381. ;
  382. incoming.stream?.close?.(import_node_http22.constants.NGHTTP2_NO_ERROR);
  383. } catch {
  384. }
  385. return;
  386. }
  387. let bytesRead = 0;
  388. const cleanup = () => {
  389. clearTimeout(timer);
  390. incoming.off("data", onData);
  391. incoming.off("end", cleanup);
  392. incoming.off("error", cleanup);
  393. };
  394. const forceClose = () => {
  395. cleanup();
  396. const socket = incoming.socket;
  397. if (socket && !socket.destroyed) {
  398. socket.destroySoon();
  399. }
  400. };
  401. const timer = setTimeout(forceClose, DRAIN_TIMEOUT_MS);
  402. timer.unref?.();
  403. const onData = (chunk) => {
  404. bytesRead += chunk.length;
  405. if (bytesRead > MAX_DRAIN_BYTES) {
  406. forceClose();
  407. }
  408. };
  409. incoming.on("data", onData);
  410. incoming.on("end", cleanup);
  411. incoming.on("error", cleanup);
  412. incoming.resume();
  413. };
  414. var handleRequestError = () => new Response(null, {
  415. status: 400
  416. });
  417. var handleFetchError = (e) => new Response(null, {
  418. status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
  419. });
  420. var handleResponseError = (e, outgoing) => {
  421. const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
  422. if (err.code === "ERR_STREAM_PREMATURE_CLOSE") {
  423. console.info("The user aborted a request.");
  424. } else {
  425. console.error(e);
  426. if (!outgoing.headersSent) {
  427. outgoing.writeHead(500, { "Content-Type": "text/plain" });
  428. }
  429. outgoing.end(`Error: ${err.message}`);
  430. outgoing.destroy(err);
  431. }
  432. };
  433. var flushHeaders = (outgoing) => {
  434. if ("flushHeaders" in outgoing && outgoing.writable) {
  435. outgoing.flushHeaders();
  436. }
  437. };
  438. var responseViaCache = async (res, outgoing) => {
  439. let [status, body, header] = res[cacheKey];
  440. let hasContentLength = false;
  441. if (!header) {
  442. header = { "content-type": "text/plain; charset=UTF-8" };
  443. } else if (header instanceof Headers) {
  444. hasContentLength = header.has("content-length");
  445. header = buildOutgoingHttpHeaders(header);
  446. } else if (Array.isArray(header)) {
  447. const headerObj = new Headers(header);
  448. hasContentLength = headerObj.has("content-length");
  449. header = buildOutgoingHttpHeaders(headerObj);
  450. } else {
  451. for (const key in header) {
  452. if (key.length === 14 && key.toLowerCase() === "content-length") {
  453. hasContentLength = true;
  454. break;
  455. }
  456. }
  457. }
  458. if (!hasContentLength) {
  459. if (typeof body === "string") {
  460. header["Content-Length"] = Buffer.byteLength(body);
  461. } else if (body instanceof Uint8Array) {
  462. header["Content-Length"] = body.byteLength;
  463. } else if (body instanceof Blob) {
  464. header["Content-Length"] = body.size;
  465. }
  466. }
  467. outgoing.writeHead(status, header);
  468. if (typeof body === "string" || body instanceof Uint8Array) {
  469. outgoing.end(body);
  470. } else if (body instanceof Blob) {
  471. outgoing.end(new Uint8Array(await body.arrayBuffer()));
  472. } else {
  473. flushHeaders(outgoing);
  474. await writeFromReadableStream(body, outgoing)?.catch(
  475. (e) => handleResponseError(e, outgoing)
  476. );
  477. }
  478. ;
  479. outgoing[outgoingEnded]?.();
  480. };
  481. var isPromise = (res) => typeof res.then === "function";
  482. var responseViaResponseObject = async (res, outgoing, options = {}) => {
  483. if (isPromise(res)) {
  484. if (options.errorHandler) {
  485. try {
  486. res = await res;
  487. } catch (err) {
  488. const errRes = await options.errorHandler(err);
  489. if (!errRes) {
  490. return;
  491. }
  492. res = errRes;
  493. }
  494. } else {
  495. res = await res.catch(handleFetchError);
  496. }
  497. }
  498. if (cacheKey in res) {
  499. return responseViaCache(res, outgoing);
  500. }
  501. const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
  502. if (res.body) {
  503. const reader = res.body.getReader();
  504. const values = [];
  505. let done = false;
  506. let currentReadPromise = void 0;
  507. if (resHeaderRecord["transfer-encoding"] !== "chunked") {
  508. let maxReadCount = 2;
  509. for (let i = 0; i < maxReadCount; i++) {
  510. currentReadPromise ||= reader.read();
  511. const chunk = await readWithoutBlocking(currentReadPromise).catch((e) => {
  512. console.error(e);
  513. done = true;
  514. });
  515. if (!chunk) {
  516. if (i === 1) {
  517. await new Promise((resolve) => setTimeout(resolve));
  518. maxReadCount = 3;
  519. continue;
  520. }
  521. break;
  522. }
  523. currentReadPromise = void 0;
  524. if (chunk.value) {
  525. values.push(chunk.value);
  526. }
  527. if (chunk.done) {
  528. done = true;
  529. break;
  530. }
  531. }
  532. if (done && !("content-length" in resHeaderRecord)) {
  533. resHeaderRecord["content-length"] = values.reduce((acc, value) => acc + value.length, 0);
  534. }
  535. }
  536. outgoing.writeHead(res.status, resHeaderRecord);
  537. values.forEach((value) => {
  538. ;
  539. outgoing.write(value);
  540. });
  541. if (done) {
  542. outgoing.end();
  543. } else {
  544. if (values.length === 0) {
  545. flushHeaders(outgoing);
  546. }
  547. await writeFromReadableStreamDefaultReader(reader, outgoing, currentReadPromise);
  548. }
  549. } else if (resHeaderRecord[X_ALREADY_SENT]) {
  550. } else {
  551. outgoing.writeHead(res.status, resHeaderRecord);
  552. outgoing.end();
  553. }
  554. ;
  555. outgoing[outgoingEnded]?.();
  556. };
  557. var getRequestListener = (fetchCallback, options = {}) => {
  558. const autoCleanupIncoming = options.autoCleanupIncoming ?? true;
  559. if (options.overrideGlobalObjects !== false && global.Request !== Request) {
  560. Object.defineProperty(global, "Request", {
  561. value: Request
  562. });
  563. Object.defineProperty(global, "Response", {
  564. value: Response2
  565. });
  566. }
  567. return async (incoming, outgoing) => {
  568. let res, req;
  569. try {
  570. req = newRequest(incoming, options.hostname);
  571. let incomingEnded = !autoCleanupIncoming || incoming.method === "GET" || incoming.method === "HEAD";
  572. if (!incomingEnded) {
  573. ;
  574. incoming[wrapBodyStream] = true;
  575. incoming.on("end", () => {
  576. incomingEnded = true;
  577. });
  578. if (incoming instanceof import_node_http22.Http2ServerRequest) {
  579. ;
  580. outgoing[outgoingEnded] = () => {
  581. if (!incomingEnded) {
  582. setTimeout(() => {
  583. if (!incomingEnded) {
  584. setTimeout(() => {
  585. drainIncoming(incoming);
  586. });
  587. }
  588. });
  589. }
  590. };
  591. }
  592. outgoing.on("finish", () => {
  593. if (!incomingEnded) {
  594. drainIncoming(incoming);
  595. }
  596. });
  597. }
  598. outgoing.on("close", () => {
  599. const abortController = req[abortControllerKey];
  600. if (abortController) {
  601. if (incoming.errored) {
  602. req[abortControllerKey].abort(incoming.errored.toString());
  603. } else if (!outgoing.writableFinished) {
  604. req[abortControllerKey].abort("Client connection prematurely closed.");
  605. }
  606. }
  607. if (!incomingEnded) {
  608. setTimeout(() => {
  609. if (!incomingEnded) {
  610. setTimeout(() => {
  611. drainIncoming(incoming);
  612. });
  613. }
  614. });
  615. }
  616. });
  617. res = fetchCallback(req, { incoming, outgoing });
  618. if (cacheKey in res) {
  619. return responseViaCache(res, outgoing);
  620. }
  621. } catch (e) {
  622. if (!res) {
  623. if (options.errorHandler) {
  624. res = await options.errorHandler(req ? e : toRequestError(e));
  625. if (!res) {
  626. return;
  627. }
  628. } else if (!req) {
  629. res = handleRequestError();
  630. } else {
  631. res = handleFetchError(e);
  632. }
  633. } else {
  634. return handleResponseError(e, outgoing);
  635. }
  636. }
  637. try {
  638. return await responseViaResponseObject(res, outgoing, options);
  639. } catch (e) {
  640. return handleResponseError(e, outgoing);
  641. }
  642. };
  643. };
  644. // src/server.ts
  645. var createAdaptorServer = (options) => {
  646. const fetchCallback = options.fetch;
  647. const requestListener = getRequestListener(fetchCallback, {
  648. hostname: options.hostname,
  649. overrideGlobalObjects: options.overrideGlobalObjects,
  650. autoCleanupIncoming: options.autoCleanupIncoming
  651. });
  652. const createServer = options.createServer || import_node_http.createServer;
  653. const server = createServer(options.serverOptions || {}, requestListener);
  654. return server;
  655. };
  656. var serve = (options, listeningListener) => {
  657. const server = createAdaptorServer(options);
  658. server.listen(options?.port ?? 3e3, options.hostname, () => {
  659. const serverInfo = server.address();
  660. listeningListener && listeningListener(serverInfo);
  661. });
  662. return server;
  663. };
  664. // Annotate the CommonJS export names for ESM import in node:
  665. 0 && (module.exports = {
  666. RequestError,
  667. createAdaptorServer,
  668. getRequestListener,
  669. serve
  670. });