Node.js : bmfont2json is not a function? - javascript

I understand that this question has been asked countless times already but I cannot find the what is wrong in my case. Below is the code:
var fs = require('fs');
var bmfont2json = require('bmfont2json');
var data = fs.readFileSync('Bitmap Font' + '/Roboto.fnt');
var obj = bmfont2json(data);
var json = JSON.stringify( obj );
This results in the error bmfont2jsonis not a function. Does anyone know the error in the above? Thank you.

As Bulkan and Maynank have already mentioned, you should not include .js at the end of the name of an NPM core module:
var bmfont2json = require('bmfont2json');
However, what you're really doing wrong is this:
var obj = bmfont2json(data);
Because this function is from the bmfont2json module, the correct code you be as follows:
var obj = bmfont2json.bmfont2json(data);
Notice that you are doing this the same way you did var data = fs.readFileSync(blah blah blah);. Since readFileSync() is a function in the fs module, you used fs.readFileSync() instead of just readFileSync(). This is how you use functions from any module in Node.js.
Therefore, the correct code is as follows:
var fs = require('fs');
var bmfont2json = require('bmfont2json.js');
var data = fs.readFileSync('Bitmap Font' + '/Roboto.fnt');
var obj = bmfont2json(data);
var json = JSON.stingify( obj );

I tried your code on my machine, I did some changes (var data = fs.readFileSync(__dirname+'/Bitmap Font' + '/Roboto.fnt');) in your code.
Please try below code:
var fs = require('fs');
var bmfont2json = require('bmfont2json');
var data = fs.readFileSync(__dirname+'/Bitmap Font' + '/Roboto.fnt');
var obj = bmfont2json(data);
var json = JSON.stringify( obj );
console.log(json);
example.fnt:
info face="Arial" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1 outline=0
common lineHeight=32 base=26 scaleW=256 scaleH=256 pages=0 packed=0 alphaChnl=1 redChnl=0 greenChnl=0 blueChnl=0
chars count=0
When you will replace your .fnt file with example the output will be:
{ "pages":[],
"chars":[],
"kernings":[],
"info":"face":"Arial",
"size":32,
"bold":0,
"italic":0,
"charset":"",
"unicode":1,
"stretchH":100,
"smooth":1,
"aa":1,
"padding":[0,0,0,0],
"spacing":[1,1],
"outline":0},
"common":{
"lineHeight":32,
"base":26,
"scaleW":256,
"scaleH":256,
"pages":0,
"packed":0,
"alphaChnl":1,
"redChnl":0,
"greenChnl":0,
"blueChnl":0
}
}
Try above code and then let me know.

Try importing bmfont2json as follows;
var bmfont2json = require('bmfont2json');

I think the require statement that you have written is wrong var bmfont2json = require('bmfont2json.js');
The correct way is to not include .js like this var bmfont2json = require('bmfont2json');

var bmfont2json = require('bmfont2json');
as i believe you are using bmfont2json npm.
For more, you can refer to:
https://www.npmjs.com/package/bmfont2json

Related

Nodejs require file depends on each other

Is it normal to have require for each other in both files? I have a requirement where AWS.js is managed in a separate file but AWS.js needs variable from index.js and index.js imports AWS.js.
As you see in the below example, I might have to include require for each other. Any solution to this?
index.js:
I can't move these variables to AWS.js because the event.Records[0].Sns.Message belongs to a function in index.js
var AWS = require('./AWS.js');
var sns = JSON.parse(event.Records[0].Sns.Message);
var sns_MetricName = sns.Trigger.MetricName;
var sns_NameSpace = sns.Trigger.Namespace;
AWS.js:
if(sns_NameSpace == "AWS/S3") {
keyFilter = ["BucketName", "StorageType"]
}
Workaround is to have both require each other. Any solution to this? Is this normal approach?
index.js:
var AWS = require('./AWS.js');
var sns = JSON.parse(event.Records[0].Sns.Message);
var sns_MetricName = sns.Trigger.MetricName;
var sns_NameSpace = sns.Trigger.Namespace;
exports.sns = sns;
exports.sns_MetricName = sns_MetricName;
exports.sns_NameSpace = sns_NameSpace;
AWS.js:
var index = require('./index.js');
if(index.sns_NameSpace == "AWS/S3") {
keyFilter = ["BucketName", "StorageType"]
}
Inject your dependencies. If AWS requires sns_NameSpace give it sns_NameSpace, don't import index:
AWS.js:
var keyFilter;
function init (sns_NameSpace) {
if(sns_NameSpace == "AWS/S3") {
keyFilter = ["BucketName", "StorageType"]
}
}
exports.init = init;
exports.keyFilter = keyFilter;
index.js:
var AWS = require('./AWS.js');
var sns = JSON.parse(event.Records[0].Sns.Message);
var sns_MetricName = sns.Trigger.MetricName;
var sns_NameSpace = sns.Trigger.Namespace;
AWS.init(sns_NameSpace); // <------- Pass the value HERE!!
console.log(AWS.keyFilter); // <---- Prints ["BucketName","StorageType"]
exports.sns = sns;
exports.sns_MetricName = sns_MetricName;
exports.sns_NameSpace = sns_NameSpace;

