@niscorp/moss 0.3.3 → 0.5.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 (175) hide show
  1. package/CHANGELOG.md +534 -0
  2. package/DESIGN.md +265 -49
  3. package/DOCS.md +1524 -341
  4. package/README.md +37 -20
  5. package/dist/app-BoVB0_0j.d.ts +143 -0
  6. package/dist/app-Cc8VnOz8.d.cts +143 -0
  7. package/dist/chunk-3RIWTXGI.cjs +174 -0
  8. package/dist/chunk-3RIWTXGI.cjs.map +1 -0
  9. package/dist/chunk-4ZGAFZGS.js +152 -0
  10. package/dist/chunk-4ZGAFZGS.js.map +1 -0
  11. package/dist/chunk-5U2ZZUI5.cjs +137 -0
  12. package/dist/chunk-5U2ZZUI5.cjs.map +1 -0
  13. package/dist/chunk-6QPTMKRE.cjs +53 -0
  14. package/dist/chunk-6QPTMKRE.cjs.map +1 -0
  15. package/dist/chunk-BQRPR2F7.js +2704 -0
  16. package/dist/chunk-BQRPR2F7.js.map +1 -0
  17. package/dist/chunk-BZK37HR3.cjs +2716 -0
  18. package/dist/chunk-BZK37HR3.cjs.map +1 -0
  19. package/dist/chunk-FTFME3HS.cjs +383 -0
  20. package/dist/chunk-FTFME3HS.cjs.map +1 -0
  21. package/dist/{chunk-NE2EXMTO.js → chunk-FTZ6NFKZ.js} +43 -16
  22. package/dist/chunk-FTZ6NFKZ.js.map +1 -0
  23. package/dist/chunk-IUJ2TRNB.cjs +57 -0
  24. package/dist/chunk-IUJ2TRNB.cjs.map +1 -0
  25. package/dist/chunk-IW3A2YIC.js +49 -0
  26. package/dist/chunk-IW3A2YIC.js.map +1 -0
  27. package/dist/chunk-JYBHJDT7.cjs +30 -0
  28. package/dist/chunk-JYBHJDT7.cjs.map +1 -0
  29. package/dist/chunk-NRZ3Z2FF.js +61 -0
  30. package/dist/chunk-NRZ3Z2FF.js.map +1 -0
  31. package/dist/chunk-PCPQMC7A.js +26 -0
  32. package/dist/chunk-PCPQMC7A.js.map +1 -0
  33. package/dist/chunk-T5BRTFKP.js +53 -0
  34. package/dist/chunk-T5BRTFKP.js.map +1 -0
  35. package/dist/chunk-TGKDS2T7.cjs +330 -0
  36. package/dist/chunk-TGKDS2T7.cjs.map +1 -0
  37. package/dist/chunk-TLNRFAQ6.js +326 -0
  38. package/dist/chunk-TLNRFAQ6.js.map +1 -0
  39. package/dist/chunk-VKQPOZDJ.cjs +72 -0
  40. package/dist/chunk-VKQPOZDJ.cjs.map +1 -0
  41. package/dist/chunk-ZWB6QDGF.js +374 -0
  42. package/dist/chunk-ZWB6QDGF.js.map +1 -0
  43. package/dist/client/node.cjs.map +1 -1
  44. package/dist/client/node.d.cts +3 -1
  45. package/dist/client/node.d.ts +3 -1
  46. package/dist/client/node.js.map +1 -1
  47. package/dist/client.cjs +14 -302
  48. package/dist/client.cjs.map +1 -1
  49. package/dist/client.d.cts +9 -33
  50. package/dist/client.d.ts +9 -33
  51. package/dist/client.js +2 -2
  52. package/dist/document-CU3muGbz.d.cts +169 -0
  53. package/dist/document-UFbAd-Ka.d.ts +169 -0
  54. package/dist/env-CY8EGWnD.d.cts +24 -0
  55. package/dist/env-DEPqqHEb.d.ts +24 -0
  56. package/dist/index-BuDuOSyx.d.cts +130 -0
  57. package/dist/index-BuDuOSyx.d.ts +130 -0
  58. package/dist/index.cjs +45 -3918
  59. package/dist/index.cjs.map +1 -1
  60. package/dist/index.d.cts +46 -100
  61. package/dist/index.d.ts +46 -100
  62. package/dist/index.js +4 -94
  63. package/dist/index.js.map +1 -1
  64. package/dist/integrations.cjs +975 -0
  65. package/dist/integrations.cjs.map +1 -0
  66. package/dist/integrations.d.cts +63 -0
  67. package/dist/integrations.d.ts +63 -0
  68. package/dist/integrations.js +972 -0
  69. package/dist/integrations.js.map +1 -0
  70. package/dist/messages-CSwCJgH3.d.cts +51 -0
  71. package/dist/messages-CSwCJgH3.d.ts +51 -0
  72. package/dist/node.cjs +17 -3457
  73. package/dist/node.cjs.map +1 -1
  74. package/dist/node.d.cts +13 -10
  75. package/dist/node.d.ts +13 -10
  76. package/dist/node.js +5 -3
  77. package/dist/protocol.cjs +24 -0
  78. package/dist/protocol.cjs.map +1 -0
  79. package/dist/protocol.d.cts +11 -0
  80. package/dist/protocol.d.ts +11 -0
  81. package/dist/protocol.js +3 -0
  82. package/dist/protocol.js.map +1 -0
  83. package/dist/slots-96gNc9uH.d.cts +12 -0
  84. package/dist/slots-BX5N0Juv.d.ts +14 -0
  85. package/dist/slots-CU-AbnJg.d.cts +14 -0
  86. package/dist/slots-sLXDrlbG.d.ts +12 -0
  87. package/dist/socket-D90h1xSV.d.cts +24 -0
  88. package/dist/socket-D90h1xSV.d.ts +24 -0
  89. package/dist/targets-DInbLzff.d.cts +9 -0
  90. package/dist/targets-DInbLzff.d.ts +9 -0
  91. package/dist/terminal/dom/index.cjs +8 -3
  92. package/dist/terminal/dom/index.cjs.map +1 -1
  93. package/dist/terminal/dom/index.d.cts +3 -0
  94. package/dist/terminal/dom/index.d.ts +3 -0
  95. package/dist/terminal/dom/index.js +8 -3
  96. package/dist/terminal/dom/index.js.map +1 -1
  97. package/dist/terminal/dom/server.cjs +2 -7
  98. package/dist/terminal/dom/server.cjs.map +1 -1
  99. package/dist/terminal/dom/server.d.cts +3 -5
  100. package/dist/terminal/dom/server.d.ts +3 -5
  101. package/dist/terminal/dom/server.js +2 -7
  102. package/dist/terminal/dom/server.js.map +1 -1
  103. package/dist/terminal/index.cjs +22 -300
  104. package/dist/terminal/index.cjs.map +1 -1
  105. package/dist/terminal/index.d.cts +7 -2
  106. package/dist/terminal/index.d.ts +7 -2
  107. package/dist/terminal/index.js +21 -22
  108. package/dist/terminal/index.js.map +1 -1
  109. package/dist/terminal/ink/index.d.ts +3 -2
  110. package/dist/terminal/ink/index.js +149 -106
  111. package/dist/terminal/ink/index.js.map +1 -1
  112. package/dist/terminal/react/index.cjs +21 -71
  113. package/dist/terminal/react/index.cjs.map +1 -1
  114. package/dist/terminal/react/index.d.cts +5 -2
  115. package/dist/terminal/react/index.d.ts +5 -2
  116. package/dist/terminal/react/index.js +17 -20
  117. package/dist/terminal/react/index.js.map +1 -1
  118. package/dist/terminal/react/server.cjs +4 -68
  119. package/dist/terminal/react/server.cjs.map +1 -1
  120. package/dist/terminal/react/server.d.cts +9 -6
  121. package/dist/terminal/react/server.d.ts +9 -6
  122. package/dist/terminal/react/server.js +3 -18
  123. package/dist/terminal/react/server.js.map +1 -1
  124. package/dist/terminal/tty/index.cjs +147 -130
  125. package/dist/terminal/tty/index.cjs.map +1 -1
  126. package/dist/terminal/tty/index.d.cts +4 -2
  127. package/dist/terminal/tty/index.d.ts +4 -2
  128. package/dist/terminal/tty/index.js +147 -130
  129. package/dist/terminal/tty/index.js.map +1 -1
  130. package/dist/terminal/vue/index.cjs +20 -72
  131. package/dist/terminal/vue/index.cjs.map +1 -1
  132. package/dist/terminal/vue/index.d.cts +8 -12
  133. package/dist/terminal/vue/index.d.ts +8 -12
  134. package/dist/terminal/vue/index.js +45 -1
  135. package/dist/terminal/vue/index.js.map +1 -1
  136. package/dist/terminal/vue/server.cjs +4 -76
  137. package/dist/terminal/vue/server.cjs.map +1 -1
  138. package/dist/terminal/vue/server.d.cts +7 -6
  139. package/dist/terminal/vue/server.d.ts +7 -6
  140. package/dist/terminal/vue/server.js +3 -10
  141. package/dist/terminal/vue/server.js.map +1 -1
  142. package/dist/tide.cjs +41 -0
  143. package/dist/tide.cjs.map +1 -0
  144. package/dist/tide.d.cts +25 -0
  145. package/dist/tide.d.ts +25 -0
  146. package/dist/tide.js +39 -0
  147. package/dist/tide.js.map +1 -0
  148. package/dist/unverified.cjs +12 -0
  149. package/dist/unverified.cjs.map +1 -0
  150. package/dist/unverified.d.cts +7 -0
  151. package/dist/unverified.d.ts +7 -0
  152. package/dist/unverified.js +10 -0
  153. package/dist/unverified.js.map +1 -0
  154. package/dist/vite.cjs +96 -232
  155. package/dist/vite.cjs.map +1 -1
  156. package/dist/vite.d.cts +6 -8
  157. package/dist/vite.d.ts +6 -8
  158. package/dist/vite.js +97 -71
  159. package/dist/vite.js.map +1 -1
  160. package/package.json +56 -13
  161. package/dist/chunk-4T6KAXBP.js +0 -256
  162. package/dist/chunk-4T6KAXBP.js.map +0 -1
  163. package/dist/chunk-AUZMT3GL.js +0 -3406
  164. package/dist/chunk-AUZMT3GL.js.map +0 -1
  165. package/dist/chunk-EMRDIZLY.js +0 -99
  166. package/dist/chunk-EMRDIZLY.js.map +0 -1
  167. package/dist/chunk-HOZID4EM.js +0 -366
  168. package/dist/chunk-HOZID4EM.js.map +0 -1
  169. package/dist/chunk-NE2EXMTO.js.map +0 -1
  170. package/dist/chunk-SK4PDGEE.js +0 -55
  171. package/dist/chunk-SK4PDGEE.js.map +0 -1
  172. package/dist/document-DhWxieUc.d.cts +0 -1024
  173. package/dist/document-DhWxieUc.d.ts +0 -1024
  174. package/dist/slots-Do9ScG7v.d.cts +0 -37
  175. package/dist/slots-Do9ScG7v.d.ts +0 -37
