hypha-rpc 0.1.5 → 0.1.6-post1

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.
@@ -231,7 +231,7 @@ class RPC extends _utils_js__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
231
231
  this._emit_message = connection.emit_message.bind(connection);
232
232
  connection.on_message(this._on_message.bind(this));
233
233
  this._connection = connection;
234
- const updateServices = async () => {
234
+ const onConnected = async (connectionInfo) => {
235
235
  if (!this._silent && this._connection.manager_id) {
236
236
  console.log("Connection established, reporting services...");
237
237
  for (let service of Object.values(this._services)) {
@@ -244,12 +244,13 @@ class RPC extends _utils_js__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
244
244
  }
245
245
  } else {
246
246
  console.log(
247
- "Connection established, no manager id to report services",
247
+ "Connection established", connectionInfo
248
248
  );
249
249
  }
250
+ this._fire("connected", connectionInfo);
250
251
  };
251
- connection.on_connect(updateServices);
252
- updateServices();
252
+ connection.on_connected(onConnected);
253
+ onConnected();
253
254
  } else {
254
255
  this._emit_message = function () {
255
256
  console.log("No connection to emit message");
@@ -387,7 +388,11 @@ class RPC extends _utils_js__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
387
388
  Object.assign(main, extra.value);
388
389
  }
389
390
  this._fire(main["type"], main);
390
- } else {
391
+ }
392
+ else if (typeof message === "object") {
393
+ this._fire(message["type"], message);
394
+ }
395
+ else {
391
396
  throw new Error("Invalid message format");
392
397
  }
393
398
  }
@@ -398,7 +403,7 @@ class RPC extends _utils_js__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
398
403
  }
399
404
 
400
405
  async disconnect() {
401
- this._fire("disconnect");
406
+ this._fire("disconnected");
402
407
  await this._connection.disconnect();
403
408
  }
404
409
 
@@ -478,7 +483,7 @@ class RPC extends _utils_js__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
478
483
  svc.id = `${provider}:${service_id}`;
479
484
  return svc;
480
485
  } catch (e) {
481
- console.error("Failed to get remote service: " + service_uri, e);
486
+ console.warn("Failed to get remote service: " + service_uri, e);
482
487
  throw e;
483
488
  }
484
489
  }
