@secret-momo/utils 1.2.1 → 1.4.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/lib/download/aria2.d.ts +2 -2
- package/lib/download/aria2.js +1 -1
- package/lib/download/index.d.ts +2 -2
- package/lib/download/index.js +1 -1
- package/lib/download/types.d.ts +11 -0
- package/package.json +14 -10
package/lib/download/aria2.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DownloadOptions } from './types';
|
|
1
|
+
import type { DownloadOptions, DownloadResult } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* 使用 aria2c 下载视频资源到指定目录。
|
|
4
4
|
*
|
|
@@ -11,4 +11,4 @@ import type { DownloadOptions } from './types';
|
|
|
11
11
|
* @param options.dir 资源保存目录,不存在时会自动创建
|
|
12
12
|
* @returns 下载成功后的文件路径,下载过程失败时返回 `null`
|
|
13
13
|
*/
|
|
14
|
-
export declare function aria2DownloadVideo({ url, dir }: DownloadOptions): Promise<
|
|
14
|
+
export declare function aria2DownloadVideo({ url, dir }: DownloadOptions): Promise<DownloadResult>;
|
package/lib/download/aria2.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{mkdirSync as
|
|
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 l}from"../pino.js";import{pickVideoExtension as d}from"./extension.js";import{fileSize as p}from"../file.js";const t=m(n);async function w(){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 w();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","-d",e,"-o",u(o),r]),{isSuccess:!0,filePath:o,fileSize:await p(o)}}catch(i){return l.child({name:"aria2DownloadVideo"}).error(i),{isSuccess:!1,error:i}}}export{h as aria2DownloadVideo};
|
package/lib/download/index.d.ts
CHANGED
|
@@ -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<
|
|
27
|
+
export declare function downloadImage({ url, dir }: DownloadOptions): Promise<DownloadResult>;
|
package/lib/download/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{mkdirSync as
|
|
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};
|
package/lib/download/types.d.ts
CHANGED
|
@@ -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,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@secret-momo/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"module": "src/index.ts",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": "24.19.0"
|
|
8
|
+
},
|
|
6
9
|
"publishConfig": {
|
|
7
10
|
"access": "public"
|
|
8
11
|
},
|
|
@@ -27,29 +30,30 @@
|
|
|
27
30
|
]
|
|
28
31
|
},
|
|
29
32
|
"scripts": {
|
|
33
|
+
"postinstall": "bun outdated",
|
|
30
34
|
"prepublishOnly": "bun run build",
|
|
31
35
|
"build": "rm -rf lib && rollup -c && tsc -p tsconfig.build.json"
|
|
32
36
|
},
|
|
33
37
|
"devDependencies": {
|
|
34
|
-
"@eslint/config-helpers": "0.
|
|
38
|
+
"@eslint/config-helpers": "0.7.0",
|
|
35
39
|
"@eslint/js": "10.0.1",
|
|
36
|
-
"@types/bun": "1.
|
|
37
|
-
"eslint": "10.
|
|
38
|
-
"eslint-plugin-import-x": "4.
|
|
40
|
+
"@types/bun": "1.4.0",
|
|
41
|
+
"eslint": "10.9.0",
|
|
42
|
+
"eslint-plugin-import-x": "4.17.1",
|
|
39
43
|
"eslint-plugin-react": "7.37.5",
|
|
40
44
|
"eslint-plugin-react-hooks": "7.1.1",
|
|
41
45
|
"eslint-plugin-unused-imports": "4.4.1",
|
|
42
46
|
"husky": "9.1.7",
|
|
43
47
|
"jiti": "2.7.0",
|
|
44
|
-
"lint-staged": "17.0
|
|
45
|
-
"prettier": "3.
|
|
46
|
-
"rollup": "4.62.
|
|
48
|
+
"lint-staged": "17.3.0",
|
|
49
|
+
"prettier": "3.9.6",
|
|
50
|
+
"rollup": "4.62.5",
|
|
47
51
|
"rollup-plugin-esbuild": "6.2.1",
|
|
48
52
|
"typescript": "6.0.3",
|
|
49
|
-
"typescript-eslint": "8.
|
|
53
|
+
"typescript-eslint": "8.67.0"
|
|
50
54
|
},
|
|
51
55
|
"dependencies": {
|
|
52
|
-
"dayjs": "1.11.
|
|
56
|
+
"dayjs": "1.11.23",
|
|
53
57
|
"pino": "10.3.1",
|
|
54
58
|
"pino-pretty": "13.1.3"
|
|
55
59
|
}
|