1 import * as WebTorrent from 'webtorrent'
2 import { readFile, readdir } from 'fs'
4 let webtorrent = new WebTorrent()
6 function readFilePromise (path: string) {
7 return new Promise<Buffer>((res, rej) => {
8 readFile(path, (err, data) => {
9 if (err) return rej(err)
16 function readdirPromise (path: string) {
17 return new Promise<string[]>((res, rej) => {
18 readdir(path, (err, files) => {
19 if (err) return rej(err)
26 // Default interval -> 2 minutes
27 function dateIsValid (dateString: string, interval = 120000) {
28 const dateToCheck = new Date(dateString)
29 const now = new Date()
31 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
34 function wait (milliseconds: number) {
35 return new Promise(resolve => setTimeout(resolve, milliseconds))
38 function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
39 if (refreshWebTorrent === true) webtorrent = new WebTorrent()
41 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
44 // ---------------------------------------------------------------------------