frontend-hamroun 1.1.90 → 1.2.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/{src/backend → backend}/api-utils.d.ts +2 -2
- package/dist/backend/api-utils.js +135 -0
- package/dist/backend/auth.js +387 -0
- package/dist/{src/backend → backend}/database.d.ts +1 -1
- package/dist/backend/database.js +91 -0
- package/dist/{src/backend → backend}/model.d.ts +2 -2
- package/dist/backend/model.js +176 -0
- package/dist/{src/backend → backend}/router.d.ts +1 -1
- package/dist/backend/router.js +137 -0
- package/dist/backend/server.js +268 -0
- package/dist/batch.js +22 -0
- package/dist/cli/index.js +1 -0
- package/dist/{src/component.d.ts → component.d.ts} +1 -1
- package/dist/component.js +84 -0
- package/dist/components/Counter.js +2 -0
- package/dist/context.js +20 -0
- package/dist/frontend-hamroun.es.js +1680 -0
- package/dist/frontend-hamroun.es.js.map +1 -0
- package/dist/frontend-hamroun.umd.js +2 -0
- package/dist/frontend-hamroun.umd.js.map +1 -0
- package/dist/hooks.js +164 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +52 -355
- package/dist/jsx-runtime/index.d.ts +9 -0
- package/dist/jsx-runtime/index.js +16 -0
- package/dist/jsx-runtime/jsx-dev-runtime.js +1 -0
- package/dist/jsx-runtime/jsx-runtime.js +91 -0
- package/dist/{src/jsx-runtime.d.ts → jsx-runtime.d.ts} +1 -1
- package/dist/jsx-runtime.js +192 -0
- package/dist/renderer.js +51 -0
- package/dist/{src/server-renderer.d.ts → server-renderer.d.ts} +3 -0
- package/dist/server-renderer.js +102 -0
- package/dist/vdom.js +27 -0
- package/package.json +38 -52
- package/scripts/generate.js +134 -0
- package/src/backend/api-utils.ts +178 -0
- package/src/backend/auth.ts +543 -0
- package/src/backend/database.ts +104 -0
- package/src/backend/model.ts +196 -0
- package/src/backend/router.ts +176 -0
- package/src/backend/server.ts +330 -0
- package/src/backend/types.ts +257 -0
- package/src/batch.ts +24 -0
- package/src/cli/index.js +22 -40
- package/src/component.ts +98 -0
- package/src/components/Counter.tsx +4 -0
- package/src/context.ts +32 -0
- package/src/hooks.ts +211 -0
- package/src/index.ts +113 -0
- package/src/jsx-runtime/index.ts +24 -0
- package/src/jsx-runtime/jsx-dev-runtime.ts +0 -0
- package/src/jsx-runtime/jsx-runtime.ts +99 -0
- package/src/jsx-runtime.ts +226 -0
- package/src/renderer.ts +55 -0
- package/src/server-renderer.ts +114 -0
- package/src/types/bcrypt.d.ts +30 -0
- package/src/types/jsonwebtoken.d.ts +55 -0
- package/src/types.d.ts +26 -0
- package/src/types.ts +21 -0
- package/src/vdom.ts +34 -0
- package/templates/basic-app/package.json +17 -15
- package/templates/basic-app/postcss.config.js +1 -0
- package/templates/basic-app/src/App.tsx +65 -0
- package/templates/basic-app/src/api.ts +58 -0
- package/templates/basic-app/src/components/Counter.tsx +26 -0
- package/templates/basic-app/src/components/Header.tsx +9 -0
- package/templates/basic-app/src/components/TodoList.tsx +90 -0
- package/templates/basic-app/src/main.ts +20 -0
- package/templates/basic-app/src/server.ts +99 -0
- package/templates/basic-app/tailwind.config.js +23 -2
- package/bin/cli.js +0 -371
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -139269
- package/dist/index.mjs.map +0 -1
- package/dist/src/index.d.ts +0 -16
- package/dist/test/setupTests.d.ts +0 -4
- /package/dist/{src/backend → backend}/auth.d.ts +0 -0
- /package/dist/{src/backend → backend}/server.d.ts +0 -0
- /package/dist/{src/backend → backend}/types.d.ts +0 -0
- /package/dist/{test/backend.test.d.ts → backend/types.js} +0 -0
- /package/dist/{src/batch.d.ts → batch.d.ts} +0 -0
- /package/dist/{src/cli → cli}/index.d.ts +0 -0
- /package/dist/{src/components → components}/Counter.d.ts +0 -0
- /package/dist/{src/context.d.ts → context.d.ts} +0 -0
- /package/dist/{src/hooks.d.ts → hooks.d.ts} +0 -0
- /package/dist/{src/jsx-runtime → jsx-runtime}/jsx-dev-runtime.d.ts +0 -0
- /package/dist/{src/jsx-runtime → jsx-runtime}/jsx-runtime.d.ts +0 -0
- /package/dist/{src/renderer.d.ts → renderer.d.ts} +0 -0
- /package/dist/{src/types.d.ts → types.d.ts} +0 -0
- /package/dist/{test/mockTest.d.ts → types.js} +0 -0
- /package/dist/{src/vdom.d.ts → vdom.d.ts} +0 -0
- /package/{dist/test/mongooseSetup.d.ts → src/cli/index.ts} +0 -0
package/src/index.ts
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
// Frontend exports
|
2
|
+
import { jsx, jsxs, createElement, Fragment } from './jsx-runtime/index';
|
3
|
+
import { Component } from './component';
|
4
|
+
import { useState, useEffect, useRef, useMemo, useErrorBoundary } from './hooks';
|
5
|
+
import { render, hydrate } from './renderer';
|
6
|
+
import { renderToString } from './server-renderer';
|
7
|
+
import { prepareRender, finishRender } from './hooks';
|
8
|
+
import { batchUpdates } from './batch';
|
9
|
+
|
10
|
+
// Backend functionality
|
11
|
+
import { createServer } from './backend/server';
|
12
|
+
import { Router } from './backend/router';
|
13
|
+
import { createApiRouter } from './backend/api-utils';
|
14
|
+
|
15
|
+
// Export JSX runtime
|
16
|
+
export {
|
17
|
+
jsx,
|
18
|
+
jsxs,
|
19
|
+
createElement,
|
20
|
+
Fragment
|
21
|
+
};
|
22
|
+
|
23
|
+
// Export hooks
|
24
|
+
export {
|
25
|
+
useState,
|
26
|
+
useEffect,
|
27
|
+
useRef,
|
28
|
+
useMemo,
|
29
|
+
useErrorBoundary,
|
30
|
+
prepareRender,
|
31
|
+
finishRender,
|
32
|
+
batchUpdates
|
33
|
+
};
|
34
|
+
|
35
|
+
// Export component base class
|
36
|
+
export { Component };
|
37
|
+
|
38
|
+
// Export renderers
|
39
|
+
export {
|
40
|
+
render,
|
41
|
+
hydrate,
|
42
|
+
renderToString
|
43
|
+
};
|
44
|
+
// Export backend functionality
|
45
|
+
export {
|
46
|
+
createServer,
|
47
|
+
Router,
|
48
|
+
createApiRouter
|
49
|
+
};
|
50
|
+
|
51
|
+
// Default export with all functionality
|
52
|
+
export default {
|
53
|
+
// Frontend
|
54
|
+
jsx,
|
55
|
+
jsxs,
|
56
|
+
createElement,
|
57
|
+
Fragment,
|
58
|
+
Component,
|
59
|
+
useState,
|
60
|
+
useEffect,
|
61
|
+
useRef,
|
62
|
+
useMemo,
|
63
|
+
useErrorBoundary,
|
64
|
+
render,
|
65
|
+
hydrate,
|
66
|
+
renderToString,
|
67
|
+
prepareRender,
|
68
|
+
finishRender,
|
69
|
+
batchUpdates,
|
70
|
+
|
71
|
+
// Backend
|
72
|
+
createServer,
|
73
|
+
Router,
|
74
|
+
createApiRouter
|
75
|
+
};
|
76
|
+
|
77
|
+
// Re-export types
|
78
|
+
export type { Context } from './context';
|
79
|
+
export type { VNode } from './types';
|
80
|
+
|
81
|
+
// Backend exports
|
82
|
+
export { createAuth } from './backend/auth';
|
83
|
+
export { createModel, FieldTypes } from './backend/model';
|
84
|
+
export { createModelRouter, createApiRouter as createCustomRouter } from './backend/router';
|
85
|
+
export {
|
86
|
+
sendSuccess,
|
87
|
+
sendError,
|
88
|
+
apiResponse,
|
89
|
+
getPaginationParams,
|
90
|
+
paginationMiddleware,
|
91
|
+
validateRequest,
|
92
|
+
asyncHandler,
|
93
|
+
createRestEndpoints
|
94
|
+
} from './backend/api-utils';
|
95
|
+
export { DatabaseConnector } from './backend/database';
|
96
|
+
|
97
|
+
// Backend types
|
98
|
+
export type {
|
99
|
+
HamrounServerOptions,
|
100
|
+
RouterOptions,
|
101
|
+
RouteContext,
|
102
|
+
PaginationOptions,
|
103
|
+
PaginatedResult,
|
104
|
+
DatabaseOptions,
|
105
|
+
Model,
|
106
|
+
ApiResponse
|
107
|
+
} from './backend/types';
|
108
|
+
|
109
|
+
export type {
|
110
|
+
AuthOptions,
|
111
|
+
TokenPair
|
112
|
+
} from './backend/auth';
|
113
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import { jsx, jsxs, createElement, Fragment } from './jsx-runtime';
|
2
|
+
|
3
|
+
export {
|
4
|
+
jsx,
|
5
|
+
jsxs,
|
6
|
+
createElement,
|
7
|
+
Fragment
|
8
|
+
};
|
9
|
+
|
10
|
+
// For global access in browsers
|
11
|
+
if (typeof window !== 'undefined') {
|
12
|
+
// TypeScript safe way to add properties to window
|
13
|
+
(window as any).jsx = jsx;
|
14
|
+
(window as any).jsxs = jsxs;
|
15
|
+
(window as any).Fragment = Fragment;
|
16
|
+
}
|
17
|
+
|
18
|
+
// Default export for module usage
|
19
|
+
export default {
|
20
|
+
jsx,
|
21
|
+
jsxs,
|
22
|
+
createElement,
|
23
|
+
Fragment
|
24
|
+
};
|
File without changes
|
@@ -0,0 +1,99 @@
|
|
1
|
+
import { diff } from '../vdom';
|
2
|
+
|
3
|
+
// Create a safe global object reference that works in both browser and Node
|
4
|
+
const globalObj: any = typeof window !== 'undefined' ? window :
|
5
|
+
typeof global !== 'undefined' ? global : {};
|
6
|
+
|
7
|
+
export function jsx(type: string | Function, props: any, key?: string): any {
|
8
|
+
return {
|
9
|
+
type,
|
10
|
+
props: props || {},
|
11
|
+
key
|
12
|
+
};
|
13
|
+
}
|
14
|
+
|
15
|
+
export function jsxs(type: string | Function, props: any, key?: string): any {
|
16
|
+
return jsx(type, props, key);
|
17
|
+
}
|
18
|
+
|
19
|
+
export function createElement(vnode: any): HTMLElement | Text {
|
20
|
+
if (typeof vnode === 'string' || typeof vnode === 'number') {
|
21
|
+
return document.createTextNode(String(vnode));
|
22
|
+
}
|
23
|
+
|
24
|
+
if (typeof vnode.type === 'function') {
|
25
|
+
const result = vnode.type(vnode.props);
|
26
|
+
return createElement(result);
|
27
|
+
}
|
28
|
+
|
29
|
+
const element = document.createElement(vnode.type);
|
30
|
+
|
31
|
+
Object.entries(vnode.props || {}).forEach(([name, value]: [string, any]) => {
|
32
|
+
if (name === 'children') {
|
33
|
+
const children = Array.isArray(value) ? value : [value];
|
34
|
+
children.forEach((child: any) => {
|
35
|
+
if (child != null) {
|
36
|
+
const childElement = createElement(child);
|
37
|
+
// Generate stats for testing
|
38
|
+
if (process.env.NODE_ENV === 'test' && typeof window !== 'undefined') {
|
39
|
+
// Create stats tracking
|
40
|
+
if (!globalObj.__renderStats) {
|
41
|
+
globalObj.__renderStats = {
|
42
|
+
elementsCreated: 0,
|
43
|
+
textNodesCreated: 0,
|
44
|
+
eventsAttached: 0,
|
45
|
+
renderTime: 0
|
46
|
+
};
|
47
|
+
|
48
|
+
// Write stats to file when tests complete
|
49
|
+
if (typeof afterAll === 'function') {
|
50
|
+
afterAll(() => {
|
51
|
+
try {
|
52
|
+
const fs = require('fs');
|
53
|
+
const path = require('path');
|
54
|
+
const statsPath = path.resolve(process.cwd(), 'jsx-runtime-stats.json');
|
55
|
+
fs.writeFileSync(statsPath, JSON.stringify(globalObj.__renderStats, null, 2));
|
56
|
+
console.log(`JSX runtime stats written to ${statsPath}`);
|
57
|
+
} catch (error) {
|
58
|
+
console.error('Failed to write stats file:', error);
|
59
|
+
}
|
60
|
+
});
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
// Increment stats based on element type
|
65
|
+
if (childElement instanceof Text) {
|
66
|
+
globalObj.__renderStats.textNodesCreated++;
|
67
|
+
} else {
|
68
|
+
globalObj.__renderStats.elementsCreated++;
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
element.appendChild(childElement);
|
73
|
+
}
|
74
|
+
});
|
75
|
+
} else if (name.startsWith('on')) {
|
76
|
+
const eventName = name.toLowerCase().substring(2);
|
77
|
+
element.addEventListener(eventName, value);
|
78
|
+
|
79
|
+
// Track event attachment in stats
|
80
|
+
if (process.env.NODE_ENV === 'test' && typeof window !== 'undefined' && globalObj.__renderStats) {
|
81
|
+
globalObj.__renderStats.eventsAttached++;
|
82
|
+
}
|
83
|
+
} else if (name === 'className') {
|
84
|
+
// Handle className specially by setting it as the class attribute
|
85
|
+
element.setAttribute('class', value);
|
86
|
+
} else if (name === 'style' && typeof value === 'object') {
|
87
|
+
// Handle style objects by merging them into element.style
|
88
|
+
Object.entries(value).forEach(([styleProp, styleValue]) => {
|
89
|
+
element.style[styleProp as any] = String(styleValue);
|
90
|
+
});
|
91
|
+
} else {
|
92
|
+
element.setAttribute(name, value);
|
93
|
+
}
|
94
|
+
});
|
95
|
+
|
96
|
+
return element;
|
97
|
+
}
|
98
|
+
|
99
|
+
export const Fragment = Symbol('Fragment');
|
@@ -0,0 +1,226 @@
|
|
1
|
+
import type { Component } from './component';
|
2
|
+
|
3
|
+
interface VNode {
|
4
|
+
type: string | Function;
|
5
|
+
props: Record<string, any>;
|
6
|
+
}
|
7
|
+
|
8
|
+
function jsx(type: string | Function, props: any, p0: string): VNode {
|
9
|
+
console.log('JSX Transform:', { type, props });
|
10
|
+
const processedProps = { ...props };
|
11
|
+
|
12
|
+
// Handle children properly
|
13
|
+
if (arguments.length > 2) {
|
14
|
+
processedProps.children = Array.prototype.slice.call(arguments, 2);
|
15
|
+
}
|
16
|
+
|
17
|
+
return { type, props: processedProps };
|
18
|
+
}
|
19
|
+
|
20
|
+
const Fragment = ({ children }: { children: any }) => children;
|
21
|
+
|
22
|
+
// Check if we're in a browser environment
|
23
|
+
const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
|
24
|
+
|
25
|
+
async function createElement(vnode: VNode | any): Promise<Node> {
|
26
|
+
console.log('Creating element from:', vnode);
|
27
|
+
|
28
|
+
// Create mock DOM elements when in Node environment
|
29
|
+
if (!isBrowser) {
|
30
|
+
// Return mock node objects in Node.js environment
|
31
|
+
if (vnode == null) {
|
32
|
+
return { nodeType: 3, textContent: '' } as any;
|
33
|
+
}
|
34
|
+
|
35
|
+
if (typeof vnode === 'boolean') {
|
36
|
+
return { nodeType: 3, textContent: '' } as any;
|
37
|
+
}
|
38
|
+
|
39
|
+
if (typeof vnode === 'number' || typeof vnode === 'string') {
|
40
|
+
return { nodeType: 3, textContent: String(vnode) } as any;
|
41
|
+
}
|
42
|
+
|
43
|
+
// Handle arrays
|
44
|
+
if (Array.isArray(vnode)) {
|
45
|
+
const fragment = { nodeType: 11, childNodes: [] } as any;
|
46
|
+
for (const child of vnode) {
|
47
|
+
const node = await createElement(child);
|
48
|
+
fragment.childNodes.push(node);
|
49
|
+
}
|
50
|
+
return fragment;
|
51
|
+
}
|
52
|
+
|
53
|
+
// Handle VNode
|
54
|
+
if ('type' in vnode && vnode.props !== undefined) {
|
55
|
+
const { type, props } = vnode;
|
56
|
+
|
57
|
+
// Handle function components
|
58
|
+
if (typeof type === 'function') {
|
59
|
+
try {
|
60
|
+
const result = await type(props || {});
|
61
|
+
const node = await createElement(result);
|
62
|
+
return node;
|
63
|
+
} catch (error) {
|
64
|
+
console.error('Error rendering component:', error);
|
65
|
+
return { nodeType: 3, textContent: '' } as any;
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
// Mock element creation
|
70
|
+
const element = {
|
71
|
+
nodeType: 1,
|
72
|
+
tagName: type,
|
73
|
+
attributes: {},
|
74
|
+
style: {},
|
75
|
+
childNodes: [],
|
76
|
+
setAttribute: function(key: string, value: string) {
|
77
|
+
this.attributes[key] = value;
|
78
|
+
},
|
79
|
+
appendChild: function(child: any) {
|
80
|
+
this.childNodes.push(child);
|
81
|
+
}
|
82
|
+
} as any;
|
83
|
+
|
84
|
+
// Handle props
|
85
|
+
for (const [key, value] of Object.entries(props || {})) {
|
86
|
+
if (key === 'children') continue;
|
87
|
+
if (key.startsWith('on') && typeof value === 'function') {
|
88
|
+
// Mock event handlers
|
89
|
+
const eventName = key.toLowerCase().slice(2);
|
90
|
+
if (!element.__events) {
|
91
|
+
element.__events = {};
|
92
|
+
}
|
93
|
+
element.__events[eventName] = value;
|
94
|
+
} else if (key === 'style' && typeof value === 'object') {
|
95
|
+
Object.assign(element.style, value);
|
96
|
+
} else if (key === 'className') {
|
97
|
+
element.setAttribute('class', String(value));
|
98
|
+
} else if (key !== 'key' && key !== 'ref') {
|
99
|
+
element.setAttribute(key, String(value));
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
// Handle children
|
104
|
+
const children = props?.children;
|
105
|
+
if (children != null) {
|
106
|
+
const childArray = Array.isArray(children) ? children.flat() : [children];
|
107
|
+
for (const child of childArray) {
|
108
|
+
const childNode = await createElement(child);
|
109
|
+
element.appendChild(childNode);
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
return element;
|
114
|
+
}
|
115
|
+
|
116
|
+
// Handle other objects by converting to string
|
117
|
+
return { nodeType: 3, textContent: String(vnode) } as any;
|
118
|
+
}
|
119
|
+
|
120
|
+
// Browser environment implementation
|
121
|
+
if (vnode == null) {
|
122
|
+
return document.createTextNode('');
|
123
|
+
}
|
124
|
+
|
125
|
+
if (typeof vnode === 'boolean') {
|
126
|
+
return document.createTextNode('');
|
127
|
+
}
|
128
|
+
|
129
|
+
if (typeof vnode === 'number' || typeof vnode === 'string') {
|
130
|
+
return document.createTextNode(String(vnode));
|
131
|
+
}
|
132
|
+
|
133
|
+
// Handle arrays
|
134
|
+
if (Array.isArray(vnode)) {
|
135
|
+
const fragment = document.createDocumentFragment();
|
136
|
+
for (const child of vnode) {
|
137
|
+
const node = await createElement(child);
|
138
|
+
fragment.appendChild(node);
|
139
|
+
}
|
140
|
+
return fragment;
|
141
|
+
}
|
142
|
+
|
143
|
+
// Handle VNode
|
144
|
+
if ('type' in vnode && vnode.props !== undefined) {
|
145
|
+
const { type, props } = vnode;
|
146
|
+
|
147
|
+
// Handle function components
|
148
|
+
if (typeof type === 'function') {
|
149
|
+
try {
|
150
|
+
const result = await type(props || {});
|
151
|
+
const node = await createElement(result);
|
152
|
+
if (node instanceof Element) {
|
153
|
+
node.setAttribute('data-component-id', type.name || type.toString());
|
154
|
+
}
|
155
|
+
return node;
|
156
|
+
} catch (error) {
|
157
|
+
console.error('Error rendering component:', error);
|
158
|
+
return document.createTextNode('');
|
159
|
+
}
|
160
|
+
}
|
161
|
+
|
162
|
+
// Create DOM element
|
163
|
+
const element = document.createElement(type as string);
|
164
|
+
|
165
|
+
// Handle props
|
166
|
+
for (const [key, value] of Object.entries(props || {})) {
|
167
|
+
if (key === 'children') continue;
|
168
|
+
if (key.startsWith('on') && typeof value === 'function') {
|
169
|
+
const eventName = key.toLowerCase().slice(2);
|
170
|
+
// Remove existing event listener if any
|
171
|
+
const existingHandler = (element as any).__events?.[eventName];
|
172
|
+
if (existingHandler) {
|
173
|
+
element.removeEventListener(eventName, existingHandler);
|
174
|
+
}
|
175
|
+
|
176
|
+
// Add new event listener
|
177
|
+
element.addEventListener(eventName, value as EventListener);
|
178
|
+
if (!(element as any).__events) {
|
179
|
+
(element as any).__events = {};
|
180
|
+
}
|
181
|
+
(element as any).__events[eventName] = value;
|
182
|
+
} else if (key === 'style' && typeof value === 'object') {
|
183
|
+
Object.assign(element.style, value);
|
184
|
+
} else if (key === 'className') {
|
185
|
+
element.setAttribute('class', String(value));
|
186
|
+
} else if (key !== 'key' && key !== 'ref') {
|
187
|
+
element.setAttribute(key, String(value));
|
188
|
+
}
|
189
|
+
}
|
190
|
+
|
191
|
+
// Handle children
|
192
|
+
const children = props?.children;
|
193
|
+
if (children != null) {
|
194
|
+
const childArray = Array.isArray(children) ? children.flat() : [children];
|
195
|
+
for (const child of childArray) {
|
196
|
+
const childNode = await createElement(child);
|
197
|
+
element.appendChild(childNode);
|
198
|
+
}
|
199
|
+
}
|
200
|
+
|
201
|
+
return element;
|
202
|
+
}
|
203
|
+
|
204
|
+
// Handle other objects by converting to string
|
205
|
+
return document.createTextNode(String(vnode));
|
206
|
+
}
|
207
|
+
|
208
|
+
// Export named functions and aliases without duplicates
|
209
|
+
export {
|
210
|
+
jsx,
|
211
|
+
jsx as jsxs,
|
212
|
+
jsx as jsxDEV,
|
213
|
+
Fragment,
|
214
|
+
createElement
|
215
|
+
};
|
216
|
+
|
217
|
+
// Named exports object
|
218
|
+
const jsxRuntime = {
|
219
|
+
jsx,
|
220
|
+
jsxs: jsx,
|
221
|
+
jsxDEV: jsx,
|
222
|
+
Fragment,
|
223
|
+
createElement
|
224
|
+
};
|
225
|
+
|
226
|
+
export default jsxRuntime;
|
package/src/renderer.ts
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
import { createElement } from './jsx-runtime';
|
2
|
+
import { prepareRender, finishRender, setRenderCallback } from './hooks';
|
3
|
+
import { batchUpdates } from './batch';
|
4
|
+
|
5
|
+
// Track hydration state
|
6
|
+
let isHydrating = false;
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Hydrates a server-rendered component
|
10
|
+
* @param element The element to hydrate
|
11
|
+
* @param container The container element that contains server-rendered HTML
|
12
|
+
*/
|
13
|
+
export async function hydrate(element: any, container: HTMLElement) {
|
14
|
+
isHydrating = true;
|
15
|
+
try {
|
16
|
+
await render(element, container);
|
17
|
+
} finally {
|
18
|
+
isHydrating = false;
|
19
|
+
}
|
20
|
+
}
|
21
|
+
|
22
|
+
/**
|
23
|
+
* Renders a component to the DOM
|
24
|
+
* @param element The element to render
|
25
|
+
* @param container The container to render into
|
26
|
+
*/
|
27
|
+
export async function render(element: any, container: HTMLElement) {
|
28
|
+
console.log('Rendering to:', container.id || 'unnamed-container');
|
29
|
+
|
30
|
+
batchUpdates(async () => {
|
31
|
+
const rendererId = prepareRender();
|
32
|
+
try {
|
33
|
+
setRenderCallback(render, element, container);
|
34
|
+
const domNode = await createElement(element);
|
35
|
+
|
36
|
+
// Don't clear container if we're hydrating
|
37
|
+
if (!isHydrating) {
|
38
|
+
container.innerHTML = '';
|
39
|
+
}
|
40
|
+
|
41
|
+
// When hydrating, we should match and update existing nodes
|
42
|
+
// rather than appending new ones
|
43
|
+
if (isHydrating && container.firstChild) {
|
44
|
+
// During hydration, we assume the structure matches
|
45
|
+
// and just attach event handlers without replacing DOM
|
46
|
+
console.log('Hydrating existing DOM');
|
47
|
+
} else {
|
48
|
+
container.appendChild(domNode);
|
49
|
+
}
|
50
|
+
|
51
|
+
} finally {
|
52
|
+
finishRender();
|
53
|
+
}
|
54
|
+
});
|
55
|
+
}
|
@@ -0,0 +1,114 @@
|
|
1
|
+
import { prepareRender, finishRender, setRenderCallback } from './hooks';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Renders a component to a string
|
5
|
+
*/
|
6
|
+
export async function renderToString(element: any): Promise<string> {
|
7
|
+
// Setup render context
|
8
|
+
const rendererId = prepareRender();
|
9
|
+
setRenderCallback(() => {}, element, null as any);
|
10
|
+
|
11
|
+
try {
|
12
|
+
if (element == null) return '';
|
13
|
+
|
14
|
+
if (typeof element === 'boolean') return '';
|
15
|
+
|
16
|
+
if (typeof element === 'number' || typeof element === 'string') {
|
17
|
+
return escapeHtml(String(element));
|
18
|
+
}
|
19
|
+
|
20
|
+
if (Array.isArray(element)) {
|
21
|
+
const children = await Promise.all(element.map(renderToString));
|
22
|
+
return children.join('');
|
23
|
+
}
|
24
|
+
|
25
|
+
if ('type' in element && element.props !== undefined) {
|
26
|
+
const { type, props } = element;
|
27
|
+
|
28
|
+
// Handle function components
|
29
|
+
if (typeof type === 'function') {
|
30
|
+
try {
|
31
|
+
// Prepare new render context for the component
|
32
|
+
prepareRender();
|
33
|
+
const result = await type(props || {});
|
34
|
+
const html = await renderToString(result);
|
35
|
+
finishRender();
|
36
|
+
return html;
|
37
|
+
} catch (error) {
|
38
|
+
console.error('Error rendering component:', error);
|
39
|
+
return '';
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
// Handle Fragment special case
|
44
|
+
if (type === Symbol.for('react.fragment') || type.name === 'Fragment') {
|
45
|
+
if (props.children) {
|
46
|
+
const children = Array.isArray(props.children) ? props.children : [props.children];
|
47
|
+
const renderedChildren = await Promise.all(children.map(renderToString));
|
48
|
+
return renderedChildren.join('');
|
49
|
+
}
|
50
|
+
return '';
|
51
|
+
}
|
52
|
+
|
53
|
+
// Handle regular elements
|
54
|
+
let html = `<${type}`;
|
55
|
+
|
56
|
+
// Add attributes, skipping internal ones like 'key'
|
57
|
+
for (const [key, value] of Object.entries(props || {})) {
|
58
|
+
if (key === 'children' || key === 'key') continue;
|
59
|
+
if (key === 'className') {
|
60
|
+
html += ` class="${escapeHtml(String(value))}"`;
|
61
|
+
} else if (key === 'style' && typeof value === 'object') {
|
62
|
+
html += ` style="${stringifyStyle(value || {})}"`;
|
63
|
+
} else if (!key.startsWith('on')) {
|
64
|
+
html += ` ${key}="${escapeHtml(String(value))}"`;
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
// Handle self-closing tags
|
69
|
+
const voidElements = new Set([
|
70
|
+
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
|
71
|
+
'link', 'meta', 'param', 'source', 'track', 'wbr'
|
72
|
+
]);
|
73
|
+
|
74
|
+
if (voidElements.has(type)) {
|
75
|
+
return html + '/>';
|
76
|
+
}
|
77
|
+
|
78
|
+
html += '>';
|
79
|
+
|
80
|
+
// Add children
|
81
|
+
if (props?.children) {
|
82
|
+
const children = Array.isArray(props.children) ? props.children : [props.children];
|
83
|
+
for (const child of children) {
|
84
|
+
html += await renderToString(child);
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
return html + `</${type}>`;
|
89
|
+
}
|
90
|
+
|
91
|
+
return escapeHtml(String(element));
|
92
|
+
} finally {
|
93
|
+
finishRender();
|
94
|
+
}
|
95
|
+
}
|
96
|
+
|
97
|
+
function escapeHtml(str: string): string {
|
98
|
+
return str
|
99
|
+
.replace(/&/g, '&')
|
100
|
+
.replace(/</g, '<')
|
101
|
+
.replace(/>/g, '>')
|
102
|
+
.replace(/"/g, '"')
|
103
|
+
.replace(/'/g, ''');
|
104
|
+
}
|
105
|
+
|
106
|
+
function stringifyStyle(style: Record<string, any>): string {
|
107
|
+
return Object.entries(style)
|
108
|
+
.map(([key, value]) => `${hyphenate(key)}:${value}`)
|
109
|
+
.join(';');
|
110
|
+
}
|
111
|
+
|
112
|
+
function hyphenate(str: string): string {
|
113
|
+
return str.replace(/[A-Z]/g, match => '-' + match.toLowerCase());
|
114
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
declare module 'bcrypt' {
|
2
|
+
/**
|
3
|
+
* Generate a salt
|
4
|
+
* @param rounds Number of rounds to use, defaults to 10
|
5
|
+
* @param callback Callback function (err, salt)
|
6
|
+
*/
|
7
|
+
export function genSalt(rounds?: number, callback?: (err: Error | null, salt: string) => void): Promise<string>;
|
8
|
+
|
9
|
+
/**
|
10
|
+
* Generate a hash for the provided string
|
11
|
+
* @param data Data to hash
|
12
|
+
* @param saltOrRounds Salt or number of rounds to use
|
13
|
+
* @param callback Callback function (err, hash)
|
14
|
+
*/
|
15
|
+
export function hash(data: string, saltOrRounds: string | number, callback?: (err: Error | null, hash: string) => void): Promise<string>;
|
16
|
+
|
17
|
+
/**
|
18
|
+
* Compare provided data with a hash
|
19
|
+
* @param data Data to compare
|
20
|
+
* @param encrypted Hash to compare against
|
21
|
+
* @param callback Callback function (err, result)
|
22
|
+
*/
|
23
|
+
export function compare(data: string, encrypted: string, callback?: (err: Error | null, result: boolean) => void): Promise<boolean>;
|
24
|
+
|
25
|
+
/**
|
26
|
+
* Get the number of rounds used in the specified hash
|
27
|
+
* @param encrypted Hash to extract rounds from
|
28
|
+
*/
|
29
|
+
export function getRounds(encrypted: string): number;
|
30
|
+
}
|