channel-rpc
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -26,2 +26,13 @@ const MessageTypes = { | ||
} | ||
// get debugEnabled from localStorage | ||
let debugEnabled = false; | ||
try { | ||
debugEnabled = !!localStorage.getItem("channel-rpc-debug"); | ||
} | ||
catch (err) { } | ||
function debug(...args) { | ||
if (!debugEnabled) | ||
return; | ||
console.log(...args); | ||
} | ||
function defer(timeout) { | ||
@@ -66,3 +77,3 @@ const deferred = { | ||
// DEBUG | ||
console.log(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] ChannelRpcRequest`, ev.data); | ||
debug(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] RECEIVE_REQUEST`, ev.data); | ||
this._handleRpcRequest(ev.source, ev.data.payload); | ||
@@ -110,3 +121,3 @@ }; | ||
async _handleRpcRequest(source, payload) { | ||
console.log(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] handleMessage`, payload); | ||
debug(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] HANDLE_REQUEST_RPC`, payload); | ||
if (!isJsonRpcRequest(payload)) { | ||
@@ -121,7 +132,7 @@ const res = { | ||
}; | ||
console.log(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] reply`, res); | ||
debug(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] reply`, res); | ||
this._sendResponse(source, res); | ||
return; | ||
} | ||
console.log(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] handleMessage method[${payload.method}]`, this._handlers); | ||
debug(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] HANDLE_REQUEST_RPC method[${payload.method}]`, this._handlers, payload); | ||
const handler = this._handlers[payload.method]; | ||
@@ -137,3 +148,3 @@ if (!handler) { | ||
}; | ||
console.log(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] reply`, res); | ||
debug(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] SEND_RESPONSE`, res); | ||
this._sendResponse(source, res); | ||
@@ -149,3 +160,3 @@ return; | ||
}; | ||
console.log(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] reply`, res); | ||
debug(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] SEND_RESPONSE`, res); | ||
this._sendResponse(source, res); | ||
@@ -163,3 +174,3 @@ } | ||
}; | ||
console.log(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] reply`, res); | ||
debug(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] SEND_RESPONSE`, res); | ||
this._sendResponse(source, res); | ||
@@ -183,3 +194,3 @@ } | ||
return (...args) => { | ||
console.log("[CHANNEL_RPC][CLIENT] invoke stub method", prop, args); | ||
debug(`[CHANNEL_RPC_CLIENT][channel=${channelId}] INVOKE`, prop, args); | ||
return this._sendRequest(String(prop), args); | ||
@@ -195,3 +206,3 @@ }; | ||
// DEBUG | ||
console.log(`[CHANNEL_RPC_CLIENT][channel=${this.channelId}] ChannelRpcResponse`, ev.data); | ||
debug(`[CHANNEL_RPC_CLIENT][channel=${this.channelId}] HANDLE_RESPONSE`, ev.data); | ||
this._handleRpcResponse(ev.data.payload); | ||
@@ -219,3 +230,3 @@ }); | ||
}; | ||
console.log("[CHANNEL_RPC_CLIENT] Send invoke", req); | ||
debug("[CHANNEL_RPC_CLIENT] SEND_REQUEST", req); | ||
const channelReq = { | ||
@@ -230,3 +241,3 @@ type: MessageTypes.ChannelRpcRequest, | ||
_handleRpcResponse(payload) { | ||
console.log("[CHANNEL_RPC_CLIENT] handleRpcResponse", payload); | ||
debug("[CHANNEL_RPC_CLIENT] HANDLE_RESPONSE_RPC", payload); | ||
if (isJsonRpcSuccessResponse(payload)) { | ||
@@ -241,5 +252,7 @@ const { id, result } = payload; | ||
else { | ||
throw new Error(`[CHANNEL_RPC_CLIENT][channel=${this.channelId}] UNKNOWN_RESPONSE: ${JSON.stringify(payload)}`); | ||
const err = new Error(`[CHANNEL_RPC_CLIENT][channel=${this.channelId}] UNKNOWN_RESPONSE: ${JSON.stringify(payload)}`); | ||
debug("[CHANNEL_RPC_CLIENT] HANDLE_RESPONSE_RPC, ERROR", err); | ||
throw err; | ||
} | ||
} | ||
} |
{ | ||
"name": "channel-rpc", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "JSON-RPC over postMessage", | ||
@@ -5,0 +5,0 @@ "author": "kouhin", |
@@ -73,2 +73,11 @@ interface JsonRpcRequest { | ||
} | ||
// get debugEnabled from localStorage | ||
let debugEnabled = false; | ||
try { | ||
debugEnabled = !!localStorage.getItem("channel-rpc-debug"); | ||
} catch (err) {} | ||
function debug(...args: any[]) { | ||
if (!debugEnabled) return; | ||
console.log(...args); | ||
} | ||
@@ -164,6 +173,7 @@ function defer<T>(timeout: number): Deferred<T> { | ||
// DEBUG | ||
console.log( | ||
`[CHANNEL_RPC_SERVER][channel=${this.channelId}] ChannelRpcRequest`, | ||
debug( | ||
`[CHANNEL_RPC_SERVER][channel=${this.channelId}] RECEIVE_REQUEST`, | ||
ev.data | ||
); | ||
this._handleRpcRequest(ev.source, ev.data.payload); | ||
@@ -190,4 +200,4 @@ }; | ||
): Promise<void> { | ||
console.log( | ||
`[CHANNEL_RPC_SERVER][channel=${this.channelId}] handleMessage`, | ||
debug( | ||
`[CHANNEL_RPC_SERVER][channel=${this.channelId}] HANDLE_REQUEST_RPC`, | ||
payload | ||
@@ -204,3 +214,3 @@ ); | ||
}; | ||
console.log(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] reply`, res); | ||
debug(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] reply`, res); | ||
this._sendResponse(source, res); | ||
@@ -210,5 +220,6 @@ return; | ||
console.log( | ||
`[CHANNEL_RPC_SERVER][channel=${this.channelId}] handleMessage method[${payload.method}]`, | ||
this._handlers | ||
debug( | ||
`[CHANNEL_RPC_SERVER][channel=${this.channelId}] HANDLE_REQUEST_RPC method[${payload.method}]`, | ||
this._handlers, | ||
payload | ||
); | ||
@@ -225,3 +236,6 @@ const handler = this._handlers[payload.method]; | ||
}; | ||
console.log(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] reply`, res); | ||
debug( | ||
`[CHANNEL_RPC_SERVER][channel=${this.channelId}] SEND_RESPONSE`, | ||
res | ||
); | ||
this._sendResponse(source, res); | ||
@@ -237,3 +251,6 @@ return; | ||
}; | ||
console.log(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] reply`, res); | ||
debug( | ||
`[CHANNEL_RPC_SERVER][channel=${this.channelId}] SEND_RESPONSE`, | ||
res | ||
); | ||
this._sendResponse(source, res); | ||
@@ -250,3 +267,6 @@ } catch (err) { | ||
}; | ||
console.log(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] reply`, res); | ||
debug( | ||
`[CHANNEL_RPC_SERVER][channel=${this.channelId}] SEND_RESPONSE`, | ||
res | ||
); | ||
this._sendResponse(source, res); | ||
@@ -288,3 +308,7 @@ } | ||
return (...args: unknown[]) => { | ||
console.log("[CHANNEL_RPC][CLIENT] invoke stub method", prop, args); | ||
debug( | ||
`[CHANNEL_RPC_CLIENT][channel=${channelId}] INVOKE`, | ||
prop, | ||
args | ||
); | ||
return this._sendRequest(String(prop), args); | ||
@@ -301,4 +325,4 @@ }; | ||
// DEBUG | ||
console.log( | ||
`[CHANNEL_RPC_CLIENT][channel=${this.channelId}] ChannelRpcResponse`, | ||
debug( | ||
`[CHANNEL_RPC_CLIENT][channel=${this.channelId}] HANDLE_RESPONSE`, | ||
ev.data | ||
@@ -332,3 +356,3 @@ ); | ||
}; | ||
console.log("[CHANNEL_RPC_CLIENT] Send invoke", req); | ||
debug("[CHANNEL_RPC_CLIENT] SEND_REQUEST", req); | ||
const channelReq: ChannelRpcRequest = { | ||
@@ -344,3 +368,3 @@ type: MessageTypes.ChannelRpcRequest, | ||
private _handleRpcResponse(payload: unknown) { | ||
console.log("[CHANNEL_RPC_CLIENT] handleRpcResponse", payload); | ||
debug("[CHANNEL_RPC_CLIENT] HANDLE_RESPONSE_RPC", payload); | ||
if (isJsonRpcSuccessResponse(payload)) { | ||
@@ -353,3 +377,3 @@ const { id, result } = payload; | ||
} else { | ||
throw new Error( | ||
const err = new Error( | ||
`[CHANNEL_RPC_CLIENT][channel=${ | ||
@@ -359,4 +383,6 @@ this.channelId | ||
); | ||
debug("[CHANNEL_RPC_CLIENT] HANDLE_RESPONSE_RPC, ERROR", err); | ||
throw err; | ||
} | ||
} | ||
} |
24441
622