diff --git a/.semaphore/semaphore.yml b/.semaphore/semaphore.yml index 70b8507..a0ce4c1 100644 --- a/.semaphore/semaphore.yml +++ b/.semaphore/semaphore.yml @@ -8,7 +8,7 @@ blocks: - name: Test task: jobs: - - name: Test + - name: Test (JWT) commands: - checkout - sem-version node 12 @@ -17,5 +17,14 @@ blocks: - cache store - npm run build --if-present - npm test + - name: Test (Repl Identity) + commands: + - checkout + - sem-version node 12 + - cache restore + - npm install + - cache store + - npm run build --if-present + - USE_FILE=1 npm test secrets: - name: replit-database diff --git a/index.js b/index.js index 81a2b69..630befb 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,7 @@ const fetch = require("node-fetch"); +const fs = require("fs"); + +const replitDBFilename = "/tmp/replitdb" class Client { /** @@ -7,7 +10,15 @@ class Client { */ constructor(key) { if (key) this.key = key; - else this.key = process.env.REPLIT_DB_URL; + else { + // Try to read string from file, fall back to + // environment variable if it doesn't exist + try { + this.key = fs.readFileSync(replitDBFilename, "utf8"); + } catch (err) { + this.key = process.env.REPLIT_DB_URL; + } + } } // Native Functions diff --git a/index.test.js b/index.test.js index 4958e2c..622fe93 100644 --- a/index.test.js +++ b/index.test.js @@ -4,14 +4,15 @@ const Client = require("./index"); let client; beforeAll(async () => { - const pass = process.env.JWT_PASSWORD; - const resp = await fetch("https://database-test-jwt.util.repl.co", { + const pass = process.env.USE_FILE ? process.env.RIDT_PASSWORD : process.env.JWT_PASSWORD; + const url = process.env.USE_FILE ? "https://database-test-ridt.util.repl.co" : "https://database-test-jwt.util.repl.co"; + const resp = await fetch(url, { headers: { Authorization: "Basic " + btoa("test:" + pass), }, }); - const url = await resp.text(); - client = new Client(url); + const token = await resp.text(); + client = new Client(token); await client.empty(); });