Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

alice-bob

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

alice-bob - npm Package Compare versions

Comparing version 1.2.0 to 1.3.0

45

dist/cjs/index.d.ts
/**
* Payload.
*/
export interface Payload {
export interface Payload<T> {
/**

@@ -12,3 +12,3 @@ * Payload id.

*/
method: string | symbol;
method: keyof T;
/**

@@ -19,7 +19,7 @@ * The arguments passed to the method.

}
export declare type PayloadMethod = (payload: Payload) => Promise<void> | void;
export declare type PayloadMethod<T> = (payload: Payload<T>) => Promise<unknown> | unknown;
/**
* Agent.
*/
export declare type Agent<T> = {
export declare type Agent<A, B> = {
/**

@@ -39,3 +39,3 @@ * Whether or not to log debugging information.

*/
send: PayloadMethod;
send: PayloadMethod<Agent<B, A>>;
/**

@@ -46,3 +46,3 @@ * Returns the send method. Used in contexts where it might

*/
deferredSend: () => PayloadMethod;
deferredSend: () => PayloadMethod<Agent<B, A>>;
/**

@@ -52,3 +52,3 @@ * Called by the user with the payload when it is received from their transport.

*/
receive: PayloadMethod;
receive: PayloadMethod<Agent<A, B>>;
/**

@@ -69,7 +69,3 @@ * Overridable logging function. Defaults to `console.log()` and prepends `agent.name`.

__reject__: (id: number, message: string) => void;
} & T;
export interface AgentsOptions {
debug?: boolean;
}
export declare type AgentRecord<T> = Record<string | symbol, (...args: unknown[]) => T>;
} & A;
/**

@@ -93,7 +89,7 @@ * AliceBob class.

*/
local: Agent<A>;
local: Agent<A, B>;
/**
* The remote Agent.
*/
remote: Agent<B>;
remote: Agent<B, A>;
private send;

@@ -108,3 +104,3 @@ /**

*/
constructor(send?: PayloadMethod);
constructor(send?: PayloadMethod<Agent<A, B>>);
/**

@@ -116,8 +112,17 @@ * Returns the agents tuple `[alice, bob]`.

* const [alice, bob] = new Alice<Local, Remote>().agents()
*
* // to enable debugging on local (alice)
* const [alice, bob] = new Alice<Local, Remote>().agents({ debug: true })
*
* // use different names:
* const [alice, bob] = new Alice<Local, Remote>().agents(
* { name: 'server', debug: true },
* { name: 'client' }
* )
* ```
*
* @param [options]
* @param [options.debug] Whether to enable debugging.
* @param [local] Local agent overrides.
* @param [remote] Remote agent overrides.
*/
agents({ debug }?: AgentsOptions): [Agent<A>, Agent<B>];
agents(local?: Partial<Agent<A, B>> | null, remote?: Partial<Agent<B, A>> | null): [Agent<A, B>, Agent<B, A>];
}

@@ -135,3 +140,3 @@ /**

*/
constructor(send?: PayloadMethod);
constructor(send?: PayloadMethod<Agent<A, B>>);
}

@@ -149,3 +154,3 @@ /**

*/
constructor(send?: PayloadMethod);
constructor(send?: PayloadMethod<Agent<A, B>>);
}

@@ -47,3 +47,2 @@ "use strict";

this.local.log(' ├> SEND ├>', `${id} ${this.local.name}`.padEnd(12), '│', method, args);
// this.local.log(' ├> SEND ├>', id, this.remote.name.padEnd(10), '│', method, args)
return this.local.send({ id, method, args });

@@ -60,38 +59,36 @@ };

