Skip to content

Commit

Permalink
Fix Encoding Errors (#45)
Browse files Browse the repository at this point in the history
* add iconv-lite custom text encoding

* split encoding classes into files

* fix decoder setting error handling

* clean up
  • Loading branch information
kent-williams authored Feb 3, 2025
1 parent 8ec1da0 commit d4652de
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 19 deletions.
18 changes: 9 additions & 9 deletions metro.config.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config')

const defaultConfig = getDefaultConfig(__dirname)
const { assetExts, sourceExts } = defaultConfig.resolver

/**
* Metro configuration
* * https://reactnative.dev/docs/metro
*
* @type {import('metro-config').MetroConfig}
*/
const config = {
transformer: {
babelTransformerPath: require.resolve('react-native-svg-transformer'),
},
resolver: {
assetExts: assetExts.filter((ext) => ext !== 'svg'),
sourceExts: [...sourceExts, 'svg'],
extraNodeModules: {
'iconv-lite': require.resolve('iconv-lite'),
'iconv-lite/encodings': require.resolve('iconv-lite/encodings'),
},
},
}

module.exports = mergeConfig(defaultConfig, config)
// Ensure iconv-lite and its encodings are included in the build
const mergedConfig = mergeConfig(defaultConfig, config)
mergedConfig.resolver.assetExts.push('json') // Include JSON files for encodings

module.exports = mergedConfig
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"geojson2h3": "^1.1.1",
"h3-js": "^4.1.0",
"i18next": "^19.8.3",
"iconv-lite": "^0.6.3",
"lodash": "^4.17.20",
"number-to-locale-string-polyfill": "^1.0.9",
"postinstall-postinstall": "^2.1.0",
Expand Down
14 changes: 14 additions & 0 deletions src/utils/TextDecoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import iconv from 'iconv-lite'

export default class TextDecoder {
constructor(encoding = 'utf-8') {
this.encoding = encoding
}

decode(inputBuffer) {
if (!iconv.encodingExists(this.encoding)) {
throw new RangeError(`Unknown encoding: ${this.encoding}`)
}
return iconv.decode(Buffer.from(inputBuffer), this.encoding)
}
}
14 changes: 14 additions & 0 deletions src/utils/TextEncoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import iconv from 'iconv-lite'

export default class TextEncoder {
constructor(encoding = 'utf-8') {
this.encoding = encoding
}

encode(str) {
if (!iconv.encodingExists(this.encoding)) {
throw new RangeError(`Unknown encoding: ${this.encoding}`)
}
return Buffer.from(iconv.encode(str, this.encoding))
}
}
34 changes: 25 additions & 9 deletions src/utils/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import { Platform } from 'react-native'
import iconv from 'iconv-lite'
import encodings from 'iconv-lite/encodings'
import buffer from 'buffer'
import TextEncoder from './TextEncoder'
import TextDecoder from './TextDecoder'

// noinspection JSConstantReassignment
global.document = {
addEventListener: () => {},
}
global.Buffer = global.Buffer || buffer.Buffer

if (Platform.OS === 'android') {
require('number-to-locale-string-polyfill')
}
// Force load encodings
iconv.encodings = encodings

global.Buffer = global.Buffer || buffer.Buffer
console.log(Object.getOwnPropertyDescriptor(global, 'TextDecoder'))

Check warning on line 12 in src/utils/polyfill.js

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement

if (typeof global.TextDecoder === 'undefined') {
global.TextEncoder = TextEncoder
global.TextDecoder = TextDecoder

// Define global.TextDecoder
Object.defineProperty(global, 'TextDecoder', {
value: global.TextDecoder,
writable: false,
configurable: false,
enumerable: true,
})
} else {
console.warn(

Check warning on line 26 in src/utils/polyfill.js

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
'TextDecoder is already defined and not configurable. Skipping override.',
)
}
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11410,6 +11410,7 @@ __metadata:
h3-js: "npm:^4.1.0"
husky: "npm:^4.3.0"
i18next: "npm:^19.8.3"
iconv-lite: "npm:^0.6.3"
jest: "npm:^29.6.3"
jest-circus: "npm:^27.0.4"
jest-expo: "npm:52.0.0-preview.1"
Expand Down Expand Up @@ -11673,7 +11674,7 @@ __metadata:
languageName: node
linkType: hard

"iconv-lite@npm:0.6.3, iconv-lite@npm:^0.6.2":
"iconv-lite@npm:0.6.3, iconv-lite@npm:^0.6.2, iconv-lite@npm:^0.6.3":
version: 0.6.3
resolution: "iconv-lite@npm:0.6.3"
dependencies:
Expand Down

0 comments on commit d4652de

Please sign in to comment.