From e44132ff96ef57ff8d01ca0f9a81abbaab8ffa30 Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Tue, 20 Oct 2020 19:51:57 +0800 Subject: [PATCH] fix(io): remove trivial internal util.ts module (denoland/deno#8032) --- io/bufio_test.ts | 9 ++++----- io/util.ts | 30 ------------------------------ io/util_test.ts | 18 ------------------ mime/multipart.ts | 9 ++++++--- 4 files changed, 10 insertions(+), 56 deletions(-) delete mode 100644 io/util.ts delete mode 100644 io/util_test.ts diff --git a/io/bufio_test.ts b/io/bufio_test.ts index d647082ad516..03f699d50797 100644 --- a/io/bufio_test.ts +++ b/io/bufio_test.ts @@ -17,7 +17,6 @@ import { import * as iotest from "./_iotest.ts"; import { StringReader } from "./readers.ts"; import { StringWriter } from "./writers.ts"; -import { charCode } from "./util.ts"; import { copyBytes } from "../bytes/mod.ts"; const encoder = new TextEncoder(); @@ -140,7 +139,7 @@ Deno.test("bufioBufferFull", async function (): Promise { const decoder = new TextDecoder(); try { - await buf.readSlice(charCode("!")); + await buf.readSlice("!".charCodeAt(0)); fail("readSlice should throw"); } catch (err) { assert(err instanceof BufferFullError); @@ -148,7 +147,7 @@ Deno.test("bufioBufferFull", async function (): Promise { assertEquals(decoder.decode(err.partial), "And now, hello, "); } - const line = await buf.readSlice(charCode("!")); + const line = await buf.readSlice("!".charCodeAt(0)); assert(line !== null); const actual = decoder.decode(line); assertEquals(actual, "world!"); @@ -332,7 +331,7 @@ Deno.test("bufioWriter", async function (): Promise { for (let i = 0; i < data.byteLength; i++) { // eslint-disable-next-line @typescript-eslint/restrict-plus-operands - data[i] = charCode(" ") + (i % (charCode("~") - charCode(" "))); + data[i] = " ".charCodeAt(0) + (i % ("~".charCodeAt(0) - " ".charCodeAt(0))); } const w = new Deno.Buffer(); @@ -366,7 +365,7 @@ Deno.test("bufioWriterSync", function (): void { for (let i = 0; i < data.byteLength; i++) { // eslint-disable-next-line @typescript-eslint/restrict-plus-operands - data[i] = charCode(" ") + (i % (charCode("~") - charCode(" "))); + data[i] = " ".charCodeAt(0) + (i % ("~".charCodeAt(0) - " ".charCodeAt(0))); } const w = new Deno.Buffer(); diff --git a/io/util.ts b/io/util.ts deleted file mode 100644 index 0055d7094590..000000000000 --- a/io/util.ts +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import * as path from "../path/mod.ts"; - -export function charCode(s: string): number { - return s.charCodeAt(0); -} - -/** 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: Deno.File; filepath: string }> { - const r = Math.floor(Math.random() * 1000000); - const filepath = path.resolve( - `${dir}/${opts.prefix || ""}${r}${opts.postfix || ""}`, - ); - await Deno.mkdir(path.dirname(filepath), { recursive: true }); - const file = await Deno.open(filepath, { - create: true, - read: true, - write: true, - append: true, - }); - return { file, filepath }; -} diff --git a/io/util_test.ts b/io/util_test.ts deleted file mode 100644 index c602a90d36a0..000000000000 --- a/io/util_test.ts +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { assert } from "../testing/asserts.ts"; -import * as path from "../path/mod.ts"; -import { tempFile } from "./util.ts"; - -Deno.test({ - name: "[io/util] tempfile", - fn: async function (): Promise { - const f = await tempFile(".", { - prefix: "prefix-", - postfix: "-postfix", - }); - const base = path.basename(f.filepath); - assert(!!base.match(/^prefix-.+?-postfix$/)); - f.file.close(); - await Deno.remove(f.filepath); - }, -}); diff --git a/mime/multipart.ts b/mime/multipart.ts index 6597a1bea273..73af72357715 100644 --- a/mime/multipart.ts +++ b/mime/multipart.ts @@ -3,7 +3,6 @@ import { equal, findIndex, findLastIndex, hasPrefix } from "../bytes/mod.ts"; import { copyN } from "../io/ioutil.ts"; import { MultiReader } from "../io/readers.ts"; import { extname } from "../path/mod.ts"; -import { tempFile } from "../io/util.ts"; import { BufReader, BufWriter } from "../io/bufio.ts"; import { encoder } from "../encoding/utf8.ts"; import { assert } from "../_util/assert.ts"; @@ -310,10 +309,14 @@ export class MultipartReader { if (n > maxMemory) { // too big, write to disk and flush buffer const ext = extname(p.fileName); - const { file, filepath } = await tempFile(".", { + const filepath = await Deno.makeTempFile({ + dir: ".", prefix: "multipart-", - postfix: ext, + suffix: ext, }); + + const file = await Deno.open(filepath, { write: true }); + try { const size = await Deno.copy(new MultiReader(buf, p), file);