diff --git a/sdk/typescript/examples/plain-html/src/index.ts b/sdk/typescript/examples/plain-html/src/index.ts index 3871d1c0a7..a0e429beb1 100644 --- a/sdk/typescript/examples/plain-html/src/index.ts +++ b/sdk/typescript/examples/plain-html/src/index.ts @@ -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', diff --git a/sdk/typescript/packages/sdk/package.json b/sdk/typescript/packages/sdk/package.json index 2fa4497f03..90622cc3e0 100644 --- a/sdk/typescript/packages/sdk/package.json +++ b/sdk/typescript/packages/sdk/package.json @@ -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", diff --git a/sdk/typescript/packages/sdk/src/mixnet/wasm/index.ts b/sdk/typescript/packages/sdk/src/mixnet/wasm/index.ts index 042c273791..b60550aee0 100644 --- a/sdk/typescript/packages/sdk/src/mixnet/wasm/index.ts +++ b/sdk/typescript/packages/sdk/src/mixnet/wasm/index.ts @@ -11,6 +11,7 @@ import { LoadedEvent, MimeTypes, StringMessageReceivedEvent, + RawMessageReceivedEvent, } from './types'; /** @@ -89,6 +90,12 @@ export const createNymMixnetClient = async (options?: { getSubscriptions(EventKinds.BinaryMessageReceived).unshift(handler); }; }, + subscribeToRawMessageReceivedEvent: (handler) => { + getSubscriptions(EventKinds.RawMessageReceived).push(handler); + return () => { + getSubscriptions(EventKinds.RawMessageReceived).unshift(handler); + }; + }, }; // let comlink handle interop with the web worker diff --git a/sdk/typescript/packages/sdk/src/mixnet/wasm/types.ts b/sdk/typescript/packages/sdk/src/mixnet/wasm/types.ts index ce4a4cd20d..2360414075 100644 --- a/sdk/typescript/packages/sdk/src/mixnet/wasm/types.ts +++ b/sdk/typescript/packages/sdk/src/mixnet/wasm/types.ts @@ -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; send: (args: { payload: Payload; recipient: string; replySurbs?: number }) => Promise; + rawSend: (args: { payload: Uint8Array; recipient: string; replySurbs?: number }) => Promise; } 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; subscribeToConnected: EventHandlerSubscribeFn; subscribeToTextMessageReceivedEvent: EventHandlerSubscribeFn; subscribeToBinaryMessageReceivedEvent: EventHandlerSubscribeFn; + subscribeToRawMessageReceivedEvent: EventHandlerSubscribeFn; } diff --git a/sdk/typescript/packages/sdk/src/mixnet/wasm/worker.ts b/sdk/typescript/packages/sdk/src/mixnet/wasm/worker.ts index dbc9ae36d7..4b663b9500 100644 --- a/sdk/typescript/packages/sdk/src/mixnet/wasm/worker.ts +++ b/sdk/typescript/packages/sdk/src/mixnet/wasm/worker.ts @@ -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({ + 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({ kind: EventKinds.StringMessageReceived, args: { mimeType, payload: stringMessage, payloadRaw: payload, headers }, @@ -185,6 +195,7 @@ init(wasmBytes()) return; } + // the payload is a binary type postMessageWithType({ 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