@massalabs/gossip-sdk 0.0.2-dev.20260130132559 → 0.0.2-dev.20260210150149

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.
@@ -19,7 +19,7 @@
19
19
  */
20
20
  import { type Contact, type GossipDatabase } from './db';
21
21
  import type { UpdateContactNameResult, DeleteContactResult } from './utils/contacts';
22
- import type { UserPublicKeys } from '#wasm';
22
+ import type { UserPublicKeys } from './wasm/bindings';
23
23
  import type { SessionModule } from './wasm/session';
24
24
  export type { UpdateContactNameResult, DeleteContactResult };
25
25
  /**
@@ -46,7 +46,7 @@ import { type MessageResult, type SendMessageResult } from './services/message';
46
46
  import { AuthService } from './services/auth';
47
47
  import type { DeleteContactResult, UpdateContactNameResult } from './utils/contacts';
48
48
  import { type ValidationResult } from './utils/validation';
49
- import type { UserPublicKeys } from '#wasm';
49
+ import type { UserPublicKeys } from './wasm/bindings';
50
50
  import { type SdkEventType, type SdkEventHandlers } from './core/SdkEventEmitter';
51
51
  export type { SdkEventType, SdkEventHandlers };
52
52
  export interface GossipSdkInitOptions {
package/dist/index.d.ts CHANGED
@@ -4,9 +4,9 @@
4
4
  * Main entry point for the Gossip SDK.
5
5
  * Works in both browser and Node.js environments.
6
6
  *
7
- * WASM is loaded via the #wasm subpath import which resolves conditionally:
8
- * - Browser: web target (uses import.meta.url)
9
- * - Node: nodejs target (uses fs, no import.meta.url)
7
+ * WASM is loaded via the web target build with runtime detection:
8
+ * - Browser: init() uses import.meta.url + fetch
9
+ * - Node.js / Jiti: init(bytes) reads the .wasm file from disk
10
10
  *
11
11
  * @example
12
12
  * ```typescript
@@ -56,4 +56,4 @@ export { MESSAGE_TYPE_KEEP_ALIVE, serializeKeepAliveMessage, serializeRegularMes
56
56
  export type { DeserializedMessage } from './utils/messageSerialization';
57
57
  export { generateMnemonic, validateMnemonic, mnemonicToSeed, accountFromMnemonic, PRIVATE_KEY_VERSION, } from './crypto/bip39';
58
58
  export { encrypt, decrypt, deriveKey } from './crypto/encryption';
59
- export { UserPublicKeys, UserSecretKeys, SessionStatus, SessionConfig, SessionManagerWrapper, SendMessageOutput, ReceiveMessageOutput, AnnouncementResult, generate_user_keys, } from '#wasm';
59
+ export { UserPublicKeys, UserSecretKeys, SessionStatus, SessionConfig, SessionManagerWrapper, SendMessageOutput, ReceiveMessageOutput, AnnouncementResult, generate_user_keys, } from './wasm/bindings';
package/dist/index.js CHANGED
@@ -4,9 +4,9 @@
4
4
  * Main entry point for the Gossip SDK.
5
5
  * Works in both browser and Node.js environments.
6
6
  *
7
- * WASM is loaded via the #wasm subpath import which resolves conditionally:
8
- * - Browser: web target (uses import.meta.url)
9
- * - Node: nodejs target (uses fs, no import.meta.url)
7
+ * WASM is loaded via the web target build with runtime detection:
8
+ * - Browser: init() uses import.meta.url + fetch
9
+ * - Node.js / Jiti: init(bytes) reads the .wasm file from disk
10
10
  *
11
11
  * @example
12
12
  * ```typescript
@@ -58,4 +58,4 @@ export { MESSAGE_TYPE_KEEP_ALIVE, serializeKeepAliveMessage, serializeRegularMes
58
58
  export { generateMnemonic, validateMnemonic, mnemonicToSeed, accountFromMnemonic, PRIVATE_KEY_VERSION, } from './crypto/bip39';
59
59
  export { encrypt, decrypt, deriveKey } from './crypto/encryption';
60
60
  // WASM types re-exported for convenience
61
- export { UserPublicKeys, UserSecretKeys, SessionStatus, SessionConfig, SessionManagerWrapper, SendMessageOutput, ReceiveMessageOutput, AnnouncementResult, generate_user_keys, } from '#wasm';
61
+ export { UserPublicKeys, UserSecretKeys, SessionStatus, SessionConfig, SessionManagerWrapper, SendMessageOutput, ReceiveMessageOutput, AnnouncementResult, generate_user_keys, } from './wasm/bindings';
@@ -5,7 +5,7 @@
5
5
  */
