From d1eaba6bc7e11a5d87da1f1a7aadf879dd274441 Mon Sep 17 00:00:00 2001 From: LanPodder Date: Wed, 11 Jan 2023 01:14:19 +0100 Subject: [PATCH 1/4] databaseAccountEndpoint is now correctly http or https based on --no-ssl flag and updates to requests headers host if called from docker environment --- src/account/index.ts | 15 ++++++++++++--- src/handler/read-meta.ts | 6 +++++- src/index.ts | 8 ++++---- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/account/index.ts b/src/account/index.ts index 78889c2..e04ca45 100644 --- a/src/account/index.ts +++ b/src/account/index.ts @@ -4,8 +4,9 @@ import Item from "./item"; export default class Account extends Item { databases: Databases; + ssl: boolean; - constructor(host: string, port: number) { + constructor(host: string, port: number, ssl: boolean) { super({ _rid: host, _self: "", @@ -16,13 +17,13 @@ export default class Account extends Item { writableLocations: [ { name: "South Central US", - databaseAccountEndpoint: `https://${host}:${port}/` + databaseAccountEndpoint: `http${ssl ? "s" : ""}://${host}:${port}/` } ], readableLocations: [ { name: "South Central US", - databaseAccountEndpoint: `https://${host}:${port}/` + databaseAccountEndpoint: `http${ssl ? "s" : ""}://${host}:${port}/` } ], enableMultipleWriteLocations: false, @@ -45,9 +46,17 @@ export default class Account extends Item { queryEngineConfiguration: '{"maxSqlQueryInputLength":262144,"maxJoinsPerSqlQuery":5,"maxLogicalAndPerSqlQuery":500,"maxLogicalOrPerSqlQuery":500,"maxUdfRefPerSqlQuery":10,"maxInExpressionItemsCount":16000,"queryMaxInMemorySortDocumentCount":500,"maxQueryRequestTimeoutFraction":0.9,"sqlAllowNonFiniteNumbers":false,"sqlAllowAggregateFunctions":true,"sqlAllowSubQuery":true,"sqlAllowScalarSubQuery":true,"allowNewKeywords":true,"sqlAllowLike":false,"maxSpatialQueryCells":12,"spatialMaxGeometryPointCount":256,"sqlAllowTop":true,"enableSpatialIndexing":true}' }); + this.ssl = ssl; this.databases = new Databases(this, ["/id"]); } + updateHostName(host: string){ + //when running this server in a dockerized environment, the hostname becomes the servername + //and databaseAccountEndpoint needs to point to the hostname provided by the caller + this._data.writableLocations[0].databaseAccountEndpoint = `http${this.ssl ? "s" : ""}://${host}/`; + this._data.readableLocations[0].databaseAccountEndpoint = `http${this.ssl ? "s" : ""}://${host}/`; + } + database(idOrRid: string) { return this.databases._item("/id", idOrRid, idOrRid); } diff --git a/src/handler/read-meta.ts b/src/handler/read-meta.ts index e795f45..6e63f31 100644 --- a/src/handler/read-meta.ts +++ b/src/handler/read-meta.ts @@ -1,3 +1,7 @@ import Account from "../account"; +import * as http from "http"; -export default (account: Account) => account.read(); +export default (account: Account, req: http.IncomingMessage) => { + account.updateHostName(req.headers.host); + account.read(); +} diff --git a/src/index.ts b/src/index.ts index 7158966..1acafd6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -68,7 +68,7 @@ const handleRequest = ( }); }; -const createAccount = (address: string | net.AddressInfo) => { +const createAccount = (address: string | net.AddressInfo, ssl: boolean) => { if (!address || typeof address !== "object") { throw new Error(`Unexpected address type: ${address}`); } @@ -76,7 +76,7 @@ const createAccount = (address: string | net.AddressInfo) => { const { address: host, port } = address as net.AddressInfo; const hostname = host === "0.0.0.0" || host === "::" ? "localhost" : host; - return new Account(hostname, port); + return new Account(hostname, port, ssl); }; export function createHttpServer(opts?: http.ServerOptions) { @@ -87,7 +87,7 @@ export function createHttpServer(opts?: http.ServerOptions) { handleRequest(account, req, res); }) .on("listening", () => { - account = createAccount(server.address()); + account = createAccount(server.address(), false); }); return server; @@ -110,7 +110,7 @@ export function createHttpsServer(opts?: https.ServerOptions) { handleRequest(account, req, res); }) .on("listening", () => { - account = createAccount(server.address()); + account = createAccount(server.address(), true); }); return server; From eaab8f596c258f34c5aa107f78ad883c4e1f550a Mon Sep 17 00:00:00 2001 From: LanPodder Date: Wed, 11 Jan 2023 01:20:38 +0100 Subject: [PATCH 2/4] forgot a return there --- src/handler/read-meta.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/handler/read-meta.ts b/src/handler/read-meta.ts index 6e63f31..6b35ea9 100644 --- a/src/handler/read-meta.ts +++ b/src/handler/read-meta.ts @@ -3,5 +3,5 @@ import * as http from "http"; export default (account: Account, req: http.IncomingMessage) => { account.updateHostName(req.headers.host); - account.read(); + return account.read(); } From 219eb81e13db46d6f9193e8311016a49875d25c0 Mon Sep 17 00:00:00 2001 From: LanPodder Date: Wed, 11 Jan 2023 14:38:31 +0100 Subject: [PATCH 3/4] lint --- src/account/index.ts | 4 ++-- src/handler/read-meta.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/account/index.ts b/src/account/index.ts index e04ca45..7b305c7 100644 --- a/src/account/index.ts +++ b/src/account/index.ts @@ -51,8 +51,8 @@ export default class Account extends Item { } updateHostName(host: string){ - //when running this server in a dockerized environment, the hostname becomes the servername - //and databaseAccountEndpoint needs to point to the hostname provided by the caller + // when running this server in a dockerized environment, the hostname becomes the servername + // and databaseAccountEndpoint needs to point to the hostname provided by the caller this._data.writableLocations[0].databaseAccountEndpoint = `http${this.ssl ? "s" : ""}://${host}/`; this._data.readableLocations[0].databaseAccountEndpoint = `http${this.ssl ? "s" : ""}://${host}/`; } diff --git a/src/handler/read-meta.ts b/src/handler/read-meta.ts index 6b35ea9..e619d18 100644 --- a/src/handler/read-meta.ts +++ b/src/handler/read-meta.ts @@ -1,5 +1,5 @@ -import Account from "../account"; import * as http from "http"; +import Account from "../account"; export default (account: Account, req: http.IncomingMessage) => { account.updateHostName(req.headers.host); From 16ec49f4d81d4cf23d828d3f40a7fecc1987bd3c Mon Sep 17 00:00:00 2001 From: LanPodder Date: Wed, 11 Jan 2023 14:51:45 +0100 Subject: [PATCH 4/4] lint --- src/account/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/account/index.ts b/src/account/index.ts index 7b305c7..8709873 100644 --- a/src/account/index.ts +++ b/src/account/index.ts @@ -4,6 +4,7 @@ import Item from "./item"; export default class Account extends Item { databases: Databases; + ssl: boolean; constructor(host: string, port: number, ssl: boolean) {