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

feat: update parser-js to v2 #523

Merged
merged 33 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c7ba693
feat: update parser-js to v2
Souvikns Sep 20, 2023
5c90e5b
chore: fixing tests
Souvikns Sep 20, 2023
c6d4136
fix the tests
Souvikns Sep 21, 2023
c7d50d0
feat: updating adapters
Souvikns Sep 22, 2023
35a7c68
Update client.ts
Souvikns Sep 22, 2023
523b527
fix: some minor issues
Souvikns Sep 22, 2023
0d6f324
fix: code smells
Souvikns Sep 25, 2023
55881a9
fix: minor issues with parsing
Souvikns Sep 26, 2023
1181316
fix: migration mistakes
Souvikns Sep 28, 2023
e1c12f5
fix: security
Souvikns Sep 29, 2023
ddd88f3
fix: channelName and address
Souvikns Oct 2, 2023
acdb3dd
Update index.ts
Souvikns Oct 2, 2023
5124aa7
Update client.ts
Souvikns Oct 2, 2023
93e6466
fix: update parser to latest pre release
Souvikns Oct 6, 2023
1f6ccb5
fix: review changes.
Souvikns Oct 10, 2023
bd0d57a
Merge remote-tracking branch 'upstream/master' into update-parser
Souvikns Oct 11, 2023
c5828af
Update client.ts
Souvikns Oct 11, 2023
4f48945
fix: reverting markdown template
Souvikns Oct 11, 2023
893a03b
fix: review suggestions
Souvikns Oct 12, 2023
617a15c
ignore docs generation for dummy.
KhudaDad414 Oct 12, 2023
a7aec69
better error message
KhudaDad414 Oct 12, 2023
9ddc198
improve types for flight-management example
KhudaDad414 Oct 12, 2023
f2ca30b
ignore docs
KhudaDad414 Oct 12, 2023
cfb3b15
upgrade package-lock
KhudaDad414 Oct 12, 2023
e9b991d
resolve small bug
KhudaDad414 Oct 12, 2023
4a6eff5
fix(hopefully) test error
KhudaDad414 Oct 12, 2023
8bd20ed
ci: upgrade test coverage
KhudaDad414 Oct 12, 2023
c92ac97
discard changes in coverall
KhudaDad414 Oct 12, 2023
b3f57c0
Merge remote-tracking branch 'upstream/master' into pr/Souvikns/523
KhudaDad414 Oct 12, 2023
cd0801d
Update adapter.ts
Souvikns Oct 16, 2023
9715e99
chore: update to latest parser-js pre-release
Souvikns Oct 16, 2023
f0d7d84
upgrade parser
KhudaDad414 Oct 16, 2023
22ec915
Merge remote-tracking branch 'upstream/master' into pr/Souvikns/523
KhudaDad414 Oct 20, 2023
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
882 changes: 123 additions & 759 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@asyncapi/generator": "^1.12.0",
"@asyncapi/html-template": "^0.28.4",
"@asyncapi/markdown-template": "^1.3.3",
"@asyncapi/parser": "^1.13.1",
"@asyncapi/parser": "^2.1.0",
KhudaDad414 marked this conversation as resolved.
Show resolved Hide resolved
"@types/jest": "^27.4.0",
"@types/qs": "^6.9.7",
"ajv": "^6.12.6",
Expand Down
12 changes: 6 additions & 6 deletions src/adapters/http/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ class HttpClientAdapter extends Adapter {
let headers = {}
const authConfig = await clientAuthConfig(this.serverName)
const serverUrl = this.serverUrlExpanded
for (const channelName of this.channelNames) {
const channelInfo = this.parsedAsyncAPI.channel(channelName)
const httpChannelBinding = channelInfo.binding('http')
const channelServers = channelInfo.servers()
for (const channelName of this.channelAddresses) {
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
const channelInfo = this.parsedAsyncAPI.channels().get(channelName)
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
const httpChannelBinding = channelInfo.bindings().get('http')
const channelServers = channelInfo.servers().all().map(e => e.id())
const isChannelServers =
!channelServers.length || channelServers.includes(message.serverName)
if (httpChannelBinding && isChannelServers) {
const method = httpChannelBinding.method
let url = `${serverUrl}/${channelName}`
const method = httpChannelBinding.json().method
let url = new URL( serverUrl + this.parsedAsyncAPI.channels().get(channelName).address())
const gleeAuth = new GleeAuth(
this.AsyncAPIServer,
this.parsedAsyncAPI,
Expand Down
6 changes: 3 additions & 3 deletions src/adapters/http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class HttpAdapter extends Adapter {
this.httpResponses.set(this.serverName, res)
let { pathname } = new URL(req.url, serverUrl)
pathname = pathname.startsWith('/') ? pathname.substring(1) : pathname
if (!this.parsedAsyncAPI.channel(pathname)) {
if (!this.parsedAsyncAPI.channels().get(pathname)) {
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
res.end('HTTP/1.1 404 Not Found1\r\n\r\n')
const err = new Error(
`A client attempted to connect to channel ${pathname} but this channel is not defined in your AsyncAPI file. here`
Expand All @@ -105,8 +105,8 @@ class HttpAdapter extends Adapter {
const searchParams = { query }
const payload = body
const httpChannelBinding = this.parsedAsyncAPI
.channel(pathname)
.binding('http')
.channels().get(pathname)
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
.bindings().get('http')
if (httpChannelBinding) {
this._checkHttpBinding(
req,
Expand Down
6 changes: 3 additions & 3 deletions src/adapters/kafka/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class KafkaAdapter extends Adapter {
'kafka'
)
const auth: KafkaAuthConfig = await this.getAuthConfig(kafkaOptions?.auth)
const securityRequirements = (this.AsyncAPIServer.security() || []).map(
const securityRequirements = this.AsyncAPIServer.security().map(
(sec) => {
const secName = Object.keys(sec.json())[0]
return this.parsedAsyncAPI.components().securityScheme(secName)
const secName = Object.keys(sec.values())[0]
return this.parsedAsyncAPI.components().securitySchemes().get(secName)
}
)
const userAndPasswordSecurityReq = securityRequirements.find(
Expand Down
41 changes: 22 additions & 19 deletions src/adapters/mqtt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mqtt, { IPublishPacket, MqttClient, QoS } from 'mqtt'
import Adapter from '../../lib/adapter.js'
import GleeMessage from '../../lib/message.js'
import { MqttAuthConfig, MqttAdapterConfig } from '../../lib/index.js'
import { SecurityScheme } from '@asyncapi/parser'
import { SecuritySchemesInterface as SecurityScheme } from '@asyncapi/parser'
import { logLineWithIcon } from '../../lib/logger.js'

interface IMQTTHeaders {
Expand Down Expand Up @@ -42,22 +42,27 @@ class MqttAdapter extends Adapter {
}

private getSecurityReqs() {
const securityRequirements = (this.AsyncAPIServer.security() || []).map(
(sec) => {
const secName = Object.keys(sec.json())[0]
return this.parsedAsyncAPI.components().securityScheme(secName)

let userAndPasswordSecurityReq
let X509SecurityReq

const securityRequirements = this.AsyncAPIServer.security().map(e => e.map(e => e.scheme()))
Souvikns marked this conversation as resolved.
Show resolved Hide resolved

securityRequirements.forEach(security => {
for (const sec of security) {
if(sec.type() === 'userPassword') {
KhudaDad414 marked this conversation as resolved.
Show resolved Hide resolved
userAndPasswordSecurityReq = sec
}

if (sec.type() === 'x509') {
X509SecurityReq = sec
}
}
)
const userAndPasswordSecurityReq = securityRequirements.find(
(sec) => sec.type() === 'userPassword'
)
const X509SecurityReq = securityRequirements.find(
(sec) => sec.type() === 'X509'
)
})

return {
userAndPasswordSecurityReq,
X509SecurityReq,
X509SecurityReq
}
}

Expand Down Expand Up @@ -127,8 +132,7 @@ class MqttAdapter extends Adapter {

private subscribe(channels: string[]) {
channels.forEach((channel) => {
const operation = this.parsedAsyncAPI.channel(channel).publish()
const binding = operation.binding('mqtt')
const binding = this.parsedAsyncAPI.channels().get(channel).bindings().get('mqtt').value()
this.client.subscribe(channel, {
qos: binding?.qos ? binding.qos : 0,
}, (err, granted) => {
Expand All @@ -154,8 +158,8 @@ class MqttAdapter extends Adapter {
)
const auth: MqttAuthConfig = await this.getAuthConfig(mqttOptions?.auth)
const subscribedChannels = this.getSubscribedChannels()
const mqttServerBinding = this.AsyncAPIServer.binding('mqtt')
const mqtt5ServerBinding = this.AsyncAPIServer.binding('mqtt5')
const mqttServerBinding = this.AsyncAPIServer.bindings().get('mqtt')
const mqtt5ServerBinding = this.AsyncAPIServer.bindings().get('mqtt5')

const { userAndPasswordSecurityReq, X509SecurityReq } =
this.getSecurityReqs()
Expand Down Expand Up @@ -205,8 +209,7 @@ class MqttAdapter extends Adapter {

_send(message: GleeMessage): Promise<void> {
return new Promise((resolve, reject) => {
const operation = this.parsedAsyncAPI.channel(message.channel).subscribe()
const binding = operation ? operation.binding('mqtt') : undefined
const binding = this.parsedAsyncAPI.channels().get(message.channel).bindings().get('mqtt').value()
KhudaDad414 marked this conversation as resolved.
Show resolved Hide resolved
this.client.publish(
message.channel,
message.payload,
Expand Down
13 changes: 6 additions & 7 deletions src/adapters/ws/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class WsClientAdapter extends Adapter {
this.serverName,
authConfig
)
let url = new URL(this.AsyncAPIServer.url() + channel)
let url = new URL(this.AsyncAPIServer.url() + this.parsedAsyncAPI.channels().get(channel).address())
if (authConfig) {
const modedAuth = await gleeAuth.processClientAuth(url, headers, {})
headers = modedAuth.headers
Expand All @@ -47,7 +47,7 @@ class WsClientAdapter extends Adapter {
this.clients.push({
channel,
client: new ws(url, { headers }),
binding: this.parsedAsyncAPI.channel(channel).binding('ws'),
binding: this.parsedAsyncAPI.channels().get(channel).bindings().get('ws'),
})
}

Expand Down Expand Up @@ -77,13 +77,12 @@ class WsClientAdapter extends Adapter {
private getWsChannels() {
const channels = []
for (const channel of this.channelNames) {
if (this.parsedAsyncAPI.channel(channel).hasBinding('ws')) {
if (this.parsedAsyncAPI.channel(channel).hasServers()) {
if (this.parsedAsyncAPI.channels().get(channel).bindings().get('ws')) {
if (this.parsedAsyncAPI.channels().get(channel).servers().all().length !== 0) { // NOSONAR
if (
this.parsedAsyncAPI
.channel(channel)
.servers()
.includes(this.serverName)
.channels().get(channel)
.servers().get(this.serverName)
) {
channels.push(channel)
}
Expand Down
6 changes: 3 additions & 3 deletions src/adapters/ws/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class WebSocketsAdapter extends Adapter {
pathname = pathname.substring(1)
}

if (!this.parsedAsyncAPI.channel(pathname)) {
if (!this.parsedAsyncAPI.channels().get(pathname)) {
this.emitPathnameError(socket, pathname)
}

Expand Down Expand Up @@ -232,8 +232,8 @@ class WebSocketsAdapter extends Adapter {
)

const wsChannelBinding = this.parsedAsyncAPI
.channel(pathname)
.binding('ws')
.channels().get(pathname)
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
.bindings().get('ws')

if (wsChannelBinding) {
const correctBindings = await this.checkBindings(socket, {
Expand Down
55 changes: 27 additions & 28 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default async function GleeAppInitializer() {
await registerAuth(GLEE_AUTH_DIR)

const parsedAsyncAPI = await getParsedAsyncAPI()
const channelNames = parsedAsyncAPI.channelNames()
const channelNames = parsedAsyncAPI.channels().all().map(e => e.address())
Souvikns marked this conversation as resolved.
Show resolved Hide resolved

const app = new Glee(config)

Expand All @@ -78,35 +78,34 @@ export default async function GleeAppInitializer() {
await generateDocs(parsedAsyncAPI, config, null)

channelNames.forEach((channelName) => {
const channel = parsedAsyncAPI.channel(channelName)
if (channel.hasPublish()) {
const operationId = channel.publish().json('operationId')
if (operationId) {
const schema = {
oneOf: channel
.publish()
.messages()
.map((message) => message.payload().json()),
} as any
app.use(channelName, validate(schema), (event, next) => {
triggerFunction({
app,
operationId,
message: event,
const channel = parsedAsyncAPI.channels().get(channelName)
if (channel.operations().filterByReceive().length !==0) {

channel.operations().filterByReceive().forEach(operation => {
const receiveOperation = operation
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
const operationId = operation.operationId()
if (operationId) {
const schema = {
onOf: receiveOperation.messages().filterByReceive().map(m => m.payload())
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
} as any
app.use(channelName, validate(schema), (event, next) => {
triggerFunction({
app,
operationId,
message: event
}).then(next).catch(next)
})
.then(next)
.catch(next)
})
}
}
})
}
if (channel.hasSubscribe()) {
const schema = {
oneOf: channel
.subscribe()
.messages()
.map((message) => message.payload().json()),
} as any
app.useOutbound(channelName, validate(schema), json2string)
if (channel.operations().filterBySend().length !== 0) {
channel.operations().filterBySend().forEach(operation => {
const sendOperation = operation
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
const schema = {
onOf: sendOperation.messages().filterBySend().map(m => m.payload())
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
} as any
app.useOutbound(channelName, validate(schema), json2string)
})
}
})

Expand Down
20 changes: 13 additions & 7 deletions src/lib/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable security/detect-object-injection */
import { AsyncAPIDocument, Server } from '@asyncapi/parser'
import { AsyncAPIDocumentInterface as AsyncAPIDocument, ServerInterface as Server } from '@asyncapi/parser'
import EventEmitter from 'events'
import uriTemplates from 'uri-templates'
import GleeConnection from './connection.js'
Expand Down Expand Up @@ -27,6 +27,7 @@ class GleeAdapter extends EventEmitter {
private _AsyncAPIServer: Server
private _parsedAsyncAPI: AsyncAPIDocument
private _channelNames: string[]
private _channelAddresses: string[]
private _connections: GleeConnection[]
private _serverUrlExpanded: string

Expand All @@ -41,7 +42,7 @@ class GleeAdapter extends EventEmitter {
constructor(
glee: Glee,
serverName: string,
server: Server,
server: Server | undefined,
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
parsedAsyncAPI: AsyncAPIDocument
) {
super()
Expand All @@ -51,7 +52,8 @@ class GleeAdapter extends EventEmitter {
this._AsyncAPIServer = server

this._parsedAsyncAPI = parsedAsyncAPI
this._channelNames = this._parsedAsyncAPI.channelNames()
this._channelNames = this._parsedAsyncAPI.channels().all().map(e => e.id())
this._channelAddresses = this._parsedAsyncAPI.channels().all().map(c => c.address())
this._connections = []

const uriTemplateValues = new Map()
Expand Down Expand Up @@ -194,6 +196,10 @@ class GleeAdapter extends EventEmitter {
return this._channelNames
}

get channelAddresses(): string[] {
return this._channelAddresses
}

get connections(): GleeConnection[] {
return this._connections
}
Expand Down Expand Up @@ -229,12 +235,12 @@ class GleeAdapter extends EventEmitter {
*/
getSubscribedChannels(): string[] {
return this._channelNames.filter((channelName) => {
const channel = this._parsedAsyncAPI.channel(channelName)
if (!channel.hasPublish()) return false
const channel = this._parsedAsyncAPI.channels().get(channelName)
if (channel.operations().filterBySend().length == 0) return true

const channelServers = channel.hasServers()
const channelServers = channel.servers()
? channel.servers()
: channel.ext('x-servers') || this._parsedAsyncAPI.serverNames()
: channel.extensions().get('x-servers')?.value() || this._parsedAsyncAPI.allServers()
return channelServers.includes(this._serverName)
})
}
Expand Down
6 changes: 4 additions & 2 deletions src/lib/asyncapiFile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { readFile } from 'fs/promises'
import asyncapi, { AsyncAPIDocument } from '@asyncapi/parser'
import { AsyncAPIDocumentInterface as AsyncAPIDocument, Parser, toAsyncAPIDocument} from '@asyncapi/parser'
import { getConfigs } from './configs.js'

export async function getParsedAsyncAPI(): Promise<AsyncAPIDocument> {
const { ASYNCAPI_FILE_PATH } = getConfigs()
const asyncapiFileContent = await readFile(ASYNCAPI_FILE_PATH, 'utf-8')

Check warning on line 7 in src/lib/asyncapiFile.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Found readFile from package "fs/promises" with non literal argument at index 0

Check warning on line 7 in src/lib/asyncapiFile.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - macos-latest

Found readFile from package "fs/promises" with non literal argument at index 0
return asyncapi.parse(asyncapiFileContent)
const parser = new Parser()
const {document} = await parser.parse(asyncapiFileContent)
return toAsyncAPIDocument(document)
}
4 changes: 2 additions & 2 deletions src/lib/connection.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Server as AsyncAPIServer, AsyncAPIDocument } from '@asyncapi/parser'
import { ServerInterface as AsyncAPIServer, AsyncAPIDocumentInterface as AsyncAPIDocument } from '@asyncapi/parser'

interface IGleeConnectionConstructor {
connection: any
channels: string[]
serverName: string
server: AsyncAPIServer
server?: AsyncAPIServer
parsedAsyncAPI: AsyncAPIDocument
}

Expand Down
5 changes: 1 addition & 4 deletions src/lib/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

export async function register(dir: string) {
try {
const statsDir = await stat(dir)

Check warning on line 57 in src/lib/functions.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Found stat from package "fs/promises" with non literal argument at index 0

Check warning on line 57 in src/lib/functions.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - macos-latest

Found stat from package "fs/promises" with non literal argument at index 0
if (!statsDir.isDirectory()) return
} catch (e) {
if (e.code === 'ENOENT') return
Expand Down Expand Up @@ -118,10 +118,7 @@

res?.send?.forEach((msg) => {
const localServerProtocols = ['ws', 'wss', 'http', 'https']
const serverProtocol = parsedAsyncAPI
.server(msg.server || message.serverName)
.protocol()
.toLowerCase()
const serverProtocol = parsedAsyncAPI.servers().get(msg.server || message.serverName).protocol().toLocaleLowerCase()
const isBroadcast =
localServerProtocols.includes(serverProtocol) &&
!isRemoteServer(parsedAsyncAPI, msg.server)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/glee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import EventEmitter from 'events'
import async from 'async'
import Debug from 'debug'
import { AsyncAPIDocument, Server } from '@asyncapi/parser'
import { AsyncAPIDocumentInterface as AsyncAPIDocument, ServerInterface as Server } from '@asyncapi/parser'
import GleeAdapter from './adapter.js'
import GleeClusterAdapter from './cluster.js'
import GleeRouter, {
Expand Down Expand Up @@ -78,7 +78,7 @@ export default class Glee extends EventEmitter {
serverName,
server,
parsedAsyncAPI,
}: { serverName: string; server: Server; parsedAsyncAPI: AsyncAPIDocument }
}: { serverName: string; server: Server | undefined; parsedAsyncAPI: AsyncAPIDocument }
) {
this._adapters.push({ Adapter, serverName, server, parsedAsyncAPI })
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AsyncAPIDocument } from '@asyncapi/parser'
import { AsyncAPIDocumentInterface as AsyncAPIDocument } from '@asyncapi/parser'
import GleeAdapter from './adapter.js'
import GleeClusterAdapter from './cluster.js'
import GleeConnection from './connection.js'
Expand Down
Loading
Loading