Skip to content

Commit

Permalink
fix: use async setTimeout for sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite authored Oct 30, 2024
1 parent 2ced938 commit fb1473f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/core/packager.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { setTimeout } from 'node:timers/promises';
import pMap from 'p-map';
import pc from 'picocolors';
import type { RepopackConfigMerged } from '../config/configTypes.js';
import { logger } from '../shared/logger.js';
import { getProcessConcurrency } from '../shared/processConcurrency.js';
import { sleep } from '../shared/sleep.js';
import type { RepopackProgressCallback } from '../shared/types.js';
import { collectFiles as defaultCollectFiles } from './file/fileCollect.js';
import { processFiles as defaultProcessFiles } from './file/fileProcess.js';
Expand Down Expand Up @@ -94,7 +94,7 @@ export const pack = async (
progressCallback(`Calculating metrics... (${index + 1}/${processedFiles.length}) ${pc.dim(file.path)}`);

// Sleep for a short time to prevent blocking the event loop
await sleep(1);
await setTimeout(1);

return { path: file.path, charCount, tokenCount };
},
Expand Down
35 changes: 25 additions & 10 deletions src/core/security/securityCheck.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { setTimeout } from 'node:timers/promises';
import { lintSource } from '@secretlint/core';
import { creator } from '@secretlint/secretlint-rule-preset-recommend';
import type { SecretLintCoreConfig, SecretLintCoreResult } from '@secretlint/types';
import type {
SecretLintCoreConfig,
SecretLintCoreResult,
} from '@secretlint/types';
import pMap from 'p-map';
import pc from 'picocolors';
import { logger } from '../../shared/logger.js';
import { getProcessConcurrency } from '../../shared/processConcurrency.js';
import { sleep } from '../../shared/sleep.js';
import type { RepopackProgressCallback } from '../../shared/types.js';
import type { RawFile } from '../file/fileTypes.js';

Expand All @@ -16,19 +19,27 @@ export interface SuspiciousFileResult {

export const runSecurityCheck = async (
rawFiles: RawFile[],
progressCallback: RepopackProgressCallback = () => {},
progressCallback: RepopackProgressCallback = () => {}
): Promise<SuspiciousFileResult[]> => {
const secretLintConfig = createSecretLintConfig();

const results = await pMap(
rawFiles,
async (rawFile, index) => {
const secretLintResult = await runSecretLint(rawFile.path, rawFile.content, secretLintConfig);
const secretLintResult = await runSecretLint(
rawFile.path,
rawFile.content,
secretLintConfig
);

progressCallback(`Running security check... (${index + 1}/${rawFiles.length}) ${pc.dim(rawFile.path)}`);
progressCallback(
`Running security check... (${index + 1}/${rawFiles.length}) ${pc.dim(
rawFile.path
)}`
);

// Sleep for a short time to prevent blocking the event loop
await sleep(1);
await setTimeout(1);

if (secretLintResult.messages.length > 0) {
return {
Expand All @@ -41,16 +52,18 @@ export const runSecurityCheck = async (
},
{
concurrency: getProcessConcurrency(),
},
}
);

return results.filter((result): result is SuspiciousFileResult => result != null);
return results.filter(
(result): result is SuspiciousFileResult => result != null
);
};

export const runSecretLint = async (
filePath: string,
content: string,
config: SecretLintCoreConfig,
config: SecretLintCoreConfig
): Promise<SecretLintCoreResult> => {
const result = await lintSource({
source: {
Expand All @@ -66,7 +79,9 @@ export const runSecretLint = async (

if (result.messages.length > 0) {
logger.trace(`Found ${result.messages.length} issues in ${filePath}`);
logger.trace(result.messages.map((message) => ` - ${message.message}`).join('\n'));
logger.trace(
result.messages.map((message) => ` - ${message.message}`).join('\n')
);
}

return result;
Expand Down
1 change: 0 additions & 1 deletion src/shared/sleep.ts

This file was deleted.

0 comments on commit fb1473f

Please sign in to comment.