@hyperspan/framework 0.0.1
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.
- package/.prettierrc +7 -0
- package/README.md +83 -0
- package/bun.lockb +0 -0
- package/dist/index.js +468 -0
- package/dist/server.js +1935 -0
- package/package.json +38 -0
- package/src/app.ts +186 -0
- package/src/clientjs/hyperspan-client.ts +218 -0
- package/src/clientjs/idomorph.esm.js +854 -0
- package/src/clientjs/md5.js +176 -0
- package/src/document.ts +10 -0
- package/src/forms.ts +110 -0
- package/src/html.test.ts +69 -0
- package/src/html.ts +342 -0
- package/src/index.ts +14 -0
- package/src/server.ts +366 -0
- package/tsconfig.json +26 -0
package/.prettierrc
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# Hyperspan. Simple. Full-Stack. Streaming.
|
|
2
|
+
|
|
3
|
+
> [!NOTE] > **Hyperspan is still in the early stages of development, your feedback is appreciated.**
|
|
4
|
+
|
|
5
|
+
No JSX. No Virtual DOM. No hydration time. No nonsense. Just blazing fast HTML strings with reactive templates.
|
|
6
|
+
|
|
7
|
+
Hyperspan is a full-stack framework built with [Bun](https://bun.sh) that is focused on simplicity and perforamnce.
|
|
8
|
+
|
|
9
|
+
## Who Is Hyperspan For?
|
|
10
|
+
|
|
11
|
+
Hyperspan is best for websites, web applications, and APIs that need fast streaming and dynamic server responses. It is
|
|
12
|
+
great for performance critical applications and delivers no client-side JavaScript by default. This helps to ensure your
|
|
13
|
+
website stays fast and lightweight for end users as it grows over time, since all client JavaScript is explicitly
|
|
14
|
+
opt-in.
|
|
15
|
+
|
|
16
|
+
## Why Bun?
|
|
17
|
+
|
|
18
|
+
[Bun](https://bun.sh) is a Node-compatible runtime that offers better performance and built-in TypeScript support so
|
|
19
|
+
transpiling is not required for writing type-safe server-side code. Bun also offers some other extras like built-in
|
|
20
|
+
ultra fast testing utilities so extra dependencies are not required to write high-quality code.
|
|
21
|
+
|
|
22
|
+
## React vs. Hyperspan
|
|
23
|
+
|
|
24
|
+
Hyperspan is not a React replacement. React is a client-side library to build interactive JavaScript based UIs.
|
|
25
|
+
Hyperspan is a full-stack server-side framework built to deliver high-performance streaming and dynamic responses, and
|
|
26
|
+
does not deliver any JavaScript to the client by default.
|
|
27
|
+
|
|
28
|
+
Hyperspan can be best thought of as an _alternative_ to React-based frameworks like Next.js. It has all the same types
|
|
29
|
+
of things, like file-based page routes, API routes, middleware, components, etc. but Hyperspan has a very different
|
|
30
|
+
runtime and execution model. React-based frameworks ship all React components and code to the client by default,
|
|
31
|
+
resulting in surprisingly large JS bundle sizes that grow more over time as your site does. Hyperspan takes the opposite
|
|
32
|
+
approach, and does not ship ANY components to the client by default. Client components are available, but are explicitly
|
|
33
|
+
opt-in and should generally be used sparingly.
|
|
34
|
+
|
|
35
|
+
React is all JavaScript, all the time, delivered to the client. Hyperspan is mostly static content and markup delivered
|
|
36
|
+
to the client, with JavaScript sprinkles where needed.
|
|
37
|
+
|
|
38
|
+
## Philosophy
|
|
39
|
+
|
|
40
|
+
### P1: Server-Side First.
|
|
41
|
+
|
|
42
|
+
Current JavaScript frameworks ship everything to the browser by default and use non-intuitive ways to opt-out of this.
|
|
43
|
+
The first clue that this approach was backwards was when JavaScript bundle sizes started being measured in hundreds of
|
|
44
|
+
kilobytes to megabytes.
|
|
45
|
+
|
|
46
|
+
Keeping bundle sizes down in large projects over time can be challenging. A full stack framework should render as much
|
|
47
|
+
as possible on the server by default for better performance. Sending JavaScript code to the client and increasing bundle
|
|
48
|
+
sizes for all your users should be explicit and opt-in.
|
|
49
|
+
|
|
50
|
+
Modern frameworks make it too easy to _accidentally_ ship code to the client that you don't want. They often mix client
|
|
51
|
+
and server concerns in a way that is hard to understand and keep separate. They return cryptic and confusing errors when
|
|
52
|
+
you make mistakes around client and server boundaries.
|
|
53
|
+
|
|
54
|
+
We've lost the forrest from the trees. The complixity has become too much, and users are paying the price.
|
|
55
|
+
|
|
56
|
+
This is the way back for a full-stack framework:
|
|
57
|
+
|
|
58
|
+
- Server fetching and rendering by default.
|
|
59
|
+
- Minimal client-side JavaScript by default. Code that ships to the client should be explicit and opt-in.
|
|
60
|
+
- Most of the work and state should be maintained on the server. The framework should help make this easy.
|
|
61
|
+
|
|
62
|
+
### P2: Performance Oriented.
|
|
63
|
+
|
|
64
|
+
Server-side code is fast because it sends HTML strings to the client and lets the browser do its job - the job that is
|
|
65
|
+
was built specifically to do. There is no sense in typing up the JavaScript thread for rendering markup that can just be
|
|
66
|
+
sent directly.
|
|
67
|
+
|
|
68
|
+
You don't need a Virtual DOM or expensive reconciliation diff calculations to make interactive web sites or
|
|
69
|
+
applications. You just need a more intelligent template that knows the pieces that update.
|
|
70
|
+
|
|
71
|
+
HTML strings are the fastest and easiest way to update the parts of the DOM that change. We have known this since the
|
|
72
|
+
Web 1.0 days of AJAX and template partials. Modern JavaScript built-ins like Tagged Template Literals are an obvious
|
|
73
|
+
choice to do do the same thing today, and are built into the language.
|
|
74
|
+
|
|
75
|
+
### P3: Works the Same In Every Context.
|
|
76
|
+
|
|
77
|
+
All templates and components should be rendered asynchronously to allow you to do any other kind of asynchronous work in
|
|
78
|
+
them that you need to, in any context that you do it in. It's just JavaScript. Not special framework-flavored
|
|
79
|
+
JavaScript.
|
|
80
|
+
|
|
81
|
+
There should be no difference in how a template or component is rendered in one context vs. another. It should not
|
|
82
|
+
matter if a component runs on the client or the server, or fetches data or streams in. The core conceptual semantics
|
|
83
|
+
should be the same everywhere.
|
package/bun.lockb
ADDED
|
Binary file
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
7
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
8
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
9
|
+
for (let key of __getOwnPropNames(mod))
|
|
10
|
+
if (!__hasOwnProp.call(to, key))
|
|
11
|
+
__defProp(to, key, {
|
|
12
|
+
get: () => mod[key],
|
|
13
|
+
enumerable: true
|
|
14
|
+
});
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
18
|
+
|
|
19
|
+
// node_modules/escape-html/index.js
|
|
20
|
+
var require_escape_html = __commonJS((exports, module) => {
|
|
21
|
+
var escapeHtml = function(string) {
|
|
22
|
+
var str = "" + string;
|
|
23
|
+
var match = matchHtmlRegExp.exec(str);
|
|
24
|
+
if (!match) {
|
|
25
|
+
return str;
|
|
26
|
+
}
|
|
27
|
+
var escape;
|
|
28
|
+
var html = "";
|
|
29
|
+
var index = 0;
|
|
30
|
+
var lastIndex = 0;
|
|
31
|
+
for (index = match.index;index < str.length; index++) {
|
|
32
|
+
switch (str.charCodeAt(index)) {
|
|
33
|
+
case 34:
|
|
34
|
+
escape = """;
|
|
35
|
+
break;
|
|
36
|
+
case 38:
|
|
37
|
+
escape = "&";
|
|
38
|
+
break;
|
|
39
|
+
case 39:
|
|
40
|
+
escape = "'";
|
|
41
|
+
break;
|
|
42
|
+
case 60:
|
|
43
|
+
escape = "<";
|
|
44
|
+
break;
|
|
45
|
+
case 62:
|
|
46
|
+
escape = ">";
|
|
47
|
+
break;
|
|
48
|
+
default:
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (lastIndex !== index) {
|
|
52
|
+
html += str.substring(lastIndex, index);
|
|
53
|
+
}
|
|
54
|
+
lastIndex = index + 1;
|
|
55
|
+
html += escape;
|
|
56
|
+
}
|
|
57
|
+
return lastIndex !== index ? html + str.substring(lastIndex, index) : html;
|
|
58
|
+
};
|
|
59
|
+
/*!
|
|
60
|
+
* escape-html
|
|
61
|
+
* Copyright(c) 2012-2013 TJ Holowaychuk
|
|
62
|
+
* Copyright(c) 2015 Andreas Lubbe
|
|
63
|
+
* Copyright(c) 2015 Tiancheng "Timothy" Gu
|
|
64
|
+
* MIT Licensed
|
|
65
|
+
*/
|
|
66
|
+
var matchHtmlRegExp = /["'&<>]/;
|
|
67
|
+
module.exports = escapeHtml;
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// src/html.ts
|
|
71
|
+
var import_escape_html = __toESM(require_escape_html(), 1);
|
|
72
|
+
|
|
73
|
+
// src/clientjs/md5.js
|
|
74
|
+
var md5cycle = function(x, k) {
|
|
75
|
+
var a = x[0], b = x[1], c = x[2], d = x[3];
|
|
76
|
+
a = ff(a, b, c, d, k[0], 7, -680876936);
|
|
77
|
+
d = ff(d, a, b, c, k[1], 12, -389564586);
|
|
78
|
+
c = ff(c, d, a, b, k[2], 17, 606105819);
|
|
79
|
+
b = ff(b, c, d, a, k[3], 22, -1044525330);
|
|
80
|
+
a = ff(a, b, c, d, k[4], 7, -176418897);
|
|
81
|
+
d = ff(d, a, b, c, k[5], 12, 1200080426);
|
|
82
|
+
c = ff(c, d, a, b, k[6], 17, -1473231341);
|
|
83
|
+
b = ff(b, c, d, a, k[7], 22, -45705983);
|
|
84
|
+
a = ff(a, b, c, d, k[8], 7, 1770035416);
|
|
85
|
+
d = ff(d, a, b, c, k[9], 12, -1958414417);
|
|
86
|
+
c = ff(c, d, a, b, k[10], 17, -42063);
|
|
87
|
+
b = ff(b, c, d, a, k[11], 22, -1990404162);
|
|
88
|
+
a = ff(a, b, c, d, k[12], 7, 1804603682);
|
|
89
|
+
d = ff(d, a, b, c, k[13], 12, -40341101);
|
|
90
|
+
c = ff(c, d, a, b, k[14], 17, -1502002290);
|
|
91
|
+
b = ff(b, c, d, a, k[15], 22, 1236535329);
|
|
92
|
+
a = gg(a, b, c, d, k[1], 5, -165796510);
|
|
93
|
+
d = gg(d, a, b, c, k[6], 9, -1069501632);
|
|
94
|
+
c = gg(c, d, a, b, k[11], 14, 643717713);
|
|
95
|
+
b = gg(b, c, d, a, k[0], 20, -373897302);
|
|
96
|
+
a = gg(a, b, c, d, k[5], 5, -701558691);
|
|
97
|
+
d = gg(d, a, b, c, k[10], 9, 38016083);
|
|
98
|
+
c = gg(c, d, a, b, k[15], 14, -660478335);
|
|
99
|
+
b = gg(b, c, d, a, k[4], 20, -405537848);
|
|
100
|
+
a = gg(a, b, c, d, k[9], 5, 568446438);
|
|
101
|
+
d = gg(d, a, b, c, k[14], 9, -1019803690);
|
|
102
|
+
c = gg(c, d, a, b, k[3], 14, -187363961);
|
|
103
|
+
b = gg(b, c, d, a, k[8], 20, 1163531501);
|
|
104
|
+
a = gg(a, b, c, d, k[13], 5, -1444681467);
|
|
105
|
+
d = gg(d, a, b, c, k[2], 9, -51403784);
|
|
106
|
+
c = gg(c, d, a, b, k[7], 14, 1735328473);
|
|
107
|
+
b = gg(b, c, d, a, k[12], 20, -1926607734);
|
|
108
|
+
a = hh(a, b, c, d, k[5], 4, -378558);
|
|
109
|
+
d = hh(d, a, b, c, k[8], 11, -2022574463);
|
|
110
|
+
c = hh(c, d, a, b, k[11], 16, 1839030562);
|
|
111
|
+
b = hh(b, c, d, a, k[14], 23, -35309556);
|
|
112
|
+
a = hh(a, b, c, d, k[1], 4, -1530992060);
|
|
113
|
+
d = hh(d, a, b, c, k[4], 11, 1272893353);
|
|
114
|
+
c = hh(c, d, a, b, k[7], 16, -155497632);
|
|
115
|
+
b = hh(b, c, d, a, k[10], 23, -1094730640);
|
|
116
|
+
a = hh(a, b, c, d, k[13], 4, 681279174);
|
|
117
|
+
d = hh(d, a, b, c, k[0], 11, -358537222);
|
|
118
|
+
c = hh(c, d, a, b, k[3], 16, -722521979);
|
|
119
|
+
b = hh(b, c, d, a, k[6], 23, 76029189);
|
|
120
|
+
a = hh(a, b, c, d, k[9], 4, -640364487);
|
|
121
|
+
d = hh(d, a, b, c, k[12], 11, -421815835);
|
|
122
|
+
c = hh(c, d, a, b, k[15], 16, 530742520);
|
|
123
|
+
b = hh(b, c, d, a, k[2], 23, -995338651);
|
|
124
|
+
a = ii(a, b, c, d, k[0], 6, -198630844);
|
|
125
|
+
d = ii(d, a, b, c, k[7], 10, 1126891415);
|
|
126
|
+
c = ii(c, d, a, b, k[14], 15, -1416354905);
|
|
127
|
+
b = ii(b, c, d, a, k[5], 21, -57434055);
|
|
128
|
+
a = ii(a, b, c, d, k[12], 6, 1700485571);
|
|
129
|
+
d = ii(d, a, b, c, k[3], 10, -1894986606);
|
|
130
|
+
c = ii(c, d, a, b, k[10], 15, -1051523);
|
|
131
|
+
b = ii(b, c, d, a, k[1], 21, -2054922799);
|
|
132
|
+
a = ii(a, b, c, d, k[8], 6, 1873313359);
|
|
133
|
+
d = ii(d, a, b, c, k[15], 10, -30611744);
|
|
134
|
+
c = ii(c, d, a, b, k[6], 15, -1560198380);
|
|
135
|
+
b = ii(b, c, d, a, k[13], 21, 1309151649);
|
|
136
|
+
a = ii(a, b, c, d, k[4], 6, -145523070);
|
|
137
|
+
d = ii(d, a, b, c, k[11], 10, -1120210379);
|
|
138
|
+
c = ii(c, d, a, b, k[2], 15, 718787259);
|
|
139
|
+
b = ii(b, c, d, a, k[9], 21, -343485551);
|
|
140
|
+
x[0] = add32(a, x[0]);
|
|
141
|
+
x[1] = add32(b, x[1]);
|
|
142
|
+
x[2] = add32(c, x[2]);
|
|
143
|
+
x[3] = add32(d, x[3]);
|
|
144
|
+
};
|
|
145
|
+
var cmn = function(q, a, b, x, s, t) {
|
|
146
|
+
a = add32(add32(a, q), add32(x, t));
|
|
147
|
+
return add32(a << s | a >>> 32 - s, b);
|
|
148
|
+
};
|
|
149
|
+
var ff = function(a, b, c, d, x, s, t) {
|
|
150
|
+
return cmn(b & c | ~b & d, a, b, x, s, t);
|
|
151
|
+
};
|
|
152
|
+
var gg = function(a, b, c, d, x, s, t) {
|
|
153
|
+
return cmn(b & d | c & ~d, a, b, x, s, t);
|
|
154
|
+
};
|
|
155
|
+
var hh = function(a, b, c, d, x, s, t) {
|
|
156
|
+
return cmn(b ^ c ^ d, a, b, x, s, t);
|
|
157
|
+
};
|
|
158
|
+
var ii = function(a, b, c, d, x, s, t) {
|
|
159
|
+
return cmn(c ^ (b | ~d), a, b, x, s, t);
|
|
160
|
+
};
|
|
161
|
+
var md51 = function(s) {
|
|
162
|
+
var txt = "";
|
|
163
|
+
var n = s.length, state = [1732584193, -271733879, -1732584194, 271733878], i;
|
|
164
|
+
for (i = 64;i <= s.length; i += 64) {
|
|
165
|
+
md5cycle(state, md5blk(s.substring(i - 64, i)));
|
|
166
|
+
}
|
|
167
|
+
s = s.substring(i - 64);
|
|
168
|
+
var tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
|
169
|
+
for (i = 0;i < s.length; i++)
|
|
170
|
+
tail[i >> 2] |= s.charCodeAt(i) << (i % 4 << 3);
|
|
171
|
+
tail[i >> 2] |= 128 << (i % 4 << 3);
|
|
172
|
+
if (i > 55) {
|
|
173
|
+
md5cycle(state, tail);
|
|
174
|
+
for (i = 0;i < 16; i++)
|
|
175
|
+
tail[i] = 0;
|
|
176
|
+
}
|
|
177
|
+
tail[14] = n * 8;
|
|
178
|
+
md5cycle(state, tail);
|
|
179
|
+
return state;
|
|
180
|
+
};
|
|
181
|
+
var md5blk = function(s) {
|
|
182
|
+
var md5blks = [], i;
|
|
183
|
+
for (i = 0;i < 64; i += 4) {
|
|
184
|
+
md5blks[i >> 2] = s.charCodeAt(i) + (s.charCodeAt(i + 1) << 8) + (s.charCodeAt(i + 2) << 16) + (s.charCodeAt(i + 3) << 24);
|
|
185
|
+
}
|
|
186
|
+
return md5blks;
|
|
187
|
+
};
|
|
188
|
+
var rhex = function(n) {
|
|
189
|
+
var s = "", j = 0;
|
|
190
|
+
for (;j < 4; j++)
|
|
191
|
+
s += hex_chr[n >> j * 8 + 4 & 15] + hex_chr[n >> j * 8 & 15];
|
|
192
|
+
return s;
|
|
193
|
+
};
|
|
194
|
+
var hex = function(x) {
|
|
195
|
+
for (var i = 0;i < x.length; i++)
|
|
196
|
+
x[i] = rhex(x[i]);
|
|
197
|
+
return x.join("");
|
|
198
|
+
};
|
|
199
|
+
var add32 = function(a, b) {
|
|
200
|
+
return a + b & 4294967295;
|
|
201
|
+
};
|
|
202
|
+
function md5(s) {
|
|
203
|
+
return hex(md51(s));
|
|
204
|
+
}
|
|
205
|
+
var hex_chr = "0123456789abcdef".split("");
|
|
206
|
+
|
|
207
|
+
// src/html.ts
|
|
208
|
+
function html(strings, ...values) {
|
|
209
|
+
const content = [];
|
|
210
|
+
if (values.length === 0) {
|
|
211
|
+
content.push({ kind: "string_safe", value: strings.join("\n") });
|
|
212
|
+
return new HSTemplate(content);
|
|
213
|
+
}
|
|
214
|
+
let i = 0;
|
|
215
|
+
for (i = 0;i < values.length; i++) {
|
|
216
|
+
content.push({ kind: "string_safe", value: strings[i] });
|
|
217
|
+
let tValue = values[i] === undefined || values[i] === null || values[i] === "" ? "" : values[i];
|
|
218
|
+
if (!Array.isArray(tValue)) {
|
|
219
|
+
tValue = [tValue];
|
|
220
|
+
}
|
|
221
|
+
for (let j = 0;j < tValue.length; j++) {
|
|
222
|
+
content.push({ kind: _typeOf(tValue[j]), value: tValue[j] });
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
content.push({ kind: "string_safe", value: strings[i] });
|
|
226
|
+
return new HSTemplate(content);
|
|
227
|
+
}
|
|
228
|
+
async function* _render(obj, promises = [], { js }) {
|
|
229
|
+
let { kind, value } = obj;
|
|
230
|
+
let id = randomId();
|
|
231
|
+
if (!kind || !value) {
|
|
232
|
+
kind = _typeOf(obj);
|
|
233
|
+
value = obj;
|
|
234
|
+
}
|
|
235
|
+
if (value instanceof HSTemplate) {
|
|
236
|
+
yield* renderToStream(value);
|
|
237
|
+
} else if (typeof value.render !== "undefined") {
|
|
238
|
+
value.id = id;
|
|
239
|
+
yield await value.render();
|
|
240
|
+
} else if (value === undefined || value === null) {
|
|
241
|
+
yield "";
|
|
242
|
+
} else {
|
|
243
|
+
switch (kind) {
|
|
244
|
+
case "string":
|
|
245
|
+
yield import_escape_html.default(value);
|
|
246
|
+
break;
|
|
247
|
+
case "string_safe":
|
|
248
|
+
yield value;
|
|
249
|
+
break;
|
|
250
|
+
case "array":
|
|
251
|
+
yield* value;
|
|
252
|
+
break;
|
|
253
|
+
case "promise":
|
|
254
|
+
const promise = value.then((v) => {
|
|
255
|
+
return _render(v, promises, { js });
|
|
256
|
+
});
|
|
257
|
+
const pid = "async_" + id;
|
|
258
|
+
promises.push({ id: pid, pending: true, promise });
|
|
259
|
+
yield* renderToStream(html`<div id="${pid}">Loading...</div>`);
|
|
260
|
+
break;
|
|
261
|
+
case "function":
|
|
262
|
+
const fns = renderFunctionToString(value);
|
|
263
|
+
const fnId = "fn_" + md5(fns);
|
|
264
|
+
if (!IS_CLIENT || !window.hyperspan._fn.has(fnId)) {
|
|
265
|
+
js.push(`hyperspan.fn('${fnId}', ${fns});`);
|
|
266
|
+
}
|
|
267
|
+
yield `"hyperspan:${fnId}"`;
|
|
268
|
+
break;
|
|
269
|
+
case "json":
|
|
270
|
+
yield "";
|
|
271
|
+
break;
|
|
272
|
+
case "object":
|
|
273
|
+
if (typeof value.render === "function") {
|
|
274
|
+
yield value.render();
|
|
275
|
+
} else if (typeof value.toString === "function") {
|
|
276
|
+
yield value.toString();
|
|
277
|
+
} else {
|
|
278
|
+
yield value;
|
|
279
|
+
}
|
|
280
|
+
break;
|
|
281
|
+
case "generator":
|
|
282
|
+
yield* value;
|
|
283
|
+
break;
|
|
284
|
+
case "date":
|
|
285
|
+
yield value.toISOString();
|
|
286
|
+
break;
|
|
287
|
+
default:
|
|
288
|
+
yield String(value);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
async function* renderToStream(template) {
|
|
293
|
+
let promises = [];
|
|
294
|
+
let js = [];
|
|
295
|
+
if (typeof template === "string") {
|
|
296
|
+
return template;
|
|
297
|
+
}
|
|
298
|
+
for (let i = 0;i < template.content.length; i++) {
|
|
299
|
+
yield* _render(template.content[i], promises, { js });
|
|
300
|
+
}
|
|
301
|
+
while (promises.length > 0) {
|
|
302
|
+
const promisesToRun = promises.map((p) => p.promise.then((v) => {
|
|
303
|
+
return { id: p.id, pending: false, value: v, promise: null };
|
|
304
|
+
}));
|
|
305
|
+
const result = await Promise.race(promisesToRun);
|
|
306
|
+
yield* renderToStream(html`<template id="${result.id}_content">${result.value}</template>`);
|
|
307
|
+
promises = promises.filter((p) => {
|
|
308
|
+
return p.id !== result.id;
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
if (js.length !== 0) {
|
|
312
|
+
yield "<script>" + js.join("\n") + "</script>";
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
async function renderToString(template) {
|
|
316
|
+
let result = "";
|
|
317
|
+
for await (const chunk of renderToStream(template)) {
|
|
318
|
+
result += chunk;
|
|
319
|
+
}
|
|
320
|
+
return result;
|
|
321
|
+
}
|
|
322
|
+
function compressHTMLString(str) {
|
|
323
|
+
return str.replace(/(<(pre|script|style|textarea)[^]+?<\/\2)|(^|>)\s+|\s+(?=<|$)/g, "$1$3");
|
|
324
|
+
}
|
|
325
|
+
var randomId = function() {
|
|
326
|
+
return Math.random().toString(36).substring(2, 9);
|
|
327
|
+
};
|
|
328
|
+
function _typeOf(obj) {
|
|
329
|
+
if (obj instanceof Promise)
|
|
330
|
+
return "promise";
|
|
331
|
+
if (obj instanceof Date)
|
|
332
|
+
return "date";
|
|
333
|
+
if (obj instanceof String)
|
|
334
|
+
return "string";
|
|
335
|
+
if (obj instanceof Number)
|
|
336
|
+
return "number";
|
|
337
|
+
if (obj instanceof Boolean)
|
|
338
|
+
return "boolean";
|
|
339
|
+
if (obj instanceof Function)
|
|
340
|
+
return "function";
|
|
341
|
+
if (Array.isArray(obj))
|
|
342
|
+
return "array";
|
|
343
|
+
if (Number.isNaN(obj))
|
|
344
|
+
return "nan";
|
|
345
|
+
if (obj === undefined)
|
|
346
|
+
return "undefined";
|
|
347
|
+
if (obj === null)
|
|
348
|
+
return "null";
|
|
349
|
+
if (isGenerator(obj))
|
|
350
|
+
return "generator";
|
|
351
|
+
if (isPlainObject(obj))
|
|
352
|
+
return "json";
|
|
353
|
+
return typeof obj;
|
|
354
|
+
}
|
|
355
|
+
var isGenerator = function(obj) {
|
|
356
|
+
return obj && typeof obj.next == "function" && typeof obj.throw == "function";
|
|
357
|
+
};
|
|
358
|
+
var isPlainObject = function(val) {
|
|
359
|
+
return Object == val.constructor;
|
|
360
|
+
};
|
|
361
|
+
function clientComponent(id, wc) {
|
|
362
|
+
const comp = {
|
|
363
|
+
...wc,
|
|
364
|
+
state: wc.state || {},
|
|
365
|
+
id,
|
|
366
|
+
randomId() {
|
|
367
|
+
return Math.random().toString(36).substring(2, 9);
|
|
368
|
+
},
|
|
369
|
+
setState(fn) {
|
|
370
|
+
try {
|
|
371
|
+
const val = typeof fn === "function" ? fn(this.state) : fn;
|
|
372
|
+
this.state = val;
|
|
373
|
+
const el = document.getElementById(this.id);
|
|
374
|
+
if (el) {
|
|
375
|
+
el.dataset.state = JSON.stringify(val);
|
|
376
|
+
}
|
|
377
|
+
} catch (e) {
|
|
378
|
+
console.error(e);
|
|
379
|
+
}
|
|
380
|
+
return this.state;
|
|
381
|
+
},
|
|
382
|
+
mergeState(newState) {
|
|
383
|
+
return this.setState(Object.assign(this.state, newState));
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
if (typeof window !== "undefined") {
|
|
387
|
+
window.hyperspan.wc.set(id, comp);
|
|
388
|
+
}
|
|
389
|
+
return (attrs, state) => {
|
|
390
|
+
const _state = Object.assign({}, comp.state, state || {});
|
|
391
|
+
return html`
|
|
392
|
+
<script>
|
|
393
|
+
${html.raw(renderObjectToLiteralString(comp))};
|
|
394
|
+
</script>
|
|
395
|
+
<hs-wc id="${attrs?.id || id}" data-state="${JSON.stringify(_state)}"></hs-wc>
|
|
396
|
+
`;
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
function renderFunctionToString(fn) {
|
|
400
|
+
let fns = fn.toString();
|
|
401
|
+
const firstLine = fns.split("\n")[0];
|
|
402
|
+
const isFatArrow = firstLine.includes("=>");
|
|
403
|
+
const isAsync = firstLine.includes("async");
|
|
404
|
+
const hasFunctionWord = firstLine.includes("function");
|
|
405
|
+
if (isFatArrow) {
|
|
406
|
+
fns = "function (...args) { return (" + fns + ")(..args); }";
|
|
407
|
+
} else {
|
|
408
|
+
if (!hasFunctionWord) {
|
|
409
|
+
fns = "function " + fns;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
if (isAsync) {
|
|
413
|
+
fns = "async " + fns.replace("async ", "");
|
|
414
|
+
}
|
|
415
|
+
return fns;
|
|
416
|
+
}
|
|
417
|
+
var renderObjectToLiteralString = function(obj) {
|
|
418
|
+
const lines = [];
|
|
419
|
+
let str = 'hyperspan.wc.set("' + obj.id + '", {\n';
|
|
420
|
+
for (const prop in obj) {
|
|
421
|
+
const kind = _typeOf(obj[prop]);
|
|
422
|
+
let val = obj[prop];
|
|
423
|
+
switch (kind) {
|
|
424
|
+
case "string":
|
|
425
|
+
lines.push([prop, ": ", '"' + val + '"']);
|
|
426
|
+
break;
|
|
427
|
+
case "object":
|
|
428
|
+
case "json":
|
|
429
|
+
lines.push([prop, ": ", "JSON.parse('" + JSON.stringify(val) + "')"]);
|
|
430
|
+
break;
|
|
431
|
+
case "function":
|
|
432
|
+
const fn = val.toString();
|
|
433
|
+
const isFatArrow = fn.split("\n")[0].includes("=>");
|
|
434
|
+
if (isFatArrow) {
|
|
435
|
+
lines.push([prop, ": ", fn]);
|
|
436
|
+
} else {
|
|
437
|
+
lines.push([fn]);
|
|
438
|
+
}
|
|
439
|
+
break;
|
|
440
|
+
default:
|
|
441
|
+
lines.push([prop, ": ", val]);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
str += lines.map((line) => line.join("") + ",").join("\n");
|
|
445
|
+
str += "\n})";
|
|
446
|
+
return str;
|
|
447
|
+
};
|
|
448
|
+
var IS_CLIENT = typeof window !== "undefined";
|
|
449
|
+
|
|
450
|
+
class HSTemplate {
|
|
451
|
+
__hsTemplate = true;
|
|
452
|
+
content;
|
|
453
|
+
constructor(content) {
|
|
454
|
+
this.content = content;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
html.raw = (value) => {
|
|
458
|
+
return new HSTemplate([{ kind: "string_safe", value }]);
|
|
459
|
+
};
|
|
460
|
+
export {
|
|
461
|
+
renderToString,
|
|
462
|
+
renderToStream,
|
|
463
|
+
html,
|
|
464
|
+
compressHTMLString,
|
|
465
|
+
clientComponent,
|
|
466
|
+
_typeOf,
|
|
467
|
+
HSTemplate
|
|
468
|
+
};
|