@xsai/utils-stream 0.1.3 → 0.2.0-beta.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.
- package/dist/index.js +6 -13
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
var sleep = async (delay) => delay === 0 ? Promise.resolve() : new Promise((resolve) => setTimeout(resolve, delay));
|
|
1
|
+
const sleep = async (delay) => delay === 0 ? Promise.resolve() : new Promise((resolve) => setTimeout(resolve, delay));
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
var simulateReadableStream = ({ chunkDelay, chunks, initialDelay }) => new ReadableStream({
|
|
3
|
+
const simulateReadableStream = ({ chunkDelay, chunks, initialDelay }) => new ReadableStream({
|
|
6
4
|
pull: async (controller) => {
|
|
7
5
|
for (const [index, chunk] of chunks.entries()) {
|
|
8
6
|
await sleep(index === 0 ? initialDelay : chunkDelay);
|
|
@@ -12,14 +10,13 @@ var simulateReadableStream = ({ chunkDelay, chunks, initialDelay }) => new Reada
|
|
|
12
10
|
}
|
|
13
11
|
});
|
|
14
12
|
|
|
15
|
-
|
|
16
|
-
var CHUNKING_REGEXPS = {
|
|
13
|
+
const CHUNKING_REGEXPS = {
|
|
17
14
|
// eslint-disable-next-line sonarjs/slow-regex
|
|
18
15
|
line: /[^\n]*\n/,
|
|
19
16
|
// eslint-disable-next-line sonarjs/slow-regex
|
|
20
17
|
word: /\s*\S+\s+/
|
|
21
18
|
};
|
|
22
|
-
|
|
19
|
+
const smoothStream = ({ chunking = "word", delay = 10 } = {}) => {
|
|
23
20
|
const chunkingRegexp = typeof chunking === "string" ? CHUNKING_REGEXPS[chunking] : chunking;
|
|
24
21
|
let buffer = "";
|
|
25
22
|
return new TransformStream({
|
|
@@ -37,7 +34,6 @@ var smoothStream = ({ chunking = "word", delay = 10 } = {}) => {
|
|
|
37
34
|
});
|
|
38
35
|
};
|
|
39
36
|
|
|
40
|
-
// src/to-async-iterator.ts
|
|
41
37
|
async function* toAsyncIterator(stream) {
|
|
42
38
|
const reader = stream.getReader();
|
|
43
39
|
try {
|
|
@@ -51,8 +47,5 @@ async function* toAsyncIterator(stream) {
|
|
|
51
47
|
reader.releaseLock();
|
|
52
48
|
}
|
|
53
49
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
smoothStream,
|
|
57
|
-
toAsyncIterator
|
|
58
|
-
};
|
|
50
|
+
|
|
51
|
+
export { simulateReadableStream, smoothStream, toAsyncIterator };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai/utils-stream",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0-beta.2",
|
|
5
5
|
"description": "extra-small AI SDK for Browser, Node.js, Deno, Bun or Edge Runtime.",
|
|
6
6
|
"author": "Moeru AI",
|
|
7
7
|
"license": "MIT",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"scripts": {
|
|
32
|
-
"build": "
|
|
32
|
+
"build": "pkgroll"
|
|
33
33
|
},
|
|
34
34
|
"main": "./dist/index.js",
|
|
35
35
|
"types": "./dist/index.d.ts"
|