@server/next 0.32.0 → 0.33.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.d.ts +4 -11
  2. package/index.js +18 -53
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -104,17 +104,16 @@ type AuthUser<T = Record<string, any>> = T & {
104
104
  strategy: Strategy;
105
105
  email: string;
106
106
  };
107
- type ProviderString = Provider | `${Provider}|${Provider}`;
108
- type AuthOption = `${Strategy}:${Provider | ProviderString}` | {
109
- provider: Provider | ProviderString | Provider[];
107
+ type AuthOption = `${Strategy}:${Provider}` | {
110
108
  strategy: Strategy;
109
+ providers: Provider | Provider[];
111
110
  session?: KVStore;
112
111
  store?: KVStore;
113
112
  redirect?: string;
114
113
  cleanUser?: <T = AuthUser>(user: T) => T | Promise<T>;
115
114
  };
116
115
  type AuthSettings = {
117
- provider: Provider[];
116
+ providers: Provider[];
118
117
  strategy: Strategy;
119
118
  store: KVStore;
120
119
  session: KVStore;
@@ -197,11 +196,6 @@ type PathToParams<Path extends string> = ParamsToObject<ExtractPathParams<Path>>
197
196
  type BunEnv = Record<string, string> & {
198
197
  upgrade?: (req: Request) => boolean;
199
198
  };
200
- type EventCallback = (data: Context & SerializableValue) => void;
201
- type Events = Record<string, EventCallback[]> & {
202
- on?: (key: string, cb: (value?: Context & SerializableValue) => void) => void;
203
- trigger?: (key: string, value?: Partial<Context & SerializableValue>) => void;
204
- };
205
199
  type Context<Params extends Record<string, string | undefined> = Record<string, string>, O extends ServerConfig = object> = {
206
200
  method: Method;
207
201
  ip: string;
@@ -224,7 +218,6 @@ type Context<Params extends Record<string, string | undefined> = Record<string,
224
218
  User?: infer U;
225
219
  } ? U extends Record<"User", infer Inner> ? Inner : AuthUser : AuthUser;
226
220
  init: number;
227
- events: Events;
228
221
  req?: Request;
229
222
  res?: Response & {
230
223
  cookies?: Record<string, string>;
@@ -458,4 +451,4 @@ declare class Server<O extends ServerConfig = {}> extends Router<O> {
458
451
  }
459
452
  declare function server<Session extends Record<string, any> = {}, User extends Record<string, any> = {}>(options?: Options): Server<ServerConfig<Session, User>>;
460
453
 
461
- export { type AuthOption, type AuthSession, type AuthSettings, type AuthUser, type BasicValue, type Body, type BodyMode, type Bucket, type BunEnv, type Context, type Cookie, type CorsSettings, type EventCallback, type ExtractPathParams, type InferParamType, type InlineReply, type KVStore, type LimitOptions, type LogLevel, type Logger, type Method, type Middleware, type Options, type ParamTypeMap, type ParamsToObject, type PathToParams, type Platform, type Provider, type Route, type RouteOptions, type RouterMethod, type SecurityOptions, type SecuritySettings, type SerializableValue, Server, type ServerConfig, TypedServerError as ServerError, type Settings, type Strategy, type Time, UploadPipeline, type UploadedFile, cookies, server as default, download, file, headers, json, redirect, router, send, status, type, upload };
454
+ export { type AuthOption, type AuthSession, type AuthSettings, type AuthUser, type BasicValue, type Body, type BodyMode, type Bucket, type BunEnv, type Context, type Cookie, type CorsSettings, type ExtractPathParams, type InferParamType, type InlineReply, type KVStore, type LimitOptions, type LogLevel, type Logger, type Method, type Middleware, type Options, type ParamTypeMap, type ParamsToObject, type PathToParams, type Platform, type Provider, type Route, type RouteOptions, type RouterMethod, type SecurityOptions, type SecuritySettings, type SerializableValue, Server, type ServerConfig, TypedServerError as ServerError, type Settings, type Strategy, type Time, UploadPipeline, type UploadedFile, cookies, server as default, download, file, headers, json, redirect, router, send, status, type, upload };
package/index.js CHANGED
@@ -54,7 +54,7 @@ ServerError_default.extend({
54
54
  message: "Invalid Authorization type '{strategy}', valid one is '{valid}'"
55
55
  },
56
56
  AUTH_INVALID_STATE: { status: 403, message: "Invalid OAuth state" },
57
- AUTH_NO_PROVIDER: "No provider passed to the option 'auth.provider'",
57
+ AUTH_NO_PROVIDER: "No provider passed to the option 'auth.providers'",
58
58
  AUTH_INVALID_PROVIDER: {
59
59
  status: 401,
60
60
  message: "Invalid provider '{provider}', valid ones are: '{valid}'"
@@ -1113,37 +1113,27 @@ function defaultCleanUser(fullUser) {
1113
1113
  const { password: _password, ...user } = fullUser;
1114
1114
  return user;
1115
1115
  }
1116
- var providersKeys = Object.keys(providers_default);
1117
- function getProviders(provider) {
1118
- if (typeof provider === "string") {
1119
- provider = provider.split("|");
1120
- }
1121
- const invalidProvider = provider.find((p) => !providersKeys.includes(p));
1122
- if (invalidProvider) {
1123
- throw new Error(
1124
- `Provider "${invalidProvider}" not found, available ones are "${providersKeys.join('", "')}"`
1125
- );
1126
- }
1127
- return provider;
1128
- }
1116
+ var available = Object.keys(providers_default);
1129
1117
  function parseAuthOptions(auth2, all) {
1130
1118
  if (!auth2) return null;
1131
1119
  if (typeof auth2 === "string") {
1132
- const [strategy2, providerRaw] = auth2.split(":");
1133
- const provider2 = providerRaw && providerRaw.split("|");
1134
- auth2 = { strategy: strategy2, provider: provider2 };
1135
- }
1136
- if (!auth2.strategy) {
1137
- throw new Error("Auth options needs a strategy");
1120
+ const [strategy2, provider] = auth2.split(":");
1121
+ auth2 = { strategy: strategy2, providers: provider ? [provider] : [] };
1138
1122
  }
1139
- if (!auth2.strategy.length) {
1123
+ if (!auth2.strategy?.length) {
1140
1124
  throw new Error("Auth options needs a strategy");
1141
1125
  }
1142
1126
  const strategy = auth2.strategy;
1143
- if (!auth2.provider?.length) {
1127
+ const list = Array.isArray(auth2.providers) ? auth2.providers : auth2.providers ? [auth2.providers] : [];
1128
+ if (!list.length) {
1144
1129
  throw new Error("Auth options needs a provider");
1145
1130
  }
1146
- const provider = getProviders(auth2.provider);
1131
+ const invalid = list.find((p) => !available.includes(p));
1132
+ if (invalid) {
1133
+ throw new Error(
1134
+ `Provider "${invalid}" not found, available ones are "${available.join('", "')}"`
1135
+ );
1136
+ }
1147
1137
  const redirect2 = auth2.redirect || defaultRedirect;
1148
1138
  const cleanUser = auth2.cleanUser || defaultCleanUser;
1149
1139
  if (!auth2.store && !all.store) {
@@ -1155,13 +1145,10 @@ function parseAuthOptions(auth2, all) {
1155
1145
  const store = auth2.store || all.store.prefix("user:");
1156
1146
  const session2 = auth2.session || all.store.prefix("auth:");
1157
1147
  return {
1158
- // Base main configuration
1159
1148
  strategy,
1160
- provider,
1161
- // Extra configuration
1149
+ providers: list,
1162
1150
  redirect: redirect2,
1163
1151
  cleanUser,
1164
- // Stores for the auth session and users
1165
1152
  store,
1166
1153
  session: session2
1167
1154
  };
@@ -1452,7 +1439,7 @@ function config(options = {}) {
1452
1439
  });
1453
1440
  const loc = (v) => typeof v === "string" ? v : "enabled";
1454
1441
  if (settings.auth) {
1455
- log.message("auth", `${settings.auth.provider.join(", ")} auth enabled`);
1442
+ log.message("auth", `${settings.auth.providers.join(", ")} auth enabled`);
1456
1443
  }
1457
1444
  if (settings.public) log.message("public", loc(options.public));
1458
1445
  if (settings.uploads) log.message("uploads", loc(options.uploads));
@@ -2062,10 +2049,10 @@ async function getUser(ctx) {
2062
2049
  valid: options.strategy
2063
2050
  });
2064
2051
  }
2065
- if (!options.provider.includes(auth2.provider)) {
2052
+ if (!options.providers.includes(auth2.provider)) {
2066
2053
  throw ServerError_default.AUTH_INVALID_PROVIDER({
2067
2054
  provider: auth2.provider,
2068
- valid: options.provider
2055
+ valid: options.providers
2069
2056
  });
2070
2057
  }
2071
2058
  const user = await ctx.options.auth.store.get(auth2.user);
@@ -2109,7 +2096,7 @@ function auth(app) {
2109
2096
  ctx.user = await getUser(ctx);
2110
2097
  });
2111
2098
  app.post("/auth/logout", logout);
2112
- const enabled = app.settings.auth.provider;
2099
+ const enabled = app.settings.auth.providers;
2113
2100
  for (const name of oauth2) {
2114
2101
  if (!enabled.includes(name)) continue;
2115
2102
  const key = name.toUpperCase();
@@ -2384,22 +2371,6 @@ function timer(ctx) {
2384
2371
  // src/context/node.ts
2385
2372
  import { TLSSocket } from "tls";
2386
2373
 
2387
- // src/context/createEvents.ts
2388
- function createEvents() {
2389
- const events = {};
2390
- events.on = (name, callback3) => {
2391
- events[name] = events[name] || [];
2392
- events[name].push(callback3);
2393
- };
2394
- events.trigger = (name, data) => {
2395
- if (!events[name]) return;
2396
- for (const cb of events[name]) {
2397
- cb(data);
2398
- }
2399
- };
2400
- return events;
2401
- }
2402
-
2403
2374
  // src/context/isValidMethod.ts
2404
2375
  var methods = [
2405
2376
  "get",
@@ -2443,7 +2414,6 @@ async function createNode(req, app) {
2443
2414
  }),
2444
2415
  getStream: () => toWeb(req)
2445
2416
  };
2446
- const events = createEvents();
2447
2417
  const ctx = {
2448
2418
  options: app.settings,
2449
2419
  platform: app.platform,
@@ -2454,7 +2424,6 @@ async function createNode(req, app) {
2454
2424
  cookies: cookies2,
2455
2425
  session: {},
2456
2426
  init,
2457
- events,
2458
2427
  app,
2459
2428
  ip: clientIp(headers2, {
2460
2429
  remoteAddress: req.socket.remoteAddress || "",
@@ -2485,7 +2454,6 @@ async function createWinter(req, app, server2) {
2485
2454
  getBuffer: async () => Buffer.from(await req.arrayBuffer()),
2486
2455
  getStream: () => req.body ?? void 0
2487
2456
  };
2488
- const events = createEvents();
2489
2457
  const ctx = {
2490
2458
  options: app.settings,
2491
2459
  platform: app.platform,
@@ -2496,7 +2464,6 @@ async function createWinter(req, app, server2) {
2496
2464
  cookies: cookies2,
2497
2465
  session: {},
2498
2466
  init,
2499
- events,
2500
2467
  app,
2501
2468
  ip: clientIp(headers2, {
2502
2469
  remoteAddress: server2?.requestIP?.(req)?.address || "",
@@ -2513,7 +2480,6 @@ var Winter = async (app, request, env2) => {
2513
2480
  Object.assign(globalThis.env, env2);
2514
2481
  const ctx = await createWinter(request, app, env2);
2515
2482
  const res = await handleRequest(app, ctx);
2516
- ctx.events.trigger("finish", { ...ctx, res, end: performance.now() });
2517
2483
  return res;
2518
2484
  };
2519
2485
  var Node = async (app) => {
@@ -2545,7 +2511,6 @@ var Netlify = async (app, request, context) => {
2545
2511
  }
2546
2512
  const ctx = await createWinter(request, app);
2547
2513
  const res = await handleRequest(app, ctx);
2548
- ctx.events.trigger("finish", { ...ctx, res, end: performance.now() });
2549
2514
  return res;
2550
2515
  };
2551
2516
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@server/next",
3
- "version": "0.32.0",
3
+ "version": "0.33.0",
4
4
  "description": "A fully-fledged web server with routing, file uploads, sessions, static files, schema validation, websockets, testing, etc.",
5
5
  "homepage": "https://server-js.com/",
6
6
  "repository": "github:franciscop/server-next",