cantian-request 0.0.11 → 0.0.13
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/sse.d.ts +4 -1
- package/dist/sse.js +48 -11
- package/dist/sse.js.map +1 -1
- package/package.json +1 -1
package/dist/sse.d.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
import { EventSourceMessage } from 'eventsource-parser';
|
|
2
|
-
export
|
|
2
|
+
export interface SseExtOptions {
|
|
3
|
+
idleTimeoutMs?: number;
|
|
4
|
+
}
|
|
5
|
+
export declare const sse: (input: string | URL | globalThis.Request, init?: RequestInit, extOptions?: SseExtOptions) => AsyncGenerator<EventSourceMessage | undefined, void, unknown>;
|
package/dist/sse.js
CHANGED
|
@@ -1,13 +1,39 @@
|
|
|
1
1
|
import { createParser } from 'eventsource-parser';
|
|
2
2
|
import { createThrowHttpErrorFetch } from './basic.js';
|
|
3
3
|
const basicSse = createThrowHttpErrorFetch();
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const withTimeoutInit = (init, timeoutSignal) => {
|
|
5
|
+
return {
|
|
6
|
+
...init,
|
|
7
|
+
signal: init?.signal ? AbortSignal.any([init.signal, timeoutSignal]) : timeoutSignal,
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export const sse = async function* (input, init, extOptions) {
|
|
11
|
+
const idleTimeoutMs = extOptions?.idleTimeoutMs ?? 300000;
|
|
12
|
+
const connectTimeoutController = new AbortController();
|
|
13
|
+
const connectTimeoutId = setTimeout(() => {
|
|
14
|
+
connectTimeoutController.abort(new Error(`SSE idle timeout while waiting for response (${idleTimeoutMs}ms)`));
|
|
15
|
+
}, idleTimeoutMs);
|
|
16
|
+
let response;
|
|
17
|
+
try {
|
|
18
|
+
response = await basicSse(input, withTimeoutInit(init, connectTimeoutController.signal));
|
|
19
|
+
}
|
|
20
|
+
finally {
|
|
21
|
+
clearTimeout(connectTimeoutId);
|
|
22
|
+
}
|
|
6
23
|
if (!response.body) {
|
|
7
24
|
console.error(`FETCH_ERROR_NO_BODY`);
|
|
8
25
|
throw new Error('No body');
|
|
9
26
|
}
|
|
10
27
|
const reader = response.body.getReader();
|
|
28
|
+
let idleError;
|
|
29
|
+
let readStartedAt;
|
|
30
|
+
const idleCheckId = setInterval(() => {
|
|
31
|
+
if (!idleError && readStartedAt && Date.now() - readStartedAt >= idleTimeoutMs) {
|
|
32
|
+
const idleMs = Date.now() - readStartedAt;
|
|
33
|
+
idleError = new Error(`SSE idle timeout while waiting for data (${idleMs}ms >= ${idleTimeoutMs}ms)`);
|
|
34
|
+
void reader.cancel(idleError).catch(() => { });
|
|
35
|
+
}
|
|
36
|
+
}, 1000);
|
|
11
37
|
const pending = [];
|
|
12
38
|
let parserError;
|
|
13
39
|
const decoder = new TextDecoder();
|
|
@@ -19,16 +45,29 @@ export const sse = async function* (input, init) {
|
|
|
19
45
|
parserError = error;
|
|
20
46
|
},
|
|
21
47
|
});
|
|
22
|
-
let error;
|
|
23
48
|
try {
|
|
24
49
|
while (true) {
|
|
25
50
|
while (pending.length) {
|
|
26
51
|
yield pending.shift();
|
|
27
52
|
}
|
|
28
|
-
|
|
29
|
-
chunk = await reader.read();
|
|
53
|
+
readStartedAt = Date.now();
|
|
54
|
+
const chunk = await reader.read();
|
|
55
|
+
readStartedAt = undefined;
|
|
56
|
+
if (idleError) {
|
|
57
|
+
throw idleError;
|
|
58
|
+
}
|
|
30
59
|
const { value, done } = chunk;
|
|
31
60
|
if (done) {
|
|
61
|
+
const tail = decoder.decode();
|
|
62
|
+
if (tail) {
|
|
63
|
+
parser.feed(tail);
|
|
64
|
+
if (parserError) {
|
|
65
|
+
throw parserError;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
while (pending.length) {
|
|
69
|
+
yield pending.shift();
|
|
70
|
+
}
|
|
32
71
|
break;
|
|
33
72
|
}
|
|
34
73
|
parser.feed(decoder.decode(value, { stream: true }));
|
|
@@ -37,18 +76,16 @@ export const sse = async function* (input, init) {
|
|
|
37
76
|
}
|
|
38
77
|
}
|
|
39
78
|
}
|
|
40
|
-
|
|
41
|
-
|
|
79
|
+
finally {
|
|
80
|
+
clearInterval(idleCheckId);
|
|
42
81
|
try {
|
|
43
82
|
await reader.cancel();
|
|
44
83
|
}
|
|
45
84
|
catch { }
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
finally {
|
|
49
|
-
if (!error) {
|
|
85
|
+
try {
|
|
50
86
|
reader.releaseLock();
|
|
51
87
|
}
|
|
88
|
+
catch { }
|
|
52
89
|
}
|
|
53
90
|
};
|
|
54
91
|
//# sourceMappingURL=sse.js.map
|
package/dist/sse.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sse.js","sourceRoot":"","sources":["../src/sse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAsB,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAEvD,MAAM,QAAQ,GAAG,yBAAyB,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"sse.js","sourceRoot":"","sources":["../src/sse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAsB,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAEvD,MAAM,QAAQ,GAAG,yBAAyB,EAAE,CAAC;AAM7C,MAAM,eAAe,GAAG,CAAC,IAA6B,EAAE,aAA0B,EAAe,EAAE;IACjG,OAAO;QACL,GAAG,IAAI;QACP,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,aAAa;KACrF,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,GAAG,GAAG,KAAK,SAAS,CAAC,EAAE,KAAwC,EAA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