source: imaps-frontend/node_modules/@discoveryjs/json-ext/cjs/web-streams.cjs@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[79a0317]1'use strict';
2
3const parseChunked = require('./parse-chunked.cjs');
4const stringifyChunked = require('./stringify-chunked.cjs');
5const utils = require('./utils.cjs');
6
7/* eslint-env browser */
8
9function parseFromWebStream(stream) {
10 // 2024/6/17: currently, an @@asyncIterator on a ReadableStream is not widely supported,
11 // therefore use a fallback using a reader
12 // https://caniuse.com/mdn-api_readablestream_--asynciterator
13 return parseChunked.parseChunked(utils.isIterable(stream) ? stream : async function*() {
14 const reader = stream.getReader();
15
16 try {
17 while (true) {
18 const { value, done } = await reader.read();
19
20 if (done) {
21 break;
22 }
23
24 yield value;
25 }
26 } finally {
27 reader.releaseLock();
28 }
29 });
30}
31
32function createStringifyWebStream(value, replacer, space) {
33 // 2024/6/17: the ReadableStream.from() static method is supported
34 // in Node.js 20.6+ and Firefox only
35 if (typeof ReadableStream.from === 'function') {
36 return ReadableStream.from(stringifyChunked.stringifyChunked(value, replacer, space));
37 }
38
39 // emulate ReadableStream.from()
40 return new ReadableStream({
41 start() {
42 this.generator = stringifyChunked.stringifyChunked(value, replacer, space);
43 },
44 pull(controller) {
45 const { value, done } = this.generator.next();
46
47 if (done) {
48 controller.close();
49 } else {
50 controller.enqueue(value);
51 }
52 },
53 cancel() {
54 this.generator = null;
55 }
56 });
57}
58
59exports.createStringifyWebStream = createStringifyWebStream;
60exports.parseFromWebStream = parseFromWebStream;
Note: See TracBrowser for help on using the repository browser.