Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

yield-stream

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

yield-stream - npm Package Compare versions

Comparing version 2.3.0 to 3.0.0

dist/lib/edge.d.ts

7

dist/index.d.ts

@@ -1,2 +0,5 @@

export * from "./lib";
export * from "./types";
/**
* `.` entrypoint is Edge by default. NodeJS.ReadableStream version available at
* `./node`.
*/
export * from "./platforms/edge";

@@ -1,1 +0,1 @@

export*from"./lib.js";export*from"./types.js";
export*from"./platforms/edge.js";
{
"name": "yield-stream",
"version": "2.3.0",
"version": "3.0.0",
"license": "MIT",

@@ -12,6 +12,9 @@ "type": "module",

"files": [
"dist"
"dist",
"node.d.ts",
"node.js"
],
"exports": {
"./package.json": "./package.json",
"./node": "./node.js",
".": "./dist/index.js",

@@ -25,4 +28,5 @@ "./*": "./dist/*/index.js"

"build": "tsmodule build",
"test": "ava",
"pretest": "yarn build",
"pretest": "npm pack --pack-destination test",
"test": "yarn build && ava",
"prepare": "yarn --cwd test",
"prepublishOnly": "yarn test",

@@ -46,2 +50,2 @@ "lint": "eslint src --fix"

}
}
}
# `yield-stream`
- **Docs: https://yield-stream.vercel.app**
[**Github**](https://github.com/gptlabs/yield-stream) |
[**NPM**](https://npmjs.com/package/yield-stream) |
[**Docs**](https://yield-stream.vercel.app)
A small library for switching between streams, generators, and arrays.
A small library for switching between streams, generators, and arrays. See docs
for details.
### Note: Using `NodeJS.ReadableStream`
By default, this library uses WHATWG `ReadableStream`, which is only available
on Node 18+. If you are on an older version of Node or otherwise need to use
`NodeJS.ReadableStream`, import from:
```ts
/**
* `compose(f, g, h, ...)` returns a generator function `G(data)` that yields
* all `(f · g · h · ...)(data)`.
*
* @note Used to compose multiple transforms into a `pipeline`.
*/
export const compose = <T>(
...generators: GeneratorFn<T>[]
): GeneratorFn<T> => {
return generators.reduce(
(prev, next) => async function* (data) {
for await (const chunk of prev(data)) {
yield* next(chunk);
}
},
);
};
/**
* Accepts a stream and transforms and returns a stream of the transformed
* chunks. Transforms can yield multiple chunks per input chunk.
*/
export const pipeline = <T>(
stream: ReadableStream<T>,
...transforms: GeneratorFn<T>[]
): ReadableStream<T> => {
const composed = compose(...transforms);
return generateStream(
async function* () {
for await (const chunk of yieldStream(stream)) {
yield* composed(chunk);
}
}
);
};
/**
* Accepts a stream and yields all of its chunks.
*/
export const yieldStream = async function* <T>(
stream: ReadableStream<T>,
controller?: AbortController
) {
const reader = stream.getReader();
while (true) {
if (controller?.signal.aborted) {
break;
}
const { done, value } = await reader.read();
if (done) {
break;
}
yield value;
}
};
/**
* Accepts a generator function and streams its outputs.
*/
export const generateStream = <T, TReturn, D>(
G: StreamGenerator<D, T, TReturn>,
data?: D
): ReadableStream<T> => {
return new ReadableStream<T>({
async start(controller) {
for await (const chunk of G(data)) {
controller.enqueue(chunk);
}
controller.close();
},
});
};
/**
* Accepts an array and returns a stream of its items.
*/
export const streamArray = <T>(array: T[]): ReadableStream<T> => {
return generateStream(function* () {
for (const item of array) {
yield item;
}
});
};
/**
* Accepts a stream and yields a growing buffer of all chunks received.
*/
export const buffer = async function* <T>(stream: ReadableStream<T>) {
const buffer: T[] = [];
for await (const chunk of yieldStream(stream)) {
buffer.push(chunk);
yield buffer;
}
};
import { yieldStream } from "yield-stream/node";
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc