@hyperspan/framework 1.0.17 → 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 +9 -1
- package/src/server.test.ts +68 -0
- package/src/server.ts +43 -8
- package/src/ssr/install-server-dom-mock.ts +9 -0
- package/src/ssr/mock-dom.test.ts +38 -0
- package/src/ssr/mock-dom.ts +362 -0
- package/src/types.ts +15 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyperspan/framework",
|
|
3
|
-
"version": "1.0.
|
|
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.test.ts
CHANGED
|
@@ -15,6 +15,28 @@ test('route fetch() returns a Response', async () => {
|
|
|
15
15
|
expect(await response.text()).toBe('<h1>Hello World</h1>');
|
|
16
16
|
});
|
|
17
17
|
|
|
18
|
+
test('route handler returning undefined or null yields 204 No Content', async () => {
|
|
19
|
+
const withHeader = createRoute().get((context: HS.Context) => {
|
|
20
|
+
context.res.headers.set('X-Empty', '1');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const req = new Request('http://localhost:3000/');
|
|
24
|
+
let response = await withHeader.fetch(req);
|
|
25
|
+
expect(response).toBeInstanceOf(Response);
|
|
26
|
+
expect(response.status).toBe(204);
|
|
27
|
+
expect(response.headers.get('X-Empty')).toBe('1');
|
|
28
|
+
expect(await response.text()).toBe('');
|
|
29
|
+
|
|
30
|
+
const explicitNull = createRoute().get(() => null);
|
|
31
|
+
response = await explicitNull.fetch(req);
|
|
32
|
+
expect(response.status).toBe(204);
|
|
33
|
+
expect(await response.text()).toBe('');
|
|
34
|
+
|
|
35
|
+
const asyncVoid = createRoute().get(async () => undefined);
|
|
36
|
+
response = await asyncVoid.fetch(req);
|
|
37
|
+
expect(response.status).toBe(204);
|
|
38
|
+
});
|
|
39
|
+
|
|
18
40
|
test('server with two routes can return Response from one', async () => {
|
|
19
41
|
const server = await createServer({
|
|
20
42
|
appDir: './app',
|
|
@@ -214,6 +236,52 @@ test('createContext() merge() function preserves custom headers with json() meth
|
|
|
214
236
|
expect(response.headers.get('Content-Type')).toBe('application/json');
|
|
215
237
|
});
|
|
216
238
|
|
|
239
|
+
test('route returning ReadableStream produces a streaming response', async () => {
|
|
240
|
+
const route = createRoute().get((_context: HS.Context) => {
|
|
241
|
+
return new ReadableStream({
|
|
242
|
+
start(controller) {
|
|
243
|
+
const enc = new TextEncoder();
|
|
244
|
+
controller.enqueue(enc.encode('alpha'));
|
|
245
|
+
controller.enqueue(enc.encode('beta'));
|
|
246
|
+
controller.close();
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
const request = new Request('http://localhost:3000/');
|
|
252
|
+
const response = await route.fetch(request);
|
|
253
|
+
|
|
254
|
+
expect(response).toBeInstanceOf(Response);
|
|
255
|
+
expect(response.status).toBe(200);
|
|
256
|
+
expect(response.body).toBeInstanceOf(ReadableStream);
|
|
257
|
+
expect(response.headers.get('Transfer-Encoding')).toBe('chunked');
|
|
258
|
+
expect(response.headers.get('Content-Encoding')).toBe('Identity');
|
|
259
|
+
expect(response.headers.get('Content-Type')).toBe('application/octet-stream');
|
|
260
|
+
expect(await response.text()).toBe('alphabeta');
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test('route returning ReadableStream respects Content-Type set on context.res.headers', async () => {
|
|
264
|
+
const route = createRoute().get((context: HS.Context) => {
|
|
265
|
+
context.res.headers.set('Content-Type', 'text/plain; charset=UTF-8');
|
|
266
|
+
return new ReadableStream({
|
|
267
|
+
start(controller) {
|
|
268
|
+
controller.enqueue(new TextEncoder().encode('ok'));
|
|
269
|
+
controller.close();
|
|
270
|
+
},
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
const request = new Request('http://localhost:3000/');
|
|
275
|
+
const response = await route.fetch(request);
|
|
276
|
+
|
|
277
|
+
expect(response).toBeInstanceOf(Response);
|
|
278
|
+
expect(response.status).toBe(200);
|
|
279
|
+
expect(response.headers.get('Transfer-Encoding')).toBe('chunked');
|
|
280
|
+
expect(response.headers.get('Content-Encoding')).toBe('Identity');
|
|
281
|
+
expect(response.headers.get('Content-Type')).toBe('text/plain; charset=UTF-8');
|
|
282
|
+
expect(await response.text()).toBe('ok');
|
|
283
|
+
});
|
|
284
|
+
|
|
217
285
|
test('route returning AsyncGenerator produces a streaming response', async () => {
|
|
218
286
|
async function* streamingHandler() {
|
|
219
287
|
yield '<h1>Hello</h1>';
|
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';
|
|
@@ -114,10 +115,20 @@ export function createContext(req: Request, route?: HS.Route): HS.Context {
|
|
|
114
115
|
return context;
|
|
115
116
|
}
|
|
116
117
|
|
|
118
|
+
/**
|
|
119
|
+
* 204 No Content when the route handler returns `undefined` or `null`.
|
|
120
|
+
*/
|
|
121
|
+
function noContentResponse(context: HS.Context): Response {
|
|
122
|
+
return new Response(null, {
|
|
123
|
+
status: 204,
|
|
124
|
+
headers: context.res.headers,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
117
127
|
|
|
118
128
|
/**
|
|
119
129
|
* Define a route that can handle a direct HTTP request.
|
|
120
|
-
* Route handlers should return
|
|
130
|
+
* Route handlers should return HSHtml, a Response, a stream, a generator, etc.
|
|
131
|
+
* Returning `undefined` or `null` sends 204 No Content (with merged `context.res` headers).
|
|
121
132
|
*/
|
|
122
133
|
export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
123
134
|
const _handlers: Record<string, HS.RouteHandler> = {};
|
|
@@ -236,7 +247,7 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
236
247
|
const globalMiddleware = api._middleware['*'] || [];
|
|
237
248
|
const methodMiddleware = api._middleware[method] || [];
|
|
238
249
|
|
|
239
|
-
const methodHandler = async (context
|
|
250
|
+
const methodHandler: HS.RouteHandler = async (context) => {
|
|
240
251
|
// Handle CORS preflight requests (if no OPTIONS handler is defined)
|
|
241
252
|
if (method === 'OPTIONS' && !_handlers['OPTIONS']) {
|
|
242
253
|
return context.res.html(
|
|
@@ -289,12 +300,25 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
289
300
|
return returnHTMLResponse(context, () => routeContent, responseOptions);
|
|
290
301
|
}
|
|
291
302
|
|
|
303
|
+
const streamOptions = {
|
|
304
|
+
status: context.res.status ?? 200,
|
|
305
|
+
headers: Object.fromEntries(context.res.headers.entries()),
|
|
306
|
+
};
|
|
307
|
+
|
|
292
308
|
const contentType = _typeOf(routeContent);
|
|
293
309
|
if (contentType === 'generator') {
|
|
294
|
-
return new StreamResponse(routeContent as AsyncGenerator,
|
|
310
|
+
return new StreamResponse(routeContent as AsyncGenerator, streamOptions);
|
|
295
311
|
}
|
|
296
312
|
|
|
297
|
-
|
|
313
|
+
if (routeContent instanceof ReadableStream) {
|
|
314
|
+
return new StreamResponse(routeContent, streamOptions);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (routeContent === undefined || routeContent === null) {
|
|
318
|
+
return noContentResponse(context);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return routeContent as HS.RouteHandlerReturn;
|
|
298
322
|
};
|
|
299
323
|
|
|
300
324
|
// Run the route handler and any middleware
|
|
@@ -537,17 +561,28 @@ async function showErrorReponse(
|
|
|
537
561
|
|
|
538
562
|
|
|
539
563
|
/**
|
|
540
|
-
* Streaming
|
|
564
|
+
* Streaming response: chunked transfer with Transfer-Encoding and Content-Encoding.
|
|
565
|
+
* Pass an async iterator (HTML chunks) or a ReadableStream; default Content-Type is HTML for iterators
|
|
566
|
+
* and application/octet-stream for readable streams. Optional headers override defaults.
|
|
541
567
|
*/
|
|
542
568
|
export class StreamResponse extends Response {
|
|
543
|
-
constructor(
|
|
569
|
+
constructor(
|
|
570
|
+
body: AsyncIterator<unknown> | ReadableStream,
|
|
571
|
+
options: { status?: number; headers?: Record<string, string> } = {}
|
|
572
|
+
) {
|
|
544
573
|
super();
|
|
545
574
|
const { status, headers, ...restOptions } = options;
|
|
546
|
-
const stream =
|
|
575
|
+
const stream =
|
|
576
|
+
body instanceof ReadableStream
|
|
577
|
+
? body
|
|
578
|
+
: createReadableStreamFromAsyncGenerator(body as AsyncGenerator);
|
|
579
|
+
|
|
580
|
+
const defaultContentType =
|
|
581
|
+
body instanceof ReadableStream ? 'application/octet-stream' : 'text/html; charset=UTF-8';
|
|
547
582
|
|
|
548
583
|
const mergedHeaders = new Headers({
|
|
549
584
|
'Transfer-Encoding': 'chunked',
|
|
550
|
-
'Content-Type':
|
|
585
|
+
'Content-Type': defaultContentType,
|
|
551
586
|
'Content-Encoding': 'Identity',
|
|
552
587
|
});
|
|
553
588
|
if (headers) {
|
|
@@ -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
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -100,7 +100,21 @@ export namespace Hyperspan {
|
|
|
100
100
|
disableStreaming?: (context: Hyperspan.Context) => boolean;
|
|
101
101
|
};
|
|
102
102
|
};
|
|
103
|
-
|
|
103
|
+
|
|
104
|
+
export type RouteHandlerReturn =
|
|
105
|
+
| Response
|
|
106
|
+
| HSHtml
|
|
107
|
+
| string
|
|
108
|
+
| ReadableStream
|
|
109
|
+
| AsyncIterable<unknown>
|
|
110
|
+
| Iterable<unknown>
|
|
111
|
+
| undefined
|
|
112
|
+
| null
|
|
113
|
+
| void;
|
|
114
|
+
|
|
115
|
+
export type RouteHandler = (
|
|
116
|
+
context: Hyperspan.Context
|
|
117
|
+
) => RouteHandlerReturn | Promise<RouteHandlerReturn>;
|
|
104
118
|
export type RouteHandlerOptions = {
|
|
105
119
|
middleware?: Hyperspan.MiddlewareFunction[];
|
|
106
120
|
}
|