Skip to content

Commit

Permalink
Build from edgedb-js 4841882625cda365e0cd3629b6537de18ce83df6
Browse files Browse the repository at this point in the history
  • Loading branch information
edgedb-ci committed Mar 1, 2023
1 parent cb5319a commit c306d2d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 26 deletions.
4 changes: 4 additions & 0 deletions _src/adapter.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export async function readFileUtf8(...pathParts: string[]): Promise<string> {
return await Deno.readTextFile(path.join(...pathParts));
}

export function hasFSReadPermission(): boolean {
return Deno.permissions.querySync({name: "read"}).state === "granted";
}

export async function readDir(path: string) {
try {
const files: string[] = [];
Expand Down
13 changes: 13 additions & 0 deletions _src/adapter.shared.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,16 @@ export async function HMAC(
)
);
}

export function getEnv(envName: string, required = false): string | undefined {
if (!required) {
const state = Deno.permissions.querySync({
name: "env",
variable: envName
}).state;
if (state !== "granted") {
return undefined;
}
}
return Deno.env.get(envName);
}
16 changes: 14 additions & 2 deletions _src/conUtils.server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import {process} from "./globals.deno.ts";

import {path, fs, readFileUtf8, exists, hashSHA1toHex} from "./adapter.deno.ts";
import {
path,
fs,
readFileUtf8,
exists,
hashSHA1toHex,
hasFSReadPermission
} from "./adapter.deno.ts";
import * as platform from "./platform.ts";
import {getConnectArgumentsParser} from "./conUtils.ts";

const projectDirCache = new Map<string, string | null>();

