@soga/fetcher 1.3.8 → 1.3.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.js CHANGED
@@ -1 +1 @@
1
- var e,t=Object.create,a=Object.defineProperty,r=Object.getOwnPropertyDescriptor,i=Object.getOwnPropertyNames,s=Object.getPrototypeOf,n=Object.prototype.hasOwnProperty,o=(e,t,s,o)=>{if(t&&"object"==typeof t||"function"==typeof t)for(let c of i(t))n.call(e,c)||c===s||a(e,c,{get:()=>t[c],enumerable:!(o=r(t,c))||o.enumerable});return e},c=(e,r,i)=>(i=null!=e?t(s(e)):{},o(!r&&e&&e.__esModule?i:a(i,"default",{value:e,enumerable:!0}),e)),d={};((e,t)=>{for(var r in t)a(e,r,{get:t[r],enumerable:!0})})(d,{AliFetcher:()=>v,BaiduFetcher:()=>p,fetchAliBuffer:()=>M,fetchBaiduBuffer:()=>U,getAliDownloadBuffer:()=>K,getAliFetcher:()=>D,getAliPreviewBuffer:()=>I,getBaiduDownloadBuffer:()=>C,getBaiduFetcher:()=>y,getBaiduPreviewBuffer:()=>F}),module.exports=(e=d,o(a({},"__esModule",{value:!0}),e));var l=c(require("axios")),u=require("typeorm"),f=require("@soga/entities"),h={},w=null,_=0,p=class{axios;domain="https://pan.baidu.com";urlRepository;constructor(e){this.axios=l.default.create({baseURL:this.domain}),this.axios.interceptors.response.use(e=>e.data,e=>Promise.reject(e)),this.urlRepository=e.getRepository(f.FetchUrl)}async getDownloadUrl({access_token:e,fs_id:t,ua:a}){return await this.getDownloadUrlByRange({access_token:e,fs_id:t,start:0,end:0,ua:a})}async getArrayBuffer(e){return await this.cleanExpired(),await this.multipleGetArrayBuffer({...e,times:0})}async getDownloadUrlByRange({access_token:e,fs_id:t,start:a=0,end:r=0,ua:i}){const s=this.getKey({fs_id:t,access_token:e,ua:i});try{for(;h[s];)await new Promise(e=>setTimeout(e,100));h[s]=!0;const n=await this.urlRepository.findOne({where:{key:s,speed:(0,u.MoreThan)(20),expire:(0,u.MoreThan)(Date.now())},order:{speed:"DESC",fail:"ASC"}});if(n)return n.fail>10?(await this.urlRepository.delete({key:s}),""):n.dlink;const{dlink:o,speed:c}=await this.fetchBestDlink({access_token:e,fs_id:t,start:a,end:r,ua:i});if(o)return await this.urlRepository.save({dlink:o,key:s,speed:c,expire:Date.now()+252e5}),o;throw new Error("baidu fetch download url failed")}finally{delete h[s]}}async cleanExpired(){Date.now()-_>6e5&&(await this.urlRepository.delete({expire:(0,u.LessThan)(Date.now())}),_=Date.now())}async fetchBestDlink({fs_id:e,start:t=0,end:a=0,ua:r,access_token:i}){const s=[];let n="";const o=await this.fetchDownloadUrl({fs_id:e,access_token:i,ua:r});if(!o)return{dlink:"",speed:0};const c=Math.min(t+127,a),d={"User-Agent":r,Range:`bytes=${t}-${c}`},u=c-t+1,f={start:0,end:0};for(let e=0;e<50;e++){const e=(await l.default.get(o,{headers:d,maxRedirects:0,validateStatus:e=>e>=200&&e<303})).headers.location;if(!e)return{dlink:"",speed:0};const t=new URL(e),{hostname:a}=t;if(s.includes(a))await new Promise(e=>setTimeout(e,50));else{n=e;try{return f.start=Date.now(),await l.default.get(e,{headers:d,timeout:4e3}),f.end=Date.now(),{dlink:e,speed:1e3*u/(f.end-f.start)}}catch(e){s.push(a),await new Promise(e=>setTimeout(e,200));continue}}}return{dlink:n,speed:1e3*u/(f.end-f.start)}}async fetchDownloadUrl({fs_id:e,ua:t="pan.baidu.com",access_token:a}){const r=await this.axios.get("/rest/2.0/xpan/multimedia",{params:{access_token:a,dlink:"1",method:"filemetas",fsids:JSON.stringify([e])},headers:{"User-Agent":t}}),{errno:i,list:s}=r||{};if(0!=i)throw new Error(`获取下载链接失败: ${i}`);if(!s||!s.length)throw new Error("获取下载链接失败");return`${s[0].dlink}&access_token=${a}`}async multipleGetArrayBuffer({access_token:e,fs_id:t,start:a,end:r,abort_controller:i,times:s=0,ua:n}){const o=await this.getDownloadUrlByRange({access_token:e,fs_id:t,start:a,end:r,ua:n});if(!o)throw new Error("获取下载链接失败");try{const e=r-a+1,t=Math.max(1e3*Math.ceil(e/1024/20),4e3);return(await l.default.get(o,{headers:{Range:`bytes=${a}-${r}`,"User-Agent":n,Accept:"application/octet-stream"},signal:i?.signal,responseType:"arraybuffer",timeout:t})).data}catch(c){if("AbortError"===c.name)return;const d=this.getKey({fs_id:t,access_token:e,ua:n});if(await this.urlRepository.increment({key:d,dlink:o},"fail",1),await new Promise(e=>setTimeout(e,1e3)),(s||0)<3)return await this.multipleGetArrayBuffer({access_token:e,fs_id:t,start:a,end:r,times:(s||0)+1,abort_controller:i,ua:n});throw c}}getKey({access_token:e,fs_id:t,ua:a}){const r=a?encodeURIComponent(a):"ua";return`baidu-${encodeURIComponent(e)}-${r}-${t}`}},y=e=>(w||(w=new p(e)),w),m=c(require("axios")),g=require("typeorm"),k=require("@soga/entities"),b={},B=null,v=class{axios;domain="https://openapi.alipan.com";urlRepository;constructor(e){this.axios=m.default.create({baseURL:this.domain}),this.axios.interceptors.response.use(e=>e.data,e=>Promise.reject(e)),this.urlRepository=e.getRepository(k.FetchUrl)}async getDownloadUrl({access_token:e,drive_id:t,file_id:a,ua:r}){const i=this.getKey({access_token:e,file_id:a,ua:r});try{for(;b[i];)await new Promise(e=>setTimeout(e,100));b[i]=!0;const s=await this.urlRepository.findOne({where:{key:i,fail:(0,g.LessThan)(2),expire:(0,g.MoreThan)(Date.now())},order:{fail:"ASC"}});if(s)return s.fail>1?(await this.urlRepository.delete({key:i}),""):s.dlink;const{url:n,expiration:o}=await this.fetchDownloadInfo({access_token:e,drive_id:t,file_id:a,ua:r});if(n)return await this.urlRepository.save({dlink:n,key:i,speed:100,expire:new Date(o).getTime()}),n;throw new Error("ali fetch download url failed")}finally{delete b[i]}}async getArrayBuffer(e){return await this.multipleGetArrayBuffer({...e,times:0})}async multipleGetArrayBuffer({access_token:e,drive_id:t,file_id:a,start:r,end:i,abort_controller:s,times:n=0,ua:o}){const c=await this.getDownloadUrl({access_token:e,drive_id:t,file_id:a,ua:o});if(!c)throw new Error("获取下载链接失败");try{const e=i-r+1,t=Math.max(1e3*Math.ceil(e/1024/50),3e3);return(await m.default.get(c,{headers:{Range:`bytes=${r}-${i}`,"User-Agent":o,Accept:"application/octet-stream"},responseType:"arraybuffer",timeout:t,signal:s?.signal})).data}catch(s){if("AbortError"===s.name)return;const d=this.getKey({access_token:e,file_id:a,ua:o});if(await this.urlRepository.increment({key:d,dlink:c},"fail",1),await new Promise(e=>setTimeout(e,1e3)),(n||0)<3)return await this.multipleGetArrayBuffer({access_token:e,drive_id:t,file_id:a,start:r,end:i,times:(n||0)+1,ua:o});throw s}}async fetchDownloadInfo({access_token:e,drive_id:t,file_id:a,ua:r}){const i={Authorization:`Bearer ${e}`,"User-Agent":r},{url:s,expiration:n}=await this.axios.post("/adrive/v1.0/openFile/getDownloadUrl",{drive_id:t,file_id:a},{headers:i});return{url:s,expiration:n}}getKey({access_token:e,file_id:t,ua:a}){const r=a?encodeURIComponent(a):"ua";return`ali-${encodeURIComponent(e)}-${r}-${t}`}},D=e=>(B||(B=new v(e)),B),R=require("@soga/baidu-ua"),x={},A=({part_md5:e,start:t,end:a})=>`${e}-${t}-${a}`;async function U(e){const t=function(e){const{start:t,end:a,total:r}=e,i=Math.max(a-t+1,0);if(2==e.host_vip_type){const r=Math.ceil(i/2097152),s=Math.ceil(i/r),n=[];for(let i=0;i<r;i++){const r=t+i*s,o=Math.min(a,r+s-1);n.push({...e,start:r,end:o,auto_ua:!1})}return{ranges:n,threads:2}}{const s=Math.ceil(i/131072),n=Math.ceil(i/s),o=[];for(let r=0;r<s;r++){const i=t+r*n,s=Math.min(a,i+n-1);o.push({...e,start:i,end:s,auto_ua:!0})}return{ranges:o,threads:Math.min(r<52428800?4:3,s)}}}(e),a=await async function(e,t){const{ranges:a,threads:r}=e,i=[...a],s=async()=>{if(!i.length)return;const e=i.shift();if(e){try{const a=A(e);if(!x[a]?.data){const r=y(t),{access_token:i,fs_id:s,host_id:n}=e,{ua:o,finish:c}=await(0,R.getDownloadBaiduUA)(n,e.total),d=await r.getArrayBuffer({access_token:i,fs_id:s,start:e.start,end:e.end,ua:e.auto_ua?o:e.ua});if(c(),!d||0===d.byteLength)return;x[a]={updated:Date.now(),data:Buffer.from(d)}}}catch(t){i.unshift(e)}await s()}},n=[];for(let e=0;e<r;e++)n.push(s().catch(e=>{}));await Promise.all(n).catch(e=>{});let o=!0;const c=a.map(e=>{const t=A(e);return x[t]?.data?x[t].data:(o=!1,null)});if(o){const e=Buffer.concat(c);return a.forEach(e=>{const t=A(e);delete x[t]}),e}return null}(t,e.dataSource);return a}async function M(e){const t=function(e){const{start:t,end:a}=e,r=Math.max(a-t+1,0),i=Math.ceil(r/1572864),s=Math.ceil(r/i),n=[];for(let r=0;r<i;r++){const i=t+r*s,o=Math.min(a,i+s-1);n.push({...e,start:i,end:o})}return{ranges:n,threads:2}}(e),a=await async function(e,t){const{ranges:a,threads:r}=e,i=[...a],s=async()=>{if(!i.length)return;const e=i.shift();if(e){try{const a=A(e);if(!x[a]?.data){const r=D(t),{access_token:i,drive_id:s,file_id:n}=e,o=await r.getArrayBuffer({access_token:i,drive_id:s,file_id:n,start:e.start,end:e.end,ua:e.ua,abort_controller:e.abort_controller});x[a]={updated:Date.now(),data:Buffer.from(o)}}}catch(t){i.unshift(e)}await s()}},n=[];for(let e=0;e<r;e++)n.push(s().catch(e=>{}));await Promise.all(n).catch(e=>{});let o=!0;const c=a.map(e=>{const t=A(e);return x[t]?.data?x[t].data:(o=!1,null)});if(o){const e=Buffer.concat(c);return a.forEach(e=>{const t=A(e);delete x[t]}),e}return null}(t,e.dataSource);return a}var $=require("@soga/lowdb"),P=require("fs-extra"),T=require("path"),E=class{folder_path;cache_path;db;cache_type;constructor(e,t){this.folder_path=e,this.cache_type=t,this.cache_path=(0,T.resolve)(e,`${t}_cache`)}async init(){const e=(0,T.resolve)(this.cache_path,"cache.json");this.db=await(0,$.getDb)(e,{})}async read(e){return this.db.data[e]}async write(e,t){this.db.data[e]=t,await this.db.write()}async delete(e){delete this.db.data[e],await this.db.write()}async writeBuffer(e,t){const a=(0,T.resolve)(this.cache_path,e);await(0,P.writeFile)(a,t),this.db.data[e]=!0,await this.db.write()}async readBuffer(e){if(!this.db.data[e])return null;const t=(0,T.resolve)(this.cache_path,e);return await(0,P.readFile)(t)}async deleteBuffer(e){if(!this.db.data[e])return;delete this.db.data[e],await this.db.write();const t=(0,T.resolve)(this.cache_path,e);try{await(0,P.remove)(t)}catch(e){}}},O={},j=0,S=async(e,t)=>{if(O[e]){for(;!O[e].inited;)await new Promise(e=>setTimeout(e,20));O[e].updated_at=Date.now();const t=Date.now();return t-j>18e5&&(j=t,Object.keys(O).forEach(e=>{t-O[e].updated_at>288e5&&delete O[e]})),O[e].store}const a=new E(e,t);return O[e]={updated_at:Date.now(),store:a,inited:!1},await a.init(),O[e].inited=!0,a},q=async({id:e,dataSource:t,record_id:a,start:r,end:i,total:s,part_md5:n,access_token:o,host_id:c,host_vip_type:d,fs_id:l,cache_folder:u,abort_controller:f},h)=>{let w=null;const _=e||A({part_md5:n,start:r,end:i});if(u){w=await S(u,h);const e=await w.readBuffer(_);if(e)return e}const p=await U({dataSource:t,record_id:a,start:r,end:i,total:s,access_token:o,host_id:c,host_vip_type:d,fs_id:l,part_md5:n,abort_controller:f});return p&&w&&await w.writeBuffer(_,p),p},F=async e=>await q(e,"preview"),C=async e=>await q(e,"download"),G=async({id:e,dataSource:t,record_id:a,start:r,end:i,total:s,part_md5:n,access_token:o,host_id:c,host_vip_type:d,drive_id:l,file_id:u,cache_folder:f,abort_controller:h},w)=>{const _=e||A({part_md5:n,start:r,end:i});let p=null;if(f){p=await S(f,w);const e=await p.readBuffer(_);if(e)return e}const y=await M({dataSource:t,record_id:a,start:r,end:i,total:s,access_token:o,host_id:c,host_vip_type:d,drive_id:l,file_id:u,part_md5:n,abort_controller:h});return y&&p&&await p.writeBuffer(_,y),y},I=async e=>await G(e,"preview"),K=async e=>await G(e,"download");
1
+ var e,t=Object.create,a=Object.defineProperty,r=Object.getOwnPropertyDescriptor,i=Object.getOwnPropertyNames,s=Object.getPrototypeOf,n=Object.prototype.hasOwnProperty,o=(e,t,s,o)=>{if(t&&"object"==typeof t||"function"==typeof t)for(let c of i(t))n.call(e,c)||c===s||a(e,c,{get:()=>t[c],enumerable:!(o=r(t,c))||o.enumerable});return e},c=(e,r,i)=>(i=null!=e?t(s(e)):{},o(!r&&e&&e.__esModule?i:a(i,"default",{value:e,enumerable:!0}),e)),d={};((e,t)=>{for(var r in t)a(e,r,{get:t[r],enumerable:!0})})(d,{AliFetcher:()=>v,BaiduFetcher:()=>p,fetchAliBuffer:()=>M,fetchBaiduBuffer:()=>U,getAliDownloadBuffer:()=>K,getAliFetcher:()=>D,getAliPreviewBuffer:()=>I,getBaiduDownloadBuffer:()=>C,getBaiduFetcher:()=>y,getBaiduPreviewBuffer:()=>F}),module.exports=(e=d,o(a({},"__esModule",{value:!0}),e));var l=c(require("axios")),u=require("typeorm"),f=require("@soga/entities"),h={},w=null,_=0,p=class{axios;domain="https://pan.baidu.com";urlRepository;constructor(e){this.axios=l.default.create({baseURL:this.domain}),this.axios.interceptors.response.use(e=>e.data,e=>Promise.reject(e)),this.urlRepository=e.getRepository(f.FetchUrl)}async getDownloadUrl({access_token:e,fs_id:t,ua:a}){return await this.getDownloadUrlByRange({access_token:e,fs_id:t,start:0,end:0,ua:a})}async getArrayBuffer(e){return await this.cleanExpired(),await this.multipleGetArrayBuffer({...e,times:0})}async getDownloadUrlByRange({access_token:e,fs_id:t,start:a=0,end:r=0,ua:i}){const s=this.getKey({fs_id:t,access_token:e,ua:i});try{for(;h[s];)await new Promise(e=>setTimeout(e,100));h[s]=!0;const n=await this.urlRepository.findOne({where:{key:s,speed:(0,u.MoreThan)(20),expire:(0,u.MoreThan)(Date.now())},order:{speed:"DESC",fail:"ASC"}});if(n)return n.fail>10?(await this.urlRepository.delete({key:s}),""):n.dlink;const{dlink:o,speed:c}=await this.fetchBestDlink({access_token:e,fs_id:t,start:a,end:r,ua:i});if(o)return await this.urlRepository.save({dlink:o,key:s,speed:c,expire:Date.now()+144e5}),o;throw new Error("baidu fetch download url failed")}finally{delete h[s]}}async cleanExpired(){Date.now()-_>6e5&&(await this.urlRepository.delete({expire:(0,u.LessThan)(Date.now())}),_=Date.now())}async fetchBestDlink({fs_id:e,start:t=0,end:a=0,ua:r,access_token:i}){const s=[];let n="";const o=await this.fetchDownloadUrl({fs_id:e,access_token:i,ua:r});if(!o)return{dlink:"",speed:0};const c=Math.min(t+127,a),d={"User-Agent":r,Range:`bytes=${t}-${c}`},u=c-t+1,f={start:0,end:0};for(let e=0;e<50;e++){const e=(await l.default.get(o,{headers:d,maxRedirects:0,validateStatus:e=>e>=200&&e<303})).headers.location;if(!e)return{dlink:"",speed:0};const t=new URL(e),{hostname:a}=t;if(s.includes(a))await new Promise(e=>setTimeout(e,50));else{n=e;try{return f.start=Date.now(),await l.default.get(e,{headers:d,timeout:4e3}),f.end=Date.now(),{dlink:e,speed:1e3*u/(f.end-f.start)}}catch(e){s.push(a),await new Promise(e=>setTimeout(e,200));continue}}}return{dlink:n,speed:1e3*u/(f.end-f.start)}}async fetchDownloadUrl({fs_id:e,ua:t="pan.baidu.com",access_token:a}){const r=await this.axios.get("/rest/2.0/xpan/multimedia",{params:{access_token:a,dlink:"1",method:"filemetas",fsids:JSON.stringify([e])},headers:{"User-Agent":t}}),{errno:i,list:s}=r||{};if(0!=i)throw new Error(`获取下载链接失败: ${i}`);if(!s||!s.length)throw new Error("获取下载链接失败");return`${s[0].dlink}&access_token=${a}`}async multipleGetArrayBuffer({access_token:e,fs_id:t,start:a,end:r,abort_controller:i,times:s=0,ua:n}){const o=await this.getDownloadUrlByRange({access_token:e,fs_id:t,start:a,end:r,ua:n});if(!o)throw new Error("获取下载链接失败");try{const e=r-a+1,t=Math.max(1e3*Math.ceil(e/1024/20),4e3);return(await l.default.get(o,{headers:{Range:`bytes=${a}-${r}`,"User-Agent":n,Accept:"application/octet-stream"},signal:i?.signal,responseType:"arraybuffer",timeout:t})).data}catch(c){if("AbortError"===c.name)return;const d=this.getKey({fs_id:t,access_token:e,ua:n});if(await this.urlRepository.increment({key:d,dlink:o},"fail",1),await new Promise(e=>setTimeout(e,1e3)),(s||0)<3)return await this.multipleGetArrayBuffer({access_token:e,fs_id:t,start:a,end:r,times:(s||0)+1,abort_controller:i,ua:n});throw c}}getKey({access_token:e,fs_id:t,ua:a}){const r=a?encodeURIComponent(a):"ua";return`baidu-${encodeURIComponent(e)}-${r}-${t}`}},y=e=>(w||(w=new p(e)),w),m=c(require("axios")),g=require("typeorm"),k=require("@soga/entities"),b={},B=null,v=class{axios;domain="https://openapi.alipan.com";urlRepository;constructor(e){this.axios=m.default.create({baseURL:this.domain}),this.axios.interceptors.response.use(e=>e.data,e=>Promise.reject(e)),this.urlRepository=e.getRepository(k.FetchUrl)}async getDownloadUrl({access_token:e,drive_id:t,file_id:a,ua:r}){const i=this.getKey({access_token:e,file_id:a,ua:r});try{for(;b[i];)await new Promise(e=>setTimeout(e,100));b[i]=!0;const s=await this.urlRepository.findOne({where:{key:i,fail:(0,g.LessThan)(2),expire:(0,g.MoreThan)(Date.now())},order:{fail:"ASC"}});if(s)return s.fail>1?(await this.urlRepository.delete({key:i}),""):s.dlink;const{url:n,expiration:o}=await this.fetchDownloadInfo({access_token:e,drive_id:t,file_id:a,ua:r});if(n)return await this.urlRepository.save({dlink:n,key:i,speed:100,expire:new Date(o).getTime()}),n;throw new Error("ali fetch download url failed")}finally{delete b[i]}}async getArrayBuffer(e){return await this.multipleGetArrayBuffer({...e,times:0})}async multipleGetArrayBuffer({access_token:e,drive_id:t,file_id:a,start:r,end:i,abort_controller:s,times:n=0,ua:o}){const c=await this.getDownloadUrl({access_token:e,drive_id:t,file_id:a,ua:o});if(!c)throw new Error("获取下载链接失败");try{const e=i-r+1,t=Math.max(1e3*Math.ceil(e/1024/50),3e3);return(await m.default.get(c,{headers:{Range:`bytes=${r}-${i}`,"User-Agent":o,Accept:"application/octet-stream"},responseType:"arraybuffer",timeout:t,signal:s?.signal})).data}catch(s){if("AbortError"===s.name)return;const d=this.getKey({access_token:e,file_id:a,ua:o});if(await this.urlRepository.increment({key:d,dlink:c},"fail",1),await new Promise(e=>setTimeout(e,1e3)),(n||0)<3)return await this.multipleGetArrayBuffer({access_token:e,drive_id:t,file_id:a,start:r,end:i,times:(n||0)+1,ua:o});throw s}}async fetchDownloadInfo({access_token:e,drive_id:t,file_id:a,ua:r}){const i={Authorization:`Bearer ${e}`,"User-Agent":r},{url:s,expiration:n}=await this.axios.post("/adrive/v1.0/openFile/getDownloadUrl",{drive_id:t,file_id:a},{headers:i});return{url:s,expiration:n}}getKey({access_token:e,file_id:t,ua:a}){const r=a?encodeURIComponent(a):"ua";return`ali-${encodeURIComponent(e)}-${r}-${t}`}},D=e=>(B||(B=new v(e)),B),R=require("@soga/baidu-ua"),x={},A=({part_md5:e,start:t,end:a})=>`${e}-${t}-${a}`;async function U(e){const t=function(e){const{start:t,end:a,total:r}=e,i=Math.max(a-t+1,0);if(2==e.host_vip_type){const r=Math.ceil(i/1048576),s=Math.ceil(i/r),n=[];for(let i=0;i<r;i++){const r=t+i*s,o=Math.min(a,r+s-1);n.push({...e,start:r,end:o,auto_ua:!1})}return{ranges:n,threads:2}}{const s=Math.ceil(i/131072),n=Math.ceil(i/s),o=[];for(let r=0;r<s;r++){const i=t+r*n,s=Math.min(a,i+n-1);o.push({...e,start:i,end:s,auto_ua:!0})}return{ranges:o,threads:Math.min(r<52428800?4:3,s)}}}(e),a=await async function(e,t){const{ranges:a,threads:r}=e,i=[...a],s=async()=>{if(!i.length)return;const e=i.shift();if(e){try{const a=A(e);if(!x[a]?.data){const r=y(t),{access_token:i,fs_id:s,host_id:n}=e,{ua:o,finish:c}=await(0,R.getDownloadBaiduUA)(n,e.total),d=await r.getArrayBuffer({access_token:i,fs_id:s,start:e.start,end:e.end,ua:e.auto_ua?o:e.ua});if(c(),!d||0===d.byteLength)return;x[a]={updated:Date.now(),data:Buffer.from(d)}}}catch(t){i.unshift(e)}await s()}},n=[];for(let e=0;e<r;e++)n.push(s().catch(e=>{}));await Promise.all(n).catch(e=>{});let o=!0;const c=a.map(e=>{const t=A(e);return x[t]?.data?x[t].data:(o=!1,null)});if(o){const e=Buffer.concat(c);return a.forEach(e=>{const t=A(e);delete x[t]}),e}return null}(t,e.dataSource);return a}async function M(e){const t=function(e){const{start:t,end:a}=e,r=Math.max(a-t+1,0),i=Math.ceil(r/1572864),s=Math.ceil(r/i),n=[];for(let r=0;r<i;r++){const i=t+r*s,o=Math.min(a,i+s-1);n.push({...e,start:i,end:o})}return{ranges:n,threads:2}}(e),a=await async function(e,t){const{ranges:a,threads:r}=e,i=[...a],s=async()=>{if(!i.length)return;const e=i.shift();if(e){try{const a=A(e);if(!x[a]?.data){const r=D(t),{access_token:i,drive_id:s,file_id:n}=e,o=await r.getArrayBuffer({access_token:i,drive_id:s,file_id:n,start:e.start,end:e.end,ua:e.ua,abort_controller:e.abort_controller});x[a]={updated:Date.now(),data:Buffer.from(o)}}}catch(t){i.unshift(e)}await s()}},n=[];for(let e=0;e<r;e++)n.push(s().catch(e=>{}));await Promise.all(n).catch(e=>{});let o=!0;const c=a.map(e=>{const t=A(e);return x[t]?.data?x[t].data:(o=!1,null)});if(o){const e=Buffer.concat(c);return a.forEach(e=>{const t=A(e);delete x[t]}),e}return null}(t,e.dataSource);return a}var $=require("@soga/lowdb"),P=require("fs-extra"),T=require("path"),E=class{folder_path;cache_path;db;cache_type;constructor(e,t){this.folder_path=e,this.cache_type=t,this.cache_path=(0,T.resolve)(e,`${t}_cache`)}async init(){const e=(0,T.resolve)(this.cache_path,"cache.json");this.db=await(0,$.getDb)(e,{})}async read(e){return this.db.data[e]}async write(e,t){this.db.data[e]=t,await this.db.write()}async delete(e){delete this.db.data[e],await this.db.write()}async writeBuffer(e,t){const a=(0,T.resolve)(this.cache_path,e);await(0,P.writeFile)(a,t),this.db.data[e]=!0,await this.db.write()}async readBuffer(e){if(!this.db.data[e])return null;const t=(0,T.resolve)(this.cache_path,e);return await(0,P.readFile)(t)}async deleteBuffer(e){if(!this.db.data[e])return;delete this.db.data[e],await this.db.write();const t=(0,T.resolve)(this.cache_path,e);try{await(0,P.remove)(t)}catch(e){}}},O={},j=0,S=async(e,t)=>{if(O[e]){for(;!O[e].inited;)await new Promise(e=>setTimeout(e,20));O[e].updated_at=Date.now();const t=Date.now();return t-j>18e5&&(j=t,Object.keys(O).forEach(e=>{t-O[e].updated_at>288e5&&delete O[e]})),O[e].store}const a=new E(e,t);return O[e]={updated_at:Date.now(),store:a,inited:!1},await a.init(),O[e].inited=!0,a},q=async({id:e,dataSource:t,record_id:a,start:r,end:i,total:s,part_md5:n,access_token:o,host_id:c,host_vip_type:d,fs_id:l,cache_folder:u,abort_controller:f},h)=>{let w=null;const _=e||A({part_md5:n,start:r,end:i});if(u){w=await S(u,h);const e=await w.readBuffer(_);if(e)return e}const p=await U({dataSource:t,record_id:a,start:r,end:i,total:s,access_token:o,host_id:c,host_vip_type:d,fs_id:l,part_md5:n,abort_controller:f});return p&&w&&await w.writeBuffer(_,p),p},F=async e=>await q(e,"preview"),C=async e=>await q(e,"download"),G=async({id:e,dataSource:t,record_id:a,start:r,end:i,total:s,part_md5:n,access_token:o,host_id:c,host_vip_type:d,drive_id:l,file_id:u,cache_folder:f,abort_controller:h},w)=>{const _=e||A({part_md5:n,start:r,end:i});let p=null;if(f){p=await S(f,w);const e=await p.readBuffer(_);if(e)return e}const y=await M({dataSource:t,record_id:a,start:r,end:i,total:s,access_token:o,host_id:c,host_vip_type:d,drive_id:l,file_id:u,part_md5:n,abort_controller:h});return y&&p&&await p.writeBuffer(_,y),y},I=async e=>await G(e,"preview"),K=async e=>await G(e,"download");
package/dist/main.mjs CHANGED
@@ -1 +1 @@
1
- import t from"axios";import{LessThan as e,MoreThan as a}from"typeorm";import{FetchUrl as r}from"@soga/entities";var i={},s=null,n=0,o=class{axios;domain="https://pan.baidu.com";urlRepository;constructor(e){this.axios=t.create({baseURL:this.domain}),this.axios.interceptors.response.use(t=>t.data,t=>Promise.reject(t)),this.urlRepository=e.getRepository(r)}async getDownloadUrl({access_token:t,fs_id:e,ua:a}){return await this.getDownloadUrlByRange({access_token:t,fs_id:e,start:0,end:0,ua:a})}async getArrayBuffer(t){return await this.cleanExpired(),await this.multipleGetArrayBuffer({...t,times:0})}async getDownloadUrlByRange({access_token:t,fs_id:e,start:r=0,end:s=0,ua:n}){const o=this.getKey({fs_id:e,access_token:t,ua:n});try{for(;i[o];)await new Promise(t=>setTimeout(t,100));i[o]=!0;const c=await this.urlRepository.findOne({where:{key:o,speed:a(20),expire:a(Date.now())},order:{speed:"DESC",fail:"ASC"}});if(c)return c.fail>10?(await this.urlRepository.delete({key:o}),""):c.dlink;const{dlink:d,speed:l}=await this.fetchBestDlink({access_token:t,fs_id:e,start:r,end:s,ua:n});if(d)return await this.urlRepository.save({dlink:d,key:o,speed:l,expire:Date.now()+252e5}),d;throw new Error("baidu fetch download url failed")}finally{delete i[o]}}async cleanExpired(){Date.now()-n>6e5&&(await this.urlRepository.delete({expire:e(Date.now())}),n=Date.now())}async fetchBestDlink({fs_id:e,start:a=0,end:r=0,ua:i,access_token:s}){const n=[];let o="";const c=await this.fetchDownloadUrl({fs_id:e,access_token:s,ua:i});if(!c)return{dlink:"",speed:0};const d=Math.min(a+127,r),l={"User-Agent":i,Range:`bytes=${a}-${d}`},u=d-a+1,f={start:0,end:0};for(let e=0;e<50;e++){const e=(await t.get(c,{headers:l,maxRedirects:0,validateStatus:t=>t>=200&&t<303})).headers.location;if(!e)return{dlink:"",speed:0};const a=new URL(e),{hostname:r}=a;if(n.includes(r))await new Promise(t=>setTimeout(t,50));else{o=e;try{return f.start=Date.now(),await t.get(e,{headers:l,timeout:4e3}),f.end=Date.now(),{dlink:e,speed:1e3*u/(f.end-f.start)}}catch(t){n.push(r),await new Promise(t=>setTimeout(t,200));continue}}}return{dlink:o,speed:1e3*u/(f.end-f.start)}}async fetchDownloadUrl({fs_id:t,ua:e="pan.baidu.com",access_token:a}){const r=await this.axios.get("/rest/2.0/xpan/multimedia",{params:{access_token:a,dlink:"1",method:"filemetas",fsids:JSON.stringify([t])},headers:{"User-Agent":e}}),{errno:i,list:s}=r||{};if(0!=i)throw new Error(`获取下载链接失败: ${i}`);if(!s||!s.length)throw new Error("获取下载链接失败");return`${s[0].dlink}&access_token=${a}`}async multipleGetArrayBuffer({access_token:e,fs_id:a,start:r,end:i,abort_controller:s,times:n=0,ua:o}){const c=await this.getDownloadUrlByRange({access_token:e,fs_id:a,start:r,end:i,ua:o});if(!c)throw new Error("获取下载链接失败");try{const e=i-r+1,a=Math.max(1e3*Math.ceil(e/1024/20),4e3);return(await t.get(c,{headers:{Range:`bytes=${r}-${i}`,"User-Agent":o,Accept:"application/octet-stream"},signal:s?.signal,responseType:"arraybuffer",timeout:a})).data}catch(t){if("AbortError"===t.name)return;const d=this.getKey({fs_id:a,access_token:e,ua:o});if(await this.urlRepository.increment({key:d,dlink:c},"fail",1),await new Promise(t=>setTimeout(t,1e3)),(n||0)<3)return await this.multipleGetArrayBuffer({access_token:e,fs_id:a,start:r,end:i,times:(n||0)+1,abort_controller:s,ua:o});throw t}}getKey({access_token:t,fs_id:e,ua:a}){const r=a?encodeURIComponent(a):"ua";return`baidu-${encodeURIComponent(t)}-${r}-${e}`}},c=t=>(s||(s=new o(t)),s);import d from"axios";import{LessThan as l,MoreThan as u}from"typeorm";import{FetchUrl as f}from"@soga/entities";var h={},w=null,_=class{axios;domain="https://openapi.alipan.com";urlRepository;constructor(t){this.axios=d.create({baseURL:this.domain}),this.axios.interceptors.response.use(t=>t.data,t=>Promise.reject(t)),this.urlRepository=t.getRepository(f)}async getDownloadUrl({access_token:t,drive_id:e,file_id:a,ua:r}){const i=this.getKey({access_token:t,file_id:a,ua:r});try{for(;h[i];)await new Promise(t=>setTimeout(t,100));h[i]=!0;const s=await this.urlRepository.findOne({where:{key:i,fail:l(2),expire:u(Date.now())},order:{fail:"ASC"}});if(s)return s.fail>1?(await this.urlRepository.delete({key:i}),""):s.dlink;const{url:n,expiration:o}=await this.fetchDownloadInfo({access_token:t,drive_id:e,file_id:a,ua:r});if(n)return await this.urlRepository.save({dlink:n,key:i,speed:100,expire:new Date(o).getTime()}),n;throw new Error("ali fetch download url failed")}finally{delete h[i]}}async getArrayBuffer(t){return await this.multipleGetArrayBuffer({...t,times:0})}async multipleGetArrayBuffer({access_token:t,drive_id:e,file_id:a,start:r,end:i,abort_controller:s,times:n=0,ua:o}){const c=await this.getDownloadUrl({access_token:t,drive_id:e,file_id:a,ua:o});if(!c)throw new Error("获取下载链接失败");try{const t=i-r+1,e=Math.max(1e3*Math.ceil(t/1024/50),3e3);return(await d.get(c,{headers:{Range:`bytes=${r}-${i}`,"User-Agent":o,Accept:"application/octet-stream"},responseType:"arraybuffer",timeout:e,signal:s?.signal})).data}catch(s){if("AbortError"===s.name)return;const d=this.getKey({access_token:t,file_id:a,ua:o});if(await this.urlRepository.increment({key:d,dlink:c},"fail",1),await new Promise(t=>setTimeout(t,1e3)),(n||0)<3)return await this.multipleGetArrayBuffer({access_token:t,drive_id:e,file_id:a,start:r,end:i,times:(n||0)+1,ua:o});throw s}}async fetchDownloadInfo({access_token:t,drive_id:e,file_id:a,ua:r}){const i={Authorization:`Bearer ${t}`,"User-Agent":r},{url:s,expiration:n}=await this.axios.post("/adrive/v1.0/openFile/getDownloadUrl",{drive_id:e,file_id:a},{headers:i});return{url:s,expiration:n}}getKey({access_token:t,file_id:e,ua:a}){const r=a?encodeURIComponent(a):"ua";return`ali-${encodeURIComponent(t)}-${r}-${e}`}},p=t=>(w||(w=new _(t)),w);import{getDownloadBaiduUA as y}from"@soga/baidu-ua";var m={},k=({part_md5:t,start:e,end:a})=>`${t}-${e}-${a}`;async function g(t){const e=function(t){const{start:e,end:a,total:r}=t,i=Math.max(a-e+1,0);if(2==t.host_vip_type){const r=Math.ceil(i/2097152),s=Math.ceil(i/r),n=[];for(let i=0;i<r;i++){const r=e+i*s,o=Math.min(a,r+s-1);n.push({...t,start:r,end:o,auto_ua:!1})}return{ranges:n,threads:2}}{const s=Math.ceil(i/131072),n=Math.ceil(i/s),o=[];for(let r=0;r<s;r++){const i=e+r*n,s=Math.min(a,i+n-1);o.push({...t,start:i,end:s,auto_ua:!0})}return{ranges:o,threads:Math.min(r<52428800?4:3,s)}}}(t),a=await async function(t,e){const{ranges:a,threads:r}=t,i=[...a],s=async()=>{if(!i.length)return;const t=i.shift();if(t){try{const a=k(t);if(!m[a]?.data){const r=c(e),{access_token:i,fs_id:s,host_id:n}=t,{ua:o,finish:d}=await y(n,t.total),l=await r.getArrayBuffer({access_token:i,fs_id:s,start:t.start,end:t.end,ua:t.auto_ua?o:t.ua});if(d(),!l||0===l.byteLength)return;m[a]={updated:Date.now(),data:Buffer.from(l)}}}catch(e){i.unshift(t)}await s()}},n=[];for(let t=0;t<r;t++)n.push(s().catch(t=>{}));await Promise.all(n).catch(t=>{});let o=!0;const d=a.map(t=>{const e=k(t);return m[e]?.data?m[e].data:(o=!1,null)});if(o){const t=Buffer.concat(d);return a.forEach(t=>{const e=k(t);delete m[e]}),t}return null}(e,t.dataSource);return a}async function b(t){const e=function(t){const{start:e,end:a}=t,r=Math.max(a-e+1,0),i=Math.ceil(r/1572864),s=Math.ceil(r/i),n=[];for(let r=0;r<i;r++){const i=e+r*s,o=Math.min(a,i+s-1);n.push({...t,start:i,end:o})}return{ranges:n,threads:2}}(t),a=await async function(t,e){const{ranges:a,threads:r}=t,i=[...a],s=async()=>{if(!i.length)return;const t=i.shift();if(t){try{const a=k(t);if(!m[a]?.data){const r=p(e),{access_token:i,drive_id:s,file_id:n}=t,o=await r.getArrayBuffer({access_token:i,drive_id:s,file_id:n,start:t.start,end:t.end,ua:t.ua,abort_controller:t.abort_controller});m[a]={updated:Date.now(),data:Buffer.from(o)}}}catch(e){i.unshift(t)}await s()}},n=[];for(let t=0;t<r;t++)n.push(s().catch(t=>{}));await Promise.all(n).catch(t=>{});let o=!0;const c=a.map(t=>{const e=k(t);return m[e]?.data?m[e].data:(o=!1,null)});if(o){const t=Buffer.concat(c);return a.forEach(t=>{const e=k(t);delete m[e]}),t}return null}(e,t.dataSource);return a}import{getDb as R}from"@soga/lowdb";import{readFile as x,remove as D,writeFile as v}from"fs-extra";import{resolve as B}from"path";var A=class{folder_path;cache_path;db;cache_type;constructor(t,e){this.folder_path=t,this.cache_type=e,this.cache_path=B(t,`${e}_cache`)}async init(){const t=B(this.cache_path,"cache.json");this.db=await R(t,{})}async read(t){return this.db.data[t]}async write(t,e){this.db.data[t]=e,await this.db.write()}async delete(t){delete this.db.data[t],await this.db.write()}async writeBuffer(t,e){const a=B(this.cache_path,t);await v(a,e),this.db.data[t]=!0,await this.db.write()}async readBuffer(t){if(!this.db.data[t])return null;const e=B(this.cache_path,t);return await x(e)}async deleteBuffer(t){if(!this.db.data[t])return;delete this.db.data[t],await this.db.write();const e=B(this.cache_path,t);try{await D(e)}catch(t){}}},U={},$=0,M=async(t,e)=>{if(U[t]){for(;!U[t].inited;)await new Promise(t=>setTimeout(t,20));U[t].updated_at=Date.now();const e=Date.now();return e-$>18e5&&($=e,Object.keys(U).forEach(t=>{e-U[t].updated_at>288e5&&delete U[t]})),U[t].store}const a=new A(t,e);return U[t]={updated_at:Date.now(),store:a,inited:!1},await a.init(),U[t].inited=!0,a},E=async({id:t,dataSource:e,record_id:a,start:r,end:i,total:s,part_md5:n,access_token:o,host_id:c,host_vip_type:d,fs_id:l,cache_folder:u,abort_controller:f},h)=>{let w=null;const _=t||k({part_md5:n,start:r,end:i});if(u){w=await M(u,h);const t=await w.readBuffer(_);if(t)return t}const p=await g({dataSource:e,record_id:a,start:r,end:i,total:s,access_token:o,host_id:c,host_vip_type:d,fs_id:l,part_md5:n,abort_controller:f});return p&&w&&await w.writeBuffer(_,p),p},T=async t=>await E(t,"preview"),P=async t=>await E(t,"download"),S=async({id:t,dataSource:e,record_id:a,start:r,end:i,total:s,part_md5:n,access_token:o,host_id:c,host_vip_type:d,drive_id:l,file_id:u,cache_folder:f,abort_controller:h},w)=>{const _=t||k({part_md5:n,start:r,end:i});let p=null;if(f){p=await M(f,w);const t=await p.readBuffer(_);if(t)return t}const y=await b({dataSource:e,record_id:a,start:r,end:i,total:s,access_token:o,host_id:c,host_vip_type:d,drive_id:l,file_id:u,part_md5:n,abort_controller:h});return y&&p&&await p.writeBuffer(_,y),y},C=async t=>await S(t,"preview"),G=async t=>await S(t,"download");export{_ as AliFetcher,o as BaiduFetcher,b as fetchAliBuffer,g as fetchBaiduBuffer,G as getAliDownloadBuffer,p as getAliFetcher,C as getAliPreviewBuffer,P as getBaiduDownloadBuffer,c as getBaiduFetcher,T as getBaiduPreviewBuffer};
1
+ import t from"axios";import{LessThan as e,MoreThan as a}from"typeorm";import{FetchUrl as r}from"@soga/entities";var i={},s=null,n=0,o=class{axios;domain="https://pan.baidu.com";urlRepository;constructor(e){this.axios=t.create({baseURL:this.domain}),this.axios.interceptors.response.use(t=>t.data,t=>Promise.reject(t)),this.urlRepository=e.getRepository(r)}async getDownloadUrl({access_token:t,fs_id:e,ua:a}){return await this.getDownloadUrlByRange({access_token:t,fs_id:e,start:0,end:0,ua:a})}async getArrayBuffer(t){return await this.cleanExpired(),await this.multipleGetArrayBuffer({...t,times:0})}async getDownloadUrlByRange({access_token:t,fs_id:e,start:r=0,end:s=0,ua:n}){const o=this.getKey({fs_id:e,access_token:t,ua:n});try{for(;i[o];)await new Promise(t=>setTimeout(t,100));i[o]=!0;const c=await this.urlRepository.findOne({where:{key:o,speed:a(20),expire:a(Date.now())},order:{speed:"DESC",fail:"ASC"}});if(c)return c.fail>10?(await this.urlRepository.delete({key:o}),""):c.dlink;const{dlink:d,speed:l}=await this.fetchBestDlink({access_token:t,fs_id:e,start:r,end:s,ua:n});if(d)return await this.urlRepository.save({dlink:d,key:o,speed:l,expire:Date.now()+144e5}),d;throw new Error("baidu fetch download url failed")}finally{delete i[o]}}async cleanExpired(){Date.now()-n>6e5&&(await this.urlRepository.delete({expire:e(Date.now())}),n=Date.now())}async fetchBestDlink({fs_id:e,start:a=0,end:r=0,ua:i,access_token:s}){const n=[];let o="";const c=await this.fetchDownloadUrl({fs_id:e,access_token:s,ua:i});if(!c)return{dlink:"",speed:0};const d=Math.min(a+127,r),l={"User-Agent":i,Range:`bytes=${a}-${d}`},u=d-a+1,f={start:0,end:0};for(let e=0;e<50;e++){const e=(await t.get(c,{headers:l,maxRedirects:0,validateStatus:t=>t>=200&&t<303})).headers.location;if(!e)return{dlink:"",speed:0};const a=new URL(e),{hostname:r}=a;if(n.includes(r))await new Promise(t=>setTimeout(t,50));else{o=e;try{return f.start=Date.now(),await t.get(e,{headers:l,timeout:4e3}),f.end=Date.now(),{dlink:e,speed:1e3*u/(f.end-f.start)}}catch(t){n.push(r),await new Promise(t=>setTimeout(t,200));continue}}}return{dlink:o,speed:1e3*u/(f.end-f.start)}}async fetchDownloadUrl({fs_id:t,ua:e="pan.baidu.com",access_token:a}){const r=await this.axios.get("/rest/2.0/xpan/multimedia",{params:{access_token:a,dlink:"1",method:"filemetas",fsids:JSON.stringify([t])},headers:{"User-Agent":e}}),{errno:i,list:s}=r||{};if(0!=i)throw new Error(`获取下载链接失败: ${i}`);if(!s||!s.length)throw new Error("获取下载链接失败");return`${s[0].dlink}&access_token=${a}`}async multipleGetArrayBuffer({access_token:e,fs_id:a,start:r,end:i,abort_controller:s,times:n=0,ua:o}){const c=await this.getDownloadUrlByRange({access_token:e,fs_id:a,start:r,end:i,ua:o});if(!c)throw new Error("获取下载链接失败");try{const e=i-r+1,a=Math.max(1e3*Math.ceil(e/1024/20),4e3);return(await t.get(c,{headers:{Range:`bytes=${r}-${i}`,"User-Agent":o,Accept:"application/octet-stream"},signal:s?.signal,responseType:"arraybuffer",timeout:a})).data}catch(t){if("AbortError"===t.name)return;const d=this.getKey({fs_id:a,access_token:e,ua:o});if(await this.urlRepository.increment({key:d,dlink:c},"fail",1),await new Promise(t=>setTimeout(t,1e3)),(n||0)<3)return await this.multipleGetArrayBuffer({access_token:e,fs_id:a,start:r,end:i,times:(n||0)+1,abort_controller:s,ua:o});throw t}}getKey({access_token:t,fs_id:e,ua:a}){const r=a?encodeURIComponent(a):"ua";return`baidu-${encodeURIComponent(t)}-${r}-${e}`}},c=t=>(s||(s=new o(t)),s);import d from"axios";import{LessThan as l,MoreThan as u}from"typeorm";import{FetchUrl as f}from"@soga/entities";var h={},w=null,_=class{axios;domain="https://openapi.alipan.com";urlRepository;constructor(t){this.axios=d.create({baseURL:this.domain}),this.axios.interceptors.response.use(t=>t.data,t=>Promise.reject(t)),this.urlRepository=t.getRepository(f)}async getDownloadUrl({access_token:t,drive_id:e,file_id:a,ua:r}){const i=this.getKey({access_token:t,file_id:a,ua:r});try{for(;h[i];)await new Promise(t=>setTimeout(t,100));h[i]=!0;const s=await this.urlRepository.findOne({where:{key:i,fail:l(2),expire:u(Date.now())},order:{fail:"ASC"}});if(s)return s.fail>1?(await this.urlRepository.delete({key:i}),""):s.dlink;const{url:n,expiration:o}=await this.fetchDownloadInfo({access_token:t,drive_id:e,file_id:a,ua:r});if(n)return await this.urlRepository.save({dlink:n,key:i,speed:100,expire:new Date(o).getTime()}),n;throw new Error("ali fetch download url failed")}finally{delete h[i]}}async getArrayBuffer(t){return await this.multipleGetArrayBuffer({...t,times:0})}async multipleGetArrayBuffer({access_token:t,drive_id:e,file_id:a,start:r,end:i,abort_controller:s,times:n=0,ua:o}){const c=await this.getDownloadUrl({access_token:t,drive_id:e,file_id:a,ua:o});if(!c)throw new Error("获取下载链接失败");try{const t=i-r+1,e=Math.max(1e3*Math.ceil(t/1024/50),3e3);return(await d.get(c,{headers:{Range:`bytes=${r}-${i}`,"User-Agent":o,Accept:"application/octet-stream"},responseType:"arraybuffer",timeout:e,signal:s?.signal})).data}catch(s){if("AbortError"===s.name)return;const d=this.getKey({access_token:t,file_id:a,ua:o});if(await this.urlRepository.increment({key:d,dlink:c},"fail",1),await new Promise(t=>setTimeout(t,1e3)),(n||0)<3)return await this.multipleGetArrayBuffer({access_token:t,drive_id:e,file_id:a,start:r,end:i,times:(n||0)+1,ua:o});throw s}}async fetchDownloadInfo({access_token:t,drive_id:e,file_id:a,ua:r}){const i={Authorization:`Bearer ${t}`,"User-Agent":r},{url:s,expiration:n}=await this.axios.post("/adrive/v1.0/openFile/getDownloadUrl",{drive_id:e,file_id:a},{headers:i});return{url:s,expiration:n}}getKey({access_token:t,file_id:e,ua:a}){const r=a?encodeURIComponent(a):"ua";return`ali-${encodeURIComponent(t)}-${r}-${e}`}},p=t=>(w||(w=new _(t)),w);import{getDownloadBaiduUA as y}from"@soga/baidu-ua";var m={},k=({part_md5:t,start:e,end:a})=>`${t}-${e}-${a}`;async function g(t){const e=function(t){const{start:e,end:a,total:r}=t,i=Math.max(a-e+1,0);if(2==t.host_vip_type){const r=Math.ceil(i/1048576),s=Math.ceil(i/r),n=[];for(let i=0;i<r;i++){const r=e+i*s,o=Math.min(a,r+s-1);n.push({...t,start:r,end:o,auto_ua:!1})}return{ranges:n,threads:2}}{const s=Math.ceil(i/131072),n=Math.ceil(i/s),o=[];for(let r=0;r<s;r++){const i=e+r*n,s=Math.min(a,i+n-1);o.push({...t,start:i,end:s,auto_ua:!0})}return{ranges:o,threads:Math.min(r<52428800?4:3,s)}}}(t),a=await async function(t,e){const{ranges:a,threads:r}=t,i=[...a],s=async()=>{if(!i.length)return;const t=i.shift();if(t){try{const a=k(t);if(!m[a]?.data){const r=c(e),{access_token:i,fs_id:s,host_id:n}=t,{ua:o,finish:d}=await y(n,t.total),l=await r.getArrayBuffer({access_token:i,fs_id:s,start:t.start,end:t.end,ua:t.auto_ua?o:t.ua});if(d(),!l||0===l.byteLength)return;m[a]={updated:Date.now(),data:Buffer.from(l)}}}catch(e){i.unshift(t)}await s()}},n=[];for(let t=0;t<r;t++)n.push(s().catch(t=>{}));await Promise.all(n).catch(t=>{});let o=!0;const d=a.map(t=>{const e=k(t);return m[e]?.data?m[e].data:(o=!1,null)});if(o){const t=Buffer.concat(d);return a.forEach(t=>{const e=k(t);delete m[e]}),t}return null}(e,t.dataSource);return a}async function b(t){const e=function(t){const{start:e,end:a}=t,r=Math.max(a-e+1,0),i=Math.ceil(r/1572864),s=Math.ceil(r/i),n=[];for(let r=0;r<i;r++){const i=e+r*s,o=Math.min(a,i+s-1);n.push({...t,start:i,end:o})}return{ranges:n,threads:2}}(t),a=await async function(t,e){const{ranges:a,threads:r}=t,i=[...a],s=async()=>{if(!i.length)return;const t=i.shift();if(t){try{const a=k(t);if(!m[a]?.data){const r=p(e),{access_token:i,drive_id:s,file_id:n}=t,o=await r.getArrayBuffer({access_token:i,drive_id:s,file_id:n,start:t.start,end:t.end,ua:t.ua,abort_controller:t.abort_controller});m[a]={updated:Date.now(),data:Buffer.from(o)}}}catch(e){i.unshift(t)}await s()}},n=[];for(let t=0;t<r;t++)n.push(s().catch(t=>{}));await Promise.all(n).catch(t=>{});let o=!0;const c=a.map(t=>{const e=k(t);return m[e]?.data?m[e].data:(o=!1,null)});if(o){const t=Buffer.concat(c);return a.forEach(t=>{const e=k(t);delete m[e]}),t}return null}(e,t.dataSource);return a}import{getDb as R}from"@soga/lowdb";import{readFile as x,remove as D,writeFile as v}from"fs-extra";import{resolve as B}from"path";var A=class{folder_path;cache_path;db;cache_type;constructor(t,e){this.folder_path=t,this.cache_type=e,this.cache_path=B(t,`${e}_cache`)}async init(){const t=B(this.cache_path,"cache.json");this.db=await R(t,{})}async read(t){return this.db.data[t]}async write(t,e){this.db.data[t]=e,await this.db.write()}async delete(t){delete this.db.data[t],await this.db.write()}async writeBuffer(t,e){const a=B(this.cache_path,t);await v(a,e),this.db.data[t]=!0,await this.db.write()}async readBuffer(t){if(!this.db.data[t])return null;const e=B(this.cache_path,t);return await x(e)}async deleteBuffer(t){if(!this.db.data[t])return;delete this.db.data[t],await this.db.write();const e=B(this.cache_path,t);try{await D(e)}catch(t){}}},U={},$=0,M=async(t,e)=>{if(U[t]){for(;!U[t].inited;)await new Promise(t=>setTimeout(t,20));U[t].updated_at=Date.now();const e=Date.now();return e-$>18e5&&($=e,Object.keys(U).forEach(t=>{e-U[t].updated_at>288e5&&delete U[t]})),U[t].store}const a=new A(t,e);return U[t]={updated_at:Date.now(),store:a,inited:!1},await a.init(),U[t].inited=!0,a},E=async({id:t,dataSource:e,record_id:a,start:r,end:i,total:s,part_md5:n,access_token:o,host_id:c,host_vip_type:d,fs_id:l,cache_folder:u,abort_controller:f},h)=>{let w=null;const _=t||k({part_md5:n,start:r,end:i});if(u){w=await M(u,h);const t=await w.readBuffer(_);if(t)return t}const p=await g({dataSource:e,record_id:a,start:r,end:i,total:s,access_token:o,host_id:c,host_vip_type:d,fs_id:l,part_md5:n,abort_controller:f});return p&&w&&await w.writeBuffer(_,p),p},T=async t=>await E(t,"preview"),P=async t=>await E(t,"download"),S=async({id:t,dataSource:e,record_id:a,start:r,end:i,total:s,part_md5:n,access_token:o,host_id:c,host_vip_type:d,drive_id:l,file_id:u,cache_folder:f,abort_controller:h},w)=>{const _=t||k({part_md5:n,start:r,end:i});let p=null;if(f){p=await M(f,w);const t=await p.readBuffer(_);if(t)return t}const y=await b({dataSource:e,record_id:a,start:r,end:i,total:s,access_token:o,host_id:c,host_vip_type:d,drive_id:l,file_id:u,part_md5:n,abort_controller:h});return y&&p&&await p.writeBuffer(_,y),y},C=async t=>await S(t,"preview"),G=async t=>await S(t,"download");export{_ as AliFetcher,o as BaiduFetcher,b as fetchAliBuffer,g as fetchBaiduBuffer,G as getAliDownloadBuffer,p as getAliFetcher,C as getAliPreviewBuffer,P as getBaiduDownloadBuffer,c as getBaiduFetcher,T as getBaiduPreviewBuffer};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soga/fetcher",
3
- "version": "1.3.8",
3
+ "version": "1.3.12",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -17,9 +17,9 @@
17
17
  "axios": "^1.9.0",
18
18
  "fs-extra": "^11.3.2",
19
19
  "typeorm": "^0.3.24",
20
- "@soga/baidu-ua": "1.3.8",
21
- "@soga/lowdb": "1.3.8",
22
- "@soga/entities": "1.3.8"
20
+ "@soga/baidu-ua": "1.3.12",
21
+ "@soga/lowdb": "1.3.12",
22
+ "@soga/entities": "1.3.12"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/node": "^24.5.2",
@@ -27,7 +27,7 @@
27
27
  "terser": "^5.43.1",
28
28
  "tsup": "^8.5.0",
29
29
  "typescript": "^5.8.3",
30
- "@soga/typescript-config": "1.3.8"
30
+ "@soga/typescript-config": "1.3.12"
31
31
  },
32
32
  "scripts": {
33
33
  "build": "rimraf dist && tsup src/main.ts --format cjs,esm --dts --minify terser"