mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 18:44:17 +00:00
handle wsEvents
This commit is contained in:
parent
fd3a1e8930
commit
12ef2284c3
@ -1,7 +1,10 @@
|
|||||||
const generateRequestDispatcher = require("./generateRequestDispatcher")
|
|
||||||
const axios = require("axios")
|
const axios = require("axios")
|
||||||
|
const WSInterface = require("../classes/ClientWSInterface")
|
||||||
const camalize = require("@corenode/utils/dist/camalize").default
|
const camalize = require("@corenode/utils/dist/camalize").default
|
||||||
|
|
||||||
|
const generateRequestDispatcher = require("./generateRequestDispatcher")
|
||||||
|
const generateWSRequestDispatcher = require("./generateWSRequestDispatcher")
|
||||||
|
|
||||||
const FixedMethods = {
|
const FixedMethods = {
|
||||||
"del": "delete"
|
"del": "delete"
|
||||||
}
|
}
|
||||||
@ -13,18 +16,34 @@ module.exports = class Bridge {
|
|||||||
this.origin = this.params.origin
|
this.origin = this.params.origin
|
||||||
this.headers = { ...this.params.headers }
|
this.headers = { ...this.params.headers }
|
||||||
|
|
||||||
this.instance = axios.create({
|
this.httpInterface = axios.create({
|
||||||
baseURL: this.origin,
|
baseURL: this.origin,
|
||||||
headers: this.headers
|
headers: this.headers
|
||||||
})
|
})
|
||||||
|
this.wsInterface = new WSInterface({
|
||||||
|
origin: this.params.wsOrigin,
|
||||||
|
managerOptions: this.params.wsOptions
|
||||||
|
})
|
||||||
|
|
||||||
this.endpoints = {}
|
this.endpoints = {}
|
||||||
|
this.wsEndpoints = {}
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
initialize = async () => {
|
initialize = async () => {
|
||||||
await this.updateEndpointsMap()
|
const instanceManifest = await this.httpInterface.get("/")
|
||||||
|
.then(res => res.data)
|
||||||
|
.catch(err => {
|
||||||
|
console.error(err)
|
||||||
|
throw new Error(`Could not get endpoints map from server. [${err.message}]`)
|
||||||
|
})
|
||||||
|
|
||||||
|
const httpMap = instanceManifest.endpointsMap
|
||||||
|
const wsMap = instanceManifest.wsEndpointsMap
|
||||||
|
|
||||||
|
await this.generateHTTPDispatchers(httpMap)
|
||||||
|
await this.generateWSDispatchers(wsMap)
|
||||||
}
|
}
|
||||||
|
|
||||||
handleRequestContext = async () => {
|
handleRequestContext = async () => {
|
||||||
@ -43,15 +62,13 @@ module.exports = class Bridge {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
updateEndpointsMap = async () => {
|
generateHTTPDispatchers = async (map) => {
|
||||||
const endpointsMap = await this.instance.get("/")
|
if (typeof map !== "object") {
|
||||||
.then(res => res.data.endpointsMap)
|
console.error("[Bridge] > createHTTPDispatchers > map is not an object")
|
||||||
.catch(err => {
|
return false
|
||||||
console.error(err)
|
}
|
||||||
throw new Error(`Could not get endpoints map from server. [${err.message}]`)
|
|
||||||
})
|
|
||||||
|
|
||||||
for await (let HttpMethod of Object.keys(endpointsMap)) {
|
for await (let HttpMethod of Object.keys(map)) {
|
||||||
HttpMethod = HttpMethod.toLowerCase()
|
HttpMethod = HttpMethod.toLowerCase()
|
||||||
|
|
||||||
const fixedMethod = FixedMethods[HttpMethod] ?? HttpMethod
|
const fixedMethod = FixedMethods[HttpMethod] ?? HttpMethod
|
||||||
@ -60,7 +77,7 @@ module.exports = class Bridge {
|
|||||||
this.endpoints[fixedMethod] = {}
|
this.endpoints[fixedMethod] = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.keys(endpointsMap[HttpMethod]).forEach((route) => {
|
Object.keys(map[HttpMethod]).forEach((route) => {
|
||||||
const tree = route.split("/")
|
const tree = route.split("/")
|
||||||
const hasTree = tree.length >= 1
|
const hasTree = tree.length >= 1
|
||||||
|
|
||||||
@ -82,7 +99,7 @@ module.exports = class Bridge {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.endpoints[fixedMethod][nameKey] = generateRequestDispatcher(
|
this.endpoints[fixedMethod][nameKey] = generateRequestDispatcher(
|
||||||
this.instance,
|
this.httpInterface,
|
||||||
fixedMethod,
|
fixedMethod,
|
||||||
route,
|
route,
|
||||||
this.handleRequestContext,
|
this.handleRequestContext,
|
||||||
@ -93,4 +110,20 @@ module.exports = class Bridge {
|
|||||||
|
|
||||||
return this.endpoints
|
return this.endpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
|
generateWSDispatchers = async (map) => {
|
||||||
|
if (typeof map !== "object") {
|
||||||
|
console.error("[Bridge] > createWSDispatchers > map is not an object")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for await (let wsChannel of Object.keys(map)) {
|
||||||
|
const endpoint = map[wsChannel]
|
||||||
|
|
||||||
|
endpoint.nsp[0] == "/" ? endpoint.nsp = endpoint.nsp.slice(1) : null
|
||||||
|
endpoint.method = endpoint.channel[0] == "/" ? endpoint.channel.slice(1) : endpoint.channel
|
||||||
|
|
||||||
|
this.wsEndpoints[endpoint.method] = generateWSRequestDispatcher(this.wsInterface.sockets[endpoint.nsp ?? "main"], endpoint.channel)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user