-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implements unverified requests / responses.
* Implements IFrame communication stream.
- Loading branch information
Showing
18 changed files
with
1,438 additions
and
2,655 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"plantuml.diagramsRoot": "docs", | ||
"plantuml.exportFormat": "svg", | ||
"plantuml.exportOutDir": "docs/exported" | ||
"plantuml.exportOutDir": "docs/exported", | ||
"editor.formatOnSave": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { | ||
connectToWorker, | ||
connectWorkerToParent, | ||
} from "@hediet/json-rpc-browser"; | ||
import { | ||
contract, | ||
ConsoleRpcLogger, | ||
Contract, | ||
unverifiedRequest, | ||
unverifiedNotification, | ||
} from "@hediet/json-rpc"; | ||
|
||
const api = contract({ | ||
name: "API", | ||
server: { | ||
calculate: unverifiedRequest<{ name: string }, string>(), | ||
}, | ||
client: { | ||
progress: unverifiedNotification<{ progress: number }>(), | ||
}, | ||
}); | ||
|
||
if (typeof window !== "undefined") { | ||
function writeln(str: string) { | ||
document.writeln(str + "<br />"); | ||
} | ||
|
||
// window: | ||
const worker = new Worker(new URL("./demo1-webworker.ts", import.meta.url)); | ||
const { server } = Contract.getServerFromStream( | ||
api, | ||
connectToWorker(worker), | ||
{ logger: new ConsoleRpcLogger() }, | ||
{ | ||
progress: ({ progress }) => { | ||
writeln(`${progress}`); | ||
}, | ||
} | ||
); | ||
|
||
(async () => { | ||
try { | ||
const result1 = await server.calculate({ name: "foo" }); | ||
writeln(`result1: ${result1}`); | ||
const result2 = await server.calculate({ name: "bar" }); | ||
writeln(`result2: ${result2}`); | ||
} catch (e) { | ||
writeln(`${e}`); | ||
} | ||
})(); | ||
} else { | ||
// worker: | ||
Contract.registerServerToStream( | ||
api, | ||
connectWorkerToParent(), | ||
{ logger: new ConsoleRpcLogger(), sendExceptionDetails: true }, | ||
{ | ||
calculate: async ({ name }, { counterpart }) => { | ||
for (let i = 0; i <= 10; i++) { | ||
for (let j = 0; j < 100000000; j++) {} | ||
if (i === 5 && name === "bar") { | ||
throw "`bar` is not supported."; | ||
} | ||
counterpart.progress({ progress: i / 10 }); | ||
} | ||
return "bla" + name; | ||
}, | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { | ||
connectToIFrame, | ||
connectIFrameToParent, | ||
} from "@hediet/json-rpc-browser"; | ||
import { | ||
contract, | ||
ConsoleRpcLogger, | ||
Contract, | ||
unverifiedRequest, | ||
unverifiedNotification, | ||
} from "@hediet/json-rpc"; | ||
|
||
const api = contract({ | ||
name: "API", | ||
server: { | ||
calculate: unverifiedRequest<{ name: string }, string>(), | ||
}, | ||
client: { | ||
progress: unverifiedNotification<{ progress: number }>(), | ||
}, | ||
}); | ||
|
||
if (window.self === window.top) { | ||
// parent | ||
document.body.innerHTML = ` | ||
<h1>IFrame Demo (Parent Window)</h1> | ||
<iframe id="iframe1" src="${document.URL}"></iframe> | ||
<div id="myDiv" /> | ||
`; | ||
const myDiv = document.getElementById("myDiv")!; | ||
|
||
function writeln(str: string) { | ||
myDiv.innerHTML += str + "<br />"; | ||
} | ||
|
||
const { server } = Contract.getServerFromStream( | ||
api, | ||
connectToIFrame( | ||
document.getElementById("iframe1") as HTMLIFrameElement | ||
), | ||
{ logger: new ConsoleRpcLogger() }, | ||
{ | ||
progress: ({ progress }) => { | ||
writeln(`${progress}`); | ||
}, | ||
} | ||
); | ||
|
||
(async () => { | ||
try { | ||
const result1 = await server.calculate({ name: "foo" }); | ||
writeln(`result1: ${result1}`); | ||
const result2 = await server.calculate({ name: "bar" }); | ||
writeln(`result2: ${result2}`); | ||
} catch (e) { | ||
writeln(`${e}`); | ||
} | ||
})(); | ||
} else { | ||
// IFrame | ||
document.body.innerHTML = `(IFrame Window)`; | ||
|
||
Contract.registerServerToStream( | ||
api, | ||
connectIFrameToParent(), | ||
{ logger: new ConsoleRpcLogger(), sendExceptionDetails: true }, | ||
{ | ||
calculate: async ({ name }, { counterpart }) => { | ||
for (let i = 0; i <= 10; i++) { | ||
for (let j = 0; j < 100000000; j++) {} | ||
if (i === 5 && name === "bar") { | ||
throw new Error("Example Error."); | ||
} | ||
counterpart.progress({ progress: i / 10 }); | ||
} | ||
return "bla" + name; | ||
}, | ||
} | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as webpack from "webpack"; | ||
import path = require("path"); | ||
import HtmlWebpackPlugin = require("html-webpack-plugin"); | ||
|
||
const r = (file: string) => path.resolve(__dirname, file); | ||
|
||
module.exports = { | ||
entry: { | ||
demo1: r("./src/demo1-webworker.ts"), | ||
demo2: r("./src/demo2-iframe.ts"), | ||
}, | ||
output: { path: r("dist") }, | ||
resolve: { | ||
extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"], | ||
}, | ||
devtool: "source-map", | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.tsx?$/, | ||
loader: "ts-loader", | ||
options: { transpileOnly: true }, | ||
}, | ||
], | ||
}, | ||
plugins: [ | ||
new HtmlWebpackPlugin({ chunks: ["demo1"], filename: "demo1.html" }), | ||
new HtmlWebpackPlugin({ chunks: ["demo2"], filename: "demo2.html" }), | ||
], | ||
} as webpack.Configuration; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { BaseMessageStream, Message } from "@hediet/json-rpc"; | ||
|
||
export interface WindowLike { | ||
postMessage(data: any, ...misc: any[]): void; | ||
addEventListener( | ||
ev: "message", | ||
handler: (ev: { data: any; source: WindowLike | undefined }) => void | ||
): void; | ||
} | ||
|
||
export class WindowLikeStream extends BaseMessageStream { | ||
constructor( | ||
private readonly windowLike: WindowLike, | ||
private readonly source: WindowLike | undefined = undefined, | ||
private readonly loadingState: | ||
| { loaded: boolean; onLoaded: Promise<void> } | ||
| undefined = undefined | ||
) { | ||
super(); | ||
|
||
windowLike.addEventListener("message", ({ data, source }) => { | ||
if (this.source && source !== this.source) { | ||
return; | ||
} | ||
if ("rpcMsg" in data) { | ||
this.onMessage(data.rpcMsg); | ||
} | ||
}); | ||
} | ||
|
||
public async write(message: Message): Promise<void> { | ||
if (this.loadingState && !this.loadingState.loaded) { | ||
await this.loadingState.onLoaded; | ||
} | ||
this.windowLike.postMessage({ rpcMsg: message }, "*"); | ||
} | ||
|
||
public toString(): string { | ||
return `${this.id}@${this.windowLike}`; | ||
} | ||
} |
Oops, something went wrong.