6
6
  import { type Discussion, type GossipDatabase } from '../db';
7
7
  import { IMessageProtocol } from '../api/messageProtocol';
8
- import { UserPublicKeys } from '#wasm';
8
+ import { UserPublicKeys } from '../wasm/bindings';
9
9
  import { SessionModule } from '../wasm/session';
10
10
  import { GossipSdkEvents } from '../types/events';
11
11
  import { SdkConfig } from '../config/sdk';
@@ -36,6 +36,12 @@ export declare class AnnouncementService {
36
36
  }>;
37
37
  fetchAndProcessAnnouncements(): Promise<AnnouncementReceptionResult>;
38
38
  resendAnnouncements(failedDiscussions: Discussion[]): Promise<void>;
39
+ /**
40
+ * Persist lastBulletinCounter for the current user.
41
+ * Uses put() so the cursor is saved even when no profile row exists yet
42
+ * (e.g. headless bot that never created a profile via app UI).
43
+ */
44
+ private _upsertLastBulletinCounter;
39
45
  private _fetchAnnouncements;
40
46
  private _generateTemporaryContactName;
41
47
  private _processIncomingAnnouncement;
@@ -5,7 +5,7 @@
5
5
  */
6
6
  import { DiscussionStatus, DiscussionDirection, } from '../db';
7
7
  import { decodeUserId, encodeUserId } from '../utils/userId';
8
- import { SessionStatus } from '#wasm';
8
+ import { SessionStatus } from '../wasm/bindings';
9
9
  import { sessionStatusToString } from '../wasm/session';
10
10
  import { Logger } from '../utils/logs';
11
11
  import { defaultSdkConfig } from '../config/sdk';
@@ -153,9 +153,7 @@ export class AnnouncementService {
153
153
  }
154
154
  if (fetchedCounters.length > 0) {
155
155
  const highestCounter = fetchedCounters.reduce((a, b) => Number(a) > Number(b) ? a : b);
156
- await this.db.userProfile.update(this.session.userIdEncoded, {
157
- lastBulletinCounter: highestCounter,
158
- });
156
+ await this._upsertLastBulletinCounter(highestCounter);
159
157
  log.info('updated lastBulletinCounter', { highestCounter });
160
158
  }
161
159
  return {
@@ -194,9 +192,7 @@ export class AnnouncementService {
194
192
  // If API returned a batch with max <= cursor (same or older page), advance past it
195
193
  // so we don't re-fetch the same page forever (e.g. API returning "latest" regardless of after)
196
194
  const nextCounter = highestNum <= cursorNum ? String(cursorNum + 1) : highestCounter;
197
- await this.db.userProfile.update(this.session.userIdEncoded, {
198
- lastBulletinCounter: nextCounter,
199
- });
195
+ await this._upsertLastBulletinCounter(nextCounter);
200
196
  log.info('updated lastBulletinCounter', {
201
197
  lastBulletinCounter: nextCounter,
202
198
  });
@@ -311,6 +307,32 @@ export class AnnouncementService {
311
307
  broken: brokenDiscussions.length,
312
308
  });
313
309
  }
