remult-sqlite-github 0.1.0

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/index.cjs ADDED
@@ -0,0 +1,1100 @@
1
+ 'use strict';
2
+
3
+ var remult = require('remult');
4
+ var remultBetterSqlite3 = require('remult/remult-better-sqlite3');
5
+ var fs = require('fs');
6
+ var path = require('path');
7
+ var crypto = require('crypto');
8
+ var zlib = require('zlib');
9
+ var util = require('util');
10
+ var Database = require('better-sqlite3');
11
+
12
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
13
+
14
+ function _interopNamespace(e) {
15
+ if (e && e.__esModule) return e;
16
+ var n = Object.create(null);
17
+ if (e) {
18
+ Object.keys(e).forEach(function (k) {
19
+ if (k !== 'default') {
20
+ var d = Object.getOwnPropertyDescriptor(e, k);
21
+ Object.defineProperty(n, k, d.get ? d : {
22
+ enumerable: true,
23
+ get: function () { return e[k]; }
24
+ });
25
+ }
26
+ });
27
+ }
28
+ n.default = e;
29
+ return Object.freeze(n);
30
+ }
31
+
32
+ var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
33
+ var path__namespace = /*#__PURE__*/_interopNamespace(path);
34
+ var crypto__namespace = /*#__PURE__*/_interopNamespace(crypto);
35
+ var zlib__namespace = /*#__PURE__*/_interopNamespace(zlib);
36
+ var Database__default = /*#__PURE__*/_interopDefault(Database);
37
+
38
+ // src/index.ts
39
+ var GITHUB_API_BASE = "https://api.github.com";
40
+ var GitHubOperations = class {
41
+ config;
42
+ maxRetries;
43
+ cachedToken = null;
44
+ tokenExpiry = 0;
45
+ onEvent;
46
+ verbose;
47
+ constructor(config, maxRetries = 3, onEvent, verbose = false) {
48
+ this.config = config;
49
+ this.maxRetries = maxRetries;
50
+ this.onEvent = onEvent;
51
+ this.verbose = verbose;
52
+ }
53
+ /**
54
+ * Get a file from the repository
55
+ */
56
+ async getFile(path3) {
57
+ const fullPath = this.getFullPath(path3);
58
+ const url = `${GITHUB_API_BASE}/repos/${this.config.owner}/${this.config.repo}/contents/${fullPath}?ref=${this.config.branch}`;
59
+ const response = await this.withRetry(() => this.fetch(url));
60
+ if (response.status === 404) {
61
+ return null;
62
+ }
63
+ if (!response.ok) {
64
+ throw new Error(
65
+ `Failed to get file ${path3}: ${response.status} ${response.statusText}`
66
+ );
67
+ }
68
+ const data = await response.json();
69
+ if (data.content) {
70
+ const content = Buffer.from(data.content, "base64");
71
+ return {
72
+ content,
73
+ sha: data.sha,
74
+ size: data.size
75
+ };
76
+ }
77
+ if (data.sha && data.size > 0) {
78
+ return this.getBlob(data.sha, data.size);
79
+ }
80
+ return null;
81
+ }
82
+ /**
83
+ * Get a large file using the blob API
84
+ */
85
+ async getBlob(sha, size) {
86
+ const url = `${GITHUB_API_BASE}/repos/${this.config.owner}/${this.config.repo}/git/blobs/${sha}`;
87
+ const response = await this.withRetry(() => this.fetch(url));
88
+ if (!response.ok) {
89
+ throw new Error(
90
+ `Failed to get blob ${sha}: ${response.status} ${response.statusText}`
91
+ );
92
+ }
93
+ const data = await response.json();
94
+ const content = Buffer.from(data.content, "base64");
95
+ return {
96
+ content,
97
+ sha,
98
+ size
99
+ };
100
+ }
101
+ /**
102
+ * Create or update a file in the repository
103
+ * Returns the new commit SHA
104
+ */
105
+ async putFile(path3, content, message, sha) {
106
+ const fullPath = this.getFullPath(path3);
107
+ const url = `${GITHUB_API_BASE}/repos/${this.config.owner}/${this.config.repo}/contents/${fullPath}`;
108
+ const body = {
109
+ message,
110
+ content: content.toString("base64"),
111
+ branch: this.config.branch
112
+ };
113
+ if (sha) {
114
+ body.sha = sha;
115
+ }
116
+ const response = await this.withRetry(
117
+ () => this.fetch(url, {
118
+ method: "PUT",
119
+ body: JSON.stringify(body)
120
+ }),
121
+ true
122
+ // Allow conflict retry
123
+ );
124
+ if (response.status === 409) {
125
+ throw new ConflictError(
126
+ `Conflict updating ${path3}: file was modified concurrently`
127
+ );
128
+ }
129
+ if (!response.ok) {
130
+ const errorText = await response.text();
131
+ throw new Error(
132
+ `Failed to put file ${path3}: ${response.status} ${response.statusText} - ${errorText}`
133
+ );
134
+ }
135
+ const data = await response.json();
136
+ this.emitEvent({
137
+ type: "commit_created",
138
+ sha: data.commit.sha,
139
+ message
140
+ });
141
+ return data.content.sha;
142
+ }
143
+ /**
144
+ * Delete a file from the repository
145
+ */
146
+ async deleteFile(path3, sha, message) {
147
+ const fullPath = this.getFullPath(path3);
148
+ const url = `${GITHUB_API_BASE}/repos/${this.config.owner}/${this.config.repo}/contents/${fullPath}`;
149
+ const response = await this.withRetry(
150
+ () => this.fetch(url, {
151
+ method: "DELETE",
152
+ body: JSON.stringify({
153
+ message,
154
+ sha,
155
+ branch: this.config.branch
156
+ })
157
+ })
158
+ );
159
+ if (!response.ok && response.status !== 404) {
160
+ throw new Error(
161
+ `Failed to delete file ${path3}: ${response.status} ${response.statusText}`
162
+ );
163
+ }
164
+ }
165
+ /**
166
+ * List files in a directory
167
+ */
168
+ async listFiles(path3) {
169
+ const fullPath = this.getFullPath(path3);
170
+ const url = `${GITHUB_API_BASE}/repos/${this.config.owner}/${this.config.repo}/contents/${fullPath}?ref=${this.config.branch}`;
171
+ const response = await this.withRetry(() => this.fetch(url));
172
+ if (response.status === 404) {
173
+ return [];
174
+ }
175
+ if (!response.ok) {
176
+ throw new Error(
177
+ `Failed to list files at ${path3}: ${response.status} ${response.statusText}`
178
+ );
179
+ }
180
+ const data = await response.json();
181
+ if (!Array.isArray(data)) {
182
+ return [
183
+ {
184
+ name: data.name,
185
+ path: data.path,
186
+ sha: data.sha,
187
+ size: data.size,
188
+ type: data.type
189
+ }
190
+ ];
191
+ }
192
+ return data.map(
193
+ (item) => ({
194
+ name: item.name,
195
+ path: item.path,
196
+ sha: item.sha,
197
+ size: item.size,
198
+ type: item.type
199
+ })
200
+ );
201
+ }
202
+ /**
203
+ * Check current rate limit status
204
+ */
205
+ async checkRateLimit() {
206
+ const url = `${GITHUB_API_BASE}/rate_limit`;
207
+ const response = await this.fetch(url);
208
+ if (!response.ok) {
209
+ throw new Error(`Failed to check rate limit: ${response.statusText}`);
210
+ }
211
+ const data = await response.json();
212
+ return {
213
+ remaining: data.resources.core.remaining,
214
+ resetAt: new Date(data.resources.core.reset * 1e3),
215
+ limit: data.resources.core.limit
216
+ };
217
+ }
218
+ /**
219
+ * Ensure the branch exists, create if it doesn't
220
+ * For empty repos, we skip branch creation since files can be pushed directly
221
+ */
222
+ async ensureBranch() {
223
+ const url = `${GITHUB_API_BASE}/repos/${this.config.owner}/${this.config.repo}/branches/${this.config.branch}`;
224
+ const response = await this.fetch(url);
225
+ if (response.ok) {
226
+ return;
227
+ }
228
+ if (response.status !== 404) {
229
+ const errorText = await response.text();
230
+ throw new Error(
231
+ `Failed to check branch '${this.config.branch}': ${response.status} ${response.statusText}. ${errorText}`
232
+ );
233
+ }
234
+ const repoUrl = `${GITHUB_API_BASE}/repos/${this.config.owner}/${this.config.repo}`;
235
+ const repoResponse = await this.fetch(repoUrl);
236
+ if (!repoResponse.ok) {
237
+ const errorText = await repoResponse.text();
238
+ throw new Error(
239
+ `Failed to get repository info for '${this.config.owner}/${this.config.repo}': ${repoResponse.status} ${repoResponse.statusText}. ${errorText}`
240
+ );
241
+ }
242
+ const repoData = await repoResponse.json();
243
+ const defaultBranch = repoData.default_branch;
244
+ const refUrl = `${GITHUB_API_BASE}/repos/${this.config.owner}/${this.config.repo}/git/refs/heads/${defaultBranch}`;
245
+ const refResponse = await this.fetch(refUrl);
246
+ if (!refResponse.ok) {
247
+ if (refResponse.status === 409 || refResponse.status === 404) {
248
+ this.log(
249
+ `Repository appears to be empty. Branch '${this.config.branch}' will be created on first snapshot.`
250
+ );
251
+ return;
252
+ }
253
+ const errorText = await refResponse.text();
254
+ throw new Error(
255
+ `Failed to get default branch ref: ${refResponse.status} ${refResponse.statusText}. ${errorText}`
256
+ );
257
+ }
258
+ if (this.config.branch === defaultBranch) {
259
+ return;
260
+ }
261
+ const refData = await refResponse.json();
262
+ const sha = refData.object.sha;
263
+ const createUrl = `${GITHUB_API_BASE}/repos/${this.config.owner}/${this.config.repo}/git/refs`;
264
+ const createResponse = await this.fetch(createUrl, {
265
+ method: "POST",
266
+ body: JSON.stringify({
267
+ ref: `refs/heads/${this.config.branch}`,
268
+ sha
269
+ })
270
+ });
271
+ if (!createResponse.ok && createResponse.status !== 422) {
272
+ const errorText = await createResponse.text();
273
+ throw new Error(
274
+ `Failed to create branch '${this.config.branch}': ${createResponse.status} ${createResponse.statusText}. ${errorText}`
275
+ );
276
+ }
277
+ this.log(`Created branch: ${this.config.branch}`);
278
+ }
279
+ /**
280
+ * Get the full path including configured prefix
281
+ */
282
+ getFullPath(path3) {
283
+ if (!this.config.path) {
284
+ return path3;
285
+ }
286
+ return `${this.config.path}/${path3}`.replace(/\/+/g, "/");
287
+ }
288
+ /**
289
+ * Make an authenticated fetch request
290
+ */
291
+ async fetch(url, options = {}) {
292
+ const token = await this.getAuthToken();
293
+ const headers = {
294
+ Authorization: `Bearer ${token}`,
295
+ Accept: "application/vnd.github+json",
296
+ "X-GitHub-Api-Version": "2022-11-28",
297
+ ...options.headers
298
+ };
299
+ if (options.body) {
300
+ headers["Content-Type"] = "application/json";
301
+ }
302
+ return fetch(url, {
303
+ ...options,
304
+ headers
305
+ });
306
+ }
307
+ /**
308
+ * Get authentication token (PAT or GitHub App installation token)
309
+ */
310
+ async getAuthToken() {
311
+ if (this.config.token) {
312
+ return this.config.token;
313
+ }
314
+ if (this.cachedToken && Date.now() < this.tokenExpiry) {
315
+ return this.cachedToken;
316
+ }
317
+ if (!this.config.appId || !this.config.privateKey || !this.config.installationId) {
318
+ throw new Error(
319
+ "Either token or GitHub App credentials (appId, privateKey, installationId) must be provided"
320
+ );
321
+ }
322
+ const jwt = this.generateJWT(this.config.appId, this.config.privateKey);
323
+ const response = await fetch(
324
+ `${GITHUB_API_BASE}/app/installations/${this.config.installationId}/access_tokens`,
325
+ {
326
+ method: "POST",
327
+ headers: {
328
+ Authorization: `Bearer ${jwt}`,
329
+ Accept: "application/vnd.github+json",
330
+ "X-GitHub-Api-Version": "2022-11-28"
331
+ }
332
+ }
333
+ );
334
+ if (!response.ok) {
335
+ throw new Error(
336
+ `Failed to get installation token: ${response.statusText}`
337
+ );
338
+ }
339
+ const data = await response.json();
340
+ this.cachedToken = data.token;
341
+ this.tokenExpiry = Date.now() + 55 * 60 * 1e3;
342
+ return this.cachedToken;
343
+ }
344
+ /**
345
+ * Generate a JWT for GitHub App authentication
346
+ */
347
+ generateJWT(appId, privateKey) {
348
+ const now = Math.floor(Date.now() / 1e3);
349
+ const payload = {
350
+ iat: now - 60,
351
+ // Issued 60 seconds ago to account for clock drift
352
+ exp: now + 10 * 60,
353
+ // Expires in 10 minutes
354
+ iss: appId.toString()
355
+ };
356
+ const header = { alg: "RS256", typ: "JWT" };
357
+ const encodedHeader = this.base64UrlEncode(JSON.stringify(header));
358
+ const encodedPayload = this.base64UrlEncode(JSON.stringify(payload));
359
+ const signatureInput = `${encodedHeader}.${encodedPayload}`;
360
+ const signature = crypto__namespace.createSign("RSA-SHA256").update(signatureInput).sign(privateKey, "base64url");
361
+ return `${signatureInput}.${signature}`;
362
+ }
363
+ /**
364
+ * Base64 URL encode a string
365
+ */
366
+ base64UrlEncode(str) {
367
+ return Buffer.from(str).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
368
+ }
369
+ /**
370
+ * Execute a function with retry logic and exponential backoff
371
+ */
372
+ async withRetry(fn, allowConflict = false) {
373
+ let lastError;
374
+ for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
375
+ try {
376
+ const result = await fn();
377
+ if (result instanceof Response) {
378
+ await this.handleRateLimitHeaders(result);
379
+ }
380
+ return result;
381
+ } catch (error) {
382
+ lastError = error instanceof Error ? error : new Error(String(error));
383
+ if (error instanceof ConflictError && !allowConflict) {
384
+ throw error;
385
+ }
386
+ if (error instanceof Error && error.message.includes("rate limit")) {
387
+ const rateLimit = await this.checkRateLimit();
388
+ const waitTime = Math.max(
389
+ 0,
390
+ rateLimit.resetAt.getTime() - Date.now()
391
+ );
392
+ this.emitEvent({
393
+ type: "rate_limit_hit",
394
+ resetAt: rateLimit.resetAt,
395
+ remaining: rateLimit.remaining
396
+ });
397
+ if (waitTime > 0 && waitTime < 6e4) {
398
+ this.log(`Rate limited, waiting ${waitTime}ms`);
399
+ await this.sleep(waitTime);
400
+ continue;
401
+ }
402
+ }
403
+ this.emitEvent({
404
+ type: "sync_error",
405
+ error: lastError,
406
+ context: "github_operation",
407
+ willRetry: attempt < this.maxRetries
408
+ });
409
+ if (attempt < this.maxRetries) {
410
+ const delay = Math.min(1e3 * Math.pow(2, attempt), 3e4);
411
+ this.log(`Retry attempt ${attempt + 1} after ${delay}ms`);
412
+ await this.sleep(delay);
413
+ }
414
+ }
415
+ }
416
+ throw lastError || new Error("Unknown error");
417
+ }
418
+ /**
419
+ * Handle rate limit headers from response
420
+ */
421
+ async handleRateLimitHeaders(response) {
422
+ const remaining = parseInt(
423
+ response.headers.get("x-ratelimit-remaining") || "1000",
424
+ 10
425
+ );
426
+ const resetTimestamp = parseInt(
427
+ response.headers.get("x-ratelimit-reset") || "0",
428
+ 10
429
+ );
430
+ if (remaining === 0 && resetTimestamp > 0) {
431
+ const resetAt = new Date(resetTimestamp * 1e3);
432
+ const waitTime = Math.max(0, resetAt.getTime() - Date.now());
433
+ this.emitEvent({
434
+ type: "rate_limit_hit",
435
+ resetAt,
436
+ remaining: 0
437
+ });
438
+ if (waitTime > 0 && waitTime < 6e4) {
439
+ this.log(`Rate limit exhausted, waiting ${waitTime}ms`);
440
+ await this.sleep(waitTime);
441
+ }
442
+ }
443
+ }
444
+ /**
445
+ * Sleep for the specified duration
446
+ */
447
+ sleep(ms) {
448
+ return new Promise((resolve) => setTimeout(resolve, ms));
449
+ }
450
+ /**
451
+ * Emit an event if handler is registered
452
+ */
453
+ emitEvent(event) {
454
+ if (this.onEvent) {
455
+ this.onEvent(event);
456
+ }
457
+ }
458
+ /**
459
+ * Log a message if verbose mode is enabled
460
+ */
461
+ log(message) {
462
+ if (this.verbose) {
463
+ console.log(`[github-operations] ${message}`);
464
+ }
465
+ }
466
+ };
467
+ var ConflictError = class extends Error {
468
+ constructor(message) {
469
+ super(message);
470
+ this.name = "ConflictError";
471
+ }
472
+ };
473
+ var gunzip2 = util.promisify(zlib__namespace.gunzip);
474
+ var SNAPSHOT_DB_NAME = "snapshot.db";
475
+ var SNAPSHOT_DB_GZ_NAME = "snapshot.db.gz";
476
+ var SNAPSHOT_META_NAME = "snapshot.meta.json";
477
+ var RecoveryManager = class {
478
+ github;
479
+ dbPath;
480
+ onEvent;
481
+ verbose;
482
+ constructor(github, dbPath, onEvent, verbose = false) {
483
+ this.github = github;
484
+ this.dbPath = dbPath;
485
+ this.onEvent = onEvent;
486
+ this.verbose = verbose;
487
+ }
488
+ /**
489
+ * Check if local database exists
490
+ */
491
+ localDatabaseExists() {
492
+ return fs__namespace.existsSync(this.dbPath);
493
+ }
494
+ /**
495
+ * Find the latest snapshot in the repository
496
+ */
497
+ async findLatestSnapshot() {
498
+ try {
499
+ const compressedFile = await this.github.getFile(SNAPSHOT_DB_GZ_NAME);
500
+ if (compressedFile) {
501
+ return {
502
+ path: SNAPSHOT_DB_GZ_NAME,
503
+ sha: compressedFile.sha,
504
+ compressed: true
505
+ };
506
+ }
507
+ const uncompressedFile = await this.github.getFile(SNAPSHOT_DB_NAME);
508
+ if (uncompressedFile) {
509
+ return {
510
+ path: SNAPSHOT_DB_NAME,
511
+ sha: uncompressedFile.sha,
512
+ compressed: false
513
+ };
514
+ }
515
+ return null;
516
+ } catch (error) {
517
+ this.log(`Error finding snapshot: ${error}`);
518
+ return null;
519
+ }
520
+ }
521
+ /**
522
+ * Recover database from GitHub snapshot
523
+ */
524
+ async recover() {
525
+ this.emitEvent({
526
+ type: "recovery_started",
527
+ branch: ""
528
+ // Branch is handled by GitHubOperations
529
+ });
530
+ try {
531
+ const snapshot = await this.findLatestSnapshot();
532
+ if (!snapshot) {
533
+ this.log("No snapshot found in repository");
534
+ return { recovered: false };
535
+ }
536
+ this.log(`Found snapshot: ${snapshot.path}`);
537
+ const file = await this.github.getFile(snapshot.path);
538
+ if (!file) {
539
+ this.log("Failed to download snapshot");
540
+ return { recovered: false };
541
+ }
542
+ let dbContent;
543
+ if (snapshot.compressed) {
544
+ this.log("Decompressing snapshot...");
545
+ dbContent = await gunzip2(file.content);
546
+ } else {
547
+ dbContent = file.content;
548
+ }
549
+ const dir = path__namespace.dirname(this.dbPath);
550
+ if (!fs__namespace.existsSync(dir)) {
551
+ fs__namespace.mkdirSync(dir, { recursive: true });
552
+ }
553
+ fs__namespace.writeFileSync(this.dbPath, dbContent);
554
+ this.log(`Database recovered to ${this.dbPath}`);
555
+ await this.getSnapshotMetadata();
556
+ this.emitEvent({
557
+ type: "recovery_completed",
558
+ sha: file.sha
559
+ });
560
+ return {
561
+ recovered: true,
562
+ sha: file.sha,
563
+ size: dbContent.length,
564
+ compressed: snapshot.compressed
565
+ };
566
+ } catch (error) {
567
+ this.log(`Recovery error: ${error}`);
568
+ this.emitEvent({
569
+ type: "sync_error",
570
+ error: error instanceof Error ? error : new Error(String(error)),
571
+ context: "recovery",
572
+ willRetry: false
573
+ });
574
+ return { recovered: false };
575
+ }
576
+ }
577
+ /**
578
+ * Get snapshot metadata from repository
579
+ */
580
+ async getSnapshotMetadata() {
581
+ try {
582
+ const file = await this.github.getFile(SNAPSHOT_META_NAME);
583
+ if (!file) {
584
+ return null;
585
+ }
586
+ return JSON.parse(file.content.toString("utf-8"));
587
+ } catch {
588
+ return null;
589
+ }
590
+ }
591
+ /**
592
+ * Get the current snapshot SHA from the repository
593
+ */
594
+ async getCurrentSnapshotSha() {
595
+ const snapshot = await this.findLatestSnapshot();
596
+ return snapshot?.sha || null;
597
+ }
598
+ /**
599
+ * Emit an event if handler is registered
600
+ */
601
+ emitEvent(event) {
602
+ if (this.onEvent) {
603
+ this.onEvent(event);
604
+ }
605
+ }
606
+ /**
607
+ * Log a message if verbose mode is enabled
608
+ */
609
+ log(message) {
610
+ if (this.verbose) {
611
+ console.log(`[recovery] ${message}`);
612
+ }
613
+ }
614
+ };
615
+
616
+ // src/sync/sync-manager.ts
617
+ var gzip2 = util.promisify(zlib__namespace.gzip);
618
+ var SNAPSHOT_DB_NAME2 = "snapshot.db";
619
+ var SNAPSHOT_DB_GZ_NAME2 = "snapshot.db.gz";
620
+ var SNAPSHOT_META_NAME2 = "snapshot.meta.json";
621
+ var GitHubSyncManager = class {
622
+ options;
623
+ github;
624
+ recovery;
625
+ db = null;
626
+ hasChanges = false;
627
+ currentSha = null;
628
+ snapshotTimer = null;
629
+ isInitialized = false;
630
+ isClosed = false;
631
+ constructor(options) {
632
+ this.options = options;
633
+ this.github = new GitHubOperations(
634
+ options.github,
635
+ options.sync.maxRetries,
636
+ options.onEvent,
637
+ options.verbose
638
+ );
639
+ this.recovery = new RecoveryManager(
640
+ this.github,
641
+ options.dbPath,
642
+ options.onEvent,
643
+ options.verbose
644
+ );
645
+ }
646
+ /**
647
+ * Initialize the sync manager: recover from GitHub if needed, open database
648
+ */
649
+ async initialize() {
650
+ if (this.isInitialized) {
651
+ return this.db;
652
+ }
653
+ this.log("Initializing sync manager...");
654
+ await this.github.ensureBranch();
655
+ let recovered = false;
656
+ if (!this.recovery.localDatabaseExists()) {
657
+ this.log("Local database not found, attempting recovery from GitHub...");
658
+ const result = await this.recovery.recover();
659
+ recovered = result.recovered;
660
+ if (recovered) {
661
+ this.currentSha = await this.recovery.getCurrentSnapshotSha();
662
+ }
663
+ } else {
664
+ this.log("Local database exists, fetching current SHA...");
665
+ this.currentSha = await this.recovery.getCurrentSnapshotSha();
666
+ }
667
+ this.db = this.openDatabase();
668
+ if (this.options.sync.snapshotInterval > 0) {
669
+ this.startSnapshotTimer();
670
+ }
671
+ this.isInitialized = true;
672
+ this.emitEvent({
673
+ type: "initialized",
674
+ recovered
675
+ });
676
+ return this.db;
677
+ }
678
+ /**
679
+ * Open the SQLite database with configured options
680
+ */
681
+ openDatabase() {
682
+ const dir = path__namespace.dirname(this.options.dbPath);
683
+ if (dir && !fs__namespace.existsSync(dir)) {
684
+ fs__namespace.mkdirSync(dir, { recursive: true });
685
+ this.log(`Created directory: ${dir}`);
686
+ }
687
+ const sqliteOptions = this.options.sqliteOptions || {};
688
+ const db = new Database__default.default(this.options.dbPath);
689
+ if (this.options.sync.enableWal) {
690
+ db.pragma("journal_mode = WAL");
691
+ }
692
+ if (sqliteOptions.foreignKeys !== false) {
693
+ db.pragma("foreign_keys = ON");
694
+ }
695
+ if (sqliteOptions.busyTimeout) {
696
+ db.pragma(`busy_timeout = ${sqliteOptions.busyTimeout}`);
697
+ }
698
+ if (sqliteOptions.pragmas) {
699
+ for (const [key, value] of Object.entries(sqliteOptions.pragmas)) {
700
+ db.pragma(`${key} = ${value}`);
701
+ }
702
+ }
703
+ this.log("Database opened successfully");
704
+ return db;
705
+ }
706
+ /**
707
+ * Called after database writes to track changes
708
+ */
709
+ async onWrite() {
710
+ this.hasChanges = true;
711
+ }
712
+ /**
713
+ * Create a snapshot and upload to GitHub
714
+ */
715
+ async snapshot() {
716
+ if (this.isClosed) {
717
+ return;
718
+ }
719
+ if (this.options.sync.snapshotOnChange && !this.hasChanges) {
720
+ this.log("No changes detected, skipping snapshot");
721
+ return;
722
+ }
723
+ if (!this.db) {
724
+ throw new Error("Database not initialized");
725
+ }
726
+ this.log("Creating snapshot...");
727
+ try {
728
+ if (this.options.sync.enableWal) {
729
+ this.db.pragma("wal_checkpoint(TRUNCATE)");
730
+ }
731
+ const dbContent = fs__namespace.readFileSync(this.options.dbPath);
732
+ const checksum = crypto__namespace.createHash("sha256").update(dbContent).digest("hex");
733
+ let uploadContent;
734
+ let filename;
735
+ const compressed = this.options.sync.compression;
736
+ if (compressed) {
737
+ uploadContent = await gzip2(dbContent);
738
+ filename = SNAPSHOT_DB_GZ_NAME2;
739
+ this.log(`Compressed ${dbContent.length} -> ${uploadContent.length} bytes`);
740
+ } else {
741
+ uploadContent = dbContent;
742
+ filename = SNAPSHOT_DB_NAME2;
743
+ }
744
+ await this.uploadWithConflictRetry(filename, uploadContent, checksum, compressed);
745
+ this.hasChanges = false;
746
+ this.emitEvent({
747
+ type: "snapshot_uploaded",
748
+ sha: this.currentSha,
749
+ size: uploadContent.length,
750
+ compressed
751
+ });
752
+ this.log(`Snapshot uploaded successfully (${uploadContent.length} bytes)`);
753
+ } catch (error) {
754
+ this.emitEvent({
755
+ type: "sync_error",
756
+ error: error instanceof Error ? error : new Error(String(error)),
757
+ context: "snapshot",
758
+ willRetry: false
759
+ });
760
+ throw error;
761
+ }
762
+ }
763
+ /**
764
+ * Upload file with automatic conflict resolution
765
+ */
766
+ async uploadWithConflictRetry(filename, content, checksum, compressed, retries = 3) {
767
+ for (let attempt = 0; attempt < retries; attempt++) {
768
+ try {
769
+ const newSha = await this.github.putFile(
770
+ filename,
771
+ content,
772
+ `Update database snapshot`,
773
+ this.currentSha || void 0
774
+ );
775
+ this.currentSha = newSha;
776
+ const metadata = {
777
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
778
+ size: content.length,
779
+ checksum,
780
+ compressed
781
+ };
782
+ const existingMeta = await this.github.getFile(SNAPSHOT_META_NAME2);
783
+ await this.github.putFile(
784
+ SNAPSHOT_META_NAME2,
785
+ Buffer.from(JSON.stringify(metadata, null, 2)),
786
+ `Update snapshot metadata`,
787
+ existingMeta?.sha
788
+ );
789
+ const oldFilename = compressed ? SNAPSHOT_DB_NAME2 : SNAPSHOT_DB_GZ_NAME2;
790
+ try {
791
+ const oldFile = await this.github.getFile(oldFilename);
792
+ if (oldFile) {
793
+ await this.github.deleteFile(
794
+ oldFilename,
795
+ oldFile.sha,
796
+ `Remove old snapshot format`
797
+ );
798
+ }
799
+ } catch {
800
+ }
801
+ return;
802
+ } catch (error) {
803
+ if (error instanceof ConflictError && attempt < retries - 1) {
804
+ this.log(`Conflict detected, refetching SHA and retrying (attempt ${attempt + 1})`);
805
+ this.currentSha = await this.recovery.getCurrentSnapshotSha();
806
+ continue;
807
+ }
808
+ throw error;
809
+ }
810
+ }
811
+ }
812
+ /**
813
+ * Force an immediate sync (alias for snapshot)
814
+ */
815
+ async forceSync() {
816
+ const originalOnChange = this.options.sync.snapshotOnChange;
817
+ this.options.sync.snapshotOnChange = false;
818
+ try {
819
+ await this.snapshot();
820
+ } finally {
821
+ this.options.sync.snapshotOnChange = originalOnChange;
822
+ }
823
+ }
824
+ /**
825
+ * Close the sync manager and perform final sync
826
+ */
827
+ async close() {
828
+ if (this.isClosed) {
829
+ return;
830
+ }
831
+ this.log("Closing sync manager...");
832
+ this.isClosed = true;
833
+ if (this.snapshotTimer) {
834
+ clearInterval(this.snapshotTimer);
835
+ this.snapshotTimer = null;
836
+ }
837
+ if (this.hasChanges && this.db) {
838
+ try {
839
+ await this.snapshot();
840
+ } catch (error) {
841
+ this.log(`Error during final snapshot: ${error}`);
842
+ }
843
+ }
844
+ if (this.db) {
845
+ this.db.close();
846
+ this.db = null;
847
+ }
848
+ this.log("Sync manager closed");
849
+ }
850
+ /**
851
+ * Start the automatic snapshot timer
852
+ */
853
+ startSnapshotTimer() {
854
+ this.snapshotTimer = setInterval(() => {
855
+ this.snapshot().catch((error) => {
856
+ this.log(`Snapshot error: ${error}`);
857
+ });
858
+ }, this.options.sync.snapshotInterval);
859
+ if (this.snapshotTimer.unref) {
860
+ this.snapshotTimer.unref();
861
+ }
862
+ }
863
+ /**
864
+ * Get the raw database instance
865
+ */
866
+ get database() {
867
+ return this.db;
868
+ }
869
+ /**
870
+ * Check if there are pending changes
871
+ */
872
+ get hasPendingChanges() {
873
+ return this.hasChanges;
874
+ }
875
+ /**
876
+ * Check if the manager is initialized
877
+ */
878
+ get initialized() {
879
+ return this.isInitialized;
880
+ }
881
+ /**
882
+ * Emit an event if handler is registered
883
+ */
884
+ emitEvent(event) {
885
+ if (this.options.onEvent) {
886
+ this.options.onEvent(event);
887
+ }
888
+ }
889
+ /**
890
+ * Log a message if verbose mode is enabled
891
+ */
892
+ log(message) {
893
+ if (this.options.verbose) {
894
+ console.log(`[sync-manager] ${message}`);
895
+ }
896
+ }
897
+ };
898
+
899
+ // src/provider.ts
900
+ var DEFAULT_SYNC_CONFIG = {
901
+ snapshotInterval: 30 * 1e3,
902
+ // 30 seconds
903
+ snapshotOnChange: true,
904
+ enableWal: false,
905
+ walThreshold: 1024 * 1024,
906
+ // 1MB
907
+ maxRetries: 3,
908
+ compression: false
909
+ };
910
+ var BetterSqlite3GitHubDataProvider = class {
911
+ syncManager;
912
+ _isInitialized = false;
913
+ initPromise = null;
914
+ innerProvider = null;
915
+ constructor(options) {
916
+ const githubConfig = {
917
+ owner: options.github.owner,
918
+ repo: options.github.repo,
919
+ branch: options.github.branch || "main",
920
+ path: options.github.path || "",
921
+ token: options.github.token,
922
+ appId: options.github.appId,
923
+ privateKey: options.github.privateKey,
924
+ installationId: options.github.installationId
925
+ };
926
+ const syncConfig = {
927
+ ...DEFAULT_SYNC_CONFIG,
928
+ ...options.sync
929
+ };
930
+ const managerOptions = {
931
+ dbPath: options.file,
932
+ github: githubConfig,
933
+ sqliteOptions: options.sqliteOptions,
934
+ sync: syncConfig,
935
+ onEvent: options.onEvent,
936
+ verbose: options.verbose || false
937
+ };
938
+ this.syncManager = new GitHubSyncManager(managerOptions);
939
+ }
940
+ /**
941
+ * Initialize the data provider
942
+ * This must be called before using the provider
943
+ */
944
+ async init() {
945
+ if (this._isInitialized) {
946
+ return;
947
+ }
948
+ if (this.initPromise) {
949
+ return this.initPromise;
950
+ }
951
+ this.initPromise = this.doInit();
952
+ await this.initPromise;
953
+ }
954
+ async doInit() {
955
+ const db = await this.syncManager.initialize();
956
+ this.innerProvider = new remultBetterSqlite3.BetterSqlite3DataProvider(db);
957
+ this._isInitialized = true;
958
+ }
959
+ /**
960
+ * Ensure the provider is initialized
961
+ */
962
+ async ensureInitialized() {
963
+ if (!this._isInitialized) {
964
+ await this.init();
965
+ }
966
+ }
967
+ getProvider() {
968
+ if (!this.innerProvider) {
969
+ throw new Error(
970
+ "Provider not initialized. Call init() before using the provider."
971
+ );
972
+ }
973
+ return this.innerProvider;
974
+ }
975
+ // SqlImplementation interface methods
976
+ getLimitSqlSyntax(limit, offset) {
977
+ return this.getProvider().getLimitSqlSyntax(limit, offset);
978
+ }
979
+ createCommand() {
980
+ const command = this.getProvider().createCommand();
981
+ const originalExecute = command.execute.bind(command);
982
+ const syncManager = this.syncManager;
983
+ const isWriteOp = this.isWriteOperation.bind(this);
984
+ command.execute = async (sql) => {
985
+ const result = await originalExecute(sql);
986
+ if (isWriteOp(sql)) {
987
+ await syncManager.onWrite();
988
+ }
989
+ return result;
990
+ };
991
+ return command;
992
+ }
993
+ async transaction(action) {
994
+ await this.ensureInitialized();
995
+ return this.getProvider().transaction(action);
996
+ }
997
+ async entityIsUsedForTheFirstTime(entity) {
998
+ await this.ensureInitialized();
999
+ return this.getProvider().entityIsUsedForTheFirstTime(entity);
1000
+ }
1001
+ async end() {
1002
+ await this.close();
1003
+ }
1004
+ // Passthrough properties from inner provider
1005
+ get orderByNullsFirst() {
1006
+ return this.innerProvider?.orderByNullsFirst;
1007
+ }
1008
+ get afterMutation() {
1009
+ return this.innerProvider?.afterMutation;
1010
+ }
1011
+ get supportsJsonColumnType() {
1012
+ return this.innerProvider?.supportsJsonColumnType;
1013
+ }
1014
+ get doesNotSupportReturningSyntax() {
1015
+ return this.innerProvider?.doesNotSupportReturningSyntax ?? true;
1016
+ }
1017
+ // Additional methods from SqliteCoreDataProvider that may be called
1018
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1019
+ async ensureSchema(entities) {
1020
+ await this.ensureInitialized();
1021
+ return this.getProvider().ensureSchema(entities);
1022
+ }
1023
+ wrapIdentifier(name) {
1024
+ return this.getProvider().wrapIdentifier(name);
1025
+ }
1026
+ addColumnSqlSyntax(x, dbName, isAlterTable) {
1027
+ return this.getProvider().addColumnSqlSyntax(x, dbName, isAlterTable);
1028
+ }
1029
+ /**
1030
+ * Check if a SQL statement is a write operation
1031
+ */
1032
+ isWriteOperation(sql) {
1033
+ const upperSql = sql.trim().toUpperCase();
1034
+ return upperSql.startsWith("INSERT") || upperSql.startsWith("UPDATE") || upperSql.startsWith("DELETE") || upperSql.startsWith("CREATE") || upperSql.startsWith("DROP") || upperSql.startsWith("ALTER");
1035
+ }
1036
+ /**
1037
+ * Force an immediate sync to GitHub
1038
+ */
1039
+ async forceSync() {
1040
+ await this.ensureInitialized();
1041
+ await this.syncManager.forceSync();
1042
+ }
1043
+ /**
1044
+ * Create a snapshot and upload to GitHub
1045
+ */
1046
+ async snapshot() {
1047
+ await this.ensureInitialized();
1048
+ await this.syncManager.snapshot();
1049
+ }
1050
+ /**
1051
+ * Close the provider and perform final sync
1052
+ */
1053
+ async close() {
1054
+ await this.syncManager.close();
1055
+ this._isInitialized = false;
1056
+ this.initPromise = null;
1057
+ this.innerProvider = null;
1058
+ }
1059
+ /**
1060
+ * Get the raw better-sqlite3 database instance
1061
+ */
1062
+ get rawDatabase() {
1063
+ return this.syncManager.database;
1064
+ }
1065
+ /**
1066
+ * Get the sync manager instance
1067
+ */
1068
+ get sync() {
1069
+ return this.syncManager;
1070
+ }
1071
+ /**
1072
+ * Check if the provider is initialized
1073
+ */
1074
+ get isInitialized() {
1075
+ return this._isInitialized;
1076
+ }
1077
+ };
1078
+
1079
+ // src/index.ts
1080
+ function createGitHubDataProvider(options) {
1081
+ let provider = null;
1082
+ let sqlDatabase = null;
1083
+ return async () => {
1084
+ if (sqlDatabase) {
1085
+ return sqlDatabase;
1086
+ }
1087
+ provider = new BetterSqlite3GitHubDataProvider(options);
1088
+ await provider.init();
1089
+ sqlDatabase = new remult.SqlDatabase(provider);
1090
+ return sqlDatabase;
1091
+ };
1092
+ }
1093
+
1094
+ exports.BetterSqlite3GitHubDataProvider = BetterSqlite3GitHubDataProvider;
1095
+ exports.ConflictError = ConflictError;
1096
+ exports.GitHubOperations = GitHubOperations;
1097
+ exports.GitHubSyncManager = GitHubSyncManager;
1098
+ exports.createGitHubDataProvider = createGitHubDataProvider;
1099
+ //# sourceMappingURL=index.cjs.map
1100
+ //# sourceMappingURL=index.cjs.map