Skip to content

Commit 41e4585

Browse files
committed
Initial commit
0 parents  commit 41e4585

16 files changed

Lines changed: 6615 additions & 0 deletions

.editorconfig

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# How-to with your editor: http://editorconfig.org/#download
4+
5+
# top-most EditorConfig file
6+
root = true
7+
8+
# Unix-style newlines with a newline ending every file
9+
[*]
10+
end_of_line = lf
11+
indent_style = tab
12+
insert_final_newline = true
13+
14+
[{Dockerfile,Procfile}]
15+
trim_trailing_whitespace = true
16+
17+
# Standard at: https://github.com/felixge/node-style-guide
18+
[{*.js,*.json}]
19+
trim_trailing_whitespace = true
20+
quote_type = single
21+
curly_bracket_next_line = false
22+
spaces_around_operators = true
23+
space_after_control_statements = true
24+
space_after_anonymous_functions = false
25+
spaces_in_brackets = false
26+
27+
[Dockerfile]
28+
indent_style = space
29+
indent_size = 4
30+
31+
[*.md]
32+
indent_style = space
33+
indent_size = 2
34+
35+
[*.json]
36+
indent_style = space
37+
indent_size = 4
38+
insert_final_newline = false
39+
40+
[*.yml]
41+
indent_style = space
42+
indent_size = 2
43+
44+
[package.json]
45+
indent_style = space
46+
indent_size = 2

.github/workflows/audit.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Audit
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
audit:
11+
name: "Audit"
12+
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
node-version: [22.x]
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Use Node.js ${{ matrix.node-version }}
19+
uses: actions/setup-node@v4
20+
with:
21+
node-version: ${{ matrix.node-version }}
22+
- run: npm audit --parseable --production --audit-level=moderate

.github/workflows/coveralls.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Coveralls
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
node-version: [22.x]
14+
steps:
15+
- uses: actions/checkout@v4
16+
- name: Use Node.js ${{ matrix.node-version }}
17+
uses: actions/setup-node@v4
18+
with:
19+
node-version: ${{ matrix.node-version }}
20+
- run: npm ci --no-optional
21+
env:
22+
CI: true
23+
- run: npm run test:coverage
24+
- name: Coveralls
25+
uses: coverallsapp/github-action@v2

.github/workflows/publish.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Publish to NPM
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*.*.*'
7+
8+
jobs:
9+
publish:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
16+
- name: Set up Node.js
17+
uses: actions/setup-node@v4
18+
with:
19+
node-version: '22'
20+
registry-url: 'https://registry.npmjs.org'
21+
22+
- name: Install dependencies
23+
run: npm ci --no-optional
24+
25+
- name: Publish pre-release to NPM
26+
if: contains(github.ref_name, '-')
27+
env:
28+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
29+
run: npm publish --tag next
30+
31+
- name: Publish release to NPM
32+
if: "!contains(github.ref_name, '-')"
33+
env:
34+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
35+
run: npm publish

.github/workflows/tests.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
name: "Tests"
12+
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
node-version: [18.x, 20.x, 22.x]
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Use Node.js ${{ matrix.node-version }}
19+
uses: actions/setup-node@v4
20+
with:
21+
node-version: ${{ matrix.node-version }}
22+
- run: npm ci --no-optional
23+
env:
24+
CI: true
25+
- run: npm run test

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/.vscode/
2+
/coverage/
3+
/dist/
4+
/node_modules/
5+
.DS_Store
6+
*.sublime-project
7+
*.sublime-workspace
8+
*.tgz
9+
ehthumbs.db
10+
Icon?
11+
Thumbs.db
12+
tsconfig.tsbuildinfo

.npmignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.github/
2+
.vscode/
3+
coverage/
4+
tests/
5+
.editorconfig
6+
.eslintrc.json
7+
.gitignore
8+
tsconfig.json
9+
jest.config.ts
10+
*.tgz
11+
tsconfig.tsbuildinfo

