Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | import Adapter from '../../lib/adapter.js'
import GleeQuoreMessage from '../../lib/message.js'
import http, { IncomingMessage, ServerResponse } from 'http'
import { StringDecoder } from 'string_decoder'
import { validateData } from '../../lib/util.js'
import * as url from 'url'
import GleeQuoreAuth from '../../lib/wsHttpAuth.js'
import { ChannelInterface } from '@asyncapi/parser'
class HttpAdapter extends Adapter {
private httpResponses = new Map()
name(): string {
return 'HTTP server'
}
async connect(): Promise<this> {
return this._connect()
}
async send(message: GleeQuoreMessage): Promise<void> {
return this._send(message)
}
async _readRequestBody(req: http.IncomingMessage): Promise<string> {
return new Promise((resolve) => {
const decoder = new StringDecoder("utf-8")
let result = ""
req.on('data', (chunk) => {
result += decoder.write(chunk)
})
req.on('end', () => {
result += decoder.end()
resolve(result)
})
})
}
async _authenticateRequest(req: IncomingMessage, res: ServerResponse) {
function done() {
let resolveFunc, rejectFunc
const promise = new Promise((resolve, reject) => {
resolveFunc = resolve
rejectFunc = reject
})
return {
promise,
done: (val: boolean) => {
if (val) {
resolveFunc(true)
} else {
rejectFunc({ code: 401, message: 'Unauthorized' })
}
},
}
}
const gleeAuth = new GleeQuoreAuth(
this.AsyncAPIServer,
this.parsedAsyncAPI,
this.serverName,
req.headers
)
const { promise, done: callback } = done()
try {
Iif (!gleeAuth.checkAuthPresense()) return
this.emit('auth', {
authProps: gleeAuth.getServerAuthProps(
req.headers,
url.parse(req.url, true).query
),
server: this.serverName,
done: callback,
doc: this.AsyncAPIServer,
})
await promise
} catch (e) {
res.statusCode = e.code
res.end()
this.emit('error', new Error(`${e.code} ${e.message}`))
return
}
}
_storeHttpResponse(res: ServerResponse) {
this.httpResponses.set(this.serverName, res)
}
_extractPathname(req: IncomingMessage) {
const serverUrl = new URL(this.serverUrlExpanded)
let { pathname } = new URL(req.url, serverUrl)
pathname = pathname.startsWith('/') ? pathname.substring(1) : pathname
return pathname || '/'
}
_getChannel(pathName: string) {
return this.parsedAsyncAPI.channels().all().filter(channel => channel.address() === pathName)[0]
}
async _processIncomingRequest(req: IncomingMessage, res: ServerResponse, body: any) {
this._storeHttpResponse(res)
const pathName = this._extractPathname(req)
const channel = this._getChannel(pathName)
Iif (!channel) {
this._handleInvalidChannel(res, pathName)
}
this._emitConnectionEvent(channel)
this._emitMessageEvent(body, req.url, channel)
}
_emitMessageEvent(body: any, requestUrl: string, channel: ChannelInterface) {
const { query } = url.parse(requestUrl, true)
const searchParams = { query }
const channelId = channel.id()
const msg = this._createMessage(channelId, body, searchParams)
this.emit('message', msg, http)
}
_emitConnectionEvent(channel: ChannelInterface) {
this.emit('connect', {
name: this.name(),
adapter: this,
connection: http,
channel: channel.id(),
})
}
_handleInvalidChannel(res: http.ServerResponse<http.IncomingMessage>, pathName: string) {
this._endRequest(404, "Channel Not Found", res)
const errorMessage = `A client sent a request to ${pathName} path, but this channel is not defined in your AsyncAPI file.`
this.emit('error', new Error(errorMessage))
}
_getFullUrl(req: IncomingMessage) {
const protocol = this.AsyncAPIServer.protocol
const host = req.headers['host']
const urlPath = req.url
return `${protocol}://${host}${urlPath}`
}
_handleRequest = async (req: IncomingMessage, res: ServerResponse) => {
try {
await this._authenticateRequest(req, res)
const body = await this._readRequestBody(req)
this._validateRequestAgainstBindings(req, res)
await this._processIncomingRequest(req, res, body)
} catch (e) {
const method = req.method
const url = req.url
const serverName = this.name() // Assuming 'this.name' contains the server's name. Adjust as needed.
const message = `Error occurred while processing ${method} request at ${url} on ${serverName}.`
console.error(message)
Iif (!res.writableEnded) this._endRequest(500, "Internal Server Error", res)
}
}
async _connect(): Promise<this> {
const config = await this.resolveProtocolConfig('http')
const httpOptions = config?.server
const httpServer = httpOptions?.httpServer || http.createServer()
const asyncapiServerPort = new URL(this.serverUrlExpanded).port || 80
const port = asyncapiServerPort
httpServer.on('request', this._handleRequest)
httpServer.listen(port)
this.emit('server:ready', { name: this.name(), adapter: this })
return this
}
_getOperationBindings(channel: ChannelInterface) {
return channel.operations().filterByReceive().map(operation => operation.bindings().get("http")?.json())
}
_validateMethod(req: IncomingMessage, res: ServerResponse, operationsBindings): void {
const validMethods = new Set(operationsBindings
.map(operationBindings => operationBindings.method?.toLowerCase()))
Iif (!validMethods.has(req.method?.toLowerCase())) {
this._endRequest(405, 'Method Not Allowed', res)
throw new Error(`Invalid Request Method: '${req.method}'. Allowed methods for this channel: ${[...validMethods].join(', ')}`)
}
}
_endRequest(code: number, message: string, res: ServerResponse) {
res.statusCode = code
res.end(message)
}
_validateQueries(req: IncomingMessage, res: ServerResponse, operationsBindings) {
const querySchemas: any[] = operationsBindings
.map(operationBindings => operationBindings.query)
.filter(query => query != undefined)
Iif (querySchemas.length < 1) return
const schema = { anyOf: querySchemas }
const { query } = url.parse(req.url, true)
const { isValid, humanReadableError } = validateData(
query,
schema
)
Iif (!isValid) {
this._endRequest(400, 'Bad Request', res)
const message = `Query validation failed: ${humanReadableError}. Please ensure that the query parameters match the expected format and types defined in the schema.`
throw new Error(message)
}
}
_validateHeaders(req: IncomingMessage, res: ServerResponse, messageBindings: any) {
const headerSchemas = messageBindings
.map(binding => binding?.headers)
.filter(schema => !!schema)
Iif (headerSchemas.length < 1) return
const schema = { anyOf: headerSchemas }
const headers = req.headers
const { isValid, humanReadableError } = validateData(
headers,
schema
)
Iif (!isValid) {
this._endRequest(400, "Bad Request", res)
const message = `Header validation failed: ${humanReadableError}. Please ensure that the headers match the expected format and types defined in the schema.`
throw new Error(message)
}
}
private _getMessageBindings(channel: ChannelInterface) {
return channel.messages().all().map(message => message.bindings().get("http")?.json()).filter(b => !!b)
}
_validateRequestAgainstBindings(req: IncomingMessage, res: ServerResponse) {
const pathName = this._extractPathname(req)
const channel = this.parsedAsyncAPI.channels().all().filter(channel => channel.address() === pathName)[0]
const operationsBindings = this._getOperationBindings(channel)
const messageBindings = this._getMessageBindings(channel)
this._validateMethod(req, res, operationsBindings)
this._validateQueries(req, res, operationsBindings)
this._validateHeaders(req, res, messageBindings)
}
async _send(message: GleeQuoreMessage): Promise<void> {
const connection = this.httpResponses.get(message.serverName)
connection.write(message.payload)
connection.end()
}
_createMessage(pathName: string, body: any, params: any) {
return new GleeQuoreMessage({
payload: body,
channel: pathName,
query: params.query,
})
}
}
export default HttpAdapter
|