hadars 0.1.35 → 0.1.37
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 +77 -0
- package/cli-lib.ts +90 -5
- package/dist/chunk-F7IIMM3J.js +1060 -0
- package/dist/chunk-UNQSQIOO.js +60 -0
- package/dist/cli.js +285 -261
- package/dist/hadars-B3b_6sj2.d.cts +188 -0
- package/dist/hadars-B3b_6sj2.d.ts +188 -0
- package/dist/index.cjs +11 -19
- package/dist/index.d.cts +105 -0
- package/dist/index.d.ts +4 -175
- package/dist/index.js +14 -42
- package/dist/{jsx-runtime-97ca74a5.d.ts → jsx-runtime-BOYrELJb.d.cts} +1 -1
- package/dist/jsx-runtime-BOYrELJb.d.ts +18 -0
- package/dist/lambda.cjs +1405 -0
- package/dist/lambda.d.cts +93 -0
- package/dist/lambda.d.ts +93 -0
- package/dist/lambda.js +499 -0
- package/dist/loader.cjs +22 -44
- package/dist/slim-react/index.cjs +29 -58
- package/dist/slim-react/index.d.cts +190 -0
- package/dist/slim-react/index.d.ts +3 -3
- package/dist/slim-react/index.js +34 -1043
- package/dist/slim-react/jsx-runtime.d.cts +1 -0
- package/dist/slim-react/jsx-runtime.d.ts +1 -1
- package/dist/ssr-render-worker.js +44 -78
- package/dist/ssr-watch.js +24 -7
- package/dist/utils/Head.tsx +2 -2
- package/package.json +11 -6
- package/src/build.ts +8 -166
- package/src/lambda.ts +287 -0
- package/src/types/hadars.ts +10 -0
- package/src/utils/Head.tsx +2 -2
- package/src/utils/rspack.ts +27 -0
- package/src/utils/ssrHandler.ts +185 -0
package/dist/lambda.cjs
ADDED
|
@@ -0,0 +1,1405 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/lambda.ts
|
|
31
|
+
var lambda_exports = {};
|
|
32
|
+
__export(lambda_exports, {
|
|
33
|
+
createLambdaHandler: () => createLambdaHandler
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(lambda_exports);
|
|
36
|
+
var import_react = require("react");
|
|
37
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
38
|
+
var import_node_url = require("url");
|
|
39
|
+
var import_promises2 = __toESM(require("fs/promises"), 1);
|
|
40
|
+
|
|
41
|
+
// src/utils/proxyHandler.tsx
|
|
42
|
+
var cloneHeaders = (headers) => {
|
|
43
|
+
return new Headers(headers);
|
|
44
|
+
};
|
|
45
|
+
var getCORSHeaders = (req) => {
|
|
46
|
+
const origin = req.headers.get("Origin") || "*";
|
|
47
|
+
return {
|
|
48
|
+
"Access-Control-Allow-Origin": origin,
|
|
49
|
+
"Access-Control-Allow-Methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
|
|
50
|
+
"Access-Control-Allow-Headers": req.headers.get("Access-Control-Request-Headers") || "*",
|
|
51
|
+
"Access-Control-Allow-Credentials": "true"
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
var createProxyHandler = (options) => {
|
|
55
|
+
const { proxy, proxyCORS } = options;
|
|
56
|
+
if (!proxy) {
|
|
57
|
+
return () => void 0;
|
|
58
|
+
}
|
|
59
|
+
if (typeof proxy === "function") {
|
|
60
|
+
return async (req) => {
|
|
61
|
+
if (req.method === "OPTIONS" && options.proxyCORS) {
|
|
62
|
+
return new Response(null, {
|
|
63
|
+
status: 204,
|
|
64
|
+
headers: getCORSHeaders(req)
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
const res = await proxy(req);
|
|
68
|
+
if (res && proxyCORS) {
|
|
69
|
+
const modifiedHeaders = new Headers(res.headers);
|
|
70
|
+
Object.entries(getCORSHeaders(req)).forEach(([key, value]) => {
|
|
71
|
+
modifiedHeaders.set(key, value);
|
|
72
|
+
});
|
|
73
|
+
return new Response(res.body, {
|
|
74
|
+
status: res.status,
|
|
75
|
+
statusText: res.statusText,
|
|
76
|
+
headers: modifiedHeaders
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return res || void 0;
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
const proxyRules = Object.entries(proxy).sort((a, b) => b[0].length - a[0].length);
|
|
83
|
+
return async (req) => {
|
|
84
|
+
for (const [path, target] of proxyRules) {
|
|
85
|
+
if (req.pathname.startsWith(path)) {
|
|
86
|
+
if (req.method === "OPTIONS" && proxyCORS) {
|
|
87
|
+
return new Response(null, {
|
|
88
|
+
status: 204,
|
|
89
|
+
headers: getCORSHeaders(req)
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
const targetURL = new URL(target);
|
|
93
|
+
targetURL.pathname = targetURL.pathname.replace(/\/$/, "") + req.pathname.slice(path.length);
|
|
94
|
+
targetURL.search = req.search;
|
|
95
|
+
const sendHeaders = cloneHeaders(req.headers);
|
|
96
|
+
sendHeaders.set("Host", targetURL.host);
|
|
97
|
+
const hasBody = !["GET", "HEAD"].includes(req.method);
|
|
98
|
+
const proxyReq = new Request(targetURL.toString(), {
|
|
99
|
+
method: req.method,
|
|
100
|
+
headers: sendHeaders,
|
|
101
|
+
body: hasBody ? req.body : void 0,
|
|
102
|
+
redirect: "follow",
|
|
103
|
+
// Node.js (undici) requires duplex:'half' when body is a ReadableStream
|
|
104
|
+
...hasBody ? { duplex: "half" } : {}
|
|
105
|
+
});
|
|
106
|
+
const res = await fetch(proxyReq);
|
|
107
|
+
const body = await res.arrayBuffer();
|
|
108
|
+
const clonedRes = new Headers(res.headers);
|
|
109
|
+
clonedRes.delete("content-length");
|
|
110
|
+
clonedRes.delete("content-encoding");
|
|
111
|
+
if (proxyCORS) {
|
|
112
|
+
Object.entries(getCORSHeaders(req)).forEach(([key, value]) => {
|
|
113
|
+
clonedRes.set(key, value);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return new Response(body, {
|
|
117
|
+
status: res.status,
|
|
118
|
+
statusText: res.statusText,
|
|
119
|
+
headers: clonedRes
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return void 0;
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// src/utils/cookies.ts
|
|
128
|
+
var parseCookies = (cookieString) => {
|
|
129
|
+
const cookies = {};
|
|
130
|
+
if (!cookieString) {
|
|
131
|
+
return cookies;
|
|
132
|
+
}
|
|
133
|
+
const pairs = cookieString.split(";");
|
|
134
|
+
for (const pair of pairs) {
|
|
135
|
+
const index = pair.indexOf("=");
|
|
136
|
+
if (index > -1) {
|
|
137
|
+
const key = pair.slice(0, index).trim();
|
|
138
|
+
const value = pair.slice(index + 1).trim();
|
|
139
|
+
cookies[key] = decodeURIComponent(value);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return cookies;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// src/utils/request.tsx
|
|
146
|
+
var parseRequest = (request) => {
|
|
147
|
+
const url = new URL(request.url);
|
|
148
|
+
const cookies = request.headers.get("Cookie") || "";
|
|
149
|
+
const cookieRecord = parseCookies(cookies);
|
|
150
|
+
return Object.assign(request, { pathname: url.pathname, search: url.search, location: url.pathname + url.search, cookies: cookieRecord });
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// src/utils/staticFile.ts
|
|
154
|
+
var import_promises = require("fs/promises");
|
|
155
|
+
var MIME = {
|
|
156
|
+
html: "text/html; charset=utf-8",
|
|
157
|
+
htm: "text/html; charset=utf-8",
|
|
158
|
+
css: "text/css",
|
|
159
|
+
js: "application/javascript",
|
|
160
|
+
mjs: "application/javascript",
|
|
161
|
+
cjs: "application/javascript",
|
|
162
|
+
json: "application/json",
|
|
163
|
+
map: "application/json",
|
|
164
|
+
png: "image/png",
|
|
165
|
+
jpg: "image/jpeg",
|
|
166
|
+
jpeg: "image/jpeg",
|
|
167
|
+
gif: "image/gif",
|
|
168
|
+
webp: "image/webp",
|
|
169
|
+
svg: "image/svg+xml",
|
|
170
|
+
ico: "image/x-icon",
|
|
171
|
+
woff: "font/woff",
|
|
172
|
+
woff2: "font/woff2",
|
|
173
|
+
ttf: "font/ttf",
|
|
174
|
+
otf: "font/otf",
|
|
175
|
+
txt: "text/plain",
|
|
176
|
+
xml: "application/xml",
|
|
177
|
+
pdf: "application/pdf"
|
|
178
|
+
};
|
|
179
|
+
async function tryServeFile(filePath) {
|
|
180
|
+
try {
|
|
181
|
+
await (0, import_promises.stat)(filePath);
|
|
182
|
+
} catch {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
try {
|
|
186
|
+
const data = await (0, import_promises.readFile)(filePath);
|
|
187
|
+
const ext = filePath.split(".").pop()?.toLowerCase() ?? "";
|
|
188
|
+
const contentType = MIME[ext] ?? "application/octet-stream";
|
|
189
|
+
return new Response(data.buffer, { headers: { "Content-Type": contentType } });
|
|
190
|
+
} catch {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// src/slim-react/types.ts
|
|
196
|
+
var SLIM_ELEMENT = Symbol.for("react.element");
|
|
197
|
+
var REACT19_ELEMENT = Symbol.for("react.transitional.element");
|
|
198
|
+
var FRAGMENT_TYPE = Symbol.for("react.fragment");
|
|
199
|
+
var SUSPENSE_TYPE = Symbol.for("react.suspense");
|
|
200
|
+
|
|
201
|
+
// src/slim-react/jsx.ts
|
|
202
|
+
function createElement(type, props, ...children) {
|
|
203
|
+
const normalizedProps = { ...props || {} };
|
|
204
|
+
if (children.length === 1) {
|
|
205
|
+
normalizedProps.children = children[0];
|
|
206
|
+
} else if (children.length > 1) {
|
|
207
|
+
normalizedProps.children = children;
|
|
208
|
+
}
|
|
209
|
+
const key = normalizedProps.key ?? null;
|
|
210
|
+
delete normalizedProps.key;
|
|
211
|
+
return {
|
|
212
|
+
$$typeof: SLIM_ELEMENT,
|
|
213
|
+
type,
|
|
214
|
+
props: normalizedProps,
|
|
215
|
+
key
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// src/slim-react/renderContext.ts
|
|
220
|
+
var MAP_KEY = "__slimReactContextMap";
|
|
221
|
+
var _g = globalThis;
|
|
222
|
+
if (!("__slimReactContextMap" in _g)) _g[MAP_KEY] = null;
|
|
223
|
+
function swapContextMap(map) {
|
|
224
|
+
const prev = _g[MAP_KEY];
|
|
225
|
+
_g[MAP_KEY] = map;
|
|
226
|
+
return prev;
|
|
227
|
+
}
|
|
228
|
+
function captureMap() {
|
|
229
|
+
return _g[MAP_KEY];
|
|
230
|
+
}
|
|
231
|
+
function getContextValue(context) {
|
|
232
|
+
const map = _g[MAP_KEY];
|
|
233
|
+
if (map && map.has(context)) return map.get(context);
|
|
234
|
+
const c = context;
|
|
235
|
+
return "_defaultValue" in c ? c._defaultValue : c._currentValue;
|
|
236
|
+
}
|
|
237
|
+
function pushContextValue(context, value) {
|
|
238
|
+
const map = _g[MAP_KEY];
|
|
239
|
+
const c = context;
|
|
240
|
+
const prev = map && map.has(context) ? map.get(context) : "_defaultValue" in c ? c._defaultValue : c._currentValue;
|
|
241
|
+
map?.set(context, value);
|
|
242
|
+
return prev;
|
|
243
|
+
}
|
|
244
|
+
function popContextValue(context, prev) {
|
|
245
|
+
_g[MAP_KEY]?.set(context, prev);
|
|
246
|
+
}
|
|
247
|
+
var GLOBAL_KEY = "__slimReactRenderState";
|
|
248
|
+
var EMPTY = { id: 1, overflow: "" };
|
|
249
|
+
function s() {
|
|
250
|
+
const g = globalThis;
|
|
251
|
+
if (!g[GLOBAL_KEY]) {
|
|
252
|
+
g[GLOBAL_KEY] = { currentTreeContext: { ...EMPTY }, localIdCounter: 0, idPrefix: "" };
|
|
253
|
+
}
|
|
254
|
+
return g[GLOBAL_KEY];
|
|
255
|
+
}
|
|
256
|
+
function resetRenderState(idPrefix = "") {
|
|
257
|
+
const st = s();
|
|
258
|
+
st.currentTreeContext = { ...EMPTY };
|
|
259
|
+
st.localIdCounter = 0;
|
|
260
|
+
st.idPrefix = idPrefix;
|
|
261
|
+
}
|
|
262
|
+
function pushTreeContext(totalChildren, index) {
|
|
263
|
+
const st = s();
|
|
264
|
+
const saved = { ...st.currentTreeContext };
|
|
265
|
+
const baseIdWithLeadingBit = st.currentTreeContext.id;
|
|
266
|
+
const baseOverflow = st.currentTreeContext.overflow;
|
|
267
|
+
const baseLength = 31 - Math.clz32(baseIdWithLeadingBit);
|
|
268
|
+
let baseId = baseIdWithLeadingBit & ~(1 << baseLength);
|
|
269
|
+
const slot = index + 1;
|
|
270
|
+
const newBits = 32 - Math.clz32(totalChildren);
|
|
271
|
+
const length = newBits + baseLength;
|
|
272
|
+
if (30 < length) {
|
|
273
|
+
const numberOfOverflowBits = baseLength - baseLength % 5;
|
|
274
|
+
const overflowStr = (baseId & (1 << numberOfOverflowBits) - 1).toString(32);
|
|
275
|
+
baseId >>= numberOfOverflowBits;
|
|
276
|
+
const newBaseLength = baseLength - numberOfOverflowBits;
|
|
277
|
+
st.currentTreeContext = {
|
|
278
|
+
id: 1 << newBits + newBaseLength | slot << newBaseLength | baseId,
|
|
279
|
+
overflow: overflowStr + baseOverflow
|
|
280
|
+
};
|
|
281
|
+
} else {
|
|
282
|
+
st.currentTreeContext = {
|
|
283
|
+
id: 1 << length | slot << baseLength | baseId,
|
|
284
|
+
overflow: baseOverflow
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
return saved;
|
|
288
|
+
}
|
|
289
|
+
function popTreeContext(saved) {
|
|
290
|
+
s().currentTreeContext = saved;
|
|
291
|
+
}
|
|
292
|
+
function pushComponentScope() {
|
|
293
|
+
const st = s();
|
|
294
|
+
const saved = st.localIdCounter;
|
|
295
|
+
st.localIdCounter = 0;
|
|
296
|
+
return saved;
|
|
297
|
+
}
|
|
298
|
+
function popComponentScope(saved) {
|
|
299
|
+
s().localIdCounter = saved;
|
|
300
|
+
}
|
|
301
|
+
function componentCalledUseId() {
|
|
302
|
+
return s().localIdCounter > 0;
|
|
303
|
+
}
|
|
304
|
+
function snapshotContext() {
|
|
305
|
+
const st = s();
|
|
306
|
+
return { tree: { ...st.currentTreeContext }, localId: st.localIdCounter };
|
|
307
|
+
}
|
|
308
|
+
function restoreContext(snap) {
|
|
309
|
+
const st = s();
|
|
310
|
+
st.currentTreeContext = { ...snap.tree };
|
|
311
|
+
st.localIdCounter = snap.localId;
|
|
312
|
+
}
|
|
313
|
+
function getTreeId() {
|
|
314
|
+
const { id, overflow } = s().currentTreeContext;
|
|
315
|
+
if (id === 1) return overflow;
|
|
316
|
+
const stripped = (id & ~(1 << 31 - Math.clz32(id))).toString(32);
|
|
317
|
+
return stripped + overflow;
|
|
318
|
+
}
|
|
319
|
+
function makeId() {
|
|
320
|
+
const st = s();
|
|
321
|
+
const treeId = getTreeId();
|
|
322
|
+
const n = st.localIdCounter++;
|
|
323
|
+
let id = "_R_" + st.idPrefix + treeId;
|
|
324
|
+
if (n > 0) id += "H" + n.toString(32);
|
|
325
|
+
return id + "_";
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// src/slim-react/hooks.ts
|
|
329
|
+
function useState(initialState) {
|
|
330
|
+
const value = typeof initialState === "function" ? initialState() : initialState;
|
|
331
|
+
return [value, () => {
|
|
332
|
+
}];
|
|
333
|
+
}
|
|
334
|
+
function useReducer(_reducer, initialState) {
|
|
335
|
+
return [initialState, () => {
|
|
336
|
+
}];
|
|
337
|
+
}
|
|
338
|
+
function useEffect(_effect, _deps) {
|
|
339
|
+
}
|
|
340
|
+
function useLayoutEffect(_effect, _deps) {
|
|
341
|
+
}
|
|
342
|
+
function useInsertionEffect(_effect, _deps) {
|
|
343
|
+
}
|
|
344
|
+
function useRef(initialValue) {
|
|
345
|
+
return { current: initialValue };
|
|
346
|
+
}
|
|
347
|
+
function useMemo(factory, _deps) {
|
|
348
|
+
return factory();
|
|
349
|
+
}
|
|
350
|
+
function useCallback(callback, _deps) {
|
|
351
|
+
return callback;
|
|
352
|
+
}
|
|
353
|
+
function useDebugValue(_value, _format) {
|
|
354
|
+
}
|
|
355
|
+
function useImperativeHandle(_ref, _createHandle, _deps) {
|
|
356
|
+
}
|
|
357
|
+
function useSyncExternalStore(_subscribe, getSnapshot, getServerSnapshot) {
|
|
358
|
+
return (getServerSnapshot || getSnapshot)();
|
|
359
|
+
}
|
|
360
|
+
function useTransition() {
|
|
361
|
+
return [false, (cb) => cb()];
|
|
362
|
+
}
|
|
363
|
+
function useDeferredValue(value) {
|
|
364
|
+
return value;
|
|
365
|
+
}
|
|
366
|
+
function useOptimistic(passthrough) {
|
|
367
|
+
return [passthrough, () => {
|
|
368
|
+
}];
|
|
369
|
+
}
|
|
370
|
+
function useActionState(_action, initialState, _permalink) {
|
|
371
|
+
return [initialState, () => {
|
|
372
|
+
}, false];
|
|
373
|
+
}
|
|
374
|
+
function use(usable) {
|
|
375
|
+
if (typeof usable === "object" && usable !== null && ("_currentValue" in usable || "_defaultValue" in usable)) {
|
|
376
|
+
return getContextValue(usable);
|
|
377
|
+
}
|
|
378
|
+
const promise = usable;
|
|
379
|
+
if (promise.status === "fulfilled") return promise.value;
|
|
380
|
+
if (promise.status === "rejected") throw promise.reason;
|
|
381
|
+
throw promise;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// src/slim-react/dispatcher.ts
|
|
385
|
+
var ReactNS = __toESM(require("react"), 1);
|
|
386
|
+
var _internals = ReactNS.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE;
|
|
387
|
+
var slimDispatcher = {
|
|
388
|
+
useId: makeId,
|
|
389
|
+
readContext: (ctx) => getContextValue(ctx),
|
|
390
|
+
useContext: (ctx) => getContextValue(ctx),
|
|
391
|
+
useState,
|
|
392
|
+
useReducer,
|
|
393
|
+
useEffect,
|
|
394
|
+
useLayoutEffect,
|
|
395
|
+
useInsertionEffect,
|
|
396
|
+
useRef,
|
|
397
|
+
useMemo,
|
|
398
|
+
useCallback,
|
|
399
|
+
useDebugValue,
|
|
400
|
+
useImperativeHandle,
|
|
401
|
+
useSyncExternalStore,
|
|
402
|
+
useTransition,
|
|
403
|
+
useDeferredValue,
|
|
404
|
+
useOptimistic,
|
|
405
|
+
useActionState,
|
|
406
|
+
use,
|
|
407
|
+
// React internals that compiled output may call
|
|
408
|
+
useMemoCache: (size) => new Array(size).fill(void 0),
|
|
409
|
+
useCacheRefresh: () => () => {
|
|
410
|
+
},
|
|
411
|
+
useHostTransitionStatus: () => false
|
|
412
|
+
};
|
|
413
|
+
function installDispatcher() {
|
|
414
|
+
if (!_internals) return null;
|
|
415
|
+
const prev = _internals.H;
|
|
416
|
+
_internals.H = slimDispatcher;
|
|
417
|
+
return prev;
|
|
418
|
+
}
|
|
419
|
+
function restoreDispatcher(prev) {
|
|
420
|
+
if (_internals) _internals.H = prev;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// src/slim-react/render.ts
|
|
424
|
+
var VOID_ELEMENTS = /* @__PURE__ */ new Set([
|
|
425
|
+
"area",
|
|
426
|
+
"base",
|
|
427
|
+
"br",
|
|
428
|
+
"col",
|
|
429
|
+
"embed",
|
|
430
|
+
"hr",
|
|
431
|
+
"img",
|
|
432
|
+
"input",
|
|
433
|
+
"link",
|
|
434
|
+
"meta",
|
|
435
|
+
"param",
|
|
436
|
+
"source",
|
|
437
|
+
"track",
|
|
438
|
+
"wbr"
|
|
439
|
+
]);
|
|
440
|
+
var HTML_ESC = { "&": "&", "<": "<", ">": ">", "'": "'" };
|
|
441
|
+
function escapeHtml(str) {
|
|
442
|
+
return str.replace(/[&<>']/g, (c) => HTML_ESC[c]);
|
|
443
|
+
}
|
|
444
|
+
var ATTR_ESC = { "&": "&", '"': """, "<": "<", ">": ">" };
|
|
445
|
+
function escapeAttr(str) {
|
|
446
|
+
return str.replace(/[&"<>]/g, (c) => ATTR_ESC[c]);
|
|
447
|
+
}
|
|
448
|
+
function styleObjectToString(style) {
|
|
449
|
+
let result = "";
|
|
450
|
+
for (const key in style) {
|
|
451
|
+
if (result) result += ";";
|
|
452
|
+
const cssKey = key.replace(/[A-Z]/g, (m) => "-" + m.toLowerCase());
|
|
453
|
+
result += cssKey + ":" + style[key];
|
|
454
|
+
}
|
|
455
|
+
return result;
|
|
456
|
+
}
|
|
457
|
+
var SVG_ATTR_MAP = {
|
|
458
|
+
// Presentation / geometry
|
|
459
|
+
accentHeight: "accent-height",
|
|
460
|
+
alignmentBaseline: "alignment-baseline",
|
|
461
|
+
arabicForm: "arabic-form",
|
|
462
|
+
baselineShift: "baseline-shift",
|
|
463
|
+
capHeight: "cap-height",
|
|
464
|
+
clipPath: "clip-path",
|
|
465
|
+
clipRule: "clip-rule",
|
|
466
|
+
colorInterpolation: "color-interpolation",
|
|
467
|
+
colorInterpolationFilters: "color-interpolation-filters",
|
|
468
|
+
colorProfile: "color-profile",
|
|
469
|
+
dominantBaseline: "dominant-baseline",
|
|
470
|
+
enableBackground: "enable-background",
|
|
471
|
+
fillOpacity: "fill-opacity",
|
|
472
|
+
fillRule: "fill-rule",
|
|
473
|
+
floodColor: "flood-color",
|
|
474
|
+
floodOpacity: "flood-opacity",
|
|
475
|
+
fontFamily: "font-family",
|
|
476
|
+
fontSize: "font-size",
|
|
477
|
+
fontSizeAdjust: "font-size-adjust",
|
|
478
|
+
fontStretch: "font-stretch",
|
|
479
|
+
fontStyle: "font-style",
|
|
480
|
+
fontVariant: "font-variant",
|
|
481
|
+
fontWeight: "font-weight",
|
|
482
|
+
glyphName: "glyph-name",
|
|
483
|
+
glyphOrientationHorizontal: "glyph-orientation-horizontal",
|
|
484
|
+
glyphOrientationVertical: "glyph-orientation-vertical",
|
|
485
|
+
horizAdvX: "horiz-adv-x",
|
|
486
|
+
horizOriginX: "horiz-origin-x",
|
|
487
|
+
imageRendering: "image-rendering",
|
|
488
|
+
letterSpacing: "letter-spacing",
|
|
489
|
+
lightingColor: "lighting-color",
|
|
490
|
+
markerEnd: "marker-end",
|
|
491
|
+
markerMid: "marker-mid",
|
|
492
|
+
markerStart: "marker-start",
|
|
493
|
+
overlinePosition: "overline-position",
|
|
494
|
+
overlineThickness: "overline-thickness",
|
|
495
|
+
paintOrder: "paint-order",
|
|
496
|
+
panose1: "panose-1",
|
|
497
|
+
pointerEvents: "pointer-events",
|
|
498
|
+
renderingIntent: "rendering-intent",
|
|
499
|
+
shapeRendering: "shape-rendering",
|
|
500
|
+
stopColor: "stop-color",
|
|
501
|
+
stopOpacity: "stop-opacity",
|
|
502
|
+
strikethroughPosition: "strikethrough-position",
|
|
503
|
+
strikethroughThickness: "strikethrough-thickness",
|
|
504
|
+
strokeDasharray: "stroke-dasharray",
|
|
505
|
+
strokeDashoffset: "stroke-dashoffset",
|
|
506
|
+
strokeLinecap: "stroke-linecap",
|
|
507
|
+
strokeLinejoin: "stroke-linejoin",
|
|
508
|
+
strokeMiterlimit: "stroke-miterlimit",
|
|
509
|
+
strokeOpacity: "stroke-opacity",
|
|
510
|
+
strokeWidth: "stroke-width",
|
|
511
|
+
textAnchor: "text-anchor",
|
|
512
|
+
textDecoration: "text-decoration",
|
|
513
|
+
textRendering: "text-rendering",
|
|
514
|
+
underlinePosition: "underline-position",
|
|
515
|
+
underlineThickness: "underline-thickness",
|
|
516
|
+
unicodeBidi: "unicode-bidi",
|
|
517
|
+
unicodeRange: "unicode-range",
|
|
518
|
+
unitsPerEm: "units-per-em",
|
|
519
|
+
vAlphabetic: "v-alphabetic",
|
|
520
|
+
vHanging: "v-hanging",
|
|
521
|
+
vIdeographic: "v-ideographic",
|
|
522
|
+
vMathematical: "v-mathematical",
|
|
523
|
+
vertAdvY: "vert-adv-y",
|
|
524
|
+
vertOriginX: "vert-origin-x",
|
|
525
|
+
vertOriginY: "vert-origin-y",
|
|
526
|
+
wordSpacing: "word-spacing",
|
|
527
|
+
writingMode: "writing-mode",
|
|
528
|
+
xHeight: "x-height",
|
|
529
|
+
// Namespace-prefixed
|
|
530
|
+
xlinkActuate: "xlink:actuate",
|
|
531
|
+
xlinkArcrole: "xlink:arcrole",
|
|
532
|
+
xlinkHref: "xlink:href",
|
|
533
|
+
xlinkRole: "xlink:role",
|
|
534
|
+
xlinkShow: "xlink:show",
|
|
535
|
+
xlinkTitle: "xlink:title",
|
|
536
|
+
xlinkType: "xlink:type",
|
|
537
|
+
xmlBase: "xml:base",
|
|
538
|
+
xmlLang: "xml:lang",
|
|
539
|
+
xmlSpace: "xml:space",
|
|
540
|
+
xmlns: "xmlns",
|
|
541
|
+
xmlnsXlink: "xmlns:xlink",
|
|
542
|
+
// Filter / lighting
|
|
543
|
+
baseFrequency: "baseFrequency",
|
|
544
|
+
colorInterpolation_filters: "color-interpolation-filters",
|
|
545
|
+
diffuseConstant: "diffuseConstant",
|
|
546
|
+
edgeMode: "edgeMode",
|
|
547
|
+
filterUnits: "filterUnits",
|
|
548
|
+
gradientTransform: "gradientTransform",
|
|
549
|
+
gradientUnits: "gradientUnits",
|
|
550
|
+
kernelMatrix: "kernelMatrix",
|
|
551
|
+
kernelUnitLength: "kernelUnitLength",
|
|
552
|
+
lengthAdjust: "lengthAdjust",
|
|
553
|
+
limitingConeAngle: "limitingConeAngle",
|
|
554
|
+
markerHeight: "markerHeight",
|
|
555
|
+
markerWidth: "markerWidth",
|
|
556
|
+
maskContentUnits: "maskContentUnits",
|
|
557
|
+
maskUnits: "maskUnits",
|
|
558
|
+
numOctaves: "numOctaves",
|
|
559
|
+
pathLength: "pathLength",
|
|
560
|
+
patternContentUnits: "patternContentUnits",
|
|
561
|
+
patternTransform: "patternTransform",
|
|
562
|
+
patternUnits: "patternUnits",
|
|
563
|
+
pointsAtX: "pointsAtX",
|
|
564
|
+
pointsAtY: "pointsAtY",
|
|
565
|
+
pointsAtZ: "pointsAtZ",
|
|
566
|
+
preserveAspectRatio: "preserveAspectRatio",
|
|
567
|
+
primitiveUnits: "primitiveUnits",
|
|
568
|
+
refX: "refX",
|
|
569
|
+
refY: "refY",
|
|
570
|
+
repeatCount: "repeatCount",
|
|
571
|
+
repeatDur: "repeatDur",
|
|
572
|
+
specularConstant: "specularConstant",
|
|
573
|
+
specularExponent: "specularExponent",
|
|
574
|
+
spreadMethod: "spreadMethod",
|
|
575
|
+
startOffset: "startOffset",
|
|
576
|
+
stdDeviation: "stdDeviation",
|
|
577
|
+
stitchTiles: "stitchTiles",
|
|
578
|
+
surfaceScale: "surfaceScale",
|
|
579
|
+
systemLanguage: "systemLanguage",
|
|
580
|
+
tableValues: "tableValues",
|
|
581
|
+
targetX: "targetX",
|
|
582
|
+
targetY: "targetY",
|
|
583
|
+
textLength: "textLength",
|
|
584
|
+
viewBox: "viewBox",
|
|
585
|
+
xChannelSelector: "xChannelSelector",
|
|
586
|
+
yChannelSelector: "yChannelSelector"
|
|
587
|
+
};
|
|
588
|
+
function renderAttributes(props, isSvg) {
|
|
589
|
+
let attrs = "";
|
|
590
|
+
for (const key in props) {
|
|
591
|
+
const value = props[key];
|
|
592
|
+
if (key === "children" || key === "key" || key === "ref" || key === "dangerouslySetInnerHTML" || key === "suppressHydrationWarning" || key === "suppressContentEditableWarning")
|
|
593
|
+
continue;
|
|
594
|
+
if (key.startsWith("on") && key.length > 2 && key[2] === key[2].toUpperCase())
|
|
595
|
+
continue;
|
|
596
|
+
let attrName;
|
|
597
|
+
if (isSvg && key in SVG_ATTR_MAP) {
|
|
598
|
+
attrName = SVG_ATTR_MAP[key];
|
|
599
|
+
} else {
|
|
600
|
+
attrName = key === "className" ? "class" : key === "htmlFor" ? "for" : key === "tabIndex" ? "tabindex" : key === "defaultValue" ? "value" : key === "defaultChecked" ? "checked" : key;
|
|
601
|
+
}
|
|
602
|
+
if (value === false || value == null) {
|
|
603
|
+
if (value === false && (attrName.startsWith("aria-") || attrName.startsWith("data-"))) {
|
|
604
|
+
attrs += ` ${attrName}="false"`;
|
|
605
|
+
}
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
if (value === true) {
|
|
609
|
+
if (attrName.startsWith("aria-") || attrName.startsWith("data-")) {
|
|
610
|
+
attrs += ` ${attrName}="true"`;
|
|
611
|
+
} else {
|
|
612
|
+
attrs += ` ${attrName}=""`;
|
|
613
|
+
}
|
|
614
|
+
continue;
|
|
615
|
+
}
|
|
616
|
+
if (key === "style" && typeof value === "object") {
|
|
617
|
+
const styleStr = styleObjectToString(value);
|
|
618
|
+
if (styleStr) attrs += ` style="${escapeAttr(styleStr)}"`;
|
|
619
|
+
continue;
|
|
620
|
+
}
|
|
621
|
+
attrs += ` ${attrName}="${escapeAttr(String(value))}"`;
|
|
622
|
+
}
|
|
623
|
+
return attrs;
|
|
624
|
+
}
|
|
625
|
+
var BufferWriter = class {
|
|
626
|
+
chunks = [];
|
|
627
|
+
lastWasText = false;
|
|
628
|
+
write(chunk) {
|
|
629
|
+
this.chunks.push(chunk);
|
|
630
|
+
this.lastWasText = false;
|
|
631
|
+
}
|
|
632
|
+
text(s2) {
|
|
633
|
+
this.chunks.push(s2);
|
|
634
|
+
this.lastWasText = true;
|
|
635
|
+
}
|
|
636
|
+
flush(target) {
|
|
637
|
+
for (const c of this.chunks) target.write(c);
|
|
638
|
+
target.lastWasText = this.lastWasText;
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
function renderNode(node, writer, isSvg = false) {
|
|
642
|
+
if (node == null || typeof node === "boolean") return;
|
|
643
|
+
if (typeof node === "string") {
|
|
644
|
+
writer.text(escapeHtml(node));
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
if (typeof node === "number") {
|
|
648
|
+
writer.text(String(node));
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
if (Array.isArray(node)) {
|
|
652
|
+
return renderChildArray(node, writer, isSvg);
|
|
653
|
+
}
|
|
654
|
+
if (typeof node === "object" && node !== null && Symbol.iterator in node && !("$$typeof" in node)) {
|
|
655
|
+
return renderChildArray(
|
|
656
|
+
Array.from(node),
|
|
657
|
+
writer,
|
|
658
|
+
isSvg
|
|
659
|
+
);
|
|
660
|
+
}
|
|
661
|
+
if (typeof node === "object" && node !== null && "$$typeof" in node) {
|
|
662
|
+
const elType = node["$$typeof"];
|
|
663
|
+
if (elType !== SLIM_ELEMENT && elType !== REACT19_ELEMENT) return;
|
|
664
|
+
const element = node;
|
|
665
|
+
const { type, props } = element;
|
|
666
|
+
if (type === FRAGMENT_TYPE) {
|
|
667
|
+
return renderChildren(props.children, writer, isSvg);
|
|
668
|
+
}
|
|
669
|
+
if (type === SUSPENSE_TYPE) {
|
|
670
|
+
return renderSuspense(props, writer, isSvg);
|
|
671
|
+
}
|
|
672
|
+
if (typeof type === "function") {
|
|
673
|
+
return renderComponent(type, props, writer, isSvg);
|
|
674
|
+
}
|
|
675
|
+
if (typeof type === "object" && type !== null) {
|
|
676
|
+
return renderComponent(type, props, writer, isSvg);
|
|
677
|
+
}
|
|
678
|
+
if (typeof type === "string") {
|
|
679
|
+
return renderHostElement(type, props, writer, isSvg);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
function markSelectedOptionsMulti(children, selectedValues) {
|
|
684
|
+
if (children == null || typeof children === "boolean") return children;
|
|
685
|
+
if (typeof children === "string" || typeof children === "number") return children;
|
|
686
|
+
if (Array.isArray(children)) {
|
|
687
|
+
return children.map((c) => markSelectedOptionsMulti(c, selectedValues));
|
|
688
|
+
}
|
|
689
|
+
if (typeof children === "object" && "$$typeof" in children) {
|
|
690
|
+
const elType = children["$$typeof"];
|
|
691
|
+
if (elType !== SLIM_ELEMENT && elType !== REACT19_ELEMENT) return children;
|
|
692
|
+
const el = children;
|
|
693
|
+
if (el.type === "option") {
|
|
694
|
+
const optValue = el.props.value !== void 0 ? el.props.value : el.props.children;
|
|
695
|
+
const isSelected = selectedValues.has(String(optValue));
|
|
696
|
+
return { ...el, props: { ...el.props, selected: isSelected || void 0 } };
|
|
697
|
+
}
|
|
698
|
+
if (el.type === "optgroup" || el.type === FRAGMENT_TYPE) {
|
|
699
|
+
const newChildren = markSelectedOptionsMulti(el.props.children, selectedValues);
|
|
700
|
+
return { ...el, props: { ...el.props, children: newChildren } };
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
return children;
|
|
704
|
+
}
|
|
705
|
+
function renderHostElement(tag, props, writer, isSvg) {
|
|
706
|
+
const enteringSvg = tag === "svg";
|
|
707
|
+
const childSvg = isSvg || enteringSvg;
|
|
708
|
+
if (tag === "textarea") {
|
|
709
|
+
const textContent = props.value ?? props.defaultValue ?? props.children ?? "";
|
|
710
|
+
const filteredProps = {};
|
|
711
|
+
for (const k of Object.keys(props)) {
|
|
712
|
+
if (k !== "value" && k !== "defaultValue" && k !== "children") filteredProps[k] = props[k];
|
|
713
|
+
}
|
|
714
|
+
writer.write(`<textarea${renderAttributes(filteredProps, false)}>`);
|
|
715
|
+
writer.text(escapeHtml(String(textContent)));
|
|
716
|
+
writer.write("</textarea>");
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
if (tag === "select") {
|
|
720
|
+
const selectedValue = props.value ?? props.defaultValue;
|
|
721
|
+
const filteredProps = {};
|
|
722
|
+
for (const k of Object.keys(props)) {
|
|
723
|
+
if (k !== "value" && k !== "defaultValue") filteredProps[k] = props[k];
|
|
724
|
+
}
|
|
725
|
+
writer.write(`<select${renderAttributes(filteredProps, false)}>`);
|
|
726
|
+
const selectedSet = selectedValue == null ? null : Array.isArray(selectedValue) ? new Set(selectedValue.map(String)) : /* @__PURE__ */ new Set([String(selectedValue)]);
|
|
727
|
+
const patchedChildren = selectedSet != null ? markSelectedOptionsMulti(props.children, selectedSet) : props.children;
|
|
728
|
+
const inner2 = renderChildren(patchedChildren, writer, false);
|
|
729
|
+
if (inner2 && typeof inner2.then === "function") {
|
|
730
|
+
return inner2.then(() => {
|
|
731
|
+
writer.write("</select>");
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
writer.write("</select>");
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
737
|
+
writer.write(`<${tag}${renderAttributes(props, childSvg)}`);
|
|
738
|
+
if (VOID_ELEMENTS.has(tag)) {
|
|
739
|
+
writer.write("/>");
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
writer.write(">");
|
|
743
|
+
const childContext = tag === "foreignObject" ? false : childSvg;
|
|
744
|
+
let inner = void 0;
|
|
745
|
+
if (props.dangerouslySetInnerHTML) {
|
|
746
|
+
writer.write(props.dangerouslySetInnerHTML.__html);
|
|
747
|
+
} else {
|
|
748
|
+
inner = renderChildren(props.children, writer, childContext);
|
|
749
|
+
}
|
|
750
|
+
if (inner && typeof inner.then === "function") {
|
|
751
|
+
return inner.then(() => {
|
|
752
|
+
writer.write(`</${tag}>`);
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
writer.write(`</${tag}>`);
|
|
756
|
+
}
|
|
757
|
+
var REACT_MEMO = Symbol.for("react.memo");
|
|
758
|
+
var REACT_FORWARD_REF = Symbol.for("react.forward_ref");
|
|
759
|
+
var REACT_PROVIDER = Symbol.for("react.provider");
|
|
760
|
+
var REACT_CONTEXT = Symbol.for("react.context");
|
|
761
|
+
var REACT_CONSUMER = Symbol.for("react.consumer");
|
|
762
|
+
var REACT_LAZY = Symbol.for("react.lazy");
|
|
763
|
+
function renderComponent(type, props, writer, isSvg) {
|
|
764
|
+
const typeOf = type?.$$typeof;
|
|
765
|
+
if (typeOf === REACT_MEMO) {
|
|
766
|
+
return renderNode(
|
|
767
|
+
{ $$typeof: SLIM_ELEMENT, type: type.type, props, key: null },
|
|
768
|
+
writer,
|
|
769
|
+
isSvg
|
|
770
|
+
);
|
|
771
|
+
}
|
|
772
|
+
if (typeOf === REACT_FORWARD_REF) {
|
|
773
|
+
return renderComponent(type.render, props, writer, isSvg);
|
|
774
|
+
}
|
|
775
|
+
if (typeOf === REACT_LAZY) {
|
|
776
|
+
const resolved = type._init(type._payload);
|
|
777
|
+
const LazyComp = resolved?.default ?? resolved;
|
|
778
|
+
return renderComponent(LazyComp, props, writer, isSvg);
|
|
779
|
+
}
|
|
780
|
+
if (typeOf === REACT_CONSUMER) {
|
|
781
|
+
const ctx2 = type._context;
|
|
782
|
+
const value = ctx2 ? getContextValue(ctx2) : void 0;
|
|
783
|
+
const result2 = typeof props.children === "function" ? props.children(value) : null;
|
|
784
|
+
const savedScope2 = pushComponentScope();
|
|
785
|
+
const finish2 = () => popComponentScope(savedScope2);
|
|
786
|
+
const r2 = renderNode(result2, writer, isSvg);
|
|
787
|
+
if (r2 && typeof r2.then === "function") {
|
|
788
|
+
return r2.then(finish2);
|
|
789
|
+
}
|
|
790
|
+
finish2();
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
const isProvider = "_context" in type || typeOf === REACT_PROVIDER || typeOf === REACT_CONTEXT && "value" in props;
|
|
794
|
+
let prevCtxValue;
|
|
795
|
+
let ctx;
|
|
796
|
+
if (isProvider) {
|
|
797
|
+
ctx = type._context ?? type;
|
|
798
|
+
prevCtxValue = pushContextValue(ctx, props.value);
|
|
799
|
+
}
|
|
800
|
+
const savedScope = pushComponentScope();
|
|
801
|
+
if (isProvider && typeof type !== "function") {
|
|
802
|
+
const finish2 = () => {
|
|
803
|
+
popComponentScope(savedScope);
|
|
804
|
+
popContextValue(ctx, prevCtxValue);
|
|
805
|
+
};
|
|
806
|
+
const r2 = renderChildren(props.children, writer, isSvg);
|
|
807
|
+
if (r2 && typeof r2.then === "function") {
|
|
808
|
+
const m = captureMap();
|
|
809
|
+
return r2.then(
|
|
810
|
+
() => {
|
|
811
|
+
swapContextMap(m);
|
|
812
|
+
finish2();
|
|
813
|
+
},
|
|
814
|
+
(e) => {
|
|
815
|
+
swapContextMap(m);
|
|
816
|
+
finish2();
|
|
817
|
+
throw e;
|
|
818
|
+
}
|
|
819
|
+
);
|
|
820
|
+
}
|
|
821
|
+
finish2();
|
|
822
|
+
return;
|
|
823
|
+
}
|
|
824
|
+
let result;
|
|
825
|
+
const prevDispatcher = installDispatcher();
|
|
826
|
+
try {
|
|
827
|
+
if (type.prototype && typeof type.prototype.render === "function") {
|
|
828
|
+
const instance = new type(props);
|
|
829
|
+
if (typeof type.getDerivedStateFromProps === "function") {
|
|
830
|
+
const derived = type.getDerivedStateFromProps(props, instance.state ?? {});
|
|
831
|
+
if (derived != null) instance.state = { ...instance.state ?? {}, ...derived };
|
|
832
|
+
}
|
|
833
|
+
result = instance.render();
|
|
834
|
+
} else {
|
|
835
|
+
result = type(props);
|
|
836
|
+
}
|
|
837
|
+
} catch (e) {
|
|
838
|
+
restoreDispatcher(prevDispatcher);
|
|
839
|
+
popComponentScope(savedScope);
|
|
840
|
+
if (isProvider) popContextValue(ctx, prevCtxValue);
|
|
841
|
+
throw e;
|
|
842
|
+
}
|
|
843
|
+
restoreDispatcher(prevDispatcher);
|
|
844
|
+
let savedIdTree;
|
|
845
|
+
if (!(result instanceof Promise) && componentCalledUseId()) {
|
|
846
|
+
savedIdTree = pushTreeContext(1, 0);
|
|
847
|
+
}
|
|
848
|
+
const finish = () => {
|
|
849
|
+
if (savedIdTree !== void 0) popTreeContext(savedIdTree);
|
|
850
|
+
popComponentScope(savedScope);
|
|
851
|
+
if (isProvider) popContextValue(ctx, prevCtxValue);
|
|
852
|
+
};
|
|
853
|
+
if (result instanceof Promise) {
|
|
854
|
+
const m = captureMap();
|
|
855
|
+
return result.then((resolved) => {
|
|
856
|
+
swapContextMap(m);
|
|
857
|
+
let asyncSavedIdTree;
|
|
858
|
+
if (componentCalledUseId()) {
|
|
859
|
+
asyncSavedIdTree = pushTreeContext(1, 0);
|
|
860
|
+
}
|
|
861
|
+
const asyncFinish = () => {
|
|
862
|
+
if (asyncSavedIdTree !== void 0) popTreeContext(asyncSavedIdTree);
|
|
863
|
+
popComponentScope(savedScope);
|
|
864
|
+
if (isProvider) popContextValue(ctx, prevCtxValue);
|
|
865
|
+
};
|
|
866
|
+
const r2 = renderNode(resolved, writer, isSvg);
|
|
867
|
+
if (r2 && typeof r2.then === "function") {
|
|
868
|
+
const m2 = captureMap();
|
|
869
|
+
return r2.then(
|
|
870
|
+
() => {
|
|
871
|
+
swapContextMap(m2);
|
|
872
|
+
asyncFinish();
|
|
873
|
+
},
|
|
874
|
+
(e) => {
|
|
875
|
+
swapContextMap(m2);
|
|
876
|
+
asyncFinish();
|
|
877
|
+
throw e;
|
|
878
|
+
}
|
|
879
|
+
);
|
|
880
|
+
}
|
|
881
|
+
asyncFinish();
|
|
882
|
+
}, (e) => {
|
|
883
|
+
swapContextMap(m);
|
|
884
|
+
finish();
|
|
885
|
+
throw e;
|
|
886
|
+
});
|
|
887
|
+
}
|
|
888
|
+
const r = renderNode(result, writer, isSvg);
|
|
889
|
+
if (r && typeof r.then === "function") {
|
|
890
|
+
const m = captureMap();
|
|
891
|
+
return r.then(
|
|
892
|
+
() => {
|
|
893
|
+
swapContextMap(m);
|
|
894
|
+
finish();
|
|
895
|
+
},
|
|
896
|
+
(e) => {
|
|
897
|
+
swapContextMap(m);
|
|
898
|
+
finish();
|
|
899
|
+
throw e;
|
|
900
|
+
}
|
|
901
|
+
);
|
|
902
|
+
}
|
|
903
|
+
finish();
|
|
904
|
+
}
|
|
905
|
+
function isTextLike(node) {
|
|
906
|
+
return typeof node === "string" || typeof node === "number";
|
|
907
|
+
}
|
|
908
|
+
function renderChildArray(children, writer, isSvg) {
|
|
909
|
+
const totalChildren = children.length;
|
|
910
|
+
for (let i = 0; i < totalChildren; i++) {
|
|
911
|
+
if (isTextLike(children[i]) && writer.lastWasText) {
|
|
912
|
+
writer.write("<!-- -->");
|
|
913
|
+
}
|
|
914
|
+
const savedTree = pushTreeContext(totalChildren, i);
|
|
915
|
+
const r = renderNode(children[i], writer, isSvg);
|
|
916
|
+
if (r && typeof r.then === "function") {
|
|
917
|
+
const m = captureMap();
|
|
918
|
+
return r.then(() => {
|
|
919
|
+
swapContextMap(m);
|
|
920
|
+
popTreeContext(savedTree);
|
|
921
|
+
return renderChildArrayFrom(children, i + 1, writer, isSvg);
|
|
922
|
+
});
|
|
923
|
+
}
|
|
924
|
+
popTreeContext(savedTree);
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
function renderChildArrayFrom(children, startIndex, writer, isSvg) {
|
|
928
|
+
const totalChildren = children.length;
|
|
929
|
+
for (let i = startIndex; i < totalChildren; i++) {
|
|
930
|
+
if (isTextLike(children[i]) && writer.lastWasText) {
|
|
931
|
+
writer.write("<!-- -->");
|
|
932
|
+
}
|
|
933
|
+
const savedTree = pushTreeContext(totalChildren, i);
|
|
934
|
+
const r = renderNode(children[i], writer, isSvg);
|
|
935
|
+
if (r && typeof r.then === "function") {
|
|
936
|
+
const m = captureMap();
|
|
937
|
+
return r.then(() => {
|
|
938
|
+
swapContextMap(m);
|
|
939
|
+
popTreeContext(savedTree);
|
|
940
|
+
return renderChildArrayFrom(children, i + 1, writer, isSvg);
|
|
941
|
+
});
|
|
942
|
+
}
|
|
943
|
+
popTreeContext(savedTree);
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
function renderChildren(children, writer, isSvg = false) {
|
|
947
|
+
if (children == null) return;
|
|
948
|
+
if (Array.isArray(children)) {
|
|
949
|
+
return renderChildArray(children, writer, isSvg);
|
|
950
|
+
}
|
|
951
|
+
return renderNode(children, writer, isSvg);
|
|
952
|
+
}
|
|
953
|
+
var MAX_SUSPENSE_RETRIES = 25;
|
|
954
|
+
async function renderSuspense(props, writer, isSvg = false) {
|
|
955
|
+
const { children, fallback } = props;
|
|
956
|
+
let attempts = 0;
|
|
957
|
+
const snap = snapshotContext();
|
|
958
|
+
while (attempts < MAX_SUSPENSE_RETRIES) {
|
|
959
|
+
restoreContext(snap);
|
|
960
|
+
let buffer = new BufferWriter();
|
|
961
|
+
try {
|
|
962
|
+
const r = renderNode(children, buffer, isSvg);
|
|
963
|
+
if (r && typeof r.then === "function") {
|
|
964
|
+
const m = captureMap();
|
|
965
|
+
await r;
|
|
966
|
+
swapContextMap(m);
|
|
967
|
+
}
|
|
968
|
+
writer.write("<!--$-->");
|
|
969
|
+
buffer.flush(writer);
|
|
970
|
+
writer.write("<!--/$-->");
|
|
971
|
+
return;
|
|
972
|
+
} catch (error) {
|
|
973
|
+
if (error && typeof error.then === "function") {
|
|
974
|
+
const m = captureMap();
|
|
975
|
+
await error;
|
|
976
|
+
swapContextMap(m);
|
|
977
|
+
attempts++;
|
|
978
|
+
} else {
|
|
979
|
+
throw error;
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
restoreContext(snap);
|
|
984
|
+
writer.write("<!--$?-->");
|
|
985
|
+
if (fallback) {
|
|
986
|
+
const r = renderNode(fallback, writer, isSvg);
|
|
987
|
+
if (r && typeof r.then === "function") {
|
|
988
|
+
const m = captureMap();
|
|
989
|
+
await r;
|
|
990
|
+
swapContextMap(m);
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
writer.write("<!--/$-->");
|
|
994
|
+
}
|
|
995
|
+
async function renderToString(element, options) {
|
|
996
|
+
const idPrefix = options?.identifierPrefix ?? "";
|
|
997
|
+
const contextMap = /* @__PURE__ */ new Map();
|
|
998
|
+
const prev = swapContextMap(contextMap);
|
|
999
|
+
try {
|
|
1000
|
+
for (let attempt = 0; attempt < MAX_SUSPENSE_RETRIES; attempt++) {
|
|
1001
|
+
resetRenderState(idPrefix);
|
|
1002
|
+
swapContextMap(contextMap);
|
|
1003
|
+
const chunks = [];
|
|
1004
|
+
const writer = {
|
|
1005
|
+
lastWasText: false,
|
|
1006
|
+
write(c) {
|
|
1007
|
+
chunks.push(c);
|
|
1008
|
+
this.lastWasText = false;
|
|
1009
|
+
},
|
|
1010
|
+
text(s2) {
|
|
1011
|
+
chunks.push(s2);
|
|
1012
|
+
this.lastWasText = true;
|
|
1013
|
+
}
|
|
1014
|
+
};
|
|
1015
|
+
try {
|
|
1016
|
+
const r = renderNode(element, writer);
|
|
1017
|
+
if (r && typeof r.then === "function") {
|
|
1018
|
+
const m = captureMap();
|
|
1019
|
+
await r;
|
|
1020
|
+
swapContextMap(m);
|
|
1021
|
+
}
|
|
1022
|
+
return chunks.join("");
|
|
1023
|
+
} catch (error) {
|
|
1024
|
+
if (error && typeof error.then === "function") {
|
|
1025
|
+
const m = captureMap();
|
|
1026
|
+
await error;
|
|
1027
|
+
swapContextMap(m);
|
|
1028
|
+
continue;
|
|
1029
|
+
}
|
|
1030
|
+
throw error;
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
throw new Error("[slim-react] renderToString exceeded maximum retries");
|
|
1034
|
+
} finally {
|
|
1035
|
+
swapContextMap(prev);
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
// src/utils/segmentCache.ts
|
|
1040
|
+
function getStore() {
|
|
1041
|
+
const g = globalThis;
|
|
1042
|
+
if (!g.__hadarsSegmentStore) {
|
|
1043
|
+
g.__hadarsSegmentStore = /* @__PURE__ */ new Map();
|
|
1044
|
+
}
|
|
1045
|
+
return g.__hadarsSegmentStore;
|
|
1046
|
+
}
|
|
1047
|
+
function setSegment(key, html, ttl) {
|
|
1048
|
+
getStore().set(key, {
|
|
1049
|
+
html,
|
|
1050
|
+
expiresAt: ttl != null ? Date.now() + ttl : null
|
|
1051
|
+
});
|
|
1052
|
+
}
|
|
1053
|
+
function processSegmentCache(html) {
|
|
1054
|
+
let prev;
|
|
1055
|
+
do {
|
|
1056
|
+
prev = html;
|
|
1057
|
+
html = html.replace(
|
|
1058
|
+
/<hadars-c([^>]*)>([\s\S]*?)<\/hadars-c>/g,
|
|
1059
|
+
(match, attrs, content) => {
|
|
1060
|
+
const cacheM = /data-cache="([^"]+)"/.exec(attrs);
|
|
1061
|
+
const keyM = /data-key="([^"]+)"/.exec(attrs);
|
|
1062
|
+
const ttlM = /data-ttl="(\d+)"/.exec(attrs);
|
|
1063
|
+
if (!cacheM || !keyM) return match;
|
|
1064
|
+
if (cacheM[1] === "miss") {
|
|
1065
|
+
setSegment(keyM[1], content, ttlM ? Number(ttlM[1]) : void 0);
|
|
1066
|
+
return content;
|
|
1067
|
+
}
|
|
1068
|
+
if (cacheM[1] === "hit") return content;
|
|
1069
|
+
return match;
|
|
1070
|
+
}
|
|
1071
|
+
);
|
|
1072
|
+
} while (html !== prev);
|
|
1073
|
+
return html;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
// src/utils/response.tsx
|
|
1077
|
+
var ESC = { "&": "&", "<": "<", ">": ">", '"': """ };
|
|
1078
|
+
var escAttr = (s2) => s2.replace(/[&<>"]/g, (c) => ESC[c] ?? c);
|
|
1079
|
+
var escText = (s2) => s2.replace(/[&<>]/g, (c) => ESC[c] ?? c);
|
|
1080
|
+
var ATTR = {
|
|
1081
|
+
className: "class",
|
|
1082
|
+
htmlFor: "for",
|
|
1083
|
+
httpEquiv: "http-equiv",
|
|
1084
|
+
charSet: "charset",
|
|
1085
|
+
crossOrigin: "crossorigin",
|
|
1086
|
+
noModule: "nomodule",
|
|
1087
|
+
referrerPolicy: "referrerpolicy",
|
|
1088
|
+
fetchPriority: "fetchpriority"
|
|
1089
|
+
};
|
|
1090
|
+
function renderHeadTag(tag, id, opts, selfClose = false) {
|
|
1091
|
+
let attrs = ` id="${escAttr(id)}"`;
|
|
1092
|
+
let inner = "";
|
|
1093
|
+
for (const [k, v] of Object.entries(opts)) {
|
|
1094
|
+
if (k === "key" || k === "children") continue;
|
|
1095
|
+
if (k === "dangerouslySetInnerHTML") {
|
|
1096
|
+
inner = v.__html ?? "";
|
|
1097
|
+
continue;
|
|
1098
|
+
}
|
|
1099
|
+
const attr = ATTR[k] ?? k;
|
|
1100
|
+
if (v === true) attrs += ` ${attr}`;
|
|
1101
|
+
else if (v !== false && v != null) attrs += ` ${attr}="${escAttr(String(v))}"`;
|
|
1102
|
+
}
|
|
1103
|
+
return selfClose ? `<${tag}${attrs}>` : `<${tag}${attrs}>${inner}</${tag}>`;
|
|
1104
|
+
}
|
|
1105
|
+
var getHeadHtml = (seoData) => {
|
|
1106
|
+
let html = `<title>${escText(seoData.title ?? "")}</title>`;
|
|
1107
|
+
for (const [id, opts] of Object.entries(seoData.meta))
|
|
1108
|
+
html += renderHeadTag("meta", id, opts, true);
|
|
1109
|
+
for (const [id, opts] of Object.entries(seoData.link))
|
|
1110
|
+
html += renderHeadTag("link", id, opts, true);
|
|
1111
|
+
for (const [id, opts] of Object.entries(seoData.style))
|
|
1112
|
+
html += renderHeadTag("style", id, opts);
|
|
1113
|
+
for (const [id, opts] of Object.entries(seoData.script))
|
|
1114
|
+
html += renderHeadTag("script", id, opts);
|
|
1115
|
+
return html;
|
|
1116
|
+
};
|
|
1117
|
+
var getReactResponse = async (req, opts) => {
|
|
1118
|
+
const App = opts.document.body;
|
|
1119
|
+
const { getInitProps, getAfterRenderProps, getFinalProps } = opts.document;
|
|
1120
|
+
const context = {
|
|
1121
|
+
head: { title: "Hadars App", meta: {}, link: {}, style: {}, script: {}, status: 200 }
|
|
1122
|
+
};
|
|
1123
|
+
let props = {
|
|
1124
|
+
...getInitProps ? await getInitProps(req) : {},
|
|
1125
|
+
location: req.location,
|
|
1126
|
+
context
|
|
1127
|
+
};
|
|
1128
|
+
const unsuspend = { cache: /* @__PURE__ */ new Map() };
|
|
1129
|
+
globalThis.__hadarsUnsuspend = unsuspend;
|
|
1130
|
+
let bodyHtml;
|
|
1131
|
+
try {
|
|
1132
|
+
bodyHtml = await renderToString(createElement(App, props));
|
|
1133
|
+
if (getAfterRenderProps) {
|
|
1134
|
+
props = await getAfterRenderProps(props, bodyHtml);
|
|
1135
|
+
bodyHtml = await renderToString(
|
|
1136
|
+
createElement(App, { ...props, location: req.location, context })
|
|
1137
|
+
);
|
|
1138
|
+
}
|
|
1139
|
+
} finally {
|
|
1140
|
+
globalThis.__hadarsUnsuspend = null;
|
|
1141
|
+
}
|
|
1142
|
+
const { context: _, ...restProps } = getFinalProps ? await getFinalProps(props) : props;
|
|
1143
|
+
const serverData = {};
|
|
1144
|
+
for (const [key, entry] of unsuspend.cache) {
|
|
1145
|
+
if (entry.status === "fulfilled") serverData[key] = entry.value;
|
|
1146
|
+
}
|
|
1147
|
+
const clientProps = {
|
|
1148
|
+
...restProps,
|
|
1149
|
+
location: req.location,
|
|
1150
|
+
...Object.keys(serverData).length > 0 ? { __serverData: serverData } : {}
|
|
1151
|
+
};
|
|
1152
|
+
return {
|
|
1153
|
+
bodyHtml: processSegmentCache(bodyHtml),
|
|
1154
|
+
clientProps,
|
|
1155
|
+
status: context.head.status,
|
|
1156
|
+
headHtml: getHeadHtml(context.head)
|
|
1157
|
+
};
|
|
1158
|
+
};
|
|
1159
|
+
|
|
1160
|
+
// src/utils/ssrHandler.ts
|
|
1161
|
+
var HEAD_MARKER = '<meta name="HADARS_HEAD">';
|
|
1162
|
+
var BODY_MARKER = '<meta name="HADARS_BODY">';
|
|
1163
|
+
var encoder = new TextEncoder();
|
|
1164
|
+
async function buildSsrHtml(bodyHtml, clientProps, headHtml, getPrecontentHtml) {
|
|
1165
|
+
const [precontentHtml, postContent] = await getPrecontentHtml(headHtml);
|
|
1166
|
+
const scriptContent = JSON.stringify({ hadars: { props: clientProps } }).replace(/</g, "\\u003c");
|
|
1167
|
+
return precontentHtml + `<div id="app">${bodyHtml}</div><script id="hadars" type="application/json">${scriptContent}</script>` + postContent;
|
|
1168
|
+
}
|
|
1169
|
+
var makePrecontentHtmlGetter = (htmlFilePromise) => {
|
|
1170
|
+
let preHead = null;
|
|
1171
|
+
let postHead = null;
|
|
1172
|
+
let postContent = null;
|
|
1173
|
+
return async (headHtml) => {
|
|
1174
|
+
if (preHead === null || postHead === null || postContent === null) {
|
|
1175
|
+
const html = await htmlFilePromise;
|
|
1176
|
+
const headEnd = html.indexOf(HEAD_MARKER);
|
|
1177
|
+
const contentStart = html.indexOf(BODY_MARKER);
|
|
1178
|
+
preHead = html.slice(0, headEnd);
|
|
1179
|
+
postHead = html.slice(headEnd + HEAD_MARKER.length, contentStart);
|
|
1180
|
+
postContent = html.slice(contentStart + BODY_MARKER.length);
|
|
1181
|
+
}
|
|
1182
|
+
return [preHead + headHtml + postHead, postContent];
|
|
1183
|
+
};
|
|
1184
|
+
};
|
|
1185
|
+
async function transformStream(data, stream) {
|
|
1186
|
+
const writer = stream.writable.getWriter();
|
|
1187
|
+
writer.write(data);
|
|
1188
|
+
writer.close();
|
|
1189
|
+
const chunks = [];
|
|
1190
|
+
const reader = stream.readable.getReader();
|
|
1191
|
+
while (true) {
|
|
1192
|
+
const { done, value } = await reader.read();
|
|
1193
|
+
if (done) break;
|
|
1194
|
+
chunks.push(value);
|
|
1195
|
+
}
|
|
1196
|
+
const total = chunks.reduce((n, c) => n + c.length, 0);
|
|
1197
|
+
const out = new Uint8Array(total);
|
|
1198
|
+
let offset = 0;
|
|
1199
|
+
for (const c of chunks) {
|
|
1200
|
+
out.set(c, offset);
|
|
1201
|
+
offset += c.length;
|
|
1202
|
+
}
|
|
1203
|
+
return out;
|
|
1204
|
+
}
|
|
1205
|
+
var gzipCompress = (d) => transformStream(d, new globalThis.CompressionStream("gzip"));
|
|
1206
|
+
var gzipDecompress = (d) => transformStream(d, new globalThis.DecompressionStream("gzip"));
|
|
1207
|
+
async function buildCacheEntry(res, ttl) {
|
|
1208
|
+
const buf = await res.arrayBuffer();
|
|
1209
|
+
const body = await gzipCompress(new Uint8Array(buf));
|
|
1210
|
+
const headers = [];
|
|
1211
|
+
res.headers.forEach((v, k) => {
|
|
1212
|
+
if (k.toLowerCase() !== "content-encoding" && k.toLowerCase() !== "content-length") {
|
|
1213
|
+
headers.push([k, v]);
|
|
1214
|
+
}
|
|
1215
|
+
});
|
|
1216
|
+
headers.push(["content-encoding", "gzip"]);
|
|
1217
|
+
return { body, status: res.status, headers, expiresAt: ttl != null ? Date.now() + ttl : null };
|
|
1218
|
+
}
|
|
1219
|
+
async function serveFromEntry(entry, req) {
|
|
1220
|
+
const accept = req.headers.get("Accept-Encoding") ?? "";
|
|
1221
|
+
if (accept.includes("gzip")) {
|
|
1222
|
+
return new Response(entry.body.buffer, { status: entry.status, headers: entry.headers });
|
|
1223
|
+
}
|
|
1224
|
+
const plain = await gzipDecompress(entry.body);
|
|
1225
|
+
const headers = entry.headers.filter(([k]) => k.toLowerCase() !== "content-encoding");
|
|
1226
|
+
return new Response(plain.buffer, { status: entry.status, headers });
|
|
1227
|
+
}
|
|
1228
|
+
function createRenderCache(opts, handler) {
|
|
1229
|
+
const store = /* @__PURE__ */ new Map();
|
|
1230
|
+
const inFlight = /* @__PURE__ */ new Map();
|
|
1231
|
+
return async (req, ctx) => {
|
|
1232
|
+
const hadarsReq = parseRequest(req);
|
|
1233
|
+
const cacheOpts = await opts(hadarsReq);
|
|
1234
|
+
const key = cacheOpts?.key ?? null;
|
|
1235
|
+
if (key != null) {
|
|
1236
|
+
const entry = store.get(key);
|
|
1237
|
+
if (entry) {
|
|
1238
|
+
const expired = entry.expiresAt != null && Date.now() >= entry.expiresAt;
|
|
1239
|
+
if (!expired) return serveFromEntry(entry, req);
|
|
1240
|
+
store.delete(key);
|
|
1241
|
+
}
|
|
1242
|
+
let flight = inFlight.get(key);
|
|
1243
|
+
if (!flight) {
|
|
1244
|
+
const ttl = cacheOpts?.ttl;
|
|
1245
|
+
flight = handler(new Request(req), ctx).then(async (res) => {
|
|
1246
|
+
if (!res || res.status < 200 || res.status >= 300 || res.headers.has("set-cookie")) {
|
|
1247
|
+
return null;
|
|
1248
|
+
}
|
|
1249
|
+
const newEntry2 = await buildCacheEntry(res, ttl);
|
|
1250
|
+
store.set(key, newEntry2);
|
|
1251
|
+
return newEntry2;
|
|
1252
|
+
}).catch(() => null).finally(() => inFlight.delete(key));
|
|
1253
|
+
inFlight.set(key, flight);
|
|
1254
|
+
}
|
|
1255
|
+
const newEntry = await flight;
|
|
1256
|
+
if (newEntry) return serveFromEntry(newEntry, req);
|
|
1257
|
+
}
|
|
1258
|
+
return handler(req, ctx);
|
|
1259
|
+
};
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
// src/lambda.ts
|
|
1263
|
+
function eventToRequest(event) {
|
|
1264
|
+
let method;
|
|
1265
|
+
let path;
|
|
1266
|
+
let queryString;
|
|
1267
|
+
let headers;
|
|
1268
|
+
let rawBody;
|
|
1269
|
+
let isBase64;
|
|
1270
|
+
let host;
|
|
1271
|
+
if ("version" in event && event.version === "2.0") {
|
|
1272
|
+
method = event.requestContext.http.method;
|
|
1273
|
+
path = event.rawPath;
|
|
1274
|
+
queryString = event.rawQueryString;
|
|
1275
|
+
headers = { ...event.headers };
|
|
1276
|
+
if (event.cookies?.length) headers["cookie"] = event.cookies.join("; ");
|
|
1277
|
+
rawBody = event.body;
|
|
1278
|
+
isBase64 = event.isBase64Encoded;
|
|
1279
|
+
host = event.requestContext.domainName;
|
|
1280
|
+
} else {
|
|
1281
|
+
const e = event;
|
|
1282
|
+
method = e.httpMethod;
|
|
1283
|
+
path = e.path;
|
|
1284
|
+
const qs = e.queryStringParameters;
|
|
1285
|
+
queryString = qs ? new URLSearchParams(qs).toString() : "";
|
|
1286
|
+
headers = e.headers ?? {};
|
|
1287
|
+
rawBody = e.body;
|
|
1288
|
+
isBase64 = e.isBase64Encoded;
|
|
1289
|
+
host = headers["host"] ?? "lambda";
|
|
1290
|
+
}
|
|
1291
|
+
const url = `https://${host}${path}${queryString ? "?" + queryString : ""}`;
|
|
1292
|
+
let body;
|
|
1293
|
+
if (rawBody) body = isBase64 ? Buffer.from(rawBody, "base64") : rawBody;
|
|
1294
|
+
return new Request(url, {
|
|
1295
|
+
method,
|
|
1296
|
+
headers,
|
|
1297
|
+
body: ["GET", "HEAD"].includes(method) ? void 0 : body
|
|
1298
|
+
});
|
|
1299
|
+
}
|
|
1300
|
+
var TEXT_RE = /\b(?:text\/|application\/(?:json|javascript|xml)|image\/svg\+xml)/;
|
|
1301
|
+
async function responseToLambda(response) {
|
|
1302
|
+
const headers = {};
|
|
1303
|
+
response.headers.forEach((v, k) => {
|
|
1304
|
+
headers[k] = v;
|
|
1305
|
+
});
|
|
1306
|
+
const contentType = response.headers.get("Content-Type") ?? "";
|
|
1307
|
+
if (TEXT_RE.test(contentType)) {
|
|
1308
|
+
return {
|
|
1309
|
+
statusCode: response.status,
|
|
1310
|
+
headers,
|
|
1311
|
+
body: await response.text(),
|
|
1312
|
+
isBase64Encoded: false
|
|
1313
|
+
};
|
|
1314
|
+
}
|
|
1315
|
+
return {
|
|
1316
|
+
statusCode: response.status,
|
|
1317
|
+
headers,
|
|
1318
|
+
body: Buffer.from(await response.arrayBuffer()).toString("base64"),
|
|
1319
|
+
isBase64Encoded: true
|
|
1320
|
+
};
|
|
1321
|
+
}
|
|
1322
|
+
var HadarsFolder = "./.hadars";
|
|
1323
|
+
var StaticPath = `${HadarsFolder}/static`;
|
|
1324
|
+
var SSR_FILENAME = "index.ssr.js";
|
|
1325
|
+
var noopCtx = { upgrade: () => false };
|
|
1326
|
+
function createLambdaHandler(options, bundled) {
|
|
1327
|
+
const cwd = process.cwd();
|
|
1328
|
+
const fetchHandler = options.fetch;
|
|
1329
|
+
const handleProxy = createProxyHandler(options);
|
|
1330
|
+
const getPrecontentHtml = bundled ? makePrecontentHtmlGetter(Promise.resolve(bundled.outHtml)) : makePrecontentHtmlGetter(import_promises2.default.readFile(import_node_path.default.join(cwd, StaticPath, "out.html"), "utf-8"));
|
|
1331
|
+
let ssrModulePromise = null;
|
|
1332
|
+
const getSsrModule = () => {
|
|
1333
|
+
if (bundled) return Promise.resolve(bundled.ssrModule);
|
|
1334
|
+
if (!ssrModulePromise) {
|
|
1335
|
+
ssrModulePromise = import((0, import_node_url.pathToFileURL)(import_node_path.default.resolve(cwd, HadarsFolder, SSR_FILENAME)).href);
|
|
1336
|
+
}
|
|
1337
|
+
return ssrModulePromise;
|
|
1338
|
+
};
|
|
1339
|
+
const runHandler = async (req) => {
|
|
1340
|
+
const request = parseRequest(req);
|
|
1341
|
+
if (fetchHandler) {
|
|
1342
|
+
const res = await fetchHandler(request);
|
|
1343
|
+
if (res) return res;
|
|
1344
|
+
}
|
|
1345
|
+
const proxied = await handleProxy(request);
|
|
1346
|
+
if (proxied) return proxied;
|
|
1347
|
+
const urlPath = new URL(request.url).pathname;
|
|
1348
|
+
if (!bundled) {
|
|
1349
|
+
const staticRes = await tryServeFile(import_node_path.default.join(cwd, StaticPath, urlPath));
|
|
1350
|
+
if (staticRes) return staticRes;
|
|
1351
|
+
const projectStaticPath = import_node_path.default.resolve(cwd, "static");
|
|
1352
|
+
const projectRes = await tryServeFile(import_node_path.default.join(projectStaticPath, urlPath));
|
|
1353
|
+
if (projectRes) return projectRes;
|
|
1354
|
+
const routeClean = urlPath.replace(/(^\/|\/$)/g, "");
|
|
1355
|
+
if (routeClean) {
|
|
1356
|
+
const routeRes = await tryServeFile(
|
|
1357
|
+
import_node_path.default.join(cwd, StaticPath, routeClean, "index.html")
|
|
1358
|
+
);
|
|
1359
|
+
if (routeRes) return routeRes;
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
try {
|
|
1363
|
+
const {
|
|
1364
|
+
default: Component,
|
|
1365
|
+
getInitProps,
|
|
1366
|
+
getAfterRenderProps,
|
|
1367
|
+
getFinalProps
|
|
1368
|
+
} = await getSsrModule();
|
|
1369
|
+
const { bodyHtml, clientProps, status, headHtml } = await getReactResponse(request, {
|
|
1370
|
+
document: {
|
|
1371
|
+
body: Component,
|
|
1372
|
+
lang: "en",
|
|
1373
|
+
getInitProps,
|
|
1374
|
+
getAfterRenderProps,
|
|
1375
|
+
getFinalProps
|
|
1376
|
+
}
|
|
1377
|
+
});
|
|
1378
|
+
if (request.headers.get("Accept") === "application/json") {
|
|
1379
|
+
const serverData = clientProps.__serverData ?? {};
|
|
1380
|
+
return new Response(JSON.stringify({ serverData }), {
|
|
1381
|
+
status,
|
|
1382
|
+
headers: { "Content-Type": "application/json; charset=utf-8" }
|
|
1383
|
+
});
|
|
1384
|
+
}
|
|
1385
|
+
const html = await buildSsrHtml(bodyHtml, clientProps, headHtml, getPrecontentHtml);
|
|
1386
|
+
return new Response(html, {
|
|
1387
|
+
status,
|
|
1388
|
+
headers: { "Content-Type": "text/html; charset=utf-8" }
|
|
1389
|
+
});
|
|
1390
|
+
} catch (err) {
|
|
1391
|
+
console.error("[hadars] SSR render error:", err);
|
|
1392
|
+
return new Response("Internal Server Error", { status: 500 });
|
|
1393
|
+
}
|
|
1394
|
+
};
|
|
1395
|
+
const finalHandler = options.cache ? createRenderCache(options.cache, (req) => runHandler(req)) : (req) => runHandler(req);
|
|
1396
|
+
return async (event) => {
|
|
1397
|
+
const req = eventToRequest(event);
|
|
1398
|
+
const response = await finalHandler(req, noopCtx) ?? new Response("Not Found", { status: 404 });
|
|
1399
|
+
return responseToLambda(response);
|
|
1400
|
+
};
|
|
1401
|
+
}
|
|
1402
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1403
|
+
0 && (module.exports = {
|
|
1404
|
+
createLambdaHandler
|
|
1405
|
+
});
|