@zap-socket/server 0.0.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.
@@ -0,0 +1,16 @@
1
+ import { z } from "zod";
2
+ export type EventInput = z.ZodTypeAny | undefined;
3
+ export type ZapEvent<T extends EventInput, R = any> = T extends z.ZodTypeAny ? {
4
+ input: T;
5
+ process: (input: z.infer<T>) => R;
6
+ } : {
7
+ input: z.ZodVoid;
8
+ process: () => R;
9
+ };
10
+ export type EventMap = Record<string, ZapEvent<any, any>>;
11
+ export declare const createZapEvent: <T extends EventInput, R>(eventObj: T extends z.ZodTypeAny ? {
12
+ input: T;
13
+ process: (input: z.infer<T>) => R;
14
+ } : {
15
+ process: () => R;
16
+ }) => ZapEvent<T, R>;
package/dist/events.js ADDED
@@ -0,0 +1,10 @@
1
+ import { z } from "zod";
2
+ export const createZapEvent = (eventObj) => {
3
+ if ("input" in eventObj) {
4
+ return eventObj;
5
+ }
6
+ return {
7
+ input: z.void(),
8
+ process: eventObj.process
9
+ };
10
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./events";
2
+ export * from "./server";
3
+ export * from "./utils";
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./events";
2
+ export * from "./server";
3
+ export * from "./utils";
@@ -0,0 +1,17 @@
1
+ import type { EventMap } from "./events";
2
+ interface ZapServerConstructorT {
3
+ port: number;
4
+ }
5
+ export declare class ZapServer {
6
+ private wss;
7
+ private wsToId;
8
+ private idToWs;
9
+ private events;
10
+ constructor({ port }: ZapServerConstructorT);
11
+ private removeClient;
12
+ attachEvents<T extends EventMap>(events: T): void;
13
+ }
14
+ export declare const createZapServer: ({ port }: {
15
+ port: number;
16
+ }) => ZapServer;
17
+ export {};
package/dist/server.js ADDED
@@ -0,0 +1,53 @@
1
+ import { WebSocketServer } from "ws";
2
+ import { generateId } from "./utils";
3
+ export class ZapServer {
4
+ wss;
5
+ wsToId;
6
+ idToWs;
7
+ events = {};
8
+ constructor({ port }) {
9
+ this.wss = new WebSocketServer({ port });
10
+ this.wsToId = new Map();
11
+ this.idToWs = new Map();
12
+ this.wss.on("connection", (ws) => {
13
+ const clientId = generateId();
14
+ console.log(`client ${clientId} connected`);
15
+ this.wsToId.set(ws, clientId);
16
+ this.idToWs.set(clientId, ws);
17
+ ws.on("message", (message) => {
18
+ if (!this.wsToId.get(ws)) {
19
+ const id = generateId();
20
+ this.wsToId.set(ws, id);
21
+ this.idToWs.set(id, ws);
22
+ ws.send("ID " + id);
23
+ }
24
+ for (const [event, { process }] of Object.entries(this.events)) {
25
+ const parsedMessage = JSON.parse(message.toString());
26
+ if (parsedMessage["type"] === event) {
27
+ const { input } = parsedMessage;
28
+ process(input);
29
+ }
30
+ }
31
+ });
32
+ ws.on("close", () => {
33
+ this.removeClient(ws);
34
+ });
35
+ ws.on("error", (err) => {
36
+ console.error(`WebSocket error for ${clientId}:`, err);
37
+ });
38
+ });
39
+ }
40
+ removeClient(ws) {
41
+ const clientId = this.wsToId.get(ws);
42
+ if (clientId) {
43
+ this.wsToId.delete(ws);
44
+ this.idToWs.delete(clientId);
45
+ }
46
+ }
47
+ attachEvents(events) {
48
+ this.events = events;
49
+ }
50
+ }
51
+ export const createZapServer = ({ port }) => {
52
+ return new ZapServer({ port });
53
+ };
@@ -0,0 +1 @@
1
+ export declare const generateId: (length?: number) => string;
package/dist/utils.js ADDED
@@ -0,0 +1,10 @@
1
+ export const generateId = (length = 6) => {
2
+ const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
3
+ let id = "";
4
+ const randomValues = new Uint8Array(length);
5
+ crypto.getRandomValues(randomValues);
6
+ randomValues.forEach((num) => {
7
+ id += chars[num % chars.length];
8
+ });
9
+ return id;
10
+ };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@zap-socket/server",
3
+ "version": "0.0.1",
4
+ "description": "A fully typesafe tRPC-inspired WebSocket library with Zod validation, req-res model, and native subscriptions.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "repository": "https://github.com/atharvparlikar/zap-socket",
8
+ "author": "Atharv Parlikar",
9
+ "license": "MIT",
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "scripts": {
14
+ "dev": "tsup src/index.ts --watch",
15
+ "build": "tsc",
16
+ "clean": "rm -rf dist"
17
+ },
18
+ "devDependencies": {
19
+ "@types/ws": "^8.18.0",
20
+ "eslint": "^9.22.0",
21
+ "tsup": "^8.4.0",
22
+ "typescript": "^5.8.2",
23
+ "vitest": "^3.0.9"
24
+ },
25
+ "dependencies": {
26
+ "zod": "^3.24.2"
27
+ }
28
+ }