package/dist/vite.cjs CHANGED
@@ -1,203 +1,125 @@
1
1
  'use strict';
2
2
 
3
+ var chunk5U2ZZUI5_cjs = require('./chunk-5U2ZZUI5.cjs');
4
+ var chunkBZK37HR3_cjs = require('./chunk-BZK37HR3.cjs');
5
+ require('./chunk-FTFME3HS.cjs');
6
+ require('./chunk-JYBHJDT7.cjs');
7
+ require('./chunk-3RIWTXGI.cjs');
3
8
  var promises = require('fs/promises');
9
+ var net = require('net');
4
10
  var path = require('path');
5
11
  var nodeServer = require('@hono/node-server');
6
- var document = require('@niscorp/nova/document');
7
- require('@hono/node-server/serve-static');
8
- var ws = require('ws');
9
- require('@niscorp/nova');
10
12
 
11
- // src/vite.ts
12
-
13
- // src/session-cookie.ts
14
- var DEFAULT_TOKEN_KEY = "nisc.token";
15
- var tokenFromCookie = (header, name = DEFAULT_TOKEN_KEY) => {
16
- if (header === void 0 || header === null) return null;
17
- const wanted = encodeURIComponent(name);
18
- for (const part of header.split(";")) {
19
- const at = part.indexOf("=");
20
- if (at < 0 || part.slice(0, at).trim() !== wanted) continue;
21
- try {
22
- const value = decodeURIComponent(part.slice(at + 1).trim());
23
- return value === "" ? null : value;
24
- } catch {
25
- return null;
26
- }
27
- }
28
- return null;
29
- };
30
- var nameOf = (key, port) => `${key}${port === "" ? "" : `.${port}`}`;
31
- var cookiesAt = (where, key, token, lastsMs) => {
32
- const name = encodeURIComponent(nameOf(key, where.port));
33
- const life = token === null ? "; Max-Age=0" : typeof lastsMs === "number" ? `; Max-Age=${Math.max(0, Math.floor(lastsMs / 1e3))}` : "";
34
- const kept = `Path=/; SameSite=Lax${where.secure ? "; Secure" : ""}${life}`;
35
- return [`${where.secure ? "__Host-" : ""}${name}=${encodeURIComponent(token ?? "")}; HttpOnly; ${kept}`, `${name}.held=${token === null ? "" : "1"}; ${kept}`];
36
- };
37
- var originOf = (request) => {
38
- const said2 = request.headers.get("origin");
39
- if (said2 !== null && URL.canParse(said2)) return said2;
40
- const here = new URL(request.url);
41
- const scheme = request.headers.get("x-forwarded-proto")?.split(",")[0]?.trim() || here.protocol.slice(0, -1);
42
- return `${scheme}://${request.headers.get("host") ?? here.host}`;
43
- };
44
- var sessionCookies = (page, token, options = {}) => {
45
- const at = new URL(typeof page === "string" ? page : originOf(page));
46
- return cookiesAt({ port: at.port, secure: at.protocol === "https:" }, options.key ?? DEFAULT_TOKEN_KEY, token, options.lastsMs);
13
+ var DEFAULT_WATCH = /([\\/]src[\\/](app|server|db|ui)[\\/])|([\\/]nisc\.config\.[cm]?[jt]s$)/;
14
+ var REBOOT_DEBOUNCE_MS = 200;
15
+ var loopback = () => {
16
+ const addresses = new net.BlockList();
17
+ addresses.addSubnet("127.0.0.0", 8, "ipv4");
18
+ addresses.addAddress("::1", "ipv6");
19
+ return addresses;
47
20
  };
48
- var sessionTokenOf = (cookies, where, key = DEFAULT_TOKEN_KEY) => {
49
- const name = nameOf(key, where.port);
50
- for (const secure of where.secure === void 0 ? [true, false] : [where.secure]) {
51
- const token = tokenFromCookie(cookies, `${secure ? "__Host-" : ""}${name}`);
52
- if (token !== null) return { token, clear: cookiesAt({ port: where.port, secure }, key, null) };
21
+ var LOOPBACK = loopback();
22
+ var isFromThisMachine = (address) => address !== void 0 && (net.isIPv4(address) ? LOOPBACK.check(address, "ipv4") : net.isIPv6(address) && LOOPBACK.check(address, "ipv6"));
23
+ var signInRefusal = (req, fromNetwork) => {
24
+ if (!fromNetwork && !isFromThisMachine(req.socket.remoteAddress)) {
25
+ return `The dev sign-in (/dev/as/\u2026) answers requests from this machine only, and this one came from ${req.socket.remoteAddress ?? "an address the server cannot see"}. It hands out a session for whoever the address names. To sign in from another device, set \`signInFromNetwork: true\` on mossDev: anybody who can reach this dev server then signs in as anybody.`;
53
26
  }
54
- return null;
27
+ return void 0;
55
28
  };
56
-
57
- // src/document.ts
58
- var SNAPSHOT_ELEMENT_ID = "nisc-snapshot";
59
- var DEFAULT_ROOT = '<div id="root"></div>';
60
- var embedSnapshot = (snapshot, principal, path) => {
61
- const { frame, trees, seed, live } = snapshot;
62
- const text = JSON.stringify({ frame, trees, principal: principal !== null, live, ...seed !== void 0 ? { seed } : {}, ...path !== void 0 ? { path } : {} }).replace(/</g, "\\u003c").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
63
- return `<script type="application/json" id="${SNAPSHOT_ELEMENT_ID}">${text}</script>`;
29
+ var retire = async (booted) => {
30
+ booted.transport?.close();
31
+ booted.app.server.socket.stop();
32
+ await (booted.app.close ?? (() => booted.app.server.close()))();
64
33
  };
65
- var documentHeaders = (principal) => principal === null ? { "cache-control": "no-cache", vary: "Cookie" } : { "cache-control": "private, no-store", vary: "Cookie" };
66
- var escapeAttribute = (value) => value.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;");
67
- var renderDocument = async (config) => {
68
- const { server, template, request, draw } = config;
69
- const tokenKey = config.tokenKey ?? DEFAULT_TOKEN_KEY;
70
- const root = config.root ?? DEFAULT_ROOT;
71
- const undrawn = { html: template, headers: documentHeaders(null), drawn: false, principal: null };
72
- if (!template.includes(root)) return undrawn;
73
- try {
74
- const ports = [/:(\d+)$/.exec(request.host ?? "")?.[1] ?? "", ...config.site !== void 0 && URL.canParse(config.site) ? [new URL(config.site).port] : []];
75
- const token = ports.map((port) => sessionTokenOf(request.cookie, { port }, tokenKey)?.token ?? null).find((found) => found !== null) ?? null;
76
- const principal = token === null ? null : await server.principalOf(token);
77
- const route = server.page(request.path);
78
- const host = route?.host ?? server.shells;
79
- if (host === void 0) return undrawn;
80
- const snapshot = await host.snapshot(principal === null ? null : token, principal, {
81
- ...config.waitMs !== void 0 ? { waitMs: config.waitMs } : {},
82
- ...route !== void 0 ? { inputs: route.inputs } : {}
34
+ var said = (error) => error instanceof Error ? error.message : String(error);
35
+ var devMiddleware = (vite, options, owned, current) => (req, res, next) => {
36
+ const url = req.url ?? "/";
37
+ const path$1 = url.split("?")[0] ?? "/";
38
+ const who = /^\/dev\/as\/([\w.@%-]+)$/.exec(path$1)?.[1];
39
+ if (who !== void 0) {
40
+ void current().then(async ({ app }) => {
41
+ if (app.signIn === void 0) {
42
+ next();
43
+ return;
44
+ }
45
+ const refusal = signInRefusal(req, options.signInFromNetwork === true);
46
+ if (refusal !== void 0) {
47
+ res.statusCode = 403;
48
+ res.setHeader("content-type", "text/plain; charset=utf-8");
49
+ res.end(refusal);
50
+ return;
51
+ }
52
+ const token = await app.signIn(decodeURIComponent(who));
53
+ if (token === null) {
54
+ res.setHeader("content-type", "text/html; charset=utf-8");
55
+ res.end(`<p>nobody called ${who.replace(/[<&"]/g, "")}</p>`);
56
+ return;
57
+ }
58
+ const scheme = "encrypted" in req.socket && req.socket.encrypted === true ? "https" : "http";
59
+ res.statusCode = 302;
60
+ res.setHeader("set-cookie", chunkBZK37HR3_cjs.sessionCookies(`${scheme}://${req.headers.host ?? "localhost"}`, token, app.tokenKey !== void 0 ? { key: app.tokenKey } : {}));
61
+ res.setHeader("location", "/");
62
+ res.end();
63
+ }).catch(() => {
64
+ res.statusCode = 500;
65
+ res.end("the dev sign-in failed");
83
66
  });
84
- const screen = await draw(snapshot);
85
- const attributes = Object.entries(config.htmlAttributes?.(snapshot) ?? {}).map(([name, value]) => ` ${name}="${escapeAttribute(value)}"`).join("");
86
- const placed = template.replace(root, () => `${root.replace("></div>", () => `>${screen}</div>`)}${embedSnapshot(snapshot, principal, request.path)}`).replace(/<html([^>]*)>/, (whole, existing) => attributes === "" ? whole : `<html${existing}${attributes}>`);
87
- const head = document.headOf({ frame: () => snapshot.frame, canvasTree: (id) => snapshot.trees[id] ?? [] });
88
- for (const why of head?.refused ?? []) console.error(`[moss/document] "${request.path}" \u2014 ${why}`);
89
- const html = document.placeHead(placed, head?.elements, { ...config.site !== void 0 ? { site: config.site } : {}, path: request.path });
90
- return {
91
- html,
92
- ...head !== void 0 ? { head } : {},
93
- headers: documentHeaders(principal),
94
- drawn: true,
95
- principal,
96
- ...route !== void 0 ? { page: route.name } : {},
97
- live: snapshot.live,
98
- why: snapshot.why,
99
- drawnWith: snapshot.drawnWith,
100
- settled: snapshot.settled
101
- };
102
- } catch (error) {
103
- console.error(`[moss/document] "${request.path}" could not be drawn \u2014 serving it undrawn:`, error);
104
- return undrawn;
67
+ return;
105
68
  }
106
- };
107
- var SUBPROTOCOL = "nisc";
108
-
109
- // src/node.ts
110
- var MAX_MESSAGE_BYTES = 256 * 1024;
111
- var attachSocket = (httpServer, accept, path = "/socket", options) => {
112
- const wss = new ws.WebSocketServer({
113
- noServer: true,
114
- perMessageDeflate: true,
115
- maxPayload: MAX_MESSAGE_BYTES,
116
- // Of what a terminal offers, `nisc` is answered and nothing else: the
117
- // other entry is its token (socket.ts), which is not said back.
118
- handleProtocols: (offered) => offered.has(SUBPROTOCOL) ? SUBPROTOCOL : false
119
- });
120
- const answers = /* @__PURE__ */ new WeakMap();
121
- wss.on("headers", (headers, req) => {
122
- for (const cookie of answers.get(req) ?? []) headers.push(`Set-Cookie: ${cookie}`);
123
- });
124
- httpServer.on("upgrade", (req, socket, head) => {
125
- const url = req.url ?? "/";
126
- if (new URL(url, "http://nisc.local").pathname !== path) return;
127
- socket.on("error", () => {
69
+ if (owned.test(path$1)) {
70
+ void current().then(({ listener }) => listener(req, res)).catch(() => {
71
+ res.statusCode = 503;
72
+ res.end("the app server did not boot \u2014 see the dev server\u2019s log");
128
73
  });
129
- let ws;
130
- let answered = false;
131
- let cookies = [];
132
- const answer = () => {
133
- if (answered) return ws;
134
- answered = true;
135
- answers.set(req, cookies);
136
- wss.handleUpgrade(req, socket, head, (opened) => {
137
- ws = opened;
138
- opened.on("error", (error) => {
139
- console.warn(`[moss/node] a connection was closed on what it sent: ${error.message}`);
140
- });
141
- });
142
- return ws;
143
- };
144
- accept(url, {
145
- send: (text) => void answer()?.send(text),
146
- close: (code, reason) => void answer()?.close(code, reason),
147
- onMessage: (fn) => void answer()?.on("message", (data) => fn(String(data))),
148
- onClose: (fn) => {
149
- const opened = answer();
150
- if (opened === void 0) queueMicrotask(fn);
151
- else opened.on("close", () => fn());
152
- },
153
- upgrade: {
154
- offered: String(req.headers["sec-websocket-protocol"] ?? "").split(",").map((entry) => entry.trim()),
155
- origin: req.headers.origin ?? null,
156
- host: req.headers.host ?? null,
157
- cookie: req.headers.cookie ?? null,
158
- answer: (given) => void (cookies = given)
159
- }
160
- }).catch((error) => {
161
- console.error("[moss/node] a connection escaped accept:", error);
162
- try {
163
- answer()?.close(1011, "accept failed");
164
- } catch {
165
- }
74
+ return;
75
+ }
76
+ if (req.method !== "GET") {
77
+ next();
78
+ return;
79
+ }
80
+ void current().then(async ({ app }) => {
81
+ if (app.draw === void 0 || path$1 !== "/" && path$1 !== "/index.html" && app.server.page(path$1) === void 0) {
82
+ next();
83
+ return;
84
+ }
85
+ const template = await vite.transformIndexHtml(url, await promises.readFile(path.resolve(vite.config.root, options.index ?? "index.html"), "utf8"));
86
+ const page = await chunkBZK37HR3_cjs.renderDocument({
87
+ server: app.server,
88
+ template,
89
+ request: { path: path$1 === "/index.html" ? "/" : path$1, cookie: req.headers.cookie ?? null, host: req.headers.host ?? null },
90
+ draw: app.draw,
91
+ ...app.htmlAttributes !== void 0 ? { htmlAttributes: app.htmlAttributes } : {},
92
+ ...app.site !== void 0 ? { site: app.site } : {},
93
+ ...app.tokenKey !== void 0 ? { tokenKey: app.tokenKey } : {}
166
94
  });
167
- });
95
+ res.statusCode = 200;
96
+ res.setHeader("content-type", "text/html; charset=utf-8");
97
+ for (const [name, value] of Object.entries(page.headers)) res.setHeader(name, value);
98
+ res.end(page.html);
99
+ }).catch(() => next());
168
100
  };
169
- var MOSS_PATHS = /^\/(api|catalog|socket|operator|integrations)(\/|$)/;
170
-
171
- // src/vite.ts
172
- var DEFAULT_WATCH = /([\\/]src[\\/](app|server|db|ui)[\\/])|([\\/]nisc\.config\.[cm]?[jt]s$)/;
173
- var REBOOT_DEBOUNCE_MS = 200;
174
- var retire = async (booted) => {
175
- booted.app.server.socket.stop();
176
- await (booted.app.close ?? (() => booted.app.server.close()))();
177
- };
178
- var said = (error) => error instanceof Error ? error.message : String(error);
179
101
  var mossDev = (options) => ({
180
102
  name: "moss-dev",
181
103
  apply: "serve",
182
104
  configureServer: (vite) => {
183
105
  const label = options.label ?? "moss";
184
106
  const watch = options.watch ?? DEFAULT_WATCH;
185
- const owned = options.owned ?? MOSS_PATHS;
107
+ const owned = options.owned ?? chunk5U2ZZUI5_cjs.MOSS_PATHS;
186
108
  const load = (id) => vite.ssrLoadModule(id);
109
+ const httpServer = vite.httpServer;
187
110
  const boot = async () => {
188
111
  const app = await options.app(load);
189
- return { app, listener: nodeServer.getRequestListener(app.server.fetch) };
112
+ return { app, listener: nodeServer.getRequestListener(app.server.fetch), ...httpServer !== null ? { transport: chunk5U2ZZUI5_cjs.createTransport(app.server.socket) } : {} };
190
113
  };
191
114
  let current = boot();
192
115
  current.catch((error) => vite.config.logger.error(`[${label}] the app did not boot: ${said(error)}`, { timestamp: true }));
193
- if (vite.httpServer !== null) {
194
- attachSocket(
195
- vite.httpServer,
196
- Object.assign(async (url, connection) => (await current).app.server.socket(url, connection), {
197
- // whichever server is current owns the revalidation timer; this handler owns none
198
- stop: () => void current.then(({ app }) => app.server.socket.stop(), () => void 0)
199
- })
200
- );
116
+ if (httpServer !== null) {
117
+ chunk5U2ZZUI5_cjs.listenForUpgrades(httpServer, "/socket", (req, socket, head) => {
118
+ void current.then(
119
+ ({ transport }) => transport?.upgrade(req, socket, head),
120
+ () => socket.end("HTTP/1.1 503 Service Unavailable\r\nConnection: close\r\nContent-Length: 0\r\n\r\n")
121
+ );
122
+ });
201
123
  }
202
124
  let timer;
203
125
  const reboot = (file) => {
@@ -223,65 +145,7 @@ var mossDev = (options) => ({
223
145
  vite.watcher.on("add", reboot);
224
146
  vite.watcher.on("unlink", reboot);
225
147
  vite.httpServer?.once("close", () => void current.then(retire, () => void 0));
226
- vite.middlewares.use((req, res, next) => {
227
- const url = req.url ?? "/";
228
- const path$1 = url.split("?")[0] ?? "/";
229
- const who = /^\/dev\/as\/([\w.@%-]+)$/.exec(path$1)?.[1];
230
- if (who !== void 0) {
231
- void current.then(async ({ app }) => {
232
- if (app.signIn === void 0) {
233
- next();
234
- return;
235
- }
236
- const token = await app.signIn(decodeURIComponent(who));
237
- if (token === null) {
238
- res.setHeader("content-type", "text/html; charset=utf-8");
239
- res.end(`<p>nobody called ${who.replace(/[<&"]/g, "")}</p>`);
240
- return;
241
- }
242
- const scheme = req.socket.encrypted === true ? "https" : "http";
243
- res.statusCode = 302;
244
- res.setHeader("set-cookie", sessionCookies(`${scheme}://${req.headers.host ?? "localhost"}`, token, app.tokenKey !== void 0 ? { key: app.tokenKey } : {}));
245
- res.setHeader("location", "/");
246
- res.end();
247
- }).catch(() => {
248
- res.statusCode = 500;
249
- res.end("the dev sign-in failed");
250
- });
251
- return;
252
- }
253
- if (owned.test(path$1)) {
254
- void current.then(({ listener }) => listener(req, res)).catch(() => {
255
- res.statusCode = 503;
256
- res.end("the app server did not boot \u2014 see the dev server\u2019s log");
257
- });
258
- return;
259
- }
260
- if (req.method !== "GET") {
261
- next();
262
- return;
263
- }
264
- void current.then(async ({ app }) => {
265
- if (app.draw === void 0 || path$1 !== "/" && path$1 !== "/index.html" && app.server.page(path$1) === void 0) {
266
- next();
267
- return;
268
- }
269
- const template = await vite.transformIndexHtml(url, await promises.readFile(path.resolve(vite.config.root, options.index ?? "index.html"), "utf8"));
270
- const page = await renderDocument({
271
- server: app.server,
272
- template,
273
- request: { path: path$1 === "/index.html" ? "/" : path$1, cookie: req.headers.cookie ?? null, host: req.headers.host ?? null },
274
- draw: app.draw,
275
- ...app.htmlAttributes !== void 0 ? { htmlAttributes: app.htmlAttributes } : {},
276
- ...app.site !== void 0 ? { site: app.site } : {},
277
- ...app.tokenKey !== void 0 ? { tokenKey: app.tokenKey } : {}
278
- });
279
- res.statusCode = 200;
280
- res.setHeader("content-type", "text/html; charset=utf-8");
281
- for (const [name, value] of Object.entries(page.headers)) res.setHeader(name, value);
282
- res.end(page.html);
283
- }).catch(() => next());
284
- });
148
+ vite.middlewares.use(devMiddleware(vite, options, owned, () => current));
285
149
  }
286
150
  });
287
151