2020-03-08 19:14:53 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-05-04 18:59:37 -04:00
|
|
|
import {
|
2020-06-23 22:32:43 -04:00
|
|
|
Encodings,
|
2020-05-04 18:59:37 -04:00
|
|
|
WriteFileOptions,
|
|
|
|
isFileOptions,
|
|
|
|
CallbackWithError,
|
|
|
|
getOpenOptions,
|
|
|
|
} from "./_fs_common.ts";
|
2020-03-08 19:14:53 -04:00
|
|
|
import { notImplemented } from "../_utils.ts";
|
2020-05-03 15:14:38 -04:00
|
|
|
import { fromFileUrl } from "../path.ts";
|
2020-03-08 19:14:53 -04:00
|
|
|
|
|
|
|
/**
|
2020-05-03 15:14:38 -04:00
|
|
|
* TODO: Also accept 'data' parameter as a Node polyfill Buffer type once these
|
2020-03-14 17:46:39 -04:00
|
|
|
* are implemented. See https://github.com/denoland/deno/issues/3403
|
2020-03-08 19:14:53 -04:00
|
|
|
*/
|
2020-03-12 10:12:27 -04:00
|
|
|
export function appendFile(
|
2020-05-03 15:14:38 -04:00
|
|
|
pathOrRid: string | number | URL,
|
2020-03-08 19:14:53 -04:00
|
|
|
data: string,
|
2020-06-23 22:32:43 -04:00
|
|
|
optionsOrCallback: Encodings | WriteFileOptions | CallbackWithError,
|
2020-03-12 10:12:27 -04:00
|
|
|
callback?: CallbackWithError
|
|
|
|
): void {
|
2020-05-03 15:14:38 -04:00
|
|
|
pathOrRid = pathOrRid instanceof URL ? fromFileUrl(pathOrRid) : pathOrRid;
|
2020-03-12 10:12:27 -04:00
|
|
|
const callbackFn: CallbackWithError | undefined =
|
2020-03-08 19:14:53 -04:00
|
|
|
optionsOrCallback instanceof Function ? optionsOrCallback : callback;
|
2020-06-23 22:32:43 -04:00
|
|
|
const options: Encodings | WriteFileOptions | undefined =
|
2020-03-08 19:14:53 -04:00
|
|
|
optionsOrCallback instanceof Function ? undefined : optionsOrCallback;
|
|
|
|
if (!callbackFn) {
|
|
|
|
throw new Error("No callback function supplied");
|
|
|
|
}
|
|
|
|
|
|
|
|
validateEncoding(options);
|
|
|
|
let rid = -1;
|
2020-04-20 05:29:37 -04:00
|
|
|
const buffer: Uint8Array = new TextEncoder().encode(data);
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
if (typeof pathOrRid === "number") {
|
|
|
|
rid = pathOrRid;
|
|
|
|
Deno.write(rid, buffer).then(resolve).catch(reject);
|
|
|
|
} else {
|
|
|
|
const mode: number | undefined = isFileOptions(options)
|
|
|
|
? options.mode
|
|
|
|
: undefined;
|
|
|
|
const flag: string | undefined = isFileOptions(options)
|
|
|
|
? options.flag
|
|
|
|
: undefined;
|
2020-03-08 19:14:53 -04:00
|
|
|
|
2020-04-20 05:29:37 -04:00
|
|
|
if (mode) {
|
|
|
|
//TODO rework once https://github.com/denoland/deno/issues/4017 completes
|
|
|
|
notImplemented("Deno does not yet support setting mode on create");
|
2020-03-08 19:14:53 -04:00
|
|
|
}
|
2020-05-03 15:14:38 -04:00
|
|
|
Deno.open(pathOrRid as string, getOpenOptions(flag))
|
2020-04-20 05:29:37 -04:00
|
|
|
.then(({ rid: openedFileRid }) => {
|
|
|
|
rid = openedFileRid;
|
|
|
|
return Deno.write(openedFileRid, buffer);
|
|
|
|
})
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
2020-03-08 19:14:53 -04:00
|
|
|
}
|
2020-03-12 10:12:27 -04:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
closeRidIfNecessary(typeof pathOrRid === "string", rid);
|
|
|
|
callbackFn();
|
|
|
|
})
|
2020-03-28 13:03:49 -04:00
|
|
|
.catch((err) => {
|
2020-03-12 10:12:27 -04:00
|
|
|
closeRidIfNecessary(typeof pathOrRid === "string", rid);
|
|
|
|
callbackFn(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2020-03-08 19:14:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-03 15:14:38 -04:00
|
|
|
* TODO: Also accept 'data' parameter as a Node polyfill Buffer type once these
|
2020-03-14 17:46:39 -04:00
|
|
|
* are implemented. See https://github.com/denoland/deno/issues/3403
|
2020-03-08 19:14:53 -04:00
|
|
|
*/
|
|
|
|
export function appendFileSync(
|
2020-05-03 15:14:38 -04:00
|
|
|
pathOrRid: string | number | URL,
|
2020-03-08 19:14:53 -04:00
|
|
|
data: string,
|
2020-06-23 22:32:43 -04:00
|
|
|
options?: Encodings | WriteFileOptions
|
2020-03-08 19:14:53 -04:00
|
|
|
): void {
|
|
|
|
let rid = -1;
|
|
|
|
|
|
|
|
validateEncoding(options);
|
2020-05-03 15:14:38 -04:00
|
|
|
pathOrRid = pathOrRid instanceof URL ? fromFileUrl(pathOrRid) : pathOrRid;
|
2020-03-08 19:14:53 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (mode) {
|
|
|
|
// TODO rework once https://github.com/denoland/deno/issues/4017 completes
|
|
|
|
notImplemented("Deno does not yet support setting mode on create");
|
|
|
|
}
|
|
|
|
|
|
|
|
const file = Deno.openSync(pathOrRid, getOpenOptions(flag));
|
|
|
|
rid = file.rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
const buffer: Uint8Array = new TextEncoder().encode(data);
|
|
|
|
|
|
|
|
Deno.writeSync(rid, buffer);
|
|
|
|
} finally {
|
2020-03-12 10:12:27 -04:00
|
|
|
closeRidIfNecessary(typeof pathOrRid === "string", rid);
|
2020-03-08 19:14:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function validateEncoding(
|
2020-06-23 22:32:43 -04:00
|
|
|
encodingOption: Encodings | WriteFileOptions | undefined
|
2020-03-08 19:14:53 -04:00
|
|
|
): 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");
|
|
|
|
}
|
|
|
|
}
|