diff options
Diffstat (limited to 'std/mime')
-rw-r--r-- | std/mime/multipart.ts | 36 | ||||
-rw-r--r-- | std/mime/multipart_test.ts | 47 |
2 files changed, 39 insertions, 44 deletions
diff --git a/std/mime/multipart.ts b/std/mime/multipart.ts index 73a6544b5..42be47895 100644 --- a/std/mime/multipart.ts +++ b/std/mime/multipart.ts @@ -1,10 +1,4 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -const { Buffer, copy, remove } = Deno; -const { min, max } = Math; -type Closer = Deno.Closer; -type Reader = Deno.Reader; -type Writer = Deno.Writer; import { equal, findIndex, findLastIndex, hasPrefix } from "../bytes/mod.ts"; import { copyN } from "../io/ioutil.ts"; import { MultiReader } from "../io/readers.ts"; @@ -150,7 +144,7 @@ export function scanUntilBoundary( return buf.length; } -class PartReader implements Reader, Closer { +class PartReader implements Deno.Reader, Deno.Closer { n: number | null = 0; total = 0; @@ -163,7 +157,7 @@ class PartReader implements Reader, Closer { // or we find a reason to stop (boundary or EOF). let peekLength = 1; while (this.n === 0) { - peekLength = max(peekLength, br.buffered()); + peekLength = Math.max(peekLength, br.buffered()); const peekBuf = await br.peek(peekLength); if (peekBuf === null) { throw new Deno.errors.UnexpectedEof(); @@ -187,7 +181,7 @@ class PartReader implements Reader, Closer { return null; } - const nread = min(p.length, this.n); + const nread = Math.min(p.length, this.n); const buf = p.subarray(0, nread); const r = await br.readFull(buf); assert(r === buf); @@ -272,7 +266,7 @@ export class MultipartReader { readonly dashBoundary = encoder.encode(`--${this.boundary}`); readonly bufReader: BufReader; - constructor(reader: Reader, private boundary: string) { + constructor(reader: Deno.Reader, private boundary: string) { this.bufReader = new BufReader(reader); } @@ -287,7 +281,7 @@ export class MultipartReader { const fileMap = new Map<string, FormFile | FormFile[]>(); const valueMap = new Map<string, string>(); let maxValueBytes = maxMemory + (10 << 20); - const buf = new Buffer(new Uint8Array(maxValueBytes)); + const buf = new Deno.Buffer(new Uint8Array(maxValueBytes)); for (;;) { const p = await this.nextPart(); if (p === null) { @@ -321,7 +315,7 @@ export class MultipartReader { postfix: ext, }); try { - const size = await copy(new MultiReader(buf, p), file); + const size = await Deno.copy(new MultiReader(buf, p), file); file.close(); formFile = { @@ -331,7 +325,7 @@ export class MultipartReader { size, }; } catch (e) { - await remove(filepath); + await Deno.remove(filepath); throw e; } } else { @@ -465,13 +459,13 @@ function multipatFormData( }; } -class PartWriter implements Writer { +class PartWriter implements Deno.Writer { closed = false; private readonly partHeader: string; private headersWritten = false; constructor( - private writer: Writer, + private writer: Deno.Writer, readonly boundary: string, public headers: Headers, isFirstBoundary: boolean @@ -531,7 +525,7 @@ export class MultipartWriter { private bufWriter: BufWriter; private isClosed = false; - constructor(private readonly writer: Writer, boundary?: string) { + constructor(private readonly writer: Deno.Writer, boundary?: string) { if (boundary !== void 0) { this._boundary = checkBoundary(boundary); } else { @@ -544,7 +538,7 @@ export class MultipartWriter { return `multipart/form-data; boundary=${this.boundary}`; } - private createPart(headers: Headers): Writer { + private createPart(headers: Headers): Deno.Writer { if (this.isClosed) { throw new Error("multipart: writer is closed"); } @@ -561,7 +555,7 @@ export class MultipartWriter { return part; } - createFormFile(field: string, filename: string): Writer { + createFormFile(field: string, filename: string): Deno.Writer { const h = new Headers(); h.set( "Content-Disposition", @@ -571,7 +565,7 @@ export class MultipartWriter { return this.createPart(h); } - createFormField(field: string): Writer { + createFormField(field: string): Deno.Writer { const h = new Headers(); h.set("Content-Disposition", `form-data; name="${field}"`); h.set("Content-Type", "application/octet-stream"); @@ -586,10 +580,10 @@ export class MultipartWriter { async writeFile( field: string, filename: string, - file: Reader + file: Deno.Reader ): Promise<void> { const f = await this.createFormFile(field, filename); - await copy(file, f); + await Deno.copy(file, f); } private flush(): Promise<void> { diff --git a/std/mime/multipart_test.ts b/std/mime/multipart_test.ts index 858dc3919..c0282ee3b 100644 --- a/std/mime/multipart_test.ts +++ b/std/mime/multipart_test.ts @@ -1,6 +1,4 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -const { Buffer, open, test } = Deno; import { assert, assertEquals, @@ -23,7 +21,7 @@ const dashBoundary = e.encode("--" + boundary); const nlDashBoundary = e.encode("\r\n--" + boundary); const testdataDir = path.resolve("mime", "testdata"); -test("multipartScanUntilBoundary1", function (): void { +Deno.test("multipartScanUntilBoundary1", function (): void { const data = `--${boundary}`; const n = scanUntilBoundary( e.encode(data), @@ -35,7 +33,7 @@ test("multipartScanUntilBoundary1", function (): void { assertEquals(n, null); }); -test("multipartScanUntilBoundary2", function (): void { +Deno.test("multipartScanUntilBoundary2", function (): void { const data = `foo\r\n--${boundary}`; const n = scanUntilBoundary( e.encode(data), @@ -47,7 +45,7 @@ test("multipartScanUntilBoundary2", function (): void { assertEquals(n, 3); }); -test("multipartScanUntilBoundary3", function (): void { +Deno.test("multipartScanUntilBoundary3", function (): void { const data = `foobar`; const n = scanUntilBoundary( e.encode(data), @@ -59,7 +57,7 @@ test("multipartScanUntilBoundary3", function (): void { assertEquals(n, data.length); }); -test("multipartScanUntilBoundary4", function (): void { +Deno.test("multipartScanUntilBoundary4", function (): void { const data = `foo\r\n--`; const n = scanUntilBoundary( e.encode(data), @@ -71,30 +69,30 @@ test("multipartScanUntilBoundary4", function (): void { assertEquals(n, 3); }); -test("multipartMatchAfterPrefix1", function (): void { +Deno.test("multipartMatchAfterPrefix1", function (): void { const data = `${boundary}\r`; const v = matchAfterPrefix(e.encode(data), e.encode(boundary), false); assertEquals(v, 1); }); -test("multipartMatchAfterPrefix2", function (): void { +Deno.test("multipartMatchAfterPrefix2", function (): void { const data = `${boundary}hoge`; const v = matchAfterPrefix(e.encode(data), e.encode(boundary), false); assertEquals(v, -1); }); -test("multipartMatchAfterPrefix3", function (): void { +Deno.test("multipartMatchAfterPrefix3", function (): void { const data = `${boundary}`; const v = matchAfterPrefix(e.encode(data), e.encode(boundary), false); assertEquals(v, 0); }); -test("multipartMultipartWriter", async function (): Promise<void> { - const buf = new Buffer(); +Deno.test("multipartMultipartWriter", async function (): Promise<void> { + const buf = new Deno.Buffer(); const mw = new MultipartWriter(buf); await mw.writeField("foo", "foo"); await mw.writeField("bar", "bar"); - const f = await open(path.resolve("./mime/testdata/sample.txt"), { + const f = await Deno.open(path.resolve("./mime/testdata/sample.txt"), { read: true, }); await mw.writeFile("file", "sample.txt", f); @@ -102,7 +100,7 @@ test("multipartMultipartWriter", async function (): Promise<void> { f.close(); }); -test("multipartMultipartWriter2", function (): void { +Deno.test("multipartMultipartWriter2", function (): void { const w = new StringWriter(); assertThrows( (): MultipartWriter => new MultipartWriter(w, ""), @@ -131,7 +129,7 @@ test("multipartMultipartWriter2", function (): void { ); }); -test("multipartMultipartWriter3", async function (): Promise<void> { +Deno.test("multipartMultipartWriter3", async function (): Promise<void> { const w = new StringWriter(); const mw = new MultipartWriter(w); await mw.writeField("foo", "foo"); @@ -174,10 +172,10 @@ test("multipartMultipartWriter3", async function (): Promise<void> { ); }); -test({ +Deno.test({ name: "[mime/multipart] readForm() basic", async fn() { - const o = await open(path.resolve("./mime/testdata/sample.txt")); + const o = await Deno.open(path.resolve("./mime/testdata/sample.txt")); const mr = new MultipartReader( o, "--------------------------434049563556637648550474" @@ -196,18 +194,21 @@ test({ }, }); -test({ +Deno.test({ name: "[mime/multipart] readForm() should store big file completely in temp file", async fn() { const multipartFile = path.join(testdataDir, "form-data.dat"); const sampleFile = await Deno.makeTempFile(); - const writer = await open(multipartFile, { write: true, create: true }); + const writer = await Deno.open(multipartFile, { + write: true, + create: true, + }); const size = 1 << 24; // 16mb await Deno.truncate(sampleFile, size); - const bigFile = await open(sampleFile, { read: true }); + const bigFile = await Deno.open(sampleFile, { read: true }); const mw = new MultipartWriter(writer); await mw.writeField("deno", "land"); @@ -243,10 +244,10 @@ test({ }, }); -test({ +Deno.test({ name: "[mime/multipart] removeAll() should remove all tempfiles", async fn() { - const o = await open(path.resolve("./mime/testdata/sample.txt")); + const o = await Deno.open(path.resolve("./mime/testdata/sample.txt")); const mr = new MultipartReader( o, "--------------------------434049563556637648550474" @@ -270,10 +271,10 @@ test({ }, }); -test({ +Deno.test({ name: "[mime/multipart] entries()", async fn() { - const o = await open(path.resolve("./mime/testdata/sample.txt")); + const o = await Deno.open(path.resolve("./mime/testdata/sample.txt")); const mr = new MultipartReader( o, "--------------------------434049563556637648550474" |