310
+ /**
311
+ * Persist lastBulletinCounter for the current user.
312
+ * Uses put() so the cursor is saved even when no profile row exists yet
313
+ * (e.g. headless bot that never created a profile via app UI).
314
+ */
315
+ async _upsertLastBulletinCounter(nextCounter) {
316
+ const userId = this.session.userIdEncoded;
317
+ const existing = await this.db.userProfile.get(userId);
318
+ if (existing) {
319
+ await this.db.userProfile.update(userId, {
320
+ lastBulletinCounter: nextCounter,
321
+ updatedAt: new Date(),
322
+ });
323
+ return;
324
+ }
325
+ // Minimal profile so cursor persists; headless bots may never create a full profile
326
+ await this.db.userProfile.put({
327
+ userId,
328
+ username: '',
329
+ status: 'offline',
330
+ lastSeen: new Date(),
331
+ createdAt: new Date(),
332
+ updatedAt: new Date(),
333
+ lastBulletinCounter: nextCounter,
334
+ });
335
+ }
314
336
  async _fetchAnnouncements(cursor, limit) {
315
337
  const fetchLimit = limit ?? this.config.announcements.fetchLimit;
316
338
  const log = logger.forMethod('_fetchAnnouncements');
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Handles storing and retrieving public keys by userId hash via the auth API.
5
5
  */
6
- import { UserPublicKeys } from '#wasm';
6
+ import { UserPublicKeys } from '../wasm/bindings';
7
7
  import { IMessageProtocol } from '../api/messageProtocol/types';
8
8
  import { type GossipDatabase } from '../db';
9
9
  export type PublicKeyResult = {
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Handles storing and retrieving public keys by userId hash via the auth API.
5
5
  */
6
- import { UserPublicKeys } from '#wasm';
6
+ import { UserPublicKeys } from '../wasm/bindings';
7
7
  import { decodeUserId } from '../utils/userId';
8
8
  import { encodeToBase64, decodeFromBase64 } from '../utils/base64';
9
9
  export class AuthService {
@@ -4,7 +4,7 @@
4
4
  * Class-based service for initializing, accepting, and managing discussions.
5
5
  */
6
6
  import { DiscussionStatus, MessageDirection, MessageStatus, DiscussionDirection, } from '../db';
7
- import { UserPublicKeys, SessionStatus } from '#wasm';
7
+ import { UserPublicKeys, SessionStatus } from '../wasm/bindings';
8
8
  import { EstablishSessionError } from './announcement';
9
9
  import { sessionStatusToString } from '../wasm/session';
10
10
  import { decodeUserId } from '../utils/userId';
@@ -6,7 +6,7 @@
6
6
  */
7
7
  import { DiscussionStatus, MessageDirection, MessageStatus, MessageType, } from '../db';
8
8
  import { decodeUserId, encodeUserId } from '../utils/userId';
9
- import { SessionStatus } from '#wasm';
9
+ import { SessionStatus } from '../wasm/bindings';
10
10
  import { serializeRegularMessage, serializeReplyMessage, serializeForwardMessage, serializeKeepAliveMessage, deserializeMessage, } from '../utils/messageSerialization';
11
11
  import { encodeToBase64 } from '../utils/base64';
12
12
  import { sessionStatusToString } from '../wasm/session';
@@ -6,7 +6,7 @@
6
6
  */
7
7
  import { MessageDirection, MessageStatus, MessageType, } from '../db';
8
8
  import { sessionStatusToString } from '../wasm/session';
9
- import { SessionStatus } from '#wasm';
9
+ import { SessionStatus } from '../wasm/bindings';
10
10
  import { decodeUserId, encodeUserId } from '../utils/userId';
11
11
  import { Logger } from '../utils/logs';
12
12
  const logger = new Logger('RefreshService');
@@ -0,0 +1,9 @@
1
+ /**
2
+ * WASM Bindings Barrel
3
+ *
4
+ * Re-exports all WASM-generated types and functions from the web target.
5
+ * This centralizes the WASM import so the rest of the codebase uses
6
+ * a standard relative import instead of conditional #wasm subpath imports.
7
+ */
8
+ export { default as init } from '../assets/generated/wasm/gossip_wasm';
9
+ export * from '../assets/generated/wasm/gossip_wasm';
@@ -0,0 +1,9 @@
1
+ /**
2
+ * WASM Bindings Barrel
3
+ *
4
+ * Re-exports all WASM-generated types and functions from the web target.
5
+ * This centralizes the WASM import so the rest of the codebase uses
6
+ * a standard relative import instead of conditional #wasm subpath imports.
7
+ */
8
+ export { default as init } from '../assets/generated/wasm/gossip_wasm';
9
+ export * from '../assets/generated/wasm/gossip_wasm';
@@ -5,7 +5,7 @@
5
5
  * and AEAD (Authenticated Encryption with Additional Data) operations,
6
6
  * ensuring proper initialization before calling any WASM functions.
7
7
  */
8
- import { EncryptionKey, Nonce } from '#wasm';
8
+ import { EncryptionKey, Nonce } from './bindings';
9
9
  export { EncryptionKey, Nonce };
10
10
  /**
11
11
  * Generate a new random encryption key (64 bytes)
@@ -6,7 +6,7 @@
6
6
  * ensuring proper initialization before calling any WASM functions.
7
7
  */
8
8
  import { ensureWasmInitialized } from './loader';
9
- import { EncryptionKey, Nonce, aead_encrypt as _aead_encrypt, aead_decrypt as _aead_decrypt, } from '#wasm';
9
+ import { EncryptionKey, Nonce, aead_encrypt as _aead_encrypt, aead_decrypt as _aead_decrypt, } from './bindings';
10
10
  // Re-export classes
11
11
  export { EncryptionKey, Nonce };
12
12
  /**
@@ -1,10 +1,10 @@
1
1
  /**
2
2
  * WASM Module Loader and Initialization Service
3
3
  *
4
- * This file handles WASM initialization. The actual wasm module is resolved
5
- * via the #wasm import which conditionally loads the correct target:
6
- * - Browser: web target (has init function, uses import.meta.url + fetch)
7
- * - Node: nodejs target (auto-initializes, no init function needed)
4
+ * This file handles WASM initialization. It uses a single web-target build
5
+ * and detects the runtime to load the WASM binary appropriately:
6
+ * - Browser: init() with no args (uses import.meta.url + fetch internally)
7
+ * - Node.js / Jiti: init(bytes) with WASM bytes read from the filesystem
8
8
  */
9
9
  /**
10
10
  * Initialize WASM modules if not already initialized
@@ -1,15 +1,12 @@
1
1
  /**
2
2
  * WASM Module Loader and Initialization Service
3
3
  *
4
- * This file handles WASM initialization. The actual wasm module is resolved
5
- * via the #wasm import which conditionally loads the correct target:
6
- * - Browser: web target (has init function, uses import.meta.url + fetch)
7
- * - Node: nodejs target (auto-initializes, no init function needed)
4
+ * This file handles WASM initialization. It uses a single web-target build
5
+ * and detects the runtime to load the WASM binary appropriately:
6
+ * - Browser: init() with no args (uses import.meta.url + fetch internally)
7
+ * - Node.js / Jiti: init(bytes) with WASM bytes read from the filesystem
8
8
  */
9
- import * as wasmModule from '#wasm';
10
- // The web target has a default export (init function), nodejs target doesn't
11
- const init = wasmModule
12
- .default;
9
+ import { init } from './bindings';
13
10
  /**
14
11
  * WASM Initialization State
15
12
  */
@@ -17,6 +14,14 @@ let isInitializing = false;
17
14
  let isInitialized = false;
18
15
  let initializationPromise = null;
19
16
  let initError = null;
17
+ /**
18
+ * Detect if running in a Node.js-like environment (Node, Bun, Jiti, etc.)
19
+ */
20
+ function isNodeRuntime() {
21
+ return (typeof process !== 'undefined' &&
22
+ process.versions != null &&
23
+ process.versions.node != null);
24
+ }
20
25
  /**
21
26
  * Initialize WASM modules if not already initialized
22
27
  * This function is idempotent - safe to call multiple times
@@ -35,13 +40,21 @@ export async function initializeWasm() {
35
40
  initError = null;
36
41
  initializationPromise = (async () => {
37
42
  try {
38
- // The #wasm import resolves to the correct target based on environment:
39
- // - Browser (web target): has init() function that needs to be called
40
- // - Node (nodejs target): auto-initializes on import, no init needed
41
- if (typeof init === 'function') {
43
+ if (isNodeRuntime()) {
44
+ // Node.js / Jiti: read WASM bytes from filesystem and pass to init()
45
+ // Dynamic imports ensure these Node.js modules are tree-shaken in browser builds
46
+ const fs = await import('node:fs');
47
+ const url = await import('node:url');
48
+ const path = await import('node:path');
49
+ const currentDir = path.dirname(url.fileURLToPath(import.meta.url));
50
+ const wasmPath = path.resolve(currentDir, '../assets/generated/wasm/gossip_wasm_bg.wasm');
51
+ const wasmBytes = fs.readFileSync(wasmPath);
52
+ await init(wasmBytes);
53
+ }
54
+ else {
55
+ // Browser: use default loading (import.meta.url + fetch internally)
42
56
  await init();
43
57
  }
44
- // For nodejs target, wasm is already initialized on import
45
58
  isInitialized = true;
46
59
  isInitializing = false;
47
60
  }
@@ -4,7 +4,7 @@
4
4
  * This file contains the real WASM implementation of the SessionModule
5
5
  * using SessionManagerWrapper and related WASM classes.
6
6
  */
7
- import { UserPublicKeys, UserSecretKeys, ReceiveMessageOutput, SendMessageOutput, SessionStatus, EncryptionKey, SessionConfig, AnnouncementResult, UserKeys } from '#wasm';
7
+ import { UserPublicKeys, UserSecretKeys, ReceiveMessageOutput, SendMessageOutput, SessionStatus, EncryptionKey, SessionConfig, AnnouncementResult, UserKeys } from './bindings';
8
8
  import { UserProfile } from '../db';
9
9
  export declare class SessionModule {
10
10
  private sessionManager;
@@ -4,7 +4,7 @@
4
4
  * This file contains the real WASM implementation of the SessionModule
5
5
  * using SessionManagerWrapper and related WASM classes.
6
6
  */
7
- import { SessionManagerWrapper, SessionStatus, SessionConfig, } from '#wasm';
7
+ import { SessionManagerWrapper, SessionStatus, SessionConfig, } from './bindings';
8
8
  import { encodeUserId } from '../utils/userId';
9
9
  export class SessionModule {
10
10
  constructor(userKeys, onPersist, config) {
@@ -4,7 +4,7 @@
4
4
  * This file provides proxy functions for user key generation,
5
5
  * ensuring proper initialization before calling any WASM functions.
6
6
  */
7
- import { UserKeys } from '#wasm';
7
+ import { UserKeys } from './bindings';
8
8
  export { UserKeys };
9
9
  /**
10
10
  * Generate user keys from a passphrase using password-based key derivation
@@ -5,7 +5,7 @@
5
5
  * ensuring proper initialization before calling any WASM functions.
6
6
  */
7
7
  import { ensureWasmInitialized } from './loader';
8
- import { generate_user_keys as _generate_user_keys, UserKeys } from '#wasm';
8
+ import { generate_user_keys as _generate_user_keys, UserKeys, } from './bindings';
9
9
  // Re-export classes
10
10
  export { UserKeys };
11
11
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@massalabs/gossip-sdk",
3
- "version": "0.0.2-dev.20260130132559",
3
+ "version": "0.0.2-dev.20260210150149",
4
4
  "description": "Gossip SDK for automation, chatbot, and integration use cases",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -9,12 +9,6 @@
9
9
  "dist",
10
10
  "README.md"
11
11
  ],
12
- "imports": {
13
- "#wasm": {
14
- "browser": "./dist/assets/generated/wasm/gossip_wasm.js",
15
- "default": "./dist/assets/generated/wasm-node/gossip_wasm.js"
16
- }
17
- },
18
12
  "exports": {
19
13
  ".": {
20
14
  "types": "./dist/index.d.ts",
@@ -28,11 +22,13 @@
28
22
  "scripts": {
29
23
  "prebuild": "rimraf dist",
30
24
  "build": "tsc && npm run copy:wasm",
31
- "copy:wasm": "mkdir -p dist/assets/generated/wasm dist/assets/generated/wasm-node && cp -R src/assets/generated/wasm/* dist/assets/generated/wasm/ && cp -R src/assets/generated/wasm-node/* dist/assets/generated/wasm-node/",
25
+ "copy:wasm": "mkdir -p dist/assets/generated/wasm && cp -R src/assets/generated/wasm/* dist/assets/generated/wasm/",
32
26
  "test": "vitest",
33
27
  "test:run": "vitest run",
34
28
  "test:coverage": "vitest run --coverage",
35
- "test:e2e": "vitest run test/e2e/"
29
+ "test:e2e": "vitest run test/e2e/",
30
+ "lint": "eslint .",
31
+ "lint:fix": "eslint . --fix"
36
32
  },
37
33
  "keywords": [
38
34
  "gossip",