@hyperspan/framework 0.0.3 → 0.1.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.
package/dist/index.js DELETED
@@ -1,471 +0,0 @@
1
- var __create = Object.create;
2
- var __getProtoOf = Object.getPrototypeOf;
3
- var __defProp = Object.defineProperty;
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
- function escapeHtml(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 = "&quot;";
35
- break;
36
- case 38:
37
- escape = "&amp;";
38
- break;
39
- case 39:
40
- escape = "&#39;";
41
- break;
42
- case 60:
43
- escape = "&lt;";
44
- break;
45
- case 62:
46
- escape = "&gt;";
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
- function md5cycle(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
- function cmn(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
- function ff(a, b, c, d, x, s, t) {
150
- return cmn(b & c | ~b & d, a, b, x, s, t);
151
- }
152
- function gg(a, b, c, d, x, s, t) {
153
- return cmn(b & d | c & ~d, a, b, x, s, t);
154
- }
155
- function hh(a, b, c, d, x, s, t) {
156
- return cmn(b ^ c ^ d, a, b, x, s, t);
157
- }
158
- function ii(a, b, c, d, x, s, t) {
159
- return cmn(c ^ (b | ~d), a, b, x, s, t);
160
- }
161
- function md51(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
- function md5blk(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
- function rhex(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
- function hex(x) {
195
- for (var i = 0;i < x.length; i++)
196
- x[i] = rhex(x[i]);
197
- return x.join("");
198
- }
199
- function add32(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 || value.__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 "number":
273
- yield String(value);
274
- break;
275
- case "object":
276
- if (typeof value.render === "function") {
277
- yield value.render();
278
- } else if (typeof value.toString === "function") {
279
- yield value.toString();
280
- } else {
281
- yield value;
282
- }
283
- break;
284
- case "generator":
285
- yield* value;
286
- break;
287
- case "date":
288
- yield value.toISOString();
289
- break;
290
- default:
291
- yield String(value);
292
- }
293
- }
294
- }
295
- async function* renderToStream(template) {
296
- let promises = [];
297
- let js = [];
298
- if (typeof template === "string") {
299
- return template;
300
- }
301
- for (let i = 0;i < template.content.length; i++) {
302
- yield* _render(template.content[i], promises, { js });
303
- }
304
- while (promises.length > 0) {
305
- const promisesToRun = promises.map((p) => p.promise.then((v) => {
306
- return { id: p.id, pending: false, value: v, promise: null };
307
- }));
308
- const result = await Promise.race(promisesToRun);
309
- yield* renderToStream(html`<template id="${result.id}_content">${result.value}</template>`);
310
- promises = promises.filter((p) => {
311
- return p.id !== result.id;
312
- });
313
- }
314
- if (js.length !== 0) {
315
- yield "<script>" + js.join("\n") + "</script>";
316
- }
317
- }
318
- async function renderToString(template) {
319
- let result = "";
320
- for await (const chunk of renderToStream(template)) {
321
- result += chunk;
322
- }
323
- return result;
324
- }
325
- function compressHTMLString(str) {
326
- return str.replace(/(<(pre|script|style|textarea)[^]+?<\/\2)|(^|>)\s+|\s+(?=<|$)/g, "$1$3");
327
- }
328
- function randomId() {
329
- return Math.random().toString(36).substring(2, 9);
330
- }
331
- function _typeOf(obj) {
332
- if (obj instanceof Promise)
333
- return "promise";
334
- if (obj instanceof Date)
335
- return "date";
336
- if (obj instanceof String)
337
- return "string";
338
- if (obj instanceof Number)
339
- return "number";
340
- if (obj instanceof Boolean)
341
- return "boolean";
342
- if (obj instanceof Function)
343
- return "function";
344
- if (Array.isArray(obj))
345
- return "array";
346
- if (Number.isNaN(obj))
347
- return "nan";
348
- if (obj === undefined)
349
- return "undefined";
350
- if (obj === null)
351
- return "null";
352
- if (isGenerator(obj))
353
- return "generator";
354
- if (isPlainObject(obj))
355
- return "json";
356
- return typeof obj;
357
- }
358
- function isGenerator(obj) {
359
- return obj && typeof obj.next == "function" && typeof obj.throw == "function";
360
- }
361
- function isPlainObject(val) {
362
- return Object == val.constructor;
363
- }
364
- function clientComponent(id, wc) {
365
- const comp = {
366
- ...wc,
367
- state: wc.state || {},
368
- id,
369
- randomId() {
370
- return Math.random().toString(36).substring(2, 9);
371
- },
372
- setState(fn) {
373
- try {
374
- const val = typeof fn === "function" ? fn(this.state) : fn;
375
- this.state = val;
376
- const el = document.getElementById(this.id);
377
- if (el) {
378
- el.dataset.state = JSON.stringify(val);
379
- }
380
- } catch (e) {
381
- console.error(e);
382
- }
383
- return this.state;
384
- },
385
- mergeState(newState) {
386
- return this.setState(Object.assign(this.state, newState));
387
- }
388
- };
389
- if (typeof window !== "undefined") {
390
- window.hyperspan.wc.set(id, comp);
391
- }
392
- return (attrs, state) => {
393
- const _state = Object.assign({}, comp.state, state || {});
394
- return html`
395
- <script>
396
- ${html.raw(renderObjectToLiteralString(comp))};
397
- </script>
398
- <hs-wc id="${attrs?.id || id}" data-state="${JSON.stringify(_state)}"></hs-wc>
399
- `;
400
- };
401
- }
402
- function renderFunctionToString(fn) {
403
- let fns = fn.toString();
404
- const firstLine = fns.split("\n")[0];
405
- const isFatArrow = firstLine.includes("=>");
406
- const isAsync = firstLine.includes("async");
407
- const hasFunctionWord = firstLine.includes("function");
408
- if (isFatArrow) {
409
- fns = "function (...args) { return (" + fns + ")(..args); }";
410
- } else {
411
- if (!hasFunctionWord) {
412
- fns = "function " + fns;
413
- }
414
- }
415
- if (isAsync) {
416
- fns = "async " + fns.replace("async ", "");
417
- }
418
- return fns;
419
- }
420
- function renderObjectToLiteralString(obj) {
421
- const lines = [];
422
- let str = 'hyperspan.wc.set("' + obj.id + '", {\n';
423
- for (const prop in obj) {
424
- const kind = _typeOf(obj[prop]);
425
- let val = obj[prop];
426
- switch (kind) {
427
- case "string":
428
- lines.push([prop, ": ", '"' + val + '"']);
429
- break;
430
- case "object":
431
- case "json":
432
- lines.push([prop, ": ", "JSON.parse('" + JSON.stringify(val) + "')"]);
433
- break;
434
- case "function":
435
- const fn = val.toString();
436
- const isFatArrow = fn.split("\n")[0].includes("=>");
437
- if (isFatArrow) {
438
- lines.push([prop, ": ", fn]);
439
- } else {
440
- lines.push([fn]);
441
- }
442
- break;
443
- default:
444
- lines.push([prop, ": ", val]);
445
- }
446
- }
447
- str += lines.map((line) => line.join("") + ",").join("\n");
448
- str += "\n})";
449
- return str;
450
- }
451
- var IS_CLIENT = typeof window !== "undefined";
452
-
453
- class HSTemplate {
454
- __hsTemplate = true;
455
- content;
456
- constructor(content) {
457
- this.content = content;
458
- }
459
- }
460
- html.raw = (value) => {
461
- return new HSTemplate([{ kind: "string_safe", value }]);
462
- };
463
- export {
464
- renderToString,
465
- renderToStream,
466
- html,
467
- compressHTMLString,
468
- clientComponent,
469
- _typeOf,
470
- HSTemplate
471
- };
package/dist/server.d.ts DELETED
@@ -1,109 +0,0 @@
1
- // Generated by dts-bundle-generator v9.5.1
2
-
3
- import Headers$1 from '@mjackson/headers';
4
-
5
- declare class HSTemplate {
6
- __hsTemplate: boolean;
7
- content: any[];
8
- constructor(content: any[]);
9
- }
10
- export type THTTPMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
11
- declare class HSRequestContext {
12
- req: Request;
13
- locals: Record<string, any>;
14
- headers: Headers$1;
15
- route: {
16
- params: Record<string, string>;
17
- query: URLSearchParams;
18
- };
19
- constructor(req: Request, params?: Record<string, string>);
20
- /**
21
- * Response helper
22
- * Merges a Response object while preserving all headers added in context/middleware
23
- */
24
- resMerge(res: Response): Response;
25
- /**
26
- * HTML response helper
27
- * Preserves all headers added in context/middleware
28
- */
29
- html(content: string, options?: ResponseInit): Response;
30
- /**
31
- * JSON response helper
32
- * Preserves all headers added in context/middleware
33
- */
34
- json(content: any, options?: ResponseInit): Response;
35
- notFound(msg?: string): Response;
36
- }
37
- export type THSRouteHandler = (context: HSRequestContext) => (Response | null | void) | Promise<Response | null | void>;
38
- declare class HSApp {
39
- private _router;
40
- private _mw;
41
- _defaultRoute: THSRouteHandler;
42
- constructor();
43
- get(path: string, handler: THSRouteHandler): this;
44
- post(path: string, handler: THSRouteHandler): this;
45
- put(path: string, handler: THSRouteHandler): this;
46
- delete(path: string, handler: THSRouteHandler): this;
47
- all(path: string, handler: THSRouteHandler): this;
48
- addRoute(methods: THTTPMethod[], path: string, handler: THSRouteHandler): this;
49
- defaultRoute(handler: THSRouteHandler): void;
50
- private _route;
51
- run(req: Request): Promise<Response>;
52
- }
53
- export declare const IS_PROD: boolean;
54
- /**
55
- * Run route from file
56
- */
57
- export declare function runFileRoute(routeFile: string, context: HSRequestContext): Promise<any>;
58
- export type THSServerConfig = {
59
- appDir: string;
60
- staticFileRoot: string;
61
- beforeRoutesAdded?: (app: HSApp) => void;
62
- afterRoutesAdded?: (app: HSApp) => void;
63
- };
64
- export type THSRouteMap = {
65
- file: string;
66
- route: string;
67
- params: string[];
68
- };
69
- export declare function buildRoutes(config: THSServerConfig): Promise<THSRouteMap[]>;
70
- /**
71
- * Create and start Bun HTTP server
72
- */
73
- export declare function createServer(config: THSServerConfig): Promise<HSApp>;
74
- /**
75
- * Build client JS for end users (minimal JS for Hyperspan to work)
76
- */
77
- export declare let clientJSFile: string;
78
- export declare function buildClientJS(): Promise<string>;
79
- /**
80
- * Find client CSS file built for end users
81
- * @TODO: Build this in code here vs. relying on tailwindcss CLI tool from package scripts
82
- */
83
- export declare let clientCSSFile: string;
84
- export declare function buildClientCSS(): Promise<string | undefined>;
85
- /**
86
- * Streaming HTML Response
87
- */
88
- export declare class StreamResponse {
89
- constructor(iterator: AsyncIterator<unknown>, options?: {});
90
- }
91
- /**
92
- * Does what it says on the tin...
93
- */
94
- export declare function createReadableStreamFromAsyncGenerator(output: AsyncGenerator): ReadableStream<any>;
95
- /**
96
- * Form route
97
- * Automatically handles and parses form data
98
- *
99
- * 1. Renders component as initial form markup
100
- * 2. Bind form onSubmit function to custom client JS handling
101
- * 3. Submits form with JavaScript fetch()
102
- * 4. Replaces form content with content from server
103
- * 5. All validation and save logic is on the server
104
- * 6. Handles any Exception thrown on server as error displayed in client
105
- */
106
- export type TFormRouteFn = (context: HSRequestContext) => HSTemplate | Response;
107
- export declare function formRoute(handlerFn: TFormRouteFn): (context: HSRequestContext) => void;
108
-
109
- export {};