bittorrent-fetch
Advanced tools
Comparing version 1.0.1 to 1.0.2
17
index.js
@@ -20,3 +20,3 @@ const makeFetch = require('make-fetch') | ||
const SUPPORTED_METHODS = ['GET', 'POST', 'DELETE', 'HEAD'] | ||
const SUPPORTED_METHODS = ['GET', 'PUT', 'DELETE', 'HEAD'] | ||
// const sideType = '-' | ||
@@ -27,3 +27,3 @@ const hostType = '_' | ||
const prog = new Map() | ||
// const prog = new Map() | ||
@@ -118,3 +118,3 @@ // async function getBody (body) { | ||
if (mid.mainPath === '/') { | ||
return {statusCode: 200, headers: {'Content-Type': mainRes, 'Content-Length': String(torrentData.length)}, data: mainReq ? [`<html><head><title>${torrentData.name}</title></head><body><div>${torrentData.files.map(file => { return `<p><a href="/${file.urlPath}">${file.name}</a></p>` })}</div></body></html>`] : [JSON.stringify(torrentData.files.map(file => { return `/${file.urlPath}` }))]} | ||
return {statusCode: 200, headers: {'Content-Type': mainRes, 'Content-Length': String(torrentData.length)}, data: mainReq ? [`<html><head><title>${torrentData.name}</title></head><body><div>${torrentData.files.map(file => { return `<p><a href="${file.urlPath}">${file.name}</a></p>` })}</div></body></html>`] : [JSON.stringify(torrentData.files.map(file => { return `${file.urlPath}` }))]} | ||
} else { | ||
@@ -157,11 +157,2 @@ foundFile = torrentData.files.find(file => { return mid.mainPath === file.urlPath }) | ||
return {statusCode: 200, headers: {'Content-Type': mainRes}, data: mainReq ? [`<html><head><title>${torrentData.name}</title></head><body><div><p>infohash: ${torrentData.infohash}</p><p>title: ${torrentData.title}</p></div></body></html>`] : [JSON.stringify({ hash: torrentData.hash, title: torrentData.title })]} | ||
// if(prog.has(reqHeaders['x-title'])){} | ||
// if(reqHeaders['x-redo'] &&) | ||
// torrentData = await app.publishTorrent(update, {redo: reqHeaders['x-redo'], title: reqHeaders['x-title']}, null, headers, body) | ||
// prog.set(torrentData.title, torrentData.torrent) | ||
// if(!reqHeaders['x-title'] || !checkTitle.test(reqHeaders['x-title'])){ | ||
// return {statusCode: 400, headers: mainRes, data: mainReq ? ['<html><head><title>Bittorrent-Fetch</title></head><body><div><p>must have title</p></div></body></html>'] : [JSON.stringify('must have title')]} | ||
// } else { | ||
// } | ||
} | ||
@@ -178,3 +169,3 @@ } | ||
} else { | ||
const torrentData = await app.publishTorrent(update, {sub: mid.mainHost, title: reqHeaders['x-title']}, count, reqHeaders, body) | ||
const torrentData = await app.publishTorrent(update, {sub: reqHeaders['x-sub'], title: mid.mainHost}, count, reqHeaders, body) | ||
return {statusCode: 200, headers: {'Content-Type': mainRes}, data: mainReq ? [`<html><head><title>${torrentData.name}</title></head><body><div><p>infohash: ${torrentData.infohash}</p><p>title: ${torrentData.title}</p></div></body></html>`] : [JSON.stringify({ hash: torrentData.hash, title: torrentData.title })]} | ||
@@ -181,0 +172,0 @@ } |
63
main.js
const WebTorrent = require('webtorrent') | ||
const fs = require('fs-extra') | ||
const path = require('path') | ||
const crypto = require('crypto') | ||
const sha1 = require('simple-sha1') | ||
@@ -40,4 +39,4 @@ const ed = require('ed25519-supercop') | ||
this._folder = finalOpts.folder | ||
this._storage = this._folder + path.sep + finalOpts.storage | ||
this._author = this._folder + path.sep + finalOpts.author | ||
this._storage = path.join(this._folder, finalOpts.storage) | ||
this._author = path.join(this._folder, finalOpts.author) | ||
if (!fs.pathExistsSync(this._storage)) { | ||
@@ -49,3 +48,12 @@ fs.ensureDirSync(this._storage) | ||
} | ||
this.webtorrent = new WebTorrent({ dht: { verify: ed.verify } }) | ||
this.webtorrent = (() => { | ||
if(finalOpts.webtorrent){ | ||
return finalOpts.webtorrent | ||
} else { | ||
const WebTorrent = require('webtorrent') | ||
return new WebTorrent({ dht: { verify: ed.verify } }) | ||
} | ||
})(finalOpts) | ||
this.webtorrent.on('error', error => { | ||
@@ -62,5 +70,5 @@ console.error(error) | ||
if (this._readyToGo) { | ||
this.keepUpdated().catch(error => { console.error(error) }) | ||
this.keepUpdated().then(data => console.log('routine update had an resolve', data)).catch(error => console.error('routine update had a reject', error)) | ||
} | ||
}, 3600000) | ||
}, 1800000) | ||
} | ||
@@ -80,3 +88,3 @@ | ||
} | ||
await new Promise((resolve, reject) => setTimeout(resolve, 3000)) | ||
await new Promise((resolve, reject) => setTimeout(resolve, 4000)) | ||
} | ||
@@ -90,3 +98,3 @@ for (const torrent of this.webtorrent.torrents) { | ||
} | ||
await new Promise((resolve, reject) => setTimeout(resolve, 3000)) | ||
await new Promise((resolve, reject) => setTimeout(resolve, 4000)) | ||
} | ||
@@ -199,4 +207,6 @@ } | ||
async loadTorrent(id){ | ||
let mainData = this.findTheTorrent(id) | ||
if(mainData) return mainData | ||
const mainData = this.findTheTorrent(id) | ||
if(mainData){ | ||
return mainData | ||
} | ||
@@ -341,3 +351,6 @@ const folderPath = path.join(this._storage, id) | ||
// ]) | ||
await this.handleFormData(folderPath, headers, data) | ||
const additionalData = await this.handleFormData(folderPath, headers, data) | ||
if(additionalData.length){ | ||
await fs.writeFile(path.join(folderPath, Date.now() + '-data.txt'), `Outercon\n\n${additionalData.map(file => {return file.key + ': ' + file.value + '\n'})}`) | ||
} | ||
const checkFolderPath = await fs.readdir(folderPath, { withFileTypes: false }) | ||
@@ -407,3 +420,6 @@ if (!checkFolderPath.length) { | ||
// ]) | ||
await this.handleFormData(folderPath, headers, data) | ||
const additionalData = await this.handleFormData(folderPath, headers, data) | ||
if(additionalData.length){ | ||
await fs.writeFile(path.join(folderPath, Date.now() + '-data.txt'), `Outercon\n\n${additionalData.map(file => {return file.key + ': ' + file.value + '\n'})}`) | ||
} | ||
const checkFolderPath = await fs.readdir(folderPath, { withFileTypes: false }) | ||
@@ -433,2 +449,6 @@ if (!checkFolderPath.length) { | ||
async shredTorrent(id){ | ||
const folderPath = path.join(this._storage, id) | ||
if(!await fs.pathExists(folderPath)){ | ||
throw new Error('did not find any torrent data to delete') | ||
} | ||
const activeTorrent = this.findTheTorrent(id) | ||
@@ -446,8 +466,5 @@ if(activeTorrent){ | ||
} | ||
const folderPath = path.join(this._storage, id) | ||
await fs.remove(folderPath) | ||
const authorPath = path.join(this._author, id) | ||
if(!await fs.pathExists(folderPath)){ | ||
throw new Error('did not find any torrent data to delete') | ||
} | ||
await fs.remove(folderPath) | ||
if(await fs.pathExists(authorPath)){ | ||
@@ -473,3 +490,5 @@ await fs.remove(authorPath) | ||
return new Promise((resolve, reject) => { | ||
const textData = [] | ||
function handleRemoval () { | ||
bb.off('field', handleFields) | ||
bb.off('file', handleFiles) | ||
@@ -479,5 +498,8 @@ bb.off('error', handleErrors) | ||
} | ||
function handleFields (key, value) { | ||
console.log(key, value) | ||
textData.push({key, value}) | ||
} | ||
function handleFiles (name, file, info) { | ||
// fs.writeFile(path.join(folderPath, info.filename), Readable.from(file)) | ||
// const saveTo = fs.createWriteStream(path.join(folderPath, info.filename)) | ||
console.log(name, file, info) | ||
Readable.from(file).pipe(fs.createWriteStream(path.join(folderPath, info.filename))) | ||
@@ -491,4 +513,5 @@ } | ||
handleRemoval() | ||
resolve(null) | ||
resolve(textData) | ||
} | ||
bb.on('field', handleFields) | ||
bb.on('file', handleFiles) | ||
@@ -495,0 +518,0 @@ bb.on('error', handleErrors) |
{ | ||
"name": "bittorrent-fetch", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "coming soon", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "node run.js" | ||
}, | ||
@@ -31,3 +31,7 @@ "repository": { | ||
"webtorrent": "^1.8.13" | ||
}, | ||
"devDependencies": { | ||
"form-data": "^4.0.0", | ||
"tape": "^5.5.3" | ||
} | ||
} |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
38730
5
804
0
2
2
3