2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-11-29 13:55:38 +00:00
|
|
|
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals, assertRejects } from "@std/assert";
|
2022-11-29 13:55:38 +00:00
|
|
|
import { LimitedBytesTransformStream } from "./limited_bytes_transform_stream.ts";
|
|
|
|
|
2024-05-28 01:27:40 +00:00
|
|
|
Deno.test("LimitedBytesTransformStream - specified size is the boundary of the chunks", async function () {
|
|
|
|
const r = ReadableStream.from([
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
new Uint8Array([7, 8, 9]),
|
|
|
|
new Uint8Array([10, 11, 12]),
|
|
|
|
new Uint8Array([13, 14, 15]),
|
|
|
|
new Uint8Array([16, 17, 18]),
|
|
|
|
]).pipeThrough(new LimitedBytesTransformStream(6));
|
|
|
|
|
|
|
|
const chunks = await Array.fromAsync(r);
|
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("LimitedBytesTransformStream - specified size is not the boundary of the chunks", async function () {
|
2023-11-10 03:57:52 +00:00
|
|
|
const r = ReadableStream.from([
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
new Uint8Array([7, 8, 9]),
|
|
|
|
new Uint8Array([10, 11, 12]),
|
|
|
|
new Uint8Array([13, 14, 15]),
|
|
|
|
new Uint8Array([16, 17, 18]),
|
2023-11-10 19:00:28 +00:00
|
|
|
]).pipeThrough(new LimitedBytesTransformStream(7));
|
2022-11-29 13:55:38 +00:00
|
|
|
|
2023-11-10 19:00:28 +00:00
|
|
|
const chunks = await Array.fromAsync(r);
|
2024-05-28 01:27:40 +00:00
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("LimitedBytesTransformStream - specified size is 0", async function () {
|
|
|
|
const r = ReadableStream.from([
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
new Uint8Array([7, 8, 9]),
|
|
|
|
new Uint8Array([10, 11, 12]),
|
|
|
|
new Uint8Array([13, 14, 15]),
|
|
|
|
new Uint8Array([16, 17, 18]),
|
|
|
|
]).pipeThrough(new LimitedBytesTransformStream(0));
|
|
|
|
|
|
|
|
const chunks = await Array.fromAsync(r);
|
|
|
|
assertEquals(chunks.length, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("LimitedBytesTransformStream - specified size is equal to the total size of the chunks", async function () {
|
|
|
|
const r = ReadableStream.from([
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
new Uint8Array([7, 8, 9]),
|
|
|
|
new Uint8Array([10, 11, 12]),
|
|
|
|
new Uint8Array([13, 14, 15]),
|
|
|
|
new Uint8Array([16, 17, 18]),
|
|
|
|
]).pipeThrough(new LimitedBytesTransformStream(18));
|
|
|
|
|
|
|
|
const chunks = await Array.fromAsync(r);
|
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
new Uint8Array([7, 8, 9]),
|
|
|
|
new Uint8Array([10, 11, 12]),
|
|
|
|
new Uint8Array([13, 14, 15]),
|
|
|
|
new Uint8Array([16, 17, 18]),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("LimitedBytesTransformStream - specified size is greater than the total size of the chunks", async function () {
|
|
|
|
const r = ReadableStream.from([
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
new Uint8Array([7, 8, 9]),
|
|
|
|
new Uint8Array([10, 11, 12]),
|
|
|
|
new Uint8Array([13, 14, 15]),
|
|
|
|
new Uint8Array([16, 17, 18]),
|
|
|
|
]).pipeThrough(new LimitedBytesTransformStream(19));
|
|
|
|
|
|
|
|
const chunks = await Array.fromAsync(r);
|
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
new Uint8Array([7, 8, 9]),
|
|
|
|
new Uint8Array([10, 11, 12]),
|
|
|
|
new Uint8Array([13, 14, 15]),
|
|
|
|
new Uint8Array([16, 17, 18]),
|
|
|
|
]);
|
2022-11-29 13:55:38 +00:00
|
|
|
});
|
|
|
|
|
2024-05-28 01:27:40 +00:00
|
|
|
Deno.test("LimitedBytesTransformStream - error is set to true and the specified size is 0", async function () {
|
|
|
|
const r = ReadableStream.from([
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
new Uint8Array([7, 8, 9]),
|
|
|
|
new Uint8Array([10, 11, 12]),
|
|
|
|
new Uint8Array([13, 14, 15]),
|
|
|
|
new Uint8Array([16, 17, 18]),
|
|
|
|
]).pipeThrough(new LimitedBytesTransformStream(0, { error: true }));
|
|
|
|
|
|
|
|
await assertRejects(async () => await Array.fromAsync(r), RangeError);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("LimitedBytesTransformStream - error is set to true and the specified size is the boundary of the chunks", async function () {
|
|
|
|
const r = ReadableStream.from([
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
new Uint8Array([7, 8, 9]),
|
|
|
|
new Uint8Array([10, 11, 12]),
|
|
|
|
new Uint8Array([13, 14, 15]),
|
|
|
|
new Uint8Array([16, 17, 18]),
|
|
|
|
]).pipeThrough(new LimitedBytesTransformStream(6, { error: true }));
|
|
|
|
|
|
|
|
await assertRejects(async () => await Array.fromAsync(r), RangeError);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("LimitedBytesTransformStream - error is set to true and the specified size is not the boundary of the chunks", async function () {
|
2023-11-10 03:57:52 +00:00
|
|
|
const r = ReadableStream.from([
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
new Uint8Array([7, 8, 9]),
|
|
|
|
new Uint8Array([10, 11, 12]),
|
|
|
|
new Uint8Array([13, 14, 15]),
|
|
|
|
new Uint8Array([16, 17, 18]),
|
2023-11-10 19:00:28 +00:00
|
|
|
]).pipeThrough(new LimitedBytesTransformStream(7, { error: true }));
|
2022-11-29 13:55:38 +00:00
|
|
|
|
2023-11-10 19:00:28 +00:00
|
|
|
await assertRejects(async () => await Array.fromAsync(r), RangeError);
|
2022-11-29 13:55:38 +00:00
|
|
|
});
|
2024-05-28 01:27:40 +00:00
|
|
|
|
|
|
|
Deno.test("LimitedBytesTransformStream - error is set to true and specified size is equal to the total size of the chunks", async function () {
|
|
|
|
const r = ReadableStream.from([
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
new Uint8Array([7, 8, 9]),
|
|
|
|
new Uint8Array([10, 11, 12]),
|
|
|
|
new Uint8Array([13, 14, 15]),
|
|
|
|
new Uint8Array([16, 17, 18]),
|
|
|
|
]).pipeThrough(new LimitedBytesTransformStream(18, { error: true }));
|
|
|
|
|
|
|
|
const chunks = await Array.fromAsync(r);
|
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
new Uint8Array([7, 8, 9]),
|
|
|
|
new Uint8Array([10, 11, 12]),
|
|
|
|
new Uint8Array([13, 14, 15]),
|
|
|
|
new Uint8Array([16, 17, 18]),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("LimitedBytesTransformStream - error is set to true and specified size is greater than the total size of the chunks", async function () {
|
|
|
|
const r = ReadableStream.from([
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
new Uint8Array([7, 8, 9]),
|
|
|
|
new Uint8Array([10, 11, 12]),
|
|
|
|
new Uint8Array([13, 14, 15]),
|
|
|
|
new Uint8Array([16, 17, 18]),
|
|
|
|
]).pipeThrough(new LimitedBytesTransformStream(19, { error: true }));
|
|
|
|
|
|
|
|
const chunks = await Array.fromAsync(r);
|
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
new Uint8Array([7, 8, 9]),
|
|
|
|
new Uint8Array([10, 11, 12]),
|
|
|
|
new Uint8Array([13, 14, 15]),
|
|
|
|
new Uint8Array([16, 17, 18]),
|
|
|
|
]);
|
|
|
|
});
|