local-lite 1.0.2 → 1.0.3
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/dist/actions/manager.js +6 -2
- package/dist/actions/store.js +33 -19
- package/dist/adapters/local/expo-sqlite.js +4 -2
- package/dist/adapters/local/libsql.js +3 -1
- package/dist/adapters/local/op-sqlite.js +2 -1
- package/dist/adapters/network/index.js +10 -5
- package/dist/adapters/remote/convex.js +37 -10
- package/dist/adapters/remote/firestore.js +11 -4
- package/dist/adapters/remote/self-hosted.js +8 -4
- package/dist/adapters/remote/supabase.js +95 -29
- package/dist/auth/apple.js +8 -4
- package/dist/auth/github.js +15 -7
- package/dist/auth/google.js +7 -4
- package/dist/auth/identity.js +5 -1
- package/dist/auth/local.js +74 -21
- package/dist/auth/microsoft.js +22 -8
- package/dist/auth/session.js +45 -34
- package/dist/auth/unified.js +3 -2
- package/dist/backups/cloud-storage.js +10 -7
- package/dist/backups/encryption.js +4 -2
- package/dist/backups/onedrive.js +3 -3
- package/dist/backups/operations.js +4 -2
- package/dist/config/index.d.ts +1 -1
- package/dist/config/resolve-local.js +18 -6
- package/dist/config/resolve-remote.js +2 -1
- package/dist/core/client.d.ts +2 -2
- package/dist/core/client.js +64 -26
- package/dist/core/index.d.ts +1 -1
- package/dist/core/maintenance.js +5 -4
- package/dist/core/meta-store.js +3 -3
- package/dist/core/outbox-store.js +32 -25
- package/dist/core/query-builder.js +23 -10
- package/dist/core/query.js +19 -7
- package/dist/core/schema-manager.js +6 -2
- package/dist/core/sync-worker.js +38 -12
- package/dist/core/table-manager.js +8 -2
- package/dist/core/types.d.ts +1 -1
- package/dist/devtools/BenchmarkModal.d.ts +1 -1
- package/dist/devtools/BenchmarkModal.js +234 -54
- package/dist/devtools/SyncInspector.js +260 -37
- package/dist/email/index.js +46 -18
- package/dist/keys/generator.js +4 -1
- package/dist/keys/manager.js +22 -4
- package/dist/keys/signed-token.js +12 -4
- package/dist/keys/store.js +23 -16
- package/dist/kv/manager.js +5 -2
- package/dist/kv/store.js +13 -13
- package/dist/react/auth.js +12 -12
- package/dist/react/backups.js +1 -1
- package/dist/react/index.js +14 -9
- package/dist/react/runner.js +1 -1
- package/dist/react/status.js +6 -2
- package/dist/reactive/event-emitter.js +2 -2
- package/dist/scheduler/cron.js +3 -1
- package/dist/scheduler/store.js +37 -31
- package/dist/schema/generator.js +37 -34
- package/dist/search/manager.js +3 -1
- package/dist/search/store.js +17 -16
- package/dist/server/email.js +3 -2
- package/dist/server/index.js +4 -4
- package/dist/server/sync.js +11 -4
- package/dist/storage/driver.js +2 -1
- package/dist/storage/expo.js +5 -2
- package/dist/storage/manager.js +3 -1
- package/dist/storage/store.js +26 -25
- package/dist/utils/dynamic.js +4 -2
- package/dist/utils/logger.js +13 -6
- package/dist/utils/portability.js +21 -9
- package/package.json +1 -1
|
@@ -42,7 +42,8 @@ class SupabaseRemoteAdapter {
|
|
|
42
42
|
this.updatedAtColumn = options.updatedAtColumn ?? 'updated_at';
|
|
43
43
|
this.primaryKey = options.primaryKey ?? 'id';
|
|
44
44
|
this.deletedColumn = options.deletedColumn ?? 'deleted';
|
|
45
|
-
this.deletedValue =
|
|
45
|
+
this.deletedValue =
|
|
46
|
+
options.deletedValue !== undefined ? options.deletedValue : true;
|
|
46
47
|
this.pushRpcName = options.pushRpcName;
|
|
47
48
|
this.schema = options.schema ?? 'public';
|
|
48
49
|
this.channelName = options.channelName ?? 'local_first_sync';
|
|
@@ -77,14 +78,18 @@ class SupabaseRemoteAdapter {
|
|
|
77
78
|
const previousClient = this.getClient();
|
|
78
79
|
this.clientResolver = client;
|
|
79
80
|
const newClient = this.getClient();
|
|
80
|
-
if (this.activeChannel &&
|
|
81
|
+
if (this.activeChannel &&
|
|
82
|
+
previousClient?.removeChannel &&
|
|
83
|
+
previousClient !== newClient) {
|
|
81
84
|
try {
|
|
82
85
|
previousClient.removeChannel(this.activeChannel);
|
|
83
86
|
}
|
|
84
87
|
catch { }
|
|
85
88
|
this.activeChannel = undefined;
|
|
86
89
|
}
|
|
87
|
-
if (this.activeLiveCallback &&
|
|
90
|
+
if (this.activeLiveCallback &&
|
|
91
|
+
newClient &&
|
|
92
|
+
typeof newClient.channel === 'function') {
|
|
88
93
|
this.subscribeToLiveChanges(this.activeLiveCallback);
|
|
89
94
|
}
|
|
90
95
|
}
|
|
@@ -145,7 +150,10 @@ class SupabaseRemoteAdapter {
|
|
|
145
150
|
const rowData = this.buildRowData(batch[0]);
|
|
146
151
|
const { error } = await client.from(op.tableName).upsert(rowData);
|
|
147
152
|
if (error) {
|
|
148
|
-
const msg = (0, logger_js_1.formatSupabaseError)(error, {
|
|
153
|
+
const msg = (0, logger_js_1.formatSupabaseError)(error, {
|
|
154
|
+
table: op.tableName,
|
|
155
|
+
operation: 'INSERT',
|
|
156
|
+
});
|
|
149
157
|
this.logger.error(msg);
|
|
150
158
|
failedIds.push(batch[0].id);
|
|
151
159
|
errors[batch[0].id] = msg;
|
|
@@ -155,7 +163,10 @@ class SupabaseRemoteAdapter {
|
|
|
155
163
|
}
|
|
156
164
|
}
|
|
157
165
|
catch (err) {
|
|
158
|
-
const msg = (0, logger_js_1.formatSupabaseError)(err, {
|
|
166
|
+
const msg = (0, logger_js_1.formatSupabaseError)(err, {
|
|
167
|
+
table: op.tableName,
|
|
168
|
+
operation: 'INSERT',
|
|
169
|
+
});
|
|
159
170
|
this.logger.error(msg);
|
|
160
171
|
failedIds.push(batch[0].id);
|
|
161
172
|
errors[batch[0].id] = msg;
|
|
@@ -167,9 +178,14 @@ class SupabaseRemoteAdapter {
|
|
|
167
178
|
for (const item of batch) {
|
|
168
179
|
try {
|
|
169
180
|
const singleRow = this.buildRowData(item);
|
|
170
|
-
const { error: singleErr } = await client
|
|
181
|
+
const { error: singleErr } = await client
|
|
182
|
+
.from(op.tableName)
|
|
183
|
+
.upsert(singleRow);
|
|
171
184
|
if (singleErr) {
|
|
172
|
-
const msg = (0, logger_js_1.formatSupabaseError)(singleErr, {
|
|
185
|
+
const msg = (0, logger_js_1.formatSupabaseError)(singleErr, {
|
|
186
|
+
table: op.tableName,
|
|
187
|
+
operation: 'INSERT',
|
|
188
|
+
});
|
|
173
189
|
failedIds.push(item.id);
|
|
174
190
|
errors[item.id] = msg;
|
|
175
191
|
}
|
|
@@ -178,7 +194,10 @@ class SupabaseRemoteAdapter {
|
|
|
178
194
|
}
|
|
179
195
|
}
|
|
180
196
|
catch (singleErr) {
|
|
181
|
-
const msg = (0, logger_js_1.formatSupabaseError)(singleErr, {
|
|
197
|
+
const msg = (0, logger_js_1.formatSupabaseError)(singleErr, {
|
|
198
|
+
table: op.tableName,
|
|
199
|
+
operation: 'INSERT',
|
|
200
|
+
});
|
|
182
201
|
failedIds.push(item.id);
|
|
183
202
|
errors[item.id] = msg;
|
|
184
203
|
}
|
|
@@ -223,7 +242,10 @@ class SupabaseRemoteAdapter {
|
|
|
223
242
|
.update(patch)
|
|
224
243
|
.eq(this.primaryKey, op.rowId);
|
|
225
244
|
if (error) {
|
|
226
|
-
const msg = (0, logger_js_1.formatSupabaseError)(error, {
|
|
245
|
+
const msg = (0, logger_js_1.formatSupabaseError)(error, {
|
|
246
|
+
table: op.tableName,
|
|
247
|
+
operation: 'DELETE',
|
|
248
|
+
});
|
|
227
249
|
this.logger.error(msg);
|
|
228
250
|
failedIds.push(op.id);
|
|
229
251
|
errors[op.id] = msg;
|
|
@@ -233,7 +255,10 @@ class SupabaseRemoteAdapter {
|
|
|
233
255
|
}
|
|
234
256
|
}
|
|
235
257
|
catch (err) {
|
|
236
|
-
const msg = (0, logger_js_1.formatSupabaseError)(err, {
|
|
258
|
+
const msg = (0, logger_js_1.formatSupabaseError)(err, {
|
|
259
|
+
table: op.tableName,
|
|
260
|
+
operation: 'DELETE',
|
|
261
|
+
});
|
|
237
262
|
this.logger.error(msg);
|
|
238
263
|
failedIds.push(op.id);
|
|
239
264
|
errors[op.id] = msg;
|
|
@@ -260,7 +285,10 @@ class SupabaseRemoteAdapter {
|
|
|
260
285
|
.update(patch)
|
|
261
286
|
.eq(this.primaryKey, item.rowId);
|
|
262
287
|
if (singleErr) {
|
|
263
|
-
const msg = (0, logger_js_1.formatSupabaseError)(singleErr, {
|
|
288
|
+
const msg = (0, logger_js_1.formatSupabaseError)(singleErr, {
|
|
289
|
+
table: op.tableName,
|
|
290
|
+
operation: 'DELETE',
|
|
291
|
+
});
|
|
264
292
|
failedIds.push(item.id);
|
|
265
293
|
errors[item.id] = msg;
|
|
266
294
|
}
|
|
@@ -269,7 +297,10 @@ class SupabaseRemoteAdapter {
|
|
|
269
297
|
}
|
|
270
298
|
}
|
|
271
299
|
catch (singleErr) {
|
|
272
|
-
const msg = (0, logger_js_1.formatSupabaseError)(singleErr, {
|
|
300
|
+
const msg = (0, logger_js_1.formatSupabaseError)(singleErr, {
|
|
301
|
+
table: op.tableName,
|
|
302
|
+
operation: 'DELETE',
|
|
303
|
+
});
|
|
273
304
|
failedIds.push(item.id);
|
|
274
305
|
errors[item.id] = msg;
|
|
275
306
|
}
|
|
@@ -302,15 +333,23 @@ class SupabaseRemoteAdapter {
|
|
|
302
333
|
.eq(this.primaryKey, item.rowId)
|
|
303
334
|
.select(this.primaryKey);
|
|
304
335
|
if (error) {
|
|
305
|
-
const msg = (0, logger_js_1.formatSupabaseError)(error, {
|
|
336
|
+
const msg = (0, logger_js_1.formatSupabaseError)(error, {
|
|
337
|
+
table: op.tableName,
|
|
338
|
+
operation: 'UPDATE',
|
|
339
|
+
});
|
|
306
340
|
this.logger.error(msg);
|
|
307
341
|
failedIds.push(item.id);
|
|
308
342
|
errors[item.id] = msg;
|
|
309
343
|
}
|
|
310
344
|
else if (!data || data.length === 0) {
|
|
311
|
-
const { error: upsertErr } = await client
|
|
345
|
+
const { error: upsertErr } = await client
|
|
346
|
+
.from(op.tableName)
|
|
347
|
+
.upsert(patch);
|
|
312
348
|
if (upsertErr) {
|
|
313
|
-
const msg = (0, logger_js_1.formatSupabaseError)(upsertErr, {
|
|
349
|
+
const msg = (0, logger_js_1.formatSupabaseError)(upsertErr, {
|
|
350
|
+
table: op.tableName,
|
|
351
|
+
operation: 'UPSERT_FALLBACK',
|
|
352
|
+
});
|
|
314
353
|
this.logger.error(msg);
|
|
315
354
|
failedIds.push(item.id);
|
|
316
355
|
errors[item.id] = msg;
|
|
@@ -324,7 +363,10 @@ class SupabaseRemoteAdapter {
|
|
|
324
363
|
}
|
|
325
364
|
}
|
|
326
365
|
catch (err) {
|
|
327
|
-
const msg = (0, logger_js_1.formatSupabaseError)(err, {
|
|
366
|
+
const msg = (0, logger_js_1.formatSupabaseError)(err, {
|
|
367
|
+
table: op.tableName,
|
|
368
|
+
operation: 'UPDATE',
|
|
369
|
+
});
|
|
328
370
|
this.logger.error(msg);
|
|
329
371
|
failedIds.push(item.id);
|
|
330
372
|
errors[item.id] = msg;
|
|
@@ -344,9 +386,14 @@ class SupabaseRemoteAdapter {
|
|
|
344
386
|
for (const item of batch) {
|
|
345
387
|
try {
|
|
346
388
|
const singleRow = this.buildRowData(item);
|
|
347
|
-
const { error: singleErr } = await client
|
|
389
|
+
const { error: singleErr } = await client
|
|
390
|
+
.from(op.tableName)
|
|
391
|
+
.upsert(singleRow);
|
|
348
392
|
if (singleErr) {
|
|
349
|
-
const msg = (0, logger_js_1.formatSupabaseError)(singleErr, {
|
|
393
|
+
const msg = (0, logger_js_1.formatSupabaseError)(singleErr, {
|
|
394
|
+
table: op.tableName,
|
|
395
|
+
operation: 'UPDATE',
|
|
396
|
+
});
|
|
350
397
|
failedIds.push(item.id);
|
|
351
398
|
errors[item.id] = msg;
|
|
352
399
|
}
|
|
@@ -355,7 +402,10 @@ class SupabaseRemoteAdapter {
|
|
|
355
402
|
}
|
|
356
403
|
}
|
|
357
404
|
catch (singleErr) {
|
|
358
|
-
const msg = (0, logger_js_1.formatSupabaseError)(singleErr, {
|
|
405
|
+
const msg = (0, logger_js_1.formatSupabaseError)(singleErr, {
|
|
406
|
+
table: op.tableName,
|
|
407
|
+
operation: 'UPDATE',
|
|
408
|
+
});
|
|
359
409
|
failedIds.push(item.id);
|
|
360
410
|
errors[item.id] = msg;
|
|
361
411
|
}
|
|
@@ -407,7 +457,9 @@ class SupabaseRemoteAdapter {
|
|
|
407
457
|
let query = client.from(table).select('*');
|
|
408
458
|
if (isTableScoped && scope && typeof scope === 'object') {
|
|
409
459
|
for (const [col, val] of Object.entries(scope)) {
|
|
410
|
-
if (val !== undefined &&
|
|
460
|
+
if (val !== undefined &&
|
|
461
|
+
val !== null &&
|
|
462
|
+
typeof query.eq === 'function') {
|
|
411
463
|
query = query.eq(col, val);
|
|
412
464
|
}
|
|
413
465
|
}
|
|
@@ -425,7 +477,8 @@ class SupabaseRemoteAdapter {
|
|
|
425
477
|
// If PostgreSQL returns 42703 (undefined column) because a scoped column does not exist on this table,
|
|
426
478
|
// gracefully fallback to querying without the scope filter so synchronization doesn't halt.
|
|
427
479
|
if (error &&
|
|
428
|
-
(error.code === '42703' ||
|
|
480
|
+
(error.code === '42703' ||
|
|
481
|
+
String(error.message || '').includes('does not exist')) &&
|
|
429
482
|
isTableScoped) {
|
|
430
483
|
this.logger.debug(`[local-lite] Column in scope does not exist on table "${table}". Retrying without scope filter.`);
|
|
431
484
|
let fallbackQuery = client.from(table).select('*');
|
|
@@ -433,7 +486,9 @@ class SupabaseRemoteAdapter {
|
|
|
433
486
|
fallbackQuery = fallbackQuery.gt(this.updatedAtColumn, currentSinceIso);
|
|
434
487
|
}
|
|
435
488
|
if (typeof fallbackQuery.order === 'function') {
|
|
436
|
-
fallbackQuery = fallbackQuery.order(this.updatedAtColumn, {
|
|
489
|
+
fallbackQuery = fallbackQuery.order(this.updatedAtColumn, {
|
|
490
|
+
ascending: true,
|
|
491
|
+
});
|
|
437
492
|
}
|
|
438
493
|
if (typeof fallbackQuery.limit === 'function') {
|
|
439
494
|
fallbackQuery = fallbackQuery.limit(this.fetchLimit);
|
|
@@ -461,7 +516,8 @@ class SupabaseRemoteAdapter {
|
|
|
461
516
|
});
|
|
462
517
|
}
|
|
463
518
|
// Stop paging if unpaginated query or fewer rows than limit returned
|
|
464
|
-
if (typeof query.limit !== 'function' ||
|
|
519
|
+
if (typeof query.limit !== 'function' ||
|
|
520
|
+
data.length < this.fetchLimit) {
|
|
465
521
|
hasMore = false;
|
|
466
522
|
}
|
|
467
523
|
else {
|
|
@@ -493,7 +549,8 @@ class SupabaseRemoteAdapter {
|
|
|
493
549
|
}
|
|
494
550
|
catch (err) {
|
|
495
551
|
const msg = (0, logger_js_1.formatSupabaseError)(err, { table, operation: 'PULL' });
|
|
496
|
-
if (msg.includes('Network Offline') ||
|
|
552
|
+
if (msg.includes('Network Offline') ||
|
|
553
|
+
msg.includes('Failed to reach')) {
|
|
497
554
|
this.logger.warn(msg);
|
|
498
555
|
}
|
|
499
556
|
else {
|
|
@@ -504,7 +561,8 @@ class SupabaseRemoteAdapter {
|
|
|
504
561
|
}));
|
|
505
562
|
for (const tableChanges of results) {
|
|
506
563
|
for (const change of tableChanges) {
|
|
507
|
-
if (change.timestamp !== undefined &&
|
|
564
|
+
if (change.timestamp !== undefined &&
|
|
565
|
+
change.timestamp > maxServerTimestamp) {
|
|
508
566
|
maxServerTimestamp = change.timestamp;
|
|
509
567
|
}
|
|
510
568
|
changes.push(change);
|
|
@@ -539,7 +597,8 @@ class SupabaseRemoteAdapter {
|
|
|
539
597
|
const val = record[this.deletedColumn];
|
|
540
598
|
if (val === true || val === 1)
|
|
541
599
|
return true;
|
|
542
|
-
if (val &&
|
|
600
|
+
if (val &&
|
|
601
|
+
!['false', '0', 'null', 'undefined'].includes(String(val).toLowerCase())) {
|
|
543
602
|
return true;
|
|
544
603
|
}
|
|
545
604
|
return false;
|
|
@@ -670,7 +729,9 @@ class SupabaseRemoteAdapter {
|
|
|
670
729
|
throw new Error('Supabase client is not available.');
|
|
671
730
|
if (typeof client.functions?.invoke === 'function') {
|
|
672
731
|
try {
|
|
673
|
-
const { data, error } = await client.functions.invoke(functionName, {
|
|
732
|
+
const { data, error } = await client.functions.invoke(functionName, {
|
|
733
|
+
body: args,
|
|
734
|
+
});
|
|
674
735
|
if (!error)
|
|
675
736
|
return data;
|
|
676
737
|
if (error?.status !== 404 && !error?.message?.includes?.('not found')) {
|
|
@@ -716,7 +777,10 @@ class SupabaseRemoteAdapter {
|
|
|
716
777
|
* Enables or disables an automated cron job on Supabase.
|
|
717
778
|
*/
|
|
718
779
|
async setAutomationEnabled(jobId, enabled) {
|
|
719
|
-
return this.call('set_automation_enabled', {
|
|
780
|
+
return this.call('set_automation_enabled', {
|
|
781
|
+
job_id: jobId,
|
|
782
|
+
enabled,
|
|
783
|
+
});
|
|
720
784
|
}
|
|
721
785
|
/**
|
|
722
786
|
* Lists cloud backup archives stored on Supabase Storage.
|
|
@@ -746,7 +810,9 @@ class SupabaseRemoteAdapter {
|
|
|
746
810
|
async getDownloadUrl(fileIdOrKey) {
|
|
747
811
|
const client = this.getClient();
|
|
748
812
|
if (typeof client?.storage?.from === 'function') {
|
|
749
|
-
const { data } = await client.storage
|
|
813
|
+
const { data } = await client.storage
|
|
814
|
+
.from('backups')
|
|
815
|
+
.createSignedUrl(fileIdOrKey, 3600);
|
|
750
816
|
return data?.signedUrl ?? null;
|
|
751
817
|
}
|
|
752
818
|
return null;
|
package/dist/auth/apple.js
CHANGED
|
@@ -26,9 +26,11 @@ function base64UrlEncode(bytes) {
|
|
|
26
26
|
const second = bytes[index + 1];
|
|
27
27
|
const third = bytes[index + 2];
|
|
28
28
|
result += alphabet[first >> 2];
|
|
29
|
-
result +=
|
|
29
|
+
result +=
|
|
30
|
+
alphabet[((first & 3) << 4) | (second === undefined ? 0 : second >> 4)];
|
|
30
31
|
if (second !== undefined) {
|
|
31
|
-
result +=
|
|
32
|
+
result +=
|
|
33
|
+
alphabet[((second & 15) << 2) | (third === undefined ? 0 : third >> 6)];
|
|
32
34
|
}
|
|
33
35
|
if (third !== undefined)
|
|
34
36
|
result += alphabet[third & 63];
|
|
@@ -90,7 +92,7 @@ function createAppleAuthTokenStore(storage, prefix = 'local-lite-apple-auth') {
|
|
|
90
92
|
const raw = await storage.getItem(tokensKey);
|
|
91
93
|
return validateTokens(raw) ? raw : null;
|
|
92
94
|
},
|
|
93
|
-
setTokens: tokens => storage.setItem(tokensKey, tokens),
|
|
95
|
+
setTokens: (tokens) => storage.setItem(tokensKey, tokens),
|
|
94
96
|
removeTokens: () => storage.removeItem(tokensKey),
|
|
95
97
|
getUser: async () => {
|
|
96
98
|
const raw = await storage.getItem(userKey);
|
|
@@ -191,7 +193,9 @@ class AppleAuthClient {
|
|
|
191
193
|
}
|
|
192
194
|
const claims = parseJwtClaims(authorization.id_token);
|
|
193
195
|
const id = claims.sub || 'apple-user';
|
|
194
|
-
const email = claims.email ||
|
|
196
|
+
const email = claims.email ||
|
|
197
|
+
authorization.user?.email ||
|
|
198
|
+
`${id}@privaterelay.appleid.com`;
|
|
195
199
|
const givenName = authorization.user?.name?.firstName;
|
|
196
200
|
const familyName = authorization.user?.name?.lastName;
|
|
197
201
|
const nameParts = [givenName, familyName].filter(Boolean);
|
package/dist/auth/github.js
CHANGED
|
@@ -27,9 +27,11 @@ function base64UrlEncode(bytes) {
|
|
|
27
27
|
const second = bytes[index + 1];
|
|
28
28
|
const third = bytes[index + 2];
|
|
29
29
|
result += alphabet[first >> 2];
|
|
30
|
-
result +=
|
|
30
|
+
result +=
|
|
31
|
+
alphabet[((first & 3) << 4) | (second === undefined ? 0 : second >> 4)];
|
|
31
32
|
if (second !== undefined) {
|
|
32
|
-
result +=
|
|
33
|
+
result +=
|
|
34
|
+
alphabet[((second & 15) << 2) | (third === undefined ? 0 : third >> 6)];
|
|
33
35
|
}
|
|
34
36
|
if (third !== undefined)
|
|
35
37
|
result += alphabet[third & 63];
|
|
@@ -75,7 +77,7 @@ function createGitHubAuthTokenStore(storage, prefix = 'local-lite-github-auth')
|
|
|
75
77
|
const raw = await storage.getItem(tokensKey);
|
|
76
78
|
return validateTokens(raw) ? raw : null;
|
|
77
79
|
},
|
|
78
|
-
setTokens: tokens => storage.setItem(tokensKey, tokens),
|
|
80
|
+
setTokens: (tokens) => storage.setItem(tokensKey, tokens),
|
|
79
81
|
removeTokens: () => storage.removeItem(tokensKey),
|
|
80
82
|
getUser: async () => {
|
|
81
83
|
const raw = await storage.getItem(userKey);
|
|
@@ -184,7 +186,9 @@ class GitHubAuthClient {
|
|
|
184
186
|
});
|
|
185
187
|
const payload = (await response.json());
|
|
186
188
|
if (!response.ok || payload.error) {
|
|
187
|
-
throw new Error(payload.error_description ||
|
|
189
|
+
throw new Error(payload.error_description ||
|
|
190
|
+
payload.error ||
|
|
191
|
+
`GitHub OAuth token exchange failed (${response.status}).`);
|
|
188
192
|
}
|
|
189
193
|
if (!payload.access_token) {
|
|
190
194
|
throw new Error('GitHub OAuth did not return an access token.');
|
|
@@ -193,7 +197,9 @@ class GitHubAuthClient {
|
|
|
193
197
|
accessToken: payload.access_token,
|
|
194
198
|
tokenType: payload.token_type,
|
|
195
199
|
scope: payload.scope,
|
|
196
|
-
expiresAt: payload.expires_in
|
|
200
|
+
expiresAt: payload.expires_in
|
|
201
|
+
? Date.now() + payload.expires_in * 1000
|
|
202
|
+
: undefined,
|
|
197
203
|
};
|
|
198
204
|
await this.tokenStore.setTokens(storedTokens);
|
|
199
205
|
const user = await this.fetchUserProfile(storedTokens.accessToken);
|
|
@@ -265,7 +271,7 @@ class GitHubAuthClient {
|
|
|
265
271
|
});
|
|
266
272
|
if (emailsRes.ok) {
|
|
267
273
|
const emails = (await emailsRes.json());
|
|
268
|
-
const primary = emails.find(e => e.primary && e.verified) || emails[0];
|
|
274
|
+
const primary = emails.find((e) => e.primary && e.verified) || emails[0];
|
|
269
275
|
if (primary)
|
|
270
276
|
email = primary.email;
|
|
271
277
|
}
|
|
@@ -275,7 +281,9 @@ class GitHubAuthClient {
|
|
|
275
281
|
}
|
|
276
282
|
}
|
|
277
283
|
const rawId = String(raw.id ?? raw.sub ?? 'github_user');
|
|
278
|
-
const normalizedEmail = email
|
|
284
|
+
const normalizedEmail = email
|
|
285
|
+
? email.trim().toLowerCase()
|
|
286
|
+
: `${raw.login}@users.noreply.github.com`;
|
|
279
287
|
const id = (0, identity_js_1.resolveUnifiedUserId)({ id: rawId, email: normalizedEmail, emailVerified: true }, 'github');
|
|
280
288
|
const name = raw.name || raw.login || 'GitHub User';
|
|
281
289
|
const avatarUrl = raw.avatar_url || null;
|
package/dist/auth/google.js
CHANGED
|
@@ -30,9 +30,11 @@ function base64UrlEncode(bytes) {
|
|
|
30
30
|
const second = bytes[index + 1];
|
|
31
31
|
const third = bytes[index + 2];
|
|
32
32
|
result += alphabet[first >> 2];
|
|
33
|
-
result +=
|
|
33
|
+
result +=
|
|
34
|
+
alphabet[((first & 3) << 4) | (second === undefined ? 0 : second >> 4)];
|
|
34
35
|
if (second !== undefined) {
|
|
35
|
-
result +=
|
|
36
|
+
result +=
|
|
37
|
+
alphabet[((second & 15) << 2) | (third === undefined ? 0 : third >> 6)];
|
|
36
38
|
}
|
|
37
39
|
if (third !== undefined)
|
|
38
40
|
result += alphabet[third & 63];
|
|
@@ -45,7 +47,8 @@ function validateTokens(value) {
|
|
|
45
47
|
const tokens = value;
|
|
46
48
|
return (typeof tokens.accessToken === 'string' &&
|
|
47
49
|
typeof tokens.expiresAt === 'number' &&
|
|
48
|
-
(tokens.refreshToken === undefined ||
|
|
50
|
+
(tokens.refreshToken === undefined ||
|
|
51
|
+
typeof tokens.refreshToken === 'string') &&
|
|
49
52
|
(tokens.idToken === undefined || typeof tokens.idToken === 'string'));
|
|
50
53
|
}
|
|
51
54
|
function validateUser(value) {
|
|
@@ -81,7 +84,7 @@ function createGoogleAuthTokenStore(storage, prefix = 'local-lite-google-auth')
|
|
|
81
84
|
const raw = await storage.getItem(tokensKey);
|
|
82
85
|
return validateTokens(raw) ? raw : null;
|
|
83
86
|
},
|
|
84
|
-
setTokens: tokens => storage.setItem(tokensKey, tokens),
|
|
87
|
+
setTokens: (tokens) => storage.setItem(tokensKey, tokens),
|
|
85
88
|
removeTokens: () => storage.removeItem(tokensKey),
|
|
86
89
|
getUser: async () => {
|
|
87
90
|
const raw = await storage.getItem(userKey);
|
package/dist/auth/identity.js
CHANGED
|
@@ -30,7 +30,11 @@ function createUnifiedAuthUser(rawUser, provider, options) {
|
|
|
30
30
|
rawUser.email_verified === true ||
|
|
31
31
|
provider === 'microsoft' ||
|
|
32
32
|
provider === 'github';
|
|
33
|
-
const providerAccountId = String(rawUser.providerAccountId ??
|
|
33
|
+
const providerAccountId = String(rawUser.providerAccountId ??
|
|
34
|
+
rawUser.id ??
|
|
35
|
+
rawUser.sub ??
|
|
36
|
+
rawUser.userId ??
|
|
37
|
+
'');
|
|
34
38
|
const id = resolveUnifiedUserId({ id: providerAccountId, email, emailVerified }, provider, options);
|
|
35
39
|
const name = typeof rawUser.name === 'string' && rawUser.name.trim()
|
|
36
40
|
? rawUser.name.trim()
|
package/dist/auth/local.js
CHANGED
|
@@ -65,7 +65,9 @@ async function derivePbkdf2Key(password, salt, iterations, algorithm = exports.D
|
|
|
65
65
|
const hashName = isSha256 ? 'SHA-256' : 'SHA-512';
|
|
66
66
|
const hashFn = isSha256 ? sha2_js_1.sha256 : sha2_js_1.sha512;
|
|
67
67
|
// 1. Try hardware-accelerated WebCrypto if supported
|
|
68
|
-
const subtle = typeof globalThis !== 'undefined'
|
|
68
|
+
const subtle = typeof globalThis !== 'undefined'
|
|
69
|
+
? globalThis.crypto?.subtle
|
|
70
|
+
: undefined;
|
|
69
71
|
if (subtle?.importKey && subtle?.deriveBits) {
|
|
70
72
|
try {
|
|
71
73
|
const keyMaterial = await subtle.importKey('raw', (0, utils_js_1.utf8ToBytes)(password), { name: 'PBKDF2' }, false, ['deriveBits']);
|
|
@@ -143,7 +145,10 @@ function mapStoredUserRow(row) {
|
|
|
143
145
|
let metadata = null;
|
|
144
146
|
if (row.metadata) {
|
|
145
147
|
try {
|
|
146
|
-
metadata =
|
|
148
|
+
metadata =
|
|
149
|
+
typeof row.metadata === 'string'
|
|
150
|
+
? JSON.parse(row.metadata)
|
|
151
|
+
: row.metadata;
|
|
147
152
|
}
|
|
148
153
|
catch {
|
|
149
154
|
metadata = row.metadata;
|
|
@@ -159,7 +164,9 @@ function mapStoredUserRow(row) {
|
|
|
159
164
|
status: row.status || 'active',
|
|
160
165
|
failedLoginAttempts: Number(row.failed_login_attempts || 0),
|
|
161
166
|
lockedUntil: row.locked_until != null ? Number(row.locked_until) : null,
|
|
162
|
-
emailVerified: row.email_verified === 1 ||
|
|
167
|
+
emailVerified: row.email_verified === 1 ||
|
|
168
|
+
row.email_verified === '1' ||
|
|
169
|
+
row.email_verified === true,
|
|
163
170
|
createdAt: Number(row.created_at || 0),
|
|
164
171
|
updatedAt: Number(row.updated_at || 0),
|
|
165
172
|
lastLoginAt: row.last_login_at != null ? Number(row.last_login_at) : null,
|
|
@@ -212,10 +219,14 @@ class LocalAuthEngine {
|
|
|
212
219
|
this.algorithm = resolvedOptions.algorithm || exports.DEFAULT_PASSWORD_ALGORITHM;
|
|
213
220
|
this.iterations = resolvedOptions.iterations || exports.DEFAULT_PASSWORD_ITERATIONS;
|
|
214
221
|
this.saltBytes = resolvedOptions.saltBytes || exports.DEFAULT_SALT_BYTES;
|
|
215
|
-
this.maxFailedAttempts =
|
|
216
|
-
|
|
217
|
-
this.
|
|
218
|
-
|
|
222
|
+
this.maxFailedAttempts =
|
|
223
|
+
resolvedOptions.maxFailedAttempts ?? exports.DEFAULT_LOCKOUT_ATTEMPTS;
|
|
224
|
+
this.lockoutDurationMs =
|
|
225
|
+
resolvedOptions.lockoutDurationMs ?? exports.DEFAULT_LOCKOUT_DURATION_MS;
|
|
226
|
+
this.resetTokenExpiryMs =
|
|
227
|
+
resolvedOptions.resetTokenExpiryMs ?? exports.DEFAULT_RESET_TOKEN_EXPIRY_MS;
|
|
228
|
+
this.minPasswordLength =
|
|
229
|
+
resolvedOptions.minPasswordLength ?? exports.DEFAULT_MIN_PASSWORD_LENGTH;
|
|
219
230
|
this.customPasswordValidator = resolvedOptions.validatePassword;
|
|
220
231
|
this.remoteAuthHandler = resolvedOptions.remoteAuthHandler;
|
|
221
232
|
if (resolvedSession instanceof session_js_1.SessionManager) {
|
|
@@ -332,7 +343,9 @@ class LocalAuthEngine {
|
|
|
332
343
|
if (!email || !email.includes('@')) {
|
|
333
344
|
throw new Error('A valid email address is required.');
|
|
334
345
|
}
|
|
335
|
-
const username = options.username
|
|
346
|
+
const username = options.username
|
|
347
|
+
? options.username.trim().toLowerCase()
|
|
348
|
+
: null;
|
|
336
349
|
this.assertValidPassword(options.password);
|
|
337
350
|
// Uniqueness checks
|
|
338
351
|
const existingEmail = await this.getUserByEmail(email);
|
|
@@ -352,7 +365,9 @@ class LocalAuthEngine {
|
|
|
352
365
|
iterations: this.iterations,
|
|
353
366
|
algorithm: this.algorithm,
|
|
354
367
|
});
|
|
355
|
-
const metadataStr = options.metadata
|
|
368
|
+
const metadataStr = options.metadata
|
|
369
|
+
? JSON.stringify(options.metadata)
|
|
370
|
+
: null;
|
|
356
371
|
const role = options.role || 'user';
|
|
357
372
|
const emailVerified = options.emailVerified ? 1 : 0;
|
|
358
373
|
await this.db.execute(`INSERT INTO ${this.sqlUsersTable} (
|
|
@@ -466,14 +481,21 @@ class LocalAuthEngine {
|
|
|
466
481
|
// Reset failed attempts and update last login timestamp
|
|
467
482
|
await this.db.execute(`UPDATE ${this.sqlUsersTable} SET failed_login_attempts = 0, locked_until = NULL, status = 'active', last_login_at = ?, updated_at = ? WHERE id = ?;`, [now, now, row.id]);
|
|
468
483
|
// Auto-upgrade legacy or slower hashes to modern PBKDF2-SHA256 standard
|
|
469
|
-
if (row.algorithm !== this.algorithm ||
|
|
484
|
+
if (row.algorithm !== this.algorithm ||
|
|
485
|
+
row.iterations !== this.iterations) {
|
|
470
486
|
try {
|
|
471
487
|
const upgraded = await hashPassword(password, {
|
|
472
488
|
saltBytes: this.saltBytes,
|
|
473
489
|
iterations: this.iterations,
|
|
474
490
|
algorithm: this.algorithm,
|
|
475
491
|
});
|
|
476
|
-
await this.db.execute(`UPDATE ${this.sqlUsersTable} SET password_hash = ?, salt = ?, iterations = ?, algorithm = ? WHERE id = ?;`, [
|
|
492
|
+
await this.db.execute(`UPDATE ${this.sqlUsersTable} SET password_hash = ?, salt = ?, iterations = ?, algorithm = ? WHERE id = ?;`, [
|
|
493
|
+
upgraded.hash,
|
|
494
|
+
upgraded.salt,
|
|
495
|
+
upgraded.iterations,
|
|
496
|
+
upgraded.algorithm,
|
|
497
|
+
row.id,
|
|
498
|
+
]);
|
|
477
499
|
}
|
|
478
500
|
catch {
|
|
479
501
|
// Non-critical upgrade
|
|
@@ -517,7 +539,10 @@ class LocalAuthEngine {
|
|
|
517
539
|
async signOut() {
|
|
518
540
|
const current = await this.sessionManager.getActiveSession();
|
|
519
541
|
await this.sessionManager.clearSession();
|
|
520
|
-
this.events?.emit(this.usersTable, {
|
|
542
|
+
this.events?.emit(this.usersTable, {
|
|
543
|
+
operation: 'SIGN_OUT',
|
|
544
|
+
userId: current?.userId,
|
|
545
|
+
});
|
|
521
546
|
}
|
|
522
547
|
/**
|
|
523
548
|
* Subscribes to authentication state changes (sign in, sign out, user updates).
|
|
@@ -527,10 +552,14 @@ class LocalAuthEngine {
|
|
|
527
552
|
return () => { };
|
|
528
553
|
const unsubs = [
|
|
529
554
|
this.events.on(this.usersTable, () => {
|
|
530
|
-
this.getCurrentUser()
|
|
555
|
+
this.getCurrentUser()
|
|
556
|
+
.then(listener)
|
|
557
|
+
.catch(() => { });
|
|
531
558
|
}),
|
|
532
559
|
this.events.on('_local_sessions', () => {
|
|
533
|
-
this.getCurrentUser()
|
|
560
|
+
this.getCurrentUser()
|
|
561
|
+
.then(listener)
|
|
562
|
+
.catch(() => { });
|
|
534
563
|
}),
|
|
535
564
|
];
|
|
536
565
|
return () => {
|
|
@@ -599,7 +628,14 @@ class LocalAuthEngine {
|
|
|
599
628
|
iterations: this.iterations,
|
|
600
629
|
algorithm: this.algorithm,
|
|
601
630
|
});
|
|
602
|
-
await this.db.execute(`UPDATE ${this.sqlUsersTable} SET password_hash = ?, salt = ?, iterations = ?, algorithm = ?, updated_at = ? WHERE id = ?;`, [
|
|
631
|
+
await this.db.execute(`UPDATE ${this.sqlUsersTable} SET password_hash = ?, salt = ?, iterations = ?, algorithm = ?, updated_at = ? WHERE id = ?;`, [
|
|
632
|
+
creds.hash,
|
|
633
|
+
creds.salt,
|
|
634
|
+
creds.iterations,
|
|
635
|
+
creds.algorithm,
|
|
636
|
+
now,
|
|
637
|
+
options.userId,
|
|
638
|
+
]);
|
|
603
639
|
this.events?.emit(this.usersTable, {
|
|
604
640
|
operation: 'PASSWORD_CHANGED',
|
|
605
641
|
userId: options.userId,
|
|
@@ -678,7 +714,9 @@ class LocalAuthEngine {
|
|
|
678
714
|
params.push(email);
|
|
679
715
|
}
|
|
680
716
|
if (updates.username !== undefined) {
|
|
681
|
-
const username = updates.username
|
|
717
|
+
const username = updates.username
|
|
718
|
+
? updates.username.trim().toLowerCase()
|
|
719
|
+
: null;
|
|
682
720
|
if (username && username !== existing.username) {
|
|
683
721
|
const conflict = await this.getUserByUsername(username);
|
|
684
722
|
if (conflict && conflict.id !== id) {
|
|
@@ -716,7 +754,10 @@ class LocalAuthEngine {
|
|
|
716
754
|
if (active && active.userId === id) {
|
|
717
755
|
await this.sessionManager.updateUser(updated);
|
|
718
756
|
}
|
|
719
|
-
this.events?.emit(this.usersTable, {
|
|
757
|
+
this.events?.emit(this.usersTable, {
|
|
758
|
+
operation: 'USER_UPDATED',
|
|
759
|
+
user: updated,
|
|
760
|
+
});
|
|
720
761
|
return updated;
|
|
721
762
|
}
|
|
722
763
|
/**
|
|
@@ -724,12 +765,17 @@ class LocalAuthEngine {
|
|
|
724
765
|
*/
|
|
725
766
|
async deleteUser(id) {
|
|
726
767
|
await this.db.execute(`DELETE FROM ${this.sqlTokensTable} WHERE user_id = ?;`, [id]);
|
|
727
|
-
await this.db.execute(`DELETE FROM ${this.sqlUsersTable} WHERE id = ?;`, [
|
|
768
|
+
await this.db.execute(`DELETE FROM ${this.sqlUsersTable} WHERE id = ?;`, [
|
|
769
|
+
id,
|
|
770
|
+
]);
|
|
728
771
|
const active = await this.sessionManager.getActiveSession();
|
|
729
772
|
if (active && active.userId === id) {
|
|
730
773
|
await this.sessionManager.clearSession();
|
|
731
774
|
}
|
|
732
|
-
this.events?.emit(this.usersTable, {
|
|
775
|
+
this.events?.emit(this.usersTable, {
|
|
776
|
+
operation: 'USER_DELETED',
|
|
777
|
+
userId: id,
|
|
778
|
+
});
|
|
733
779
|
}
|
|
734
780
|
/**
|
|
735
781
|
* Manually locks a user account.
|
|
@@ -739,7 +785,11 @@ class LocalAuthEngine {
|
|
|
739
785
|
const duration = durationMs ?? this.lockoutDurationMs;
|
|
740
786
|
const lockedUntil = now + duration;
|
|
741
787
|
await this.db.execute(`UPDATE ${this.sqlUsersTable} SET status = 'locked', locked_until = ?, updated_at = ? WHERE id = ?;`, [lockedUntil, now, userId]);
|
|
742
|
-
this.events?.emit(this.usersTable, {
|
|
788
|
+
this.events?.emit(this.usersTable, {
|
|
789
|
+
operation: 'ACCOUNT_LOCKED',
|
|
790
|
+
userId,
|
|
791
|
+
lockedUntil,
|
|
792
|
+
});
|
|
743
793
|
}
|
|
744
794
|
/**
|
|
745
795
|
* Manually unlocks a user account and clears failed login attempts.
|
|
@@ -747,7 +797,10 @@ class LocalAuthEngine {
|
|
|
747
797
|
async unlockAccount(userId) {
|
|
748
798
|
const now = Date.now();
|
|
749
799
|
await this.db.execute(`UPDATE ${this.sqlUsersTable} SET status = 'active', failed_login_attempts = 0, locked_until = NULL, updated_at = ? WHERE id = ?;`, [now, userId]);
|
|
750
|
-
this.events?.emit(this.usersTable, {
|
|
800
|
+
this.events?.emit(this.usersTable, {
|
|
801
|
+
operation: 'ACCOUNT_UNLOCKED',
|
|
802
|
+
userId,
|
|
803
|
+
});
|
|
751
804
|
}
|
|
752
805
|
/**
|
|
753
806
|
* Lists local users with optional role, status filtering, and pagination.
|