kap-r2 1.0.1 → 1.0.2

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 +45 -30
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { createReadStream } from "node:fs";
2
- import { stat } from "node:fs/promises";
2
+ import { stat, readFile } from "node:fs/promises";
3
3
  import path from "node:path";
4
- import { S3Client } from "@aws-sdk/client-s3";
5
- import { Upload } from "@aws-sdk/lib-storage";
4
+ import https from "node:https";
5
+ import aws4 from "aws4";
6
6
  const contentTypes = new Map([
7
7
  [".gif", "image/gif"],
8
8
  [".mp4", "video/mp4"],
@@ -39,41 +39,56 @@ const action = async (context) => {
39
39
  }
40
40
  const filePath = await context.filePath();
41
41
  const fileStats = await stat(filePath);
42
+ const fileBuffer = await readFile(filePath);
42
43
  context.setProgress("Uploading to R2…", 0);
43
- const client = new S3Client({
44
- region: "auto",
45
- endpoint: `https://${accountId}.r2.cloudflarestorage.com`,
46
- credentials: {
47
- accessKeyId,
48
- secretAccessKey,
49
- },
50
- });
51
44
  const filename = path.basename(filePath);
52
45
  const key = directory ? path.posix.join(directory, filename) : filename;
53
46
  const extension = path.extname(filename);
54
47
  const contentType = contentTypes.get(extension) || "application/octet-stream";
48
+ const host = `${bucket}.${accountId}.r2.cloudflarestorage.com`;
49
+ const encodedKey = key.split("/").map(encodeURIComponent).join("/");
50
+ const request = aws4.sign({
51
+ host,
52
+ path: `/${encodedKey}`,
53
+ method: "PUT",
54
+ headers: {
55
+ "Content-Type": contentType,
56
+ "Content-Length": fileStats.size,
57
+ },
58
+ body: fileBuffer,
59
+ service: "s3",
60
+ region: "auto",
61
+ }, {
62
+ accessKeyId,
63
+ secretAccessKey,
64
+ });
55
65
  try {
56
- const upload = new Upload({
57
- client,
58
- params: {
59
- Bucket: bucket,
60
- Key: key,
61
- Body: createReadStream(filePath),
62
- ContentType: contentType,
63
- },
64
- queueSize: 4,
65
- partSize: 1024 * 1024 * 5,
66
- leavePartsOnError: false,
67
- });
68
- upload.on("httpUploadProgress", (progress) => {
69
- if (progress.loaded && fileStats.size) {
70
- const percentage = progress.loaded / fileStats.size;
71
- context.setProgress("Uploading to R2…", percentage);
72
- }
66
+ await new Promise((resolve, reject) => {
67
+ const req = https.request({
68
+ hostname: host,
69
+ path: `/${encodedKey}`,
70
+ method: "PUT",
71
+ headers: request.headers,
72
+ }, (res) => {
73
+ if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
74
+ resolve();
75
+ }
76
+ else {
77
+ const chunks = [];
78
+ res.on("data", (chunk) => chunks.push(chunk));
79
+ res.on("end", () => reject(new Error(`HTTP ${res.statusCode}: ${Buffer.concat(chunks).toString()}`)));
80
+ }
81
+ });
82
+ req.on("error", reject);
83
+ const stream = createReadStream(filePath);
84
+ let uploaded = 0;
85
+ stream.on("data", (chunk) => {
86
+ uploaded += Buffer.byteLength(chunk);
87
+ context.setProgress("Uploading to R2…", uploaded / fileStats.size);
88
+ });
89
+ stream.pipe(req);
73
90
  });
74
- await upload.done();
75
91
  const baseUrl = publicUrl.replace(/\/$/, "");
76
- const encodedKey = key.split("/").map(encodeURIComponent).join("/");
77
92
  const uploadUrl = `${baseUrl}/${encodedKey}`;
78
93
  context.copyToClipboard(uploadUrl);
79
94
  context.notify("R2 URL copied to the clipboard");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kap-r2",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Share on Cloudflare R2",
5
5
  "license": "MIT",
6
6
  "repository": "tristanremy/kap-r2",
@@ -26,15 +26,15 @@
26
26
  "version": ">=3.0.0"
27
27
  },
28
28
  "dependencies": {
29
- "@aws-sdk/client-s3": "^3.728.0",
30
- "@aws-sdk/lib-storage": "^3.728.0"
29
+ "aws4": "^1.13.2"
31
30
  },
32
31
  "devDependencies": {
32
+ "@types/aws4": "^1.11.6",
33
33
  "@types/node": "^20.0.0",
34
34
  "typescript": "^5.0.0"
35
35
  },
36
36
  "engines": {
37
- "node": ">=18"
37
+ "node": ">=14"
38
38
  },
39
39
  "scripts": {
40
40
  "build": "tsc"