Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
yield-stream
Advanced tools
yield-stream
A small library for switching between streams, generators, and arrays.
import { GeneratorFn, StreamGenerator } from "./types";
/**
* `compose(f, g, h, ...)` returns a generator function `G(data)` that yields
* all `(f · g · h · ...)(data)`.
*/
export const compose = <T>(
...generators: GeneratorFn<T>[]
) => {
return generators.reduce(
(prev, next) => async function* (data) {
for await (const chunk of prev(data)) {
yield* next(chunk);
}
},
);
};
/**
* Runs each chunk through all of the given transforms.
*/
export const pipeline = <T>(
stream: ReadableStream<T>,
...transforms: GeneratorFn<T>[]
) => {
const composed = compose(...transforms);
return generateStream(
async function* () {
for await (const chunk of yieldStream(stream)) {
yield* composed(chunk);
}
}
);
};
/**
* Iterates over a stream, yielding each chunk.
*/
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;
}
};
/**
* Creates a ReadableStream from a generator function.
*/
export const generateStream = <T, TReturn, D>(
G: StreamGenerator<D, T, TReturn>,
data?: D
) => {
return new ReadableStream<T>({
async start(controller) {
for await (const chunk of G(data)) {
controller.enqueue(chunk);
}
controller.close();
},
});
};
/**
* Creates a ReadableStream that yields all values in an array.
*/
export const streamArray = <T>(array: T[]) => {
return generateStream(function* () {
for (const item of array) {
yield item;
}
});
};
FAQs
[**Github**](https://github.com/gptlabs/yield-stream) | [**NPM**](https://npmjs.com/package/yield-stream) | [**Docs**](https://yield-stream.vercel.app)
The npm package yield-stream receives a total of 1,447 weekly downloads. As such, yield-stream popularity was classified as popular.
We found that yield-stream demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.