Merge pull request #3111 from nymtech/feature/sdk-1.1.7
Typescript SDK 1.1.7
This commit is contained in:
@@ -9,6 +9,9 @@ let nym: NymMixnetClient | null = null;
|
||||
async function main() {
|
||||
nym = await createNymMixnetClient();
|
||||
|
||||
// add nym client to the Window globally, so that it can be used from the dev tools console
|
||||
(window as any).nym = nym;
|
||||
|
||||
if (!nym) {
|
||||
console.error('Oh no! Could not create client');
|
||||
return;
|
||||
@@ -38,6 +41,8 @@ async function main() {
|
||||
};
|
||||
}
|
||||
|
||||
nym.events.subscribeToRawMessageReceivedEvent((e) => console.log('Received: ', e.args.payload));
|
||||
|
||||
// start up the client
|
||||
await nym.client.start({
|
||||
clientId: 'My awesome client',
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@nymproject/sdk",
|
||||
"version": "1.1.6",
|
||||
"version": "1.1.7",
|
||||
"license": "Apache-2.0",
|
||||
"author": "Nym Technologies SA",
|
||||
"type": "module",
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
LoadedEvent,
|
||||
MimeTypes,
|
||||
StringMessageReceivedEvent,
|
||||
RawMessageReceivedEvent,
|
||||
} from './types';
|
||||
|
||||
/**
|
||||
@@ -89,6 +90,12 @@ export const createNymMixnetClient = async (options?: {
|
||||
getSubscriptions<BinaryMessageReceivedEvent>(EventKinds.BinaryMessageReceived).unshift(handler);
|
||||
};
|
||||
},
|
||||
subscribeToRawMessageReceivedEvent: (handler) => {
|
||||
getSubscriptions<RawMessageReceivedEvent>(EventKinds.RawMessageReceived).push(handler);
|
||||
return () => {
|
||||
getSubscriptions<RawMessageReceivedEvent>(EventKinds.RawMessageReceived).unshift(handler);
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
// let comlink handle interop with the web worker
|
||||
|
||||
@@ -60,19 +60,24 @@ export interface Debug {
|
||||
* This is going to be superseded by key rotation once implemented.
|
||||
*/
|
||||
maximum_reply_surb_age_ms: bigint;
|
||||
/**
|
||||
* Defines maximum amount of time the client is going to wait for reply surbs before
|
||||
* deciding it's never going to get them and would drop all pending messages
|
||||
*/
|
||||
maximum_reply_surb_drop_waiting_period_ms: bigint;
|
||||
/**
|
||||
* Defines the maximum number of reply surbs the client would request.
|
||||
*/
|
||||
maximum_reply_surb_request_size: number;
|
||||
/**
|
||||
* Defines the maximum number of reply surbs the client wants to keep in its storage at any times.
|
||||
*/
|
||||
maximum_reply_surb_storage_threshold: number;
|
||||
/**
|
||||
* Defines maximum amount of time the client is going to wait for reply surbs before explicitly asking
|
||||
* for more even though in theory they wouldn't need to.
|
||||
*/
|
||||
maximum_reply_surb_waiting_period_ms: bigint;
|
||||
maximum_reply_surb_rerequest_waiting_period_ms: bigint;
|
||||
/**
|
||||
* Defines the maximum number of reply surbs the client wants to keep in its storage at any times.
|
||||
*/
|
||||
maximum_reply_surb_storage_threshold: number;
|
||||
/**
|
||||
* The parameter of Poisson distribution determining how long, on average,
|
||||
* it is going to take another 'real traffic stream' message to be sent.
|
||||
@@ -167,6 +172,7 @@ export interface IWebWorker {
|
||||
setTextMimeTypes: (mimeTypes: string[]) => void;
|
||||
getTextMimeTypes: () => string[];
|
||||
send: (args: { payload: Payload; recipient: string; replySurbs?: number }) => void;
|
||||
rawSend: (args: { payload: Uint8Array; recipient: string; replySurbs?: number }) => void;
|
||||
}
|
||||
|
||||
export interface IWebWorkerAsync {
|
||||
@@ -176,6 +182,7 @@ export interface IWebWorkerAsync {
|
||||
setTextMimeTypes: (mimeTypes: string[]) => void;
|
||||
getTextMimeTypes: () => Promise<string[]>;
|
||||
send: (args: { payload: Payload; recipient: string; replySurbs?: number }) => Promise<void>;
|
||||
rawSend: (args: { payload: Uint8Array; recipient: string; replySurbs?: number }) => Promise<void>;
|
||||
}
|
||||
|
||||
export enum EventKinds {
|
||||
@@ -183,6 +190,7 @@ export enum EventKinds {
|
||||
Connected = 'Connected',
|
||||
StringMessageReceived = 'StringMessageReceived',
|
||||
BinaryMessageReceived = 'BinaryMessageReceived',
|
||||
RawMessageReceived = 'RawMessageReceived',
|
||||
}
|
||||
|
||||
export interface LoadedEvent {
|
||||
@@ -218,9 +226,17 @@ export interface BinaryMessageReceivedEvent {
|
||||
};
|
||||
}
|
||||
|
||||
export interface RawMessageReceivedEvent {
|
||||
kind: EventKinds.RawMessageReceived;
|
||||
args: {
|
||||
payload: Uint8Array;
|
||||
};
|
||||
}
|
||||
|
||||
export interface IWebWorkerEvents {
|
||||
subscribeToLoaded: EventHandlerSubscribeFn<LoadedEvent>;
|
||||
subscribeToConnected: EventHandlerSubscribeFn<ConnectedEvent>;
|
||||
subscribeToTextMessageReceivedEvent: EventHandlerSubscribeFn<StringMessageReceivedEvent>;
|
||||
subscribeToBinaryMessageReceivedEvent: EventHandlerSubscribeFn<BinaryMessageReceivedEvent>;
|
||||
subscribeToRawMessageReceivedEvent: EventHandlerSubscribeFn<RawMessageReceivedEvent>;
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import type {
|
||||
LoadedEvent,
|
||||
NymClientConfig,
|
||||
OnRawPayloadFn,
|
||||
RawMessageReceivedEvent,
|
||||
StringMessageReceivedEvent,
|
||||
} from './types';
|
||||
import { EventKinds, MimeTypes } from './types';
|
||||
@@ -136,8 +137,8 @@ class ClientWrapper {
|
||||
recipient,
|
||||
replySurbs = 0,
|
||||
}: {
|
||||
recipient: string;
|
||||
payload: Uint8Array;
|
||||
recipient: string;
|
||||
replySurbs?: number;
|
||||
}) => {
|
||||
if (!this.client) {
|
||||
@@ -170,7 +171,14 @@ init(wasmBytes())
|
||||
wrapper.init(
|
||||
new Config(config.clientId, config.nymApiUrl, gatewayEndpoint, config.debug || default_debug()),
|
||||
async (message) => {
|
||||
// fire an event with the raw message
|
||||
postMessageWithType<RawMessageReceivedEvent>({
|
||||
kind: EventKinds.RawMessageReceived,
|
||||
args: { payload: message },
|
||||
});
|
||||
|
||||
try {
|
||||
// try to decode the payload to extract the mime-type, headers and payload body
|
||||
const decodedPayload = decode_payload(message);
|
||||
const { payload, headers } = decodedPayload;
|
||||
const mimeType = decodedPayload.mimeType as MimeTypes;
|
||||
@@ -178,6 +186,8 @@ init(wasmBytes())
|
||||
if (wrapper.getTextMimeTypes().includes(mimeType)) {
|
||||
const stringMessage = parse_utf8_string(payload);
|
||||
|
||||
// the payload is a string type (in the options at creation time, string mime-types are set, or fall back
|
||||
// to defaults, such as `text/plain`, `application/json`, etc)
|
||||
postMessageWithType<StringMessageReceivedEvent>({
|
||||
kind: EventKinds.StringMessageReceived,
|
||||
args: { mimeType, payload: stringMessage, payloadRaw: payload, headers },
|
||||
@@ -185,6 +195,7 @@ init(wasmBytes())
|
||||
return;
|
||||
}
|
||||
|
||||
// the payload is a binary type
|
||||
postMessageWithType<BinaryMessageReceivedEvent>({
|
||||
kind: EventKinds.BinaryMessageReceived,
|
||||
args: { mimeType, payload, headers },
|
||||
@@ -247,6 +258,12 @@ init(wasmBytes())
|
||||
.send({ payload, recipient, replySurbs })
|
||||
.catch((e) => console.error('[Nym WASM client] Failed to send message', e));
|
||||
},
|
||||
rawSend(args) {
|
||||
const { recipient, payload, replySurbs } = args;
|
||||
wrapper
|
||||
.send({ payload, replySurbs, recipient })
|
||||
.catch((e) => console.error('[Nym WASM client] Failed to send message', e));
|
||||
},
|
||||
};
|
||||
|
||||
// start comlink listening for messages and handle them above
|
||||
|
||||
Reference in New Issue
Block a user