mirror of
https://github.com/directus/directus.git
synced 2026-02-05 00:14:57 -05:00
* Declare return types on functions And a very few other type related minor fixes * Minor syntax fixes * Remove unnecessary escape chars in regexes * Remove unnecessary awaits * Replace deprecated req.connection with req.socket * Replace deprecated upload with uploadOne * Remove unnecessary eslint-disable-next-line comments * Comment empty functions / catch or finally clauses * Fix irregular whitespaces * Add missing returns (null) * Remove unreachable code * A few logical fixes * Remove / Handle non-null assertions which are certainly unnecessary (e.g. in tests)
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { promisify } from 'util';
|
|
import { pipeline as nodePipeline } from 'stream';
|
|
import Storage from './Storage';
|
|
|
|
/**
|
|
* Returns a boolean indication if stream param
|
|
* is a readable stream or not.
|
|
*/
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
export function isReadableStream(stream: any): stream is NodeJS.ReadableStream {
|
|
return (
|
|
stream !== null &&
|
|
typeof stream === 'object' &&
|
|
typeof stream.pipe === 'function' &&
|
|
typeof stream._read === 'function' &&
|
|
typeof stream._readableState === 'object' &&
|
|
stream.readable !== false
|
|
);
|
|
}
|
|
|
|
export const pipeline = promisify(nodePipeline);
|
|
|
|
export async function streamToString(stream: NodeJS.ReadableStream): Promise<string> {
|
|
const chunks: string[] = [];
|
|
stream.setEncoding('utf-8');
|
|
for await (const chunk of stream) {
|
|
chunks.push(chunk as string);
|
|
}
|
|
return chunks.join('');
|
|
}
|
|
|
|
export async function getFlatList(storage: Storage, prefix?: string): Promise<string[]> {
|
|
const result: string[] = [];
|
|
for await (const file of storage.flatList(prefix)) {
|
|
result.push(file.path);
|
|
}
|
|
return result;
|
|
}
|