yedra 0.10.2 → 0.10.4

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/lib.d.ts CHANGED
@@ -23,3 +23,4 @@ export { string } from './validation/string.js';
23
23
  export { _undefined as undefined } from './validation/undefined.js';
24
24
  export { union } from './validation/union.js';
25
25
  export { unknown } from './validation/unknown.js';
26
+ export { uuid } from './validation/uuid.js';
package/dist/lib.js CHANGED
@@ -28,3 +28,4 @@ export { string } from './validation/string.js';
28
28
  export { _undefined as undefined } from './validation/undefined.js';
29
29
  export { union } from './validation/union.js';
30
30
  export { unknown } from './validation/unknown.js';
31
+ export { uuid } from './validation/uuid.js';
@@ -32,6 +32,7 @@ export declare class App {
32
32
  }): object;
33
33
  listen(port: number): void;
34
34
  private static errorResponse;
35
+ private static handleWebSocketErrors;
35
36
  private matchEndpoint;
36
37
  }
37
38
  export declare const app: (routes: string) => Promise<App>;
@@ -67,14 +67,14 @@ export class App {
67
67
  return response;
68
68
  },
69
69
  websocket: {
70
- async open(ws) {
71
- ws.data.handler.open(ws);
70
+ open(ws) {
71
+ App.handleWebSocketErrors(ws, () => ws.data.handler.open(ws));
72
72
  },
73
- async message(ws, message) {
74
- ws.data.handler.message(Buffer.from(message));
73
+ message(ws, message) {
74
+ App.handleWebSocketErrors(ws, () => ws.data.handler.message(Buffer.from(message)));
75
75
  },
76
- async close(ws, code, reason) {
77
- ws.data.handler.close(code, reason);
76
+ close(ws, code, reason) {
77
+ App.handleWebSocketErrors(ws, () => ws.data.handler.close(code, reason));
78
78
  },
79
79
  },
80
80
  });
@@ -88,6 +88,19 @@ export class App {
88
88
  status,
89
89
  });
90
90
  }
91
+ static async handleWebSocketErrors(ws, operation) {
92
+ try {
93
+ await operation();
94
+ }
95
+ catch (error) {
96
+ if (error instanceof HttpError) {
97
+ ws.close(1000, error.message);
98
+ }
99
+ else {
100
+ ws.close(1011, 'Internal Server Error');
101
+ }
102
+ }
103
+ }
91
104
  matchEndpoint(url, method) {
92
105
  let invalidMethod = false;
93
106
  let result = undefined;
@@ -0,0 +1,11 @@
1
+ import { ModifiableSchema } from './modifiable.js';
2
+ declare class UuidSchema extends ModifiableSchema<string> {
3
+ parse(obj: unknown): string;
4
+ documentation(): object;
5
+ }
6
+ /**
7
+ * A schema matching a universally unique identifier UUID
8
+ * of version 4.
9
+ */
10
+ export declare const uuid: () => UuidSchema;
11
+ export {};
@@ -0,0 +1,23 @@
1
+ import { validate, version } from 'uuid';
2
+ import { Issue, ValidationError } from './error.js';
3
+ import { ModifiableSchema } from './modifiable.js';
4
+ class UuidSchema extends ModifiableSchema {
5
+ parse(obj) {
6
+ if (typeof obj !== 'string' || !validate(obj) || version(obj) !== 4) {
7
+ throw new ValidationError([
8
+ new Issue('invalidType', [], 'uuid', typeof obj),
9
+ ]);
10
+ }
11
+ return obj;
12
+ }
13
+ documentation() {
14
+ return {
15
+ type: 'string',
16
+ };
17
+ }
18
+ }
19
+ /**
20
+ * A schema matching a universally unique identifier UUID
21
+ * of version 4.
22
+ */
23
+ export const uuid = () => new UuidSchema();
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "yedra",
3
- "version": "0.10.2",
3
+ "version": "0.10.4",
4
4
  "repository": "github:0codekit/yedra",
5
5
  "main": "dist/index.js",
6
6
  "devDependencies": {
7
7
  "@biomejs/biome": "^1.8.3",
8
8
  "@types/bun": "^1.1.6",
9
- "@types/node": "^22.0.0",
9
+ "@types/node": "^22.0.2",
10
10
  "typescript": "^5.5.4"
11
11
  },
12
12
  "bugs": "https://github.com/0codekit/yedra/issues",
@@ -21,6 +21,8 @@
21
21
  "types": "dist/index.d.ts",
22
22
  "type": "module",
23
23
  "dependencies": {
24
- "glob": "^11.0.0"
24
+ "@types/uuid": "^10.0.0",
25
+ "glob": "^11.0.0",
26
+ "uuid": "^10.0.0"
25
27
  }
26
28
  }