vovk 3.0.0-draft.57 → 3.0.0-draft.58
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/StreamJSONResponse.d.ts +0 -1
- package/dist/StreamJSONResponse.js +1 -2
- package/dist/client/createRPC.d.ts +2 -1
- package/dist/client/createRPC.js +2 -2
- package/dist/client/defaultFetcher.js +5 -4
- package/dist/client/index.d.ts +2 -2
- package/dist/client/index.js +4 -1
- package/dist/client/types.d.ts +2 -1
- package/dist/createVovkApp.d.ts +0 -1
- package/dist/index.d.ts +18 -20
- package/dist/index.js +1 -4
- package/dist/types.d.ts +0 -12
- package/dist/utils/getSchema.d.ts +0 -1
- package/dist/utils/getSchema.js +0 -8
- package/package.json +1 -1
- package/src/StreamJSONResponse.ts +1 -2
- package/src/client/createRPC.ts +4 -1
- package/src/client/defaultFetcher.ts +6 -4
- package/src/client/index.ts +8 -2
- package/src/client/types.ts +2 -0
- package/src/createVovkApp.ts +0 -1
- package/src/index.ts +4 -5
- package/src/types.ts +0 -17
- package/src/utils/getSchema.ts +1 -11
- package/.DS_Store +0 -0
- package/.npmignore +0 -3
- package/.turbo/turbo-build.log +0 -6
- package/.turbo/turbo-ncu.log +0 -9
- package/.turbo/turbo-tsc.log +0 -6
- package/dist/worker/createWPC.d.ts +0 -2
- package/dist/worker/createWPC.js +0 -141
- package/dist/worker/index.d.ts +0 -2
- package/dist/worker/index.js +0 -7
- package/dist/worker/types.d.ts +0 -31
- package/dist/worker/types.js +0 -2
- package/dist/worker/worker.d.ts +0 -1
- package/dist/worker/worker.js +0 -43
- package/src/worker/createWPC.ts +0 -153
- package/src/worker/index.ts +0 -2
- package/src/worker/types.ts +0 -45
- package/src/worker/worker.ts +0 -53
|
@@ -4,8 +4,7 @@ exports.StreamJSONResponse = void 0;
|
|
|
4
4
|
require("./utils/shim");
|
|
5
5
|
class StreamJSONResponse extends Response {
|
|
6
6
|
static defaultHeaders = {
|
|
7
|
-
'content-type': '
|
|
8
|
-
'x-vovk-stream': 'true',
|
|
7
|
+
'content-type': 'application/jsonl',
|
|
9
8
|
};
|
|
10
9
|
isClosed = false;
|
|
11
10
|
controller;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { type VovkControllerSchema, type KnownAny } from '../types';
|
|
2
2
|
import { type VovkClientOptions, type VovkClient, type VovkDefaultFetcherOptions } from './types';
|
|
3
|
-
|
|
3
|
+
declare const createRPC: <T, OPTS extends Record<string, KnownAny> = VovkDefaultFetcherOptions>(controllerSchema: VovkControllerSchema, segmentName?: string, options?: VovkClientOptions<OPTS>) => VovkClient<T, OPTS>;
|
|
4
|
+
export default createRPC;
|
package/dist/client/createRPC.js
CHANGED
|
@@ -3,7 +3,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createRPC = void 0;
|
|
7
6
|
const defaultFetcher_1 = __importDefault(require("./defaultFetcher"));
|
|
8
7
|
const defaultHandler_1 = require("./defaultHandler");
|
|
9
8
|
const defaultStreamHandler_1 = require("./defaultStreamHandler");
|
|
@@ -67,9 +66,10 @@ const createRPC = (controllerSchema, segmentName, options) => {
|
|
|
67
66
|
return input.transform ? fetcherPromise.then(input.transform) : fetcherPromise;
|
|
68
67
|
};
|
|
69
68
|
handler.schema = handlerSchema;
|
|
69
|
+
handler.controllerSchema = schema;
|
|
70
70
|
// @ts-expect-error TODO
|
|
71
71
|
client[staticMethodName] = handler;
|
|
72
72
|
}
|
|
73
73
|
return client;
|
|
74
74
|
};
|
|
75
|
-
exports.
|
|
75
|
+
exports.default = createRPC;
|
|
@@ -38,12 +38,13 @@ const defaultFetcher = async ({ httpMethod, getEndpoint, validate, defaultHandle
|
|
|
38
38
|
// handle network errors
|
|
39
39
|
throw new HttpException_1.HttpException(types_1.HttpStatus.NULL, e?.message ?? exports.DEFAULT_ERROR_MESSAGE);
|
|
40
40
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
if (response.headers.get('x-vovk-stream') === 'true') {
|
|
41
|
+
const contentType = response.headers.get('content-type');
|
|
42
|
+
if (contentType?.includes('application/jsonl')) {
|
|
45
43
|
return defaultStreamHandler(response);
|
|
46
44
|
}
|
|
45
|
+
if (contentType?.includes('application/json')) {
|
|
46
|
+
return defaultHandler(response);
|
|
47
|
+
}
|
|
47
48
|
return response;
|
|
48
49
|
};
|
|
49
50
|
exports.default = defaultFetcher;
|
package/dist/client/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { createRPC } from './createRPC';
|
|
2
|
-
export type { VovkClientFetcher, VovkClientOptions, VovkDefaultFetcherOptions, VovkValidateOnClient } from './types';
|
|
1
|
+
export { default as createRPC } from './createRPC';
|
|
2
|
+
export type { VovkClient, VovkClientFetcher, VovkClientOptions, VovkDefaultFetcherOptions, VovkValidateOnClient, } from './types';
|
package/dist/client/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.createRPC = void 0;
|
|
4
7
|
var createRPC_1 = require("./createRPC");
|
|
5
|
-
Object.defineProperty(exports, "createRPC", { enumerable: true, get: function () { return createRPC_1.
|
|
8
|
+
Object.defineProperty(exports, "createRPC", { enumerable: true, get: function () { return __importDefault(createRPC_1).default; } });
|
package/dist/client/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { KnownAny, HttpMethod, ControllerStaticMethod, VovkControllerBody, VovkControllerQuery, VovkControllerParams, VovkHandlerSchema } from '../types';
|
|
1
|
+
import type { KnownAny, HttpMethod, ControllerStaticMethod, VovkControllerBody, VovkControllerQuery, VovkControllerParams, VovkHandlerSchema, VovkControllerSchema } from '../types';
|
|
2
2
|
import type { StreamJSONResponse } from '../StreamJSONResponse';
|
|
3
3
|
import type { NextResponse } from 'next/server';
|
|
4
4
|
export type StaticMethodInput<T extends ControllerStaticMethod> = (VovkControllerBody<T> extends undefined | void ? {
|
|
@@ -36,6 +36,7 @@ type ClientMethod<T extends (...args: KnownAny[]) => void | object | StreamJSONR
|
|
|
36
36
|
transform: (staticMethodReturn: Awaited<StaticMethodReturn<T>>) => R;
|
|
37
37
|
}> | void)) => ReturnType<T> extends Promise<StreamJSONResponse<infer U>> | StreamJSONResponse<infer U> | Iterator<infer U> | AsyncIterator<infer U> ? Promise<StreamAsyncIterator<U>> : R extends object ? Promise<R> : StaticMethodReturnPromise<T>) & {
|
|
38
38
|
schema: VovkHandlerSchema;
|
|
39
|
+
controllerSchema: VovkControllerSchema;
|
|
39
40
|
};
|
|
40
41
|
type OmitNever<T> = {
|
|
41
42
|
[K in keyof T as T[K] extends never ? never : K]: T[K];
|
package/dist/createVovkApp.d.ts
CHANGED
|
@@ -32,7 +32,6 @@ export declare function createVovkApp(): {
|
|
|
32
32
|
initVovk: (options: {
|
|
33
33
|
segmentName?: string;
|
|
34
34
|
controllers: Record<string, StaticClass>;
|
|
35
|
-
workers?: Record<string, StaticClass>;
|
|
36
35
|
exposeValidation?: boolean;
|
|
37
36
|
emitSchema?: boolean;
|
|
38
37
|
onError?: (err: Error, req: VovkRequest) => void | Promise<void>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,38 +1,36 @@
|
|
|
1
1
|
import { createVovkApp } from './createVovkApp';
|
|
2
|
-
import { HttpStatus as HttpStatus, HttpMethod as HttpMethod, type VovkErrorResponse, type VovkRequest, type VovkBody, type VovkQuery, type VovkParams, type VovkReturnType, type VovkYieldType, type VovkControllerBody, type VovkControllerQuery, type VovkControllerParams, type VovkControllerYieldType, type VovkSchema, type
|
|
3
|
-
import { type VovkClientOptions, type VovkClientFetcher, type VovkDefaultFetcherOptions, type VovkValidateOnClient, createRPC } from './client';
|
|
2
|
+
import { HttpStatus as HttpStatus, HttpMethod as HttpMethod, type KnownAny, type VovkErrorResponse, type VovkRequest, type VovkBody, type VovkQuery, type VovkParams, type VovkReturnType, type VovkYieldType, type VovkControllerBody, type VovkControllerQuery, type VovkControllerParams, type VovkControllerYieldType, type VovkSchema, type VovkControllerSchema, type VovkHandlerSchema } from './types';
|
|
3
|
+
import { type VovkClient, type VovkClientOptions, type VovkClientFetcher, type VovkDefaultFetcherOptions, type VovkValidateOnClient, createRPC } from './client';
|
|
4
4
|
import { HttpException } from './HttpException';
|
|
5
5
|
import { createDecorator } from './createDecorator';
|
|
6
6
|
import { StreamJSONResponse } from './StreamJSONResponse';
|
|
7
|
-
import { worker, createWPC } from './worker';
|
|
8
7
|
import { generateStaticAPI } from './utils/generateStaticAPI';
|
|
9
8
|
import { setClientValidatorsForHandler } from './utils/setClientValidatorsForHandler';
|
|
10
|
-
export { type VovkClientFetcher, type VovkDefaultFetcherOptions, type VovkValidateOnClient, type VovkSchema, type VovkErrorResponse, type VovkRequest, type VovkControllerBody, type VovkControllerQuery, type VovkControllerParams, type VovkControllerYieldType, type VovkBody, type VovkQuery, type VovkParams, type VovkYieldType, type VovkReturnType, type VovkClientOptions, type
|
|
9
|
+
export { type KnownAny, type VovkClient, type VovkClientFetcher, type VovkDefaultFetcherOptions, type VovkValidateOnClient, type VovkSchema, type VovkErrorResponse, type VovkRequest, type VovkControllerBody, type VovkControllerQuery, type VovkControllerParams, type VovkControllerYieldType, type VovkBody, type VovkQuery, type VovkParams, type VovkYieldType, type VovkReturnType, type VovkClientOptions, type VovkControllerSchema, type VovkHandlerSchema, StreamJSONResponse, HttpException, HttpStatus, HttpMethod, createVovkApp, createDecorator, createRPC, generateStaticAPI, setClientValidatorsForHandler, };
|
|
11
10
|
export declare const get: {
|
|
12
|
-
(givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget:
|
|
13
|
-
auto: (options?: import("./types").DecoratorOptions) => (givenTarget:
|
|
11
|
+
(givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
|
|
12
|
+
auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
|
|
14
13
|
}, post: {
|
|
15
|
-
(givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget:
|
|
16
|
-
auto: (options?: import("./types").DecoratorOptions) => (givenTarget:
|
|
14
|
+
(givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
|
|
15
|
+
auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
|
|
17
16
|
}, put: {
|
|
18
|
-
(givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget:
|
|
19
|
-
auto: (options?: import("./types").DecoratorOptions) => (givenTarget:
|
|
17
|
+
(givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
|
|
18
|
+
auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
|
|
20
19
|
}, patch: {
|
|
21
|
-
(givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget:
|
|
22
|
-
auto: (options?: import("./types").DecoratorOptions) => (givenTarget:
|
|
20
|
+
(givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
|
|
21
|
+
auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
|
|
23
22
|
}, del: {
|
|
24
|
-
(givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget:
|
|
25
|
-
auto: (options?: import("./types").DecoratorOptions) => (givenTarget:
|
|
23
|
+
(givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
|
|
24
|
+
auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
|
|
26
25
|
}, head: {
|
|
27
|
-
(givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget:
|
|
28
|
-
auto: (options?: import("./types").DecoratorOptions) => (givenTarget:
|
|
26
|
+
(givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
|
|
27
|
+
auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
|
|
29
28
|
}, options: {
|
|
30
|
-
(givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget:
|
|
31
|
-
auto: (options?: import("./types").DecoratorOptions) => (givenTarget:
|
|
32
|
-
}, prefix: (givenPath?: string) => (givenTarget:
|
|
29
|
+
(givenPath?: string | undefined, options?: import("./types").DecoratorOptions | undefined): ReturnType<(givenPath?: string, options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void>;
|
|
30
|
+
auto: (options?: import("./types").DecoratorOptions) => (givenTarget: KnownAny, propertyKey: string) => void;
|
|
31
|
+
}, prefix: (givenPath?: string) => (givenTarget: KnownAny) => any, initVovk: (options: {
|
|
33
32
|
segmentName?: string;
|
|
34
33
|
controllers: Record<string, import("./types").StaticClass>;
|
|
35
|
-
workers?: Record<string, import("./types").StaticClass>;
|
|
36
34
|
exposeValidation?: boolean;
|
|
37
35
|
emitSchema?: boolean;
|
|
38
36
|
onError?: (err: Error, req: VovkRequest) => void | Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var _a;
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.initVovk = exports.prefix = exports.options = exports.head = exports.del = exports.patch = exports.put = exports.post = exports.get = exports.setClientValidatorsForHandler = exports.generateStaticAPI = exports.createRPC = exports.
|
|
4
|
+
exports.initVovk = exports.prefix = exports.options = exports.head = exports.del = exports.patch = exports.put = exports.post = exports.get = exports.setClientValidatorsForHandler = exports.generateStaticAPI = exports.createRPC = exports.createDecorator = exports.createVovkApp = exports.HttpMethod = exports.HttpStatus = exports.HttpException = exports.StreamJSONResponse = void 0;
|
|
5
5
|
const createVovkApp_1 = require("./createVovkApp");
|
|
6
6
|
Object.defineProperty(exports, "createVovkApp", { enumerable: true, get: function () { return createVovkApp_1.createVovkApp; } });
|
|
7
7
|
const types_1 = require("./types");
|
|
@@ -15,9 +15,6 @@ const createDecorator_1 = require("./createDecorator");
|
|
|
15
15
|
Object.defineProperty(exports, "createDecorator", { enumerable: true, get: function () { return createDecorator_1.createDecorator; } });
|
|
16
16
|
const StreamJSONResponse_1 = require("./StreamJSONResponse");
|
|
17
17
|
Object.defineProperty(exports, "StreamJSONResponse", { enumerable: true, get: function () { return StreamJSONResponse_1.StreamJSONResponse; } });
|
|
18
|
-
const worker_1 = require("./worker");
|
|
19
|
-
Object.defineProperty(exports, "worker", { enumerable: true, get: function () { return worker_1.worker; } });
|
|
20
|
-
Object.defineProperty(exports, "createWPC", { enumerable: true, get: function () { return worker_1.createWPC; } });
|
|
21
18
|
const generateStaticAPI_1 = require("./utils/generateStaticAPI");
|
|
22
19
|
Object.defineProperty(exports, "generateStaticAPI", { enumerable: true, get: function () { return generateStaticAPI_1.generateStaticAPI; } });
|
|
23
20
|
const setClientValidatorsForHandler_1 = require("./utils/setClientValidatorsForHandler");
|
package/dist/types.d.ts
CHANGED
|
@@ -18,17 +18,9 @@ export type VovkControllerSchema = {
|
|
|
18
18
|
prefix?: string;
|
|
19
19
|
handlers: Record<string, VovkHandlerSchema>;
|
|
20
20
|
};
|
|
21
|
-
export type VovkWorkerSchema = {
|
|
22
|
-
workerName: string;
|
|
23
|
-
originalWorkerName: string;
|
|
24
|
-
handlers: Record<string, {
|
|
25
|
-
isGenerator?: true;
|
|
26
|
-
}>;
|
|
27
|
-
};
|
|
28
21
|
export type VovkSchema = {
|
|
29
22
|
emitSchema: boolean;
|
|
30
23
|
segmentName: string;
|
|
31
|
-
workers: Record<string, VovkWorkerSchema>;
|
|
32
24
|
controllers: Record<string, VovkControllerSchema>;
|
|
33
25
|
};
|
|
34
26
|
export type VovkErrorResponse = {
|
|
@@ -47,10 +39,6 @@ export type VovkControllerInternal = {
|
|
|
47
39
|
export type VovkController = StaticClass & VovkControllerInternal & {
|
|
48
40
|
[key: string]: unknown;
|
|
49
41
|
};
|
|
50
|
-
export type VovkWorker = StaticClass & {
|
|
51
|
-
_handlers: VovkWorkerSchema['handlers'];
|
|
52
|
-
[key: string]: unknown;
|
|
53
|
-
};
|
|
54
42
|
export type DecoratorOptions = {
|
|
55
43
|
cors?: boolean;
|
|
56
44
|
headers?: Record<string, string>;
|
package/dist/utils/getSchema.js
CHANGED
|
@@ -8,7 +8,6 @@ function getSchema(options) {
|
|
|
8
8
|
emitSchema,
|
|
9
9
|
segmentName: options.segmentName ?? '',
|
|
10
10
|
controllers: {},
|
|
11
|
-
workers: {},
|
|
12
11
|
};
|
|
13
12
|
if (!emitSchema)
|
|
14
13
|
return schema;
|
|
@@ -24,12 +23,5 @@ function getSchema(options) {
|
|
|
24
23
|
},
|
|
25
24
|
};
|
|
26
25
|
}
|
|
27
|
-
for (const [workerName, worker] of Object.entries(options.workers ?? {})) {
|
|
28
|
-
schema.workers[workerName] = {
|
|
29
|
-
workerName,
|
|
30
|
-
originalWorkerName: worker.name,
|
|
31
|
-
handlers: { ...worker._handlers },
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
26
|
return schema;
|
|
35
27
|
}
|
package/package.json
CHANGED
package/src/client/createRPC.ts
CHANGED
|
@@ -29,7 +29,7 @@ const getHandlerPath = <T extends ControllerStaticMethod>(
|
|
|
29
29
|
return `${result}${queryStr ? '?' : ''}${queryStr}`;
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
const createRPC = <T, OPTS extends Record<string, KnownAny> = VovkDefaultFetcherOptions>(
|
|
33
33
|
controllerSchema: VovkControllerSchema,
|
|
34
34
|
segmentName?: string,
|
|
35
35
|
options?: VovkClientOptions<OPTS>
|
|
@@ -107,6 +107,7 @@ export const createRPC = <T, OPTS extends Record<string, KnownAny> = VovkDefault
|
|
|
107
107
|
};
|
|
108
108
|
|
|
109
109
|
handler.schema = handlerSchema;
|
|
110
|
+
handler.controllerSchema = schema;
|
|
110
111
|
|
|
111
112
|
// @ts-expect-error TODO
|
|
112
113
|
client[staticMethodName] = handler;
|
|
@@ -114,3 +115,5 @@ export const createRPC = <T, OPTS extends Record<string, KnownAny> = VovkDefault
|
|
|
114
115
|
|
|
115
116
|
return client;
|
|
116
117
|
};
|
|
118
|
+
|
|
119
|
+
export default createRPC;
|
|
@@ -43,14 +43,16 @@ const defaultFetcher: VovkClientFetcher<VovkDefaultFetcherOptions> = async (
|
|
|
43
43
|
throw new HttpException(HttpStatus.NULL, (e as Error)?.message ?? DEFAULT_ERROR_MESSAGE);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
return defaultHandler(response);
|
|
48
|
-
}
|
|
46
|
+
const contentType = response.headers.get('content-type');
|
|
49
47
|
|
|
50
|
-
if (
|
|
48
|
+
if (contentType?.includes('application/jsonl')) {
|
|
51
49
|
return defaultStreamHandler(response);
|
|
52
50
|
}
|
|
53
51
|
|
|
52
|
+
if (contentType?.includes('application/json')) {
|
|
53
|
+
return defaultHandler(response);
|
|
54
|
+
}
|
|
55
|
+
|
|
54
56
|
return response;
|
|
55
57
|
};
|
|
56
58
|
|
package/src/client/index.ts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
export { createRPC } from './createRPC';
|
|
2
|
-
export type {
|
|
1
|
+
export { default as createRPC } from './createRPC';
|
|
2
|
+
export type {
|
|
3
|
+
VovkClient,
|
|
4
|
+
VovkClientFetcher,
|
|
5
|
+
VovkClientOptions,
|
|
6
|
+
VovkDefaultFetcherOptions,
|
|
7
|
+
VovkValidateOnClient,
|
|
8
|
+
} from './types';
|
package/src/client/types.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
VovkControllerQuery,
|
|
7
7
|
VovkControllerParams,
|
|
8
8
|
VovkHandlerSchema,
|
|
9
|
+
VovkControllerSchema,
|
|
9
10
|
} from '../types';
|
|
10
11
|
import type { StreamJSONResponse } from '../StreamJSONResponse';
|
|
11
12
|
import type { NextResponse } from 'next/server';
|
|
@@ -64,6 +65,7 @@ type ClientMethod<
|
|
|
64
65
|
? Promise<R>
|
|
65
66
|
: StaticMethodReturnPromise<T>) & {
|
|
66
67
|
schema: VovkHandlerSchema;
|
|
68
|
+
controllerSchema: VovkControllerSchema;
|
|
67
69
|
};
|
|
68
70
|
|
|
69
71
|
type OmitNever<T> = {
|
package/src/createVovkApp.ts
CHANGED
|
@@ -121,7 +121,6 @@ export function createVovkApp() {
|
|
|
121
121
|
const initVovk = (options: {
|
|
122
122
|
segmentName?: string;
|
|
123
123
|
controllers: Record<string, StaticClass>;
|
|
124
|
-
workers?: Record<string, StaticClass>;
|
|
125
124
|
exposeValidation?: boolean;
|
|
126
125
|
emitSchema?: boolean;
|
|
127
126
|
onError?: (err: Error, req: VovkRequest) => void | Promise<void>;
|
package/src/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { createVovkApp } from './createVovkApp';
|
|
|
2
2
|
import {
|
|
3
3
|
HttpStatus as HttpStatus,
|
|
4
4
|
HttpMethod as HttpMethod,
|
|
5
|
+
type KnownAny,
|
|
5
6
|
type VovkErrorResponse,
|
|
6
7
|
type VovkRequest,
|
|
7
8
|
type VovkBody,
|
|
@@ -14,11 +15,11 @@ import {
|
|
|
14
15
|
type VovkControllerParams,
|
|
15
16
|
type VovkControllerYieldType,
|
|
16
17
|
type VovkSchema,
|
|
17
|
-
type VovkWorkerSchema,
|
|
18
18
|
type VovkControllerSchema,
|
|
19
19
|
type VovkHandlerSchema,
|
|
20
20
|
} from './types';
|
|
21
21
|
import {
|
|
22
|
+
type VovkClient,
|
|
22
23
|
type VovkClientOptions,
|
|
23
24
|
type VovkClientFetcher,
|
|
24
25
|
type VovkDefaultFetcherOptions,
|
|
@@ -28,11 +29,12 @@ import {
|
|
|
28
29
|
import { HttpException } from './HttpException';
|
|
29
30
|
import { createDecorator } from './createDecorator';
|
|
30
31
|
import { StreamJSONResponse } from './StreamJSONResponse';
|
|
31
|
-
import { worker, createWPC } from './worker';
|
|
32
32
|
import { generateStaticAPI } from './utils/generateStaticAPI';
|
|
33
33
|
import { setClientValidatorsForHandler } from './utils/setClientValidatorsForHandler';
|
|
34
34
|
|
|
35
35
|
export {
|
|
36
|
+
type KnownAny,
|
|
37
|
+
type VovkClient,
|
|
36
38
|
type VovkClientFetcher,
|
|
37
39
|
type VovkDefaultFetcherOptions,
|
|
38
40
|
type VovkValidateOnClient,
|
|
@@ -49,7 +51,6 @@ export {
|
|
|
49
51
|
type VovkYieldType,
|
|
50
52
|
type VovkReturnType,
|
|
51
53
|
type VovkClientOptions,
|
|
52
|
-
type VovkWorkerSchema,
|
|
53
54
|
type VovkControllerSchema,
|
|
54
55
|
type VovkHandlerSchema,
|
|
55
56
|
StreamJSONResponse,
|
|
@@ -58,8 +59,6 @@ export {
|
|
|
58
59
|
HttpMethod,
|
|
59
60
|
createVovkApp,
|
|
60
61
|
createDecorator,
|
|
61
|
-
worker,
|
|
62
|
-
createWPC,
|
|
63
62
|
createRPC,
|
|
64
63
|
generateStaticAPI,
|
|
65
64
|
setClientValidatorsForHandler,
|
package/src/types.ts
CHANGED
|
@@ -20,21 +20,9 @@ export type VovkControllerSchema = {
|
|
|
20
20
|
handlers: Record<string, VovkHandlerSchema>;
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
-
export type VovkWorkerSchema = {
|
|
24
|
-
workerName: string;
|
|
25
|
-
originalWorkerName: string;
|
|
26
|
-
handlers: Record<
|
|
27
|
-
string,
|
|
28
|
-
{
|
|
29
|
-
isGenerator?: true;
|
|
30
|
-
}
|
|
31
|
-
>;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
23
|
export type VovkSchema = {
|
|
35
24
|
emitSchema: boolean;
|
|
36
25
|
segmentName: string;
|
|
37
|
-
workers: Record<string, VovkWorkerSchema>;
|
|
38
26
|
controllers: Record<string, VovkControllerSchema>;
|
|
39
27
|
};
|
|
40
28
|
|
|
@@ -58,11 +46,6 @@ export type VovkController = StaticClass &
|
|
|
58
46
|
[key: string]: unknown;
|
|
59
47
|
};
|
|
60
48
|
|
|
61
|
-
export type VovkWorker = StaticClass & {
|
|
62
|
-
_handlers: VovkWorkerSchema['handlers'];
|
|
63
|
-
[key: string]: unknown;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
49
|
export type DecoratorOptions = {
|
|
67
50
|
cors?: boolean;
|
|
68
51
|
headers?: Record<string, string>;
|
package/src/utils/getSchema.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import type { VovkSchema, VovkController,
|
|
1
|
+
import type { VovkSchema, VovkController, StaticClass } from '../types';
|
|
2
2
|
|
|
3
3
|
export default function getSchema(options: {
|
|
4
4
|
emitSchema?: boolean;
|
|
5
5
|
segmentName?: string;
|
|
6
6
|
controllers: Record<string, StaticClass>;
|
|
7
|
-
workers?: Record<string, StaticClass>;
|
|
8
7
|
exposeValidation?: boolean;
|
|
9
8
|
}) {
|
|
10
9
|
const exposeValidation = options?.exposeValidation ?? true;
|
|
@@ -13,7 +12,6 @@ export default function getSchema(options: {
|
|
|
13
12
|
emitSchema,
|
|
14
13
|
segmentName: options.segmentName ?? '',
|
|
15
14
|
controllers: {},
|
|
16
|
-
workers: {},
|
|
17
15
|
};
|
|
18
16
|
|
|
19
17
|
if (!emitSchema) return schema;
|
|
@@ -33,13 +31,5 @@ export default function getSchema(options: {
|
|
|
33
31
|
};
|
|
34
32
|
}
|
|
35
33
|
|
|
36
|
-
for (const [workerName, worker] of Object.entries(options.workers ?? {}) as [string, VovkWorker][]) {
|
|
37
|
-
schema.workers[workerName] = {
|
|
38
|
-
workerName,
|
|
39
|
-
originalWorkerName: worker.name,
|
|
40
|
-
handlers: { ...worker._handlers },
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
34
|
return schema;
|
|
45
35
|
}
|
package/.DS_Store
DELETED
|
Binary file
|
package/.npmignore
DELETED
package/.turbo/turbo-build.log
DELETED
package/.turbo/turbo-ncu.log
DELETED
package/.turbo/turbo-tsc.log
DELETED
package/dist/worker/createWPC.js
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createWPC = createWPC;
|
|
4
|
-
function createWPC(workerSchema, currentWorker) {
|
|
5
|
-
if (!workerSchema)
|
|
6
|
-
throw new Error('Worker schema is not provided');
|
|
7
|
-
const schema = workerSchema;
|
|
8
|
-
const instance = {
|
|
9
|
-
worker: currentWorker,
|
|
10
|
-
};
|
|
11
|
-
let callsKey = 0;
|
|
12
|
-
instance.terminate = () => {
|
|
13
|
-
if (instance._isTerminated)
|
|
14
|
-
return;
|
|
15
|
-
instance._isTerminated = true;
|
|
16
|
-
instance.worker?.terminate();
|
|
17
|
-
instance.worker = null;
|
|
18
|
-
};
|
|
19
|
-
instance.employ = (worker) => {
|
|
20
|
-
if (instance._isTerminated)
|
|
21
|
-
return instance;
|
|
22
|
-
instance._isTerminated = true;
|
|
23
|
-
instance.worker = worker;
|
|
24
|
-
return instance;
|
|
25
|
-
};
|
|
26
|
-
instance.fork = (worker) => createWPC(schema, worker);
|
|
27
|
-
for (const methodName of Object.keys(schema.handlers)) {
|
|
28
|
-
const { isGenerator } = schema.handlers[methodName];
|
|
29
|
-
if (isGenerator) {
|
|
30
|
-
const method = (...args) => {
|
|
31
|
-
const key = callsKey;
|
|
32
|
-
callsKey += 1;
|
|
33
|
-
return {
|
|
34
|
-
async *[Symbol.asyncIterator]() {
|
|
35
|
-
if (!instance.worker) {
|
|
36
|
-
throw new Error('Worker is not provided or terminated');
|
|
37
|
-
}
|
|
38
|
-
const w = instance.worker;
|
|
39
|
-
const messageQueue = [];
|
|
40
|
-
let messageResolver = null;
|
|
41
|
-
const onMessage = (e) => {
|
|
42
|
-
const { methodName: m, key: k } = e.data;
|
|
43
|
-
if (k !== key || m !== methodName) {
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
if (messageResolver) {
|
|
47
|
-
messageResolver(e.data);
|
|
48
|
-
messageResolver = null;
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
messageQueue.push(e.data);
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
const onError = (e) => {
|
|
55
|
-
if (messageResolver) {
|
|
56
|
-
messageResolver({ error: e.error });
|
|
57
|
-
messageResolver = null;
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
messageQueue.push({ error: e.error });
|
|
61
|
-
}
|
|
62
|
-
w.removeEventListener('message', onMessage);
|
|
63
|
-
w.removeEventListener('error', onError);
|
|
64
|
-
throw e.error;
|
|
65
|
-
};
|
|
66
|
-
w.addEventListener('message', onMessage);
|
|
67
|
-
w.addEventListener('error', onError);
|
|
68
|
-
w.postMessage({ key, args, methodName });
|
|
69
|
-
try {
|
|
70
|
-
while (true) {
|
|
71
|
-
let message = null;
|
|
72
|
-
if (messageQueue.length > 0) {
|
|
73
|
-
message = messageQueue.shift();
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
message = await new Promise((resolve) => {
|
|
77
|
-
messageResolver = resolve;
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
const { result, error, done } = message;
|
|
81
|
-
if (error) {
|
|
82
|
-
throw error;
|
|
83
|
-
}
|
|
84
|
-
if (done) {
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
yield result;
|
|
88
|
-
}
|
|
89
|
-
w.removeEventListener('message', onMessage);
|
|
90
|
-
w.removeEventListener('error', onError);
|
|
91
|
-
}
|
|
92
|
-
catch (e) {
|
|
93
|
-
w.removeEventListener('message', onMessage);
|
|
94
|
-
w.removeEventListener('error', onError);
|
|
95
|
-
throw e;
|
|
96
|
-
}
|
|
97
|
-
},
|
|
98
|
-
};
|
|
99
|
-
};
|
|
100
|
-
// @ts-expect-error TODO
|
|
101
|
-
instance[methodName] = method;
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
const method = (...args) => {
|
|
105
|
-
if (!instance.worker) {
|
|
106
|
-
throw new Error('Worker is not provided or terminated');
|
|
107
|
-
}
|
|
108
|
-
const w = instance.worker;
|
|
109
|
-
return new Promise((resolve, reject) => {
|
|
110
|
-
const key = callsKey;
|
|
111
|
-
callsKey += 1;
|
|
112
|
-
const onError = (e) => {
|
|
113
|
-
w.removeEventListener('message', onMessage);
|
|
114
|
-
w.removeEventListener('error', onError);
|
|
115
|
-
reject(e);
|
|
116
|
-
};
|
|
117
|
-
const onMessage = (e) => {
|
|
118
|
-
const { result, error, key: k, methodName: m } = e.data;
|
|
119
|
-
if (k !== key || m !== methodName) {
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
w.removeEventListener('message', onMessage);
|
|
123
|
-
w.removeEventListener('error', onError);
|
|
124
|
-
if (error) {
|
|
125
|
-
reject(error);
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
resolve(result);
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
w.addEventListener('message', onMessage);
|
|
132
|
-
w.addEventListener('error', onError);
|
|
133
|
-
w.postMessage({ key, args, methodName });
|
|
134
|
-
});
|
|
135
|
-
};
|
|
136
|
-
// @ts-expect-error TODO
|
|
137
|
-
instance[methodName] = method;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
return instance;
|
|
141
|
-
}
|
package/dist/worker/index.d.ts
DELETED
package/dist/worker/index.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createWPC = exports.worker = void 0;
|
|
4
|
-
var worker_1 = require("./worker");
|
|
5
|
-
Object.defineProperty(exports, "worker", { enumerable: true, get: function () { return worker_1.worker; } });
|
|
6
|
-
var createWPC_1 = require("./createWPC");
|
|
7
|
-
Object.defineProperty(exports, "createWPC", { enumerable: true, get: function () { return createWPC_1.createWPC; } });
|
package/dist/worker/types.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { KnownAny } from '../types';
|
|
2
|
-
type ToPromise<T> = T extends PromiseLike<unknown> ? T : Promise<T>;
|
|
3
|
-
type ToAsyncGenerator<T> = T extends AsyncGenerator<unknown, unknown, unknown> ? T : T extends Generator<infer U, unknown, unknown> ? AsyncGenerator<U, unknown, unknown> : AsyncGenerator<T, unknown, unknown>;
|
|
4
|
-
type ToProperReturnType<T> = T extends Generator<unknown, unknown, unknown> | AsyncGenerator<unknown, unknown, unknown> ? ToAsyncGenerator<T> : ToPromise<T>;
|
|
5
|
-
type OmitNever<T> = {
|
|
6
|
-
[K in keyof T as T[K] extends never ? never : K]: T[K];
|
|
7
|
-
};
|
|
8
|
-
export type WorkerPromiseInstanceWithNever<T> = {
|
|
9
|
-
[K in keyof T]: T[K] extends (...args: KnownAny[]) => KnownAny ? (...args: Parameters<T[K]>) => ToProperReturnType<ReturnType<T[K]>> : never;
|
|
10
|
-
};
|
|
11
|
-
export type WorkerPromiseInstance<T> = OmitNever<WorkerPromiseInstanceWithNever<T>> & {
|
|
12
|
-
terminate: () => void;
|
|
13
|
-
employ: (w: Worker) => WorkerPromiseInstance<T>;
|
|
14
|
-
fork: (w: Worker) => WorkerPromiseInstance<T>;
|
|
15
|
-
worker: Worker | null;
|
|
16
|
-
_isTerminated?: true;
|
|
17
|
-
[Symbol.dispose]: () => void;
|
|
18
|
-
};
|
|
19
|
-
export interface WorkerInput {
|
|
20
|
-
methodName: string;
|
|
21
|
-
args: unknown[];
|
|
22
|
-
key: number;
|
|
23
|
-
}
|
|
24
|
-
export interface WorkerOutput {
|
|
25
|
-
methodName: string;
|
|
26
|
-
result?: unknown;
|
|
27
|
-
error?: unknown;
|
|
28
|
-
done?: true;
|
|
29
|
-
key: number;
|
|
30
|
-
}
|
|
31
|
-
export {};
|
package/dist/worker/types.js
DELETED
package/dist/worker/worker.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function worker(): (t: object) => void;
|
package/dist/worker/worker.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.worker = worker;
|
|
4
|
-
function worker() {
|
|
5
|
-
return (t) => {
|
|
6
|
-
const target = t;
|
|
7
|
-
target._handlers = {};
|
|
8
|
-
for (const key of Object.getOwnPropertyNames(target)) {
|
|
9
|
-
const member = target[key];
|
|
10
|
-
if (typeof member === 'function') {
|
|
11
|
-
const prototype = Object.getPrototypeOf(member);
|
|
12
|
-
const isGenerator = prototype === Object.getPrototypeOf(function* () { }) ||
|
|
13
|
-
prototype === Object.getPrototypeOf(async function* () { });
|
|
14
|
-
target._handlers[key] = {};
|
|
15
|
-
if (isGenerator) {
|
|
16
|
-
target._handlers[key].isGenerator = true;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
if (typeof self === 'undefined')
|
|
21
|
-
return; // no-op in non-worker environment
|
|
22
|
-
const w = self;
|
|
23
|
-
w.onmessage = async (evt) => {
|
|
24
|
-
const { methodName, args, key } = evt.data;
|
|
25
|
-
try {
|
|
26
|
-
const result = await target[methodName](...args);
|
|
27
|
-
if (result && typeof result === 'object' && 'next' in result && typeof result.next === 'function') {
|
|
28
|
-
const iterable = result;
|
|
29
|
-
for await (const result of iterable) {
|
|
30
|
-
w.postMessage({ result, key, methodName });
|
|
31
|
-
}
|
|
32
|
-
w.postMessage({ done: true, key, methodName });
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
w.postMessage({ result, key, methodName });
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
catch (e) {
|
|
39
|
-
w.postMessage({ error: e, key, methodName });
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
}
|
package/src/worker/createWPC.ts
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import type { VovkWorkerSchema } from '../types';
|
|
2
|
-
import type { WorkerInput, WorkerOutput, WorkerPromiseInstance } from './types';
|
|
3
|
-
|
|
4
|
-
export function createWPC<T extends object>(workerSchema: object, currentWorker?: Worker): WorkerPromiseInstance<T> {
|
|
5
|
-
if (!workerSchema) throw new Error('Worker schema is not provided');
|
|
6
|
-
const schema = workerSchema as T & VovkWorkerSchema;
|
|
7
|
-
const instance = {
|
|
8
|
-
worker: currentWorker,
|
|
9
|
-
} as WorkerPromiseInstance<T>;
|
|
10
|
-
let callsKey = 0;
|
|
11
|
-
|
|
12
|
-
instance.terminate = () => {
|
|
13
|
-
if (instance._isTerminated) return;
|
|
14
|
-
instance._isTerminated = true;
|
|
15
|
-
instance.worker?.terminate();
|
|
16
|
-
instance.worker = null;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
instance.employ = (worker: Worker) => {
|
|
20
|
-
if (instance._isTerminated) return instance;
|
|
21
|
-
instance._isTerminated = true;
|
|
22
|
-
instance.worker = worker;
|
|
23
|
-
return instance;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
instance.fork = (worker: Worker) => createWPC<T>(schema, worker);
|
|
27
|
-
|
|
28
|
-
for (const methodName of Object.keys(schema.handlers) as (keyof T & string)[]) {
|
|
29
|
-
const { isGenerator } = schema.handlers[methodName];
|
|
30
|
-
|
|
31
|
-
if (isGenerator) {
|
|
32
|
-
const method = (...args: unknown[]) => {
|
|
33
|
-
const key = callsKey;
|
|
34
|
-
callsKey += 1;
|
|
35
|
-
return {
|
|
36
|
-
async *[Symbol.asyncIterator]() {
|
|
37
|
-
if (!instance.worker) {
|
|
38
|
-
throw new Error('Worker is not provided or terminated');
|
|
39
|
-
}
|
|
40
|
-
const w = instance.worker;
|
|
41
|
-
const messageQueue: WorkerOutput[] = [];
|
|
42
|
-
let messageResolver: ((message: WorkerOutput) => void) | null = null;
|
|
43
|
-
|
|
44
|
-
const onMessage = (e: MessageEvent<WorkerOutput>) => {
|
|
45
|
-
const { methodName: m, key: k } = e.data;
|
|
46
|
-
if (k !== key || m !== methodName) {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
if (messageResolver) {
|
|
50
|
-
messageResolver(e.data);
|
|
51
|
-
messageResolver = null;
|
|
52
|
-
} else {
|
|
53
|
-
messageQueue.push(e.data);
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const onError = (e: ErrorEvent) => {
|
|
58
|
-
if (messageResolver) {
|
|
59
|
-
messageResolver({ error: e.error } as WorkerOutput);
|
|
60
|
-
messageResolver = null;
|
|
61
|
-
} else {
|
|
62
|
-
messageQueue.push({ error: e.error } as WorkerOutput);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
w.removeEventListener('message', onMessage);
|
|
66
|
-
w.removeEventListener('error', onError);
|
|
67
|
-
throw e.error;
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
w.addEventListener('message', onMessage);
|
|
71
|
-
w.addEventListener('error', onError);
|
|
72
|
-
|
|
73
|
-
w.postMessage({ key, args, methodName } satisfies WorkerInput);
|
|
74
|
-
|
|
75
|
-
try {
|
|
76
|
-
while (true) {
|
|
77
|
-
let message: WorkerOutput | null = null;
|
|
78
|
-
if (messageQueue.length > 0) {
|
|
79
|
-
message = messageQueue.shift()!;
|
|
80
|
-
} else {
|
|
81
|
-
message = await new Promise<WorkerOutput>((resolve) => {
|
|
82
|
-
messageResolver = resolve;
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const { result, error, done } = message;
|
|
87
|
-
|
|
88
|
-
if (error) {
|
|
89
|
-
throw error;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (done) {
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
yield result;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
w.removeEventListener('message', onMessage);
|
|
100
|
-
w.removeEventListener('error', onError);
|
|
101
|
-
} catch (e) {
|
|
102
|
-
w.removeEventListener('message', onMessage);
|
|
103
|
-
w.removeEventListener('error', onError);
|
|
104
|
-
throw e;
|
|
105
|
-
}
|
|
106
|
-
},
|
|
107
|
-
};
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
// @ts-expect-error TODO
|
|
111
|
-
instance[methodName] = method;
|
|
112
|
-
} else {
|
|
113
|
-
const method = (...args: unknown[]) => {
|
|
114
|
-
if (!instance.worker) {
|
|
115
|
-
throw new Error('Worker is not provided or terminated');
|
|
116
|
-
}
|
|
117
|
-
const w = instance.worker;
|
|
118
|
-
return new Promise((resolve, reject) => {
|
|
119
|
-
const key = callsKey;
|
|
120
|
-
callsKey += 1;
|
|
121
|
-
|
|
122
|
-
const onError = (e: ErrorEvent) => {
|
|
123
|
-
w.removeEventListener('message', onMessage);
|
|
124
|
-
w.removeEventListener('error', onError);
|
|
125
|
-
reject(e);
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
const onMessage = (e: MessageEvent<WorkerOutput>) => {
|
|
129
|
-
const { result, error, key: k, methodName: m } = e.data;
|
|
130
|
-
if (k !== key || m !== methodName) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
w.removeEventListener('message', onMessage);
|
|
134
|
-
w.removeEventListener('error', onError);
|
|
135
|
-
if (error) {
|
|
136
|
-
reject(error);
|
|
137
|
-
} else {
|
|
138
|
-
resolve(result);
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
w.addEventListener('message', onMessage);
|
|
143
|
-
w.addEventListener('error', onError);
|
|
144
|
-
w.postMessage({ key, args, methodName } satisfies WorkerInput);
|
|
145
|
-
});
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
// @ts-expect-error TODO
|
|
149
|
-
instance[methodName] = method;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return instance;
|
|
153
|
-
}
|
package/src/worker/index.ts
DELETED
package/src/worker/types.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import type { KnownAny } from '../types';
|
|
2
|
-
|
|
3
|
-
type ToPromise<T> = T extends PromiseLike<unknown> ? T : Promise<T>;
|
|
4
|
-
type ToAsyncGenerator<T> =
|
|
5
|
-
T extends AsyncGenerator<unknown, unknown, unknown>
|
|
6
|
-
? T
|
|
7
|
-
: T extends Generator<infer U, unknown, unknown>
|
|
8
|
-
? AsyncGenerator<U, unknown, unknown>
|
|
9
|
-
: AsyncGenerator<T, unknown, unknown>;
|
|
10
|
-
type ToProperReturnType<T> = T extends Generator<unknown, unknown, unknown> | AsyncGenerator<unknown, unknown, unknown>
|
|
11
|
-
? ToAsyncGenerator<T>
|
|
12
|
-
: ToPromise<T>;
|
|
13
|
-
|
|
14
|
-
type OmitNever<T> = {
|
|
15
|
-
[K in keyof T as T[K] extends never ? never : K]: T[K];
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export type WorkerPromiseInstanceWithNever<T> = {
|
|
19
|
-
[K in keyof T]: T[K] extends (...args: KnownAny[]) => KnownAny
|
|
20
|
-
? (...args: Parameters<T[K]>) => ToProperReturnType<ReturnType<T[K]>>
|
|
21
|
-
: never;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export type WorkerPromiseInstance<T> = OmitNever<WorkerPromiseInstanceWithNever<T>> & {
|
|
25
|
-
terminate: () => void;
|
|
26
|
-
employ: (w: Worker) => WorkerPromiseInstance<T>;
|
|
27
|
-
fork: (w: Worker) => WorkerPromiseInstance<T>;
|
|
28
|
-
worker: Worker | null;
|
|
29
|
-
_isTerminated?: true;
|
|
30
|
-
[Symbol.dispose]: () => void;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export interface WorkerInput {
|
|
34
|
-
methodName: string;
|
|
35
|
-
args: unknown[];
|
|
36
|
-
key: number;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface WorkerOutput {
|
|
40
|
-
methodName: string;
|
|
41
|
-
result?: unknown;
|
|
42
|
-
error?: unknown;
|
|
43
|
-
done?: true;
|
|
44
|
-
key: number;
|
|
45
|
-
}
|
package/src/worker/worker.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import type { VovkWorker } from '../types';
|
|
2
|
-
import type { WorkerInput, WorkerOutput } from './types';
|
|
3
|
-
|
|
4
|
-
export function worker() {
|
|
5
|
-
return (t: object) => {
|
|
6
|
-
const target = t as Record<
|
|
7
|
-
string,
|
|
8
|
-
(
|
|
9
|
-
...args: unknown[]
|
|
10
|
-
) => Iterable<unknown> | AsyncIterable<unknown> | Promise<Iterable<unknown> | AsyncIterable<unknown>>
|
|
11
|
-
> &
|
|
12
|
-
VovkWorker;
|
|
13
|
-
target._handlers = {};
|
|
14
|
-
|
|
15
|
-
for (const key of Object.getOwnPropertyNames(target)) {
|
|
16
|
-
const member = target[key];
|
|
17
|
-
if (typeof member === 'function') {
|
|
18
|
-
const prototype = Object.getPrototypeOf(member) as unknown;
|
|
19
|
-
const isGenerator =
|
|
20
|
-
prototype === Object.getPrototypeOf(function* () {}) ||
|
|
21
|
-
prototype === Object.getPrototypeOf(async function* () {});
|
|
22
|
-
target._handlers[key] = {};
|
|
23
|
-
|
|
24
|
-
if (isGenerator) {
|
|
25
|
-
target._handlers[key].isGenerator = true;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (typeof self === 'undefined') return; // no-op in non-worker environment
|
|
31
|
-
|
|
32
|
-
const w = self as unknown as Worker;
|
|
33
|
-
|
|
34
|
-
w.onmessage = async (evt: MessageEvent<WorkerInput>) => {
|
|
35
|
-
const { methodName, args, key } = evt.data;
|
|
36
|
-
try {
|
|
37
|
-
const result = await target[methodName](...args);
|
|
38
|
-
|
|
39
|
-
if (result && typeof result === 'object' && 'next' in result && typeof result.next === 'function') {
|
|
40
|
-
const iterable = result as Iterable<unknown> | AsyncIterable<unknown>;
|
|
41
|
-
for await (const result of iterable) {
|
|
42
|
-
w.postMessage({ result, key, methodName } satisfies WorkerOutput);
|
|
43
|
-
}
|
|
44
|
-
w.postMessage({ done: true, key, methodName } satisfies WorkerOutput);
|
|
45
|
-
} else {
|
|
46
|
-
w.postMessage({ result, key, methodName } satisfies WorkerOutput);
|
|
47
|
-
}
|
|
48
|
-
} catch (e) {
|
|
49
|
-
w.postMessage({ error: e, key, methodName } satisfies WorkerOutput);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
};
|
|
53
|
-
}
|