[node] Allow setting encoding for Dirent readdir (#36092)

* Allow setting encoding for Dirent readdir

* Update promisified version of readdir
This commit is contained in:
Marc-Antoine Ouimet 2019-06-11 15:09:08 -04:00 committed by Ron Buckton
parent fa812c285d
commit d12324cc22
6 changed files with 66 additions and 12 deletions

8
types/node/fs.d.ts vendored
View File

@ -858,7 +858,7 @@ declare module "fs" {
* @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
* @param options If called with `withFileTypes: true` the result data will be an array of Dirent.
*/
function readdir(path: PathLike, options: { withFileTypes: true }, callback: (err: NodeJS.ErrnoException | null, files: Dirent[]) => void): void;
function readdir(path: PathLike, options: { encoding?: string | null; withFileTypes: true }, callback: (err: NodeJS.ErrnoException | null, files: Dirent[]) => void): void;
// NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime.
namespace readdir {
@ -888,7 +888,7 @@ declare module "fs" {
* @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
* @param options If called with `withFileTypes: true` the result data will be an array of Dirent
*/
function __promisify__(path: PathLike, options: { withFileTypes: true }): Promise<Dirent[]>;
function __promisify__(path: PathLike, options: { encoding?: string | null; withFileTypes: true }): Promise<Dirent[]>;
}
/**
@ -913,11 +913,11 @@ declare module "fs" {
function readdirSync(path: PathLike, options?: { encoding?: string | null; withFileTypes?: false } | string | null): string[] | Buffer[];
/**
* Asynchronous readdir(3) - read a directory.
* Synchronous readdir(3) - read a directory.
* @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
* @param options If called with `withFileTypes: true` the result data will be an array of Dirent.
*/
function readdirSync(path: PathLike, options: { withFileTypes: true }): Dirent[];
function readdirSync(path: PathLike, options: { encoding?: string | null; withFileTypes: true }): Dirent[];
/**
* Asynchronous close(2) - close a file descriptor.

View File

@ -109,6 +109,7 @@ import Module = require("module");
listS = fs.readdirSync('path', undefined);
const listDir: fs.Dirent[] = fs.readdirSync('path', { withFileTypes: true });
const listDir2: Buffer[] = fs.readdirSync('path', { withFileTypes: false, encoding: 'buffer' });
const listDir3: fs.Dirent[] = fs.readdirSync('path', { encoding: 'utf8', withFileTypes: true });
let listB: Buffer[];
listB = fs.readdirSync('path', { encoding: 'buffer' });
@ -120,6 +121,23 @@ import Module = require("module");
fs.readdir('path', { withFileTypes: true }, (err: NodeJS.ErrnoException | null, files: fs.Dirent[]) => {});
}
async function testPromisify() {
const rd = util.promisify(fs.readdir);
let listS: string[];
listS = await rd('path');
listS = await rd('path', 'utf8');
listS = await rd('path', null);
listS = await rd('path', undefined);
listS = await rd('path', { encoding: 'utf8' });
listS = await rd('path', { encoding: null });
listS = await rd('path', { encoding: null, withFileTypes: false });
listS = await rd('path', { encoding: 'utf8', withFileTypes: false });
const listDir: fs.Dirent[] = await rd('path', { withFileTypes: true });
const listDir2: Buffer[] = await rd('path', { withFileTypes: false, encoding: 'buffer' });
const listDir3: fs.Dirent[] = await rd('path', { encoding: 'utf8', withFileTypes: true });
}
{
fs.mkdtemp('/tmp/foo-', (err, folder) => {
console.log(folder);

View File

@ -858,7 +858,7 @@ declare module "fs" {
* @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
* @param options If called with `withFileTypes: true` the result data will be an array of Dirent.
*/
function readdir(path: PathLike, options: { withFileTypes: true }, callback: (err: NodeJS.ErrnoException | null, files: Dirent[]) => void): void;
function readdir(path: PathLike, options: { encoding?: string | null; withFileTypes: true }, callback: (err: NodeJS.ErrnoException | null, files: Dirent[]) => void): void;
// NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime.
namespace readdir {
@ -888,7 +888,7 @@ declare module "fs" {
* @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
* @param options If called with `withFileTypes: true` the result data will be an array of Dirent
*/
function __promisify__(path: PathLike, options: { withFileTypes: true }): Promise<Dirent[]>;
function __promisify__(path: PathLike, options: { encoding?: string | null; withFileTypes: true }): Promise<Dirent[]>;
}
/**
@ -913,11 +913,11 @@ declare module "fs" {
function readdirSync(path: PathLike, options?: { encoding?: string | null; withFileTypes?: false } | string | null): string[] | Buffer[];
/**
* Asynchronous readdir(3) - read a directory.
* Synchronous readdir(3) - read a directory.
* @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
* @param options If called with `withFileTypes: true` the result data will be an array of Dirent.
*/
function readdirSync(path: PathLike, options: { withFileTypes: true }): Dirent[];
function readdirSync(path: PathLike, options: { encoding?: string | null; withFileTypes: true }): Dirent[];
/**
* Asynchronous close(2) - close a file descriptor.

View File

@ -261,6 +261,7 @@ import { Buffer as ImportedBuffer, SlowBuffer as ImportedSlowBuffer, transcode,
listS = fs.readdirSync('path', undefined);
const listDir: fs.Dirent[] = fs.readdirSync('path', { withFileTypes: true });
const listDir2: Buffer[] = fs.readdirSync('path', { withFileTypes: false, encoding: 'buffer' });
const listDir3: fs.Dirent[] = fs.readdirSync('path', { encoding: 'utf8', withFileTypes: true });
let listB: Buffer[];
listB = fs.readdirSync('path', { encoding: 'buffer' });
@ -272,6 +273,23 @@ import { Buffer as ImportedBuffer, SlowBuffer as ImportedSlowBuffer, transcode,
fs.readdir('path', { withFileTypes: true }, (err: NodeJS.ErrnoException, files: fs.Dirent[]) => {});
}
async function testPromisify() {
const rd = util.promisify(fs.readdir);
let listS: string[];
listS = await rd('path');
listS = await rd('path', 'utf8');
listS = await rd('path', null);
listS = await rd('path', undefined);
listS = await rd('path', { encoding: 'utf8' });
listS = await rd('path', { encoding: null });
listS = await rd('path', { encoding: null, withFileTypes: false });
listS = await rd('path', { encoding: 'utf8', withFileTypes: false });
const listDir: fs.Dirent[] = await rd('path', { withFileTypes: true });
const listDir2: Buffer[] = await rd('path', { withFileTypes: false, encoding: 'buffer' });
const listDir3: fs.Dirent[] = await rd('path', { encoding: 'utf8', withFileTypes: true });
}
{
fs.mkdtemp('/tmp/foo-', (err, folder) => {
console.log(folder);

View File

@ -858,7 +858,7 @@ declare module "fs" {
* @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
* @param options If called with `withFileTypes: true` the result data will be an array of Dirent.
*/
function readdir(path: PathLike, options: { withFileTypes: true }, callback: (err: NodeJS.ErrnoException | null, files: Dirent[]) => void): void;
function readdir(path: PathLike, options: { encoding?: string | null; withFileTypes: true }, callback: (err: NodeJS.ErrnoException | null, files: Dirent[]) => void): void;
// NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime.
namespace readdir {
@ -888,7 +888,7 @@ declare module "fs" {
* @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
* @param options If called with `withFileTypes: true` the result data will be an array of Dirent
*/
function __promisify__(path: PathLike, options: { withFileTypes: true }): Promise<Dirent[]>;
function __promisify__(path: PathLike, options: { encoding?: string | null; withFileTypes: true }): Promise<Dirent[]>;
}
/**
@ -913,11 +913,11 @@ declare module "fs" {
function readdirSync(path: PathLike, options?: { encoding?: string | null; withFileTypes?: false } | string | null): string[] | Buffer[];
/**
* Asynchronous readdir(3) - read a directory.
* Synchronous readdir(3) - read a directory.
* @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
* @param options If called with `withFileTypes: true` the result data will be an array of Dirent.
*/
function readdirSync(path: PathLike, options: { withFileTypes: true }): Dirent[];
function readdirSync(path: PathLike, options: { encoding?: string | null; withFileTypes: true }): Dirent[];
/**
* Asynchronous close(2) - close a file descriptor.

View File

@ -109,6 +109,7 @@ import Module = require("module");
listS = fs.readdirSync('path', undefined);
const listDir: fs.Dirent[] = fs.readdirSync('path', { withFileTypes: true });
const listDir2: Buffer[] = fs.readdirSync('path', { withFileTypes: false, encoding: 'buffer' });
const listDir3: fs.Dirent[] = fs.readdirSync('path', { encoding: 'utf8', withFileTypes: true });
let listB: Buffer[];
listB = fs.readdirSync('path', { encoding: 'buffer' });
@ -120,6 +121,23 @@ import Module = require("module");
fs.readdir('path', { withFileTypes: true }, (err: NodeJS.ErrnoException | null, files: fs.Dirent[]) => {});
}
async function testPromisify() {
const rd = util.promisify(fs.readdir);
let listS: string[];
listS = await rd('path');
listS = await rd('path', 'utf8');
listS = await rd('path', null);
listS = await rd('path', undefined);
listS = await rd('path', { encoding: 'utf8' });
listS = await rd('path', { encoding: null });
listS = await rd('path', { encoding: null, withFileTypes: false });
listS = await rd('path', { encoding: 'utf8', withFileTypes: false });
const listDir: fs.Dirent[] = await rd('path', { withFileTypes: true });
const listDir2: Buffer[] = await rd('path', { withFileTypes: false, encoding: 'buffer' });
const listDir3: fs.Dirent[] = await rd('path', { encoding: 'utf8', withFileTypes: true });
}
{
fs.mkdtemp('/tmp/foo-', (err, folder) => {
console.log(folder);