elysia 2.0.0-beta.21 → 2.0.0-beta.22
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/_virtual/_rolldown/runtime.mjs +1 -0
- package/dist/adapter/bun/index.d.ts +6 -8
- package/dist/adapter/bun/index.js +6 -8
- package/dist/adapter/bun/index.mjs +7 -8
- package/dist/adapter/constants.mjs +1 -0
- package/dist/adapter/default-headers.mjs +1 -0
- package/dist/adapter/index.mjs +1 -0
- package/dist/adapter/origin.mjs +1 -0
- package/dist/adapter/response-ownership.mjs +1 -0
- package/dist/adapter/skip-clone.mjs +1 -0
- package/dist/adapter/types.mjs +1 -0
- package/dist/adapter/utils.d.ts +5 -8
- package/dist/adapter/utils.js +24 -18
- package/dist/adapter/utils.mjs +25 -18
- package/dist/adapter/web-standard/handler.mjs +2 -1
- package/dist/adapter/web-standard/index.mjs +1 -0
- package/dist/adapter/web-standard/utils.mjs +1 -0
- package/dist/base.d.ts +118 -69
- package/dist/base.js +213 -193
- package/dist/base.mjs +220 -199
- package/dist/compile/aot-capture.js +2 -2
- package/dist/compile/aot-capture.mjs +4 -3
- package/dist/compile/aot-emit.js +7 -8
- package/dist/compile/aot-emit.mjs +8 -8
- package/dist/compile/aot-reconstruct.mjs +3 -2
- package/dist/compile/aot.d.ts +3 -3
- package/dist/compile/aot.js +5 -5
- package/dist/compile/aot.mjs +6 -5
- package/dist/compile/handler/descriptor.d.ts +2 -5
- package/dist/compile/handler/descriptor.js +12 -17
- package/dist/compile/handler/descriptor.mjs +16 -21
- package/dist/compile/handler/frozen-validator.d.ts +2 -2
- package/dist/compile/handler/frozen-validator.js +3 -3
- package/dist/compile/handler/frozen-validator.mjs +4 -3
- package/dist/compile/handler/index.d.ts +3 -1
- package/dist/compile/handler/index.js +77 -118
- package/dist/compile/handler/index.mjs +83 -125
- package/dist/compile/handler/jit.d.ts +12 -12
- package/dist/compile/handler/jit.js +60 -43
- package/dist/compile/handler/jit.mjs +63 -47
- package/dist/compile/handler/params.d.ts +5 -1
- package/dist/compile/handler/params.js +14 -5
- package/dist/compile/handler/params.mjs +16 -7
- package/dist/compile/handler/reconstruct.d.ts +2 -2
- package/dist/compile/handler/reconstruct.js +2 -2
- package/dist/compile/handler/reconstruct.mjs +3 -2
- package/dist/compile/handler/static-clone-resolver.mjs +1 -0
- package/dist/compile/handler/utils.d.ts +13 -7
- package/dist/compile/handler/utils.js +75 -52
- package/dist/compile/handler/utils.mjs +77 -54
- package/dist/compile/jit-probe.mjs +1 -0
- package/dist/compile/lexer.js +4 -4
- package/dist/compile/lexer.mjs +5 -4
- package/dist/compile/utils.d.ts +2 -1
- package/dist/compile/utils.js +8 -3
- package/dist/compile/utils.mjs +8 -3
- package/dist/compiled.mjs +1 -0
- package/dist/constants.mjs +1 -0
- package/dist/context.d.ts +1 -1
- package/dist/context.js +1 -0
- package/dist/context.mjs +3 -1
- package/dist/cookie/config.d.ts +2 -2
- package/dist/cookie/config.js +21 -26
- package/dist/cookie/config.mjs +22 -26
- package/dist/cookie/cookie.mjs +1 -0
- package/dist/cookie/crypto.mjs +1 -0
- package/dist/cookie/error.mjs +1 -0
- package/dist/cookie/lib.mjs +1 -0
- package/dist/cookie/serialize.mjs +1 -0
- package/dist/cookie/types.mjs +1 -0
- package/dist/cookie/utils.d.ts +1 -1
- package/dist/cookie/utils.js +64 -45
- package/dist/cookie/utils.mjs +66 -46
- package/dist/error.d.ts +10 -6
- package/dist/error.js +22 -6
- package/dist/error.mjs +25 -9
- package/dist/generation.mjs +1 -0
- package/dist/handler/error.d.ts +12 -15
- package/dist/handler/error.js +43 -40
- package/dist/handler/error.mjs +45 -41
- package/dist/handler/fetch.js +59 -21
- package/dist/handler/fetch.mjs +61 -22
- package/dist/handler/utils.d.ts +16 -3
- package/dist/handler/utils.js +14 -13
- package/dist/handler/utils.mjs +16 -15
- package/dist/index.d.ts +9 -9
- package/dist/index.js +1 -0
- package/dist/index.mjs +3 -2
- package/dist/memory.mjs +1 -0
- package/dist/package.js +1 -1
- package/dist/package.mjs +2 -1
- package/dist/parse-query.js +76 -76
- package/dist/parse-query.mjs +77 -76
- package/dist/plugin/aot/bun.mjs +1 -0
- package/dist/plugin/aot/core.js +7 -4
- package/dist/plugin/aot/core.mjs +8 -5
- package/dist/plugin/aot/esbuild.mjs +1 -0
- package/dist/plugin/aot/rspack-loader.mjs +1 -0
- package/dist/plugin/aot/rspack.mjs +1 -0
- package/dist/plugin/aot/treeshake.mjs +1 -0
- package/dist/plugin/aot/unplugin.mjs +4 -3
- package/dist/plugin/aot/vite.mjs +1 -0
- package/dist/plugin/aot/worker.mjs +1 -0
- package/dist/plugin/auto-head.mjs +1 -0
- package/dist/plugin/trace.mjs +1 -0
- package/dist/plugin/websocket.mjs +1 -0
- package/dist/route-table.mjs +1 -0
- package/dist/schema-snapshot.mjs +1 -0
- package/dist/sucrose.d.ts +2 -2
- package/dist/sucrose.js +11 -11
- package/dist/sucrose.mjs +12 -11
- package/dist/trace.js +8 -10
- package/dist/trace.mjs +10 -11
- package/dist/type/bridge-live.d.ts +1 -1
- package/dist/type/bridge-live.mjs +1 -0
- package/dist/type/bridge.d.ts +1 -1
- package/dist/type/bridge.mjs +1 -0
- package/dist/type/coerce-plan.mjs +1 -0
- package/dist/type/coerce.d.ts +1 -1
- package/dist/type/coerce.mjs +1 -0
- package/dist/type/compat.mjs +1 -0
- package/dist/type/constants.d.ts +1 -1
- package/dist/type/constants.mjs +1 -0
- package/dist/type/elysia/accelerate.mjs +1 -0
- package/dist/type/elysia/array-buffer.d.ts +3 -2
- package/dist/type/elysia/array-buffer.js +5 -5
- package/dist/type/elysia/array-buffer.mjs +6 -5
- package/dist/type/elysia/array-string.mjs +1 -0
- package/dist/type/elysia/array.mjs +1 -0
- package/dist/type/elysia/boolean-string.mjs +1 -0
- package/dist/type/elysia/boolean.mjs +1 -0
- package/dist/type/elysia/cookie.mjs +1 -0
- package/dist/type/elysia/date.mjs +1 -0
- package/dist/type/elysia/file-type.mjs +1 -0
- package/dist/type/elysia/file.mjs +1 -0
- package/dist/type/elysia/files.mjs +1 -0
- package/dist/type/elysia/form.mjs +1 -0
- package/dist/type/elysia/integer-string.mjs +1 -0
- package/dist/type/elysia/integer.mjs +1 -0
- package/dist/type/elysia/intersect.mjs +1 -0
- package/dist/type/elysia/json-string.mjs +1 -0
- package/dist/type/elysia/keep-config.js +8 -8
- package/dist/type/elysia/keep-config.mjs +9 -8
- package/dist/type/elysia/maybe-empty.mjs +1 -0
- package/dist/type/elysia/no-validate.mjs +1 -0
- package/dist/type/elysia/nullable.mjs +1 -0
- package/dist/type/elysia/number.mjs +1 -0
- package/dist/type/elysia/numeric-enum.d.ts +4 -2
- package/dist/type/elysia/numeric-enum.mjs +1 -0
- package/dist/type/elysia/numeric.mjs +1 -0
- package/dist/type/elysia/object-string.mjs +1 -0
- package/dist/type/elysia/object.mjs +1 -0
- package/dist/type/elysia/optional.mjs +1 -0
- package/dist/type/elysia/problem.mjs +1 -0
- package/dist/type/elysia/string.mjs +1 -0
- package/dist/type/elysia/uint8-array.d.ts +2 -1
- package/dist/type/elysia/uint8-array.js +2 -2
- package/dist/type/elysia/uint8-array.mjs +3 -2
- package/dist/type/elysia/union-enum.mjs +1 -0
- package/dist/type/elysia/union.mjs +1 -0
- package/dist/type/elysia/utils.js +3 -3
- package/dist/type/elysia/utils.mjs +4 -3
- package/dist/type/exports.mjs +1 -0
- package/dist/type/index.mjs +2 -1
- package/dist/type/shared.mjs +1 -0
- package/dist/type/sync-require.mjs +1 -0
- package/dist/type/typebox-system-lite.mjs +1 -0
- package/dist/type/typebox-system-require.mjs +1 -0
- package/dist/type/typebox-type-live.mjs +1 -0
- package/dist/type/typebox-value-live.mjs +1 -0
- package/dist/type/typebox-value-ops.mjs +1 -0
- package/dist/type/typebox-value.mjs +1 -0
- package/dist/type/types.d.ts +1 -1
- package/dist/type/types.mjs +1 -0
- package/dist/type/utils.mjs +1 -0
- package/dist/type/validator/clean-safe.mjs +1 -0
- package/dist/type/validator/custom-error.js +41 -33
- package/dist/type/validator/custom-error.mjs +42 -33
- package/dist/type/validator/default-precompute.d.ts +2 -2
- package/dist/type/validator/default-precompute.js +10 -11
- package/dist/type/validator/default-precompute.mjs +13 -13
- package/dist/type/validator/exact-mirror.mjs +1 -0
- package/dist/type/validator/frozen-check.js +7 -7
- package/dist/type/validator/frozen-check.mjs +8 -7
- package/dist/type/validator/index.d.ts +2 -2
- package/dist/type/validator/index.js +4 -8
- package/dist/type/validator/index.mjs +5 -8
- package/dist/type/validator/string-codec-aot.d.ts +1 -1
- package/dist/type/validator/string-codec-aot.mjs +1 -0
- package/dist/type/validator/validator-cache.js +18 -18
- package/dist/type/validator/validator-cache.mjs +19 -18
- package/dist/types.d.ts +105 -69
- package/dist/types.mjs +1 -0
- package/dist/universal/constants.mjs +1 -0
- package/dist/universal/file.mjs +1 -0
- package/dist/universal/index.mjs +1 -0
- package/dist/universal/is-production.mjs +1 -0
- package/dist/universal/server.mjs +1 -0
- package/dist/utils.d.ts +49 -31
- package/dist/utils.js +221 -109
- package/dist/utils.mjs +215 -106
- package/dist/validator/index.d.ts +3 -3
- package/dist/validator/index.js +4 -4
- package/dist/validator/index.mjs +5 -4
- package/dist/validator/route.d.ts +2 -2
- package/dist/validator/route.mjs +1 -0
- package/dist/ws/context.js +1 -1
- package/dist/ws/context.mjs +2 -1
- package/dist/ws/index.mjs +1 -0
- package/dist/ws/parser.mjs +1 -0
- package/dist/ws/route.js +34 -25
- package/dist/ws/route.mjs +40 -30
- package/dist/ws/types.d.ts +2 -3
- package/dist/ws/types.mjs +1 -0
- package/package.json +2 -2
package/dist/ws/route.js
CHANGED
|
@@ -2,6 +2,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
2
2
|
const require_universal_constants = require('../universal/constants.js');
|
|
3
3
|
const require_utils = require('../utils.js');
|
|
4
4
|
const require_type_bridge = require('../type/bridge.js');
|
|
5
|
+
const require_universal_is_production = require('../universal/is-production.js');
|
|
5
6
|
const require_error = require('../error.js');
|
|
6
7
|
const require_cookie_crypto = require('../cookie/crypto.js');
|
|
7
8
|
const require_adapter_web_standard_handler = require('../adapter/web-standard/handler.js');
|
|
@@ -94,17 +95,22 @@ async function handleWSResponse(ws, value, mapResponses) {
|
|
|
94
95
|
ws.send(mapped);
|
|
95
96
|
}
|
|
96
97
|
function wsErrorFrameFallback(error) {
|
|
97
|
-
|
|
98
|
+
const type = error?.type;
|
|
99
|
+
if (require_handler_error.claimsProblemType(error, type)) try {
|
|
98
100
|
const status = require_handler_error.resolveStatus(error.status);
|
|
99
101
|
const served = typeof status === "number" && status >= 100 ? status : 500;
|
|
100
102
|
const stringify = (value) => typeof value === "object" && value !== null ? JSON.stringify(value) : String(value);
|
|
101
|
-
const problemFrame = (detail) =>
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
103
|
+
const problemFrame = (detail, trusted = true) => {
|
|
104
|
+
const code = trusted ? error.code : void 0;
|
|
105
|
+
return JSON.stringify(require_error.problemBody({
|
|
106
|
+
type: typeof type === "string" ? type : "about:blank",
|
|
107
|
+
...typeof code === "string" ? { code } : {},
|
|
108
|
+
detail,
|
|
109
|
+
status: served
|
|
110
|
+
}));
|
|
111
|
+
};
|
|
112
|
+
if (require_universal_is_production.isProduction() && served >= 500 && !(error instanceof require_error.HTTPError)) return error instanceof Error && typeof status === "number" && status >= 500 ? problemFrame(require_error.titleOf(served), false) : wsLegacyFrame(error);
|
|
113
|
+
const serveMessage = () => problemFrame(require_handler_error.statusFallbackBody(error, served, require_error.titleOf(served)));
|
|
108
114
|
const serveDetail = () => {
|
|
109
115
|
const detail = require_handler_error.readAnnotation(error, "detail", true);
|
|
110
116
|
if (detail === void 0) return serveMessage();
|
|
@@ -120,7 +126,7 @@ function wsErrorFrameFallback(error) {
|
|
|
120
126
|
}
|
|
121
127
|
function wsLegacyFrame(error) {
|
|
122
128
|
if (error?.status) {
|
|
123
|
-
const body = require_handler_error.statusFallbackBody(error, error.status);
|
|
129
|
+
const body = require_handler_error.statusFallbackBody(error, require_handler_error.resolveStatus(error.status));
|
|
124
130
|
return typeof body === "object" ? JSON.stringify(body) : String(body);
|
|
125
131
|
}
|
|
126
132
|
return require_error.internalServerErrorBodyString(error);
|
|
@@ -160,7 +166,7 @@ function sendErrorFrame(ws, error) {
|
|
|
160
166
|
} catch {}
|
|
161
167
|
}, () => {});
|
|
162
168
|
}
|
|
163
|
-
function
|
|
169
|
+
function validateChannel(validator, value, type) {
|
|
164
170
|
return validator.hasCodec || validator instanceof require_validator_index.StandardValidator ? validator.From(value, type) : validator.EncodeFrom(value, type);
|
|
165
171
|
}
|
|
166
172
|
const wsOptions = [
|
|
@@ -173,11 +179,12 @@ const wsOptions = [
|
|
|
173
179
|
"perMessageDeflate"
|
|
174
180
|
];
|
|
175
181
|
function buildWSRoute(route, app) {
|
|
176
|
-
const
|
|
182
|
+
const localHook = require_compile_handler_index.resolveWSLocalHook(require_compile_handler_index.localMacroRoot(route[7] ?? route[3] ?? app, app), route[4], app);
|
|
183
|
+
const hook = localHook ?? require_utils.nullObject();
|
|
177
184
|
const instance = route[3] ?? app;
|
|
178
185
|
const appHookChain = route[5];
|
|
179
186
|
const inheritedChain = route[6];
|
|
180
|
-
const composed = require_compile_handler_index.composeRouteHook(instance,
|
|
187
|
+
const composed = require_compile_handler_index.composeRouteHook(instance, localHook, appHookChain, inheritedChain, app, route[7]) ?? hook;
|
|
181
188
|
let validators;
|
|
182
189
|
const programId = require_generation.frozenRootOf(app)["~programId"];
|
|
183
190
|
const captured = require_compile_aot.Compiled.getHandler(programId, "WS", route[1]);
|
|
@@ -210,14 +217,14 @@ function buildWSRoute(route, app) {
|
|
|
210
217
|
});
|
|
211
218
|
} catch (error) {
|
|
212
219
|
if (!require_compile_handler_frozen_validator.isBridgeNotInitialized(error)) throw error;
|
|
213
|
-
if (live) throw new Error(require_compile_aot.
|
|
220
|
+
if (live) throw new Error(require_compile_aot.aotDriftMessage("WS", route[1]), { cause: error });
|
|
214
221
|
const frozen = require_compile_handler_frozen_validator.buildFrozenRouteValidator(composed, app, "WS", route[1]);
|
|
215
222
|
if (!frozen) throw error;
|
|
216
223
|
validators = frozen;
|
|
217
224
|
}
|
|
218
225
|
if (live && captured) require_compile_aot.warnAotDrift("WS", route[1]);
|
|
219
226
|
const responseValidator = validators.response;
|
|
220
|
-
const defaultResponseValidator = responseValidator
|
|
227
|
+
const defaultResponseValidator = responseValidator?.get(200) ?? responseValidator?.values().next().value;
|
|
221
228
|
const cookieIsOptional = !!composed.cookie?.["~optional"];
|
|
222
229
|
const queryChannels = require_compile_handler_utils.getQueryParseChannels(validators.query?.schema);
|
|
223
230
|
const queryArray = queryChannels?.array;
|
|
@@ -226,14 +233,16 @@ function buildWSRoute(route, app) {
|
|
|
226
233
|
const parseHooks = concatHooks(hook.parse);
|
|
227
234
|
const transforms = concatHooks(flatAppHook.transform, hook.transform);
|
|
228
235
|
const allBeforeHandles = concatHooks(flatAppHook.beforeHandle, hook.beforeHandle);
|
|
229
|
-
const
|
|
230
|
-
const
|
|
231
|
-
const upgradeDeriveModes = require_compile_handler_utils.deriveModes(allBeforeHandles, deriveEntries);
|
|
232
|
-
const messageBeforeHandles = allBeforeHandles.filter((fn) => !deriveSet?.has(fn));
|
|
236
|
+
const upgradeDeriveModes = require_compile_handler_utils.deriveModes(allBeforeHandles, require_compile_handler_utils.joinDeriveEntries([flatAppHook, hook]) ?? concatHooks(flatAppHook["~deriveEntries"], hook["~deriveEntries"]));
|
|
237
|
+
const messageBeforeHandles = allBeforeHandles.filter((_, i) => upgradeDeriveModes?.[i] === void 0);
|
|
233
238
|
const afterHandles = concatHooks(flatAppHook.afterHandle, hook.afterHandle);
|
|
234
239
|
const mapResponses = concatHooks(flatAppHook.mapResponse, hook.mapResponse);
|
|
235
240
|
const afterResponses = concatHooks(flatAppHook.afterResponse, hook.afterResponse);
|
|
236
241
|
const errorHandlers = concatHooks(hook.error, flatAppHook.error);
|
|
242
|
+
const finalizeOwnError = require_handler_utils.finalizeRouteErrorOf({
|
|
243
|
+
error: errorHandlers,
|
|
244
|
+
mapResponse: mapResponses
|
|
245
|
+
});
|
|
237
246
|
const inference = require_sucrose.sucrose(hook.message, {
|
|
238
247
|
beforeHandle: [
|
|
239
248
|
...allBeforeHandles,
|
|
@@ -252,7 +261,7 @@ function buildWSRoute(route, app) {
|
|
|
252
261
|
afterResponse: afterResponses
|
|
253
262
|
});
|
|
254
263
|
const cookieConfig = validators.cookie || inference.cookie ? require_cookie_config.compileCookieConfig(composed.cookie, require_generation.frozenRootOf(app)["~config"]?.cookie, require_generation.frozenRootOf(app)["~ext"]?.models, composed.schemas) : void 0;
|
|
255
|
-
const signCookies = cookieConfig?.hasSign ? (set) => require_cookie_utils.signCookieValues(set.cookie, cookieConfig) : void 0;
|
|
264
|
+
const signCookies = cookieConfig?.hasSign ? (set) => require_cookie_utils.signCookieValues(set.cookie, cookieConfig, set) : void 0;
|
|
256
265
|
const parseMessage = require_ws_parser.createMessageParser(parseHooks);
|
|
257
266
|
const handleUpgradeError = require_handler_error.createErrorHandler(errorHandlers.length ? errorHandlers : void 0, ((response, set, context) => {
|
|
258
267
|
const map = () => require_adapter_web_standard_handler.mapResponse(response, set, context?.request);
|
|
@@ -303,7 +312,7 @@ function buildWSRoute(route, app) {
|
|
|
303
312
|
if (bodyValidator) {
|
|
304
313
|
let decoded;
|
|
305
314
|
try {
|
|
306
|
-
decoded =
|
|
315
|
+
decoded = validateChannel(bodyValidator, message, "body");
|
|
307
316
|
} catch (error) {
|
|
308
317
|
return onMessageValidationError(ws, error);
|
|
309
318
|
}
|
|
@@ -396,7 +405,7 @@ function buildWSRoute(route, app) {
|
|
|
396
405
|
const request = context.request;
|
|
397
406
|
try {
|
|
398
407
|
if (validators.params) {
|
|
399
|
-
let r =
|
|
408
|
+
let r = validateChannel(validators.params, context.params ?? require_utils.nullObject(), "params");
|
|
400
409
|
if (r instanceof Promise) r = await r;
|
|
401
410
|
context.params = r;
|
|
402
411
|
}
|
|
@@ -404,7 +413,7 @@ function buildWSRoute(route, app) {
|
|
|
404
413
|
const url = request.url;
|
|
405
414
|
let r = require_parse_query.parseQueryFromURL(url, context.qi ?? url.indexOf("?"), queryArray, queryObject);
|
|
406
415
|
if (validators.query) {
|
|
407
|
-
r =
|
|
416
|
+
r = validateChannel(validators.query, r, "query");
|
|
408
417
|
if (r instanceof Promise) r = await r;
|
|
409
418
|
}
|
|
410
419
|
context.query = r;
|
|
@@ -412,7 +421,7 @@ function buildWSRoute(route, app) {
|
|
|
412
421
|
if (validators.headers || inference.headers) {
|
|
413
422
|
let r = require_universal_constants.isBun ? request.headers.toJSON() : Object.fromEntries(request.headers);
|
|
414
423
|
if (validators.headers) {
|
|
415
|
-
r =
|
|
424
|
+
r = validateChannel(validators.headers, r, "headers");
|
|
416
425
|
if (r instanceof Promise) r = await r;
|
|
417
426
|
}
|
|
418
427
|
context.headers = r;
|
|
@@ -423,7 +432,7 @@ function buildWSRoute(route, app) {
|
|
|
423
432
|
const raw = await require_cookie_utils.parseCookieRaw(cookieHeader, cookieConfig);
|
|
424
433
|
let r = raw;
|
|
425
434
|
if (!cookieIsOptional || Object.keys(raw).length) {
|
|
426
|
-
r =
|
|
435
|
+
r = validateChannel(validators.cookie, raw, "cookie");
|
|
427
436
|
if (r instanceof Promise) r = await r;
|
|
428
437
|
}
|
|
429
438
|
context.cookie = require_cookie_utils.buildCookieJar(context.set, r, cookieConfig);
|
|
@@ -481,7 +490,7 @@ function buildWSRoute(route, app) {
|
|
|
481
490
|
try {
|
|
482
491
|
return await handleUpgradeError(context, error);
|
|
483
492
|
} catch (thrown) {
|
|
484
|
-
return
|
|
493
|
+
return finalizeOwnError(app, context, thrown, signCookies);
|
|
485
494
|
}
|
|
486
495
|
}
|
|
487
496
|
};
|
package/dist/ws/route.mjs
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
|
+
// @bun
|
|
1
2
|
import { isBun } from "../universal/constants.mjs";
|
|
2
|
-
import { assignOwn,
|
|
3
|
+
import { assignOwn, isSocketQuiet, nullObject } from "../utils.mjs";
|
|
3
4
|
import { isBridgeLive } from "../type/bridge.mjs";
|
|
4
|
-
import {
|
|
5
|
+
import { isProduction } from "../universal/is-production.mjs";
|
|
6
|
+
import { ElysiaError, ElysiaStatus, HTTPError, ValidationError, elysiaErrorProblem, internalServerErrorBodyString, problemBody, problemResponse, titleOf } from "../error.mjs";
|
|
5
7
|
import { hasSyncHmac } from "../cookie/crypto.mjs";
|
|
6
8
|
import { mapResponse } from "../adapter/web-standard/handler.mjs";
|
|
7
9
|
import { parseQueryFromURL } from "../parse-query.mjs";
|
|
8
|
-
import { Capture, Compiled,
|
|
10
|
+
import { Capture, Compiled, aotDriftMessage, warnAotDrift } from "../compile/aot.mjs";
|
|
9
11
|
import { frozenRootOf } from "../generation.mjs";
|
|
10
12
|
import { compileCookieConfig } from "../cookie/config.mjs";
|
|
11
13
|
import { buildCookieJar, parseCookieRaw, parseCookieRawDeferred, parseCookieRawLazy, signCookieValues } from "../cookie/utils.mjs";
|
|
12
|
-
import {
|
|
14
|
+
import { finalizeRouteErrorOf } from "../handler/utils.mjs";
|
|
13
15
|
import { adoptErrorType, claimsProblemType, createErrorHandler, readAnnotation, resolveStatus, statusFallbackBody } from "../handler/error.mjs";
|
|
14
|
-
import { deriveModes, getQueryParseChannels, replaceDeriveContext } from "../compile/handler/utils.mjs";
|
|
16
|
+
import { deriveModes, getQueryParseChannels, joinDeriveEntries, replaceDeriveContext } from "../compile/handler/utils.mjs";
|
|
15
17
|
import { sucrose } from "../sucrose.mjs";
|
|
16
18
|
import { StandardValidator } from "../validator/index.mjs";
|
|
17
19
|
import { RouteValidator } from "../validator/route.mjs";
|
|
@@ -93,17 +95,22 @@ async function handleWSResponse(ws, value, mapResponses) {
|
|
|
93
95
|
ws.send(mapped);
|
|
94
96
|
}
|
|
95
97
|
function wsErrorFrameFallback(error) {
|
|
96
|
-
|
|
98
|
+
const type = error?.type;
|
|
99
|
+
if (claimsProblemType(error, type)) try {
|
|
97
100
|
const status = resolveStatus(error.status);
|
|
98
101
|
const served = typeof status === "number" && status >= 100 ? status : 500;
|
|
99
102
|
const stringify = (value) => typeof value === "object" && value !== null ? JSON.stringify(value) : String(value);
|
|
100
|
-
const problemFrame = (detail) =>
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
103
|
+
const problemFrame = (detail, trusted = true) => {
|
|
104
|
+
const code = trusted ? error.code : void 0;
|
|
105
|
+
return JSON.stringify(problemBody({
|
|
106
|
+
type: typeof type === "string" ? type : "about:blank",
|
|
107
|
+
...typeof code === "string" ? { code } : {},
|
|
108
|
+
detail,
|
|
109
|
+
status: served
|
|
110
|
+
}));
|
|
111
|
+
};
|
|
112
|
+
if (isProduction() && served >= 500 && !(error instanceof HTTPError)) return error instanceof Error && typeof status === "number" && status >= 500 ? problemFrame(titleOf(served), false) : wsLegacyFrame(error);
|
|
113
|
+
const serveMessage = () => problemFrame(statusFallbackBody(error, served, titleOf(served)));
|
|
107
114
|
const serveDetail = () => {
|
|
108
115
|
const detail = readAnnotation(error, "detail", true);
|
|
109
116
|
if (detail === void 0) return serveMessage();
|
|
@@ -119,7 +126,7 @@ function wsErrorFrameFallback(error) {
|
|
|
119
126
|
}
|
|
120
127
|
function wsLegacyFrame(error) {
|
|
121
128
|
if (error?.status) {
|
|
122
|
-
const body = statusFallbackBody(error, error.status);
|
|
129
|
+
const body = statusFallbackBody(error, resolveStatus(error.status));
|
|
123
130
|
return typeof body === "object" ? JSON.stringify(body) : String(body);
|
|
124
131
|
}
|
|
125
132
|
return internalServerErrorBodyString(error);
|
|
@@ -159,7 +166,7 @@ function sendErrorFrame(ws, error) {
|
|
|
159
166
|
} catch {}
|
|
160
167
|
}, () => {});
|
|
161
168
|
}
|
|
162
|
-
function
|
|
169
|
+
function validateChannel(validator, value, type) {
|
|
163
170
|
return validator.hasCodec || validator instanceof StandardValidator ? validator.From(value, type) : validator.EncodeFrom(value, type);
|
|
164
171
|
}
|
|
165
172
|
const wsOptions = [
|
|
@@ -172,11 +179,12 @@ const wsOptions = [
|
|
|
172
179
|
"perMessageDeflate"
|
|
173
180
|
];
|
|
174
181
|
function buildWSRoute(route, app) {
|
|
175
|
-
const
|
|
182
|
+
const localHook = resolveWSLocalHook(localMacroRoot(route[7] ?? route[3] ?? app, app), route[4], app);
|
|
183
|
+
const hook = localHook ?? nullObject();
|
|
176
184
|
const instance = route[3] ?? app;
|
|
177
185
|
const appHookChain = route[5];
|
|
178
186
|
const inheritedChain = route[6];
|
|
179
|
-
const composed = composeRouteHook(instance,
|
|
187
|
+
const composed = composeRouteHook(instance, localHook, appHookChain, inheritedChain, app, route[7]) ?? hook;
|
|
180
188
|
let validators;
|
|
181
189
|
const programId = frozenRootOf(app)["~programId"];
|
|
182
190
|
const captured = Compiled.getHandler(programId, "WS", route[1]);
|
|
@@ -209,14 +217,14 @@ function buildWSRoute(route, app) {
|
|
|
209
217
|
});
|
|
210
218
|
} catch (error) {
|
|
211
219
|
if (!isBridgeNotInitialized(error)) throw error;
|
|
212
|
-
if (live) throw new Error(
|
|
220
|
+
if (live) throw new Error(aotDriftMessage("WS", route[1]), { cause: error });
|
|
213
221
|
const frozen = buildFrozenRouteValidator(composed, app, "WS", route[1]);
|
|
214
222
|
if (!frozen) throw error;
|
|
215
223
|
validators = frozen;
|
|
216
224
|
}
|
|
217
225
|
if (live && captured) warnAotDrift("WS", route[1]);
|
|
218
226
|
const responseValidator = validators.response;
|
|
219
|
-
const defaultResponseValidator = responseValidator
|
|
227
|
+
const defaultResponseValidator = responseValidator?.get(200) ?? responseValidator?.values().next().value;
|
|
220
228
|
const cookieIsOptional = !!composed.cookie?.["~optional"];
|
|
221
229
|
const queryChannels = getQueryParseChannels(validators.query?.schema);
|
|
222
230
|
const queryArray = queryChannels?.array;
|
|
@@ -225,14 +233,16 @@ function buildWSRoute(route, app) {
|
|
|
225
233
|
const parseHooks = concatHooks(hook.parse);
|
|
226
234
|
const transforms = concatHooks(flatAppHook.transform, hook.transform);
|
|
227
235
|
const allBeforeHandles = concatHooks(flatAppHook.beforeHandle, hook.beforeHandle);
|
|
228
|
-
const
|
|
229
|
-
const
|
|
230
|
-
const upgradeDeriveModes = deriveModes(allBeforeHandles, deriveEntries);
|
|
231
|
-
const messageBeforeHandles = allBeforeHandles.filter((fn) => !deriveSet?.has(fn));
|
|
236
|
+
const upgradeDeriveModes = deriveModes(allBeforeHandles, joinDeriveEntries([flatAppHook, hook]) ?? concatHooks(flatAppHook["~deriveEntries"], hook["~deriveEntries"]));
|
|
237
|
+
const messageBeforeHandles = allBeforeHandles.filter((_, i) => upgradeDeriveModes?.[i] === void 0);
|
|
232
238
|
const afterHandles = concatHooks(flatAppHook.afterHandle, hook.afterHandle);
|
|
233
239
|
const mapResponses = concatHooks(flatAppHook.mapResponse, hook.mapResponse);
|
|
234
240
|
const afterResponses = concatHooks(flatAppHook.afterResponse, hook.afterResponse);
|
|
235
241
|
const errorHandlers = concatHooks(hook.error, flatAppHook.error);
|
|
242
|
+
const finalizeOwnError = finalizeRouteErrorOf({
|
|
243
|
+
error: errorHandlers,
|
|
244
|
+
mapResponse: mapResponses
|
|
245
|
+
});
|
|
236
246
|
const inference = sucrose(hook.message, {
|
|
237
247
|
beforeHandle: [
|
|
238
248
|
...allBeforeHandles,
|
|
@@ -251,7 +261,7 @@ function buildWSRoute(route, app) {
|
|
|
251
261
|
afterResponse: afterResponses
|
|
252
262
|
});
|
|
253
263
|
const cookieConfig = validators.cookie || inference.cookie ? compileCookieConfig(composed.cookie, frozenRootOf(app)["~config"]?.cookie, frozenRootOf(app)["~ext"]?.models, composed.schemas) : void 0;
|
|
254
|
-
const signCookies = cookieConfig?.hasSign ? (set) => signCookieValues(set.cookie, cookieConfig) : void 0;
|
|
264
|
+
const signCookies = cookieConfig?.hasSign ? (set) => signCookieValues(set.cookie, cookieConfig, set) : void 0;
|
|
255
265
|
const parseMessage = createMessageParser(parseHooks);
|
|
256
266
|
const handleUpgradeError = createErrorHandler(errorHandlers.length ? errorHandlers : void 0, ((response, set, context) => {
|
|
257
267
|
const map = () => mapResponse(response, set, context?.request);
|
|
@@ -302,7 +312,7 @@ function buildWSRoute(route, app) {
|
|
|
302
312
|
if (bodyValidator) {
|
|
303
313
|
let decoded;
|
|
304
314
|
try {
|
|
305
|
-
decoded =
|
|
315
|
+
decoded = validateChannel(bodyValidator, message, "body");
|
|
306
316
|
} catch (error) {
|
|
307
317
|
return onMessageValidationError(ws, error);
|
|
308
318
|
}
|
|
@@ -395,7 +405,7 @@ function buildWSRoute(route, app) {
|
|
|
395
405
|
const request = context.request;
|
|
396
406
|
try {
|
|
397
407
|
if (validators.params) {
|
|
398
|
-
let r =
|
|
408
|
+
let r = validateChannel(validators.params, context.params ?? nullObject(), "params");
|
|
399
409
|
if (r instanceof Promise) r = await r;
|
|
400
410
|
context.params = r;
|
|
401
411
|
}
|
|
@@ -403,7 +413,7 @@ function buildWSRoute(route, app) {
|
|
|
403
413
|
const url = request.url;
|
|
404
414
|
let r = parseQueryFromURL(url, context.qi ?? url.indexOf("?"), queryArray, queryObject);
|
|
405
415
|
if (validators.query) {
|
|
406
|
-
r =
|
|
416
|
+
r = validateChannel(validators.query, r, "query");
|
|
407
417
|
if (r instanceof Promise) r = await r;
|
|
408
418
|
}
|
|
409
419
|
context.query = r;
|
|
@@ -411,7 +421,7 @@ function buildWSRoute(route, app) {
|
|
|
411
421
|
if (validators.headers || inference.headers) {
|
|
412
422
|
let r = isBun ? request.headers.toJSON() : Object.fromEntries(request.headers);
|
|
413
423
|
if (validators.headers) {
|
|
414
|
-
r =
|
|
424
|
+
r = validateChannel(validators.headers, r, "headers");
|
|
415
425
|
if (r instanceof Promise) r = await r;
|
|
416
426
|
}
|
|
417
427
|
context.headers = r;
|
|
@@ -422,7 +432,7 @@ function buildWSRoute(route, app) {
|
|
|
422
432
|
const raw = await parseCookieRaw(cookieHeader, cookieConfig);
|
|
423
433
|
let r = raw;
|
|
424
434
|
if (!cookieIsOptional || Object.keys(raw).length) {
|
|
425
|
-
r =
|
|
435
|
+
r = validateChannel(validators.cookie, raw, "cookie");
|
|
426
436
|
if (r instanceof Promise) r = await r;
|
|
427
437
|
}
|
|
428
438
|
context.cookie = buildCookieJar(context.set, r, cookieConfig);
|
|
@@ -480,7 +490,7 @@ function buildWSRoute(route, app) {
|
|
|
480
490
|
try {
|
|
481
491
|
return await handleUpgradeError(context, error);
|
|
482
492
|
} catch (thrown) {
|
|
483
|
-
return
|
|
493
|
+
return finalizeOwnError(app, context, thrown, signCookies);
|
|
484
494
|
}
|
|
485
495
|
}
|
|
486
496
|
};
|
package/dist/ws/types.d.ts
CHANGED
|
@@ -107,9 +107,8 @@ interface WSValidatorLike {
|
|
|
107
107
|
Check(data: unknown): boolean;
|
|
108
108
|
Errors(data: unknown): any[];
|
|
109
109
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
} | undefined;
|
|
110
|
+
/** keyed by numeric status */
|
|
111
|
+
type WSResponseValidator = Map<number, WSValidatorLike> | undefined;
|
|
113
112
|
/**
|
|
114
113
|
* App-wide WebSocket server-tuning options, the Bun server-level
|
|
115
114
|
* `websocket: {...}` knobs (`maxPayloadLength`, `idleTimeout`,
|
package/dist/ws/types.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "elysia",
|
|
3
3
|
"description": "Ergonomic Framework for Human",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.22",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "saltyAom",
|
|
7
7
|
"url": "https://github.com/SaltyAom",
|
|
@@ -216,7 +216,7 @@
|
|
|
216
216
|
],
|
|
217
217
|
"dependencies": {
|
|
218
218
|
"deuri": "^3.0.0",
|
|
219
|
-
"memoirist": "1.2.
|
|
219
|
+
"memoirist": "1.2.3"
|
|
220
220
|
},
|
|
221
221
|
"devDependencies": {
|
|
222
222
|
"@eslint/eslintrc": "^3.3.5",
|