this.local.log('<┤ RECV │', `${this.remote.name} ${id}`.padStart(12), '<┤', method, args);
// this.local.log('<┤ RECV │', this.remote.name.padStart(10), id, '<┤', method, args)
let error;
let result;
const fn = this.local[method];
if (typeof fn !== 'function') {
throw new TypeError(`Agent method "${method.toString()}" is not a function. Instead found: ${typeof fn}`);
}
const hasCallback = typeof method === 'string' && method[0] !== '_';
if (hasCallback) {
// save `error` out of try catch scope so we can report in `finally`
// otherwise if `this.send()` throws earlier we lose it
let error;
try {
const result = await this.local[method](...args);
try {
result = await fn(...args);
if (hasCallback)
await this.send({
id: ++this.id,
method: '__resolve__',
args: [id, result]
args: [id, result],
});
}
catch (e) {
error = e;
}
catch (e) {
error = e;
if (hasCallback)
await this.send({
id: ++this.id,
method: '__reject__',
args: [id, error.message]
args: [id, error.message],
});
}
finally {
// we log instead of throwing because the
// error belongs to the caller(remote).
// we don't want the remote to be able to
// raise exceptions in our execution thread
if (error)
this.local.log(error);
}
}
else {
// eslint-disable-next-line
;
this.local[method](...args);
finally {
// we log instead of throwing because the
// error belongs to the caller(remote).
// we don't want the remote to be able to
// raise exceptions in our execution thread
if (error && this.local.debug)
this.local.log(error);
}
return result;
};

@@ -104,4 +101,3 @@ this.local = {

if (this.local.deferredSend) {
this.local.send = this.local.deferredSend();
this.local.send(data);
return this.local.deferredSend()(data);
}

@@ -115,6 +111,6 @@ else {

__resolve__: (id, result) => (0, util_1.pop)(this.callbacks, id).resolve(result),
__reject__: (id, message) => (0, util_1.pop)(this.callbacks, id).reject(new Error(message))
__reject__: (id, message) => (0, util_1.pop)(this.callbacks, id).reject(new Error(message)),
};
this.remote = new Proxy({
name: 'remote'
name: 'remote',
}, {

@@ -147,3 +143,3 @@ get: (target, prop) => {

return true;
}
},
});

@@ -154,3 +150,6 @@ }

// but in the future this might change so we keep it
// because it allows for:
//
// const [alice, bob] = new Alice()
//
// *[Symbol.iterator]() {

@@ -166,9 +165,19 @@ // yield this.local

