New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

@kogs/utils

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kogs/utils - npm Package Compare versions

Comparing version
1.1.7
to
1.2.7
+9
-0
index.d.ts

@@ -6,3 +6,11 @@ /// <reference types="node" />

type StreamFilter = (chunk: ReadableChunk) => Promise<boolean>;
type FileFilter = (entryPath: string) => boolean;
/**
* Recursively scans the given directory and returns an array of file paths.
* @param dir - Directory to be scanned.
* @param filter - Optional filter function that returns true if the file should be included in the result, and false otherwise.
* @returns An array of file paths.
*/
export declare function collectFiles(dir: string, filter?: FileFilter): Promise<string[]>;
/**
* Creates a readable stream and pushes the given array of chunks to it.

@@ -40,2 +48,3 @@ * @param input - Array of chunks to be pushed to the stream.

declare const _default: {
collectFiles: typeof collectFiles;
arrayToStream: typeof arrayToStream;

@@ -42,0 +51,0 @@ streamToArray: typeof streamToArray;

import stream from 'node:stream';
import path from 'node:path';
import fs from 'node:fs';
/**
* Recursively scans the given directory and returns an array of file paths.
* @param dir - Directory to be scanned.
* @param filter - Optional filter function that returns true if the file should be included in the result, and false otherwise.
* @returns An array of file paths.
*/
export async function collectFiles(dir, filter) {
const entries = [];
const collect = async (dir) => {
const dirEntries = await fs.promises.readdir(dir, { withFileTypes: true });
for (const entry of dirEntries) {
const entryPath = path.join(dir, entry.name);
if (entry.isDirectory())
await collect(entryPath);
else if (!filter || filter(entryPath))
entries.push(entryPath);
}
};
await collect(dir);
return entries;
}
/**
* Creates a readable stream and pushes the given array of chunks to it.

@@ -83,2 +106,3 @@ * @param input - Array of chunks to be pushed to the stream.

export default {
collectFiles,
arrayToStream,

@@ -85,0 +109,0 @@ streamToArray,

+1
-1
{
"name": "@kogs/utils",
"version": "1.1.7",
"version": "1.2.7",
"type": "module",

@@ -5,0 +5,0 @@ "description": "A collection of standalone utility functions.",

@@ -27,2 +27,3 @@ # @kogs/utils

- [`collectFiles`](#collectfiles) - Collect all files in a directory recursively.
- [`arrayToStream`](#arraytostream) - Convert an array of values to a readable stream.

@@ -34,2 +35,25 @@ - [`streamToArray`](#streamtoarray) - Convert a readable stream to an array of values.

### collectFiles
`collectFiles(dir: string, filter?: FileFilter): Promise<string[]>`
```js
// Relevant types:
type FileFilter = (entryPath: string) => boolean;
```
This method accepts a directory path and returns a promise that resolves with an array of file paths recursively contained within the directory.
```js
const files = await collectFiles('/path/to/dir');
// files[0] = '/path/to/dir/file1.txt'
// files[1] = '/path/to/dir/file2.log'
```
If `filter` is defined, it will be used to filter the files returned by the method. The `filter` function will be passed the combined path of the directory and file name, and should return `true` if the file should be included in the result.
```js
const files = await collectFiles('/path/to/dir', e => e.endsWith('.txt'));
// files[0] = '/path/to/dir/file1.txt'
```
### arrayToStream

@@ -36,0 +60,0 @@ `arrayToStream(input: Array<ReadableChunk>, objectMode: boolean = true): stream.Readable`