Файл remove-files.js:
var fs = require('fs');
var doNotRemoveFiles = [
'remove-files.js',
'node_modules',
'@types',
'dist',
'docs',
'src',
'tests',
'.gitignore',
'.npmignore',
'.npmrc',
'.stylelintrc',
'karma.conf.js',
'package.json',
'postcss.config.js',
'readme.md',
'tsconfig.json',
'tslint.json',
'webpack.config.js',
];
fs.readdir('./', function (error, fileNames) {
if (error) {throw error;}
var fileNamesCounter = fileNames.length;
fileNames.forEach(function (fileName) {
var len = doNotRemoveFiles.length;
while (len--) {
if (doNotRemoveFiles[len] === fileName) {return;}
}
fs.stat('./' + fileName, function (error, stats) {
if (stats.isFile()) {
fs.unlink('./' + fileName, function (error) {
if (error) {throw error;}
fileNamesCounter--;
if (fileNamesCounter === 0) {
console.log('DONE');
}
});
} else if (stats.isDirectory()) {
fs.rmdir('./' + fileName, function (error) { // Remove empty "etc" directory.
if (error) {throw error;}
fileNamesCounter--;
if (fileNamesCounter === 0) {
console.log('DONE');
}
});
}
});
});
});
четверг, 8 августа 2019 г.
Node.js UUID
Файл uuid.js:
var crypto = require('crypto');
function rng () {
return crypto.randomBytes(16);
}
/**
* Convert array of 16 byte values to UUID string format of the form:
* XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
*/
var byteToHex = [];
for (var i = 0; i < 256; ++i) {
byteToHex[i] = (i + 0x100).toString(16).substr(1);
}
function bytesToUuid (buf, offset) {
var i = offset || 0;
var bth = byteToHex;
// join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4
return [
bth[buf[i++]], bth[buf[i++]],
bth[buf[i++]], bth[buf[i++]], '-',
bth[buf[i++]], bth[buf[i++]], '-',
bth[buf[i++]], bth[buf[i++]], '-',
bth[buf[i++]], bth[buf[i++]], '-',
bth[buf[i++]], bth[buf[i++]],
bth[buf[i++]], bth[buf[i++]],
bth[buf[i++]], bth[buf[i++]]
].join('');
}
function uuid (options, buf, offset) {
var i = buf && offset || 0;
if (typeof options === 'string') {
buf = options === 'binary' ? new Array(16) : null;
options = null;
}
options = options || {};
var rnds = options.random || (options.rng || rng)();
// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
rnds[6] = (rnds[6] & 0x0f) | 0x40;
rnds[8] = (rnds[8] & 0x3f) | 0x80;
// Copy bytes to buffer, if provided
if (buf) {
for (var ii = 0; ii < 16; ++ii) {
buf[i + ii] = rnds[ii];
}
}
return buf || bytesToUuid(rnds);
}
Файл index.js:
var uuid = require('./uuid');
console.log(uuid());
module.exports = uuid;
var crypto = require('crypto');
function rng () {
return crypto.randomBytes(16);
}
/**
* Convert array of 16 byte values to UUID string format of the form:
* XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
*/
var byteToHex = [];
for (var i = 0; i < 256; ++i) {
byteToHex[i] = (i + 0x100).toString(16).substr(1);
}
function bytesToUuid (buf, offset) {
var i = offset || 0;
var bth = byteToHex;
// join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4
return [
bth[buf[i++]], bth[buf[i++]],
bth[buf[i++]], bth[buf[i++]], '-',
bth[buf[i++]], bth[buf[i++]], '-',
bth[buf[i++]], bth[buf[i++]], '-',
bth[buf[i++]], bth[buf[i++]], '-',
bth[buf[i++]], bth[buf[i++]],
bth[buf[i++]], bth[buf[i++]],
bth[buf[i++]], bth[buf[i++]]
].join('');
}
function uuid (options, buf, offset) {
var i = buf && offset || 0;
if (typeof options === 'string') {
buf = options === 'binary' ? new Array(16) : null;
options = null;
}
options = options || {};
var rnds = options.random || (options.rng || rng)();
// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
rnds[6] = (rnds[6] & 0x0f) | 0x40;
rnds[8] = (rnds[8] & 0x3f) | 0x80;
// Copy bytes to buffer, if provided
if (buf) {
for (var ii = 0; ii < 16; ++ii) {
buf[i + ii] = rnds[ii];
}
}
return buf || bytesToUuid(rnds);
}
Файл index.js:
var uuid = require('./uuid');
console.log(uuid());
module.exports = uuid;
Node.js CLI Spinner
Файл spinner.js
function spinner () {
var sequence = ['|', '/', '-', '\\'];
var index = 0;
var intervalId;
return {
start: function (doNotBlockNodeJsProcess) {
index = 0;
process.stdout.write(sequence[index]);
intervalId = setInterval(function () {
process.stdout.write(sequence[index].replace(/./g, '\b'));
if (index < sequence.length - 1) {
index = index + 1;
} else {
index = 0;
}
process.stdout.write(sequence[index]);
}, 100);
if (doNotBlockNodeJsProcess) {
intervalId.unref();
}
},
stop: function () {
clearInterval(intervalId);
process.stdout.write(sequence[index].replace(/./g, '\b'));
}
};
}
module.exports = spinner;
Файл index.js
var spinner = require('spinner')();
spinner.start(true);
setTimeout(function () {
spinner.stop();
}, 5000);
function spinner () {
var sequence = ['|', '/', '-', '\\'];
var index = 0;
var intervalId;
return {
start: function (doNotBlockNodeJsProcess) {
index = 0;
process.stdout.write(sequence[index]);
intervalId = setInterval(function () {
process.stdout.write(sequence[index].replace(/./g, '\b'));
if (index < sequence.length - 1) {
index = index + 1;
} else {
index = 0;
}
process.stdout.write(sequence[index]);
}, 100);
if (doNotBlockNodeJsProcess) {
intervalId.unref();
}
},
stop: function () {
clearInterval(intervalId);
process.stdout.write(sequence[index].replace(/./g, '\b'));
}
};
}
module.exports = spinner;
Файл index.js
var spinner = require('spinner')();
spinner.start(true);
setTimeout(function () {
spinner.stop();
}, 5000);
XHR Test request in browser console
var data = {a: 1, b: 2};
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://site.com/send/data', false);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(data));
if (xhr.status !== 200) {
console.log('ERROR');
} else {
console.log(xhr.responseText);
}
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://site.com/send/data', false);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(data));
if (xhr.status !== 200) {
console.log('ERROR');
} else {
console.log(xhr.responseText);
}
Подписаться на:
Сообщения (Atom)