* const [alice, bob] = new Alice<Local, Remote>().agents()
*
* // to enable debugging on local (alice)
* const [alice, bob] = new Alice<Local, Remote>().agents({ debug: true })
*
* // use different names:
* const [alice, bob] = new Alice<Local, Remote>().agents(
* { name: 'server', debug: true },
* { name: 'client' }
* )
* ```
*
* @param [options]
* @param [options.debug] Whether to enable debugging.
* @param [local] Local agent overrides.
* @param [remote] Remote agent overrides.
*/
agents({ debug = false } = { debug: false }) {
this.local.debug = debug;
agents(local, remote) {
Object.assign(this.local, local);
Object.assign(this.remote, remote);
return [this.local, this.remote];

@@ -175,0 +184,0 @@ }

/**
* Payload.
*/
export interface Payload {
export interface Payload<T> {
/**

@@ -12,3 +12,3 @@ * Payload id.

*/
method: string | symbol;
method: keyof T;
/**

@@ -19,7 +19,7 @@ * The arguments passed to the method.

}
export declare type PayloadMethod = (payload: Payload) => Promise<void> | void;
export declare type PayloadMethod<T> = (payload: Payload<T>) => Promise<unknown> | unknown;
/**
* Agent.
*/
export declare type Agent<T> = {
export declare type Agent<A, B> = {
/**

@@ -39,3 +39,3 @@ * Whether or not to log debugging information.

*/
send: PayloadMethod;
send: PayloadMethod<Agent<B, A>>;
/**

@@ -46,3 +46,3 @@ * Returns the send method. Used in contexts where it might

*/
deferredSend: () => PayloadMethod;
deferredSend: () => PayloadMethod<Agent<B, A>>;
/**

@@ -52,3 +52,3 @@ * Called by the user with the payload when it is received from their transport.

*/
receive: PayloadMethod;
receive: PayloadMethod<Agent<A, B>>;
/**

@@ -69,7 +69,3 @@ * Overridable logging function. Defaults to `console.log()` and prepends `agent.name`.

__reject__: (id: number, message: string) => void;
} & T;
export interface AgentsOptions {
debug?: boolean;
}
export declare type AgentRecord<T> = Record<string | symbol, (...args: unknown[]) => T>;
} & A;
/**

@@ -93,7 +89,7 @@ * AliceBob class.

*/
local: Agent<A>;
local: Agent<A, B>;
/**
* The remote Agent.
*/
remote: Agent<B>;
remote: Agent<B, A>;
private send;

@@ -108,3 +104,3 @@ /**

*/
constructor(send?: PayloadMethod);
constructor(send?: PayloadMethod<Agent<A, B>>);
/**

@@ -116,8 +112,17 @@ * Returns the agents tuple `[alice, bob]`.

* const [alice, bob] = new Alice<Local, Remote>().agents()
*
* // to enable debugging on local (alice)
* const [alice, bob] = new Alice<Local, Remote>().agents({ debug: true })
*
* // use different names:
* const [alice, bob] = new Alice<Local, Remote>().agents(
* { name: 'server', debug: true },
* { name: 'client' }
* )
* ```
*
* @param [options]
* @param [options.debug] Whether to enable debugging.
* @param [local] Local agent overrides.
* @param [remote] Remote agent overrides.
*/
agents({ debug }?: AgentsOptions): [Agent<A>, Agent<B>];
agents(local?: Partial<Agent<A, B>> | null, remote?: Partial<Agent<B, A>> | null): [Agent<A, B>, Agent<B, A>];
}

@@ -135,3 +140,3 @@ /**

*/
constructor(send?: PayloadMethod);
constructor(send?: PayloadMethod<Agent<A, B>>);
}

@@ -149,3 +154,3 @@ /**

*/
constructor(send?: PayloadMethod);
constructor(send?: PayloadMethod<Agent<A, B>>);
}

@@ -44,3 +44,2 @@ import { pop } from './util';

this.local.log(' ├> SEND ├>', `${id} ${this.local.name}`.padEnd(12), '│', method, args);
// this.local.log(' ├> SEND ├>', id, this.remote.name.padEnd(10), '│', method, args)
return this.local.send({ id, method, args });

@@ -57,38 +56,36 @@ };

this.local.log('<┤ RECV │', `${this.remote.name} ${id}`.padStart(12), '<┤', method, args);
// this.local.log('<┤ RECV │', this.remote.name.padStart(10), id, '<┤', method, args)
let error;
let result;
const fn = this.local[method];
if (typeof fn !== 'function') {
throw new TypeError(`Agent method "${method.toString()}" is not a function. Instead found: ${typeof fn}`);
}
const hasCallback = typeof method === 'string' && method[0] !== '_';
if (hasCallback) {
// save `error` out of try catch scope so we can report in `finally`
// otherwise if `this.send()` throws earlier we lose it
let error;
try {
const result = await this.local[method](...args);
try {
result = await fn(...args);
if (hasCallback)
await this.send({
id: ++this.id,
method: '__resolve__',
args: [id, result]
args: [id, result],
});
}
catch (e) {
error = e;
}
catch (e) {
error = e;
if (hasCallback)
await this.send({
id: ++this.id,
method: '__reject__',
args: [id, error.message]
args: [id, error.message],
});
}
finally {
// we log instead of throwing because the
// error belongs to the caller(remote).
// we don't want the remote to be able to
// raise exceptions in our execution thread
if (error)
this.local.log(error);
}
}
else {
// eslint-disable-next-line
;
this.local[method](...args);
finally {
// we log instead of throwing because the
// error belongs to the caller(remote).
// we don't want the remote to be able to
// raise exceptions in our execution thread
if (error && this.local.debug)
this.local.log(error);
}
return result;
};

@@ -101,4 +98,3 @@ this.local = {

if (this.local.deferredSend) {
this.local.send = this.local.deferredSend();
this.local.send(data);
return this.local.deferredSend()(data);
}

@@ -112,6 +108,6 @@ else {

__resolve__: (id, result) => pop(this.callbacks, id).resolve(result),
__reject__: (id, message) => pop(this.callbacks, id).reject(new Error(message))
__reject__: (id, message) => pop(this.callbacks, id).reject(new Error(message)),
};
this.remote = new Proxy({
name: 'remote'
name: 'remote',
}, {

@@ -144,3 +140,3 @@ get: (target, prop) => {

return true;
}
},
});

@@ -151,3 +147,6 @@ }

