list-fetch
Advanced tools
Comparing version 26.0.3 to 26.0.4
79
index.js
module.exports = async function makeBTFetch (opts = {}) { | ||
const {makeRoutedFetch} = await import('make-fetch') | ||
const {fetch, router} = makeRoutedFetch() | ||
const streamToIterator = require('stream-async-iterator') | ||
const {fetch, router} = makeRoutedFetch({onNotFound: handleEmpty, onError: handleError}) | ||
// const streamToIterator = require('stream-async-iterator') | ||
const mime = require('mime/lite') | ||
@@ -17,2 +17,3 @@ const parseRange = require('range-parser') | ||
const hostType = '_' | ||
const btTimeout = 30000 | ||
@@ -23,2 +24,22 @@ const app = await new Promise((resolve) => {if(finalOpts.torrentz){resolve(finalOpts.torrentz)}else{resolve(new Torrentz(finalOpts))}}) | ||
function handleEmpty(request) { | ||
const { url, headers: reqHeaders, method, body, signal } = request | ||
if(signal){ | ||
signal.removeEventListener('abort', takeCareOfIt) | ||
} | ||
const mainReq = !reqHeaders.has('accept') || !reqHeaders.get('accept').includes('application/json') | ||
const mainRes = mainReq ? 'text/html; charset=utf-8' : 'application/json; charset=utf-8' | ||
return {status: 400, headers: { 'Content-Type': mainRes }, body: mainReq ? `<html><head><title>${url}</title></head><body><div><p>did not find any data</p></div></body></html>` : JSON.stringify('did not find any data')} | ||
} | ||
function handleError(e, request) { | ||
const { url, headers: reqHeaders, method, body, signal } = request | ||
if(signal){ | ||
signal.removeEventListener('abort', takeCareOfIt) | ||
} | ||
const mainReq = !reqHeaders.has('accept') || !reqHeaders.get('accept').includes('application/json') | ||
const mainRes = mainReq ? 'text/html; charset=utf-8' : 'application/json; charset=utf-8' | ||
return {status: 500, headers: { 'X-Error': e.name, 'Content-Type': mainRes }, body: mainReq ? `<html><head><title>${e.name}</title></head><body><div><p>${e.stack}</p></div></body></html>` : JSON.stringify(e.stack)} | ||
} | ||
function handleFormData(formdata) { | ||
@@ -206,17 +227,18 @@ const arr = [] | ||
const torrentData = await app.torrentData(JSON.parse(reqHeaders.get('x-data') || searchParams.get('x-data'))) | ||
return sendTheData(signal, {status: 200, headers: {'X-Length': `${torrentData.length}`}, body: []}) | ||
return sendTheData(signal, {status: 200, headers: {'X-Length': `${torrentData.length}`}, body: ''}) | ||
} else { | ||
const torrentData = await app.authorData() | ||
return sendTheData(signal, {status: 200, headers: {'X-Length': `${torrentData.length}`}, body: []}) | ||
return sendTheData(signal, {status: 200, headers: {'X-Length': `${torrentData.length}`}, body: ''}) | ||
} | ||
} else { | ||
const useOpts = { timeout: reqHeaders.has('x-timer') || searchParams.has('x-timer') ? reqHeaders.get('x-timer') !== '0' || searchParams.get('x-timer') !== '0' ? Number(reqHeaders.get('x-timer') || searchParams.get('x-timer')) * 1000 : undefined : btTimeout } | ||
if (reqHeaders.has('x-copy') || searchParams.has('x-copy')) { | ||
try { | ||
const torrentData = await app.userTorrent(mid.mainId, mid.mainPath, {id: JSON.parse(reqHeaders.get('x-copy') || searchParams.get('x-copy'))}) | ||
return sendTheData(signal, {status: 200, headers: {'X-Path': torrentData}, body: []}) | ||
const torrentData = await app.userTorrent(mid.mainId, mid.mainPath, {...useOpts, id: JSON.parse(reqHeaders.get('x-copy') || searchParams.get('x-copy'))}) | ||
return sendTheData(signal, {status: 200, headers: {'X-Path': torrentData}, body: ''}) | ||
} catch (error) { | ||
return sendTheData(signal, {status: 400, headers: {'X-Error': error.name}, data: []}) | ||
return sendTheData(signal, {status: 400, headers: {'X-Error': error.name}, body: ''}) | ||
} | ||
} else { | ||
const torrentData = await app.loadTorrent(mid.mainId, mid.mainPath, {timeout: (reqHeaders.has('x-timer') && reqHeaders.get('x-timer') !== '0') || (searchParams.has('x-timer') && searchParams.get('x-timer') !== '0') ? Number(reqHeaders.get('x-timer') || searchParams.get('x-timer')) : 0}) | ||
const torrentData = await app.loadTorrent(mid.mainId, mid.mainPath, useOpts) | ||
if (torrentData) { | ||
@@ -232,3 +254,3 @@ if(torrentData.infoHash){ | ||
return sendTheData(signal, {status: 200, headers: useHeaders, body: []}) | ||
return sendTheData(signal, {status: 200, headers: useHeaders, body: ''}) | ||
} else if(Array.isArray(torrentData)){ | ||
@@ -238,3 +260,3 @@ let checkLength = 0 | ||
sendTheData(signal, {status: 200, headers: {'X-Link': `bt://${mid.mainHost}${mid.mainPath}`, 'Link': `<bt://${mid.mainHost}${mid.mainPath}>; rel="canonical"`,'Content-Length': String(checkLength)}, body: []}) | ||
sendTheData(signal, {status: 200, headers: {'X-Link': `bt://${mid.mainHost}${mid.mainPath}`, 'Link': `<bt://${mid.mainHost}${mid.mainPath}>; rel="canonical"`,'Content-Length': String(checkLength)}, body: ''}) | ||
} else { | ||
@@ -249,6 +271,6 @@ const useHeaders = {} | ||
return sendTheData(signal, {status: 200, headers: useHeaders, body: []}) | ||
return sendTheData(signal, {status: 200, headers: useHeaders, body: ''}) | ||
} | ||
} else { | ||
return sendTheData(signal, {status: 400, headers: {'X-Error': 'did not find any data'}, body: []}) | ||
return sendTheData(signal, {status: 400, headers: {'X-Error': 'did not find any data'}, body: ''}) | ||
} | ||
@@ -276,20 +298,21 @@ } | ||
const torrentData = await app.torrentData(JSON.parse(reqHeaders.get('x-data') || searchParams.get('x-data'))) | ||
return sendTheData(signal, {status: 200, headers: {'Content-Type': mainRes}, body: mainReq ? [`<html><head><title>${mid.mainLink}</title></head><body><div>${torrentData.length ? torrentData.map(htmlIden) : '<p>there are no data</p>'}</div></body></html>`] : [JSON.stringify(torrentData.map(jsonIden))]}) | ||
return sendTheData(signal, {status: 200, headers: {'Content-Type': mainRes}, body: mainReq ? `<html><head><title>${mid.mainLink}</title></head><body><div>${torrentData.length ? torrentData.map(htmlIden) : '<p>there are no data</p>'}</div></body></html>` : JSON.stringify(torrentData.map(jsonIden))}) | ||
} else { | ||
const torrentData = await app.authorData() | ||
return sendTheData(signal, {status: 200, headers: {'Content-Type': mainRes}, body: mainReq ? [`<html><head><title>${mid.mainLink}</title></head><body><div>${torrentData.length ? torrentData.map((data) => {if(data.address){data.link = `<a href='bt://${data.address}/'>${data.address}</a>`} else if(data.title){data.link = `<a href='bt://${data.infohash}/'>${data.infohash}</a>`} return `<p>${JSON.stringify(data)}</p>`;}) : '<p>there are no data</p>'}</div></body></html>`] : [JSON.stringify(torrentData.map((data) => {if(data.address){data.link = `bt://${data.address}/`} else if(data.title){data.link = `bt://${data.infohash}/`} return data;}))]}) | ||
return sendTheData(signal, {status: 200, headers: {'Content-Type': mainRes}, body: mainReq ? `<html><head><title>${mid.mainLink}</title></head><body><div>${torrentData.length ? torrentData.map((data) => {if(data.address){data.link = `<a href='bt://${data.address}/'>${data.address}</a>`} else if(data.title){data.link = `<a href='bt://${data.infohash}/'>${data.infohash}</a>`} return `<p>${JSON.stringify(data)}</p>`;}) : '<p>there are no data</p>'}</div></body></html>` : JSON.stringify(torrentData.map((data) => {if(data.address){data.link = `bt://${data.address}/`} else if(data.title){data.link = `bt://${data.infohash}/`} return data;}))}) | ||
} | ||
} else { | ||
const torrentData = await app.loadTorrent(mid.mainId, mid.mainPath, {timeout: (reqHeaders.has('x-timer') && reqHeaders.get('x-timer') !== '0') || (searchParams.has('x-timer') && searchParams.get('x-timer') !== '0') ? Number(reqHeaders.get('x-timer') || searchParams.get('x-timer')) : 0}) | ||
const useOpts = { timeout: reqHeaders.has('x-timer') || searchParams.has('x-timer') ? reqHeaders.get('x-timer') !== '0' || searchParams.get('x-timer') !== '0' ? Number(reqHeaders.get('x-timer') || searchParams.get('x-timer')) * 1000 : undefined : btTimeout } | ||
const torrentData = await app.loadTorrent(mid.mainId, mid.mainPath, useOpts) | ||
if(torrentData){ | ||
if(torrentData.infoHash){ | ||
return sendTheData(signal, {status: 200, headers: {'X-Link': `bt://${mid.mainHost}${mid.mainPath}`, 'Link': `<bt://${mid.mainHost}${mid.mainPath}>; rel="canonical"`, 'Content-Type': mainRes, 'Content-Length': String(torrentData.length)}, body: mainReq ? [`<html><head><title>${mid.mainLink}</title></head><body><div><h1>${torrentData.infohash}</h1>${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 sendTheData(signal, {status: 200, headers: {'X-Link': `bt://${mid.mainHost}${mid.mainPath}`, 'Link': `<bt://${mid.mainHost}${mid.mainPath}>; rel="canonical"`, 'Content-Type': mainRes, 'Content-Length': String(torrentData.length)}, body: mainReq ? `<html><head><title>${mid.mainLink}</title></head><body><div><h1>${torrentData.infohash}</h1>${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 if (Array.isArray(torrentData)) { | ||
let checkLength = 0 | ||
torrentData.forEach((data) => {checkLength = checkLength + data.length}) | ||
return sendTheData(signal, {status: 200, headers: {'X-Link': `bt://${mid.mainHost}${mid.mainPath}`, 'Link': `<bt://${mid.mainHost}${mid.mainPath}>; rel="canonical"`, 'Content-Type': mainRes, 'Content-Length': String(checkLength)}, body: mainReq ? [`<html><head><title>${mid.mainLink}</title></head><body><div><h1>Directory</h1><p><a href='../'>..</a></p>${torrentData.map(file => { return `<p><a href='${file.urlPath}'>${file.name}</a></p>` })}</div></body></html>`] : [JSON.stringify(torrentData.map(file => { return file.urlPath }))]}) | ||
return sendTheData(signal, {status: 200, headers: {'X-Link': `bt://${mid.mainHost}${mid.mainPath}`, 'Link': `<bt://${mid.mainHost}${mid.mainPath}>; rel="canonical"`, 'Content-Type': mainRes, 'Content-Length': String(checkLength)}, body: mainReq ? `<html><head><title>${mid.mainLink}</title></head><body><div><h1>Directory</h1><p><a href='../'>..</a></p>${torrentData.map(file => { return `<p><a href='${file.urlPath}'>${file.name}</a></p>` })}</div></body></html>` : JSON.stringify(torrentData.map(file => { return file.urlPath }))}) | ||
} else { | ||
const mainRange = reqHeaders.has('Range') || reqHeaders.has('range') | ||
if (mainRange) { | ||
const ranges = parseRange(torrentData.length, mainRange) | ||
const ranges = parseRange(torrentData.length, reqHeaders.get('Range') || reqHeaders.get('range')) | ||
if (ranges && ranges.length && ranges.type === 'bytes') { | ||
@@ -299,12 +322,12 @@ const [{ start, end }] = ranges | ||
return sendTheData(signal, {status: 206, headers: {'X-Link': `bt://${mid.mainHost}${mid.mainPath}`, 'Link': `<bt://${mid.mainHost}${mid.mainPath}>; rel="canonical"`, 'Content-Length': `${length}`, 'Content-Range': `bytes ${start}-${end}/${torrentData.length}`, 'Content-Type': getMimeType(torrentData.path)}, body: streamToIterator(torrentData.createReadStream({ start, end }))}) | ||
return sendTheData(signal, {status: 206, headers: {'X-Link': `bt://${mid.mainHost}${mid.mainPath}`, 'Link': `<bt://${mid.mainHost}${mid.mainPath}>; rel="canonical"`, 'Content-Length': `${length}`, 'Content-Range': `bytes ${start}-${end}/${torrentData.length}`, 'Content-Type': getMimeType(torrentData.path)}, body: torrentData.createReadStream({ start, end })}) | ||
} else { | ||
return sendTheData(signal, {status: 416, headers: {'Content-Type': mainRes, 'Content-Length': String(torrentData.length)}, body: mainReq ? ['<html><head><title>range</title></head><body><div><p>malformed or unsatisfiable range</p></div></body></html>'] : [JSON.stringify('malformed or unsatisfiable range')]}) | ||
return sendTheData(signal, {status: 416, headers: {'Content-Type': mainRes, 'Content-Length': String(torrentData.length)}, body: mainReq ? '<html><head><title>range</title></head><body><div><p>malformed or unsatisfiable range</p></div></body></html>' : JSON.stringify('malformed or unsatisfiable range')}) | ||
} | ||
} else { | ||
return sendTheData(signal, {status: 200, headers: {'Content-Type': getMimeType(torrentData.path), 'X-Link': `bt://${mid.mainHost}${mid.mainPath}`, 'Link': `<bt://${mid.mainHost}${mid.mainPath}>; rel="canonical"`, 'Content-Length': String(torrentData.length)}, body: streamToIterator(torrentData.createReadStream())}) | ||
return sendTheData(signal, {status: 200, headers: {'Content-Type': getMimeType(torrentData.path), 'X-Link': `bt://${mid.mainHost}${mid.mainPath}`, 'Link': `<bt://${mid.mainHost}${mid.mainPath}>; rel="canonical"`, 'Content-Length': String(torrentData.length)}, body: torrentData.createReadStream()}) | ||
} | ||
} | ||
} else { | ||
return sendTheData(signal, {status: 400, headers: {'Content-Type': mainRes}, body: mainReq ? [`<html><head><title>${mid.mainLink}</title></head><body><div><p>could not find the data</p></div></body></html>`] : [JSON.stringify('could not find the data')]}) | ||
return sendTheData(signal, {status: 400, headers: {'Content-Type': mainRes}, body: mainReq ? `<html><head><title>${mid.mainLink}</title></head><body><div><p>could not find the data</p></div></body></html>` : JSON.stringify('could not find the data')}) | ||
} | ||
@@ -331,3 +354,2 @@ } | ||
count: reqHeaders.has('x-version') || searchParams.has('x-version') ? Number(reqHeaders.get('x-version') || searchParams.get('x-version')) : null, | ||
timeout: (reqHeaders.has('x-timer') && reqHeaders.get('x-timer') !== '0') || (searchParams.has('x-timer') && searchParams.get('x-timer') !== '0') ? Number(reqHeaders.get('x-timer') || searchParams.get('x-timer')) : 0, | ||
opt: reqHeaders.has('x-opt') || searchParams.has('x-opt') ? JSON.parse(reqHeaders.get('x-opt') || decodeURIComponent(searchParams.get('x-opt'))) : null | ||
@@ -343,7 +365,6 @@ } | ||
} | ||
return sendTheData(signal, {status: 200, headers: {'X-Link': `bt://${mid.mainHost}${mid.mainPath}`, 'Link': `<bt://${mid.mainHost}${mid.mainPath}>; rel="canonical"`, 'Content-Length': String(torrentData.length), 'Content-Type': mainRes, ...useHeaders}, body: mainReq ? [`<html><head><title>${mid.mainLink}</title></head><body><div>${JSON.stringify(torrentData.saved)}</div></body></html>`] : [JSON.stringify(torrentData.saved)]}) | ||
return sendTheData(signal, {status: 200, headers: {'X-Link': `bt://${mid.mainHost}${mid.mainPath}`, 'Link': `<bt://${mid.mainHost}${mid.mainPath}>; rel="canonical"`, 'Content-Length': String(torrentData.length), 'Content-Type': mainRes, ...useHeaders}, body: mainReq ? `<html><head><title>${mid.mainLink}</title></head><body><div>${JSON.stringify(torrentData.saved)}</div></body></html>` : JSON.stringify(torrentData.saved)}) | ||
} else { | ||
const useOpts = { | ||
count: reqHeaders.has('x-version') || searchParams.has('x-version') ? Number(reqHeaders.get('x-version') || searchParams.get('x-version')) : null, | ||
timeout: (reqHeaders.has('x-timer') && reqHeaders.get('x-timer') !== '0') || (searchParams.has('x-timer') && searchParams.get('x-timer') !== '0') ? Number(reqHeaders.get('x-timer') || searchParams.get('x-timer')) : 0, | ||
opt: reqHeaders.has('x-opt') || searchParams.has('x-opt') ? JSON.parse(reqHeaders.get('x-opt') || decodeURIComponent(searchParams.get('x-opt'))) : null | ||
@@ -359,3 +380,3 @@ } | ||
} | ||
return sendTheData(signal, { status: 200, headers: { 'X-Link': `bt://${mid.mainHost}${mid.mainPath}`, 'Link': `<bt://${mid.mainHost}${mid.mainPath}>; rel="canonical"`, 'Content-Length': String(torrentData.length), 'Content-Type': mainRes, ...useHeaders }, body: mainReq ? [`<html><head><title>${mid.mainLink}</title></head><body><div>${JSON.stringify(torrentData.saved)}</div></body></html>`] : [JSON.stringify(torrentData.saved)] }) | ||
return sendTheData(signal, { status: 200, headers: { 'X-Link': `bt://${mid.mainHost}${mid.mainPath}`, 'Link': `<bt://${mid.mainHost}${mid.mainPath}>; rel="canonical"`, 'Content-Length': String(torrentData.length), 'Content-Type': mainRes, ...useHeaders }, body: mainReq ? `<html><head><title>${mid.mainLink}</title></head><body><div>${JSON.stringify(torrentData.saved)}</div></body></html>` : JSON.stringify(torrentData.saved) }) | ||
} | ||
@@ -379,5 +400,5 @@ } | ||
if (mid.mainQuery) { | ||
return sendTheData(signal, { status: 400, headers: { 'Content-Type': mainRes }, body: mainReq ? [`<html><head><title>${mid.mainLink}</title></head><body><div><p>must not use underscore</p></div></body></html>`] : [JSON.stringify('must not use underscore')] }) | ||
return sendTheData(signal, { status: 400, headers: { 'Content-Type': mainRes }, body: mainReq ? `<html><head><title>${mid.mainLink}</title></head><body><div><p>must not use underscore</p></div></body></html>` : JSON.stringify('must not use underscore') }) | ||
} else { | ||
const torrentData = await app.shredTorrent(mid.mainId, mid.mainPath, {timeout: (reqHeaders.has('x-timer') && reqHeaders.get('x-timer') !== '0') || (searchParams.has('x-timer') && searchParams.get('x-timer') !== '0') ? Number(reqHeaders.get('x-timer') || Number(searchParams.get('x-timer'))) : 0}) | ||
const torrentData = await app.shredTorrent(mid.mainId, mid.mainPath, {}) | ||
const useHead = {} | ||
@@ -390,3 +411,3 @@ for (const test of ['id', 'path', 'infohash', 'title', 'address']) { | ||
return sendTheData(signal, {status: 200, headers: {'Content-Type': mainRes, ...useHead}, body: mainReq ? [`<html><head><title>${mid.mainLink}</title></head><body><div>${JSON.stringify(torrentData)}</div></body></html>`] : [JSON.stringify(torrentData)]}) | ||
return sendTheData(signal, {status: 200, headers: {'Content-Type': mainRes, ...useHead}, body: mainReq ? `<html><head><title>${mid.mainLink}</title></head><body><div>${JSON.stringify(torrentData)}</div></body></html>` : JSON.stringify(torrentData)}) | ||
} | ||
@@ -393,0 +414,0 @@ } |
{ | ||
"name": "list-fetch", | ||
"version": "26.0.3", | ||
"version": "26.0.4", | ||
"description": "coming soon", | ||
@@ -24,4 +24,3 @@ "main": "index.js", | ||
"range-parser": "^1.2.1", | ||
"stream-async-iterator": "^2.0.0", | ||
"torrentz": "^8.0.4" | ||
"torrentz": "^8.0.6" | ||
}, | ||
@@ -28,0 +27,0 @@ "devDependencies": { |
23164
4
403
- Removedstream-async-iterator@^2.0.0
- Removed@tootallnate/once@1.1.2(transitive)
- Removedms@2.1.3(transitive)
- Removedstream-async-iterator@2.0.0(transitive)
Updatedtorrentz@^8.0.6