Comparing version 0.0.26 to 0.0.27
{ | ||
"name": "webdetta", | ||
"version": "0.0.26", | ||
"version": "0.0.27", | ||
"author": "Fedot Kriutchenko <fodyadev@gmail.com>", | ||
@@ -5,0 +5,0 @@ "description": "", |
@@ -55,4 +55,3 @@ // MIT License | ||
to: data.from, | ||
res, | ||
...(err ? { err } : {}) | ||
...(err ? { err } : { res }) | ||
})); | ||
@@ -59,0 +58,0 @@ } |
import { safe } from '../common/func.js'; | ||
import { RpcServer } from '../rpc/server.js'; | ||
import { processCall } from '../rpc/proto.js'; | ||
import bytes from 'bytes'; | ||
@@ -11,2 +9,3 @@ import express from 'express'; | ||
import WSS from './wss.js'; | ||
import { generateSDK } from '../rpc/sdk.js'; | ||
@@ -23,12 +22,12 @@ const validatePath = path => { | ||
const resolveProxyOptions = safe(async (rslv, req, ...args) => { | ||
const result = typeof rslv == 'string' ? rslv : await rslv(req, ...args); | ||
const options = typeof result == 'string' ? { url: result } : result; | ||
const url = new URL(options.url); | ||
delete options.url; | ||
let result = typeof rslv == 'string' ? rslv : await rslv(req, ...args); | ||
result = typeof result == 'string' ? { target: result } : result; | ||
const url = new URL(result.target); | ||
req.url = url.pathname + url.search; | ||
options.target = url.origin; | ||
return options; | ||
result.target = url.origin; | ||
return result; | ||
}); | ||
const Server = () => { | ||
let isSecure = false; | ||
const app = express(); | ||
@@ -41,2 +40,3 @@ const server = http.createServer(app); | ||
server, | ||
launch: (...args) => { | ||
@@ -46,12 +46,10 @@ server.listen(...args); | ||
}, | ||
wsApi: (path, { pool, onOpen, onClose, ctx, methods }) => { | ||
validatePath(path); | ||
const upgrade = RpcServer(); | ||
if (pool) upgrade.all = pool; | ||
upgrade.methods = methods; | ||
upgrade.onOpen = onOpen; | ||
upgrade.onClose = onClose; | ||
wss.route(path, (req, ws) => ctx.call(upgrade(ws), req)); | ||
const upgrade = rpcApiWs({ pool, onOpen, onClose, methods }); | ||
wss.route(path, (req, ws) => ctx.call(api.upgrade(ws), req)); | ||
return instance; | ||
}, | ||
httpApi: (path, { bodyLimit='50mb', ctx, methods }) => { | ||
@@ -65,16 +63,8 @@ validatePath(path); | ||
); | ||
const api = rpcApiHttp(methods); | ||
handlers.push(async (req, res) => { | ||
try { | ||
let ctx_ = {}; | ||
await ctx.call(ctx_, req, res); | ||
if (res.headersSent) return; | ||
const name = req.params.name; | ||
const args = req.body; | ||
const [result, err] = await processCall(methods, ctx_, name, args); | ||
if (err) throw err; | ||
res.status(200).send(JSON.stringify(result)); | ||
} catch (e) { | ||
console.error(e); | ||
res.status(500).send(JSON.stringify(e)); | ||
} | ||
await ctx.call(req.ctx = {}, req, res); | ||
if (res.headersSent) return; | ||
const r = await api.processCall(req.ctx, req.params.name, req.body); | ||
res.status(r.status).send(r.result); | ||
}); | ||
@@ -84,2 +74,3 @@ app.post(path, ...handlers); | ||
}, | ||
wsHandler: (path, handler) => { | ||
@@ -90,2 +81,3 @@ validatePath(path); | ||
}, | ||
httpHandler: collectMethods((methods=['all'], path, ...args) => { | ||
@@ -96,2 +88,3 @@ validatePath(path); | ||
}), | ||
wsProxy: (path, resolve) => { | ||
@@ -105,2 +98,3 @@ validatePath(path); | ||
}, | ||
httpProxy: collectMethods((methods=['all'], path, resolve) => { | ||
@@ -115,2 +109,18 @@ validatePath(path); | ||
}), | ||
sdk: (path, methods) => { | ||
const { handlers, clientCode } = generateSDK(methods); | ||
instance.httpHandler.get(path, (req, res) => { | ||
const url = Object.assign(new URL('http://localhost'), { | ||
host: req.headers.host, | ||
protocol: isSecure ? 'wss:' : 'ws:', | ||
pathname: path | ||
}); | ||
res.contentType('text/javascript'); | ||
res.send(clientCode(url)); | ||
}); | ||
instance.wsApi(path, { ctx: () => {}, methods: handlers }); | ||
return instance; | ||
}, | ||
static: (path, ...dirs) => { | ||
@@ -122,2 +132,3 @@ validatePath(path); | ||
}, | ||
cors: (path, options) => { | ||
@@ -128,2 +139,3 @@ validatePath(path); | ||
}, | ||
shutdown: async () => { | ||
@@ -130,0 +142,0 @@ await new Promise(r => server.close(r)); |
import { WebSocketServer } from 'ws'; | ||
import { pathToRegexp } from 'path-to-regexp'; | ||
import { safe } from '../common/func.js'; | ||
@@ -15,3 +14,6 @@ function heartbeat() { | ||
regex: pathToRegexp(path), | ||
handler: safe(handler) | ||
handler: async (...a) => { | ||
try { await handler(...a); } | ||
catch (e) { console.error(e); } | ||
} | ||
}); | ||
@@ -18,0 +20,0 @@ const route = route_(false); |
27106
27
783