server.js 20 KB

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