@@ -790,8 +795,12 @@ class RPC extends _utils_js__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
790
795
  emit(main_message, extra_data) {
791
796
  (0,_utils_js__WEBPACK_IMPORTED_MODULE_0__.assert)(
792
797
  typeof main_message === "object" && main_message.type,
793
- "Invalid message, must be an object with a type field.",
798
+ "Invalid message, must be an object with a `type` fields.",
794
799
  );
800
+ if(!main_message.to) {
801
+ this._fire(main_message.type, main_message);
802
+ return
803
+ }
795
804
  let message_package = (0,_msgpack_msgpack__WEBPACK_IMPORTED_MODULE_2__.encode)(main_message);
796
805
  if (extra_data) {
797
806
  const extra = (0,_msgpack_msgpack__WEBPACK_IMPORTED_MODULE_2__.encode)(extra_data);
@@ -1976,11 +1985,11 @@ class WebRTCConnection {
1976
1985
  this._data_channel = channel;
1977
1986
  this._handle_message = null;
1978
1987
  this._reconnection_token = null;
1979
- this._disconnect_handler = null;
1980
- this._handle_connect = () => {};
1988
+ this._handle_disconnected = null;
1989
+ this._handle_connected = () => {};
1981
1990
  this.manager_id = null;
1982
1991
  this._data_channel.onopen = async () => {
1983
- this._handle_connect && this._handle_connect();
1992
+ this._handle_connected && this._handle_connected({channel: this._data_channel});
1984
1993
  };
1985
1994
  this._data_channel.onmessage = async (event) => {
1986
1995
  let data = event.data;
@@ -1991,18 +2000,18 @@ class WebRTCConnection {
1991
2000
  };
1992
2001
  const self = this;
1993
2002
  this._data_channel.onclose = function () {
1994
- if (this._disconnect_handler) this._disconnect_handler("closed");
2003
+ if (this._handle_disconnected) this._handle_disconnected("closed");
1995
2004
  console.log("websocket closed");
1996
2005
  self._data_channel = null;
1997
2006
  };
1998
2007
  }
1999
2008
 
2000
2009
  on_disconnected(handler) {
2001
- this._disconnect_handler = handler;
2010
+ this._handle_disconnected = handler;
2002
2011
  }
2003
2012
 
2004
- on_connect(handler) {
2005
- this._handle_connect = handler;
2013
+ on_connected(handler) {
2014
+ this._handle_connected = handler;
2006
2015
  }
2007
2016
 
2008
2017
  on_message(handler) {
@@ -4178,6 +4187,7 @@ class WebsocketRPCConnection {
4178
4187
  client_id,
4179
4188
  workspace,
4180
4189
  token,
4190
+ reconnection_token = null,
4181
4191
  timeout = 60,
4182
4192
  WebSocketClass = null,
4183
4193
  ) {
@@ -4186,11 +4196,11 @@ class WebsocketRPCConnection {
4186
4196
  this._client_id = client_id;
4187
4197
  this._workspace = workspace;
4188
4198
  this._token = token;
4189
- this._reconnection_token = null;
4199
+ this._reconnection_token = reconnection_token;
4190
4200
  this._websocket = null;
4191
4201
  this._handle_message = null;
4192
- this._handle_connect = null; // Connection open event handler
4193
- this._disconnect_handler = null; // Disconnection event handler
4202
+ this._handle_connected = null; // Connection open event handler
4203
+ this._handle_disconnected = null; // Disconnection event handler
4194
4204
  this._timeout = timeout;
4195
4205
  this._WebSocketClass = WebSocketClass || WebSocket; // Allow overriding the WebSocket class
4196
4206
  this._closed = false;
@@ -4205,12 +4215,12 @@ class WebsocketRPCConnection {
4205
4215
  this._handle_message = handler;
4206
4216
  }
4207
4217
 
4208
- on_connect(handler) {
4209
- this._handle_connect = handler;
4218
+ on_connected(handler) {
4219
+ this._handle_connected = handler;
4210
4220
  }
4211
4221
 
4212
4222
  on_disconnected(handler) {
4213
- this._disconnect_handler = handler;
4223
+ this._handle_disconnected = handler;
4214
4224
  }
4215
4225
 
4216
4226
  async _attempt_connection(server_url, attempt_fallback = true) {
@@ -4238,8 +4248,8 @@ class WebsocketRPCConnection {
4238
4248
  this._attempt_connection_with_query_params(server_url)
4239
4249
  .then(resolve)
4240
4250
  .catch(reject);
4241
- } else if (this._disconnect_handler) {
4242
- this._disconnect_handler(event.reason);
4251
+ } else if (this._handle_disconnected) {
4252
+ this._handle_disconnected(event.reason);
4243
4253
  }
4244
4254
  };
4245
4255
  });
@@ -4291,15 +4301,18 @@ class WebsocketRPCConnection {
4291
4301
  console.log(
4292
4302
  `Successfully connected to the server, workspace: ${this.connection_info.workspace}, manager_id: ${this.manager_id}`,
4293
4303
  );
4304
+ if(this.connection_info.announcement){
4305
+ console.log(`${this.connection_info.announcement}`);
4306
+ }
4294
4307
  resolve(this.connection_info);
4295
4308
  } else if (first_message.type == "error") {
4296
- const error = first_message.error || "Unknown error";
4309
+ const error = "ConnectionAbortedError: " + first_message.message;
4297
4310
  console.error("Failed to connect, " + error);
4298
4311
  reject(new Error(error));
4299
4312
  return;
4300
4313
  } else {
4301
- console.error("Unexpected message received from the server:", data);
4302
- reject(new Error("Unexpected message received from the server"));
4314
+ console.error("ConnectionAbortedError: Unexpected message received from the server:", data);
4315
+ reject(new Error("ConnectionAbortedError: Unexpected message received from the server"));
4303
4316
  return;
4304
4317
  }
4305
4318
  };
@@ -4341,8 +4354,8 @@ class WebsocketRPCConnection {
4341
4354
 
4342
4355
  this._websocket.onclose = this._handle_close.bind(this);
4343
4356
 
4344
- if (this._handle_connect) {
4345
- this._handle_connect(this);
4357
+ if (this._handle_connected) {
4358
+ this._handle_connected(this.connection_info);
4346
4359
  }
4347
4360
  return this.connection_info;
4348
4361
  } catch (error) {
@@ -4361,14 +4374,12 @@ class WebsocketRPCConnection {
4361
4374
  this._websocket &&
4362
4375
  this._websocket.readyState === WebSocket.CLOSED
4363
4376
  ) {
4364
- if ([1000].includes(event.code)) {
4377
+ if ([1000, 1001].includes(event.code)) {
4365
4378
  console.info(
4366
- "Websocket connection closed (code: %s): %s",
4367
- event.code,
4368
- event.reason,
4379
+ `Websocket connection closed (code: ${event.code}): ${event.reason}`
4369
4380
  );
4370
- if (this._disconnect_handler) {
4371
- this._disconnect_handler(event.reason);
4381
+ if (this._handle_disconnected) {
4382
+ this._handle_disconnected(event.reason);
4372
4383
  }
4373
4384
  this._closed = true;
4374
4385
  } else if (this._enable_reconnect) {
@@ -4380,10 +4391,11 @@ class WebsocketRPCConnection {
4380
4391
  let retry = 0;
4381
4392
  const reconnect = async () => {
4382
4393
  try {
4383
- console.info(
4394
+ console.warn(
4384
4395
  `Reconnecting to ${this._server_url.split("?")[0]} (attempt #${retry})`,
4385
4396
  );
4386
4397
  await this.open();
4398
+ console.warn(`Successfully reconnected to server ${this._server_url}`);
4387
4399
  } catch (e) {
4388
4400
  if (`${e}`.includes("ConnectionAbortedError")) {
4389
4401
  console.warn("Failed to reconnect, connection aborted:", e);
@@ -4392,7 +4404,6 @@ class WebsocketRPCConnection {
4392
4404
  console.warn("Failed to reconnect, connection aborted:", e);
4393
4405
  return;
4394
4406
  }
4395
- console.warn("Failed to reconnect:", e);
4396
4407
  await new Promise((resolve) => setTimeout(resolve, 1000));
4397
4408
  if (
4398
4409
  this._websocket &&
@@ -4411,8 +4422,8 @@ class WebsocketRPCConnection {
4411
4422
  reconnect();
4412
4423
  }
4413
4424
  } else {
4414
- if (this._disconnect_handler) {
4415
- this._disconnect_handler(event.reason);
4425
+ if (this._handle_disconnected) {
4426
+ this._handle_disconnected(event.reason);
4416
4427
  }
4417
4428
  }
4418
4429
  }
@@ -4457,6 +4468,7 @@ async function login(config) {
4457
4468
  const service_id = config.login_service_id || "public/*:hypha-login";
4458
4469
  const timeout = config.login_timeout || 60;
4459
4470
  const callback = config.login_callback;
4471
+ const profile = config.profile;
4460
4472
 
4461
4473
  const server = await connectToServer({
4462
4474
  name: "initial login client",
@@ -4464,13 +4476,14 @@ async function login(config) {
4464
4476
  });
4465
4477
  try {
4466
4478
  const svc = await server.get_service(service_id);
4479
+ (0,_utils_js__WEBPACK_IMPORTED_MODULE_1__.assert)(svc, `Failed to get the login service: ${service_id}`);
4467
4480
  const context = await svc.start();
4468
4481
  if (callback) {
4469
4482
  await callback(context);
4470
4483
  } else {
4471
4484
  console.log(`Please open your browser and login at ${context.login_url}`);
4472
4485
  }
4473
- return await svc.check(context.key, timeout);
4486
+ return await svc.check(context.key, timeout, profile);
4474
4487
  } catch (error) {
4475
4488
  throw error;
4476
4489
  } finally {
@@ -4497,6 +4510,7 @@ async function connectToServer(config) {
4497
4510
  clientId,
4498
4511
  config.workspace,
4499
4512
  config.token,
4513
+ config.reconnection_token,
4500
4514
  config.method_timeout || 60,
4501
4515
  config.WebSocketClass,
4502
4516
  );
@@ -4533,17 +4547,13 @@ async function connectToServer(config) {
4533
4547
  return await wm.get_service(query + ":default");
4534
4548
  }
4535
4549
 
4536
- async function disconnect() {
4537
- await rpc.disconnect();
4538
- await connection.disconnect();
4539
- }
4540
4550
  if (connection_info) {
4541
4551
  wm.config = Object.assign(wm.config, connection_info);
4542
4552
  }
4543
4553
  wm.export = _export;
4544
4554
  wm.getPlugin = getPlugin;
4545
4555
  wm.listPlugins = wm.listServices;
4546
- wm.disconnect = disconnect;
4556
+ wm.disconnect = rpc.disconnect.bind(rpc);
4547
4557
  wm.registerCodec = rpc.register_codec.bind(rpc);
4548
4558
  wm.emit = rpc.emit;
4549
4559
  wm.on = rpc.on;
@@ -4551,7 +4561,7 @@ async function connectToServer(config) {
4551
4561
  rpc.on("force-exit", async (message) => {
4552
4562
  if (message.from === "*/" + connection.manager_id) {
4553
4563
  console.log("Disconnecting from server, reason:", message.reason);
4554
- await disconnect();
4564
+ await rpc.disconnect();
4555
4565
  }
4556
4566
  });
4557
4567
  }
@@ -4565,7 +4575,9 @@ async function connectToServer(config) {
4565
4575
  [undefined, true, false, "auto"].includes(webrtc),
4566
4576
  "webrtc must be true, false or 'auto'",
4567
4577
  );
4568
- const svc = await _get_service(query);
4578
+ // pass other arguments to get_service
4579
+ const otherArgs = Array.prototype.slice.call(arguments, 3);
4580
+ const svc = await _get_service(query, ...otherArgs);
4569
4581
  if (webrtc === true || webrtc === "auto") {
4570
4582
  if (svc.id.includes(":") && svc.id.includes("/")) {
4571
4583
  const client = svc.id.split(":")[0];