React native crypto stream module is undefined - node.js

I'm giving a try with [react-native-crypto][1] in order to learn how to convert nodejs to be used in React Native project in the future. Unfortunately, I couldn't get it running successfully. I've faced an issue with stream is undefined. ERROR TypeError: undefined is not an object (evaluating '_$$_REQUIRE(_dependencyMap[0], "stream").Transform.call').
If you have ever faced a similar problem, I'm so grateful for your help.
Also, I attach the screenshot of the issue as the following

For anyone still trying to solve this issue, I have figured out a solution that worked for me. So within node_modules/cipher-base/index.js, the top of the file should have a line which defines the variable Transform as var Transform = require('stream').Transform. For some reason, it does not like the module stream and as such it needs to be changed to readable-stream. Therefore the variable Transform should now read var Transform = require('readable-stream').Transform.
From what I have gathered, the stream module it is trying to refer to isnt actually a module that can be used. The reason why it gets referenced however seems to be because the tsconfig.json file in the root directory specifies "stream": ["./node_modules/readable-stream"] as a path, almost as if to make stream refer to the readable-stream module, which in theory it should refer to when it is called. But in this case it doesnt happen so we need to explicitly define that we are refering to the readable-stream module.
Hope this helps anyone else out there and prevents others scratching their heads for hours on end like it did for me!

I have figured it out by editing in metro.config.js as the following:
resolver: {
extraNodeModules: {
stream: require.resolve('stream-browserify'),
}
},

Related

How to Mock non-existent file import in nodejs Lambda

