@@ -2,7 +2,7 @@ import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnCon
2
2
import type { ColumnBaseConfig } from '~/column.ts' ;
3
3
import { entityKind } from '~/entity.ts' ;
4
4
import type { AnySQLiteTable } from '~/sqlite-core/table.ts' ;
5
- import { type Equal , getColumnNameAndConfig } from '~/utils.ts' ;
5
+ import { type Equal , getColumnNameAndConfig , textDecoder } from '~/utils.ts' ;
6
6
import { SQLiteColumn , SQLiteColumnBuilder } from './common.ts' ;
7
7
8
8
type BlobMode = 'buffer' | 'json' | 'bigint' ;
@@ -41,18 +41,19 @@ export class SQLiteBigInt<T extends ColumnBaseConfig<'bigint', 'SQLiteBigInt'>>
41
41
}
42
42
43
43
override mapFromDriverValue ( value : Buffer | Uint8Array | ArrayBuffer ) : bigint {
44
- if ( Buffer . isBuffer ( value ) ) {
45
- return BigInt ( value . toString ( ) ) ;
46
- }
47
-
48
- // for sqlite durable objects
49
- // eslint-disable-next-line no-instanceof/no-instanceof
50
- if ( value instanceof ArrayBuffer ) {
51
- const decoder = new TextDecoder ( ) ;
52
- return BigInt ( decoder . decode ( value ) ) ;
44
+ if ( typeof Buffer !== 'undefined' && Buffer . from ) {
45
+ const buf = Buffer . isBuffer ( value )
46
+ ? value
47
+ // eslint-disable-next-line no-instanceof/no-instanceof
48
+ : value instanceof ArrayBuffer
49
+ ? Buffer . from ( value )
50
+ : value . buffer
51
+ ? Buffer . from ( value . buffer , value . byteOffset , value . byteLength )
52
+ : Buffer . from ( value ) ;
53
+ return BigInt ( buf . toString ( 'utf8' ) ) ;
53
54
}
54
55
55
- return BigInt ( String . fromCodePoint ( ... value ) ) ;
56
+ return BigInt ( textDecoder ! . decode ( value ) ) ;
56
57
}
57
58
58
59
override mapToDriverValue ( value : bigint ) : Buffer {
@@ -97,18 +98,19 @@ export class SQLiteBlobJson<T extends ColumnBaseConfig<'json', 'SQLiteBlobJson'>
97
98
}
98
99
99
100
override mapFromDriverValue ( value : Buffer | Uint8Array | ArrayBuffer ) : T [ 'data' ] {
100
- if ( Buffer . isBuffer ( value ) ) {
101
- return JSON . parse ( value . toString ( ) ) ;
102
- }
103
-
104
- // for sqlite durable objects
105
- // eslint-disable-next-line no-instanceof/no-instanceof
106
- if ( value instanceof ArrayBuffer ) {
107
- const decoder = new TextDecoder ( ) ;
108
- return JSON . parse ( decoder . decode ( value ) ) ;
101
+ if ( typeof Buffer !== 'undefined' && Buffer . from ) {
102
+ const buf = Buffer . isBuffer ( value )
103
+ ? value
104
+ // eslint-disable-next-line no-instanceof/no-instanceof
105
+ : value instanceof ArrayBuffer
106
+ ? Buffer . from ( value )
107
+ : value . buffer
108
+ ? Buffer . from ( value . buffer , value . byteOffset , value . byteLength )
109
+ : Buffer . from ( value ) ;
110
+ return JSON . parse ( buf . toString ( 'utf8' ) ) ;
109
111
}
110
112
111
- return JSON . parse ( String . fromCodePoint ( ... value ) ) ;
113
+ return JSON . parse ( textDecoder ! . decode ( value ) ) ;
112
114
}
113
115
114
116
override mapToDriverValue ( value : T [ 'data' ] ) : Buffer {
0 commit comments