Started to implement server
This commit is contained in:
1 parent
1f2ea8b7e5
commit
17a1b4bed3
177 files changed
+29894
-74
No files matched your search
@@ -0,0 +1,44 @@
|
||||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
import { assert } from "../_util/asserts.ts";
|
||||
|
||||
/**
|
||||
* A transform stream that only transforms from the zero-indexed `start` and `end` bytes (both inclusive).
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { ByteSliceStream } from "https://deno.land/std@$STD_VERSION/streams/byte_slice_stream.ts";
|
||||
* const response = await fetch("https://example.com");
|
||||
* const rangedStream = response.body!
|
||||
* .pipeThrough(new ByteSliceStream(3, 8));
|
||||
* ```
|
||||
*/
|
||||
export class ByteSliceStream extends TransformStream<Uint8Array, Uint8Array> {
|
||||
#offsetStart = 0;
|
||||
#offsetEnd = 0;
|
||||
|
||||
constructor(start = 0, end = Infinity) {
|
||||
super({
|
||||
start: () => {
|
||||
assert(start >= 0, "`start` must be greater than 0");
|
||||
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);
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user