2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-06-27 02:18:22 -04:00
|
|
|
|
|
|
|
// TODO(petamoriken): enable prefer-primordials for node polyfills
|
|
|
|
// deno-lint-ignore-file prefer-primordials
|
|
|
|
|
2024-01-26 14:04:07 -05:00
|
|
|
import { core } from "ext:core/mod.js";
|
|
|
|
const { internalRidSymbol } = core;
|
2023-02-14 11:38:45 -05:00
|
|
|
import {
|
|
|
|
O_APPEND,
|
|
|
|
O_CREAT,
|
|
|
|
O_EXCL,
|
|
|
|
O_RDWR,
|
|
|
|
O_TRUNC,
|
|
|
|
O_WRONLY,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_node/_fs/_fs_constants.ts";
|
|
|
|
import { getOpenOptions } from "ext:deno_node/_fs/_fs_common.ts";
|
|
|
|
import { parseFileMode } from "ext:deno_node/internal/validators.mjs";
|
|
|
|
import { ERR_INVALID_ARG_TYPE } from "ext:deno_node/internal/errors.ts";
|
|
|
|
import { getValidatedPath } from "ext:deno_node/internal/fs/utils.mjs";
|
feat(node_compat): Added base implementation of FileHandle (#19294)
<!--
Before submitting a PR, please read https://deno.com/manual/contributing
1. Give the PR a descriptive title.
Examples of good title:
- fix(std/http): Fix race condition in server
- docs(console): Update docstrings
- feat(doc): Handle nested reexports
Examples of bad title:
- fix #7123
- update docs
- fix bugs
2. Ensure there is a related issue and it is referenced in the PR text.
3. Ensure there are tests that cover the changes.
4. Ensure `cargo test` passes.
5. Ensure `./tools/format.js` passes without changing files.
6. Ensure `./tools/lint.js` passes.
7. Open as a draft PR if your work is still in progress. The CI won't
run
all steps, but you can add '[ci]' to a commit message to force it to.
8. If you would like to run the benchmarks on the CI, add the 'ci-bench'
label.
-->
## WHY
ref: https://github.com/denoland/deno/issues/19165
Node's fs/promises includes a FileHandle class, but deno does not. The
open function in Node's fs/promises returns a FileHandle, which provides
an IO interface to the file. However, deno's open function returns a
resource id.
### deno
```js
> const fs = await import("node:fs/promises");
undefined
> const file3 = await fs.open("./README.md");
undefined
> file3
3
> file3.read
undefined
Node:
```
### Node
```js
> const fs = await import("fs/promises");
undefined
> const file3 = await fs.open("./tests/e2e_unit/testdata/file.txt");
undefined
> file3
FileHandle {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
close: [Function: close],
[Symbol(kCapture)]: false,
[Symbol(kHandle)]: FileHandle {},
[Symbol(kFd)]: 24,
[Symbol(kRefs)]: 1,
[Symbol(kClosePromise)]: null
}
> file3.read
[Function: read]
```
To be compatible with Node, deno's open function should also return a
FileHandle.
## WHAT
I have implemented the first step in adding a FileHandle.
- Changed the return value of the open function to a FileHandle object
- Implemented the readFile method in FileHandle
- Add test code
## What to do next
This PR is the first step in adding a FileHandle, and there are things
that should be done next.
- Add functionality equivalent to Node's FileHandle to FileHandle
(currently there is only readFile)
---------
Co-authored-by: Matt Mastracci <matthew@mastracci.com>
2023-06-02 10:28:05 -04:00
|
|
|
import { FileHandle } from "ext:deno_node/internal/fs/handle.ts";
|
2023-07-02 14:19:30 -04:00
|
|
|
import type { Buffer } from "node:buffer";
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
function existsSync(filePath: string | URL): boolean {
|
|
|
|
try {
|
|
|
|
Deno.lstatSync(filePath);
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof Deno.errors.NotFound) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const FLAGS_AX = O_APPEND | O_CREAT | O_WRONLY | O_EXCL;
|
|
|
|
const FLAGS_AX_PLUS = O_APPEND | O_CREAT | O_RDWR | O_EXCL;
|
|
|
|
const FLAGS_WX = O_TRUNC | O_CREAT | O_WRONLY | O_EXCL;
|
|
|
|
const FLAGS_WX_PLUS = O_TRUNC | O_CREAT | O_RDWR | O_EXCL;
|
|
|
|
|
|
|
|
export type openFlags =
|
|
|
|
| "a"
|
|
|
|
| "ax"
|
|
|
|
| "a+"
|
|
|
|
| "ax+"
|
|
|
|
| "as"
|
|
|
|
| "as+"
|
|
|
|
| "r"
|
|
|
|
| "r+"
|
|
|
|
| "rs+"
|
|
|
|
| "w"
|
|
|
|
| "wx"
|
|
|
|
| "w+"
|
|
|
|
| "wx+"
|
|
|
|
| number;
|
|
|
|
|
|
|
|
type openCallback = (err: Error | null, fd: number) => void;
|
|
|
|
|
|
|
|
function convertFlagAndModeToOptions(
|
|
|
|
flag?: openFlags,
|
|
|
|
mode?: number,
|
|
|
|
): Deno.OpenOptions | undefined {
|
2023-10-22 04:02:55 -04:00
|
|
|
if (flag === undefined && mode === undefined) return undefined;
|
|
|
|
if (flag === undefined && mode) return { mode };
|
2023-02-14 11:38:45 -05:00
|
|
|
return { ...getOpenOptions(flag), mode };
|
|
|
|
}
|
|
|
|
|
|
|
|
export function open(path: string | Buffer | URL, callback: openCallback): void;
|
|
|
|
export function open(
|
|
|
|
path: string | Buffer | URL,
|
|
|
|
flags: openFlags,
|
|
|
|
callback: openCallback,
|
|
|
|
): void;
|
|
|
|
export function open(
|
|
|
|
path: string | Buffer | URL,
|
|
|
|
flags: openFlags,
|
|
|
|
mode: number,
|
|
|
|
callback: openCallback,
|
|
|
|
): void;
|
|
|
|
export function open(
|
|
|
|
path: string | Buffer | URL,
|
|
|
|
flags: openCallback | openFlags,
|
|
|
|
mode?: openCallback | number,
|
|
|
|
callback?: openCallback,
|
|
|
|
) {
|
|
|
|
if (flags === undefined) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
"flags or callback",
|
|
|
|
["string", "function"],
|
|
|
|
flags,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
path = getValidatedPath(path);
|
|
|
|
if (arguments.length < 3) {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
callback = flags as any;
|
|
|
|
flags = "r";
|
|
|
|
mode = 0o666;
|
|
|
|
} else if (typeof mode === "function") {
|
|
|
|
callback = mode;
|
|
|
|
mode = 0o666;
|
|
|
|
} else {
|
|
|
|
mode = parseFileMode(mode, "mode", 0o666);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof callback !== "function") {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
"callback",
|
|
|
|
"function",
|
|
|
|
callback,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags === undefined) {
|
|
|
|
flags = "r";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
existenceCheckRequired(flags as openFlags) &&
|
|
|
|
existsSync(path as string)
|
|
|
|
) {
|
|
|
|
const err = new Error(`EEXIST: file already exists, open '${path}'`);
|
|
|
|
(callback as (err: Error) => void)(err);
|
|
|
|
} else {
|
|
|
|
if (flags === "as" || flags === "as+") {
|
|
|
|
let err: Error | null = null, res: number;
|
|
|
|
try {
|
|
|
|
res = openSync(path, flags, mode);
|
|
|
|
} catch (error) {
|
|
|
|
err = error instanceof Error ? error : new Error("[non-error thrown]");
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
(callback as (err: Error) => void)(err);
|
|
|
|
} else {
|
|
|
|
callback(null, res!);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Deno.open(
|
|
|
|
path as string,
|
|
|
|
convertFlagAndModeToOptions(flags as openFlags, mode),
|
|
|
|
).then(
|
2024-01-26 14:04:07 -05:00
|
|
|
(file) => callback!(null, file[internalRidSymbol]),
|
2023-02-14 11:38:45 -05:00
|
|
|
(err) => (callback as (err: Error) => void)(err),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
feat(node_compat): Added base implementation of FileHandle (#19294)
<!--
Before submitting a PR, please read https://deno.com/manual/contributing
1. Give the PR a descriptive title.
Examples of good title:
- fix(std/http): Fix race condition in server
- docs(console): Update docstrings
- feat(doc): Handle nested reexports
Examples of bad title:
- fix #7123
- update docs
- fix bugs
2. Ensure there is a related issue and it is referenced in the PR text.
3. Ensure there are tests that cover the changes.
4. Ensure `cargo test` passes.
5. Ensure `./tools/format.js` passes without changing files.
6. Ensure `./tools/lint.js` passes.
7. Open as a draft PR if your work is still in progress. The CI won't
run
all steps, but you can add '[ci]' to a commit message to force it to.
8. If you would like to run the benchmarks on the CI, add the 'ci-bench'
label.
-->
## WHY
ref: https://github.com/denoland/deno/issues/19165
Node's fs/promises includes a FileHandle class, but deno does not. The
open function in Node's fs/promises returns a FileHandle, which provides
an IO interface to the file. However, deno's open function returns a
resource id.
### deno
```js
> const fs = await import("node:fs/promises");
undefined
> const file3 = await fs.open("./README.md");
undefined
> file3
3
> file3.read
undefined
Node:
```
### Node
```js
> const fs = await import("fs/promises");
undefined
> const file3 = await fs.open("./tests/e2e_unit/testdata/file.txt");
undefined
> file3
FileHandle {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
close: [Function: close],
[Symbol(kCapture)]: false,
[Symbol(kHandle)]: FileHandle {},
[Symbol(kFd)]: 24,
[Symbol(kRefs)]: 1,
[Symbol(kClosePromise)]: null
}
> file3.read
[Function: read]
```
To be compatible with Node, deno's open function should also return a
FileHandle.
## WHAT
I have implemented the first step in adding a FileHandle.
- Changed the return value of the open function to a FileHandle object
- Implemented the readFile method in FileHandle
- Add test code
## What to do next
This PR is the first step in adding a FileHandle, and there are things
that should be done next.
- Add functionality equivalent to Node's FileHandle to FileHandle
(currently there is only readFile)
---------
Co-authored-by: Matt Mastracci <matthew@mastracci.com>
2023-06-02 10:28:05 -04:00
|
|
|
export function openPromise(
|
|
|
|
path: string | Buffer | URL,
|
|
|
|
flags?: openFlags = "r",
|
|
|
|
mode? = 0o666,
|
|
|
|
): Promise<FileHandle> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
open(path, flags, mode, (err, fd) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
else resolve(new FileHandle(fd));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
export function openSync(path: string | Buffer | URL): number;
|
|
|
|
export function openSync(
|
|
|
|
path: string | Buffer | URL,
|
|
|
|
flags?: openFlags,
|
|
|
|
): number;
|
|
|
|
export function openSync(path: string | Buffer | URL, mode?: number): number;
|
|
|
|
export function openSync(
|
|
|
|
path: string | Buffer | URL,
|
|
|
|
flags?: openFlags,
|
|
|
|
mode?: number,
|
|
|
|
): number;
|
|
|
|
export function openSync(
|
|
|
|
path: string | Buffer | URL,
|
|
|
|
flags?: openFlags,
|
|
|
|
maybeMode?: number,
|
|
|
|
) {
|
|
|
|
const mode = parseFileMode(maybeMode, "mode", 0o666);
|
|
|
|
path = getValidatedPath(path);
|
|
|
|
|
|
|
|
if (flags === undefined) {
|
|
|
|
flags = "r";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
existenceCheckRequired(flags) &&
|
|
|
|
existsSync(path as string)
|
|
|
|
) {
|
|
|
|
throw new Error(`EEXIST: file already exists, open '${path}'`);
|
|
|
|
}
|
|
|
|
|
2024-01-25 18:36:03 -05:00
|
|
|
return Deno.openSync(
|
|
|
|
path as string,
|
|
|
|
convertFlagAndModeToOptions(flags, mode),
|
2024-01-26 14:04:07 -05:00
|
|
|
)[internalRidSymbol];
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function existenceCheckRequired(flags: openFlags | number) {
|
|
|
|
return (
|
|
|
|
(typeof flags === "string" &&
|
|
|
|
["ax", "ax+", "wx", "wx+"].includes(flags)) ||
|
|
|
|
(typeof flags === "number" && (
|
|
|
|
((flags & FLAGS_AX) === FLAGS_AX) ||
|
|
|
|
((flags & FLAGS_AX_PLUS) === FLAGS_AX_PLUS) ||
|
|
|
|
((flags & FLAGS_WX) === FLAGS_WX) ||
|
|
|
|
((flags & FLAGS_WX_PLUS) === FLAGS_WX_PLUS)
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|