-
Notifications
You must be signed in to change notification settings - Fork 624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
compat(fs): opendir
and opendirSync
#2576
Merged
kt3k
merged 15 commits into
denoland:main
from
iuioiua:compat(fs)-opendir-and-opendirSync
Sep 1, 2022
Merged
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1ffb91b
compat(fs): opendir and opendirSync
iuioiua f97f87a
Merge branch 'denoland:main' into compat(fs)-opendir-and-opendirSync
iuioiua da74ec2
bugs fixed
iuioiua 16d5ad0
Merge branch 'compat(fs)-opendir-and-opendirSync' of https://github.c…
iuioiua 879ab74
added: license headers
iuioiua 122d8d9
fixed: encoding validation
iuioiua aeb172d
fixed: encoding checks (using assertEncoding)
iuioiua 045e61f
Merge branch 'denoland:main' into compat(fs)-opendir-and-opendirSync
iuioiua cd3bb99
fixed: callback isn't called twice
iuioiua 4d9b36f
added: comments for clarity
iuioiua 138b640
fixed: fmt
iuioiua c3d52de
added: further comment
iuioiua 821f495
minor tweaks
iuioiua ad42f81
fixed: double-callback test on windows
iuioiua fd4705e
removed: node test
iuioiua File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. | ||
|
||
import Dir from "./_fs_dir.ts"; | ||
import { Buffer } from "../buffer.ts"; | ||
import { assertEncoding, getValidatedPath } from "../internal/fs/utils.mjs"; | ||
import { denoErrorToNodeError } from "../internal/errors.ts"; | ||
import { validateFunction } from "../internal/validators.mjs"; | ||
import { promisify } from "../internal/util.mjs"; | ||
|
||
type Options = { | ||
encoding?: string; | ||
bufferSize?: number; | ||
}; | ||
type Callback = (err?: Error | null, dir?: Dir) => void; | ||
|
||
function _validateFunction(callback: unknown): asserts callback is Callback { | ||
validateFunction(callback, "callback"); | ||
} | ||
|
||
function checkBufferSize(value: number, name: string) { | ||
if (!Number.isInteger(value) || value < 1 || value > 4294967295) { | ||
throw new RangeError( | ||
`The value of "${name}" is out of range. It must be >= 1 && <= 4294967295. Received ${value}`, | ||
); | ||
} | ||
} | ||
|
||
export function opendir( | ||
path: string | Buffer | URL, | ||
options: Options | Callback, | ||
callback?: Callback, | ||
) { | ||
callback = typeof options === "function" ? options : callback; | ||
_validateFunction(callback); | ||
|
||
path = getValidatedPath(path).toString(); | ||
|
||
options = typeof options === "object" ? options : {}; | ||
options = Object.assign({ | ||
encoding: "utf8", | ||
bufferSize: 32, | ||
}, options); | ||
|
||
try { | ||
assertEncoding(options.encoding); | ||
|
||
checkBufferSize(options.bufferSize!, "options.bufferSize"); | ||
|
||
/** Throws if path is invalid */ | ||
Deno.readDirSync(path); | ||
|
||
callback(null, new Dir(path)); | ||
} catch (err) { | ||
callback(denoErrorToNodeError(err as Error, { syscall: "opendir" })); | ||
} | ||
} | ||
|
||
export const opendirPromise = promisify(opendir) as ( | ||
path: string | Buffer | URL, | ||
options?: Options, | ||
) => Promise<Dir>; | ||
|
||
export function opendirSync( | ||
path: string | Buffer | URL, | ||
options: Options = { | ||
encoding: "utf8", | ||
bufferSize: 32, | ||
}, | ||
): Dir { | ||
path = getValidatedPath(path).toString(); | ||
|
||
options = Object.assign({ | ||
encoding: "utf8", | ||
bufferSize: 32, | ||
}, options); | ||
|
||
try { | ||
assertEncoding(options.encoding); | ||
|
||
checkBufferSize(options.bufferSize!, "options.bufferSize"); | ||
|
||
/** Throws if path is invalid */ | ||
Deno.readDirSync(path); | ||
|
||
return new Dir(path); | ||
} catch (err) { | ||
throw denoErrorToNodeError(err as Error, { syscall: "opendir" }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { | ||
assert, | ||
assertEquals, | ||
assertFalse, | ||
assertInstanceOf, | ||
assertThrows, | ||
} from "../../testing/asserts.ts"; | ||
import { opendir, opendirSync } from "./_fs_opendir.ts"; | ||
import { Buffer } from "../buffer.ts"; | ||
|
||
Deno.test("[node/fs] opendir()", async (t) => { | ||
const path = await Deno.makeTempDir(); | ||
const file = await Deno.makeTempFile(); | ||
|
||
await t.step( | ||
"fails if encoding is invalid", | ||
() => | ||
opendir( | ||
path, | ||
{ encoding: "invalid-encoding" }, | ||
(err) => assertInstanceOf(err, TypeError), | ||
), | ||
); | ||
|
||
await t.step( | ||
"fails if bufferSize is invalid", | ||
() => | ||
opendir( | ||
path, | ||
{ bufferSize: -1 }, | ||
(err) => assertInstanceOf(err, RangeError), | ||
), | ||
); | ||
|
||
await t.step( | ||
"fails if directory does not exist", | ||
() => | ||
opendir( | ||
"directory-that-does-not-exist", | ||
(err) => assertInstanceOf(err, Error), | ||
), | ||
); | ||
|
||
await t.step( | ||
"fails if not a directory", | ||
() => | ||
opendir( | ||
file, | ||
(err) => assertInstanceOf(err, Error), | ||
), | ||
); | ||
|
||
await t.step( | ||
"passes if path is a string", | ||
() => | ||
opendir( | ||
path, | ||
(err, dir) => { | ||
assertEquals(err, null); | ||
assert(dir); | ||
}, | ||
), | ||
); | ||
|
||
await t.step( | ||
"passes if path is a Buffer", | ||
() => | ||
opendir( | ||
Buffer.from(path), | ||
(err, dir) => { | ||
assertFalse(err); | ||
assert(dir); | ||
}, | ||
), | ||
); | ||
|
||
await t.step( | ||
"passes if path is a URL", | ||
() => | ||
opendir( | ||
new URL(`file://` + path), | ||
(err, dir) => { | ||
assertFalse(err); | ||
assert(dir); | ||
}, | ||
), | ||
); | ||
|
||
await Deno.remove(path); | ||
await Deno.remove(file); | ||
}); | ||
|
||
Deno.test("[node/fs] opendirSync()", async (t) => { | ||
const path = await Deno.makeTempDir(); | ||
const file = await Deno.makeTempFile(); | ||
|
||
await t.step("fails if encoding is invalid", () => { | ||
assertThrows( | ||
() => opendirSync(path, { encoding: "invalid-encoding" }), | ||
TypeError, | ||
); | ||
}); | ||
|
||
await t.step("fails if bufferSize is invalid", () => { | ||
assertThrows( | ||
() => opendirSync(path, { bufferSize: -1 }), | ||
RangeError, | ||
); | ||
}); | ||
|
||
await t.step("fails if directory does not exist", () => { | ||
assertThrows(() => opendirSync("directory-that-does-not-exist")); | ||
}); | ||
|
||
await t.step("fails if not a directory", () => { | ||
assertThrows(() => opendirSync(file)); | ||
}); | ||
|
||
await t.step("passes if path is a string", () => { | ||
assert(opendirSync(path)); | ||
}); | ||
|
||
await t.step("passes if path is a Buffer", () => { | ||
assert(opendirSync(Buffer.from(path))); | ||
}); | ||
|
||
await t.step("passes if path is a URL", () => { | ||
assert(opendirSync(new URL(`file://` + path))); | ||
}); | ||
|
||
await Deno.remove(path); | ||
await Deno.remove(file); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
"test-fs-append-file.js", | ||
"test-fs-chmod-mask.js", | ||
"test-fs-chmod.js", | ||
"test-fs-opendir.js", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great to have this enabled! 👍 |
||
"test-fs-rmdir-recursive.js", | ||
"test-fs-write-file.js", | ||
"test-fs-write.js", | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to have the promise version 👍