From 99764605fd56cc118a9b7c43a7dc0cf6387afde1 Mon Sep 17 00:00:00 2001 From: Matt Carvin <90224411+mcarvin8@users.noreply.github.com> Date: Tue, 21 Jan 2025 13:05:26 -0500 Subject: [PATCH] fix: import concurrency functions from xml-disassembler --- package.json | 2 +- pnpm-lock.yaml | 8 ++++---- src/service/deleteReassembledXML.ts | 6 ++++-- src/service/getConcurrencyThreshold.ts | 10 --------- src/service/json2xmlReassembler.ts | 6 ++++-- src/service/transform2JSON.ts | 8 +++++--- src/service/withConcurrencyLimit.ts | 22 -------------------- src/service/xml2jsonDisassembler.ts | 6 ++++-- test/main.spec.ts | 28 -------------------------- 9 files changed, 22 insertions(+), 74 deletions(-) delete mode 100644 src/service/getConcurrencyThreshold.ts delete mode 100644 src/service/withConcurrencyLimit.ts diff --git a/package.json b/package.json index b35f7fb..1d1b9df 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ }, "dependencies": { "tslib": "^2.6.2", - "xml-disassembler": "^1.3.9" + "xml-disassembler": "^1.3.10" }, "repository": { "type": "git", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5224101..ecae277 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,8 +9,8 @@ dependencies: specifier: ^2.6.2 version: 2.8.1 xml-disassembler: - specifier: ^1.3.9 - version: 1.3.9 + specifier: ^1.3.10 + version: 1.3.10 devDependencies: "@rollup/plugin-terser": @@ -4660,10 +4660,10 @@ packages: signal-exit: 3.0.7 dev: true - /xml-disassembler@1.3.9: + /xml-disassembler@1.3.10: resolution: { - integrity: sha512-/y+VjjNffBLXlLN+2hUIqUCZmEv0rjrFrmRqec9/YSrZDZWlKt/45urH+E81ZFDx4iqao8RtM7LZ17df8MRRXA==, + integrity: sha512-NqL1FdKI5c7d7fkB69VDFOMlRspX7u5D7fEpNWgXn9F8PIlyInHWYisBqSsmTk3YUZaJdM54VCMI92d+rFEYvQ==, } engines: { node: ">=18", pnpm: ">=8" } dependencies: diff --git a/src/service/deleteReassembledXML.ts b/src/service/deleteReassembledXML.ts index 7cb7880..2a3febe 100644 --- a/src/service/deleteReassembledXML.ts +++ b/src/service/deleteReassembledXML.ts @@ -2,8 +2,10 @@ import { readdir, rm } from "node:fs/promises"; import { join } from "node:path/posix"; -import { getConcurrencyThreshold } from "./getConcurrencyThreshold"; -import { withConcurrencyLimit } from "./withConcurrencyLimit"; +import { + getConcurrencyThreshold, + withConcurrencyLimit, +} from "xml-disassembler"; export async function deleteReassembledXML( disassembledPath: string, diff --git a/src/service/getConcurrencyThreshold.ts b/src/service/getConcurrencyThreshold.ts deleted file mode 100644 index 0e360f4..0000000 --- a/src/service/getConcurrencyThreshold.ts +++ /dev/null @@ -1,10 +0,0 @@ -"use strict"; - -import { availableParallelism } from "node:os"; - -export function getConcurrencyThreshold(): number { - const AVAILABLE_PARALLELISM: number = availableParallelism - ? availableParallelism() - : Infinity; - return Math.min(AVAILABLE_PARALLELISM, 6); -} diff --git a/src/service/json2xmlReassembler.ts b/src/service/json2xmlReassembler.ts index d8f9339..61e6f66 100644 --- a/src/service/json2xmlReassembler.ts +++ b/src/service/json2xmlReassembler.ts @@ -2,13 +2,15 @@ import { stat, readdir } from "node:fs/promises"; import { join } from "node:path/posix"; +import { + getConcurrencyThreshold, + withConcurrencyLimit, +} from "xml-disassembler"; import { logger } from "@src/index"; import { reassembleHandler } from "@src/service/reassembleHandler"; import { transform2XML } from "@src/service/transform2XML"; import { deleteReassembledXML } from "@src/service/deleteReassembledXML"; -import { withConcurrencyLimit } from "./withConcurrencyLimit"; -import { getConcurrencyThreshold } from "./getConcurrencyThreshold"; export class JsonToXmlReassembler { async reassemble(xmlAttributes: { diff --git a/src/service/transform2JSON.ts b/src/service/transform2JSON.ts index 30f05b6..9ffa322 100644 --- a/src/service/transform2JSON.ts +++ b/src/service/transform2JSON.ts @@ -2,11 +2,13 @@ import { readdir, rm, writeFile } from "node:fs/promises"; import { join } from "node:path/posix"; -import { parseXML } from "xml-disassembler"; +import { + parseXML, + getConcurrencyThreshold, + withConcurrencyLimit, +} from "xml-disassembler"; import { logger } from "@src/index"; -import { withConcurrencyLimit } from "./withConcurrencyLimit"; -import { getConcurrencyThreshold } from "./getConcurrencyThreshold"; export async function transform2JSON(xmlPath: string): Promise { const tasks: (() => Promise)[] = []; diff --git a/src/service/withConcurrencyLimit.ts b/src/service/withConcurrencyLimit.ts deleted file mode 100644 index 53a5ab7..0000000 --- a/src/service/withConcurrencyLimit.ts +++ /dev/null @@ -1,22 +0,0 @@ -export async function withConcurrencyLimit( - tasks: (() => Promise)[], - limit: number, -): Promise { - const results: Promise[] = []; - const executing: Promise[] = []; // Change this to Promise[] - - for (const task of tasks) { - const p = task().then((result) => { - executing.splice(executing.indexOf(p), 1); - return result; - }); - results.push(p); - executing.push(p); - - if (executing.length >= limit) { - await Promise.race(executing); // Wait for the first one to complete - } - } - - return Promise.all(results); -} diff --git a/src/service/xml2jsonDisassembler.ts b/src/service/xml2jsonDisassembler.ts index 2727e95..bf75b0d 100644 --- a/src/service/xml2jsonDisassembler.ts +++ b/src/service/xml2jsonDisassembler.ts @@ -3,12 +3,14 @@ import { existsSync } from "node:fs"; import { stat, readdir } from "node:fs/promises"; import { resolve, join, basename, dirname, extname } from "node:path/posix"; +import { + getConcurrencyThreshold, + withConcurrencyLimit, +} from "xml-disassembler"; import { logger } from "@src/index"; import { disassembleHandler } from "@src/service/disassembleHandler"; import { transform2JSON } from "@src/service/transform2JSON"; -import { withConcurrencyLimit } from "./withConcurrencyLimit"; -import { getConcurrencyThreshold } from "./getConcurrencyThreshold"; export class XmlToJsonDisassembler { async disassemble(xmlAttributes: { diff --git a/test/main.spec.ts b/test/main.spec.ts index 5e0ec28..ed8cc18 100644 --- a/test/main.spec.ts +++ b/test/main.spec.ts @@ -30,7 +30,6 @@ describe("main function", () => { afterEach(async () => { jest.restoreAllMocks(); - jest.resetModules(); }); afterAll(async () => { @@ -121,33 +120,6 @@ describe("main function", () => { await rm(fakeFile); expect(logger.error).toHaveBeenCalled(); }); - it("should return the minimum of available parallelism and 6", () => { - jest.mock("node:os", () => ({ - availableParallelism: jest.fn(() => 4), // Mock availableParallelism to return 4 - })); - const { - getConcurrencyThreshold, - } = require("../src/service/getConcurrencyThreshold"); - expect(getConcurrencyThreshold()).toBe(4); - }); - it("should return 6 if availableParallelism returns a higher value", () => { - jest.mock("node:os", () => ({ - availableParallelism: jest.fn(() => 10), // Mock availableParallelism to return 10 - })); - const { - getConcurrencyThreshold, - } = require("../src/service/getConcurrencyThreshold"); - expect(getConcurrencyThreshold()).toBe(6); - }); - it("should return 6 if availableParallelism is undefined", () => { - jest.mock("node:os", () => ({ - availableParallelism: undefined, // Simulate unavailable function - })); - const { - getConcurrencyThreshold, - } = require("../src/service/getConcurrencyThreshold"); - expect(getConcurrencyThreshold()).toBe(6); - }); // This should always be the final test it("should compare the files created in the mock directory against the baselines to confirm no changes.", async () => { await compareDirectories(baselineDir, mockDir);