Skip to content

Commit 719afc1

Browse files
committed
feat: ✨ 支持动态函数
1 parent 3fd9226 commit 719afc1

File tree

4 files changed

+40
-31
lines changed

4 files changed

+40
-31
lines changed

libs/zip-downloader/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @xiaohuohumax/xpath-selector
22

3+
## 2.2.0
4+
5+
### Minor Changes
6+
7+
- 支持动态函数
8+
39
## 2.1.0
410

511
### Minor Changes

libs/zip-downloader/README.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@
1919

2020
**Options 参数说明:**
2121

22-
| 参数名 | 类型 | 是否必填 | 默认值 | 说明 |
23-
| ------------- | -------------------------------- | -------- | ------ | -------------------------------------------------------------------------------- |
24-
| `filename` | string || | 保存的文件名,**添加此参数时会将压缩包保存到本地,未配置返回压缩包的 Blob 对象** |
25-
| `resources` | Resource[] || | 资源列表,数组,每个元素为对象,包含 `name``url` 或者 `blob` 字段 |
26-
| `concurrency` | number || `10` | 并发数,默认 `10` |
27-
| `onProgress` | (index: number) => Promise<void> || | 下载进度回调函数,参数为当前正在下载的资源索引 |
22+
| 参数名 | 类型 | 是否必填 | 默认值 | 说明 |
23+
| ------------- | ----------------------------------------- | -------- | ------ | -------------------------------------------------------------------------------- |
24+
| `filename` | string || | 保存的文件名,**添加此参数时会将压缩包保存到本地,未配置返回压缩包的 Blob 对象** |
25+
| `resources` | (Resource \| () => Promise\<Resource\>)[] || | 资源列表,数组,每个元素为对象,包含 `name``url` 或者 `blob` 字段 |
26+
| `concurrency` | number || `10` | 并发数,默认 `10` |
27+
| `onProgress` | (index: number) => Promise\<void\> || | 下载进度回调函数,参数为当前正在下载的资源索引 |
2828

2929
**Resource 参数说明:**
3030

31-
| 参数名 | 类型 | 是否必填 | 默认值 | 说明 |
32-
| ------ | ----------------------------- | -------- | ------ | ------------- |
33-
| `name` | string || | 资源名称 |
34-
| `url` | string || | URL 类型资源 |
35-
| `blob` | Blob 或者 () => Promise<Blob> || | Blob 类型资源 |
31+
| 参数名 | 类型 | 是否必填 | 默认值 | 说明 |
32+
| ------ | ------------------------------- | -------- | ------ | ------------- |
33+
| `name` | string || | 资源名称 |
34+
| `url` | string || | URL 类型资源 |
35+
| `blob` | Blob 或者 () => Promise\<Blob\> || | Blob 类型资源 |
3636

3737
## 📦 使用示例
3838

@@ -48,12 +48,13 @@ await zipDownloader({
4848
blob: new Blob(['hello world'], { type: 'text/plain' }),
4949
},
5050
{
51-
name: 'world.txt',
52-
blob: async () => {
53-
const response = await fetch('https://example.com/world.txt')
54-
return response.blob()
55-
},
51+
name: 'example.txt',
52+
blob: () => fetch('https://example.com').then(response => response.blob()),
5653
},
54+
() => fetch('https://example.com').then(response => ({
55+
name: 'example2.txt',
56+
blob: () => response.blob(),
57+
})),
5758
],
5859
concurrency: 10,
5960
async onProgress(index) {
@@ -73,12 +74,13 @@ const blob = await zipDownloader({
7374
blob: new Blob(['hello world'], { type: 'text/plain' }),
7475
},
7576
{
76-
name: 'world.txt',
77-
blob: async () => {
78-
const response = await fetch('https://example.com/world.txt')
79-
return response.blob()
80-
},
77+
name: 'example.txt',
78+
blob: () => fetch('https://example.com').then(response => response.blob()),
8179
},
80+
() => fetch('https://example.com').then(response => ({
81+
name: 'example2.txt',
82+
blob: () => response.blob(),
83+
})),
8284
],
8385
concurrency: 10,
8486
async onProgress(index) {

libs/zip-downloader/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@xiaohuohumax/zip-downloader",
33
"type": "module",
4-
"version": "2.1.0",
4+
"version": "2.2.0",
55
"description": "Zip Downloader -- 资源下载器(下载资源、Zip 压缩、下载到本地)",
66
"author": {
77
"name": "xiaohuohumax",

libs/zip-downloader/src/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ export interface BlobResource {
1111
blob: Blob | (() => Promise<Blob>)
1212
}
1313

14-
function isBlobResource(resource: Resource): resource is BlobResource {
15-
return (resource as BlobResource).blob instanceof Blob
16-
}
17-
18-
export type Resource = UrlResource | BlobResource
14+
export type Resource = UrlResource | BlobResource | (() => Promise<UrlResource | BlobResource>)
1915

2016
interface OptionsBase {
2117
resources: Resource[]
@@ -42,10 +38,15 @@ export default async function zipDownloader(options: Options): Promise<void | Bl
4238
const limit = pLimit(options.concurrency || 10)
4339
await Promise.all(options.resources.map((resource, index) => limit(async () => {
4440
await options.onProgress?.(index)
45-
const reader = isBlobResource(resource)
46-
? new BlobReader(typeof resource.blob === 'function' ? await resource.blob() : resource.blob)
47-
: new HttpReader(resource.url)
48-
return writer.add(resource.name, reader)
41+
if (typeof resource === 'function') {
42+
resource = await resource()
43+
}
44+
if (typeof resource === 'object' && 'url' in resource) {
45+
return writer.add(resource.name, new HttpReader(resource.url))
46+
}
47+
if (typeof resource === 'object' && 'blob' in resource) {
48+
return writer.add(resource.name, new BlobReader(typeof resource.blob === 'function' ? await resource.blob() : resource.blob))
49+
}
4950
})))
5051
const blob = await writer.close()
5152
if (!isSaveOptions(options)) {

0 commit comments

Comments
 (0)