_id
stringlengths 2
6
| title
stringlengths 0
58
| partition
stringclasses 3
values | text
stringlengths 52
373k
| language
stringclasses 1
value | meta_information
dict |
---|---|---|---|---|---|
q63300
|
normalize
|
test
|
function normalize (obj) {
var result = obj;
if (typeof obj !== "function") {
if (typeof obj !== "undefined") {
if (Object.prototype.toString.call(obj) !== "[object Object]") {
result = (function (value) { return function () { return value; }; }(obj));
} else {
result = (function (o) { return function (key, passthru) {
if (o[key] === void 0) {
return o.__default || (passthru ? key : undefined);
} else {
return o[key];
}
}; }(obj));
}
} else {
result = function (passthru) { return passthru; };
}
}
return result;
}
|
javascript
|
{
"resource": ""
}
|
q63301
|
supplyMissingDefault
|
test
|
function supplyMissingDefault (options, name) {
if (options[name]() === void 0) {
options[name] = _.wrap(options[name], function(func, key) {
var res = func(key);
return res === void 0 ? defaults[name] : res;
});
}
}
|
javascript
|
{
"resource": ""
}
|
q63302
|
prepOptions
|
test
|
function prepOptions (options, listener) {
// Ensure defaults are represented
common.ensure(options, defaults);
// Normalize certain arguments if they are not functions.
// The certain arguments are per-page options.
// However, outputPath is a special case
[
"selector", "timeout", "useJQuery", "verbose", "phantomjsOptions"
]
.forEach(function (perPageOption) {
options[perPageOption] = normalize(options[perPageOption]);
supplyMissingDefault(options, perPageOption);
});
options._inputEmitter = new EventEmitter();
options._inputEmitter.on("input", listener);
}
|
javascript
|
{
"resource": ""
}
|
q63303
|
getOutputPath
|
test
|
function getOutputPath (options, page, parse) {
var pagePart = urlm.parse(page),
// if outputPath was normalized with an object, let the key passthru
outputPath = options.outputPath(page, true);
// check for bad output path
if (!outputPath) {
return false;
}
// if the outputPath is really still a url, fix it to path+hash
if (common.isUrl(outputPath)) {
outputPath = pagePart.path + (pagePart.hash ? pagePart.hash : "");
}
// if the caller wants the url parse output, return it
if (parse) {
parse.url = pagePart;
}
return outputPath;
}
|
javascript
|
{
"resource": ""
}
|
q63304
|
mapOutputFile
|
test
|
function mapOutputFile (options, page, parse) {
if (!_.isFunction(options.outputPath)) {
options.outputPath = normalize(options.outputPath);
}
var outputPath = getOutputPath(options, page, parse);
var outputDir = options.outputDir;
var fileName = "index.html";
if (options.sitemapOutputDir) {
outputDir = path.join(options.outputDir, options.sitemapOutputDir);
fileName = "";
}
return ( outputPath && path.join(outputDir, outputPath, fileName) ) || false;
}
|
javascript
|
{
"resource": ""
}
|
q63305
|
test
|
function (options, generator, listener) {
options = options || {};
prepOptions(options, listener);
return generator(options);
}
|
javascript
|
{
"resource": ""
}
|
|
q63306
|
test
|
function (options, page) {
var parse = {};
var outputFile = mapOutputFile(options, page, parse);
if (outputFile) {
options._inputEmitter.emit("input", {
outputFile: outputFile,
// make the url
url: urlm.format({
protocol: options.protocol,
auth: options.auth,
hostname: options.hostname,
port: options.port,
pathname: parse.url.pathname,
search: parse.url.search,
hash: parse.url.hash
}),
// map the input page to a selector
selector: options.selector(page),
// map the input page to a timeout
timeout: options.timeout(page),
checkInterval: options.checkInterval,
// map the input page to a useJQuery flag
useJQuery: options.useJQuery(page),
// map the input page to a verbose flag
verbose: options.verbose(page),
// map the input page to phantomJS options
phantomjsOptions: options.phantomjsOptions(page),
// useful for testing, debugging
__page: page
});
}
return outputFile;
}
|
javascript
|
{
"resource": ""
}
|
|
q63307
|
pathExists
|
test
|
function pathExists (path, options) {
options = options || {
returnFile: false
};
// Defaults to F_OK
return nodeCall(fs.access, path)
.then(function () {
return options.returnFile ? path : true;
})
.catch(function () {
if (fs.existsSync(path)) {
return options.returnFile ? path : true;
}
return false;
});
}
|
javascript
|
{
"resource": ""
}
|
q63308
|
test
|
function () {
// If the path we're given by phantomjs is to a .cmd, it is pointing to a global copy.
// Using the cmd as the process to execute causes problems cleaning up the processes
// so we walk from the cmd to the phantomjs.exe and use that instead.
var phantomSource = require("phantomjs-prebuilt").path;
if (path.extname(phantomSource).toLowerCase() === ".cmd") {
return path.join(path.dirname( phantomSource ), "//node_modules//phantomjs-prebuilt//lib//phantom//bin//phantomjs.exe");
}
return phantomSource;
}
|
javascript
|
{
"resource": ""
}
|
|
q63309
|
worker
|
test
|
function worker (input, options, notifier, qcb) {
var cp,
customModule,
snapshotScript = options.snapshotScript,
phantomjsOptions = Array.isArray(input.phantomjsOptions) ? input.phantomjsOptions : [input.phantomjsOptions];
// If the outputFile has NOT already been seen by the notifier, process.
if (!notifier.known(input.outputFile)) {
// map snapshotScript object script to a real path
if (_.isObject(options.snapshotScript)) {
snapshotScript = path.join(__dirname, phantomDir, options.snapshotScript.script) + ".js";
customModule = options.snapshotScript.module;
}
cp = spawn(
options.phantomjs,
phantomjsOptions.concat([
snapshotScript,
input.outputFile,
input.url,
input.selector,
input.timeout,
input.checkInterval,
input.useJQuery,
input.verbose,
customModule
]), { cwd: process.cwd(), stdio: "inherit", detached: true }
);
cp.on("error", function (e) {
notifier.remove(input.outputFile);
notifier.setError(e);
console.error(e);
qcb(e);
});
cp.on("exit", function (code) {
qcb(code);
});
// start counting
notifier.add(input.outputFile, input.timeout);
}
else {
// The input.outputFile is being or has been processed this run.
qcb(0);
}
}
|
javascript
|
{
"resource": ""
}
|
q63310
|
prepOptions
|
test
|
function prepOptions (options) {
// ensure this module's defaults are represented in the options.
common.ensure(options, defaults);
// if array data source, ensure input type is "array".
if (Array.isArray(options.source)) {
options.input = "array";
}
}
|
javascript
|
{
"resource": ""
}
|
q63311
|
test
|
function (options, listener) {
var inputGenerator, notifier, started, result, q, emitter, completion;
options = options || {};
prepOptions(options);
// create the inputGenerator, default to robots
inputGenerator = inputFactory.create(options.input);
// clean the snapshot output directory
if (options.outputDirClean) {
rimraf(options.outputDir);
}
// start async completion notification.
notifier = new Notifier();
emitter = new EventEmitter();
started = notifier.start(options.pollInterval, inputGenerator,
function (err, completed) {
emitter.emit("complete", err, completed);
});
if (started) {
// create the completion Promise.
completion = new Promise(function (resolve, reject) {
function completionResolver (err, completed) {
try {
_.isFunction(listener) && listener(err, completed);
} catch (e) {
console.error("User supplied listener exception", e);
}
if (err) {
err.notCompleted = notifier.filesNotDone;
err.completed = completed;
reject(err);
} else {
resolve(completed);
}
}
emitter.addListener("complete", completionResolver);
});
// create a worker queue with a parallel process limit.
q = asyncLib.queue(function (task, callback) {
task(_.once(callback));
}, options.processLimit);
// have the queue call notifier.empty when last item
// from the queue is given to a worker.
q.empty = notifier.qEmpty.bind(notifier);
// expose abort callback to input generators via options.
options._abort = function (err) {
notifier.abort(q, err);
};
// generate input for the snapshots.
result = inputGenerator.run(options, function (input) {
// give the worker the input and place into the queue
q.push(_.partial(worker, input, options, notifier));
})
// after input generation, resolve on browser completion.
.then(function () {
return completion;
});
} else {
result = Promise.reject("failed to start async notifier");
}
return result;
}
|
javascript
|
{
"resource": ""
}
|
|
q63312
|
createLockFactory
|
test
|
function createLockFactory () {
// Create the per instance async lock.
var lock = new AsyncLock();
// Make a random id
var rid = crypto.randomBytes(16).toString("hex");
/**
* Force a serial execution context.
*
* @param {Function} fn - The function to guard.
* @param {Number} timeout - The max time to wait for the lock.
*/
return function lockFactory (fn, timeout) {
return function protectedContext () {
lock.acquire("cs-guard-" + rid, function (done) {
fn(function () {
done(null, 0);
});
}, NOOP, {
timeout: timeout
});
};
};
}
|
javascript
|
{
"resource": ""
}
|
q63313
|
Notifier
|
test
|
function Notifier () {
// Create the serial execution context mechanism.
this.csFactory = createLockFactory();
// The private files collection
// Contains a file and timer: "filename": {timer: timerid}
// Used for tracking work left to do. When empty, work is done.
this.files = {};
// Contains files successfully processed
this.filesDone = [];
// Contains files unsuccessfully processed
this.filesNotDone = [];
// true if a timeout occurred, or set by abort
this.errors = [];
// the holder of the current failure timeout padding
this.padTimeout = TIMEOUT_PAD_FLOOR;
// our reference to the listener
this.callback = null;
// our reference to the watcher (an interval id)
// initial value undefined is important
// this.watcher;
// the working pollInterval for this run
// this.interval
// flag set by qEmpty callback
// when the last item from the queue is given to a worker
this.qempty = false;
}
|
javascript
|
{
"resource": ""
}
|
q63314
|
start
|
test
|
function start (pollInterval, input, listener) {
var result = (
pollInterval > 0 &&
typeof listener === "function" &&
(!!input)
);
if (result) {
if (this.isStarted()) {
throw new Error("Notifier already started");
}
this.callback = listener;
this.interval = parseInt(pollInterval, 10);
// Poll the filesystem for the files to exist
// Checks the child process expected output to determine success or failure
// if the file exists, then it succeeded.
this.watcher = setInterval(this.csFactory(function (done) {
var self = this;
var eoi = typeof input.EOI === "function" && input.EOI();
if (eoi) {
Promise.all(Object.keys(self.files).map(function (file) {
return pathExists(file, {
returnFile: true
});
}))
.then(function (files) {
var callback = self.callback;
try {
files.forEach(function (file) {
file && self._remove(file, true);
});
if (self._isDone()) {
self._closeWatcher();
if (self.callback) {
self.callback = null;
setImmediate(function () {
callback(self.getError(), self.filesDone);
});
}
}
} catch (e) {
console.error(e);
}
done();
});
} else {
done();
}
}.bind(this), L_WAIT), this.interval);
} else {
console.error("Bad poll interval, async listener, or input generator supplied");
}
return result;
}
|
javascript
|
{
"resource": ""
}
|
q63315
|
add
|
test
|
function add (outputFile, timeout) {
var failTimeout = timeout;
var timer;
if (!this.isStarted()) {
throw new Error("MUST call `start` before `add`");
}
if (!this._exists(outputFile)) {
// make sure we evaluate after the child process
failTimeout = parseInt(timeout, 10) + parseInt(this.padTimeout, 10);
// Stagger and grow the failure timeout padding, add 1s every 10 processes
this.padTimeout += 100;
// setup a timeout handler to detect failure
timer = setTimeout(this.csFactory(function (done) {
var self = this;
// if the output file has not already been removed
if (self._exists(outputFile)) {
pathExists(outputFile)
.then(function (fsExists) {
var callback = self.callback;
try {
if (!fsExists) {
self._setError(new Error(
"'" + outputFile + "' did not get a snapshot before timeout"
));
}
self._remove(outputFile, fsExists);
if (self._isDone()) {
self._closeWatcher();
if (self.callback) {
self.callback = null;
setImmediate(function () {
callback(self.getError(), self.filesDone);
});
}
}
} catch (e) {
console.error(e);
}
done();
});
} else {
done();
}
}.bind(this), L_WAIT), parseInt(failTimeout, 10));
// add the file tracking object
this.files[outputFile] = {
timer: timer
};
}
}
|
javascript
|
{
"resource": ""
}
|
q63316
|
known
|
test
|
function known (outputFile) {
var result = false;
this.csFactory(function (done) {
result =
this._exists(outputFile) || this.filesDone.indexOf(outputFile) > -1;
done();
}.bind(this), L_WAIT)();
return result;
}
|
javascript
|
{
"resource": ""
}
|
q63317
|
_remove
|
test
|
function _remove (outputFile, done) {
if (this._exists(outputFile)) {
if (done) {
this.filesDone.push(outputFile);
} else {
this.filesNotDone.push(outputFile);
}
clearTimeout(this.files[outputFile].timer);
delete this.files[outputFile];
}
}
|
javascript
|
{
"resource": ""
}
|
q63318
|
remove
|
test
|
function remove (outputFile, done) {
this.csFactory(function (_done) {
this._remove(outputFile, done);
_done();
}.bind(this), L_WAIT)();
}
|
javascript
|
{
"resource": ""
}
|
q63319
|
test
|
function (time) {
fs.write(options.outputFile, filter(page.content), "w");
globals.exit(0, "snapshot for "+options.url+" finished in "+time+" ms\n written to "+options.outputFile);
}
|
javascript
|
{
"resource": ""
}
|
|
q63320
|
oneline
|
test
|
function oneline (line, options) {
var key = "Allow: ",
index = line.indexOf(key);
if (index !== -1) {
var page = line.substr(index + key.length).replace(/^\s+|\s+$/g, "");
return page.indexOf("*") === -1 && base.input(options, page);
}
return true;
}
|
javascript
|
{
"resource": ""
}
|
q63321
|
getRobotsUrl
|
test
|
function getRobotsUrl (options, callback) {
request({
url: options.source,
timeout: options.timeout()
}, function (err, res, body) {
var error = err || common.checkResponse(res, "text/plain");
if (error) {
callback(common.prependMsgToErr(error, options.source, true));
} else {
body.toString().split('\n').every(function(line) {
// Process the line input, but break if base.input returns false.
// For now, this can only happen if no outputDir is defined,
// which is a fatal bad option problem and will happen immediately.
if (!oneline(line, options)) {
error = common.prependMsgToErr(base.generatorError(), line, true);
return false;
}
return true;
});
callback(error);
}
});
}
|
javascript
|
{
"resource": ""
}
|
q63322
|
getRobotsFile
|
test
|
function getRobotsFile (options, callback) {
fs.readFile(options.source, function (err, data) {
if (!err) {
data.toString().split('\n').every(function (line) {
// Process the line input, but break if base.input returns false.
// For now, this can only happen if no outputDir is defined,
// which is a fatal bad option problem and will happen immediately.
if (!oneline(line, options)) {
err = common.prependMsgToErr(base.generatorError(), line, true);
return false;
}
return true;
});
}
callback(err);
});
}
|
javascript
|
{
"resource": ""
}
|
q63323
|
bubble
|
test
|
function bubble(values) {
return values.map(d => {
if (d.key && d.values) {
if (d.values[0].key === "undefined") return d.values[0].values[0];
else d.values = bubble(d.values);
}
return d;
});
}
|
javascript
|
{
"resource": ""
}
|
q63324
|
exclude
|
test
|
function exclude(a, b, v) {
const aStart = a.start({type: "bigInteger"});
const bStart = b.start({type: "bigInteger"});
const aEnd = a.end({type: "bigInteger"});
const bEnd = b.end({type: "bigInteger"});
const parts = [];
// compareTo returns negative if left is less than right
// aaa
// bbb
// aaa
// bbb
if (aStart.compareTo(bEnd) > 0 || aEnd.compareTo(bStart) < 0) {
return [a.cidr];
}
// aaa
// bbb
if (aStart.compareTo(bStart) === 0 && aEnd.compareTo(bEnd) === 0) {
return [];
}
// aa
// bbbb
if (aStart.compareTo(bStart) > 0 && aEnd.compareTo(bEnd) < 0) {
return [];
}
// aaaa
// bbbb
// aaaa
// bb
if (aStart.compareTo(bStart) < 0 && aEnd.compareTo(bEnd) <= 0) {
parts.push({
start: aStart,
end: bStart.subtract(one),
});
}
// aaa
// bbb
// aaaa
// bbb
if (aStart.compareTo(bStart) >= 0 && aEnd.compareTo(bEnd) > 0) {
parts.push({
start: bEnd.add(one),
end: aEnd,
});
}
// aaaa
// bb
if (aStart.compareTo(bStart) < 0 && aEnd.compareTo(bEnd) > 0) {
parts.push({
start: aStart,
end: bStart.subtract(one),
});
parts.push({
start: bEnd.add(one),
end: aEnd,
});
}
const remaining = [];
for (const part of parts) {
for (const subpart of subparts(part, v)) {
remaining.push(formatPart(subpart, v));
}
}
return cidrTools.merge(remaining);
}
|
javascript
|
{
"resource": ""
}
|
q63325
|
getMsTimestamp
|
test
|
function getMsTimestamp(){
var ts = new Date().getTime();
if(lastMsTs >= ts){
lastMsTs++;
}
else{
lastMsTs = ts;
}
return lastMsTs;
}
|
javascript
|
{
"resource": ""
}
|
q63326
|
parseUrl
|
test
|
function parseUrl(url) {
var serverOptions = {
host: "localhost",
port: 80
};
if(url.indexOf("https") === 0){
serverOptions.port = 443;
}
var host = url.split("://").pop();
serverOptions.host = host;
var lastPos = host.indexOf(":");
if (lastPos > -1) {
serverOptions.host = host.slice(0,lastPos);
serverOptions.port = Number(host.slice(lastPos+1,host.length));
}
return serverOptions;
}
|
javascript
|
{
"resource": ""
}
|
q63327
|
prepareParams
|
test
|
function prepareParams(params){
var str = [];
for(var i in params){
str.push(i+"="+encodeURIComponent(params[i]));
}
return str.join("&");
}
|
javascript
|
{
"resource": ""
}
|
q63328
|
stripTrailingSlash
|
test
|
function stripTrailingSlash(str) {
if(str.substr(str.length - 1) === "/") {
return str.substr(0, str.length - 1);
}
return str;
}
|
javascript
|
{
"resource": ""
}
|
q63329
|
getProperties
|
test
|
function getProperties(orig, props){
var ob = {};
var prop;
for(var i = 0; i < props.length; i++){
prop = props[i];
if(typeof orig[prop] !== "undefined"){
ob[prop] = orig[prop];
}
}
return ob;
}
|
javascript
|
{
"resource": ""
}
|
q63330
|
add_cly_events
|
test
|
function add_cly_events(event){
if(!event.key){
log("Event must have key property");
return;
}
if(cluster.isMaster){
if(!event.count){
event.count = 1;
}
var props = ["key", "count", "sum", "dur", "segmentation"];
var e = getProperties(event, props);
e.timestamp = getMsTimestamp();
var date = new Date();
e.hour = date.getHours();
e.dow = date.getDay();
log("Adding event: ", event);
eventQueue.push(e);
storeSet("cly_event", eventQueue);
}
else{
process.send({ cly: {event: event} });
}
}
|
javascript
|
{
"resource": ""
}
|
q63331
|
prepareRequest
|
test
|
function prepareRequest(request) {
request.app_key = Countly.app_key;
request.device_id = Countly.device_id;
request.sdk_name = SDK_NAME;
request.sdk_version = SDK_VERSION;
if(Countly.check_consent("location")){
if(Countly.country_code){
request.country_code = Countly.country_code;
}
if(Countly.city){
request.city = Countly.city;
}
if(Countly.ip_address !== null){
request.ip_address = Countly.ip_address;
}
}
else{
request.location = "";
}
request.timestamp = getMsTimestamp();
var date = new Date();
request.hour = date.getHours();
request.dow = date.getDay();
}
|
javascript
|
{
"resource": ""
}
|
q63332
|
toRequestQueue
|
test
|
function toRequestQueue(request){
if(cluster.isMaster){
if(!Countly.app_key || !Countly.device_id){
log("app_key or device_id is missing");
return;
}
prepareRequest(request);
if(requestQueue.length > queueSize){
requestQueue.shift();
}
requestQueue.push(request);
storeSet("cly_queue", requestQueue);
}
else{
process.send({ cly: {cly_queue: request} });
}
}
|
javascript
|
{
"resource": ""
}
|
q63333
|
getMetrics
|
test
|
function getMetrics(){
var m = JSON.parse(JSON.stringify(metrics));
//getting app version
m._app_version = m._app_version || Countly.app_version;
m._os = m._os || os.type();
m._os_version = m._os_version || os.release();
platform = os.type();
log("Got metrics", m);
return m;
}
|
javascript
|
{
"resource": ""
}
|
q63334
|
makeRequest
|
test
|
function makeRequest(url, path, params, callback) {
try {
log("Sending HTTP request");
var serverOptions = parseUrl(url);
var data = prepareParams(params);
var method = "GET";
var options = {
host: serverOptions.host,
port: serverOptions.port,
path: path+"?"+data,
method: "GET"
};
if(data.length >= 2000){
method = "POST";
}
else if(Countly.force_post){
method = "POST";
}
if(method === "POST"){
options.method = "POST";
options.path = path;
options.headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": Buffer.byteLength(data)
};
}
var protocol = http;
if(url.indexOf("https") === 0){
protocol = https;
}
var req = protocol.request(options, function(res) {
var str = "";
res.on("data", function (chunk) {
str += chunk;
});
res.on("end", function () {
if(res.statusCode >= 200 && res.statusCode < 300){
callback(false, params, str);
}
else{
callback(true, params);
}
});
});
if(method === "POST"){
// write data to request body
req.write(data);
}
req.on("error", function(err){
log("Connection failed.", err);
if (typeof callback === "function") {
callback(true, params);
}
});
req.end();
} catch (e) {
// fallback
log("Failed HTTP request", e);
if (typeof callback === "function") { callback(true, params); }
}
}
|
javascript
|
{
"resource": ""
}
|
q63335
|
allSettled
|
test
|
function allSettled(promises) {
"use strict";
const wrappedPromises = promises.map((curPromise) => curPromise.reflect());
return Promise.all(wrappedPromises);
}
|
javascript
|
{
"resource": ""
}
|
q63336
|
after
|
test
|
function after(parent, index) {
var siblings = parent.children
var sibling = siblings[++index]
var other
if (is('WhiteSpaceNode', sibling)) {
sibling = siblings[++index]
if (is('PunctuationNode', sibling) && punctuation.test(toString(sibling))) {
sibling = siblings[++index]
}
if (is('WordNode', sibling)) {
other = sibling
}
}
return other
}
|
javascript
|
{
"resource": ""
}
|
q63337
|
classify
|
test
|
function classify(value) {
var type = null
var normal
value = value.replace(digits, toWords).split(split, 1)[0]
normal = lower(value)
if (requiresA(value)) {
type = 'a'
}
if (requiresAn(value)) {
type = type === 'a' ? 'a-or-an' : 'an'
}
if (!type && normal === value) {
type = vowel.test(normal.charAt(0)) ? 'an' : 'a'
}
return type
}
|
javascript
|
{
"resource": ""
}
|
q63338
|
factory
|
test
|
function factory(list) {
var expressions = []
var sensitive = []
var insensitive = []
construct()
return test
function construct() {
var length = list.length
var index = -1
var value
var normal
while (++index < length) {
value = list[index]
normal = value === lower(value)
if (value.charAt(value.length - 1) === '*') {
// Regexes are insensitive now, once we need them this should check for
// `normal` as well.
expressions.push(new RegExp('^' + value.slice(0, -1), 'i'))
} else if (normal) {
insensitive.push(value)
} else {
sensitive.push(value)
}
}
}
function test(value) {
var normal = lower(value)
var length
var index
if (sensitive.indexOf(value) !== -1 || insensitive.indexOf(normal) !== -1) {
return true
}
length = expressions.length
index = -1
while (++index < length) {
if (expressions[index].test(value)) {
return true
}
}
return false
}
}
|
javascript
|
{
"resource": ""
}
|
q63339
|
test
|
function(rcb) {
return function() {
if (frc || !db[cname]) {
bindColCtls(db);
}
if (rcb) {
rcb.apply(this, arguments);
}
}
}
|
javascript
|
{
"resource": ""
}
|
|
q63340
|
test
|
function() {
Object.defineProperty(this, "_impl", {
value : new EJDBImpl(),
configurable : false,
enumerable : false,
writable : false
});
return this;
}
|
javascript
|
{
"resource": ""
}
|
|
q63341
|
dummyText
|
test
|
function dummyText ( opts ) {
var corpus = opts.corpus || 'lorem',
i = opts.start,
isRandom = typeof(i) === 'undefined',
mustReset = typeof(origin) === 'undefined',
skip = opts.skip || 1,
sentences = opts.sentences || 1,
words = opts.words,
text = texts[corpus] || texts.lorem,
len = text.length,
output = [],
s;
if ( isRandom ) {
i = Math.floor( Math.random() * len );
}
if ( mustReset ) {
origin = i;
}
if ( isRandom ) {
// possible modulo of a negative number, so take care here.
i = ((i + len - origin) % len + len) % len;
}
while( sentences-- ) {
s = text[i];
if ( words ) {
s = s.split(' ').slice(0,words).join(' ');
}
output.push( s );
i = (i + skip) % len;
}
return output.join(' ');
}
|
javascript
|
{
"resource": ""
}
|
q63342
|
Back
|
test
|
function Back(options) {
if (!(this instanceof Back)) {
return new Back(options);
}
this.settings = extend(options);
this.reconnect = null;
}
|
javascript
|
{
"resource": ""
}
|
q63343
|
css
|
test
|
function css(files, output, options) {
options = Object.assign({
banner: false
}, options);
return () => {
var build = gulp.src(files)
if (options.banner)
build = build.pipe($.header(banner, {pkg}));
build = build
.pipe($.rename('d3.compose.css'))
.pipe(gulp.dest(output));
return build;
};
}
|
javascript
|
{
"resource": ""
}
|
q63344
|
series
|
test
|
function series() {
const tasks = Array.prototype.slice.call(arguments);
var fn = cb => cb();
if (typeof tasks[tasks.length - 1] === 'function')
fn = tasks.pop();
return (cb) => {
const tasks_with_cb = tasks.concat([(err) => {
if (err) return cb(err);
fn(cb);
}]);
runSequence.apply(this, tasks_with_cb);
}
}
|
javascript
|
{
"resource": ""
}
|
q63345
|
simpleTypeFilter
|
test
|
function simpleTypeFilter(doc, oldDoc, candidateDocType) {
if (oldDoc) {
if (doc._deleted) {
return oldDoc.type === candidateDocType;
} else {
return doc.type === oldDoc.type && oldDoc.type === candidateDocType;
}
} else {
return doc.type === candidateDocType;
}
}
|
javascript
|
{
"resource": ""
}
|
q63346
|
padRight
|
test
|
function padRight(value, desiredLength, padding) {
while (value.length < desiredLength) {
value += padding;
}
return value;
}
|
javascript
|
{
"resource": ""
}
|
q63347
|
resolveCollectionDefinition
|
test
|
function resolveCollectionDefinition(doc, oldDoc, collectionDefinition, itemPrefix) {
if (utils.isValueNullOrUndefined(collectionDefinition)) {
return [ ];
} else {
if (typeof collectionDefinition === 'function') {
var fnResults = collectionDefinition(doc, oldDoc);
return resolveCollectionItems(fnResults, itemPrefix);
} else {
return resolveCollectionItems(collectionDefinition, itemPrefix);
}
}
}
|
javascript
|
{
"resource": ""
}
|
q63348
|
assignRolesToUsers
|
test
|
function assignRolesToUsers(doc, oldDoc, accessAssignmentDefinition) {
var users = resolveCollectionDefinition(doc, oldDoc, accessAssignmentDefinition.users);
var roles = resolveRoleCollectionDefinition(doc, oldDoc, accessAssignmentDefinition.roles);
role(users, roles);
return {
type: 'role',
users: users,
roles: roles
};
}
|
javascript
|
{
"resource": ""
}
|
q63349
|
getAllDocChannels
|
test
|
function getAllDocChannels(docDefinition) {
var docChannelMap = utils.resolveDocumentConstraint(docDefinition.channels);
var allChannels = [ ];
if (docChannelMap) {
appendToAuthorizationList(allChannels, docChannelMap.view);
appendToAuthorizationList(allChannels, docChannelMap.write);
appendToAuthorizationList(allChannels, docChannelMap.add);
appendToAuthorizationList(allChannels, docChannelMap.replace);
appendToAuthorizationList(allChannels, docChannelMap.remove);
}
return allChannels;
}
|
javascript
|
{
"resource": ""
}
|
q63350
|
outputHelpIfNecessary
|
test
|
function outputHelpIfNecessary(cmd, options) {
options = options || [];
for (var i = 0; i < options.length; i++) {
if (options[i] === '--help' || options[i] === '-h') {
cmd.outputHelp();
process.exit(0);
}
}
}
|
javascript
|
{
"resource": ""
}
|
q63351
|
humanReadableArgName
|
test
|
function humanReadableArgName(arg) {
var nameOutput = arg.name + (arg.variadic === true ? '...' : '');
return arg.required
? '<' + nameOutput + '>'
: '[' + nameOutput + ']';
}
|
javascript
|
{
"resource": ""
}
|
q63352
|
validateObjectProperties
|
test
|
function validateObjectProperties(propertyValidators, allowUnknownProperties, ignoreInternalProperties) {
var currentItemEntry = itemStack[itemStack.length - 1];
var objectValue = currentItemEntry.itemValue;
var oldObjectValue = currentItemEntry.oldItemValue;
var supportedProperties = [ ];
for (var propertyValidatorName in propertyValidators) {
var validator = propertyValidators[propertyValidatorName];
if (utils.isValueNullOrUndefined(validator) || utils.isValueNullOrUndefined(resolveItemConstraint(validator.type))) {
// Skip over non-validator fields/properties
continue;
}
var propertyValue = objectValue[propertyValidatorName];
var oldPropertyValue;
if (!utils.isValueNullOrUndefined(oldObjectValue)) {
oldPropertyValue = oldObjectValue[propertyValidatorName];
}
supportedProperties.push(propertyValidatorName);
itemStack.push({
itemValue: propertyValue,
oldItemValue: oldPropertyValue,
itemName: propertyValidatorName
});
validateItemValue(validator);
itemStack.pop();
}
// Verify there are no unsupported properties in the object
if (!allowUnknownProperties) {
for (var propertyName in objectValue) {
if (ignoreInternalProperties && propertyName.indexOf('_') === 0) {
// These properties are special cases that should always be allowed - generally only applied at the root
// level of the document
continue;
}
if (supportedProperties.indexOf(propertyName) < 0) {
var objectPath = buildItemPath(itemStack);
var fullPropertyPath = objectPath ? objectPath + '.' + propertyName : propertyName;
validationErrors.push('property "' + fullPropertyPath + '" is not supported');
}
}
}
}
|
javascript
|
{
"resource": ""
}
|
q63353
|
buildItemPath
|
test
|
function buildItemPath(itemStack) {
var nameComponents = [ ];
for (var i = 0; i < itemStack.length; i++) {
var itemName = itemStack[i].itemName;
if (!itemName) {
// Skip null or empty names (e.g. the first element is typically the root of the document, which has no name)
continue;
} else if (nameComponents.length < 1 || itemName.indexOf('[') === 0) {
nameComponents.push(itemName);
} else {
nameComponents.push('.' + itemName);
}
}
return nameComponents.join('');
}
|
javascript
|
{
"resource": ""
}
|
q63354
|
getBusinessId
|
test
|
function getBusinessId(doc, oldDoc) {
var regex = /^biz\.([A-Za-z0-9_-]+)(?:\..+)?$/;
var matchGroups = regex.exec(doc._id);
if (matchGroups) {
return matchGroups[1];
} else if (oldDoc && oldDoc.businessId) {
// The document ID doesn't contain a business ID, so use the property from the old document
return oldDoc.businessId || null;
} else {
// Neither the document ID nor the old document's contents contain a business ID, so use the property from the new document
return doc.businessId || null;
}
}
|
javascript
|
{
"resource": ""
}
|
q63355
|
toDefaultSyncChannels
|
test
|
function toDefaultSyncChannels(doc, oldDoc, basePrivilegeName) {
var businessId = getBusinessId(doc, oldDoc);
return function(doc, oldDoc) {
return {
view: [ toSyncChannel(businessId, 'VIEW_' + basePrivilegeName) ],
add: [ toSyncChannel(businessId, 'ADD_' + basePrivilegeName) ],
replace: [ toSyncChannel(businessId, 'CHANGE_' + basePrivilegeName) ],
remove: [ toSyncChannel(businessId, 'REMOVE_' + basePrivilegeName) ]
};
};
}
|
javascript
|
{
"resource": ""
}
|
q63356
|
isIso8601DateTimeString
|
test
|
function isIso8601DateTimeString(value) {
var dateAndTimePieces = splitDateAndTime(value);
var date = extractDateStructureFromDateAndTime(dateAndTimePieces);
if (date) {
var timeAndTimezone = extractTimeStructuresFromDateAndTime(dateAndTimePieces);
var time = timeAndTimezone.time;
var timezone = timeAndTimezone.timezone;
return isValidDateStructure(date) &&
isValidTimeStructure(time) &&
(timezone === null || isValidTimeZoneStructure(timezone));
} else {
return false;
}
}
|
javascript
|
{
"resource": ""
}
|
q63357
|
normalizeIso8601Time
|
test
|
function normalizeIso8601Time(time, timezoneOffsetMinutes) {
var msPerSecond = 1000;
var msPerMinute = 60000;
var msPerHour = 3600000;
var effectiveTimezoneOffset = timezoneOffsetMinutes || 0;
var rawTimeMs =
(time.hour * msPerHour) + (time.minute * msPerMinute) + (time.second * msPerSecond) + time.millisecond;
return rawTimeMs - (effectiveTimezoneOffset * msPerMinute);
}
|
javascript
|
{
"resource": ""
}
|
q63358
|
compareTimes
|
test
|
function compareTimes(a, b) {
if (typeof a !== 'string' || typeof b !== 'string') {
return NaN;
}
return normalizeIso8601Time(parseIso8601Time(a)) - normalizeIso8601Time(parseIso8601Time(b));
}
|
javascript
|
{
"resource": ""
}
|
q63359
|
compareDates
|
test
|
function compareDates(a, b) {
var aPieces = extractDatePieces(a);
var bPieces = extractDatePieces(b);
if (aPieces === null || bPieces === null) {
return NaN;
}
for (var pieceIndex = 0; pieceIndex < aPieces.length; pieceIndex++) {
if (aPieces[pieceIndex] < bPieces[pieceIndex]) {
return -1;
} else if (aPieces[pieceIndex] > bPieces[pieceIndex]) {
return 1;
}
}
// If we got here, the two parameters represent the same date/point in time
return 0;
}
|
javascript
|
{
"resource": ""
}
|
q63360
|
normalizeIso8601TimeZone
|
test
|
function normalizeIso8601TimeZone(value) {
return value ? value.multiplicationFactor * ((value.hour * 60) + value.minute) : -(new Date().getTimezoneOffset());
}
|
javascript
|
{
"resource": ""
}
|
q63361
|
start
|
test
|
async function start() {
log.i('--Nexus/Start');
//build the setup promise array
let startArray = [];
for (let pid in Start) {
startArray.push(new Promise((resolve, _reject) => {
let com = {};
com.Cmd = Start[pid];
com.Passport = {};
com.Passport.To = pid;
com.Passport.Pid = genPid();
sendMessage(com, resolve);
}));
}
await Promise.all(startArray);
log.v('--Nexus: All Starts Complete');
}
|
javascript
|
{
"resource": ""
}
|
q63362
|
exit
|
test
|
async function exit(code = 0) {
log.i('--Nexus/Stop');
//build the Stop promise array
let stopTasks = [];
log.i('Nexus unloading node modules');
log.v(Object.keys(require.cache).join('\n'));
for (let pid in Stop) {
stopTasks.push(new Promise((resolve, _reject) => {
let com = {};
com.Cmd = Stop[pid];
com.Passport = {};
com.Passport.To = pid;
com.Passport.Pid = genPid();
sendMessage(com, resolve);
}));
}
await Promise.all(stopTasks);
log.v('--Nexus: All Stops Complete');
dispatchEvent('exit', { exitCode: code });
}
|
javascript
|
{
"resource": ""
}
|
q63363
|
sendMessage
|
test
|
function sendMessage(com, fun = _ => _) {
if (!('Passport' in com)) {
log.w('Message has no Passport, ignored');
log.w(' ' + JSON.stringify(com));
fun('No Passport');
return;
}
if (!('To' in com.Passport) || !com.Passport.To) {
log.w('Message has no destination entity, ignored');
log.w(' ' + JSON.stringify(com));
fun('No recipient in message', com);
return;
}
if (!('Pid' in com.Passport)) {
log.w('Message has no message id, ignored');
log.w(' ' + JSON.stringify(com));
fun('No message id', com);
return;
}
let pid = com.Passport.To;
let apx = com.Passport.Apex || pid;
if (pid in EntCache) {
done(null, EntCache[pid]);
return;
} else {
getEntityContext(pid, done);
}
async function done(err, entContextVolatile) {
let entApex = await new Promise(res =>
entContextVolatile.lock((val) => {
res(val.Apex);
return val;
})
);
if (err) {
log.w(err);
log.w(JSON.stringify(com, null, 2));
fun(err, com);
return;
}
//TODO pid instanceOf ApexEntity
if ((EntCache[pid].Apex == EntCache[pid].Pid) || (entApex == apx)) {
let entContext = await new Promise(res => entContextVolatile.lock((context) => {
res(context); return context;
}));
entContext.instance.dispatch(com, reply);
} else {
let err = 'Trying to send a message to a non-Apex'
+ 'entity outside of the sending module';
log.w(err);
log.w(JSON.stringify(com, null, 2));
fun(err, com);
}
}
function reply(err, q) {
// log.i('NEXUS MESSAGE:', com)
fun(err, q);
}
}
|
javascript
|
{
"resource": ""
}
|
q63364
|
deleteEntity
|
test
|
function deleteEntity(pid, fun = (err, _pid) => { if (err) log.e(err); }) {
cacheInterface.deleteEntity(pid, (err, removedPidArray) => {
//remove ent from EntCache (in RAM)
for (let i = 0; i < removedPidArray.length; i++) {
let entPid = removedPidArray[i];
if (entPid in EntCache) {
delete EntCache[entPid];
}
}
log.v(`Removed ${(removedPidArray.length == 1) ? 'Entity' : 'Entities'
} ${removedPidArray.join(' ')}`);
fun(err, pid);
});
}
|
javascript
|
{
"resource": ""
}
|
q63365
|
saveEntity
|
test
|
async function saveEntity(par, fun = (err, _pid) => { if (err) log.e(err); }) {
let saveEntity = (async (par) => {
await new Promise((res, rej) => {
cacheInterface.saveEntityPar(par, (err, pid) => {
if (err){
log.e(err, 'saving ', pid);
rej(err);
}
log.v(`Saved entity ${par.Pid}`);
res();
});
});
});
//check if the entity is the modules Apex
if (par.Pid != par.Apex) {
//check if the Apex exists in the cache
cacheInterface.getEntityPar(par.Apex, async (err) => {
if (err) {
//get the Apex's par from the EntCache
let apexPar = await new Promise((res, _rej) => {
EntCache[par.Apex].lock((entityContext) => {
res(entityContext.Par);
return entityContext;
});
});
log.v('Must first save the Apex -- Saving...');
await saveEntity(apexPar);
await saveEntity(par);
fun(null, par.Pid);
} else {
//this entity is not the apex and the apex is alread in the cache
await saveEntity(par);
fun(null, par.Pid);
}
});
} else {
await saveEntity(par);
fun(null, par.Pid);
}
}
|
javascript
|
{
"resource": ""
}
|
q63366
|
getFile
|
test
|
function getFile(module, filename, fun = _ => _) {
let mod = ModCache[module];
if (filename in mod.files) {
mod.file(filename).async('string').then((dat) => {
fun(null, dat);
});
return;
}
let err = `Error: File ${filename} does not exist in module ${module}`;
log.e(err);
fun(err);
}
|
javascript
|
{
"resource": ""
}
|
q63367
|
getEntityContext
|
test
|
async function getEntityContext(pid, fun = _ => _) {
EntCache[pid] = new Volatile({});
await EntCache[pid].lock((_entityContext) => {
return new Promise((res, _rej) => {
cacheInterface.getEntityPar(pid, (err, data) => {
let par = JSON.parse(data.toString());
if (err) {
log.e(`Error retrieving a ${data.moduleType} from cache. Pid: ${pid}`);
log.e(err);
fun('Unavailable');
return;
}
let impkey = par.Module + '/' + par.Entity;
if (impkey in ImpCache) {
BuildEnt();
return;
}
GetModule(par.Module, async function (err, mod) {
if (err) {
log.e('Module <' + par.Module + '> not available');
fun('Module not available');
return;
}
if (!(par.Entity in mod.files)) {
log.e('<' + par.Entity + '> not in module <' + par.Module + '>');
fun('Null entity');
return;
}
let entString = await new Promise(async (res, _rej) => {
mod.file(par.Entity).async('string').then((string) => res(string));
});
log.v(`Spinning up entity ${par.Module}-${par.Entity.split('.')[0]}`);
ImpCache[impkey] = indirectEvalImp(impkey, entString, log,
createRequireFromModuleType(par.Module));
BuildEnt();
});
function BuildEnt() {
// TODO: rethink the whole process of having to call out a setup and start
res(new Entity(Nxs, ImpCache[impkey], par, log));
}
});
});
});
fun(null, EntCache[pid]);
}
|
javascript
|
{
"resource": ""
}
|
q63368
|
GetModule
|
test
|
function GetModule(ModName, fun = _ => _) {
ModName = ModName.replace(/:\//g, '.');
if (ModName in ModCache) return fun(null, ModCache[ModName]);
else cacheInterface.getModule(ModName, (err, moduleZip) => {
if (err) return fun(err);
ModCache[ModName] = moduleZip;
return fun(null, ModCache[ModName]);
});
}
|
javascript
|
{
"resource": ""
}
|
q63369
|
processSources
|
test
|
function processSources(cfg) {
if (typeof cfg['Sources'] === 'undefined') {
log.e('You must defined a Sources object.\n');
rejectSetup('You must defined a Sources object.');
return;
}
let val, sources, subval;
for (let key in cfg) {
val = cfg[key];
if (key == 'Sources') {
Config.Sources = {};
sources = cfg['Sources'];
for (let subkey in sources) {
subval = sources[subkey];
switch (typeof subval) {
case 'string': {
Config.Sources[subkey] = Macro(subval);
break;
}
case 'object': {
Config.Sources[subkey] = {};
for (let id in subval) {
Config.Sources[subkey][id.toLowerCase()] =
(typeof subval[id] == 'string') ?
Macro(subval[id]) : subval[id];
}
if (!('port' in Config.Sources[subkey])) {
Config.Sources[subkey]['port'] = 27000;
}
break;
}
default: {
log.e(`Invalid Source ${subkey} of type ${typeof subval}.` +
'Must be of type string or object');
}
}
}
} else {
Config[key] = val;
}
}
}
|
javascript
|
{
"resource": ""
}
|
q63370
|
generateModuleCatalog
|
test
|
function generateModuleCatalog() {
// Create new cache and install high level
// module subdirectories. Each of these also
// has a link to the source of that module (Module.json).
let keys = Object.keys(Config.Modules);
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
if (key == 'Deferred') {
let arr = Config.Modules['Deferred'];
for (let idx = 0; idx < arr.length; idx++) {
let mod = arr[idx];
log.v(`Deferring ${mod.Module || mod}`);
if (typeof mod == 'string') {
log.w('Adding Module names directly to Deferred is deprecated');
log.w(`Deferring { Module: '${mod}' } instead`);
mod = { Module: mod };
}
if (!('Module' in mod)) {
log.e('Malformed Deferred Module listing', mod);
rejectSetup('Malformed Deferred Module listing');
return;
}
logModule(key, mod);
}
} else {
if (typeof Config.Modules[key].Module != 'string') {
log.e('Malformed Module Definition');
log.e(JSON.stringify(Config.Modules[key], null, 2));
}
logModule(key, Config.Modules[key]);
}
}
/**
* Add the module to the Modules object if unique
* @param {object} mod The module object
* @param {string} mod.Module The name of the module
* @param {object, string} mod.Source The Module broker or path reference
*/
function logModule(key, mod) {
let folder = mod.Module.replace(/[/:]/g, '.');
if (!('Source' in mod)) {
log.e(`No Source Declared in module: ${key}: ${mod.Module}`);
rejectSetup(`No Source Declared in module: ${key}`);
return;
}
let source = {
Source: mod.Source,
Version: mod.Version
};
if (!(folder in Modules)) {
Modules[folder] = source;
} else {
if (Modules[folder].Source != source.Source
|| (Modules[folder].Version != source.Version)) {
log.e(`Broker Mismatch Exception: ${key}\n`
+ `${JSON.stringify(Modules[folder], null, 2)} - `
+ `\n${JSON.stringify(source, null, 2)}`);
rejectSetup('Broker Mismatch Exception');
return;
}
}
}
}
|
javascript
|
{
"resource": ""
}
|
q63371
|
logModule
|
test
|
function logModule(key, mod) {
let folder = mod.Module.replace(/[/:]/g, '.');
if (!('Source' in mod)) {
log.e(`No Source Declared in module: ${key}: ${mod.Module}`);
rejectSetup(`No Source Declared in module: ${key}`);
return;
}
let source = {
Source: mod.Source,
Version: mod.Version
};
if (!(folder in Modules)) {
Modules[folder] = source;
} else {
if (Modules[folder].Source != source.Source
|| (Modules[folder].Version != source.Version)) {
log.e(`Broker Mismatch Exception: ${key}\n`
+ `${JSON.stringify(Modules[folder], null, 2)} - `
+ `\n${JSON.stringify(source, null, 2)}`);
rejectSetup('Broker Mismatch Exception');
return;
}
}
}
|
javascript
|
{
"resource": ""
}
|
q63372
|
buildApexInstances
|
test
|
async function buildApexInstances(processPidReferences) {
if (processPidReferences) {
// Assign pids to all instance in Config.Modules
for (let instname in Config.Modules) {
if (instname == 'Deferred')
continue;
Apex[instname] = genPid();
}
log.v('Apex List', JSON.stringify(Apex, null, 2));
}
// Now populate all of the modules from config.json
for (let instname in Config.Modules) {
if (instname === 'Deferred')
continue;
await processApexPar(Apex[instname], Config.Modules[instname], processPidReferences);
}
}
|
javascript
|
{
"resource": ""
}
|
q63373
|
buildDir
|
test
|
async function buildDir(path) {
let dirObj = {};
if (fs.existsSync(path)) {
let files = fs.readdirSync(path);
let itemPromises = [];
for (let file of files) {
itemPromises.push(new Promise(async (resolve) => {
let curPath = path + '/' + file;
if (fs.lstatSync(curPath).isDirectory()) {
// recurse
dirObj[file] = await buildDir(curPath);
resolve();
} else {
fs.readFile(curPath, function (err, data) {
// log.v(curPath.length > 80 ? curPath.substr(0, 35)
// + ' ... ' + curPath.substr(-40, 40) : curPath);
dirObj[file] = data.toString();
resolve();
});
// dirObj[file] = fs.readFileSync(curPath).toString(encoding);
}
}));
}
await Promise.all(itemPromises);
return dirObj;
}
}
|
javascript
|
{
"resource": ""
}
|
q63374
|
genPid
|
test
|
function genPid() {
if (!Uuid) {
// module.paths = [Path.join(Path.resolve(CacheDir), 'node_modules')];
Uuid = require('uuid/v4');
}
let str = Uuid();
let pid = str.replace(/-/g, '').toUpperCase();
return pid;
}
|
javascript
|
{
"resource": ""
}
|
q63375
|
genesis
|
test
|
async function genesis(system) {
log.i(' [Save Cache]'.padStart(80, '='));
log.i('Genesis Compile Start:');
let cacheState = null;
if (fs.existsSync(CacheDir)) cacheState = 'exists';
cacheInterface = new CacheInterface({
path: CacheDir, log
});
cleanCache();
log.i('Saving modules and updating dependencies ...');
await cacheModules(system.ModCache);
if (!(__options.state == 'updateOnly')) {
log.i('Saving entities ...');
await cacheApexes(system.Apex, system.Config.Modules);
}
Stop();
/////////////////////////////////////////////////////////////
//
// Only helper functions beyond this point of this scope
//
/**
* Remove the cache if it currently exists in the given directory
*/
function cleanCache() {
// Remove the provided cache directory
if (__options.state == 'development' && cacheState) {
__options.state = 'updateOnly';
return;
}
log.v('Removing the old cache.');
cacheInterface.clean();
}
/**
* Write the modules to the cache
* @param {Object} ModCache //the set of module zips required for this system
*/
async function cacheModules(ModCache) {
let timer = log.time('cacheModules');
let ModulePromiseArray = [];
for (let folder in ModCache) {
ModulePromiseArray.push(new Promise(async (res) => {
await cacheInterface.addModule(folder, ModCache[folder]);
log.v(`Finished installing dependencies for ${folder}`);
res();
}));
}
await Promise.all(ModulePromiseArray);
log.timeEnd(timer);
}
/**
* Write the module Apexes to the cache
* @param {Object} Apexes //The id:Pid of each apex
* @param {Object} ModuleDefinitions //the id:ModuleDefinition from Config
*/
async function cacheApexes(Apexes, ModuleDefinitions) {
let ModulePromiseArray = [];
for (let moduleId in Apexes) {
ModulePromiseArray.push(
await cacheInterface.createInstance(ModuleDefinitions[moduleId], Apexes[moduleId])
);
}
await Promise.all(ModulePromiseArray);
}
/**
* Resolves the main promise created during genesis call
*/
async function Stop() {
log.i(`Genesis Compile Stop: ${new Date().toString()}`);
log.i(' [Finished]'.padStart(80, '='));
for(const xgrl in BrokerCache) {
const broker = BrokerCache[xgrl];
broker.cleanup();
}
log.timeEnd(compileTimer);
resolveMain();
}
}
|
javascript
|
{
"resource": ""
}
|
q63376
|
cacheModules
|
test
|
async function cacheModules(ModCache) {
let timer = log.time('cacheModules');
let ModulePromiseArray = [];
for (let folder in ModCache) {
ModulePromiseArray.push(new Promise(async (res) => {
await cacheInterface.addModule(folder, ModCache[folder]);
log.v(`Finished installing dependencies for ${folder}`);
res();
}));
}
await Promise.all(ModulePromiseArray);
log.timeEnd(timer);
}
|
javascript
|
{
"resource": ""
}
|
q63377
|
cacheApexes
|
test
|
async function cacheApexes(Apexes, ModuleDefinitions) {
let ModulePromiseArray = [];
for (let moduleId in Apexes) {
ModulePromiseArray.push(
await cacheInterface.createInstance(ModuleDefinitions[moduleId], Apexes[moduleId])
);
}
await Promise.all(ModulePromiseArray);
}
|
javascript
|
{
"resource": ""
}
|
q63378
|
Stop
|
test
|
async function Stop() {
log.i(`Genesis Compile Stop: ${new Date().toString()}`);
log.i(' [Finished]'.padStart(80, '='));
for(const xgrl in BrokerCache) {
const broker = BrokerCache[xgrl];
broker.cleanup();
}
log.timeEnd(compileTimer);
resolveMain();
}
|
javascript
|
{
"resource": ""
}
|
q63379
|
getProtocolModule
|
test
|
function getProtocolModule(protocol) {
return new Promise(function (resolve, reject) {
let cacheFilepath = path.join(appdata, protocol);
if (fs.existsSync(cacheFilepath)) {
return resolve(JSON.parse(fs.readFileSync(cacheFilepath).toString()));
}
let options = {
host: 'protocols.xgraphdev.com',
port: 443,
path: '/' + protocol,
method: 'GET',
rejectUnauthorized: false,
};
let req = https.request(options, function (res) {
res.setEncoding('utf8');
let response = '';
res.on('data', function (chunk) {
response += chunk;
});
res.on('end', _ => {
try {
resolve(JSON.parse(response));
try { fs.writeFileSync(cacheFilepath, response); } catch (e) {
reject({
code: 1,
text: `fail to save protocol at ${cacheFilepath}` +
'\n delete file and try again'
});
}
} catch (e) {
reject({ code: 0, text: 'try and retrieve locally' });
}
});
});
req.on('error', function (e) {
log.e('problem with request: ' + e.message);
reject({ code: 1, text: 'problem with request: ' + e.message });
});
// write data to request body
req.end();
});
}
|
javascript
|
{
"resource": ""
}
|
q63380
|
remDir
|
test
|
function remDir(path) {
return (new Promise(async (resolve, _reject) => {
if (fs.existsSync(path)) {
let files = fs.readdirSync(path);
let promiseArray = [];
for (let fileIndex = 0; fileIndex < files.length; fileIndex++) {
promiseArray.push(new Promise(async (resolve2, _reject2) => {
let curPath = path + '/' + files[fileIndex];
if (fs.lstatSync(curPath).isDirectory()) {
// recurse
await remDir(curPath);
resolve2();
} else {
// delete file
log.v('Removing Entity ', files[fileIndex].split('.')[0]);
fs.unlinkSync(curPath);
resolve2();
}
}));
}
//make sure all the sub files and directories have been removed;
await Promise.all(promiseArray);
log.v('Removing Module Directory ', path);
fs.rmdirSync(path);
resolve();
} else {
log.v('trying to remove nonexistant path ', path);
resolve();
}
}));
}
|
javascript
|
{
"resource": ""
}
|
q63381
|
getMousePosition
|
test
|
function getMousePosition(e) {
var mouseObj = void 0,
originalEvent = e.originalEvent ? e.originalEvent : e;
mouseObj = 'changedTouches' in originalEvent && originalEvent.changedTouches ? originalEvent.changedTouches[0] : originalEvent;
// clientX, Y 쓰면 스크롤에서 문제 발생
return {
clientX: mouseObj.pageX,
clientY: mouseObj.pageY
};
}
|
javascript
|
{
"resource": ""
}
|
q63382
|
proxyRequest
|
test
|
function proxyRequest(req, res, rule) {
var router,
target,
path;
injectProxyHeaders(req, rule);
// rewrite the base path of the requested URL
path = req.url.replace(rule.regexp, rule.target.path);
if (useGateway) {
// for HTTP LAN proxies, rewrite the request URL from a relative URL to an absolute URL
// also add a header that can be inspected by the unit tests
req.url = url.parse(util.format('%s//%s:%s%s', rule.target.protocol, rule.target.host, rule.target.port, path)).href;
req.headers['X-Forwarded-Url'] = req.url;
// the proxy target is really the HTTP LAN proxy
target = config.gateway;
logger.info('proxy: %s %s --> %s:%s --> %s//%s:%s%s', req.method, req.url, config.gateway.host, config.gateway.port, rule.target.protocol, rule.target.host, rule.target.port, path);
} else {
target = rule.target;
logger.info('proxy: %s %s --> %s//%s:%s%s', req.method, req.url, rule.target.protocol, rule.target.host, rule.target.port, path);
req.url = path;
}
var errorCallback = function errorCallback(err, proxyRequest, proxyResponse) {
var status = 500;
if (proxyResponse !== undefined && proxyResponse !== null && proxyResponse.statusCode >= 400) {
status = proxyResponse.statusCode;
}
logger.error('proxy: error - %s %s - %s', proxyRequest.method, proxyRequest.url, err.message);
if( res.status && typeof res.status === 'function' ){
res.status(status).json({ error: status, message: err.message });
}
};
// get a ProxyServer from the cache
router = createRouter(target);
// proxy the request
router.web(req, res, errorCallback);
}
|
javascript
|
{
"resource": ""
}
|
q63383
|
injectProxyHeaders
|
test
|
function injectProxyHeaders(req, rule){
// the HTTP host header is often needed by the target webserver config
req.headers['host'] = rule.target.host + (rule.target.originalPort ? util.format(':%d', rule.target.originalPort) : '');
// document that this request was proxied
req.headers['via'] = util.format('http://%s:%s', req.connection.address().address, req.connection.address().port);
// inject any custom headers as configured
config.headers.forEach(function(header) {
var value = header.value,
name = header.name;
if(typeof(value) === 'function') {
value = value.call(undefined, req);
}
if(typeof(value) !== 'string') {
value = '';
}
if (typeof(name) === 'string') {
req.headers[name.toLowerCase()] = value;
}
});
injectAuthHeader(req);
}
|
javascript
|
{
"resource": ""
}
|
q63384
|
parseFile
|
test
|
function parseFile(filepath, config) {
var contents;
filepath = filepath || path.join(process.cwd(), '/json-proxy.json');
// if we were passed a config file, read and parse it
if (fs.existsSync(filepath)) {
try {
var data = fs.readFileSync(filepath);
contents = JSON.parse(data.toString());
config = parseConfig(contents, config);
// replace the token $config_dir in the webroot arg
if (config.server.webroot && config.server.webroot.length > 0) {
config.server.webroot = config.server.webroot.replace("$config_dir", path.dirname(filepath));
}
} catch (ex) {
throw new Error('Cannot parse the config file "' + filepath + '": ' + ex);
}
}
return config;
}
|
javascript
|
{
"resource": ""
}
|
q63385
|
parseConfig
|
test
|
function parseConfig(contents, config) {
contents.server = contents.server || {};
contents.proxy = contents.proxy || {};
if (contents.proxy.gateway && typeof(contents.proxy.gateway) === "string" && contents.proxy.gateway.length > 0) {
contents.proxy.gateway = parseGateway(contents.proxy.gateway);
}
contents.proxy.forward = parseConfigMap(contents.proxy.forward, parseForwardRule);
contents.proxy.headers = parseConfigMap(contents.proxy.headers, parseHeaderRule);
// override any values in the config object with values specified in the file;
config.server.port = contents.server.port || config.server.port;
config.server.webroot = contents.server.webroot || config.server.webroot;
config.server.html5mode = contents.server.html5mode || config.server.html5mode;
config.proxy.gateway = contents.proxy.gateway || config.proxy.gateway;
config.proxy.forward = contents.proxy.forward || config.proxy.forward;
config.proxy.headers = contents.proxy.headers || config.proxy.headers;
return config;
}
|
javascript
|
{
"resource": ""
}
|
q63386
|
parseConfigMap
|
test
|
function parseConfigMap(map, callback) {
var result = [];
if (!(map instanceof Object)) {
return map;
}
for(var property in map) {
if (map.hasOwnProperty(property)) {
result.push(callback(property, map[property]));
}
}
return result;
}
|
javascript
|
{
"resource": ""
}
|
q63387
|
parseCommandLine
|
test
|
function parseCommandLine(argv, config) {
if (argv) {
// read the command line arguments if no config file was given
parseCommandLineArgument(argv.port, function(item){
config.server.port = item;
});
parseCommandLineArgument(argv.html5mode, function(item){
config.server.html5mode = item;
});
parseCommandLineArgument(argv._, function(item){
config.server.webroot = path.normalize(item);
});
parseCommandLineArgument(argv.gateway, function(item){
config.proxy.gateway = parseGateway(item);
});
parseCommandLineArgument(argv.forward, function(item){
var rule = parseForwardRule(item);
var match = false;
config.proxy.forward.forEach(function(item) {
if (item.regexp.source === rule.regexp.source) {
item.target = rule.target;
match = true;
}
});
if (!match) {
config.proxy.forward.push(rule);
}
});
parseCommandLineArgument(argv.header, function(item){
var rule = parseHeaderRule(item);
var match = false;
config.proxy.headers.forEach(function(item) {
if (item.name === rule.name) {
item.value = rule.value;
match = true;
}
});
if (!match) {
config.proxy.headers.push(rule);
}
});
}
return config;
}
|
javascript
|
{
"resource": ""
}
|
q63388
|
parseCommandLineArgument
|
test
|
function parseCommandLineArgument(arg, fn) {
if (typeof(fn) !== 'function')
return;
if (Array.isArray(arg)) {
arg.forEach(function(item) {
fn.call(null, item);
});
} else {
if (arg !== null && arg !== undefined) {
fn.call(null, arg);
}
}
}
|
javascript
|
{
"resource": ""
}
|
q63389
|
parseForwardRule
|
test
|
function parseForwardRule() {
var token,
rule;
if (arguments[0] === undefined || arguments[0] === null) {
return;
}
if (typeof(arguments[0]) === "object") {
return arguments[0];
}
try {
token = tokenize.apply(null, arguments);
rule = { regexp: new RegExp('^' + token.name, 'i'), target: parseTargetServer(token.value) };
} catch (e) {
throw new Error('cannot parse the forwarding rule ' + arguments[0] + ' - ' + e);
}
return rule;
}
|
javascript
|
{
"resource": ""
}
|
q63390
|
withCode
|
test
|
function withCode(code, msg) {
const err = new Error(msg);
err.code = code;
return err;
}
|
javascript
|
{
"resource": ""
}
|
q63391
|
updateWorkingState
|
test
|
function updateWorkingState(repoState, branch, newWorkingState) {
let workingStates = repoState.getWorkingStates();
const key = branch.getFullName();
if (newWorkingState === null) {
// Delete
workingStates = workingStates.delete(key);
} else {
// Update the entry in the map
workingStates = workingStates.set(key, newWorkingState);
}
return repoState.set('workingStates', workingStates);
}
|
javascript
|
{
"resource": ""
}
|
q63392
|
fetchBranches
|
test
|
function fetchBranches(repoState, driver) {
const oldBranches = repoState.getBranches();
return driver.fetchBranches()
.then((branches) => {
return repoState.set('branches', branches);
})
.then(function refreshWorkingStates(repoState) {
// Remove outdated WorkingStates
return oldBranches.reduce((repoState, oldBranch) => {
const fullName = oldBranch.getFullName();
const newBranch = repoState.getBranch(fullName);
if (newBranch === null || newBranch.getSha() !== oldBranch.getSha()) {
// Was removed OR updated
return updateWorkingState(repoState, oldBranch, null);
} else {
// Unchanged
return repoState;
}
}, repoState);
});
}
|
javascript
|
{
"resource": ""
}
|
q63393
|
initialize
|
test
|
function initialize(driver) {
const repoState = RepositoryState.createEmpty();
return fetchBranches(repoState, driver)
.then((repoState) => {
const branches = repoState.getBranches();
const master = branches.find(function isMaster(branch) {
return branch.getFullName() === 'master';
});
const branch = master || branches.first();
return fetchTree(repoState, driver, branch)
.then((repoState) => {
return checkout(repoState, branch);
});
});
}
|
javascript
|
{
"resource": ""
}
|
q63394
|
enforceArrayBuffer
|
test
|
function enforceArrayBuffer(b, encoding) {
if (isArrayBuffer(b)) return b;
else if (isBuffer(b)) return fromBuffer(b);
else return fromString(b, encoding);
}
|
javascript
|
{
"resource": ""
}
|
q63395
|
enforceString
|
test
|
function enforceString(b, encoding) {
if (is.string(b)) return b;
if (isArrayBuffer(b)) b = toBuffer(b);
return b.toString(encoding);
}
|
javascript
|
{
"resource": ""
}
|
q63396
|
equals
|
test
|
function equals(buf1, buf2) {
if (buf1.byteLength != buf2.byteLength) return false;
const dv1 = new Int8Array(buf1);
const dv2 = new Int8Array(buf2);
for (let i = 0 ; i != buf1.byteLength ; i++) {
if (dv1[i] != dv2[i]) return false;
}
return true;
}
|
javascript
|
{
"resource": ""
}
|
q63397
|
getMergedFileSet
|
test
|
function getMergedFileSet(workingState) {
return Immutable.Set.fromKeys(
getMergedTreeEntries(workingState).filter(
treeEntry => treeEntry.getType() === TreeEntry.TYPES.BLOB
)
);
}
|
javascript
|
{
"resource": ""
}
|
q63398
|
getMergedTreeEntries
|
test
|
function getMergedTreeEntries(workingState) {
const removedOrModified = workingState.getChanges().groupBy((change, path) => {
if (change.getType() === CHANGES.REMOVE) {
return 'remove';
} else {
// Must be UDPATE or CREATE
return 'modified';
}
});
const setToRemove = Immutable.Set.fromKeys(removedOrModified.get('remove', []));
const withoutRemoved = workingState.getTreeEntries().filter((treeEntry, path) => {
return !setToRemove.contains(path);
});
const addedTreeEntries = removedOrModified.get('modified', []).map(
function toTreeEntry(change) {
return new TreeEntry({
sha: change.hasSha() ? change.getSha() : null,
mode: '100644'
});
}
);
return withoutRemoved.concat(addedTreeEntries);
}
|
javascript
|
{
"resource": ""
}
|
q63399
|
findSha
|
test
|
function findSha(workingState, filepath) {
// Lookup potential changes
const change = workingState.getChanges().get(filepath);
// Else lookup tree
const treeEntry = workingState.getTreeEntries().get(filepath);
if (change) {
if (change.getType() == CHANGES.REMOVE) {
throw error.fileNotFound(filepath);
} else {
return change.getSha();
}
} else if (treeEntry) {
return treeEntry.getSha();
} else {
throw error.fileNotFound(filepath);
}
}
|
javascript
|
{
"resource": ""
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.