123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import { parseChunked } from './parse-chunked.js';
- import { stringifyChunked } from './stringify-chunked.js';
- import { isIterable } from './utils.js';
- export function parseFromWebStream(stream) {
-
-
-
- return parseChunked(isIterable(stream) ? stream : async function*() {
- const reader = stream.getReader();
- try {
- while (true) {
- const { value, done } = await reader.read();
- if (done) {
- break;
- }
- yield value;
- }
- } finally {
- reader.releaseLock();
- }
- });
- }
- export function createStringifyWebStream(value, replacer, space) {
-
-
- if (typeof ReadableStream.from === 'function') {
- return ReadableStream.from(stringifyChunked(value, replacer, space));
- }
-
- return new ReadableStream({
- start() {
- this.generator = stringifyChunked(value, replacer, space);
- },
- pull(controller) {
- const { value, done } = this.generator.next();
- if (done) {
- controller.close();
- } else {
- controller.enqueue(value);
- }
- },
- cancel() {
- this.generator = null;
- }
- });
- };
|