@zap-socket/server 0.0.6 → 0.0.7
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/server.d.ts +5 -0
- package/dist/server.js +60 -58
- package/package.json +1 -1
    
        package/dist/server.d.ts
    CHANGED
    
    | @@ -6,6 +6,11 @@ interface ZapServerConstructorT { | |
| 6 6 | 
             
            }
         | 
| 7 7 | 
             
            export declare class ZapServer<T extends EventMap> {
         | 
| 8 8 | 
             
                wss: WebSocketServer;
         | 
| 9 | 
            +
                onconnect: (handler: (ctx: {
         | 
| 10 | 
            +
                    id: string;
         | 
| 11 | 
            +
                    ws: WebSocket;
         | 
| 12 | 
            +
                }) => void) => void;
         | 
| 13 | 
            +
                private onconnectHandler;
         | 
| 9 14 | 
             
                private wsToId;
         | 
| 10 15 | 
             
                private idToWs;
         | 
| 11 16 | 
             
                private _events;
         | 
    
        package/dist/server.js
    CHANGED
    
    | @@ -5,6 +5,8 @@ const isClientEvent = (event) => { | |
| 5 5 | 
             
            };
         | 
| 6 6 | 
             
            export class ZapServer {
         | 
| 7 7 | 
             
                wss;
         | 
| 8 | 
            +
                onconnect;
         | 
| 9 | 
            +
                onconnectHandler;
         | 
| 8 10 | 
             
                wsToId;
         | 
| 9 11 | 
             
                idToWs;
         | 
| 10 12 | 
             
                _events = {};
         | 
| @@ -13,78 +15,78 @@ export class ZapServer { | |
| 13 15 | 
             
                    this.wsToId = new Map();
         | 
| 14 16 | 
             
                    this.idToWs = new Map();
         | 
| 15 17 | 
             
                    this._events = events;
         | 
| 18 | 
            +
                    this.onconnectHandler = () => { };
         | 
| 19 | 
            +
                    this.onconnect = (handler) => {
         | 
| 20 | 
            +
                        this.onconnectHandler = handler;
         | 
| 21 | 
            +
                    };
         | 
| 16 22 | 
             
                    this.wss.on("listening", () => {
         | 
| 17 23 | 
             
                        if (callback)
         | 
| 18 24 | 
             
                            callback();
         | 
| 19 25 | 
             
                    });
         | 
| 20 26 | 
             
                    this.wss.on("connection", (ws, req) => {
         | 
| 21 27 | 
             
                        ws.on("message", (message) => {
         | 
| 22 | 
            -
                            if (!this.wsToId.get(ws)) {
         | 
| 28 | 
            +
                            if (!this.wsToId.get(ws) && message.toString() === "OPEN") {
         | 
| 23 29 | 
             
                                const id = generateId();
         | 
| 24 30 | 
             
                                this.wsToId.set(ws, id);
         | 
| 25 31 | 
             
                                this.idToWs.set(id, ws);
         | 
| 26 32 | 
             
                                ws.send("ID " + id);
         | 
| 27 | 
            -
                                 | 
| 33 | 
            +
                                this.onconnectHandler({
         | 
| 34 | 
            +
                                    id,
         | 
| 35 | 
            +
                                    ws
         | 
| 36 | 
            +
                                });
         | 
| 28 37 | 
             
                                return;
         | 
| 29 38 | 
             
                            }
         | 
| 30 39 | 
             
                            const clientId = this.wsToId.get(ws);
         | 
| 31 | 
            -
                             | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 41 | 
            -
             | 
| 42 | 
            -
             | 
| 43 | 
            -
             | 
| 44 | 
            -
             | 
| 45 | 
            -
             | 
| 46 | 
            -
             | 
| 47 | 
            -
             | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 50 | 
            -
             | 
| 51 | 
            -
             | 
| 52 | 
            -
             | 
| 53 | 
            -
             | 
| 54 | 
            -
             | 
| 55 | 
            -
             | 
| 56 | 
            -
             | 
| 57 | 
            -
             | 
| 58 | 
            -
             | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
| 66 | 
            -
                                            return;
         | 
| 67 | 
            -
                                        ws.send(serialized);
         | 
| 68 | 
            -
                                    }
         | 
| 69 | 
            -
                                    else if (streamId) {
         | 
| 70 | 
            -
                                        const consumeStream = async () => {
         | 
| 71 | 
            -
                                            const result = process(data, { server: this, id: this.wsToId.get(ws), buffer: ctx });
         | 
| 72 | 
            -
                                            for await (const x of result) {
         | 
| 73 | 
            -
                                                const packet = {
         | 
| 74 | 
            -
                                                    streamId,
         | 
| 75 | 
            -
                                                    fragment: x
         | 
| 76 | 
            -
                                                };
         | 
| 77 | 
            -
                                                this.sendMessageRaw(clientId, packet);
         | 
| 78 | 
            -
                                            }
         | 
| 79 | 
            -
                                            this.sendMessageRaw(clientId, {
         | 
| 80 | 
            -
                                                streamId,
         | 
| 81 | 
            -
                                                done: true
         | 
| 82 | 
            -
                                            });
         | 
| 83 | 
            -
                                        };
         | 
| 84 | 
            -
                                        consumeStream();
         | 
| 85 | 
            -
                                    }
         | 
| 86 | 
            -
                                    return; // finally return to avoid looping through rest of events unneccessarily
         | 
| 40 | 
            +
                            const parsedMessage = deserialize(message.toString());
         | 
| 41 | 
            +
                            if (!parsedMessage)
         | 
| 42 | 
            +
                                return;
         | 
| 43 | 
            +
                            const { event, stream, data, requestId, streamId } = parsedMessage;
         | 
| 44 | 
            +
                            const key = event || stream;
         | 
| 45 | 
            +
                            const eventObj = this._events[key];
         | 
| 46 | 
            +
                            if (!eventObj || !isClientEvent(eventObj))
         | 
| 47 | 
            +
                                return;
         | 
| 48 | 
            +
                            const { process, middleware } = eventObj;
         | 
| 49 | 
            +
                            // Setup middleware context
         | 
| 50 | 
            +
                            const ctx = {};
         | 
| 51 | 
            +
                            if (middleware) {
         | 
| 52 | 
            +
                                for (const m of middleware) {
         | 
| 53 | 
            +
                                    const metadata = {
         | 
| 54 | 
            +
                                        id: clientId,
         | 
| 55 | 
            +
                                        ip: req.socket.remoteAddress,
         | 
| 56 | 
            +
                                        timestamp: Date.now(),
         | 
| 57 | 
            +
                                        size: message.toString().length,
         | 
| 58 | 
            +
                                    };
         | 
| 59 | 
            +
                                    const msg = {
         | 
| 60 | 
            +
                                        event: key,
         | 
| 61 | 
            +
                                        data: parsedMessage,
         | 
| 62 | 
            +
                                        metadata,
         | 
| 63 | 
            +
                                    };
         | 
| 64 | 
            +
                                    if (!m(ctx, msg))
         | 
| 65 | 
            +
                                        return;
         | 
| 66 | 
            +
                                }
         | 
| 67 | 
            +
                            }
         | 
| 68 | 
            +
                            // All middleware passed
         | 
| 69 | 
            +
                            const context = { server: this, id: this.wsToId.get(ws), buffer: ctx };
         | 
| 70 | 
            +
                            if (requestId) { // req-res premitive
         | 
| 71 | 
            +
                                const result = process(data, context);
         | 
| 72 | 
            +
                                if (result === undefined) { // just ACK the request process returns nothing
         | 
| 73 | 
            +
                                    ws.send("ACK " + requestId);
         | 
| 74 | 
            +
                                    return;
         | 
| 87 75 | 
             
                                }
         | 
| 76 | 
            +
                                const serialized = serialize({ requestId, event: key, data: result });
         | 
| 77 | 
            +
                                if (!serialized)
         | 
| 78 | 
            +
                                    return;
         | 
| 79 | 
            +
                                ws.send(serialized);
         | 
| 80 | 
            +
                            }
         | 
| 81 | 
            +
                            else if (streamId) { // stream premitive
         | 
| 82 | 
            +
                                const consumeStream = async () => {
         | 
| 83 | 
            +
                                    const result = process(data, context);
         | 
| 84 | 
            +
                                    for await (const fragment of result) {
         | 
| 85 | 
            +
                                        this.sendMessageRaw(clientId, { streamId, fragment });
         | 
| 86 | 
            +
                                    }
         | 
| 87 | 
            +
                                    this.sendMessageRaw(clientId, { streamId, done: true });
         | 
| 88 | 
            +
                                };
         | 
| 89 | 
            +
                                consumeStream();
         | 
| 88 90 | 
             
                            }
         | 
| 89 91 | 
             
                        });
         | 
| 90 92 | 
             
                        ws.on("close", () => {
         | 
    
        package/package.json
    CHANGED