2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-18 12:36:00 +00:00
|
|
|
// This module is browser compatible.
|
2022-11-29 13:55:38 +00:00
|
|
|
|
|
|
|
/**
|
2023-12-04 06:12:52 +00:00
|
|
|
* A transform stream that only transforms from the zero-indexed `start` and
|
|
|
|
* `end` bytes (both inclusive).
|
2022-11-29 13:55:38 +00:00
|
|
|
*
|
2024-05-28 01:27:40 +00:00
|
|
|
* @example Basic usage
|
2022-11-29 13:55:38 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { ByteSliceStream } from "@std/streams/byte-slice-stream";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
*
|
|
|
|
* const stream = ReadableStream.from([
|
|
|
|
* new Uint8Array([0, 1]),
|
|
|
|
* new Uint8Array([2, 3, 4]),
|
|
|
|
* ]);
|
|
|
|
* const slicedStream = stream.pipeThrough(new ByteSliceStream(1, 3));
|
|
|
|
*
|
|
|
|
* assertEquals(
|
|
|
|
* await Array.fromAsync(slicedStream),
|
|
|
|
* [new Uint8Array([1]), new Uint8Array([2, 3])]
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Get a range of bytes from a fetch response body
|
|
|
|
* ```ts
|
|
|
|
* import { ByteSliceStream } from "@std/streams/byte-slice-stream";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-12-04 06:12:52 +00:00
|
|
|
*
|
2022-11-29 13:55:38 +00:00
|
|
|
* const response = await fetch("https://example.com");
|
|
|
|
* const rangedStream = response.body!
|
|
|
|
* .pipeThrough(new ByteSliceStream(3, 8));
|
2024-05-28 01:27:40 +00:00
|
|
|
* const collected = await Array.fromAsync(rangedStream);
|
|
|
|
* assertEquals(collected[0]?.length, 6);
|
2022-11-29 13:55:38 +00:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export class ByteSliceStream extends TransformStream<Uint8Array, Uint8Array> {
|
|
|
|
#offsetStart = 0;
|
|
|
|
#offsetEnd = 0;
|
|
|
|
|
2024-05-28 01:27:40 +00:00
|
|
|
/**
|
|
|
|
* Constructs a new instance.
|
|
|
|
*
|
|
|
|
* @param start The zero-indexed byte index to start reading from.
|
|
|
|
* @param end The zero-indexed byte index to stop reading at. Inclusive.
|
|
|
|
*/
|
2023-12-22 04:35:15 +00:00
|
|
|
constructor(start = 0, end: number = Infinity) {
|
2022-11-29 13:55:38 +00:00
|
|
|
super({
|
|
|
|
start: () => {
|
2024-06-05 22:12:42 +00:00
|
|
|
if (start < 0) {
|
2024-08-23 03:31:01 +00:00
|
|
|
throw new RangeError(
|
|
|
|
`Cannot construct ByteSliceStream as start must be >= 0: received ${start}`,
|
|
|
|
);
|
2024-06-05 22:12:42 +00:00
|
|
|
}
|
2022-11-29 13:55:38 +00:00
|
|
|
end += 1;
|
|
|
|
},
|
|
|
|
transform: (chunk, controller) => {
|
|
|
|
this.#offsetStart = this.#offsetEnd;
|
|
|
|
this.#offsetEnd += chunk.byteLength;
|
|
|
|
if (this.#offsetEnd > start) {
|
|
|
|
if (this.#offsetStart < start) {
|
|
|
|
chunk = chunk.slice(start - this.#offsetStart);
|
|
|
|
}
|
|
|
|
if (this.#offsetEnd >= end) {
|
|
|
|
chunk = chunk.slice(0, chunk.byteLength - this.#offsetEnd + end);
|
|
|
|
controller.enqueue(chunk);
|
|
|
|
controller.terminate();
|
|
|
|
} else {
|
|
|
|
controller.enqueue(chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|