hypha-rpc 0.20.3 → 0.20.5
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.
|
@@ -24,8 +24,9 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
24
24
|
/* harmony export */ RPC: () => (/* binding */ RPC)
|
|
25
25
|
/* harmony export */ });
|
|
26
26
|
/* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils */ "./src/utils/index.js");
|
|
27
|
-
/* harmony import */ var
|
|
28
|
-
/* harmony import */ var _msgpack_msgpack__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @msgpack/msgpack */ "./node_modules/@msgpack/msgpack/dist.es5+esm/
|
|
27
|
+
/* harmony import */ var _utils_schema__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils/schema */ "./src/utils/schema.js");
|
|
28
|
+
/* harmony import */ var _msgpack_msgpack__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @msgpack/msgpack */ "./node_modules/@msgpack/msgpack/dist.es5+esm/decode.mjs");
|
|
29
|
+
/* harmony import */ var _msgpack_msgpack__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! @msgpack/msgpack */ "./node_modules/@msgpack/msgpack/dist.es5+esm/encode.mjs");
|
|
29
30
|
/**
|
|
30
31
|
* Contains the RPC object used both by the application
|
|
31
32
|
* site, and by each plugin
|
|
@@ -34,6 +35,7 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
34
35
|
|
|
35
36
|
|
|
36
37
|
|
|
38
|
+
|
|
37
39
|
const API_VERSION = "0.3.0";
|
|
38
40
|
const CHUNK_SIZE = 1024 * 500;
|
|
39
41
|
|
|
@@ -82,8 +84,86 @@ function _get_schema(obj, name = null, skipContext = false) {
|
|
|
82
84
|
} else {
|
|
83
85
|
return { type: "function" };
|
|
84
86
|
}
|
|
87
|
+
} else if (typeof obj === "number") {
|
|
88
|
+
return { type: "number" };
|
|
89
|
+
} else if (typeof obj === "string") {
|
|
90
|
+
return { type: "string" };
|
|
91
|
+
} else if (typeof obj === "boolean") {
|
|
92
|
+
return { type: "boolean" };
|
|
93
|
+
} else if (obj === null) {
|
|
94
|
+
return { type: "null" };
|
|
95
|
+
} else {
|
|
96
|
+
return {};
|
|
85
97
|
}
|
|
86
|
-
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function _annotate_service(service, serviceTypeInfo) {
|
|
101
|
+
function validateKeys(serviceDict, schemaDict, path = "root") {
|
|
102
|
+
// Validate that all keys in schemaDict exist in serviceDict
|
|
103
|
+
for (let key in schemaDict) {
|
|
104
|
+
if (!serviceDict.hasOwnProperty(key)) {
|
|
105
|
+
throw new Error(`Missing key '${key}' in service at path '${path}'`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Check for any unexpected keys in serviceDict
|
|
110
|
+
for (let key in serviceDict) {
|
|
111
|
+
if (key !== "type" && !schemaDict.hasOwnProperty(key)) {
|
|
112
|
+
throw new Error(`Unexpected key '${key}' in service at path '${path}'`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function annotateRecursive(newService, schemaInfo, path = "root") {
|
|
118
|
+
if (typeof newService === "object" && !Array.isArray(newService)) {
|
|
119
|
+
validateKeys(newService, schemaInfo, path);
|
|
120
|
+
for (let k in newService) {
|
|
121
|
+
let v = newService[k];
|
|
122
|
+
let newPath = `${path}.${k}`;
|
|
123
|
+
if (typeof v === "object" && !Array.isArray(v)) {
|
|
124
|
+
annotateRecursive(v, schemaInfo[k], newPath);
|
|
125
|
+
} else if (typeof v === "function") {
|
|
126
|
+
if (schemaInfo.hasOwnProperty(k)) {
|
|
127
|
+
newService[k] = (0,_utils_schema__WEBPACK_IMPORTED_MODULE_1__.schemaFunction)(v, {
|
|
128
|
+
name: schemaInfo[k]["name"],
|
|
129
|
+
description: schemaInfo[k].description || "",
|
|
130
|
+
parameters: schemaInfo[k]["parameters"],
|
|
131
|
+
});
|
|
132
|
+
} else {
|
|
133
|
+
throw new Error(
|
|
134
|
+
`Missing schema for function '${k}' at path '${newPath}'`,
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
} else if (Array.isArray(newService)) {
|
|
140
|
+
if (newService.length !== schemaInfo.length) {
|
|
141
|
+
throw new Error(`Length mismatch at path '${path}'`);
|
|
142
|
+
}
|
|
143
|
+
newService.forEach((v, i) => {
|
|
144
|
+
let newPath = `${path}[${i}]`;
|
|
145
|
+
if (typeof v === "object" && !Array.isArray(v)) {
|
|
146
|
+
annotateRecursive(v, schemaInfo[i], newPath);
|
|
147
|
+
} else if (typeof v === "function") {
|
|
148
|
+
if (schemaInfo.hasOwnProperty(i)) {
|
|
149
|
+
newService[i] = (0,_utils_schema__WEBPACK_IMPORTED_MODULE_1__.schemaFunction)(v, {
|
|
150
|
+
name: schemaInfo[i]["name"],
|
|
151
|
+
description: schemaInfo[i].description || "",
|
|
152
|
+
parameters: schemaInfo[i]["parameters"],
|
|
153
|
+
});
|
|
154
|
+
} else {
|
|
155
|
+
throw new Error(
|
|
156
|
+
`Missing schema for function at index ${i} in path '${newPath}'`,
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
validateKeys(service, serviceTypeInfo["definition"]);
|
|
165
|
+
annotateRecursive(service, serviceTypeInfo["definition"]);
|
|
166
|
+
return service;
|
|
87
167
|
}
|
|
88
168
|
|
|
89
169
|
function getFunctionInfo(func) {
|
|
@@ -377,7 +457,7 @@ class RPC extends _utils__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
|
|
|
377
457
|
}
|
|
378
458
|
cache[key] = concatArrayBuffers(cache[key]);
|
|
379
459
|
console.debug(`Processing message ${key} (bytes=${cache[key].byteLength})`);
|
|
380
|
-
let unpacker = (0,
|
|
460
|
+
let unpacker = (0,_msgpack_msgpack__WEBPACK_IMPORTED_MODULE_2__.decodeMulti)(cache[key]);
|
|
381
461
|
const { done, value } = unpacker.next();
|
|
382
462
|
const main = value;
|
|
383
463
|
// Make sure the fields are from trusted source
|
|
@@ -405,7 +485,7 @@ class RPC extends _utils__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
|
|
|
405
485
|
const main = JSON.parse(message);
|
|
406
486
|
this._fire(main["type"], main);
|
|
407
487
|
} else if (message instanceof ArrayBuffer) {
|
|
408
|
-
let unpacker = (0,
|
|
488
|
+
let unpacker = (0,_msgpack_msgpack__WEBPACK_IMPORTED_MODULE_2__.decodeMulti)(message);
|
|
409
489
|
const { done, value } = unpacker.next();
|
|
410
490
|
const main = value;
|
|
411
491
|
// Add trusted context to the method call
|
|
@@ -629,15 +709,36 @@ class RPC extends _utils__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
|
|
|
629
709
|
|
|
630
710
|
_extract_service_info(service) {
|
|
631
711
|
const skipContext = service.config.require_context;
|
|
632
|
-
const
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
712
|
+
const serviceSchema = _get_schema(service, null, skipContext);
|
|
713
|
+
const serviceInfo = {
|
|
714
|
+
config: service.config,
|
|
715
|
+
id: `${this._client_id}:${service["id"]}`,
|
|
716
|
+
name: service.name || service["id"],
|
|
717
|
+
description: service.description || "",
|
|
718
|
+
type: service.type || "generic",
|
|
719
|
+
docs: service.docs || null,
|
|
720
|
+
app_id: this._app_id,
|
|
721
|
+
service_schema: serviceSchema,
|
|
722
|
+
};
|
|
636
723
|
return serviceInfo;
|
|
637
724
|
}
|
|
638
725
|
|
|
639
|
-
async
|
|
640
|
-
|
|
726
|
+
async get_service_schema(service) {
|
|
727
|
+
const skipContext = service.config.require_context;
|
|
728
|
+
return _get_schema(service, null, skipContext);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
async register_service(api, overwrite, notify = true, check_type = false) {
|
|
732
|
+
if (check_type && api.type) {
|
|
733
|
+
try {
|
|
734
|
+
const manager_svc = await this.get_manager_service();
|
|
735
|
+
const type_info = await manager_svc.get_service_type(api.type);
|
|
736
|
+
api = _annotate_service(api, type_info);
|
|
737
|
+
} catch (e) {
|
|
738
|
+
throw new Error(`Failed to get service type ${api.type}, error: ${e}`);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
|
|
641
742
|
const service = this.add_service(api, overwrite);
|
|
642
743
|
const serviceInfo = this._extract_service_info(service);
|
|
643
744
|
if (notify) {
|
|
@@ -822,9 +923,9 @@ class RPC extends _utils__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
|
|
|
822
923
|
this._fire(main_message.type, main_message);
|
|
823
924
|
return;
|
|
824
925
|
}
|
|
825
|
-
let message_package = (0,
|
|
926
|
+
let message_package = (0,_msgpack_msgpack__WEBPACK_IMPORTED_MODULE_3__.encode)(main_message);
|
|
826
927
|
if (extra_data) {
|
|
827
|
-
const extra = (0,
|
|
928
|
+
const extra = (0,_msgpack_msgpack__WEBPACK_IMPORTED_MODULE_3__.encode)(extra_data);
|
|
828
929
|
message_package = new Uint8Array([...message_package, ...extra]);
|
|
829
930
|
}
|
|
830
931
|
const total_size = message_package.length;
|
|
@@ -947,9 +1048,9 @@ class RPC extends _utils__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
|
|
|
947
1048
|
);
|
|
948
1049
|
}
|
|
949
1050
|
// The message consists of two segments, the main message and extra data
|
|
950
|
-
let message_package = (0,
|
|
1051
|
+
let message_package = (0,_msgpack_msgpack__WEBPACK_IMPORTED_MODULE_3__.encode)(main_message);
|
|
951
1052
|
if (extra_data) {
|
|
952
|
-
const extra = (0,
|
|
1053
|
+
const extra = (0,_msgpack_msgpack__WEBPACK_IMPORTED_MODULE_3__.encode)(extra_data);
|
|
953
1054
|
message_package = new Uint8Array([...message_package, ...extra]);
|
|
954
1055
|
}
|
|
955
1056
|
const total_size = message_package.length;
|
|
@@ -4868,6 +4969,21 @@ async function connectToServer(config) {
|
|
|
4868
4969
|
type: "object",
|
|
4869
4970
|
},
|
|
4870
4971
|
});
|
|
4972
|
+
wm.getServiceSchema = (0,_utils_schema_js__WEBPACK_IMPORTED_MODULE_2__.schemaFunction)(rpc.get_service_schema, {
|
|
4973
|
+
name: "getServiceSchema",
|
|
4974
|
+
description: "Get the service schema.",
|
|
4975
|
+
parameters: {
|
|
4976
|
+
properties: {
|
|
4977
|
+
service: {
|
|
4978
|
+
description: "The service to extract schema",
|
|
4979
|
+
type: "object",
|
|
4980
|
+
},
|
|
4981
|
+
},
|
|
4982
|
+
required: ["service"],
|
|
4983
|
+
type: "object",
|
|
4984
|
+
},
|
|
4985
|
+
});
|
|
4986
|
+
|
|
4871
4987
|
wm.registerService = (0,_utils_schema_js__WEBPACK_IMPORTED_MODULE_2__.schemaFunction)(rpc.register_service.bind(rpc), {
|
|
4872
4988
|
name: "registerService",
|
|
4873
4989
|
description: "Register a service.",
|