Comparing version 12.0.9 to 13.0.0
101
index.js
@@ -23,3 +23,2 @@ const WebTorrent = require('webtorrent') | ||
const finalOpts = { ...defOpts, ...opts } | ||
// this._timeout = finalOpts.timeout | ||
this._routine = finalOpts.routine | ||
@@ -83,20 +82,13 @@ this.checkHash = /^[a-fA-F0-9]{40}$/ | ||
async handleTheData(useTimeOut, waitForData, useCaught) { | ||
async resOrRej(res, rej){ | ||
try { | ||
if (useTimeOut.num) { | ||
return await Promise.race([ | ||
new Promise((resolve, reject) => { setTimeout(() => { if (useTimeOut.res) { resolve(`${useTimeOut.id} took too long, it timed out - ${useTimeOut.kind}`) } else { const err = new Error(`${useTimeOut.id} took too long, it timed out - ${useTimeOut.kind}`); err.name = 'ErrorTimeout'; reject(err); } }, useTimeOut.num) }), | ||
waitForData | ||
]) | ||
} else { | ||
return await waitForData | ||
} | ||
return await res | ||
} catch (error) { | ||
if (useCaught.err) { | ||
if (useCaught.cb) { | ||
await useCaught.cb() | ||
} | ||
if (rej) { | ||
// if (useCaught.cb) { | ||
// await useCaught.cb() | ||
// } | ||
throw error | ||
} else { | ||
return useCaught.cb ? await useCaught.cb() : useCaught.cb | ||
return rej | ||
} | ||
@@ -106,2 +98,24 @@ } | ||
// async handleTheData(useTimeOut, waitForData, useCaught) { | ||
// try { | ||
// if (useTimeOut.num) { | ||
// return await Promise.race([ | ||
// new Promise((resolve, reject) => { setTimeout(() => { if (useTimeOut.res) { resolve(`${useTimeOut.id} took too long, it timed out - ${useTimeOut.kind}`) } else { const err = new Error(`${useTimeOut.id} took too long, it timed out - ${useTimeOut.kind}`); err.name = 'ErrorTimeout'; reject(err); } }, useTimeOut.num) }), | ||
// waitForData | ||
// ]) | ||
// } else { | ||
// return await waitForData | ||
// } | ||
// } catch (error) { | ||
// if (useCaught.err) { | ||
// if (useCaught.cb) { | ||
// await useCaught.cb() | ||
// } | ||
// throw error | ||
// } else { | ||
// return useCaught.cb ? await useCaught.cb() : useCaught.cb | ||
// } | ||
// } | ||
// } | ||
// when we resume or seed a user created BEP46 torrent that has already been created before | ||
@@ -288,4 +302,2 @@ // we have to check that the infohash of the torrent(remember we do not know the infohash before) matches the signature | ||
const useTimeout = opts.timeout | ||
if(id.infohash){ | ||
@@ -296,6 +308,6 @@ const testTorrent = this.checkForTorrent(id.infohash, pathToData) | ||
} | ||
const authorStuff = await this.handleTheData({num: 0}, this.db.get(`${this._fixed.seed}${this._fixed.infohash}${id.infohash}`), {err: false, cb: null}) | ||
const authorStuff = await this.resOrRej(this.db.get(`${this._fixed.seed}${this._fixed.infohash}${id.infohash}`), null) | ||
if (authorStuff) { | ||
const folderPath = path.join(this._storage, authorStuff.dir) | ||
const checkTorrent = testTorrent || await this.handleTheData({ id: authorStuff.infohash, num: useTimeout, kind: 'start', res: false }, this.startTorrent(folderPath, { ...authorStuff.desc, destroyStoreOnDestroy: false }), {err: true, cb: null}) | ||
const checkTorrent = testTorrent || await this.resOrRej(this.startTorrent(folderPath, { ...authorStuff.desc, destroyStoreOnDestroy: false }), true) | ||
if(checkTorrent.infoHash !== authorStuff.infohash){ | ||
@@ -315,5 +327,5 @@ this.webtorrent.remove(checkTorrent.infoHash, { destroyStore: false }) | ||
const folderPath = path.join(this._storage, id.infohash) | ||
const checkTorrent = testTorrent || await this.handleTheData({ id: id.infohash, num: useTimeout, kind: 'mid', res: false }, this.midTorrent(id.infohash, { path: folderPath, destroyStoreOnDestroy: false }), { err: true, cb: null}) | ||
const checkTorrent = testTorrent || await this.resOrRej(this.midTorrent(id.infohash, { path: folderPath, destroyStoreOnDestroy: false }), true) | ||
checkTorrent.infohash = checkTorrent.infoHash | ||
await this.handleTheData({num: 0}, this.db.put(`${this._fixed.load}${this._fixed.infohash}${checkTorrent.infohash}`, {size: checkTorrent.length, length: checkTorrent.files.length, infohash: checkTorrent.infohash, name: checkTorrent.name, dir: checkTorrent.dir}), {err: true, cb: async () => { await this.stopTorrent(checkTorrent.infohash, { destroyStore: false }) }}) | ||
await this.resOrRej(this.db.put(`${this._fixed.load}${this._fixed.infohash}${checkTorrent.infohash}`, {size: checkTorrent.length, length: checkTorrent.files.length, infohash: checkTorrent.infohash, name: checkTorrent.name, dir: checkTorrent.dir}), true) | ||
checkTorrent.folder = folderPath | ||
@@ -332,6 +344,6 @@ checkTorrent.address = null | ||
} | ||
const authorStuff = await this.handleTheData({num: 0}, this.db.get(`${this._fixed.seed}${this._fixed.address}${id.address}`), {err: false, cb: null}) | ||
const authorStuff = await this.resOrRej(this.db.get(`${this._fixed.seed}${this._fixed.address}${id.address}`), null) | ||
if(authorStuff){ | ||
const folderPath = path.join(this._storage, authorStuff.dir) | ||
const checkTorrent = testTorrent || await this.handleTheData({ id: authorStuff.address, num: useTimeout, kind: 'start', res: false }, this.startTorrent(folderPath, { ...authorStuff.desc, destroyStoreOnDestroy: true }), {err: true, cb: null}) | ||
const checkTorrent = testTorrent || await this.resOrRej(this.startTorrent(folderPath, { ...authorStuff.desc, destroyStoreOnDestroy: true }), true) | ||
if(checkTorrent.infoHash !== authorStuff.infohash){ | ||
@@ -342,3 +354,3 @@ this.webtorrent.remove(checkTorrent.infoHash, { destroyStore: false }) | ||
const checkProperty = await this.handleTheData({ id: authorStuff.address, num: useTimeout, kind: 'own', res: false }, this.ownData(authorStuff, checkTorrent.infoHash), {err: true, cb: async () => {await this.stopTorrent(checkTorrent.infoHash, { destroyStore: false })}}) | ||
const checkProperty = await this.resOrRej(this.ownData(authorStuff, checkTorrent.infoHash), true) | ||
// don't overwrite the torrent's infohash even though they will both be the same | ||
@@ -358,3 +370,3 @@ checkProperty.folder = folderPath | ||
const checkProperty = await this.handleTheData({ id: id.address, num: useTimeout, kind: 'resolve', res: false }, this.resolveFunc(id.address), {err: true, cb: null}) | ||
const checkProperty = await this.resOrRej(this.resolveFunc(id.address), true) | ||
@@ -368,3 +380,3 @@ checkProperty.folder = folderPath | ||
const checkTorrent = testTorrent || await this.handleTheData({ id: checkProperty.address, num: useTimeout, kind: 'mid', res: false }, this.midTorrent(checkProperty.infohash, { path: dataPath, destroyStoreOnDestroy: false }), { err: true, cb: null }) | ||
const checkTorrent = testTorrent || await this.resOrRej(this.midTorrent(checkProperty.infohash, { path: dataPath, destroyStoreOnDestroy: false }), true) | ||
// don't overwrite the torrent's infohash even though they will both be the same | ||
@@ -374,3 +386,3 @@ for (const prop in checkProperty) { | ||
} | ||
await this.handleTheData({num: 0}, this.db.put(`${this._fixed.load}${this._fixed.address}${checkTorrent.address}`, {size: checkTorrent.length, length: checkTorrent.files.length, address: checkTorrent.address, infohash: checkTorrent.infohash, name: checkTorrent.name}), {err: true, cb: async () => {await this.stopTorrent(checkTorrent.address, { destroyStore: false })}}) | ||
await this.resOrRej(this.db.put(`${this._fixed.load}${this._fixed.address}${checkTorrent.address}`, {size: checkTorrent.length, length: checkTorrent.files.length, address: checkTorrent.address, infohash: checkTorrent.infohash, name: checkTorrent.name}), true) | ||
checkTorrent.own = false | ||
@@ -391,3 +403,2 @@ checkTorrent.dir = null | ||
} | ||
const useTimeout = opts.timeout | ||
if(id.infohash || id.infohash === null){ | ||
@@ -404,3 +415,3 @@ | ||
const saved = Array.isArray(data) ? await this.handleFormData(dataPath, data, pathToData, useTimeout) : await this.handleRegData(dataPath, data, pathToData, useTimeout) | ||
const saved = Array.isArray(data) ? await this.handleFormData(dataPath, data, pathToData) : await this.handleRegData(dataPath, data, pathToData) | ||
const extraFile = path.join(folderPath, 'neta.json') | ||
@@ -433,3 +444,3 @@ if (await fs.pathExists(extraFile)) { | ||
const checkTorrent = await this.handleTheData({ id: 'torrent', num: useTimeout, kind: 'data', res: false }, this.dataFromTorrent(folderPath, authorStuff.desc), {err: true, cb: null}) | ||
const checkTorrent = await this.resOrRej(this.dataFromTorrent(folderPath, authorStuff.desc), true) | ||
@@ -472,3 +483,3 @@ checkTorrent.folder = folderPath | ||
const saved = Array.isArray(data) ? await this.handleFormData(dataPath, data, pathToData, useTimeout) : await this.handleRegData(dataPath, data, pathToData, useTimeout) | ||
const saved = Array.isArray(data) ? await this.handleFormData(dataPath, data, pathToData) : await this.handleRegData(dataPath, data, pathToData) | ||
const extraFile = path.join(folderPath, 'neta.json') | ||
@@ -501,5 +512,5 @@ if (await fs.pathExists(extraFile)) { | ||
const checkTorrent = await this.handleTheData({ id: 'torrent', num: useTimeout, kind: 'data', res: false }, this.dataFromTorrent(folderPath, authorStuff.desc), {err: true, cb: null}) | ||
const checkTorrent = await this.resOrRej(this.dataFromTorrent(folderPath, authorStuff.desc), true) | ||
const checkProperty = await this.handleTheData({ id: authorStuff.address, num: useTimeout, kind: 'publish', res: false }, this.publishFunc(authorStuff.address, id.secret, { ih: checkTorrent.infoHash, ...authorStuff.stuff }, authorStuff.sequence), {err: true, cb: null}) | ||
const checkProperty = await this.resOrRej(this.publishFunc(authorStuff.address, id.secret, { ih: checkTorrent.infoHash, ...authorStuff.stuff }, authorStuff.sequence), true) | ||
// don't overwrite the torrent's infohash even though they will both be the same | ||
@@ -526,4 +537,2 @@ // checkProperty.folder = folderPath | ||
const useTimeout = opts.timeout | ||
if(info.infohash){ | ||
@@ -534,5 +543,5 @@ if(this.checkId.has(info.infohash)){ | ||
const activeTorrent = await this.handleTheData({ id: info.infohash, num: useTimeout, kind: 'stop', res: false }, this.stopTorrent(info.infohash, { destroyStore: false }), {err: true, cb: null}) | ||
const activeTorrent = await this.resOrRej(this.stopTorrent(info.infohash, { destroyStore: false }), true) | ||
const authorStuff = await this.handleTheData({num: 0}, this.db.get(`${this._fixed.seed}${this._fixed.infohash}${info.infohash}`), {err: false, cb: null}) | ||
const authorStuff = await this.resOrRej(this.db.get(`${this._fixed.seed}${this._fixed.infohash}${info.infohash}`), false) | ||
if(authorStuff){ | ||
@@ -566,3 +575,3 @@ const folderPath = path.join(this._storage, authorStuff.dir) | ||
const dataFromFolder = await this.handleTheData({ id: authorStuff.infohash, num: useTimeout, kind: 'torrent', res: false }, this.dataFromTorrent(folderPath, authorStuff.desc), {err: true, cb: async () => {await fs.remove(folderPath);await this.db.del(`${this._fixed.seed}${this._fixed.infohash}${authorStuff.infohash}`);}}) | ||
const dataFromFolder = await this.resOrRej(this.dataFromTorrent(folderPath, authorStuff.desc), true) | ||
@@ -615,5 +624,5 @@ if (authorStuff.infohash !== dataFromFolder.infoHash) { | ||
} | ||
const activeTorrent = await this.handleTheData({ id: info.address, num: useTimeout, kind: 'stop', res: false }, this.stopTorrent(info.address, { destroyStore: false }), {err: true, cb: null}) | ||
const activeTorrent = await this.resOrRej(this.stopTorrent(info.address, { destroyStore: false }), true) | ||
const authorStuff = await this.handleTheData({num: 0}, this.db.get(`${this._fixed.seed}${this._fixed.address}${info.address}`), {err: false, cb: null}) | ||
const authorStuff = await this.resOrRej(this.db.get(`${this._fixed.seed}${this._fixed.address}${info.address}`), false) | ||
if(authorStuff){ | ||
@@ -650,5 +659,5 @@ const folderPath = path.join(this._storage, authorStuff.dir) | ||
const dataFromFolder = await this.handleTheData({ id: authorStuff.address, num: useTimeout, kind: 'torrent', res: false }, this.dataFromTorrent(folderPath, authorStuff.desc), {err: true, cb: async () => {await fs.remove(folderPath);await this.db.del(`${this._fixed.seed}${this._fixed.address}${authorStuff.address}`);}}) | ||
const dataFromFolder = await this.resOrRej(this.dataFromTorrent(folderPath, authorStuff.desc), true) | ||
const dataFromProp = await this.handleTheData({id: authorStuff.address, num: 0, kind: 'publish', res: false}, this.publishFunc(authorStuff.address, info.secret, {...authorStuff.stuff, ih: dataFromFolder.infoHash}, authorStuff.sequence + 1), {err: true, cb: null}) | ||
const dataFromProp = await this.resOrRej(this.publishFunc(authorStuff.address, info.secret, {...authorStuff.stuff, ih: dataFromFolder.infoHash}, authorStuff.sequence + 1), true) | ||
dataFromProp.dir = authorStuff.dir | ||
@@ -808,3 +817,3 @@ dataFromProp.desc = authorStuff.desc | ||
async handleFormData(folderPath, data, fullPath, sec) { | ||
async handleFormData(folderPath, data, fullPath) { | ||
await fs.ensureDir(folderPath) | ||
@@ -815,3 +824,3 @@ const arr = [] | ||
const tempPath = path.join(folderPath, useName) | ||
await this.handleTheData({ id: tempPath, num: sec, res: false, kind: 'formdata' }, pipelinePromise(Readable.from(info.stream()), fs.createWriteStream(tempPath)), {err: true, cb: null}) | ||
await this.resOrRej(pipelinePromise(Readable.from(info.stream()), fs.createWriteStream(tempPath)), true) | ||
arr.push(path.join(fullPath, useName).replace(/\\/g, "/")) | ||
@@ -822,5 +831,5 @@ } | ||
async handleRegData(mainPath, body, fullPath, sec) { | ||
async handleRegData(mainPath, body, fullPath) { | ||
await fs.ensureDir(path.dirname(mainPath)) | ||
await this.handleTheData({ id: mainPath, num: sec, res: false, kind: 'regdata' }, pipelinePromise(Readable.from(body), fs.createWriteStream(mainPath)), {err: true, cb: null}) | ||
await this.resOrRej(pipelinePromise(Readable.from(body), fs.createWriteStream(mainPath)), true) | ||
return fullPath | ||
@@ -827,0 +836,0 @@ } |
{ | ||
"name": "torrentz", | ||
"version": "12.0.9", | ||
"version": "13.0.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
779
37253