Put variable name in JSON Array (fetched by an api)

I am very new to Javascript but I will try to put this in convenient way. I am having this api where I am fetching the rank of a crypto (Ripple; currently ranked 7 and is subject to change overtime ), code below:
function myFunction() {
var url = "https://api.coinpaprika.com/v1/coins/xrp-xrp";
var XRPresponse = UrlFetchApp.fetch(url);
var XRPjson = XRPresponse.getContentText();
var XRPdata = JSON.parse(XRPjson);
var XRPrank = XRPdata.rank;
}
Now this is another function for an api where I extract other infos (having 5000+ crytos listed, including ripple)
function myXRP() {
var url = "https://api.coinpaprika.com/v1/tickers";
var response = UrlFetchApp.fetch(url);
var json = response.getContentText();
var data = JSON.parse(json);
var XRP = data[7].symbol;
// Here instead of [7], I need to put the value extracted from XRPrank above so that whenever the rank is changed I get the latest value on data.[].
If someone could please advise.
In JavaScript there are several ways to achieve what you are looking for. The following is an adaptation of your current code with what I think are the minimal changes that you have to do, 1. use return followed by XRPrank 2. Call myFunction from myXRP and replace the data index by XRPrank.
function myFunction() {
var url = "https://api.coinpaprika.com/v1/coins/xrp-xrp";
var XRPresponse = UrlFetchApp.fetch(url);
var XRPjson = XRPresponse.getContentText();
var XRPdata = JSON.parse(XRPjson);
var XRPrank = XRPdata.rank;
return XRPrank; // add this
}
function myXRP() {
var url = "https://api.coinpaprika.com/v1/tickers";
var response = UrlFetchApp.fetch(url);
var json = response.getContentText();
var data = JSON.parse(json);
var XRPrank = myFunction(); // add this
// var XRP = data[7].symbol; instead of this
var XRP = data[XRPrank].symbol; // use this
}
Resources
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

ASN1 Object's schema was not verified against input data for CERT

Hi I've adapted the HTML certificate parser code to use nodejs from here:
https://github.com/GlobalSign/PKI.js/blob/master/examples/certificate-decode-example.html
However, I keep getting Error: Object's schema was not verified against input data for CERT
Obviously, theres a schema verification issue thats seems to be specific to node JS.
Am I missing something here ?
var merge = require("node.extend");
var common = require("asn1js/org/pkijs/common");
var _asn1js = require("asn1js");
var _pkijs = require("pkijs");
var _x509schema = require("pkijs/org/pkijs/x509_schema");
// #region Merging function/object declarations for ASN1js and PKIjs
var asn1js = merge(true, _asn1js, common);
var x509schema = merge(true, _x509schema, asn1js);
var pkijs_1 = merge(true, _pkijs, asn1js);
var pkijs = merge(true, pkijs_1, x509schema);
certb = `
MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG
A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv
b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAw
MDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i
YWxTaWduIG52LXNhMRAwDgYDVQQLEwdSb290IENBMRswGQYDVQQDExJHbG9iYWxT
aWduIFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDaDuaZ
jc6j40+Kfvvxi4Mla+pIH/EqsLmVEQS98GPR4mdmzxzdzxtIK+6NiY6arymAZavp
xy0Sy6scTHAHoT0KMM0VjU/43dSMUBUc71DuxC73/OlS8pF94G3VNTCOXkNz8kHp
1Wrjsok6Vjk4bwY8iGlbKk3Fp1S4bInMm/k8yuX9ifUSPJJ4ltbcdG6TRGHRjcdG
snUOhugZitVtbNV4FpWi6cgKOOvyJBNPc1STE4U6G7weNLWLBYy5d4ux2x8gkasJ
U26Qzns3dLlwR5EiUWMWea6xrkEmCMgZK9FGqkjWZCrXgzT/LCrBbBlDSgeF59N8
9iFo7+ryUp9/k5DPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8E
BTADAQH/MB0GA1UdDgQWBBRge2YaRQ2XyolQL30EzTSo//z9SzANBgkqhkiG9w0B
AQUFAAOCAQEA1nPnfE920I2/7LqivjTFKDK1fPxsnCwrvQmeU79rXqoRSLblCKOz
yj1hTdNGCbM+w6DjY1Ub8rrvrTnhQ7k4o+YviiY776BQVvnGCv04zcQLcFGUl5gE
38NflNUVyRRBnMRddWQVDf9VMOyGj/8N7yy5Y0b2qvzfvGn9LhJIZJrglfCm7ymP
AbEVtQwdpf5pLGkkeB6zpxxxYu7KyJesF12KwvhHhm4qxFYxldBniYUr+WymXUad
DKqC5JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveCX4XSQRjbgbME
HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==
`;
var asn1 = pkijs.org.pkijs.fromBER(certb);
var cert_simpl = new pkijs.org.pkijs.simpl.CERT({ schema: asn1.result });

.setText() not working

I have a function which by design should:
Create new directory
Copy docs from specified dir to newly created one
Set text to that copied docs
And I implemented it in such way:
function copyDocsAndSetText(fromDirId, toDirId) {
var toDir = DriveApp.openFolderById(toDirId);
var cwd = toDir.createFolder("New folder");
var fromDir = DriveApp.openFolderById(fromDirId);
var originalFiles = fromDir.getFiles();
while (originalFiles.hasNext()) {
var file = originalFiles.next();
file.makeCopy(cwd);
}
var copiedFiles = cwd.getFiles();
while (copiedFiles.hasNext()) {
var file = copiedFiles.next();
var doc = DocumentApp.openById(file.getId());
doc.getBody().setText("It works!");
}
}
I should see "It works!" as a content of each file inside newly created directory but I don't. What I'm doing wrong? Or it is some kind of bug?
You have done a small mistake, replace openFolderById with getFolderById
var toDir = DriveApp.getFolderById(toDirId);
var fromDir = DriveApp.getFolderById(fromDirId);

Node.js - how to use external library (VersionOne JS SDK)?

I'm trying to use VersionOne JS SDK in Node.js (https://github.com/versionone/VersionOne.SDK.JavaScript). I'm simply downloading whole library, placing it alongside with my js file:
var v1 = require('./v1sdk/v1sdk.js');
var V1Server = v1.V1Server;
console.log(v1);
console.log(V1Server);
Unfortunately something seems wrong, the output I get after calling
node app.js
is:
{}
undefined
Can somebody point me what I'm doing wrong or check whether the sdk is valid.
Thanks!
You can see in the source where V1Server is defined, that it's a class with a constructor. So you need to use the new keyword and pass the arguments for your environment.
https://github.com/versionone/VersionOne.SDK.JavaScript/blob/master/client.coffee#L37
var server = new V1Server('cloud'); //and more if you need
Can you try the sample.js script that I just updated from here:
https://github.com/versionone/VersionOne.SDK.JavaScript/blob/master/sample.js
It pulls in the two modules like this:
var V1Meta = require('./v1meta').V1Meta;
var V1Server = require('./client').V1Server;
var hostname = "www14.v1host.com";
var instance = "v1sdktesting";
var username = "api";
var password = "api";
var port = "443";
var protocol = "https";
var server = new V1Server(hostname, instance, username, password, port, protocol);
var v1 = new V1Meta(server);
v1.query({
from: "Member",
where: {
IsSelf: 'true'
},
select: ['Email', 'Username', 'ID'],
success: function(result) {
console.log(result.Email);
console.log(result.Username);
console.log(result.ID);
},
error: function(err) { // NOTE: this is not working correctly yet, not called...
console.log(err);
}
});
You might have to get the latest and build the JS from CoffeeScript.
I think I was trying out "browserify" last year and that's how the "v1sdk.js" file got generated. But I'm not sure if that's the best approach if you're using node. It's probably better just to do it the way the sample.js file is doing it.
However, I did also check in a change to v1sdk.coffee which property exports the two other modules, just as a convenience. With that, you can look at sample2.js. The only different part there is this, which is more like you were trying to do with your example:
var v1sdk = require('./v1sdk');
var hostname = "www14.v1host.com";
var instance = "v1sdktesting";
var username = "api";
var password = "api";
var port = "443";
var protocol = "https";
var server = new v1sdk.V1Server(hostname, instance, username, password, port, protocol);
var v1 = new v1sdk.V1Meta(server);

Categories

Resources