@xxxaz/json-rpc-schema-typing 0.10.20 → 0.10.22
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/JsonSchemaValidator.js +1 -1
- package/cjs/JsonSchemaValidator.js.map +1 -1
- package/esm/JsonSchemaValidator.js +1 -1
- package/esm/JsonSchemaValidator.js.map +1 -1
- package/package.json +3 -2
- package/src/JsonRpcException.ts +135 -0
- package/src/JsonRpcMethod.ts +111 -0
- package/src/JsonRpcValidator.ts +41 -0
- package/src/JsonSchemaValidator.ts +195 -0
- package/src/WebSocketWrapper.ts +130 -0
- package/src/client/JsonRpcClient.ts +269 -0
- package/src/client/JsonRpcHttp2Client.ts +114 -0
- package/src/client/JsonRpcHttpClient.ts +71 -0
- package/src/client/JsonRpcMessagePortClient.ts +121 -0
- package/src/client/JsonRpcWebSocketClient.ts +143 -0
- package/src/client/index.ts +3 -0
- package/src/index.ts +11 -0
- package/src/router/FileSystemRouter.ts +78 -0
- package/src/router/JsonRpcRouter.ts +62 -0
- package/src/router/StaticRouter.ts +51 -0
- package/src/router/hashObject.ts +31 -0
- package/src/router/index.ts +3 -0
- package/src/schemas/Complex.ts +92 -0
- package/src/schemas/Primitive.ts +21 -0
- package/src/schemas/Structure.ts +42 -0
- package/src/schemas/index.ts +3 -0
- package/src/server/JsonRpcHttpReceiver.ts +126 -0
- package/src/server/JsonRpcMessagePortReceiver.ts +72 -0
- package/src/server/JsonRpcServer.ts +101 -0
- package/src/server/JsonRpcWebSocketReceiver.ts +49 -0
- package/src/server/index.ts +2 -0
- package/src/types.ts +81 -0
- package/src/utility.ts +54 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JsonSchemaValidator.js","sourceRoot":"","sources":["../src/JsonSchemaValidator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6BAAgF;AAIhF,MAAM,SAAS,GACX,OAAO,MAAM,KAA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C;QACP,CAAC,CAAC,CAAC;IACP,CAAC;AACL,CAAC,CAAC,EAAE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JsonSchemaValidator.js","sourceRoot":"","sources":["../src/JsonSchemaValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAwD,MAAM,KAAK,CAAC;AAIhF,MAAM,SAAS,GACX,OAAO,MAAM,KAAK,WAAW,IAAI,OAAO,MAAM,CAAC,QAAQ,KAAK,WAAW,CAAC;AAC5E,MAAM,WAAW,GACb,OAAO,IAAI,KAAK,WAAW;IAC3B,OAAQ,IAAY,CAAC,WAAW,KAAK,UAAU;IAC/C,OAAQ,UAAkB,CAAC,MAAM,KAAK,WAAW,CAAC;AACtD,MAAM,MAAM,GACR,OAAO,OAAO,KAAK,WAAW,IAAI,CAAC,CAAE,OAAe,CAAC,QAAQ,EAAE,IAAI,CAAC;AAExE,SAAS,WAAW;IAChB,IAAI,UAAU,CAAC,UAAU,EAAE,CAAC;QACxB,OAAO,UAAU,CAAC,UAAU,CAAC;IACjC,CAAC;IACD,IAAI,CAAC;QACD,OAAO,QAAQ,CAAC,
|
|
1
|
+
{"version":3,"file":"JsonSchemaValidator.js","sourceRoot":"","sources":["../src/JsonSchemaValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAwD,MAAM,KAAK,CAAC;AAIhF,MAAM,SAAS,GACX,OAAO,MAAM,KAAK,WAAW,IAAI,OAAO,MAAM,CAAC,QAAQ,KAAK,WAAW,CAAC;AAC5E,MAAM,WAAW,GACb,OAAO,IAAI,KAAK,WAAW;IAC3B,OAAQ,IAAY,CAAC,WAAW,KAAK,UAAU;IAC/C,OAAQ,UAAkB,CAAC,MAAM,KAAK,WAAW,CAAC;AACtD,MAAM,MAAM,GACR,OAAO,OAAO,KAAK,WAAW,IAAI,CAAC,CAAE,OAAe,CAAC,QAAQ,EAAE,IAAI,CAAC;AAExE,SAAS,WAAW;IAChB,I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CAAC,SAAS,EAAE,CAAC,EAAE,EAAE,EAAE;YACpC,MAAM,GAAG,GAAG,EAAE,CAAC,IAAmB,CAAC;YACnC,IAAI,GAAG,CAAC,MAAM,KAAK,UAAU;gBAAE,OAAO;YACtC,SAAS,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,EAAE;gBACtB,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;YAC7B,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;IACP,CAAC;AACL,CAAC,CAAC,EAAE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xxxaz/json-rpc-schema-typing",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.22",
|
|
4
4
|
"description": "",
|
|
5
5
|
"author": "xxx Inc.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
}
|
|
35
35
|
},
|
|
36
36
|
"files": [
|
|
37
|
+
"src",
|
|
37
38
|
"types",
|
|
38
39
|
"cjs",
|
|
39
40
|
"esm"
|
|
@@ -53,7 +54,7 @@
|
|
|
53
54
|
"@types/json-schema": "^7.0.15",
|
|
54
55
|
"@types/node": "^22.0.0",
|
|
55
56
|
"@types/ws": "^8.18.1",
|
|
56
|
-
"@xxxaz/stream-api-json": "^1.0.
|
|
57
|
+
"@xxxaz/stream-api-json": "^1.0.9",
|
|
57
58
|
"ajv": "^8.17.1",
|
|
58
59
|
"json-schema-to-ts": "^3.1.1",
|
|
59
60
|
"ws": "^8.18.2",
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { type JsonSerializable } from "@xxxaz/stream-api-json";
|
|
2
|
+
import { JsonRpcError } from "./types.js";
|
|
3
|
+
import * as InitialDefinedErros from './JsonRpcException.js';
|
|
4
|
+
|
|
5
|
+
type JsonRpcExceptionClass = {
|
|
6
|
+
readonly defaultMessage?: string;
|
|
7
|
+
readonly code: number;
|
|
8
|
+
new(message?: string, data?: JsonSerializable): JsonRpcException;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export abstract class JsonRpcException extends Error {
|
|
12
|
+
static #defined: Map<JsonRpcExceptionClass, string> = new Map();
|
|
13
|
+
static defineExceptions(classes: { [name: string]: object }) {
|
|
14
|
+
Object.entries(classes).forEach(([ name, cls ])=> {
|
|
15
|
+
if(!JsonRpcException.isPrototypeOf(cls)) return;
|
|
16
|
+
const ex = cls as JsonRpcExceptionClass;
|
|
17
|
+
if(!this.#defined.has(ex) && ex.code) {
|
|
18
|
+
this.#defined.set(ex, name);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static deserialize(error: JsonRpcError): JsonRpcException {
|
|
24
|
+
const { code, message, data } = error;
|
|
25
|
+
for(const cls of this.#defined.keys()) {
|
|
26
|
+
if(cls.code !== code) continue;
|
|
27
|
+
return new cls(message, data);
|
|
28
|
+
}
|
|
29
|
+
return new ResponseUncaughtError(error);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
constructor(message?: string, data?: JsonSerializable) {
|
|
33
|
+
if(!message) message = (new.target as any).defaultMessage;
|
|
34
|
+
super(message);
|
|
35
|
+
this.data = data;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
readonly data?: JsonSerializable;
|
|
39
|
+
|
|
40
|
+
get name(): string {
|
|
41
|
+
return JsonRpcException.#defined.get(this.constructor as JsonRpcExceptionClass)
|
|
42
|
+
?? this.constructor.name;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
get code(): number {
|
|
46
|
+
return (this.constructor as JsonRpcExceptionClass).code;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
serialize(): JsonRpcError {
|
|
50
|
+
return {
|
|
51
|
+
code: this.code,
|
|
52
|
+
message: this.message,
|
|
53
|
+
data: this.data,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
toJSON() {
|
|
58
|
+
return this.serialize();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export class ParseError extends JsonRpcException {
|
|
64
|
+
static readonly code = -32700;
|
|
65
|
+
static readonly defaultMessage = 'Invalid JSON was received by the server.';
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export class InvalidRequest extends JsonRpcException {
|
|
69
|
+
static readonly code = -32600;
|
|
70
|
+
static readonly defaultMessage = 'The JSON sent is not a valid Request object.';
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export class MethodNotFound extends JsonRpcException {
|
|
74
|
+
static readonly code = -32601;
|
|
75
|
+
static readonly defaultMessage = 'The method does not exist / is not available.';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export class InvalidParams extends JsonRpcException {
|
|
79
|
+
static readonly code = -32602;
|
|
80
|
+
static readonly defaultMessage = 'Invalid method parameter(s)';
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export class InternalError extends JsonRpcException {
|
|
84
|
+
static readonly code = -32603;
|
|
85
|
+
static readonly defaultMessage = 'Internal JSON-RPC error.';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export class InvalidContext extends InternalError {
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export class InvalidReturn extends InternalError {
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
*
|
|
97
|
+
* @property {number} code define to between -32000 to -32099
|
|
98
|
+
*/
|
|
99
|
+
export abstract class ServerError extends JsonRpcException {
|
|
100
|
+
static define(classes: { [name: string]: JsonRpcExceptionClass }) {
|
|
101
|
+
Object.entries(classes).forEach(([name, cls])=>{
|
|
102
|
+
if(!ServerError.prototype.isPrototypeOf(cls)) {
|
|
103
|
+
throw new Error(`${name} is not extends by ServerError`);
|
|
104
|
+
}
|
|
105
|
+
if(cls.code < -32099 || -32000 < cls.code) {
|
|
106
|
+
throw new Error(`Defined ServerError code must be between -32000 to -32099 (${name} ${cls.code})`);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
JsonRpcException.defineExceptions(classes);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export class ResponseUncaughtError extends JsonRpcException {
|
|
114
|
+
constructor(readonly error: JsonRpcError) {
|
|
115
|
+
super(error.message ?? 'Uncaught Error', error.data);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export class ClientUncaughtError extends JsonRpcException {
|
|
120
|
+
constructor(message: string, data?: JsonSerializable) {
|
|
121
|
+
super(message, data);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
export class ClientHttpError extends JsonRpcException {
|
|
125
|
+
constructor(message: string, data?: JsonSerializable) {
|
|
126
|
+
super(message, data);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
export class WebSocketConnectionError extends JsonRpcException {
|
|
130
|
+
constructor(message: string, data?: JsonSerializable) {
|
|
131
|
+
super(message, data);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
JsonRpcException.defineExceptions(InitialDefinedErros);
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { type JSONSchema, type FromSchema } from "./types.js";
|
|
2
|
+
import { InvalidContext } from './JsonRpcException.js';
|
|
3
|
+
import { $Object, $Tuple } from './schemas/Structure.js';
|
|
4
|
+
|
|
5
|
+
type ByPositionSchema = {
|
|
6
|
+
readonly type: 'array';
|
|
7
|
+
readonly items: readonly JSONSchema[];
|
|
8
|
+
};
|
|
9
|
+
type ObjectProperties = {
|
|
10
|
+
readonly [key: string]: JSONSchema;
|
|
11
|
+
};
|
|
12
|
+
type ByNameSchema = {
|
|
13
|
+
readonly type: 'object';
|
|
14
|
+
readonly properties: ObjectProperties;
|
|
15
|
+
};
|
|
16
|
+
export type ParameterSchema = ByPositionSchema|ByNameSchema|undefined;
|
|
17
|
+
|
|
18
|
+
export interface JsonRpcMethodSchema<PrmSch extends ParameterSchema, RtnSch extends JSONSchema|undefined> {
|
|
19
|
+
readonly $params?: PrmSch;
|
|
20
|
+
readonly $return?: RtnSch;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type Params<Schema>
|
|
24
|
+
= Schema extends ByPositionSchema
|
|
25
|
+
? FromSchema<Schema>
|
|
26
|
+
: Schema extends ByNameSchema
|
|
27
|
+
? [FromSchema<Schema>]
|
|
28
|
+
: Schema extends undefined
|
|
29
|
+
? []
|
|
30
|
+
: never;
|
|
31
|
+
export type Return<Schema>
|
|
32
|
+
= Schema extends undefined
|
|
33
|
+
? void
|
|
34
|
+
: Schema extends JSONSchema
|
|
35
|
+
? FromSchema<Schema>
|
|
36
|
+
: never;
|
|
37
|
+
|
|
38
|
+
export type JsonRpcMethod<Context, PrmSch extends ParameterSchema, RtnSch extends JSONSchema|undefined>
|
|
39
|
+
= (this: Context, ...params: Params<PrmSch>) => Promise<Return<RtnSch>>|Return<RtnSch>;
|
|
40
|
+
|
|
41
|
+
type ContextClass<Ctx> = { new(...args: any[]): Ctx };
|
|
42
|
+
type Context<Cls, FB> = Cls extends ContextClass<infer Ctx> ? Ctx : FB;
|
|
43
|
+
|
|
44
|
+
const methodKey = Symbol('JsonRpcMethodDefinition.method');
|
|
45
|
+
export class JsonRpcMethodDefinition<Context, PrmSch extends ParameterSchema, RtnSch extends JSONSchema|undefined> implements JsonRpcMethodSchema<PrmSch, RtnSch> {
|
|
46
|
+
static readonly method: typeof methodKey = methodKey;
|
|
47
|
+
readonly [methodKey]: JsonRpcMethod<Context, PrmSch, RtnSch>;
|
|
48
|
+
|
|
49
|
+
constructor(
|
|
50
|
+
method: JsonRpcMethod<Context, PrmSch, RtnSch>,
|
|
51
|
+
readonly $params: PrmSch,
|
|
52
|
+
readonly $return: RtnSch,
|
|
53
|
+
readonly $contextClass?: ContextClass<Context>,
|
|
54
|
+
) {
|
|
55
|
+
this[methodKey] = method;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
$apply(ctx: Context, params: any) {
|
|
59
|
+
if(this.$contextClass && !(ctx instanceof this.$contextClass)) {
|
|
60
|
+
const passedCtxType = (ctx as any)?.constructor?.name ?? typeof ctx;
|
|
61
|
+
throw new InvalidContext(`Invalid context: expected ${this.$contextClass.name} but got ${passedCtxType}`);
|
|
62
|
+
}
|
|
63
|
+
if (this.$params?.type === 'object') params = [params];
|
|
64
|
+
return this[methodKey].apply(ctx, params);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static get builder(){
|
|
68
|
+
return new DefinitionBuilder(undefined, undefined, undefined);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
class DefinitionBuilder<CtxCls extends ContextClass<any>|undefined, PrmSch extends ParameterSchema, RtnSch extends JSONSchema|undefined> implements JsonRpcMethodSchema<PrmSch, RtnSch> {
|
|
73
|
+
constructor(
|
|
74
|
+
readonly $contextClass: CtxCls,
|
|
75
|
+
readonly $params: PrmSch,
|
|
76
|
+
readonly $return: RtnSch,
|
|
77
|
+
) {}
|
|
78
|
+
|
|
79
|
+
define<Ctx = {}>(method: JsonRpcMethod<Context<CtxCls, Ctx>, PrmSch, RtnSch>) {
|
|
80
|
+
return new JsonRpcMethodDefinition<Context<CtxCls, Ctx>, PrmSch, RtnSch>(method, this.$params, this.$return, this.$contextClass);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
contextClass<CtxCls extends ContextClass<any>>(contextClass: CtxCls) {
|
|
84
|
+
return new DefinitionBuilder(contextClass, this.$params, this.$return);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
params<NewPrm extends ParameterSchema>(params: NewPrm) {
|
|
88
|
+
return new DefinitionBuilder(this.$contextClass, params, this.$return);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
paramsByName<NewPrm extends ObjectProperties>(params: NewPrm) {
|
|
92
|
+
return new DefinitionBuilder(this.$contextClass, $Object(params), this.$return);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
paramsByPosition<NewPrm extends readonly JSONSchema[]>(...params: NewPrm) {
|
|
96
|
+
return new DefinitionBuilder(this.$contextClass, $Tuple(...params), this.$return);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return<NewRtn extends JSONSchema|undefined>(rtn: NewRtn) {
|
|
100
|
+
return new DefinitionBuilder(this.$contextClass, this.$params, rtn);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** @deprecated paramsByPosition を使用してください */
|
|
104
|
+
paramsSchema<NewPrm extends readonly JSONSchema[]>(...params: NewPrm) {
|
|
105
|
+
return this.paramsByPosition(...params);
|
|
106
|
+
}
|
|
107
|
+
/** @deprecated return を使用してください */
|
|
108
|
+
returnSchema<NewRtn extends JSONSchema|undefined>(rtn: NewRtn) {
|
|
109
|
+
return this.return(rtn);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { FromSchema } from 'json-schema-to-ts';
|
|
2
|
+
import { JsonRpcMethodSchema, ParameterSchema } from "./JsonRpcMethod.js";
|
|
3
|
+
import { JSONSchema } from "./types.js";
|
|
4
|
+
import { InvalidParams, InvalidReturn } from './JsonRpcException.js';
|
|
5
|
+
import { JsonSchemaValidator } from './JsonSchemaValidator.js';
|
|
6
|
+
|
|
7
|
+
type Valid<S> = S extends JSONSchema ? FromSchema<S> : never;
|
|
8
|
+
|
|
9
|
+
export class JsonRpcValidator<
|
|
10
|
+
P extends ParameterSchema,
|
|
11
|
+
R extends JSONSchema | undefined,
|
|
12
|
+
Sch extends JsonRpcMethodSchema<P, R>
|
|
13
|
+
> {
|
|
14
|
+
constructor(readonly schema: Sch) {
|
|
15
|
+
this.#paramValidator = schema.$params ? new JsonSchemaValidator(schema.$params): null;
|
|
16
|
+
this.#returnValidator = schema.$return ? new JsonSchemaValidator(schema.$return) : null;
|
|
17
|
+
}
|
|
18
|
+
#paramValidator:JsonSchemaValidator | null;
|
|
19
|
+
#returnValidator:JsonSchemaValidator | null;
|
|
20
|
+
|
|
21
|
+
validateParams(params: unknown): Valid<Sch['$params']> {
|
|
22
|
+
if(!this.#paramValidator) return params as Valid<Sch['$params']>;
|
|
23
|
+
const validated = this.#paramValidator.validate(params);
|
|
24
|
+
if(!validated.valid && validated.errors.length > 0) {
|
|
25
|
+
const message = validated.errors.map(({ instancePath, message }) => `params${instancePath ?? ''}: ${message}`).join('\n');
|
|
26
|
+
throw new InvalidParams(message, validated.errors);
|
|
27
|
+
}
|
|
28
|
+
return params as Valid<Sch['$params']>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
validateReturn(result: unknown): Valid<Sch['$return']> {
|
|
32
|
+
if(!this.#returnValidator) return result as Valid<Sch['$return']>;
|
|
33
|
+
const validated = this.#returnValidator.validate(result);
|
|
34
|
+
if (!validated.valid && validated.errors.length > 0) {
|
|
35
|
+
const message = validated.errors.map(({ instancePath, message }) => `return.${instancePath ?? ''}: ${message}`).join('\n');
|
|
36
|
+
throw new InvalidReturn(message, validated.errors);
|
|
37
|
+
}
|
|
38
|
+
return result as Valid<Sch['$return']>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { Ajv, Options as AjvOptions, ValidateFunction, ErrorObject } from 'ajv';
|
|
2
|
+
import { type JSONSchema } from 'json-schema-to-ts';
|
|
3
|
+
import type { Worker as NodeWorker } from 'worker_threads';
|
|
4
|
+
|
|
5
|
+
const isBrowser =
|
|
6
|
+
typeof window !== 'undefined' && typeof window.document !== 'undefined';
|
|
7
|
+
const isWebWorker =
|
|
8
|
+
typeof self !== 'undefined' &&
|
|
9
|
+
typeof (self as any).postMessage === 'function' &&
|
|
10
|
+
typeof (globalThis as any).Window === 'undefined';
|
|
11
|
+
const isNode =
|
|
12
|
+
typeof process !== 'undefined' && !!(process as any).versions?.node;
|
|
13
|
+
|
|
14
|
+
function currentFile() {
|
|
15
|
+
if (globalThis.__filename) {
|
|
16
|
+
return globalThis.__filename;
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
return Function("return import.meta.filename")();
|
|
20
|
+
} catch (err) {
|
|
21
|
+
if (!(err instanceof SyntaxError)) throw err;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const script = globalThis.document?.currentScript as HTMLScriptElement | undefined;
|
|
25
|
+
if (script?.src) {
|
|
26
|
+
return script?.src;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
type SerializedError = {
|
|
34
|
+
propertyName?: string;
|
|
35
|
+
message?: string;
|
|
36
|
+
data?: any;
|
|
37
|
+
keyword?: string;
|
|
38
|
+
instancePath?: string;
|
|
39
|
+
schemaPath?: string;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
function * serializeErrors(error?: ErrorObject[]|null) : Generator<SerializedError> {
|
|
43
|
+
if(!error) return;
|
|
44
|
+
for(const { propertyName, message, schemaPath, data, keyword, instancePath, params } of error) {
|
|
45
|
+
const err = {} as SerializedError;
|
|
46
|
+
if(propertyName) err.propertyName = propertyName;
|
|
47
|
+
if(message) err.message = message;
|
|
48
|
+
if(data) err.data = data;
|
|
49
|
+
if(keyword) err.keyword = keyword;
|
|
50
|
+
if(instancePath) err.instancePath = instancePath;
|
|
51
|
+
if(schemaPath) err.schemaPath = schemaPath;
|
|
52
|
+
if (Object.keys(err).length > 0) {
|
|
53
|
+
yield err;
|
|
54
|
+
}
|
|
55
|
+
if (params.errors) {
|
|
56
|
+
yield * serializeErrors(params.errors);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
type ValidationResult =
|
|
63
|
+
| { valid: true }
|
|
64
|
+
| {
|
|
65
|
+
valid: false;
|
|
66
|
+
errors: SerializedError[];
|
|
67
|
+
errorsText: string;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const methodName = '@xxxaz/json-schema-typing.JsonSchemaValidator.validate';
|
|
71
|
+
|
|
72
|
+
export class JsonSchemaValidator {
|
|
73
|
+
constructor(
|
|
74
|
+
readonly schema: JSONSchema & object,
|
|
75
|
+
ajvOptions?: AjvOptions
|
|
76
|
+
) {
|
|
77
|
+
this.#ajv = new Ajv(ajvOptions);
|
|
78
|
+
this.#validate = this.#ajv.compile(this.schema);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
readonly #ajv: Ajv;
|
|
82
|
+
readonly #validate: ValidateFunction;
|
|
83
|
+
|
|
84
|
+
validate(data: unknown): ValidationResult {
|
|
85
|
+
const valid = this.#validate(data);
|
|
86
|
+
if (valid) return { valid: true };
|
|
87
|
+
const errors = Array.from(serializeErrors(this.#validate.errors ?? []));
|
|
88
|
+
|
|
89
|
+
// TODO: エラーメッセージの整形を検討する
|
|
90
|
+
const errorsText = this.#ajv.errorsText(this.#validate.errors, {
|
|
91
|
+
separator: '\n',
|
|
92
|
+
});
|
|
93
|
+
return {
|
|
94
|
+
valid: false,
|
|
95
|
+
errors,
|
|
96
|
+
errorsText,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async validateAsync(data: unknown): Promise<ValidationResult> {
|
|
101
|
+
const worker = await getWorker(this.schema);
|
|
102
|
+
if (!worker) {
|
|
103
|
+
// Worker が使えない環境では同期処理を行う
|
|
104
|
+
return this.validate(data);
|
|
105
|
+
}
|
|
106
|
+
const id = crypto.randomUUID();
|
|
107
|
+
const listener = Promise.withResolvers<ValidationResult>();
|
|
108
|
+
listeners[id] = listener;
|
|
109
|
+
worker.postMessage({ method: methodName, schema: this.schema, data, id });
|
|
110
|
+
return listener.promise;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
type WorkerType = NodeWorker | Worker;
|
|
115
|
+
let workers: WeakMap<object, WorkerType> = new WeakMap();
|
|
116
|
+
let listeners: Record<string, PromiseWithResolvers<ValidationResult>> = {};
|
|
117
|
+
async function getWorker(key: object): Promise<WorkerType | null> {
|
|
118
|
+
if (workers.has(key)) return workers.get(key) ?? null;
|
|
119
|
+
|
|
120
|
+
const src = currentFile();
|
|
121
|
+
if (!src) return null;
|
|
122
|
+
|
|
123
|
+
let worker: WorkerType;
|
|
124
|
+
if (isNode) {
|
|
125
|
+
const NodeWorkerThreads = await import('worker_threads');
|
|
126
|
+
worker = new NodeWorkerThreads.Worker(src);
|
|
127
|
+
worker.on('message', (data) => onMessage(data));
|
|
128
|
+
worker.on('error', (err) => onError(err));
|
|
129
|
+
worker.on('messageerror', (data) => onError(data));
|
|
130
|
+
} else if (isBrowser || isWebWorker) {
|
|
131
|
+
worker = new Worker(src);
|
|
132
|
+
worker.addEventListener('message', (ev) => onMessage(ev.data));
|
|
133
|
+
worker.addEventListener('error', (ev) => onError(ev.error));
|
|
134
|
+
worker.addEventListener('messageerror', (ev) => onFaild(ev.data.id));
|
|
135
|
+
} else {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
workers.set(key, worker);
|
|
139
|
+
return worker;
|
|
140
|
+
}
|
|
141
|
+
function onMessage(data: {id: string, result?: ValidationResult, error?: any}) {
|
|
142
|
+
const listener = listeners[data.id];
|
|
143
|
+
if (listener) {
|
|
144
|
+
if (data.error) {
|
|
145
|
+
listener.reject(data.error);
|
|
146
|
+
} else {
|
|
147
|
+
listener.resolve(data.result!);
|
|
148
|
+
}
|
|
149
|
+
delete listeners[data.id];
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function onError(err: Error) {
|
|
153
|
+
Object.values(listeners).forEach(({ reject }) => reject(err));
|
|
154
|
+
listeners = {};
|
|
155
|
+
}
|
|
156
|
+
function onFaild(data: { id: string }) {
|
|
157
|
+
const listener = listeners[data.id];
|
|
158
|
+
if (listener) {
|
|
159
|
+
listener.reject(new Error(`Worker validation failed: ${data.id}`));
|
|
160
|
+
delete listeners[data.id];
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
(async function kickValidateInWorker(): Promise<void> {
|
|
165
|
+
type ReceiveMesg = { method: string; id: string; schema: JSONSchema & object; data: unknown };
|
|
166
|
+
const onMessage = (msg: ReceiveMesg, callback: (result: {}) => void) => {
|
|
167
|
+
const { id, schema, data } = msg;
|
|
168
|
+
try {
|
|
169
|
+
const result = new JsonSchemaValidator(schema).validate(data);
|
|
170
|
+
callback({ id, result });
|
|
171
|
+
} catch (error) {
|
|
172
|
+
callback({ id, error });
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
if (isNode) {
|
|
176
|
+
const NodeWorkerThreads = await import('worker_threads');
|
|
177
|
+
const { parentPort } = NodeWorkerThreads;
|
|
178
|
+
if (!parentPort) return;
|
|
179
|
+
parentPort.on('message', (msg: ReceiveMesg) => {
|
|
180
|
+
if (msg.method !== methodName) return;
|
|
181
|
+
onMessage(msg, (result) => parentPort.postMessage(result));
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
if (isWebWorker) {
|
|
185
|
+
self.addEventListener('message', (ev) => {
|
|
186
|
+
const msg = ev.data as ReceiveMesg;
|
|
187
|
+
if (msg.method !== methodName) return;
|
|
188
|
+
onMessage(msg, (result) => {
|
|
189
|
+
self.postMessage(result);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
})();
|
|
194
|
+
|
|
195
|
+
|