kap-r2 1.0.6 → 1.0.8

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.
Files changed (2) hide show
  1. package/dist/index.js +26 -2
  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"],
@@ -21,18 +27,23 @@ const isValidBucketName = (name) => {
21
27
  return /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/.test(name);
22
28
  };
23
29
  const action = async (context) => {
30
+ await log(`--- Action started ---`);
24
31
  const accountId = context.config.get("accountId");
25
32
  const accessKeyId = context.config.get("accessKeyId");
26
33
  const secretAccessKey = context.config.get("secretAccessKey");
27
34
  const bucket = context.config.get("bucket");
28
35
  const directory = context.config.get("directory") || "";
29
36
  const publicUrl = context.config.get("publicUrl");
37
+ await log(`AccountId: ${accountId}, Bucket: ${bucket}, PublicUrl: ${publicUrl}`);
38
+ await log(`AccountId valid: ${isValidAccountId(accountId)}, Bucket valid: ${isValidBucketName(bucket)}`);
30
39
  if (!isValidAccountId(accountId)) {
40
+ await log(`FAIL: Invalid account ID`);
31
41
  context.notify("Invalid Account ID. It should be a 32-character hex string.");
32
42
  context.cancel();
33
43
  return;
34
44
  }
35
45
  if (!isValidBucketName(bucket)) {
46
+ await log(`FAIL: Invalid bucket name`);
36
47
  context.notify("Invalid Bucket Name. Use lowercase letters, numbers, and hyphens.");
37
48
  context.cancel();
38
49
  return;
@@ -45,6 +56,9 @@ const action = async (context) => {
45
56
  const filePath = await context.filePath();
46
57
  const fileStats = await (0, promises_1.stat)(filePath);
47
58
  const fileBuffer = await (0, promises_1.readFile)(filePath);
59
+ await log(`--- New upload ---`);
60
+ await log(`File: ${filePath}, Size: ${fileStats.size}`);
61
+ await log(`Account: ${accountId}, Bucket: ${bucket}`);
48
62
  context.setProgress("Uploading to R2…", 0);
49
63
  const filename = path_1.default.basename(filePath);
50
64
  const key = directory ? path_1.default.posix.join(directory, filename) : filename;
@@ -53,6 +67,9 @@ const action = async (context) => {
53
67
  const host = `${accountId}.r2.cloudflarestorage.com`;
54
68
  const encodedKey = key.split("/").map(encodeURIComponent).join("/");
55
69
  const fullPath = `/${bucket}/${encodedKey}`;
70
+ await log(`Host: ${host}`);
71
+ await log(`Path: ${fullPath}`);
72
+ await log(`Content-Type: ${contentType}`);
56
73
  const request = aws4_1.default.sign({
57
74
  host,
58
75
  path: fullPath,
@@ -68,8 +85,10 @@ const action = async (context) => {
68
85
  accessKeyId,
69
86
  secretAccessKey,
70
87
  });
88
+ await log(`Signed headers: ${JSON.stringify(request.headers)}`);
71
89
  try {
72
90
  await new Promise((resolve, reject) => {
91
+ log(`Making HTTPS request...`);
73
92
  const req = https_1.default.request({
74
93
  hostname: host,
75
94
  path: fullPath,
@@ -78,8 +97,10 @@ const action = async (context) => {
78
97
  }, (res) => {
79
98
  const chunks = [];
80
99
  res.on("data", (chunk) => chunks.push(chunk));
81
- res.on("end", () => {
100
+ res.on("end", async () => {
82
101
  const body = Buffer.concat(chunks).toString();
102
+ await log(`Response status: ${res.statusCode}`);
103
+ await log(`Response body: ${body}`);
83
104
  if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
84
105
  resolve();
85
106
  }
@@ -88,7 +109,10 @@ const action = async (context) => {
88
109
  }
89
110
  });
90
111
  });
91
- req.on("error", reject);
112
+ req.on("error", (err) => {
113
+ log(`Request error: ${err.message}`);
114
+ reject(err);
115
+ });
92
116
  context.setProgress("Uploading to R2…", 0.5);
93
117
  req.end(fileBuffer);
94
118
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kap-r2",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "Share on Cloudflare R2",
5
5
  "license": "MIT",
6
6
  "repository": "tristanremy/kap-r2",