Skip to content

Commit

Permalink
fix: make some improvements (asyncapi#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhudaDad414 authored and Ruchip16 committed Jan 11, 2023
1 parent 31f12ea commit f86c74c
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 110 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src/lib/index'
223 changes: 157 additions & 66 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"@types/debug": "^4.1.7",
"@types/socket.io": "^3.0.2",
"@types/uri-templates": "^0.1.31",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.9.0",
"@typescript-eslint/parser": "^5.9.0",
"all-contributors-cli": "^6.14.2",
Expand Down
7 changes: 3 additions & 4 deletions src/adapters/ws/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,16 @@ class WebSocketsAdapter extends Adapter {
}
}
}

if (servers.has(pathname)) {
servers.get(pathname).handleUpgrade(request, socket, head, (ws) => {
servers.get(pathname).emit('server:connection:open', ws, request)

servers.get(pathname).emit('connect', ws, request)
ws.on('message', (payload) => {
const msg = this._createMessage(pathname, payload)
this.emit('message', msg, ws)
})

this.emit('connect', { name: this.name(), adapter: this, connection: ws, channel: pathname })
this.emit('server:connection:open', { name: this.name(), adapter: this, connection: ws, channel: pathname, request })
})
} else {
socket.destroy()
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default async function GleeAppInitializer () {
if (channel.hasPublish()) {
const operationId = channel.publish().json('operationId')
if (operationId) {
const schema = channel.publish().message().payload().json()
const schema = {oneOf: channel.publish().messages().map(message => message.payload().json())} as any
app.use(channelName, validate(schema), (event, next) => {
triggerFunction({
app,
Expand All @@ -78,7 +78,7 @@ export default async function GleeAppInitializer () {
}
}
if (channel.hasSubscribe()) {
const schema = channel.subscribe().message().payload().json()
const schema = {oneOf: channel.subscribe().messages().map(message => message.payload().json())} as any
app.useOutbound(channelName, validate(schema), json2string)
}
})
Expand Down
4 changes: 1 addition & 3 deletions src/lib/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ async function loadConfigsFromFile() {
ASYNCAPI_FILE_PATH = projectConfigs.ASYNCAPI_FILE_PATH ? path.resolve(GLEE_DIR, projectConfigs.ASYNCAPI_FILE_PATH) : ASYNCAPI_FILE_PATH
return projectConfigs
} catch (e) {
if (e.code !== 'ERR_MODULE_NOT_FOUND') {
return console.error(e)
}
return console.error(e)
}
}

Expand Down
34 changes: 15 additions & 19 deletions src/lib/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,23 @@ export async function trigger({
return
}

if (res?.send) {
res.send.forEach((msg) => {
app.send(new GleeMessage({
payload: msg.payload,
headers: msg.headers,
channel: msg.channel || message.channel,
serverName: msg.server,
broadcast: true,
}))
})
}
res?.send?.forEach((msg) => {
app.send(new GleeMessage({
payload: msg.payload,
headers: msg.headers,
channel: msg.channel || message.channel,
serverName: msg.server,
broadcast: true,
}))
})

if (res?.reply) {
res.reply.forEach((msg) => {
message.reply({
payload: msg.payload,
headers: msg.headers,
channel: msg.channel,
})
res?.reply?.forEach((msg) => {
message.reply({
payload: msg.payload,
headers: msg.headers,
channel: msg.channel,
})
}
})
} catch (err) {
if (err.code === 'ERR_MODULE_NOT_FOUND') {
const functionsPath = relative(GLEE_DIR, GLEE_FUNCTIONS_DIR)
Expand Down
30 changes: 14 additions & 16 deletions src/lib/lifecycleEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,20 @@ export async function run(lifecycleEvent: string, params: GleeFunctionEvent) {
const responses = await Promise.all(handlers.map(info => info.fn(params)))

responses.forEach(res => {
if (res.send) {
res.send.forEach((event: GleeFunctionReturnSend) => {
try {
params.glee.send(new GleeMessage({
payload: event.payload,
headers: event.headers,
channel: event.channel,
serverName: event.server,
connection: params.connection,
}))
} catch (e) {
console.error(`The ${lifecycleEvent} lifecycle function failed to send an event to channel ${event.channel}.`)
console.error(e)
}
})
}
res?.send?.forEach((event: GleeFunctionReturnSend) => {
try {
params.glee.send(new GleeMessage({
payload: event.payload,
headers: event.headers,
channel: event.channel,
serverName: event.server,
connection: params.connection,
}))
} catch (e) {
console.error(`The ${lifecycleEvent} lifecycle function failed to send an event to channel ${event.channel}.`)
console.error(e)
}
})
})
} catch (e) {
console.error(e)
Expand Down

0 comments on commit f86c74c

Please sign in to comment.