vovk 3.0.0-draft.448 → 3.0.0-draft.450
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/cjs/client/defaultStreamHandler.d.ts +2 -2
- package/cjs/client/defaultStreamHandler.js +12 -17
- package/cjs/client/fetcher.js +3 -3
- package/cjs/client/types.d.ts +2 -1
- package/mjs/client/defaultStreamHandler.d.ts +2 -2
- package/mjs/client/defaultStreamHandler.js +12 -17
- package/mjs/client/fetcher.js +3 -3
- package/mjs/client/types.d.ts +2 -1
- package/package.json +1 -1
|
@@ -2,8 +2,8 @@ import { VovkHandlerSchema } from '../types';
|
|
|
2
2
|
import type { VovkStreamAsyncIterable } from './types';
|
|
3
3
|
import '../utils/shim';
|
|
4
4
|
export declare const DEFAULT_ERROR_MESSAGE = "Unknown error at defaultStreamHandler";
|
|
5
|
-
export declare const defaultStreamHandler: ({ response,
|
|
5
|
+
export declare const defaultStreamHandler: ({ response, abortController, }: {
|
|
6
6
|
response: Response;
|
|
7
|
-
|
|
7
|
+
abortController: AbortController;
|
|
8
8
|
schema: VovkHandlerSchema;
|
|
9
9
|
}) => Promise<VovkStreamAsyncIterable<unknown>>;
|
|
@@ -5,7 +5,7 @@ const types_1 = require("../types");
|
|
|
5
5
|
const HttpException_1 = require("../HttpException");
|
|
6
6
|
require("../utils/shim");
|
|
7
7
|
exports.DEFAULT_ERROR_MESSAGE = 'Unknown error at defaultStreamHandler';
|
|
8
|
-
const defaultStreamHandler = async ({ response,
|
|
8
|
+
const defaultStreamHandler = async ({ response, abortController, }) => {
|
|
9
9
|
if (!response.ok) {
|
|
10
10
|
let result;
|
|
11
11
|
try {
|
|
@@ -20,8 +20,6 @@ const defaultStreamHandler = async ({ response, controller, }) => {
|
|
|
20
20
|
if (!response.body)
|
|
21
21
|
throw new HttpException_1.HttpException(types_1.HttpStatus.NULL, 'Stream body is falsy. Check your controller code.');
|
|
22
22
|
const reader = response.body.getReader();
|
|
23
|
-
// if streaming is too rapid, we need to make sure that the loop is stopped
|
|
24
|
-
let canceled = false;
|
|
25
23
|
const subscribers = new Set();
|
|
26
24
|
async function* asyncIterator() {
|
|
27
25
|
let prepend = '';
|
|
@@ -30,7 +28,7 @@ const defaultStreamHandler = async ({ response, controller, }) => {
|
|
|
30
28
|
let value;
|
|
31
29
|
try {
|
|
32
30
|
let done;
|
|
33
|
-
if (
|
|
31
|
+
if (abortController.signal.aborted)
|
|
34
32
|
break;
|
|
35
33
|
({ value, done } = await reader.read());
|
|
36
34
|
if (done)
|
|
@@ -57,21 +55,20 @@ const defaultStreamHandler = async ({ response, controller, }) => {
|
|
|
57
55
|
}
|
|
58
56
|
if (data) {
|
|
59
57
|
subscribers.forEach((cb) => {
|
|
60
|
-
if (!
|
|
58
|
+
if (!abortController.signal.aborted)
|
|
61
59
|
cb(data, i);
|
|
62
60
|
});
|
|
63
61
|
i++;
|
|
64
62
|
if ('isError' in data && 'reason' in data) {
|
|
65
63
|
const upcomingError = data.reason;
|
|
66
|
-
|
|
67
|
-
controller.abort(data.reason);
|
|
64
|
+
abortController.abort(data.reason);
|
|
68
65
|
if (typeof upcomingError === 'string') {
|
|
69
66
|
throw new Error(upcomingError);
|
|
70
67
|
}
|
|
71
|
-
|
|
68
|
+
abortController.abort('Stream disposed');
|
|
72
69
|
throw upcomingError;
|
|
73
70
|
}
|
|
74
|
-
else if (!
|
|
71
|
+
else if (!abortController.signal.aborted) {
|
|
75
72
|
yield data;
|
|
76
73
|
}
|
|
77
74
|
}
|
|
@@ -88,26 +85,24 @@ const defaultStreamHandler = async ({ response, controller, }) => {
|
|
|
88
85
|
return {
|
|
89
86
|
status: response.status,
|
|
90
87
|
asPromise,
|
|
88
|
+
abortController,
|
|
91
89
|
[Symbol.asyncIterator]: asyncIterator,
|
|
92
90
|
[Symbol.dispose]: () => {
|
|
93
|
-
|
|
94
|
-
controller.abort('Stream disposed');
|
|
91
|
+
abortController.abort('Stream disposed');
|
|
95
92
|
},
|
|
96
93
|
[Symbol.asyncDispose]: () => {
|
|
97
|
-
|
|
98
|
-
controller.abort('Stream async disposed');
|
|
94
|
+
abortController.abort('Stream async disposed');
|
|
99
95
|
},
|
|
100
96
|
onIterate: (cb) => {
|
|
101
|
-
if (
|
|
97
|
+
if (abortController.signal.aborted)
|
|
102
98
|
return () => { };
|
|
103
99
|
subscribers.add(cb);
|
|
104
100
|
return () => {
|
|
105
101
|
subscribers.delete(cb);
|
|
106
102
|
};
|
|
107
103
|
},
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return controller.abort(reason ?? 'Stream aborted intentionally');
|
|
104
|
+
abort: (reason) => {
|
|
105
|
+
return abortController.abort(reason ?? 'Stream aborted intentionally');
|
|
111
106
|
},
|
|
112
107
|
};
|
|
113
108
|
};
|
package/cjs/client/fetcher.js
CHANGED
|
@@ -58,8 +58,8 @@ function createFetcher({ prepareRequestInit, transformResponse, onSuccess, onErr
|
|
|
58
58
|
else if (body) {
|
|
59
59
|
requestInit.body = JSON.stringify(body);
|
|
60
60
|
}
|
|
61
|
-
const
|
|
62
|
-
requestInit.signal =
|
|
61
|
+
const abortController = new AbortController();
|
|
62
|
+
requestInit.signal = abortController.signal;
|
|
63
63
|
requestInit = prepareRequestInit ? await prepareRequestInit(requestInit, inputOptions) : requestInit;
|
|
64
64
|
try {
|
|
65
65
|
response = await fetch(endpoint, requestInit);
|
|
@@ -75,7 +75,7 @@ function createFetcher({ prepareRequestInit, transformResponse, onSuccess, onErr
|
|
|
75
75
|
}
|
|
76
76
|
const contentType = interpretAs ?? response.headers.get('content-type');
|
|
77
77
|
if (contentType?.startsWith('application/jsonl')) {
|
|
78
|
-
respData = defaultStreamHandler({ response,
|
|
78
|
+
respData = defaultStreamHandler({ response, abortController, schema });
|
|
79
79
|
}
|
|
80
80
|
else if (contentType?.startsWith('application/json')) {
|
|
81
81
|
respData = defaultHandler({ response, schema });
|
package/cjs/client/types.d.ts
CHANGED
|
@@ -34,7 +34,8 @@ export type VovkStreamAsyncIterable<T> = {
|
|
|
34
34
|
[Symbol.asyncDispose](): Promise<void> | void;
|
|
35
35
|
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
36
36
|
onIterate: (cb: (data: T, i: number) => void) => () => void;
|
|
37
|
-
|
|
37
|
+
abort: () => Promise<void> | void;
|
|
38
|
+
abortController: AbortController;
|
|
38
39
|
};
|
|
39
40
|
type StaticMethodReturn<T extends ControllerStaticMethod> = ReturnType<T> extends NextResponse<infer U> | Promise<NextResponse<infer U>> ? U : ReturnType<T> extends Response | Promise<Response> ? Awaited<ReturnType<T>> : ReturnType<T>;
|
|
40
41
|
type StaticMethodReturnPromise<T extends ControllerStaticMethod> = ToPromise<StaticMethodReturn<T>>;
|
|
@@ -2,8 +2,8 @@ import { VovkHandlerSchema } from '../types';
|
|
|
2
2
|
import type { VovkStreamAsyncIterable } from './types';
|
|
3
3
|
import '../utils/shim';
|
|
4
4
|
export declare const DEFAULT_ERROR_MESSAGE = "Unknown error at defaultStreamHandler";
|
|
5
|
-
export declare const defaultStreamHandler: ({ response,
|
|
5
|
+
export declare const defaultStreamHandler: ({ response, abortController, }: {
|
|
6
6
|
response: Response;
|
|
7
|
-
|
|
7
|
+
abortController: AbortController;
|
|
8
8
|
schema: VovkHandlerSchema;
|
|
9
9
|
}) => Promise<VovkStreamAsyncIterable<unknown>>;
|
|
@@ -5,7 +5,7 @@ const types_1 = require("../types");
|
|
|
5
5
|
const HttpException_1 = require("../HttpException");
|
|
6
6
|
require("../utils/shim");
|
|
7
7
|
exports.DEFAULT_ERROR_MESSAGE = 'Unknown error at defaultStreamHandler';
|
|
8
|
-
const defaultStreamHandler = async ({ response,
|
|
8
|
+
const defaultStreamHandler = async ({ response, abortController, }) => {
|
|
9
9
|
if (!response.ok) {
|
|
10
10
|
let result;
|
|
11
11
|
try {
|
|
@@ -20,8 +20,6 @@ const defaultStreamHandler = async ({ response, controller, }) => {
|
|
|
20
20
|
if (!response.body)
|
|
21
21
|
throw new HttpException_1.HttpException(types_1.HttpStatus.NULL, 'Stream body is falsy. Check your controller code.');
|
|
22
22
|
const reader = response.body.getReader();
|
|
23
|
-
// if streaming is too rapid, we need to make sure that the loop is stopped
|
|
24
|
-
let canceled = false;
|
|
25
23
|
const subscribers = new Set();
|
|
26
24
|
async function* asyncIterator() {
|
|
27
25
|
let prepend = '';
|
|
@@ -30,7 +28,7 @@ const defaultStreamHandler = async ({ response, controller, }) => {
|
|
|
30
28
|
let value;
|
|
31
29
|
try {
|
|
32
30
|
let done;
|
|
33
|
-
if (
|
|
31
|
+
if (abortController.signal.aborted)
|
|
34
32
|
break;
|
|
35
33
|
({ value, done } = await reader.read());
|
|
36
34
|
if (done)
|
|
@@ -57,21 +55,20 @@ const defaultStreamHandler = async ({ response, controller, }) => {
|
|
|
57
55
|
}
|
|
58
56
|
if (data) {
|
|
59
57
|
subscribers.forEach((cb) => {
|
|
60
|
-
if (!
|
|
58
|
+
if (!abortController.signal.aborted)
|
|
61
59
|
cb(data, i);
|
|
62
60
|
});
|
|
63
61
|
i++;
|
|
64
62
|
if ('isError' in data && 'reason' in data) {
|
|
65
63
|
const upcomingError = data.reason;
|
|
66
|
-
|
|
67
|
-
controller.abort(data.reason);
|
|
64
|
+
abortController.abort(data.reason);
|
|
68
65
|
if (typeof upcomingError === 'string') {
|
|
69
66
|
throw new Error(upcomingError);
|
|
70
67
|
}
|
|
71
|
-
|
|
68
|
+
abortController.abort('Stream disposed');
|
|
72
69
|
throw upcomingError;
|
|
73
70
|
}
|
|
74
|
-
else if (!
|
|
71
|
+
else if (!abortController.signal.aborted) {
|
|
75
72
|
yield data;
|
|
76
73
|
}
|
|
77
74
|
}
|
|
@@ -88,26 +85,24 @@ const defaultStreamHandler = async ({ response, controller, }) => {
|
|
|
88
85
|
return {
|
|
89
86
|
status: response.status,
|
|
90
87
|
asPromise,
|
|
88
|
+
abortController,
|
|
91
89
|
[Symbol.asyncIterator]: asyncIterator,
|
|
92
90
|
[Symbol.dispose]: () => {
|
|
93
|
-
|
|
94
|
-
controller.abort('Stream disposed');
|
|
91
|
+
abortController.abort('Stream disposed');
|
|
95
92
|
},
|
|
96
93
|
[Symbol.asyncDispose]: () => {
|
|
97
|
-
|
|
98
|
-
controller.abort('Stream async disposed');
|
|
94
|
+
abortController.abort('Stream async disposed');
|
|
99
95
|
},
|
|
100
96
|
onIterate: (cb) => {
|
|
101
|
-
if (
|
|
97
|
+
if (abortController.signal.aborted)
|
|
102
98
|
return () => { };
|
|
103
99
|
subscribers.add(cb);
|
|
104
100
|
return () => {
|
|
105
101
|
subscribers.delete(cb);
|
|
106
102
|
};
|
|
107
103
|
},
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return controller.abort(reason ?? 'Stream aborted intentionally');
|
|
104
|
+
abort: (reason) => {
|
|
105
|
+
return abortController.abort(reason ?? 'Stream aborted intentionally');
|
|
111
106
|
},
|
|
112
107
|
};
|
|
113
108
|
};
|
package/mjs/client/fetcher.js
CHANGED
|
@@ -58,8 +58,8 @@ function createFetcher({ prepareRequestInit, transformResponse, onSuccess, onErr
|
|
|
58
58
|
else if (body) {
|
|
59
59
|
requestInit.body = JSON.stringify(body);
|
|
60
60
|
}
|
|
61
|
-
const
|
|
62
|
-
requestInit.signal =
|
|
61
|
+
const abortController = new AbortController();
|
|
62
|
+
requestInit.signal = abortController.signal;
|
|
63
63
|
requestInit = prepareRequestInit ? await prepareRequestInit(requestInit, inputOptions) : requestInit;
|
|
64
64
|
try {
|
|
65
65
|
response = await fetch(endpoint, requestInit);
|
|
@@ -75,7 +75,7 @@ function createFetcher({ prepareRequestInit, transformResponse, onSuccess, onErr
|
|
|
75
75
|
}
|
|
76
76
|
const contentType = interpretAs ?? response.headers.get('content-type');
|
|
77
77
|
if (contentType?.startsWith('application/jsonl')) {
|
|
78
|
-
respData = defaultStreamHandler({ response,
|
|
78
|
+
respData = defaultStreamHandler({ response, abortController, schema });
|
|
79
79
|
}
|
|
80
80
|
else if (contentType?.startsWith('application/json')) {
|
|
81
81
|
respData = defaultHandler({ response, schema });
|
package/mjs/client/types.d.ts
CHANGED
|
@@ -34,7 +34,8 @@ export type VovkStreamAsyncIterable<T> = {
|
|
|
34
34
|
[Symbol.asyncDispose](): Promise<void> | void;
|
|
35
35
|
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
36
36
|
onIterate: (cb: (data: T, i: number) => void) => () => void;
|
|
37
|
-
|
|
37
|
+
abort: () => Promise<void> | void;
|
|
38
|
+
abortController: AbortController;
|
|
38
39
|
};
|
|
39
40
|
type StaticMethodReturn<T extends ControllerStaticMethod> = ReturnType<T> extends NextResponse<infer U> | Promise<NextResponse<infer U>> ? U : ReturnType<T> extends Response | Promise<Response> ? Awaited<ReturnType<T>> : ReturnType<T>;
|
|
40
41
|
type StaticMethodReturnPromise<T extends ControllerStaticMethod> = ToPromise<StaticMethodReturn<T>>;
|