-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # typegate/deno.lock # typegate/import_map.json # typegate/src/typegate/mod.ts # typegate/src/typegraph/types.ts # typegate/tests/utils/mod.ts # typegate/tests/utils/test.ts
- Loading branch information
Showing
37 changed files
with
1,695 additions
and
341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,7 +83,7 @@ jobs: | |
pnpm install --frozen-lockfile --recursive | ||
cd .. | ||
deno cache --import-map typegate/import_map.json typegate/src/main.ts typegate/tests/utils/mod.ts | ||
- uses: pre-commit/[email protected].0 | ||
- uses: pre-commit/[email protected].1 | ||
|
||
test-website: | ||
needs: changes | ||
|
@@ -243,7 +243,7 @@ jobs: | |
ports: | ||
- 6379:6379 | ||
options: >- | ||
--health-cmd "redis-cli ping" | ||
--health-cmd "redis-cli -a password ping" | ||
--health-interval 10s | ||
--health-timeout 5s | ||
--health-retries 5 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
"zod": "https://deno.land/x/[email protected]/mod.ts", | ||
"monads": "https://deno.land/x/[email protected]/mod.ts", | ||
"jwt": "https://deno.land/x/[email protected]/mod.ts", | ||
"redis": "https://deno.land/x/redis@v0.31.0/mod.ts", | ||
"redis": "https://deno.land/x/redis@v0.32.1/mod.ts", | ||
"oauth2_client": "https://deno.land/x/[email protected]/mod.ts", | ||
"test/mock_fetch": "https://deno.land/x/[email protected]/mod.ts", | ||
"levenshtein": "https://deno.land/x/[email protected]/mod.ts", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Copyright Metatype OÜ, licensed under the Elastic License 2.0. | ||
// SPDX-License-Identifier: Elastic-2.0 | ||
|
||
import { z } from "zod"; | ||
import { configOrExit, zBooleanString } from "../log.ts"; | ||
import { RedisConnectOptions } from "redis"; | ||
import { S3ClientConfig } from "aws-sdk/client-s3"; | ||
import { mapKeys } from "std/collections/map_keys.ts"; | ||
import { parse } from "std/flags/mod.ts"; | ||
|
||
export const syncConfigSchemaNaked = { | ||
sync_redis_url: z.string().optional().transform((s) => { | ||
if (s == undefined) return undefined; | ||
const url = new URL(s); | ||
if (url.password === "") { | ||
url.password = Deno.env.get("SYNC_REDIS_PASSWORD") ?? ""; | ||
} | ||
return url; | ||
}), | ||
sync_s3_host: z.string().optional().transform((s) => { | ||
if (s == undefined) return undefined; | ||
return new URL(s); | ||
}), | ||
sync_s3_region: z.string().optional(), | ||
sync_s3_bucket: z.string().optional(), | ||
sync_s3_access_key: z.string().optional(), | ||
sync_s3_secret_key: z.string().optional(), | ||
sync_s3_path_style: zBooleanString.optional(), | ||
}; | ||
|
||
export const syncConfigSchema = z.object(syncConfigSchemaNaked); | ||
|
||
export type SyncConfigRaw = Required<z.output<typeof syncConfigSchema>>; | ||
|
||
export function validateSyncConfig( | ||
config: z.output<typeof syncConfigSchema>, | ||
): SyncConfigRaw | null { | ||
const syncVars = new Set( | ||
Object.keys(config).filter((key) => key.startsWith("sync_")), | ||
); | ||
|
||
if (syncVars.size === 0) { | ||
return null; | ||
} | ||
|
||
const missingVars = Object.keys(syncConfigSchemaNaked).filter( | ||
(key) => { | ||
const value = config[key as keyof typeof config]; | ||
if (value != undefined) return false; | ||
|
||
// not required - set to default | ||
if (key === "sync_s3_path_style") { | ||
config.sync_s3_path_style = false; | ||
return false; | ||
} | ||
return true; | ||
}, | ||
).map((key) => key.toUpperCase()); | ||
|
||
if (missingVars.length > 0) { | ||
const missingVarsStr = missingVars.join(", "); | ||
const msg = `Environment variables required for sync: ${missingVarsStr}.`; | ||
const suggestion = | ||
"Make sure to set these variables or set SYNC_ENABLED=false."; | ||
throw new Error(`${msg}\n${suggestion}`); | ||
} | ||
|
||
const finalConfig = config as SyncConfigRaw; | ||
return finalConfig; | ||
} | ||
|
||
export type SyncConfig = { | ||
redis: RedisConnectOptions; | ||
s3: S3ClientConfig; | ||
s3Bucket: string; | ||
}; | ||
|
||
export function syncConfigFromRaw( | ||
config: SyncConfigRaw | null, | ||
): SyncConfig | null { | ||
if (config == null) return null; | ||
|
||
const redisDbStr = config.sync_redis_url.pathname.substring(1); | ||
const redisDb = parseInt(redisDbStr, 10); | ||
if (isNaN(redisDb)) { | ||
throw new Error(`Invalid redis db number: '${redisDbStr}'`); | ||
} | ||
|
||
return { | ||
redis: { | ||
hostname: config.sync_redis_url.hostname, | ||
port: config.sync_redis_url.port, | ||
...config.sync_redis_url.password.length > 0 | ||
? { password: config.sync_redis_url.password } | ||
: {}, | ||
db: parseInt(config.sync_redis_url.pathname.substring(1), 10), | ||
}, | ||
s3: { | ||
endpoint: config.sync_s3_host.href, | ||
region: config.sync_s3_region, | ||
credentials: { | ||
accessKeyId: config.sync_s3_access_key, | ||
secretAccessKey: config.sync_s3_secret_key, | ||
}, | ||
forcePathStyle: config.sync_s3_path_style, | ||
}, | ||
s3Bucket: config.sync_s3_bucket, | ||
}; | ||
} | ||
|
||
export type ConfigSource = "vars" | "args"; | ||
|
||
export async function syncConfigFromEnv( | ||
sources: ConfigSource[], | ||
): Promise<SyncConfig | null> { | ||
const rawObjects = sources.map((source) => { | ||
switch (source) { | ||
case "vars": | ||
return mapKeys( | ||
Deno.env.toObject(), | ||
(k: string) => k.toLowerCase(), | ||
); | ||
case "args": | ||
return parse(Deno.args) as Record<string, unknown>; | ||
} | ||
}); | ||
const syncConfigRaw = await configOrExit(rawObjects, syncConfigSchemaNaked); | ||
|
||
return syncConfigFromRaw(validateSyncConfig(syncConfigRaw)); | ||
} |
Oops, something went wrong.