@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/README.md CHANGED
@@ -1,83 +1,5 @@
1
- # Hyperspan. Simple. Full-Stack. Streaming.
1
+ # @hyperspan/framework
2
2
 
3
- > [!NOTE] > **Hyperspan is still in the early stages of development, your feedback is appreciated.**
3
+ > [!NOTE] > **The Hyperspan framework package is still in the heavy development. APIs may change without notice.**
4
4
 
5
- Hyperspan is a full-stack framework built with [Bun](https://bun.sh) that is focused on simplicity and ease of use.
6
-
7
- No JSX. No Virtual DOM. No hydration time. No nonsense. Just blazing fast HTML strings with reactive templates.
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.
5
+ Full Hyperspan framework docs coming soon...
package/package.json CHANGED
@@ -1,51 +1,51 @@
1
1
  {
2
2
  "name": "@hyperspan/framework",
3
- "version": "0.0.3",
3
+ "version": "0.1.0",
4
4
  "description": "Hyperspan Web Framework",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
5
+ "type": "module",
6
+ "module": "src/server.ts",
7
7
  "public": true,
8
8
  "publishConfig": {
9
9
  "access": "public"
10
10
  },
11
- "scripts": {
12
- "build": "bun ./build.ts",
13
- "clean": "rm -rf dist",
14
- "test": "bun test",
15
- "prepack": "npm run clean && npm run build"
16
- },
17
- "repository": {
18
- "type": "git",
19
- "url": "https://github.com/vlucas/hyperspan-framework"
20
- },
11
+ "author": "Vance Lucas <vance@vancelucas.com>",
12
+ "license": "BSD-3-Clause",
21
13
  "keywords": [
22
14
  "framework",
23
15
  "node",
24
16
  "bun",
25
- "web",
26
- "framework",
17
+ "web framework",
27
18
  "javascript",
28
- "typescript"
19
+ "typescript",
20
+ "streaming",
21
+ "hypermedia"
29
22
  ],
30
- "author": "Vance Lucas <vance@vancelucas.com>",
31
- "license": "BSD-3-Clause",
23
+ "homepage": "https://www.hyperspan.dev",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://github.com/vlucas/hyperspan-framework"
27
+ },
32
28
  "bugs": {
33
29
  "url": "https://github.com/vlucas/hyperspan-framework/issues"
34
30
  },
35
- "homepage": "https://www.hyperspan.dev",
36
- "dependencies": {
37
- "@fastify/deepmerge": "^2.0.0",
38
- "@mjackson/headers": "^0.7.2",
39
- "escape-html": "^1.0.3",
40
- "isbot": "^5.1.17",
41
- "trek-middleware": "^1.2.0",
42
- "trek-router": "^1.2.0"
31
+ "scripts": {
32
+ "test": "bun test"
43
33
  },
44
34
  "devDependencies": {
45
- "@types/bun": "^1.1.11",
46
- "@types/escape-html": "^1.0.4",
47
- "@types/node": "^22.7.5",
48
- "bun-plugin-dts": "^0.3.0",
49
- "typescript": "^5.6.3"
35
+ "@types/bun": "^1.1.9",
36
+ "@types/node": "^22.5.5",
37
+ "@types/react": "^19.1.0",
38
+ "bun-types": "latest",
39
+ "prettier": "^3.2.5"
40
+ },
41
+ "peerDependencies": {
42
+ "typescript": "^5.0.0"
43
+ },
44
+ "dependencies": {
45
+ "@hyperspan/html": "^0.1.0",
46
+ "@preact/compat": "^18.3.1",
47
+ "hono": "^4.7.4",
48
+ "isbot": "^5.1.25",
49
+ "valibot": "^1.0.0-rc.3"
50
50
  }
51
51
  }
package/src/assets.ts ADDED
@@ -0,0 +1,141 @@
1
+ import { html } from '@hyperspan/html';
2
+ import { md5 } from './clientjs/md5';
3
+ import { readdir } from 'node:fs/promises';
4
+ import { resolve } from 'node:path';
5
+
6
+ export const IS_PROD = process.env.NODE_ENV === 'production';
7
+ const PWD = import.meta.dir;
8
+
9
+ /**
10
+ * Build client JS for end users (minimal JS for Hyperspan to work)
11
+ */
12
+ export const clientJSFiles = new Map<string, { src: string; type?: string }>();
13
+ export async function buildClientJS() {
14
+ const sourceFile = resolve(PWD, '../', './hyperspan/clientjs/hyperspan-client.ts');
15
+ const output = await Bun.build({
16
+ entrypoints: [sourceFile],
17
+ outdir: `./public/_hs/js`,
18
+ naming: IS_PROD ? '[dir]/[name]-[hash].[ext]' : undefined,
19
+ minify: IS_PROD,
20
+ });
21
+
22
+ const jsFile = output.outputs[0].path.split('/').reverse()[0];
23
+ clientJSFiles.set('_hs', { src: '/_hs/js/' + jsFile });
24
+ return jsFile;
25
+ }
26
+
27
+ /**
28
+ * Find client CSS file built for end users
29
+ * @TODO: Build this in code here vs. relying on tailwindcss CLI tool from package scripts
30
+ */
31
+ export const clientCSSFiles = new Map<string, string>();
32
+ export async function buildClientCSS() {
33
+ if (clientCSSFiles.has('_hs')) {
34
+ return clientCSSFiles.get('_hs');
35
+ }
36
+
37
+ // Find file already built from tailwindcss CLI
38
+ const cssDir = './public/_hs/css/';
39
+ const cssFiles = await readdir(cssDir);
40
+ let foundCSSFile: string = '';
41
+
42
+ for (const file of cssFiles) {
43
+ // Only looking for CSS files
44
+ if (!file.endsWith('.css')) {
45
+ continue;
46
+ }
47
+
48
+ foundCSSFile = file.replace(cssDir, '');
49
+ clientCSSFiles.set('_hs', foundCSSFile);
50
+ break;
51
+ }
52
+
53
+ if (!foundCSSFile) {
54
+ console.log(`Unable to build CSS files from ${cssDir}`);
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Output HTML style tag for Hyperspan app
60
+ */
61
+ export function hyperspanStyleTags() {
62
+ const cssFiles = Array.from(clientCSSFiles.entries());
63
+ return html`${cssFiles.map(
64
+ ([key, file]) => html`<link rel="stylesheet" href="/_hs/css/${file}" />`
65
+ )}`;
66
+ }
67
+
68
+ /**
69
+ * Output HTML script tag for Hyperspan app
70
+ * Required for functioning streaming so content can pop into place properly once ready
71
+ */
72
+ export function hyperspanScriptTags() {
73
+ const jsFiles = Array.from(clientJSFiles.entries());
74
+ return html`
75
+ <script type="importmap">
76
+ {
77
+ "imports": {
78
+ "preact": "https://esm.sh/preact@10.26.4",
79
+ "preact/": "https://esm.sh/preact@10.26.4/",
80
+ "react": "https://esm.sh/preact@10.26.4/compat",
81
+ "react/": "https://esm.sh/preact@10.26.4/compat/",
82
+ "react-dom": "https://esm.sh/preact@10.26.4/compat"
83
+ }
84
+ }
85
+ </script>
86
+ ${jsFiles.map(
87
+ ([key, file]) =>
88
+ html`<script
89
+ id="js-${key}"
90
+ type="${file.type || 'text/javascript'}"
91
+ src="${file.src}"
92
+ ></script>`
93
+ )}
94
+ `;
95
+ }
96
+
97
+ /**
98
+ * Return a Preact component, mounted as an island in a <script> tag so it can be embedded into the page response.
99
+ */
100
+ export async function createPreactIsland(file: string) {
101
+ let filePath = file.replace('file://', '');
102
+
103
+ let resultStr = 'import{h,render}from"preact";';
104
+ const build = await Bun.build({
105
+ entrypoints: [filePath],
106
+ minify: true,
107
+ external: ['react', 'preact'],
108
+ // @ts-ignore
109
+ env: 'APP_PUBLIC_*', // Inlines any ENV that starts with 'APP_PUBLIC_'
110
+ });
111
+
112
+ for (const output of build.outputs) {
113
+ resultStr += await output.text(); // string
114
+ }
115
+
116
+ // Find default export - this is our component
117
+ const r = /export\{([a-zA-Z]+) as default\}/g;
118
+ const matchExport = r.exec(resultStr);
119
+ const jsId = md5(resultStr);
120
+
121
+ if (!matchExport) {
122
+ throw new Error(
123
+ 'File does not have a default export! Ensure a function has export default to use this.'
124
+ );
125
+ }
126
+
127
+ // Preact render/mount component
128
+ const fn = matchExport[1];
129
+ let _mounted = false;
130
+
131
+ // Return HTML that will embed this component
132
+ return (props: any) => {
133
+ if (!_mounted) {
134
+ _mounted = true;
135
+ resultStr += `render(h(${fn}, ${JSON.stringify(props)}), document.getElementById("${jsId}"));`;
136
+ }
137
+ return html.raw(
138
+ `<div id="${jsId}"></div><script type="module" data-source-id="${jsId}">${resultStr}</script>`
139
+ );
140
+ };
141
+ }
@@ -1,7 +1,10 @@
1
- import { html, renderToString } from '../html';
2
- import { Idiomorph } from './idomorph.esm';
1
+ import { html } from '../html';
2
+ import { Idiomorph } from './idiomorph.esm';
3
3
 
4
- function setupAsyncContentObserver() {
4
+ /**
5
+ * Used for streaming content from the server to the client.
6
+ */
7
+ function htmlAsyncContentObserver() {
5
8
  if (typeof MutationObserver != 'undefined') {
6
9
  // Hyperspan - Async content loader
7
10
  // Puts streamed content in its place immediately after it is added to the DOM
@@ -41,178 +44,7 @@ function setupAsyncContentObserver() {
41
44
  asyncContentObserver.observe(document.body, { childList: true, subtree: true });
42
45
  }
43
46
  }
44
- setupAsyncContentObserver();
45
-
46
- /**
47
- * Event binding for added/updated content
48
- */
49
- function setupEventBindingObserver() {
50
- if (typeof MutationObserver != 'undefined') {
51
- const eventBindingObserver = new MutationObserver((list) => {
52
- bindHyperspanEvents(document.body);
53
- });
54
- eventBindingObserver.observe(document.body, { childList: true, subtree: true });
55
- }
56
- }
57
- setupEventBindingObserver();
58
-
59
- /**
60
- * Global Window assignments...
61
- */
62
-
63
- // @ts-ignore
64
- const hyperspan: any = {
65
- _fn: new Map(),
66
- wc: new Map(),
67
- compIdOrLast(id?: string) {
68
- let comp = hyperspan.wc.get(id);
69
-
70
- // Get last component if id lookup failed
71
- if (!comp) {
72
- const lastComp = Array.from(hyperspan.wc).pop();
73
- // @ts-ignore - The value returned from a Map is a tuple. The second value (lastComp[1]) is the actual value
74
- comp = lastComp ? lastComp[1] : false;
75
- }
76
-
77
- return comp || false;
78
- },
79
- fn(id: string, ufn: any) {
80
- const comp = this.compIdOrLast(id);
81
-
82
- const fnd = {
83
- id,
84
- cid: comp ? comp.id : null,
85
- fn: comp ? ufn.bind(comp) : ufn,
86
- comp,
87
- };
88
-
89
- this._fn.set(id, fnd);
90
- },
91
- // Binds function execution to the component instance so 'this' keyword works as expected inside event handlers
92
- fnc(id: string, ...args: any[]) {
93
- const fnd = this._fn.get(id);
94
-
95
- if (!fnd) {
96
- console.log('[Hyperspan] Unable to find function with id ' + id);
97
- return;
98
- }
99
-
100
- if (fnd.comp) {
101
- fnd.fn.call(fnd.comp, ...args);
102
- } else {
103
- fnd.fn(...args);
104
- }
105
- },
106
- };
107
-
108
- /**
109
- * Web component (foundation of client components)
110
- */
111
- class HyperspanComponent extends HTMLElement {
112
- constructor() {
113
- super();
114
- }
115
-
116
- static get observedAttributes() {
117
- return ['data-state'];
118
- }
119
-
120
- randomId() {
121
- return Math.random().toString(36).substring(2, 9);
122
- }
123
-
124
- async render() {
125
- let content = '<div>Loading...</div>';
126
-
127
- const comp = hyperspan.wc.get(this.id);
128
-
129
- if (comp) {
130
- content = await renderToString(comp.render());
131
- }
132
-
133
- Idiomorph.morph(this, content, { morphStyle: 'innerHTML' });
134
- }
135
-
136
- connectedCallback() {
137
- const comp = hyperspan.wc.get(this.id);
138
-
139
- if (comp) {
140
- comp.mount && comp.mount();
141
- }
142
- }
143
-
144
- attributeChangedCallback() {
145
- this.render();
146
- }
147
- }
148
-
149
- // Bind events
150
- function bindHyperspanEvents(webComponentEl: HTMLElement) {
151
- const domEvents = [
152
- 'click',
153
- 'dblclick',
154
- 'contextmenu',
155
- 'hover',
156
- 'focus',
157
- 'blur',
158
- 'mouseup',
159
- 'mousedown',
160
- 'touchstart',
161
- 'touchend',
162
- 'touchcancel',
163
- 'touchmove',
164
- 'submit',
165
- 'change',
166
- 'scroll',
167
- 'keyup',
168
- 'keydown',
169
- ];
170
- const eventEls = Array.from(
171
- webComponentEl.querySelectorAll('[on' + domEvents.join('], [on') + ']')
172
- );
173
-
174
- for (let i = 0; i < eventEls.length; i++) {
175
- const el = eventEls[i] as HTMLElement;
176
- const elEvents = el.getAttributeNames();
177
-
178
- elEvents
179
- .filter((ev) => ev.startsWith('on'))
180
- .map((event) => {
181
- const fnId = el.getAttribute(event)?.replace('hyperspan:', '');
182
-
183
- if (fnId && el.dataset[event] !== fnId) {
184
- const eventName = event.replace('on', '');
185
- el.addEventListener(eventName, globalEventDispatch);
186
- el.dataset[event] = fnId;
187
- el.removeAttribute(event);
188
- }
189
- });
190
- }
191
- }
192
-
193
- // Proxies all events to the function they go to by event type
194
- function globalEventDispatch(e: Event) {
195
- let el = e.target as HTMLElement;
196
-
197
- if (el) {
198
- const dataName = 'on' + e.type;
199
- let fnId = el.dataset[dataName];
200
-
201
- if (!fnId) {
202
- el = el.closest('[data-' + dataName + ']') || el;
203
- }
204
-
205
- fnId = el.dataset[dataName];
206
-
207
- if (fnId) {
208
- hyperspan.fnc(fnId, e, el);
209
- }
210
- }
211
- }
212
-
213
- customElements.define('hs-wc', HyperspanComponent);
47
+ htmlAsyncContentObserver();
214
48
 
215
- // @ts-ignore
216
- window.hyperspan = hyperspan;
217
49
  // @ts-ignore
218
50
  window.html = html;