@philiprehberger/async-batcher 0.1.5
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.
- package/LICENSE +21 -0
- package/README.md +80 -0
- package/dist/index.cjs +71 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +69 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 philiprehberger
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# @philiprehberger/async-batcher
|
|
2
|
+
|
|
3
|
+
[](https://github.com/philiprehberger/ts-async-batcher/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/@philiprehberger/async-batcher)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
Automatic batching and deduplication for async operations
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @philiprehberger/async-batcher
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { createBatcher } from '@philiprehberger/async-batcher';
|
|
19
|
+
|
|
20
|
+
const userLoader = createBatcher(async (ids: string[]) => {
|
|
21
|
+
// Called once with all collected IDs
|
|
22
|
+
return db.users.findMany({ where: { id: { in: ids } } });
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// These are automatically batched into a single call
|
|
26
|
+
const user1 = await userLoader.load('user-1');
|
|
27
|
+
const user2 = await userLoader.load('user-2');
|
|
28
|
+
|
|
29
|
+
// Load multiple at once
|
|
30
|
+
const users = await userLoader.loadMany(['user-3', 'user-4']);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Options
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
const loader = createBatcher(batchFn, {
|
|
37
|
+
maxBatchSize: 100, // Flush when batch reaches this size (default: 100)
|
|
38
|
+
windowMs: 10, // Batch window in milliseconds (default: 10)
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### How It Works
|
|
43
|
+
|
|
44
|
+
1. `load(key)` adds the key to an internal queue and returns a promise
|
|
45
|
+
2. After `windowMs` or when `maxBatchSize` is reached, the queue is flushed
|
|
46
|
+
3. Duplicate keys within a batch window are deduplicated — the batch function receives unique keys only
|
|
47
|
+
4. Results are matched back to callers by index (batch function must return results in the same order as keys)
|
|
48
|
+
|
|
49
|
+
## API
|
|
50
|
+
|
|
51
|
+
| Export | Description |
|
|
52
|
+
|--------|-------------|
|
|
53
|
+
| `createBatcher(batchFn, options?)` | Create a new batcher instance |
|
|
54
|
+
|
|
55
|
+
### `Batcher<K, V>`
|
|
56
|
+
|
|
57
|
+
| Method | Description |
|
|
58
|
+
|--------|-------------|
|
|
59
|
+
| `load(key)` | Load a single value, batched automatically |
|
|
60
|
+
| `loadMany(keys)` | Load multiple values, returns `Promise<V[]>` |
|
|
61
|
+
|
|
62
|
+
### `BatcherOptions`
|
|
63
|
+
|
|
64
|
+
| Option | Type | Default | Description |
|
|
65
|
+
|--------|------|---------|-------------|
|
|
66
|
+
| `maxBatchSize` | `number` | `100` | Max keys per batch |
|
|
67
|
+
| `windowMs` | `number` | `10` | Batch collection window in ms |
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
## Development
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
npm install
|
|
74
|
+
npm run build
|
|
75
|
+
npm test
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/batcher.ts
|
|
4
|
+
function createBatcher(batchFn, options = {}) {
|
|
5
|
+
const { maxBatchSize = 100, windowMs = 10 } = options;
|
|
6
|
+
let queue = [];
|
|
7
|
+
let timer = null;
|
|
8
|
+
function scheduleFlush() {
|
|
9
|
+
if (timer === null) {
|
|
10
|
+
timer = setTimeout(flush, windowMs);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function flush() {
|
|
14
|
+
timer = null;
|
|
15
|
+
if (queue.length === 0) return;
|
|
16
|
+
const batch = queue;
|
|
17
|
+
queue = [];
|
|
18
|
+
const keyMap = /* @__PURE__ */ new Map();
|
|
19
|
+
const uniqueKeys = [];
|
|
20
|
+
for (const entry of batch) {
|
|
21
|
+
if (!keyMap.has(entry.key)) {
|
|
22
|
+
keyMap.set(entry.key, uniqueKeys.length);
|
|
23
|
+
uniqueKeys.push(entry.key);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
batchFn(uniqueKeys).then(
|
|
27
|
+
(results) => {
|
|
28
|
+
if (results.length !== uniqueKeys.length) {
|
|
29
|
+
const err = new Error(
|
|
30
|
+
`Batch function returned ${results.length} results for ${uniqueKeys.length} keys`
|
|
31
|
+
);
|
|
32
|
+
for (const entry of batch) {
|
|
33
|
+
entry.reject(err);
|
|
34
|
+
}
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
for (const entry of batch) {
|
|
38
|
+
const index = keyMap.get(entry.key);
|
|
39
|
+
entry.resolve(results[index]);
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
(error) => {
|
|
43
|
+
for (const entry of batch) {
|
|
44
|
+
entry.reject(error);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
function load(key) {
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
queue.push({ key, resolve, reject });
|
|
52
|
+
if (queue.length >= maxBatchSize) {
|
|
53
|
+
if (timer !== null) {
|
|
54
|
+
clearTimeout(timer);
|
|
55
|
+
timer = null;
|
|
56
|
+
}
|
|
57
|
+
flush();
|
|
58
|
+
} else {
|
|
59
|
+
scheduleFlush();
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
function loadMany(keys) {
|
|
64
|
+
return Promise.all(keys.map((k) => load(k)));
|
|
65
|
+
}
|
|
66
|
+
return { load, loadMany };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
exports.createBatcher = createBatcher;
|
|
70
|
+
//# sourceMappingURL=index.cjs.map
|
|
71
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/batcher.ts"],"names":[],"mappings":";;;AAQO,SAAS,aAAA,CACd,OAAA,E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AAA,UAClB;AACA,UAAA;AAAA,QACF;AAEA,QAAA,KAAA,MAAW,SAAS,KAAA,EAAO;AACzB,UAAA,MAAM,KAAA,GAAQ,MAAA,CAAO,GAAA,CAAI,KAAA,CAAM,GAAG,CAAA;AAClC,UAAA,KAAA,CAAM,OAAA,CAAQ,OAAA,CAAQ,KAAK,CAAC,CAAA;AAAA,QAC9B;AAAA,MACF,CAAA;AAAA,MACA,CAAC,KAAA,KAAU;AACT,QAAA,KAAA,MAAW,SAAS,KAAA,EAAO;AACzB,UAAA,KAAA,CAAM,OAAO,KAAK,CAAA;AAAA,QACpB;AAAA,MACF;AAAA,KACF;AAAA,EACF;AAEA,EAAA,SAAS,KAAK,GAAA,EAAoB;AAChC,IAAA,OAAO,IAAI,OAAA,CAAW,CAAC,OAAA,EAAS,MAAA,KAAW;AACzC,MAAA,KAAA,CAAM,IAAA,CAAK,EAAE,GAAA,EAAK,OAAA,EAAS,QAAQ,CAAA;AAEnC,MAAA,IAAI,KAAA,CAAM,UAAU,YAAA,EAAc;AAChC,QAAA,IAAI,UAAU,IAAA,EAAM;AAClB,UAAA,YAAA,CAAa,KAAK,CAAA;AAClB,UAAA,KAAA,GAAQ,IAAA;AAAA,QACV;AACA,QAAA,KAAA,EAAM;AAAA,MACR,CAAA,MAAO;AACL,QAAA,aAAA,EAAc;AAAA,MAChB;AAAA,IACF,CAAC,CAAA;AAAA,EACH;AAEA,EAAA,SAAS,SAAS,IAAA,EAAyB;AACzC,IAAA,OAAO,OAAA,CAAQ,IAAI,IAAA,CAAK,GAAA,CAAI,CAAC,CAAA,KAAM,IAAA,CAAK,CAAC,CAAC,CAAC,CAAA;AAAA,EAC7C;AAEA,EAAA,OAAO,EAAE,MAAM,QAAA,EAAS;AAC1B","file":"index.cjs","sourcesContent":["import type { BatcherOptions, Batcher } from './types.js';\n\ninterface QueueEntry<K, V> {\n key: K;\n resolve: (value: V) => void;\n reject: (error: unknown) => void;\n}\n\nexport function createBatcher<K, V>(\n batchFn: (keys: K[]) => Promise<V[]>,\n options: BatcherOptions = {},\n): Batcher<K, V> {\n const { maxBatchSize = 100, windowMs = 10 } = options;\n\n let queue: QueueEntry<K, V>[] = [];\n let timer: ReturnType<typeof setTimeout> | null = null;\n\n function scheduleFlush(): void {\n if (timer === null) {\n timer = setTimeout(flush, windowMs);\n }\n }\n\n function flush(): void {\n timer = null;\n if (queue.length === 0) return;\n\n const batch = queue;\n queue = [];\n\n const keyMap = new Map<K, number>();\n const uniqueKeys: K[] = [];\n\n for (const entry of batch) {\n if (!keyMap.has(entry.key)) {\n keyMap.set(entry.key, uniqueKeys.length);\n uniqueKeys.push(entry.key);\n }\n }\n\n batchFn(uniqueKeys).then(\n (results) => {\n if (results.length !== uniqueKeys.length) {\n const err = new Error(\n `Batch function returned ${results.length} results for ${uniqueKeys.length} keys`,\n );\n for (const entry of batch) {\n entry.reject(err);\n }\n return;\n }\n\n for (const entry of batch) {\n const index = keyMap.get(entry.key)!;\n entry.resolve(results[index]);\n }\n },\n (error) => {\n for (const entry of batch) {\n entry.reject(error);\n }\n },\n );\n }\n\n function load(key: K): Promise<V> {\n return new Promise<V>((resolve, reject) => {\n queue.push({ key, resolve, reject });\n\n if (queue.length >= maxBatchSize) {\n if (timer !== null) {\n clearTimeout(timer);\n timer = null;\n }\n flush();\n } else {\n scheduleFlush();\n }\n });\n }\n\n function loadMany(keys: K[]): Promise<V[]> {\n return Promise.all(keys.map((k) => load(k)));\n }\n\n return { load, loadMany };\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface BatcherOptions {
|
|
2
|
+
maxBatchSize?: number;
|
|
3
|
+
windowMs?: number;
|
|
4
|
+
}
|
|
5
|
+
interface Batcher<K, V> {
|
|
6
|
+
load(key: K): Promise<V>;
|
|
7
|
+
loadMany(keys: K[]): Promise<V[]>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
declare function createBatcher<K, V>(batchFn: (keys: K[]) => Promise<V[]>, options?: BatcherOptions): Batcher<K, V>;
|
|
11
|
+
|
|
12
|
+
export { type Batcher, type BatcherOptions, createBatcher };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface BatcherOptions {
|
|
2
|
+
maxBatchSize?: number;
|
|
3
|
+
windowMs?: number;
|
|
4
|
+
}
|
|
5
|
+
interface Batcher<K, V> {
|
|
6
|
+
load(key: K): Promise<V>;
|
|
7
|
+
loadMany(keys: K[]): Promise<V[]>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
declare function createBatcher<K, V>(batchFn: (keys: K[]) => Promise<V[]>, options?: BatcherOptions): Batcher<K, V>;
|
|
11
|
+
|
|
12
|
+
export { type Batcher, type BatcherOptions, createBatcher };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// src/batcher.ts
|
|
2
|
+
function createBatcher(batchFn, options = {}) {
|
|
3
|
+
const { maxBatchSize = 100, windowMs = 10 } = options;
|
|
4
|
+
let queue = [];
|
|
5
|
+
let timer = null;
|
|
6
|
+
function scheduleFlush() {
|
|
7
|
+
if (timer === null) {
|
|
8
|
+
timer = setTimeout(flush, windowMs);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function flush() {
|
|
12
|
+
timer = null;
|
|
13
|
+
if (queue.length === 0) return;
|
|
14
|
+
const batch = queue;
|
|
15
|
+
queue = [];
|
|
16
|
+
const keyMap = /* @__PURE__ */ new Map();
|
|
17
|
+
const uniqueKeys = [];
|
|
18
|
+
for (const entry of batch) {
|
|
19
|
+
if (!keyMap.has(entry.key)) {
|
|
20
|
+
keyMap.set(entry.key, uniqueKeys.length);
|
|
21
|
+
uniqueKeys.push(entry.key);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
batchFn(uniqueKeys).then(
|
|
25
|
+
(results) => {
|
|
26
|
+
if (results.length !== uniqueKeys.length) {
|
|
27
|
+
const err = new Error(
|
|
28
|
+
`Batch function returned ${results.length} results for ${uniqueKeys.length} keys`
|
|
29
|
+
);
|
|
30
|
+
for (const entry of batch) {
|
|
31
|
+
entry.reject(err);
|
|
32
|
+
}
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
for (const entry of batch) {
|
|
36
|
+
const index = keyMap.get(entry.key);
|
|
37
|
+
entry.resolve(results[index]);
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
(error) => {
|
|
41
|
+
for (const entry of batch) {
|
|
42
|
+
entry.reject(error);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
function load(key) {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
queue.push({ key, resolve, reject });
|
|
50
|
+
if (queue.length >= maxBatchSize) {
|
|
51
|
+
if (timer !== null) {
|
|
52
|
+
clearTimeout(timer);
|
|
53
|
+
timer = null;
|
|
54
|
+
}
|
|
55
|
+
flush();
|
|
56
|
+
} else {
|
|
57
|
+
scheduleFlush();
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function loadMany(keys) {
|
|
62
|
+
return Promise.all(keys.map((k) => load(k)));
|
|
63
|
+
}
|
|
64
|
+
return { load, loadMany };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { createBatcher };
|
|
68
|
+
//# sourceMappingURL=index.js.map
|
|
69
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/batcher.ts"],"names":[],"mappings":";AAQO,SAAS,aAAA,CACd,OAAA,EACA,OAAA,GAA0B,EAAC,EACZ;AACf,EAAA,MAAM,EAAE,YAAA,GAAe,GAAA,EAAK,QAAA,GAAW,IAAG,GAAI,OAAA;AAE9C,EAAA,IAAI,QAA4B,EAAC;AACjC,EAAA,IAAI,KAAA,GAA8C,IAAA;AAElD,EAAA,SAAS,aAAA,GAAsB;AAC7B,IAAA,IAAI,UAAU,IAAA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AAA,MACF,CAAA;AAAA,MACA,CAAC,KAAA,KAAU;AACT,QAAA,KAAA,MAAW,SAAS,KAAA,EAAO;AACzB,UAAA,KAAA,CAAM,OAAO,KAAK,CAAA;AAAA,QACpB;AAAA,MACF;AAAA,KACF;AAAA,EACF;AAEA,EAAA,SAAS,KAAK,GAAA,EAAoB;AAChC,IAAA,OAAO,IAAI,OAAA,CAAW,CAAC,OAAA,EAAS,MAAA,KAAW;AACzC,MAAA,KAAA,CAAM,IAAA,CAAK,EAAE,GAAA,EAAK,OAAA,EAAS,QAAQ,CAAA;AAEnC,MAAA,IAAI,KAAA,CAAM,UAAU,YAAA,EAAc;AAChC,QAAA,IAAI,UAAU,IAAA,EAAM;AAClB,UAAA,YAAA,CAAa,KAAK,CAAA;AAClB,UAAA,KAAA,GAAQ,IAAA;AAAA,QACV;AACA,QAAA,KAAA,EAAM;AAAA,MACR,CAAA,MAAO;AACL,QAAA,aAAA,EAAc;AAAA,MAChB;AAAA,IACF,CAAC,CAAA;AAAA,EACH;AAEA,EAAA,SAAS,SAAS,IAAA,EAAyB;AACzC,IAAA,OAAO,OAAA,CAAQ,IAAI,IAAA,CAAK,GAAA,CAAI,CAAC,CAAA,KAAM,IAAA,CAAK,CAAC,CAAC,CAAC,CAAA;AAAA,EAC7C;AAEA,EAAA,OAAO,EAAE,MAAM,QAAA,EAAS;AAC1B","file":"index.js","sourcesContent":["import type { BatcherOptions, Batcher } from './types.js';\n\ninterface QueueEntry<K, V> {\n key: K;\n resolve: (value: V) => void;\n reject: (error: unknown) => void;\n}\n\nexport function createBatcher<K, V>(\n batchFn: (keys: K[]) => Promise<V[]>,\n options: BatcherOptions = {},\n): Batcher<K, V> {\n const { maxBatchSize = 100, windowMs = 10 } = options;\n\n let queue: QueueEntry<K, V>[] = [];\n let timer: ReturnType<typeof setTimeout> | null = null;\n\n function scheduleFlush(): void {\n if (timer === null) {\n timer = setTimeout(flush, windowMs);\n }\n }\n\n function flush(): void {\n timer = null;\n if (queue.length === 0) return;\n\n const batch = queue;\n queue = [];\n\n const keyMap = new Map<K, number>();\n const uniqueKeys: K[] = [];\n\n for (const entry of batch) {\n if (!keyMap.has(entry.key)) {\n keyMap.set(entry.key, uniqueKeys.length);\n uniqueKeys.push(entry.key);\n }\n }\n\n batchFn(uniqueKeys).then(\n (results) => {\n if (results.length !== uniqueKeys.length) {\n const err = new Error(\n `Batch function returned ${results.length} results for ${uniqueKeys.length} keys`,\n );\n for (const entry of batch) {\n entry.reject(err);\n }\n return;\n }\n\n for (const entry of batch) {\n const index = keyMap.get(entry.key)!;\n entry.resolve(results[index]);\n }\n },\n (error) => {\n for (const entry of batch) {\n entry.reject(error);\n }\n },\n );\n }\n\n function load(key: K): Promise<V> {\n return new Promise<V>((resolve, reject) => {\n queue.push({ key, resolve, reject });\n\n if (queue.length >= maxBatchSize) {\n if (timer !== null) {\n clearTimeout(timer);\n timer = null;\n }\n flush();\n } else {\n scheduleFlush();\n }\n });\n }\n\n function loadMany(keys: K[]): Promise<V[]> {\n return Promise.all(keys.map((k) => load(k)));\n }\n\n return { load, loadMany };\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@philiprehberger/async-batcher",
|
|
3
|
+
"version": "0.1.5",
|
|
4
|
+
"description": "Automatic batching and deduplication for async operations",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup",
|
|
26
|
+
"dev": "tsup --watch",
|
|
27
|
+
"typecheck": "tsc --noEmit",
|
|
28
|
+
"prepublishOnly": "npm run build",
|
|
29
|
+
"test": "node --test"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"tsup": "^8.0.0",
|
|
33
|
+
"typescript": "^5.0.0"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"batch",
|
|
37
|
+
"dataloader",
|
|
38
|
+
"async",
|
|
39
|
+
"deduplication",
|
|
40
|
+
"performance"
|
|
41
|
+
],
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/philiprehberger/ts-async-batcher.git"
|
|
46
|
+
},
|
|
47
|
+
"homepage": "https://github.com/philiprehberger/ts-async-batcher#readme",
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/philiprehberger/ts-async-batcher/issues"
|
|
50
|
+
},
|
|
51
|
+
"author": "Philip Rehberger",
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=18.0.0"
|
|
54
|
+
},
|
|
55
|
+
"sideEffects": false
|
|
56
|
+
}
|