diff --git a/jsonrpc.d.ts b/jsonrpc.d.ts index ab4a006..f2a08ac 100644 --- a/jsonrpc.d.ts +++ b/jsonrpc.d.ts @@ -187,7 +187,7 @@ export declare const parseJsonRpcString: typeof parse; * @api public */ export declare function parseObject(obj: any): IParsedObject; -declare const jsonRpcGlobal: { +export declare const jsonrpc: { JsonRpc: typeof JsonRpc; JsonRpcError: typeof JsonRpcError; request: typeof request; @@ -199,11 +199,4 @@ declare const jsonRpcGlobal: { parseJsonRpcObject: typeof parseJsonRpcObject; parseJsonRpcString: typeof parse; }; -export default jsonRpcGlobal; -export declare const jsonrpc: typeof jsonRpcGlobal; -declare global { - const jsonrpc: typeof jsonRpcGlobal; - interface Window { - jsonrpc: typeof jsonRpcGlobal; - } -} +export default jsonrpc; diff --git a/jsonrpc.js b/jsonrpc.js index 7429a2e..3d60eab 100644 --- a/jsonrpc.js +++ b/jsonrpc.js @@ -1,2 +1,2 @@ -var r=Object.prototype.hasOwnProperty,e="function"==typeof Number.isSafeInteger?Number.isSafeInteger:function(r){return"number"==typeof r&&isFinite(r)&&r===Math.floor(r)&&Math.abs(r)<=9007199254740991},t=function(){this.jsonrpc="2.0"};t.prototype.serialize=function(){return JSON.stringify(this)},t.VERSION="2.0";var n=function(r){function e(e,t,n){r.call(this),this.id=e,this.method=t,void 0!==n&&(this.params=n)}return r&&(e.__proto__=r),(e.prototype=Object.create(r&&r.prototype)).constructor=e,e}(t),o=function(r){function e(e,t){r.call(this),this.method=e,void 0!==t&&(this.params=t)}return r&&(e.__proto__=r),(e.prototype=Object.create(r&&r.prototype)).constructor=e,e}(t),i=function(r){function e(e,t){r.call(this),this.id=e,this.result=t}return r&&(e.__proto__=r),(e.prototype=Object.create(r&&r.prototype)).constructor=e,e}(t),s=function(r){function e(e,t){r.call(this),this.id=e,this.error=t,this.id=e,this.error=t}return r&&(e.__proto__=r),(e.prototype=Object.create(r&&r.prototype)).constructor=e,e}(t),a=function(r,e){this.payload=r,this.type=e,this.payload=r,this.type=e},u=function(r,t,n){this.message=r,this.code=e(t)?t:0,null!=n&&(this.data=n)};function c(r,e,t){var o=new n(r,e,t);return y(o,!0),o}function l(r,e){var t=new o(r,e);return y(t,!0),t}function p(r,e){var t=new i(r,e);return y(t,!0),t}function f(r,e){var t=new s(r,e);return y(t,!0),t}function d(r){if(!g(r))return new a(u.invalidRequest(r),"invalid");var e;try{e=JSON.parse(r)}catch(e){return new a(u.parseError(r),"invalid")}return h(e)}function h(r){if(!Array.isArray(r))return m(r);if(0===r.length)return new a(u.invalidRequest(r),"invalid");for(var e=[],t=0,n=r.length;t boolean = typeof Number.isSafeInteger === 'function'\n ? Number.isSafeInteger // ECMAScript 2015\n : function (num) {\n return typeof num === 'number' && isFinite(num) && num === Math.floor(num) && Math.abs(num) <= 9007199254740991\n }\n\n/**\n * JsonRpc Class\n *\n * @return {Object} JsonRpc object\n * @api public\n */\nexport interface IJsonRpcType {\n readonly jsonrpc: string\n}\n\nexport class JsonRpc implements IJsonRpcType {\n static VERSION: string = '2.0'\n readonly jsonrpc: string\n\n constructor () {\n this.jsonrpc = '2.0'\n }\n\n serialize () {\n return JSON.stringify(this)\n }\n\n}\n\nexport class RequestObject extends JsonRpc {\n public id: ID\n public method: string\n public params?: RpcParams\n constructor (id: ID, method: string, params?: RpcParams) {\n super()\n this.id = id\n this.method = method\n if (params !== undefined ) {\n this.params = params\n }\n }\n}\n\nexport class NotificationObject extends JsonRpc {\n public method: string\n public params?: RpcParams\n constructor (method: string, params?: RpcParams) {\n super()\n this.method = method\n if (params !== undefined ) {\n this.params = params\n }\n }\n}\n\nexport class SuccessObject extends JsonRpc {\n public id: ID\n public result: Defined\n constructor (id: ID, result: Defined) {\n super()\n this.id = id\n this.result = result\n }\n}\n\nexport class ErrorObject extends JsonRpc {\n // tslint:disable-next-line:no-shadowed-variable\n constructor (public id: ID, public error: JsonRpcError) {\n super()\n this.id = id\n this.error = error\n }\n}\n\n/**\n * JsonRpcParsed Class\n *\n * @param {JsonRpc|JsonRpcError} payload\n * @param {type: } type\n * @api public\n */\nexport const enum RpcStatusType {\n request = 'request',\n notification = 'notification',\n success = 'success',\n error = 'error',\n invalid = 'invalid',\n}\n\nexport class JsonRpcParsed {\n constructor (\n public payload: JsonRpc | JsonRpcError,\n public type: RpcStatusType,\n ) {\n this.payload = payload\n this.type = type\n }\n}\n\n/**\n * JsonRpcError Class\n *\n * @param {String} message\n * @param {Integer} code\n * @return {String} name: optional\n * @api public\n */\nexport class JsonRpcError {\n static invalidRequest = function (data: any): JsonRpcError {\n return new JsonRpcError('Invalid request', -32600, data)\n }\n\n static methodNotFound = function (data: any): JsonRpcError {\n return new JsonRpcError('Method not found', -32601, data)\n }\n\n static invalidParams = function (data: any): JsonRpcError {\n return new JsonRpcError('Invalid params', -32602, data)\n }\n\n static internalError = function (data: any): JsonRpcError {\n return new JsonRpcError('Internal error', -32603, data)\n }\n\n static parseError = function (data: any): JsonRpcError {\n return new JsonRpcError('Parse error', -32700, data)\n }\n\n public message: string\n public code: number\n public data?: any\n constructor (message: string, code: number, data?: any) {\n this.message = message\n this.code = isInteger(code) ? code : 0\n if (data != null ) {\n this.data = data\n }\n }\n}\n\n/**\n * Creates a JSON-RPC 2.0 request object\n *\n * @param {String|Integer} id\n * @param {String} method\n * @param {Object|Array} [params]: optional\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function request (\n id: ID,\n method: string,\n params?: RpcParams,\n): RequestObject {\n const object = new RequestObject(id, method, params)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 notification object\n *\n * @param {String} method\n * @param {Object|Array} [params]: optional\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function notification (\n method: string,\n params?: RpcParams,\n): NotificationObject {\n const object = new NotificationObject(method, params)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 success response object\n *\n * @param {String|Integer} id\n * @param {Mixed} result\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function success (id: ID, result: Defined): SuccessObject {\n const object = new SuccessObject(id, result)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 error response object\n *\n * @param {String|Integer} id\n * @param {Object} JsonRpcError error\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function error (id: ID, err: JsonRpcError): ErrorObject {\n const object = new ErrorObject(id, err)\n validateMessage(object, true)\n return object\n}\n\nexport interface IParsedObjectSuccess {\n type: RpcStatusType.success,\n payload: SuccessObject\n}\n\nexport interface IParsedObjectNotification {\n type: RpcStatusType.notification,\n payload: NotificationObject\n}\n\nexport interface IParsedObjectRequest {\n type: RpcStatusType.request,\n payload: RequestObject\n}\n\nexport interface IParsedObjectError {\n type: RpcStatusType.error,\n payload: ErrorObject\n}\n\nexport interface IParsedObjectInvalid {\n type: RpcStatusType.invalid,\n payload: JsonRpcError\n}\n\n/**\n * Takes a JSON-RPC 2.0 payload (String) and tries to parse it into a JSON.\n * If successful, determine what object is it (response, notification,\n * success, error, or invalid), and return it's type and properly formatted object.\n *\n * @param {String} msg\n * @return {Object|Array} an array, or an object of this format:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport type IParsedObject = IParsedObjectSuccess | IParsedObjectNotification |\n IParsedObjectRequest | IParsedObjectError| IParsedObjectInvalid;\n\nexport function parse (\n message: string,\n): IParsedObject | IParsedObject[] {\n if (!isString(message)) {\n return new JsonRpcParsed(\n JsonRpcError.invalidRequest(message),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n let jsonrpcObj: JsonRpc | JsonRpc[]\n try {\n jsonrpcObj = JSON.parse(message)\n } catch (err) {\n return new JsonRpcParsed(\n JsonRpcError.parseError(message),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n return parseJsonRpcObject(jsonrpcObj)\n}\n\n/**\n * Takes a JSON-RPC 2.0 payload (Object) or batch (Object[]) and tries to parse it.\n * If successful, determine what objects are inside (response, notification,\n * success, error, or invalid), and return their types and properly formatted objects.\n *\n * @param {Object|Array} jsonrpcObj\n * @return {Object|Array} a single object or an array of `JsonRpcParsed` objects with `type` and `payload`:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport function parseJsonRpcObject (jsonrpcObj: JsonRpc | JsonRpc[]): IParsedObject | IParsedObject[] {\n if (!Array.isArray(jsonrpcObj)) {\n return parseObject(jsonrpcObj)\n }\n\n if (jsonrpcObj.length === 0) {\n return new JsonRpcParsed(\n JsonRpcError.invalidRequest(jsonrpcObj),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n const parsedObjectArray: IParsedObject[] = []\n for (let i = 0, len = jsonrpcObj.length; i < len; i++) {\n parsedObjectArray[i] = parseObject(jsonrpcObj[i])\n }\n\n return parsedObjectArray\n}\n\n/**\n * Alias for `parse` method.\n * Takes a JSON-RPC 2.0 payload (String) and tries to parse it into a JSON.\n * @api public\n */\nexport const parseJsonRpcString = parse\n\n/**\n * Takes a JSON-RPC 2.0 payload (Object) and tries to parse it into a JSON.\n * If successful, determine what object is it (response, notification,\n * success, error, or invalid), and return it's type and properly formatted object.\n *\n * @param {Object} obj\n * @return {Object} an `JsonRpcParsed` object with `type` and `payload`:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport function parseObject (obj: any): IParsedObject {\n let err: JsonRpcError | null = null\n let payload: JsonRpc | JsonRpcError | null = null\n let payloadType: RpcStatusType = RpcStatusType.invalid\n\n if (obj == null || obj.jsonrpc !== JsonRpc.VERSION) {\n err = JsonRpcError.invalidRequest(obj)\n payloadType = RpcStatusType.invalid\n } else if (!hasOwnProperty.call(obj, 'id')) {\n const tmp = obj as NotificationObject\n payload = new NotificationObject(tmp.method, tmp.params)\n err = validateMessage(payload)\n payloadType = RpcStatusType.notification\n } else if (hasOwnProperty.call(obj, 'method')) {\n const tmp = obj as RequestObject\n payload = new RequestObject(tmp.id, tmp.method, tmp.params)\n err = validateMessage(payload)\n payloadType = RpcStatusType.request\n } else if (hasOwnProperty.call(obj, 'result')) {\n const tmp = obj as SuccessObject\n payload = new SuccessObject(tmp.id, tmp.result)\n err = validateMessage(payload)\n payloadType = RpcStatusType.success\n } else if (hasOwnProperty.call(obj, 'error')) {\n const tmp = obj as ErrorObject\n payloadType = RpcStatusType.error\n if (tmp.error == null) {\n err = JsonRpcError.internalError(tmp)\n } else {\n const errorObj = new JsonRpcError(\n tmp.error.message,\n tmp.error.code,\n tmp.error.data,\n )\n if (errorObj.message !== tmp.error.message || errorObj.code !== tmp.error.code) {\n err = JsonRpcError.internalError(tmp)\n } else {\n payload = new ErrorObject(tmp.id, errorObj)\n err = validateMessage(payload)\n }\n }\n }\n\n if (err == null && payload != null) {\n return new JsonRpcParsed(payload, payloadType) as IParsedObject\n }\n return new JsonRpcParsed(\n err != null ? err : JsonRpcError.invalidRequest(obj),\n RpcStatusType.invalid,\n ) as IParsedObject\n}\n\n// if error, return error, else return null\nfunction validateMessage (obj: JsonRpc, throwIt?: boolean): JsonRpcError | null {\n let err: JsonRpcError | null = null\n if (obj instanceof RequestObject) {\n err = checkId(obj.id)\n if (err == null) {\n err = checkMethod(obj.method)\n }\n if (err == null) {\n err = checkParams(obj.params)\n }\n } else if (obj instanceof NotificationObject) {\n err = checkMethod(obj.method)\n if (err == null) {\n err = checkParams(obj.params)\n }\n } else if (obj instanceof SuccessObject) {\n err = checkId(obj.id)\n if (err == null) {\n err = checkResult(obj.result)\n }\n } else if (obj instanceof ErrorObject) {\n err = checkId(obj.id, true)\n if (err == null) {\n err = checkError(obj.error as JsonRpcError)\n }\n }\n if (throwIt && err != null) {\n throw err\n }\n return err\n}\n\nfunction checkId (id: ID, maybeNull?: boolean): JsonRpcError | null {\n if (maybeNull && id === null) {\n return null\n }\n return isString(id) || isInteger(id as number)\n ? null\n : JsonRpcError.internalError('\"id\" must be provided, a string or an integer.')\n}\n\nfunction checkMethod (method: string): JsonRpcError | null {\n return isString(method) ? null : JsonRpcError.invalidRequest(method)\n}\n\nfunction checkResult (result: Defined): JsonRpcError | null {\n return result === undefined\n ? JsonRpcError.internalError('Result must exist for success Response objects')\n : null\n}\n\nfunction checkParams (params?: RpcParams): JsonRpcError | null {\n if (params === undefined) {\n return null\n }\n if (Array.isArray(params) || isObject(params)) {\n // ensure params can be stringify\n try {\n JSON.stringify(params)\n return null\n } catch (err) {\n return JsonRpcError.parseError(params)\n }\n }\n return JsonRpcError.invalidParams(params)\n}\n\nfunction checkError (err: JsonRpcError): JsonRpcError | null {\n if (!(err instanceof JsonRpcError)) {\n return JsonRpcError.internalError('Error must be an instance of JsonRpcError')\n }\n\n if (!isInteger(err.code)) {\n return JsonRpcError.internalError('Invalid error code. It must be an integer.')\n }\n\n if (!isString(err.message)) {\n return JsonRpcError.internalError('Message must exist or must be a string.')\n }\n\n return null\n}\n\nfunction isString (obj: any): boolean {\n return obj !== '' && typeof obj === 'string'\n}\n\nfunction isObject (obj: any): boolean {\n return obj != null && typeof obj === 'object' && !Array.isArray(obj)\n}\n\nconst jsonRpcGlobal = {\n JsonRpc,\n JsonRpcError,\n request,\n notification,\n success,\n error,\n parse,\n parseObject,\n parseJsonRpcObject,\n parseJsonRpcString,\n}\n\nexport default jsonRpcGlobal\nexport const jsonrpc: typeof jsonRpcGlobal = jsonRpcGlobal\n\ndeclare global {\n const jsonrpc: typeof jsonRpcGlobal\n\n interface Window { jsonrpc: typeof jsonRpcGlobal }\n}\n\n((global || window) as any).jsonrpc = jsonRpcGlobal // tslint:disable-line\n"],"names":["const","hasOwnProperty","Object","prototype","isInteger","Number","isSafeInteger","num","isFinite","Math","floor","abs","JsonRpc","constructor","jsonrpc","serialize","JSON","stringify","this","RequestObject","id","method","params","undefined","NotificationObject","SuccessObject","result","ErrorObject","error","JsonRpcParsed","payload","type","JsonRpcError","message","code","data","request","object","validateMessage","notification","success","err","parse","isString","invalidRequest","jsonrpcObj","parseError","parseJsonRpcObject","Array","isArray","parseObject","length","parsedObjectArray","i","len","parseJsonRpcString","obj","payloadType","VERSION","call","tmp","internalError","errorObj","throwIt","checkId","checkMethod","checkParams","checkError","maybeNull","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EAAa5C,UACbsB,EAAStB,GAAU,KAAOW,EAAaY,eAAevB,GAS/D,SAAS6C,EAAa5C,WACLC,IAAXD,SACK,QAEL0B,MAAMC,QAAQ3B,IAiCJ,OADGkC,EAhCqBlC,IAiCD,iBAARkC,IAAqBR,MAAMC,QAAQO,cA9B5DxC,KAAKC,UAAUK,GACR,KACP,MAAOmB,UACAT,EAAac,WAAWxB,GA0BrC,IAAmBkC,SAvBVxB,EAAaqC,cAAc/C,GAmBpC,SAASqB,EAAUa,SACF,KAARA,GAA6B,iBAARA,EAO9BxD,IAAMsE,EAAgB,SACpB1D,eACAoB,UACAI,eACAG,UACAC,QACAZ,QACAc,cACAQ,qBACAH,qBACAQ,GAIWzC,EAAgCwD,GAQ3CC,QAAUC,QAAgB1D,QAAUwD"} \ No newline at end of file +{"version":3,"file":"jsonrpc.js","sources":["jsonrpc.ts"],"sourcesContent":["// **Github:** https://github.com/teambition/jsonrpc-lite\n//\n// http://www.jsonrpc.org/specification\n// **License:** MIT\n'use strict'\n\nexport type ID = string | number\nexport type Defined = string | number | boolean | object | null\nexport type RpcParams = object | Defined[]\n\nconst hasOwnProperty = Object.prototype.hasOwnProperty\nconst isInteger: (num: number) => boolean = typeof Number.isSafeInteger === 'function'\n ? Number.isSafeInteger // ECMAScript 2015\n : function (num) {\n return typeof num === 'number' && isFinite(num) && num === Math.floor(num) && Math.abs(num) <= 9007199254740991\n }\n\n/**\n * JsonRpc Class\n *\n * @return {Object} JsonRpc object\n * @api public\n */\nexport interface IJsonRpcType {\n readonly jsonrpc: string\n}\n\nexport class JsonRpc implements IJsonRpcType {\n static VERSION: string = '2.0'\n readonly jsonrpc: string\n\n constructor () {\n this.jsonrpc = '2.0'\n }\n\n serialize () {\n return JSON.stringify(this)\n }\n\n}\n\nexport class RequestObject extends JsonRpc {\n public id: ID\n public method: string\n public params?: RpcParams\n constructor (id: ID, method: string, params?: RpcParams) {\n super()\n this.id = id\n this.method = method\n if (params !== undefined ) {\n this.params = params\n }\n }\n}\n\nexport class NotificationObject extends JsonRpc {\n public method: string\n public params?: RpcParams\n constructor (method: string, params?: RpcParams) {\n super()\n this.method = method\n if (params !== undefined ) {\n this.params = params\n }\n }\n}\n\nexport class SuccessObject extends JsonRpc {\n public id: ID\n public result: Defined\n constructor (id: ID, result: Defined) {\n super()\n this.id = id\n this.result = result\n }\n}\n\nexport class ErrorObject extends JsonRpc {\n // tslint:disable-next-line:no-shadowed-variable\n constructor (public id: ID, public error: JsonRpcError) {\n super()\n this.id = id\n this.error = error\n }\n}\n\n/**\n * JsonRpcParsed Class\n *\n * @param {JsonRpc|JsonRpcError} payload\n * @param {type: } type\n * @api public\n */\nexport const enum RpcStatusType {\n request = 'request',\n notification = 'notification',\n success = 'success',\n error = 'error',\n invalid = 'invalid',\n}\n\nexport class JsonRpcParsed {\n constructor (\n public payload: JsonRpc | JsonRpcError,\n public type: RpcStatusType,\n ) {\n this.payload = payload\n this.type = type\n }\n}\n\n/**\n * JsonRpcError Class\n *\n * @param {String} message\n * @param {Integer} code\n * @return {String} name: optional\n * @api public\n */\nexport class JsonRpcError {\n static invalidRequest = function (data: any): JsonRpcError {\n return new JsonRpcError('Invalid request', -32600, data)\n }\n\n static methodNotFound = function (data: any): JsonRpcError {\n return new JsonRpcError('Method not found', -32601, data)\n }\n\n static invalidParams = function (data: any): JsonRpcError {\n return new JsonRpcError('Invalid params', -32602, data)\n }\n\n static internalError = function (data: any): JsonRpcError {\n return new JsonRpcError('Internal error', -32603, data)\n }\n\n static parseError = function (data: any): JsonRpcError {\n return new JsonRpcError('Parse error', -32700, data)\n }\n\n public message: string\n public code: number\n public data?: any\n constructor (message: string, code: number, data?: any) {\n this.message = message\n this.code = isInteger(code) ? code : 0\n if (data != null ) {\n this.data = data\n }\n }\n}\n\n/**\n * Creates a JSON-RPC 2.0 request object\n *\n * @param {String|Integer} id\n * @param {String} method\n * @param {Object|Array} [params]: optional\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function request (\n id: ID,\n method: string,\n params?: RpcParams,\n): RequestObject {\n const object = new RequestObject(id, method, params)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 notification object\n *\n * @param {String} method\n * @param {Object|Array} [params]: optional\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function notification (\n method: string,\n params?: RpcParams,\n): NotificationObject {\n const object = new NotificationObject(method, params)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 success response object\n *\n * @param {String|Integer} id\n * @param {Mixed} result\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function success (id: ID, result: Defined): SuccessObject {\n const object = new SuccessObject(id, result)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 error response object\n *\n * @param {String|Integer} id\n * @param {Object} JsonRpcError error\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function error (id: ID, err: JsonRpcError): ErrorObject {\n const object = new ErrorObject(id, err)\n validateMessage(object, true)\n return object\n}\n\nexport interface IParsedObjectSuccess {\n type: RpcStatusType.success,\n payload: SuccessObject\n}\n\nexport interface IParsedObjectNotification {\n type: RpcStatusType.notification,\n payload: NotificationObject\n}\n\nexport interface IParsedObjectRequest {\n type: RpcStatusType.request,\n payload: RequestObject\n}\n\nexport interface IParsedObjectError {\n type: RpcStatusType.error,\n payload: ErrorObject\n}\n\nexport interface IParsedObjectInvalid {\n type: RpcStatusType.invalid,\n payload: JsonRpcError\n}\n\n/**\n * Takes a JSON-RPC 2.0 payload (String) and tries to parse it into a JSON.\n * If successful, determine what object is it (response, notification,\n * success, error, or invalid), and return it's type and properly formatted object.\n *\n * @param {String} msg\n * @return {Object|Array} an array, or an object of this format:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport type IParsedObject = IParsedObjectSuccess | IParsedObjectNotification |\n IParsedObjectRequest | IParsedObjectError| IParsedObjectInvalid;\n\nexport function parse (\n message: string,\n): IParsedObject | IParsedObject[] {\n if (!isString(message)) {\n return new JsonRpcParsed(\n JsonRpcError.invalidRequest(message),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n let jsonrpcObj: JsonRpc | JsonRpc[]\n try {\n jsonrpcObj = JSON.parse(message)\n } catch (err) {\n return new JsonRpcParsed(\n JsonRpcError.parseError(message),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n return parseJsonRpcObject(jsonrpcObj)\n}\n\n/**\n * Takes a JSON-RPC 2.0 payload (Object) or batch (Object[]) and tries to parse it.\n * If successful, determine what objects are inside (response, notification,\n * success, error, or invalid), and return their types and properly formatted objects.\n *\n * @param {Object|Array} jsonrpcObj\n * @return {Object|Array} a single object or an array of `JsonRpcParsed` objects with `type` and `payload`:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport function parseJsonRpcObject (jsonrpcObj: JsonRpc | JsonRpc[]): IParsedObject | IParsedObject[] {\n if (!Array.isArray(jsonrpcObj)) {\n return parseObject(jsonrpcObj)\n }\n\n if (jsonrpcObj.length === 0) {\n return new JsonRpcParsed(\n JsonRpcError.invalidRequest(jsonrpcObj),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n const parsedObjectArray: IParsedObject[] = []\n for (let i = 0, len = jsonrpcObj.length; i < len; i++) {\n parsedObjectArray[i] = parseObject(jsonrpcObj[i])\n }\n\n return parsedObjectArray\n}\n\n/**\n * Alias for `parse` method.\n * Takes a JSON-RPC 2.0 payload (String) and tries to parse it into a JSON.\n * @api public\n */\nexport const parseJsonRpcString = parse\n\n/**\n * Takes a JSON-RPC 2.0 payload (Object) and tries to parse it into a JSON.\n * If successful, determine what object is it (response, notification,\n * success, error, or invalid), and return it's type and properly formatted object.\n *\n * @param {Object} obj\n * @return {Object} an `JsonRpcParsed` object with `type` and `payload`:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport function parseObject (obj: any): IParsedObject {\n let err: JsonRpcError | null = null\n let payload: JsonRpc | JsonRpcError | null = null\n let payloadType: RpcStatusType = RpcStatusType.invalid\n\n if (obj == null || obj.jsonrpc !== JsonRpc.VERSION) {\n err = JsonRpcError.invalidRequest(obj)\n payloadType = RpcStatusType.invalid\n } else if (!hasOwnProperty.call(obj, 'id')) {\n const tmp = obj as NotificationObject\n payload = new NotificationObject(tmp.method, tmp.params)\n err = validateMessage(payload)\n payloadType = RpcStatusType.notification\n } else if (hasOwnProperty.call(obj, 'method')) {\n const tmp = obj as RequestObject\n payload = new RequestObject(tmp.id, tmp.method, tmp.params)\n err = validateMessage(payload)\n payloadType = RpcStatusType.request\n } else if (hasOwnProperty.call(obj, 'result')) {\n const tmp = obj as SuccessObject\n payload = new SuccessObject(tmp.id, tmp.result)\n err = validateMessage(payload)\n payloadType = RpcStatusType.success\n } else if (hasOwnProperty.call(obj, 'error')) {\n const tmp = obj as ErrorObject\n payloadType = RpcStatusType.error\n if (tmp.error == null) {\n err = JsonRpcError.internalError(tmp)\n } else {\n const errorObj = new JsonRpcError(\n tmp.error.message,\n tmp.error.code,\n tmp.error.data,\n )\n if (errorObj.message !== tmp.error.message || errorObj.code !== tmp.error.code) {\n err = JsonRpcError.internalError(tmp)\n } else {\n payload = new ErrorObject(tmp.id, errorObj)\n err = validateMessage(payload)\n }\n }\n }\n\n if (err == null && payload != null) {\n return new JsonRpcParsed(payload, payloadType) as IParsedObject\n }\n return new JsonRpcParsed(\n err != null ? err : JsonRpcError.invalidRequest(obj),\n RpcStatusType.invalid,\n ) as IParsedObject\n}\n\n// if error, return error, else return null\nfunction validateMessage (obj: JsonRpc, throwIt?: boolean): JsonRpcError | null {\n let err: JsonRpcError | null = null\n if (obj instanceof RequestObject) {\n err = checkId(obj.id)\n if (err == null) {\n err = checkMethod(obj.method)\n }\n if (err == null) {\n err = checkParams(obj.params)\n }\n } else if (obj instanceof NotificationObject) {\n err = checkMethod(obj.method)\n if (err == null) {\n err = checkParams(obj.params)\n }\n } else if (obj instanceof SuccessObject) {\n err = checkId(obj.id)\n if (err == null) {\n err = checkResult(obj.result)\n }\n } else if (obj instanceof ErrorObject) {\n err = checkId(obj.id, true)\n if (err == null) {\n err = checkError(obj.error as JsonRpcError)\n }\n }\n if (throwIt && err != null) {\n throw err\n }\n return err\n}\n\nfunction checkId (id: ID, maybeNull?: boolean): JsonRpcError | null {\n if (maybeNull && id === null) {\n return null\n }\n return isString(id) || isInteger(id as number)\n ? null\n : JsonRpcError.internalError('\"id\" must be provided, a string or an integer.')\n}\n\nfunction checkMethod (method: string): JsonRpcError | null {\n return isString(method) ? null : JsonRpcError.invalidRequest(method)\n}\n\nfunction checkResult (result: Defined): JsonRpcError | null {\n return result === undefined\n ? JsonRpcError.internalError('Result must exist for success Response objects')\n : null\n}\n\nfunction checkParams (params?: RpcParams): JsonRpcError | null {\n if (params === undefined) {\n return null\n }\n if (Array.isArray(params) || isObject(params)) {\n // ensure params can be stringify\n try {\n JSON.stringify(params)\n return null\n } catch (err) {\n return JsonRpcError.parseError(params)\n }\n }\n return JsonRpcError.invalidParams(params)\n}\n\nfunction checkError (err: JsonRpcError): JsonRpcError | null {\n if (!(err instanceof JsonRpcError)) {\n return JsonRpcError.internalError('Error must be an instance of JsonRpcError')\n }\n\n if (!isInteger(err.code)) {\n return JsonRpcError.internalError('Invalid error code. It must be an integer.')\n }\n\n if (!isString(err.message)) {\n return JsonRpcError.internalError('Message must exist or must be a string.')\n }\n\n return null\n}\n\nfunction isString (obj: any): boolean {\n return obj !== '' && typeof obj === 'string'\n}\n\nfunction isObject (obj: any): boolean {\n return obj != null && typeof obj === 'object' && !Array.isArray(obj)\n}\n\nexport const jsonrpc = {\n JsonRpc,\n JsonRpcError,\n request,\n notification,\n success,\n error,\n parse,\n parseObject,\n parseJsonRpcObject,\n parseJsonRpcString,\n}\n\nexport default jsonrpc\n"],"names":["const","hasOwnProperty","Object","prototype","isInteger","Number","isSafeInteger","num","isFinite","Math","floor","abs","JsonRpc","constructor","jsonrpc","serialize","JSON","stringify","this","RequestObject","id","method","params","undefined","NotificationObject","SuccessObject","result","ErrorObject","error","JsonRpcParsed","payload","type","JsonRpcError","message","code","data","request","object","validateMessage","notification","success","err","parse","isString","invalidRequest","jsonrpcObj","parseError","parseJsonRpcObject","Array","isArray","parseObject","length","parsedObjectArray","i","len","parseJsonRpcString","obj","payloadType","VERSION","call","tmp","internalError","errorObj","throwIt","checkId","checkMethod","checkParams","checkError","maybeNull","invalidParams"],"mappings":"aAUAA,IAAMC,EAAiBC,OAAOC,UAAUF,eAClCG,EAAsE,mBAAzBC,OAAOC,cACtDD,OAAOC,cACP,SAAUC,SACY,iBAARA,GAAoBC,SAASD,IAAQA,IAAQE,KAAKC,MAAMH,IAAQE,KAAKE,IAAIJ,IAAQ,kBAatFK,EAIXC,gBACOC,QAAU,OAGjBC,YAAAA,4BACSC,KAAKC,UAAUC,OARjBN,UAAkB,UAadO,cAIXN,WAAaO,EAAQC,EAAgBC,qBAE9BF,GAAKA,OACLC,OAASA,OACCE,IAAXD,SACGA,OAASA,0FATeV,GActBY,cAGXX,WAAaQ,EAAgBC,qBAEtBD,OAASA,OACCE,IAAXD,SACGA,OAASA,0FAPoBV,GAY3Ba,cAGXZ,WAAaO,EAAQM,qBAEdN,GAAKA,OACLM,OAASA,yFANiBd,GAUtBe,cAEXd,WAAoBO,EAAeQ,wBAAfR,aAAeQ,OAE5BR,GAAKA,OACLQ,MAAQA,yFALgBhB,GAwBpBiB,EACXhB,SACSiB,EACAC,gBADAD,YACAC,OAEFD,QAAUA,OACVC,KAAOA,GAYHC,EAwBXnB,SAAaoB,EAAiBC,EAAcC,QACrCF,QAAUA,OACVC,KAAO9B,EAAU8B,GAAQA,EAAO,EACzB,MAARC,SACGA,KAAOA,IAclB,SAAgBC,EACdhB,EACAC,EACAC,OAEMe,EAAS,IAAIlB,EAAcC,EAAIC,EAAQC,UAC7CgB,EAAgBD,GAAQ,GACjBA,WAWOE,EACdlB,EACAC,OAEMe,EAAS,IAAIb,EAAmBH,EAAQC,UAC9CgB,EAAgBD,GAAQ,GACjBA,WAWOG,EAASpB,EAAQM,OACzBW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CAAWX,EAAI5B,QAGrBmC,GAAkB,MAAPtB,QACPA,SAEDA,EAGT,SAASuB,EAAS5C,EAAQgD,UACpBA,GAAoB,OAAPhD,EACR,KAEFuB,EAASvB,IAAOhB,EAAUgB,GAC7B,KACAY,EAAa6B,cAAc,kDAGjC,SAASI,EAAa5C,UACbsB,EAAStB,GAAU,KAAOW,EAAaY,eAAevB,GAS/D,SAAS6C,EAAa5C,WACLC,IAAXD,SACK,QAEL0B,MAAMC,QAAQ3B,IAiCJ,OADGkC,EAhCqBlC,IAiCD,iBAARkC,IAAqBR,MAAMC,QAAQO,cA9B5DxC,KAAKC,UAAUK,GACR,KACP,MAAOmB,UACAT,EAAac,WAAWxB,GA0BrC,IAAmBkC,SAvBVxB,EAAaqC,cAAc/C,GAmBpC,SAASqB,EAAUa,SACF,KAARA,GAA6B,iBAARA,EAO9B,IAAa1C,EAAU,SACrBF,eACAoB,UACAI,eACAG,UACAC,QACAZ,QACAc,cACAQ,qBACAH,qBACAQ"} \ No newline at end of file diff --git a/jsonrpc.mjs b/jsonrpc.mjs index 7dcc83f..49035c9 100644 --- a/jsonrpc.mjs +++ b/jsonrpc.mjs @@ -1,2 +1,2 @@ -var r=Object.prototype.hasOwnProperty,n="function"==typeof Number.isSafeInteger?Number.isSafeInteger:function(r){return"number"==typeof r&&isFinite(r)&&r===Math.floor(r)&&Math.abs(r)<=9007199254740991},t=function(){this.jsonrpc="2.0"};t.prototype.serialize=function(){return JSON.stringify(this)},t.VERSION="2.0";var e=function(r){function n(n,t,e){r.call(this),this.id=n,this.method=t,void 0!==e&&(this.params=e)}return r&&(n.__proto__=r),(n.prototype=Object.create(r&&r.prototype)).constructor=n,n}(t),o=function(r){function n(n,t){r.call(this),this.method=n,void 0!==t&&(this.params=t)}return r&&(n.__proto__=r),(n.prototype=Object.create(r&&r.prototype)).constructor=n,n}(t),i=function(r){function n(n,t){r.call(this),this.id=n,this.result=t}return r&&(n.__proto__=r),(n.prototype=Object.create(r&&r.prototype)).constructor=n,n}(t),s=function(r){function n(n,t){r.call(this),this.id=n,this.error=t,this.id=n,this.error=t}return r&&(n.__proto__=r),(n.prototype=Object.create(r&&r.prototype)).constructor=n,n}(t),a=function(r,n){this.payload=r,this.type=n,this.payload=r,this.type=n},u=function(r,t,e){this.message=r,this.code=n(t)?t:0,null!=e&&(this.data=e)};function l(r,n,t){var o=new e(r,n,t);return y(o,!0),o}function c(r,n){var t=new o(r,n);return y(t,!0),t}function f(r,n){var t=new i(r,n);return y(t,!0),t}function p(r,n){var t=new s(r,n);return y(t,!0),t}function d(r){if(!E(r))return new a(u.invalidRequest(r),"invalid");var n;try{n=JSON.parse(r)}catch(n){return new a(u.parseError(r),"invalid")}return h(n)}function h(r){if(!Array.isArray(r))return m(r);if(0===r.length)return new a(u.invalidRequest(r),"invalid");for(var n=[],t=0,e=r.length;t boolean = typeof Number.isSafeInteger === 'function'\n ? Number.isSafeInteger // ECMAScript 2015\n : function (num) {\n return typeof num === 'number' && isFinite(num) && num === Math.floor(num) && Math.abs(num) <= 9007199254740991\n }\n\n/**\n * JsonRpc Class\n *\n * @return {Object} JsonRpc object\n * @api public\n */\nexport interface IJsonRpcType {\n readonly jsonrpc: string\n}\n\nexport class JsonRpc implements IJsonRpcType {\n static VERSION: string = '2.0'\n readonly jsonrpc: string\n\n constructor () {\n this.jsonrpc = '2.0'\n }\n\n serialize () {\n return JSON.stringify(this)\n }\n\n}\n\nexport class RequestObject extends JsonRpc {\n public id: ID\n public method: string\n public params?: RpcParams\n constructor (id: ID, method: string, params?: RpcParams) {\n super()\n this.id = id\n this.method = method\n if (params !== undefined ) {\n this.params = params\n }\n }\n}\n\nexport class NotificationObject extends JsonRpc {\n public method: string\n public params?: RpcParams\n constructor (method: string, params?: RpcParams) {\n super()\n this.method = method\n if (params !== undefined ) {\n this.params = params\n }\n }\n}\n\nexport class SuccessObject extends JsonRpc {\n public id: ID\n public result: Defined\n constructor (id: ID, result: Defined) {\n super()\n this.id = id\n this.result = result\n }\n}\n\nexport class ErrorObject extends JsonRpc {\n // tslint:disable-next-line:no-shadowed-variable\n constructor (public id: ID, public error: JsonRpcError) {\n super()\n this.id = id\n this.error = error\n }\n}\n\n/**\n * JsonRpcParsed Class\n *\n * @param {JsonRpc|JsonRpcError} payload\n * @param {type: } type\n * @api public\n */\nexport const enum RpcStatusType {\n request = 'request',\n notification = 'notification',\n success = 'success',\n error = 'error',\n invalid = 'invalid',\n}\n\nexport class JsonRpcParsed {\n constructor (\n public payload: JsonRpc | JsonRpcError,\n public type: RpcStatusType,\n ) {\n this.payload = payload\n this.type = type\n }\n}\n\n/**\n * JsonRpcError Class\n *\n * @param {String} message\n * @param {Integer} code\n * @return {String} name: optional\n * @api public\n */\nexport class JsonRpcError {\n static invalidRequest = function (data: any): JsonRpcError {\n return new JsonRpcError('Invalid request', -32600, data)\n }\n\n static methodNotFound = function (data: any): JsonRpcError {\n return new JsonRpcError('Method not found', -32601, data)\n }\n\n static invalidParams = function (data: any): JsonRpcError {\n return new JsonRpcError('Invalid params', -32602, data)\n }\n\n static internalError = function (data: any): JsonRpcError {\n return new JsonRpcError('Internal error', -32603, data)\n }\n\n static parseError = function (data: any): JsonRpcError {\n return new JsonRpcError('Parse error', -32700, data)\n }\n\n public message: string\n public code: number\n public data?: any\n constructor (message: string, code: number, data?: any) {\n this.message = message\n this.code = isInteger(code) ? code : 0\n if (data != null ) {\n this.data = data\n }\n }\n}\n\n/**\n * Creates a JSON-RPC 2.0 request object\n *\n * @param {String|Integer} id\n * @param {String} method\n * @param {Object|Array} [params]: optional\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function request (\n id: ID,\n method: string,\n params?: RpcParams,\n): RequestObject {\n const object = new RequestObject(id, method, params)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 notification object\n *\n * @param {String} method\n * @param {Object|Array} [params]: optional\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function notification (\n method: string,\n params?: RpcParams,\n): NotificationObject {\n const object = new NotificationObject(method, params)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 success response object\n *\n * @param {String|Integer} id\n * @param {Mixed} result\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function success (id: ID, result: Defined): SuccessObject {\n const object = new SuccessObject(id, result)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 error response object\n *\n * @param {String|Integer} id\n * @param {Object} JsonRpcError error\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function error (id: ID, err: JsonRpcError): ErrorObject {\n const object = new ErrorObject(id, err)\n validateMessage(object, true)\n return object\n}\n\nexport interface IParsedObjectSuccess {\n type: RpcStatusType.success,\n payload: SuccessObject\n}\n\nexport interface IParsedObjectNotification {\n type: RpcStatusType.notification,\n payload: NotificationObject\n}\n\nexport interface IParsedObjectRequest {\n type: RpcStatusType.request,\n payload: RequestObject\n}\n\nexport interface IParsedObjectError {\n type: RpcStatusType.error,\n payload: ErrorObject\n}\n\nexport interface IParsedObjectInvalid {\n type: RpcStatusType.invalid,\n payload: JsonRpcError\n}\n\n/**\n * Takes a JSON-RPC 2.0 payload (String) and tries to parse it into a JSON.\n * If successful, determine what object is it (response, notification,\n * success, error, or invalid), and return it's type and properly formatted object.\n *\n * @param {String} msg\n * @return {Object|Array} an array, or an object of this format:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport type IParsedObject = IParsedObjectSuccess | IParsedObjectNotification |\n IParsedObjectRequest | IParsedObjectError| IParsedObjectInvalid;\n\nexport function parse (\n message: string,\n): IParsedObject | IParsedObject[] {\n if (!isString(message)) {\n return new JsonRpcParsed(\n JsonRpcError.invalidRequest(message),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n let jsonrpcObj: JsonRpc | JsonRpc[]\n try {\n jsonrpcObj = JSON.parse(message)\n } catch (err) {\n return new JsonRpcParsed(\n JsonRpcError.parseError(message),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n return parseJsonRpcObject(jsonrpcObj)\n}\n\n/**\n * Takes a JSON-RPC 2.0 payload (Object) or batch (Object[]) and tries to parse it.\n * If successful, determine what objects are inside (response, notification,\n * success, error, or invalid), and return their types and properly formatted objects.\n *\n * @param {Object|Array} jsonrpcObj\n * @return {Object|Array} a single object or an array of `JsonRpcParsed` objects with `type` and `payload`:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport function parseJsonRpcObject (jsonrpcObj: JsonRpc | JsonRpc[]): IParsedObject | IParsedObject[] {\n if (!Array.isArray(jsonrpcObj)) {\n return parseObject(jsonrpcObj)\n }\n\n if (jsonrpcObj.length === 0) {\n return new JsonRpcParsed(\n JsonRpcError.invalidRequest(jsonrpcObj),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n const parsedObjectArray: IParsedObject[] = []\n for (let i = 0, len = jsonrpcObj.length; i < len; i++) {\n parsedObjectArray[i] = parseObject(jsonrpcObj[i])\n }\n\n return parsedObjectArray\n}\n\n/**\n * Alias for `parse` method.\n * Takes a JSON-RPC 2.0 payload (String) and tries to parse it into a JSON.\n * @api public\n */\nexport const parseJsonRpcString = parse\n\n/**\n * Takes a JSON-RPC 2.0 payload (Object) and tries to parse it into a JSON.\n * If successful, determine what object is it (response, notification,\n * success, error, or invalid), and return it's type and properly formatted object.\n *\n * @param {Object} obj\n * @return {Object} an `JsonRpcParsed` object with `type` and `payload`:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport function parseObject (obj: any): IParsedObject {\n let err: JsonRpcError | null = null\n let payload: JsonRpc | JsonRpcError | null = null\n let payloadType: RpcStatusType = RpcStatusType.invalid\n\n if (obj == null || obj.jsonrpc !== JsonRpc.VERSION) {\n err = JsonRpcError.invalidRequest(obj)\n payloadType = RpcStatusType.invalid\n } else if (!hasOwnProperty.call(obj, 'id')) {\n const tmp = obj as NotificationObject\n payload = new NotificationObject(tmp.method, tmp.params)\n err = validateMessage(payload)\n payloadType = RpcStatusType.notification\n } else if (hasOwnProperty.call(obj, 'method')) {\n const tmp = obj as RequestObject\n payload = new RequestObject(tmp.id, tmp.method, tmp.params)\n err = validateMessage(payload)\n payloadType = RpcStatusType.request\n } else if (hasOwnProperty.call(obj, 'result')) {\n const tmp = obj as SuccessObject\n payload = new SuccessObject(tmp.id, tmp.result)\n err = validateMessage(payload)\n payloadType = RpcStatusType.success\n } else if (hasOwnProperty.call(obj, 'error')) {\n const tmp = obj as ErrorObject\n payloadType = RpcStatusType.error\n if (tmp.error == null) {\n err = JsonRpcError.internalError(tmp)\n } else {\n const errorObj = new JsonRpcError(\n tmp.error.message,\n tmp.error.code,\n tmp.error.data,\n )\n if (errorObj.message !== tmp.error.message || errorObj.code !== tmp.error.code) {\n err = JsonRpcError.internalError(tmp)\n } else {\n payload = new ErrorObject(tmp.id, errorObj)\n err = validateMessage(payload)\n }\n }\n }\n\n if (err == null && payload != null) {\n return new JsonRpcParsed(payload, payloadType) as IParsedObject\n }\n return new JsonRpcParsed(\n err != null ? err : JsonRpcError.invalidRequest(obj),\n RpcStatusType.invalid,\n ) as IParsedObject\n}\n\n// if error, return error, else return null\nfunction validateMessage (obj: JsonRpc, throwIt?: boolean): JsonRpcError | null {\n let err: JsonRpcError | null = null\n if (obj instanceof RequestObject) {\n err = checkId(obj.id)\n if (err == null) {\n err = checkMethod(obj.method)\n }\n if (err == null) {\n err = checkParams(obj.params)\n }\n } else if (obj instanceof NotificationObject) {\n err = checkMethod(obj.method)\n if (err == null) {\n err = checkParams(obj.params)\n }\n } else if (obj instanceof SuccessObject) {\n err = checkId(obj.id)\n if (err == null) {\n err = checkResult(obj.result)\n }\n } else if (obj instanceof ErrorObject) {\n err = checkId(obj.id, true)\n if (err == null) {\n err = checkError(obj.error as JsonRpcError)\n }\n }\n if (throwIt && err != null) {\n throw err\n }\n return err\n}\n\nfunction checkId (id: ID, maybeNull?: boolean): JsonRpcError | null {\n if (maybeNull && id === null) {\n return null\n }\n return isString(id) || isInteger(id as number)\n ? null\n : JsonRpcError.internalError('\"id\" must be provided, a string or an integer.')\n}\n\nfunction checkMethod (method: string): JsonRpcError | null {\n return isString(method) ? null : JsonRpcError.invalidRequest(method)\n}\n\nfunction checkResult (result: Defined): JsonRpcError | null {\n return result === undefined\n ? JsonRpcError.internalError('Result must exist for success Response objects')\n : null\n}\n\nfunction checkParams (params?: RpcParams): JsonRpcError | null {\n if (params === undefined) {\n return null\n }\n if (Array.isArray(params) || isObject(params)) {\n // ensure params can be stringify\n try {\n JSON.stringify(params)\n return null\n } catch (err) {\n return JsonRpcError.parseError(params)\n }\n }\n return JsonRpcError.invalidParams(params)\n}\n\nfunction checkError (err: JsonRpcError): JsonRpcError | null {\n if (!(err instanceof JsonRpcError)) {\n return JsonRpcError.internalError('Error must be an instance of JsonRpcError')\n }\n\n if (!isInteger(err.code)) {\n return JsonRpcError.internalError('Invalid error code. It must be an integer.')\n }\n\n if (!isString(err.message)) {\n return JsonRpcError.internalError('Message must exist or must be a string.')\n }\n\n return null\n}\n\nfunction isString (obj: any): boolean {\n return obj !== '' && typeof obj === 'string'\n}\n\nfunction isObject (obj: any): boolean {\n return obj != null && typeof obj === 'object' && !Array.isArray(obj)\n}\n\nconst jsonRpcGlobal = {\n JsonRpc,\n JsonRpcError,\n request,\n notification,\n success,\n error,\n parse,\n parseObject,\n parseJsonRpcObject,\n parseJsonRpcString,\n}\n\nexport default jsonRpcGlobal\nexport const jsonrpc: typeof jsonRpcGlobal = jsonRpcGlobal\n\ndeclare global {\n const jsonrpc: typeof jsonRpcGlobal\n\n interface Window { jsonrpc: typeof jsonRpcGlobal }\n}\n\n((global || window) as any).jsonrpc = jsonRpcGlobal // tslint:disable-line\n"],"names":["const","hasOwnProperty","Object","prototype","isInteger","Number","isSafeInteger","num","isFinite","Math","floor","abs","JsonRpc","constructor","jsonrpc","serialize","JSON","stringify","this","RequestObject","id","method","params","undefined","NotificationObject","SuccessObject","result","ErrorObject","error","JsonRpcParsed","payload","type","JsonRpcError","message","code","data","request","object","validateMessage","notification","success","err","parse","isString","invalidRequest","jsonrpcObj","parseError","parseJsonRpcObject","Array","isArray","parseObject","length","parsedObjectArray","i","len","parseJsonRpcString","obj","payloadType","VERSION","call","tmp","internalError","errorObj","throwIt","checkId","checkMethod","checkParams","checkError","maybeNull","invalidParams","jsonRpcGlobal","global","window"],"mappings":"AAUAA,IAAMC,EAAiBC,OAAOC,UAAUF,eAClCG,EAAsE,mBAAzBC,OAAOC,cACtDD,OAAOC,cACP,SAAUC,SACY,iBAARA,GAAoBC,SAASD,IAAQA,IAAQE,KAAKC,MAAMH,IAAQE,KAAKE,IAAIJ,IAAQ,kBAatFK,EAIXC,gBACOC,QAAU,OAGjBC,YAAAA,4BACSC,KAAKC,UAAUC,OARjBN,UAAkB,UAadO,cAIXN,WAAaO,EAAQC,EAAgBC,qBAE9BF,GAAKA,OACLC,OAASA,OACCE,IAAXD,SACGA,OAASA,0FATeV,GActBY,cAGXX,WAAaQ,EAAgBC,qBAEtBD,OAASA,OACCE,IAAXD,SACGA,OAASA,0FAPoBV,GAY3Ba,cAGXZ,WAAaO,EAAQM,qBAEdN,GAAKA,OACLM,OAASA,yFANiBd,GAUtBe,cAEXd,WAAoBO,EAAeQ,wBAAfR,aAAeQ,OAE5BR,GAAKA,OACLQ,MAAQA,yFALgBhB,GAwBpBiB,EACXhB,SACSiB,EACAC,gBADAD,YACAC,OAEFD,QAAUA,OACVC,KAAOA,GAYHC,EAwBXnB,SAAaoB,EAAiBC,EAAcC,QACrCF,QAAUA,OACVC,KAAO9B,EAAU8B,GAAQA,EAAO,EACzB,MAARC,SACGA,KAAOA,IAclB,SAAgBC,EACdhB,EACAC,EACAC,OAEMe,EAAS,IAAIlB,EAAcC,EAAIC,EAAQC,UAC7CgB,EAAgBD,GAAQ,GACjBA,WAWOE,EACdlB,EACAC,OAEMe,EAAS,IAAIb,EAAmBH,EAAQC,UAC9CgB,EAAgBD,GAAQ,GACjBA,WAWOG,EAASpB,EAAQM,OACzBW,EAAS,IAAIZ,EAAcL,EAA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AAkB,MAAPtB,QACPA,SAEDA,EAGT,SAASuB,EAAS5C,EAAQgD,UACpBA,GAAoB,OAAPhD,EACR,KAEFuB,EAASvB,IAAOhB,EAAUgB,GAC7B,KACAY,EAAa6B,cAAc,kDAGjC,SAASI,EAAa5C,UACbsB,EAAStB,GAAU,KAAOW,EAAaY,eAAevB,GAS/D,SAAS6C,EAAa5C,WACLC,IAAXD,SACK,QAEL0B,MAAMC,QAAQ3B,IAiCJ,OADGkC,EAhCqBlC,IAiCD,iBAARkC,IAAqBR,MAAMC,QAAQO,cA9B5DxC,KAAKC,UAAUK,GACR,KACP,MAAOmB,UACAT,EAAac,WAAWxB,GA0BrC,IAAmBkC,SAvBVxB,EAAaqC,cAAc/C,GAmBpC,SAASqB,EAAUa,SACF,KAARA,GAA6B,iBAARA,EAO9BxD,IAAMsE,EAAgB,SACpB1D,eACAoB,UACAI,eACAG,UACAC,QACAZ,QACAc,cACAQ,qBACAH,qBACAQ,GAIWzC,EAAgCwD,GAQ3CC,QAAUC,QAAgB1D,QAAUwD"} \ No newline at end of file +{"version":3,"file":"jsonrpc.mjs","sources":["jsonrpc.ts"],"sourcesContent":["// **Github:** https://github.com/teambition/jsonrpc-lite\n//\n// http://www.jsonrpc.org/specification\n// **License:** MIT\n'use strict'\n\nexport type ID = string | number\nexport type Defined = string | number | boolean | object | null\nexport type RpcParams = object | Defined[]\n\nconst hasOwnProperty = Object.prototype.hasOwnProperty\nconst isInteger: (num: number) => boolean = typeof Number.isSafeInteger === 'function'\n ? Number.isSafeInteger // ECMAScript 2015\n : function (num) {\n return typeof num === 'number' && isFinite(num) && num === Math.floor(num) && Math.abs(num) <= 9007199254740991\n }\n\n/**\n * JsonRpc Class\n *\n * @return {Object} JsonRpc object\n * @api public\n */\nexport interface IJsonRpcType {\n readonly jsonrpc: string\n}\n\nexport class JsonRpc implements IJsonRpcType {\n static VERSION: string = '2.0'\n readonly jsonrpc: string\n\n constructor () {\n this.jsonrpc = '2.0'\n }\n\n serialize () {\n return JSON.stringify(this)\n }\n\n}\n\nexport class RequestObject extends JsonRpc {\n public id: ID\n public method: string\n public params?: RpcParams\n constructor (id: ID, method: string, params?: RpcParams) {\n super()\n this.id = id\n this.method = method\n if (params !== undefined ) {\n this.params = params\n }\n }\n}\n\nexport class NotificationObject extends JsonRpc {\n public method: string\n public params?: RpcParams\n constructor (method: string, params?: RpcParams) {\n super()\n this.method = method\n if (params !== undefined ) {\n this.params = params\n }\n }\n}\n\nexport class SuccessObject extends JsonRpc {\n public id: ID\n public result: Defined\n constructor (id: ID, result: Defined) {\n super()\n this.id = id\n this.result = result\n }\n}\n\nexport class ErrorObject extends JsonRpc {\n // tslint:disable-next-line:no-shadowed-variable\n constructor (public id: ID, public error: JsonRpcError) {\n super()\n this.id = id\n this.error = error\n }\n}\n\n/**\n * JsonRpcParsed Class\n *\n * @param {JsonRpc|JsonRpcError} payload\n * @param {type: } type\n * @api public\n */\nexport const enum RpcStatusType {\n request = 'request',\n notification = 'notification',\n success = 'success',\n error = 'error',\n invalid = 'invalid',\n}\n\nexport class JsonRpcParsed {\n constructor (\n public payload: JsonRpc | JsonRpcError,\n public type: RpcStatusType,\n ) {\n this.payload = payload\n this.type = type\n }\n}\n\n/**\n * JsonRpcError Class\n *\n * @param {String} message\n * @param {Integer} code\n * @return {String} name: optional\n * @api public\n */\nexport class JsonRpcError {\n static invalidRequest = function (data: any): JsonRpcError {\n return new JsonRpcError('Invalid request', -32600, data)\n }\n\n static methodNotFound = function (data: any): JsonRpcError {\n return new JsonRpcError('Method not found', -32601, data)\n }\n\n static invalidParams = function (data: any): JsonRpcError {\n return new JsonRpcError('Invalid params', -32602, data)\n }\n\n static internalError = function (data: any): JsonRpcError {\n return new JsonRpcError('Internal error', -32603, data)\n }\n\n static parseError = function (data: any): JsonRpcError {\n return new JsonRpcError('Parse error', -32700, data)\n }\n\n public message: string\n public code: number\n public data?: any\n constructor (message: string, code: number, data?: any) {\n this.message = message\n this.code = isInteger(code) ? code : 0\n if (data != null ) {\n this.data = data\n }\n }\n}\n\n/**\n * Creates a JSON-RPC 2.0 request object\n *\n * @param {String|Integer} id\n * @param {String} method\n * @param {Object|Array} [params]: optional\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function request (\n id: ID,\n method: string,\n params?: RpcParams,\n): RequestObject {\n const object = new RequestObject(id, method, params)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 notification object\n *\n * @param {String} method\n * @param {Object|Array} [params]: optional\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function notification (\n method: string,\n params?: RpcParams,\n): NotificationObject {\n const object = new NotificationObject(method, params)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 success response object\n *\n * @param {String|Integer} id\n * @param {Mixed} result\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function success (id: ID, result: Defined): SuccessObject {\n const object = new SuccessObject(id, result)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 error response object\n *\n * @param {String|Integer} id\n * @param {Object} JsonRpcError error\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function error (id: ID, err: JsonRpcError): ErrorObject {\n const object = new ErrorObject(id, err)\n validateMessage(object, true)\n return object\n}\n\nexport interface IParsedObjectSuccess {\n type: RpcStatusType.success,\n payload: SuccessObject\n}\n\nexport interface IParsedObjectNotification {\n type: RpcStatusType.notification,\n payload: NotificationObject\n}\n\nexport interface IParsedObjectRequest {\n type: RpcStatusType.request,\n payload: RequestObject\n}\n\nexport interface IParsedObjectError {\n type: RpcStatusType.error,\n payload: ErrorObject\n}\n\nexport interface IParsedObjectInvalid {\n type: RpcStatusType.invalid,\n payload: JsonRpcError\n}\n\n/**\n * Takes a JSON-RPC 2.0 payload (String) and tries to parse it into a JSON.\n * If successful, determine what object is it (response, notification,\n * success, error, or invalid), and return it's type and properly formatted object.\n *\n * @param {String} msg\n * @return {Object|Array} an array, or an object of this format:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport type IParsedObject = IParsedObjectSuccess | IParsedObjectNotification |\n IParsedObjectRequest | IParsedObjectError| IParsedObjectInvalid;\n\nexport function parse (\n message: string,\n): IParsedObject | IParsedObject[] {\n if (!isString(message)) {\n return new JsonRpcParsed(\n JsonRpcError.invalidRequest(message),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n let jsonrpcObj: JsonRpc | JsonRpc[]\n try {\n jsonrpcObj = JSON.parse(message)\n } catch (err) {\n return new JsonRpcParsed(\n JsonRpcError.parseError(message),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n return parseJsonRpcObject(jsonrpcObj)\n}\n\n/**\n * Takes a JSON-RPC 2.0 payload (Object) or batch (Object[]) and tries to parse it.\n * If successful, determine what objects are inside (response, notification,\n * success, error, or invalid), and return their types and properly formatted objects.\n *\n * @param {Object|Array} jsonrpcObj\n * @return {Object|Array} a single object or an array of `JsonRpcParsed` objects with `type` and `payload`:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport function parseJsonRpcObject (jsonrpcObj: JsonRpc | JsonRpc[]): IParsedObject | IParsedObject[] {\n if (!Array.isArray(jsonrpcObj)) {\n return parseObject(jsonrpcObj)\n }\n\n if (jsonrpcObj.length === 0) {\n return new JsonRpcParsed(\n JsonRpcError.invalidRequest(jsonrpcObj),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n const parsedObjectArray: IParsedObject[] = []\n for (let i = 0, len = jsonrpcObj.length; i < len; i++) {\n parsedObjectArray[i] = parseObject(jsonrpcObj[i])\n }\n\n return parsedObjectArray\n}\n\n/**\n * Alias for `parse` method.\n * Takes a JSON-RPC 2.0 payload (String) and tries to parse it into a JSON.\n * @api public\n */\nexport const parseJsonRpcString = parse\n\n/**\n * Takes a JSON-RPC 2.0 payload (Object) and tries to parse it into a JSON.\n * If successful, determine what object is it (response, notification,\n * success, error, or invalid), and return it's type and properly formatted object.\n *\n * @param {Object} obj\n * @return {Object} an `JsonRpcParsed` object with `type` and `payload`:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport function parseObject (obj: any): IParsedObject {\n let err: JsonRpcError | null = null\n let payload: JsonRpc | JsonRpcError | null = null\n let payloadType: RpcStatusType = RpcStatusType.invalid\n\n if (obj == null || obj.jsonrpc !== JsonRpc.VERSION) {\n err = JsonRpcError.invalidRequest(obj)\n payloadType = RpcStatusType.invalid\n } else if (!hasOwnProperty.call(obj, 'id')) {\n const tmp = obj as NotificationObject\n payload = new NotificationObject(tmp.method, tmp.params)\n err = validateMessage(payload)\n payloadType = RpcStatusType.notification\n } else if (hasOwnProperty.call(obj, 'method')) {\n const tmp = obj as RequestObject\n payload = new RequestObject(tmp.id, tmp.method, tmp.params)\n err = validateMessage(payload)\n payloadType = RpcStatusType.request\n } else if (hasOwnProperty.call(obj, 'result')) {\n const tmp = obj as SuccessObject\n payload = new SuccessObject(tmp.id, tmp.result)\n err = validateMessage(payload)\n payloadType = RpcStatusType.success\n } else if (hasOwnProperty.call(obj, 'error')) {\n const tmp = obj as ErrorObject\n payloadType = RpcStatusType.error\n if (tmp.error == null) {\n err = JsonRpcError.internalError(tmp)\n } else {\n const errorObj = new JsonRpcError(\n tmp.error.message,\n tmp.error.code,\n tmp.error.data,\n )\n if (errorObj.message !== tmp.error.message || errorObj.code !== tmp.error.code) {\n err = JsonRpcError.internalError(tmp)\n } else {\n payload = new ErrorObject(tmp.id, errorObj)\n err = validateMessage(payload)\n }\n }\n }\n\n if (err == null && payload != null) {\n return new JsonRpcParsed(payload, payloadType) as IParsedObject\n }\n return new JsonRpcParsed(\n err != null ? err : JsonRpcError.invalidRequest(obj),\n RpcStatusType.invalid,\n ) as IParsedObject\n}\n\n// if error, return error, else return null\nfunction validateMessage (obj: JsonRpc, throwIt?: boolean): JsonRpcError | null {\n let err: JsonRpcError | null = null\n if (obj instanceof RequestObject) {\n err = checkId(obj.id)\n if (err == null) {\n err = checkMethod(obj.method)\n }\n if (err == null) {\n err = checkParams(obj.params)\n }\n } else if (obj instanceof NotificationObject) {\n err = checkMethod(obj.method)\n if (err == null) {\n err = checkParams(obj.params)\n }\n } else if (obj instanceof SuccessObject) {\n err = checkId(obj.id)\n if (err == null) {\n err = checkResult(obj.result)\n }\n } else if (obj instanceof ErrorObject) {\n err = checkId(obj.id, true)\n if (err == null) {\n err = checkError(obj.error as JsonRpcError)\n }\n }\n if (throwIt && err != null) {\n throw err\n }\n return err\n}\n\nfunction checkId (id: ID, maybeNull?: boolean): JsonRpcError | null {\n if (maybeNull && id === null) {\n return null\n }\n return isString(id) || isInteger(id as number)\n ? null\n : JsonRpcError.internalError('\"id\" must be provided, a string or an integer.')\n}\n\nfunction checkMethod (method: string): JsonRpcError | null {\n return isString(method) ? null : JsonRpcError.invalidRequest(method)\n}\n\nfunction checkResult (result: Defined): JsonRpcError | null {\n return result === undefined\n ? JsonRpcError.internalError('Result must exist for success Response objects')\n : null\n}\n\nfunction checkParams (params?: RpcParams): JsonRpcError | null {\n if (params === undefined) {\n return null\n }\n if (Array.isArray(params) || isObject(params)) {\n // ensure params can be stringify\n try {\n JSON.stringify(params)\n return null\n } catch (err) {\n return JsonRpcError.parseError(params)\n }\n }\n return JsonRpcError.invalidParams(params)\n}\n\nfunction checkError (err: JsonRpcError): JsonRpcError | null {\n if (!(err instanceof JsonRpcError)) {\n return JsonRpcError.internalError('Error must be an instance of JsonRpcError')\n }\n\n if (!isInteger(err.code)) {\n return JsonRpcError.internalError('Invalid error code. It must be an integer.')\n }\n\n if (!isString(err.message)) {\n return JsonRpcError.internalError('Message must exist or must be a string.')\n }\n\n return null\n}\n\nfunction isString (obj: any): boolean {\n return obj !== '' && typeof obj === 'string'\n}\n\nfunction isObject (obj: any): boolean {\n return obj != null && typeof obj === 'object' && !Array.isArray(obj)\n}\n\nexport const jsonrpc = {\n JsonRpc,\n JsonRpcError,\n request,\n notification,\n success,\n error,\n parse,\n parseObject,\n parseJsonRpcObject,\n parseJsonRpcString,\n}\n\nexport default jsonrpc\n"],"names":["const","hasOwnProperty","Object","prototype","isInteger","Number","isSafeInteger","num","isFinite","Math","floor","abs","JsonRpc","constructor","jsonrpc","serialize","JSON","stringify","this","RequestObject","id","method","params","undefined","NotificationObject","SuccessObject","result","ErrorObject","error","JsonRpcParsed","payload","type","JsonRpcError","message","code","data","request","object","validateMessage","notification","success","err","parse","isString","invalidRequest","jsonrpcObj","parseError","parseJsonRpcObject","Array","isArray","parseObject","length","parsedObjectArray","i","len","parseJsonRpcString","obj","payloadType","VERSION","call","tmp","internalError","errorObj","throwIt","checkId","checkMethod","checkParams","checkError","maybeNull","invalidParams"],"mappings":"AAUAA,IAAMC,EAAiBC,OAAOC,UAAUF,eAClCG,EAAsE,mBAAzBC,OAAOC,cACtDD,OAAOC,cACP,SAAUC,SACY,iBAARA,GAAoBC,SAASD,IAAQA,IAAQE,KAAKC,MAAMH,IAAQE,KAAKE,IAAIJ,IAAQ,kBAatFK,EAIXC,gBACOC,QAAU,OAGjBC,YAAAA,4BACSC,KAAKC,UAAUC,OARjBN,UAAkB,UAadO,cAIXN,WAAaO,EAAQC,EAAgBC,qBAE9BF,GAAKA,OACLC,OAASA,OACCE,IAAXD,SACGA,OAASA,0FATeV,GActBY,cAGXX,WAAaQ,EAAgBC,qBAEtBD,OAASA,OACCE,IAAXD,SACGA,OAASA,0FAPoBV,GAY3Ba,cAGXZ,WAAaO,EAAQM,qBAEdN,GAAKA,OACLM,OAASA,yFANiBd,GAUtBe,cAEXd,WAAoBO,EAAeQ,wBAAfR,aAAeQ,OAE5BR,GAAKA,OACLQ,MAAQA,yFALgBhB,GAwBpBiB,EACXhB,SACSiB,EACAC,gBADAD,YACAC,OAEFD,QAAUA,OACVC,KAAOA,GAYHC,EAwBXnB,SAAaoB,EAAiBC,EAAcC,QACrCF,QAAUA,OACVC,KAAO9B,EAAU8B,GAAQA,EAAO,EACzB,MAARC,SACGA,KAAOA,IAclB,SAAgBC,EACdhB,EACAC,EACAC,OAEMe,EAAS,IAAIlB,EAAcC,E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4CN,SAAqBA,UACbA,aAAeT,EAIhB5B,EAAUqC,EAAIP,MAIdS,EAASF,EAAIR,SAIX,KAHED,EAAa6B,cAAc,2CAJ3B7B,EAAa6B,cAAc,8CAJ3B7B,EAAa6B,cAAc,6CA9C1BM,CAAWX,EAAI5B,QAGrBmC,GAAkB,MAAPtB,QACPA,SAEDA,EAGT,SAASuB,EAAS5C,EAAQgD,UACpBA,GAAoB,OAAPhD,EACR,KAEFuB,EAASvB,IAAOhB,EAAUgB,GAC7B,KACAY,EAAa6B,cAAc,kDAGjC,SAASI,EAAa5C,UACbsB,EAAStB,GAAU,KAAOW,EAAaY,eAAevB,GAS/D,SAAS6C,EAAa5C,WACLC,IAAXD,SACK,QAEL0B,MAAMC,QAAQ3B,IAiCJ,OADGkC,EAhCqBlC,IAiCD,iBAARkC,IAAqBR,MAAMC,QAAQO,cA9B5DxC,KAAKC,UAAUK,GACR,KACP,MAAOmB,UACAT,EAAac,WAAWxB,GA0BrC,IAAmBkC,SAvBVxB,EAAaqC,cAAc/C,GAmBpC,SAASqB,EAAUa,SACF,KAARA,GAA6B,iBAARA,EAO9B,IAAa1C,EAAU,SACrBF,eACAoB,UACAI,eACAG,UACAC,QACAZ,QACAc,cACAQ,qBACAH,qBA1KgCL"} \ No newline at end of file diff --git a/jsonrpc.ts b/jsonrpc.ts index dba9304..ab175c0 100644 --- a/jsonrpc.ts +++ b/jsonrpc.ts @@ -481,7 +481,7 @@ function isObject (obj: any): boolean { return obj != null && typeof obj === 'object' && !Array.isArray(obj) } -const jsonRpcGlobal = { +export const jsonrpc = { JsonRpc, JsonRpcError, request, @@ -494,13 +494,4 @@ const jsonRpcGlobal = { parseJsonRpcString, } -export default jsonRpcGlobal -export const jsonrpc: typeof jsonRpcGlobal = jsonRpcGlobal - -declare global { - const jsonrpc: typeof jsonRpcGlobal - - interface Window { jsonrpc: typeof jsonRpcGlobal } -} - -((global || window) as any).jsonrpc = jsonRpcGlobal // tslint:disable-line +export default jsonrpc diff --git a/jsonrpc.umd.js b/jsonrpc.umd.js index 9f3aec7..60e691f 100644 --- a/jsonrpc.umd.js +++ b/jsonrpc.umd.js @@ -1,2 +1,2 @@ -!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(r.jsonrpcLite={})}(this,function(r){var e=Object.prototype.hasOwnProperty,n="function"==typeof Number.isSafeInteger?Number.isSafeInteger:function(r){return"number"==typeof r&&isFinite(r)&&r===Math.floor(r)&&Math.abs(r)<=9007199254740991},t=function(){this.jsonrpc="2.0"};t.prototype.serialize=function(){return JSON.stringify(this)},t.VERSION="2.0";var o=function(r){function e(e,n,t){r.call(this),this.id=e,this.method=n,void 0!==t&&(this.params=t)}return r&&(e.__proto__=r),(e.prototype=Object.create(r&&r.prototype)).constructor=e,e}(t),i=function(r){function e(e,n){r.call(this),this.method=e,void 0!==n&&(this.params=n)}return r&&(e.__proto__=r),(e.prototype=Object.create(r&&r.prototype)).constructor=e,e}(t),s=function(r){function e(e,n){r.call(this),this.id=e,this.result=n}return r&&(e.__proto__=r),(e.prototype=Object.create(r&&r.prototype)).constructor=e,e}(t),a=function(r){function e(e,n){r.call(this),this.id=e,this.error=n,this.id=e,this.error=n}return r&&(e.__proto__=r),(e.prototype=Object.create(r&&r.prototype)).constructor=e,e}(t),u=function(r,e){this.payload=r,this.type=e,this.payload=r,this.type=e},c=function(r,e,t){this.message=r,this.code=n(e)?e:0,null!=t&&(this.data=t)};function l(r,e,n){var t=new o(r,e,n);return b(t,!0),t}function f(r,e){var n=new i(r,e);return b(n,!0),n}function p(r,e){var n=new s(r,e);return b(n,!0),n}function d(r,e){var n=new a(r,e);return b(n,!0),n}function h(r){if(!g(r))return new u(c.invalidRequest(r),"invalid");var e;try{e=JSON.parse(r)}catch(e){return new u(c.parseError(r),"invalid")}return v(e)}function v(r){if(!Array.isArray(r))return y(r);if(0===r.length)return new u(c.invalidRequest(r),"invalid");for(var e=[],n=0,t=r.length;n boolean = typeof Number.isSafeInteger === 'function'\n ? Number.isSafeInteger // ECMAScript 2015\n : function (num) {\n return typeof num === 'number' && isFinite(num) && num === Math.floor(num) && Math.abs(num) <= 9007199254740991\n }\n\n/**\n * JsonRpc Class\n *\n * @return {Object} JsonRpc object\n * @api public\n */\nexport interface IJsonRpcType {\n readonly jsonrpc: string\n}\n\nexport class JsonRpc implements IJsonRpcType {\n static VERSION: string = '2.0'\n readonly jsonrpc: string\n\n constructor () {\n this.jsonrpc = '2.0'\n }\n\n serialize () {\n return JSON.stringify(this)\n }\n\n}\n\nexport class RequestObject extends JsonRpc {\n public id: ID\n public method: string\n public params?: RpcParams\n constructor (id: ID, method: string, params?: RpcParams) {\n super()\n this.id = id\n this.method = method\n if (params !== undefined ) {\n this.params = params\n }\n }\n}\n\nexport class NotificationObject extends JsonRpc {\n public method: string\n public params?: RpcParams\n constructor (method: string, params?: RpcParams) {\n super()\n this.method = method\n if (params !== undefined ) {\n this.params = params\n }\n }\n}\n\nexport class SuccessObject extends JsonRpc {\n public id: ID\n public result: Defined\n constructor (id: ID, result: Defined) {\n super()\n this.id = id\n this.result = result\n }\n}\n\nexport class ErrorObject extends JsonRpc {\n // tslint:disable-next-line:no-shadowed-variable\n constructor (public id: ID, public error: JsonRpcError) {\n super()\n this.id = id\n this.error = error\n }\n}\n\n/**\n * JsonRpcParsed Class\n *\n * @param {JsonRpc|JsonRpcError} payload\n * @param {type: } type\n * @api public\n */\nexport const enum RpcStatusType {\n request = 'request',\n notification = 'notification',\n success = 'success',\n error = 'error',\n invalid = 'invalid',\n}\n\nexport class JsonRpcParsed {\n constructor (\n public payload: JsonRpc | JsonRpcError,\n public type: RpcStatusType,\n ) {\n this.payload = payload\n this.type = type\n }\n}\n\n/**\n * JsonRpcError Class\n *\n * @param {String} message\n * @param {Integer} code\n * @return {String} name: optional\n * @api public\n */\nexport class JsonRpcError {\n static invalidRequest = function (data: any): JsonRpcError {\n return new JsonRpcError('Invalid request', -32600, data)\n }\n\n static methodNotFound = function (data: any): JsonRpcError {\n return new JsonRpcError('Method not found', -32601, data)\n }\n\n static invalidParams = function (data: any): JsonRpcError {\n return new JsonRpcError('Invalid params', -32602, data)\n }\n\n static internalError = function (data: any): JsonRpcError {\n return new JsonRpcError('Internal error', -32603, data)\n }\n\n static parseError = function (data: any): JsonRpcError {\n return new JsonRpcError('Parse error', -32700, data)\n }\n\n public message: string\n public code: number\n public data?: any\n constructor (message: string, code: number, data?: any) {\n this.message = message\n this.code = isInteger(code) ? code : 0\n if (data != null ) {\n this.data = data\n }\n }\n}\n\n/**\n * Creates a JSON-RPC 2.0 request object\n *\n * @param {String|Integer} id\n * @param {String} method\n * @param {Object|Array} [params]: optional\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function request (\n id: ID,\n method: string,\n params?: RpcParams,\n): RequestObject {\n const object = new RequestObject(id, method, params)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 notification object\n *\n * @param {String} method\n * @param {Object|Array} [params]: optional\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function notification (\n method: string,\n params?: RpcParams,\n): NotificationObject {\n const object = new NotificationObject(method, params)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 success response object\n *\n * @param {String|Integer} id\n * @param {Mixed} result\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function success (id: ID, result: Defined): SuccessObject {\n const object = new SuccessObject(id, result)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 error response object\n *\n * @param {String|Integer} id\n * @param {Object} JsonRpcError error\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function error (id: ID, err: JsonRpcError): ErrorObject {\n const object = new ErrorObject(id, err)\n validateMessage(object, true)\n return object\n}\n\nexport interface IParsedObjectSuccess {\n type: RpcStatusType.success,\n payload: SuccessObject\n}\n\nexport interface IParsedObjectNotification {\n type: RpcStatusType.notification,\n payload: NotificationObject\n}\n\nexport interface IParsedObjectRequest {\n type: RpcStatusType.request,\n payload: RequestObject\n}\n\nexport interface IParsedObjectError {\n type: RpcStatusType.error,\n payload: ErrorObject\n}\n\nexport interface IParsedObjectInvalid {\n type: RpcStatusType.invalid,\n payload: JsonRpcError\n}\n\n/**\n * Takes a JSON-RPC 2.0 payload (String) and tries to parse it into a JSON.\n * If successful, determine what object is it (response, notification,\n * success, error, or invalid), and return it's type and properly formatted object.\n *\n * @param {String} msg\n * @return {Object|Array} an array, or an object of this format:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport type IParsedObject = IParsedObjectSuccess | IParsedObjectNotification |\n IParsedObjectRequest | IParsedObjectError| IParsedObjectInvalid;\n\nexport function parse (\n message: string,\n): IParsedObject | IParsedObject[] {\n if (!isString(message)) {\n return new JsonRpcParsed(\n JsonRpcError.invalidRequest(message),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n let jsonrpcObj: JsonRpc | JsonRpc[]\n try {\n jsonrpcObj = JSON.parse(message)\n } catch (err) {\n return new JsonRpcParsed(\n JsonRpcError.parseError(message),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n return parseJsonRpcObject(jsonrpcObj)\n}\n\n/**\n * Takes a JSON-RPC 2.0 payload (Object) or batch (Object[]) and tries to parse it.\n * If successful, determine what objects are inside (response, notification,\n * success, error, or invalid), and return their types and properly formatted objects.\n *\n * @param {Object|Array} jsonrpcObj\n * @return {Object|Array} a single object or an array of `JsonRpcParsed` objects with `type` and `payload`:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport function parseJsonRpcObject (jsonrpcObj: JsonRpc | JsonRpc[]): IParsedObject | IParsedObject[] {\n if (!Array.isArray(jsonrpcObj)) {\n return parseObject(jsonrpcObj)\n }\n\n if (jsonrpcObj.length === 0) {\n return new JsonRpcParsed(\n JsonRpcError.invalidRequest(jsonrpcObj),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n const parsedObjectArray: IParsedObject[] = []\n for (let i = 0, len = jsonrpcObj.length; i < len; i++) {\n parsedObjectArray[i] = parseObject(jsonrpcObj[i])\n }\n\n return parsedObjectArray\n}\n\n/**\n * Alias for `parse` method.\n * Takes a JSON-RPC 2.0 payload (String) and tries to parse it into a JSON.\n * @api public\n */\nexport const parseJsonRpcString = parse\n\n/**\n * Takes a JSON-RPC 2.0 payload (Object) and tries to parse it into a JSON.\n * If successful, determine what object is it (response, notification,\n * success, error, or invalid), and return it's type and properly formatted object.\n *\n * @param {Object} obj\n * @return {Object} an `JsonRpcParsed` object with `type` and `payload`:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport function parseObject (obj: any): IParsedObject {\n let err: JsonRpcError | null = null\n let payload: JsonRpc | JsonRpcError | null = null\n let payloadType: RpcStatusType = RpcStatusType.invalid\n\n if (obj == null || obj.jsonrpc !== JsonRpc.VERSION) {\n err = JsonRpcError.invalidRequest(obj)\n payloadType = RpcStatusType.invalid\n } else if (!hasOwnProperty.call(obj, 'id')) {\n const tmp = obj as NotificationObject\n payload = new NotificationObject(tmp.method, tmp.params)\n err = validateMessage(payload)\n payloadType = RpcStatusType.notification\n } else if (hasOwnProperty.call(obj, 'method')) {\n const tmp = obj as RequestObject\n payload = new RequestObject(tmp.id, tmp.method, tmp.params)\n err = validateMessage(payload)\n payloadType = RpcStatusType.request\n } else if (hasOwnProperty.call(obj, 'result')) {\n const tmp = obj as SuccessObject\n payload = new SuccessObject(tmp.id, tmp.result)\n err = validateMessage(payload)\n payloadType = RpcStatusType.success\n } else if (hasOwnProperty.call(obj, 'error')) {\n const tmp = obj as ErrorObject\n payloadType = RpcStatusType.error\n if (tmp.error == null) {\n err = JsonRpcError.internalError(tmp)\n } else {\n const errorObj = new JsonRpcError(\n tmp.error.message,\n tmp.error.code,\n tmp.error.data,\n )\n if (errorObj.message !== tmp.error.message || errorObj.code !== tmp.error.code) {\n err = JsonRpcError.internalError(tmp)\n } else {\n payload = new ErrorObject(tmp.id, errorObj)\n err = validateMessage(payload)\n }\n }\n }\n\n if (err == null && payload != null) {\n return new JsonRpcParsed(payload, payloadType) as IParsedObject\n }\n return new JsonRpcParsed(\n err != null ? err : JsonRpcError.invalidRequest(obj),\n RpcStatusType.invalid,\n ) as IParsedObject\n}\n\n// if error, return error, else return null\nfunction validateMessage (obj: JsonRpc, throwIt?: boolean): JsonRpcError | null {\n let err: JsonRpcError | null = null\n if (obj instanceof RequestObject) {\n err = checkId(obj.id)\n if (err == null) {\n err = checkMethod(obj.method)\n }\n if (err == null) {\n err = checkParams(obj.params)\n }\n } else if (obj instanceof NotificationObject) {\n err = checkMethod(obj.method)\n if (err == null) {\n err = checkParams(obj.params)\n }\n } else if (obj instanceof SuccessObject) {\n err = checkId(obj.id)\n if (err == null) {\n err = checkResult(obj.result)\n }\n } else if (obj instanceof ErrorObject) {\n err = checkId(obj.id, true)\n if (err == null) {\n err = checkError(obj.error as JsonRpcError)\n }\n }\n if (throwIt && err != null) {\n throw err\n }\n return err\n}\n\nfunction checkId (id: ID, maybeNull?: boolean): JsonRpcError | null {\n if (maybeNull && id === null) {\n return null\n }\n return isString(id) || isInteger(id as number)\n ? null\n : JsonRpcError.internalError('\"id\" must be provided, a string or an integer.')\n}\n\nfunction checkMethod (method: string): JsonRpcError | null {\n return isString(method) ? null : JsonRpcError.invalidRequest(method)\n}\n\nfunction checkResult (result: Defined): JsonRpcError | null {\n return result === undefined\n ? JsonRpcError.internalError('Result must exist for success Response objects')\n : null\n}\n\nfunction checkParams (params?: RpcParams): JsonRpcError | null {\n if (params === undefined) {\n return null\n }\n if (Array.isArray(params) || isObject(params)) {\n // ensure params can be stringify\n try {\n JSON.stringify(params)\n return null\n } catch (err) {\n return JsonRpcError.parseError(params)\n }\n }\n return JsonRpcError.invalidParams(params)\n}\n\nfunction checkError (err: JsonRpcError): JsonRpcError | null {\n if (!(err instanceof JsonRpcError)) {\n return JsonRpcError.internalError('Error must be an instance of JsonRpcError')\n }\n\n if (!isInteger(err.code)) {\n return JsonRpcError.internalError('Invalid error code. It must be an integer.')\n }\n\n if (!isString(err.message)) {\n return JsonRpcError.internalError('Message must exist or must be a string.')\n }\n\n return null\n}\n\nfunction isString (obj: any): boolean {\n return obj !== '' && typeof obj === 'string'\n}\n\nfunction isObject (obj: any): boolean {\n return obj != null && typeof obj === 'object' && !Array.isArray(obj)\n}\n\nconst jsonRpcGlobal = {\n JsonRpc,\n JsonRpcError,\n request,\n notification,\n success,\n error,\n parse,\n parseObject,\n parseJsonRpcObject,\n parseJsonRpcString,\n}\n\nexport default jsonRpcGlobal\nexport const jsonrpc: typeof jsonRpcGlobal = jsonRpcGlobal\n\ndeclare global {\n const jsonrpc: typeof jsonRpcGlobal\n\n interface Window { jsonrpc: typeof jsonRpcGlobal }\n}\n\n((global || window) as any).jsonrpc = jsonRpcGlobal // tslint:disable-line\n"],"names":["const","hasOwnProperty","Object","prototype","isInteger","Number","isSafeInteger","num","isFinite","Math","floor","abs","JsonRpc","constructor","jsonrpc","serialize","JSON","stringify","this","RequestObject","id","method","params","undefined","NotificationObject","SuccessObject","result","ErrorObject","error","JsonRpcParsed","payload","type","JsonRpcError","message","code","data","request","object","validateMessage","notification","success","err","parse","isString","invalidRequest","jsonrpcObj","parseError","parseJsonRpcObject","Array","isArray","parseObject","length","parsedObjectArray","i","len","parseJsonRpcString","obj","payloadType","VERSION","call","tmp","internalError","errorObj","throwIt","checkId","checkMethod","checkParams","checkError","maybeNull","invalidParams","jsonRpcGlobal","global","window"],"mappings":"iLAUAA,IAAMC,EAAiBC,OAAOC,UAAUF,eAClCG,EAAsE,mBAAzBC,OAAOC,cACtDD,OAAOC,cACP,SAAUC,SACY,iBAARA,GAAoBC,SAASD,IAAQA,IAAQE,KAAKC,MAAMH,IAAQE,KAAKE,IAAIJ,IAAQ,kBAatFK,EAIXC,gBACOC,QAAU,OAGjBC,YAAAA,4BACSC,KAAKC,UAAUC,OARjBN,UAAkB,UAadO,cAIXN,WAAaO,EAAQC,EAAgBC,qBAE9BF,GAAKA,OACLC,OAASA,OACCE,IAAXD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,UACAI,eACAG,UACAC,QACAZ,QACAc,cACAQ,qBACAH,qBACAQ,GAIWzC,EAAgCwD,GAQ3CC,QAAUC,QAAgB1D,QAAUwD"} \ No newline at end of file +{"version":3,"file":"jsonrpc.umd.js","sources":["jsonrpc.ts"],"sourcesContent":["// **Github:** https://github.com/teambition/jsonrpc-lite\n//\n// http://www.jsonrpc.org/specification\n// **License:** MIT\n'use strict'\n\nexport type ID = string | number\nexport type Defined = string | number | boolean | object | null\nexport type RpcParams = object | Defined[]\n\nconst hasOwnProperty = Object.prototype.hasOwnProperty\nconst isInteger: (num: number) => boolean = typeof Number.isSafeInteger === 'function'\n ? Number.isSafeInteger // ECMAScript 2015\n : function (num) {\n return typeof num === 'number' && isFinite(num) && num === Math.floor(num) && Math.abs(num) <= 9007199254740991\n }\n\n/**\n * JsonRpc Class\n *\n * @return {Object} JsonRpc object\n * @api public\n */\nexport interface IJsonRpcType {\n readonly jsonrpc: string\n}\n\nexport class JsonRpc implements IJsonRpcType {\n static VERSION: string = '2.0'\n readonly jsonrpc: string\n\n constructor () {\n this.jsonrpc = '2.0'\n }\n\n serialize () {\n return JSON.stringify(this)\n }\n\n}\n\nexport class RequestObject extends JsonRpc {\n public id: ID\n public method: string\n public params?: RpcParams\n constructor (id: ID, method: string, params?: RpcParams) {\n super()\n this.id = id\n this.method = method\n if (params !== undefined ) {\n this.params = params\n }\n }\n}\n\nexport class NotificationObject extends JsonRpc {\n public method: string\n public params?: RpcParams\n constructor (method: string, params?: RpcParams) {\n super()\n this.method = method\n if (params !== undefined ) {\n this.params = params\n }\n }\n}\n\nexport class SuccessObject extends JsonRpc {\n public id: ID\n public result: Defined\n constructor (id: ID, result: Defined) {\n super()\n this.id = id\n this.result = result\n }\n}\n\nexport class ErrorObject extends JsonRpc {\n // tslint:disable-next-line:no-shadowed-variable\n constructor (public id: ID, public error: JsonRpcError) {\n super()\n this.id = id\n this.error = error\n }\n}\n\n/**\n * JsonRpcParsed Class\n *\n * @param {JsonRpc|JsonRpcError} payload\n * @param {type: } type\n * @api public\n */\nexport const enum RpcStatusType {\n request = 'request',\n notification = 'notification',\n success = 'success',\n error = 'error',\n invalid = 'invalid',\n}\n\nexport class JsonRpcParsed {\n constructor (\n public payload: JsonRpc | JsonRpcError,\n public type: RpcStatusType,\n ) {\n this.payload = payload\n this.type = type\n }\n}\n\n/**\n * JsonRpcError Class\n *\n * @param {String} message\n * @param {Integer} code\n * @return {String} name: optional\n * @api public\n */\nexport class JsonRpcError {\n static invalidRequest = function (data: any): JsonRpcError {\n return new JsonRpcError('Invalid request', -32600, data)\n }\n\n static methodNotFound = function (data: any): JsonRpcError {\n return new JsonRpcError('Method not found', -32601, data)\n }\n\n static invalidParams = function (data: any): JsonRpcError {\n return new JsonRpcError('Invalid params', -32602, data)\n }\n\n static internalError = function (data: any): JsonRpcError {\n return new JsonRpcError('Internal error', -32603, data)\n }\n\n static parseError = function (data: any): JsonRpcError {\n return new JsonRpcError('Parse error', -32700, data)\n }\n\n public message: string\n public code: number\n public data?: any\n constructor (message: string, code: number, data?: any) {\n this.message = message\n this.code = isInteger(code) ? code : 0\n if (data != null ) {\n this.data = data\n }\n }\n}\n\n/**\n * Creates a JSON-RPC 2.0 request object\n *\n * @param {String|Integer} id\n * @param {String} method\n * @param {Object|Array} [params]: optional\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function request (\n id: ID,\n method: string,\n params?: RpcParams,\n): RequestObject {\n const object = new RequestObject(id, method, params)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 notification object\n *\n * @param {String} method\n * @param {Object|Array} [params]: optional\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function notification (\n method: string,\n params?: RpcParams,\n): NotificationObject {\n const object = new NotificationObject(method, params)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 success response object\n *\n * @param {String|Integer} id\n * @param {Mixed} result\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function success (id: ID, result: Defined): SuccessObject {\n const object = new SuccessObject(id, result)\n validateMessage(object, true)\n return object\n}\n\n/**\n * Creates a JSON-RPC 2.0 error response object\n *\n * @param {String|Integer} id\n * @param {Object} JsonRpcError error\n * @return {Object} JsonRpc object\n * @api public\n */\nexport function error (id: ID, err: JsonRpcError): ErrorObject {\n const object = new ErrorObject(id, err)\n validateMessage(object, true)\n return object\n}\n\nexport interface IParsedObjectSuccess {\n type: RpcStatusType.success,\n payload: SuccessObject\n}\n\nexport interface IParsedObjectNotification {\n type: RpcStatusType.notification,\n payload: NotificationObject\n}\n\nexport interface IParsedObjectRequest {\n type: RpcStatusType.request,\n payload: RequestObject\n}\n\nexport interface IParsedObjectError {\n type: RpcStatusType.error,\n payload: ErrorObject\n}\n\nexport interface IParsedObjectInvalid {\n type: RpcStatusType.invalid,\n payload: JsonRpcError\n}\n\n/**\n * Takes a JSON-RPC 2.0 payload (String) and tries to parse it into a JSON.\n * If successful, determine what object is it (response, notification,\n * success, error, or invalid), and return it's type and properly formatted object.\n *\n * @param {String} msg\n * @return {Object|Array} an array, or an object of this format:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport type IParsedObject = IParsedObjectSuccess | IParsedObjectNotification |\n IParsedObjectRequest | IParsedObjectError| IParsedObjectInvalid;\n\nexport function parse (\n message: string,\n): IParsedObject | IParsedObject[] {\n if (!isString(message)) {\n return new JsonRpcParsed(\n JsonRpcError.invalidRequest(message),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n let jsonrpcObj: JsonRpc | JsonRpc[]\n try {\n jsonrpcObj = JSON.parse(message)\n } catch (err) {\n return new JsonRpcParsed(\n JsonRpcError.parseError(message),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n return parseJsonRpcObject(jsonrpcObj)\n}\n\n/**\n * Takes a JSON-RPC 2.0 payload (Object) or batch (Object[]) and tries to parse it.\n * If successful, determine what objects are inside (response, notification,\n * success, error, or invalid), and return their types and properly formatted objects.\n *\n * @param {Object|Array} jsonrpcObj\n * @return {Object|Array} a single object or an array of `JsonRpcParsed` objects with `type` and `payload`:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport function parseJsonRpcObject (jsonrpcObj: JsonRpc | JsonRpc[]): IParsedObject | IParsedObject[] {\n if (!Array.isArray(jsonrpcObj)) {\n return parseObject(jsonrpcObj)\n }\n\n if (jsonrpcObj.length === 0) {\n return new JsonRpcParsed(\n JsonRpcError.invalidRequest(jsonrpcObj),\n RpcStatusType.invalid,\n ) as IParsedObject\n }\n\n const parsedObjectArray: IParsedObject[] = []\n for (let i = 0, len = jsonrpcObj.length; i < len; i++) {\n parsedObjectArray[i] = parseObject(jsonrpcObj[i])\n }\n\n return parsedObjectArray\n}\n\n/**\n * Alias for `parse` method.\n * Takes a JSON-RPC 2.0 payload (String) and tries to parse it into a JSON.\n * @api public\n */\nexport const parseJsonRpcString = parse\n\n/**\n * Takes a JSON-RPC 2.0 payload (Object) and tries to parse it into a JSON.\n * If successful, determine what object is it (response, notification,\n * success, error, or invalid), and return it's type and properly formatted object.\n *\n * @param {Object} obj\n * @return {Object} an `JsonRpcParsed` object with `type` and `payload`:\n *\n * {\n * type: \n * payload: \n * }\n *\n * @api public\n */\nexport function parseObject (obj: any): IParsedObject {\n let err: JsonRpcError | null = null\n let payload: JsonRpc | JsonRpcError | null = null\n let payloadType: RpcStatusType = RpcStatusType.invalid\n\n if (obj == null || obj.jsonrpc !== JsonRpc.VERSION) {\n err = JsonRpcError.invalidRequest(obj)\n payloadType = RpcStatusType.invalid\n } else if (!hasOwnProperty.call(obj, 'id')) {\n const tmp = obj as NotificationObject\n payload = new NotificationObject(tmp.method, tmp.params)\n err = validateMessage(payload)\n payloadType = RpcStatusType.notification\n } else if (hasOwnProperty.call(obj, 'method')) {\n const tmp = obj as RequestObject\n payload = new RequestObject(tmp.id, tmp.method, tmp.params)\n err = validateMessage(payload)\n payloadType = RpcStatusType.request\n } else if (hasOwnProperty.call(obj, 'result')) {\n const tmp = obj as SuccessObject\n payload = new SuccessObject(tmp.id, tmp.result)\n err = validateMessage(payload)\n payloadType = RpcStatusType.success\n } else if (hasOwnProperty.call(obj, 'error')) {\n const tmp = obj as ErrorObject\n payloadType = RpcStatusType.error\n if (tmp.error == null) {\n err = JsonRpcError.internalError(tmp)\n } else {\n const errorObj = new JsonRpcError(\n tmp.error.message,\n tmp.error.code,\n tmp.error.data,\n )\n if (errorObj.message !== tmp.error.message || errorObj.code !== tmp.error.code) {\n err = JsonRpcError.internalError(tmp)\n } else {\n payload = new ErrorObject(tmp.id, errorObj)\n err = validateMessage(payload)\n }\n }\n }\n\n if (err == null && payload != null) {\n return new JsonRpcParsed(payload, payloadType) as IParsedObject\n }\n return new JsonRpcParsed(\n err != null ? err : JsonRpcError.invalidRequest(obj),\n RpcStatusType.invalid,\n ) as IParsedObject\n}\n\n// if error, return error, else return null\nfunction validateMessage (obj: JsonRpc, throwIt?: boolean): JsonRpcError | null {\n let err: JsonRpcError | null = null\n if (obj instanceof RequestObject) {\n err = checkId(obj.id)\n if (err == null) {\n err = checkMethod(obj.method)\n }\n if (err == null) {\n err = checkParams(obj.params)\n }\n } else if (obj instanceof NotificationObject) {\n err = checkMethod(obj.method)\n if (err == null) {\n err = checkParams(obj.params)\n }\n } else if (obj instanceof SuccessObject) {\n err = checkId(obj.id)\n if (err == null) {\n err = checkResult(obj.result)\n }\n } else if (obj instanceof ErrorObject) {\n err = checkId(obj.id, true)\n if (err == null) {\n err = checkError(obj.error as JsonRpcError)\n }\n }\n if (throwIt && err != null) {\n throw err\n }\n return err\n}\n\nfunction checkId (id: ID, maybeNull?: boolean): JsonRpcError | null {\n if (maybeNull && id === null) {\n return null\n }\n return isString(id) || isInteger(id as number)\n ? null\n : JsonRpcError.internalError('\"id\" must be provided, a string or an integer.')\n}\n\nfunction checkMethod (method: string): JsonRpcError | null {\n return isString(method) ? null : JsonRpcError.invalidRequest(method)\n}\n\nfunction checkResult (result: Defined): JsonRpcError | null {\n return result === undefined\n ? JsonRpcError.internalError('Result must exist for success Response objects')\n : null\n}\n\nfunction checkParams (params?: RpcParams): JsonRpcError | null {\n if (params === undefined) {\n return null\n }\n if (Array.isArray(params) || isObject(params)) {\n // ensure params can be stringify\n try {\n JSON.stringify(params)\n return null\n } catch (err) {\n return JsonRpcError.parseError(params)\n }\n }\n return JsonRpcError.invalidParams(params)\n}\n\nfunction checkError (err: JsonRpcError): JsonRpcError | null {\n if (!(err instanceof JsonRpcError)) {\n return JsonRpcError.internalError('Error must be an instance of JsonRpcError')\n }\n\n if (!isInteger(err.code)) {\n return JsonRpcError.internalError('Invalid error code. It must be an integer.')\n }\n\n if (!isString(err.message)) {\n return JsonRpcError.internalError('Message must exist or must be a string.')\n }\n\n return null\n}\n\nfunction isString (obj: any): boolean {\n return obj !== '' && typeof obj === 'string'\n}\n\nfunction isObject (obj: any): boolean {\n return obj != null && typeof obj === 'object' && !Array.isArray(obj)\n}\n\nexport const jsonrpc = {\n JsonRpc,\n JsonRpcError,\n request,\n notification,\n success,\n error,\n parse,\n parseObject,\n parseJsonRpcObject,\n parseJsonRpcString,\n}\n\nexport default jsonrpc\n"],"names":["const","hasOwnProperty","Object","prototype","isInteger","Number","isSafeInteger","num","isFinite","Math","floor","abs","JsonRpc","constructor","jsonrpc","serialize","JSON","stringify","this","RequestObject","id","method","params","undefined","NotificationObject","SuccessObject","result","ErrorObject","error","JsonRpcParsed","payload","type","JsonRpcError","message","code","data","request","object","validateMessage","notification","success","err","parse","isString","invalidRequest","jsonrpcObj","parseError","parseJsonRpcObject","Array","isArray","parseObject","length","parsedObjectArray","i","len","parseJsonRpcString","obj","payloadType","VERSION","call","tmp","internalError","errorObj","throwIt","checkId","checkMethod","checkParams","checkError","maybeNull","invalidParams"],"mappings":"0LAUAA,IAAMC,EAAiBC,OAAOC,UAAUF,eAClCG,EAAsE,mBAAzBC,OAAOC,cACtDD,OAAOC,cACP,SAAUC,SACY,iBAARA,GAAoBC,SAASD,IAAQA,IAAQE,KAAKC,MAAMH,IAA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AAKC,UAAUK,GACR,KACP,MAAOmB,UACAT,EAAac,WAAWxB,GA0BrC,IAAmBkC,SAvBVxB,EAAaqC,cAAc/C,GAmBpC,SAASqB,EAAUa,SACF,KAARA,GAA6B,iBAARA,MAOjB1C,EAAU,SACrBF,eACAoB,UACAI,eACAG,UACAC,QACAZ,QACAc,cACAQ,qBACAH,qBACAQ"} \ No newline at end of file diff --git a/package.json b/package.json index c54904b..8e5b522 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "dependencies": {}, "devDependencies": { "@types/node": "^12.11.1", + "esm": "^3.2.25", "microbundle": "^0.11.0", "tman": "^1.9.0", "ts-node": "^8.4.1", @@ -41,8 +42,8 @@ }, "scripts": { "build:tsc": "tsc -p tsconfig.build.json", - "build:rollup": "microbundle jsonrpc.ts", - "build": "yarn build:rollup", + "build:microbundle": "microbundle jsonrpc.ts --name jsonrpc --strict", + "build": "yarn build:microbundle", "lint": "tslint -p tsconfig.json -t stylish jsonrpc.ts", "test": "npm run lint && tman" }, diff --git a/test/index.js b/test/index.js index 40b9fb9..a9798c9 100644 --- a/test/index.js +++ b/test/index.js @@ -3,7 +3,9 @@ const assert = require('assert') const tman = require('tman') -test(require('../jsonrpc')) +test(require('../jsonrpc.js')) +test(require('../jsonrpc.umd')) +test(require('esm')(module)('../jsonrpc.mjs')) require('ts-node/register') test(require('../jsonrpc.ts').default) @@ -392,9 +394,5 @@ function test (jsonrpc) { tman.it('jsonrpc.parseJsonRpcString', function() { assert.strictEqual(jsonrpc.parse, jsonrpc.parseJsonRpcString) }) - - tman.it('jsonrpc exposed as global', function() { - assert.strictEqual(!!global.jsonrpc, true) - }) }) } diff --git a/yarn.lock b/yarn.lock index e09a578..2525aba 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1037,6 +1037,11 @@ escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5: resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ= +esm@^3.2.25: + version "3.2.25" + resolved "https://registry.yarnpkg.com/esm/-/esm-3.2.25.tgz#342c18c29d56157688ba5ce31f8431fbb795cc10" + integrity sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA== + esprima@^2.6.0: version "2.7.3" resolved "https://registry.yarnpkg.com/esprima/-/esprima-2.7.3.tgz#96e3b70d5779f6ad49cd032673d1c312767ba581"