async function findProjectDir(): Promise<string | null> {
async function findProjectDir(
required: boolean = true
): Promise<string | null> {
if (!required && !hasFSReadPermission()) {
return null;
}
const workingDir = process.cwd();

if (projectDirCache.has(workingDir)) {
Expand Down
45 changes: 22 additions & 23 deletions _src/conUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
* limitations under the License.
*/

import {process} from "./globals.deno.ts";

import * as errors from "./errors/index.ts";
import {
Credentials,
getCredentialsPath,
readCredentialsFile,
validateCredentials
} from "./credentials.ts";
import {getEnv} from "./adapter.shared.deno.ts";
import {Duration, parseHumanDurationString} from "./datatypes/datetime.ts";
import {checkValidEdgeDBDuration} from "./codecs/datetime.ts";
import {InterfaceError} from "./errors/index.ts";
Expand All @@ -46,7 +45,7 @@ interface PartiallyNormalizedConfig {
connectionParams: ResolvedConnectConfig;

// true if the program is run in a directory with `edgedb.toml`
inProject: boolean;
inProject: () => Promise<boolean>;
// true if the connection params were initialized from a project
fromProject: boolean;
// true if any of the connection params were sourced from environment
Expand Down Expand Up @@ -80,7 +79,7 @@ export interface ConnectConfig {
}

export interface ServerUtils {
findProjectDir: () => Promise<string | null>;
findProjectDir: (required?: boolean) => Promise<string | null>;
findStashPath: (projectDir: string) => Promise<string>;
readFileUtf8: (...path: string[]) => Promise<string>;
searchConfigDir: (...configPath: string[]) => Promise<string>;
Expand Down Expand Up @@ -261,7 +260,7 @@ export class ResolvedConnectConfig {
.join(", ")}`
);
}
const clientSecurity = process.env.EDGEDB_CLIENT_SECURITY;
const clientSecurity = getEnv("EDGEDB_CLIENT_SECURITY");
if (clientSecurity !== undefined) {
if (
!["default", "insecure_dev_mode", "strict"].includes(
Expand Down Expand Up @@ -485,7 +484,6 @@ async function parseConnectDsnAndArgs(
const resolvedConfig = new ResolvedConnectConfig();
let fromEnv = false;
let fromProject = false;
const projectDir = await serverUtils?.findProjectDir();

const [dsn, instanceName]: [string | undefined, string | undefined] =
config.instanceName == null &&
Expand Down Expand Up @@ -542,7 +540,7 @@ async function parseConnectDsnAndArgs(
if (!hasCompoundOptions) {
// resolve config from env vars

let port: string | undefined = process.env.EDGEDB_PORT;
let port: string | undefined = getEnv("EDGEDB_PORT");
if (resolvedConfig._port === null && port?.startsWith("tcp://")) {
// EDGEDB_PORT is set by 'docker --link' so ignore and warn
// tslint:disable-next-line: no-console
Expand All @@ -556,21 +554,21 @@ async function parseConnectDsnAndArgs(
await resolveConfigOptions(
resolvedConfig,
{
dsn: process.env.EDGEDB_DSN,
instanceName: process.env.EDGEDB_INSTANCE,
credentials: process.env.EDGEDB_CREDENTIALS,
credentialsFile: process.env.EDGEDB_CREDENTIALS_FILE,
host: process.env.EDGEDB_HOST,
dsn: getEnv("EDGEDB_DSN"),
instanceName: getEnv("EDGEDB_INSTANCE"),
credentials: getEnv("EDGEDB_CREDENTIALS"),
credentialsFile: getEnv("EDGEDB_CREDENTIALS_FILE"),
host: getEnv("EDGEDB_HOST"),
port,
database: process.env.EDGEDB_DATABASE,
user: process.env.EDGEDB_USER,
password: process.env.EDGEDB_PASSWORD,
secretKey: process.env.EDGEDB_SECRET_KEY,
cloudProfile: process.env.EDGEDB_CLOUD_PROFILE,
tlsCA: process.env.EDGEDB_TLS_CA,
tlsCAFile: process.env.EDGEDB_TLS_CA_FILE,
tlsSecurity: process.env.EDGEDB_CLIENT_TLS_SECURITY,
waitUntilAvailable: process.env.EDGEDB_WAIT_UNTIL_AVAILABLE
database: getEnv("EDGEDB_DATABASE"),
user: getEnv("EDGEDB_USER"),
password: getEnv("EDGEDB_PASSWORD"),
secretKey: getEnv("EDGEDB_SECRET_KEY"),
cloudProfile: getEnv("EDGEDB_CLOUD_PROFILE"),
tlsCA: getEnv("EDGEDB_TLS_CA"),
tlsCAFile: getEnv("EDGEDB_TLS_CA_FILE"),
tlsSecurity: getEnv("EDGEDB_CLIENT_TLS_SECURITY"),
waitUntilAvailable: getEnv("EDGEDB_WAIT_UNTIL_AVAILABLE")
},
{
dsn: `'EDGEDB_DSN' environment variable`,
Expand Down Expand Up @@ -605,6 +603,7 @@ async function parseConnectDsnAndArgs(
"(or edge runtime) environment"
);
}
const projectDir = await serverUtils?.findProjectDir();
if (!projectDir) {
throw new errors.ClientConnectionError(
"no 'edgedb.toml' found and no connection options specified" +
Expand Down Expand Up @@ -647,7 +646,7 @@ async function parseConnectDsnAndArgs(

return {
connectionParams: resolvedConfig,
inProject: !!projectDir,
inProject: async () => (await serverUtils?.findProjectDir(false)) != null,
fromEnv,
fromProject
};
Expand Down Expand Up @@ -908,7 +907,7 @@ async function parseDSNIntoConfig(
if (param === null) {
const env = searchParams.get(`${paramName}_env`);
if (env != null) {
param = process.env[env] ?? null;
param = getEnv(env, true) ?? null;
if (param === null) {
throw new InterfaceError(
`'${paramName}_env' environment variable '${env}' doesn't exist`
Expand Down
6 changes: 5 additions & 1 deletion _src/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ export async function retryingConnect(
e
];

if (config.inProject && !config.fromProject && !config.fromEnv) {
if (
!config.fromProject &&
!config.fromEnv &&
(await config.inProject())
) {
logMsg.push(
`\n\n\n` +
`Hint: it looks like the program is running from a ` +
Expand Down

0 comments on commit c306d2d

Please sign in to comment.