@push-rpc/next
Advanced tools
Comparing version 2.0.0-beta.1 to 2.0.0-beta.2
/// <reference types="node" /> | ||
import http from "http"; | ||
export declare class ConnectionsServer { | ||
constructor(server: http.Server, options: ConnectionsServerOptions, connectionClosed: (clientId: string) => void); | ||
constructor(server: http.Server, options: ConnectionsServerOptions, connectionClosed: (clientId: string) => void, closeSocketsWithDifferentPath: boolean); | ||
publish(clientId: string, itemName: string, parameters: unknown[], data: unknown): void; | ||
@@ -6,0 +6,0 @@ private wss; |
@@ -8,5 +8,17 @@ "use strict"; | ||
class ConnectionsServer { | ||
constructor(server, options, connectionClosed) { | ||
constructor(server, options, connectionClosed, closeSocketsWithDifferentPath) { | ||
this.clientSockets = new Map(); | ||
this.wss = new ws_1.WebSocketServer({ server, path: options.path }); | ||
this.wss = new ws_1.WebSocketServer({ noServer: true }); | ||
server.on("upgrade", (request, socket, head) => { | ||
if (request.url?.startsWith(options.path)) { | ||
this.wss.handleUpgrade(request, socket, head, (ws) => { | ||
this.wss.emit("connection", ws, request); | ||
}); | ||
} | ||
else { | ||
if (closeSocketsWithDifferentPath) { | ||
socket.destroy(); | ||
} | ||
} | ||
}); | ||
this.wss.on("connection", (ws) => { | ||
@@ -13,0 +25,0 @@ ws.alive = true; |
@@ -14,3 +14,7 @@ "use strict"; | ||
const isJson = req.headersDistinct["content-type"]?.includes("application/json") ?? false; | ||
const body = isJson ? (0, json_js_1.safeParseJson)(await readBody(req)) : []; | ||
const textBody = await readBody(req); | ||
let body = isJson && !!textBody ? (0, json_js_1.safeParseJson)(textBody) : []; | ||
if (!Array.isArray(body)) { | ||
body = [body]; | ||
} | ||
let result; | ||
@@ -17,0 +21,0 @@ switch (req.method) { |
@@ -92,3 +92,3 @@ "use strict"; | ||
this.localSubscriptions.unsubscribeAll(clientId); | ||
}); | ||
}, !("server" in this.options)); | ||
this.httpServer.addListener("request", (req, res) => (0, http_js_1.serveHttpRequest)(req, res, options.path, { | ||
@@ -95,0 +95,0 @@ call: this.call, |
{ | ||
"name": "@push-rpc/next", | ||
"version": "2.0.0-beta.1", | ||
"version": "2.0.0-beta.2", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
@@ -10,6 +10,19 @@ import {safeStringify} from "../utils/json.js" | ||
options: ConnectionsServerOptions, | ||
connectionClosed: (clientId: string) => void | ||
connectionClosed: (clientId: string) => void, | ||
closeSocketsWithDifferentPath: boolean | ||
) { | ||
this.wss = new WebSocketServer({server, path: options.path}) | ||
this.wss = new WebSocketServer({noServer: true}) | ||
server.on("upgrade", (request, socket, head) => { | ||
if (request.url?.startsWith(options.path)) { | ||
this.wss.handleUpgrade(request, socket, head, (ws) => { | ||
this.wss.emit("connection", ws, request) | ||
}) | ||
} else { | ||
if (closeSocketsWithDifferentPath) { | ||
socket.destroy() | ||
} | ||
} | ||
}) | ||
this.wss.on("connection", (ws: WebSocket & {alive: boolean}) => { | ||
@@ -16,0 +29,0 @@ ws.alive = true |
@@ -24,4 +24,9 @@ import * as http from "http" | ||
const isJson = req.headersDistinct["content-type"]?.includes("application/json") ?? false | ||
const body = isJson ? safeParseJson(await readBody(req)) : [] | ||
const textBody = await readBody(req) | ||
let body = isJson && !!textBody ? safeParseJson(textBody) : [] | ||
if (!Array.isArray(body)) { | ||
body = [body] | ||
} | ||
let result: unknown | ||
@@ -28,0 +33,0 @@ switch (req.method) { |
@@ -46,3 +46,4 @@ import {PublishServicesOptions, RpcServer} from "./index.js" | ||
this.localSubscriptions.unsubscribeAll(clientId) | ||
} | ||
}, | ||
!("server" in this.options) | ||
) | ||
@@ -49,0 +50,0 @@ |
@@ -187,2 +187,22 @@ import {assert} from "chai" | ||
}) | ||
it("non array in params converts to 1st param", async () => { | ||
let param: any | ||
await startTestServer({ | ||
async hello1(param1) { | ||
param = param1 | ||
}, | ||
}) | ||
await fetch(`http://127.0.0.1:${TEST_PORT}/rpc/hello1`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({param1: "yes1"}), | ||
}) | ||
assert.deepEqual(param, {param1: "yes1"}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
184265
4120