@nimiq/rpc
Advanced tools
Comparing version 0.0.11 to 0.1.0-beta.1
@@ -17,2 +17,3 @@ 'use strict'; | ||
})(exports.ResponseStatus || (exports.ResponseStatus = {})); | ||
const POSTMESSAGE_RETURN_URL = '<postMessage>'; | ||
@@ -175,3 +176,3 @@ /* tslint:disable:no-bitwise */ | ||
/** | ||
* @param {boolean} [storeState=true] Whether to store state in sessionStorage | ||
* @param storeState Whether to store state in sessionStorage | ||
*/ | ||
@@ -257,6 +258,9 @@ constructor(storeState = true) { | ||
return null; | ||
// Only allow returning to same origin | ||
const returnURL = new URL(params.get('returnURL')); | ||
if (returnURL.origin !== referrer.origin) | ||
return null; | ||
const answerByPostMessage = params.get('returnURL') === POSTMESSAGE_RETURN_URL && window.opener; | ||
if (!answerByPostMessage) { | ||
// Only allow returning to same origin | ||
const returnURL = new URL(params.get('returnURL')); | ||
if (returnURL.origin !== referrer.origin) | ||
return null; | ||
} | ||
// Parse args | ||
@@ -281,8 +285,5 @@ let args = []; | ||
returnURL: params.get('returnURL'), | ||
source: answerByPostMessage ? window.opener : null, | ||
}; | ||
} | ||
/** | ||
* @param {URL|Location} url | ||
* @return {{origin:string, data:{id:number, status:string, result:*}}} | ||
*/ | ||
static receiveRedirectResponse(url) { | ||
@@ -321,4 +322,3 @@ // Need referrer for origin check | ||
params.set('id', state.id.toString()); | ||
// TODO: what if it already includes a query string | ||
return `${state.returnURL}?${params.toString()}`; | ||
return `${state.returnURL}${new URL(state.returnURL).search.length > 0 ? '&' : '?'}${params.toString()}`; | ||
} | ||
@@ -360,3 +360,3 @@ static prepareRedirectInvocation(targetURL, id, returnURL, command, args) { | ||
if (callback) { | ||
this._waitingRequests.remove(data.id); | ||
// this._waitingRequests.remove(data.id); | ||
console.debug('RpcClient RECEIVE', data); | ||
@@ -371,2 +371,5 @@ if (data.status === exports.ResponseStatus.OK) { | ||
} | ||
if (data.result.name) { | ||
error.name = data.result.name; | ||
} | ||
callback.reject(error, data.id, state); | ||
@@ -430,8 +433,6 @@ } | ||
window.removeEventListener('message', this._receiveListener); | ||
this._connected = false; | ||
} | ||
_connect() { | ||
return new Promise((resolve, reject) => { | ||
/** | ||
* @param {MessageEvent} message | ||
*/ | ||
const connectedListener = (message) => { | ||
@@ -449,2 +450,5 @@ const { source, origin, data } = message; | ||
error.stack = data.result.stack; | ||
if (data.result.name) { | ||
error.name = data.result.name; | ||
} | ||
console.error(error); | ||
@@ -497,3 +501,3 @@ } | ||
} | ||
else { | ||
else if (!UrlRpcEncoder.receiveRedirectCommand(window.location)) { | ||
this._rejectOnBack(); | ||
@@ -521,3 +525,3 @@ } | ||
if (callback) { | ||
this._waitingRequests.remove(id); | ||
// this._waitingRequests.remove(id); | ||
console.debug('RpcClient BACK'); | ||
@@ -556,3 +560,4 @@ const error = new Error('Request aborted'); | ||
this._id = message.data.id; | ||
this._postMessage = 'source' in message && !('returnURL' in message); | ||
this._postMessage = 'source' in message | ||
&& !('returnURL' in message && message.returnURL !== POSTMESSAGE_RETURN_URL); | ||
this._returnURL = 'returnURL' in message ? message.returnURL : null; | ||
@@ -588,7 +593,8 @@ this._data = message.data; | ||
result = typeof result === 'object' | ||
? { message: result.message, stack: result.stack } | ||
? { message: result.message, stack: result.stack, name: result.name } | ||
: { message: result }; | ||
} | ||
// TODO: Clear waiting request storage? | ||
if (this._postMessage) { | ||
// Send via postMessage (e.g., popup) | ||
// Send via postMessage (e.g., popup or url-encoded popup) | ||
let target; | ||
@@ -595,0 +601,0 @@ // If source is given, choose accordingly |
@@ -14,2 +14,3 @@ class RandomUtils { | ||
})(ResponseStatus || (ResponseStatus = {})); | ||
const POSTMESSAGE_RETURN_URL = '<postMessage>'; | ||
@@ -172,3 +173,3 @@ /* tslint:disable:no-bitwise */ | ||
/** | ||
* @param {boolean} [storeState=true] Whether to store state in sessionStorage | ||
* @param storeState Whether to store state in sessionStorage | ||
*/ | ||
@@ -254,6 +255,9 @@ constructor(storeState = true) { | ||
return null; | ||
// Only allow returning to same origin | ||
const returnURL = new URL(params.get('returnURL')); | ||
if (returnURL.origin !== referrer.origin) | ||
return null; | ||
const answerByPostMessage = params.get('returnURL') === POSTMESSAGE_RETURN_URL && window.opener; | ||
if (!answerByPostMessage) { | ||
// Only allow returning to same origin | ||
const returnURL = new URL(params.get('returnURL')); | ||
if (returnURL.origin !== referrer.origin) | ||
return null; | ||
} | ||
// Parse args | ||
@@ -278,8 +282,5 @@ let args = []; | ||
returnURL: params.get('returnURL'), | ||
source: answerByPostMessage ? window.opener : null, | ||
}; | ||
} | ||
/** | ||
* @param {URL|Location} url | ||
* @return {{origin:string, data:{id:number, status:string, result:*}}} | ||
*/ | ||
static receiveRedirectResponse(url) { | ||
@@ -318,4 +319,3 @@ // Need referrer for origin check | ||
params.set('id', state.id.toString()); | ||
// TODO: what if it already includes a query string | ||
return `${state.returnURL}?${params.toString()}`; | ||
return `${state.returnURL}${new URL(state.returnURL).search.length > 0 ? '&' : '?'}${params.toString()}`; | ||
} | ||
@@ -357,3 +357,3 @@ static prepareRedirectInvocation(targetURL, id, returnURL, command, args) { | ||
if (callback) { | ||
this._waitingRequests.remove(data.id); | ||
// this._waitingRequests.remove(data.id); | ||
console.debug('RpcClient RECEIVE', data); | ||
@@ -368,2 +368,5 @@ if (data.status === ResponseStatus.OK) { | ||
} | ||
if (data.result.name) { | ||
error.name = data.result.name; | ||
} | ||
callback.reject(error, data.id, state); | ||
@@ -427,8 +430,6 @@ } | ||
window.removeEventListener('message', this._receiveListener); | ||
this._connected = false; | ||
} | ||
_connect() { | ||
return new Promise((resolve, reject) => { | ||
/** | ||
* @param {MessageEvent} message | ||
*/ | ||
const connectedListener = (message) => { | ||
@@ -446,2 +447,5 @@ const { source, origin, data } = message; | ||
error.stack = data.result.stack; | ||
if (data.result.name) { | ||
error.name = data.result.name; | ||
} | ||
console.error(error); | ||
@@ -494,3 +498,3 @@ } | ||
} | ||
else { | ||
else if (!UrlRpcEncoder.receiveRedirectCommand(window.location)) { | ||
this._rejectOnBack(); | ||
@@ -518,3 +522,3 @@ } | ||
if (callback) { | ||
this._waitingRequests.remove(id); | ||
// this._waitingRequests.remove(id); | ||
console.debug('RpcClient BACK'); | ||
@@ -553,3 +557,4 @@ const error = new Error('Request aborted'); | ||
this._id = message.data.id; | ||
this._postMessage = 'source' in message && !('returnURL' in message); | ||
this._postMessage = 'source' in message | ||
&& !('returnURL' in message && message.returnURL !== POSTMESSAGE_RETURN_URL); | ||
this._returnURL = 'returnURL' in message ? message.returnURL : null; | ||
@@ -585,7 +590,8 @@ this._data = message.data; | ||
result = typeof result === 'object' | ||
? { message: result.message, stack: result.stack } | ||
? { message: result.message, stack: result.stack, name: result.name } | ||
: { message: result }; | ||
} | ||
// TODO: Clear waiting request storage? | ||
if (this._postMessage) { | ||
// Send via postMessage (e.g., popup) | ||
// Send via postMessage (e.g., popup or url-encoded popup) | ||
let target; | ||
@@ -592,0 +598,0 @@ // If source is given, choose accordingly |
@@ -19,2 +19,3 @@ (function (global, factory) { | ||
})(exports.ResponseStatus || (exports.ResponseStatus = {})); | ||
const POSTMESSAGE_RETURN_URL = '<postMessage>'; | ||
@@ -177,3 +178,3 @@ /* tslint:disable:no-bitwise */ | ||
/** | ||
* @param {boolean} [storeState=true] Whether to store state in sessionStorage | ||
* @param storeState Whether to store state in sessionStorage | ||
*/ | ||
@@ -259,6 +260,9 @@ constructor(storeState = true) { | ||
return null; | ||
// Only allow returning to same origin | ||
const returnURL = new URL(params.get('returnURL')); | ||
if (returnURL.origin !== referrer.origin) | ||
return null; | ||
const answerByPostMessage = params.get('returnURL') === POSTMESSAGE_RETURN_URL && window.opener; | ||
if (!answerByPostMessage) { | ||
// Only allow returning to same origin | ||
const returnURL = new URL(params.get('returnURL')); | ||
if (returnURL.origin !== referrer.origin) | ||
return null; | ||
} | ||
// Parse args | ||
@@ -283,8 +287,5 @@ let args = []; | ||
returnURL: params.get('returnURL'), | ||
source: answerByPostMessage ? window.opener : null, | ||
}; | ||
} | ||
/** | ||
* @param {URL|Location} url | ||
* @return {{origin:string, data:{id:number, status:string, result:*}}} | ||
*/ | ||
static receiveRedirectResponse(url) { | ||
@@ -323,4 +324,3 @@ // Need referrer for origin check | ||
params.set('id', state.id.toString()); | ||
// TODO: what if it already includes a query string | ||
return `${state.returnURL}?${params.toString()}`; | ||
return `${state.returnURL}${new URL(state.returnURL).search.length > 0 ? '&' : '?'}${params.toString()}`; | ||
} | ||
@@ -362,3 +362,3 @@ static prepareRedirectInvocation(targetURL, id, returnURL, command, args) { | ||
if (callback) { | ||
this._waitingRequests.remove(data.id); | ||
// this._waitingRequests.remove(data.id); | ||
console.debug('RpcClient RECEIVE', data); | ||
@@ -373,2 +373,5 @@ if (data.status === exports.ResponseStatus.OK) { | ||
} | ||
if (data.result.name) { | ||
error.name = data.result.name; | ||
} | ||
callback.reject(error, data.id, state); | ||
@@ -432,8 +435,6 @@ } | ||
window.removeEventListener('message', this._receiveListener); | ||
this._connected = false; | ||
} | ||
_connect() { | ||
return new Promise((resolve, reject) => { | ||
/** | ||
* @param {MessageEvent} message | ||
*/ | ||
const connectedListener = (message) => { | ||
@@ -451,2 +452,5 @@ const { source, origin, data } = message; | ||
error.stack = data.result.stack; | ||
if (data.result.name) { | ||
error.name = data.result.name; | ||
} | ||
console.error(error); | ||
@@ -499,3 +503,3 @@ } | ||
} | ||
else { | ||
else if (!UrlRpcEncoder.receiveRedirectCommand(window.location)) { | ||
this._rejectOnBack(); | ||
@@ -523,3 +527,3 @@ } | ||
if (callback) { | ||
this._waitingRequests.remove(id); | ||
// this._waitingRequests.remove(id); | ||
console.debug('RpcClient BACK'); | ||
@@ -558,3 +562,4 @@ const error = new Error('Request aborted'); | ||
this._id = message.data.id; | ||
this._postMessage = 'source' in message && !('returnURL' in message); | ||
this._postMessage = 'source' in message | ||
&& !('returnURL' in message && message.returnURL !== POSTMESSAGE_RETURN_URL); | ||
this._returnURL = 'returnURL' in message ? message.returnURL : null; | ||
@@ -590,7 +595,8 @@ this._data = message.data; | ||
result = typeof result === 'object' | ||
? { message: result.message, stack: result.stack } | ||
? { message: result.message, stack: result.stack, name: result.name } | ||
: { message: result }; | ||
} | ||
// TODO: Clear waiting request storage? | ||
if (this._postMessage) { | ||
// Send via postMessage (e.g., popup) | ||
// Send via postMessage (e.g., popup or url-encoded popup) | ||
let target; | ||
@@ -597,0 +603,0 @@ // If source is given, choose accordingly |
@@ -23,2 +23,3 @@ interface Message { | ||
returnURL: string; | ||
source: MessagePort | Window | ServiceWorker | string | null; | ||
} | ||
@@ -29,2 +30,3 @@ export declare enum ResponseStatus { | ||
} | ||
export declare const POSTMESSAGE_RETURN_URL = "<postMessage>"; | ||
export {}; |
@@ -7,3 +7,3 @@ export declare class RequestIdStorage { | ||
/** | ||
* @param {boolean} [storeState=true] Whether to store state in sessionStorage | ||
* @param storeState Whether to store state in sessionStorage | ||
*/ | ||
@@ -10,0 +10,0 @@ constructor(storeState?: boolean); |
@@ -5,6 +5,2 @@ import { RedirectRequest, ResponseMessage, ResponseStatus } from './Messages'; | ||
static receiveRedirectCommand(url: URL | Location): RedirectRequest | null; | ||
/** | ||
* @param {URL|Location} url | ||
* @return {{origin:string, data:{id:number, status:string, result:*}}} | ||
*/ | ||
static receiveRedirectResponse(url: URL | Location): ResponseMessage | null; | ||
@@ -11,0 +7,0 @@ static prepareRedirectReply(state: State, status: ResponseStatus, result: any): string; |
{ | ||
"name": "@nimiq/rpc", | ||
"version": "0.0.11", | ||
"version": "0.1.0-beta.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
97433
2224