mirror of
https://github.com/denoland/deno.git
synced 2024-10-31 09:14:20 -04:00
bdc97b3976
Just some clean up reorganization around flatbuffer/minimal dispatch code. This is prep for adding a JSON dispatcher.
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
import { sendSync, sendAsync, msg, flatbuffers } from "./dispatch_flatbuffers";
|
|
import { FileInfo, FileInfoImpl } from "./file_info";
|
|
import { assert } from "./util";
|
|
|
|
function req(path: string): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
|
|
const builder = flatbuffers.createBuilder();
|
|
const path_ = builder.createString(path);
|
|
const inner = msg.ReadDir.createReadDir(builder, path_);
|
|
return [builder, msg.Any.ReadDir, inner];
|
|
}
|
|
|
|
function res(baseRes: null | msg.Base): FileInfo[] {
|
|
assert(baseRes != null);
|
|
assert(msg.Any.ReadDirRes === baseRes!.innerType());
|
|
const res = new msg.ReadDirRes();
|
|
assert(baseRes!.inner(res) != null);
|
|
const fileInfos: FileInfo[] = [];
|
|
for (let i = 0; i < res.entriesLength(); i++) {
|
|
fileInfos.push(new FileInfoImpl(res.entries(i)!));
|
|
}
|
|
return fileInfos;
|
|
}
|
|
|
|
/** Reads the directory given by path and returns a list of file info
|
|
* synchronously.
|
|
*
|
|
* const files = Deno.readDirSync("/");
|
|
*/
|
|
export function readDirSync(path: string): FileInfo[] {
|
|
return res(sendSync(...req(path)));
|
|
}
|
|
|
|
/** Reads the directory given by path and returns a list of file info.
|
|
*
|
|
* const files = await Deno.readDir("/");
|
|
*/
|
|
export async function readDir(path: string): Promise<FileInfo[]> {
|
|
return res(await sendAsync(...req(path)));
|
|
}
|