From 5348ef9379526cab59091499e112ae7022e9ef03 Mon Sep 17 00:00:00 2001 From: Matt Carvin <90224411+mcarvin8@users.noreply.github.com> Date: Tue, 21 Jan 2025 13:09:27 -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/transform2YAML.ts | 8 +++++--- src/service/withConcurrencyLimit.ts | 22 -------------------- src/service/xml2yamlDisassembler.ts | 6 ++++-- src/service/yaml2xmlReassembler.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 8374065..9cfef47 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", "yaml": "^2.7.0" }, "repository": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cc11161..0410b94 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 yaml: specifier: ^2.7.0 version: 2.7.0 @@ -4608,10 +4608,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..ae93dc1 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 { + withConcurrencyLimit, + getConcurrencyThreshold, +} 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/transform2YAML.ts b/src/service/transform2YAML.ts index 9ad34b9..795362a 100644 --- a/src/service/transform2YAML.ts +++ b/src/service/transform2YAML.ts @@ -3,11 +3,13 @@ import { readdir, rm, writeFile } from "node:fs/promises"; import { join } from "node:path/posix"; import { stringify } from "yaml"; -import { parseXML } from "xml-disassembler"; +import { + parseXML, + withConcurrencyLimit, + getConcurrencyThreshold, +} from "xml-disassembler"; import { logger } from "@src/index"; -import { withConcurrencyLimit } from "./withConcurrencyLimit"; -import { getConcurrencyThreshold } from "./getConcurrencyThreshold"; export async function transform2YAML(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/xml2yamlDisassembler.ts b/src/service/xml2yamlDisassembler.ts index 6be7ac5..8115e76 100644 --- a/src/service/xml2yamlDisassembler.ts +++ b/src/service/xml2yamlDisassembler.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 { + withConcurrencyLimit, + getConcurrencyThreshold, +} from "xml-disassembler"; import { logger } from "@src/index"; import { disassembleHandler } from "@src/service/disassembleHandler"; import { transform2YAML } from "@src/service/transform2YAML"; -import { withConcurrencyLimit } from "./withConcurrencyLimit"; -import { getConcurrencyThreshold } from "./getConcurrencyThreshold"; export class XmlToYamlDisassembler { async disassemble(xmlAttributes: { diff --git a/src/service/yaml2xmlReassembler.ts b/src/service/yaml2xmlReassembler.ts index 249c709..fd6b5d3 100644 --- a/src/service/yaml2xmlReassembler.ts +++ b/src/service/yaml2xmlReassembler.ts @@ -2,13 +2,15 @@ import { stat, readdir } from "node:fs/promises"; import { join } from "node:path/posix"; +import { + withConcurrencyLimit, + getConcurrencyThreshold, +} 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 YamlToXmlReassembler { async reassemble(xmlAttributes: { diff --git a/test/main.spec.ts b/test/main.spec.ts index f533dd1..5912d1e 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 () => { @@ -120,33 +119,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);