Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'kew' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
.then(function (data) {
test.equal('value', data, 'The get() should return the right value from the secondary instance')
test.equals(1, cache2.getRequestCounts()['get'], 'The secondary cache should get one get()')
// the following is a fragile test... any better ideas?
return Q.delay(20)
})
.then(function () {
var pkgPath = path.join(libPath, 'phantom')
var phantomPath = null
var tmpPath = null
// If the user manually installed PhantomJS, we want
// to use the existing version.
//
// Do not re-use a manually-installed PhantomJS with
// a different version.
//
// Do not re-use an npm-installed PhantomJS, because
// that can lead to weird circular dependencies between
// local versions and global versions.
// https://github.com/Obvious/phantomjs/issues/85
// https://github.com/Medium/phantomjs/pull/184
var whichDeferred = kew.defer()
which('phantomjs', whichDeferred.makeNodeResolver())
whichDeferred.promise
.then(function (result) {
phantomPath = result
// Horrible hack to avoid problems during global install. We check to see if
// the file `which` found is our own bin script.
if (phantomPath.indexOf(path.join('npm', 'phantomjs')) !== -1) {
console.log('Looks like an `npm install -g` on windows; unable to check for already installed version.')
throw new Error('Global install')
}
var contents = fs.readFileSync(phantomPath, 'utf8')
if (/NPM_INSTALL_MARKER/.test(contents)) {
console.log('Looks like an `npm install -g`; unable to check for already installed version.')
throw new Error('Global install')
function tryPhantomjsInLib() {
return kew.fcall(function () {
return findValidPhantomJsBinary(path.resolve(__dirname, './lib/location.js'))
}).then(function (binaryLocation) {
if (binaryLocation) {
console.log('PhantomJS is previously installed at', binaryLocation)
exit(0)
}
}).fail(function () {
// silently swallow any errors
})
}
function tryPhantomjsInLib() {
return kew.fcall(function () {
return findValidPhantomJsBinary(path.resolve(__dirname, './lib/location.js'))
}).then(function (binaryLocation) {
if (binaryLocation) {
console.log('PhantomJS is previously installed at', binaryLocation)
exit(0)
}
}).fail(function () {
// silently swallow any errors
})
}
function extractDownload(filePath) {
var deferred = kew.defer()
// extract to a unique directory in case multiple processes are
// installing and extracting at once
var extractedPath = filePath + '-extract-' + Date.now()
var options = {cwd: extractedPath}
mkdirp.sync(extractedPath, '0777')
// Make double sure we have 0777 permissions; some operating systems
// default umask does not allow write by default.
fs.chmodSync(extractedPath, '0777')
if (filePath.substr(-4) === '.zip') {
console.log('Extracting zip contents')
try {
var zip = new AdmZip(filePath)
zip.extractAllTo(extractedPath, true)
return kew.nfcall(fs.remove, targetPath).then(function () {
// Look for the extracted directory, so we can rename it.
var files = fs.readdirSync(extractedPath)
for (var i = 0; i < files.length; i++) {
var file = path.join(extractedPath, files[i])
if (fs.statSync(file).isDirectory() && file.indexOf(helper.version) != -1) {
console.log('Copying extracted folder', file, '->', targetPath)
return kew.nfcall(fs.move, file, targetPath)
}
}
console.log('Could not find extracted file', files)
throw new Error('Could not find extracted file')
})
}
return iterateArrayAsync(folderEntries, function(folderEntry) {
var childLocation = path.join(location, folderEntry);
// get stats
return libQ.nfcall(fs.stat, childLocation).then(function(stats) {
// 'Stats' are a little bit different from 'Dirent'
stats.name = folderEntry;
return stats;
}).fail(function(err) {
// skip errors
console.log(err);
});
});
});
function copyIntoPlace(extractedPath, targetPath) {
console.log('Removing', targetPath)
return kew.nfcall(fs.remove, targetPath).then(function () {
// Look for the extracted directory, so we can rename it.
var files = fs.readdirSync(extractedPath)
for (var i = 0; i < files.length; i++) {
var file = path.join(extractedPath, files[i])
if (fs.statSync(file).isDirectory() && file.indexOf(helper.version) != -1) {
console.log('Copying extracted folder', file, '->', targetPath)
return kew.nfcall(fs.move, file, targetPath)
}
}
console.log('Could not find extracted file', files)
throw new Error('Could not find extracted file')
})
}
CoreMetadataCache.prototype.fetchAlbumArt = function(sMbid, sBasePath) {
var self = this;
var bufferImage = null;
var sPath = '';
console.log('fetching art for ' + sMbid);
return libQ.nfcall(libFast.bind(self.coverArtClient.release, self.coverArtClient), sMbid, {piece: 'front'})
.then(function(out) {
bufferImage = out.image;
sPath = sBasePath + '/' + sMbid + out.extension;
console.log(sPath);
return libQ.nfcall(libFileSystem.open, sPath, 'w');
})
.then(function(file) {
return libQ.nfcall(libFileSystem.write, file, bufferImage, 0, 'binary');
})
.then(function(result) {
console.log('file written');
return sPath;
})
.fail(function(error) {
// Have this clause to catch errors so the parent promise does not abort
return sPath;
.then(function (infoCmdOutput) {
var items = {}
infoCmdOutput.split('\n')
.filter(function(str) {return str.indexOf(':') > 0})
.map(function(str) {return str.trim().split(':')})
.map(function(item) {items[item[0]] = item[1]})
var serverInfo = new ServerInfo()
try {
serverInfo.memoryBytes = parseInt(items['used_memory'], 10)
serverInfo.memoryRssBytes = parseInt(items['used_memory_rss'], 10)
serverInfo.evictedKeys = parseInt(items['evicted_keys'], 10)
serverInfo.numOfConnections = parseInt(items['connected_clients'], 10)
// The db0 key's value is something like: 'keys=12,expires=20'
serverInfo.numOfKeys = parseInt(items['db0'].split(',')[0].split('=')[1], 10)
} catch (e) {
Q.reject(new Error('Malformatted output from the "INFO" command of Redis'))
}
return Q.resolve(serverInfo)
})
}