1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -05:00

fix(std/io): remove trivial internal util.ts module (#8032)

This commit is contained in:
Casper Beyer 2020-10-20 19:51:57 +08:00 committed by GitHub
parent 9cf06f76fd
commit 17467d01da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 56 deletions

View file

@ -17,7 +17,6 @@ import {
import * as iotest from "./_iotest.ts"; import * as iotest from "./_iotest.ts";
import { StringReader } from "./readers.ts"; import { StringReader } from "./readers.ts";
import { StringWriter } from "./writers.ts"; import { StringWriter } from "./writers.ts";
import { charCode } from "./util.ts";
import { copyBytes } from "../bytes/mod.ts"; import { copyBytes } from "../bytes/mod.ts";
const encoder = new TextEncoder(); const encoder = new TextEncoder();
@ -140,7 +139,7 @@ Deno.test("bufioBufferFull", async function (): Promise<void> {
const decoder = new TextDecoder(); const decoder = new TextDecoder();
try { try {
await buf.readSlice(charCode("!")); await buf.readSlice("!".charCodeAt(0));
fail("readSlice should throw"); fail("readSlice should throw");
} catch (err) { } catch (err) {
assert(err instanceof BufferFullError); assert(err instanceof BufferFullError);
@ -148,7 +147,7 @@ Deno.test("bufioBufferFull", async function (): Promise<void> {
assertEquals(decoder.decode(err.partial), "And now, hello, "); assertEquals(decoder.decode(err.partial), "And now, hello, ");
} }
const line = await buf.readSlice(charCode("!")); const line = await buf.readSlice("!".charCodeAt(0));
assert(line !== null); assert(line !== null);
const actual = decoder.decode(line); const actual = decoder.decode(line);
assertEquals(actual, "world!"); assertEquals(actual, "world!");
@ -332,7 +331,7 @@ Deno.test("bufioWriter", async function (): Promise<void> {
for (let i = 0; i < data.byteLength; i++) { for (let i = 0; i < data.byteLength; i++) {
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands // 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(); const w = new Deno.Buffer();
@ -366,7 +365,7 @@ Deno.test("bufioWriterSync", function (): void {
for (let i = 0; i < data.byteLength; i++) { for (let i = 0; i < data.byteLength; i++) {
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands // 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(); const w = new Deno.Buffer();

View file

@ -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 };
}

View file

@ -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<void> {
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);
},
});

View file

@ -3,7 +3,6 @@ import { equal, findIndex, findLastIndex, hasPrefix } from "../bytes/mod.ts";
import { copyN } from "../io/ioutil.ts"; import { copyN } from "../io/ioutil.ts";
import { MultiReader } from "../io/readers.ts"; import { MultiReader } from "../io/readers.ts";
import { extname } from "../path/mod.ts"; import { extname } from "../path/mod.ts";
import { tempFile } from "../io/util.ts";
import { BufReader, BufWriter } from "../io/bufio.ts"; import { BufReader, BufWriter } from "../io/bufio.ts";
import { encoder } from "../encoding/utf8.ts"; import { encoder } from "../encoding/utf8.ts";
import { assert } from "../_util/assert.ts"; import { assert } from "../_util/assert.ts";
@ -310,10 +309,14 @@ export class MultipartReader {
if (n > maxMemory) { if (n > maxMemory) {
// too big, write to disk and flush buffer // too big, write to disk and flush buffer
const ext = extname(p.fileName); const ext = extname(p.fileName);
const { file, filepath } = await tempFile(".", { const filepath = await Deno.makeTempFile({
dir: ".",
prefix: "multipart-", prefix: "multipart-",
postfix: ext, suffix: ext,
}); });
const file = await Deno.open(filepath, { write: true });
try { try {
const size = await Deno.copy(new MultiReader(buf, p), file); const size = await Deno.copy(new MultiReader(buf, p), file);