var token = utils.generateToken(64);
I have a vague idea what it might be for but I wanted to know specifically just to be sure.
I didn't find anything in what's supposed to be the official doc
That function will generate a unique id for you, the function can written like this:
Reference here
var uid = require('uid2');
function generateToken(number) {
return uid(number);
}
you need package uid2 to run that function
npm install uid2
Read here to know how it work:
https://github.com/coreh/uid2/blob/master/index.js
Related
I'm using Zapier's code module to write some Nodejs to generate a UUID.
I know that I can use node's crypto "library" because I've used it in the past to hash some text (MD5) to send to an API endpoint. I also know that I can use the crypto function randomInt.
For example, the following will correctly without errors return a number in the stated range:
const crypto = require('crypto');
let num = crypto.randomInt(1, 7);
output = [{id: num}];
When I try to use the randomUUID function (documentation), like the following, I get the error "TypeError: crypto.randomUUID is not a function" from Zapier:
const crypto = require('crypto');
let uuid = crypto.randomUUID();
output = [{id: uuid}];
I almost gave up there, but tried one last weird thing:
const crypto = require('crypto');
let uuid = crypto.randomUUID; //changed this
output = [{id: uuid}];
Note that I removed the () and this didn't throw an error and returned something like:
LT6TvivKgpFu5Yk3OvQmti1Hq1aNy5ZM
That looks a lot like a proper UUID since it has the right number of characters, but not the expected hyphens like (for example) 88368f2a-d5db-47d8-a05f-534fab0a0045
So, two questions:
Why is Zapier saying that randomUUID() is not a function? Does it not support this or am I coding something weirdly wrong or not requiring something needed? Is there a different way to do this?
When I use randomUUID (no ()) is this actually a reliable UUID?
EDIT: more info from Zapier, they are saying that their logs show that the code step cannot find module 'uuid'
When I use randomUUID (no ()) is this actually a reliable UUID?
The value you're getting there is not a reliable UUID. UUIDs will contain only 0-9a-f characters, the example you have there doesn't match.
Sorry I can't help you with what's going wrong with the other part! You could maybe try something like this
let crypto;
try {
crypto = require('crypto');
} catch (err) {
console.log('crypto support is disabled!');
}
to make sure the crypto package is actually available and there's not some error being silently suppressed? You may also want to check the version, as randomUUID() appears to have been added in v14.17.0
Also nit but should probably be const uuid = crypto.randomUUID(); instead of let ;)
I'm studying deploying smart contract following the steps on this article.
I used absolute path for import instead of relative path because compiler was not able to look into import files in node_modules so it compiles
the truffle migrate seems good because when I input JCoinCrowdsale.deployed() it returns full info (I named JCoin for this example)
but when I input JCoinCrowdsale.deployed().then(inst => { crowdsale = inst }) , it returns undefined
Any clue on this?
You did it correctly! And I see that you assign the result of the promise JCoinCrowdsale.deployed() to the variable crowdsale.
The reason it shows undefined is due to the fact that this function inst => { crowdsale = inst } does not explicitly return anything.
If you type crowdsale on to the truffle console you will be able to see the same JavaScript object you get when you just type JCoinCrowdsale.deployed().
Hope it helps and wish you all the best in your learning :-)
Try below:
var crowdsale = JCoinCrowdsale.deployed().then(function(inst) { crowdsale = inst })
I try to switch from Grunt to Gulp and I have an issue:
I read two streams from two files
var fileStream = gulp.src(file);
var injectionStream = gulp.src(injection)
.pipe(replace('#class-name#', argv.cname));
If my console argument "--remove" is absent I have no problem to concatenate these streams
.pipe(concat('animation.styl'))
.pipe(gulp.dest('./dist'))
However when '--remove' is true I want to delete injection, in other words, subtract injectionStream from fileStream.
I tried:
var es = require('event-stream');
es.replace()
var replace = require('gulp-replace');
It works with strings, but I cannot succeed with streams read from files. Can anybody give me a small hint?
And maybe it is an incorrect tool for generation task and I should stay with Grunt and\or other tools like yo,etc?
Thank you for your time!
Nice work :) Looks like gulp-replace might be an easier way for folk coming here from google..
var replace = require('gulp-replace');
gulp.task('templates', function(){
gulp.src(['file.txt'])
.pipe(replace(/foo(.{3})/g, '$1foo'))
.pipe(gulp.dest('build/file.txt'));
});
I finally found the solution!
fs.readFile(injectionFile, 'utf8', function (err, injStr) {
injStr = injStr.replace(/svv/g, cname);
fileStream
.pipe(gulpif(rm!=true,injectString.append(injStr),replace(injStr,'')))
.pipe(concat(initialFile))
.pipe(gulp.dest(animation))
...
It took a while, but I AM HAPPY it's over.
Thank you
I want to use an yeoman generator inside a NodeJS project
I installed yeoman-generatorand generator-git (the generator that I want use) as locally dependency, and, at this moment my code is like this:
var env = require('yeoman-generator')();
var path = require('path');
var gitGenerator = require('generator-git');
var workingDirectory = path.join(process.cwd(), 'install_here/');
generator = env.create(gitGenerator);
obviously the last line doesn't work and doesn't generate the scaffold.
The question: How to?
Importantly, I want to stay in local dependency level!
#simon-boudrias's solution does work, but after I changed the process.chdir(), this.templatePath() and this.destinationPath() returns same path.
I could have use this.sourcePath() to tweak the template path, but having to change this to each yeoman generator is not so useful. After digging to yo-cli, I found the following works without affecting the path.
var env = require('yeoman-environment').createEnv();
env.lookup(function() {
env.run('generator-name');
});
env.create() only instantiate a generator - it doesn't run it.
To run it, you could call generator.run(). But that's not ideal.
The best way IMO would be this way:
var path = require('path');
var env = require('yeoman-generator')();
var gitGenerator = require('generator-git');
// Optionnal: look every generator in your system. That'll allow composition if needed:
// env.lookup();
env.registerStub(gitGenerator, 'git:app');
env.run('git:app');
If necessary, make sure to process.chdir() in the right directory before launching your generator.
Relevant documentation on the Yeoman Environment class can be found here: http://yeoman.io/environment/Environment.html
Also see: http://yeoman.io/authoring/integrating-yeoman.html
The yeoman-test module is also very useful if you want to pass predefined answers to your prompts. This worked for me.
var yeomanTest = require('yeoman-test');
var answers = require('from/some/file.json');
var context = yeomanTest.run(path.resolve('path/to/generator'));
context.settings.tmpdir = false; // don't run in tempdir
context.withGenerators([
'paths/to/subgenerators',
'more/of/them'
])
.withOptions({ // execute with options
'skip-install': true,
'skip-sdk': true
})
.withPrompts(answers) // answer prompts
.on('end', function () {
// do some stuff here
});
In the meteor server.There is object like this:
A.js
testObject = function(){}
and I want to get testObject by testObject's name "testObject"
if "A.js" in Client . I know I can get the Object by
var a = window["testObject"]
because of window is a global Object and save all other global Object.
but I don't know how to get it in Server.
Any suggestions appreciated!
An easy way to retain a global scope reference is just to wrap your code in an IIFE closure like this:
(function( namespace ) {
console.log( namespace["testObject"] );
}( this ));
This will work on both server and client.
just like node code. use like this
global["testObject"]
I get the answer on FreeNode .Thank you #bline