]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/utils/miscs/miscs.ts
2aac37791ac5d7673ca4cbf382187ffdccf147b2
[github/Chocobozzz/PeerTube.git] / server / tests / utils / miscs / miscs.ts
1 import * as WebTorrent from 'webtorrent'
2
3 let webtorrent = new WebTorrent()
4
5 function immutableAssign <T, U> (target: T, source: U) {
6 return Object.assign<{}, T, U>({}, target, source)
7 }
8
9 // Default interval -> 5 minutes
10 function dateIsValid (dateString: string, interval = 300000) {
11 const dateToCheck = new Date(dateString)
12 const now = new Date()
13
14 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
15 }
16
17 function wait (milliseconds: number) {
18 return new Promise(resolve => setTimeout(resolve, milliseconds))
19 }
20
21 function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
22 if (refreshWebTorrent === true) webtorrent = new WebTorrent()
23
24 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
25 }
26
27 // ---------------------------------------------------------------------------
28
29 export {
30 dateIsValid,
31 wait,
32 webtorrentAdd,
33 immutableAssign
34 }