Skip to content

Commit 8d0ba85

Browse files
committed
Use browser compatible Uint8Array
1 parent 7ea9e2a commit 8d0ba85

File tree

5 files changed

+15
-26
lines changed

5 files changed

+15
-26
lines changed

__tests__/index.test.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import SqlString from 'sqlstring'
2-
import { cast, connect, format, hex, DatabaseError, type Cast } from '../dist/index'
2+
import { cast, connect, format, DatabaseError, type Cast } from '../dist/index'
33
import { fetch, MockAgent, setGlobalDispatcher } from 'undici'
44
import packageJSON from '../package.json'
55

@@ -610,12 +610,6 @@ describe('format', () => {
610610
})
611611
})
612612

613-
describe('hex', () => {
614-
test('exports hex function', () => {
615-
expect(hex('\0')).toEqual('0x00')
616-
})
617-
})
618-
619613
describe('cast', () => {
620614
test('casts int to number', () => {
621615
expect(cast({ name: 'test', type: 'INT8' }, '12')).toEqual(12)

__tests__/text.test.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { decode, hex } from '../src/text'
1+
import { decode } from '../src/text'
22

33
describe('text', () => {
44
describe('decode', () => {
@@ -22,14 +22,4 @@ describe('text', () => {
2222
expect(decode('\xF0\x9F\xA4\x94')).toEqual('🤔')
2323
})
2424
})
25-
26-
describe('hex', () => {
27-
test('encodes binary as hex', () => {
28-
expect(hex('\0\0')).toEqual('0x0000')
29-
})
30-
31-
test('encodes ascii as hex', () => {
32-
expect(hex('aa')).toEqual('0x6161')
33-
})
34-
})
3525
})

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { format } from './sanitization.js'
22
export { format } from './sanitization.js'
3-
export { hex } from './text.js'
4-
import { decode } from './text.js'
3+
import { decode, uint8array } from './text.js'
54
import { Version } from './version.js'
65

76
type Row<T extends ExecuteAs = 'object'> = T extends 'array' ? any[] : T extends 'object' ? Record<string, any> : never
@@ -410,7 +409,7 @@ export function cast(field: Field, value: string | null): any {
410409
return value
411410
case 'BLOB':
412411
case 'VARBINARY':
413-
return new Buffer(value, "binary");
412+
return uint8array(value)
414413
case 'JSON':
415414
return JSON.parse(decode(value))
416415
default:

src/sanitization.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { uint8ArrayToHex } from './text.js'
2+
13
type Stringable = { toString: () => string }
24
type Value = null | undefined | number | boolean | string | Array<Value> | Date | Stringable
35

@@ -47,8 +49,8 @@ function sanitize(value: Value): string {
4749
return quote(value.toISOString().slice(0, -1))
4850
}
4951

50-
if (value instanceof Buffer) {
51-
return `0x${value.toString('hex')}`
52+
if (value instanceof Uint8Array) {
53+
return uint8ArrayToHex(value)
5254
}
5355

5456
return quote(value.toString())

src/text.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
const decoder = new TextDecoder('utf-8')
22

33
export function decode(text: string | null | undefined): string {
4-
return text ? decoder.decode(Uint8Array.from(bytes(text))) : ''
4+
return text ? decoder.decode(uint8array(text)) : ''
55
}
66

7-
export function hex(text: string): string {
8-
const digits = bytes(text).map((b) => b.toString(16).padStart(2, '0'))
7+
export function uint8array(text: string): Uint8Array {
8+
return Uint8Array.from(bytes(text))
9+
}
10+
11+
export function uint8ArrayToHex(uint8: Uint8Array): string {
12+
const digits = Array.from(uint8).map((i) => i.toString(16).padStart(2, '0'))
913
return `0x${digits.join('')}`
1014
}
1115

0 commit comments

Comments
 (0)