Skip to content

Commit 029d4e8

Browse files
committed
ignore unresolved databases
1 parent 7c7cabf commit 029d4e8

File tree

4 files changed

+41
-28
lines changed

4 files changed

+41
-28
lines changed

src/javascript.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type {Expression, Identifier, Node, Options, Program} from "acorn";
33
import {fileReference} from "./files.js";
44
import {findAssignments} from "./javascript/assignments.js";
55
import {findAwaits} from "./javascript/awaits.js";
6-
import {resolveDatabase} from "./javascript/databases.js";
6+
import {resolveDatabases} from "./javascript/databases.js";
77
import {findDeclarations} from "./javascript/declarations.js";
88
import {findFeatures} from "./javascript/features.js";
99
import {rewriteFetches} from "./javascript/fetches.js";
@@ -81,7 +81,7 @@ export function transpileJavaScript(input: string, options: ParseOptions): Trans
8181
...(inputs.length ? {inputs} : null),
8282
...(options.inline ? {inline: true} : null),
8383
...(node.declarations?.length ? {outputs: node.declarations.map(({name}) => name)} : null),
84-
...(databases.length ? {databases: databases.map((d) => resolveDatabase(d))} : null),
84+
...(databases.length ? {databases: resolveDatabases(databases)} : null),
8585
...(files.length ? {files} : null),
8686
body: `${node.async ? "async " : ""}(${inputs}) => {
8787
${String(output)}${node.declarations?.length ? `\nreturn {${node.declarations.map(({name}) => name)}};` : ""}

src/javascript/databases.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,21 @@ function encodeToken(payload: {name: string; exp: number}, secret: string): stri
3535
return `${Buffer.from(data).toString("base64")}.${Buffer.from(hmac).toString("base64")}`;
3636
}
3737

38+
export function resolveDatabases(
39+
databases: DatabaseReference[],
40+
{exp = Date.now() + DATABASE_TOKEN_DURATION, env = process.env} = {}
41+
): ResolvedDatabaseReference[] {
42+
const options = {exp, env};
43+
return databases.map((d) => resolveDatabase(d, options)).filter((d): d is ResolvedDatabaseReference => !!d);
44+
}
45+
3846
export function resolveDatabase(
3947
database: DatabaseReference,
4048
{exp = Date.now() + DATABASE_TOKEN_DURATION, env = process.env} = {}
41-
): DatabaseReference | ResolvedDatabaseReference {
49+
): ResolvedDatabaseReference | undefined {
4250
const {name} = database;
4351
const config = getDatabaseProxyConfig(env, name);
44-
if (!config) {
45-
console.warn(`Unable to resolve database: ${name}`);
46-
return database;
47-
}
52+
if (!config) return void console.warn(`Unable to resolve database: ${name}`);
4853
const url = new URL("http://localhost");
4954
url.protocol = config.ssl !== "disabled" ? "https:" : "http:";
5055
url.host = config.host;

test/javascript/databases-test.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
import assert from "node:assert";
22
import {createHmac} from "node:crypto";
3-
import {type ResolvedDatabaseReference, resolveDatabase} from "../../src/javascript/databases.js";
3+
import {type ResolvedDatabaseReference, resolveDatabase, resolveDatabases} from "../../src/javascript/databases.js";
44
import type {DatabaseReference} from "../../src/javascript.js";
55

6+
const exp = Date.UTC(2020, 0, 1);
7+
8+
const snow2 = {
9+
name: "snow2",
10+
type: "snowflake",
11+
host: "localhost",
12+
port: "2899",
13+
ssl: "disabled",
14+
origin: "http://127.0.0.1:3000",
15+
secret: "0249f7ba6fff3856cb9868d115dbcd6b2ef51f1e9a55fdc6f7484013b7c191c5"
16+
};
17+
18+
const env = {
19+
OBSERVABLEHQ_DB_SECRET_snow2: Buffer.from(JSON.stringify(snow2), "utf-8").toString("base64")
20+
};
21+
622
describe("resolveDatabase", () => {
7-
const exp = Date.UTC(2020, 0, 1);
8-
const snow2 = {
9-
name: "snow2",
10-
type: "snowflake",
11-
host: "localhost",
12-
port: "2899",
13-
ssl: "disabled",
14-
origin: "http://127.0.0.1:3000",
15-
secret: "0249f7ba6fff3856cb9868d115dbcd6b2ef51f1e9a55fdc6f7484013b7c191c5"
16-
};
17-
const env = {
18-
OBSERVABLEHQ_DB_SECRET_snow2: Buffer.from(JSON.stringify(snow2), "utf-8").toString("base64")
19-
};
2023
it("returns a resolved database reference", async () => {
2124
const input: DatabaseReference = {name: "snow2"};
2225
const output = resolveDatabase(input, {env, exp});
@@ -37,9 +40,18 @@ describe("resolveDatabase", () => {
3740
resolveDatabase(input, {env});
3841
assert.deepStrictEqual(input, {name: "snow2"});
3942
});
40-
it("returns an unresolved database reference when it can’t be resolved", async () => {
43+
it("returns undefined when it can’t be resolved", async () => {
4144
const input = {name: "notthere"};
42-
assert.strictEqual(resolveDatabase(input), input);
45+
assert.strictEqual(resolveDatabase(input), undefined);
4346
assert.deepStrictEqual(input, {name: "notthere"});
4447
});
4548
});
49+
50+
describe("resolveDatabases", () => {
51+
it("returns only resolved database references", async () => {
52+
const notthere: DatabaseReference = {name: "notthere"};
53+
const snow2: DatabaseReference = {name: "snow2"};
54+
const output = resolveDatabases([notthere, snow2], {env, exp});
55+
assert.deepStrictEqual<ResolvedDatabaseReference[]>(output, [resolveDatabase(snow2, {env, exp})!]);
56+
});
57+
});

test/output/databaseclient.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@
2323
"outputs": [
2424
"db"
2525
],
26-
"databases": [
27-
{
28-
"name": "dwh-local-proxy"
29-
}
30-
],
26+
"databases": [],
3127
"body": "(DatabaseClient) => {\nconst db = DatabaseClient(\"dwh-local-proxy\");\nreturn {db};\n}"
3228
}
3329
],

0 commit comments

Comments
 (0)