@server/next 0.23.0 → 0.23.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@server/next",
3
- "version": "0.23.0",
3
+ "version": "0.23.1",
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",
@@ -20,7 +20,7 @@
20
20
  "scripts": {
21
21
  "start": "bun test --watch",
22
22
  "lint": "npx @biomejs/biome lint ./src --skip=lint/style/noParameterAssign",
23
- "test": "bun test",
23
+ "test": "bun test && check-dts ./src/**",
24
24
  "test:jest": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
25
25
  },
26
26
  "keywords": [
@@ -29,15 +29,15 @@
29
29
  "server.js"
30
30
  ],
31
31
  "type": "module",
32
- "types": "index.d.ts",
33
32
  "main": "src/index.js",
33
+ "types": "src/index.d.ts",
34
34
  "files": [
35
35
  "src/",
36
- "index.d.ts",
37
36
  "jsx-dev-runtime.js"
38
37
  ],
39
38
  "devDependencies": {
40
39
  "argon2": "^0.40.3",
40
+ "check-dts": "^0.8.2",
41
41
  "jest": "^29.7.0",
42
42
  "polystore": "^0.14.1"
43
43
  },
@@ -45,8 +45,7 @@ const randomId = (size = 16) => {
45
45
  return id;
46
46
  };
47
47
 
48
- export default function createId(source) {
49
- const size = 16;
48
+ export default function createId(source, size = 16) {
50
49
  if (source) return hash(source, size);
51
50
  return randomId(size);
52
51
  }
@@ -16,6 +16,7 @@ const altAttrs = {
16
16
  // "" and 0 are valid children, false and null and undefined are not
17
17
  const isValidChild = (child) => child || child === "" || child === 0;
18
18
 
19
+ const escapeCSS = (value) => String(value).replace(/[<>&"'`]/g, "\\$&");
19
20
  const minifyCss = (str) =>
20
21
  str
21
22
  .replace(/\s+/g, " ")
@@ -42,7 +43,7 @@ export const jsx = (tag, { children, ...props }) => {
42
43
  children = () => src;
43
44
  }
44
45
  if (tag === "style" && children && typeof children === "string") {
45
- const src = minifyCss(children);
46
+ const src = minifyCss(escapeCSS(children));
46
47
  children = () => src;
47
48
  }
48
49
  if (props.dangerouslySetInnerHTML)
@@ -1,5 +1,3 @@
1
- type Bucket = {};
2
-
3
1
  type Method =
4
2
  | "socket"
5
3
  | "get"
@@ -10,12 +8,40 @@ type Method =
10
8
  | "delete"
11
9
  | "options";
12
10
 
11
+ type Store = {
12
+ get: (key: string) => Promise<any>;
13
+ set: (
14
+ key: string,
15
+ value: any,
16
+ { expires }?: { expires?: string | number },
17
+ ) => Promise<any>;
18
+ };
19
+
20
+ type Bucket = any;
21
+
22
+ type Auth = {
23
+ type: "cookie" | "token";
24
+ provider: "github" | "email";
25
+ };
26
+ type AuthString = `${Auth["type"]}:${Auth["provider"]}`;
27
+
28
+ type Domain = `https://${string}/`;
29
+ type Origin = boolean | "*" | Domain | Domain[];
30
+ type Cors = {
31
+ origin: Origin;
32
+ methods: string;
33
+ headers: string;
34
+ credentials?: boolean;
35
+ };
36
+
13
37
  type ServerOptions = {
14
38
  port?: number;
15
39
  views?: string | Bucket;
16
40
  public?: string | Bucket;
17
41
  uploads?: string | Bucket;
18
- auth?: string | { type: string; provider: string };
42
+ cors?: boolean | Origin | Cors;
43
+ auth?: AuthString | Auth;
44
+ store?: Store;
19
45
  };
20
46
 
21
47
  type ExtractPathParams<Path extends string> =
@@ -44,7 +70,7 @@ type Context<Path extends string = string> = {
44
70
  body?: any;
45
71
  url: URL & {
46
72
  params: Simplify<PathToParams<Path>>; // Simplify here
47
- query: {};
73
+ query: { [key: string]: string };
48
74
  };
49
75
  options: ServerOptions;
50
76
  };
@@ -55,18 +81,11 @@ type ContentType = "application/json" | "text/plain" | (string & {});
55
81
 
56
82
  // (src: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers)
57
83
  type Headers = {
58
- "Cache-Control"?: string;
59
- "Content-Type"?: ContentType;
60
- Server?: string;
61
- "Set-Cookie"?: string;
62
- "Content-Length"?: string;
63
- Location?: string;
64
-
65
84
  "cache-control"?: string;
66
85
  "content-type"?: ContentType;
67
- server?: string;
68
86
  "set-cookie"?: string;
69
87
  "content-length"?: string;
88
+ server?: string;
70
89
  location?: string;
71
90
 
72
91
  [key: string]: string | undefined;
@@ -94,6 +113,21 @@ declare interface Router {
94
113
  }
95
114
 
96
115
  declare interface Server {
116
+ /**
117
+ * Launch the server with the optional configuration:
118
+ *
119
+ * ```js
120
+ * export default server({
121
+ * port: 3000,
122
+ * public: './public',
123
+ * store: kv(new Map()),
124
+ * })
125
+ * ```
126
+ *
127
+ * **[→ Getting Started](https://react-test.dev/documentation#attr)**
128
+ *
129
+ * **[→ Options Docs](https://react-test.dev/documentation#attr)**
130
+ */
97
131
  (options?: ServerOptions): this;
98
132
 
99
133
  socket(path: string, ...middle: Middleware[]): this;
package/src/index.js CHANGED
@@ -21,7 +21,6 @@ export { default as ServerError } from "./ServerError.js";
21
21
  // Allow to create a sub-router
22
22
  export { default as router } from "./router.js";
23
23
 
24
- // #region server()
25
24
  export default function server(options = {}) {
26
25
  // Make it so that the exported one is a prototype of function()
27
26
  if (!(this instanceof server)) {
@@ -0,0 +1,28 @@
1
+ import server, { headers, router } from ".";
2
+
3
+ const users = router
4
+ .post("/users", (ctx) => {
5
+ console.log(ctx.url.pathname);
6
+ console.log(ctx.url.query);
7
+ console.log(ctx.url.params); // No .id
8
+ })
9
+ .get("/users/:id?", (ctx) => {
10
+ console.log(ctx.url.params.id);
11
+ })
12
+ .del("/users/:id", (ctx) => {
13
+ console.log(ctx.url.params.id);
14
+ });
15
+
16
+ server()
17
+ .get("/", (ctx) => {
18
+ console.log(ctx.method);
19
+ console.log(ctx.url);
20
+ console.log(ctx.headers);
21
+ console.log(ctx.cookies);
22
+ console.log(ctx.body);
23
+ return headers();
24
+ })
25
+ .router(users)
26
+ .post("/", () => {
27
+ return 201;
28
+ });
@@ -1,6 +1,4 @@
1
- // import { Readable } from "node:stream";
2
-
3
- import { cors, createCookies, createId } from "./helpers/index.js";
1
+ import { cors, createId } from "./helpers/index.js";
4
2
  import { json } from "./reply.js";
5
3
  import ServerError from "./ServerError.js";
6
4