Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function connectControlChannel(server) {
/* create the control connection */
var opt = null;
var token = null;
var tokenRenewDuration = null;
if (typeof server.options.token === 'function') {
// server.options.token is a function, call it periodically to renew the token
tokenRenewDuration = new moment.duration(1, 'hours');
token = server.options.token();
} else {
// server.options.token is a string, the token cannot be renewed automatically
token = server.options.token;
}
if (token) {
opt = { headers: { 'ServiceBusAuthorization': token } };
}
server.controlChannel = new WebSocket(server.listenUri, null, opt);
// This represents the token renew timer/interval, keep a reference in order to cancel it.
var tokenRenewTimer = null;
server.controlChannel.onerror = function(event) {
// Microseconds and nanoseconds need to be handled first,
// then Moment can take over for all larger units.
if (units === 'ns') {
durationParts.nanoseconds = duration % 1000;
durationParts.microseconds = Math.floor((duration % 1000000) / 1000);
duration = Math.floor(duration / 1000000);
} else if (units === 'mms') {
durationParts.microseconds = duration % 1000;
duration = Math.floor(duration / 1000);
}
let momentUnits = units;
if (units === 'ns' || units === 'mms') {
momentUnits = 'ms';
}
const momentDuration = moment.duration(duration, momentUnits);
// Get the count of each time unit that Moment handles
allUnits
.filterBy('inMoment')
.mapBy('name')
.forEach(unit => {
durationParts[unit] = momentDuration[unit]();
});
// Format each time time bucket as a string
// e.g., { years: 5, seconds: 30 } -> [ '5 years', '30s' ]
const displayParts = allUnits.reduce((parts, unitType) => {
if (durationParts[unitType.name]) {
const count = durationParts[unitType.name];
parts.push(pluralizeUnits(count, unitType, longForm));
}
"moment.duration(String)" : function (test) {
test.expect(8);
test.equal(+moment.duration('P1D'), +moment.duration(1, 'day'), "P1D");
test.equal(+moment.duration('P1W'), +moment.duration(1, 'week'), "P1W");
test.equal(+moment.duration('P1M'), +moment.duration(1, 'month'), "P1M");
test.equal(+moment.duration('P1Y'), +moment.duration(1, 'year'), "P1Y");
test.equal(+moment.duration('PT1S'), +moment.duration(1, 'second'), "PT1S");
test.equal(+moment.duration('PT1M'), +moment.duration(1, 'minute'), "PT1M");
test.equal(+moment.duration('PT1H'), +moment.duration(1, 'hour'), "PT1H");
test.equal(+moment.duration('P1Y1M1DT1H1M1.001S'), +moment.duration({days:1,months:1,years:1,hours:1,minutes:1,seconds:1,milliseconds:1}), "PT1S");
test.done();
},
function timeSpanToDuration(timeSpan) {
const tokens = timeSpan.split(' ');
return moment.duration(parseInt(tokens[0]), tokens[1]);
}
private endTask(state: TaskState, error?: Error)
{
const time = new Date();
const report = this.report;
report.state = state;
report.end = time.toISOString();
report.duration = (time.valueOf() - (new Date(report.start).valueOf())) * 0.001;
const formattedDuration = moment.utc(
moment.duration(report.duration, "seconds").asMilliseconds()).format("HH:mm:ss.SSS");
if (state === "error") {
report.error = error.message;
this.context.logEvent({
time, module: "task", level: "error", sender: this.name,
message: `terminated with error after ${formattedDuration}`
});
}
else if (state === "cancelled") {
this.context.logEvent({
time, module: "task", level: "warning", sender: this.name,
message: `cancelled by user after ${formattedDuration}`
});
}
else {
TimeBuckets.prototype.getScaledDateFormat = function () {
const interval = this.getInterval();
const rules = config.get('dateFormat:scaled');
for (let i = rules.length - 1; i >= 0; i--) {
const rule = rules[i];
if (!rule[0] || interval >= moment.duration(rule[0])) {
return rule[1];
}
}
return config.get('dateFormat');
};
export function* shapeshiftOrderTimeRemaining(): SagaIterator {
while (true) {
let hasShownNotification = false;
while (true) {
yield call(delay, ONE_SECOND);
const swap = yield select(getSwap);
const createdTimeStampMoment = moment(swap.orderTimestampCreatedISOString);
const validUntil = moment(createdTimeStampMoment).add(swap.validFor, 's');
const now = moment();
if (validUntil.isAfter(now)) {
const duration = moment.duration(validUntil.diff(now));
const seconds = duration.asSeconds();
yield put(orderTimeSwap(parseInt(seconds.toString(), 10)));
switch (swap.shapeshiftOrderStatus) {
case 'failed':
yield put(stopPollShapeshiftOrderStatus());
yield put(stopLoadShapeshiftRatesSwap());
yield put(stopOrderTimerSwap());
if (!hasShownNotification) {
hasShownNotification = true;
yield put(showNotification('danger', ORDER_TIMEOUT_MESSAGE, Infinity));
}
break;
case 'received':
yield put(stopOrderTimerSwap());
break;
case 'complete':
windowValid: moment.duration(10, "seconds"),
sampleSize: moment.duration(10, "seconds"),
},
"Past 30 Minutes": {
windowSize: moment.duration(30, "minutes"),
windowValid: moment.duration(30, "seconds"),
sampleSize: moment.duration(30, "seconds"),
},
"Past 1 Hour": {
windowSize: moment.duration(1, "hour"),
windowValid: moment.duration(1, "minute"),
sampleSize: moment.duration(30, "seconds"),
},
"Past 6 Hours": {
windowSize: moment.duration(6, "hours"),
windowValid: moment.duration(5, "minutes"),
sampleSize: moment.duration(1, "minutes"),
},
"Past 1 Day": {
windowSize: moment.duration(1, "day"),
windowValid: moment.duration(10, "minutes"),
sampleSize: moment.duration(5, "minutes"),
},
"Past 2 Days": {
windowSize: moment.duration(2, "day"),
windowValid: moment.duration(10, "minutes"),
sampleSize: moment.duration(5, "minutes"),
},
"Past 3 Days": {
windowSize: moment.duration(3, "day"),
windowValid: moment.duration(10, "minutes"),
sampleSize: moment.duration(5, "minutes"),
.then(data => {
const length = _.get(data, 'data.videos.0.length', null);
return `${context.timestamp} Time Streamed: ${formatDuration(
moment.duration(length, 'seconds'),
)}`;
});
}
export const init = (props: ITimeUtilsInitProps) => {
backgroundDuration = moment
.duration(props.backgroundTime, 'minutes')
.asMilliseconds()
modalDuration = moment.duration(props.modalTime, 'minutes').asMilliseconds()
defaultCallback = props.defaultCallback
hideModal = props.hideModal
onTimerEnd = props.onTimerEnd
}