@nimiq/rpc
Advanced tools
Comparing version 0.1.0-beta.5 to 0.1.0-beta.6
@@ -256,3 +256,4 @@ 'use strict'; | ||
return null; | ||
const answerByPostMessage = params.get('returnURL') === POSTMESSAGE_RETURN_URL && window.opener; | ||
const answerByPostMessage = params.get('returnURL') === POSTMESSAGE_RETURN_URL | ||
&& (window.opener || window.parent); | ||
if (!answerByPostMessage) { | ||
@@ -283,3 +284,3 @@ // Only allow returning to same origin | ||
returnURL: params.get('returnURL'), | ||
source: answerByPostMessage ? window.opener : null, | ||
source: answerByPostMessage ? (window.opener || window.parent) : null, | ||
}; | ||
@@ -340,2 +341,3 @@ } | ||
this._responseHandlers = new Map(); | ||
this._preserveRequests = false; | ||
} | ||
@@ -358,3 +360,4 @@ onResponse(command, resolve, reject) { | ||
if (callback) { | ||
// this._waitingRequests.remove(data.id); | ||
if (!this._preserveRequests) | ||
this._waitingRequests.remove(data.id); | ||
console.debug('RpcClient RECEIVE', data); | ||
@@ -405,13 +408,23 @@ if (data.status === exports.ResponseStatus.OK) { | ||
async call(command, ...args) { | ||
return this._call({ | ||
command, | ||
args, | ||
id: RandomUtils.generateRandomId(), | ||
}); | ||
} | ||
async callAndPersist(command, ...args) { | ||
return this._call({ | ||
command, | ||
args, | ||
id: RandomUtils.generateRandomId(), | ||
persistInUrl: true, | ||
}); | ||
} | ||
async _call(obj) { | ||
if (!this._connected) | ||
throw new Error('Client is not connected, call init first'); | ||
return new Promise((resolve, reject) => { | ||
const obj = { | ||
command, | ||
args, | ||
id: RandomUtils.generateRandomId(), | ||
}; | ||
// Store the request resolvers | ||
this._responseHandlers.set(obj.id, { resolve, reject }); | ||
this._waitingRequests.add(obj.id, command); | ||
this._waitingRequests.add(obj.id, obj.command); | ||
// Periodically check if recepient window is still open | ||
@@ -425,3 +438,3 @@ const checkIfServerWasClosed = () => { | ||
setTimeout(checkIfServerWasClosed, 500); | ||
console.debug('RpcClient REQUEST', command, args); | ||
console.debug('RpcClient REQUEST', obj.command, obj.args); | ||
this._target.postMessage(obj, this._allowedOrigin); | ||
@@ -481,3 +494,3 @@ }); | ||
// @ts-ignore | ||
connectTimer = setTimeout(tryToConnect, 1000); | ||
connectTimer = setTimeout(tryToConnect, 100); | ||
}; | ||
@@ -489,30 +502,7 @@ // @ts-ignore | ||
} | ||
class ReceiveOnlyPostMessageRpcClient extends PostMessageRpcClient { | ||
constructor(targetWindow, allowedOrigin, requestId) { | ||
super(targetWindow, allowedOrigin); | ||
this._requestId = requestId; | ||
} | ||
async init() { | ||
window.addEventListener('message', this._receiveListener); | ||
} | ||
async listenFor(command) { | ||
return new Promise((resolve, reject) => { | ||
// Store the request resolvers | ||
this._responseHandlers.set(this._requestId, { resolve, reject }); | ||
this._waitingRequests.add(this._requestId, command); | ||
// Periodically check if recepient window is still open | ||
const checkIfServerWasClosed = () => { | ||
if (this._target.closed) { | ||
reject(new Error('Window was closed')); | ||
} | ||
setTimeout(checkIfServerWasClosed, 500); | ||
}; | ||
setTimeout(checkIfServerWasClosed, 500); | ||
}); | ||
} | ||
} | ||
class RedirectRpcClient extends RpcClient { | ||
constructor(targetURL, allowedOrigin) { | ||
constructor(targetURL, allowedOrigin, preserveRequests = false) { | ||
super(allowedOrigin, /*storeState*/ true); | ||
this._target = targetURL; | ||
this._preserveRequests = preserveRequests; | ||
} | ||
@@ -523,2 +513,8 @@ async init() { | ||
this._receive(message); | ||
// The URL the user goes back to in the browser history (the page | ||
// that this RpcClient is inited on) may itself have been a | ||
// URL-encoded RPC request. Thus before triggering a potential | ||
// rejection of the called request, we make sure that there is | ||
// no RPC request in the URL, to be able to re-start the actual | ||
// request that the user goes back to. | ||
} | ||
@@ -548,3 +544,4 @@ else if (!UrlRpcEncoder.receiveRedirectCommand(window.location)) { | ||
if (callback) { | ||
// this._waitingRequests.remove(id); | ||
if (!this._preserveRequests) | ||
this._waitingRequests.remove(id); | ||
console.debug('RpcClient BACK'); | ||
@@ -620,3 +617,3 @@ const error = new Error('Request aborted'); | ||
if (this._postMessage) { | ||
// Send via postMessage (e.g., popup or url-encoded popup) | ||
// Send via postMessage (e.g., popup or url-persisted popup) | ||
let target; | ||
@@ -650,2 +647,5 @@ // If source is given, choose accordingly | ||
} | ||
toRequestUrl(baseUrl = '') { | ||
return UrlRpcEncoder.prepareRedirectInvocation(baseUrl, this.id, this.returnURL || POSTMESSAGE_RETURN_URL, this.data.command, this.data.args); | ||
} | ||
} | ||
@@ -707,2 +707,5 @@ | ||
console.debug('RpcServer ACCEPT', state.data); | ||
if (state.data.persistInUrl && window.history && window.history.replaceState) { | ||
window.history.replaceState(history.state, document.title, state.toRequestUrl(`${window.location.origin}${window.location.pathname}`)); | ||
} | ||
// Call method | ||
@@ -735,7 +738,4 @@ const result = requestedMethod(state, ...args); | ||
exports.PostMessageRpcClient = PostMessageRpcClient; | ||
exports.ReceiveOnlyPostMessageRpcClient = ReceiveOnlyPostMessageRpcClient; | ||
exports.RedirectRpcClient = RedirectRpcClient; | ||
exports.RpcServer = RpcServer; | ||
exports.State = State; | ||
exports.UrlRpcEncoder = UrlRpcEncoder; | ||
exports.RandomUtils = RandomUtils; |
@@ -253,3 +253,4 @@ class RandomUtils { | ||
return null; | ||
const answerByPostMessage = params.get('returnURL') === POSTMESSAGE_RETURN_URL && window.opener; | ||
const answerByPostMessage = params.get('returnURL') === POSTMESSAGE_RETURN_URL | ||
&& (window.opener || window.parent); | ||
if (!answerByPostMessage) { | ||
@@ -280,3 +281,3 @@ // Only allow returning to same origin | ||
returnURL: params.get('returnURL'), | ||
source: answerByPostMessage ? window.opener : null, | ||
source: answerByPostMessage ? (window.opener || window.parent) : null, | ||
}; | ||
@@ -337,2 +338,3 @@ } | ||
this._responseHandlers = new Map(); | ||
this._preserveRequests = false; | ||
} | ||
@@ -355,3 +357,4 @@ onResponse(command, resolve, reject) { | ||
if (callback) { | ||
// this._waitingRequests.remove(data.id); | ||
if (!this._preserveRequests) | ||
this._waitingRequests.remove(data.id); | ||
console.debug('RpcClient RECEIVE', data); | ||
@@ -402,13 +405,23 @@ if (data.status === ResponseStatus.OK) { | ||
async call(command, ...args) { | ||
return this._call({ | ||
command, | ||
args, | ||
id: RandomUtils.generateRandomId(), | ||
}); | ||
} | ||
async callAndPersist(command, ...args) { | ||
return this._call({ | ||
command, | ||
args, | ||
id: RandomUtils.generateRandomId(), | ||
persistInUrl: true, | ||
}); | ||
} | ||
async _call(obj) { | ||
if (!this._connected) | ||
throw new Error('Client is not connected, call init first'); | ||
return new Promise((resolve, reject) => { | ||
const obj = { | ||
command, | ||
args, | ||
id: RandomUtils.generateRandomId(), | ||
}; | ||
// Store the request resolvers | ||
this._responseHandlers.set(obj.id, { resolve, reject }); | ||
this._waitingRequests.add(obj.id, command); | ||
this._waitingRequests.add(obj.id, obj.command); | ||
// Periodically check if recepient window is still open | ||
@@ -422,3 +435,3 @@ const checkIfServerWasClosed = () => { | ||
setTimeout(checkIfServerWasClosed, 500); | ||
console.debug('RpcClient REQUEST', command, args); | ||
console.debug('RpcClient REQUEST', obj.command, obj.args); | ||
this._target.postMessage(obj, this._allowedOrigin); | ||
@@ -478,3 +491,3 @@ }); | ||
// @ts-ignore | ||
connectTimer = setTimeout(tryToConnect, 1000); | ||
connectTimer = setTimeout(tryToConnect, 100); | ||
}; | ||
@@ -486,30 +499,7 @@ // @ts-ignore | ||
} | ||
class ReceiveOnlyPostMessageRpcClient extends PostMessageRpcClient { | ||
constructor(targetWindow, allowedOrigin, requestId) { | ||
super(targetWindow, allowedOrigin); | ||
this._requestId = requestId; | ||
} | ||
async init() { | ||
window.addEventListener('message', this._receiveListener); | ||
} | ||
async listenFor(command) { | ||
return new Promise((resolve, reject) => { | ||
// Store the request resolvers | ||
this._responseHandlers.set(this._requestId, { resolve, reject }); | ||
this._waitingRequests.add(this._requestId, command); | ||
// Periodically check if recepient window is still open | ||
const checkIfServerWasClosed = () => { | ||
if (this._target.closed) { | ||
reject(new Error('Window was closed')); | ||
} | ||
setTimeout(checkIfServerWasClosed, 500); | ||
}; | ||
setTimeout(checkIfServerWasClosed, 500); | ||
}); | ||
} | ||
} | ||
class RedirectRpcClient extends RpcClient { | ||
constructor(targetURL, allowedOrigin) { | ||
constructor(targetURL, allowedOrigin, preserveRequests = false) { | ||
super(allowedOrigin, /*storeState*/ true); | ||
this._target = targetURL; | ||
this._preserveRequests = preserveRequests; | ||
} | ||
@@ -520,2 +510,8 @@ async init() { | ||
this._receive(message); | ||
// The URL the user goes back to in the browser history (the page | ||
// that this RpcClient is inited on) may itself have been a | ||
// URL-encoded RPC request. Thus before triggering a potential | ||
// rejection of the called request, we make sure that there is | ||
// no RPC request in the URL, to be able to re-start the actual | ||
// request that the user goes back to. | ||
} | ||
@@ -545,3 +541,4 @@ else if (!UrlRpcEncoder.receiveRedirectCommand(window.location)) { | ||
if (callback) { | ||
// this._waitingRequests.remove(id); | ||
if (!this._preserveRequests) | ||
this._waitingRequests.remove(id); | ||
console.debug('RpcClient BACK'); | ||
@@ -617,3 +614,3 @@ const error = new Error('Request aborted'); | ||
if (this._postMessage) { | ||
// Send via postMessage (e.g., popup or url-encoded popup) | ||
// Send via postMessage (e.g., popup or url-persisted popup) | ||
let target; | ||
@@ -647,2 +644,5 @@ // If source is given, choose accordingly | ||
} | ||
toRequestUrl(baseUrl = '') { | ||
return UrlRpcEncoder.prepareRedirectInvocation(baseUrl, this.id, this.returnURL || POSTMESSAGE_RETURN_URL, this.data.command, this.data.args); | ||
} | ||
} | ||
@@ -704,2 +704,5 @@ | ||
console.debug('RpcServer ACCEPT', state.data); | ||
if (state.data.persistInUrl && window.history && window.history.replaceState) { | ||
window.history.replaceState(history.state, document.title, state.toRequestUrl(`${window.location.origin}${window.location.pathname}`)); | ||
} | ||
// Call method | ||
@@ -730,2 +733,2 @@ const result = requestedMethod(state, ...args); | ||
export { RpcClient, PostMessageRpcClient, ReceiveOnlyPostMessageRpcClient, RedirectRpcClient, RpcServer, State, ResponseStatus, UrlRpcEncoder, RandomUtils }; | ||
export { RpcClient, PostMessageRpcClient, RedirectRpcClient, RpcServer, State, ResponseStatus }; |
@@ -258,3 +258,4 @@ (function (global, factory) { | ||
return null; | ||
const answerByPostMessage = params.get('returnURL') === POSTMESSAGE_RETURN_URL && window.opener; | ||
const answerByPostMessage = params.get('returnURL') === POSTMESSAGE_RETURN_URL | ||
&& (window.opener || window.parent); | ||
if (!answerByPostMessage) { | ||
@@ -285,3 +286,3 @@ // Only allow returning to same origin | ||
returnURL: params.get('returnURL'), | ||
source: answerByPostMessage ? window.opener : null, | ||
source: answerByPostMessage ? (window.opener || window.parent) : null, | ||
}; | ||
@@ -342,2 +343,3 @@ } | ||
this._responseHandlers = new Map(); | ||
this._preserveRequests = false; | ||
} | ||
@@ -360,3 +362,4 @@ onResponse(command, resolve, reject) { | ||
if (callback) { | ||
// this._waitingRequests.remove(data.id); | ||
if (!this._preserveRequests) | ||
this._waitingRequests.remove(data.id); | ||
console.debug('RpcClient RECEIVE', data); | ||
@@ -407,13 +410,23 @@ if (data.status === exports.ResponseStatus.OK) { | ||
async call(command, ...args) { | ||
return this._call({ | ||
command, | ||
args, | ||
id: RandomUtils.generateRandomId(), | ||
}); | ||
} | ||
async callAndPersist(command, ...args) { | ||
return this._call({ | ||
command, | ||
args, | ||
id: RandomUtils.generateRandomId(), | ||
persistInUrl: true, | ||
}); | ||
} | ||
async _call(obj) { | ||
if (!this._connected) | ||
throw new Error('Client is not connected, call init first'); | ||
return new Promise((resolve, reject) => { | ||
const obj = { | ||
command, | ||
args, | ||
id: RandomUtils.generateRandomId(), | ||
}; | ||
// Store the request resolvers | ||
this._responseHandlers.set(obj.id, { resolve, reject }); | ||
this._waitingRequests.add(obj.id, command); | ||
this._waitingRequests.add(obj.id, obj.command); | ||
// Periodically check if recepient window is still open | ||
@@ -427,3 +440,3 @@ const checkIfServerWasClosed = () => { | ||
setTimeout(checkIfServerWasClosed, 500); | ||
console.debug('RpcClient REQUEST', command, args); | ||
console.debug('RpcClient REQUEST', obj.command, obj.args); | ||
this._target.postMessage(obj, this._allowedOrigin); | ||
@@ -483,3 +496,3 @@ }); | ||
// @ts-ignore | ||
connectTimer = setTimeout(tryToConnect, 1000); | ||
connectTimer = setTimeout(tryToConnect, 100); | ||
}; | ||
@@ -491,30 +504,7 @@ // @ts-ignore | ||
} | ||
class ReceiveOnlyPostMessageRpcClient extends PostMessageRpcClient { | ||
constructor(targetWindow, allowedOrigin, requestId) { | ||
super(targetWindow, allowedOrigin); | ||
this._requestId = requestId; | ||
} | ||
async init() { | ||
window.addEventListener('message', this._receiveListener); | ||
} | ||
async listenFor(command) { | ||
return new Promise((resolve, reject) => { | ||
// Store the request resolvers | ||
this._responseHandlers.set(this._requestId, { resolve, reject }); | ||
this._waitingRequests.add(this._requestId, command); | ||
// Periodically check if recepient window is still open | ||
const checkIfServerWasClosed = () => { | ||
if (this._target.closed) { | ||
reject(new Error('Window was closed')); | ||
} | ||
setTimeout(checkIfServerWasClosed, 500); | ||
}; | ||
setTimeout(checkIfServerWasClosed, 500); | ||
}); | ||
} | ||
} | ||
class RedirectRpcClient extends RpcClient { | ||
constructor(targetURL, allowedOrigin) { | ||
constructor(targetURL, allowedOrigin, preserveRequests = false) { | ||
super(allowedOrigin, /*storeState*/ true); | ||
this._target = targetURL; | ||
this._preserveRequests = preserveRequests; | ||
} | ||
@@ -525,2 +515,8 @@ async init() { | ||
this._receive(message); | ||
// The URL the user goes back to in the browser history (the page | ||
// that this RpcClient is inited on) may itself have been a | ||
// URL-encoded RPC request. Thus before triggering a potential | ||
// rejection of the called request, we make sure that there is | ||
// no RPC request in the URL, to be able to re-start the actual | ||
// request that the user goes back to. | ||
} | ||
@@ -550,3 +546,4 @@ else if (!UrlRpcEncoder.receiveRedirectCommand(window.location)) { | ||
if (callback) { | ||
// this._waitingRequests.remove(id); | ||
if (!this._preserveRequests) | ||
this._waitingRequests.remove(id); | ||
console.debug('RpcClient BACK'); | ||
@@ -622,3 +619,3 @@ const error = new Error('Request aborted'); | ||
if (this._postMessage) { | ||
// Send via postMessage (e.g., popup or url-encoded popup) | ||
// Send via postMessage (e.g., popup or url-persisted popup) | ||
let target; | ||
@@ -652,2 +649,5 @@ // If source is given, choose accordingly | ||
} | ||
toRequestUrl(baseUrl = '') { | ||
return UrlRpcEncoder.prepareRedirectInvocation(baseUrl, this.id, this.returnURL || POSTMESSAGE_RETURN_URL, this.data.command, this.data.args); | ||
} | ||
} | ||
@@ -709,2 +709,5 @@ | ||
console.debug('RpcServer ACCEPT', state.data); | ||
if (state.data.persistInUrl && window.history && window.history.replaceState) { | ||
window.history.replaceState(history.state, document.title, state.toRequestUrl(`${window.location.origin}${window.location.pathname}`)); | ||
} | ||
// Call method | ||
@@ -737,8 +740,5 @@ const result = requestedMethod(state, ...args); | ||
exports.PostMessageRpcClient = PostMessageRpcClient; | ||
exports.ReceiveOnlyPostMessageRpcClient = ReceiveOnlyPostMessageRpcClient; | ||
exports.RedirectRpcClient = RedirectRpcClient; | ||
exports.RpcServer = RpcServer; | ||
exports.State = State; | ||
exports.UrlRpcEncoder = UrlRpcEncoder; | ||
exports.RandomUtils = RandomUtils; | ||
@@ -745,0 +745,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
export * from './RpcClient'; | ||
export * from './RpcServer'; | ||
export * from './UrlRpcEncoder'; | ||
export * from './RandomUtils'; |
@@ -9,4 +9,5 @@ import { ResponseMessage } from './Messages'; | ||
protected readonly _allowedOrigin: string; | ||
protected _waitingRequests: RequestIdStorage; | ||
protected _responseHandlers: Map<string | number, ResponseHandler>; | ||
protected readonly _waitingRequests: RequestIdStorage; | ||
protected readonly _responseHandlers: Map<string | number, ResponseHandler>; | ||
protected readonly _preserveRequests: boolean; | ||
protected constructor(allowedOrigin: string, storeState?: boolean); | ||
@@ -20,20 +21,22 @@ onResponse(command: string, resolve: (result: any, id?: number, state?: string | null) => any, reject: (error: any, id?: number, state?: string | null) => any): void; | ||
export declare class PostMessageRpcClient extends RpcClient { | ||
protected readonly _target: Window; | ||
private readonly _target; | ||
private readonly _receiveListener; | ||
private _connected; | ||
protected readonly _receiveListener: (message: MessageEvent) => any; | ||
constructor(targetWindow: Window, allowedOrigin: string); | ||
init(): Promise<void>; | ||
call(command: string, ...args: any[]): Promise<any>; | ||
callAndPersist(command: string, ...args: any[]): Promise<any>; | ||
_call(obj: { | ||
command: string; | ||
args: any[]; | ||
id: number; | ||
persistInUrl?: boolean; | ||
}): Promise<any>; | ||
close(): void; | ||
private _connect; | ||
} | ||
export declare class ReceiveOnlyPostMessageRpcClient extends PostMessageRpcClient { | ||
private _requestId; | ||
constructor(targetWindow: Window, allowedOrigin: string, requestId: number); | ||
init(): Promise<void>; | ||
listenFor(command: string): Promise<any>; | ||
} | ||
export declare class RedirectRpcClient extends RpcClient { | ||
protected readonly _preserveRequests: boolean; | ||
private readonly _target; | ||
constructor(targetURL: string, allowedOrigin: string); | ||
constructor(targetURL: string, allowedOrigin: string, preserveRequests?: boolean); | ||
init(): Promise<void>; | ||
@@ -40,0 +43,0 @@ close(): void; |
@@ -6,3 +6,8 @@ import { PostMessage, RedirectRequest, ResponseStatus } from './Messages'; | ||
readonly origin: string; | ||
readonly data: any; | ||
readonly data: { | ||
command: string; | ||
args: any[]; | ||
id: number; | ||
persistInUrl?: boolean | undefined; | ||
}; | ||
readonly returnURL: string | null; | ||
@@ -20,2 +25,3 @@ readonly source: string | MessagePort | ServiceWorker | Window | null; | ||
reply(status: ResponseStatus, result: any): void; | ||
toRequestUrl(baseUrl?: string): string; | ||
} |
{ | ||
"name": "@nimiq/rpc", | ||
"version": "0.1.0-beta.5", | ||
"version": "0.1.0-beta.6", | ||
"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
102188
2320