@secret-momo/utils 1.3.0 → 1.4.1

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.
@@ -1,14 +1,14 @@
1
- import type { DownloadOptions } from './types';
1
+ import type { DownloadOptions, DownloadResult } from './types';
2
2
  /**
3
3
  * 使用 aria2c 下载视频资源到指定目录。
4
4
  *
5
5
  * 下载前会检查本机是否安装 `aria2c`;若不存在会抛出异常,并提示通过
6
- * `brew install aria2` 安装。下载时使用 `aria2c -x8 -s8 -k1M` 参数,
7
- * 并根据 URL 或响应 `Content-Type` 推断视频扩展名。
6
+ * `brew install aria2` 安装。下载时使用 `aria2c -x8 -s8 -k1M` 参数和抖音
7
+ * referer,并根据 URL 或响应 `Content-Type` 推断视频扩展名。
8
8
  *
9
9
  * @param options 下载配置
10
10
  * @param options.url 视频资源 URL
11
11
  * @param options.dir 资源保存目录,不存在时会自动创建
12
12
  * @returns 下载成功后的文件路径,下载过程失败时返回 `null`
13
13
  */
14
- export declare function aria2DownloadVideo({ url, dir }: DownloadOptions): Promise<string | null>;
14
+ export declare function aria2DownloadVideo({ url, dir }: DownloadOptions): Promise<DownloadResult>;
@@ -1 +1 @@
1
- import{mkdirSync as e}from"node:fs";import{join as n,basename as u}from"node:path";import{execFile as c}from"node:child_process";import{promisify as m}from"node:util";import{dateTimeToNow as s}from"../date-time.js";import{fetch as d}from"../fetch.js";import{logger as f}from"../pino.js";import{pickVideoExtension as l}from"./extension.js";const a=m(c);async function p(){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 w(r){try{return(await d(r,{method:"HEAD"})).headers.get("content-type")}catch{return null}}async function y({url:r,dir:t}){await p();try{e(t,{recursive:!0});const o=l(r,await w(r)),i=n(t,`${s("YYYYMMDD_HHmmss")}.${o}`);return await a("aria2c",["-x8","-s8","-k1M","-d",t,"-o",u(i),r]),i}catch(o){return f.child({name:"aria2DownloadVideo"}).error(o),null}}export{y as aria2DownloadVideo};
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,5 +1,5 @@
1
1
  import { aria2DownloadVideo } from './aria2';
2
- import type { DownloadOptions } from './types';
2
+ import type { DownloadOptions, DownloadResult } from './types';
3
3
  export { aria2DownloadVideo };
4
4
  /**
5
5
  * 下载视频资源到指定目录。
@@ -24,4 +24,4 @@ export declare function downloadVideo({ url, dir }: DownloadOptions): Promise<st
24
24
  * @param options.dir 资源保存目录,不存在时会自动创建
25
25
  * @returns 下载成功后的文件路径,失败时返回 `null`
26
26
  */
27
- export declare function downloadImage({ url, dir }: DownloadOptions): Promise<string | null>;
27
+ export declare function downloadImage({ url, dir }: DownloadOptions): Promise<DownloadResult>;
@@ -1 +1 @@
1
- import{mkdirSync as i,createWriteStream as a}from"node:fs";import{join as m}from"node:path";import{Readable as d}from"node:stream";import{pipeline as c}from"node:stream/promises";import{fetch as s}from"../fetch.js";import{dateTimeToNow as p}from"../date-time.js";import{pickImageExtension as l,pickVideoExtension as w}from"./extension.js";import{logger as f}from"../pino.js";import{aria2DownloadVideo as k}from"./aria2.js";async function y({url:r,dir:e}){try{i(e,{recursive:!0});const o=await s(r);if(!o.body)throw new Error("Response has no body");const n=w(r,o.headers.get("content-type")),t=m(e,`${p("YYYYMMDD_HHmmss")}.${n}`);return await c(d.fromWeb(o.body),a(t)),t}catch(o){return f.child({name:"downloadVideo"}).error(o),null}}async function u({url:r,dir:e}){try{i(e,{recursive:!0});const o=await s(r);if(!o.body)throw new Error("Response has no body");const n=l(r,o.headers.get("content-type")),t=m(e,`${p("YYYYMMDD_HHmmss")}.${n}`);return await c(d.fromWeb(o.body),a(t)),t}catch(o){return f.child({name:"downloadImage"}).error(o),null}}export{k as aria2DownloadVideo,u as downloadImage,y as downloadVideo};
1
+ import{mkdirSync as n,createWriteStream as a}from"node:fs";import{join as m}from"node:path";import{Readable as s}from"node:stream";import{pipeline as d}from"node:stream/promises";import{fetch as c}from"../fetch.js";import{dateTimeToNow as f}from"../date-time.js";import{pickImageExtension as l,pickVideoExtension as w}from"./extension.js";import{logger as p}from"../pino.js";import{aria2DownloadVideo as I}from"./aria2.js";import{fileSize as u}from"../file.js";async function y({url:e,dir:t}){try{n(t,{recursive:!0});const o=await c(e);if(!o.body)throw new Error("Response has no body");const i=w(e,o.headers.get("content-type")),r=m(t,`${f("YYYYMMDD_HHmmss")}.${i}`);return await d(s.fromWeb(o.body),a(r)),r}catch(o){return p.child({name:"downloadVideo"}).error(o),null}}async function h({url:e,dir:t}){try{n(t,{recursive:!0});const o=await c(e);if(!o.body)throw new Error("Response has no body");const i=l(e,o.headers.get("content-type")),r=m(t,`${f("YYYYMMDD_HHmmss")}.${i}`);return await d(s.fromWeb(o.body),a(r)),{isSuccess:!0,filePath:r,fileSize:await u(r)}}catch(o){return p.child({name:"downloadImage"}).error(o),{isSuccess:!1,error:o}}}export{I as aria2DownloadVideo,h as downloadImage,y as downloadVideo};
@@ -4,3 +4,14 @@ export interface DownloadOptions {
4
4
  /** 资源保存目录 */
5
5
  dir: string;
6
6
  }
7
+ export type DownloadResult = {
8
+ isSuccess: true;
9
+ /** 下载成功后的文件路径 */
10
+ filePath: string;
11
+ /** 文件大小 */
12
+ fileSize: string;
13
+ } | {
14
+ isSuccess: false;
15
+ /** 下载失败的原因 */
16
+ error: Error;
17
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secret-momo/utils",
3
- "version": "1.3.0",
3
+ "version": "1.4.1",
4
4
  "module": "src/index.ts",
5
5
  "type": "module",
6
6
  "engines": {