Skip to content

Commit

Permalink
Replace recursive readdir (#4377)
Browse files Browse the repository at this point in the history
* Replace recursive readdir

* lint
  • Loading branch information
soulgalore authored Dec 29, 2024
1 parent 09a3835 commit 398f14d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 33 deletions.
11 changes: 4 additions & 7 deletions lib/plugins/gcs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ import path from 'node:path';
import fs from 'node:fs';

import { SitespeedioPlugin } from '@sitespeed.io/plugin';
import readdir from 'recursive-readdir';

// Documentation of @google-cloud/storage: https://cloud.google.com/nodejs/docs/reference/storage/2.3.x/Bucket#upload
import { Storage } from '@google-cloud/storage';
import intel from 'intel';
import { throwIfMissing } from '../../support/util.js';
import { recursiveReaddir } from '../../support/fileUtil.js';

const log = intel.getLogger('sitespeedio.plugin.gcs');

function ignoreDirectories(file, stats) {
return stats.isDirectory();
}

async function uploadLatestFiles(dir, gcsOptions, prefix) {
const config = {
projectId: gcsOptions.projectId,
Expand All @@ -23,7 +20,7 @@ async function uploadLatestFiles(dir, gcsOptions, prefix) {
const storage = new Storage(config);
const bucket = storage.bucket(gcsOptions.bucketname);

const files = await readdir(dir, [ignoreDirectories]);
const files = await recursiveReaddir(dir, true);
const promises = [];

for (let file of files) {
Expand All @@ -33,7 +30,7 @@ async function uploadLatestFiles(dir, gcsOptions, prefix) {
}

async function upload(dir, gcsOptions, prefix) {
const files = await readdir(dir);
const files = await recursiveReaddir(dir);
const promises = [];

const storage = new Storage({
Expand Down
10 changes: 3 additions & 7 deletions lib/plugins/s3/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import path from 'node:path';
import { promises as fsPromises } from 'node:fs';

import readdir from 'recursive-readdir';
import pLimit from 'p-limit';
import intel from 'intel';
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';

import { SitespeedioPlugin } from '@sitespeed.io/plugin';
import { throwIfMissing } from '../../support/util.js';
import { recursiveReaddir } from '../../support/fileUtil.js';
import { getContentType } from './contentType.js';

const log = intel.getLogger('sitespeedio.plugin.s3');

function ignoreDirectories(file, stats) {
return stats.isDirectory();
}

async function uploadFile(file, s3Client, s3Options, prefix, baseDir) {
const stream = await fsPromises.readFile(file);
const contentType = getContentType(file);
Expand Down Expand Up @@ -89,7 +85,7 @@ export default class S3Plugin extends SitespeedioPlugin {
);

try {
const files = await readdir(baseDir);
const files = await recursiveReaddir(baseDir);
const limit = pLimit(s3Options.maxAsyncS3 || 20);
const uploadPromises = files.map(file =>
limit(() =>
Expand All @@ -108,7 +104,7 @@ export default class S3Plugin extends SitespeedioPlugin {
const rootPath = path.join(baseDir, '..');
const directoriesAsArray = rootPath.split(path.sep);
const rootName = directoriesAsArray.at(-1);
const latestFiles = await readdir(rootPath, [ignoreDirectories]);
const latestFiles = await recursiveReaddir(rootPath, true);
const latestUploadPromises = latestFiles.map(file =>
limit(() =>
uploadFile(file, this.s3Client, s3Options, rootName, rootPath)
Expand Down
8 changes: 2 additions & 6 deletions lib/plugins/scp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import fs from 'node:fs';

import { SitespeedioPlugin } from '@sitespeed.io/plugin';
import { Client } from 'node-scp';
import readdir from 'recursive-readdir';
import intel from 'intel';
import { throwIfMissing } from '../../support/util.js';
import { recursiveReaddir } from '../../support/fileUtil.js';

const log = intel.getLogger('sitespeedio.plugin.scp');

Expand Down Expand Up @@ -75,12 +75,8 @@ async function uploadFiles(files, scpOptions, prefix) {
}
}

function ignoreDirectories(file, stats) {
return stats.isDirectory();
}

async function uploadLatestFiles(dir, scpOptions, prefix) {
const files = await readdir(dir, [ignoreDirectories]);
const files = await recursiveReaddir(dir, true);

return uploadFiles(files, scpOptions, prefix);
}
Expand Down
23 changes: 23 additions & 0 deletions lib/support/fileUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { readdir } from 'node:fs/promises';
import path from 'node:path';

export async function recursiveReaddir(dir, includeDirectories = false) {
const results = [];
const entries = await readdir(dir, { withFileTypes: true });

for (const entry of entries) {
const fullPath = path.join(dir, entry.name);

if (entry.isDirectory()) {
if (includeDirectories) {
results.push(fullPath);
}
const subPaths = await recursiveReaddir(fullPath, includeDirectories);
results.push(...subPaths);
} else {
results.push(fullPath);
}
}

return results;
}
12 changes: 0 additions & 12 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@
"os-name": "6.0.0",
"p-limit": "6.1.0",
"pug": "3.0.3",
"recursive-readdir": "2.2.3",
"simplecrawler": "1.1.9",
"@sitespeed.io/plugin": "0.0.6",
"tape": "5.8.1",
Expand Down

0 comments on commit 398f14d

Please sign in to comment.