kap-r2 1.0.4 → 1.0.6
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.js +17 -19
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.shareServices = void 0;
|
|
7
|
-
const fs_1 = require("fs");
|
|
8
7
|
const promises_1 = require("fs/promises");
|
|
9
8
|
const path_1 = __importDefault(require("path"));
|
|
10
9
|
const https_1 = __importDefault(require("https"));
|
|
@@ -51,11 +50,12 @@ const action = async (context) => {
|
|
|
51
50
|
const key = directory ? path_1.default.posix.join(directory, filename) : filename;
|
|
52
51
|
const extension = path_1.default.extname(filename);
|
|
53
52
|
const contentType = contentTypes.get(extension) || "application/octet-stream";
|
|
54
|
-
const host = `${
|
|
53
|
+
const host = `${accountId}.r2.cloudflarestorage.com`;
|
|
55
54
|
const encodedKey = key.split("/").map(encodeURIComponent).join("/");
|
|
55
|
+
const fullPath = `/${bucket}/${encodedKey}`;
|
|
56
56
|
const request = aws4_1.default.sign({
|
|
57
57
|
host,
|
|
58
|
-
path:
|
|
58
|
+
path: fullPath,
|
|
59
59
|
method: "PUT",
|
|
60
60
|
headers: {
|
|
61
61
|
"Content-Type": contentType,
|
|
@@ -72,27 +72,25 @@ const action = async (context) => {
|
|
|
72
72
|
await new Promise((resolve, reject) => {
|
|
73
73
|
const req = https_1.default.request({
|
|
74
74
|
hostname: host,
|
|
75
|
-
path:
|
|
75
|
+
path: fullPath,
|
|
76
76
|
method: "PUT",
|
|
77
77
|
headers: request.headers,
|
|
78
78
|
}, (res) => {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
79
|
+
const chunks = [];
|
|
80
|
+
res.on("data", (chunk) => chunks.push(chunk));
|
|
81
|
+
res.on("end", () => {
|
|
82
|
+
const body = Buffer.concat(chunks).toString();
|
|
83
|
+
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
|
|
84
|
+
resolve();
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
reject(new Error(`HTTP ${res.statusCode}: ${body}`));
|
|
88
|
+
}
|
|
89
|
+
});
|
|
87
90
|
});
|
|
88
91
|
req.on("error", reject);
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
stream.on("data", (chunk) => {
|
|
92
|
-
uploaded += Buffer.byteLength(chunk);
|
|
93
|
-
context.setProgress("Uploading to R2…", uploaded / fileStats.size);
|
|
94
|
-
});
|
|
95
|
-
stream.pipe(req);
|
|
92
|
+
context.setProgress("Uploading to R2…", 0.5);
|
|
93
|
+
req.end(fileBuffer);
|
|
96
94
|
});
|
|
97
95
|
const baseUrl = publicUrl.replace(/\/$/, "");
|
|
98
96
|
const uploadUrl = `${baseUrl}/${encodedKey}`;
|