diff options
| author | Yusuke Sakurai <kerokerokerop@gmail.com> | 2019-02-11 08:49:48 +0900 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2019-02-10 18:49:48 -0500 |
| commit | 33f62789cde407059abba0a7ac18b2145c648ea7 (patch) | |
| tree | 454d487f232a61f6c57e2ebdad66e49bf939e4d6 /io/util.ts | |
| parent | ed20bda6ec324b8143c6210024647d2692232c26 (diff) | |
feat: multipart, etc.. (denoland/deno_std#180)
Original: https://github.com/denoland/deno_std/commit/fda9c98d055091fa886fa444ebd1adcd2ecd21bc
Diffstat (limited to 'io/util.ts')
| -rw-r--r-- | io/util.ts | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/io/util.ts b/io/util.ts index 8726a1887..954808c6c 100644 --- a/io/util.ts +++ b/io/util.ts @@ -1,6 +1,7 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { Buffer, Reader } from "deno"; - +import { Buffer, File, mkdir, open, Reader } from "deno"; +import { encode } from "../strings/strings.ts"; +import * as path from "../fs/path.ts"; // `off` is the offset into `dst` where it will at which to begin writing values // from `src`. // Returns the number of bytes copied. @@ -18,8 +19,23 @@ export function charCode(s: string): number { return s.charCodeAt(0); } -const encoder = new TextEncoder(); export function stringsReader(s: string): Reader { - const ui8 = encoder.encode(s); - return new Buffer(ui8.buffer as ArrayBuffer); + return new Buffer(encode(s).buffer); +} + +/** Create or open a temporal file at specified directory with prefix and postfix */ +export async function tempFile( + dir: string, + opts: { + prefix?: string; + postfix?: string; + } = { prefix: "", postfix: "" } +): Promise<{ file: File; filepath: string }> { + const r = Math.floor(Math.random() * 1000000); + const filepath = path.resolve( + `${dir}/${opts.prefix || ""}${r}${opts.postfix || ""}` + ); + await mkdir(path.dirname(filepath), true); + const file = await open(filepath, "a"); + return { file, filepath }; } |
