Initial commit
This commit is contained in:
25
node_modules/json-rpc-2.0/dist/client.d.ts
generated
vendored
Normal file
25
node_modules/json-rpc-2.0/dist/client.d.ts
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
import { JSONRPCErrorResponse, JSONRPCID, JSONRPCParams, JSONRPCRequest, JSONRPCResponse } from "./models";
|
||||
export type SendRequest<ClientParams> = (payload: any, clientParams: ClientParams) => PromiseLike<void> | void;
|
||||
export type CreateID = () => JSONRPCID;
|
||||
export interface JSONRPCRequester<ClientParams> {
|
||||
request(method: string, params?: JSONRPCParams, clientParams?: ClientParams): PromiseLike<any>;
|
||||
requestAdvanced(request: JSONRPCRequest, clientParams?: ClientParams): PromiseLike<JSONRPCResponse>;
|
||||
requestAdvanced(request: JSONRPCRequest[], clientParams?: ClientParams): PromiseLike<JSONRPCResponse[]>;
|
||||
}
|
||||
export declare class JSONRPCClient<ClientParams = void> implements JSONRPCRequester<ClientParams> {
|
||||
private _send;
|
||||
private createID?;
|
||||
private idToResolveMap;
|
||||
private id;
|
||||
constructor(_send: SendRequest<ClientParams>, createID?: CreateID | undefined);
|
||||
private _createID;
|
||||
timeout(delay: number, overrideCreateJSONRPCErrorResponse?: (id: JSONRPCID) => JSONRPCErrorResponse): JSONRPCRequester<ClientParams>;
|
||||
request(method: string, params: JSONRPCParams, clientParams: ClientParams): PromiseLike<any>;
|
||||
private requestWithID;
|
||||
requestAdvanced(request: JSONRPCRequest, clientParams: ClientParams): PromiseLike<JSONRPCResponse>;
|
||||
requestAdvanced(request: JSONRPCRequest[], clientParams: ClientParams): PromiseLike<JSONRPCResponse[]>;
|
||||
notify(method: string, params: JSONRPCParams, clientParams: ClientParams): void;
|
||||
send(payload: any, clientParams: ClientParams): Promise<void>;
|
||||
rejectAllPendingRequests(message: string): void;
|
||||
receive(responses: JSONRPCResponse | JSONRPCResponse[]): void;
|
||||
}
|
||||
187
node_modules/json-rpc-2.0/dist/client.js
generated
vendored
Normal file
187
node_modules/json-rpc-2.0/dist/client.js
generated
vendored
Normal file
@@ -0,0 +1,187 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.JSONRPCClient = void 0;
|
||||
var models_1 = require("./models");
|
||||
var internal_1 = require("./internal");
|
||||
var JSONRPCClient = /** @class */ (function () {
|
||||
function JSONRPCClient(_send, createID) {
|
||||
this._send = _send;
|
||||
this.createID = createID;
|
||||
this.idToResolveMap = new Map();
|
||||
this.id = 0;
|
||||
}
|
||||
JSONRPCClient.prototype._createID = function () {
|
||||
if (this.createID) {
|
||||
return this.createID();
|
||||
}
|
||||
else {
|
||||
return ++this.id;
|
||||
}
|
||||
};
|
||||
JSONRPCClient.prototype.timeout = function (delay, overrideCreateJSONRPCErrorResponse) {
|
||||
var _this = this;
|
||||
if (overrideCreateJSONRPCErrorResponse === void 0) { overrideCreateJSONRPCErrorResponse = function (id) {
|
||||
return (0, models_1.createJSONRPCErrorResponse)(id, internal_1.DefaultErrorCode, "Request timeout");
|
||||
}; }
|
||||
var timeoutRequest = function (ids, request) {
|
||||
var timeoutID = setTimeout(function () {
|
||||
ids.forEach(function (id) {
|
||||
var resolve = _this.idToResolveMap.get(id);
|
||||
if (resolve) {
|
||||
_this.idToResolveMap.delete(id);
|
||||
resolve(overrideCreateJSONRPCErrorResponse(id));
|
||||
}
|
||||
});
|
||||
}, delay);
|
||||
return request().then(function (result) {
|
||||
clearTimeout(timeoutID);
|
||||
return result;
|
||||
}, function (error) {
|
||||
clearTimeout(timeoutID);
|
||||
return Promise.reject(error);
|
||||
});
|
||||
};
|
||||
var requestAdvanced = function (request, clientParams) {
|
||||
var ids = (!Array.isArray(request) ? [request] : request)
|
||||
.map(function (request) { return request.id; })
|
||||
.filter(isDefinedAndNonNull);
|
||||
return timeoutRequest(ids, function () {
|
||||
return _this.requestAdvanced(request, clientParams);
|
||||
});
|
||||
};
|
||||
return {
|
||||
request: function (method, params, clientParams) {
|
||||
var id = _this._createID();
|
||||
return timeoutRequest([id], function () {
|
||||
return _this.requestWithID(method, params, clientParams, id);
|
||||
});
|
||||
},
|
||||
requestAdvanced: function (request, clientParams) { return requestAdvanced(request, clientParams); },
|
||||
};
|
||||
};
|
||||
JSONRPCClient.prototype.request = function (method, params, clientParams) {
|
||||
return this.requestWithID(method, params, clientParams, this._createID());
|
||||
};
|
||||
JSONRPCClient.prototype.requestWithID = function (method, params, clientParams, id) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var request, response;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
request = (0, models_1.createJSONRPCRequest)(id, method, params);
|
||||
return [4 /*yield*/, this.requestAdvanced(request, clientParams)];
|
||||
case 1:
|
||||
response = _a.sent();
|
||||
if (response.result !== undefined && !response.error) {
|
||||
return [2 /*return*/, response.result];
|
||||
}
|
||||
else if (response.result === undefined && response.error) {
|
||||
return [2 /*return*/, Promise.reject(new models_1.JSONRPCErrorException(response.error.message, response.error.code, response.error.data))];
|
||||
}
|
||||
else {
|
||||
return [2 /*return*/, Promise.reject(new Error("An unexpected error occurred"))];
|
||||
}
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
JSONRPCClient.prototype.requestAdvanced = function (requests, clientParams) {
|
||||
var _this = this;
|
||||
var areRequestsOriginallyArray = Array.isArray(requests);
|
||||
if (!Array.isArray(requests)) {
|
||||
requests = [requests];
|
||||
}
|
||||
var requestsWithID = requests.filter(function (request) {
|
||||
return isDefinedAndNonNull(request.id);
|
||||
});
|
||||
var promises = requestsWithID.map(function (request) {
|
||||
return new Promise(function (resolve) { return _this.idToResolveMap.set(request.id, resolve); });
|
||||
});
|
||||
var promise = Promise.all(promises).then(function (responses) {
|
||||
if (areRequestsOriginallyArray || !responses.length) {
|
||||
return responses;
|
||||
}
|
||||
else {
|
||||
return responses[0];
|
||||
}
|
||||
});
|
||||
return this.send(areRequestsOriginallyArray ? requests : requests[0], clientParams).then(function () { return promise; }, function (error) {
|
||||
requestsWithID.forEach(function (request) {
|
||||
_this.receive((0, models_1.createJSONRPCErrorResponse)(request.id, internal_1.DefaultErrorCode, (error && error.message) || "Failed to send a request"));
|
||||
});
|
||||
return promise;
|
||||
});
|
||||
};
|
||||
JSONRPCClient.prototype.notify = function (method, params, clientParams) {
|
||||
var request = (0, models_1.createJSONRPCNotification)(method, params);
|
||||
this.send(request, clientParams).then(undefined, function () { return undefined; });
|
||||
};
|
||||
JSONRPCClient.prototype.send = function (payload, clientParams) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
return [2 /*return*/, this._send(payload, clientParams)];
|
||||
});
|
||||
});
|
||||
};
|
||||
JSONRPCClient.prototype.rejectAllPendingRequests = function (message) {
|
||||
this.idToResolveMap.forEach(function (resolve, id) {
|
||||
return resolve((0, models_1.createJSONRPCErrorResponse)(id, internal_1.DefaultErrorCode, message));
|
||||
});
|
||||
this.idToResolveMap.clear();
|
||||
};
|
||||
JSONRPCClient.prototype.receive = function (responses) {
|
||||
var _this = this;
|
||||
if (!Array.isArray(responses)) {
|
||||
responses = [responses];
|
||||
}
|
||||
responses.forEach(function (response) {
|
||||
var resolve = _this.idToResolveMap.get(response.id);
|
||||
if (resolve) {
|
||||
_this.idToResolveMap.delete(response.id);
|
||||
resolve(response);
|
||||
}
|
||||
});
|
||||
};
|
||||
return JSONRPCClient;
|
||||
}());
|
||||
exports.JSONRPCClient = JSONRPCClient;
|
||||
var isDefinedAndNonNull = function (value) {
|
||||
return value !== undefined && value !== null;
|
||||
};
|
||||
1
node_modules/json-rpc-2.0/dist/client.spec.d.ts
generated
vendored
Normal file
1
node_modules/json-rpc-2.0/dist/client.spec.d.ts
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export {};
|
||||
430
node_modules/json-rpc-2.0/dist/client.spec.js
generated
vendored
Normal file
430
node_modules/json-rpc-2.0/dist/client.spec.js
generated
vendored
Normal file
@@ -0,0 +1,430 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var mocha_1 = require("mocha");
|
||||
var chai_1 = require("chai");
|
||||
var sinon = require("sinon");
|
||||
var _1 = require(".");
|
||||
(0, mocha_1.describe)("JSONRPCClient", function () {
|
||||
var client;
|
||||
var id;
|
||||
var lastRequest;
|
||||
var lastClientParams;
|
||||
var resolve;
|
||||
var reject;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
id = 0;
|
||||
lastRequest = undefined;
|
||||
resolve = undefined;
|
||||
reject = undefined;
|
||||
var send = function (request, clientParams) {
|
||||
lastRequest = request;
|
||||
lastClientParams = clientParams;
|
||||
return new Promise(function (givenResolve, givenReject) {
|
||||
resolve = givenResolve;
|
||||
reject = givenReject;
|
||||
});
|
||||
};
|
||||
client = new _1.JSONRPCClient(send, function () { return ++id; });
|
||||
});
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
(0, mocha_1.describe)("requesting", function () {
|
||||
var result;
|
||||
var error;
|
||||
var promise;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
result = undefined;
|
||||
error = undefined;
|
||||
promise = client.request("foo", ["bar"], { token: "" }).then(function (givenResult) { return (result = givenResult); }, function (givenError) { return (error = givenError); });
|
||||
});
|
||||
(0, mocha_1.it)("should send the request", function () {
|
||||
(0, chai_1.expect)(lastRequest).to.deep.equal({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: id,
|
||||
method: "foo",
|
||||
params: ["bar"],
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("succeeded on client side", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
resolve();
|
||||
});
|
||||
(0, mocha_1.describe)("and succeeded on server side too", function () {
|
||||
var response;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
response = {
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: id,
|
||||
result: "foo",
|
||||
};
|
||||
client.receive(response);
|
||||
return promise;
|
||||
});
|
||||
(0, mocha_1.it)("should resolve the result", function () {
|
||||
(0, chai_1.expect)(result).to.equal(response.result);
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("and succeeded on server side with falsy but defined result", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
client.receive({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: id,
|
||||
result: 0,
|
||||
});
|
||||
return promise;
|
||||
});
|
||||
(0, mocha_1.it)("should resolve the result", function () {
|
||||
(0, chai_1.expect)(result).to.equal(0);
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("but failed on server side", function () {
|
||||
var response;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
response = {
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: id,
|
||||
error: {
|
||||
code: 0,
|
||||
message: "This is a test. Do not panic.",
|
||||
data: { optional: "data" },
|
||||
},
|
||||
};
|
||||
client.receive(response);
|
||||
return promise;
|
||||
});
|
||||
(0, mocha_1.it)("should reject with the error message, code and data", function () {
|
||||
(0, chai_1.expect)(error.message).to.equal(response.error.message);
|
||||
(0, chai_1.expect)(error.code).to.equal(response.error.code);
|
||||
(0, chai_1.expect)(error.data).to.equal(response.error.data);
|
||||
});
|
||||
(0, mocha_1.it)("should reject with a JSONRPCErrorException", function () {
|
||||
(0, chai_1.expect)(error instanceof Error).to.be.true;
|
||||
(0, chai_1.expect)(error instanceof _1.JSONRPCErrorException).to.be.true;
|
||||
(0, chai_1.expect)(error.toObject()).to.deep.equal(response.error);
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("but server responded invalid response", function () {
|
||||
(0, mocha_1.describe)("like having both result and error", function () {
|
||||
var response;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
response = {
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: id,
|
||||
result: "foo",
|
||||
error: {
|
||||
code: 0,
|
||||
message: "bar",
|
||||
},
|
||||
};
|
||||
client.receive(response);
|
||||
return promise;
|
||||
});
|
||||
(0, mocha_1.it)("should reject", function () {
|
||||
(0, chai_1.expect)(error).to.not.be.undefined;
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("like not having both result and error", function () {
|
||||
var response;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
response = {
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: id,
|
||||
};
|
||||
client.receive(response);
|
||||
return promise;
|
||||
});
|
||||
(0, mocha_1.it)("should reject", function () {
|
||||
(0, chai_1.expect)(error).to.not.be.undefined;
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("but I reject all pending requests", function () {
|
||||
var message;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
message = "Connection is closed.";
|
||||
client.rejectAllPendingRequests(message);
|
||||
return promise;
|
||||
});
|
||||
(0, mocha_1.it)("should reject the request", function () {
|
||||
(0, chai_1.expect)(error.message).to.equal(message);
|
||||
});
|
||||
(0, mocha_1.describe)("receiving a response", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
client.receive({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: id,
|
||||
result: "foo",
|
||||
});
|
||||
return promise;
|
||||
});
|
||||
(0, mocha_1.it)("should not resolve the promise again", function () {
|
||||
(0, chai_1.expect)(result).to.be.undefined;
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("failed on client side", function () {
|
||||
var expected;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
expected = new Error("This is a test. Do not panic.");
|
||||
reject(expected);
|
||||
return promise;
|
||||
});
|
||||
(0, mocha_1.it)("should reject the promise", function () {
|
||||
(0, chai_1.expect)(error.message).to.equal(expected.message);
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("failed on client side with no error object", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
reject(undefined);
|
||||
return promise;
|
||||
});
|
||||
(0, mocha_1.it)("should reject the promise", function () {
|
||||
(0, chai_1.expect)(error.message).to.equal("Failed to send a request");
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("failed on client side with an error object without message", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
reject({});
|
||||
return promise;
|
||||
});
|
||||
(0, mocha_1.it)("should reject the promise", function () {
|
||||
(0, chai_1.expect)(error.message).to.equal("Failed to send a request");
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("requesting batch", function () {
|
||||
var requests;
|
||||
var actualResponses;
|
||||
var expectedResponses;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
requests = [
|
||||
{ jsonrpc: _1.JSONRPC, id: 0, method: "foo" },
|
||||
{ jsonrpc: _1.JSONRPC, id: 1, method: "foo2" },
|
||||
];
|
||||
client
|
||||
.requestAdvanced(requests, { token: "" })
|
||||
.then(function (responses) { return (actualResponses = responses); });
|
||||
resolve();
|
||||
expectedResponses = [
|
||||
{ jsonrpc: _1.JSONRPC, id: 0, result: "foo" },
|
||||
{ jsonrpc: _1.JSONRPC, id: 1, result: "foo2" },
|
||||
];
|
||||
client.receive(expectedResponses);
|
||||
});
|
||||
(0, mocha_1.it)("should send requests in batch", function () {
|
||||
(0, chai_1.expect)(lastRequest).to.deep.equal(requests);
|
||||
});
|
||||
(0, mocha_1.it)("should return responses", function () {
|
||||
(0, chai_1.expect)(actualResponses).to.deep.equal(expectedResponses);
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("requesting with client params", function () {
|
||||
var expected;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
expected = { token: "baz" };
|
||||
client.request("foo", undefined, expected);
|
||||
});
|
||||
(0, mocha_1.it)("should pass the client params to send function", function () {
|
||||
(0, chai_1.expect)(lastClientParams).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("requesting with timeout", function () {
|
||||
var delay;
|
||||
var fakeTimers;
|
||||
var promise;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
fakeTimers = sinon.useFakeTimers();
|
||||
delay = 1000;
|
||||
promise = client.timeout(delay).request("foo");
|
||||
resolve();
|
||||
});
|
||||
(0, mocha_1.describe)("timing out", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
fakeTimers.tick(delay);
|
||||
});
|
||||
(0, mocha_1.it)("should reject", function () {
|
||||
return promise.then(function () { return Promise.reject(new Error("Expected to fail")); }, function () { return undefined; });
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("not timing out", function () {
|
||||
var result;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
result = "foo";
|
||||
client.receive({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: lastRequest.id,
|
||||
result: result,
|
||||
});
|
||||
});
|
||||
(0, mocha_1.it)("should respond", function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
var actual;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, promise];
|
||||
case 1:
|
||||
actual = _a.sent();
|
||||
(0, chai_1.expect)(actual).to.equal(result);
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("requesting advanced with timeout", function () {
|
||||
var delay;
|
||||
var fakeTimers;
|
||||
var promise;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
fakeTimers = sinon.useFakeTimers();
|
||||
delay = 1000;
|
||||
promise = client.timeout(delay).requestAdvanced({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: ++id,
|
||||
method: "foo",
|
||||
});
|
||||
resolve();
|
||||
});
|
||||
(0, mocha_1.describe)("timing out", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
fakeTimers.tick(delay);
|
||||
});
|
||||
(0, mocha_1.it)("should reject", function () {
|
||||
return promise.then(function (result) {
|
||||
if (!result.error) {
|
||||
return Promise.reject(new Error("Expected to fail"));
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("not timing out", function () {
|
||||
var result;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
result = {
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: lastRequest.id,
|
||||
result: result,
|
||||
};
|
||||
client.receive(result);
|
||||
});
|
||||
(0, mocha_1.it)("should respond", function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
var actual;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, promise];
|
||||
case 1:
|
||||
actual = _a.sent();
|
||||
(0, chai_1.expect)(actual).to.deep.equal(result);
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("requesting with custom timeout error response", function () {
|
||||
var delay;
|
||||
var fakeTimers;
|
||||
var errorCode;
|
||||
var errorMessage;
|
||||
var errorData;
|
||||
var promise;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
fakeTimers = sinon.useFakeTimers();
|
||||
delay = 1000;
|
||||
errorCode = 123;
|
||||
errorMessage = "Custom error message";
|
||||
errorData = "Custom error data";
|
||||
promise = client
|
||||
.timeout(delay, function (id) {
|
||||
return (0, _1.createJSONRPCErrorResponse)(id, errorCode, errorMessage, errorData);
|
||||
})
|
||||
.requestAdvanced({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: ++id,
|
||||
method: "foo",
|
||||
});
|
||||
resolve();
|
||||
});
|
||||
(0, mocha_1.describe)("timing out", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
fakeTimers.tick(delay);
|
||||
});
|
||||
(0, mocha_1.it)("should reject with the custom error", function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
var actual, expected;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, promise];
|
||||
case 1:
|
||||
actual = _a.sent();
|
||||
expected = {
|
||||
code: errorCode,
|
||||
message: errorMessage,
|
||||
data: errorData,
|
||||
};
|
||||
(0, chai_1.expect)(actual.error).to.deep.equal(expected);
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("notifying", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
client.notify("foo", ["bar"], { token: "" });
|
||||
});
|
||||
(0, mocha_1.it)("should send the notification", function () {
|
||||
(0, chai_1.expect)(lastRequest).to.deep.equal({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
method: "foo",
|
||||
params: ["bar"],
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("notifying with client params", function () {
|
||||
var expected;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
expected = { token: "baz" };
|
||||
client.notify("foo", undefined, expected);
|
||||
});
|
||||
(0, mocha_1.it)("should pass the client params to send function", function () {
|
||||
(0, chai_1.expect)(lastClientParams).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
5
node_modules/json-rpc-2.0/dist/index.d.ts
generated
vendored
Normal file
5
node_modules/json-rpc-2.0/dist/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
export * from "./client";
|
||||
export * from "./interfaces";
|
||||
export * from "./models";
|
||||
export * from "./server";
|
||||
export * from "./server-and-client";
|
||||
21
node_modules/json-rpc-2.0/dist/index.js
generated
vendored
Normal file
21
node_modules/json-rpc-2.0/dist/index.js
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
||||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
__exportStar(require("./client"), exports);
|
||||
__exportStar(require("./interfaces"), exports);
|
||||
__exportStar(require("./models"), exports);
|
||||
__exportStar(require("./server"), exports);
|
||||
__exportStar(require("./server-and-client"), exports);
|
||||
1
node_modules/json-rpc-2.0/dist/index.spec.d.ts
generated
vendored
Normal file
1
node_modules/json-rpc-2.0/dist/index.spec.d.ts
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
import "mocha";
|
||||
40
node_modules/json-rpc-2.0/dist/index.spec.js
generated
vendored
Normal file
40
node_modules/json-rpc-2.0/dist/index.spec.js
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
require("mocha");
|
||||
var chai_1 = require("chai");
|
||||
var _1 = require(".");
|
||||
describe("JSONRPCClient and JSONRPCServer", function () {
|
||||
var server;
|
||||
var client;
|
||||
var id;
|
||||
beforeEach(function () {
|
||||
id = 0;
|
||||
server = new _1.JSONRPCServer();
|
||||
client = new _1.JSONRPCClient(function (request) {
|
||||
return server.receive(request).then(function (response) {
|
||||
if (response) {
|
||||
client.receive(response);
|
||||
}
|
||||
});
|
||||
}, function () { return ++id; });
|
||||
});
|
||||
it("sending a request should resolve the result", function () {
|
||||
beforeEach(function () {
|
||||
server.addMethod("foo", function () { return "bar"; });
|
||||
return client
|
||||
.request("foo", undefined)
|
||||
.then(function (result) { return (0, chai_1.expect)(result).to.equal("bar"); });
|
||||
});
|
||||
});
|
||||
it("sending a notification should send a notification", function () {
|
||||
var received;
|
||||
beforeEach(function () {
|
||||
server.addMethod("foo", function (_a) {
|
||||
var text = _a[0];
|
||||
return (received = text);
|
||||
});
|
||||
client.notify("foo", ["bar"]);
|
||||
(0, chai_1.expect)(received).to.equal("bar");
|
||||
});
|
||||
});
|
||||
});
|
||||
15
node_modules/json-rpc-2.0/dist/interfaces.d.ts
generated
vendored
Normal file
15
node_modules/json-rpc-2.0/dist/interfaces.d.ts
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
import type { JSONRPCClient } from "./client";
|
||||
import type { JSONRPCServer } from "./server";
|
||||
import type { JSONRPCServerAndClient } from "./server-and-client";
|
||||
type MethodsType = Record<string, (params?: any) => any>;
|
||||
export interface TypedJSONRPCClient<Methods extends MethodsType, ClientParams = void> extends JSONRPCClient<ClientParams> {
|
||||
request<Method extends Extract<keyof Methods, string>>(method: Method, ...args: Parameters<Methods[Method]>[0] extends undefined ? [void, ClientParams] : [Parameters<Methods[Method]>[0], ClientParams]): PromiseLike<ReturnType<Methods[Method]>>;
|
||||
}
|
||||
export interface TypedJSONRPCServer<Methods extends MethodsType, ServerParams = void> extends JSONRPCServer<ServerParams> {
|
||||
addMethod<Method extends Extract<keyof Methods, string>>(name: Method, method: (params: Parameters<Methods[Method]>[0], serverParams: ServerParams) => ReturnType<Methods[Method]> | PromiseLike<ReturnType<Methods[Method]>>): void;
|
||||
}
|
||||
export interface TypedJSONRPCServerAndClient<ServerMethods extends MethodsType, ClientMethods extends MethodsType, ServerParams = void, ClientParams = void> extends JSONRPCServerAndClient<ServerParams, ClientParams> {
|
||||
request<Method extends Extract<keyof ClientMethods, string>>(method: Method, ...args: Parameters<ClientMethods[Method]>[0] extends undefined ? [void, ClientParams] : [Parameters<ClientMethods[Method]>[0], ClientParams]): PromiseLike<ReturnType<ClientMethods[Method]>>;
|
||||
addMethod<Method extends Extract<keyof ServerMethods, string>>(name: Method, method: (params: Parameters<ServerMethods[Method]>[0], serverParams: ServerParams) => ReturnType<ServerMethods[Method]> | PromiseLike<ReturnType<ServerMethods[Method]>>): void;
|
||||
}
|
||||
export {};
|
||||
2
node_modules/json-rpc-2.0/dist/interfaces.js
generated
vendored
Normal file
2
node_modules/json-rpc-2.0/dist/interfaces.js
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
1
node_modules/json-rpc-2.0/dist/interfaces.spec.d.ts
generated
vendored
Normal file
1
node_modules/json-rpc-2.0/dist/interfaces.spec.d.ts
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
import "mocha";
|
||||
201
node_modules/json-rpc-2.0/dist/interfaces.spec.js
generated
vendored
Normal file
201
node_modules/json-rpc-2.0/dist/interfaces.spec.js
generated
vendored
Normal file
@@ -0,0 +1,201 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
require("mocha");
|
||||
var chai_1 = require("chai");
|
||||
var client_1 = require("./client");
|
||||
var server_1 = require("./server");
|
||||
var server_and_client_1 = require("./server-and-client");
|
||||
describe("interfaces", function () {
|
||||
describe("independent server and client", function () {
|
||||
var client;
|
||||
var server;
|
||||
beforeEach(function () {
|
||||
client = new client_1.JSONRPCClient(function (request) { return __awaiter(void 0, void 0, void 0, function () {
|
||||
var response;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, server.receive(request)];
|
||||
case 1:
|
||||
response = _a.sent();
|
||||
if (response) {
|
||||
client.receive(response);
|
||||
}
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
server = new server_1.JSONRPCServer();
|
||||
});
|
||||
describe("calling method with no args no return", function () {
|
||||
var noArgsNoReturnCalled;
|
||||
beforeEach(function () {
|
||||
noArgsNoReturnCalled = false;
|
||||
server.addMethod("noArgsNoReturn", function () {
|
||||
noArgsNoReturnCalled = true;
|
||||
});
|
||||
return client.request("noArgsNoReturn");
|
||||
});
|
||||
it("should call the method", function () {
|
||||
(0, chai_1.expect)(noArgsNoReturnCalled).to.be.true;
|
||||
});
|
||||
});
|
||||
describe("calling method with no args", function () {
|
||||
var expected;
|
||||
var actual;
|
||||
beforeEach(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
expected = "return value";
|
||||
server.addMethod("noArgs", function () {
|
||||
return expected;
|
||||
});
|
||||
return [4 /*yield*/, client.request("noArgs")];
|
||||
case 1:
|
||||
actual = _a.sent();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
it("should call the method", function () {
|
||||
(0, chai_1.expect)(actual).to.equal(expected);
|
||||
});
|
||||
});
|
||||
describe("calling method with object args", function () {
|
||||
var actual;
|
||||
beforeEach(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
server.addMethod("objectArgs", function (_a) {
|
||||
var foo = _a.foo, bar = _a.bar;
|
||||
return "".concat(foo, ".").concat(bar);
|
||||
});
|
||||
return [4 /*yield*/, client.request("objectArgs", {
|
||||
foo: "string value",
|
||||
bar: 123,
|
||||
})];
|
||||
case 1:
|
||||
actual = _a.sent();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
it("should call the method", function () {
|
||||
(0, chai_1.expect)(actual).to.equal("string value.123");
|
||||
});
|
||||
});
|
||||
describe("calling method with array args", function () {
|
||||
var actual;
|
||||
beforeEach(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
server.addMethod("arrayArgs", function (_a) {
|
||||
var foo = _a[0], bar = _a[1];
|
||||
return "".concat(foo, ".").concat(bar);
|
||||
});
|
||||
return [4 /*yield*/, client.request("arrayArgs", ["string value", 123])];
|
||||
case 1:
|
||||
actual = _a.sent();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
it("should call the method", function () {
|
||||
(0, chai_1.expect)(actual).to.equal("string value.123");
|
||||
});
|
||||
});
|
||||
});
|
||||
describe("server and client", function () {
|
||||
var serverAndClientA;
|
||||
var serverAndClientB;
|
||||
beforeEach(function () {
|
||||
serverAndClientA = new server_and_client_1.JSONRPCServerAndClient(new server_1.JSONRPCServer(), new client_1.JSONRPCClient(function (request) {
|
||||
return serverAndClientB.receiveAndSend(request);
|
||||
}));
|
||||
serverAndClientB = new server_and_client_1.JSONRPCServerAndClient(new server_1.JSONRPCServer(), new client_1.JSONRPCClient(function (request) {
|
||||
return serverAndClientA.receiveAndSend(request);
|
||||
}));
|
||||
serverAndClientA.addMethod("echo", function (_a) {
|
||||
var message = _a.message;
|
||||
return message;
|
||||
});
|
||||
serverAndClientB.addMethod("sum", function (_a) {
|
||||
var x = _a.x, y = _a.y;
|
||||
return x + y;
|
||||
});
|
||||
});
|
||||
describe("calling method from server A to B", function () {
|
||||
var actual;
|
||||
beforeEach(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, serverAndClientA.request("sum", { x: 1, y: 2 })];
|
||||
case 1:
|
||||
actual = _a.sent();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
it("should call the method", function () {
|
||||
(0, chai_1.expect)(actual).to.equal(3);
|
||||
});
|
||||
});
|
||||
describe("calling method from server B to A", function () {
|
||||
var expected;
|
||||
var actual;
|
||||
beforeEach(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
expected = "hello";
|
||||
return [4 /*yield*/, serverAndClientB.request("echo", { message: expected })];
|
||||
case 1:
|
||||
actual = _a.sent();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
it("should call the method", function () {
|
||||
(0, chai_1.expect)(actual).to.equal(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
1
node_modules/json-rpc-2.0/dist/internal.d.ts
generated
vendored
Normal file
1
node_modules/json-rpc-2.0/dist/internal.d.ts
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export declare const DefaultErrorCode = 0;
|
||||
4
node_modules/json-rpc-2.0/dist/internal.js
generated
vendored
Normal file
4
node_modules/json-rpc-2.0/dist/internal.js
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.DefaultErrorCode = void 0;
|
||||
exports.DefaultErrorCode = 0;
|
||||
51
node_modules/json-rpc-2.0/dist/models.d.ts
generated
vendored
Normal file
51
node_modules/json-rpc-2.0/dist/models.d.ts
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
export type JSONRPC = "2.0";
|
||||
export declare const JSONRPC: JSONRPC;
|
||||
export type JSONRPCID = string | number | null;
|
||||
export type JSONRPCParams = any;
|
||||
export declare const isJSONRPCID: (id: any) => id is JSONRPCID;
|
||||
export interface JSONRPCRequest {
|
||||
jsonrpc: JSONRPC;
|
||||
method: string;
|
||||
params?: JSONRPCParams;
|
||||
id?: JSONRPCID;
|
||||
}
|
||||
export type JSONRPCResponse = JSONRPCSuccessResponse | JSONRPCErrorResponse;
|
||||
export interface JSONRPCSuccessResponse {
|
||||
jsonrpc: JSONRPC;
|
||||
id: JSONRPCID;
|
||||
result: any;
|
||||
error?: undefined;
|
||||
}
|
||||
export interface JSONRPCErrorResponse {
|
||||
jsonrpc: JSONRPC;
|
||||
id: JSONRPCID;
|
||||
result?: undefined;
|
||||
error: JSONRPCError;
|
||||
}
|
||||
export declare const isJSONRPCRequest: (payload: any) => payload is JSONRPCRequest;
|
||||
export declare const isJSONRPCRequests: (payload: any) => payload is JSONRPCRequest[];
|
||||
export declare const isJSONRPCResponse: (payload: any) => payload is JSONRPCResponse;
|
||||
export declare const isJSONRPCResponses: (payload: any) => payload is JSONRPCResponse[];
|
||||
export interface JSONRPCError {
|
||||
code: number;
|
||||
message: string;
|
||||
data?: any;
|
||||
}
|
||||
export declare class JSONRPCErrorException extends Error implements JSONRPCError {
|
||||
code: number;
|
||||
data?: any;
|
||||
constructor(message: string, code: number, data?: any);
|
||||
toObject(): JSONRPCError;
|
||||
}
|
||||
export declare enum JSONRPCErrorCode {
|
||||
ParseError = -32700,
|
||||
InvalidRequest = -32600,
|
||||
MethodNotFound = -32601,
|
||||
InvalidParams = -32602,
|
||||
InternalError = -32603
|
||||
}
|
||||
export declare const createJSONRPCErrorResponse: (id: JSONRPCID, code: number, message: string, data?: any) => JSONRPCErrorResponse;
|
||||
export declare const createJSONRPCSuccessResponse: (id: JSONRPCID, result?: any) => JSONRPCSuccessResponse;
|
||||
export declare const createJSONRPCRequest: (id: JSONRPCID, method: string, params?: JSONRPCParams) => JSONRPCRequest;
|
||||
export declare const createJSONRPCNotification: (method: string, params?: JSONRPCParams) => JSONRPCRequest;
|
||||
export type ErrorListener = (message: string, data: unknown) => void;
|
||||
109
node_modules/json-rpc-2.0/dist/models.js
generated
vendored
Normal file
109
node_modules/json-rpc-2.0/dist/models.js
generated
vendored
Normal file
@@ -0,0 +1,109 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = function (d, b) {
|
||||
extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
if (typeof b !== "function" && b !== null)
|
||||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createJSONRPCNotification = exports.createJSONRPCRequest = exports.createJSONRPCSuccessResponse = exports.createJSONRPCErrorResponse = exports.JSONRPCErrorCode = exports.JSONRPCErrorException = exports.isJSONRPCResponses = exports.isJSONRPCResponse = exports.isJSONRPCRequests = exports.isJSONRPCRequest = exports.isJSONRPCID = exports.JSONRPC = void 0;
|
||||
exports.JSONRPC = "2.0";
|
||||
var isJSONRPCID = function (id) {
|
||||
return typeof id === "string" || typeof id === "number" || id === null;
|
||||
};
|
||||
exports.isJSONRPCID = isJSONRPCID;
|
||||
var isJSONRPCRequest = function (payload) {
|
||||
return (payload.jsonrpc === exports.JSONRPC &&
|
||||
payload.method !== undefined &&
|
||||
payload.result === undefined &&
|
||||
payload.error === undefined);
|
||||
};
|
||||
exports.isJSONRPCRequest = isJSONRPCRequest;
|
||||
var isJSONRPCRequests = function (payload) {
|
||||
return Array.isArray(payload) && payload.every(exports.isJSONRPCRequest);
|
||||
};
|
||||
exports.isJSONRPCRequests = isJSONRPCRequests;
|
||||
var isJSONRPCResponse = function (payload) {
|
||||
return (payload.jsonrpc === exports.JSONRPC &&
|
||||
payload.id !== undefined &&
|
||||
(payload.result !== undefined || payload.error !== undefined));
|
||||
};
|
||||
exports.isJSONRPCResponse = isJSONRPCResponse;
|
||||
var isJSONRPCResponses = function (payload) {
|
||||
return Array.isArray(payload) && payload.every(exports.isJSONRPCResponse);
|
||||
};
|
||||
exports.isJSONRPCResponses = isJSONRPCResponses;
|
||||
var createJSONRPCError = function (code, message, data) {
|
||||
var error = { code: code, message: message };
|
||||
if (data != null) {
|
||||
error.data = data;
|
||||
}
|
||||
return error;
|
||||
};
|
||||
var JSONRPCErrorException = /** @class */ (function (_super) {
|
||||
__extends(JSONRPCErrorException, _super);
|
||||
function JSONRPCErrorException(message, code, data) {
|
||||
var _this = _super.call(this, message) || this;
|
||||
// Manually set the prototype to fix TypeScript issue:
|
||||
// https://github.com/Microsoft/TypeScript-wiki/blob/main/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
|
||||
Object.setPrototypeOf(_this, JSONRPCErrorException.prototype);
|
||||
_this.code = code;
|
||||
_this.data = data;
|
||||
return _this;
|
||||
}
|
||||
JSONRPCErrorException.prototype.toObject = function () {
|
||||
return createJSONRPCError(this.code, this.message, this.data);
|
||||
};
|
||||
return JSONRPCErrorException;
|
||||
}(Error));
|
||||
exports.JSONRPCErrorException = JSONRPCErrorException;
|
||||
var JSONRPCErrorCode;
|
||||
(function (JSONRPCErrorCode) {
|
||||
JSONRPCErrorCode[JSONRPCErrorCode["ParseError"] = -32700] = "ParseError";
|
||||
JSONRPCErrorCode[JSONRPCErrorCode["InvalidRequest"] = -32600] = "InvalidRequest";
|
||||
JSONRPCErrorCode[JSONRPCErrorCode["MethodNotFound"] = -32601] = "MethodNotFound";
|
||||
JSONRPCErrorCode[JSONRPCErrorCode["InvalidParams"] = -32602] = "InvalidParams";
|
||||
JSONRPCErrorCode[JSONRPCErrorCode["InternalError"] = -32603] = "InternalError";
|
||||
})(JSONRPCErrorCode = exports.JSONRPCErrorCode || (exports.JSONRPCErrorCode = {}));
|
||||
var createJSONRPCErrorResponse = function (id, code, message, data) {
|
||||
return {
|
||||
jsonrpc: exports.JSONRPC,
|
||||
id: id,
|
||||
error: createJSONRPCError(code, message, data),
|
||||
};
|
||||
};
|
||||
exports.createJSONRPCErrorResponse = createJSONRPCErrorResponse;
|
||||
var createJSONRPCSuccessResponse = function (id, result) {
|
||||
return {
|
||||
jsonrpc: exports.JSONRPC,
|
||||
id: id,
|
||||
result: result !== null && result !== void 0 ? result : null,
|
||||
};
|
||||
};
|
||||
exports.createJSONRPCSuccessResponse = createJSONRPCSuccessResponse;
|
||||
var createJSONRPCRequest = function (id, method, params) {
|
||||
return {
|
||||
jsonrpc: exports.JSONRPC,
|
||||
id: id,
|
||||
method: method,
|
||||
params: params,
|
||||
};
|
||||
};
|
||||
exports.createJSONRPCRequest = createJSONRPCRequest;
|
||||
var createJSONRPCNotification = function (method, params) {
|
||||
return {
|
||||
jsonrpc: exports.JSONRPC,
|
||||
method: method,
|
||||
params: params,
|
||||
};
|
||||
};
|
||||
exports.createJSONRPCNotification = createJSONRPCNotification;
|
||||
24
node_modules/json-rpc-2.0/dist/server-and-client.d.ts
generated
vendored
Normal file
24
node_modules/json-rpc-2.0/dist/server-and-client.d.ts
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
import { JSONRPCMethod, JSONRPCServer, JSONRPCServerMiddleware, SimpleJSONRPCMethod } from "./server";
|
||||
import { JSONRPCClient, JSONRPCRequester } from "./client";
|
||||
import { ErrorListener, JSONRPCParams, JSONRPCRequest, JSONRPCResponse } from "./models";
|
||||
export interface JSONRPCServerAndClientOptions {
|
||||
errorListener?: ErrorListener;
|
||||
}
|
||||
export declare class JSONRPCServerAndClient<ServerParams = void, ClientParams = void> {
|
||||
server: JSONRPCServer<ServerParams>;
|
||||
client: JSONRPCClient<ClientParams>;
|
||||
private readonly errorListener;
|
||||
constructor(server: JSONRPCServer<ServerParams>, client: JSONRPCClient<ClientParams>, options?: JSONRPCServerAndClientOptions);
|
||||
applyServerMiddleware(...middlewares: JSONRPCServerMiddleware<ServerParams>[]): void;
|
||||
hasMethod(name: string): boolean;
|
||||
addMethod(name: string, method: SimpleJSONRPCMethod<ServerParams>): void;
|
||||
addMethodAdvanced(name: string, method: JSONRPCMethod<ServerParams>): void;
|
||||
removeMethod(name: string): void;
|
||||
timeout(delay: number): JSONRPCRequester<ClientParams>;
|
||||
request(method: string, params: JSONRPCParams, clientParams: ClientParams): PromiseLike<any>;
|
||||
requestAdvanced(jsonRPCRequest: JSONRPCRequest, clientParams: ClientParams): PromiseLike<JSONRPCResponse>;
|
||||
requestAdvanced(jsonRPCRequest: JSONRPCRequest[], clientParams: ClientParams): PromiseLike<JSONRPCResponse[]>;
|
||||
notify(method: string, params: JSONRPCParams, clientParams: ClientParams): void;
|
||||
rejectAllPendingRequests(message: string): void;
|
||||
receiveAndSend(payload: any, serverParams: ServerParams, clientParams: ClientParams): Promise<void>;
|
||||
}
|
||||
113
node_modules/json-rpc-2.0/dist/server-and-client.js
generated
vendored
Normal file
113
node_modules/json-rpc-2.0/dist/server-and-client.js
generated
vendored
Normal file
@@ -0,0 +1,113 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.JSONRPCServerAndClient = void 0;
|
||||
var models_1 = require("./models");
|
||||
var JSONRPCServerAndClient = /** @class */ (function () {
|
||||
function JSONRPCServerAndClient(server, client, options) {
|
||||
if (options === void 0) { options = {}; }
|
||||
var _a;
|
||||
this.server = server;
|
||||
this.client = client;
|
||||
this.errorListener = (_a = options.errorListener) !== null && _a !== void 0 ? _a : console.warn;
|
||||
}
|
||||
JSONRPCServerAndClient.prototype.applyServerMiddleware = function () {
|
||||
var _a;
|
||||
var middlewares = [];
|
||||
for (var _i = 0; _i < arguments.length; _i++) {
|
||||
middlewares[_i] = arguments[_i];
|
||||
}
|
||||
(_a = this.server).applyMiddleware.apply(_a, middlewares);
|
||||
};
|
||||
JSONRPCServerAndClient.prototype.hasMethod = function (name) {
|
||||
return this.server.hasMethod(name);
|
||||
};
|
||||
JSONRPCServerAndClient.prototype.addMethod = function (name, method) {
|
||||
this.server.addMethod(name, method);
|
||||
};
|
||||
JSONRPCServerAndClient.prototype.addMethodAdvanced = function (name, method) {
|
||||
this.server.addMethodAdvanced(name, method);
|
||||
};
|
||||
JSONRPCServerAndClient.prototype.removeMethod = function (name) {
|
||||
this.server.removeMethod(name);
|
||||
};
|
||||
JSONRPCServerAndClient.prototype.timeout = function (delay) {
|
||||
return this.client.timeout(delay);
|
||||
};
|
||||
JSONRPCServerAndClient.prototype.request = function (method, params, clientParams) {
|
||||
return this.client.request(method, params, clientParams);
|
||||
};
|
||||
JSONRPCServerAndClient.prototype.requestAdvanced = function (jsonRPCRequest, clientParams) {
|
||||
return this.client.requestAdvanced(jsonRPCRequest, clientParams);
|
||||
};
|
||||
JSONRPCServerAndClient.prototype.notify = function (method, params, clientParams) {
|
||||
this.client.notify(method, params, clientParams);
|
||||
};
|
||||
JSONRPCServerAndClient.prototype.rejectAllPendingRequests = function (message) {
|
||||
this.client.rejectAllPendingRequests(message);
|
||||
};
|
||||
JSONRPCServerAndClient.prototype.receiveAndSend = function (payload, serverParams, clientParams) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var response, message;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
if (!((0, models_1.isJSONRPCResponse)(payload) || (0, models_1.isJSONRPCResponses)(payload))) return [3 /*break*/, 1];
|
||||
this.client.receive(payload);
|
||||
return [3 /*break*/, 4];
|
||||
case 1:
|
||||
if (!((0, models_1.isJSONRPCRequest)(payload) || (0, models_1.isJSONRPCRequests)(payload))) return [3 /*break*/, 3];
|
||||
return [4 /*yield*/, this.server.receive(payload, serverParams)];
|
||||
case 2:
|
||||
response = _a.sent();
|
||||
if (response) {
|
||||
return [2 /*return*/, this.client.send(response, clientParams)];
|
||||
}
|
||||
return [3 /*break*/, 4];
|
||||
case 3:
|
||||
message = "Received an invalid JSON-RPC message";
|
||||
this.errorListener(message, payload);
|
||||
return [2 /*return*/, Promise.reject(new Error(message))];
|
||||
case 4: return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
return JSONRPCServerAndClient;
|
||||
}());
|
||||
exports.JSONRPCServerAndClient = JSONRPCServerAndClient;
|
||||
1
node_modules/json-rpc-2.0/dist/server-and-client.spec.d.ts
generated
vendored
Normal file
1
node_modules/json-rpc-2.0/dist/server-and-client.spec.d.ts
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export {};
|
||||
244
node_modules/json-rpc-2.0/dist/server-and-client.spec.js
generated
vendored
Normal file
244
node_modules/json-rpc-2.0/dist/server-and-client.spec.js
generated
vendored
Normal file
@@ -0,0 +1,244 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var mocha_1 = require("mocha");
|
||||
var chai_1 = require("chai");
|
||||
var sinon = require("sinon");
|
||||
var _1 = require(".");
|
||||
var client_1 = require("./client");
|
||||
(0, mocha_1.describe)("JSONRPCServerAndClient", function () {
|
||||
var serverAndClient1;
|
||||
var serverAndClient2;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
serverAndClient1 = new _1.JSONRPCServerAndClient(new _1.JSONRPCServer(), new client_1.JSONRPCClient(function (payload) {
|
||||
return serverAndClient2.receiveAndSend(payload, undefined);
|
||||
}));
|
||||
serverAndClient2 = new _1.JSONRPCServerAndClient(new _1.JSONRPCServer(), new client_1.JSONRPCClient(function (payload, params) {
|
||||
return serverAndClient1.receiveAndSend(payload, params);
|
||||
}));
|
||||
serverAndClient1.addMethod("echo1", function (_a) {
|
||||
var message = _a.message;
|
||||
return message;
|
||||
});
|
||||
serverAndClient1.addMethodAdvanced("echo1-2", function (jsonRPCRequest, params) { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
return [2 /*return*/, ({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: jsonRPCRequest.id,
|
||||
result: "".concat(params === null || params === void 0 ? void 0 : params.userID, " said ").concat(jsonRPCRequest.params.message),
|
||||
})];
|
||||
});
|
||||
}); });
|
||||
serverAndClient2.addMethod("echo2", function (_a) {
|
||||
var message = _a.message;
|
||||
return message;
|
||||
});
|
||||
});
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
(0, mocha_1.describe)("requesting from server 1", function () {
|
||||
var result;
|
||||
(0, mocha_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, serverAndClient1.request("echo2", { message: "foo" })];
|
||||
case 1:
|
||||
result = _a.sent();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
(0, mocha_1.it)("should request to server 2", function () {
|
||||
(0, chai_1.expect)(result).to.equal("foo");
|
||||
});
|
||||
(0, mocha_1.describe)("removing the method", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
serverAndClient2.removeMethod("echo2");
|
||||
});
|
||||
(0, mocha_1.describe)("requesting from server 1", function () {
|
||||
var response;
|
||||
(0, mocha_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, serverAndClient1.requestAdvanced({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
method: "echo2",
|
||||
params: { message: "foo" },
|
||||
})];
|
||||
case 1:
|
||||
response = _a.sent();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
(0, mocha_1.it)("should return not found", function () {
|
||||
var _a;
|
||||
(0, chai_1.expect)((_a = response.error) === null || _a === void 0 ? void 0 : _a.code).to.equal(_1.JSONRPCErrorCode.MethodNotFound);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("requesting from server 2", function () {
|
||||
var result;
|
||||
(0, mocha_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, serverAndClient2.request("echo1", { message: "bar" })];
|
||||
case 1:
|
||||
result = _a.sent();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
(0, mocha_1.it)("should request to server 1", function () {
|
||||
(0, chai_1.expect)(result).to.equal("bar");
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("requesting from server 1 using advanced method", function () {
|
||||
var response;
|
||||
(0, mocha_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
var request;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
request = {
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
method: "echo1-2",
|
||||
params: { message: "test" },
|
||||
};
|
||||
return [4 /*yield*/, serverAndClient2.requestAdvanced(request, {
|
||||
userID: "baz",
|
||||
})];
|
||||
case 1:
|
||||
response = _a.sent();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
(0, mocha_1.it)("should request to server 1", function () {
|
||||
(0, chai_1.expect)(response.result).to.equal("baz said test");
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("requesting in batch", function () {
|
||||
var responses;
|
||||
(0, mocha_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
var requests;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
requests = [
|
||||
{ jsonrpc: _1.JSONRPC, id: 0, method: "echo2", params: { message: "1" } },
|
||||
{ jsonrpc: _1.JSONRPC, id: 1, method: "echo2", params: { message: "2" } },
|
||||
];
|
||||
return [4 /*yield*/, serverAndClient1.requestAdvanced(requests)];
|
||||
case 1:
|
||||
responses = _a.sent();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
(0, mocha_1.it)("should return responses", function () {
|
||||
(0, chai_1.expect)(responses).to.deep.equal([
|
||||
{ jsonrpc: _1.JSONRPC, id: 0, result: "1" },
|
||||
{ jsonrpc: _1.JSONRPC, id: 1, result: "2" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("receiving invalid JSON-RPC message", function () {
|
||||
var promise;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
promise = serverAndClient1.receiveAndSend({});
|
||||
});
|
||||
(0, mocha_1.it)("should fail", function () {
|
||||
return promise.then(function () { return Promise.reject(new Error("Expected to fail")); }, function () { return undefined; });
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("having a pending request", function () {
|
||||
var promise;
|
||||
var resolve;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
serverAndClient2.addMethod("hang", function () {
|
||||
return new Promise(function (givenResolve) { return (resolve = givenResolve); });
|
||||
});
|
||||
promise = serverAndClient1.request("hang", undefined);
|
||||
});
|
||||
(0, mocha_1.describe)("rejecting all pending requests", function () {
|
||||
var message;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
message = "Connection is closed.";
|
||||
serverAndClient1.rejectAllPendingRequests(message);
|
||||
resolve();
|
||||
});
|
||||
(0, mocha_1.it)("should reject the pending request", function () {
|
||||
return promise.then(function () { return Promise.reject(new Error("Expected to fail")); }, function (error) { return (0, chai_1.expect)(error.message).to.equal(message); });
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("requesting with timeout", function () {
|
||||
var fakeTimers;
|
||||
var delay;
|
||||
var resolve;
|
||||
var promise;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
fakeTimers = sinon.useFakeTimers();
|
||||
delay = 100;
|
||||
serverAndClient2.addMethod("timeout", function () { return new Promise(function (givenResolve) { return (resolve = givenResolve); }); });
|
||||
promise = serverAndClient1.timeout(delay).request("timeout");
|
||||
});
|
||||
(0, mocha_1.describe)("timing out", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
fakeTimers.tick(delay);
|
||||
resolve();
|
||||
});
|
||||
(0, mocha_1.it)("should reject", function () {
|
||||
return promise.then(function () { return Promise.reject(new Error("Expected to fail")); }, function () { return undefined; });
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("not timing out", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
resolve();
|
||||
});
|
||||
(0, mocha_1.it)("should succeed", function () {
|
||||
return promise;
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
32
node_modules/json-rpc-2.0/dist/server.d.ts
generated
vendored
Normal file
32
node_modules/json-rpc-2.0/dist/server.d.ts
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
import { JSONRPCRequest, JSONRPCResponse, JSONRPCParams, JSONRPCID, JSONRPCErrorResponse, ErrorListener } from "./models";
|
||||
export type SimpleJSONRPCMethod<ServerParams = void> = (params: JSONRPCParams, serverParams: ServerParams) => any;
|
||||
export type JSONRPCMethod<ServerParams = void> = (request: JSONRPCRequest, serverParams: ServerParams) => JSONRPCResponsePromise;
|
||||
export type JSONRPCResponsePromise = PromiseLike<JSONRPCResponse | null>;
|
||||
export type JSONRPCServerMiddlewareNext<ServerParams> = (request: JSONRPCRequest, serverParams: ServerParams) => JSONRPCResponsePromise;
|
||||
export type JSONRPCServerMiddleware<ServerParams> = (next: JSONRPCServerMiddlewareNext<ServerParams>, request: JSONRPCRequest, serverParams: ServerParams) => JSONRPCResponsePromise;
|
||||
export interface JSONRPCServerOptions {
|
||||
errorListener?: ErrorListener;
|
||||
}
|
||||
export declare class JSONRPCServer<ServerParams = void> {
|
||||
private nameToMethodDictionary;
|
||||
private middleware;
|
||||
private readonly errorListener;
|
||||
mapErrorToJSONRPCErrorResponse: (id: JSONRPCID, error: any) => JSONRPCErrorResponse;
|
||||
constructor(options?: JSONRPCServerOptions);
|
||||
hasMethod(name: string): boolean;
|
||||
addMethod(name: string, method: SimpleJSONRPCMethod<ServerParams>): void;
|
||||
removeMethod(name: string): void;
|
||||
private toJSONRPCMethod;
|
||||
addMethodAdvanced(name: string, method: JSONRPCMethod<ServerParams>): void;
|
||||
receiveJSON(json: string, serverParams?: ServerParams): PromiseLike<JSONRPCResponse | JSONRPCResponse[] | null>;
|
||||
private tryParseRequestJSON;
|
||||
receive(request: JSONRPCRequest, serverParams?: ServerParams): PromiseLike<JSONRPCResponse | null>;
|
||||
receive(request: JSONRPCRequest | JSONRPCRequest[], serverParams?: ServerParams): PromiseLike<JSONRPCResponse | JSONRPCResponse[] | null>;
|
||||
private receiveMultiple;
|
||||
private receiveSingle;
|
||||
applyMiddleware(...middlewares: JSONRPCServerMiddleware<ServerParams>[]): void;
|
||||
private combineMiddlewares;
|
||||
private middlewareReducer;
|
||||
private callMethod;
|
||||
private mapErrorToJSONRPCErrorResponseIfNecessary;
|
||||
}
|
||||
259
node_modules/json-rpc-2.0/dist/server.js
generated
vendored
Normal file
259
node_modules/json-rpc-2.0/dist/server.js
generated
vendored
Normal file
@@ -0,0 +1,259 @@
|
||||
"use strict";
|
||||
var __assign = (this && this.__assign) || function () {
|
||||
__assign = Object.assign || function(t) {
|
||||
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
||||
s = arguments[i];
|
||||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
||||
t[p] = s[p];
|
||||
}
|
||||
return t;
|
||||
};
|
||||
return __assign.apply(this, arguments);
|
||||
};
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
||||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
||||
if (ar || !(i in from)) {
|
||||
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
||||
ar[i] = from[i];
|
||||
}
|
||||
}
|
||||
return to.concat(ar || Array.prototype.slice.call(from));
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.JSONRPCServer = void 0;
|
||||
var models_1 = require("./models");
|
||||
var internal_1 = require("./internal");
|
||||
var createParseErrorResponse = function () {
|
||||
return (0, models_1.createJSONRPCErrorResponse)(null, models_1.JSONRPCErrorCode.ParseError, "Parse error");
|
||||
};
|
||||
var createInvalidRequestResponse = function (request) {
|
||||
return (0, models_1.createJSONRPCErrorResponse)((0, models_1.isJSONRPCID)(request.id) ? request.id : null, models_1.JSONRPCErrorCode.InvalidRequest, "Invalid Request");
|
||||
};
|
||||
var createMethodNotFoundResponse = function (id) {
|
||||
return (0, models_1.createJSONRPCErrorResponse)(id, models_1.JSONRPCErrorCode.MethodNotFound, "Method not found");
|
||||
};
|
||||
var JSONRPCServer = /** @class */ (function () {
|
||||
function JSONRPCServer(options) {
|
||||
if (options === void 0) { options = {}; }
|
||||
var _a;
|
||||
this.mapErrorToJSONRPCErrorResponse = defaultMapErrorToJSONRPCErrorResponse;
|
||||
this.nameToMethodDictionary = {};
|
||||
this.middleware = null;
|
||||
this.errorListener = (_a = options.errorListener) !== null && _a !== void 0 ? _a : console.warn;
|
||||
}
|
||||
JSONRPCServer.prototype.hasMethod = function (name) {
|
||||
return !!this.nameToMethodDictionary[name];
|
||||
};
|
||||
JSONRPCServer.prototype.addMethod = function (name, method) {
|
||||
this.addMethodAdvanced(name, this.toJSONRPCMethod(method));
|
||||
};
|
||||
JSONRPCServer.prototype.removeMethod = function (name) {
|
||||
delete this.nameToMethodDictionary[name];
|
||||
};
|
||||
JSONRPCServer.prototype.toJSONRPCMethod = function (method) {
|
||||
return function (request, serverParams) {
|
||||
var response = method(request.params, serverParams);
|
||||
return Promise.resolve(response).then(function (result) {
|
||||
return mapResultToJSONRPCResponse(request.id, result);
|
||||
});
|
||||
};
|
||||
};
|
||||
JSONRPCServer.prototype.addMethodAdvanced = function (name, method) {
|
||||
var _a;
|
||||
this.nameToMethodDictionary = __assign(__assign({}, this.nameToMethodDictionary), (_a = {}, _a[name] = method, _a));
|
||||
};
|
||||
JSONRPCServer.prototype.receiveJSON = function (json, serverParams) {
|
||||
var request = this.tryParseRequestJSON(json);
|
||||
if (request) {
|
||||
return this.receive(request, serverParams);
|
||||
}
|
||||
else {
|
||||
return Promise.resolve(createParseErrorResponse());
|
||||
}
|
||||
};
|
||||
JSONRPCServer.prototype.tryParseRequestJSON = function (json) {
|
||||
try {
|
||||
return JSON.parse(json);
|
||||
}
|
||||
catch (_a) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
JSONRPCServer.prototype.receive = function (request, serverParams) {
|
||||
if (Array.isArray(request)) {
|
||||
return this.receiveMultiple(request, serverParams);
|
||||
}
|
||||
else {
|
||||
return this.receiveSingle(request, serverParams);
|
||||
}
|
||||
};
|
||||
JSONRPCServer.prototype.receiveMultiple = function (requests, serverParams) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var responses;
|
||||
var _this = this;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, Promise.all(requests.map(function (request) { return _this.receiveSingle(request, serverParams); }))];
|
||||
case 1:
|
||||
responses = (_a.sent()).filter(isNonNull);
|
||||
if (responses.length === 1) {
|
||||
return [2 /*return*/, responses[0]];
|
||||
}
|
||||
else if (responses.length) {
|
||||
return [2 /*return*/, responses];
|
||||
}
|
||||
else {
|
||||
return [2 /*return*/, null];
|
||||
}
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
JSONRPCServer.prototype.receiveSingle = function (request, serverParams) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var method, response;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
method = this.nameToMethodDictionary[request.method];
|
||||
if (!!(0, models_1.isJSONRPCRequest)(request)) return [3 /*break*/, 1];
|
||||
return [2 /*return*/, createInvalidRequestResponse(request)];
|
||||
case 1: return [4 /*yield*/, this.callMethod(method, request, serverParams)];
|
||||
case 2:
|
||||
response = _a.sent();
|
||||
return [2 /*return*/, mapResponse(request, response)];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
JSONRPCServer.prototype.applyMiddleware = function () {
|
||||
var middlewares = [];
|
||||
for (var _i = 0; _i < arguments.length; _i++) {
|
||||
middlewares[_i] = arguments[_i];
|
||||
}
|
||||
if (this.middleware) {
|
||||
this.middleware = this.combineMiddlewares(__spreadArray([
|
||||
this.middleware
|
||||
], middlewares, true));
|
||||
}
|
||||
else {
|
||||
this.middleware = this.combineMiddlewares(middlewares);
|
||||
}
|
||||
};
|
||||
JSONRPCServer.prototype.combineMiddlewares = function (middlewares) {
|
||||
if (!middlewares.length) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return middlewares.reduce(this.middlewareReducer);
|
||||
}
|
||||
};
|
||||
JSONRPCServer.prototype.middlewareReducer = function (prevMiddleware, nextMiddleware) {
|
||||
return function (next, request, serverParams) {
|
||||
return prevMiddleware(function (request, serverParams) { return nextMiddleware(next, request, serverParams); }, request, serverParams);
|
||||
};
|
||||
};
|
||||
JSONRPCServer.prototype.callMethod = function (method, request, serverParams) {
|
||||
var _this = this;
|
||||
var callMethod = function (request, serverParams) {
|
||||
if (method) {
|
||||
return method(request, serverParams);
|
||||
}
|
||||
else if (request.id !== undefined) {
|
||||
return Promise.resolve(createMethodNotFoundResponse(request.id));
|
||||
}
|
||||
else {
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
};
|
||||
var onError = function (error) {
|
||||
_this.errorListener("An unexpected error occurred while executing \"".concat(request.method, "\" JSON-RPC method:"), error);
|
||||
return Promise.resolve(_this.mapErrorToJSONRPCErrorResponseIfNecessary(request.id, error));
|
||||
};
|
||||
try {
|
||||
return (this.middleware || noopMiddleware)(callMethod, request, serverParams).then(undefined, onError);
|
||||
}
|
||||
catch (error) {
|
||||
return onError(error);
|
||||
}
|
||||
};
|
||||
JSONRPCServer.prototype.mapErrorToJSONRPCErrorResponseIfNecessary = function (id, error) {
|
||||
if (id !== undefined) {
|
||||
return this.mapErrorToJSONRPCErrorResponse(id, error);
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
return JSONRPCServer;
|
||||
}());
|
||||
exports.JSONRPCServer = JSONRPCServer;
|
||||
var isNonNull = function (value) { return value !== null; };
|
||||
var noopMiddleware = function (next, request, serverParams) { return next(request, serverParams); };
|
||||
var mapResultToJSONRPCResponse = function (id, result) {
|
||||
if (id !== undefined) {
|
||||
return (0, models_1.createJSONRPCSuccessResponse)(id, result);
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
var defaultMapErrorToJSONRPCErrorResponse = function (id, error) {
|
||||
var _a;
|
||||
var message = (_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : "An unexpected error occurred";
|
||||
var code = internal_1.DefaultErrorCode;
|
||||
var data;
|
||||
if (error instanceof models_1.JSONRPCErrorException) {
|
||||
code = error.code;
|
||||
data = error.data;
|
||||
}
|
||||
return (0, models_1.createJSONRPCErrorResponse)(id, code, message, data);
|
||||
};
|
||||
var mapResponse = function (request, response) {
|
||||
if (response) {
|
||||
return response;
|
||||
}
|
||||
else if (request.id !== undefined) {
|
||||
return (0, models_1.createJSONRPCErrorResponse)(request.id, models_1.JSONRPCErrorCode.InternalError, "Internal error");
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
1
node_modules/json-rpc-2.0/dist/server.spec.d.ts
generated
vendored
Normal file
1
node_modules/json-rpc-2.0/dist/server.spec.d.ts
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export {};
|
||||
746
node_modules/json-rpc-2.0/dist/server.spec.js
generated
vendored
Normal file
746
node_modules/json-rpc-2.0/dist/server.spec.js
generated
vendored
Normal file
@@ -0,0 +1,746 @@
|
||||
"use strict";
|
||||
var __assign = (this && this.__assign) || function () {
|
||||
__assign = Object.assign || function(t) {
|
||||
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
||||
s = arguments[i];
|
||||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
||||
t[p] = s[p];
|
||||
}
|
||||
return t;
|
||||
};
|
||||
return __assign.apply(this, arguments);
|
||||
};
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var mocha_1 = require("mocha");
|
||||
var chai_1 = require("chai");
|
||||
var _1 = require(".");
|
||||
(0, mocha_1.describe)("JSONRPCServer", function () {
|
||||
var server;
|
||||
var response;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
response = null;
|
||||
server = new _1.JSONRPCServer();
|
||||
});
|
||||
var waitUntil = function (predicate) {
|
||||
return Promise.resolve().then(function () {
|
||||
if (!predicate()) {
|
||||
return waitUntil(predicate);
|
||||
}
|
||||
});
|
||||
};
|
||||
(0, mocha_1.describe)("having an echo method", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
var echoMethod = function (_a, serverParams) {
|
||||
var text = _a.text;
|
||||
if (serverParams) {
|
||||
return "".concat(serverParams.userID, " said ").concat(text);
|
||||
}
|
||||
else {
|
||||
return text;
|
||||
}
|
||||
};
|
||||
server.addMethod("echo", echoMethod);
|
||||
});
|
||||
(0, mocha_1.describe)("receiving a request to the method", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
return server
|
||||
.receive({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
method: "echo",
|
||||
params: { text: "foo" },
|
||||
})
|
||||
.then(function (givenResponse) { return (response = givenResponse); });
|
||||
});
|
||||
(0, mocha_1.it)("should echo the text", function () {
|
||||
(0, chai_1.expect)(response).to.deep.equal({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
result: "foo",
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("receiving a request to the method with user ID", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
return server
|
||||
.receiveJSON(JSON.stringify({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
method: "echo",
|
||||
params: { text: "foo" },
|
||||
}), { userID: "bar" })
|
||||
.then(function (givenResponse) { return (response = givenResponse); });
|
||||
});
|
||||
(0, mocha_1.it)("should echo the text with the user ID", function () {
|
||||
(0, chai_1.expect)(response).to.deep.equal({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
result: "bar said foo",
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("removing the echo method", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
server.removeMethod("echo");
|
||||
});
|
||||
(0, mocha_1.describe)("receiving a request to the method", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
return server
|
||||
.receive({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
method: "echo",
|
||||
params: { text: "foo" },
|
||||
})
|
||||
.then(function (givenResponse) { return (response = givenResponse); });
|
||||
});
|
||||
(0, mocha_1.it)("should respond not found", function () {
|
||||
var _a;
|
||||
(0, chai_1.expect)((_a = response === null || response === void 0 ? void 0 : response.error) === null || _a === void 0 ? void 0 : _a.code).to.equal(_1.JSONRPCErrorCode.MethodNotFound);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("responding undefined", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
server.addMethod("ack", function () { return undefined; });
|
||||
return server
|
||||
.receive({ jsonrpc: _1.JSONRPC, id: 0, method: "ack" })
|
||||
.then(function (givenResponse) { return (response = givenResponse); });
|
||||
});
|
||||
(0, mocha_1.it)("should response with null result", function () {
|
||||
(0, chai_1.expect)(response).to.deep.equal({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
result: null,
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("throwing", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
server.addMethod("throw", function () {
|
||||
throw new Error("Test throwing");
|
||||
});
|
||||
return server
|
||||
.receive({ jsonrpc: _1.JSONRPC, id: 0, method: "throw" })
|
||||
.then(function (givenResponse) { return (response = givenResponse); });
|
||||
});
|
||||
(0, mocha_1.it)("should respond error", function () {
|
||||
(0, chai_1.expect)(response).to.deep.equal({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
error: {
|
||||
code: 0,
|
||||
message: "Test throwing",
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("throwing JSONRPCErrorException", function () {
|
||||
var expected;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
expected = {
|
||||
message: "thrown",
|
||||
code: 1234,
|
||||
data: {
|
||||
foo: "bar",
|
||||
},
|
||||
};
|
||||
server.addMethod("throw", function () {
|
||||
throw new _1.JSONRPCErrorException(expected.message, expected.code, expected.data);
|
||||
});
|
||||
return server
|
||||
.receive({ jsonrpc: _1.JSONRPC, id: 0, method: "throw" })
|
||||
.then(function (givenResponse) { return (response = givenResponse); });
|
||||
});
|
||||
(0, mocha_1.it)("should respond error with custom code and data", function () {
|
||||
(0, chai_1.expect)(response.error).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("rejecting", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
server.addMethodAdvanced("reject", function () {
|
||||
return Promise.reject(new Error("Test rejecting"));
|
||||
});
|
||||
return server
|
||||
.receive({ jsonrpc: _1.JSONRPC, id: 0, method: "reject" })
|
||||
.then(function (givenResponse) { return (response = givenResponse); });
|
||||
});
|
||||
(0, mocha_1.it)("should respond error", function () {
|
||||
(0, chai_1.expect)(response).to.deep.equal({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
error: {
|
||||
code: 0,
|
||||
message: "Test rejecting",
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("responding to a notification", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
server.addMethod("foo", function () { return "foo"; });
|
||||
return server
|
||||
.receive({ jsonrpc: _1.JSONRPC, method: "foo" })
|
||||
.then(function (givenResponse) { return (response = givenResponse); });
|
||||
});
|
||||
(0, mocha_1.it)("should not respond", function () {
|
||||
(0, chai_1.expect)(response).to.be.null;
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("error on a notification", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
server.addMethod("foo", function () { return Promise.reject(new Error("foo")); });
|
||||
return server
|
||||
.receive({ jsonrpc: _1.JSONRPC, method: "foo" })
|
||||
.then(function (givenResponse) { return (response = givenResponse); });
|
||||
});
|
||||
(0, mocha_1.it)("should not respond", function () {
|
||||
(0, chai_1.expect)(response).to.be.null;
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("responding null to a request", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
server.addMethodAdvanced("foo", function () { return Promise.resolve(null); });
|
||||
return server
|
||||
.receive({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
method: "foo",
|
||||
})
|
||||
.then(function (givenResponse) { return (response = givenResponse); });
|
||||
});
|
||||
(0, mocha_1.it)("should respond error", function () {
|
||||
(0, chai_1.expect)(response).to.deep.equal({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
error: {
|
||||
code: _1.JSONRPCErrorCode.InternalError,
|
||||
message: "Internal error",
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("receiving a request to an unknown method", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
return server
|
||||
.receive({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
method: "foo",
|
||||
})
|
||||
.then(function (givenResponse) { return (response = givenResponse); });
|
||||
});
|
||||
(0, mocha_1.it)("should respond error", function () {
|
||||
(0, chai_1.expect)(response).to.deep.equal({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
error: {
|
||||
code: _1.JSONRPCErrorCode.MethodNotFound,
|
||||
message: "Method not found",
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
[{}, "", "invalid JSON"].forEach(function (invalidJSON) {
|
||||
(0, mocha_1.describe)("receiving an invalid JSON (".concat(invalidJSON, ")"), function () {
|
||||
var response;
|
||||
(0, mocha_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, server.receiveJSON(invalidJSON)];
|
||||
case 1:
|
||||
response = (_a.sent());
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
(0, mocha_1.it)("should respond an error", function () {
|
||||
(0, chai_1.expect)(response.error.code).to.equal(_1.JSONRPCErrorCode.ParseError);
|
||||
});
|
||||
});
|
||||
});
|
||||
[
|
||||
{},
|
||||
{ jsonrpc: _1.JSONRPC },
|
||||
{ jsonrpc: _1.JSONRPC + "invalid", method: "" },
|
||||
].forEach(function (invalidRequest) {
|
||||
(0, mocha_1.describe)("receiving an invalid request (".concat(JSON.stringify(invalidRequest), ")"), function () {
|
||||
var response;
|
||||
(0, mocha_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, server.receive(invalidRequest)];
|
||||
case 1:
|
||||
response = (_a.sent());
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
(0, mocha_1.it)("should respond an error", function () {
|
||||
(0, chai_1.expect)(response.error.code).to.equal(_1.JSONRPCErrorCode.InvalidRequest);
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("having a custom mapErrorToJSONRPCErrorResponse method", function () {
|
||||
var errorMessagePrefix;
|
||||
var errorData;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
errorMessagePrefix = "Error: ";
|
||||
errorData = {
|
||||
foo: "bar",
|
||||
};
|
||||
server.mapErrorToJSONRPCErrorResponse = function (id, error) {
|
||||
return (0, _1.createJSONRPCErrorResponse)(id, error.code, "".concat(errorMessagePrefix).concat(error.message), errorData);
|
||||
};
|
||||
});
|
||||
(0, mocha_1.describe)("rejecting", function () {
|
||||
var errorCode;
|
||||
var errorMessage;
|
||||
var response;
|
||||
(0, mocha_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
errorCode = 123;
|
||||
errorMessage = "test message";
|
||||
server.addMethod("throw", function () {
|
||||
var error = new Error(errorMessage);
|
||||
error.code = errorCode;
|
||||
throw error;
|
||||
});
|
||||
return [4 /*yield*/, server.receive({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
method: "throw",
|
||||
})];
|
||||
case 1:
|
||||
response = (_a.sent());
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
(0, mocha_1.it)("should respond a custom error code", function () {
|
||||
(0, chai_1.expect)(response.error.code).to.equal(errorCode);
|
||||
});
|
||||
(0, mocha_1.it)("should respond a custom error message", function () {
|
||||
(0, chai_1.expect)(response.error.message).to.equal("".concat(errorMessagePrefix).concat(errorMessage));
|
||||
});
|
||||
(0, mocha_1.it)("should respond a custom error data", function () {
|
||||
(0, chai_1.expect)(response.error.data).to.deep.equal(errorData);
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("having an async method", function () {
|
||||
var methodName;
|
||||
var receivedRequest;
|
||||
var receivedServerParams;
|
||||
var returnedResponse;
|
||||
var returnFromMethod;
|
||||
var throwFromMethod;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
methodName = "foo";
|
||||
server.addMethodAdvanced(methodName, function (request, serverParams) {
|
||||
receivedRequest = request;
|
||||
receivedServerParams = serverParams;
|
||||
return new Promise(function (resolve, reject) {
|
||||
returnedResponse = {
|
||||
id: request.id,
|
||||
jsonrpc: _1.JSONRPC,
|
||||
result: {
|
||||
foo: "bar",
|
||||
},
|
||||
};
|
||||
returnFromMethod = function () {
|
||||
resolve(returnedResponse);
|
||||
};
|
||||
throwFromMethod = function (error) {
|
||||
reject(error);
|
||||
};
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("using middleware", function () {
|
||||
var middlewareCalled;
|
||||
var nextReturned;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
middlewareCalled = false;
|
||||
nextReturned = false;
|
||||
server.applyMiddleware(function (next, request, serverParams) {
|
||||
middlewareCalled = true;
|
||||
return next(request, serverParams).then(function (result) {
|
||||
nextReturned = true;
|
||||
return result;
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("requesting", function () {
|
||||
var givenRequest;
|
||||
var givenServerParams;
|
||||
var actualResponse;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
givenRequest = {
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
method: methodName,
|
||||
params: { foo: "bar" },
|
||||
};
|
||||
givenServerParams = { userID: "baz" };
|
||||
server
|
||||
.receive(givenRequest, givenServerParams)
|
||||
.then(function (response) { return (actualResponse = response); });
|
||||
return consumeAllEvents();
|
||||
});
|
||||
(0, mocha_1.it)("should call the middleware", function () {
|
||||
(0, chai_1.expect)(middlewareCalled).to.be.true;
|
||||
});
|
||||
(0, mocha_1.it)("should receive a request", function () {
|
||||
(0, chai_1.expect)(receivedRequest).to.deep.equal(givenRequest);
|
||||
});
|
||||
(0, mocha_1.it)("should received server params", function () {
|
||||
(0, chai_1.expect)(receivedServerParams).to.deep.equal(givenServerParams);
|
||||
});
|
||||
(0, mocha_1.it)("should not return from the next middleware yet", function () {
|
||||
(0, chai_1.expect)(nextReturned).to.be.false;
|
||||
});
|
||||
(0, mocha_1.describe)("finishing the request", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
returnFromMethod();
|
||||
return consumeAllEvents();
|
||||
});
|
||||
(0, mocha_1.it)("should return from the next middleware", function () {
|
||||
(0, chai_1.expect)(nextReturned).to.be.true;
|
||||
});
|
||||
(0, mocha_1.it)("should return a response", function () {
|
||||
(0, chai_1.expect)(actualResponse).to.deep.equal(returnedResponse);
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("using another middleware", function () {
|
||||
var secondMiddlewareCalled;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
secondMiddlewareCalled = false;
|
||||
server.applyMiddleware(function (next, request, serverParams) {
|
||||
secondMiddlewareCalled = true;
|
||||
return next(request, serverParams);
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("requesting", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
server.receive({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
method: methodName,
|
||||
params: {},
|
||||
});
|
||||
});
|
||||
(0, mocha_1.it)("should call the first middleware", function () {
|
||||
(0, chai_1.expect)(middlewareCalled).to.be.true;
|
||||
});
|
||||
(0, mocha_1.it)("should call the second middleware", function () {
|
||||
(0, chai_1.expect)(secondMiddlewareCalled).to.be.true;
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("using a middleware that changes request and server params", function () {
|
||||
var changedParams;
|
||||
var changedServerParams;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
changedParams = {
|
||||
foo: "bar",
|
||||
};
|
||||
changedServerParams = {
|
||||
userID: "changed user ID",
|
||||
};
|
||||
server.applyMiddleware(function (next, request) {
|
||||
return next(__assign(__assign({}, request), { params: changedParams }), changedServerParams);
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("requesting", function () {
|
||||
var givenRequest;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
givenRequest = {
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
method: methodName,
|
||||
params: {
|
||||
foo: "foo",
|
||||
},
|
||||
};
|
||||
server.receive(givenRequest);
|
||||
returnFromMethod();
|
||||
return consumeAllEvents();
|
||||
});
|
||||
(0, mocha_1.it)("should change the request", function () {
|
||||
var expectedRequest = __assign(__assign({}, givenRequest), { params: changedParams });
|
||||
(0, chai_1.expect)(receivedRequest).to.deep.equal(expectedRequest);
|
||||
});
|
||||
(0, mocha_1.it)("should change the server params", function () {
|
||||
(0, chai_1.expect)(receivedServerParams).to.deep.equal(changedServerParams);
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("using a middleware that changes response", function () {
|
||||
var changedResponse;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
server.applyMiddleware(function (next, request, serverParams) {
|
||||
return next(request, serverParams).then(function (response) {
|
||||
changedResponse = {
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: response.id,
|
||||
result: {
|
||||
foo: new Date().toString(),
|
||||
},
|
||||
};
|
||||
return changedResponse;
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("requesting", function () {
|
||||
var actualResponse;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
server
|
||||
.receive({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
method: methodName,
|
||||
params: {},
|
||||
})
|
||||
.then(function (response) { return (actualResponse = response); });
|
||||
returnFromMethod();
|
||||
return consumeAllEvents();
|
||||
});
|
||||
(0, mocha_1.it)("should return the changed response", function () {
|
||||
(0, chai_1.expect)(actualResponse).to.deep.equal(changedResponse);
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("using middleware that catches exception", function () {
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
server.applyMiddleware(function (next, request, serverParams) { return __awaiter(void 0, void 0, void 0, function () {
|
||||
var error_1;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
_a.trys.push([0, 2, , 3]);
|
||||
return [4 /*yield*/, next(request, serverParams)];
|
||||
case 1: return [2 /*return*/, _a.sent()];
|
||||
case 2:
|
||||
error_1 = _a.sent();
|
||||
return [2 /*return*/, (0, _1.createJSONRPCErrorResponse)(request.id, error_1.code || _1.JSONRPCErrorCode.InternalError, error_1.message)];
|
||||
case 3: return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
});
|
||||
(0, mocha_1.describe)("throwing", function () {
|
||||
var error;
|
||||
var actualResponse;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
server
|
||||
.receive({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
method: methodName,
|
||||
params: {},
|
||||
})
|
||||
.then(function (response) { return (actualResponse = response); });
|
||||
error = { code: 123, message: "test" };
|
||||
throwFromMethod(error);
|
||||
return consumeAllEvents();
|
||||
});
|
||||
(0, mocha_1.it)("should catch the exception on middleware", function () {
|
||||
var expected = (0, _1.createJSONRPCErrorResponse)(0, error.code, error.message);
|
||||
(0, chai_1.expect)(actualResponse).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("throwing from non-advanced method", function () {
|
||||
var message;
|
||||
var code;
|
||||
var actualResponse;
|
||||
(0, mocha_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
message = "thrown from non-advanced method";
|
||||
code = 456;
|
||||
server.addMethod("throw", function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
throw { message: message, code: code };
|
||||
});
|
||||
}); });
|
||||
return [4 /*yield*/, server.receive({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
method: "throw",
|
||||
})];
|
||||
case 1:
|
||||
actualResponse = (_a.sent());
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
(0, mocha_1.it)("should catch the exception on middleware", function () {
|
||||
var expected = (0, _1.createJSONRPCErrorResponse)(0, code, message);
|
||||
(0, chai_1.expect)(actualResponse).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("using multiple middleware", function () {
|
||||
var count;
|
||||
var first;
|
||||
var second;
|
||||
var third;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
count = 0;
|
||||
server.applyMiddleware(function (next, request, serverParams) {
|
||||
first = ++count;
|
||||
return next(request, serverParams);
|
||||
}, function (next, request, serverParams) {
|
||||
second = ++count;
|
||||
return next(request, serverParams);
|
||||
}, function (next, request, serverParams) {
|
||||
third = ++count;
|
||||
return next(request, serverParams);
|
||||
});
|
||||
server.receive({
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: 0,
|
||||
method: methodName,
|
||||
});
|
||||
return consumeAllEvents();
|
||||
});
|
||||
(0, mocha_1.it)("should call middleware in the applied order", function () {
|
||||
(0, chai_1.expect)([first, second, third]).to.deep.equal([1, 2, 3]);
|
||||
});
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("receiving batch requests", function () {
|
||||
var responses;
|
||||
(0, mocha_1.beforeEach)(function () {
|
||||
server.addMethod("echo", function (_a) {
|
||||
var message = _a.message;
|
||||
return message;
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("of 3 requests", function () {
|
||||
(0, mocha_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, server.receive([
|
||||
{ jsonrpc: _1.JSONRPC, id: 0, method: "echo", params: { message: "1" } },
|
||||
{ jsonrpc: _1.JSONRPC, id: 1, method: "echo", params: { message: "2" } },
|
||||
{ jsonrpc: _1.JSONRPC, id: 2, method: "echo", params: { message: "3" } },
|
||||
])];
|
||||
case 1:
|
||||
responses = _a.sent();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
(0, mocha_1.it)("should return 3 responses", function () {
|
||||
(0, chai_1.expect)(responses.map(function (response) { return response.result; })).to.deep.equal(["1", "2", "3"]);
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("of 1 request", function () {
|
||||
(0, mocha_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, server.receive([
|
||||
{ jsonrpc: _1.JSONRPC, id: 0, method: "echo", params: { message: "1" } },
|
||||
])];
|
||||
case 1:
|
||||
responses = _a.sent();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
(0, mocha_1.it)("should return 1 response", function () {
|
||||
(0, chai_1.expect)(responses.result).to.equal("1");
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("of notifications", function () {
|
||||
(0, mocha_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, server.receive([
|
||||
{ jsonrpc: _1.JSONRPC, method: "echo", params: { message: "1" } },
|
||||
])];
|
||||
case 1:
|
||||
responses = _a.sent();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
(0, mocha_1.it)("should return null", function () {
|
||||
(0, chai_1.expect)(responses).to.be.null;
|
||||
});
|
||||
});
|
||||
(0, mocha_1.describe)("of a valid and an invalid request", function () {
|
||||
(0, mocha_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, server.receive([
|
||||
1,
|
||||
{ jsonrpc: _1.JSONRPC, id: 0, method: "echo", params: { message: "1" } },
|
||||
])];
|
||||
case 1:
|
||||
responses = _a.sent();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
(0, mocha_1.it)("should return a failure and a success response", function () {
|
||||
(0, chai_1.expect)(responses).to.deep.equal([
|
||||
{
|
||||
jsonrpc: _1.JSONRPC,
|
||||
id: null,
|
||||
error: {
|
||||
code: _1.JSONRPCErrorCode.InvalidRequest,
|
||||
message: "Invalid Request",
|
||||
},
|
||||
},
|
||||
{ jsonrpc: _1.JSONRPC, id: 0, result: "1" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
var consumeAllEvents = function () { return new Promise(function (resolve) { return setTimeout(resolve, 0); }); };
|
||||
Reference in New Issue
Block a user