// but in the future this might change so we keep it
// because it allows for:
//
// const [alice, bob] = new Alice()
//
// *[Symbol.iterator]() {

@@ -163,9 +162,19 @@ // yield this.local

* const [alice, bob] = new Alice<Local, Remote>().agents()
*
* // to enable debugging on local (alice)
* const [alice, bob] = new Alice<Local, Remote>().agents({ debug: true })
*
* // use different names:
* const [alice, bob] = new Alice<Local, Remote>().agents(
* { name: 'server', debug: true },
* { name: 'client' }
* )
* ```
*
* @param [options]
* @param [options.debug] Whether to enable debugging.
* @param [local] Local agent overrides.
* @param [remote] Remote agent overrides.
*/
agents({ debug = false } = { debug: false }) {
this.local.debug = debug;
agents(local, remote) {
Object.assign(this.local, local);
Object.assign(this.remote, remote);
return [this.local, this.remote];

@@ -172,0 +181,0 @@ }

@@ -6,3 +6,3 @@ {

"description": "transport agnostic strongly typed duplex rpc interfaces",
"version": "1.2.0",
"version": "1.3.0",
"license": "MIT",

@@ -9,0 +9,0 @@ "repository": {

@@ -146,3 +146,3 @@ <h1 align="center">👫<br>alice-bob</h1>

[src/index.ts:6-19](https://github.com/stagas/alice-bob/blob/b63afb0d1105e8fd012dfb698294cd0868c5c8bc/src/index.ts#L6-L19 "Source code on GitHub")
[src/index.ts:6-19](https://github.com/stagas/alice-bob/blob/49f3271f05164b93761078f72c5b81df75400102/src/index.ts#L6-L19 "Source code on GitHub")

@@ -153,3 +153,3 @@ Payload.

[src/index.ts:10-10](https://github.com/stagas/alice-bob/blob/b63afb0d1105e8fd012dfb698294cd0868c5c8bc/src/index.ts#L10-L10 "Source code on GitHub")
[src/index.ts:10-10](https://github.com/stagas/alice-bob/blob/49f3271f05164b93761078f72c5b81df75400102/src/index.ts#L10-L10 "Source code on GitHub")

@@ -162,11 +162,11 @@ Payload id.

[src/index.ts:14-14](https://github.com/stagas/alice-bob/blob/b63afb0d1105e8fd012dfb698294cd0868c5c8bc/src/index.ts#L14-L14 "Source code on GitHub")
[src/index.ts:14-14](https://github.com/stagas/alice-bob/blob/49f3271f05164b93761078f72c5b81df75400102/src/index.ts#L14-L14 "Source code on GitHub")
Method to call.
Type: ([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))
Type: any
#### args
[src/index.ts:18-18](https://github.com/stagas/alice-bob/blob/b63afb0d1105e8fd012dfb698294cd0868c5c8bc/src/index.ts#L18-L18 "Source code on GitHub")
[src/index.ts:18-18](https://github.com/stagas/alice-bob/blob/49f3271f05164b93761078f72c5b81df75400102/src/index.ts#L18-L18 "Source code on GitHub")

@@ -179,3 +179,3 @@ The arguments passed to the method.

[src/index.ts:26-68](https://github.com/stagas/alice-bob/blob/b63afb0d1105e8fd012dfb698294cd0868c5c8bc/src/index.ts#L23-L25 "Source code on GitHub")
[src/index.ts:28-70](https://github.com/stagas/alice-bob/blob/49f3271f05164b93761078f72c5b81df75400102/src/index.ts#L25-L27 "Source code on GitHub")

@@ -188,3 +188,3 @@ Agent.

[src/index.ts:30-30](https://github.com/stagas/alice-bob/blob/b63afb0d1105e8fd012dfb698294cd0868c5c8bc/src/index.ts#L30-L30 "Source code on GitHub")
[src/index.ts:32-32](https://github.com/stagas/alice-bob/blob/49f3271f05164b93761078f72c5b81df75400102/src/index.ts#L32-L32 "Source code on GitHub")

@@ -197,3 +197,3 @@ Whether or not to log debugging information.

[src/index.ts:36-36](https://github.com/stagas/alice-bob/blob/b63afb0d1105e8fd012dfb698294cd0868c5c8bc/src/index.ts#L36-L36 "Source code on GitHub")
[src/index.ts:38-38](https://github.com/stagas/alice-bob/blob/49f3271f05164b93761078f72c5b81df75400102/src/index.ts#L38-L38 "Source code on GitHub")

@@ -207,11 +207,11 @@ The name of the agent. Defaults to either 'alice' or 'bob' depending

[src/index.ts:41-41](https://github.com/stagas/alice-bob/blob/b63afb0d1105e8fd012dfb698294cd0868c5c8bc/src/index.ts#L41-L41 "Source code on GitHub")
[src/index.ts:43-43](https://github.com/stagas/alice-bob/blob/49f3271f05164b93761078f72c5b81df75400102/src/index.ts#L43-L43 "Source code on GitHub")
The send method overriden by the user to any transport.
Type: PayloadMethod
Type: PayloadMethod<[Agent](#agent)\<B, A>>
#### deferredSend
[src/index.ts:47-47](https://github.com/stagas/alice-bob/blob/b63afb0d1105e8fd012dfb698294cd0868c5c8bc/src/index.ts#L47-L47 "Source code on GitHub")
[src/index.ts:49-49](https://github.com/stagas/alice-bob/blob/49f3271f05164b93761078f72c5b81df75400102/src/index.ts#L49-L49 "Source code on GitHub")

@@ -221,7 +221,7 @@ Returns the send method. Used in contexts where it might

Type: function (): PayloadMethod
Type: function (): PayloadMethod<[Agent](#agent)\<B, A>>
#### log
[src/index.ts:57-57](https://github.com/stagas/alice-bob/blob/b63afb0d1105e8fd012dfb698294cd0868c5c8bc/src/index.ts#L57-L57 "Source code on GitHub")
[src/index.ts:59-59](https://github.com/stagas/alice-bob/blob/49f3271f05164b93761078f72c5b81df75400102/src/index.ts#L59-L59 "Source code on GitHub")

@@ -234,3 +234,3 @@ Overridable logging function. Defaults to `console.log()` and prepends `agent.name`.

[src/index.ts:82-269](https://github.com/stagas/alice-bob/blob/b63afb0d1105e8fd012dfb698294cd0868c5c8bc/src/index.ts#L82-L269 "Source code on GitHub")
[src/index.ts:78-283](https://github.com/stagas/alice-bob/blob/49f3271f05164b93761078f72c5b81df75400102/src/index.ts#L78-L283 "Source code on GitHub")

@@ -241,23 +241,23 @@ AliceBob class.

* `send` **PayloadMethod?** The `send` payload method provided by the user. Will be called with a payload to be sent.
* `send` **PayloadMethod<[Agent](#agent)\<A, B>>?** The `send` payload method provided by the user. Will be called with a payload to be sent.
#### local
[src/index.ts:95-95](https://github.com/stagas/alice-bob/blob/b63afb0d1105e8fd012dfb698294cd0868c5c8bc/src/index.ts#L95-L95 "Source code on GitHub")
[src/index.ts:91-91](https://github.com/stagas/alice-bob/blob/49f3271f05164b93761078f72c5b81df75400102/src/index.ts#L91-L91 "Source code on GitHub")
The local Agent.
Type: [Agent](#agent)\<A>
Type: [Agent](#agent)\<A, B>
#### remote
[src/index.ts:99-99](https://github.com/stagas/alice-bob/blob/b63afb0d1105e8fd012dfb698294cd0868c5c8bc/src/index.ts#L99-L99 "Source code on GitHub")
[src/index.ts:95-95](https://github.com/stagas/alice-bob/blob/49f3271f05164b93761078f72c5b81df75400102/src/index.ts#L95-L95 "Source code on GitHub")
The remote Agent.
Type: [Agent](#agent)\<B>
Type: [Agent](#agent)\<B, A>
#### agents
[src/index.ts:265-268](https://github.com/stagas/alice-bob/blob/b63afb0d1105e8fd012dfb698294cd0868c5c8bc/src/index.ts#L265-L268 "Source code on GitHub")
[src/index.ts:275-282](https://github.com/stagas/alice-bob/blob/49f3271f05164b93761078f72c5b81df75400102/src/index.ts#L275-L282 "Source code on GitHub")

@@ -270,2 +270,11 @@ Returns the agents tuple `[alice, bob]`.

const [alice, bob] = new Alice<Local, Remote>().agents()
// to enable debugging on local (alice)
const [alice, bob] = new Alice<Local, Remote>().agents({ debug: true })
// use different names:
const [alice, bob] = new Alice<Local, Remote>().agents(
{ name: 'server', debug: true },
{ name: 'client' }
)
```

