elysia 1.3.5 → 1.3.6

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.
@@ -134,6 +134,7 @@ var primitiveHooks = [
134
134
  {}
135
135
  );
136
136
  var isBun2 = typeof Bun < "u", hasBunHash = isBun2 && typeof Bun.hash == "function", checksum = (s) => {
137
+ if (hasBunHash) return Bun.hash(s);
137
138
  let h = 9;
138
139
  for (let i = 0; i < s.length; ) h = Math.imul(h ^ s.charCodeAt(i++), 9 ** 9);
139
140
  return h = h ^ h >>> 9;
@@ -2008,14 +2009,14 @@ var allocateIf = (value, condition) => condition ? value : "", createContext = (
2008
2009
  hasTrace && (fnLiteral += `const id=randomId()
2009
2010
  `);
2010
2011
  let isDynamic = /[:*]/.test(route.path), getQi = `const u=request.url,s=u.indexOf('/',${app.config.handler?.standardHostname ?? !0 ? 11 : 7}),qi=u.indexOf('?', s + 1)
2011
- `;
2012
- inference.query && (fnLiteral += getQi);
2013
- let getPath = inference.path ? isDynamic ? "get path(){" + (inference.query ? "" : getQi) + `if(qi===-1)return u.substring(s)
2012
+ `, needsQuery = inference.query || !!route.hooks.query || !!route.standaloneValidators?.find((x) => x.query);
2013
+ needsQuery && (fnLiteral += getQi);
2014
+ let getPath = inference.path ? isDynamic ? "get path(){" + (needsQuery ? "" : getQi) + `if(qi===-1)return u.substring(s)
2014
2015
  return u.substring(s,qi)
2015
2016
  },` : `path:'${route.path}',` : "";
2016
- fnLiteral += allocateIf("const c=", !isInline) + "{request,store," + allocateIf("qi,", inference.query) + allocateIf("params:request.params,", isDynamic) + getPath + allocateIf(
2017
+ fnLiteral += allocateIf("const c=", !isInline) + "{request,store," + allocateIf("qi,", needsQuery) + allocateIf("params:request.params,", isDynamic) + getPath + allocateIf(
2017
2018
  "url:request.url,",
2018
- hasTrace || inference.url || inference.query
2019
+ hasTrace || inference.url || needsQuery
2019
2020
  ) + "redirect,error:status,status,set:{headers:" + (isNotEmpty(defaultHeaders) ? "Object.assign({},app.setHeaders)" : "Object.create(null)") + ",status:200}", inference.server && (fnLiteral += ",get server(){return app.getServer()}"), hasTrace && (fnLiteral += ",[ELYSIA_REQUEST_ID]:id");
2020
2021
  {
2021
2022
  let decoratorsLiteral = "";
@@ -2039,8 +2040,10 @@ return u.substring(s,qi)
2039
2040
  );
2040
2041
  let fnLiteral = "const handler=data.handler,app=data.app,store=data.store,decorator=data.decorator,redirect=data.redirect,route=data.route,mapEarlyResponse=data.mapEarlyResponse," + allocateIf("randomId=data.randomId,", hasTrace) + allocateIf("ELYSIA_REQUEST_ID=data.ELYSIA_REQUEST_ID,", hasTrace) + allocateIf("ELYSIA_TRACE=data.ELYSIA_TRACE,", hasTrace) + allocateIf("trace=data.trace,", hasTrace) + allocateIf("hoc=data.hoc,", hasHoc) + `status=data.status
2041
2042
  `;
2042
- return app.event.request?.length && (fnLiteral += `const onRequest=app.event.request.map(x=>x.fn)
2043
- `), fnLiteral += `${app.event.request?.find(isAsync) ? "async" : ""} function map(request){`, hasTrace || inference.query || app.event.request?.length ? (fnLiteral += createContext(app, route, inference), fnLiteral += createOnRequestHandler(app), fnLiteral += "return handler(c)}") : fnLiteral += `return handler(${createContext(app, route, inference, !0)})}`, fnLiteral += createHoc(app), Function(
2043
+ app.event.request?.length && (fnLiteral += `const onRequest=app.event.request.map(x=>x.fn)
2044
+ `), fnLiteral += `${app.event.request?.find(isAsync) ? "async" : ""} function map(request){`;
2045
+ let needsQuery = inference.query || !!route.hooks.query || !!route.standaloneValidators?.find((x) => x.query);
2046
+ return hasTrace || needsQuery ? (fnLiteral += createContext(app, route, inference), fnLiteral += createOnRequestHandler(app), fnLiteral += "return handler(c)}") : fnLiteral += `return handler(${createContext(app, route, inference, !0)})}`, fnLiteral += createHoc(app), Function(
2044
2047
  "data",
2045
2048
  fnLiteral
2046
2049
  )({
@@ -697,7 +697,7 @@ var createNativeStaticHandler = (handle, hooks, setHeaders = {}) => {
697
697
  return response instanceof Promise ? response.then((response2) => {
698
698
  if (response2)
699
699
  return response2.headers.has("content-type") || response2.headers.append("content-type", "text/plain"), response2.clone();
700
- }) : (response.headers.has("content-type") || response.headers.append("content-type", "text/plain"), response.clone.bind(response));
700
+ }) : (response.headers.has("content-type") || response.headers.append("content-type", "text/plain"), () => response.clone());
701
701
  };
702
702
  export {
703
703
  createNativeStaticHandler
@@ -138,6 +138,7 @@ var primitiveHooks = [
138
138
  {}
139
139
  );
140
140
  var isBun2 = typeof Bun < "u", hasBunHash = isBun2 && typeof Bun.hash == "function", checksum = (s) => {
141
+ if (hasBunHash) return Bun.hash(s);
141
142
  let h = 9;
142
143
  for (let i = 0; i < s.length; ) h = Math.imul(h ^ s.charCodeAt(i++), 9 ** 9);
143
144
  return h = h ^ h >>> 9;
@@ -1184,6 +1185,15 @@ else c.body[key]=value}`;
1184
1185
  }
1185
1186
  }
1186
1187
  },
1188
+ async stop(app, closeActiveConnections) {
1189
+ if (!app.server)
1190
+ throw new Error(
1191
+ "Elysia isn't running. Call `app.listen` to start the server."
1192
+ );
1193
+ if (app.server && (app.server.stop(closeActiveConnections), app.server = null, app.event.stop?.length))
1194
+ for (let i = 0; i < app.event.stop.length; i++)
1195
+ app.event.stop[i].fn(app);
1196
+ },
1187
1197
  composeGeneralHandler: {
1188
1198
  parameters: "r",
1189
1199
  createContext(app) {
@@ -2125,6 +2135,8 @@ var hasProperty = (expectedProperty, _schema) => {
2125
2135
  }, _replaceSchemaType = (schema, options, config) => {
2126
2136
  if (!schema) return schema;
2127
2137
  let root = config.root;
2138
+ if (options.onlyFirstObject && schema.type === "object")
2139
+ return options.to(schema) ?? schema;
2128
2140
  if (options.untilObjectFound && !root && schema.type === "object")
2129
2141
  return schema;
2130
2142
  let fromSymbol = options.from[Kind3];
@@ -2321,17 +2333,20 @@ var hasProperty = (expectedProperty, _schema) => {
2321
2333
  }));
2322
2334
  break;
2323
2335
  }
2324
- return schema;
2336
+ return schema.type === "array" && schema.items && (Array.isArray(schema.items) ? schema.items = schema.items.map(
2337
+ (item) => _replaceSchemaType(item, options, {
2338
+ ...config,
2339
+ root: !1
2340
+ })
2341
+ ) : schema.items = _replaceSchemaType(schema.items, options, {
2342
+ ...config,
2343
+ root: !1
2344
+ })), schema;
2325
2345
  }, createCleaner = (schema) => (value) => {
2326
2346
  if (typeof value == "object")
2327
2347
  try {
2328
2348
  return Value3.Clean(schema, value);
2329
2349
  } catch {
2330
- try {
2331
- return Value3.Clean(schema, value);
2332
- } catch {
2333
- return value;
2334
- }
2335
2350
  }
2336
2351
  return value;
2337
2352
  }, getSchemaValidator = (s, {
@@ -2403,7 +2418,18 @@ var hasProperty = (expectedProperty, _schema) => {
2403
2418
  })
2404
2419
  ]), schema.type === "object" && hasAdditional && (schema.additionalProperties = !1));
2405
2420
  } else
2406
- schema.type === "object" && !("additionalProperties" in schema) && (schema.additionalProperties = additionalProperties);
2421
+ schema.type === "object" && !("additionalProperties" in schema) ? schema.additionalProperties = additionalProperties : schema = replaceSchemaType(schema, {
2422
+ onlyFirstObject: !0,
2423
+ from: t.Object({}),
2424
+ // @ts-ignore
2425
+ to({ properties, ...options }) {
2426
+ if (properties && !("additionalProperties" in schema))
2427
+ return t.Object(properties, {
2428
+ ...options,
2429
+ additionalProperties: !1
2430
+ });
2431
+ }
2432
+ });
2407
2433
  if (dynamic) {
2408
2434
  let validator = {
2409
2435
  schema,
@@ -3068,14 +3094,14 @@ var allocateIf = (value, condition) => condition ? value : "", createContext = (
3068
3094
  hasTrace && (fnLiteral += `const id=randomId()
3069
3095
  `);
3070
3096
  let isDynamic = /[:*]/.test(route.path), getQi = `const u=request.url,s=u.indexOf('/',${app.config.handler?.standardHostname ?? !0 ? 11 : 7}),qi=u.indexOf('?', s + 1)
3071
- `;
3072
- inference.query && (fnLiteral += getQi);
3073
- let getPath = inference.path ? isDynamic ? "get path(){" + (inference.query ? "" : getQi) + `if(qi===-1)return u.substring(s)
3097
+ `, needsQuery = inference.query || !!route.hooks.query || !!route.standaloneValidators?.find((x) => x.query);
3098
+ needsQuery && (fnLiteral += getQi);
3099
+ let getPath = inference.path ? isDynamic ? "get path(){" + (needsQuery ? "" : getQi) + `if(qi===-1)return u.substring(s)
3074
3100
  return u.substring(s,qi)
3075
3101
  },` : `path:'${route.path}',` : "";
3076
- fnLiteral += allocateIf("const c=", !isInline) + "{request,store," + allocateIf("qi,", inference.query) + allocateIf("params:request.params,", isDynamic) + getPath + allocateIf(
3102
+ fnLiteral += allocateIf("const c=", !isInline) + "{request,store," + allocateIf("qi,", needsQuery) + allocateIf("params:request.params,", isDynamic) + getPath + allocateIf(
3077
3103
  "url:request.url,",
3078
- hasTrace || inference.url || inference.query
3104
+ hasTrace || inference.url || needsQuery
3079
3105
  ) + "redirect,error:status,status,set:{headers:" + (isNotEmpty(defaultHeaders) ? "Object.assign({},app.setHeaders)" : "Object.create(null)") + ",status:200}", inference.server && (fnLiteral += ",get server(){return app.getServer()}"), hasTrace && (fnLiteral += ",[ELYSIA_REQUEST_ID]:id");
3080
3106
  {
3081
3107
  let decoratorsLiteral = "";
@@ -3099,8 +3125,10 @@ return u.substring(s,qi)
3099
3125
  );
3100
3126
  let fnLiteral = "const handler=data.handler,app=data.app,store=data.store,decorator=data.decorator,redirect=data.redirect,route=data.route,mapEarlyResponse=data.mapEarlyResponse," + allocateIf("randomId=data.randomId,", hasTrace) + allocateIf("ELYSIA_REQUEST_ID=data.ELYSIA_REQUEST_ID,", hasTrace) + allocateIf("ELYSIA_TRACE=data.ELYSIA_TRACE,", hasTrace) + allocateIf("trace=data.trace,", hasTrace) + allocateIf("hoc=data.hoc,", hasHoc) + `status=data.status
3101
3127
  `;
3102
- return app.event.request?.length && (fnLiteral += `const onRequest=app.event.request.map(x=>x.fn)
3103
- `), fnLiteral += `${app.event.request?.find(isAsync) ? "async" : ""} function map(request){`, hasTrace || inference.query || app.event.request?.length ? (fnLiteral += createContext(app, route, inference), fnLiteral += createOnRequestHandler(app), fnLiteral += "return handler(c)}") : fnLiteral += `return handler(${createContext(app, route, inference, !0)})}`, fnLiteral += createHoc(app), Function(
3128
+ app.event.request?.length && (fnLiteral += `const onRequest=app.event.request.map(x=>x.fn)
3129
+ `), fnLiteral += `${app.event.request?.find(isAsync) ? "async" : ""} function map(request){`;
3130
+ let needsQuery = inference.query || !!route.hooks.query || !!route.standaloneValidators?.find((x) => x.query);
3131
+ return hasTrace || needsQuery ? (fnLiteral += createContext(app, route, inference), fnLiteral += createOnRequestHandler(app), fnLiteral += "return handler(c)}") : fnLiteral += `return handler(${createContext(app, route, inference, !0)})}`, fnLiteral += createHoc(app), Function(
3104
3132
  "data",
3105
3133
  fnLiteral
3106
3134
  )({
@@ -3133,7 +3161,7 @@ var createNativeStaticHandler = (handle, hooks, setHeaders = {}) => {
3133
3161
  return response instanceof Promise ? response.then((response2) => {
3134
3162
  if (response2)
3135
3163
  return response2.headers.has("content-type") || response2.headers.append("content-type", "text/plain"), response2.clone();
3136
- }) : (response.headers.has("content-type") || response.headers.append("content-type", "text/plain"), response.clone.bind(response));
3164
+ }) : (response.headers.has("content-type") || response.headers.append("content-type", "text/plain"), () => response.clone());
3137
3165
  };
3138
3166
 
3139
3167
  // src/ws/index.ts
@@ -3414,7 +3442,6 @@ for(const [k,v] of c.request.headers.entries())c.headers[k]=v
3414
3442
  ...websocket || {}
3415
3443
  },
3416
3444
  fetch: app.fetch
3417
- // error: outerErrorHandler
3418
3445
  } : {
3419
3446
  development: !isProduction,
3420
3447
  reusePort: !0,
@@ -3434,7 +3461,6 @@ for(const [k,v] of c.request.headers.entries())c.headers[k]=v
3434
3461
  },
3435
3462
  port: options,
3436
3463
  fetch: app.fetch
3437
- // error: outerErrorHandler
3438
3464
  };
3439
3465
  if (app.server = Bun.serve(serve), app.event.start)
3440
3466
  for (let i = 0; i < app.event.start.length; i++)
@@ -3462,6 +3488,15 @@ for(const [k,v] of c.request.headers.entries())c.headers[k]=v
3462
3488
  });
3463
3489
  };
3464
3490
  },
3491
+ async stop(app, closeActiveConnections) {
3492
+ if (!app.server)
3493
+ throw new Error(
3494
+ "Elysia isn't running. Call `app.listen` to start the server."
3495
+ );
3496
+ if (app.server && (app.server.stop(closeActiveConnections), app.server = null, app.event.stop?.length))
3497
+ for (let i = 0; i < app.event.stop.length; i++)
3498
+ app.event.stop[i].fn(app);
3499
+ },
3465
3500
  ws(app, path, options) {
3466
3501
  let { parse: parse2, body, response, ...rest } = options, validateMessage = getSchemaValidator(body, {
3467
3502
  // @ts-expect-error private property
@@ -3503,7 +3538,7 @@ for(const [k,v] of c.request.headers.entries())c.headers[k]=v
3503
3538
  result instanceof Promise && await result;
3504
3539
  }
3505
3540
  let errorHandlers = [
3506
- ...Array.isArray(options.error) ? options.error : [options.error],
3541
+ ...options.error ? Array.isArray(options.error) ? options.error : [options.error] : [],
3507
3542
  ...(app.event.error ?? []).map(
3508
3543
  (x) => typeof x == "function" ? x : x.fn
3509
3544
  )
@@ -3530,9 +3565,9 @@ for(const [k,v] of c.request.headers.entries())c.headers[k]=v
3530
3565
  pong(data) {
3531
3566
  options.pong?.(data);
3532
3567
  },
3533
- open(ws) {
3568
+ open: async (ws) => {
3534
3569
  try {
3535
- handleResponse3(
3570
+ await handleResponse3(
3536
3571
  ws,
3537
3572
  options.open?.(
3538
3573
  new ElysiaWS(ws, context)
@@ -3553,7 +3588,7 @@ for(const [k,v] of c.request.headers.entries())c.headers[k]=v
3553
3588
  ).message
3554
3589
  );
3555
3590
  try {
3556
- handleResponse3(
3591
+ await handleResponse3(
3557
3592
  ws,
3558
3593
  options.message?.(
3559
3594
  new ElysiaWS(
@@ -3568,9 +3603,9 @@ for(const [k,v] of c.request.headers.entries())c.headers[k]=v
3568
3603
  handleErrors(ws, error);
3569
3604
  }
3570
3605
  },
3571
- drain(ws) {
3606
+ drain: async (ws) => {
3572
3607
  try {
3573
- handleResponse3(
3608
+ await handleResponse3(
3574
3609
  ws,
3575
3610
  options.drain?.(
3576
3611
  new ElysiaWS(ws, context)
@@ -3580,9 +3615,9 @@ for(const [k,v] of c.request.headers.entries())c.headers[k]=v
3580
3615
  handleErrors(ws, error);
3581
3616
  }
3582
3617
  },
3583
- close(ws, code, reason) {
3618
+ close: async (ws, code, reason) => {
3584
3619
  try {
3585
- handleResponse3(
3620
+ await handleResponse3(
3586
3621
  ws,
3587
3622
  options.close?.(
3588
3623
  new ElysiaWS(ws, context),
@@ -7,6 +7,21 @@ import type { AnyWSLocalHook } from '../ws/types';
7
7
  export interface ElysiaAdapter {
8
8
  name: string;
9
9
  listen(app: AnyElysia): (options: string | number | Partial<Serve>, callback?: ListenCallback) => void;
10
+ /**
11
+ * Stop server from serving
12
+ *
13
+ * ---
14
+ * @example
15
+ * ```typescript
16
+ * app.stop()
17
+ * ```
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * app.stop(true) // Abruptly any requests inflight
22
+ * ```
23
+ */
24
+ stop(app: AnyElysia, closeActiveConnections?: boolean): Promise<void>;
10
25
  isWebStandard?: boolean;
11
26
  handler: {
12
27
  /**
@@ -918,6 +918,15 @@ else c.body[key]=value}`;
918
918
  }
919
919
  }
920
920
  },
921
+ async stop(app, closeActiveConnections) {
922
+ if (!app.server)
923
+ throw new Error(
924
+ "Elysia isn't running. Call `app.listen` to start the server."
925
+ );
926
+ if (app.server && (app.server.stop(closeActiveConnections), app.server = null, app.event.stop?.length))
927
+ for (let i = 0; i < app.event.stop.length; i++)
928
+ app.event.stop[i].fn(app);
929
+ },
921
930
  composeGeneralHandler: {
922
931
  parameters: "r",
923
932
  createContext(app) {
@@ -1802,7 +1802,8 @@ export default class Elysia<const in out BasePath extends string = '', const in
1802
1802
  * app.stop(true) // Abruptly any requests inflight
1803
1803
  * ```
1804
1804
  */
1805
- stop: (closeActiveConnections?: boolean) => Promise<void>;
1805
+ stop: (closeActiveConnections?: boolean) => Promise<this>;
1806
+ [Symbol.dispose]: () => void;
1806
1807
  /**
1807
1808
  * Wait until all lazy loaded modules all load is fully
1808
1809
  */