@storecraft/storage-s3-compatible 1.0.7 → 1.0.9
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/README.md +2 -0
- package/adapter.js +40 -28
- package/package.json +4 -4
- package/tests/storage.s3-compatible.test.js +22 -0
package/README.md
CHANGED
@@ -5,6 +5,8 @@
|
|
5
5
|
width='90%' />
|
6
6
|
</div><hr/><br/>
|
7
7
|
|
8
|
+
[](https://github.com/store-craft/storecraft/actions/workflows/test.storage-s3-compatible.yml)
|
9
|
+
|
8
10
|
`fetch` ready support for an `S3` like storage:
|
9
11
|
- `Amazon S3`
|
10
12
|
- `Cloudflare R2`
|
package/adapter.js
CHANGED
@@ -95,23 +95,30 @@ export class S3CompatibleStorage {
|
|
95
95
|
*
|
96
96
|
* @param {string} key
|
97
97
|
* @param {BodyInit} body
|
98
|
+
* @param {object} [headers={}]
|
98
99
|
*/
|
99
|
-
async #put_internal(key, body) {
|
100
|
+
async #put_internal(key, body, headers={}) {
|
100
101
|
const r = await this.client.fetch(
|
101
102
|
this.get_file_url(key),
|
102
103
|
{
|
103
104
|
method: 'PUT',
|
104
|
-
body
|
105
|
+
body,
|
106
|
+
headers
|
105
107
|
}
|
106
108
|
);
|
107
109
|
|
110
|
+
if(!r.ok) {
|
111
|
+
console.log(
|
112
|
+
await r.text()
|
113
|
+
);
|
114
|
+
}
|
115
|
+
|
108
116
|
return r.ok;
|
109
117
|
}
|
110
118
|
|
111
119
|
/**
|
112
120
|
*
|
113
|
-
* @
|
114
|
-
* @param {Blob} blob
|
121
|
+
* @type {storage["putBlob"]}
|
115
122
|
*/
|
116
123
|
async putBlob(key, blob) {
|
117
124
|
return this.#put_internal(key, blob);
|
@@ -119,8 +126,7 @@ export class S3CompatibleStorage {
|
|
119
126
|
|
120
127
|
/**
|
121
128
|
*
|
122
|
-
* @
|
123
|
-
* @param {ArrayBuffer} buffer
|
129
|
+
* @type {storage["putArraybuffer"]}
|
124
130
|
*/
|
125
131
|
async putArraybuffer(key, buffer) {
|
126
132
|
return this.#put_internal(key, buffer);
|
@@ -128,17 +134,23 @@ export class S3CompatibleStorage {
|
|
128
134
|
|
129
135
|
/**
|
130
136
|
*
|
131
|
-
* @
|
132
|
-
* @param {ReadableStream} stream
|
137
|
+
* @type {storage["putStream"]}
|
133
138
|
*/
|
134
|
-
async putStream(key, stream) {
|
135
|
-
|
139
|
+
async putStream(key, stream, meta={}, bytesLength=0) {
|
140
|
+
const extra_headers = {};
|
141
|
+
if(Boolean(bytesLength)) {
|
142
|
+
extra_headers["Content-Length"] = bytesLength;
|
143
|
+
}
|
144
|
+
|
145
|
+
return this.#put_internal(
|
146
|
+
// @ts-ignore
|
147
|
+
key, stream, extra_headers
|
148
|
+
);
|
136
149
|
}
|
137
150
|
|
138
151
|
/**
|
139
152
|
*
|
140
|
-
* @
|
141
|
-
* @returns {ReturnType<import('@storecraft/core/storage').storage_driver["putSigned"]>}
|
153
|
+
* @type {storage["putSigned"]}
|
142
154
|
*/
|
143
155
|
async putSigned(key) {
|
144
156
|
const url = new URL(this.get_file_url(key));
|
@@ -171,14 +183,14 @@ export class S3CompatibleStorage {
|
|
171
183
|
}
|
172
184
|
|
173
185
|
/**
|
174
|
-
*
|
175
|
-
* @param {string} key
|
186
|
+
* @type {storage["getArraybuffer"]}
|
176
187
|
*/
|
177
188
|
async getArraybuffer(key) {
|
178
189
|
const r = await this.#get_request(key);
|
179
|
-
const b = await r.arrayBuffer();
|
180
190
|
return {
|
181
|
-
value:
|
191
|
+
value: r.ok ? (await r.arrayBuffer()) : undefined,
|
192
|
+
error: !r.ok,
|
193
|
+
message: r.ok ? undefined : await r.text(),
|
182
194
|
metadata: {
|
183
195
|
contentType: infer_content_type(key)
|
184
196
|
}
|
@@ -187,13 +199,14 @@ export class S3CompatibleStorage {
|
|
187
199
|
|
188
200
|
/**
|
189
201
|
*
|
190
|
-
* @
|
202
|
+
* @type {storage["getBlob"]}
|
191
203
|
*/
|
192
204
|
async getBlob(key) {
|
193
205
|
const r = await this.#get_request(key);
|
194
|
-
const b = await r.blob();
|
195
206
|
return {
|
196
|
-
value:
|
207
|
+
value: r.ok ? (await r.blob()) : undefined,
|
208
|
+
error: !r.ok,
|
209
|
+
message: r.ok ? undefined : await r.text(),
|
197
210
|
metadata: {
|
198
211
|
contentType: infer_content_type(key)
|
199
212
|
}
|
@@ -201,15 +214,15 @@ export class S3CompatibleStorage {
|
|
201
214
|
}
|
202
215
|
|
203
216
|
/**
|
204
|
-
*
|
205
|
-
* @param {string} key
|
206
|
-
* @param {Response} key
|
217
|
+
* @type {storage["getStream"]}
|
207
218
|
*/
|
208
219
|
async getStream(key) {
|
209
|
-
|
210
|
-
const
|
220
|
+
const r = await this.#get_request(key);
|
221
|
+
const b = r.body;
|
211
222
|
return {
|
212
|
-
value:
|
223
|
+
value: r.ok ? b : undefined,
|
224
|
+
error: !r.ok,
|
225
|
+
message: r.ok ? undefined : await r.text(),
|
213
226
|
metadata: {
|
214
227
|
contentType: infer_content_type(key)
|
215
228
|
}
|
@@ -218,8 +231,7 @@ export class S3CompatibleStorage {
|
|
218
231
|
|
219
232
|
/**
|
220
233
|
*
|
221
|
-
* @
|
222
|
-
* @returns {ReturnType<import('@storecraft/core/storage').storage_driver["getSigned"]>}
|
234
|
+
* @type {storage["getSigned"]}
|
223
235
|
*/
|
224
236
|
async getSigned(key) {
|
225
237
|
const url = new URL(this.get_file_url(key));
|
@@ -244,7 +256,7 @@ export class S3CompatibleStorage {
|
|
244
256
|
|
245
257
|
/**
|
246
258
|
*
|
247
|
-
* @
|
259
|
+
* @type {storage["remove"]}
|
248
260
|
*/
|
249
261
|
async remove(key) {
|
250
262
|
const r = await this.client.fetch(
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@storecraft/storage-s3-compatible",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.9",
|
4
4
|
"description": "Official S3-Compatible Storage adapter for storecraft",
|
5
5
|
"license": "MIT",
|
6
6
|
"author": "Tomer Shalev (https://github.com/store-craft)",
|
@@ -17,9 +17,9 @@
|
|
17
17
|
"storecraft"
|
18
18
|
],
|
19
19
|
"scripts": {
|
20
|
-
"
|
21
|
-
"
|
22
|
-
"
|
20
|
+
"test": "node ./tests/storage.s3-compatible.test.js",
|
21
|
+
"prepublishOnly": "npm version patch --force",
|
22
|
+
"sc-publish": "npm publish"
|
23
23
|
},
|
24
24
|
"type": "module",
|
25
25
|
"main": "adapter.js",
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import 'dotenv/config';
|
2
|
+
import { S3CompatibleStorage } from '@storecraft/storage-s3-compatible'
|
3
|
+
import { storage as storage_test_runner } from '@storecraft/core/test-runner'
|
4
|
+
|
5
|
+
const FORCE_PATH_STYLE = true;
|
6
|
+
|
7
|
+
const storage = new S3CompatibleStorage(
|
8
|
+
{
|
9
|
+
accessKeyId: process.env.ACCESS_KEY_ID,
|
10
|
+
secretAccessKey: process.env.SECRET_ACCESS_KEY,
|
11
|
+
bucket: process.env.BUCKET,
|
12
|
+
endpoint: process.env.ENDPOINT,
|
13
|
+
forcePathStyle: FORCE_PATH_STYLE,
|
14
|
+
region: process.env.REGION
|
15
|
+
}
|
16
|
+
);
|
17
|
+
|
18
|
+
const suite = storage_test_runner.create(storage);
|
19
|
+
|
20
|
+
suite.before(async () => { await storage.init(undefined) });
|
21
|
+
|
22
|
+
suite.run();
|