@secret-momo/utils 1.4.1 → 1.4.2

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.
@@ -3,12 +3,13 @@ import type { DownloadOptions, DownloadResult } from './types';
3
3
  * 使用 aria2c 下载视频资源到指定目录。
4
4
  *
5
5
  * 下载前会检查本机是否安装 `aria2c`;若不存在会抛出异常,并提示通过
6
- * `brew install aria2` 安装。下载时使用 `aria2c -x8 -s8 -k1M` 参数和抖音
7
- * referer,并根据 URL 或响应 `Content-Type` 推断视频扩展名。
6
+ * `brew install aria2` 安装。下载时使用 `aria2c -x8 -s8 -k1M` 参数;如提供
7
+ * referer,则将其传给 aria2c,并根据 URL 或响应 `Content-Type` 推断视频扩展名。
8
8
  *
9
9
  * @param options 下载配置
10
10
  * @param options.url 视频资源 URL
11
11
  * @param options.dir 资源保存目录,不存在时会自动创建
12
+ * @param options.referer 可选的请求 Referer
12
13
  * @returns 下载成功后的文件路径,下载过程失败时返回 `null`
13
14
  */
14
- export declare function aria2DownloadVideo({ url, dir }: DownloadOptions): Promise<DownloadResult>;
15
+ export declare function aria2DownloadVideo({ url, dir, referer, }: DownloadOptions): Promise<DownloadResult>;
@@ -1 +1 @@
1
- import{mkdirSync as a}from"node:fs";import{join as c,basename as u}from"node:path";import{execFile as n}from"node:child_process";import{promisify as m}from"node:util";import{dateTimeToNow as s}from"../date-time.js";import{fetch as f}from"../fetch.js";import{logger as w}from"../pino.js";import{pickVideoExtension as d}from"./extension.js";import{fileSize as l}from"../file.js";const t=m(n);async function p(){try{await t("aria2c",["--version"])}catch(r){throw r.code==="ENOENT"?new Error("\u672A\u627E\u5230 aria2c \u547D\u4EE4\u884C\u5DE5\u5177\uFF0C\u8BF7\u5148\u4F7F\u7528 brew install aria2 \u5B89\u88C5\u3002",{cause:r}):r}}async function y(r){try{return(await f(r,{method:"HEAD"})).headers.get("content-type")}catch{return null}}async function h({url:r,dir:e}){await p();try{a(e,{recursive:!0});const i=d(r,await y(r)),o=c(e,`${s("YYYYMMDD_HHmmss")}.${i}`);return await t("aria2c",["-x8","-s8","-k1M","--referer=https://www.douyin.com/","-d",e,"-o",u(o),r]),{isSuccess:!0,filePath:o,fileSize:await l(o)}}catch(i){return w.child({name:"aria2DownloadVideo"}).error(i),{isSuccess:!1,error:i}}}export{h as aria2DownloadVideo};
1
+ import{mkdirSync as n}from"node:fs";import{join as u,basename as m}from"node:path";import{execFile as s}from"node:child_process";import{promisify as f}from"node:util";import{dateTimeToNow as l}from"../date-time.js";import{fetch as d}from"../fetch.js";import{logger as p}from"../pino.js";import{pickVideoExtension as w}from"./extension.js";import{fileSize as h}from"../file.js";const a=f(s);async function y(){try{await a("aria2c",["--version"])}catch(r){throw r.code==="ENOENT"?new Error("\u672A\u627E\u5230 aria2c \u547D\u4EE4\u884C\u5DE5\u5177\uFF0C\u8BF7\u5148\u4F7F\u7528 brew install aria2 \u5B89\u88C5\u3002",{cause:r}):r}}async function E(r){try{return(await d(r,{method:"HEAD"})).headers.get("content-type")}catch{return null}}async function D({url:r,dir:i,referer:t}){await y();try{n(i,{recursive:!0});const e=w(r,await E(r)),o=u(i,`${l("YYYYMMDD_HHmmss")}.${e}`),c=["-x8","-s8","-k1M",...t?["--referer",t]:[],"-d",i,"-o",m(o),r];return await a("aria2c",c),{isSuccess:!0,filePath:o,fileSize:await h(o)}}catch(e){return p.child({name:"aria2DownloadVideo"}).error(e),{isSuccess:!1,error:e}}}export{D as aria2DownloadVideo};
@@ -3,6 +3,8 @@ export interface DownloadOptions {
3
3
  url: string;
4
4
  /** 资源保存目录 */
5
5
  dir: string;
6
+ /** 请求资源时使用的 Referer */
7
+ referer?: string;
6
8
  }
7
9
  export type DownloadResult = {
8
10
  isSuccess: true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secret-momo/utils",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "module": "src/index.ts",
5
5
  "type": "module",
6
6
  "engines": {