From f4d0d1bc7edd509fdc1e225d1b629a975edf42f8 Mon Sep 17 00:00:00 2001 From: Etienne Noel Date: Fri, 8 Mar 2024 21:41:29 -0800 Subject: [PATCH] - Supporting multiple parameters in the CLI. Added support for default headers in the command line for the file http server. --- .../event-payloads/command.event-payload.ts | 2 +- .../src/mappers/command-event.mapper.spec.ts | 18 ++++++++++++++- .../cli/src/mappers/command-event.mapper.ts | 23 ++++++++++++++++++- .../commands/file-server.command-options.ts | 2 ++ .../http/src/commands/file-server.command.ts | 15 +++++++++++- packages/http/src/servers/file.http-server.ts | 7 +++++- 6 files changed, 62 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/event-payloads/command.event-payload.ts b/packages/cli/src/event-payloads/command.event-payload.ts index f167cd9a7..facdd502b 100644 --- a/packages/cli/src/event-payloads/command.event-payload.ts +++ b/packages/cli/src/event-payloads/command.event-payload.ts @@ -1,5 +1,5 @@ export class CommandEventPayload { - arguments: {[key in string]: string | number | boolean} = {}; + arguments: {[key in string]: string | number | boolean | (string)[]} = {}; constructor(public name: string, public scriptPath: string) { } diff --git a/packages/cli/src/mappers/command-event.mapper.spec.ts b/packages/cli/src/mappers/command-event.mapper.spec.ts index ecb74b2fd..22152e0a8 100644 --- a/packages/cli/src/mappers/command-event.mapper.spec.ts +++ b/packages/cli/src/mappers/command-event.mapper.spec.ts @@ -33,6 +33,21 @@ describe('Command Event Mapper', function () { expect(commandEventPayload.arguments.parameter).toBe("value") } }) + it("should properly map multiple '--' arguments with the same name into an array", async () => { + const commandEventMapper = new CommandEventMapper(); + + const mapped = await commandEventMapper.map(["node", "scriptFilePath", "name", "--parameter=value1", "--parameter=value2"], {keyname: ExecutionContextKeynameEnum.Cli, context: {}}); + + const commandEventPayload: CommandEventPayload = mapped.events[0].payload; + + expect(commandEventPayload instanceof CommandEventPayload).toBeTruthy() + expect(commandEventPayload.name).toBe("name") + expect(commandEventPayload.scriptPath).toBe("scriptFilePath") + expect(Array.isArray(commandEventPayload.arguments.parameter)).toBeTruthy() + expect((commandEventPayload.arguments.parameter as string[]).length).toBe(2) + expect((commandEventPayload.arguments.parameter as string[])[0]).toBe("value1") + expect((commandEventPayload.arguments.parameter as string[])[1]).toBe("value2") + }) it("should properly transform booleans, numbers and strings into their intended representation", async () => { @@ -45,13 +60,14 @@ describe('Command Event Mapper', function () { {args:["--parameter", "FALSE"], expectedValue: false}, {args:["--parameter", "1.05"], expectedValue: 1.05}, {args:["--parameter", "54"], expectedValue: 54}, + {args:["--parameter", "allo", "--parameter", "secondAllo"], expectedValue: ["allo", "secondAllo"]}, {args:["--parameter", "A sentence"], expectedValue: "A sentence"}, ]; for (let i = 0; i < consoleArguments.length; i++){ const args = consoleArguments[i].args; const mapped = await commandEventMapper.map(["node", "scriptFilePath", "name"].concat(...args), {keyname: ExecutionContextKeynameEnum.Cli, context: {}}); - expect(mapped.events[0].payload.arguments.parameter).toBe(consoleArguments[i].expectedValue) + expect(mapped.events[0].payload.arguments.parameter).toStrictEqual(consoleArguments[i].expectedValue) } }) }); \ No newline at end of file diff --git a/packages/cli/src/mappers/command-event.mapper.ts b/packages/cli/src/mappers/command-event.mapper.ts index 467f06916..b911f1992 100644 --- a/packages/cli/src/mappers/command-event.mapper.ts +++ b/packages/cli/src/mappers/command-event.mapper.ts @@ -53,7 +53,19 @@ export class CommandEventMapper implements EventMapperInterface { + const defaultHeaders: {[key in string]: string} = {}; + + if(Array.isArray(args.header)) { + for(const header of args.header) { + const [key, value] = header.split(":"); + defaultHeaders[key] = value; + } + } else if(args.header) { + const [key, value] = args.header.split(":"); + defaultHeaders[key] = value; + } + await this.fileHttpServer.start(args.directory ?? "./", args.port, args.address, (port, address) => { this.consoleManager.writeLine(`Pristine HTTP File server listening on: '${address}:${port}'`); }, (req) => { this.consoleManager.writeLine(`Request received: ${req.url}`); - }); + }, + defaultHeaders); return ExitCodeEnum.Success; } diff --git a/packages/http/src/servers/file.http-server.ts b/packages/http/src/servers/file.http-server.ts index 1c1bf2af7..54ba6aa6a 100644 --- a/packages/http/src/servers/file.http-server.ts +++ b/packages/http/src/servers/file.http-server.ts @@ -25,7 +25,7 @@ export class FileHttpServer { return 0; } - async start(directory: string, port?: number, address?: string, listeningCallback?: (port?: number, address?: string) => void, requestCallback?:(req: IncomingMessage) => void) { + async start(directory: string, port?: number, address?: string, listeningCallback?: (port?: number, address?: string) => void, requestCallback?:(req: IncomingMessage) => void, defaultHeaders: {[id in string]: string} = {}) { port = port ?? this.getPort(); address = address ?? this.address; @@ -146,6 +146,11 @@ export class FileHttpServer { } else { // if the file is found, set Content-type and send data res.setHeader('Content-type', map[ext] ?? 'text/plain' ); + + for (const key in defaultHeaders) { + res.setHeader(key, defaultHeaders[key]); + } + res.end(data); } });