@hyperspan/framework 1.0.18 → 1.0.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperspan/framework",
3
- "version": "1.0.18",
3
+ "version": "1.0.19",
4
4
  "description": "Hyperspan Web Framework",
5
5
  "main": "src/server.ts",
6
6
  "types": "src/server.ts",
@@ -40,6 +40,14 @@
40
40
  "./actions": {
41
41
  "types": "./src/actions.ts",
42
42
  "default": "./src/actions.ts"
43
+ },
44
+ "./ssr/install-server-dom-mock": {
45
+ "types": "./src/ssr/install-server-dom-mock.ts",
46
+ "default": "./src/ssr/install-server-dom-mock.ts"
47
+ },
48
+ "./ssr/mock-dom": {
49
+ "types": "./src/ssr/mock-dom.ts",
50
+ "default": "./src/ssr/mock-dom.ts"
43
51
  }
44
52
  },
45
53
  "author": "Vance Lucas <vance@vancelucas.com>",
package/src/server.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import './ssr/install-server-dom-mock';
1
2
  import { HSHtml, html, isHSHtml, renderStream, renderAsync, render, _typeOf } from '@hyperspan/html';
2
3
  import { isbot } from 'isbot';
3
4
  import { executeMiddleware } from './middleware';
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Side-effect entry: install mock `window` / `document` before other framework imports run.
3
+ * Imported first from `server.ts` so `document` / `window` references in user or 3p code do not throw on SSR.
4
+ *
5
+ * Opt out with `HYPERSPAN_DISABLE_MOCK_DOM=1` or `true`.
6
+ */
7
+ import { installMockDom } from './mock-dom';
8
+
9
+ installMockDom();
@@ -0,0 +1,38 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import { MOCK_DOM_MARK, installMockDom, stubElement } from './mock-dom';
3
+
4
+ describe('stubElement', () => {
5
+ test('exposes DOM-like fields and nests children via appendChild', () => {
6
+ const el = stubElement('article');
7
+ expect(el.tagName).toBe('ARTICLE');
8
+ expect(el.nodeType).toBe(1);
9
+ expect(typeof el.appendChild).toBe('function');
10
+
11
+ const inner = stubElement('span');
12
+ el.appendChild(inner);
13
+ expect((el.childNodes as unknown[]).length).toBe(1);
14
+ });
15
+ });
16
+
17
+ describe('installMockDom', () => {
18
+ test('runs without throwing and can be invoked repeatedly', () => {
19
+ expect(() => installMockDom()).not.toThrow();
20
+ expect(() => installMockDom()).not.toThrow();
21
+ });
22
+
23
+ test('respects HYPERSPAN_DISABLE_MOCK_DOM', () => {
24
+ const prev = process.env.HYPERSPAN_DISABLE_MOCK_DOM;
25
+ try {
26
+ process.env.HYPERSPAN_DISABLE_MOCK_DOM = '1';
27
+ expect(installMockDom()).toBe(false);
28
+ } finally {
29
+ process.env.HYPERSPAN_DISABLE_MOCK_DOM = prev;
30
+ }
31
+ });
32
+
33
+ test('when a mock document is installed, it is annotated with MOCK_DOM_MARK', () => {
34
+ const doc = globalThis.document as Record<string, unknown> | undefined;
35
+ if (!doc?.[MOCK_DOM_MARK]) return;
36
+ expect(doc[MOCK_DOM_MARK]).toBe(true);
37
+ });
38
+ });
@@ -0,0 +1,362 @@
1
+ /** Stamped on our mock `document` so reloads don't stack duplicates */
2
+ export const MOCK_DOM_MARK = '__hyperspan_mock_dom';
3
+
4
+ function markedMockDocument(d: unknown): boolean {
5
+ return Boolean(d && typeof d === 'object' && (d as Record<string, unknown>)[MOCK_DOM_MARK] === true);
6
+ }
7
+
8
+ /** Minimal element-like node for the mock DOM; not a spec-compliant implementation */
9
+ export function stubElement(tag: string): any {
10
+ const children: unknown[] = [];
11
+ const node: Record<string, unknown> = {
12
+ tagName: String(tag).toUpperCase(),
13
+ nodeType: 1,
14
+ nodeName: String(tag).toLowerCase(),
15
+ id: '',
16
+ className: '',
17
+ textContent: '',
18
+ innerHTML: '',
19
+ childNodes: children,
20
+ get children(): unknown[] {
21
+ return children;
22
+ },
23
+ style: {},
24
+ dataset: {},
25
+ parentNode: null,
26
+ parentElement: null,
27
+ classList: {
28
+ contains: () => false,
29
+ add() {},
30
+ remove() {},
31
+ toggle() {
32
+ return false;
33
+ },
34
+ },
35
+ appendChild(child: any) {
36
+ children.push(child);
37
+ child.parentNode = node as unknown as ParentNode;
38
+ child.parentElement = node as unknown as ParentNode & Element | null;
39
+ return child;
40
+ },
41
+ insertBefore(child: unknown, ref: unknown | null) {
42
+ const ch = node.childNodes as unknown[];
43
+ const refIx = ref == null ? -1 : ch.indexOf(ref);
44
+ if (refIx < 0) children.push(child);
45
+ else children.splice(refIx, 0, child);
46
+ (child as Record<string, unknown>).parentNode = node as ParentNode as unknown as ParentNode;
47
+ (child as Record<string, unknown>).parentElement = node as ParentNode & Element | null as unknown as Element | null;
48
+ return child as ChildNode as unknown as HTMLElement;
49
+ },
50
+ removeChild(child: unknown): unknown {
51
+ const ix = children.indexOf(child);
52
+ if (ix >= 0) children.splice(ix, 1);
53
+ const c = child as Record<string, unknown>;
54
+ c.parentElement = undefined;
55
+ c.parentNode = undefined;
56
+ return child as ChildNode;
57
+ },
58
+ addEventListener() {},
59
+ removeEventListener() {},
60
+ dispatchEvent() {
61
+ return true;
62
+ },
63
+ setAttribute() {},
64
+ removeAttribute() {},
65
+ hasAttribute(): boolean {
66
+ return false;
67
+ },
68
+ getAttribute(): null | string {
69
+ return null;
70
+ },
71
+ cloneNode(): unknown {
72
+ return stubElement(tag);
73
+ },
74
+ getBoundingClientRect: () =>
75
+ ({
76
+ x: 0,
77
+ y: 0,
78
+ width: 0,
79
+ height: 0,
80
+ top: 0,
81
+ left: 0,
82
+ right: 0,
83
+ bottom: 0,
84
+ toJSON() {
85
+ return '{}';
86
+ },
87
+ }) as DOMRect,
88
+ blur() {},
89
+ focus() {},
90
+ click() {},
91
+ };
92
+ return node;
93
+ }
94
+
95
+ function memoryStorage(): Storage {
96
+ const m = new Map<string, string>();
97
+ return {
98
+ get length() {
99
+ return m.size;
100
+ },
101
+ key(i: number) {
102
+ const keys = [...m.keys()];
103
+ return keys[i] ?? null;
104
+ },
105
+ clear() {
106
+ m.clear();
107
+ },
108
+ getItem(key: string) {
109
+ return m.get(String(key)) ?? null;
110
+ },
111
+ setItem(key: string, value: string) {
112
+ m.set(String(key), String(value));
113
+ },
114
+ removeItem(key: string) {
115
+ m.delete(String(key));
116
+ },
117
+ } as Storage;
118
+ }
119
+
120
+ /**
121
+ * Installs minimal mock `window` / `document` globals for SSR on runtimes without a real DOM (e.g. Bun server).
122
+ *
123
+ * Skips installing when a real-ish `document` already exists unless it carries {@link MOCK_DOM_MARK}.
124
+ *
125
+ * Set `HYPERSPAN_DISABLE_MOCK_DOM=1` or `true` to opt out.
126
+ */
127
+ export function installMockDom(): boolean {
128
+ const env = typeof process !== 'undefined' ? process.env : {};
129
+ const disabled = env.HYPERSPAN_DISABLE_MOCK_DOM === '1' || env.HYPERSPAN_DISABLE_MOCK_DOM === 'true';
130
+ if (disabled) return false;
131
+
132
+ const g = globalThis as unknown as Record<string, unknown>;
133
+
134
+ if (markedMockDocument(g.document)) return false;
135
+
136
+ if (typeof g.document !== 'undefined' && g.document !== null && !markedMockDocument(g.document)) {
137
+ try {
138
+ const d = g.document as Partial<Document>;
139
+ if (d.body !== undefined && typeof d.createElement === 'function') {
140
+ return false;
141
+ }
142
+ } catch {
143
+ /* install mocks */
144
+ }
145
+ }
146
+
147
+ const htmlEl = stubElement('html');
148
+ const body = stubElement('body');
149
+ const head = stubElement('head');
150
+ (htmlEl as any).appendChild(head);
151
+ (htmlEl as any).appendChild(body);
152
+
153
+ const navigatorStub = {
154
+ userAgent: 'HyperspanSSR/1.0',
155
+ language: 'en-US',
156
+ languages: ['en-US'],
157
+ platform: 'server',
158
+ onLine: true,
159
+ maxTouchPoints: 0,
160
+ };
161
+
162
+ let rafId = 1;
163
+ const scheduleRaf = (cb: FrameRequestCallback) => {
164
+ queueMicrotask(() => cb(performance.now()));
165
+ return rafId++;
166
+ };
167
+
168
+ const win: Record<string, unknown> = {
169
+ name: '',
170
+ innerWidth: 1024,
171
+ innerHeight: 768,
172
+ outerWidth: 1024,
173
+ outerHeight: 768,
174
+ devicePixelRatio: 1,
175
+ scrollX: 0,
176
+ scrollY: 0,
177
+ scrollTo() {},
178
+ navigator: navigatorStub,
179
+ localStorage: memoryStorage(),
180
+ sessionStorage: memoryStorage(),
181
+ history: {
182
+ length: 1,
183
+ state: null,
184
+ scrollRestoration: 'auto' as ScrollRestoration,
185
+ replaceState() {},
186
+ pushState() {},
187
+ forward() {},
188
+ back() {},
189
+ go() {},
190
+ },
191
+ location: new URL('http://localhost/ssr'),
192
+ resizeTo() {},
193
+ resizeBy() {},
194
+ addEventListener() {},
195
+ removeEventListener() {},
196
+ dispatchEvent() {
197
+ return true;
198
+ },
199
+ alert() {},
200
+ matchMedia(query: string) {
201
+ const media = String(query);
202
+ return {
203
+ media,
204
+ matches: false,
205
+ addListener() {},
206
+ removeListener() {},
207
+ addEventListener() {},
208
+ removeEventListener() {},
209
+ dispatchEvent() {
210
+ return false;
211
+ },
212
+ onchange: null,
213
+ };
214
+ },
215
+ getComputedStyle: () =>
216
+ ({
217
+ getPropertyValue: () => '',
218
+ setProperty() {},
219
+ }) as unknown as CSSStyleDeclaration,
220
+ requestIdleCallback(cb: IdleRequestCallback) {
221
+ queueMicrotask(() =>
222
+ cb({ didTimeout: false, timeRemaining: () => Number.MAX_SAFE_INTEGER }),
223
+ );
224
+ return 1;
225
+ },
226
+ cancelIdleCallback() {},
227
+ requestAnimationFrame: scheduleRaf,
228
+ cancelAnimationFrame() {},
229
+ MutationObserver:
230
+ typeof globalThis.MutationObserver !== 'undefined'
231
+ ? globalThis.MutationObserver
232
+ : (class {
233
+ constructor(_callback: MutationCallback) {}
234
+ disconnect() {}
235
+ observe() {}
236
+ takeRecords(): MutationRecord[] {
237
+ return [];
238
+ }
239
+ } as unknown as typeof MutationObserver),
240
+ IntersectionObserver:
241
+ typeof globalThis.IntersectionObserver !== 'undefined'
242
+ ? globalThis.IntersectionObserver
243
+ : (class {
244
+ constructor(_cb: IntersectionObserverCallback, _opts?: unknown) {}
245
+ unobserve() {}
246
+ disconnect() {}
247
+ observe() {}
248
+ takeRecords() {
249
+ return [];
250
+ }
251
+ root = null;
252
+ rootMargin = '';
253
+ thresholds = [];
254
+ } as unknown as typeof IntersectionObserver),
255
+ ResizeObserver:
256
+ typeof globalThis.ResizeObserver !== 'undefined'
257
+ ? globalThis.ResizeObserver
258
+ : (class {
259
+ constructor(_callback: ResizeObserverCallback) {}
260
+ disconnect() {}
261
+ observe() {}
262
+ unobserve() {}
263
+ } as unknown as typeof ResizeObserver),
264
+ };
265
+
266
+ win.self = win as unknown as Window & typeof globalThis;
267
+ win.window = win as unknown as Window & typeof globalThis;
268
+
269
+ const documentStub: Record<string, unknown> = {
270
+ [MOCK_DOM_MARK]: true,
271
+ nodeType: 9,
272
+ defaultView: null as unknown as Window | null,
273
+ compatibilityMode: 'CSS1Compat',
274
+ documentElement: htmlEl as unknown as HTMLElement,
275
+ body: body as unknown as HTMLElement,
276
+ head: head as unknown as HTMLHeadElement,
277
+ cookie: '',
278
+ readyState: 'complete',
279
+ URL: 'http://localhost/ssr/',
280
+ referrer: '',
281
+ hidden: false,
282
+ visibilityState: 'visible' as DocumentVisibilityState,
283
+ parentElement: null,
284
+ appendChild(...args: unknown[]) {
285
+ return (body.appendChild as (...a: unknown[]) => unknown)(...args);
286
+ },
287
+ querySelector(sel: unknown) {
288
+ const s = String(sel).toLowerCase();
289
+ if (s === 'body' || s === 'html body') return body as unknown as HTMLElement | null;
290
+ if (s === 'html') return htmlEl as unknown as HTMLElement | null;
291
+ return null;
292
+ },
293
+ querySelectorAll() {
294
+ return {
295
+ length: 0,
296
+ item() {
297
+ return null;
298
+ },
299
+ forEach() {},
300
+ *[Symbol.iterator]() {},
301
+ };
302
+ },
303
+ getElementById(id: unknown) {
304
+ const sid = String(id);
305
+ const e: Record<string, unknown> = stubElement('div');
306
+ e.id = sid;
307
+ e.getAttribute = (k: string) => (k === 'id' ? sid : null);
308
+ e.hasAttribute = (k: string) => k === 'id';
309
+ return e as unknown as HTMLElement;
310
+ },
311
+ getElementsByTagName() {
312
+ return [];
313
+ },
314
+ getElementsByClassName() {
315
+ return [];
316
+ },
317
+ createElement(tag: unknown) {
318
+ return stubElement(String(tag ?? 'div')) as unknown as HTMLElement;
319
+ },
320
+ createTextNode(data: unknown) {
321
+ const v = String(data ?? '');
322
+ return { nodeType: 3, nodeValue: v, nodeName: '#text', textContent: v } as unknown as Text;
323
+ },
324
+ createDocumentFragment() {
325
+ const frag: Record<string, unknown> = {
326
+ nodeType: 11,
327
+ childNodes: [] as unknown[],
328
+ appendChild(child: unknown) {
329
+ (frag.childNodes as unknown[]).push(child);
330
+ return child;
331
+ },
332
+ };
333
+ return frag as unknown as DocumentFragment;
334
+ },
335
+ elementFromPoint: () => null,
336
+ caretRangeFromPoint: () => null,
337
+ addEventListener() {},
338
+ removeEventListener() {},
339
+ dispatchEvent() {
340
+ return true;
341
+ },
342
+ };
343
+
344
+ documentStub.defaultView = win as unknown as Window;
345
+ win.document = documentStub;
346
+
347
+ g.window = win as unknown as Window & typeof globalThis;
348
+ g.document = documentStub as unknown as Document;
349
+
350
+ try {
351
+ (globalThis as any).navigator = navigatorStub as Navigator;
352
+ } catch {
353
+ /* empty */
354
+ }
355
+
356
+ if (typeof globalThis.requestAnimationFrame !== 'function')
357
+ globalThis.requestAnimationFrame = scheduleRaf as typeof requestAnimationFrame;
358
+ if (typeof globalThis.cancelAnimationFrame !== 'function')
359
+ globalThis.cancelAnimationFrame = (() => {}) as typeof cancelAnimationFrame;
360
+
361
+ return true;
362
+ }