@@ -2,7 +2,7 @@ import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnCon
22import type { ColumnBaseConfig } from '~/column.ts' ;
33import { entityKind } from '~/entity.ts' ;
44import type { AnySQLiteTable } from '~/sqlite-core/table.ts' ;
5- import { type Equal , getColumnNameAndConfig } from '~/utils.ts' ;
5+ import { type Equal , getColumnNameAndConfig , textDecoder } from '~/utils.ts' ;
66import { SQLiteColumn , SQLiteColumnBuilder } from './common.ts' ;
77
88type BlobMode = 'buffer' | 'json' | 'bigint' ;
@@ -41,18 +41,19 @@ export class SQLiteBigInt<T extends ColumnBaseConfig<'bigint', 'SQLiteBigInt'>>
4141 }
4242
4343 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' ) ) ;
5354 }
5455
55- return BigInt ( String . fromCodePoint ( ... value ) ) ;
56+ return BigInt ( textDecoder ! . decode ( value ) ) ;
5657 }
5758
5859 override mapToDriverValue ( value : bigint ) : Buffer {
@@ -97,18 +98,19 @@ export class SQLiteBlobJson<T extends ColumnBaseConfig<'json', 'SQLiteBlobJson'>
9798 }
9899
99100 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' ) ) ;
109111 }
110112
111- return JSON . parse ( String . fromCodePoint ( ... value ) ) ;
113+ return JSON . parse ( textDecoder ! . decode ( value ) ) ;
112114 }
113115
114116 override mapToDriverValue ( value : T [ 'data' ] ) : Buffer {
0 commit comments