@adt/json-rpc
Advanced tools
Comparing version 0.0.1-SNAPSHOT.17 to 0.0.1-SNAPSHOT.18
@@ -30,19 +30,141 @@ (function (global, factory) { | ||
*/ | ||
JsonRpcElement.parse = function(str) { | ||
JsonRpcElement.parse = function (str) { | ||
// TODO: | ||
}; | ||
JsonRpcElement.prototype = Object.freeze( Object.create(null, /** @lends JsonRpcElement.prototype */ { | ||
jsonrpc : { value : "2.0" }, | ||
serialize : { value : function(){ | ||
return JSON.stringify(this); | ||
}} | ||
JsonRpcElement.prototype = Object.freeze(Object.create(null, /** @lends JsonRpcElement.prototype */{ | ||
jsonrpc: { value: "2.0" }, | ||
serialize: { value: function value() { | ||
return JSON.stringify(this); | ||
} } | ||
})); | ||
const validateId = o => { | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
if ( ( "id" in o ) === false ) { | ||
var asyncGenerator = function () { | ||
function AwaitValue(value) { | ||
this.value = value; | ||
} | ||
function AsyncGenerator(gen) { | ||
var front, back; | ||
function send(key, arg) { | ||
return new Promise(function (resolve, reject) { | ||
var request = { | ||
key: key, | ||
arg: arg, | ||
resolve: resolve, | ||
reject: reject, | ||
next: null | ||
}; | ||
if (back) { | ||
back = back.next = request; | ||
} else { | ||
front = back = request; | ||
resume(key, arg); | ||
} | ||
}); | ||
} | ||
function resume(key, arg) { | ||
try { | ||
var result = gen[key](arg); | ||
var value = result.value; | ||
if (value instanceof AwaitValue) { | ||
Promise.resolve(value.value).then(function (arg) { | ||
resume("next", arg); | ||
}, function (arg) { | ||
resume("throw", arg); | ||
}); | ||
} else { | ||
settle(result.done ? "return" : "normal", result.value); | ||
} | ||
} catch (err) { | ||
settle("throw", err); | ||
} | ||
} | ||
function settle(type, value) { | ||
switch (type) { | ||
case "return": | ||
front.resolve({ | ||
value: value, | ||
done: true | ||
}); | ||
break; | ||
case "throw": | ||
front.reject(value); | ||
break; | ||
default: | ||
front.resolve({ | ||
value: value, | ||
done: false | ||
}); | ||
break; | ||
} | ||
front = front.next; | ||
if (front) { | ||
resume(front.key, front.arg); | ||
} else { | ||
back = null; | ||
} | ||
} | ||
this._invoke = send; | ||
if (typeof gen.return !== "function") { | ||
this.return = undefined; | ||
} | ||
} | ||
if (typeof Symbol === "function" && Symbol.asyncIterator) { | ||
AsyncGenerator.prototype[Symbol.asyncIterator] = function () { | ||
return this; | ||
}; | ||
} | ||
AsyncGenerator.prototype.next = function (arg) { | ||
return this._invoke("next", arg); | ||
}; | ||
AsyncGenerator.prototype.throw = function (arg) { | ||
return this._invoke("throw", arg); | ||
}; | ||
AsyncGenerator.prototype.return = function (arg) { | ||
return this._invoke("return", arg); | ||
}; | ||
return { | ||
wrap: function (fn) { | ||
return function () { | ||
return new AsyncGenerator(fn.apply(this, arguments)); | ||
}; | ||
}, | ||
await: function (value) { | ||
return new AwaitValue(value); | ||
} | ||
}; | ||
}(); | ||
var validateId = function validateId(o) { | ||
if ("id" in o === false) { | ||
throw new Error("Required 'id' param is not present"); | ||
@@ -52,30 +174,28 @@ } | ||
const validateMethod = method => { | ||
var validateMethod = function validateMethod(method) { | ||
const methodType = typeof method; | ||
var methodType = typeof method === "undefined" ? "undefined" : _typeof(method); | ||
if ( methodType !== "string" ) { | ||
if (methodType !== "string") { | ||
throw new Error("Method must be a string, but '" + methodType + "' given"); | ||
} | ||
}; | ||
const validateParams = params => { | ||
var validateParams = function validateParams(params) { | ||
const paramsType = typeof params; | ||
const paramsIsArray = Array.isArray(params); | ||
var paramsType = typeof params === "undefined" ? "undefined" : _typeof(params); | ||
var paramsIsArray = Array.isArray(params); | ||
if ( paramsType === "undefined" ) { | ||
if (paramsType === "undefined") { | ||
return; | ||
} | ||
if ( paramsIsArray === false && ( paramsType !== "object" && paramsType !== "undefined" ) ) { | ||
if (paramsIsArray === false && paramsType !== "object" && paramsType !== "undefined") { | ||
throw new Error("Params must be an array, object or undefined, but '" + paramsType + "' given"); | ||
} | ||
}; | ||
const validateArguments = args => { | ||
var validateArguments = function validateArguments(args) { | ||
if ( typeof args === "undefined") { | ||
if (typeof args === "undefined") { | ||
throw new Error("Required parameters object not present"); | ||
@@ -95,3 +215,3 @@ } | ||
if ( ( this instanceof JsonRpcError ) === false ) { | ||
if (this instanceof JsonRpcError === false) { | ||
return new JsonRpcError(o); | ||
@@ -102,3 +222,3 @@ } | ||
if ( typeof o.code !== "number" ) { | ||
if (typeof o.code !== "number") { | ||
throw "Error code must be integer"; | ||
@@ -112,9 +232,8 @@ } | ||
return Object.freeze(this); | ||
} | ||
JsonRpcError.prototype = Object.create(null,/** @lends JsonRpcError.prototype */ { | ||
code : { value : 0, writable : true }, | ||
message : { value : "", writable : true }, | ||
data : { value : undefined, writable : true } | ||
JsonRpcError.prototype = Object.create(null, /** @lends JsonRpcError.prototype */{ | ||
code: { value: 0, writable: true }, | ||
message: { value: "", writable: true }, | ||
data: { value: undefined, writable: true } | ||
}); | ||
@@ -126,60 +245,58 @@ | ||
*/ | ||
JsonRpcError.ERRORS = Object.freeze( Object.create( null,{ | ||
JsonRpcError.ERRORS = Object.freeze(Object.create(null, { | ||
PARSE_ERROR: { | ||
value : Object.freeze(Object.create(JsonRpcError.prototype, { | ||
code: { value: -32700, enumerable : true }, | ||
message: { value : "Parse error", enumerable : true } | ||
value: Object.freeze(Object.create(JsonRpcError.prototype, { | ||
code: { value: -32700, enumerable: true }, | ||
message: { value: "Parse error", enumerable: true } | ||
})), | ||
enumerable : true | ||
enumerable: true | ||
}, | ||
INVALID_REQUEST: { | ||
value : Object.freeze(Object.create(JsonRpcError.prototype, { | ||
code: { value : -32600, enumerable : true}, | ||
message: { value : "Invalid Request", enumerable : true } | ||
value: Object.freeze(Object.create(JsonRpcError.prototype, { | ||
code: { value: -32600, enumerable: true }, | ||
message: { value: "Invalid Request", enumerable: true } | ||
})), | ||
enumerable : true | ||
enumerable: true | ||
}, | ||
METHOD_NOT_FOUND: { | ||
value : Object.freeze(Object.create(JsonRpcError.prototype, { | ||
code: { value : -32601, enumerable : true}, | ||
message: { value : "Method not found", enumerable : true } | ||
value: Object.freeze(Object.create(JsonRpcError.prototype, { | ||
code: { value: -32601, enumerable: true }, | ||
message: { value: "Method not found", enumerable: true } | ||
})), | ||
enumerable : true | ||
enumerable: true | ||
}, | ||
INVALID_PARAMS: { | ||
value : Object.freeze(Object.create(JsonRpcError.prototype, { | ||
code: { value : -32602, enumerable : true}, | ||
message: { value: "Invalid params", enumerable : true} | ||
value: Object.freeze(Object.create(JsonRpcError.prototype, { | ||
code: { value: -32602, enumerable: true }, | ||
message: { value: "Invalid params", enumerable: true } | ||
})), | ||
enumerable : true }, | ||
enumerable: true }, | ||
INTERNAL_ERROR: { | ||
value : Object.freeze(Object.create(JsonRpcError.prototype, { | ||
code: { value : -32603, enumerable : true}, | ||
message: { value : "Internal error", enumerable : true } | ||
value: Object.freeze(Object.create(JsonRpcError.prototype, { | ||
code: { value: -32603, enumerable: true }, | ||
message: { value: "Internal error", enumerable: true } | ||
})), | ||
enumerable : true } | ||
enumerable: true } | ||
})); | ||
// TODO: For now this is only one level shallow copy, but should be real deep copy with omitting functions | ||
const copyParams = params => { | ||
var copyParams = function copyParams(params) { | ||
if ( typeof params === "undefined" ) { | ||
if (typeof params === "undefined") { | ||
return undefined; | ||
} else if (Array.isArray(params)) { | ||
} else if ( Array.isArray(params) ) { | ||
return Object.freeze(params.splice(0)); | ||
} else { | ||
const ret = Object.create(null); | ||
var ret = Object.create(null); | ||
for (let a in params) { | ||
for (var a in params) { | ||
ret[a] = params[a]; | ||
Object.defineProperty(ret, a, { value : params[a] }); | ||
Object.defineProperty(ret, a, { value: params[a] }); | ||
} | ||
@@ -189,3 +306,2 @@ | ||
} | ||
}; | ||
@@ -206,21 +322,18 @@ | ||
validateMethod(o.method); | ||
const method = o.method; | ||
var method = o.method; | ||
validateParams(o.params); | ||
const params = copyParams(o.params); | ||
var params = copyParams(o.params); | ||
return Object.freeze(Object.create(JsonRpcEvent.prototype, /** @lends JsonRpcEvent.prototype */ { | ||
return Object.freeze(Object.create(JsonRpcEvent.prototype, /** @lends JsonRpcEvent.prototype */{ | ||
method : { value : method }, | ||
params : { value : params }, | ||
serialize : { value : function() { | ||
return JSON.stringify( { jsonrpc : this.jsonrpc, | ||
method : this.method, | ||
params : this.params } ); | ||
}} | ||
method: { value: method }, | ||
params: { value: params }, | ||
serialize: { value: function value() { | ||
return JSON.stringify({ jsonrpc: this.jsonrpc, | ||
method: this.method, | ||
params: this.params }); | ||
} } | ||
})); | ||
} | ||
@@ -236,18 +349,11 @@ | ||
return Object.create(UUID.prototype,/** @lends UUID.prototype */ { | ||
value : { value : uuid, writable : true}, | ||
serialize : {value : function() { | ||
return Object.create(UUID.prototype, /** @lends UUID.prototype */{ | ||
value: { value: uuid, writable: true }, | ||
serialize: { value: function value() { | ||
const val = this.value; | ||
var val = this.value; | ||
return [ | ||
val[0] + val[1], | ||
val[2], | ||
val[3], | ||
val[4], | ||
val[5] + val[6] + val[7]].join("-"); | ||
}} | ||
return [val[0] + val[1], val[2], val[3], val[4], val[5] + val[6] + val[7]].join("-"); | ||
} } | ||
}); | ||
} | ||
@@ -259,13 +365,12 @@ | ||
UUID.fromString = function(str) { | ||
UUID.fromString = function (str) { | ||
const replaced = str.replace(/-/g,""); | ||
var replaced = str.replace(/-/g, ""); | ||
const a = []; | ||
for(let i=0;i<32;i+=4){ | ||
a.push(replaced.slice(i,i+4)); | ||
var a = []; | ||
for (var i = 0; i < 32; i += 4) { | ||
a.push(replaced.slice(i, i + 4)); | ||
} | ||
return UUID(a); | ||
}; | ||
@@ -287,4 +392,4 @@ | ||
const ar=[]; | ||
for(let i=0;i<8;i++){ | ||
var ar = []; | ||
for (var i = 0; i < 8; i++) { | ||
ar.push(s4()); | ||
@@ -308,14 +413,14 @@ } | ||
validateMethod(o.method); | ||
const method = o.method; | ||
var method = o.method; | ||
validateParams(o.params); | ||
const params = copyParams(o.params); | ||
var params = copyParams(o.params); | ||
const id = "id" in o ? o.id : UUID.randomUUID().serialize(); | ||
var id = "id" in o ? o.id : UUID.randomUUID().serialize(); | ||
return Object.freeze(Object.create(JsonRpcRequest.prototype,/** @lends JsonRpcRequest.prototype */ { | ||
return Object.freeze(Object.create(JsonRpcRequest.prototype, /** @lends JsonRpcRequest.prototype */{ | ||
method : { value : method }, | ||
params : { value : params }, | ||
id : { value : id }, | ||
method: { value: method }, | ||
params: { value: params }, | ||
id: { value: id }, | ||
@@ -326,19 +431,17 @@ /** | ||
*/ | ||
serialize : { value : function() { | ||
serialize: { value: function value() { | ||
var ret = { | ||
jsonrpc : this.jsonrpc, | ||
id : this.id, | ||
method : this.method | ||
}; | ||
var ret = { | ||
jsonrpc: this.jsonrpc, | ||
id: this.id, | ||
method: this.method | ||
}; | ||
if ( typeof this.params !== "undefined" ) { | ||
ret.params = this.params; | ||
} | ||
if (typeof this.params !== "undefined") { | ||
ret.params = this.params; | ||
} | ||
return JSON.stringify(ret); | ||
}} | ||
return JSON.stringify(ret); | ||
} } | ||
})); | ||
} | ||
@@ -358,7 +461,6 @@ | ||
throw new Error("JsonRpcResponse is an abstract class. Can't instantiate or run"); | ||
} | ||
JsonRpcResponse.prototype = Object.freeze(Object.create(JsonRpcElement.prototype, { | ||
id : { value : null, writable : true} | ||
id: { value: null, writable: true } | ||
})); | ||
@@ -378,12 +480,12 @@ | ||
if ( !(o.error instanceof JsonRpcError) ) { | ||
if (!(o.error instanceof JsonRpcError)) { | ||
throw Error("error property is not an instance of JsonRpcError!"); | ||
} | ||
const id = o.id; | ||
const error = o.error; | ||
var id = o.id; | ||
var error = o.error; | ||
return Object.freeze(Object.create(JsonRpcResponseError.prototype, { | ||
id : { value : id}, | ||
error : { value : error } | ||
id: { value: id }, | ||
error: { value: error } | ||
})); | ||
@@ -406,18 +508,16 @@ } | ||
if ( ( "result" in o) === false ) { | ||
if ("result" in o === false) { | ||
throw new Error("Required 'result' param is not present"); | ||
} | ||
const id = o.id; | ||
const result = o.result; | ||
var id = o.id; | ||
var result = o.result; | ||
return Object.freeze(Object.create(JsonRpcResponseResult.prototype, /** @lends JsonRpcResponseResult.prototype */ { | ||
return Object.freeze(Object.create(JsonRpcResponseResult.prototype, /** @lends JsonRpcResponseResult.prototype */{ | ||
id : { value: id }, | ||
result : { value: result } | ||
id: { value: id }, | ||
result: { value: result } | ||
})); | ||
} | ||
@@ -424,0 +524,0 @@ |
{ | ||
"name": "@adt/json-rpc", | ||
"version": "0.0.1-SNAPSHOT.17", | ||
"version": "0.0.1-SNAPSHOT.18", | ||
"license": "MIT", | ||
@@ -9,5 +9,10 @@ "main": "dist/JsonRpc.cjs.js", | ||
"devDependencies": { | ||
"babel-core": "^6.26.0", | ||
"babel-plugin-external-helpers": "^6.22.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-es2015-rollup": "^3.0.0", | ||
"eslint": "^4.12.1", | ||
"jasmine": "^2.8.0", | ||
"rollup": "^0.50.0" | ||
"rollup": "^0.50.0", | ||
"rollup-plugin-babel": "^3.0.3" | ||
}, | ||
@@ -37,3 +42,7 @@ "scripts": { | ||
"request-response" | ||
] | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/AwesomeDevTeam/js-JsonRpc.git" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# JsonRpc | ||
# JsonRpc [![npm version](https://badge.fury.io/js/%40adt%2Fjson-rpc.svg)](https://badge.fury.io/js/%40adt%2Fjson-rpc) [![Build Status](https://travis-ci.org/AwesomeDevTeam/js-JsonRpc.svg?branch=master)](https://travis-ci.org/AwesomeDevTeam/js-JsonRpc) | ||
@@ -3,0 +3,0 @@ Base JsonRpc classes. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
36043
1013
0
8