@@ -39,13 +39,41 @@ function getErrorLineNumber(error) {
3939 return lineNumber ;
4040}
4141
42+ function sanitizeNullBytes ( obj ) {
43+ if ( typeof obj === 'string' ) {
44+ // Remove various forms of null bytes and control characters that cause Unicode escape sequence errors
45+ return obj
46+ . replace ( / \u0000 / g, '' ) // Remove null bytes
47+ . replace ( / \\ u 0 0 0 0 / g, '' ) // Remove escaped null bytes
48+ . replace ( / \x00 / g, '' ) // Remove hex null bytes
49+ . replace ( / [ \u0000 - \u001F \u007F - \u009F ] / g, '' ) // Remove all control characters
50+ . trim ( ) ; // Remove leading/trailing whitespace
51+ }
52+
53+ if ( Array . isArray ( obj ) ) {
54+ return obj . map ( sanitizeNullBytes ) ;
55+ }
56+
57+ if ( obj && typeof obj === 'object' ) {
58+ const sanitized = { } ;
59+ for ( const [ key , value ] of Object . entries ( obj ) ) {
60+ sanitized [ key ] = sanitizeNullBytes ( value ) ;
61+ }
62+ return sanitized ;
63+ }
64+
65+ return obj ;
66+ }
67+
4268class sync {
4369 async getExistingIDsforTable ( tablename ) {
4470 return await db . query ( `SELECT "Id" FROM ${ tablename } ` ) . then ( ( res ) => res . rows . map ( ( row ) => row . Id ) ) ;
4571 }
4672
4773 async insertData ( tablename , dataToInsert , column_mappings ) {
48- let result = await db . insertBulk ( tablename , dataToInsert , column_mappings ) ;
74+ const sanitizedData = sanitizeNullBytes ( dataToInsert ) ;
75+
76+ let result = await db . insertBulk ( tablename , sanitizedData , column_mappings ) ;
4977 if ( result . Result === "SUCCESS" ) {
5078 // syncTask.loggedData.push({ color: "dodgerblue", Message: dataToInsert.length + " Rows Inserted." });
5179 } else {
0 commit comments