diff options
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/config.test.ts | 88 | ||||
| -rw-r--r-- | src/util/config.ts | 42 | ||||
| -rw-r--r-- | src/util/logger.test.ts | 6 | ||||
| -rw-r--r-- | src/util/logger.ts | 2 |
4 files changed, 79 insertions, 59 deletions
diff --git a/src/util/config.test.ts b/src/util/config.test.ts index 3f6d671..21d4041 100644 --- a/src/util/config.test.ts +++ b/src/util/config.test.ts @@ -10,18 +10,21 @@ let output = ""; jest.mock("./logger", () => { const stream = new Writable(); stream._write = (chunk, _encoding, next) => { - output = output += chunk.toString(); - next(); + output = output += chunk.toString(); + next(); }; const streamTransport = new winston.transports.Stream({ stream }); return { __esModule: true, default: () => ({ - format: winston.format.combine(winston.format.splat(), winston.format.simple()), - transports: [streamTransport] - }) - } + format: winston.format.combine( + winston.format.splat(), + winston.format.simple() + ), + transports: [streamTransport], + }), + }; }); import { @@ -29,9 +32,16 @@ import { persistTemplate, handleStartup, handleShutdown, - initializeToken + initializeToken, } from "./config"; -import { Logger, MongoClient, MongoError, ReadConcern, ReadPreference, WriteConcern } from "mongodb"; +import { + Logger, + MongoClient, + MongoError, + ReadConcern, + ReadPreference, + WriteConcern, +} from "mongodb"; import { Server } from "http"; import path from "path"; import fs from "fs"; @@ -59,7 +69,7 @@ const CommonMocks = { listenerCount: jest.fn(), rawListeners: jest.fn(), emit: jest.fn(), - eventNames: jest.fn() + eventNames: jest.fn(), }; const MongoMock = (p: Promise<void>): jest.Mock<MongoClient, void[]> => @@ -79,7 +89,11 @@ const MongoMock = (p: Promise<void>): jest.Mock<MongoClient, void[]> => compressors: [], writeConcern: new WriteConcern(), dbName: "", - metadata: {driver: {name: "", version: ""}, os: {type: "", name: "linux", architecture: "", version: ""}, platform: "linx"}, + metadata: { + driver: { name: "", version: "" }, + os: { type: "", name: "linux", architecture: "", version: "" }, + platform: "linx", + }, tls: true, toURI: jest.fn(), autoEncryption: {}, @@ -116,7 +130,7 @@ const MongoMock = (p: Promise<void>): jest.Mock<MongoClient, void[]> => autoEncrypter: undefined, readConcern: new ReadConcern("local"), writeConcern: new WriteConcern(), - db: jest.fn() + db: jest.fn(), })); const ServerMock = (mockErr: Error | undefined): jest.Mock<Server, void[]> => jest.fn<Server, void[]>(() => ({ @@ -126,7 +140,7 @@ const ServerMock = (mockErr: Error | undefined): jest.Mock<Server, void[]> => timeout: 0, headersTimeout: 0, keepAliveTimeout: 0, - close: function(c: (err?: Error | undefined) => void): Server { + close: function (c: (err?: Error | undefined) => void): Server { c(mockErr); return this; }, @@ -138,7 +152,7 @@ const ServerMock = (mockErr: Error | undefined): jest.Mock<Server, void[]> => getConnections: jest.fn(), ref: jest.fn(), requestTimeout: 3600, - unref: jest.fn() + unref: jest.fn(), })); describe("initializeToken", () => { @@ -147,7 +161,7 @@ describe("initializeToken", () => { output = ""; // Act - let result = initializeToken(); + const result = initializeToken(); // Assert expect(result).toMatch(/([a-f0-9]{8}(-[a-f0-9]{4}){4}[a-f0-9]{8})/); @@ -198,12 +212,12 @@ describe("persistTemplate", () => { const template = { inputFile: "/mnt/c/Windows/System32", outputFile: "./helloworld.txt", - context: { EXAMPLE: "this" } + context: { EXAMPLE: "this" }, } as templates.Template; const processTemplate = jest .spyOn(templates, "default") .mockImplementation( - (template: templates.Template) => new Promise(res => res(template)) + (template: templates.Template) => new Promise((res) => res(template)) ); const fsAccess = jest.spyOn(fs.promises, "access").mockResolvedValue(); @@ -220,7 +234,7 @@ describe("persistTemplate", () => { const template = { inputFile: "/mnt/c/Windows/System32", outputFile: "./helloworld.txt", - context: { EXAMPLE: "this" } + context: { EXAMPLE: "this" }, } as templates.Template; const processTemplate = jest .spyOn(templates, "default") @@ -243,7 +257,7 @@ describe("persistTemplate", () => { const template = { inputFile: "/mnt/c/Windows/System32", outputFile: "./helloworld.txt", - context: { EXAMPLE: "this" } + context: { EXAMPLE: "this" }, } as templates.Template; const processTemplate = jest .spyOn(templates, "default") @@ -270,7 +284,7 @@ describe("handleStartup", () => { const config = { hostDir: "/apple", - publicDir: "/public" + publicDir: "/public", } as EnvConfig; const confStartup = (): Promise<MongoClient> => handleStartup("", config, "localhost"); @@ -280,13 +294,15 @@ describe("handleStartup", () => { const fsAccess = jest.spyOn(fs.promises, "access").mockResolvedValue(); const pathAbsolute = jest.spyOn(path, "isAbsolute").mockReturnValue(true); const pathJoin = jest.spyOn(path, "join").mockReturnValue("path"); - const mongoClient = jest.spyOn(MongoClient, "connect").mockImplementation( - () => new Promise<MongoClient>(res => res(superClient)) - ); + const mongoClient = jest + .spyOn(MongoClient, "connect") + .mockImplementation( + () => new Promise<MongoClient>((res) => res(superClient)) + ); const processTemplate = jest .spyOn(templates, "default") .mockImplementation( - (template: templates.Template) => new Promise(res => res(template)) + (template: templates.Template) => new Promise((res) => res(template)) ); const result = await confStartup(); @@ -309,13 +325,15 @@ describe("handleStartup", () => { const fsAccess = jest.spyOn(fs.promises, "access").mockRejectedValue("boo"); const pathAbsolute = jest.spyOn(path, "isAbsolute").mockReturnValue(true); const pathJoin = jest.spyOn(path, "join").mockReturnValue("path"); - const mongoClient = jest.spyOn(MongoClient, "connect").mockImplementation( - () => new Promise<MongoClient>(res => res(superClient)) - ); + const mongoClient = jest + .spyOn(MongoClient, "connect") + .mockImplementation( + () => new Promise<MongoClient>((res) => res(superClient)) + ); const processTemplate = jest .spyOn(templates, "default") .mockImplementation( - (template: templates.Template) => new Promise(res => res(template)) + (template: templates.Template) => new Promise((res) => res(template)) ); const result = await confStartup(); @@ -338,13 +356,15 @@ describe("handleStartup", () => { const fsAccess = jest.spyOn(fs.promises, "access").mockResolvedValue(); const pathAbsolute = jest.spyOn(path, "isAbsolute").mockReturnValue(false); const pathJoin = jest.spyOn(path, "join").mockReturnValue("path"); - const mongoClient = jest.spyOn(MongoClient, "connect").mockImplementation( - () => new Promise<MongoClient>(res => res(superClient)) - ); + const mongoClient = jest + .spyOn(MongoClient, "connect") + .mockImplementation( + () => new Promise<MongoClient>((res) => res(superClient)) + ); const processTemplate = jest .spyOn(templates, "default") .mockImplementation( - (template: templates.Template) => new Promise(res => res(template)) + (template: templates.Template) => new Promise((res) => res(template)) ); const result = await confStartup(); @@ -374,7 +394,7 @@ describe("handleStartup", () => { const processTemplate = jest .spyOn(templates, "default") .mockImplementation( - (template: templates.Template) => new Promise(res => res(template)) + (template: templates.Template) => new Promise((res) => res(template)) ); const result = await confStartup(); @@ -401,7 +421,7 @@ describe("handleShutdown", () => { it("should exit gracefully without error", async () => { // Arrange - const mongo = MongoMock(new Promise(r => r()))(); + const mongo = MongoMock(new Promise((r) => r()))(); const server = ServerMock(undefined)(); // Act @@ -433,7 +453,7 @@ describe("handleShutdown", () => { it("should exit with error with Server error", async () => { // Arrange - const mongo = MongoMock(new Promise(r => r()))(); + const mongo = MongoMock(new Promise((r) => r()))(); const server = ServerMock(Error("oh noooo"))(); // Act diff --git a/src/util/config.ts b/src/util/config.ts index e83eab7..b98444c 100644 --- a/src/util/config.ts +++ b/src/util/config.ts @@ -81,7 +81,7 @@ export const handleStartup = async ( await persistTemplate({ inputFile: path.join(publicDir, "templates", "sh.template"), outputFile: path.join(hostDir, "sh"), - context: { TARGET_URL: targetUrl } + context: { TARGET_URL: targetUrl }, } as Template); await persistTemplate({ inputFile: path.join(publicDir, "templates", "index.html.template"), @@ -90,8 +90,8 @@ export const handleStartup = async ( TARGET_URL: targetUrl, CURL_HTTPS: targetUrl.includes("https") ? "--proto '=https' --tlsv1.2 " - : "" - } + : "", + }, } as Template); return mongo; @@ -101,22 +101,22 @@ export const handleStartup = async ( } }; -export const handleShutdown = (mongo: MongoClient, server: Server) => ( - signal: NodeJS.Signals -): Promise<void> => { - logger.warn("%s signal received. Closing shop.", signal); +export const handleShutdown = + (mongo: MongoClient, server: Server) => + (signal: NodeJS.Signals): Promise<void> => { + logger.warn("%s signal received. Closing shop.", signal); - return mongo - .close() - .then(() => { - logger.info("MongoDB client connection closed."); - return new Promise((res, rej) => - server.close(err => { - logger.info("Express down."); - (err ? rej : res)(err); - }) - ); - }) - .then(() => process.exit(0)) - .catch(() => process.exit(1)); -}; + return mongo + .close() + .then(() => { + logger.info("MongoDB client connection closed."); + return new Promise((res, rej) => + server.close((err) => { + logger.info("Express down."); + (err ? rej : res)(err); + }) + ); + }) + .then(() => process.exit(0)) + .catch(() => process.exit(1)); + }; diff --git a/src/util/logger.test.ts b/src/util/logger.test.ts index 3a2193d..a673bad 100644 --- a/src/util/logger.test.ts +++ b/src/util/logger.test.ts @@ -8,7 +8,7 @@ describe("Logger configurer", () => { const clazz = "important-clazz-name"; const adapter = { level: "info", - message: "test/10" + message: "test/10", }; // Act @@ -30,7 +30,7 @@ describe("Logger configurer", () => { const adapter = { level: "info", message: "%s/10", - [SPLAT]: ["test"] + [SPLAT]: ["test"], }; // Act @@ -56,6 +56,6 @@ describe("Logger configurer", () => { // Assert expect(result.transports).toBeInstanceOf(Array); expect(result.transports.length).toEqual(1); - result.transports.forEach(t => expect(t).toBeInstanceOf(Transport)); + result.transports.forEach((t) => expect(t).toBeInstanceOf(Transport)); }); }); diff --git a/src/util/logger.ts b/src/util/logger.ts index 2fc0749..d779718 100644 --- a/src/util/logger.ts +++ b/src/util/logger.ts @@ -24,5 +24,5 @@ export default ( transports: Transport[]; } => ({ format: combine(splat(), timestamp(), label({ label: clazz })), - transports: [new Console({ level: level, format: consoleFormat })] + transports: [new Console({ level: level, format: consoleFormat })], }); |
