@arraypress/storage-r2 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/LICENSE +21 -0
- package/README.md +135 -0
- package/package.json +33 -0
- package/src/index.d.ts +11 -0
- package/src/index.js +156 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 David Sherlock / ArrayPress Limited
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# @arraypress/storage-r2
|
|
2
|
+
|
|
3
|
+
Cloudflare R2 native bindings adapter for `@arraypress/storage`. Uses the R2Bucket API directly for zero SDK overhead and maximum performance on Cloudflare Workers.
|
|
4
|
+
|
|
5
|
+
Works in Cloudflare Workers only (requires R2 bucket binding).
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @arraypress/storage-r2 @arraypress/storage
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### With Hono on Cloudflare Workers
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { Hono } from 'hono';
|
|
19
|
+
import { createR2Storage } from '@arraypress/storage-r2';
|
|
20
|
+
|
|
21
|
+
type Env = {
|
|
22
|
+
Bindings: {
|
|
23
|
+
BUCKET: R2Bucket;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const app = new Hono<Env>();
|
|
28
|
+
|
|
29
|
+
app.post('/upload', async (c) => {
|
|
30
|
+
const storage = createR2Storage({
|
|
31
|
+
bucket: c.env.BUCKET,
|
|
32
|
+
publicUrl: 'https://media.mystore.com',
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const body = await c.req.arrayBuffer();
|
|
36
|
+
|
|
37
|
+
const result = await storage.upload({
|
|
38
|
+
key: 'uploads/photo.jpg',
|
|
39
|
+
body: new Uint8Array(body),
|
|
40
|
+
contentType: 'image/jpeg',
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return c.json(result);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
app.get('/download/:key{.+}', async (c) => {
|
|
47
|
+
const storage = createR2Storage({ bucket: c.env.BUCKET });
|
|
48
|
+
const file = await storage.download(c.req.param('key'));
|
|
49
|
+
|
|
50
|
+
if (!file) return c.notFound();
|
|
51
|
+
|
|
52
|
+
return new Response(file.body, {
|
|
53
|
+
headers: {
|
|
54
|
+
'Content-Type': file.contentType,
|
|
55
|
+
'Content-Length': String(file.size),
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
export default app;
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### wrangler.toml
|
|
64
|
+
|
|
65
|
+
```toml
|
|
66
|
+
[[r2_buckets]]
|
|
67
|
+
binding = "BUCKET"
|
|
68
|
+
bucket_name = "my-bucket"
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Content-Addressed Uploads (Deduplication)
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { createR2Storage } from '@arraypress/storage-r2';
|
|
75
|
+
import { contentHash, contentAddressedKey } from '@arraypress/storage';
|
|
76
|
+
|
|
77
|
+
const storage = createR2Storage({ bucket: env.BUCKET });
|
|
78
|
+
const hash = await contentHash(fileBuffer);
|
|
79
|
+
const key = contentAddressedKey(hash, 'photo.jpg', 'media/');
|
|
80
|
+
|
|
81
|
+
if (!await storage.exists(key)) {
|
|
82
|
+
await storage.upload({ key, body: fileBuffer, contentType: 'image/jpeg' });
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Multipart Uploads
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
const storage = createR2Storage({ bucket: env.BUCKET });
|
|
90
|
+
|
|
91
|
+
const mpu = await storage.createMultipartUpload('large-file.zip', {
|
|
92
|
+
contentType: 'application/zip',
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const part1 = await mpu.uploadPart(1, chunk1);
|
|
96
|
+
const part2 = await mpu.uploadPart(2, chunk2);
|
|
97
|
+
|
|
98
|
+
await mpu.complete([part1, part2]);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Signed URLs
|
|
102
|
+
|
|
103
|
+
R2 native bindings have limited presigned URL support. If you need reliable signed URLs (e.g. for direct client uploads), use `@arraypress/storage-s3` with R2's S3-compatible API instead.
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
// May throw StorageError with code 'NOT_SUPPORTED' depending on environment
|
|
107
|
+
const signed = await storage.getSignedDownloadUrl({
|
|
108
|
+
key: 'uploads/photo.jpg',
|
|
109
|
+
expiresIn: 3600,
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## API Reference
|
|
114
|
+
|
|
115
|
+
### `createR2Storage(options): Storage`
|
|
116
|
+
|
|
117
|
+
Creates a Storage adapter backed by Cloudflare R2 native bindings.
|
|
118
|
+
|
|
119
|
+
#### Options
|
|
120
|
+
|
|
121
|
+
| Option | Type | Required | Description |
|
|
122
|
+
|--------|------|----------|-------------|
|
|
123
|
+
| `bucket` | `R2Bucket` | Yes | R2 bucket binding from `wrangler.toml` |
|
|
124
|
+
| `publicUrl` | `string` | No | Public URL prefix for `getPublicUrl()` (e.g. `'https://media.mystore.com'`) |
|
|
125
|
+
|
|
126
|
+
### Re-exported from `@arraypress/storage`
|
|
127
|
+
|
|
128
|
+
- `Storage` interface
|
|
129
|
+
- `StorageError` class
|
|
130
|
+
- `contentHash()`, `contentAddressedKey()`, `safeDisposition()` helpers
|
|
131
|
+
- All type definitions
|
|
132
|
+
|
|
133
|
+
## License
|
|
134
|
+
|
|
135
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@arraypress/storage-r2",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Cloudflare R2 native bindings adapter for @arraypress/storage",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"types": "src/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.d.ts",
|
|
11
|
+
"import": "./src/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"src"
|
|
16
|
+
],
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@arraypress/storage": "^1.0.0"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"storage",
|
|
22
|
+
"r2",
|
|
23
|
+
"cloudflare",
|
|
24
|
+
"workers",
|
|
25
|
+
"adapter"
|
|
26
|
+
],
|
|
27
|
+
"author": "ArrayPress",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/arraypress/storage-r2"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Storage } from '@arraypress/storage';
|
|
2
|
+
|
|
3
|
+
export interface R2StorageOptions {
|
|
4
|
+
/** R2 bucket binding from wrangler.toml. */
|
|
5
|
+
bucket: R2Bucket;
|
|
6
|
+
/** Public URL prefix for the bucket (e.g. 'https://media.mystore.com'). Used by getPublicUrl(). */
|
|
7
|
+
publicUrl?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Create a Storage adapter backed by Cloudflare R2 native bindings. */
|
|
11
|
+
export function createR2Storage(options: R2StorageOptions): Storage;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @arraypress/storage-r2
|
|
3
|
+
*
|
|
4
|
+
* Cloudflare R2 native bindings adapter for @arraypress/storage.
|
|
5
|
+
* Uses R2Bucket API directly — faster than S3 API, no SDK overhead.
|
|
6
|
+
*
|
|
7
|
+
* @module @arraypress/storage-r2
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { StorageError } from '@arraypress/storage';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Create a Storage adapter backed by Cloudflare R2 native bindings.
|
|
14
|
+
*
|
|
15
|
+
* @param {import('./index.d.ts').R2StorageOptions} options
|
|
16
|
+
* @returns {import('@arraypress/storage').Storage}
|
|
17
|
+
*/
|
|
18
|
+
export function createR2Storage(options) {
|
|
19
|
+
const { bucket, publicUrl } = options;
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
async upload({ key, body, contentType, metadata }) {
|
|
23
|
+
const object = await bucket.put(key, body, {
|
|
24
|
+
httpMetadata: { contentType },
|
|
25
|
+
...(metadata ? { customMetadata: metadata } : {}),
|
|
26
|
+
});
|
|
27
|
+
return {
|
|
28
|
+
key,
|
|
29
|
+
size: object?.size ?? 0,
|
|
30
|
+
etag: object?.etag,
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
async download(key) {
|
|
35
|
+
const object = await bucket.get(key);
|
|
36
|
+
if (!object) return null;
|
|
37
|
+
return {
|
|
38
|
+
body: object.body,
|
|
39
|
+
contentType: object.httpMetadata?.contentType || 'application/octet-stream',
|
|
40
|
+
size: object.size,
|
|
41
|
+
etag: object.etag,
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
async delete(key) {
|
|
46
|
+
await bucket.delete(key);
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
async exists(key) {
|
|
50
|
+
const head = await bucket.head(key);
|
|
51
|
+
return head !== null;
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
async list(options = {}) {
|
|
55
|
+
const { prefix, limit, cursor } = options;
|
|
56
|
+
const result = await bucket.list({
|
|
57
|
+
...(prefix ? { prefix } : {}),
|
|
58
|
+
...(limit ? { limit } : {}),
|
|
59
|
+
...(cursor ? { cursor } : {}),
|
|
60
|
+
});
|
|
61
|
+
return {
|
|
62
|
+
objects: result.objects.map((obj) => ({
|
|
63
|
+
key: obj.key,
|
|
64
|
+
size: obj.size,
|
|
65
|
+
lastModified: obj.uploaded ? new Date(obj.uploaded) : undefined,
|
|
66
|
+
etag: obj.etag,
|
|
67
|
+
})),
|
|
68
|
+
truncated: result.truncated,
|
|
69
|
+
cursor: result.truncated ? result.cursor : undefined,
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
async getSignedDownloadUrl({ key, expiresIn = 3600 }) {
|
|
74
|
+
try {
|
|
75
|
+
// R2 presigned URL support — may not be available in all environments
|
|
76
|
+
const url = await /** @type {any} */ (bucket).createPresignedUrl(key, {
|
|
77
|
+
expiresIn,
|
|
78
|
+
httpMethod: 'GET',
|
|
79
|
+
});
|
|
80
|
+
if (!url) throw new Error('No URL returned');
|
|
81
|
+
return {
|
|
82
|
+
url,
|
|
83
|
+
expiresAt: new Date(Date.now() + expiresIn * 1000),
|
|
84
|
+
};
|
|
85
|
+
} catch {
|
|
86
|
+
throw new StorageError(
|
|
87
|
+
'R2 presigned download URLs are not supported in this environment',
|
|
88
|
+
'NOT_SUPPORTED',
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
async getSignedUploadUrl({ key, contentType, expiresIn = 3600 }) {
|
|
94
|
+
try {
|
|
95
|
+
const url = await /** @type {any} */ (bucket).createPresignedUrl(key, {
|
|
96
|
+
expiresIn,
|
|
97
|
+
httpMethod: 'PUT',
|
|
98
|
+
});
|
|
99
|
+
if (!url) throw new Error('No URL returned');
|
|
100
|
+
return {
|
|
101
|
+
url,
|
|
102
|
+
expiresAt: new Date(Date.now() + expiresIn * 1000),
|
|
103
|
+
};
|
|
104
|
+
} catch {
|
|
105
|
+
throw new StorageError(
|
|
106
|
+
'R2 presigned upload URLs are not supported in this environment',
|
|
107
|
+
'NOT_SUPPORTED',
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
getPublicUrl(key) {
|
|
113
|
+
if (!publicUrl) {
|
|
114
|
+
throw new StorageError(
|
|
115
|
+
'publicUrl not configured — pass it to createR2Storage()',
|
|
116
|
+
'NOT_SUPPORTED',
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
return `${publicUrl.replace(/\/$/, '')}/${key}`;
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
async createMultipartUpload(key, options = {}) {
|
|
123
|
+
const { contentType, metadata } = options;
|
|
124
|
+
const mpu = await bucket.createMultipartUpload(key, {
|
|
125
|
+
...(contentType ? { httpMetadata: { contentType } } : {}),
|
|
126
|
+
...(metadata ? { customMetadata: metadata } : {}),
|
|
127
|
+
});
|
|
128
|
+
return wrapMultipartUpload(mpu, key);
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
resumeMultipartUpload(key, uploadId) {
|
|
132
|
+
const mpu = bucket.resumeMultipartUpload(key, uploadId);
|
|
133
|
+
return wrapMultipartUpload(mpu, key);
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Wrap an R2 multipart upload object into the Storage MultipartUpload interface.
|
|
140
|
+
*/
|
|
141
|
+
function wrapMultipartUpload(mpu, key) {
|
|
142
|
+
return {
|
|
143
|
+
uploadId: mpu.uploadId,
|
|
144
|
+
key,
|
|
145
|
+
async uploadPart(partNumber, body) {
|
|
146
|
+
const part = await mpu.uploadPart(partNumber, body);
|
|
147
|
+
return { partNumber: part.partNumber, etag: part.etag };
|
|
148
|
+
},
|
|
149
|
+
async complete(parts) {
|
|
150
|
+
await mpu.complete(parts);
|
|
151
|
+
},
|
|
152
|
+
async abort() {
|
|
153
|
+
await mpu.abort();
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
}
|