Skip to content

Commit

Permalink
style: destruct upath module import (#1652)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs authored Sep 12, 2023
1 parent 35f4304 commit 40376be
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 54 deletions.
4 changes: 2 additions & 2 deletions scripts/license-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { init } = require("license-checker");
/** @type {string[]} */
// @ts-ignore: module is a JSON file
const copyLeftLicenses = require("spdx-copyleft");
const path = require("upath");
const { joinSafe } = require("upath");

const check = promisify(init);

Expand Down Expand Up @@ -57,7 +57,7 @@ async function checkLicenses() {
const licenses = await check({
direct: true,
production: true,
start: path.joinSafe(__dirname, ".."),
start: joinSafe(__dirname, ".."),
});

const copyLeftLicensesList = Object.keys(licenses).filter((license) =>
Expand Down
4 changes: 2 additions & 2 deletions src/config/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

const { unlink } = require("node:fs/promises");
const { glob } = require("glob");
const path = require("upath");
const { joinSafe } = require("upath");
const getConfig = require(".");

describe("Configuration", () => {
const currentEnv = { ...process.env, NODE_ENV: "development" };
const tempDir = path.joinSafe(__dirname, "../temp");
const tempDir = joinSafe(__dirname, "../temp");

afterEach(() => {
// Reset the process.env to default after each test
Expand Down
22 changes: 9 additions & 13 deletions src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { getStream } = require("file-stream-rotator");
const S = require("fluent-json-schema").default;
const { stdSerializers, stdTimeFunctions } = require("pino");
const { parse: secureParse } = require("secure-json-parse");
const path = require("upath");
const { dirname, joinSafe, normalizeSafe, normalizeTrim } = require("upath");

const coreCount = require("../utils/core-count");
const { description, license, version } = require("../../package.json");
Expand Down Expand Up @@ -44,7 +44,7 @@ function parseCorsParameter(param) {
*/
async function getConfig() {
// Directory for temporarily storing files during conversion
const tempDir = path.joinSafe(__dirname, "../temp");
const tempDir = joinSafe(__dirname, "../temp");

// Validate env variables
const env = envSchema({
Expand Down Expand Up @@ -332,7 +332,7 @@ async function getConfig() {
},
poppler: {
binPath: env.POPPLER_BINARY_PATH
? path.normalizeSafe(env.POPPLER_BINARY_PATH)
? normalizeSafe(env.POPPLER_BINARY_PATH)
: env.POPPLER_BINARY_PATH,
tempDir,
},
Expand All @@ -344,7 +344,7 @@ async function getConfig() {
},
unrtf: {
binPath: env.UNRTF_BINARY_PATH
? path.normalizeSafe(env.UNRTF_BINARY_PATH)
? normalizeSafe(env.UNRTF_BINARY_PATH)
: env.UNRTF_BINARY_PATH,
tempDir,
},
Expand All @@ -360,11 +360,9 @@ async function getConfig() {
try {
config.fastifyInit.https = {
// eslint-disable-next-line security/detect-non-literal-fs-filename
cert: await readFile(
path.normalizeTrim(env.HTTPS_SSL_CERT_PATH)
),
cert: await readFile(normalizeTrim(env.HTTPS_SSL_CERT_PATH)),
// eslint-disable-next-line security/detect-non-literal-fs-filename
key: await readFile(path.normalizeTrim(env.HTTPS_SSL_KEY_PATH)),
key: await readFile(normalizeTrim(env.HTTPS_SSL_KEY_PATH)),
};
} catch (err) {
throw new Error(
Expand All @@ -378,9 +376,7 @@ async function getConfig() {
config.fastifyInit.https = {
passphrase: env.HTTPS_PFX_PASSPHRASE,
// eslint-disable-next-line security/detect-non-literal-fs-filename
pfx: await readFile(
path.normalizeTrim(env.HTTPS_PFX_FILE_PATH)
),
pfx: await readFile(normalizeTrim(env.HTTPS_PFX_FILE_PATH)),
};
} catch (err) {
throw new Error(
Expand All @@ -396,13 +392,13 @@ async function getConfig() {

// Set Pino transport
if (env.LOG_ROTATION_FILENAME) {
const logFile = path.normalizeTrim(env.LOG_ROTATION_FILENAME);
const logFile = normalizeTrim(env.LOG_ROTATION_FILENAME);

/**
* @see {@link https://github.com/rogerc/file-stream-rotator/#options | File stream rotator options}
*/
config.fastifyInit.logger.stream = getStream({
audit_file: path.joinSafe(path.dirname(logFile), ".audit.json"),
audit_file: joinSafe(dirname(logFile), ".audit.json"),
date_format: env.LOG_ROTATION_DATE_FORMAT || "YYYY-MM-DD",
filename: logFile,
frequency: env.LOG_ROTATION_FREQUENCY || "daily",
Expand Down
22 changes: 10 additions & 12 deletions src/plugins/embed-html-images/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const { readFile } = require("node:fs/promises");
const fp = require("fastify-plugin");
const { JSDOM } = require("jsdom");
const path = require("upath");
const { extname, joinSafe, normalizeTrim } = require("upath");

/**
* @author Frazer Smith
Expand All @@ -17,7 +17,7 @@ const path = require("upath");
* files during conversion.
*/
async function plugin(server, options) {
const directory = path.normalizeTrim(options.tempDir);
const directory = normalizeTrim(options.tempDir);

/**
* @param {string} html - Valid HTML.
Expand All @@ -30,16 +30,14 @@ async function plugin(server, options) {

await Promise.all(
Array.from(images, (image) => {
const imgForm = path.extname(image.src).substring(1);

return readFile(
path.joinSafe(directory, image.src),
"base64"
).then((imageAsBase64) =>
image.setAttribute(
"src",
`data:image/${imgForm};base64,${imageAsBase64}`
)
const imgForm = extname(image.src).substring(1);

return readFile(joinSafe(directory, image.src), "base64").then(
(imageAsBase64) =>
image.setAttribute(
"src",
`data:image/${imgForm};base64,${imageAsBase64}`
)
);
})
);
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/image-to-txt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fp = require("fastify-plugin");
const { createScheduler, createWorker } = require("tesseract.js");
const path = require("upath");
const { joinSafe } = require("upath");

/**
* @author Frazer Smith
Expand All @@ -28,13 +28,13 @@ async function plugin(server, options) {
*/
const workerConfig = {
cacheMethod: "readOnly",
cachePath: path.joinSafe(__dirname, "../../.."),
corePath: path.joinSafe(
cachePath: joinSafe(__dirname, "../../.."),
corePath: joinSafe(
__dirname,
"../../../node_modules/tesseract.js-core/tesseract-core.wasm.js"
),
langPath: path.joinSafe(__dirname, "../../../ocr_lang_data"),
workerPath: path.joinSafe(
langPath: joinSafe(__dirname, "../../../ocr_lang_data"),
workerPath: joinSafe(
__dirname,
"../../../node_modules/tesseract.js/src/worker-script/node/index.js"
),
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/pdf-to-html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const fixUtf8 = require("fix-utf8");
const fp = require("fastify-plugin");
const { glob } = require("glob");
const { JSDOM } = require("jsdom");
const path = require("upath");
const { joinSafe, normalizeTrim } = require("upath");
const { Poppler } = require("node-poppler");

// Import utils
Expand All @@ -32,7 +32,7 @@ const parseString = require("../../utils/parse-string");
* Defaults to `docsmith_pdf-to-html`.
*/
async function plugin(server, options) {
const directory = path.normalizeTrim(options.tempDir);
const directory = normalizeTrim(options.tempDir);
const poppler = new Poppler(options.binPath);

// Create temp directory if missing
Expand Down Expand Up @@ -67,7 +67,7 @@ async function plugin(server, options) {
if (req.conversionResults?.docLocation) {
// Remove files from temp directory after response sent
const files = await glob(
`${path.joinSafe(
`${joinSafe(
req.conversionResults.docLocation.directory,
req.conversionResults.docLocation.id
)}*`
Expand Down Expand Up @@ -111,7 +111,7 @@ async function plugin(server, options) {

// Build temp file pattern for Poppler to use for output
const id = `${config.tempFilePrefix}_${randomUUID()}`;
const tempFile = path.joinSafe(directory, id);
const tempFile = joinSafe(directory, id);

/**
* Create document location object for use by following plugins/hooks
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/pdf-to-txt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const fixUtf8 = require("fix-utf8");
const fp = require("fastify-plugin");
const { glob } = require("glob");
const { JSDOM } = require("jsdom");
const path = require("upath");
const { joinSafe, normalizeTrim } = require("upath");
const { Poppler } = require("node-poppler");

// Import utils
Expand All @@ -31,7 +31,7 @@ const parseString = require("../../utils/parse-string");
* Defaults to `docsmith_pdf-to-txt`.
*/
async function plugin(server, options) {
const directory = path.normalizeTrim(options.tempDir);
const directory = normalizeTrim(options.tempDir);
const poppler = new Poppler(options.binPath);

// Create temp directory if missing
Expand Down Expand Up @@ -80,7 +80,7 @@ async function plugin(server, options) {
if (req.conversionResults?.docLocation) {
// Remove files from temp directory after response sent
const files = await glob(
`${path.joinSafe(
`${joinSafe(
req.conversionResults.docLocation.directory,
req.conversionResults.docLocation.id
)}*`
Expand Down Expand Up @@ -134,7 +134,7 @@ async function plugin(server, options) {
});

// Build temp file pattern for Poppler to use for output
const tempFile = path.joinSafe(directory, id);
const tempFile = joinSafe(directory, id);

/**
* Create document location object for use by following plugins/hooks
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/rtf-to-html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const fixUtf8 = require("fix-utf8");
const fp = require("fastify-plugin");
const { glob } = require("glob");
const { JSDOM } = require("jsdom");
const path = require("upath");
const { joinSafe, normalizeTrim } = require("upath");
const { UnRTF } = require("node-unrtf");

/**
Expand All @@ -29,7 +29,7 @@ const { UnRTF } = require("node-unrtf");
* Defaults to `docsmith_rtf-to-html`.
*/
async function plugin(server, options) {
const directory = path.normalizeTrim(options.tempDir);
const directory = normalizeTrim(options.tempDir);
const unrtf = new UnRTF(options.binPath);

// Create temp directory if missing
Expand All @@ -49,7 +49,7 @@ async function plugin(server, options) {
if (req.conversionResults?.docLocation) {
// Remove files from temp directory after response sent
const files = await glob(
`${path.joinSafe(
`${joinSafe(
req.conversionResults.docLocation.directory,
req.conversionResults.docLocation.id
)}*`
Expand Down Expand Up @@ -95,7 +95,7 @@ async function plugin(server, options) {

// Build temp RTF file for UnRTF to read from
const id = `${config.tempFilePrefix}_${randomUUID()}`;
const tempFile = path.joinSafe(directory, `${id}.rtf`);
const tempFile = joinSafe(directory, `${id}.rtf`);
// 0600 permissions (read/write for owner only)
await writeFile(tempFile, req.body, {
encoding: "utf8",
Expand Down Expand Up @@ -145,7 +145,7 @@ async function plugin(server, options) {
* which could cause `unlink()` to throw an ENOENT error if the file has already been removed
* by another request's call of this hook
*/
return rm(path.joinSafe(process.cwd(), src), {
return rm(joinSafe(process.cwd(), src), {
force: true,
maxRetries: 10,
recursive: true,
Expand Down
4 changes: 2 additions & 2 deletions src/routes/docs/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const path = require("upath");
const { joinSafe } = require("upath");

// Import plugins
const staticPlugin = require("@fastify/static");
Expand All @@ -22,7 +22,7 @@ async function route(server) {

// Register redoc module to allow for standalone js and map to be used in docs.html
.register(staticPlugin, {
root: path.joinSafe(
root: joinSafe(
__dirname,
"..",
"..",
Expand Down
10 changes: 5 additions & 5 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const autoLoad = require("@fastify/autoload");
const fp = require("fastify-plugin");
const path = require("upath");
const { joinSafe } = require("upath");

// Import plugins
const accepts = require("@fastify/accepts");
Expand Down Expand Up @@ -112,7 +112,7 @@ async function plugin(server, config) {

// Import and register admin routes
.register(autoLoad, {
dir: path.joinSafe(__dirname, "routes", "admin"),
dir: joinSafe(__dirname, "routes", "admin"),
options: { ...config, prefix: "admin" },
})

Expand All @@ -136,7 +136,7 @@ async function plugin(server, config) {
await securedContext
// Import and register service routes
.register(autoLoad, {
dir: path.joinSafe(__dirname, "routes"),
dir: joinSafe(__dirname, "routes"),
ignorePattern: /(?:admin|docs)/u,
options: config,
});
Expand Down Expand Up @@ -171,14 +171,14 @@ async function plugin(server, config) {

// Register static files in public
.register(staticPlugin, {
root: path.joinSafe(__dirname, "public"),
root: joinSafe(__dirname, "public"),
immutable: true,
maxAge: "365 days",
prefix: "/public/",
wildcard: false,
})
.register(autoLoad, {
dir: path.joinSafe(__dirname, "routes", "docs"),
dir: joinSafe(__dirname, "routes", "docs"),
options: { ...config, prefix: "docs" },
});
})
Expand Down

0 comments on commit 40376be

Please sign in to comment.