Skip to content

Commit

Permalink
feat: update parser-js to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Souvikns committed Sep 20, 2023
1 parent ec5b767 commit c7ba693
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 782 deletions.
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",
"@types/jest": "^27.4.0",
"@types/qs": "^6.9.7",
"ajv": "^6.12.6",
Expand Down
12 changes: 6 additions & 6 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 @@ -51,7 +51,7 @@ class GleeAdapter extends EventEmitter {
this._AsyncAPIServer = server

this._parsedAsyncAPI = parsedAsyncAPI
this._channelNames = this._parsedAsyncAPI.channelNames()
this._channelNames = this._parsedAsyncAPI.channels().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().filterByReceive().length <= 0) return false

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.channels().map(e => e.address)
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 asyncapi, { 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')
return asyncapi.parse(asyncapiFileContent)
const parser = new Parser()
return toAsyncAPIDocument(parser.parse(asyncapiFileContent))
}
2 changes: 1 addition & 1 deletion src/lib/connection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Server as AsyncAPIServer, AsyncAPIDocument } from '@asyncapi/parser'
import { ServerInterface as AsyncAPIServer, AsyncAPIDocumentInterface as AsyncAPIDocument } from '@asyncapi/parser'

interface IGleeConnectionConstructor {
connection: any
Expand Down
2 changes: 1 addition & 1 deletion 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
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
8 changes: 4 additions & 4 deletions src/registerAdapters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AsyncAPIDocument, Server } from '@asyncapi/parser'
import { AsyncAPIDocumentV2 as AsyncAPIDocument, ServerInterface } from '@asyncapi/parser'
import MqttAdapter from './adapters/mqtt/index.js'
import WebSocketServerAdapter from './adapters/ws/server.js'
import WebsocketClientAdapter from './adapters/ws/client.js'
Expand All @@ -19,7 +19,7 @@ export default async (
const serverNames = await getSelectedServerNames()

serverNames.forEach((serverName) => {
const server = parsedAsyncAPI.server(serverName)
const server: ServerInterface = parsedAsyncAPI.servers().get(serverName)

if (!server) {
throw new Error(
Expand All @@ -35,13 +35,13 @@ export default async (

function registerAdapterForServer(
serverName: string,
server: Server,
server: ServerInterface,
app: Glee,
parsedAsyncAPI: AsyncAPIDocument,
config: GleeConfig
) {
const protocol = server.protocol()
const remoteServers = parsedAsyncAPI.extension('x-remoteServers')
const remoteServers = parsedAsyncAPI.extensions().get('x-remoteServers').value()
if (['mqtt', 'mqtts', 'secure-mqtt'].includes(protocol)) {
app.addAdapter(MqttAdapter, {
serverName,
Expand Down

0 comments on commit c7ba693

Please sign in to comment.