I am facing issue in testing where My logger is in lambda layer thus non-existing for nodeJs import in lambda.js during mocha-chai testing. I tried mock-fs but getting errors Can not find module /opt/logger.js or maybe I am trying wrong way and not sure if it is useful in this way. Please check below code for reference. Any help or suggestion is most welcome.
lambda.js -
const logger = require('/opt/logger.js') // coming from lambda_layer.js
lambda.test.js -
mock({
"/opt/logger.js": console.log('hello')
});
Was able to accomplish this with mockery. Thanks for allowing me to put this here for everyone. Will update this with better code soon.
mockery.registerMock({'/test/': { destroyAllHumans: actionStub })
mockery.enabled();

socket.io node.js with beaglebone issues

I never normally post stuff here asking why something isn't working, but i've been struggling all day with this issue and hope that someone might be familiar with what's happening and could help.
I took an example from github on how to control GPIOs on the beaglebone black over wi-fi by using socket.io : github.com/lgxlogic/BoneScript-SocketIO
I'm not an expert with javascript but when I follow the exact instructions given and and run the HtmlLedDemo.js file, I get the following error below. My version of node is v.5.9.0 . I have tried updating socket.io and still the same problem. Many thanks in advance.
root#beaglebone:/var/lib/cloud9# node HtmlLedDemo.js
Option log level is not valid. Please refer to the README.
Option browser client minification is not valid. Please refer to the README.
Option browser client etag is not valid. Please refer to the README.
Server running on: http://192.168.0.87:8080
/var/lib/cloud9/node_modules/bonescript/src/my.js:245
callback(resp);
^
TypeError: callback is not a function
at onUnloadSlot (/var/lib/cloud9/node_modules/bonescript/src/my.js:245:13)
at unloadSlot (/var/lib/cloud9/node_modules/bonescript/src/my.js:235:13)
at onWriteSlots (/var/lib/cloud9/node_modules/bonescript/src/my.js:210:43)
at onReadSlots (/var/lib/cloud9/node_modules/bonescript/src/my.js:199:13)
at onFindCapeMgr (/var/lib/cloud9/node_modules/bonescript/src/my.js:174:9)
at Object.exports.load_dt (/var/lib/cloud9/node_modules/bonescript/src/my.js:157:5)
at onDTBOExists (/var/lib/cloud9/node_modules/bonescript/src/my.js:332:26)
at onDTBOExistsTest (/var/lib/cloud9/node_modules/bonescript/src/my.js:279:13)
at Object.exports.create_dt (/var/lib/cloud9/node_modules/bonescript/src/my.js:274:9)
at Object.exports.setPinMode (/var/lib/cloud9/node_modules/bonescript/src/hw_capemgr.js:102:12)
root#beaglebone:/var/lib/cloud9#
Your error message says "callback is not a function". If you have a look at the supplied call stack, you can see that on line 245 of your "my.js" file, you attempt to call a function by the variable name callback. What this error message tells you is that whatever this variable contains, it is not a function.

Encoding not recognized in jest.js

I have a problem testing a project using node-mysql2, react, sequelize and jest. This problem only occurs during testing.
Encoding not recognized: 'cesu8' (searched as: 'cesu8')
at Object.getCodec (project/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:106:23)
at Object.getDecoder (project/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:122:23)
at Object.<anonymous>.exports.decode (project/node_modules/mysql2/lib/parsers/string.js:9:23)
at Packet.Object.<anonymous>.Packet.readNullTerminatedString (project/node_modules/mysql2/lib/packets/packet.js:373:23)
at Function.Object.<anonymous>.Handshake.fromPacket (project/node_modules/mysql2/lib/packets/handshake.js:18:31)
at ClientHandshake.Object.<anonymous>.ClientHandshake.handshakeInit (project/node_modules/mysql2/lib/commands/client_handshake.js:98:38)
at ClientHandshake.Object.<anonymous>.Command.execute (project/node_modules/mysql2/lib/commands/command.js:40:20)
at Connection.Object.<anonymous>.Connection.handlePacket (project/node_modules/mysql2/lib/connection.js:515:28)
at PacketParser.onPacket (project/node_modules/mysql2/lib/connection.js:94:16)
at PacketParser.executeStart (project/node_modules/mysql2/lib/packet_parser.js:77:14)
at Socket.<anonymous> (project/node_modules/mysql2/lib/connection.js:102:29)
This is problem caused by mysql2 doing dynamic lazy require of encodings and Jest not being able to handle this. Have a look at few workarounds users suggested here:
add this snippet to setupTestFrameworkScriptFile
require('mysql2/node_modules/iconv-lite').encodingExists('foo');
or this somewhere early to your code:
import iconv from 'iconv-lite';
import encodings from 'iconv-lite/encodings';
iconv.encodings = encodings;
Really only need to add:
require('iconv-lite').encodingExists('foo')
to the top of whatever file you're testing such as factory.test.js
Not sure why this is unfortunately, but the above is better for copy / pasting than the chosen answer.
In my case, the encoding error appeared after a ReferenceError
The cause was a test calling a method that was making an async database call via mysql2 for which I had forgotten to await.
The solution for me was to simply add await in the right place.
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From models/alert.test.js.
at Object.getCodec (node_modules/mysql2/node_modules/iconv-lite/lib/index.js:63:27)
at Object.getDecoder (node_modules/mysql2/node_modules/iconv-lite/lib/index.js:125:23)
at Object.<anonymous>.exports.decode (node_modules/mysql2/lib/parsers/string.js:10:25)
at Packet.readNullTerminatedString (node_modules/mysql2/lib/packets/packet.js:412:25)
at Function.fromPacket (node_modules/mysql2/lib/packets/handshake.js:62:33) ^
...
Error: Encoding not recognized: 'cesu8' (searched as: 'cesu8')
at Object.getCodec (/Users/.../node_modules/mysql2/node_modules/iconv-lite/lib/index.js:104:23)
I met a similar issue with network node module. I made a workaround by mocking the network module and not using the iconv-lite. The following is my code.
jest.mock('network', () => ({
get_active_interface: cb => {
cb?.(undefined, { type: 'Wired' });
},
}));
As for this case, maybe you could try mock the module that made the call, some code like this.
jest.mock('mysql2',()=>({
someFuncThatCallsIcoveLite:(..._)=>{}
}))
require('../../node_modules/mysql2/node_modules/iconv-lite/lib').encodingExists('foo');
This worked for me on
Node 16.17.1
ts 4.8.4
jest 29.2.2

Topojson 'default' property is Undefined

I've been trying to debug this for the last hour or so to no avail. I'm attempting to use d3-world-maps, which has a dependency on topojson.
Inside the d3-world-maps package lies the issue:
var _topojson = require('topojson');
var _topojson2 = _interopRequireDefault(_topojson);
this.countries = _topojson['default'].feature(_assetsTopo_countriesJson2['default'], _assetsTopo_countriesJson2['default'].objects.countries);
It seems that _topojson['default'] is undefined, and I'm not quite familiar with this library or its dependencies enough to figure out why...
Would anyone be able to point me in the right direction? I've logged the object and I can see that the property default is undefined (the object exists) - however I'm not sure why d3-world-maps uses it if it doesn't exist... unless I've missed something here.
Edit:
Just looked at the function:
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { 'default': obj };
}
What exactly is this doing and why?
The issue appears to be an API change in topojson. As you can see here, d3-world-maps uses * for the topojson version, which means that it will just keep getting the latest version no matter what.
Try editing the package to use a specific, older version of topojson and find the version that it's expecting and submit a pull request to the package to fix it!

scene.add(object) creates an "Uncaught TypeError: Cannot read property 'length' of undefined"

Getting that error in my javascript console(in Chrome) with a collada object I'm attempting to add with a basic loader. It's specifically coming from the "scene.add( object )" chunk of it. Everything else seems to work just fine. The code for loading the object is as follows
var ltable;
var furnLoad = new THREE.ColladaLoader();
function addlt(){
furnLoad.load('../Models/Furniture/foldingLongTable.dae', function(collada){
ltable = collada.scene;
ltable.scale.x=ltable.scale.y=ltable.scale.z=1;
ltable.updateMatrix();
ltable.position.x=ltable.position.z=ltable.position.y=0;
});
scene.add( ltable );
}
This function is called during the init of a page that, otherwise, works just fine. That page can be found here(version without this table has the same URL except for a 4 instead of a 3 at the end), and the specific object here.
What would be the recommended way to get past this error?
RESOLVED. Collada loaders just hate being part of any function and won't work when in them. So the fix is to have them outside of functions and they work just fine.
The answer to this issue is due to the location of the scene.add() being outside of the callback function. It is being called before the callback fires, hence the undefined error.

Resources