forked from helium/maker-starter-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add iconv-lite custom text encoding * split encoding classes into files * fix decoder setting error handling * clean up
- Loading branch information
1 parent
8ec1da0
commit d4652de
Showing
6 changed files
with
65 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) | ||
|
||
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( | ||
'TextDecoder is already defined and not configurable. Skipping override.', | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters