hypha-rpc 0.1.0-post5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,203 @@
1
+ import { expect } from "chai";
2
+ import { login, connectToServer } from "../src/websocket-client.js";
3
+ import {
4
+ registerRTCService,
5
+ getRTCService,
6
+ } from "../src/webrtc-client.js";
7
+
8
+ const SERVER_URL = "https://hypha.aicell.io";
9
+
10
+ class ImJoyPlugin {
11
+ async setup() {}
12
+ async add2(arg) {
13
+ return arg + 2;
14
+ }
15
+ }
16
+
17
+ describe("RPC", async () => {
18
+ it("should connect to the server", async () => {
19
+ const api = await connectToServer({
20
+ server_url: SERVER_URL,
21
+ client_id: "test-plugin-1",
22
+ });
23
+ expect(typeof api.log).to.equal("function");
24
+ await api.disconnect();
25
+ }).timeout(20000);
26
+
27
+ it("should connect via webrtc", async () => {
28
+ const service_id = "test-rtc-service-1";
29
+ const server = await connectToServer({
30
+ server_url: SERVER_URL,
31
+ client_id: "test-plugin-1",
32
+ });
33
+ await server.registerService({
34
+ id: "echo-service-rtc",
35
+ config: {
36
+ visibility: "public",
37
+ },
38
+ type: "echo",
39
+ echo: (x) => x,
40
+ });
41
+ await registerRTCService(server, service_id);
42
+ const pc = await getRTCService(server, service_id);
43
+ const svc = await pc.getService("echo-service-rtc");
44
+ expect(await svc.echo("hello")).to.equal("hello");
45
+ }).timeout(20000);
46
+
47
+ it("should login to the server", async () => {
48
+ const TOKEN = "sf31df234";
49
+
50
+ async function callback(context) {
51
+ console.log(`By passing login: ${context["login_url"]}`);
52
+ const response = await fetch(
53
+ `${context["report_url"]}?key=${context["key"]}&token=${TOKEN}`,
54
+ );
55
+ if (!response.ok) throw new Error("Network response was not ok");
56
+ }
57
+
58
+ // We use ai.imjoy.io to test the login for now
59
+ const token = await login({
60
+ server_url: SERVER_URL,
61
+ login_callback: callback,
62
+ login_timeout: 3,
63
+ });
64
+ expect(token).to.equal(TOKEN);
65
+ }).timeout(20000);
66
+
67
+ it("should connect to the server", async () => {
68
+ const api = await connectToServer({
69
+ server_url: SERVER_URL,
70
+ client_id: "test-plugin-1",
71
+ });
72
+ // await api.log("hello")
73
+ const size = 100000;
74
+ const data = await api.echo(new ArrayBuffer(size));
75
+ expect(data.byteLength).to.equal(size);
76
+ function multiply(a, b) {
77
+ /* multiply two numbers */
78
+ return a * b;
79
+ }
80
+ await api.register_service({
81
+ name: "my service",
82
+ id: "test-service",
83
+ description: "test service",
84
+ config: { visibility: "public" },
85
+ multiply,
86
+ });
87
+ const svc = await api.rpc.get_remote_service("test-service");
88
+ expect(svc.multiply.__doc__).to.equal("multiply two numbers");
89
+ expect(svc.multiply.__sig__).to.equal("multiply(a, b)");
90
+ expect(await svc.multiply(2, 2)).to.equal(4);
91
+ await api.export(new ImJoyPlugin());
92
+ const dsvc = await api.rpc.get_remote_service("default");
93
+ expect(await dsvc.add2(3)).to.equal(5);
94
+ await api.disconnect();
95
+ }).timeout(20000);
96
+
97
+ it("should encode/decode data", async () => {
98
+ const plugin_interface = {
99
+ id: "default",
100
+ embed: {
101
+ embed: {
102
+ value: 8873,
103
+ sayHello: () => {
104
+ console.log("hello");
105
+ return true;
106
+ },
107
+ },
108
+ },
109
+ echo: (msg) => {
110
+ return msg;
111
+ },
112
+ };
113
+ const server = await connectToServer({
114
+ server_url: SERVER_URL,
115
+ client_id: "test-plugin-1",
116
+ });
117
+ await server.register_service(plugin_interface);
118
+ const api = await server.rpc.get_remote_service("default");
119
+
120
+ const msg = "this is an messge.";
121
+ expect(api.embed.embed).to.include.all.keys("value", "sayHello");
122
+ expect(api.embed.embed.value).to.equal(8873);
123
+ expect(await api.embed.embed.sayHello()).to.equal(true);
124
+ expect(await api.echo(msg)).to.equal(msg);
125
+ expect(await api.echo(99)).to.equal(99);
126
+ const ret = await api.echo(new Uint16Array(new ArrayBuffer(4)));
127
+ expect(ret.length).to.equal(2);
128
+ expect(
129
+ (await api.echo(new Blob(["133"], { type: "text33" }))).type,
130
+ ).to.equal("text33");
131
+ expect((await api.echo(new Map([["1", 99]]))).get("1")).to.equal(99);
132
+ expect((await api.echo(new Set([38, "88", 38]))).size).to.equal(2);
133
+ expect((await api.echo(new ArrayBuffer(101))).byteLength).to.equal(101);
134
+ expect(await api.echo(true)).to.equal(true);
135
+ const date = new Date(2018, 11, 24, 10, 33, 30, 0);
136
+ expect((await api.echo(date)).getTime()).to.equal(date.getTime());
137
+ // const imageData = new ImageData(200, 100);
138
+ // expect((await api.echo(imageData)).width).to.equal(200);
139
+ expect(await api.echo({ a: 1, b: 93 })).to.include.all.keys("a", "b");
140
+ expect(await api.echo(["12", 33, { foo: "bar" }])).to.include(33);
141
+ expect(await api.echo(["12", 33, { foo: "bar" }])).to.include("12");
142
+ expect(await api.echo(["12", 33, { foo: "bar" }])).to.deep.include({
143
+ foo: "bar",
144
+ });
145
+ const blob = new Blob(["hello"], { type: "text/plain" });
146
+ expect(await api.echo(blob)).to.be.an.instanceof(Blob);
147
+ const file = new File(["foo"], "foo.txt", {
148
+ type: "text/plain",
149
+ });
150
+ expect(await api.echo(file)).to.be.an.instanceof(Blob);
151
+
152
+ // send an interface
153
+ const itf = {
154
+ id: "hello",
155
+ add(a, b) {
156
+ return a + b;
157
+ },
158
+ };
159
+ await server.register_service(itf);
160
+ const received_itf = await api.echo(itf);
161
+ expect(await received_itf.add(1, 3)).to.equal(4);
162
+ expect(await received_itf.add(9, 3)).to.equal(12);
163
+ expect(await received_itf.add("12", 2)).to.equal("122");
164
+ await server.disconnect();
165
+ }).timeout(40000);
166
+
167
+ it("should encode and decode custom object", async () => {
168
+ const api = await connectToServer({
169
+ server_url: SERVER_URL,
170
+ client_id: "test-plugin-1",
171
+ });
172
+
173
+ class Cat {
174
+ constructor(name, color, age) {
175
+ this.name = name;
176
+ this.color = color;
177
+ this.age = age;
178
+ }
179
+ }
180
+
181
+ api.registerCodec({
182
+ name: "cat",
183
+ type: Cat,
184
+ encoder: (obj) => {
185
+ return { name: obj.name, color: obj.color, age: obj.age };
186
+ },
187
+ decoder: (encoded_obj) => {
188
+ return new Cat(encoded_obj.name, encoded_obj.color, encoded_obj.age);
189
+ },
190
+ });
191
+
192
+ const bobo = new Cat("boboshu", "mixed", 0.67);
193
+ const cat = await api.echo(bobo);
194
+ const result =
195
+ cat instanceof Cat &&
196
+ bobo.name === cat.name &&
197
+ bobo.color === cat.color &&
198
+ bobo.age === cat.age;
199
+ expect(result).to.equal(true);
200
+
201
+ await api.disconnect();
202
+ }).timeout(20000);
203
+ });
@@ -0,0 +1,44 @@
1
+ const path = require('path');
2
+
3
+ module.exports = {
4
+ mode: process.env.NODE_ENV || 'development',
5
+ entry: {
6
+ 'hyphaWebsocketClient': path.resolve(__dirname, 'src', 'websocket-client.js'),
7
+ },
8
+ output: {
9
+ globalObject: 'this',
10
+ path: path.resolve(__dirname, 'dist'),
11
+ filename: (pathData) => {
12
+ const outputNames = {
13
+ "hyphaWebsocketClient": "hypha-rpc-websocket",
14
+ };
15
+ const name = outputNames[pathData.chunk.name];
16
+ return process.env.NODE_ENV === 'production' ? name + '.min.js' : name + '.js';
17
+ },
18
+ library: '[name]',
19
+ libraryTarget: 'umd',
20
+ umdNamedDefine: true
21
+ },
22
+ devtool: 'source-map',
23
+ devServer: {
24
+ static: {
25
+ directory: path.resolve(__dirname, 'dist'),
26
+ },
27
+ port: 9099,
28
+ hot: true,
29
+ headers: {
30
+ "Access-Control-Allow-Origin": "*",
31
+ "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
32
+ "Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization"
33
+ }
34
+ },
35
+ plugins: [],
36
+ module: {
37
+ rules: [
38
+ {
39
+ test: /\.css$/,
40
+ use: ['style-loader', 'css-loader'],
41
+ },
42
+ ],
43
+ },
44
+ };