hermium 0.1.9 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (209) hide show
  1. package/README.md +56 -0
  2. package/bin/hermium.mjs +185 -164
  3. package/dist/api.mjs +3513 -0
  4. package/dist/public/assets/css/index-Dfs9RUU9.css +1 -0
  5. package/dist/public/assets/css/styles-B8p6jk5Z.css +1 -0
  6. package/dist/public/assets/js/ChatInputBlock-Bw7AL70H.js +1 -0
  7. package/dist/public/assets/js/MarkdownMessage-8d7Y6VL-.js +1 -0
  8. package/dist/public/assets/js/base-ui-BvQbAt_1.js +1 -0
  9. package/dist/public/assets/js/chat._sessionId-BG6lVraH.js +1 -0
  10. package/dist/public/assets/js/chat.index-D2zdMPTT.js +1 -0
  11. package/dist/public/assets/js/index-C0AK45FU.js +60 -0
  12. package/dist/public/assets/js/index-Cx5En4FK.js +1 -0
  13. package/dist/public/assets/js/memory-CeSRdTkW.js +3 -0
  14. package/dist/public/assets/js/router-8uDKazL-.js +1 -0
  15. package/dist/public/assets/js/settings-Bc3Y5zXO.js +1 -0
  16. package/dist/public/assets/js/skills-DZv7sA_5.js +1 -0
  17. package/dist/public/assets/js/theme-CPkdkpaj.js +1 -0
  18. package/dist/public/assets/js/usage-DXQsT9_b.js +1 -0
  19. package/dist/public/assets/woff2/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
  20. package/dist/public/assets/woff2/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
  21. package/dist/public/assets/woff2/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
  22. package/dist/public/assets/woff2/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
  23. package/dist/public/assets/woff2/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
  24. package/dist/public/favicon.ico +0 -0
  25. package/dist/public/logo.png +0 -0
  26. package/package.json +1 -1
  27. package/dist/public/assets/IconAlertCircle-BHkmI3j7.js +0 -1
  28. package/dist/public/assets/IconAlertTriangle-wCJudlVg.js +0 -1
  29. package/dist/public/assets/IconCheck-CFuEh_p7.js +0 -1
  30. package/dist/public/assets/IconLoader2-BIx3OuF9.js +0 -1
  31. package/dist/public/assets/IconRefresh-Dgm93w3T.js +0 -1
  32. package/dist/public/assets/geist-cyrillic-wght-normal-CHSlOQsW.woff2 +0 -0
  33. package/dist/public/assets/geist-latin-ext-wght-normal-DMtmJ5ZE.woff2 +0 -0
  34. package/dist/public/assets/geist-latin-wght-normal-Dm3htQBi.woff2 +0 -0
  35. package/dist/public/assets/index-Bbz3abmO.js +0 -14
  36. package/dist/public/assets/index-CWUaRwcE.js +0 -1
  37. package/dist/public/assets/index-CinLq3cd.js +0 -1
  38. package/dist/public/assets/index-CrQs9n6q.js +0 -29
  39. package/dist/public/assets/index-CtacpN3I.js +0 -1
  40. package/dist/public/assets/index-DY7aE-9s.js +0 -2
  41. package/dist/public/assets/index-DkYGodJj.js +0 -94
  42. package/dist/public/assets/index-DvDLadUx.js +0 -1
  43. package/dist/public/assets/index-U6RcWedt.js +0 -1
  44. package/dist/public/assets/index-_6iFZ0fh.js +0 -1
  45. package/dist/public/assets/index-enFS26SU.js +0 -1
  46. package/dist/public/assets/input-eNcwlDHp.js +0 -1
  47. package/dist/public/assets/inter-cyrillic-ext-wght-normal-BOeWTOD4.woff2 +0 -0
  48. package/dist/public/assets/inter-cyrillic-wght-normal-DqGufNeO.woff2 +0 -0
  49. package/dist/public/assets/inter-greek-ext-wght-normal-DlzME5K_.woff2 +0 -0
  50. package/dist/public/assets/inter-greek-wght-normal-CkhJZR-_.woff2 +0 -0
  51. package/dist/public/assets/inter-latin-ext-wght-normal-DO1Apj_S.woff2 +0 -0
  52. package/dist/public/assets/inter-latin-wght-normal-Dx4kXJAl.woff2 +0 -0
  53. package/dist/public/assets/inter-vietnamese-wght-normal-CBcvBZtf.woff2 +0 -0
  54. package/dist/public/assets/queries-iHRgZzw2.js +0 -1
  55. package/dist/public/assets/styles-KcflDlA_.css +0 -1
  56. package/dist/public/assets/switch-B1DcZLwL.js +0 -1
  57. package/dist/public/assets/syntax-highlighter-DWPF-A_h.js +0 -6
  58. package/dist/public/assets/textarea-Di_syYTS.js +0 -1
  59. package/dist/public/favicon.png +0 -0
  60. package/dist/public/nous-logo.png +0 -0
  61. package/dist/server/index.mjs +0 -244
  62. package/dist/web-server/__23tanstack-start-plugin-adapters-Cwee5PKy.mjs +0 -6
  63. package/dist/web-server/_chunks/ssr-renderer.mjs +0 -22
  64. package/dist/web-server/_libs/babel__runtime.mjs +0 -237
  65. package/dist/web-server/_libs/bail.mjs +0 -8
  66. package/dist/web-server/_libs/base-ui__react.mjs +0 -9554
  67. package/dist/web-server/_libs/base-ui__utils.mjs +0 -1101
  68. package/dist/web-server/_libs/ccount.mjs +0 -16
  69. package/dist/web-server/_libs/character-entities-legacy.mjs +0 -111
  70. package/dist/web-server/_libs/character-entities.mjs +0 -2130
  71. package/dist/web-server/_libs/character-reference-invalid.mjs +0 -33
  72. package/dist/web-server/_libs/class-variance-authority.mjs +0 -44
  73. package/dist/web-server/_libs/clsx.mjs +0 -16
  74. package/dist/web-server/_libs/comma-separated-tokens.mjs +0 -31
  75. package/dist/web-server/_libs/cookie-es.mjs +0 -44
  76. package/dist/web-server/_libs/croner.mjs +0 -1
  77. package/dist/web-server/_libs/crossws.mjs +0 -1
  78. package/dist/web-server/_libs/decode-named-character-reference+[...].mjs +0 -8
  79. package/dist/web-server/_libs/devlop.mjs +0 -8
  80. package/dist/web-server/_libs/escape-string-regexp.mjs +0 -9
  81. package/dist/web-server/_libs/estree-util-is-identifier-name.mjs +0 -11
  82. package/dist/web-server/_libs/extend.mjs +0 -97
  83. package/dist/web-server/_libs/fault.mjs +0 -1
  84. package/dist/web-server/_libs/floating-ui__core.mjs +0 -663
  85. package/dist/web-server/_libs/floating-ui__dom.mjs +0 -624
  86. package/dist/web-server/_libs/floating-ui__react-dom.mjs +0 -279
  87. package/dist/web-server/_libs/floating-ui__utils.mjs +0 -322
  88. package/dist/web-server/_libs/format.mjs +0 -1
  89. package/dist/web-server/_libs/h3.mjs +0 -408
  90. package/dist/web-server/_libs/hast-util-parse-selector.mjs +0 -39
  91. package/dist/web-server/_libs/hast-util-to-jsx-runtime.mjs +0 -388
  92. package/dist/web-server/_libs/hast-util-whitespace.mjs +0 -10
  93. package/dist/web-server/_libs/hastscript.mjs +0 -200
  94. package/dist/web-server/_libs/highlight.js.mjs +0 -1
  95. package/dist/web-server/_libs/hookable.mjs +0 -1
  96. package/dist/web-server/_libs/html-url-attributes.mjs +0 -26
  97. package/dist/web-server/_libs/inline-style-parser.mjs +0 -142
  98. package/dist/web-server/_libs/is-alphabetical.mjs +0 -7
  99. package/dist/web-server/_libs/is-alphanumerical.mjs +0 -8
  100. package/dist/web-server/_libs/is-decimal.mjs +0 -7
  101. package/dist/web-server/_libs/is-hexadecimal.mjs +0 -7
  102. package/dist/web-server/_libs/is-plain-obj.mjs +0 -10
  103. package/dist/web-server/_libs/isbot.mjs +0 -21
  104. package/dist/web-server/_libs/longest-streak.mjs +0 -25
  105. package/dist/web-server/_libs/lowlight.mjs +0 -1
  106. package/dist/web-server/_libs/markdown-table.mjs +0 -142
  107. package/dist/web-server/_libs/mdast-util-find-and-replace.mjs +0 -109
  108. package/dist/web-server/_libs/mdast-util-from-markdown.mjs +0 -717
  109. package/dist/web-server/_libs/mdast-util-gfm-autolink-literal+[...].mjs +0 -156
  110. package/dist/web-server/_libs/mdast-util-gfm-footnote.mjs +0 -117
  111. package/dist/web-server/_libs/mdast-util-gfm-strikethrough.mjs +0 -54
  112. package/dist/web-server/_libs/mdast-util-gfm-table.mjs +0 -157
  113. package/dist/web-server/_libs/mdast-util-gfm-task-list-item.mjs +0 -77
  114. package/dist/web-server/_libs/mdast-util-gfm.mjs +0 -29
  115. package/dist/web-server/_libs/mdast-util-phrasing.mjs +0 -30
  116. package/dist/web-server/_libs/mdast-util-to-hast.mjs +0 -710
  117. package/dist/web-server/_libs/mdast-util-to-markdown.mjs +0 -798
  118. package/dist/web-server/_libs/mdast-util-to-string.mjs +0 -38
  119. package/dist/web-server/_libs/micromark-core-commonmark.mjs +0 -2259
  120. package/dist/web-server/_libs/micromark-extension-gfm-autolink-literal+[...].mjs +0 -344
  121. package/dist/web-server/_libs/micromark-extension-gfm-footnote+[...].mjs +0 -279
  122. package/dist/web-server/_libs/micromark-extension-gfm-strikethrough+[...].mjs +0 -98
  123. package/dist/web-server/_libs/micromark-extension-gfm-table.mjs +0 -491
  124. package/dist/web-server/_libs/micromark-extension-gfm-tagfilter+[...].mjs +0 -1
  125. package/dist/web-server/_libs/micromark-extension-gfm-task-list-item+[...].mjs +0 -77
  126. package/dist/web-server/_libs/micromark-extension-gfm.mjs +0 -18
  127. package/dist/web-server/_libs/micromark-factory-destination.mjs +0 -94
  128. package/dist/web-server/_libs/micromark-factory-label.mjs +0 -63
  129. package/dist/web-server/_libs/micromark-factory-space.mjs +0 -24
  130. package/dist/web-server/_libs/micromark-factory-title.mjs +0 -65
  131. package/dist/web-server/_libs/micromark-factory-whitespace.mjs +0 -22
  132. package/dist/web-server/_libs/micromark-util-character.mjs +0 -44
  133. package/dist/web-server/_libs/micromark-util-chunked.mjs +0 -36
  134. package/dist/web-server/_libs/micromark-util-classify-character+[...].mjs +0 -12
  135. package/dist/web-server/_libs/micromark-util-combine-extensions+[...].mjs +0 -41
  136. package/dist/web-server/_libs/micromark-util-decode-numeric-character-reference+[...].mjs +0 -19
  137. package/dist/web-server/_libs/micromark-util-decode-string.mjs +0 -21
  138. package/dist/web-server/_libs/micromark-util-encode.mjs +0 -1
  139. package/dist/web-server/_libs/micromark-util-html-tag-name.mjs +0 -69
  140. package/dist/web-server/_libs/micromark-util-normalize-identifier+[...].mjs +0 -6
  141. package/dist/web-server/_libs/micromark-util-resolve-all.mjs +0 -15
  142. package/dist/web-server/_libs/micromark-util-sanitize-uri.mjs +0 -41
  143. package/dist/web-server/_libs/micromark-util-subtokenize.mjs +0 -346
  144. package/dist/web-server/_libs/micromark.mjs +0 -906
  145. package/dist/web-server/_libs/ocache.mjs +0 -1
  146. package/dist/web-server/_libs/ohash.mjs +0 -1
  147. package/dist/web-server/_libs/parse-entities.mjs +0 -245
  148. package/dist/web-server/_libs/property-information.mjs +0 -1210
  149. package/dist/web-server/_libs/react-dom.mjs +0 -10779
  150. package/dist/web-server/_libs/react-markdown.mjs +0 -147
  151. package/dist/web-server/_libs/react-syntax-highlighter.mjs +0 -941
  152. package/dist/web-server/_libs/react.mjs +0 -513
  153. package/dist/web-server/_libs/refractor.mjs +0 -2425
  154. package/dist/web-server/_libs/remark-gfm.mjs +0 -20
  155. package/dist/web-server/_libs/remark-parse.mjs +0 -19
  156. package/dist/web-server/_libs/remark-rehype.mjs +0 -21
  157. package/dist/web-server/_libs/reselect.mjs +0 -1
  158. package/dist/web-server/_libs/rou3.mjs +0 -8
  159. package/dist/web-server/_libs/seroval-plugins.mjs +0 -58
  160. package/dist/web-server/_libs/seroval.mjs +0 -1775
  161. package/dist/web-server/_libs/space-separated-tokens.mjs +0 -11
  162. package/dist/web-server/_libs/srvx.mjs +0 -781
  163. package/dist/web-server/_libs/style-to-js.mjs +0 -72
  164. package/dist/web-server/_libs/style-to-object.mjs +0 -38
  165. package/dist/web-server/_libs/tabler__icons-react.mjs +0 -230
  166. package/dist/web-server/_libs/tanstack__history.mjs +0 -204
  167. package/dist/web-server/_libs/tanstack__query-core.mjs +0 -2552
  168. package/dist/web-server/_libs/tanstack__react-query.mjs +0 -190
  169. package/dist/web-server/_libs/tanstack__react-router.mjs +0 -1120
  170. package/dist/web-server/_libs/tanstack__react-store.mjs +0 -2
  171. package/dist/web-server/_libs/tanstack__router-core.mjs +0 -4288
  172. package/dist/web-server/_libs/tanstack__store.mjs +0 -1
  173. package/dist/web-server/_libs/trim-lines.mjs +0 -41
  174. package/dist/web-server/_libs/trough.mjs +0 -85
  175. package/dist/web-server/_libs/ufo.mjs +0 -54
  176. package/dist/web-server/_libs/unctx.mjs +0 -1
  177. package/dist/web-server/_libs/ungap__structured-clone.mjs +0 -224
  178. package/dist/web-server/_libs/unified.mjs +0 -661
  179. package/dist/web-server/_libs/unist-util-is.mjs +0 -100
  180. package/dist/web-server/_libs/unist-util-position.mjs +0 -27
  181. package/dist/web-server/_libs/unist-util-stringify-position.mjs +0 -27
  182. package/dist/web-server/_libs/unist-util-visit-parents.mjs +0 -83
  183. package/dist/web-server/_libs/unist-util-visit.mjs +0 -24
  184. package/dist/web-server/_libs/unstorage.mjs +0 -1
  185. package/dist/web-server/_libs/use-sync-external-store.mjs +0 -139
  186. package/dist/web-server/_libs/vfile-message.mjs +0 -138
  187. package/dist/web-server/_libs/vfile.mjs +0 -467
  188. package/dist/web-server/_libs/zod.mjs +0 -3915
  189. package/dist/web-server/_libs/zustand.mjs +0 -343
  190. package/dist/web-server/_libs/zwitch.mjs +0 -1
  191. package/dist/web-server/_ssr/index-0n2Z3BPQ.mjs +0 -369
  192. package/dist/web-server/_ssr/index-6itDALOw.mjs +0 -339
  193. package/dist/web-server/_ssr/index-BIRTrOmp.mjs +0 -449
  194. package/dist/web-server/_ssr/index-BPzfADac.mjs +0 -66
  195. package/dist/web-server/_ssr/index-BQE3bF14.mjs +0 -1870
  196. package/dist/web-server/_ssr/index-C5HpvlUP.mjs +0 -190
  197. package/dist/web-server/_ssr/index-C_ZxnypN.mjs +0 -213
  198. package/dist/web-server/_ssr/index-Ca8JFH8f.mjs +0 -612
  199. package/dist/web-server/_ssr/index-DNVESZiA.mjs +0 -513
  200. package/dist/web-server/_ssr/index.mjs +0 -1558
  201. package/dist/web-server/_ssr/input-CqXjTRQg.mjs +0 -20
  202. package/dist/web-server/_ssr/queries-3H_19mUt.mjs +0 -16
  203. package/dist/web-server/_ssr/router-sbsNus0Y.mjs +0 -2093
  204. package/dist/web-server/_ssr/start-HYkvq4Ni.mjs +0 -4
  205. package/dist/web-server/_ssr/switch-usf2F1UM.mjs +0 -33
  206. package/dist/web-server/_ssr/syntax-highlighter-5vezNTce.mjs +0 -62
  207. package/dist/web-server/_ssr/textarea-DfRheWY0.mjs +0 -18
  208. package/dist/web-server/_tanstack-start-manifest_v-DqW-pKEH.mjs +0 -4
  209. package/dist/web-server/index.mjs +0 -597
