@affectively/dash 5.3.1 → 5.4.1

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.
Files changed (77) hide show
  1. package/README.md +193 -0
  2. package/dist/src/api/firebase/auth/index.d.ts +137 -0
  3. package/dist/src/api/firebase/auth/index.js +352 -0
  4. package/dist/src/api/firebase/auth/providers.d.ts +254 -0
  5. package/dist/src/api/firebase/auth/providers.js +518 -0
  6. package/dist/src/api/firebase/database/index.d.ts +108 -0
  7. package/dist/src/api/firebase/database/index.js +368 -0
  8. package/dist/src/api/firebase/errors.d.ts +15 -0
  9. package/dist/src/api/firebase/errors.js +215 -0
  10. package/dist/src/api/firebase/firestore/data-types.d.ts +116 -0
  11. package/dist/src/api/firebase/firestore/data-types.js +280 -0
  12. package/dist/src/api/firebase/firestore/index.d.ts +7 -0
  13. package/dist/src/api/firebase/firestore/index.js +13 -0
  14. package/dist/src/api/firebase/firestore/listeners.d.ts +20 -0
  15. package/dist/src/api/firebase/firestore/listeners.js +50 -0
  16. package/dist/src/api/firebase/firestore/operations.d.ts +123 -0
  17. package/dist/src/api/firebase/firestore/operations.js +490 -0
  18. package/dist/src/api/firebase/firestore/query.d.ts +118 -0
  19. package/dist/src/api/firebase/firestore/query.js +418 -0
  20. package/dist/src/api/firebase/index.d.ts +11 -0
  21. package/dist/src/api/firebase/index.js +17 -0
  22. package/dist/src/api/firebase/storage/index.d.ts +100 -0
  23. package/dist/src/api/firebase/storage/index.js +286 -0
  24. package/dist/src/api/firebase/types.d.ts +341 -0
  25. package/dist/src/api/firebase/types.js +4 -0
  26. package/dist/src/auth/manager.d.ts +182 -0
  27. package/dist/src/auth/manager.js +598 -0
  28. package/dist/src/engine/ai.d.ts +10 -0
  29. package/dist/src/engine/ai.js +76 -0
  30. package/dist/src/engine/sqlite.d.ts +353 -0
  31. package/dist/src/engine/sqlite.js +1328 -0
  32. package/dist/src/engine/vec_extension.d.ts +5 -0
  33. package/dist/src/engine/vec_extension.js +10 -0
  34. package/dist/src/index.d.ts +21 -0
  35. package/dist/src/index.js +26 -0
  36. package/dist/src/mcp/server.d.ts +8 -0
  37. package/dist/src/mcp/server.js +87 -0
  38. package/dist/src/reactivity/signal.d.ts +3 -0
  39. package/dist/src/reactivity/signal.js +31 -0
  40. package/dist/src/schema/lens.d.ts +29 -0
  41. package/dist/src/schema/lens.js +122 -0
  42. package/dist/src/sync/AeonDurableSync.d.ts +27 -0
  43. package/dist/src/sync/AeonDurableSync.js +133 -0
  44. package/dist/src/sync/AutomergeProvider.d.ts +45 -0
  45. package/dist/src/sync/AutomergeProvider.js +153 -0
  46. package/dist/src/sync/aeon/config.d.ts +21 -0
  47. package/dist/src/sync/aeon/config.js +14 -0
  48. package/dist/src/sync/aeon/delta-adapter.d.ts +62 -0
  49. package/dist/src/sync/aeon/delta-adapter.js +98 -0
  50. package/dist/src/sync/aeon/index.d.ts +18 -0
  51. package/dist/src/sync/aeon/index.js +19 -0
  52. package/dist/src/sync/aeon/offline-adapter.d.ts +110 -0
  53. package/dist/src/sync/aeon/offline-adapter.js +227 -0
  54. package/dist/src/sync/aeon/presence-adapter.d.ts +114 -0
  55. package/dist/src/sync/aeon/presence-adapter.js +157 -0
  56. package/dist/src/sync/aeon/schema-adapter.d.ts +95 -0
  57. package/dist/src/sync/aeon/schema-adapter.js +163 -0
  58. package/dist/src/sync/backup.d.ts +12 -0
  59. package/dist/src/sync/backup.js +44 -0
  60. package/dist/src/sync/connection.d.ts +20 -0
  61. package/dist/src/sync/connection.js +50 -0
  62. package/dist/src/sync/d1-provider.d.ts +103 -0
  63. package/dist/src/sync/d1-provider.js +418 -0
  64. package/dist/src/sync/hybrid-provider.d.ts +307 -0
  65. package/dist/src/sync/hybrid-provider.js +1353 -0
  66. package/dist/src/sync/provider.d.ts +11 -0
  67. package/dist/src/sync/provider.js +67 -0
  68. package/dist/src/sync/types.d.ts +32 -0
  69. package/dist/src/sync/types.js +4 -0
  70. package/dist/src/sync/verify.d.ts +1 -0
  71. package/dist/src/sync/verify.js +23 -0
  72. package/dist/tsconfig.tsbuildinfo +1 -0
  73. package/package.json +77 -43
  74. package/dist/index.d.ts +0 -62
  75. package/dist/index.js +0 -31
  76. package/dist/sync/index.d.ts +0 -6
  77. package/dist/sync/index.js +0 -4
