@nimiq/rpc
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -315,10 +315,12 @@ 'use strict'; | ||
static prepareRedirectReply(state, status, result) { | ||
const params = new URLSearchParams(); | ||
const returnUrl = new URL(state.returnURL); | ||
const params = returnUrl.searchParams; | ||
params.set('status', status); | ||
params.set('result', JSONUtils.stringify(result)); | ||
params.set('id', state.id.toString()); | ||
return `${state.returnURL}${new URL(state.returnURL).search.length > 0 ? '&' : '?'}${params.toString()}`; | ||
return returnUrl.href; | ||
} | ||
static prepareRedirectInvocation(targetURL, id, returnURL, command, args) { | ||
const params = new URLSearchParams(); | ||
const targetUrl = new URL(targetURL); | ||
const params = targetUrl.searchParams; | ||
params.set('id', id.toString()); | ||
@@ -330,4 +332,3 @@ params.set('returnURL', returnURL); | ||
} | ||
// TODO: what if it already includes a query string | ||
return `${targetURL}?${params.toString()}`; | ||
return targetUrl.href; | ||
} | ||
@@ -359,4 +360,6 @@ } | ||
if (callback) { | ||
if (!this._preserveRequests) | ||
if (!this._preserveRequests) { | ||
this._waitingRequests.remove(data.id); | ||
this._responseHandlers.delete(data.id); | ||
} | ||
console.debug('RpcClient RECEIVE', data); | ||
@@ -421,3 +424,3 @@ if (data.status === exports.ResponseStatus.OK) { | ||
} | ||
async _call(obj) { | ||
async _call(request) { | ||
if (!this._connected) | ||
@@ -427,8 +430,9 @@ throw new Error('Client is not connected, call init first'); | ||
// Store the request resolvers | ||
this._responseHandlers.set(obj.id, { resolve, reject }); | ||
this._waitingRequests.add(obj.id, obj.command); | ||
// Periodically check if recepient window is still open | ||
this._responseHandlers.set(request.id, { resolve, reject }); | ||
this._waitingRequests.add(request.id, request.command); | ||
// Periodically check if recipient window is still open | ||
const checkIfServerWasClosed = () => { | ||
if (this._target.closed) { | ||
reject(new Error('Window was closed')); | ||
return; | ||
} | ||
@@ -438,4 +442,4 @@ setTimeout(checkIfServerWasClosed, 500); | ||
setTimeout(checkIfServerWasClosed, 500); | ||
console.debug('RpcClient REQUEST', obj.command, obj.args); | ||
this._target.postMessage(obj, this._allowedOrigin); | ||
console.debug('RpcClient REQUEST', request.command, request.args); | ||
this._target.postMessage(request, this._allowedOrigin); | ||
}); | ||
@@ -480,3 +484,3 @@ } | ||
/** | ||
* Send 'ping' command every second, until cancelled | ||
* Send 'ping' command every 100ms, until cancelled | ||
*/ | ||
@@ -494,7 +498,5 @@ const tryToConnect = () => { | ||
} | ||
// @ts-ignore | ||
connectTimer = setTimeout(tryToConnect, 100); | ||
connectTimer = window.setTimeout(tryToConnect, 100); | ||
}; | ||
// @ts-ignore | ||
connectTimer = setTimeout(tryToConnect, 100); | ||
connectTimer = window.setTimeout(tryToConnect, 100); | ||
}); | ||
@@ -504,3 +506,3 @@ } | ||
class RedirectRpcClient extends RpcClient { | ||
constructor(targetURL, allowedOrigin, preserveRequests = false) { | ||
constructor(targetURL, allowedOrigin, preserveRequests = true) { | ||
super(allowedOrigin, /*storeState*/ true); | ||
@@ -525,3 +527,3 @@ this._target = targetURL; | ||
} | ||
/* tslint:disable:no-empty */ | ||
/* tslint:disable-next-line:no-empty */ | ||
close() { } | ||
@@ -545,4 +547,6 @@ call(returnURL, command, ...args) { | ||
if (callback) { | ||
if (!this._preserveRequests) | ||
if (!this._preserveRequests) { | ||
this._waitingRequests.remove(id); | ||
this._responseHandlers.delete(id); | ||
} | ||
console.debug('RpcClient BACK'); | ||
@@ -549,0 +553,0 @@ const error = new Error('Request aborted'); |
@@ -312,10 +312,12 @@ class RandomUtils { | ||
static prepareRedirectReply(state, status, result) { | ||
const params = new URLSearchParams(); | ||
const returnUrl = new URL(state.returnURL); | ||
const params = returnUrl.searchParams; | ||
params.set('status', status); | ||
params.set('result', JSONUtils.stringify(result)); | ||
params.set('id', state.id.toString()); | ||
return `${state.returnURL}${new URL(state.returnURL).search.length > 0 ? '&' : '?'}${params.toString()}`; | ||
return returnUrl.href; | ||
} | ||
static prepareRedirectInvocation(targetURL, id, returnURL, command, args) { | ||
const params = new URLSearchParams(); | ||
const targetUrl = new URL(targetURL); | ||
const params = targetUrl.searchParams; | ||
params.set('id', id.toString()); | ||
@@ -327,4 +329,3 @@ params.set('returnURL', returnURL); | ||
} | ||
// TODO: what if it already includes a query string | ||
return `${targetURL}?${params.toString()}`; | ||
return targetUrl.href; | ||
} | ||
@@ -356,4 +357,6 @@ } | ||
if (callback) { | ||
if (!this._preserveRequests) | ||
if (!this._preserveRequests) { | ||
this._waitingRequests.remove(data.id); | ||
this._responseHandlers.delete(data.id); | ||
} | ||
console.debug('RpcClient RECEIVE', data); | ||
@@ -418,3 +421,3 @@ if (data.status === ResponseStatus.OK) { | ||
} | ||
async _call(obj) { | ||
async _call(request) { | ||
if (!this._connected) | ||
@@ -424,8 +427,9 @@ throw new Error('Client is not connected, call init first'); | ||
// Store the request resolvers | ||
this._responseHandlers.set(obj.id, { resolve, reject }); | ||
this._waitingRequests.add(obj.id, obj.command); | ||
// Periodically check if recepient window is still open | ||
this._responseHandlers.set(request.id, { resolve, reject }); | ||
this._waitingRequests.add(request.id, request.command); | ||
// Periodically check if recipient window is still open | ||
const checkIfServerWasClosed = () => { | ||
if (this._target.closed) { | ||
reject(new Error('Window was closed')); | ||
return; | ||
} | ||
@@ -435,4 +439,4 @@ setTimeout(checkIfServerWasClosed, 500); | ||
setTimeout(checkIfServerWasClosed, 500); | ||
console.debug('RpcClient REQUEST', obj.command, obj.args); | ||
this._target.postMessage(obj, this._allowedOrigin); | ||
console.debug('RpcClient REQUEST', request.command, request.args); | ||
this._target.postMessage(request, this._allowedOrigin); | ||
}); | ||
@@ -477,3 +481,3 @@ } | ||
/** | ||
* Send 'ping' command every second, until cancelled | ||
* Send 'ping' command every 100ms, until cancelled | ||
*/ | ||
@@ -491,7 +495,5 @@ const tryToConnect = () => { | ||
} | ||
// @ts-ignore | ||
connectTimer = setTimeout(tryToConnect, 100); | ||
connectTimer = window.setTimeout(tryToConnect, 100); | ||
}; | ||
// @ts-ignore | ||
connectTimer = setTimeout(tryToConnect, 100); | ||
connectTimer = window.setTimeout(tryToConnect, 100); | ||
}); | ||
@@ -501,3 +503,3 @@ } | ||
class RedirectRpcClient extends RpcClient { | ||
constructor(targetURL, allowedOrigin, preserveRequests = false) { | ||
constructor(targetURL, allowedOrigin, preserveRequests = true) { | ||
super(allowedOrigin, /*storeState*/ true); | ||
@@ -522,3 +524,3 @@ this._target = targetURL; | ||
} | ||
/* tslint:disable:no-empty */ | ||
/* tslint:disable-next-line:no-empty */ | ||
close() { } | ||
@@ -542,4 +544,6 @@ call(returnURL, command, ...args) { | ||
if (callback) { | ||
if (!this._preserveRequests) | ||
if (!this._preserveRequests) { | ||
this._waitingRequests.remove(id); | ||
this._responseHandlers.delete(id); | ||
} | ||
console.debug('RpcClient BACK'); | ||
@@ -546,0 +550,0 @@ const error = new Error('Request aborted'); |
@@ -317,10 +317,12 @@ (function (global, factory) { | ||
static prepareRedirectReply(state, status, result) { | ||
const params = new URLSearchParams(); | ||
const returnUrl = new URL(state.returnURL); | ||
const params = returnUrl.searchParams; | ||
params.set('status', status); | ||
params.set('result', JSONUtils.stringify(result)); | ||
params.set('id', state.id.toString()); | ||
return `${state.returnURL}${new URL(state.returnURL).search.length > 0 ? '&' : '?'}${params.toString()}`; | ||
return returnUrl.href; | ||
} | ||
static prepareRedirectInvocation(targetURL, id, returnURL, command, args) { | ||
const params = new URLSearchParams(); | ||
const targetUrl = new URL(targetURL); | ||
const params = targetUrl.searchParams; | ||
params.set('id', id.toString()); | ||
@@ -332,4 +334,3 @@ params.set('returnURL', returnURL); | ||
} | ||
// TODO: what if it already includes a query string | ||
return `${targetURL}?${params.toString()}`; | ||
return targetUrl.href; | ||
} | ||
@@ -361,4 +362,6 @@ } | ||
if (callback) { | ||
if (!this._preserveRequests) | ||
if (!this._preserveRequests) { | ||
this._waitingRequests.remove(data.id); | ||
this._responseHandlers.delete(data.id); | ||
} | ||
console.debug('RpcClient RECEIVE', data); | ||
@@ -423,3 +426,3 @@ if (data.status === exports.ResponseStatus.OK) { | ||
} | ||
async _call(obj) { | ||
async _call(request) { | ||
if (!this._connected) | ||
@@ -429,8 +432,9 @@ throw new Error('Client is not connected, call init first'); | ||
// Store the request resolvers | ||
this._responseHandlers.set(obj.id, { resolve, reject }); | ||
this._waitingRequests.add(obj.id, obj.command); | ||
// Periodically check if recepient window is still open | ||
this._responseHandlers.set(request.id, { resolve, reject }); | ||
this._waitingRequests.add(request.id, request.command); | ||
// Periodically check if recipient window is still open | ||
const checkIfServerWasClosed = () => { | ||
if (this._target.closed) { | ||
reject(new Error('Window was closed')); | ||
return; | ||
} | ||
@@ -440,4 +444,4 @@ setTimeout(checkIfServerWasClosed, 500); | ||
setTimeout(checkIfServerWasClosed, 500); | ||
console.debug('RpcClient REQUEST', obj.command, obj.args); | ||
this._target.postMessage(obj, this._allowedOrigin); | ||
console.debug('RpcClient REQUEST', request.command, request.args); | ||
this._target.postMessage(request, this._allowedOrigin); | ||
}); | ||
@@ -482,3 +486,3 @@ } | ||
/** | ||
* Send 'ping' command every second, until cancelled | ||
* Send 'ping' command every 100ms, until cancelled | ||
*/ | ||
@@ -496,7 +500,5 @@ const tryToConnect = () => { | ||
} | ||
// @ts-ignore | ||
connectTimer = setTimeout(tryToConnect, 100); | ||
connectTimer = window.setTimeout(tryToConnect, 100); | ||
}; | ||
// @ts-ignore | ||
connectTimer = setTimeout(tryToConnect, 100); | ||
connectTimer = window.setTimeout(tryToConnect, 100); | ||
}); | ||
@@ -506,3 +508,3 @@ } | ||
class RedirectRpcClient extends RpcClient { | ||
constructor(targetURL, allowedOrigin, preserveRequests = false) { | ||
constructor(targetURL, allowedOrigin, preserveRequests = true) { | ||
super(allowedOrigin, /*storeState*/ true); | ||
@@ -527,3 +529,3 @@ this._target = targetURL; | ||
} | ||
/* tslint:disable:no-empty */ | ||
/* tslint:disable-next-line:no-empty */ | ||
close() { } | ||
@@ -547,4 +549,6 @@ call(returnURL, command, ...args) { | ||
if (callback) { | ||
if (!this._preserveRequests) | ||
if (!this._preserveRequests) { | ||
this._waitingRequests.remove(id); | ||
this._responseHandlers.delete(id); | ||
} | ||
console.debug('RpcClient BACK'); | ||
@@ -551,0 +555,0 @@ const error = new Error('Request aborted'); |
@@ -27,8 +27,3 @@ import { ResponseMessage } from './Messages'; | ||
callAndPersist(command: string, ...args: any[]): Promise<any>; | ||
_call(obj: { | ||
command: string; | ||
args: any[]; | ||
id: number; | ||
persistInUrl?: boolean; | ||
}): Promise<any>; | ||
private _call; | ||
close(): void; | ||
@@ -35,0 +30,0 @@ private _connect; |
{ | ||
"name": "@nimiq/rpc", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "RPC Client/Server", | ||
@@ -5,0 +5,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
102394
2327
367