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 7 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
8 changes: 4 additions & 4 deletions src/adapters/http/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ class HttpClientAdapter extends Adapter {
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()
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
const method = httpChannelBinding.json().method
let url = `${serverUrl}/${channelName}`
const gleeAuth = new GleeAuth(
this.AsyncAPIServer,
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
36 changes: 19 additions & 17 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,24 @@ 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)

const parsedSecurityScehemes = this.parsedAsyncAPI.components().securitySchemes().all()
Souvikns marked this conversation as resolved.
Show resolved Hide resolved

let userAndPasswordSecurityReq
let X509SecurityReq

for (const security of parsedSecurityScehemes) {
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
if (security.type() === 'userPassword') {
userAndPasswordSecurityReq = security
}
)
const userAndPasswordSecurityReq = securityRequirements.find(
(sec) => sec.type() === 'userPassword'
)
const X509SecurityReq = securityRequirements.find(
(sec) => sec.type() === 'X509'
)
if (security.type() === 'x509') {
X509SecurityReq = security
}
}

return {
userAndPasswordSecurityReq,
X509SecurityReq,
X509SecurityReq
}
}

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

private subscribe(channels: string[]) {
channels.forEach((channel) => {
const operation = this.parsedAsyncAPI.channel(channel).publish()
const operation = this.parsedAsyncAPI.channels().get(channel).json().subscribe()
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
const binding = operation.binding('mqtt')
this.client.subscribe(channel, {
qos: binding?.qos ? binding.qos : 0,
Expand All @@ -154,8 +156,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,7 +207,7 @@ class MqttAdapter extends Adapter {

_send(message: GleeMessage): Promise<void> {
return new Promise((resolve, reject) => {
const operation = this.parsedAsyncAPI.channel(message.channel).subscribe()
const operation = this.parsedAsyncAPI.channels().get(message.channel).json().subscribe()
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
const binding = operation ? operation.binding('mqtt') : undefined
this.client.publish(
message.channel,
Expand Down
11 changes: 5 additions & 6 deletions src/adapters/ws/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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().length !== 0) { // NOSONAR
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
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
26 changes: 13 additions & 13 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().map(e => e.address())
Souvikns marked this conversation as resolved.
Show resolved Hide resolved

const app = new Glee(config)

Expand All @@ -78,15 +78,15 @@ 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')
const channel = parsedAsyncAPI.channels().get(channelName)
if (channel.operations().filterBySend().length !==0) {
const operationId = channel.operations()[0].operationId()
const publishOperation = channel.operations().filterBySend()[0]
if (operationId) {
const schema = {
oneOf: channel
.publish()
.messages()
.map((message) => message.payload().json()),
oneOf: publishOperation
.messages()
.map(m => m.payload().json()),
} as any
app.use(channelName, validate(schema), (event, next) => {
triggerFunction({
Expand All @@ -99,12 +99,12 @@ export default async function GleeAppInitializer() {
})
}
}
if (channel.hasSubscribe()) {
if (channel.operations().filterByReceive().length !== 0) {
const subscribeOperation = channel.operations().filterByReceive()[0]
const schema = {
oneOf: channel
.subscribe()
.messages()
.map((message) => message.payload().json()),
oneOf: subscribeOperation
.messages()
.map(m => m.payload().json()),
} as any
app.useOutbound(channelName, validate(schema), json2string)
}
Expand Down
14 changes: 7 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 @@ -41,7 +41,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 +51,7 @@ class GleeAdapter extends EventEmitter {
this._AsyncAPIServer = server

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

const uriTemplateValues = new Map()
Expand Down Expand Up @@ -229,12 +229,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
5 changes: 3 additions & 2 deletions src/lib/asyncapiFile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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()
return toAsyncAPIDocument(parser.parse(asyncapiFileContent))
}
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
4 changes: 2 additions & 2 deletions src/lib/servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ export async function getSelectedServerNames(): Promise<string[]> {
const parsedAsyncAPI = await getParsedAsyncAPI()

if (!process.env.GLEE_SERVER_NAMES) {
return parsedAsyncAPI.serverNames()
return parsedAsyncAPI.servers().all().map(e => e.url())
}

const arrayOfNames = process.env.GLEE_SERVER_NAMES.split(',')
return parsedAsyncAPI.serverNames().filter((name) => {
return parsedAsyncAPI.servers().all().map(e => e.url()).filter((name) => {
return arrayOfNames.includes(name)
})
}
4 changes: 2 additions & 2 deletions src/lib/util.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 Ajv from 'ajv'
import betterAjvErrors from 'better-ajv-errors'
import { pathToRegexp } from 'path-to-regexp'
Expand Down Expand Up @@ -138,7 +138,7 @@ export const isRemoteServer = (
parsedAsyncAPI: AsyncAPIDocument,
serverName: string
): boolean => {
const remoteServers = parsedAsyncAPI.extension('x-remoteServers')
const remoteServers = parsedAsyncAPI.extensions().get('x-remoteServers').value()
if (remoteServers) {
return remoteServers.includes(serverName)
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/wsHttpAuth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AsyncAPIDocument, SecurityScheme, Server } from '@asyncapi/parser'
import { AsyncAPIDocumentInterface as AsyncAPIDocument, SecuritySchemeInterface as SecurityScheme, ServerInterface as Server } from '@asyncapi/parser'
import { resolveFunctions } from './util.js'
import { EventEmitter } from 'events'
import { HttpAuthConfig, WsAuthConfig, AuthProps } from './index.js'
Expand Down Expand Up @@ -30,9 +30,9 @@ class GleeAuth extends EventEmitter {

checkClientAuthConfig() {
this.secReqs = (this.AsyncAPIServer.security() || []).map((sec) => {
const secName = Object.keys(sec.json())[0]
const secName = Object.keys(sec.values())[0]
return {
[secName]: this.parsedAsyncAPI.components().securityScheme(secName),
[secName]: this.parsedAsyncAPI.securitySchemes().get(secName).json(),
}
})

Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/existsInAsyncAPI.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { AsyncAPIDocument } from '@asyncapi/parser'
import { AsyncAPIDocumentInterface as AsyncAPIDocument } from '@asyncapi/parser'
import { MiddlewareCallback } from './index.js'
import GleeMessage from '../lib/message.js'

export default (asyncapi: AsyncAPIDocument) =>
(event: GleeMessage, next: MiddlewareCallback) => {
if (typeof event.channel !== 'string')
{return next(new Error(`Invalid channel name: ${event.channel}.`))}
if (asyncapi.channel(event.channel)) return next()
if (asyncapi.channels().get(event.channel)) return next()
next(
new Error(`Channel ${event.channel} is not defined in the AsyncAPI file.`)
)
Expand Down
Loading
Loading