Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make some improvements #363

Merged
merged 8 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -73,6 +73,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 @@ -104,27 +104,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