Skip to content

Commit 53797a9

Browse files
committed
chore: inital commit
1 parent 0386f70 commit 53797a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+17165
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
*.tgz

CONTRIBUTING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Contribution guide
2+
3+
We'd love for you to contribute to our source code and to make it even better than it is today!
4+
5+
If You have a proposal for a new feature, or You found a bug, or You have a question, feel free to open an issue and/or pull request.

LICENSE

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 Mateusz Falkowski
3+
Copyright (c) 2024 Appidea.pl Mateusz Falkowski
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
@@ -19,3 +19,31 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.
22+
23+
""
24+
25+
The MIT License (MIT)
26+
27+
Copyright (c) 2020 Fred K. Schott
28+
29+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
30+
31+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
32+
33+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34+
35+
36+
"""
37+
38+
This license applies to parts of rollup-plugin-polyfill-node originating from the
39+
https://github.com/ionic-team/rollup-plugin-node-polyfills repository:
40+
41+
The MIT License (MIT)
42+
43+
Copyright (c) 2019 these people
44+
45+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
46+
47+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
48+
49+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## react-native-node-polyfill
2+
3+
This package is a polyfill of Node.js core modules for React Native (to use with Metro).
4+
5+
Important: It does not provide the compatibility layer with React Native library-based functions like [node-libs-react-native](https://github.com/parshap/node-libs-react-native), at least in this version.
6+
7+
The benefit of this package is that it is a simple polyfill for Node.js core modules, so there is zero dependency on any native module. Just pure javascript code to _make it work_.
8+
9+
All the polyfills were forked from [rollup-plugin-polyfill-node](https://github.com/FredKSchott/rollup-plugin-polyfill-node). Thank You, all contributors, for a great job.
10+
11+
## Installation
12+
13+
```sh
14+
npm install @appidea/react-native-node-polyfill
15+
```
16+
17+
## Usage
18+
19+
Modify Your `metro.config.js` file to include the following:
20+
21+
```javascript
22+
const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');
23+
const path = require('path');
24+
// import the polyfill
25+
const nodePolyfill = require('@appidea/react-native-node-polyfill');
26+
27+
/**
28+
* Metro configuration
29+
* https://facebook.github.io/metro/docs/configuration
30+
*
31+
* @type {import('metro-config').MetroConfig}
32+
*/
33+
const config = {};
34+
35+
module.exports = mergeConfig(getDefaultConfig(__dirname), nodePolyfill, config);
36+
/* ^ ADD THIS ^ */
37+
```
38+
39+
## Contributing
40+
41+
Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
42+
43+

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@appidea/react-native-node-polyfill",
3+
"version": "1.0.0",
4+
"description": "Polyfill of Node.js internal modules for React Native",
5+
"main": "src/metro/index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"files": [
10+
"src"
11+
],
12+
"keywords": [],
13+
"author": "Appidea.pl Mateusz Falkowski and contributors",
14+
"license": "MIT"
15+
}

src/metro/index.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const path = require('path');
2+
3+
const resolveLocation = baseLocation => path.resolve(__dirname, '../polyfills/' + baseLocation);
4+
const EMPTY_PATH = resolveLocation('empty.js');
5+
6+
const libs = new Map();
7+
8+
libs.set('process', resolveLocation('process-es6.js'));
9+
libs.set('global', resolveLocation('global.js'));
10+
libs.set('buffer', resolveLocation('buffer-es6.js'));
11+
libs.set('util', resolveLocation('util.js'));
12+
libs.set('sys', libs.get('util'));
13+
libs.set('events', resolveLocation('events.js'));
14+
libs.set('stream', resolveLocation('stream.js'));
15+
libs.set('path', resolveLocation('path.js'));
16+
libs.set('querystring', resolveLocation('querystring.js'));
17+
libs.set('punycode', resolveLocation('punycode.js'));
18+
libs.set('url', resolveLocation('url.js'));
19+
libs.set('string_decoder', resolveLocation('string-decoder.js'));
20+
libs.set('http', resolveLocation('http.js'));
21+
libs.set('https', resolveLocation('http.js'));
22+
libs.set('os', resolveLocation('os.js'));
23+
libs.set('assert', resolveLocation('assert.js'));
24+
libs.set('constants', resolveLocation('constants.js'));
25+
libs.set('_stream_duplex', resolveLocation('__readable-stream/duplex.js'));
26+
libs.set('_stream_passthrough', resolveLocation('__readable-stream/passthrough.js'));
27+
libs.set('_stream_readable', resolveLocation('__readable-stream/readable.js'));
28+
libs.set('_stream_writable', resolveLocation('__readable-stream/writable.js'));
29+
libs.set('_stream_transform', resolveLocation('__readable-stream/transform.js'));
30+
libs.set('_inherits', resolveLocation('inherits.js'));
31+
libs.set('_buffer_list', resolveLocation('__readable-stream/buffer-list.js'));
32+
libs.set('timers', resolveLocation('timers.js'));
33+
libs.set('console', resolveLocation('console.js'));
34+
libs.set('vm', resolveLocation('vm.js'));
35+
libs.set('zlib', resolveLocation('zlib.js'));
36+
libs.set('tty', resolveLocation('tty.js'));
37+
libs.set('domain', resolveLocation('domain.js'));
38+
39+
// TODO: Decide if we want to implement these or not
40+
// currently causing trouble in tests
41+
libs.set('fs', EMPTY_PATH);
42+
libs.set('crypto', EMPTY_PATH);
43+
// libs.set('fs', resolveLocation('browserify-fs.js'));
44+
// libs.set('crypto', resolveLocation('crypto-browserify.js'));
45+
46+
// TODO: No good polyfill exists yet
47+
libs.set('http2', EMPTY_PATH);
48+
49+
// not shimmed
50+
libs.set('dns', EMPTY_PATH);
51+
libs.set('dgram', EMPTY_PATH);
52+
libs.set('child_process', EMPTY_PATH);
53+
libs.set('cluster', EMPTY_PATH);
54+
libs.set('module', EMPTY_PATH);
55+
libs.set('net', EMPTY_PATH);
56+
libs.set('readline', EMPTY_PATH);
57+
libs.set('repl', EMPTY_PATH);
58+
libs.set('tls', EMPTY_PATH);
59+
libs.set('perf_hooks', EMPTY_PATH);
60+
61+
const nodePolyfill = (context, moduleName, platform) => {
62+
if (libs.has(moduleName)) {
63+
const lib = libs.get(moduleName);
64+
// Logic to resolve the module name to a file path...
65+
// NOTE: Throw an error if there is no resolution.
66+
return {
67+
filePath: lib,
68+
type: 'sourceFile',
69+
};
70+
}
71+
// Optionally, chain to the standard Metro resolver.
72+
return context.resolveRequest(context, moduleName, platform);
73+
}
74+
75+
module.exports = {
76+
resolver: {
77+
resolveRequest: nodePolyfill
78+
}
79+
};

0 commit comments

Comments
 (0)