server.cjs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var solidJs = require('solid-js');
  4. var stream = require('stream');
  5. const booleans = ["allowfullscreen", "allowpaymentrequest", "async", "autofocus", "autoplay", "checked", "controls", "default", "disabled", "formnovalidate", "hidden", "ismap", "itemscope", "loop", "multiple", "muted", "nomodule", "novalidate", "open", "playsinline", "readonly", "required", "reversed", "seamless", "selected", "truespeed"];
  6. const BooleanAttributes = new Set(booleans);
  7. const Aliases = {
  8. className: "class",
  9. htmlFor: "for"
  10. };
  11. function renderToString(code, options = {}) {
  12. solidJs.sharedConfig.context = {
  13. id: "",
  14. count: 0
  15. };
  16. return {
  17. html: resolveSSRNode(code()),
  18. script: generateHydrationScript(options)
  19. };
  20. }
  21. function renderToStringAsync(code, options = {}) {
  22. options = {
  23. timeoutMs: 30000,
  24. ...options
  25. };
  26. let resources;
  27. solidJs.sharedConfig.context = {
  28. id: "",
  29. count: 0,
  30. resources: resources = {},
  31. suspense: {},
  32. async: true
  33. };
  34. const timeout = new Promise((_, reject) => setTimeout(() => reject("renderToString timed out"), options.timeoutMs));
  35. return Promise.race([solidJs.awaitSuspense(code), timeout]).then(res => {
  36. return {
  37. html: resolveSSRNode(res),
  38. script: generateHydrationScript({
  39. resources,
  40. ...options
  41. })
  42. };
  43. });
  44. }
  45. function renderToNodeStream(code, options = {}) {
  46. const stream$1 = new stream.Readable({
  47. read() {}
  48. });
  49. solidJs.sharedConfig.context = {
  50. id: "",
  51. count: 0,
  52. streaming: true,
  53. suspense: {}
  54. };
  55. let count = 0,
  56. completed = 0,
  57. checkEnd = () => {
  58. if (completed === count) stream$1.push(null);
  59. };
  60. solidJs.sharedConfig.context.writeResource = (id, p) => {
  61. count++;
  62. Promise.resolve().then(() => stream$1.push(`<script${options.nonce ? ` nonce="${options.nonce}"` : ""}>_$HYDRATION.startResource("${id}")</script>`));
  63. p.then(d => {
  64. stream$1.push(`<script${options.nonce ? ` nonce="${options.nonce}"` : ""}>_$HYDRATION.resolveResource("${id}", ${(JSON.stringify(d) || "undefined").replace(/'/g, "\\'").replace(/\\\"/g, '\\\\\\"')})</script>`);
  65. ++completed && checkEnd();
  66. });
  67. };
  68. stream$1.push(resolveSSRNode(code()));
  69. setTimeout(checkEnd);
  70. return {
  71. stream: stream$1,
  72. script: generateHydrationScript({
  73. streaming: true,
  74. ...options
  75. })
  76. };
  77. }
  78. function renderToWebStream(code, options = {}) {
  79. let checkEnd;
  80. const tmp = [];
  81. const encoder = new TextEncoder();
  82. const done = new Promise(resolve => {
  83. checkEnd = () => {
  84. if (completed === count) resolve();
  85. };
  86. });
  87. solidJs.sharedConfig.context = {
  88. id: "",
  89. count: 0,
  90. streaming: true
  91. };
  92. let count = 0,
  93. completed = 0,
  94. writer = {
  95. write(payload) {
  96. tmp.push(payload);
  97. }
  98. };
  99. solidJs.sharedConfig.context.writeResource = (id, p) => {
  100. count++;
  101. Promise.resolve().then(() => writer.write(encoder.encode(`<script${options.nonce ? ` nonce="${options.nonce}"` : ""}>_$HYDRATION.startResource("${id}")</script>`)));
  102. p.then(d => {
  103. writer.write(encoder.encode(`<script${options.nonce ? ` nonce="${options.nonce}"` : ""}>_$HYDRATION.resolveResource("${id}", ${(JSON.stringify(d) || "undefined").replace(/'/g, "\\'").replace(/\\\"/g, '\\\\\\"')})</script>`));
  104. ++completed && checkEnd();
  105. });
  106. };
  107. writer.write(encoder.encode(resolveSSRNode(code())));
  108. return {
  109. writeTo(w) {
  110. writer = w;
  111. tmp.map(chunk => writer.write(chunk));
  112. setTimeout(checkEnd);
  113. return done;
  114. },
  115. script: generateHydrationScript({
  116. streaming: true,
  117. ...options
  118. })
  119. };
  120. }
  121. function ssr(t, ...nodes) {
  122. if (nodes.length) {
  123. let result = "";
  124. for (let i = 0; i < t.length; i++) {
  125. result += t[i];
  126. const node = nodes[i];
  127. if (node !== undefined) result += resolveSSRNode(node);
  128. }
  129. t = result;
  130. }
  131. return {
  132. t
  133. };
  134. }
  135. function ssrClassList(value) {
  136. let classKeys = Object.keys(value),
  137. result = "";
  138. for (let i = 0, len = classKeys.length; i < len; i++) {
  139. const key = classKeys[i],
  140. classValue = !!value[key];
  141. if (!key || !classValue) continue;
  142. i && (result += " ");
  143. result += key;
  144. }
  145. return result;
  146. }
  147. function ssrStyle(value) {
  148. if (typeof value === "string") return value;
  149. let result = "";
  150. const k = Object.keys(value);
  151. for (let i = 0; i < k.length; i++) {
  152. const s = k[i];
  153. if (i) result += ";";
  154. result += `${s}:${escape(value[s], true)}`;
  155. }
  156. return result;
  157. }
  158. function ssrSpread(props, isSVG, skipChildren) {
  159. if (typeof props === "function") props = props();
  160. const keys = Object.keys(props);
  161. let result = "";
  162. for (let i = 0; i < keys.length; i++) {
  163. const prop = keys[i];
  164. if (prop === "children") {
  165. !skipChildren && console.warn(`SSR currently does not support spread children.`);
  166. continue;
  167. }
  168. const value = props[prop];
  169. if (prop === "style") {
  170. result += `style="${ssrStyle(value)}"`;
  171. } else if (prop === "classList") {
  172. result += `class="${ssrClassList(value)}"`;
  173. } else if (BooleanAttributes.has(prop)) {
  174. if (value) result += prop;else continue;
  175. } else {
  176. result += `${Aliases[prop] || prop}="${escape(value, true)}"`;
  177. }
  178. if (i !== keys.length - 1) result += " ";
  179. }
  180. return result;
  181. }
  182. function ssrBoolean(key, value) {
  183. return value ? " " + key : "";
  184. }
  185. function escape(s, attr) {
  186. const t = typeof s;
  187. if (t !== "string") {
  188. if (attr && t === "boolean") return String(s);
  189. return s;
  190. }
  191. const delim = attr ? '"' : "<";
  192. const escDelim = attr ? "&quot;" : "&lt;";
  193. let iDelim = s.indexOf(delim);
  194. let iAmp = s.indexOf("&");
  195. if (iDelim < 0 && iAmp < 0) return s;
  196. let left = 0,
  197. out = "";
  198. while (iDelim >= 0 && iAmp >= 0) {
  199. if (iDelim < iAmp) {
  200. if (left < iDelim) out += s.substring(left, iDelim);
  201. out += escDelim;
  202. left = iDelim + 1;
  203. iDelim = s.indexOf(delim, left);
  204. } else {
  205. if (left < iAmp) out += s.substring(left, iAmp);
  206. out += "&amp;";
  207. left = iAmp + 1;
  208. iAmp = s.indexOf("&", left);
  209. }
  210. }
  211. if (iDelim >= 0) {
  212. do {
  213. if (left < iDelim) out += s.substring(left, iDelim);
  214. out += escDelim;
  215. left = iDelim + 1;
  216. iDelim = s.indexOf(delim, left);
  217. } while (iDelim >= 0);
  218. } else while (iAmp >= 0) {
  219. if (left < iAmp) out += s.substring(left, iAmp);
  220. out += "&amp;";
  221. left = iAmp + 1;
  222. iAmp = s.indexOf("&", left);
  223. }
  224. return left < s.length ? out + s.substring(left) : out;
  225. }
  226. function resolveSSRNode(node) {
  227. const t = typeof node;
  228. if (t === "string") return node;
  229. if (node == null || t === "boolean") return "";
  230. if (Array.isArray(node)) return node.map(resolveSSRNode).join("");
  231. if (t === "object") return resolveSSRNode(node.t);
  232. if (t === "function") return resolveSSRNode(node());
  233. return String(node);
  234. }
  235. function getHydrationKey() {
  236. const hydrate = solidJs.sharedConfig.context;
  237. return `${hydrate.id}${hydrate.count++}`;
  238. }
  239. function generateHydrationScript({
  240. eventNames = ["click", "input"],
  241. streaming,
  242. resources,
  243. nonce
  244. } = {}) {
  245. let s = `<script${nonce ? ` nonce="${nonce}"` : ""}>(()=>{_$HYDRATION={events:[],completed:new WeakSet};const t=e=>e&&e.hasAttribute&&(e.hasAttribute("data-hk")&&e||t(e.host&&e.host instanceof Node?e.host:e.parentNode)),e=e=>{let o=e.composedPath&&e.composedPath()[0]||e.target,s=t(o);s&&!_$HYDRATION.completed.has(s)&&_$HYDRATION.events.push([s,e])};["${eventNames.join('","')}"].forEach(t=>document.addEventListener(t,e))})();`;
  246. if (streaming) {
  247. s += `(()=>{const e=_$HYDRATION,o={};e.startResource=e=>{let r;o[e]=[new Promise(e=>r=e),r]},e.resolveResource=(e,r)=>{const n=o[e];if(!n)return o[e]=[r];n[1](r)},e.loadResource=e=>{const r=o[e];if(r)return r[0]}})();`;
  248. }
  249. if (resources) s += `_$HYDRATION.resources = JSON.parse('${JSON.stringify(Object.keys(resources).reduce((r, k) => {
  250. r[k] = resources[k].data;
  251. return r;
  252. }, {})).replace(/'/g, "\\'").replace(/\\\"/g, '\\\\\\"')}');`;
  253. return s + `</script>`;
  254. }
  255. const isServer = true;
  256. function spread() {}
  257. function Dynamic(props) {
  258. const [p, others] = solidJs.splitProps(props, ["component"]);
  259. const comp = p.component,
  260. t = typeof comp;
  261. if (comp) {
  262. if (t === "function") return comp(others);else if (t === "string") {
  263. const [local, sOthers] = solidJs.splitProps(others, ["children"]);
  264. return ssr([`<${comp} `, ">", `</${comp}>`], ssrSpread(sOthers), local.children || "");
  265. }
  266. }
  267. }
  268. function Portal(props) {
  269. return "";
  270. }
  271. Object.defineProperty(exports, 'ErrorBoundary', {
  272. enumerable: true,
  273. get: function () {
  274. return solidJs.ErrorBoundary;
  275. }
  276. });
  277. Object.defineProperty(exports, 'For', {
  278. enumerable: true,
  279. get: function () {
  280. return solidJs.For;
  281. }
  282. });
  283. Object.defineProperty(exports, 'Index', {
  284. enumerable: true,
  285. get: function () {
  286. return solidJs.Index;
  287. }
  288. });
  289. Object.defineProperty(exports, 'Match', {
  290. enumerable: true,
  291. get: function () {
  292. return solidJs.Match;
  293. }
  294. });
  295. Object.defineProperty(exports, 'Show', {
  296. enumerable: true,
  297. get: function () {
  298. return solidJs.Show;
  299. }
  300. });
  301. Object.defineProperty(exports, 'Suspense', {
  302. enumerable: true,
  303. get: function () {
  304. return solidJs.Suspense;
  305. }
  306. });
  307. Object.defineProperty(exports, 'SuspenseList', {
  308. enumerable: true,
  309. get: function () {
  310. return solidJs.SuspenseList;
  311. }
  312. });
  313. Object.defineProperty(exports, 'Switch', {
  314. enumerable: true,
  315. get: function () {
  316. return solidJs.Switch;
  317. }
  318. });
  319. Object.defineProperty(exports, 'createComponent', {
  320. enumerable: true,
  321. get: function () {
  322. return solidJs.createComponent;
  323. }
  324. });
  325. Object.defineProperty(exports, 'mergeProps', {
  326. enumerable: true,
  327. get: function () {
  328. return solidJs.mergeProps;
  329. }
  330. });
  331. exports.Dynamic = Dynamic;
  332. exports.Portal = Portal;
  333. exports.escape = escape;
  334. exports.getHydrationKey = getHydrationKey;
  335. exports.isServer = isServer;
  336. exports.renderToNodeStream = renderToNodeStream;
  337. exports.renderToString = renderToString;
  338. exports.renderToStringAsync = renderToStringAsync;
  339. exports.renderToWebStream = renderToWebStream;
  340. exports.resolveSSRNode = resolveSSRNode;
  341. exports.spread = spread;
  342. exports.ssr = ssr;
  343. exports.ssrBoolean = ssrBoolean;
  344. exports.ssrClassList = ssrClassList;
  345. exports.ssrSpread = ssrSpread;
  346. exports.ssrStyle = ssrStyle;