From e42fb1635a7df76172b086ca2fc831dd36670aeb Mon Sep 17 00:00:00 2001 From: Benoit Ranque Date: Wed, 18 Oct 2023 20:57:07 -0400 Subject: [PATCH] correct config server behavior --- src/configuration_server.ts | 46 ++++++++++++++++++++++++++----------- src/index.ts | 16 +++++++------ src/server.ts | 12 ++++------ 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/src/configuration_server.ts b/src/configuration_server.ts index cd443ab..ebc38e4 100644 --- a/src/configuration_server.ts +++ b/src/configuration_server.ts @@ -4,7 +4,7 @@ import JSONSchema, { JSONSchemaObject } from "@json-schema-tools/meta-schema"; import { Connector } from "./connector"; import { ConnectorError } from "./error"; -import { ErrorResponseSchema } from "./generated"; +import { ErrorResponseSchema, SchemaResponse } from "./generated"; export interface ConfigurationServerOptions { port: number; @@ -18,8 +18,12 @@ const errorResponses = { 501: ErrorResponseSchema, }; -export async function start_configuration_server( - connector: Connector, +export async function start_configuration_server< + RawConfiguration, + Configuration, + State +>( + connector: Connector, options: ConfigurationServerOptions ) { const server = Fastify({ @@ -38,7 +42,7 @@ export async function start_configuration_server( }, async function get_schema( _request: FastifyRequest - ): Promise { + ): Promise { return connector.make_empty_configuration(); } ); @@ -56,12 +60,12 @@ export async function start_configuration_server( }, async ( request: FastifyRequest<{ - Body: Configuration; + Body: RawConfiguration; }> - ): Promise => { + ): Promise => { return connector.update_configuration( - // type assetion required because Configuration is a generic parameter - request.body as Configuration + // type assertion required because Configuration is a generic parameter + request.body as RawConfiguration ); } ); @@ -91,15 +95,23 @@ export async function start_configuration_server( }, }, async ( - request: FastifyRequest<{ Body: Configuration }> - ): Promise => { - return connector.validate_raw_configuration( - // type assetion required because Configuration is a generic parameter - request.body as Configuration + request: FastifyRequest<{ Body: RawConfiguration }> + ): Promise<{ schema: SchemaResponse; resolved_configuration: string }> => { + const resolvedConfiguration = await connector.validate_raw_configuration( + // type assertion required because Configuration is a generic parameter + request.body as RawConfiguration ); + const schema = await connector.get_schema(resolvedConfiguration); + + return { + schema, + resolved_configuration: encodeJSON(resolvedConfiguration), + }; } ); + server.get("/health", async () => {}); + server.setErrorHandler(function (error, _request, reply) { if (error.validation) { reply.status(400).send({ @@ -130,3 +142,11 @@ export async function start_configuration_server( process.exit(1); } } + +function encodeJSON(payload: unknown): string { + return Buffer.from(JSON.stringify(payload), "utf8").toString("base64"); +} +// unused for now, but keeping as reference in case it is needed later. +function decodeJSON(payload: string): T { + return JSON.parse(Buffer.from(payload, "base64").toString("utf8")); +} diff --git a/src/index.ts b/src/index.ts index 7649093..a7565f8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,8 +17,8 @@ export { Connector, start_configuration_server, start_server }; * This shoudl be the entrypoint of your connector * @param connector An object that implements the Connector interface */ -export function start( - connector: Connector +export function start( + connector: Connector ) { const program = new Command(); @@ -28,8 +28,8 @@ export function start( program.parseAsync(process.argv).catch(console.error); } -export function get_serve_command( - connector: Connector +export function get_serve_command( + connector: Connector ) { return new Command("serve") .addOption( @@ -53,9 +53,11 @@ export function get_serve_command( }); } -export function get_serve_configuration_command( - connector: Connector -) { +export function get_serve_configuration_command< + RawConfiguration, + Configuration, + State +>(connector: Connector) { return new Command("configuration").addCommand( new Command("serve") .addOption( diff --git a/src/server.ts b/src/server.ts index b43c7fc..a275122 100644 --- a/src/server.ts +++ b/src/server.ts @@ -61,8 +61,10 @@ export async function start_server( connector: Connector, options: ServerOptions ) { - const configuration = await get_configuration( - options.configuration + const data = fs.readFileSync(options.configuration); + const rawConfiguration = JSON.parse(data.toString("utf8")); + const configuration = await connector.validate_raw_configuration( + rawConfiguration ); const metrics = {}; // todo @@ -216,9 +218,3 @@ export async function start_server( process.exit(1); } } - -function get_configuration(path: string): Configuration { - const data = fs.readFileSync(path); - const configuration = JSON.parse(data.toString()); - return configuration as Configuration; -}