@@ -275,9 +284,8 @@

* `options` **AgentsOptions** (optional, default `{debug:false}`)
* `local` **(Partial<[Agent](#agent)\<A, B>> | null)?** Local agent overrides.
* `remote` **(Partial<[Agent](#agent)\<B, A>> | null)?** Remote agent overrides.
* `options.debug` Whether to enable debugging. (optional, default `false`)
### Alice
[src/index.ts:277-287](https://github.com/stagas/alice-bob/blob/b63afb0d1105e8fd012dfb698294cd0868c5c8bc/src/index.ts#L277-L287 "Source code on GitHub")
[src/index.ts:291-301](https://github.com/stagas/alice-bob/blob/49f3271f05164b93761078f72c5b81df75400102/src/index.ts#L291-L301 "Source code on GitHub")

@@ -290,7 +298,7 @@ **Extends AliceBob**

* `send` **PayloadMethod?** The `send` payload method provided by the user. Will be called with a payload to be sent.
* `send` **PayloadMethod<[Agent](#agent)\<A, B>>?** The `send` payload method provided by the user. Will be called with a payload to be sent.
### Bob
[src/index.ts:295-305](https://github.com/stagas/alice-bob/blob/b63afb0d1105e8fd012dfb698294cd0868c5c8bc/src/index.ts#L295-L305 "Source code on GitHub")
[src/index.ts:309-319](https://github.com/stagas/alice-bob/blob/49f3271f05164b93761078f72c5b81df75400102/src/index.ts#L309-L319 "Source code on GitHub")

@@ -303,3 +311,3 @@ **Extends AliceBob**

* `send` **PayloadMethod?** The `send` payload method provided by the user. Will be called with a payload to be sent.
* `send` **PayloadMethod<[Agent](#agent)\<A, B>>?** The `send` payload method provided by the user. Will be called with a payload to be sent.

@@ -306,0 +314,0 @@ ## Contribute

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc