ChartJS Financial candles on NodeJS - node.js

I’m trying to make Chartjs-chart-financial in NodeJS. There is no NPM package for this plugin so I have to install it manually.
I do:
Git clone https://github.com/chartjs/chartjs-chart-financial/
cd chartjs-chart-financial
npm install
gulp build
As result I get a file in /dist directory and copy it to my NodeJS project folder.
In my NodeJS app I do:
const Chart = require(‘chart.js’)
const ChartFinancial = require(‘./chartjs-chart-financial.js’)
When I run the app I get:
chartjs-chart-financial.js:227
globalOpts.elements.financial = {
TypeError: Cannot set property ‘financial’ of undefined
I have checked content of Chart.defaults and found that .elements is in ‘global’ so I changed
225 const globalOpts = Chart.defaults.global
316 const globalOpts$1 = Chart.defaults.global
401 const globalOpts$2 = Chart.defaults.global
And eventually Error
Chart.defaults.set(‘ohlc’ ... set is not a function
on line 446
So I changed them to:
446 Chart.defaults.ohlc.datasets.barPercentage = 1.0,
447 Chart.defaults.ohlc.datasets.categoryPercentage = 1.0
Then I got
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'data' of undefined
at CandlestickController.draw (C:\inetpub\github\telegram-stream-trades\chartjs-chart-financial.js:213:32)
So I chagned:
213 const rects = me.getMeta();
That handled all the errors. But still no candles drawn.
So probably the way I initially started is incorrect

Related

fs.existsSync is not a function in Nuxt JS + Lowdb

What I need:
Read a local JSON file inside NuxtJS as the page loads. So I can parse it into a prop within <option> tag.
What I have:
Lowdb (installed as dependency — to read the JSON file) inside a component, with this code:
computed: {
resultFetchCamera: function() {
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('db.json');
const db = low(adapter);
let value = db.get('Size').map('Name').value();
return value;
}
}
}
I got an error This dependency was not found: * fs in ./node_modules/lowdb/adapters/FileSync.js. Fixed with this solution. Which leads me to another error: TypeError: fs.existsSync is not a function. This solution helps out a bit but it also leads to other errors: TypeError: window.require is not a function and TypeError: FileSync is not a constructor. So, I undo the last solution and get back with the fs.existsSync error.
The question:
How to fix the fs.existsSync error (in a NuxtJS environment)?
Did I implement Lowdb to NuxtJS correctly?

Can't use crypto in NPM dependency in Electron app

I'm building an Electron app (with Ionic) to digitally sign PDFs with p12 certificates using node-signpdf (https://www.npmjs.com/package/node-signpdf). I've had problems which I solved by using require ('electron').remote for example for fs.
I've also installed the same node version in my OS (MacOS Catalina) as the same one that electron is using (Node v12.4.0).
The problem is that one of the NPM dependencies uses crypto and it shows as undefined with the next error:
HomePage.html:39 ERROR TypeError: _crypto.randomBytes is not a function
at Object.ctx.seedFileSync (prng.js:340)
at _reseedSync (prng.js:210)
at Object.ctx.generateSync (prng.js:163)
at Object.ctx.generate (prng.js:80)
at Object.ctx.getBytes (random.js:92)
at _modPow (rsa.js:431)
at Object.push../node_modules/node-forge/lib/rsa.js.pki.rsa.encrypt (rsa.js:501)
at Object.key.sign (rsa.js:1245)
at addSignerInfos (pkcs7.js:534)
at Object.sign (pkcs7.js:377)
What I can see is that node-signpdf uses node-forge as a dependency, and node-forge loads crypto inside prng.js this way:
var _crypto = null;
if(forge.util.isNodejs && !forge.options.usePureJavaScript &&
!process.versions['node-webkit']) {
_crypto = require('crypto');
}
I've tried changing that part of the code to use crypto-js or browserfy-crypto (this last one doesn't even build and hasn't been updated in years), but I keep getting the error shown above.
EDIT 1:
This is how I'm implementing the signature in my service:
public signFile(pathToFile: string, pathToCert: string): void {
const fs = (<any>window).require('fs');
let certBuffer = fs.readFileSync(pathToCert);
let fileBuffer = fs.readFileSync(pathToFile);
fileBuffer = plainAddPlaceholder({
pdfBuffer: fileBuffer,
reason: 'I have reviewed it.',
signatureLength: 1612,
});
const signedPdf = signer.sign(fileBuffer, certBuffer, {passphrase: 'qwertyui'});
}
The code adds the placeholder to add the signature, the problem comes in signer.sign
EDIT 2:
When I run the app if I type in console: require ('crypto') I see the methods, so it looks like it's loaded in the global scope, the problem seems to be in the NPM dependency of node-forge.
EDIT 3:
I've changed require('crypto') to window.require('crypto') and started working. But I think I'll have to make postinstall script to modify it.
Is there a better way?
-
How can I make this crypto thing work? I ran out of ideas. Maybe you can spare some?
Thanks for your time!

Error: Cannot find module 'togeojson'

I am attempting to use this module in node.js and am running into an "Error: Cannot find module 'togeojson'" error when I attempt to use the documented example code:
// using togeojson in nodejs
var tj = require('togeojson'),
fs = require('fs'),
// node doesn't have xml parsing or a dom. use xmldom
DOMParser = require('xmldom').DOMParser;
var kml = new DOMParser().parseFromString(fs.readFileSync('foo.kml', 'utf8'));
var converted = tj.kml(kml);
var convertedWithStyles = tj.kml(kml, { styles: true });
I ran npm init in the same directory that my app.js file (where the above code resides) is stored and I used the --save flag when installing the #mapbox/togeojson package to my application.
I am running node version 8.11.2 and npm v 6.1.0.
How do I go about debugging an issue like this in node/npm?
It is #mapbox/togeojson package, not togeojson, so it should be required like:
var tj = require('#mapbox/togeojson');

Waiting on 1 test

I have the following test
// tests/CheckboxWithLabel-test.js
jest.dontMock('../test.js');
describe('CheckboxWithLabel', function() {
it('changes the text after click', function() {
var React = require('react/addons');
var CheckboxWithLabel = require('../test.js');
var TestUtils = React.addons.TestUtils;
// Render a checkbox with label in the document
var checkbox = TestUtils.renderIntoDocument(
<CheckboxWithLabel labelOn="On" labelOff="Off" />
);
// Verify that it's Off by default
var label = TestUtils.findRenderedDOMComponentWithTag(
checkbox, 'label');
expect(label.getDOMNode().textContent).toEqual('Off');
// Simulate a click and verify that it is now On
var input = TestUtils.findRenderedDOMComponentWithTag(
checkbox, 'input');
TestUtils.Simulate.change(input);
expect(label.getDOMNode().textContent).toEqual('On');
});
});
when I try to run it though using jest, I get the following error: Waiting on 1 test
I run npm test
What should I do?
You did not specify the version of Node you were using. Your problem could be because you are using a newer version of Node and Jest works with version 0.10.0. This is an open issue on Github (https://github.com/facebook/jest/issues/243)
To downgrade your version of node, use the n package as follows:
npm install -g n # Install n globally
n 0.10 # Installing the correct version of node
This can result in some weird problems with your current packages, so delete your node_modules folder and perform a clean install.

websocket-server replacement in BrowserQuest

I am trying to get Mozilla's BrowserQuest up and running and have run into a problem. It depends on the websocket-server node package, and unfortunately that has been deprecated and removed from the npm library.
I have added the ws and node-websocket-server packages, however neither of them seem to be an easy replacement for the existing calls to websocket-server.
One prior SO post I saw said to replace websocket-server with node-websocket-server in the package.json file, however there are existing direct calls to websocket-server in the BrowserQuest codebase.
I also tried updating the server/js/ws.js file directly as follows (added node- to the websocket-server fields):
var cls = require("./lib/class"),
url = require('url'),
wsserver = require("node-websocket-server"),
miksagoConnection = require('node-websocket-server/lib/ws/connection'),
worlizeRequest = require('websocket').request,
http = require('http'),
Utils = require('./utils'),
_ = require('underscore'),
BISON = require('bison'),
WS = {},
useBison = false;
which leads to the following error:
/homenode/browserquest/BrowserQuest/server/js/map.js:15
path.exists(filepath, function(exists) {
^
TypeError: undefined is not a function
at module.exports.cls.Class.extend.init (/home/node/browserquest/BrowserQuest/server/js/map.js:15:11)
at Class (/home/node/browserquest/BrowserQuest/server/js/lib/class.js:50:23)
at module.exports.cls.Class.extend.run (/home/node/browserquest/BrowserQuest/server/js/worldserver.js:151:20)
at /homenode/browserquest/BrowserQuest/server/js/main.js:79:15
at Function._.each._.forEach (/home/node/browserquest/BrowserQuest/node_modules/underscore/underscore.js:153:9)
at main (/home/node/browserquest/BrowserQuest/server/js/main.js:77:7)
at /home/node/browserquest/BrowserQuest/server/js/main.js:134:13
at /home/node/browserquest/BrowserQuest/server/js/main.js:117:13
at fs.js:334:14
at FSReqWrap.oncomplete (fs.js:95:15)
This is updated! I also got it to work today!!
https://github.com/nenuadrian/BrowserQuest

Resources