@fetchmax/plugin-progress 1.0.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/dist/index.cjs +133 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +108 -0
- package/package.json +59 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
default: () => index_default,
|
|
24
|
+
progressPlugin: () => progressPlugin
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
function formatBytes(bytes) {
|
|
28
|
+
if (bytes === 0) return "0 B";
|
|
29
|
+
const k = 1024;
|
|
30
|
+
const sizes = ["B", "KB", "MB", "GB"];
|
|
31
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
32
|
+
return `${(bytes / Math.pow(k, i)).toFixed(2)} ${sizes[i]}`;
|
|
33
|
+
}
|
|
34
|
+
function progressPlugin(config = {}) {
|
|
35
|
+
const { onUploadProgress: _onUploadProgress, onDownloadProgress } = config;
|
|
36
|
+
return {
|
|
37
|
+
name: "progress",
|
|
38
|
+
async onRequest(requestConfig, context) {
|
|
39
|
+
if (onDownloadProgress) {
|
|
40
|
+
context.originalResponseType = requestConfig.responseType;
|
|
41
|
+
context.progressCallback = onDownloadProgress;
|
|
42
|
+
requestConfig.responseType = "stream";
|
|
43
|
+
}
|
|
44
|
+
return requestConfig;
|
|
45
|
+
},
|
|
46
|
+
async onResponse(response, _request, context) {
|
|
47
|
+
const progressCallback = context.progressCallback;
|
|
48
|
+
if (!progressCallback) {
|
|
49
|
+
return response;
|
|
50
|
+
}
|
|
51
|
+
const body = response.data;
|
|
52
|
+
if (!body || typeof body.getReader !== "function") {
|
|
53
|
+
if (response.response && !response.response.bodyUsed) {
|
|
54
|
+
try {
|
|
55
|
+
const text2 = await response.response.text();
|
|
56
|
+
const contentType2 = response.headers.get("content-type") || "";
|
|
57
|
+
const originalResponseType2 = context.originalResponseType;
|
|
58
|
+
if (originalResponseType2 === "text" || contentType2.includes("text/")) {
|
|
59
|
+
response.data = text2;
|
|
60
|
+
} else {
|
|
61
|
+
try {
|
|
62
|
+
response.data = JSON.parse(text2);
|
|
63
|
+
} catch {
|
|
64
|
+
response.data = text2;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
} catch (error) {
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return response;
|
|
71
|
+
}
|
|
72
|
+
const reader = body.getReader();
|
|
73
|
+
const contentLength = +(response.headers.get("Content-Length") || 0);
|
|
74
|
+
let receivedLength = 0;
|
|
75
|
+
const chunks = [];
|
|
76
|
+
try {
|
|
77
|
+
while (true) {
|
|
78
|
+
const { done, value } = await reader.read();
|
|
79
|
+
if (done) break;
|
|
80
|
+
chunks.push(value);
|
|
81
|
+
receivedLength += value.length;
|
|
82
|
+
progressCallback({
|
|
83
|
+
loaded: receivedLength,
|
|
84
|
+
total: contentLength,
|
|
85
|
+
percentage: contentLength ? receivedLength / contentLength * 100 : 0,
|
|
86
|
+
bytes: formatBytes(receivedLength)
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (response.response && !response.response.bodyUsed) {
|
|
91
|
+
try {
|
|
92
|
+
const text2 = await response.response.text();
|
|
93
|
+
try {
|
|
94
|
+
response.data = JSON.parse(text2);
|
|
95
|
+
} catch {
|
|
96
|
+
response.data = text2;
|
|
97
|
+
}
|
|
98
|
+
} catch (e) {
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return response;
|
|
102
|
+
}
|
|
103
|
+
const chunksAll = new Uint8Array(receivedLength);
|
|
104
|
+
let position = 0;
|
|
105
|
+
for (const chunk of chunks) {
|
|
106
|
+
chunksAll.set(chunk, position);
|
|
107
|
+
position += chunk.length;
|
|
108
|
+
}
|
|
109
|
+
const text = new TextDecoder().decode(chunksAll);
|
|
110
|
+
const originalResponseType = context.originalResponseType;
|
|
111
|
+
const contentType = response.headers.get("content-type") || "";
|
|
112
|
+
if (originalResponseType === "text" || contentType.includes("text/")) {
|
|
113
|
+
response.data = text;
|
|
114
|
+
} else if (originalResponseType === "blob" || contentType.includes("image/")) {
|
|
115
|
+
response.data = new Blob([chunksAll], { type: contentType });
|
|
116
|
+
} else if (originalResponseType === "arrayBuffer") {
|
|
117
|
+
response.data = chunksAll.buffer;
|
|
118
|
+
} else {
|
|
119
|
+
try {
|
|
120
|
+
response.data = JSON.parse(text);
|
|
121
|
+
} catch {
|
|
122
|
+
response.data = text;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return response;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
var index_default = progressPlugin;
|
|
130
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
131
|
+
0 && (module.exports = {
|
|
132
|
+
progressPlugin
|
|
133
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Plugin } from '@fetchmax/core';
|
|
2
|
+
|
|
3
|
+
interface ProgressEvent {
|
|
4
|
+
loaded: number;
|
|
5
|
+
total: number;
|
|
6
|
+
percentage: number;
|
|
7
|
+
bytes: string;
|
|
8
|
+
}
|
|
9
|
+
interface ProgressConfig {
|
|
10
|
+
onUploadProgress?: (event: ProgressEvent) => void;
|
|
11
|
+
onDownloadProgress?: (event: ProgressEvent) => void;
|
|
12
|
+
}
|
|
13
|
+
declare function progressPlugin(config?: ProgressConfig): Plugin;
|
|
14
|
+
|
|
15
|
+
export { type ProgressConfig, type ProgressEvent, progressPlugin as default, progressPlugin };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Plugin } from '@fetchmax/core';
|
|
2
|
+
|
|
3
|
+
interface ProgressEvent {
|
|
4
|
+
loaded: number;
|
|
5
|
+
total: number;
|
|
6
|
+
percentage: number;
|
|
7
|
+
bytes: string;
|
|
8
|
+
}
|
|
9
|
+
interface ProgressConfig {
|
|
10
|
+
onUploadProgress?: (event: ProgressEvent) => void;
|
|
11
|
+
onDownloadProgress?: (event: ProgressEvent) => void;
|
|
12
|
+
}
|
|
13
|
+
declare function progressPlugin(config?: ProgressConfig): Plugin;
|
|
14
|
+
|
|
15
|
+
export { type ProgressConfig, type ProgressEvent, progressPlugin as default, progressPlugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function formatBytes(bytes) {
|
|
3
|
+
if (bytes === 0) return "0 B";
|
|
4
|
+
const k = 1024;
|
|
5
|
+
const sizes = ["B", "KB", "MB", "GB"];
|
|
6
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
7
|
+
return `${(bytes / Math.pow(k, i)).toFixed(2)} ${sizes[i]}`;
|
|
8
|
+
}
|
|
9
|
+
function progressPlugin(config = {}) {
|
|
10
|
+
const { onUploadProgress: _onUploadProgress, onDownloadProgress } = config;
|
|
11
|
+
return {
|
|
12
|
+
name: "progress",
|
|
13
|
+
async onRequest(requestConfig, context) {
|
|
14
|
+
if (onDownloadProgress) {
|
|
15
|
+
context.originalResponseType = requestConfig.responseType;
|
|
16
|
+
context.progressCallback = onDownloadProgress;
|
|
17
|
+
requestConfig.responseType = "stream";
|
|
18
|
+
}
|
|
19
|
+
return requestConfig;
|
|
20
|
+
},
|
|
21
|
+
async onResponse(response, _request, context) {
|
|
22
|
+
const progressCallback = context.progressCallback;
|
|
23
|
+
if (!progressCallback) {
|
|
24
|
+
return response;
|
|
25
|
+
}
|
|
26
|
+
const body = response.data;
|
|
27
|
+
if (!body || typeof body.getReader !== "function") {
|
|
28
|
+
if (response.response && !response.response.bodyUsed) {
|
|
29
|
+
try {
|
|
30
|
+
const text2 = await response.response.text();
|
|
31
|
+
const contentType2 = response.headers.get("content-type") || "";
|
|
32
|
+
const originalResponseType2 = context.originalResponseType;
|
|
33
|
+
if (originalResponseType2 === "text" || contentType2.includes("text/")) {
|
|
34
|
+
response.data = text2;
|
|
35
|
+
} else {
|
|
36
|
+
try {
|
|
37
|
+
response.data = JSON.parse(text2);
|
|
38
|
+
} catch {
|
|
39
|
+
response.data = text2;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
} catch (error) {
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return response;
|
|
46
|
+
}
|
|
47
|
+
const reader = body.getReader();
|
|
48
|
+
const contentLength = +(response.headers.get("Content-Length") || 0);
|
|
49
|
+
let receivedLength = 0;
|
|
50
|
+
const chunks = [];
|
|
51
|
+
try {
|
|
52
|
+
while (true) {
|
|
53
|
+
const { done, value } = await reader.read();
|
|
54
|
+
if (done) break;
|
|
55
|
+
chunks.push(value);
|
|
56
|
+
receivedLength += value.length;
|
|
57
|
+
progressCallback({
|
|
58
|
+
loaded: receivedLength,
|
|
59
|
+
total: contentLength,
|
|
60
|
+
percentage: contentLength ? receivedLength / contentLength * 100 : 0,
|
|
61
|
+
bytes: formatBytes(receivedLength)
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
} catch (error) {
|
|
65
|
+
if (response.response && !response.response.bodyUsed) {
|
|
66
|
+
try {
|
|
67
|
+
const text2 = await response.response.text();
|
|
68
|
+
try {
|
|
69
|
+
response.data = JSON.parse(text2);
|
|
70
|
+
} catch {
|
|
71
|
+
response.data = text2;
|
|
72
|
+
}
|
|
73
|
+
} catch (e) {
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return response;
|
|
77
|
+
}
|
|
78
|
+
const chunksAll = new Uint8Array(receivedLength);
|
|
79
|
+
let position = 0;
|
|
80
|
+
for (const chunk of chunks) {
|
|
81
|
+
chunksAll.set(chunk, position);
|
|
82
|
+
position += chunk.length;
|
|
83
|
+
}
|
|
84
|
+
const text = new TextDecoder().decode(chunksAll);
|
|
85
|
+
const originalResponseType = context.originalResponseType;
|
|
86
|
+
const contentType = response.headers.get("content-type") || "";
|
|
87
|
+
if (originalResponseType === "text" || contentType.includes("text/")) {
|
|
88
|
+
response.data = text;
|
|
89
|
+
} else if (originalResponseType === "blob" || contentType.includes("image/")) {
|
|
90
|
+
response.data = new Blob([chunksAll], { type: contentType });
|
|
91
|
+
} else if (originalResponseType === "arrayBuffer") {
|
|
92
|
+
response.data = chunksAll.buffer;
|
|
93
|
+
} else {
|
|
94
|
+
try {
|
|
95
|
+
response.data = JSON.parse(text);
|
|
96
|
+
} catch {
|
|
97
|
+
response.data = text;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return response;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
var index_default = progressPlugin;
|
|
105
|
+
export {
|
|
106
|
+
index_default as default,
|
|
107
|
+
progressPlugin
|
|
108
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fetchmax/plugin-progress",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Progress tracking plugin for FetchMax to monitor upload and download progress",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
22
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"fetchmax",
|
|
26
|
+
"plugin",
|
|
27
|
+
"progress",
|
|
28
|
+
"upload",
|
|
29
|
+
"download",
|
|
30
|
+
"http",
|
|
31
|
+
"fetch"
|
|
32
|
+
],
|
|
33
|
+
"author": "FetchMax Contributors",
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "https://github.com/fetchmax/fetchmax.git",
|
|
38
|
+
"directory": "packages/plugins/progress"
|
|
39
|
+
},
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/fetchmax/fetchmax/issues"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/fetchmax/fetchmax#readme",
|
|
44
|
+
"sideEffects": false,
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=18.0.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"@fetchmax/core": "^1.0.0"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@fetchmax/core": "*",
|
|
56
|
+
"tsup": "^8.0.1",
|
|
57
|
+
"typescript": "^5.3.3"
|
|
58
|
+
}
|
|
59
|
+
}
|