-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(node/fs): enable
fs.appendFile
native tests (#1675)
- Loading branch information
Showing
9 changed files
with
392 additions
and
121 deletions.
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 |
---|---|---|
@@ -1,122 +1,56 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
import { | ||
CallbackWithError, | ||
getOpenOptions, | ||
isFileOptions, | ||
isFd, | ||
maybeCallback, | ||
WriteFileOptions, | ||
} from "./_fs_common.ts"; | ||
import { Encodings } from "../_utils.ts"; | ||
import { fromFileUrl } from "../path.ts"; | ||
import { copyObject, getOptions } from "../internal/fs/utils.js"; | ||
import { writeFile, writeFileSync } from "./_fs_writeFile.ts"; | ||
|
||
/** | ||
* TODO: Also accept 'data' parameter as a Node polyfill Buffer type once these | ||
* are implemented. See https://github.com/denoland/deno/issues/3403 | ||
*/ | ||
export function appendFile( | ||
pathOrRid: string | number | URL, | ||
path: string | number | URL, | ||
data: string | Uint8Array, | ||
optionsOrCallback: Encodings | WriteFileOptions | CallbackWithError, | ||
options: Encodings | WriteFileOptions | CallbackWithError, | ||
callback?: CallbackWithError, | ||
): void { | ||
pathOrRid = pathOrRid instanceof URL ? fromFileUrl(pathOrRid) : pathOrRid; | ||
const callbackFn: CallbackWithError | undefined = | ||
optionsOrCallback instanceof Function ? optionsOrCallback : callback; | ||
const options: Encodings | WriteFileOptions | undefined = | ||
optionsOrCallback instanceof Function ? undefined : optionsOrCallback; | ||
if (!callbackFn) { | ||
throw new Error("No callback function supplied"); | ||
} | ||
|
||
validateEncoding(options); | ||
let rid = -1; | ||
const buffer: Uint8Array = data instanceof Uint8Array | ||
? data | ||
: new TextEncoder().encode(data); | ||
new Promise((resolve, reject) => { | ||
if (typeof pathOrRid === "number") { | ||
rid = pathOrRid; | ||
Deno.write(rid, buffer).then(resolve, reject); | ||
} else { | ||
const mode: number | undefined = isFileOptions(options) | ||
? options.mode | ||
: undefined; | ||
const flag: string | undefined = isFileOptions(options) | ||
? options.flag | ||
: undefined; | ||
callback = maybeCallback(callback || options); | ||
options = getOptions(options, { encoding: "utf8", mode: 0o666, flag: "a" }); | ||
|
||
Deno.open(pathOrRid as string, { ...getOpenOptions(flag), mode }) | ||
.then(({ rid: openedFileRid }) => { | ||
rid = openedFileRid; | ||
return Deno.write(openedFileRid, buffer); | ||
}) | ||
.then(resolve, reject); | ||
} | ||
}) | ||
.then(() => { | ||
closeRidIfNecessary(typeof pathOrRid === "string", rid); | ||
callbackFn(null); | ||
}, (err) => { | ||
closeRidIfNecessary(typeof pathOrRid === "string", rid); | ||
callbackFn(err); | ||
}); | ||
} | ||
// Don't make changes directly on options object | ||
options = copyObject(options); | ||
|
||
function closeRidIfNecessary(isPathString: boolean, rid: number): void { | ||
if (isPathString && rid != -1) { | ||
//Only close if a path was supplied and a rid allocated | ||
Deno.close(rid); | ||
// Force append behavior when using a supplied file descriptor | ||
if (!options.flag || isFd(path)) { | ||
options.flag = "a"; | ||
} | ||
|
||
writeFile(path, data, options, callback); | ||
} | ||
|
||
/** | ||
* TODO: Also accept 'data' parameter as a Node polyfill Buffer type once these | ||
* are implemented. See https://github.com/denoland/deno/issues/3403 | ||
*/ | ||
export function appendFileSync( | ||
pathOrRid: string | number | URL, | ||
path: string | number | URL, | ||
data: string | Uint8Array, | ||
options?: Encodings | WriteFileOptions, | ||
): void { | ||
let rid = -1; | ||
|
||
validateEncoding(options); | ||
pathOrRid = pathOrRid instanceof URL ? fromFileUrl(pathOrRid) : pathOrRid; | ||
|
||
try { | ||
if (typeof pathOrRid === "number") { | ||
rid = pathOrRid; | ||
} else { | ||
const mode: number | undefined = isFileOptions(options) | ||
? options.mode | ||
: undefined; | ||
const flag: string | undefined = isFileOptions(options) | ||
? options.flag | ||
: undefined; | ||
options = getOptions(options, { encoding: "utf8", mode: 0o666, flag: "a" }); | ||
|
||
const file = Deno.openSync(pathOrRid, { ...getOpenOptions(flag), mode }); | ||
rid = file.rid; | ||
} | ||
// Don't make changes directly on options object | ||
options = copyObject(options); | ||
|
||
const buffer: Uint8Array = data instanceof Uint8Array | ||
? data | ||
: new TextEncoder().encode(data); | ||
|
||
Deno.writeSync(rid, buffer); | ||
} finally { | ||
closeRidIfNecessary(typeof pathOrRid === "string", rid); | ||
// Force append behavior when using a supplied file descriptor | ||
if (!options.flag || isFd(path)) { | ||
options.flag = "a"; | ||
} | ||
} | ||
|
||
function validateEncoding( | ||
encodingOption: Encodings | WriteFileOptions | undefined, | ||
): void { | ||
if (!encodingOption) return; | ||
|
||
if (typeof encodingOption === "string") { | ||
if (encodingOption !== "utf8") { | ||
throw new Error("Only 'utf8' encoding is currently supported"); | ||
} | ||
} else if (encodingOption.encoding && encodingOption.encoding !== "utf8") { | ||
throw new Error("Only 'utf8' encoding is currently supported"); | ||
} | ||
writeFileSync(path, data, options); | ||
} |
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
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
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
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
Oops, something went wrong.