hlsdownloader 3.0.4 → 3.1.0

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/README.md CHANGED
@@ -69,7 +69,7 @@ pnpm install hlsdownloader
69
69
  It can also be useful if you want to do content pre-fetching from CDN for your end viewers. If any `TS` or `m3u8`
70
70
  variant download is failed it continues downloading others and reports after finishing.
71
71
 
72
- It's simple as below.
72
+ It's simple as below with.
73
73
 
74
74
  ```js
75
75
  import HLSDownloader from 'hlsdownloader';
@@ -79,6 +79,9 @@ const options = {
79
79
  destination: '/tmp', // change it (optional: default '')
80
80
  concurrency: 10, // change it (optional: default = 1),
81
81
  overwrite: true, // change it (optional: default = false)
82
+ onData: function (data) {
83
+ console.log(data); // {item: "<url-just-downloaded>", total: "<total-items-to-download>", path: "<absolute-path-of-download-loation>"}
84
+ },
82
85
  };
83
86
  const downloader = new HLSDownloader(options);
84
87
  downloader.startDownload().then(response => console.log(response));
package/build/index.js CHANGED
@@ -1 +1 @@
1
- var R=Object.defineProperty;var a=(e,t)=>R(e,"name",{value:t,configurable:!0});import{createWriteStream as k}from"fs";import{access as x,constants as D,mkdir as E,unlink as v}from"fs/promises";import u from"ky";import U from"p-limit";import{dirname as I,join as f}from"path";import{Readable as C}from"stream";import{URL as T}from"url";var m=class extends Error{static{a(this,"InvalidPlaylist")}constructor(t){super(t),this.name=this.constructor.name,Error.captureStackTrace(this,this.constructor)}},p=m;var d=class extends Error{static{a(this,"ProtocolNotSupported")}constructor(t){super(t),this.name=this.constructor.name,Error.captureStackTrace(this,this.constructor)}},y=d;var P=a((e,t=["http:","https:","ftp:","sftp:"])=>{try{let{protocol:s}=new URL(e);if(s&&!t.includes(`${s}`))throw new y(`${s} is not supported. Supported protocols are ${t.join(", ")}`);return!0}catch(s){throw s}},"isValidUrl"),g=a(e=>e.substring(0,1).replace("/","")+e.substring(1),"stripFirstSlash"),b=a(e=>e.match(/^#EXTM3U/im)!==null,"isValidPlaylist"),O=a(e=>new URL(e),"parseUrl"),S=a((e,...t)=>{let s=new Set(t.flat());return Object.fromEntries(Object.entries(e).filter(([r])=>!s.has(r)))},"omit");var l={isValidPlaylist:b,isValidUrl:P,omit:S,parseUrl:O,stripFirstSlash:g};var V=".m3u8",w=class e{static{a(this,"Downloader")}static defaultKyOptions={retry:{limit:0}};pool=U(1);overwrite=!1;static unSupportedOptions=["uri","url","json","form","body","method","setHost","isStream","parseJson","prefixUrl","cookieJar","playlistURL","concurrency","allowGetBody","stringifyJson","methodRewriting"];items=[];errors=[];concurrency=1;kyOptions={};playlistURL="";destination="";constructor({playlistURL:t,destination:s,concurrency:r=1,overwrite:i=!1,...n}={concurrency:1,destination:"",playlistURL:"",overwrite:!1,options:{}}){try{l.isValidUrl(t),this.items=[t],this.playlistURL=t,this.concurrency=r,this.overwrite=i??!1,this.destination=s??"",this.pool=U(r??1),this.kyOptions=this.mergeOptions(n),this.fetchItems=this.fetchItems.bind(this),this.downloadItem=this.downloadItem.bind(this),this.mergeOptions=this.mergeOptions.bind(this),this.fetchPlaylist=this.fetchPlaylist.bind(this),this.startDownload=this.startDownload.bind(this),this.downloadItems=this.downloadItems.bind(this),this.shouldOverwrite=this.shouldOverwrite.bind(this),this.createDirectory=this.createDirectory.bind(this),this.parsePlaylist=this.parsePlaylist.bind(this),this.processPlaylistItems=this.processPlaylistItems.bind(this),this.formatPlaylistContent=this.formatPlaylistContent.bind(this)}catch(h){throw h}}async startDownload(){let{url:t,body:s}=await this.fetchPlaylist(this.playlistURL);if(this.errors.length>0)return{errors:this.errors,message:"Unsuccessful download"};let r=this.parsePlaylist(t,s);this.items=[...this.items,...r];let i=r.filter(o=>o.toLowerCase().endsWith(V)),n=await Promise.allSettled(i.map(o=>this.fetchPlaylist(o)));return r=this.formatPlaylistContent(n).map(o=>this.parsePlaylist(o?.url,o?.body)).flat(),this.items=[...this.items,...r],await this.processPlaylistItems(),this.errors.length>0?{errors:this.errors,total:this.items.length,message:"Download ended with some errors"}:{total:this.items.length,playlistURL:this.playlistURL,message:"Downloaded successfully"}}mergeOptions(t){return Object.assign(e.defaultKyOptions,l.omit(t,...e.unSupportedOptions))}parsePlaylist(t,s){return s.replace(/^#[\s\S].*/gim,"").split(/\r?\n/).reduce((r,i)=>{if(i!==""){let n=new T(i,t).href;r.push(n)}return r},[])}async fetchPlaylist(t){try{let s=await u.get(t,{...this.kyOptions}).text();if(!l.isValidPlaylist(s)){let{name:r,message:i}=new p("Invalid playlist");return this.errors.push({url:t,name:r,message:i}),{url:"",body:""}}return{url:t,body:s}}catch({name:s,message:r}){return this.errors.push({url:t,name:s,message:r}),{url:"",body:""}}}formatPlaylistContent(t){return t.reduce((s,{status:r,value:i})=>(r.toLowerCase()==="fulfilled"&&i&&s.push(i),s),[])}async processPlaylistItems(){return this.destination&&this.downloadItems()||this.fetchItems()}async downloadItem(t){try{let s=await u.get(t,{...this.kyOptions}),r=await this.createDirectory(t),i=C.fromWeb(s.body);return new Promise((n,h)=>{let o=k(r);i.pipe(o),i.on("error",c=>{i.destroy(),o.destroy(),v(r),h(c)}),o.on("finish",()=>{o.close(),n("success")}),o.on("error",c=>{o.destroy(),i.destroy(),h(c)})})}catch({name:s,message:r}){this.errors.push({name:s,message:r,url:t})}}async downloadItems(){try{if(!await this.shouldOverwrite(this.playlistURL)){let s=new Error("directory already exists");throw s.name="EEXIST",s}await this.createDirectory(this.playlistURL);let t=this.items.map(s=>this.pool(this.downloadItem,s));return Promise.allSettled(t)}catch(t){this.errors.push({url:this.playlistURL,name:t.name,message:t.message})}}async fetchItems(){return Promise.allSettled(this.items.map(t=>this.pool(async()=>{try{return await u.get(t,{...this.kyOptions})}catch({name:s,message:r}){this.errors.push({url:t,name:s,message:r})}})))}async createDirectory(t){let{pathname:s}=l.parseUrl(t),r=f(this.destination,I(s));return await E(r,{recursive:!0}),f(this.destination,l.stripFirstSlash(s))}async shouldOverwrite(t){try{let{pathname:s}=l.parseUrl(t),r=f(this.destination,I(s));return await x(r,D.F_OK),this.overwrite}catch(s){if(s.code==="ENOENT")return!0;throw s}}},L=w;var at=L;export{at as default};
1
+ var R=Object.defineProperty;var a=(e,t)=>R(e,"name",{value:t,configurable:!0});import{createWriteStream as E}from"fs";import{access as k,constants as x,mkdir as v,unlink as C}from"fs/promises";import y from"ky";import D from"p-limit";import{dirname as I,join as f}from"path";import{Readable as T}from"stream";import{URL as F}from"url";var m=class extends Error{static{a(this,"InvalidPlaylist")}constructor(t){super(t),this.name=this.constructor.name,Error.captureStackTrace(this,this.constructor)}},p=m;var d=class extends Error{static{a(this,"ProtocolNotSupported")}constructor(t){super(t),this.name=this.constructor.name,Error.captureStackTrace(this,this.constructor)}},u=d;var P=a((e,t=["http:","https:","ftp:","sftp:"])=>{try{let{protocol:s}=new URL(e);if(s&&!t.includes(`${s}`))throw new u(`${s} is not supported. Supported protocols are ${t.join(", ")}`);return!0}catch(s){throw s}},"isValidUrl"),g=a(e=>e.substring(0,1).replace("/","")+e.substring(1),"stripFirstSlash"),b=a(e=>e.match(/^#EXTM3U/im)!==null,"isValidPlaylist"),O=a(e=>new URL(e),"parseUrl"),S=a((e,...t)=>{let s=new Set(t.flat());return Object.fromEntries(Object.entries(e).filter(([r])=>!s.has(r)))},"omit"),U=a(e=>typeof e!="function","isNotFunction");var n={isValidPlaylist:b,isValidUrl:P,omit:S,parseUrl:O,stripFirstSlash:g,isNotFunction:U};var N=".m3u8",w=class e{static{a(this,"Downloader")}static defaultKyOptions={retry:{limit:0}};pool=D(1);overwrite=!1;static unSupportedOptions=["uri","url","json","form","body","method","setHost","isStream","parseJson","prefixUrl","cookieJar","playlistURL","concurrency","allowGetBody","stringifyJson","methodRewriting"];items=[];errors=[];concurrency=1;kyOptions={};playlistURL="";destination="";onData=null;constructor({playlistURL:t,destination:s,concurrency:r=1,overwrite:i=!1,onData:l=null,...h}={concurrency:1,destination:"",playlistURL:"",onData:null,overwrite:!1,options:{}}){try{if(this.items=[t],this.playlistURL=t,this.concurrency=r,this.overwrite=i??!1,this.destination=s??"",this.pool=D(r??1),this.kyOptions=this.mergeOptions(h),this.onData=l,this.fetchItems=this.fetchItems.bind(this),this.downloadItem=this.downloadItem.bind(this),this.mergeOptions=this.mergeOptions.bind(this),this.fetchPlaylist=this.fetchPlaylist.bind(this),this.startDownload=this.startDownload.bind(this),this.downloadItems=this.downloadItems.bind(this),this.shouldOverwrite=this.shouldOverwrite.bind(this),this.createDirectory=this.createDirectory.bind(this),this.parsePlaylist=this.parsePlaylist.bind(this),this.processPlaylistItems=this.processPlaylistItems.bind(this),this.formatPlaylistContent=this.formatPlaylistContent.bind(this),n.isValidUrl(t),this.onData!==null&&n.isNotFunction(this.onData))throw TypeError("The `onData` must be a function")}catch(o){throw o}}async startDownload(){let{url:t,body:s}=await this.fetchPlaylist(this.playlistURL);if(this.errors.length>0)return{errors:this.errors,message:"Unsuccessful download"};let r=this.parsePlaylist(t,s);this.items=[...this.items,...r];let i=r.filter(o=>o.toLowerCase().endsWith(N)),l=await Promise.allSettled(i.map(o=>this.fetchPlaylist(o)));return r=this.formatPlaylistContent(l).map(o=>this.parsePlaylist(o?.url,o?.body)).flat(),this.items=[...this.items,...r],await this.processPlaylistItems(),this.errors.length>0?{errors:this.errors,total:this.items.length,message:"Download ended with some errors"}:{total:this.items.length,playlistURL:this.playlistURL,message:"Downloaded successfully"}}mergeOptions(t){return Object.assign(e.defaultKyOptions,n.omit(t,...e.unSupportedOptions))}parsePlaylist(t,s){return s.replace(/^#[\s\S].*/gim,"").split(/\r?\n/).reduce((r,i)=>{if(i!==""){let l=new F(i,t).href;r.push(l)}return r},[])}async fetchPlaylist(t){try{let s=await y.get(t,{...this.kyOptions}).text();if(!n.isValidPlaylist(s)){let{name:r,message:i}=new p("Invalid playlist");return this.errors.push({url:t,name:r,message:i}),{url:"",body:""}}return{url:t,body:s}}catch({name:s,message:r}){return this.errors.push({url:t,name:s,message:r}),{url:"",body:""}}}formatPlaylistContent(t){return t.reduce((s,{status:r,value:i})=>(r.toLowerCase()==="fulfilled"&&i&&s.push(i),s),[])}async processPlaylistItems(){return this.destination&&this.downloadItems()||this.fetchItems()}async downloadItem(t){try{let s=await y.get(t,{...this.kyOptions}),r=await this.createDirectory(t),i=T.fromWeb(s.body);return new Promise((l,h)=>{let o=E(r);i.pipe(o),i.on("error",c=>{i.destroy(),o.destroy(),C(r),h(c)}),o.on("finish",()=>{o.close(),this.onData&&this.onData({item:t,total:this.items.length,path:r}),l("success")}),o.on("error",c=>{o.destroy(),i.destroy(),h(c)})})}catch({name:s,message:r}){this.errors.push({name:s,message:r,url:t})}}async downloadItems(){try{if(!await this.shouldOverwrite(this.playlistURL)){let s=new Error("directory already exists");throw s.name="EEXIST",s}await this.createDirectory(this.playlistURL);let t=this.items.map(s=>this.pool(this.downloadItem,s));return Promise.allSettled(t)}catch(t){this.errors.push({url:this.playlistURL,name:t.name,message:t.message})}}async fetchItems(){return Promise.allSettled(this.items.map(t=>this.pool(async()=>{try{return await y.get(t,{...this.kyOptions})}catch({name:s,message:r}){this.errors.push({url:t,name:s,message:r})}})))}async createDirectory(t){let{pathname:s}=n.parseUrl(t),r=f(this.destination,I(s));return await v(r,{recursive:!0}),f(this.destination,n.stripFirstSlash(s))}async shouldOverwrite(t){try{let{pathname:s}=n.parseUrl(t),r=f(this.destination,I(s));return await k(r,x.F_OK),this.overwrite}catch(s){if(s.code==="ENOENT")return!0;throw s}}},L=w;var nt=L;export{nt as default};
package/package.json CHANGED
@@ -96,5 +96,5 @@
96
96
  },
97
97
  "snyk": true,
98
98
  "type": "module",
99
- "version": "3.0.4"
99
+ "version": "3.1.0"
100
100
  }