kap-r2 1.0.6 → 1.0.7
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 +21 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -7,7 +7,13 @@ exports.shareServices = void 0;
|
|
|
7
7
|
const promises_1 = require("fs/promises");
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
9
|
const https_1 = __importDefault(require("https"));
|
|
10
|
+
const os_1 = __importDefault(require("os"));
|
|
10
11
|
const aws4_1 = __importDefault(require("aws4"));
|
|
12
|
+
const logFile = path_1.default.join(os_1.default.homedir(), "kap-r2-debug.log");
|
|
13
|
+
const log = async (msg) => {
|
|
14
|
+
const line = `[${new Date().toISOString()}] ${msg}\n`;
|
|
15
|
+
await (0, promises_1.appendFile)(logFile, line).catch(() => { });
|
|
16
|
+
};
|
|
11
17
|
const contentTypes = new Map([
|
|
12
18
|
[".gif", "image/gif"],
|
|
13
19
|
[".mp4", "video/mp4"],
|
|
@@ -45,6 +51,9 @@ const action = async (context) => {
|
|
|
45
51
|
const filePath = await context.filePath();
|
|
46
52
|
const fileStats = await (0, promises_1.stat)(filePath);
|
|
47
53
|
const fileBuffer = await (0, promises_1.readFile)(filePath);
|
|
54
|
+
await log(`--- New upload ---`);
|
|
55
|
+
await log(`File: ${filePath}, Size: ${fileStats.size}`);
|
|
56
|
+
await log(`Account: ${accountId}, Bucket: ${bucket}`);
|
|
48
57
|
context.setProgress("Uploading to R2…", 0);
|
|
49
58
|
const filename = path_1.default.basename(filePath);
|
|
50
59
|
const key = directory ? path_1.default.posix.join(directory, filename) : filename;
|
|
@@ -53,6 +62,9 @@ const action = async (context) => {
|
|
|
53
62
|
const host = `${accountId}.r2.cloudflarestorage.com`;
|
|
54
63
|
const encodedKey = key.split("/").map(encodeURIComponent).join("/");
|
|
55
64
|
const fullPath = `/${bucket}/${encodedKey}`;
|
|
65
|
+
await log(`Host: ${host}`);
|
|
66
|
+
await log(`Path: ${fullPath}`);
|
|
67
|
+
await log(`Content-Type: ${contentType}`);
|
|
56
68
|
const request = aws4_1.default.sign({
|
|
57
69
|
host,
|
|
58
70
|
path: fullPath,
|
|
@@ -68,8 +80,10 @@ const action = async (context) => {
|
|
|
68
80
|
accessKeyId,
|
|
69
81
|
secretAccessKey,
|
|
70
82
|
});
|
|
83
|
+
await log(`Signed headers: ${JSON.stringify(request.headers)}`);
|
|
71
84
|
try {
|
|
72
85
|
await new Promise((resolve, reject) => {
|
|
86
|
+
log(`Making HTTPS request...`);
|
|
73
87
|
const req = https_1.default.request({
|
|
74
88
|
hostname: host,
|
|
75
89
|
path: fullPath,
|
|
@@ -78,8 +92,10 @@ const action = async (context) => {
|
|
|
78
92
|
}, (res) => {
|
|
79
93
|
const chunks = [];
|
|
80
94
|
res.on("data", (chunk) => chunks.push(chunk));
|
|
81
|
-
res.on("end", () => {
|
|
95
|
+
res.on("end", async () => {
|
|
82
96
|
const body = Buffer.concat(chunks).toString();
|
|
97
|
+
await log(`Response status: ${res.statusCode}`);
|
|
98
|
+
await log(`Response body: ${body}`);
|
|
83
99
|
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
|
|
84
100
|
resolve();
|
|
85
101
|
}
|
|
@@ -88,7 +104,10 @@ const action = async (context) => {
|
|
|
88
104
|
}
|
|
89
105
|
});
|
|
90
106
|
});
|
|
91
|
-
req.on("error",
|
|
107
|
+
req.on("error", (err) => {
|
|
108
|
+
log(`Request error: ${err.message}`);
|
|
109
|
+
reject(err);
|
|
110
|
+
});
|
|
92
111
|
context.setProgress("Uploading to R2…", 0.5);
|
|
93
112
|
req.end(fileBuffer);
|
|
94
113
|
});
|