@breeztech/breez-sdk-spark 0.13.10-dev → 0.13.12-dev1
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.
- package/breez-sdk-spark.tgz +0 -0
- package/bundler/breez_sdk_spark_wasm.d.ts +157 -0
- package/bundler/breez_sdk_spark_wasm.js +1 -1
- package/bundler/breez_sdk_spark_wasm_bg.js +419 -41
- package/bundler/breez_sdk_spark_wasm_bg.wasm +0 -0
- package/bundler/breez_sdk_spark_wasm_bg.wasm.d.ts +27 -7
- package/deno/breez_sdk_spark_wasm.d.ts +157 -0
- package/deno/breez_sdk_spark_wasm.js +419 -41
- package/deno/breez_sdk_spark_wasm_bg.wasm +0 -0
- package/deno/breez_sdk_spark_wasm_bg.wasm.d.ts +27 -7
- package/nodejs/breez_sdk_spark_wasm.d.ts +157 -0
- package/nodejs/breez_sdk_spark_wasm.js +428 -41
- package/nodejs/breez_sdk_spark_wasm_bg.wasm +0 -0
- package/nodejs/breez_sdk_spark_wasm_bg.wasm.d.ts +27 -7
- package/nodejs/index.js +56 -0
- package/nodejs/index.mjs +9 -0
- package/nodejs/mysql-session-manager/errors.cjs +13 -0
- package/nodejs/mysql-session-manager/index.cjs +144 -0
- package/nodejs/mysql-session-manager/migrations.cjs +102 -0
- package/nodejs/mysql-session-manager/package.json +9 -0
- package/nodejs/mysql-storage/errors.cjs +19 -0
- package/nodejs/mysql-storage/index.cjs +1367 -0
- package/nodejs/mysql-storage/migrations.cjs +387 -0
- package/nodejs/mysql-storage/package.json +9 -0
- package/nodejs/mysql-token-store/errors.cjs +9 -0
- package/nodejs/mysql-token-store/index.cjs +980 -0
- package/nodejs/mysql-token-store/migrations.cjs +255 -0
- package/nodejs/mysql-token-store/package.json +9 -0
- package/nodejs/mysql-tree-store/errors.cjs +9 -0
- package/nodejs/mysql-tree-store/index.cjs +941 -0
- package/nodejs/mysql-tree-store/migrations.cjs +221 -0
- package/nodejs/mysql-tree-store/package.json +9 -0
- package/nodejs/package.json +5 -0
- package/nodejs/postgres-session-manager/errors.cjs +13 -0
- package/nodejs/postgres-session-manager/index.cjs +165 -0
- package/nodejs/postgres-session-manager/migrations.cjs +126 -0
- package/nodejs/postgres-session-manager/package.json +9 -0
- package/nodejs/postgres-storage/index.cjs +147 -92
- package/nodejs/postgres-storage/migrations.cjs +85 -4
- package/nodejs/postgres-token-store/index.cjs +178 -102
- package/nodejs/postgres-token-store/migrations.cjs +92 -3
- package/nodejs/postgres-tree-store/index.cjs +168 -83
- package/nodejs/postgres-tree-store/migrations.cjs +80 -3
- package/package.json +1 -1
- package/ssr/index.js +53 -0
- package/web/breez_sdk_spark_wasm.d.ts +184 -7
- package/web/breez_sdk_spark_wasm.js +419 -41
- package/web/breez_sdk_spark_wasm_bg.wasm +0 -0
- package/web/breez_sdk_spark_wasm_bg.wasm.d.ts +27 -7
|
Binary file
|
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
/* tslint:disable */
|
|
2
2
|
/* eslint-disable */
|
|
3
3
|
export const memory: WebAssembly.Memory;
|
|
4
|
+
export const __wbg_bitcoinchainservicehandle_free: (a: number, b: number) => void;
|
|
4
5
|
export const __wbg_breezsdk_free: (a: number, b: number) => void;
|
|
5
6
|
export const __wbg_defaultsigner_free: (a: number, b: number) => void;
|
|
7
|
+
export const __wbg_mysqlconnectionpool_free: (a: number, b: number) => void;
|
|
6
8
|
export const __wbg_passkey_free: (a: number, b: number) => void;
|
|
7
9
|
export const __wbg_sdkbuilder_free: (a: number, b: number) => void;
|
|
10
|
+
export const __wbg_sspconnectionmanager_free: (a: number, b: number) => void;
|
|
8
11
|
export const __wbg_tokenissuer_free: (a: number, b: number) => void;
|
|
12
|
+
export const bitcoinchainservicehandle_broadcastTransaction: (a: number, b: number, c: number) => any;
|
|
13
|
+
export const bitcoinchainservicehandle_getAddressUtxos: (a: number, b: number, c: number) => any;
|
|
14
|
+
export const bitcoinchainservicehandle_getTransactionHex: (a: number, b: number, c: number) => any;
|
|
15
|
+
export const bitcoinchainservicehandle_getTransactionStatus: (a: number, b: number, c: number) => any;
|
|
16
|
+
export const bitcoinchainservicehandle_recommendedFees: (a: number) => any;
|
|
9
17
|
export const breezsdk_addContact: (a: number, b: any) => any;
|
|
10
18
|
export const breezsdk_addEventListener: (a: number, b: any) => any;
|
|
11
19
|
export const breezsdk_buyBitcoin: (a: number, b: any) => any;
|
|
@@ -52,9 +60,11 @@ export const breezsdk_updateContact: (a: number, b: any) => any;
|
|
|
52
60
|
export const breezsdk_updateUserSettings: (a: number, b: any) => any;
|
|
53
61
|
export const connect: (a: any) => any;
|
|
54
62
|
export const connectWithSigner: (a: any, b: any, c: number, d: number) => any;
|
|
63
|
+
export const createMysqlConnectionPool: (a: any) => [number, number, number];
|
|
64
|
+
export const createPostgresConnectionPool: (a: any) => [number, number, number];
|
|
55
65
|
export const defaultConfig: (a: any) => any;
|
|
56
66
|
export const defaultExternalSigner: (a: number, b: number, c: number, d: number, e: any, f: number) => [number, number, number];
|
|
57
|
-
export const
|
|
67
|
+
export const defaultMysqlStorageConfig: (a: number, b: number) => any;
|
|
58
68
|
export const defaultsigner_aggregateFrost: (a: number, b: any) => any;
|
|
59
69
|
export const defaultsigner_decryptEcies: (a: number, b: number, c: number, d: number, e: number) => any;
|
|
60
70
|
export const defaultsigner_derivePublicKey: (a: number, b: number, c: number) => any;
|
|
@@ -77,6 +87,8 @@ export const defaultsigner_staticDepositSigningKey: (a: number, b: number) => an
|
|
|
77
87
|
export const defaultsigner_subtractSecrets: (a: number, b: any, c: any) => any;
|
|
78
88
|
export const getSparkStatus: () => any;
|
|
79
89
|
export const initLogging: (a: any, b: number, c: number) => any;
|
|
90
|
+
export const newRestChainService: (a: number, b: number, c: any, d: any, e: number) => number;
|
|
91
|
+
export const newSspConnectionManager: (a: number, b: number) => number;
|
|
80
92
|
export const passkey_getWallet: (a: number, b: number, c: number) => any;
|
|
81
93
|
export const passkey_isAvailable: (a: number) => any;
|
|
82
94
|
export const passkey_listLabels: (a: number) => any;
|
|
@@ -90,9 +102,14 @@ export const sdkbuilder_withDefaultStorage: (a: number, b: number, c: number) =>
|
|
|
90
102
|
export const sdkbuilder_withFiatService: (a: number, b: any) => number;
|
|
91
103
|
export const sdkbuilder_withKeySet: (a: number, b: any) => number;
|
|
92
104
|
export const sdkbuilder_withLnurlClient: (a: number, b: any) => number;
|
|
105
|
+
export const sdkbuilder_withMysqlBackend: (a: number, b: any) => [number, number, number];
|
|
106
|
+
export const sdkbuilder_withMysqlConnectionPool: (a: number, b: number) => number;
|
|
93
107
|
export const sdkbuilder_withPaymentObserver: (a: number, b: any) => number;
|
|
94
|
-
export const sdkbuilder_withPostgresBackend: (a: number, b: any) => number;
|
|
108
|
+
export const sdkbuilder_withPostgresBackend: (a: number, b: any) => [number, number, number];
|
|
109
|
+
export const sdkbuilder_withPostgresConnectionPool: (a: number, b: number) => number;
|
|
95
110
|
export const sdkbuilder_withRestChainService: (a: number, b: number, c: number, d: any, e: number) => number;
|
|
111
|
+
export const sdkbuilder_withSessionManager: (a: number, b: any) => number;
|
|
112
|
+
export const sdkbuilder_withSspConnectionManager: (a: number, b: number) => number;
|
|
96
113
|
export const sdkbuilder_withStorage: (a: number, b: any) => number;
|
|
97
114
|
export const tokenissuer_burnIssuerToken: (a: number, b: any) => any;
|
|
98
115
|
export const tokenissuer_createIssuerToken: (a: number, b: any) => any;
|
|
@@ -119,15 +136,18 @@ export const intounderlyingsink_close: (a: number) => any;
|
|
|
119
136
|
export const intounderlyingsink_write: (a: number, b: any) => any;
|
|
120
137
|
export const intounderlyingsource_cancel: (a: number) => void;
|
|
121
138
|
export const intounderlyingsource_pull: (a: number, b: any) => any;
|
|
122
|
-
export const
|
|
123
|
-
export const
|
|
124
|
-
export const
|
|
125
|
-
export const
|
|
139
|
+
export const __wbg_postgresconnectionpool_free: (a: number, b: number) => void;
|
|
140
|
+
export const defaultPostgresStorageConfig: (a: number, b: number) => any;
|
|
141
|
+
export const wasm_bindgen__convert__closures_____invoke__h459c42c6ffe5f52c: (a: number, b: number, c: any) => [number, number];
|
|
142
|
+
export const wasm_bindgen__convert__closures_____invoke__h459c42c6ffe5f52c_4: (a: number, b: number, c: any) => [number, number];
|
|
143
|
+
export const wasm_bindgen__convert__closures_____invoke__h459c42c6ffe5f52c_5: (a: number, b: number, c: any) => [number, number];
|
|
144
|
+
export const wasm_bindgen__convert__closures_____invoke__h459c42c6ffe5f52c_6: (a: number, b: number, c: any) => [number, number];
|
|
145
|
+
export const wasm_bindgen__convert__closures_____invoke__h459c42c6ffe5f52c_7: (a: number, b: number, c: any) => [number, number];
|
|
126
146
|
export const wasm_bindgen__convert__closures_____invoke__h41057d61edf43a32: (a: number, b: number, c: any, d: any) => void;
|
|
127
147
|
export const wasm_bindgen__convert__closures_____invoke__h4819aba3eed2db57: (a: number, b: number, c: any) => void;
|
|
128
148
|
export const wasm_bindgen__convert__closures_____invoke__h4819aba3eed2db57_2: (a: number, b: number, c: any) => void;
|
|
129
149
|
export const wasm_bindgen__convert__closures_____invoke__h4819aba3eed2db57_3: (a: number, b: number, c: any) => void;
|
|
130
|
-
export const
|
|
150
|
+
export const wasm_bindgen__convert__closures_____invoke__h124479769cd429fd: (a: number, b: number) => void;
|
|
131
151
|
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
132
152
|
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
133
153
|
export const __wbindgen_free: (a: number, b: number, c: number) => void;
|
package/nodejs/index.js
CHANGED
|
@@ -48,5 +48,61 @@ try {
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
// Automatically import and set up the PostgreSQL session manager for Node.js
|
|
52
|
+
try {
|
|
53
|
+
const { createPostgresSessionManager, createPostgresSessionManagerWithPool } = require('./postgres-session-manager/index.cjs');
|
|
54
|
+
global.createPostgresSessionManager = createPostgresSessionManager;
|
|
55
|
+
global.createPostgresSessionManagerWithPool = createPostgresSessionManagerWithPool;
|
|
56
|
+
} catch (error) {
|
|
57
|
+
if (error.code !== 'MODULE_NOT_FOUND') {
|
|
58
|
+
console.warn('Breez SDK: Failed to load PostgreSQL session manager:', error.message);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Automatically import and set up the MySQL storage for Node.js
|
|
63
|
+
try {
|
|
64
|
+
const { createMysqlStorage, createMysqlPool, createMysqlStorageWithPool } = require('./mysql-storage/index.cjs');
|
|
65
|
+
global.createMysqlStorage = createMysqlStorage;
|
|
66
|
+
global.createMysqlPool = createMysqlPool;
|
|
67
|
+
global.createMysqlStorageWithPool = createMysqlStorageWithPool;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
if (error.code !== 'MODULE_NOT_FOUND') {
|
|
70
|
+
console.warn('Breez SDK: Failed to load MySQL storage:', error.message);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Automatically import and set up the MySQL tree store for Node.js
|
|
75
|
+
try {
|
|
76
|
+
const { createMysqlTreeStore, createMysqlTreeStoreWithPool } = require('./mysql-tree-store/index.cjs');
|
|
77
|
+
global.createMysqlTreeStore = createMysqlTreeStore;
|
|
78
|
+
global.createMysqlTreeStoreWithPool = createMysqlTreeStoreWithPool;
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (error.code !== 'MODULE_NOT_FOUND') {
|
|
81
|
+
console.warn('Breez SDK: Failed to load MySQL tree store:', error.message);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Automatically import and set up the MySQL token store for Node.js
|
|
86
|
+
try {
|
|
87
|
+
const { createMysqlTokenStore, createMysqlTokenStoreWithPool } = require('./mysql-token-store/index.cjs');
|
|
88
|
+
global.createMysqlTokenStore = createMysqlTokenStore;
|
|
89
|
+
global.createMysqlTokenStoreWithPool = createMysqlTokenStoreWithPool;
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (error.code !== 'MODULE_NOT_FOUND') {
|
|
92
|
+
console.warn('Breez SDK: Failed to load MySQL token store:', error.message);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Automatically import and set up the MySQL session manager for Node.js
|
|
97
|
+
try {
|
|
98
|
+
const { createMysqlSessionManager, createMysqlSessionManagerWithPool } = require('./mysql-session-manager/index.cjs');
|
|
99
|
+
global.createMysqlSessionManager = createMysqlSessionManager;
|
|
100
|
+
global.createMysqlSessionManagerWithPool = createMysqlSessionManagerWithPool;
|
|
101
|
+
} catch (error) {
|
|
102
|
+
if (error.code !== 'MODULE_NOT_FOUND') {
|
|
103
|
+
console.warn('Breez SDK: Failed to load MySQL session manager:', error.message);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
51
107
|
// Export all WASM functions
|
|
52
108
|
module.exports = wasmModule;
|
package/nodejs/index.mjs
CHANGED
|
@@ -6,19 +6,28 @@ import pkg from './index.js';
|
|
|
6
6
|
export const {
|
|
7
7
|
connect,
|
|
8
8
|
connectWithSigner,
|
|
9
|
+
createMysqlConnectionPool,
|
|
10
|
+
createPostgresConnectionPool,
|
|
9
11
|
defaultConfig,
|
|
10
12
|
defaultExternalSigner,
|
|
13
|
+
defaultMysqlStorageConfig,
|
|
11
14
|
defaultPostgresStorageConfig,
|
|
12
15
|
getSparkStatus,
|
|
13
16
|
initLogging,
|
|
17
|
+
newRestChainService,
|
|
18
|
+
newSspConnectionManager,
|
|
14
19
|
task_worker_entry_point,
|
|
20
|
+
BitcoinChainServiceHandle,
|
|
15
21
|
BreezSdk,
|
|
16
22
|
DefaultSigner,
|
|
17
23
|
IntoUnderlyingByteSource,
|
|
18
24
|
IntoUnderlyingSink,
|
|
19
25
|
IntoUnderlyingSource,
|
|
26
|
+
MysqlConnectionPool,
|
|
20
27
|
Passkey,
|
|
28
|
+
PostgresConnectionPool,
|
|
21
29
|
SdkBuilder,
|
|
30
|
+
SspConnectionManager,
|
|
22
31
|
TokenIssuer,
|
|
23
32
|
} = pkg;
|
|
24
33
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// errors.cjs - Session manager error wrapper with cause chain support
|
|
2
|
+
class SessionManagerError extends Error {
|
|
3
|
+
constructor(message, cause = null) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = 'SessionManagerError';
|
|
6
|
+
this.cause = cause;
|
|
7
|
+
if (Error.captureStackTrace) {
|
|
8
|
+
Error.captureStackTrace(this, SessionManagerError);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = { SessionManagerError };
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CommonJS implementation for Node.js MySQL Session Manager.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors `postgres-session-manager/index.cjs` for MySQL 8.0+.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
let mysql;
|
|
8
|
+
try {
|
|
9
|
+
const mainModule = require.main;
|
|
10
|
+
if (mainModule) {
|
|
11
|
+
mysql = mainModule.require("mysql2/promise");
|
|
12
|
+
} else {
|
|
13
|
+
mysql = require("mysql2/promise");
|
|
14
|
+
}
|
|
15
|
+
} catch (error) {
|
|
16
|
+
try {
|
|
17
|
+
mysql = require("mysql2/promise");
|
|
18
|
+
} catch (fallbackError) {
|
|
19
|
+
throw new Error(
|
|
20
|
+
`mysql2 not found. Please install it in your project: npm install mysql2@^3.11.0\n` +
|
|
21
|
+
`Original error: ${error.message}\nFallback error: ${fallbackError.message}`
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const { SessionManagerError } = require("./errors.cjs");
|
|
27
|
+
const { MysqlSessionManagerMigrationManager } = require("./migrations.cjs");
|
|
28
|
+
|
|
29
|
+
class MysqlSessionManager {
|
|
30
|
+
/**
|
|
31
|
+
* @param {import('mysql2/promise').Pool} pool
|
|
32
|
+
* @param {Buffer|Uint8Array} identity - 33-byte secp256k1 compressed pubkey
|
|
33
|
+
* identifying the tenant. All reads and writes are scoped by this.
|
|
34
|
+
* @param {object} [logger]
|
|
35
|
+
*/
|
|
36
|
+
constructor(pool, identity, logger = null) {
|
|
37
|
+
if (!identity || identity.length !== 33) {
|
|
38
|
+
throw new SessionManagerError(
|
|
39
|
+
"tenant identity (33-byte secp256k1 pubkey) is required"
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
this.pool = pool;
|
|
43
|
+
this.identity = Buffer.from(identity);
|
|
44
|
+
this.logger = logger;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async initialize() {
|
|
48
|
+
try {
|
|
49
|
+
const migrationManager = new MysqlSessionManagerMigrationManager(this.logger);
|
|
50
|
+
await migrationManager.migrate(this.pool);
|
|
51
|
+
return this;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
throw new SessionManagerError(
|
|
54
|
+
`Failed to initialize MySQL session manager: ${error.message}`,
|
|
55
|
+
error
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async close() {
|
|
61
|
+
if (this.pool) {
|
|
62
|
+
await this.pool.end();
|
|
63
|
+
this.pool = null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @param {string} serviceIdentityKey - hex-encoded 33-byte secp256k1 pubkey
|
|
69
|
+
* @returns {Promise<{token: string, expiration: number} | null>}
|
|
70
|
+
*/
|
|
71
|
+
async getSession(serviceIdentityKey) {
|
|
72
|
+
const serviceKey = _decodePubkey(serviceIdentityKey);
|
|
73
|
+
try {
|
|
74
|
+
const [rows] = await this.pool.execute(
|
|
75
|
+
`SELECT token, expiration FROM sessions
|
|
76
|
+
WHERE user_id = ? AND service_identity_key = ?`,
|
|
77
|
+
[this.identity, serviceKey]
|
|
78
|
+
);
|
|
79
|
+
if (!rows || rows.length === 0) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
const row = rows[0];
|
|
83
|
+
return {
|
|
84
|
+
token: row.token,
|
|
85
|
+
expiration: Number(row.expiration),
|
|
86
|
+
};
|
|
87
|
+
} catch (error) {
|
|
88
|
+
throw new SessionManagerError(
|
|
89
|
+
`Failed to read session: ${error.message}`,
|
|
90
|
+
error
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* @param {string} serviceIdentityKey - hex-encoded 33-byte secp256k1 pubkey
|
|
97
|
+
* @param {{token: string, expiration: number}} session
|
|
98
|
+
*/
|
|
99
|
+
async setSession(serviceIdentityKey, session) {
|
|
100
|
+
const serviceKey = _decodePubkey(serviceIdentityKey);
|
|
101
|
+
try {
|
|
102
|
+
await this.pool.execute(
|
|
103
|
+
`INSERT INTO sessions (user_id, service_identity_key, token, expiration)
|
|
104
|
+
VALUES (?, ?, ?, ?)
|
|
105
|
+
ON DUPLICATE KEY UPDATE token = VALUES(token), expiration = VALUES(expiration)`,
|
|
106
|
+
[this.identity, serviceKey, session.token, session.expiration]
|
|
107
|
+
);
|
|
108
|
+
} catch (error) {
|
|
109
|
+
throw new SessionManagerError(
|
|
110
|
+
`Failed to write session: ${error.message}`,
|
|
111
|
+
error
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function _decodePubkey(hex) {
|
|
118
|
+
if (typeof hex !== "string" || hex.length !== 66) {
|
|
119
|
+
throw new SessionManagerError(
|
|
120
|
+
"service_identity_key must be a 66-character hex-encoded 33-byte pubkey"
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
return Buffer.from(hex, "hex");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function createMysqlSessionManager(poolConfig, identity, logger = null) {
|
|
127
|
+
const pool = mysql.createPool(poolConfig);
|
|
128
|
+
const manager = new MysqlSessionManager(pool, identity, logger);
|
|
129
|
+
await manager.initialize();
|
|
130
|
+
return manager;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function createMysqlSessionManagerWithPool(pool, identity, logger = null) {
|
|
134
|
+
const manager = new MysqlSessionManager(pool, identity, logger);
|
|
135
|
+
await manager.initialize();
|
|
136
|
+
return manager;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
module.exports = {
|
|
140
|
+
MysqlSessionManager,
|
|
141
|
+
createMysqlSessionManager,
|
|
142
|
+
createMysqlSessionManagerWithPool,
|
|
143
|
+
SessionManagerError,
|
|
144
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session manager migrations for MySQL 8.0+. Mirrors the Rust
|
|
3
|
+
* `MysqlSessionManager` schema exactly.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const { SessionManagerError } = require("./errors.cjs");
|
|
7
|
+
|
|
8
|
+
const SESSION_MIGRATIONS_TABLE = "session_schema_migrations";
|
|
9
|
+
const MIGRATION_LOCK_NAME = "breez_mysql_session_manager_migration_lock";
|
|
10
|
+
const MIGRATION_LOCK_TIMEOUT = 60;
|
|
11
|
+
|
|
12
|
+
class MysqlSessionManagerMigrationManager {
|
|
13
|
+
constructor(logger = null) {
|
|
14
|
+
this.logger = logger;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {import('mysql2/promise').Pool} pool
|
|
19
|
+
*/
|
|
20
|
+
async migrate(pool) {
|
|
21
|
+
const conn = await pool.getConnection();
|
|
22
|
+
try {
|
|
23
|
+
const [lockRows] = await conn.query(
|
|
24
|
+
"SELECT GET_LOCK(?, ?) AS acquired",
|
|
25
|
+
[MIGRATION_LOCK_NAME, MIGRATION_LOCK_TIMEOUT]
|
|
26
|
+
);
|
|
27
|
+
if (!lockRows || lockRows[0].acquired !== 1) {
|
|
28
|
+
throw new SessionManagerError(
|
|
29
|
+
`Failed to acquire session manager migration lock within ${MIGRATION_LOCK_TIMEOUT}s`
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
await conn.query("START TRANSACTION");
|
|
35
|
+
|
|
36
|
+
await conn.query(`
|
|
37
|
+
CREATE TABLE IF NOT EXISTS \`${SESSION_MIGRATIONS_TABLE}\` (
|
|
38
|
+
version INT PRIMARY KEY,
|
|
39
|
+
applied_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)
|
|
40
|
+
)
|
|
41
|
+
`);
|
|
42
|
+
|
|
43
|
+
const [versionRows] = await conn.query(
|
|
44
|
+
`SELECT COALESCE(MAX(version), 0) AS version FROM \`${SESSION_MIGRATIONS_TABLE}\``
|
|
45
|
+
);
|
|
46
|
+
const currentVersion = versionRows[0].version;
|
|
47
|
+
|
|
48
|
+
const migrations = this._getMigrations();
|
|
49
|
+
|
|
50
|
+
if (currentVersion >= migrations.length) {
|
|
51
|
+
await conn.query("COMMIT");
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
for (let i = currentVersion; i < migrations.length; i++) {
|
|
56
|
+
const migration = migrations[i];
|
|
57
|
+
const version = i + 1;
|
|
58
|
+
for (const sql of migration.sql) {
|
|
59
|
+
await conn.query(sql);
|
|
60
|
+
}
|
|
61
|
+
await conn.query(
|
|
62
|
+
`INSERT INTO \`${SESSION_MIGRATIONS_TABLE}\` (version) VALUES (?)`,
|
|
63
|
+
[version]
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
await conn.query("COMMIT");
|
|
68
|
+
} catch (error) {
|
|
69
|
+
await conn.query("ROLLBACK").catch(() => {});
|
|
70
|
+
throw new SessionManagerError(
|
|
71
|
+
`Session manager migration failed: ${error.message}`,
|
|
72
|
+
error
|
|
73
|
+
);
|
|
74
|
+
} finally {
|
|
75
|
+
await conn
|
|
76
|
+
.query("SELECT RELEASE_LOCK(?)", [MIGRATION_LOCK_NAME])
|
|
77
|
+
.catch(() => {});
|
|
78
|
+
}
|
|
79
|
+
} finally {
|
|
80
|
+
conn.release();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
_getMigrations() {
|
|
85
|
+
return [
|
|
86
|
+
{
|
|
87
|
+
name: "Create sessions table",
|
|
88
|
+
sql: [
|
|
89
|
+
`CREATE TABLE IF NOT EXISTS sessions (
|
|
90
|
+
user_id VARBINARY(33) NOT NULL,
|
|
91
|
+
service_identity_key VARBINARY(33) NOT NULL,
|
|
92
|
+
token TEXT NOT NULL,
|
|
93
|
+
expiration BIGINT NOT NULL,
|
|
94
|
+
PRIMARY KEY (user_id, service_identity_key)
|
|
95
|
+
)`,
|
|
96
|
+
],
|
|
97
|
+
},
|
|
98
|
+
];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
module.exports = { MysqlSessionManagerMigrationManager };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage Error Classes for Breez SDK MySQL Storage
|
|
3
|
+
* CommonJS version
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
class StorageError extends Error {
|
|
7
|
+
constructor(message, cause = null) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = 'StorageError';
|
|
10
|
+
this.cause = cause;
|
|
11
|
+
|
|
12
|
+
// Maintain proper stack trace for where our error was thrown (only available on V8)
|
|
13
|
+
if (Error.captureStackTrace) {
|
|
14
|
+
Error.captureStackTrace(this, StorageError);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = { StorageError };
|