From ca49bb96da6122e83ba9675e54583296b89ac5f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20M=C3=A9ndez?= Date: Tue, 13 Jul 2021 20:39:39 +0200 Subject: [PATCH] feat: expand URL variables (#9) * feat: expand URL variables * Rename .env.local to .env --- examples/dummy-java/{.env.local => .env} | 0 examples/dummy/{.env.local => .env} | 0 .../shrek-websockets/{.env.local => .env} | 0 package-lock.json | 28 ++++++++++--------- package.json | 4 ++- src/adapters/ws/index.js | 2 +- src/cli/index.js | 2 +- src/index.js | 5 +++- src/lib/adapter.js | 9 ++++++ 9 files changed, 33 insertions(+), 17 deletions(-) rename examples/dummy-java/{.env.local => .env} (100%) rename examples/dummy/{.env.local => .env} (100%) rename examples/shrek-websockets/{.env.local => .env} (100%) diff --git a/examples/dummy-java/.env.local b/examples/dummy-java/.env similarity index 100% rename from examples/dummy-java/.env.local rename to examples/dummy-java/.env diff --git a/examples/dummy/.env.local b/examples/dummy/.env similarity index 100% rename from examples/dummy/.env.local rename to examples/dummy/.env diff --git a/examples/shrek-websockets/.env.local b/examples/shrek-websockets/.env similarity index 100% rename from examples/shrek-websockets/.env.local rename to examples/shrek-websockets/.env diff --git a/package-lock.json b/package-lock.json index e0b491cf0..4faeba51b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4300,11 +4300,6 @@ } } }, - "commander": { - "version": "2.5.0", - "resolved": "https://registry.npmjs.org/commander/-/commander-2.5.0.tgz", - "integrity": "sha1-13e2pNhH1CPl1HXahkKUrB/1qp0=" - }, "commist": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/commist/-/commist-1.1.0.tgz", @@ -4958,6 +4953,16 @@ "is-obj": "^2.0.0" } }, + "dotenv": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", + "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==" + }, + "dotenv-expand": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-5.1.0.tgz", + "integrity": "sha512-YXQl1DSa4/PQyRfgrv6aoNjhasp/p4qs9FjJ4q4cQk+8m4r6k4ZSiEyytKG8f8W9gi8WsQtIObNmKd+tMzNTmA==" + }, "duplexer2": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.1.4.tgz", @@ -8771,14 +8776,6 @@ } } }, - "localenv": { - "version": "0.2.2", - "resolved": "https://registry.npmjs.org/localenv/-/localenv-0.2.2.tgz", - "integrity": "sha1-xQjynTSFvck0HT6tF/YcWr0bC6s=", - "requires": { - "commander": "2.5.0" - } - }, "locate-path": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz", @@ -15438,6 +15435,11 @@ "punycode": "^2.1.0" } }, + "uri-templates": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/uri-templates/-/uri-templates-0.2.0.tgz", + "integrity": "sha1-K1eEURzJCYaHMekjPCaAl9ELSZ8=" + }, "urix": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/urix/-/urix-0.1.0.tgz", diff --git a/package.json b/package.json index f28a91ea4..c71dff956 100644 --- a/package.json +++ b/package.json @@ -34,14 +34,16 @@ "bufferutil": "^4.0.3", "chalk": "^4.1.1", "debug": "^4.3.1", + "dotenv": "^10.0.0", + "dotenv-expand": "^5.1.0", "emojis": "^1.0.10", - "localenv": "^0.2.2", "mqtt": "^4.2.6", "node-ipc": "^10.0.1", "nodemon": "^2.0.7", "path-to-regexp": "^6.2.0", "socket.io": "^4.1.2", "terminal-image": "^2.0.0", + "uri-templates": "^0.2.0", "utf-8-validate": "^5.0.5", "uuid": "^8.3.2", "walkdir": "^0.4.1", diff --git a/src/adapters/ws/index.js b/src/adapters/ws/index.js index db3e62a7d..44c861d12 100644 --- a/src/adapters/ws/index.js +++ b/src/adapters/ws/index.js @@ -21,7 +21,7 @@ class WebSocketsAdapter extends Adapter { _connect () { return new Promise((resolve, reject) => { const channelNames = this.parsedAsyncAPI.channelNames() - const serverUrl = new URL(this.AsyncAPIServer.url()) + const serverUrl = new URL(this.serverUrlExpanded) const wsHttpServer = this.glee.options?.websocket?.httpServer || http.createServer() const asyncapiServerPort = serverUrl.port || 80 diff --git a/src/cli/index.js b/src/cli/index.js index cb37881fa..5c2d42422 100755 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -18,7 +18,7 @@ if (command === 'dev') { script: path.resolve(__dirname, 'start.js'), ext: '*', watch: [ - '.env.local', + '.env', ], env: { NODE_ENV: 'development', diff --git a/src/index.js b/src/index.js index 4ca28944c..00013b95a 100755 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ -import 'localenv' +import dotenv from 'dotenv' +import dotenvExpand from 'dotenv-expand' import { readFile } from 'fs/promises' import asyncapi from '@asyncapi/parser' import Glee from './lib/glee.js' @@ -19,6 +20,8 @@ import { startRuntimeServers } from './lib/runtimes/index.js' import { setConstants } from './lib/constants.js' import { triggerFunction } from './lib/runtimes/index.js' +dotenvExpand(dotenv.config()) + export default async function GleeAppInitializer (config = {}) { if (!process.env.GLEE_SERVER_NAMES) { throw new Error('Missing "GLEE_SERVER_NAMES" environment variable.') diff --git a/src/lib/adapter.js b/src/lib/adapter.js index 4868ca9da..822437b0a 100644 --- a/src/lib/adapter.js +++ b/src/lib/adapter.js @@ -1,4 +1,5 @@ import EventEmitter from 'events' +import uriTemplates from 'uri-templates' import GleeConnection from './connection.js' class GleeAdapter extends EventEmitter { @@ -16,9 +17,17 @@ class GleeAdapter extends EventEmitter { this.glee = glee this.serverName = serverName this.AsyncAPIServer = server + this.parsedAsyncAPI = parsedAsyncAPI this.connections = [] + const uriTemplateValues = {} + process.env.GLEE_SERVER_VARIABLES?.split(',').forEach(t => { + const [server, variable, value] = t.split(':') + if (server === this.serverName) uriTemplateValues[variable] = value + }) + this.serverUrlExpanded = uriTemplates(this.AsyncAPIServer.url()).fill(uriTemplateValues) + this.on('error', err => { this.glee.injectError(err) }) this.on('message', (message, connection) => { const conn = new GleeConnection({