README.MD

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
Async Iterable Buffer
2+
=====================
3+
4+
[![NPM Version](https://img.shields.io/npm/v/async-iterable-buffer.svg)](https://www.npmjs.com/package/async-iterable-buffer)
5+
[![Audit](https://github.com/snatalenko/async-iterable-buffer/actions/workflows/audit.yml/badge.svg)](https://github.com/snatalenko/async-iterable-buffer/actions/workflows/audit.yml)
6+
[![Tests](https://github.com/snatalenko/async-iterable-buffer/actions/workflows/tests.yml/badge.svg)](https://github.com/snatalenko/async-iterable-buffer/actions/workflows/tests.yml)
7+
[![Coverage Status](https://coveralls.io/repos/github/snatalenko/async-iterable-buffer/badge.svg?branch=main)](https://coveralls.io/github/snatalenko/async-iterable-buffer?branch=main)
8+
[![NPM Downloads](https://img.shields.io/npm/dm/async-iterable-buffer.svg)](https://www.npmjs.com/package/async-iterable-buffer)
9+
10+
11+
AsyncIterableBuffer is a lightweight, stateful asynchronous buffer that allows you to push values and consume them using async iteration (e.g. `for await...of`). It’s ideal for bridging event-driven or callback-based code with modern async/await patterns.
12+
13+
## Features
14+
15+
- **Asynchronous Iteration:** Implements the `AsyncIterableIterator` interface.
16+
- **Push and Consume:** Dynamically push values into the buffer and consume them asynchronously.
17+
- **Graceful Termination:** Signal completion with an `end()` method and handle early termination via `return()`.
18+
- **TypeScript Support:** Fully typed for TypeScript projects.
19+
- **Lightweight:** No dependencies.
20+
21+
## Installation
22+
23+
```bash
24+
npm install async-iterable-buffer
25+
```
26+
27+
## Usage
28+
29+
### Basic Example
30+
31+
```ts
32+
import { AsyncIterableBuffer } from 'async-iterable-buffer';
33+
34+
const buffer = new AsyncIterableBuffer<number>();
35+
36+
// Push some values into the buffer.
37+
buffer.push(1);
38+
buffer.push(2);
39+
buffer.push(3);
40+
41+
// Signal that no more values will be pushed.
42+
buffer.end();
43+
44+
// Consume the buffer with async iteration.
45+
(async () => {
46+
for await (const num of buffer) {
47+
console.log(num); // Outputs: 1, 2, 3
48+
}
49+
})();
50+
```
51+
52+
53+
### Advanced Usage with Early Termination
54+
55+
```ts
56+
import { AsyncIterableBuffer } from 'async-iterable-buffer';
57+
58+
const buffer = new AsyncIterableBuffer<string>();
59+
60+
// Start an async iterator.
61+
(async () => {
62+
for await (const value of buffer) {
63+
console.log(value);
64+
if (value === 'stop') {
65+
// Terminate iteration early.
66+
await buffer.return();
67+
}
68+
}
69+
})();
70+
71+
// Push values asynchronously.
72+
buffer.push('first');
73+
buffer.push('stop');
74+
buffer.push('ignored'); // This value won't be processed since the buffer is closed after "stop".
75+
```
76+
77+
78+
### API
79+
80+
API
81+
82+
new AsyncIterableBuffer<T>()
83+
84+
Creates a new instance of the asynchronous buffer.
85+
86+
Methods
87+
- **push(value: T): void**
88+
Pushes a new value into the buffer. Throws an error if the buffer has been ended.
89+
- **end(): void**
90+
Marks the buffer as completed and resolves any pending requests with { done: true }.
91+
- **next(): Promise<IteratorResult<T>>**
92+
Returns a promise that resolves to the next buffered value. If the buffer is empty but not ended, it waits until a value is pushed.
93+
- **return(): Promise<IteratorResult<T>>**
94+
Ends the buffer and returns a result indicating that the iterator is done. This is useful for early termination of iteration.
95+
- **[Symbol.asyncIterator](): AsyncIterableIterator<T>**
96+
Returns the async iterator itself, enabling for await...of loops.

0 commit comments

Comments
 (0)