@@ -11,35 +11,36 @@ function escapeElement(elementRepresentation) {
1111 return '"' + escaped + '"'
1212}
1313
14+ // Node.js v4 does not support those Buffer.from params
15+ const bufferFrom =
16+ Buffer . from ( new Uint8Array ( 1 ) . buffer , 0 , 0 ) . length === 0
17+ ? Buffer . from
18+ : ( arrayBuffer , byteOffset , length ) => Buffer . from ( arrayBuffer ) . slice ( byteOffset , byteOffset + length )
19+
1420// convert a JS array to a postgres array literal
1521// uses comma separator so won't work for types like box that use
1622// a different array separator.
1723function arrayString ( val ) {
1824 let result = '{'
1925 for ( let i = 0 ; i < val . length ; i ++ ) {
2026 if ( i > 0 ) {
21- result = result + ','
27+ result += ','
2228 }
23- if ( val [ i ] === null || typeof val [ i ] === 'undefined' ) {
24- result = result + 'NULL'
25- } else if ( Array . isArray ( val [ i ] ) ) {
26- result = result + arrayString ( val [ i ] )
27- } else if ( ArrayBuffer . isView ( val [ i ] ) ) {
28- let item = val [ i ]
29+ let item = val [ i ]
30+ if ( item == null ) {
31+ result += 'NULL'
32+ } else if ( Array . isArray ( item ) ) {
33+ result += arrayString ( item )
34+ } else if ( ArrayBuffer . isView ( item ) ) {
2935 if ( ! ( item instanceof Buffer ) ) {
30- const buf = Buffer . from ( item . buffer , item . byteOffset , item . byteLength )
31- if ( buf . length === item . byteLength ) {
32- item = buf
33- } else {
34- item = buf . slice ( item . byteOffset , item . byteOffset + item . byteLength )
35- }
36+ item = bufferFrom ( item . buffer , item . byteOffset , item . byteLength )
3637 }
3738 result += '\\\\x' + item . toString ( 'hex' )
3839 } else {
39- result += escapeElement ( prepareValue ( val [ i ] ) )
40+ result += escapeElement ( prepareValue ( item ) )
4041 }
4142 }
42- result = result + '}'
43+ result += '}'
4344 return result
4445}
4546
@@ -57,11 +58,7 @@ const prepareValue = function (val, seen) {
5758 return val
5859 }
5960 if ( ArrayBuffer . isView ( val ) ) {
60- const buf = Buffer . from ( val . buffer , val . byteOffset , val . byteLength )
61- if ( buf . length === val . byteLength ) {
62- return buf
63- }
64- return buf . slice ( val . byteOffset , val . byteOffset + val . byteLength ) // Node.js v4 does not support those Buffer.from params
61+ return bufferFrom ( val . buffer , val . byteOffset , val . byteLength )
6562 }
6663 if ( isDate ( val ) ) {
6764 if ( defaults . parseInputDatesAsUTC ) {
0 commit comments