1
0
Fork 0
peertube/server/tests/utils/miscs/miscs.ts

42 lines
1.0 KiB
TypeScript
Raw Normal View History

import { join } from 'path'
2017-09-04 19:21:47 +00:00
import * as WebTorrent from 'webtorrent'
let webtorrent = new WebTorrent()
2017-12-28 14:25:31 +00:00
function immutableAssign <T, U> (target: T, source: U) {
return Object.assign<{}, T, U>({}, target, source)
}
2017-12-29 10:51:55 +00:00
// Default interval -> 5 minutes
function dateIsValid (dateString: string, interval = 300000) {
2017-09-04 19:21:47 +00:00
const dateToCheck = new Date(dateString)
const now = new Date()
return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
}
function wait (milliseconds: number) {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
if (refreshWebTorrent === true) webtorrent = new WebTorrent()
return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
}
function root () {
// We are in server/tests/utils/miscs
return join(__dirname, '..', '..', '..', '..')
}
2017-09-04 19:21:47 +00:00
// ---------------------------------------------------------------------------
export {
dateIsValid,
wait,
2017-12-28 14:25:31 +00:00
webtorrentAdd,
immutableAssign,
root
2017-09-04 19:21:47 +00:00
}