Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'graceful-fs' 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.
// Windows may throw UNKNOWN error. If dest already exists,
// fs throws error anyway, so no need to guard against it here.
if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return fs.symlinkSync(resolvedSrc, dest)
throw err
}
if (opts.dereference) {
resolvedDest = path.resolve(process.cwd(), resolvedDest)
}
if (isSrcSubdir(resolvedSrc, resolvedDest)) {
throw new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`)
}
// prevent copy if src is a subdir of dest since unlinking
// dest in this case would result in removing src contents
// and therefore a broken symlink would be created.
if (fs.statSync(dest).isDirectory() && isSrcSubdir(resolvedDest, resolvedSrc)) {
throw new Error(`Cannot overwrite '${resolvedDest}' with '${resolvedSrc}'.`)
}
return copyLink(resolvedSrc, dest)
}
}
// Windows may throw UNKNOWN error. If dest already exists,
// fs throws error anyway, so no need to guard against it here.
if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return fs.symlinkSync(resolvedSrc, dest)
throw err
}
if (opts.dereference) {
resolvedDest = path.resolve(process.cwd(), resolvedDest)
}
if (stat.isSrcSubdir(resolvedSrc, resolvedDest)) {
throw new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`)
}
// prevent copy if src is a subdir of dest since unlinking
// dest in this case would result in removing src contents
// and therefore a broken symlink would be created.
if (fs.statSync(dest).isDirectory() && stat.isSrcSubdir(resolvedDest, resolvedSrc)) {
throw new Error(`Cannot overwrite '${resolvedDest}' with '${resolvedSrc}'.`)
}
return copyLink(resolvedSrc, dest)
}
}
try {
return fs.renameSync(src, dest)
} catch (err) {
if (err.code === 'ENOTEMPTY' || err.code === 'EEXIST' || err.code === 'EPERM') {
removeSync(dest)
options.overwrite = false // just overwriteed it, no need to do it again
return moveSync(src, dest, options)
}
if (err.code !== 'EXDEV') throw err
return moveSyncAcrossDevice(src, dest, overwrite)
}
} else {
try {
fs.linkSync(src, dest)
return fs.unlinkSync(src)
} catch (err) {
if (err.code === 'EXDEV' || err.code === 'EISDIR' || err.code === 'EPERM' || err.code === 'ENOTSUP') {
return moveSyncAcrossDevice(src, dest, overwrite)
}
throw err
}
}
}
}
var inputFile = new File({
base: inputBase,
cwd: __dirname,
path: inputPath,
contents: inputContents
});
var onEnd = function(){
buffered.length.should.equal(1);
bufEqual(fs.readFileSync(expectedPath), new Buffer(existingContents)).should.equal(true);
done();
};
// Write expected file which should not be overwritten
fs.mkdirSync(expectedBase);
fs.writeFileSync(expectedPath, existingContents);
var stream = app.dest('./out-fixtures/', {cwd: __dirname, overwrite: false});
var buffered = [];
bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd);
stream.pipe(bufferStream);
stream.write(inputFile);
stream.end();
});
fs.readlink(src, (err, resolvedSrc) => {
if (err) return cb(err)
if (opts.dereference) {
resolvedSrc = path.resolve(process.cwd(), resolvedSrc)
}
if (!destStat) {
return fs.symlink(resolvedSrc, dest, cb)
} else {
fs.readlink(dest, (err, resolvedDest) => {
if (err) {
// dest exists and is a regular file or directory,
// Windows may throw UNKNOWN error. If dest already exists,
// fs throws error anyway, so no need to guard against it here.
if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return fs.symlink(resolvedSrc, dest, cb)
return cb(err)
}
if (opts.dereference) {
resolvedDest = path.resolve(process.cwd(), resolvedDest)
}
if (stat.isSrcSubdir(resolvedSrc, resolvedDest)) {
return cb(new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`))
}
// do not copy if src is a subdir of dest since unlinking
// dest in this case would result in removing src contents
FileWriter.prototype._create = function () {
var self = this
if (self._stream) return
var so = {}
if (self.props.flags) so.flags = self.props.flags
so.mode = Writer.filemode
if (self._old && self._old.blksize) so.bufferSize = self._old.blksize
self._stream = fs.createWriteStream(self._path, so)
self._stream.on('open', function () {
// console.error("FW open", self._buffer, self._path)
self.ready = true
self._buffer.forEach(function (c) {
if (c === EOF) self._stream.end()
else self._stream.write(c)
})
self.emit('ready')
// give this a kick just in case it needs it.
self.emit('drain')
})
self._stream.on('error', function (er) { self.emit('error', er) })
self._stream.on('drain', function () { self.emit('drain') })
var ok = ewd.customWebServerRequestHandler(requestObj, response);
if (ok) return;
}
*/
fileRequestHandler(httpParams);
});
};
// WebServer definition - https or http
if (ewd.https.enabled) {
var https = require("https");
var options = {
key: fs.readFileSync(ewd.https.keyPath),
cert: fs.readFileSync(ewd.https.certificatePath),
// turn off SSL 3.0 to protect against POODLE vulnerability
secureProtocol: 'SSLv23_method',
secureOptions: constants.SSL_OP_NO_SSLv3,
};
if (ewd.traceLevel >= 1) console.log("HTTPS is enabled; listening on port " + ewd.httpPort);
webServer = https.createServer(options, webserverCallback);
// Start HTTP listening service for GT.M/Cache to use for WebSockets events
}
else {
if (ewd.traceLevel >= 1) console.log("HTTP is enabled; listening on port " + ewd.httpPort);
var http = require("http");
webServer = http.createServer(webserverCallback);
}
webServer.on('error', function(e) {
console.log('**** Error reported by web server: ' + e.code + ': ' + e.message + ' *****');
if (e.code === 'EADDRINUSE' || e.code === 'EACCES') {
// help file directory
var basepath = path.join(__dirname, '..', '..', 'doc', 'cli');
var filepath;
var data;
// filename format: command.command.txt
filepath = argv._.slice(0);
filepath.push('txt');
filepath = filepath.join('.');
// full path
filepath = path.join(basepath, filepath);
// get help info and replace $0 with process name
data = fs.readFileSync(filepath, 'utf8');
data = data.trim().replace(/\$0/g, argv.$0);
console.log('\n' + data + '\n');
callback(null);
};
var dist = data.dist
if (!dist) return cb(new Error("No dist in "+data._id+" package"))
if (!dist.tarball) return cb(new Error(
"No dist.tarball in " + data._id + " package"))
if ((response && response.statusCode !== 304) || npm.config.get("force")) {
return fetchit()
}
// we got cached data, so let's see if we have a tarball.
var pkgroot = path.join(npm.cache, name, ver)
var pkgtgz = path.join(pkgroot, "package.tgz")
var pkgjson = path.join(pkgroot, "package", "package.json")
fs.stat(pkgtgz, function (er, s) {
if (!er) {
readJson(pkgjson, function (er, data) {
er = needName(er, data)
er = needVersion(er, data)
if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR")
return cb(er)
if (er) return fetchit()
return cb(null, data)
})
} else return fetchit()
})
function fetchit () {
if (!npm.config.get("registry")) {
return cb(new Error("Cannot fetch: "+dist.tarball))
}
var t = tree[k]
, u = url.parse(t.from)
, w = t.what.split("@")
if (u && u.protocol) w[1] = t.from
return w
}).reduce(function (set, k) {
var rangeDescriptor = semver.valid(k[1], true) &&
semver.gte(k[1], "0.1.0", true)
? "~" : ""
set[k[0]] = rangeDescriptor + k[1]
return set
}, {})
// don't use readJson, because we don't want to do all the other
// tricky npm-specific stuff that's in there.
fs.readFile(saveTarget, function (er, data) {
// ignore errors here, just don't save it.
try {
data = JSON.parse(data.toString("utf8"))
} catch (ex) {
er = ex
}
if (er) {
return cb(null, installed, tree, pretty)
}
var deps = npm.config.get("save-optional") ? "optionalDependencies"
: npm.config.get("save-dev") ? "devDependencies"
: "dependencies"
if (saveBundle) {