Comparing version 1.0.2 to 1.0.5
333
index.js
const makeFetch = require('make-fetch') | ||
const path = require('path') | ||
const fs = require('fs-extra') | ||
const BTP = require('btp-torrent') | ||
const streamToIterator = require('stream-async-iterator') | ||
const mime = require('mime/lite') | ||
const checkHash = new RegExp('^[a-fA-F0-9]{40}$') | ||
const checkKey = new RegExp('^[a-fA-F0-9]{64}$') | ||
const DEFAULT_OPTS = { | ||
dir: __dirname | ||
dir: __dirname, | ||
storage: 'storage', | ||
database: 'database', | ||
magnet: 'magnet', | ||
folder: 'dir' | ||
} | ||
@@ -16,4 +22,24 @@ | ||
const SUPPORTED_METHODS = ['GET', 'PUT', 'DELETE', 'POST', 'PATCH'] | ||
// const sideType = '-' | ||
const hostType = '_' | ||
const app = new BTP({folder: finalOpts.dir, storage: finalOpts.storage, database: finalOpts.database, magnet: finalOpts.magnet, dir: finalOpts.folder}) | ||
const prog = new Map() | ||
app.on('add', data => { | ||
if(prog.has(data.prop)){ | ||
prog.delete(data.prop) | ||
prog.set(data.prop, data.info) | ||
} else { | ||
prog.set(data.prop, data.info) | ||
} | ||
}) | ||
app.on('sub', data => { | ||
if(prog.has(data)){ | ||
prog.delete(data) | ||
} | ||
}) | ||
const fetch = makeFetch(async request => { | ||
@@ -35,5 +61,5 @@ | ||
if((protocol !== 'bt:' || !method || !SUPPORTED_METHODS.includes(method) || !hostname) || (hostname.length !== 1 && hostname.length !== 40 && hostname.length !== 64) || (hostname !== hostType && !checkHash.test(hostname) && !checkKey.test(hostname))){ | ||
if((protocol !== 'bt:' || !method || !SUPPORTED_METHODS.includes(method) || !hostname) || (hostname.length === 1 && hostname !== hostType) || (hostname.length !== 1 && hostname.length !== 40 && hostname.length !== 64) || (hostname.length !== 1 && !checkHash.test(hostname) && !checkKey.test(hostname))){ | ||
console.log('something wrong with the query') | ||
return new Error('invalid query, must be a valid query') | ||
throw new Error('invalid query, must be a valid query') | ||
} | ||
@@ -44,37 +70,100 @@ | ||
let res = {statusCode: 400, headers: {}, data: []} | ||
switch (req.queryMethod) { | ||
switch (req.mainMethod) { | ||
case 'GET': { | ||
let mainData = null | ||
if(req.mainQuery){ | ||
if(req.queryReg){ | ||
mainData = await new Promise((resolve) => { | ||
req.makeQuery.once(found => {resolve(found)}) | ||
}) | ||
if(req.mainType){ | ||
if(!req.mainQuery.length){ | ||
let tempTorrents = app.webtorrent.torrents.length | ||
let tempProperties = app.webproperty.properties.length | ||
if(req.mainReq){ | ||
mainData = ['<html><head><title>Config</title></head><body><div>' + Object.keys(app._status).map(data => {return '<p>' + data + ': ' + app._status[data] + '</p>'}).join('') + '<p>Torrents: ' + tempTorrents + '</p>' + '<p>Properties: ' + tempProperties + '</p><div></body></html>'] | ||
} else { | ||
mainData = [JSON.stringify(app._status)] | ||
} | ||
res.data = mainData | ||
res.statusCode = 200 | ||
res.headers['Content-Type'] = req.mainRes | ||
} else { | ||
let checkClear = null | ||
mainData = await new Promise((resolve) => { | ||
checkClear = setTimeout(() => {resolve({not: false, message: 'timed out, most likely this has data'})}, 5000) | ||
req.makeQuery.not(found => { | ||
resolve({found, not: true, message: 'done, most likely this does not have data'}) | ||
}) | ||
}) | ||
clearTimeout(checkClear) | ||
let tempData = null | ||
if(req.mainQuery.length === 64){ | ||
if(prog.has(req.mainQuery)){ | ||
tempData = prog.get(req.mainQuery) | ||
} else { | ||
tempData = app.findTheAddress(req.mainQuery) | ||
if(tempData){ | ||
throw new Error('could not find address') | ||
} else { | ||
prog.set(req.mainQuery, tempData) | ||
} | ||
} | ||
} else if(req.mainQuery.length === 40){ | ||
if(prog.has(req.mainQuery)){ | ||
tempData = prog.get(req.mainQuery) | ||
} else { | ||
tempData = app.findTheHash(req.mainQuery) | ||
if(tempData){ | ||
throw new Error('could not find hash') | ||
} else { | ||
prog.set(req.mainQuery, tempData) | ||
} | ||
} | ||
} | ||
if(req.mainReq){ | ||
mainData = [`<html><head><title>BT-Fetch</title></head><body><div><p>address: ${tempData.address ? tempData.address : null}</p><p>infohash: ${tempData.infoHash}</p><p>sequence: ${tempData.sequence ? tempData.sequence : null}</p><p>folder: ${tempData.path}</p></div></body></html>`] | ||
} else { | ||
mainData = [JSON.stringify({address: tempData.address ? tempData.address : null, infohash: tempData.infoHash, sequence: tempData.sequence ? tempData.sequence : null, folder: tempData.path})] | ||
} | ||
res.data = mainData | ||
res.statusCode = 200 | ||
res.headers['Content-Type'] = req.mainRes | ||
} | ||
res.statusCode = 200 | ||
res.headers = {} | ||
res.data = typeof(mainData) !== 'undefined' ? [JSON.stringify(mainData)] : [] | ||
if(res.data.length){ | ||
res.headers['Content-Type'] = 'application/json; charset=utf-8' | ||
} | ||
} else { | ||
mainData = {message: 'register a user with the other methods'} | ||
res.statusCode = 200 | ||
res.headers = {} | ||
res.data = typeof(mainData) !== 'undefined' ? [JSON.stringify(mainData)] : [] | ||
if(res.data.length){ | ||
res.headers['Content-Type'] = 'application/json; charset=utf-8' | ||
let tempData = null | ||
if(req.mainQuery.length === 64){ | ||
if(prog.has(req.mainQuery)){ | ||
tempData = prog.get(req.mainQuery) | ||
} else { | ||
tempData = await app.sessionAddress(req.mainQuery) | ||
prog.set(req.mainQuery, tempData) | ||
} | ||
} else if(req.mainQuery.length === 40){ | ||
if(prog.has(req.mainQuery)){ | ||
tempData = prog.get(req.mainQuery) | ||
} else { | ||
tempData = await app.sessionHash(req.mainQuery) | ||
prog.set(req.mainQuery, tempData) | ||
} | ||
} | ||
if(req.mainPath === path.sep){ | ||
if(req.mainReq){ | ||
if(tempData.files.length === 1){ | ||
mainData = [`<html><head><title>BT-Fetch</title></head><body><div>${tempData.files.map(file => {return `<p><a href="bt://${tempData.address ? tempData.address : tempData.infoHash}${file.path.replace(tempData.path, '').replace(/\\/g, '/')}">${file.path}</a> - ${file.name}</p>`})}</div></body></html>`] | ||
} else { | ||
mainData = [`<html><head><title>BT-Fetch</title></head><body><div>${tempData.files.map(file => {return `<p><a href="bt://${tempData.address ? tempData.address : tempData.infoHash}${file.path.replace(tempData.path + path.sep + tempData.name, '').replace(/\\/g, '/')}">${file.path}</a> - ${file.name}</p>`})}</div></body></html>`] | ||
} | ||
} else { | ||
if(tempData.files.length === 1){ | ||
mainData = [JSON.stringify(tempData.files.map(file => {return 'bt://' + tempData.infoHash + '/' + file.path.replace(tempData.path + path.sep, '').replace(/\\/g, '/')}))] | ||
} else { | ||
mainData = [JSON.stringify(tempData.files.map(file => {return 'bt://' + tempData.infoHash + '/' + file.path.replace(tempData.path + path.sep + tempData.name + path.sep, '').replace(/\\/g, '/')}))] | ||
} | ||
} | ||
res.data = mainData | ||
res.statusCode = 200 | ||
res.headers['Content-Type'] = req.mainRes | ||
} else { | ||
// console.log(hostname, req.mainPath) | ||
// console.log(tempData.files.find(file => {return file.path.replace()}))forEach(file => { | ||
// console.log(file.path.replace(tempData.path + path.sep + tempData.name, '')) | ||
// }) | ||
if(tempData.files.length === 1){ | ||
mainData = streamToIterator(tempData.files.find(file => {return file.path.replace(tempData.path, '') === req.mainPath}).createReadStream()) | ||
} else { | ||
mainData = streamToIterator(tempData.files.find(file => {return file.path.replace(tempData.path + path.sep + tempData.name, '') === req.mainPath}).createReadStream()) | ||
} | ||
res.data = mainData | ||
res.statusCode = 200 | ||
res.headers['Content-Type'] = getMimeType(req.mainPath) | ||
} | ||
} | ||
@@ -86,50 +175,64 @@ break | ||
let mainData = null | ||
if(req.mainQuery){ | ||
mainData = await new Promise((resolve) => { | ||
req.makeQuery.put(body).once(found => { | ||
resolve(found) | ||
}) | ||
}) | ||
if(req.mainType){ | ||
if(!body.address || !body.secret){ | ||
let {torrent, folder} = await app.publishHash(body.folder) | ||
if(prog.has(torrent.infoHash)){ | ||
prog.delete(torrent.infoHash) | ||
prog.set(torrent.infoHash, torrent) | ||
} else { | ||
prog.set(torrent.infoHash, torrent) | ||
} | ||
if(req.mainReq){ | ||
mainData = [`<html><head><title>BT-Fetch</title></head><body><div><p>infohash: ${torrent.infoHash}</p><p>folder: ${folder}</p></div></body></html>`] | ||
} else { | ||
mainData = [JSON.stringify({infohash: torrent.infoHash, folder})] | ||
} | ||
} else { | ||
let {torrent, secret} = await app.publishAddress(body.folder, {address: body.address, secret: body.secret}) | ||
if(prog.has(torrent.address)){ | ||
prog.delete(torrent.address) | ||
prog.set(torrent.address, torrent) | ||
} else { | ||
prog.set(torrent.address, torrent) | ||
} | ||
if(req.mainReq){ | ||
mainData = [`<html><head><title>BT-Fetch</title></head><body><div><p>address: ${torrent.address}</p><p>secret: ${secret}</p><p>infohash: ${torrent.infoHash}</p><p>sequence: ${torrent.sequence}</p><p>signature: ${torrent.sig}</p><p>magnet: ${torrent.magnet}</p></div></body></html>`] | ||
} else { | ||
mainData = [JSON.stringify({address: torrent.address, infohash: torrent.infoHash, sequence: torrent.sequence, magnet: torrent.magnet, signature: torrent.sig, secret})] | ||
} | ||
} | ||
res.data = mainData | ||
res.statusCode = 200 | ||
res.headers = {} | ||
res.data = typeof(mainData) !== 'undefined' ? [JSON.stringify(mainData)] : [] | ||
if(res.data.length){ | ||
res.headers['Content-Type'] = 'application/json; charset=utf-8' | ||
} | ||
res.headers['Content-Type'] = req.mainRes | ||
} else { | ||
if(users[body.user]){ | ||
mainData = {err: 'User is currently logged in'} | ||
res.statusCode = 400 | ||
res.headers = {} | ||
res.data = typeof(mainData) !== 'undefined' ? [JSON.stringify(mainData)] : [] | ||
if(res.data.length){ | ||
res.headers['Content-Type'] = 'application/json; charset=utf-8' | ||
if(req.mainQuery.length === 64){ | ||
let torrent = await app.loadAddress(req.mainQuery) | ||
if(prog.has(torrent.address)){ | ||
prog.delete(torrent.address) | ||
prog.set(torrent.address, torrent) | ||
} else { | ||
prog.set(torrent.address, torrent) | ||
} | ||
} else { | ||
users[body.user] = gun.user() | ||
mainData = await new Promise((resolve) => { | ||
users[body.user].auth(body.user, body.pass, ack => { | ||
if(ack.err){ | ||
resolve(ack) | ||
} else { | ||
resolve({soul: ack.soul}) | ||
} | ||
}) | ||
}) | ||
if(mainData.err){ | ||
users[body.user].leave() | ||
delete users[body.user] | ||
res.statusCode = 400 | ||
res.headers = {} | ||
if(req.mainReq){ | ||
mainData = [`<html><head><title>BT-Fetch</title></head><body><div><p>address: ${torrent.address}</p></div></body></html>`] | ||
} else { | ||
res.statusCode = 200 | ||
res.headers = {} | ||
mainData = [JSON.stringify({address: torrent.address})] | ||
} | ||
res.data = typeof(mainData) !== 'undefined' ? [JSON.stringify(mainData)] : [] | ||
if(res.data.length){ | ||
res.headers['Content-Type'] = 'application/json; charset=utf-8' | ||
} else if(req.mainQuery.length === 40){ | ||
let torrent = await app.loadHash(req.mainQuery) | ||
if(prog.has(torrent.infoHash)){ | ||
prog.delete(torrent.infoHash) | ||
prog.set(torrent.infoHash, torrent) | ||
} else { | ||
prog.set(torrent.infoHash, torrent) | ||
} | ||
if(req.mainReq){ | ||
mainData = [`<html><head><title>BT-Fetch</title></head><body><div><p>infohash: ${torrent.infoHash}</p></div></body></html>`] | ||
} else { | ||
mainData = [JSON.stringify({infoHash: torrent.infoHash})] | ||
} | ||
} | ||
res.data = mainData | ||
res.statusCode = 200 | ||
res.headers['Content-Type'] = req.mainRes | ||
} | ||
@@ -141,2 +244,34 @@ break | ||
let mainData = null | ||
if(req.mainType){ | ||
if(!req.mainQuery.length){ | ||
if(body.hash){ | ||
mainData = [await app.removeHash(body.hash)] | ||
} else if(body.address){ | ||
mainData = [await app.removeAddress(body.address)] | ||
} else { | ||
throw new Error('must have hash or address in the body') | ||
} | ||
res.data = mainData | ||
res.statusCode = 200 | ||
res.headers = {} | ||
} else { | ||
if(req.mainQuery.length === 64){ | ||
mainData = [await app.stopAddress(req.mainQuery)] | ||
} else if(req.mainQuery.length === 40){ | ||
mainData = [await app.stopHash(req.mainQuery)] | ||
} | ||
res.data = mainData | ||
res.statusCode = 200 | ||
res.headers = {} | ||
} | ||
} else { | ||
if(req.mainQuery.length === 64){ | ||
mainData = [await app.clearAddress(req.mainQuery)] | ||
} else if(req.mainQuery.length === 40){ | ||
mainData = [await app.clearHash(req.mainQuery)] | ||
} | ||
res.data = mainData | ||
res.statusCode = 200 | ||
res.headers = {} | ||
} | ||
if(req.mainQuery){ | ||
@@ -244,3 +379,3 @@ mainData = await new Promise((resolve) => { | ||
} catch (e) { | ||
return {statusCode: 500, headers, data: [e.stack]} | ||
return {statusCode: 500, headers: {}, data: [e.stack]} | ||
} | ||
@@ -259,34 +394,22 @@ }) | ||
function getMimeType (path) { | ||
let mimeType = mime.getType(path) || 'text/plain' | ||
if (mimeType.startsWith('text/')) mimeType = `${mimeType}; charset=utf-8` | ||
return mimeType | ||
} | ||
function formatReq(hostname, pathname, method, search, headers){ | ||
let path = req.split('/').filter(Boolean) | ||
let count = path.length | ||
let host = decodeURIComponent(req) | ||
let queryType = host[0] === hostType ? host[0] : '' | ||
host = host.replace(queryType, '') | ||
let multiple = count > 1 ? true : false | ||
path = path.map(data => {return decodeURIComponent(data)}).join('.') | ||
let makeQuery = null | ||
let mainQuery = null | ||
if(queryType){ | ||
if(host){ | ||
if(host.includes('.') || host.includes('-') || host.includes('_')){ | ||
makeQuery = multiple ? gun.get('~' + host).path(path) : gun.get('~' + host) | ||
} else if(users[host]){ | ||
makeQuery = multiple ? users[host].path(path) : users[host] | ||
} else if(!users[host]){ | ||
makeQuery = multiple ? gun.get('~@' + host).path(path) : gun.get('~@' + host) | ||
} | ||
mainQuery = true | ||
} else { | ||
makeQuery = host | ||
mainQuery = false | ||
} | ||
} else { | ||
makeQuery = multiple ? gun.get(host).path(path) : gun.get(host) | ||
mainQuery = true | ||
} | ||
let queryMethod = method | ||
let queryProtocol = protocol | ||
let queryReg = !search.get('not') | ||
return {makeQuery, mainQuery, queryMethod, queryProtocol, queryType, queryReg} | ||
// let mainType = hostname[0] === hostType || hostname[0] === sideType ? hostname[0] : '' | ||
let mainType = hostname[0] === hostType | ||
let mainQuery = mainType ? hostname.replace(hostname[0], '') : hostname | ||
let mainHost = hostname | ||
// if(pathname){ | ||
// console.log(decodeURIComponent(pathname)) | ||
// } | ||
let mainPath = pathname ? decodeURIComponent(pathname).replace(/\//g, path.sep) : path.sep | ||
let mainMethod = method | ||
let mainReq = headers.accept && headers.accept.includes('text/html') | ||
let mainRes = mainReq ? 'text/html; charset=utf-8' : 'application/json; charset=utf-8' | ||
let mainReg = !search.get('not') | ||
return {mainQuery, mainHost, mainPath, mainMethod, mainReg, mainReq, mainRes, mainType} | ||
} | ||
@@ -293,0 +416,0 @@ |
{ | ||
"name": "btp-fetch", | ||
"version": "1.0.2", | ||
"version": "1.0.5", | ||
"description": "", | ||
@@ -13,5 +13,9 @@ "main": "index.js", | ||
"dependencies": { | ||
"btp-torrent": "^1.0.1", | ||
"btp-torrent": "^1.0.6", | ||
"fs-extra": "^10.0.0", | ||
"make-fetch": "^2.3.1", | ||
"mime": "^2.6.0", | ||
"stream-async-iterator": "^2.0.0", | ||
"webtorrent": "^1.5.8" | ||
} | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
22668
5
396
6
4
+ Addedfs-extra@^10.0.0
+ Addedmake-fetch@^2.3.1
+ Addedmime@^2.6.0
+ Addedstream-async-iterator@^2.0.0
+ Added@tootallnate/once@1.1.2(transitive)
+ Addedbuffer-from@1.1.2(transitive)
+ Addedconcat-stream@2.0.0(transitive)
+ Addeddebug@3.2.7(transitive)
+ Addedfetch-headers@2.0.0(transitive)
+ Addedfetch-request-body-to-async-iterator@1.0.4(transitive)
+ Addedmake-fetch@2.3.4(transitive)
+ Addedmime@2.6.0(transitive)
+ Addedstatuses@2.0.1(transitive)
+ Addedstream-async-iterator@2.0.0(transitive)
+ Addedtypedarray@0.0.6(transitive)
+ Addedweb-streams-polyfill@3.3.3(transitive)
Updatedbtp-torrent@^1.0.6