Skip to content

Commit

Permalink
fix: Data leak/interference with global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
vHeemstra committed Aug 1, 2023
1 parent a91d884 commit 28d241b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
24 changes: 13 additions & 11 deletions src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ import {
Source,
} from './shared';

let pot_input_files: Vinyl[] = [];
let po_input_files: string[][] = [];

/**
* Reads and parses PO file.
*
Expand Down Expand Up @@ -83,7 +80,7 @@ export const processPOT = (
if (po_filepaths.length) {
if (isVinyl) {
if (options.returnPOT) {
pot_input_files.push(pot_file);
options._pot_input_files.push(pot_file);
}

const pot_object: PoObject = gettextParser.po.parse(pot_file.contents);
Expand All @@ -94,7 +91,7 @@ export const processPOT = (
if (err) reject(err);

if (options.returnPOT) {
pot_input_files.push(
options._pot_input_files.push(
new Vinyl({
contents: Buffer.from(pot_content),
path: pot_filepath,
Expand Down Expand Up @@ -132,8 +129,8 @@ export default (cb: AsyncCallback, options: Options) => {
}

// Reset
pot_input_files = [];
po_input_files = [];
resolvedOptions._pot_input_files = [];
resolvedOptions._po_input_files = [];

// Process all POT files
Promise.all(
Expand All @@ -152,14 +149,14 @@ export default (cb: AsyncCallback, options: Options) => {
)
.then(async (value) => {
if (!value) {
po_input_files.push([]);
resolvedOptions._po_input_files.push([]);
return [];
}

// Process all PO files
const pot_object = value[0];
const po_files: string[] = value[1];
po_input_files.push(po_files);
resolvedOptions._po_input_files.push(po_files);
const po_results = await Promise.all(
po_files.map((po_file) => {
return new Promise(
Expand Down Expand Up @@ -201,14 +198,14 @@ export default (cb: AsyncCallback, options: Options) => {
if (resolvedOptions.logResults) {
logResults(
resolvedOptions._potFilenames,
po_input_files,
resolvedOptions._po_input_files,
po_output_files,
resolvedOptions.destDir
);
}

if (resolvedOptions.returnPOT) {
cb([true, pot_input_files]);
cb([true, resolvedOptions._pot_input_files]);
return;
}

Expand All @@ -217,6 +214,11 @@ export default (cb: AsyncCallback, options: Options) => {
})
.catch((error) => {
cb([false, error.toString()]);
})
.finally(() => {
// Clear memory
resolvedOptions._pot_input_files = [];
resolvedOptions._po_input_files = [];
});

return;
Expand Down
10 changes: 6 additions & 4 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class OptionsError extends PluginError {
}
}

let cwd = './';

/**
* Validate user supplied options.
*
Expand Down Expand Up @@ -142,6 +140,8 @@ export const validateOptionsInput = (options: Options): ValidatedOptions => {
export const sanitizeAndStandardizeOptionsInput = (
options: ValidatedOptions
): StandardizedOptions => {
const cwd = resolve();

if (typeof options.potSources !== 'undefined') {
if (!isArray(options.potSources)) {
options.potSources = [options.potSources];
Expand Down Expand Up @@ -200,8 +200,6 @@ export const sanitizeAndStandardizeOptionsInput = (
* @return {object}
*/
export default (options: Options): PreparedOptions => {
cwd = resolve();

// Validate/check options
options = validateOptionsInput(options);

Expand Down Expand Up @@ -230,6 +228,10 @@ export default (options: Options): PreparedOptions => {
writeFiles: true,
destDir: '',
logResults: false,

// Internals
_pot_input_files: [],
_po_input_files: [],
};

// Merge with defaults: PreparedOptions
Expand Down
2 changes: 2 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export type StandardizedOptions = Omit<
> & {
potSources?: Source[];
poSources?: string[] | null;
_pot_input_files?: Vinyl[];
_po_input_files?: string[][];
};

export type PreparedOptions = Required<StandardizedOptions>;
Expand Down

0 comments on commit 28d241b

Please sign in to comment.