@@ -1,781 +0,0 @@
1
- import nodeHTTP from "node:http";
2
- import { Readable, PassThrough } from "node:stream";
3
- import { pipeline } from "node:stream/promises";
4
- import nodeHTTPS from "node:https";
5
- import nodeHTTP2 from "node:http2";
6
- function lazyInherit(target, source, sourceKey) {
7
- for (const key of [...Object.getOwnPropertyNames(source), ...Object.getOwnPropertySymbols(source)]) {
8
- if (key === "constructor") continue;
9
- const targetDesc = Object.getOwnPropertyDescriptor(target, key);
10
- const desc = Object.getOwnPropertyDescriptor(source, key);
11
- let modified = false;
12
- if (desc.get) {
13
- modified = true;
14
- desc.get = targetDesc?.get || function() {
15
- return this[sourceKey][key];
16
- };
17
- }
18
- if (desc.set) {
19
- modified = true;
20
- desc.set = targetDesc?.set || function(value) {
21
- this[sourceKey][key] = value;
22
- };
23
- }
24
- if (!targetDesc?.value && typeof desc.value === "function") {
25
- modified = true;
26
- desc.value = function(...args) {
27
- return this[sourceKey][key](...args);
28
- };
29
- }
30
- if (modified) Object.defineProperty(target, key, desc);
31
- }
32
- }
33
- const _needsNormRE = /(?:(?:^|\/)(?:\.|\.\.|%2e|%2e\.|\.%2e|%2e%2e)(?:\/|$))|[\\^\x80-\uffff]/i;
34
- const FastURL = /* @__PURE__ */ (() => {
35
- const NativeURL = globalThis.URL;
36
- const FastURL2 = class URL {
37
- #url;
38
- #href;
39
- #protocol;
40
- #host;
41
- #pathname;
42
- #search;
43
- #searchParams;
44
- #pos;
45
- constructor(url) {
46
- if (typeof url === "string") if (url[0] === "/") this.#href = url;
47
- else this.#url = new NativeURL(url);
48
- else if (_needsNormRE.test(url.pathname)) this.#url = new NativeURL(`${url.protocol || "http:"}//${url.host || "localhost"}${url.pathname}${url.search || ""}`);
49
- else {
50
- this.#protocol = url.protocol;
51
- this.#host = url.host;
52
- this.#pathname = url.pathname;
53
- this.#search = url.search;
54
- }
55
- }
56
- static [Symbol.hasInstance](val) {
57
- return val instanceof NativeURL;
58
- }
59
- get _url() {
60
- if (this.#url) return this.#url;
61
- this.#url = new NativeURL(this.href);
62
- this.#href = void 0;
63
- this.#protocol = void 0;
64
- this.#host = void 0;
65
- this.#pathname = void 0;
66
- this.#search = void 0;
67
- this.#searchParams = void 0;
68
- this.#pos = void 0;
69
- return this.#url;
70
- }
71
- get href() {
72
- if (this.#url) return this.#url.href;
73
- if (!this.#href) this.#href = `${this.#protocol || "http:"}//${this.#host || "localhost"}${this.#pathname || "/"}${this.#search || ""}`;
74
- return this.#href;
75
- }
76
- #getPos() {
77
- if (!this.#pos) {
78
- const url = this.href;
79
- const protoIndex = url.indexOf("://");
80
- const pathnameIndex = protoIndex === -1 ? -1 : url.indexOf("/", protoIndex + 4);
81
- this.#pos = [
82
- protoIndex,
83
- pathnameIndex,
84
- pathnameIndex === -1 ? -1 : url.indexOf("?", pathnameIndex)
85
- ];
86
- }
87
- return this.#pos;
88
- }
89
- get pathname() {
90
- if (this.#url) return this.#url.pathname;
91
- if (this.#pathname === void 0) {
92
- const [, pathnameIndex, queryIndex] = this.#getPos();
93
- if (pathnameIndex === -1) return this._url.pathname;
94
- this.#pathname = this.href.slice(pathnameIndex, queryIndex === -1 ? void 0 : queryIndex);
95
- }
96
- return this.#pathname;
97
- }
98
- get search() {
99
- if (this.#url) return this.#url.search;
100
- if (this.#search === void 0) {
101
- const [, pathnameIndex, queryIndex] = this.#getPos();
102
- if (pathnameIndex === -1) return this._url.search;
103
- const url = this.href;
104
- this.#search = queryIndex === -1 || queryIndex === url.length - 1 ? "" : url.slice(queryIndex);
105
- }
106
- return this.#search;
107
- }
108
- get searchParams() {
109
- if (this.#url) return this.#url.searchParams;
110
- if (!this.#searchParams) this.#searchParams = new URLSearchParams(this.search);
111
- return this.#searchParams;
112
- }
113
- get protocol() {
114
- if (this.#url) return this.#url.protocol;
115
- if (this.#protocol === void 0) {
116
- const [protocolIndex] = this.#getPos();
117
- if (protocolIndex === -1) return this._url.protocol;
118
- this.#protocol = this.href.slice(0, protocolIndex + 1);
119
- }
120
- return this.#protocol;
121
- }
122
- toString() {
123
- return this.href;
124
- }
125
- toJSON() {
126
- return this.href;
127
- }
128
- };
129
- lazyInherit(FastURL2.prototype, NativeURL.prototype, "_url");
130
- Object.setPrototypeOf(FastURL2.prototype, NativeURL.prototype);
131
- Object.setPrototypeOf(FastURL2, NativeURL);
132
- return FastURL2;
133
- })();
134
- function resolvePortAndHost(opts) {
135
- const _port = opts.port ?? globalThis.process?.env.PORT ?? 3e3;
136
- const port = typeof _port === "number" ? _port : Number.parseInt(_port, 10);
137
- if (port < 0 || port > 65535) throw new RangeError(`Port must be between 0 and 65535 (got "${port}").`);
138
- return {
139
- port,
140
- hostname: opts.hostname ?? globalThis.process?.env.HOST
141
- };
142
- }
143
- function fmtURL(host, port, secure) {
144
- if (!host || !port) return;
145
- if (host.includes(":")) host = `[${host}]`;
146
- return `http${secure ? "s" : ""}://${host}:${port}/`;
147
- }
148
- function printListening(opts, url) {
149
- if (!url || (opts.silent ?? globalThis.process?.env?.TEST)) return;
150
- let additionalInfo = "";
151
- try {
152
- const _url = new URL(url);
153
- if (_url.hostname === "[::]" || _url.hostname === "0.0.0.0") {
154
- _url.hostname = "localhost";
155
- url = _url.href;
156
- additionalInfo = " (all interfaces)";
157
- }
158
- } catch {
159
- }
160
- let listeningOn = `➜ Listening on:`;
161
- if (globalThis.process.stdout?.isTTY) {
162
- listeningOn = `\x1B[32m${listeningOn}\x1B[0m`;
163
- url = `\x1B[36m${url}\x1B[0m`;
164
- additionalInfo = `\x1B[2m${additionalInfo}\x1B[0m`;
165
- }
166
- console.log(`${listeningOn} ${url}${additionalInfo}`);
167
- }
168
- function resolveTLSOptions(opts) {
169
- if (!opts.tls || opts.protocol === "http") return;
170
- const cert = resolveCertOrKey(opts.tls.cert);
171
- const key = resolveCertOrKey(opts.tls.key);
172
- if (!cert && !key) {
173
- if (opts.protocol === "https") throw new TypeError("TLS `cert` and `key` must be provided for `https` protocol.");
174
- return;
175
- }
176
- if (!cert || !key) throw new TypeError("TLS `cert` and `key` must be provided together.");
177
- return {
178
- cert,
179
- key,
180
- passphrase: opts.tls.passphrase
181
- };
182
- }
183
- function resolveCertOrKey(value) {
184
- if (!value) return;
185
- if (typeof value !== "string") throw new TypeError("TLS certificate and key must be strings in PEM format or file paths.");
186
- if (value.startsWith("-----BEGIN ")) return value;
187
- const { readFileSync } = process.getBuiltinModule("node:fs");
188
- return readFileSync(value, "utf8");
189
- }
190
- function createWaitUntil() {
191
- const promises = /* @__PURE__ */ new Set();
192
- return {
193
- waitUntil: (promise) => {
194
- if (typeof promise?.then !== "function") return;
195
- promises.add(Promise.resolve(promise).catch(console.error).finally(() => {
196
- promises.delete(promise);
197
- }));
198
- },
199
- wait: () => {
200
- return Promise.all(promises);
201
- }
202
- };
203
- }
204
- const noColor = /* @__PURE__ */ (() => {
205
- const env = globalThis.process?.env ?? {};
206
- return env.NO_COLOR === "1" || env.TERM === "dumb";
207
- })();
208
- const _c = (c, r = 39) => (t) => noColor ? t : `\x1B[${c}m${t}\x1B[${r}m`;
209
- const bold = /* @__PURE__ */ _c(1, 22);
210
- const red = /* @__PURE__ */ _c(31);
211
- const green = /* @__PURE__ */ _c(32);
212
- const gray = /* @__PURE__ */ _c(90);
213
- function wrapFetch(server) {
214
- const fetchHandler = server.options.fetch;
215
- const middleware = server.options.middleware || [];
216
- return middleware.length === 0 ? fetchHandler : (request) => callMiddleware(request, fetchHandler, middleware, 0);
217
- }
218
- function callMiddleware(request, fetchHandler, middleware, index) {
219
- if (index === middleware.length) return fetchHandler(request);
220
- return middleware[index](request, () => callMiddleware(request, fetchHandler, middleware, index + 1));
221
- }
222
- const errorPlugin = (server) => {
223
- const errorHandler = server.options.error;
224
- if (!errorHandler) return;
225
- server.options.middleware.unshift((_req, next) => {
226
- try {
227
- const res = next();
228
- return res instanceof Promise ? res.catch((error) => errorHandler(error)) : res;
229
- } catch (error) {
230
- return errorHandler(error);
231
- }
232
- });
233
- };
234
- const gracefulShutdownPlugin = (server) => {
235
- const config = server.options?.gracefulShutdown;
236
- if (!globalThis.process?.on || config === false || config === void 0 && (process.env.CI || process.env.TEST)) return;
237
- const gracefulTimeout = config === true || !config?.gracefulTimeout ? Number.parseInt(process.env.SERVER_SHUTDOWN_TIMEOUT || "") || 5 : config.gracefulTimeout;
238
- let isClosing = false;
239
- let isClosed = false;
240
- const w = server.options.silent ? () => {
241
- } : process.stderr.write.bind(process.stderr);
242
- const forceClose = async () => {
243
- if (isClosed) return;
244
- w(red("\x1B[2K\rForcibly closing connections...\n"));
245
- isClosed = true;
246
- await server.close(true);
247
- };
248
- const shutdown = async () => {
249
- if (isClosing || isClosed) return;
250
- setTimeout(() => {
251
- globalThis.process.once("SIGINT", forceClose);
252
- }, 100);
253
- isClosing = true;
254
- const closePromise = server.close();
255
- for (let remaining = gracefulTimeout; remaining > 0; remaining--) {
256
- w(gray(`\rStopping server gracefully (${remaining}s)... Press ${bold("Ctrl+C")} again to force close.`));
257
- if (await Promise.race([closePromise.then(() => true), new Promise((r) => setTimeout(() => r(false), 1e3))])) {
258
- w("\x1B[2K\r" + green("Server closed successfully.\n"));
259
- isClosed = true;
260
- return;
261
- }
262
- }
263
- w("\x1B[2K\rGraceful shutdown timed out.\n");
264
- await forceClose();
265
- };
266
- for (const sig of ["SIGINT", "SIGTERM"]) globalThis.process.on(sig, shutdown);
267
- };
268
- async function sendNodeResponse(nodeRes, webRes) {
269
- if (!webRes) {
270
- nodeRes.statusCode = 500;
271
- return endNodeResponse(nodeRes);
272
- }
273
- if (webRes._toNodeResponse) {
274
- const res = webRes._toNodeResponse();
275
- if (res.body) {
276
- if (res.body instanceof ReadableStream) {
277
- writeHead(nodeRes, res.status, res.statusText, res.headers);
278
- return streamBody(res.body, nodeRes);
279
- } else if (typeof res.body?.pipe === "function") return pipeBody(res.body, nodeRes, res.status, res.statusText, res.headers);
280
- writeHead(nodeRes, res.status, res.statusText, res.headers);
281
- nodeRes.write(res.body);
282
- } else writeHead(nodeRes, res.status, res.statusText, res.headers);
283
- return endNodeResponse(nodeRes);
284
- }
285
- const rawHeaders = [...webRes.headers];
286
- writeHead(nodeRes, webRes.status, webRes.statusText, rawHeaders);
287
- return webRes.body ? streamBody(webRes.body, nodeRes) : endNodeResponse(nodeRes);
288
- }
289
- function writeHead(nodeRes, status, statusText, rawHeaders) {
290
- const writeHeaders = globalThis.Deno ? rawHeaders : rawHeaders.flat();
291
- if (!nodeRes.headersSent) if (nodeRes.req?.httpVersion === "2.0") nodeRes.writeHead(status, writeHeaders);
292
- else nodeRes.writeHead(status, statusText, writeHeaders);
293
- }
294
- function endNodeResponse(nodeRes) {
295
- return new Promise((resolve) => nodeRes.end(resolve));
296
- }
297
- function pipeBody(stream, nodeRes, status, statusText, headers) {
298
- if (nodeRes.destroyed) {
299
- stream.destroy?.();
300
- return;
301
- }
302
- if (typeof stream.on !== "function" || typeof stream.destroy !== "function") {
303
- writeHead(nodeRes, status, statusText, headers);
304
- stream.pipe(nodeRes);
305
- return new Promise((resolve) => nodeRes.on("close", resolve));
306
- }
307
- if (stream.destroyed) {
308
- writeHead(nodeRes, 500, "Internal Server Error", []);
309
- return endNodeResponse(nodeRes);
310
- }
311
- return new Promise((resolve) => {
312
- function onEarlyError() {
313
- stream.off("readable", onReadable);
314
- stream.destroy();
315
- writeHead(nodeRes, 500, "Internal Server Error", []);
316
- endNodeResponse(nodeRes).then(resolve);
317
- }
318
- function onReadable() {
319
- stream.off("error", onEarlyError);
320
- if (nodeRes.destroyed) {
321
- stream.destroy();
322
- return resolve();
323
- }
324
- writeHead(nodeRes, status, statusText, headers);
325
- pipeline(stream, nodeRes).catch(() => {
326
- }).then(() => resolve());
327
- }
328
- stream.once("error", onEarlyError);
329
- stream.once("readable", onReadable);
330
- });
331
- }
332
- function streamBody(stream, nodeRes) {
333
- if (nodeRes.destroyed) {
334
- stream.cancel();
335
- return;
336
- }
337
- const reader = stream.getReader();
338
- function streamCancel(error) {
339
- reader.cancel(error).catch(() => {
340
- });
341
- if (error) nodeRes.destroy(error);
342
- }
343
- function streamHandle({ done, value }) {
344
- try {
345
- if (done) nodeRes.end();
346
- else if (nodeRes.write(value)) reader.read().then(streamHandle, streamCancel);
347
- else nodeRes.once("drain", () => reader.read().then(streamHandle, streamCancel));
348
- } catch (error) {
349
- streamCancel(error instanceof Error ? error : void 0);
350
- }
351
- }
352
- nodeRes.on("close", streamCancel);
353
- nodeRes.on("error", streamCancel);
354
- reader.read().then(streamHandle, streamCancel);
355
- return reader.closed.catch(streamCancel).finally(() => {
356
- nodeRes.off("close", streamCancel);
357
- nodeRes.off("error", streamCancel);
358
- });
359
- }
360
- const HOST_RE = /^(\[(?:[A-Fa-f0-9:.]+)\]|(?:[A-Za-z0-9_-]+\.)*[A-Za-z0-9_-]+|(?:\d{1,3}\.){3}\d{1,3})(:\d{1,5})?$/;
361
- var NodeRequestURL = class extends FastURL {
362
- #req;
363
- constructor({ req }) {
364
- const path = req.url || "/";
365
- if (path[0] === "/") {
366
- const qIndex = path.indexOf("?");
367
- const pathname = qIndex === -1 ? path : path?.slice(0, qIndex) || "/";
368
- const search = qIndex === -1 ? "" : path?.slice(qIndex) || "";
369
- let host = req.headers.host || req.headers[":authority"];
370
- if (host && !HOST_RE.test(host)) host = "_invalid_";
371
- else if (!host) if (req.socket) host = `${req.socket.localFamily === "IPv6" ? "[" + req.socket.localAddress + "]" : req.socket.localAddress}:${req.socket?.localPort || "80"}`;
372
- else host = "localhost";
373
- const protocol = req.socket?.encrypted || req.headers["x-forwarded-proto"] === "https" || req.headers[":scheme"] === "https" ? "https:" : "http:";
374
- super({
375
- protocol,
376
- host,
377
- pathname,
378
- search
379
- });
380
- } else super(path);
381
- this.#req = req;
382
- }
383
- get pathname() {
384
- return super.pathname;
385
- }
386
- set pathname(value) {
387
- this._url.pathname = value;
388
- this.#req.url = this._url.pathname + this._url.search;
389
- }
390
- };
391
- const NodeRequestHeaders = /* @__PURE__ */ (() => {
392
- const NativeHeaders = globalThis.Headers;
393
- class Headers2 {
394
- #req;
395
- #headers;
396
- constructor(req) {
397
- this.#req = req;
398
- }
399
- static [Symbol.hasInstance](val) {
400
- return val instanceof NativeHeaders;
401
- }
402
- get _headers() {
403
- if (!this.#headers) {
404
- const headers = new NativeHeaders();
405
- const rawHeaders = this.#req.rawHeaders;
406
- const len = rawHeaders.length;
407
- for (let i = 0; i < len; i += 2) {
408
- const key = rawHeaders[i];
409
- if (key.charCodeAt(0) === 58) continue;
410
- const value = rawHeaders[i + 1];
411
- headers.append(key, value);
412
- }
413
- this.#headers = headers;
414
- }
415
- return this.#headers;
416
- }
417
- get(name) {
418
- if (this.#headers) return this.#headers.get(name);
419
- const value = this.#req.headers[name.toLowerCase()];
420
- return Array.isArray(value) ? value.join(", ") : value || null;
421
- }
422
- has(name) {
423
- if (this.#headers) return this.#headers.has(name);
424
- return name.toLowerCase() in this.#req.headers;
425
- }
426
- getSetCookie() {
427
- if (this.#headers) return this.#headers.getSetCookie();
428
- const value = this.#req.headers["set-cookie"];
429
- return Array.isArray(value) ? value : value ? [value] : [];
430
- }
431
- entries() {
432
- return this._headers.entries();
433
- }
434
- [Symbol.iterator]() {
435
- return this.entries();
436
- }
437
- }
438
- lazyInherit(Headers2.prototype, NativeHeaders.prototype, "_headers");
439
- Object.setPrototypeOf(Headers2, NativeHeaders);
440
- Object.setPrototypeOf(Headers2.prototype, NativeHeaders.prototype);
441
- return Headers2;
442
- })();
443
- const NodeRequest = /* @__PURE__ */ (() => {
444
- const NativeRequest = globalThis.Request;
445
- class Request {
446
- runtime;
447
- #req;
448
- #url;
449
- #bodyStream;
450
- #request;
451
- #headers;
452
- #abortController;
453
- constructor(ctx) {
454
- this.#req = ctx.req;
455
- this.runtime = {
456
- name: "node",
457
- node: ctx
458
- };
459
- }
460
- static [Symbol.hasInstance](val) {
461
- return val instanceof NativeRequest;
462
- }
463
- get ip() {
464
- return this.#req.socket?.remoteAddress;
465
- }
466
- get method() {
467
- if (this.#request) return this.#request.method;
468
- return this.#req.method || "GET";
469
- }
470
- get _url() {
471
- return this.#url ||= new NodeRequestURL({ req: this.#req });
472
- }
473
- set _url(url) {
474
- this.#url = url;
475
- }
476
- get url() {
477
- if (this.#request) return this.#request.url;
478
- return this._url.href;
479
- }
480
- get headers() {
481
- if (this.#request) return this.#request.headers;
482
- return this.#headers ||= new NodeRequestHeaders(this.#req);
483
- }
484
- get _abortController() {
485
- if (!this.#abortController) {
486
- this.#abortController = new AbortController();
487
- const { req, res } = this.runtime.node;
488
- const abortController = this.#abortController;
489
- const abort = (err) => abortController.abort?.(err);
490
- if (res) res.once("close", () => {
491
- const reqError = req.errored;
492
- if (reqError) abort(reqError);
493
- else if (!res.writableEnded) abort();
494
- });
495
- else req.once("close", () => {
496
- if (!req.complete) abort();
497
- });
498
- }
499
- return this.#abortController;
500
- }
501
- get signal() {
502
- return this.#request ? this.#request.signal : this._abortController.signal;
503
- }
504
- get body() {
505
- if (this.#request) return this.#request.body;
506
- if (this.#bodyStream === void 0) {
507
- const method = this.method;
508
- this.#bodyStream = !(method === "GET" || method === "HEAD") ? Readable.toWeb(this.#req) : null;
509
- }
510
- return this.#bodyStream;
511
- }
512
- text() {
513
- if (this.#request) return this.#request.text();
514
- if (this.#bodyStream !== void 0) return this.#bodyStream ? new Response(this.#bodyStream).text() : Promise.resolve("");
515
- return readBody(this.#req).then((buf) => buf.toString());
516
- }
517
- json() {
518
- if (this.#request) return this.#request.json();
519
- return this.text().then((text) => JSON.parse(text));
520
- }
521
- get _request() {
522
- if (!this.#request) {
523
- const body = this.body;
524
- this.#request = new NativeRequest(this.url, {
525
- method: this.method,
526
- headers: this.headers,
527
- signal: this._abortController.signal,
528
- body,
529
- duplex: body ? "half" : void 0
530
- });
531
- this.#headers = void 0;
532
- this.#bodyStream = void 0;
533
- }
534
- return this.#request;
535
- }
536
- }
537
- lazyInherit(Request.prototype, NativeRequest.prototype, "_request");
538
- Object.setPrototypeOf(Request.prototype, NativeRequest.prototype);
539
- return Request;
540
- })();
541
- function readBody(req) {
542
- if ("rawBody" in req && Buffer.isBuffer(req.rawBody)) return Promise.resolve(req.rawBody);
543
- return new Promise((resolve, reject) => {
544
- const chunks = [];
545
- const onData = (chunk) => {
546
- chunks.push(chunk);
547
- };
548
- const onError = (err) => {
549
- reject(err);
550
- };
551
- const onEnd = () => {
552
- req.off("error", onError);
553
- req.off("data", onData);
554
- resolve(Buffer.concat(chunks));
555
- };
556
- req.on("data", onData).once("end", onEnd).once("error", onError);
557
- });
558
- }
559
- const NodeResponse = /* @__PURE__ */ (() => {
560
- const NativeResponse = globalThis.Response;
561
- const STATUS_CODES = globalThis.process?.getBuiltinModule?.("node:http")?.STATUS_CODES || {};
562
- class NodeResponse2 {
563
- #body;
564
- #init;
565
- #headers;
566
- #response;
567
- constructor(body, init) {
568
- this.#body = body;
569
- this.#init = init;
570
- }
571
- static [Symbol.hasInstance](val) {
572
- return val instanceof NativeResponse;
573
- }
574
- get status() {
575
- return this.#response?.status || this.#init?.status || 200;
576
- }
577
- get statusText() {
578
- return this.#response?.statusText || this.#init?.statusText || STATUS_CODES[this.status] || "";
579
- }
580
- get headers() {
581
- if (this.#response) return this.#response.headers;
582
- if (this.#headers) return this.#headers;
583
- const initHeaders = this.#init?.headers;
584
- return this.#headers = initHeaders instanceof Headers ? initHeaders : new Headers(initHeaders);
585
- }
586
- get ok() {
587
- if (this.#response) return this.#response.ok;
588
- const status = this.status;
589
- return status >= 200 && status < 300;
590
- }
591
- get _response() {
592
- if (this.#response) return this.#response;
593
- let body = this.#body;
594
- if (body && typeof body.pipe === "function" && !(body instanceof Readable)) {
595
- const stream = new PassThrough();
596
- body.pipe(stream);
597
- const abort = body.abort;
598
- if (abort) stream.once("close", () => abort());
599
- body = stream;
600
- }
601
- this.#response = new NativeResponse(body, this.#headers ? {
602
- ...this.#init,
603
- headers: this.#headers
604
- } : this.#init);
605
- this.#init = void 0;
606
- this.#headers = void 0;
607
- this.#body = void 0;
608
- return this.#response;
609
- }
610
- _toNodeResponse() {
611
- const status = this.status;
612
- const statusText = this.statusText;
613
- let body;
614
- let contentType;
615
- let contentLength;
616
- if (this.#response) body = this.#response.body;
617
- else if (this.#body) if (this.#body instanceof ReadableStream) body = this.#body;
618
- else if (typeof this.#body === "string") {
619
- body = this.#body;
620
- contentType = "text/plain; charset=UTF-8";
621
- contentLength = Buffer.byteLength(this.#body);
622
- } else if (this.#body instanceof ArrayBuffer) {
623
- body = Buffer.from(this.#body);
624
- contentLength = this.#body.byteLength;
625
- } else if (this.#body instanceof Uint8Array) {
626
- body = this.#body;
627
- contentLength = this.#body.byteLength;
628
- } else if (this.#body instanceof DataView) {
629
- body = Buffer.from(this.#body.buffer);
630
- contentLength = this.#body.byteLength;
631
- } else if (this.#body instanceof Blob) {
632
- body = this.#body.stream();
633
- contentType = this.#body.type;
634
- contentLength = this.#body.size;
635
- } else if (typeof this.#body.pipe === "function") body = this.#body;
636
- else body = this._response.body;
637
- const headers = [];
638
- const initHeaders = this.#init?.headers;
639
- const headerEntries = this.#response?.headers || this.#headers || (initHeaders ? Array.isArray(initHeaders) ? initHeaders : initHeaders?.entries ? initHeaders.entries() : Object.entries(initHeaders).map(([k, v]) => [k.toLowerCase(), v]) : void 0);
640
- let hasContentTypeHeader;
641
- let hasContentLength;
642
- if (headerEntries) for (const [key, value] of headerEntries) {
643
- if (Array.isArray(value)) for (const v of value) headers.push([key, v]);
644
- else headers.push([key, value]);
645
- if (key === "content-type") hasContentTypeHeader = true;
646
- else if (key === "content-length") hasContentLength = true;
647
- }
648
- if (contentType && !hasContentTypeHeader) headers.push(["content-type", contentType]);
649
- if (contentLength && !hasContentLength) headers.push(["content-length", String(contentLength)]);
650
- this.#init = void 0;
651
- this.#headers = void 0;
652
- this.#response = void 0;
653
- this.#body = void 0;
654
- return {
655
- status,
656
- statusText,
657
- headers,
658
- body
659
- };
660
- }
661
- }
662
- lazyInherit(NodeResponse2.prototype, NativeResponse.prototype, "_response");
663
- Object.setPrototypeOf(NodeResponse2, NativeResponse);
664
- Object.setPrototypeOf(NodeResponse2.prototype, NativeResponse.prototype);
665
- return NodeResponse2;
666
- })();
667
- function serve(options) {
668
- return new NodeServer(options);
669
- }
670
- var NodeServer = class {
671
- runtime = "node";
672
- options;
673
- node;
674
- serveOptions;
675
- fetch;
676
- waitUntil;
677
- #isSecure;
678
- #listeningPromise;
679
- #listenError;
680
- #wait;
681
- constructor(options) {
682
- this.options = {
683
- ...options,
684
- middleware: [...options.middleware || []]
685
- };
686
- for (const plugin of options.plugins || []) plugin(this);
687
- errorPlugin(this);
688
- const fetchHandler = this.fetch = wrapFetch(this);
689
- const handler = (nodeReq, nodeRes) => {
690
- const request = new NodeRequest({
691
- req: nodeReq,
692
- res: nodeRes
693
- });
694
- request.waitUntil = this.#wait?.waitUntil;
695
- const res = fetchHandler(request);
696
- return res instanceof Promise ? res.then((resolvedRes) => sendNodeResponse(nodeRes, resolvedRes)) : sendNodeResponse(nodeRes, res);
697
- };
698
- this.node = {
699
- handler,
700
- server: void 0
701
- };
702
- const loader = globalThis.__srvxLoader__;
703
- if (loader) {
704
- loader({ server: this });
705
- return;
706
- }
707
- gracefulShutdownPlugin(this);
708
- this.#wait = createWaitUntil();
709
- this.waitUntil = this.#wait.waitUntil;
710
- const tls = resolveTLSOptions(this.options);
711
- const { port, hostname: host } = resolvePortAndHost(this.options);
712
- this.serveOptions = {
713
- port,
714
- host,
715
- exclusive: !this.options.reusePort,
716
- ...tls ? {
717
- cert: tls.cert,
718
- key: tls.key,
719
- passphrase: tls.passphrase
720
- } : {},
721
- ...this.options.node
722
- };
723
- let server;
724
- this.#isSecure = !!this.serveOptions.cert && this.options.protocol !== "http";
725
- if (this.options.node?.http2 ?? this.#isSecure) if (this.#isSecure) server = nodeHTTP2.createSecureServer({
726
- allowHTTP1: true,
727
- ...this.serveOptions
728
- }, handler);
729
- else throw new Error("node.http2 option requires tls certificate!");
730
- else if (this.#isSecure) server = nodeHTTPS.createServer(this.serveOptions, handler);
731
- else server = nodeHTTP.createServer(this.serveOptions, handler);
732
- this.node.server = server;
733
- if (!options.manual) this.serve().catch(() => {
734
- });
735
- }
736
- serve() {
737
- if (this.#listeningPromise) return this.#listeningPromise.then(() => this);
738
- const server = this.node?.server;
739
- if (!server) return Promise.reject(/* @__PURE__ */ new Error("Server not initialized"));
740
- this.#listenError = void 0;
741
- this.#listeningPromise = new Promise((resolve, reject) => {
742
- const onError = (error) => {
743
- server.off("listening", onListening);
744
- this.#listenError = error;
745
- this.#listeningPromise = void 0;
746
- reject(error);
747
- };
748
- const onListening = () => {
749
- server.off("error", onError);
750
- printListening(this.options, this.url);
751
- resolve();
752
- };
753
- server.once("error", onError);
754
- server.once("listening", onListening);
755
- server.listen(this.serveOptions);
756
- });
757
- return this.#listeningPromise.then(() => this);
758
- }
759
- get url() {
760
- const addr = this.node?.server?.address();
761
- if (!addr) return;
762
- return typeof addr === "string" ? addr : fmtURL(addr.address, addr.port, this.#isSecure);
763
- }
764
- ready() {
765
- if (this.#listenError) return Promise.reject(this.#listenError);
766
- return Promise.resolve(this.#listeningPromise).then(() => this);
767
- }
768
- async close(closeAll) {
769
- await Promise.all([this.#wait?.wait(), new Promise((resolve, reject) => {
770
- const server = this.node?.server;
771
- if (server && closeAll && "closeAllConnections" in server) server.closeAllConnections();
772
- if (!server || !server.listening) return resolve();
773
- server.close((error) => error ? reject(error) : resolve());
774
- })]);
775
- }
776
- };
777
- export {
778
- FastURL as F,
779
- NodeResponse as N,
780
- serve as s
781
- };