@server/next 0.27.8 → 0.27.9

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 +11 -8
  2. package/index.js +46 -34
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  type Method = "get" | "post" | "put" | "patch" | "delete" | "head" | "options" | "socket";
2
- type ServerConfig = {
3
- User?: Record<string, string | number | boolean | Date | null | undefined>;
2
+ type ServerConfig<Session = {}, User = {}> = {
3
+ Session?: Session;
4
+ User?: User;
4
5
  };
5
6
  declare namespace JSX {
6
7
  interface Element {
@@ -60,7 +61,7 @@ type AuthSession = {
60
61
  strategy: Strategy;
61
62
  user: string;
62
63
  };
63
- type AuthUser<T = object> = T & {
64
+ type AuthUser<T = Record<string, any>> = T & {
64
65
  id: string | number;
65
66
  provider: Provider;
66
67
  strategy: Strategy;
@@ -156,10 +157,12 @@ type Context<Params extends Record<string, string> = Record<string, string>, O e
156
157
  time?: Time;
157
158
  socket?: WebSocket;
158
159
  sockets?: WebSocket[];
159
- session?: Record<string, BasicValue>;
160
+ session: O extends {
161
+ Session?: infer S;
162
+ } ? S extends Record<"Session", infer Inner> ? Inner : Record<string, any> : Record<string, any>;
160
163
  user?: O extends {
161
- User: infer U;
162
- } ? U & AuthUser : AuthUser;
164
+ User?: infer U;
165
+ } ? U extends Record<"User", infer Inner> ? Inner : AuthUser : AuthUser;
163
166
  init: number;
164
167
  events: Events;
165
168
  req?: Request;
@@ -271,7 +274,7 @@ declare const json: (...args: Params<"json">) => Response;
271
274
  declare const file: (...args: Params<"file">) => Promise<Response>;
272
275
  declare const redirect: (...args: Params<"redirect">) => Response;
273
276
 
274
- declare class Server<O extends ServerConfig = object> extends Router<O> {
277
+ declare class Server<O extends ServerConfig = {}> extends Router<O> {
275
278
  settings: Settings;
276
279
  platform: Platform;
277
280
  sockets: any[];
@@ -396,6 +399,6 @@ declare class Server<O extends ServerConfig = object> extends Router<O> {
396
399
  }) => Promise<Response>;
397
400
  };
398
401
  }
399
- declare function server<Options>(options?: {}): Server<Options>;
402
+ declare function server<Session extends Record<string, any> = {}, User extends Record<string, any> = {}>(options?: Options): Server<ServerConfig<Session, User>>;
400
403
 
401
404
  export { type AuthOption, type AuthSession, type AuthSettings, type AuthUser, type BasicValue, type Body, type Bucket, type BunEnv, type Context, type Cookie, type CorsSettings, type EventCallback, type ExtractPathParams, type InferParamType, type InlineReply, type KVStore, type Method, type Middleware, type Options, type ParamTypeMap, type ParamsToObject, type PathToParams, type Platform, type Provider, type RouteOptions, type RouterMethod, type SerializableValue, Server, type ServerConfig, TypedServerError as ServerError, type Settings, type Strategy, type Time, cookies, server as default, download, file, headers, json, redirect, router, send, status, type };
package/index.js CHANGED
@@ -171,6 +171,11 @@ var email_default = {
171
171
  password: emailUpdatePassword
172
172
  };
173
173
 
174
+ // src/helpers/isReadableStream.ts
175
+ function isReadableStream(obj) {
176
+ return obj !== null && typeof obj === "object" && typeof obj.pipe === "function" && typeof obj.read === "function" && typeof obj.on === "function";
177
+ }
178
+
174
179
  // src/reply.ts
175
180
  var EXPIRED = (/* @__PURE__ */ new Date(0)).toUTCString();
176
181
  var Reply = class {
@@ -217,7 +222,6 @@ var Reply = class {
217
222
  Object.values(value).map((val) => this.cookies(key, val));
218
223
  return this;
219
224
  }
220
- console.log(key, value);
221
225
  if (value === null) return this.cookies(key, { expires: EXPIRED });
222
226
  if (typeof value !== "object") return this.cookies(key, { value });
223
227
  return this.headers("set-cookie", createCookies(key, value));
@@ -262,7 +266,12 @@ var Reply = class {
262
266
  if (name === "PassThrough" || name === "Readable") {
263
267
  return new Response(toWeb(body), { status: status2, headers: headers2 });
264
268
  }
265
- headers2.set("content-type", "application/json");
269
+ if (isReadableStream(body)) {
270
+ return new Response(toWeb(body), { status: status2, headers: headers2 });
271
+ }
272
+ if (!headers2.get("content-type")) {
273
+ headers2.set("content-type", "application/json");
274
+ }
266
275
  return new Response(JSON.stringify(body), { status: status2, headers: headers2 });
267
276
  }
268
277
  };
@@ -633,6 +642,9 @@ function config(options = {}) {
633
642
  debugInfo(options, "store", (store) => store?.name || "working", "\u{1F4E6}");
634
643
  settings.cookies = options.cookies ?? null;
635
644
  debugInfo(options, "cookies", (cookies2) => cookies2?.name || "working", "\u{1F36A}");
645
+ if (options.session) {
646
+ settings.session = "store" in options.session ? options.session : { store: options.session };
647
+ }
636
648
  if (options.store && !options.session) {
637
649
  settings.session = { store: options.store.prefix("session:") };
638
650
  }
@@ -832,14 +844,17 @@ async function parseResponse(out, ctx) {
832
844
  throw ServerError_default.NO_STORE();
833
845
  }
834
846
  if (!ctx.cookies.session) {
835
- ctx.res.cookies.session = createId();
847
+ out.headers.append(
848
+ "set-cookie",
849
+ createCookies("session", { value: createId() })
850
+ );
836
851
  }
837
852
  const id = ctx.cookies.session;
838
853
  ctx.options.session.store.set(id, ctx.session);
839
854
  }
840
855
  if (ctx.options.cookies) {
841
- if (Object.keys(ctx.res.cookies).length) {
842
- for (const cookie of ctx.res.cookies) {
856
+ if (Object.keys(ctx.res?.cookies || {}).length) {
857
+ for (const cookie of Object.values(ctx.res.cookies)) {
843
858
  ctx.res.headers.append("set-cookie", cookie);
844
859
  }
845
860
  }
@@ -1293,6 +1308,9 @@ async function verify(password, hash3) {
1293
1308
  // src/auth/findSessionId.ts
1294
1309
  var validateToken = (authorization) => {
1295
1310
  const [type2, id] = authorization.trim().split(" ");
1311
+ if (!type2 || !id) {
1312
+ throw ServerError_default.AUTH_INVALID_HEADER({ type: type2 });
1313
+ }
1296
1314
  if (type2.toLowerCase() !== "bearer") {
1297
1315
  throw ServerError_default.AUTH_INVALID_HEADER({ type: type2 });
1298
1316
  }
@@ -1565,24 +1583,7 @@ var openapi_default = async (ctx) => {
1565
1583
  </html> `;
1566
1584
  };
1567
1585
 
1568
- // src/middle/timer.ts
1569
- var createTime = () => {
1570
- const times2 = [["init", performance.now()]];
1571
- const time = (name) => times2.push([name, performance.now()]);
1572
- time.times = times2;
1573
- time.headers = () => {
1574
- const r2 = (t) => Math.round(t);
1575
- const times3 = time.times;
1576
- const timing = times3.slice(1).map(([name, time2], i) => `${name};dur=${r2(time2 - times3[i][1])}`).join(", ");
1577
- return timing;
1578
- };
1579
- return time;
1580
- };
1581
- function timer(ctx) {
1582
- ctx.time = createTime();
1583
- }
1584
-
1585
- // src/auth/NoSession.ts
1586
+ // src/middle/NoSession.ts
1586
1587
  var NoSession = class {
1587
1588
  };
1588
1589
  function createNoSession() {
@@ -1603,7 +1604,7 @@ function createNoSession() {
1603
1604
  });
1604
1605
  }
1605
1606
 
1606
- // src/auth/session.ts
1607
+ // src/middle/session.ts
1607
1608
  async function session(ctx) {
1608
1609
  const store = ctx.options.session?.store;
1609
1610
  if (!store) {
@@ -1611,12 +1612,27 @@ async function session(ctx) {
1611
1612
  return;
1612
1613
  }
1613
1614
  if (ctx.cookies.session) {
1614
- const session2 = await store.get(ctx.cookies.session);
1615
- ctx.session = session2;
1616
- return;
1615
+ ctx.session = await store.get(ctx.cookies.session) || {};
1617
1616
  }
1618
1617
  }
1619
1618
 
1619
+ // src/middle/timer.ts
1620
+ var createTime = () => {
1621
+ const times2 = [["init", performance.now()]];
1622
+ const time = (name) => times2.push([name, performance.now()]);
1623
+ time.times = times2;
1624
+ time.headers = () => {
1625
+ const r2 = (t) => Math.round(t);
1626
+ const times3 = time.times;
1627
+ const timing = times3.slice(1).map(([name, time2], i) => `${name};dur=${r2(time2 - times3[i][1])}`).join(", ");
1628
+ return timing;
1629
+ };
1630
+ return time;
1631
+ };
1632
+ function timer(ctx) {
1633
+ ctx.time = createTime();
1634
+ }
1635
+
1620
1636
  // src/context/node.ts
1621
1637
  import { TLSSocket } from "tls";
1622
1638
 
@@ -1686,6 +1702,7 @@ async function createNode(req, app) {
1686
1702
  body,
1687
1703
  headers: headers2,
1688
1704
  cookies: cookies2,
1705
+ session: {},
1689
1706
  init,
1690
1707
  events,
1691
1708
  app
@@ -1719,6 +1736,7 @@ async function createWinter(req, app) {
1719
1736
  body,
1720
1737
  headers: headers2,
1721
1738
  cookies: cookies2,
1739
+ session: {},
1722
1740
  init,
1723
1741
  events,
1724
1742
  app
@@ -1903,7 +1921,6 @@ var Server = class extends Router {
1903
1921
  platform;
1904
1922
  sockets;
1905
1923
  websocket;
1906
- // Needed to be explicit for Bun/WinterCG
1907
1924
  port;
1908
1925
  constructor(options = {}) {
1909
1926
  super();
@@ -1927,9 +1944,6 @@ var Server = class extends Router {
1927
1944
  this.get(this.settings.openapi.path || "/docs", openapi_default);
1928
1945
  }
1929
1946
  }
1930
- // We need to return a function; some environment expect the default export
1931
- // to be a function that is called with the request, but we also want to
1932
- // allow chaining, so we return a function that "extends" the instance
1933
1947
  self() {
1934
1948
  const cb = this.callback.bind(this);
1935
1949
  const proto = Object.getPrototypeOf(this);
@@ -1943,7 +1957,6 @@ var Server = class extends Router {
1943
1957
  }
1944
1958
  return cb;
1945
1959
  }
1946
- // The different handlers for different platforms/runtimes
1947
1960
  node() {
1948
1961
  return Node(this);
1949
1962
  }
@@ -1953,12 +1966,11 @@ var Server = class extends Router {
1953
1966
  callback(request, context) {
1954
1967
  return Netlify(this, request, context);
1955
1968
  }
1956
- // Helper purely for testing
1957
1969
  test() {
1958
1970
  return ServerTest(this);
1959
1971
  }
1960
1972
  };
1961
- function server(options = {}) {
1973
+ function server(options) {
1962
1974
  return new Server(options).self();
1963
1975
  }
1964
1976
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@server/next",
3
- "version": "0.27.8",
3
+ "version": "0.27.9",
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": "https://github.com/franciscop/server-next.git",