@tursodatabase/database 0.5.0-pre.3 → 0.5.0-pre.4

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.
@@ -1 +1 @@
1
- {"version":3,"file":"compat.d.ts","sourceRoot":"","sources":["../compat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAkB,WAAW,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAA;AAG1G,cAAM,QAAS,SAAQ,cAAc;gBACrB,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,YAAiB;CAGpD;AAED,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAA"}
1
+ {"version":3,"file":"compat.d.ts","sourceRoot":"","sources":["../compat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAkB,WAAW,EAAE,YAAY,EAAoB,MAAM,gCAAgC,CAAA;AAoB5H,cAAM,QAAS,SAAQ,cAAc;gBACrB,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,YAAiB;CAUpD;AAED,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAA"}
package/dist/compat.js CHANGED
@@ -1,8 +1,31 @@
1
1
  import { DatabaseCompat, SqliteError } from "@tursodatabase/database-common";
2
- import { Database as NativeDB } from "#index";
2
+ import { Database as NativeDB, EncryptionCipher as NativeEncryptionCipher } from "#index";
3
+ // Map string cipher names to native enum values (lazy to avoid errors if native module lacks encryption)
4
+ function getCipherValue(cipher) {
5
+ if (!NativeEncryptionCipher) {
6
+ throw new Error('Encryption is not supported in this build');
7
+ }
8
+ const cipherMap = {
9
+ 'aes128gcm': NativeEncryptionCipher.Aes128Gcm,
10
+ 'aes256gcm': NativeEncryptionCipher.Aes256Gcm,
11
+ 'aegis256': NativeEncryptionCipher.Aegis256,
12
+ 'aegis256x2': NativeEncryptionCipher.Aegis256x2,
13
+ 'aegis128l': NativeEncryptionCipher.Aegis128l,
14
+ 'aegis128x2': NativeEncryptionCipher.Aegis128x2,
15
+ 'aegis128x4': NativeEncryptionCipher.Aegis128x4,
16
+ };
17
+ return cipherMap[cipher];
18
+ }
3
19
  class Database extends DatabaseCompat {
4
20
  constructor(path, opts = {}) {
5
- super(new NativeDB(path, opts));
21
+ const nativeOpts = { ...opts };
22
+ if (opts.encryption) {
23
+ nativeOpts.encryption = {
24
+ cipher: getCipherValue(opts.encryption.cipher),
25
+ hexkey: opts.encryption.hexkey,
26
+ };
27
+ }
28
+ super(new NativeDB(path, nativeOpts));
6
29
  }
7
30
  }
8
31
  export { Database, SqliteError };
@@ -102,3 +102,39 @@ test('blobs', () => {
102
102
  const rows = db.prepare("SELECT x'1020' as x").all();
103
103
  expect(rows).toEqual([{ x: Buffer.from([16, 32]) }]);
104
104
  });
105
+ test('encryption', () => {
106
+ const path = `test-encryption-${(Math.random() * 10000) | 0}.db`;
107
+ const hexkey = 'b1bbfda4f589dc9daaf004fe21111e00dc00c98237102f5c7002a5669fc76327';
108
+ const wrongKey = 'aaaaaaa4f589dc9daaf004fe21111e00dc00c98237102f5c7002a5669fc76327';
109
+ try {
110
+ const db = new Database(path, {
111
+ encryption: { cipher: 'aegis256', hexkey }
112
+ });
113
+ db.exec("CREATE TABLE t(x)");
114
+ db.exec("INSERT INTO t SELECT 'secret' FROM generate_series(1, 1024)");
115
+ db.exec("PRAGMA wal_checkpoint(truncate)");
116
+ db.close();
117
+ // lets re-open with the same key
118
+ const db2 = new Database(path, {
119
+ encryption: { cipher: 'aegis256', hexkey }
120
+ });
121
+ const rows = db2.prepare("SELECT COUNT(*) as cnt FROM t").all();
122
+ expect(rows).toEqual([{ cnt: 1024 }]);
123
+ db2.close();
124
+ // opening with wrong key MUST fail
125
+ expect(() => {
126
+ const db3 = new Database(path, {
127
+ encryption: { cipher: 'aegis256', hexkey: wrongKey }
128
+ });
129
+ db3.prepare("SELECT * FROM t").all();
130
+ }).toThrow();
131
+ // opening without encryption MUST fail
132
+ expect(() => {
133
+ const db5 = new Database(path);
134
+ db5.prepare("SELECT * FROM t").all();
135
+ }).toThrow();
136
+ }
137
+ finally {
138
+ unlinkSync(path);
139
+ }
140
+ });
package/index.js CHANGED
@@ -508,7 +508,8 @@ if (!nativeBinding) {
508
508
  throw new Error(`Failed to load native binding`)
509
509
  }
510
510
 
511
- const { BatchExecutor, Database, Statement } = nativeBinding
511
+ const { BatchExecutor, Database, Statement, EncryptionCipher } = nativeBinding
512
512
  export { BatchExecutor }
513
513
  export { Database }
514
514
  export { Statement }
515
+ export { EncryptionCipher }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tursodatabase/database",
3
- "version": "0.5.0-pre.3",
3
+ "version": "0.5.0-pre.4",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/tursodatabase/turso"
@@ -29,7 +29,7 @@
29
29
  "vitest": "^3.2.4"
30
30
  },
31
31
  "scripts": {
32
- "napi-build": "napi build --platform --release --esm --manifest-path ../../Cargo.toml --output-dir .",
32
+ "napi-build": "napi build --platform --profile release-official --esm --manifest-path ../../Cargo.toml --output-dir .",
33
33
  "napi-dirs": "napi create-npm-dirs",
34
34
  "napi-artifacts": "napi artifacts --output-dir .",
35
35
  "tsc-build": "npm exec tsc",
@@ -47,15 +47,15 @@
47
47
  ]
48
48
  },
49
49
  "dependencies": {
50
- "@tursodatabase/database-common": "^0.5.0-pre.3"
50
+ "@tursodatabase/database-common": "^0.5.0-pre.4"
51
51
  },
52
52
  "imports": {
53
53
  "#index": "./index.js"
54
54
  },
55
55
  "optionalDependencies": {
56
- "@tursodatabase/database-linux-x64-gnu": "0.5.0-pre.3",
57
- "@tursodatabase/database-win32-x64-msvc": "0.5.0-pre.3",
58
- "@tursodatabase/database-darwin-arm64": "0.5.0-pre.3",
59
- "@tursodatabase/database-linux-arm64-gnu": "0.5.0-pre.3"
56
+ "@tursodatabase/database-linux-x64-gnu": "0.5.0-pre.4",
57
+ "@tursodatabase/database-win32-x64-msvc": "0.5.0-pre.4",
58
+ "@tursodatabase/database-darwin-arm64": "0.5.0-pre.4",
59
+ "@tursodatabase/database-linux-arm64-gnu": "0.5.0-pre.4"
60
60
  }
61
61
  }