@cjser/multicast-stream 1.0.0-cjser.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,61 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // packages/@cjser/multicast-stream.tmp-26-1778153610618/index.js
20
+ var index_exports = {};
21
+ __export(index_exports, {
22
+ default: () => multicastStream
23
+ });
24
+ module.exports = __toCommonJS(index_exports);
25
+ var import_node_stream = require("node:stream");
26
+ function multicastStream(sourceStream) {
27
+ const consumers = /* @__PURE__ */ new Set();
28
+ let isStarted = false;
29
+ const start = () => {
30
+ if (isStarted) {
31
+ return;
32
+ }
33
+ isStarted = true;
34
+ sourceStream.on("data", (chunk) => {
35
+ for (const consumer of consumers) {
36
+ consumer.write(chunk);
37
+ }
38
+ });
39
+ sourceStream.on("end", () => {
40
+ for (const consumer of consumers) {
41
+ consumer.end();
42
+ }
43
+ });
44
+ sourceStream.on("error", (error) => {
45
+ for (const consumer of consumers) {
46
+ consumer.destroy(error);
47
+ }
48
+ });
49
+ };
50
+ return () => {
51
+ const consumer = new import_node_stream.PassThrough();
52
+ consumers.add(consumer);
53
+ consumer.on("close", () => {
54
+ consumers.delete(consumer);
55
+ });
56
+ if (!isStarted) {
57
+ start();
58
+ }
59
+ return consumer;
60
+ };
61
+ }
package/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import {type Readable, type PassThrough} from 'node:stream';
2
+
3
+ /**
4
+ Creates a function that returns independent streams for each consumer.
5
+
6
+ @param sourceStream - The source stream to multicast.
7
+ @returns A function that returns a new `PassThrough` stream for each consumer.
8
+ */
9
+ export default function multicastStream(sourceStream: Readable): () => PassThrough;
package/index.js ADDED
@@ -0,0 +1,47 @@
1
+ import {PassThrough} from 'node:stream';
2
+
3
+ export default function multicastStream(sourceStream) {
4
+ const consumers = new Set();
5
+ let isStarted = false;
6
+
7
+ const start = () => {
8
+ if (isStarted) {
9
+ return;
10
+ }
11
+
12
+ isStarted = true;
13
+
14
+ sourceStream.on('data', chunk => {
15
+ for (const consumer of consumers) {
16
+ consumer.write(chunk);
17
+ }
18
+ });
19
+
20
+ sourceStream.on('end', () => {
21
+ for (const consumer of consumers) {
22
+ consumer.end();
23
+ }
24
+ });
25
+
26
+ sourceStream.on('error', error => {
27
+ for (const consumer of consumers) {
28
+ consumer.destroy(error);
29
+ }
30
+ });
31
+ };
32
+
33
+ return () => {
34
+ const consumer = new PassThrough();
35
+ consumers.add(consumer);
36
+
37
+ consumer.on('close', () => {
38
+ consumers.delete(consumer);
39
+ });
40
+
41
+ if (!isStarted) {
42
+ start();
43
+ }
44
+
45
+ return consumer;
46
+ };
47
+ }
package/license ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "@cjser/multicast-stream",
3
+ "version": "1.0.0-cjser.2",
4
+ "description": "Create a multicast stream that lets multiple consumers independently read the same data",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://code.moenext.com/3rdeye/cjser.git"
9
+ },
10
+ "funding": "https://github.com/sponsors/sindresorhus",
11
+ "author": {
12
+ "name": "Sindre Sorhus",
13
+ "email": "sindresorhus@gmail.com",
14
+ "url": "https://sindresorhus.com"
15
+ },
16
+ "type": "module",
17
+ "exports": {
18
+ "types": "./index.d.ts",
19
+ "require": "./dist-cjser/index.cjs",
20
+ "default": "./index.js"
21
+ },
22
+ "sideEffects": false,
23
+ "engines": {
24
+ "node": ">=20"
25
+ },
26
+ "scripts": {
27
+ "test": "xo && ava && tsc index.d.ts"
28
+ },
29
+ "files": [
30
+ "index.js",
31
+ "index.d.ts",
32
+ "dist-cjser"
33
+ ],
34
+ "keywords": [
35
+ "multicast",
36
+ "broadcast",
37
+ "split",
38
+ "stream",
39
+ "readable",
40
+ "passthrough",
41
+ "multi",
42
+ "multiple",
43
+ "consumers",
44
+ "consumption",
45
+ "streaming",
46
+ "streams",
47
+ "clone"
48
+ ],
49
+ "devDependencies": {
50
+ "ava": "^6.4.0",
51
+ "typescript": "^5.8.3",
52
+ "xo": "^0.60.0"
53
+ },
54
+ "types": "./index.d.ts",
55
+ "main": "./dist-cjser/index.cjs",
56
+ "cjser": {
57
+ "sourceVersion": "1.0.0",
58
+ "cjserVersion": 2,
59
+ "original": {
60
+ "name": "multicast-stream",
61
+ "version": "1.0.0",
62
+ "exports": {
63
+ "types": "./index.d.ts",
64
+ "default": "./index.js"
65
+ },
66
+ "repository": "sindresorhus/multicast-stream",
67
+ "files": [
68
+ "index.js",
69
+ "index.d.ts"
70
+ ],
71
+ "scripts": {
72
+ "test": "xo && ava && tsc index.d.ts"
73
+ }
74
+ }
75
+ }
76
+ }
package/readme.md ADDED
@@ -0,0 +1,68 @@
1
+ <h1 align="center" title="multicast-stream">
2
+ <img src="media/logo.jpg" alt="multicast-stream logo">
3
+ </h1>
4
+
5
+ > Create a multicast stream that lets multiple consumers independently read the same data
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install multicast-stream
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ **Without this package**
16
+
17
+ Using [`Readable#text()`](https://nodejs.org/api/webstreams.html#streamconsumerstextstream) on a stream can only work with a single consumer. If you try to read the stream with multiple consumers, it will be empty as the stream can only be read once.
18
+
19
+ ```js
20
+ import {Readable} from 'node:stream';
21
+ import {text} from 'node:stream/consumers';
22
+
23
+ const sourceStream = Readable.from(['Hello', ' ', 'World']);
24
+ const [result1, result2] = await Promise.all([text(sourceStream), text(sourceStream)]);
25
+
26
+ console.log(result1); // 'Hello World'
27
+ console.log(result2); // ''
28
+ ```
29
+
30
+ **With this package**
31
+
32
+ This package allows multiple consumers to independently read the same data from a single source stream.
33
+
34
+ ```js
35
+ import {Readable} from 'node:stream';
36
+ import {text} from 'node:stream/consumers';
37
+ import multicastStream from 'multicast-stream';
38
+
39
+ const sourceStream = Readable.from(['Hello', ' ', 'World']);
40
+ const createConsumer = multicastStream(sourceStream);
41
+
42
+ const consumer1 = createConsumer();
43
+ const consumer2 = createConsumer();
44
+
45
+ const [result1, result2] = await Promise.all([text(consumer1), text(consumer2)]);
46
+
47
+ console.log(result1); // 'Hello World'
48
+ console.log(result2); // 'Hello World'
49
+ ```
50
+
51
+ ## API
52
+
53
+ ### `multicastStream(sourceStream)`
54
+
55
+ Creates a function that returns independent streams for each consumer.
56
+
57
+ #### Parameters
58
+
59
+ - `sourceStream` (`Readable`): The source stream to multicast.
60
+
61
+ #### Returns
62
+
63
+ - `() => PassThrough`: A function that returns a new `PassThrough` stream for each consumer.
64
+
65
+ ## cjser
66
+
67
+ This package is a CommonJS-compatible build generated by cjser for projects that still need `require()` support. The source version matches the original npm package version, with a cjser prerelease suffix for this generated build.
68
+ Original repository: https://github.com/sindresorhus/multicast-stream