@@ -0,0 +1,163 @@
1
+ /**
2
+ * Schema Adapter for Dash
3
+ *
4
+ * Bridges Aeon's SchemaVersionManager with Dash's LensEngine.
5
+ * Provides migration tracking, version compatibility checks, and audit trails.
6
+ */
7
+ import { SchemaVersionManager, MigrationEngine, MigrationTracker, } from '@affectively/aeon';
8
+ /**
9
+ * Adapter that bridges Aeon versioning with Dash's Lens system
10
+ */
11
+ export class DashSchemaAdapter {
12
+ versionManager;
13
+ migrationEngine;
14
+ migrationTracker;
15
+ lensEngine;
16
+ constructor(lensEngine) {
17
+ this.versionManager = new SchemaVersionManager();
18
+ this.migrationEngine = new MigrationEngine();
19
+ this.migrationTracker = new MigrationTracker();
20
+ this.lensEngine = lensEngine;
21
+ }
22
+ /**
23
+ * Register a schema version with associated Lens operations
24
+ */
25
+ registerVersion(version, description, lensOps, breaking = false) {
26
+ const [major, minor, patch] = version.split('.').map(Number);
27
+ // Register with Aeon
28
+ this.versionManager.registerVersion({
29
+ major,
30
+ minor,
31
+ patch,
32
+ timestamp: new Date().toISOString(),
33
+ description,
34
+ breaking,
35
+ });
36
+ // Register Lens if provided
37
+ if (lensOps && major > 1) {
38
+ this.lensEngine.registerLens(major - 1, major, lensOps);
39
+ }
40
+ }
41
+ /**
42
+ * Register a migration for a version upgrade
43
+ */
44
+ registerMigration(id, fromVersion, toVersion, migrate, rollback) {
45
+ const migration = {
46
+ id,
47
+ name: `${fromVersion} -> ${toVersion}`,
48
+ version: toVersion,
49
+ description: `Migrate from ${fromVersion} to ${toVersion}`,
50
+ timestamp: new Date().toISOString(),
51
+ up: migrate,
52
+ down: rollback,
53
+ };
54
+ this.migrationEngine.registerMigration(migration);
55
+ }
56
+ /**
57
+ * Execute a migration on data
58
+ */
59
+ async executeMigration(migrationId, data) {
60
+ const result = await this.migrationEngine.executeMigration(migrationId, data);
61
+ // Track the migration
62
+ const record = {
63
+ id: `record-${Date.now()}`,
64
+ migrationId,
65
+ timestamp: new Date().toISOString(),
66
+ version: result.migrationId,
67
+ direction: 'up',
68
+ status: result.success ? 'applied' : 'failed',
69
+ duration: result.duration,
70
+ itemsAffected: result.itemsAffected,
71
+ errorMessage: result.errors.length > 0 ? result.errors.join(', ') : undefined,
72
+ appliedBy: 'dash-schema-adapter',
73
+ };
74
+ this.migrationTracker.recordMigration(record);
75
+ return result;
76
+ }
77
+ /**
78
+ * Rollback a migration
79
+ */
80
+ async rollbackMigration(migrationId, data) {
81
+ const result = await this.migrationEngine.rollbackMigration(migrationId, data);
82
+ // Track the rollback
83
+ const record = {
84
+ id: `record-${Date.now()}`,
85
+ migrationId,
86
+ timestamp: new Date().toISOString(),
87
+ version: result.migrationId,
88
+ direction: 'down',
89
+ status: result.success ? 'rolled-back' : 'failed',
90
+ duration: result.duration,
91
+ itemsAffected: result.itemsAffected,
92
+ errorMessage: result.errors.length > 0 ? result.errors.join(', ') : undefined,
93
+ appliedBy: 'dash-schema-adapter',
94
+ };
95
+ this.migrationTracker.recordMigration(record);
96
+ return result;
97
+ }
98
+ /**
99
+ * Migrate data using Lens transformations
100
+ */
101
+ migrateWithLens(data, fromVersion, toVersion) {
102
+ return this.lensEngine.migrate(data, fromVersion, toVersion);
103
+ }
104
+ /**
105
+ * Get current version
106
+ */
107
+ getCurrentVersion() {
108
+ return this.versionManager.getCurrentVersion();
109
+ }
110
+ /**
111
+ * Get a specific version by string
112
+ */
113
+ getVersion(version) {
114
+ return this.versionManager.getVersion(version);
115
+ }
116
+ /**
117
+ * Get migration by ID
118
+ */
119
+ getMigration(id) {
120
+ return this.migrationEngine.getMigration(id);
121
+ }
122
+ /**
123
+ * Check if migration is needed
124
+ */
125
+ needsMigration(fromVersion, toVersion) {
126
+ return this.versionManager.canMigrate(fromVersion, toVersion);
127
+ }
128
+ /**
129
+ * Get migration path between versions
130
+ */
131
+ getMigrationPath(fromVersion, toVersion) {
132
+ const from = this.versionManager.parseVersion(fromVersion);
133
+ const to = this.versionManager.parseVersion(toVersion);
134
+ return this.versionManager.getMigrationPath(from, to);
135
+ }
136
+ /**
137
+ * Get pending migrations
138
+ */
139
+ getPendingMigrations() {
140
+ return this.migrationEngine.getPendingMigrations();
141
+ }
142
+ /**
143
+ * Get migration statistics
144
+ */
145
+ getStatistics() {
146
+ return {
147
+ engine: this.migrationEngine.getStatistics(),
148
+ tracker: this.migrationTracker.getStatistics(),
149
+ };
150
+ }
151
+ /**
152
+ * Format version for display
153
+ */
154
+ formatVersion(version) {
155
+ return `${version.major}.${version.minor}.${version.patch}`;
156
+ }
157
+ }
158
+ /**
159
+ * Create a schema adapter from an existing LensEngine
160
+ */
161
+ export function createSchemaAdapter(lensEngine) {
162
+ return new DashSchemaAdapter(lensEngine);
163
+ }
@@ -0,0 +1,12 @@
1
+ import * as Y from 'yjs';
2
+ export interface CloudStorageAdapter {
3
+ upload(name: string, data: Uint8Array): Promise<void>;
4
+ download(name: string): Promise<Uint8Array | null>;
5
+ }
6
+ export declare function generateKey(): Promise<CryptoKey>;
7
+ export declare function exportKey(key: CryptoKey): Promise<string>;
8
+ export declare function importKey(jwkString: string): Promise<CryptoKey>;
9
+ export declare function encrypt(data: Uint8Array, key: CryptoKey): Promise<Uint8Array>;
10
+ export declare function decrypt(data: Uint8Array, key: CryptoKey): Promise<Uint8Array>;
11
+ export declare function backup(roomName: string, doc: Y.Doc, key: CryptoKey, adapter: CloudStorageAdapter): Promise<void>;
12
+ export declare function restore(roomName: string, doc: Y.Doc, key: CryptoKey, adapter: CloudStorageAdapter): Promise<void>;
@@ -0,0 +1,44 @@
1
+ import * as Y from 'yjs';
2
+ // AES-GCM Encryption
3
+ export async function generateKey() {
4
+ return await crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, true, ['encrypt', 'decrypt']);
5
+ }
6
+ export async function exportKey(key) {
7
+ const exported = await crypto.subtle.exportKey('jwk', key);
8
+ return JSON.stringify(exported);
9
+ }
10
+ export async function importKey(jwkString) {
11
+ const jwk = JSON.parse(jwkString);
12
+ return await crypto.subtle.importKey('jwk', jwk, { name: 'AES-GCM' }, true, ['encrypt', 'decrypt']);
13
+ }
14
+ export async function encrypt(data, key) {
15
+ const iv = crypto.getRandomValues(new Uint8Array(12));
16
+ // Ensure we have a proper ArrayBuffer (not SharedArrayBuffer) by creating a copy
17
+ const dataCopy = new Uint8Array(data);
18
+ const encrypted = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, dataCopy);
19
+ // Combine IV and Encrypted Data
20
+ const combined = new Uint8Array(iv.length + encrypted.byteLength);
21
+ combined.set(iv);
22
+ combined.set(new Uint8Array(encrypted), iv.length);
23
+ return combined;
24
+ }
25
+ export async function decrypt(data, key) {
26
+ const iv = data.slice(0, 12);
27
+ const ciphertext = data.slice(12);
28
+ const decrypted = await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, ciphertext);
29
+ return new Uint8Array(decrypted);
30
+ }
31
+ export async function backup(roomName, doc, key, adapter) {
32
+ const state = Y.encodeStateAsUpdate(doc);
33
+ const encrypted = await encrypt(state, key);
34
+ await adapter.upload(`backup_${roomName}`, encrypted);
35
+ }
36
+ export async function restore(roomName, doc, key, adapter) {
37
+ const encrypted = await adapter.download(`backup_${roomName}`);
38
+ if (!encrypted) {
39
+ console.warn(`[CloudBackup] No backup found for room ${roomName}`);
40
+ return;
41
+ }
42
+ const state = await decrypt(encrypted, key);
43
+ Y.applyUpdate(doc, state);
44
+ }
@@ -0,0 +1,20 @@
1
+ import * as Y from 'yjs';
2
+ export interface SyncConnectionConfig {
3
+ type?: 'webrtc' | 'webtransport';
4
+ roomName: string;
5
+ doc: Y.Doc;
6
+ signalingServers?: string[];
7
+ password?: string | null;
8
+ url?: string;
9
+ awareness?: any;
10
+ }
11
+ export declare class SyncConnection {
12
+ private provider;
13
+ roomName: string;
14
+ constructor(config: SyncConnectionConfig);
15
+ disconnect(): void;
16
+ get awareness(): any;
17
+ }
18
+ export declare class WebRTCConnection extends SyncConnection {
19
+ constructor(config: Omit<SyncConnectionConfig, 'type'>);
20
+ }
@@ -0,0 +1,50 @@
1
+ import { WebrtcProvider } from 'y-webrtc';
2
+ import { HybridProvider } from './hybrid-provider.js';
3
+ export class SyncConnection {
4
+ provider;
5
+ roomName;
6
+ constructor(config) {
7
+ this.roomName = config.roomName;
8
+ const type = config.type || 'webrtc'; // Default to WebRTC for backward compatibility
9
+ if (type === 'webtransport') {
10
+ if (!config.url) {
11
+ throw new Error('WebTransport requires a "url" in config');
12
+ }
13
+ this.provider = new HybridProvider(config.url, config.roomName, config.doc, { awareness: config.awareness });
14
+ }
15
+ else {
16
+ // Default signaling servers if none provided
17
+ // Note: In production, users should host their own signaling servers
18
+ const signaling = config.signalingServers || [
19
+ 'wss://signaling.yjs.dev',
20
+ 'wss://y-webrtc-signaling-eu.herokuapp.com',
21
+ 'wss://y-webrtc-signaling-us.herokuapp.com'
22
+ ];
23
+ this.provider = new WebrtcProvider(config.roomName, config.doc, {
24
+ signaling,
25
+ password: config.password || undefined,
26
+ awareness: config.awareness
27
+ });
28
+ }
29
+ this.provider.on('synced', (event) => {
30
+ console.log(`[${type === 'webtransport' ? 'WebTransport' : 'WebRTC'}] Room: ${this.roomName}, Synced: ${event.synced}`);
31
+ });
32
+ // Relay status from provider if available
33
+ this.provider.on('status', (event) => {
34
+ // loose typing for event forwarding
35
+ console.log(`[${type}] Status update:`, event);
36
+ });
37
+ }
38
+ disconnect() {
39
+ this.provider.destroy();
40
+ }
41
+ get awareness() {
42
+ return this.provider.awareness;
43
+ }
44
+ }
45
+ // Backward compatibility alias
46
+ export class WebRTCConnection extends SyncConnection {
47
+ constructor(config) {
48
+ super({ ...config, type: 'webrtc' });
49
+ }
50
+ }
@@ -0,0 +1,103 @@
1
+ /**
2
+ * D1 Sync Provider
3
+ *
4
+ * Provides HTTP-based sync between local Dash SQLite (WASM) and Cloudflare D1.
5
+ *
6
+ * Unlike the HybridProvider which syncs Yjs CRDT documents over WebSocket,
7
+ * this provider syncs regular SQL tables via HTTP REST API.
8
+ *
9
+ * Architecture:
10
+ * - Local changes are tracked in a sync_queue table
11
+ * - Sync runs periodically or on-demand
12
+ * - Conflicts are resolved using last-write-wins with _lastModified timestamps
13
+ *
14
+ * Security:
15
+ * - Table/column names are validated against allowed patterns
16
+ * - All user data uses parameterized queries
17
+ */
18
+ export interface D1SyncConfig {
19
+ /** Base URL for the sync endpoint (e.g., 'https://example.com/api') */
20
+ baseUrl: string;
21
+ /** Tables to sync (e.g., ['reflections', 'users']) */
22
+ tables: string[];
23
+ /** Auth token getter - called before each sync */
24
+ getAuthToken: () => Promise<string | null>;
25
+ /** Sync interval in ms (default: 30000 = 30s). Set to 0 to disable auto-sync. */
26
+ syncInterval?: number;
27
+ /** Callback when sync completes */
28
+ onSyncComplete?: (result: SyncResult) => void;
29
+ /** Callback when sync fails */
30
+ onSyncError?: (error: Error) => void;
31
+ }
32
+ export interface SyncResult {
33
+ pushed: number;
34
+ pulled: number;
35
+ errors: string[];
36
+ timestamp: number;
37
+ }
38
+ export interface SyncQueueEntry {
39
+ id: number;
40
+ table_name: string;
41
+ row_id: string;
42
+ operation: 'create' | 'update' | 'delete';
43
+ data: string | null;
44
+ created_at: number;
45
+ synced: number;
46
+ }
47
+ export declare class D1SyncProvider {
48
+ private config;
49
+ private syncTimer;
50
+ private isSyncing;
51
+ private lastSyncTime;
52
+ private initialized;
53
+ constructor(config: D1SyncConfig);
54
+ /**
55
+ * Initialize the sync provider - must be called before syncing
56
+ */
57
+ initialize(): Promise<void>;
58
+ /**
59
+ * Set up SQLite triggers to track changes for a table
60
+ * Uses validated identifiers to prevent SQL injection
61
+ */
62
+ private setupTableTriggers;
63
+ /**
64
+ * Start automatic sync at the configured interval
65
+ */
66
+ startAutoSync(): void;
67
+ /**
68
+ * Stop automatic sync
69
+ */
70
+ stopAutoSync(): void;
71
+ /**
72
+ * Perform a sync operation
73
+ */
74
+ sync(): Promise<SyncResult>;
75
+ /**
76
+ * Apply changes received from the server with Last-Write-Wins (LWW) conflict resolution
77
+ * Server changes only win if their _lastModified is newer than local
78
+ */
79
+ private applyServerChanges;
80
+ /**
81
+ * Queue a change manually (for cases where triggers can't be used)
82
+ */
83
+ queueChange(tableName: string, rowId: string, operation: 'create' | 'update' | 'delete', data?: any): void;
84
+ /**
85
+ * Get the current sync status
86
+ */
87
+ getStatus(): {
88
+ initialized: boolean;
89
+ syncing: boolean;
90
+ lastSyncTime: number;
91
+ pendingChanges: number;
92
+ };
93
+ /**
94
+ * Force a full sync by resetting last sync time
95
+ */
96
+ forceFullSync(): Promise<SyncResult>;
97
+ /**
98
+ * Destroy the provider and clean up
99
+ */
100
+ destroy(): void;
101
+ }
102
+ export declare function getD1SyncProvider(config?: D1SyncConfig): D1SyncProvider;
103
+ export declare function resetD1SyncProvider(): void;