@beyonk/tus-bunny-store 0.1.0-beta.1
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 +22 -0
- package/README.md +98 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +316 -0
- package/dist/index.js.map +1 -0
- package/package.json +42 -0
- package/src/index.ts +388 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 tus - Resumable File Uploads
|
|
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.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# `@beyonk/tus-bunny-store`
|
|
2
|
+
|
|
3
|
+
> 👉 **Note**: since 1.0.0 packages are split and published under the `@tus` scope. The
|
|
4
|
+
> old package, `tus-node-server`, is considered unstable and will only receive security
|
|
5
|
+
> fixes. Make sure to use the new packages.
|
|
6
|
+
|
|
7
|
+
## Contents
|
|
8
|
+
|
|
9
|
+
- [Install](#install)
|
|
10
|
+
- [Use](#use)
|
|
11
|
+
- [API](#api)
|
|
12
|
+
- [`new BunnyStore(options)`](#new-bunnystoreoptions)
|
|
13
|
+
- [Types](#types)
|
|
14
|
+
- [Compatibility](#compatibility)
|
|
15
|
+
- [Contribute](#contribute)
|
|
16
|
+
- [License](#license)
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
In Node.js >=20.19.0, install with npm:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install @beyonk/tus-bunny-store @bunny.net/storage-sdk
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Use
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
import { Server } from "@tus/server";
|
|
30
|
+
import { BunnyStore } from "@beyonk/tus-bunny-store";
|
|
31
|
+
import * as BunnyStorageSDK from "@bunny.net/storage-sdk";
|
|
32
|
+
|
|
33
|
+
const storageZone = BunnyStorageSDK.zone.connect_with_accesskey(
|
|
34
|
+
BunnyStorageSDK.regions.StorageRegion.Falkenstein, // or your preferred region
|
|
35
|
+
"your-storage-zone-name",
|
|
36
|
+
"your-access-key"
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const server = new Server({
|
|
40
|
+
path: "/files",
|
|
41
|
+
datastore: new BunnyStore({
|
|
42
|
+
storageZone,
|
|
43
|
+
}),
|
|
44
|
+
});
|
|
45
|
+
// ...
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## API
|
|
49
|
+
|
|
50
|
+
This package exports `BunnyStore`. There is no default export.
|
|
51
|
+
|
|
52
|
+
### `new BunnyStore(options)`
|
|
53
|
+
|
|
54
|
+
Creates a new Bunny.net Edge Storage store by passing a storage zone instance.
|
|
55
|
+
|
|
56
|
+
#### `options.storageZone`
|
|
57
|
+
|
|
58
|
+
The storage zone instance created using `BunnyStorageSDK.zone.connect_with_accesskey()`
|
|
59
|
+
|
|
60
|
+
## Extensions
|
|
61
|
+
|
|
62
|
+
The tus protocol supports optional [extensions][]. Below is a table of the supported
|
|
63
|
+
extensions in `@beyonk/tus-bunny-store`.
|
|
64
|
+
|
|
65
|
+
| Extension | `@beyonk/tus-bunny-store` |
|
|
66
|
+
| ------------------------ | ------------------ |
|
|
67
|
+
| [Creation][] | ✅ |
|
|
68
|
+
| [Creation With Upload][] | ✅ |
|
|
69
|
+
| [Expiration][] | ❌ |
|
|
70
|
+
| [Checksum][] | ❌ |
|
|
71
|
+
| [Termination][] | ❌ |
|
|
72
|
+
| [Concatenation][] | ❌ |
|
|
73
|
+
|
|
74
|
+
## Types
|
|
75
|
+
|
|
76
|
+
This package is fully typed with TypeScript.
|
|
77
|
+
|
|
78
|
+
## Compatibility
|
|
79
|
+
|
|
80
|
+
This package requires Node.js >=20.19.0.
|
|
81
|
+
|
|
82
|
+
## Contribute
|
|
83
|
+
|
|
84
|
+
See
|
|
85
|
+
[`contributing.md`](https://github.com/tus/tus-node-server/blob/main/.github/contributing.md).
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
[MIT](https://github.com/tus/tus-node-server/blob/master/license) ©
|
|
90
|
+
[tus](https://github.com/tus)
|
|
91
|
+
|
|
92
|
+
[extensions]: https://tus.io/protocols/resumable-upload.html#protocol-extensions
|
|
93
|
+
[creation]: https://tus.io/protocols/resumable-upload.html#creation
|
|
94
|
+
[creation with upload]: https://tus.io/protocols/resumable-upload.html#creation-with-upload
|
|
95
|
+
[expiration]: https://tus.io/protocols/resumable-upload.html#expiration
|
|
96
|
+
[checksum]: https://tus.io/protocols/resumable-upload.html#checksum
|
|
97
|
+
[termination]: https://tus.io/protocols/resumable-upload.html#termination
|
|
98
|
+
[concatenation]: https://tus.io/protocols/resumable-upload.html#concatenation
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import stream from 'node:stream';
|
|
2
|
+
import type http from 'node:http';
|
|
3
|
+
import { Upload, DataStore } from '@tus/utils';
|
|
4
|
+
import * as BunnyStorageSDK from '@bunny.net/storage-sdk';
|
|
5
|
+
export type Options = {
|
|
6
|
+
/**
|
|
7
|
+
* The storage zone instance created using `BunnyStorageSDK.zone.connect_with_accesskey()`
|
|
8
|
+
*/
|
|
9
|
+
storageZone: BunnyStorageSDK.zone.StorageZone;
|
|
10
|
+
};
|
|
11
|
+
export declare class BunnyStore extends DataStore {
|
|
12
|
+
#private;
|
|
13
|
+
storageZone: BunnyStorageSDK.zone.StorageZone;
|
|
14
|
+
constructor(options: Options);
|
|
15
|
+
create(upload: Upload): Promise<Upload>;
|
|
16
|
+
read(file_id: string): Promise<stream.Readable>;
|
|
17
|
+
/**
|
|
18
|
+
* Write data to the file, handling resumable uploads by combining existing data with new data
|
|
19
|
+
*/
|
|
20
|
+
write(readable: http.IncomingMessage | stream.Readable, id: string, offset: number): Promise<number>;
|
|
21
|
+
getUpload(id: string): Promise<Upload>;
|
|
22
|
+
declareUploadLength(id: string, upload_length: number): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AAIjC,OAAO,EAAwB,MAAM,EAAE,SAAS,EAAC,MAAM,YAAY,CAAA;AACnE,OAAO,KAAK,eAAe,MAAM,wBAAwB,CAAA;AAIzD,MAAM,MAAM,OAAO,GAAG;IACpB;;OAEG;IACH,WAAW,EAAE,eAAe,CAAC,IAAI,CAAC,WAAW,CAAA;CAC9C,CAAA;AAqDD,qBAAa,UAAW,SAAQ,SAAS;;IAChC,WAAW,EAAE,eAAe,CAAC,IAAI,CAAC,WAAW,CAAA;gBAExC,OAAO,EAAE,OAAO;IA4FtB,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IA4CvC,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC;IAarD;;OAEG;IACG,KAAK,CACT,QAAQ,EAAE,IAAI,CAAC,eAAe,GAAG,MAAM,CAAC,QAAQ,EAChD,EAAE,EAAE,MAAM,EACV,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,CAAC;IAgIZ,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAyBtC,mBAAmB,CAAC,EAAE,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAO5E"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import stream from 'node:stream';
|
|
2
|
+
import debug from 'debug';
|
|
3
|
+
import { ReadableStream } from 'node:stream/web';
|
|
4
|
+
import { ERRORS, TUS_RESUMABLE, Upload, DataStore } from '@tus/utils';
|
|
5
|
+
import * as BunnyStorageSDK from '@bunny.net/storage-sdk';
|
|
6
|
+
const log = debug('tus-node-server:stores:bunnystore');
|
|
7
|
+
/**
|
|
8
|
+
* Convert a Node.js Readable stream to a Web ReadableStream
|
|
9
|
+
*/
|
|
10
|
+
function nodeStreamToWebStream(nodeStream) {
|
|
11
|
+
return new ReadableStream({
|
|
12
|
+
start(controller) {
|
|
13
|
+
nodeStream.on('data', (chunk) => {
|
|
14
|
+
const uint8Array = chunk instanceof Uint8Array ? chunk : new Uint8Array(chunk);
|
|
15
|
+
controller.enqueue(uint8Array);
|
|
16
|
+
});
|
|
17
|
+
nodeStream.on('end', () => {
|
|
18
|
+
controller.close();
|
|
19
|
+
});
|
|
20
|
+
nodeStream.on('error', (err) => {
|
|
21
|
+
controller.error(err);
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
cancel() {
|
|
25
|
+
nodeStream.destroy();
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Convert a Web ReadableStream to a Node.js Readable stream
|
|
31
|
+
*/
|
|
32
|
+
function webStreamToNodeStream(webStream) {
|
|
33
|
+
const reader = webStream.getReader();
|
|
34
|
+
const nodeStream = new stream.Readable({
|
|
35
|
+
async read() {
|
|
36
|
+
try {
|
|
37
|
+
const { done, value } = await reader.read();
|
|
38
|
+
if (done) {
|
|
39
|
+
this.push(null);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this.push(Buffer.from(value));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
this.destroy(error);
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
return nodeStream;
|
|
51
|
+
}
|
|
52
|
+
export class BunnyStore extends DataStore {
|
|
53
|
+
storageZone;
|
|
54
|
+
constructor(options) {
|
|
55
|
+
super();
|
|
56
|
+
if (!options.storageZone) {
|
|
57
|
+
throw new Error('BunnyStore must have a storageZone');
|
|
58
|
+
}
|
|
59
|
+
this.storageZone = options.storageZone;
|
|
60
|
+
this.extensions = ['creation', 'creation-with-upload', 'creation-defer-length'];
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get the path for the metadata info file
|
|
64
|
+
*/
|
|
65
|
+
#infoKey(id) {
|
|
66
|
+
return `${id}.info`;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Save upload metadata to a `${file_id}.info` file
|
|
70
|
+
*/
|
|
71
|
+
async #saveMetadata(upload) {
|
|
72
|
+
log(`[${upload.id}] saving metadata`);
|
|
73
|
+
const metadata = {
|
|
74
|
+
file: upload,
|
|
75
|
+
'tus-version': TUS_RESUMABLE,
|
|
76
|
+
};
|
|
77
|
+
const metadataJson = JSON.stringify(metadata);
|
|
78
|
+
const metadataStream = new stream.Readable({
|
|
79
|
+
read() {
|
|
80
|
+
this.push(Buffer.from(metadataJson));
|
|
81
|
+
this.push(null);
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
await BunnyStorageSDK.file.upload(this.storageZone, this.#infoKey(upload.id), nodeStreamToWebStream(metadataStream));
|
|
85
|
+
log(`[${upload.id}] metadata file saved`);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Retrieve upload metadata from `${file_id}.info`
|
|
89
|
+
*/
|
|
90
|
+
async #getMetadata(id) {
|
|
91
|
+
try {
|
|
92
|
+
const infoFile = await BunnyStorageSDK.file.get(this.storageZone, this.#infoKey(id));
|
|
93
|
+
const { stream: webStream } = await infoFile.data();
|
|
94
|
+
const nodeStream = webStreamToNodeStream(webStream);
|
|
95
|
+
const chunks = [];
|
|
96
|
+
for await (const chunk of nodeStream) {
|
|
97
|
+
chunks.push(chunk);
|
|
98
|
+
}
|
|
99
|
+
const metadataJson = Buffer.concat(chunks).toString('utf-8');
|
|
100
|
+
const metadata = JSON.parse(metadataJson);
|
|
101
|
+
// Reconstruct Upload object
|
|
102
|
+
metadata.file = new Upload({
|
|
103
|
+
id: metadata.file.id,
|
|
104
|
+
size: metadata.file.size,
|
|
105
|
+
offset: metadata.file.offset,
|
|
106
|
+
metadata: metadata.file.metadata,
|
|
107
|
+
storage: metadata.file.storage,
|
|
108
|
+
creation_date: metadata.file.creation_date,
|
|
109
|
+
});
|
|
110
|
+
return metadata;
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
log('[BunnyStore] getMetadata error', error);
|
|
114
|
+
throw ERRORS.FILE_NOT_FOUND;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Convert the Upload object to a format that can be stored in metadata
|
|
119
|
+
*/
|
|
120
|
+
#stringifyUploadKeys(upload) {
|
|
121
|
+
return {
|
|
122
|
+
size: upload.size ?? null,
|
|
123
|
+
sizeIsDeferred: `${upload.sizeIsDeferred}`,
|
|
124
|
+
offset: upload.offset,
|
|
125
|
+
metadata: JSON.stringify(upload.metadata),
|
|
126
|
+
storage: JSON.stringify(upload.storage),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
async create(upload) {
|
|
130
|
+
log(`[${upload.id}] creating upload`);
|
|
131
|
+
if (!upload.id) {
|
|
132
|
+
throw ERRORS.FILE_NOT_FOUND;
|
|
133
|
+
}
|
|
134
|
+
upload.storage = {
|
|
135
|
+
type: 'bunny',
|
|
136
|
+
path: upload.id,
|
|
137
|
+
bucket: BunnyStorageSDK.zone.name(this.storageZone),
|
|
138
|
+
};
|
|
139
|
+
upload.creation_date = new Date().toISOString();
|
|
140
|
+
// Create an empty file to initialize the upload
|
|
141
|
+
const emptyStream = new stream.Readable({
|
|
142
|
+
read() {
|
|
143
|
+
this.push(null);
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
const uploadOptions = {};
|
|
147
|
+
if (upload.metadata?.contentType) {
|
|
148
|
+
uploadOptions.contentType = upload.metadata.contentType;
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
await BunnyStorageSDK.file.upload(this.storageZone, upload.id, nodeStreamToWebStream(emptyStream), uploadOptions);
|
|
152
|
+
await this.#saveMetadata(upload);
|
|
153
|
+
log(`[${upload.id}] upload created`);
|
|
154
|
+
return upload;
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
log('[BunnyStore] create error', error);
|
|
158
|
+
throw ERRORS.FILE_WRITE_ERROR;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
async read(file_id) {
|
|
162
|
+
try {
|
|
163
|
+
const { stream: webStream } = await BunnyStorageSDK.file.download(this.storageZone, file_id);
|
|
164
|
+
return webStreamToNodeStream(webStream);
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
log('[BunnyStore] read error', error);
|
|
168
|
+
throw ERRORS.FILE_NOT_FOUND;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Write data to the file, handling resumable uploads by combining existing data with new data
|
|
173
|
+
*/
|
|
174
|
+
async write(readable, id, offset) {
|
|
175
|
+
if (readable.destroyed) {
|
|
176
|
+
throw ERRORS.FILE_WRITE_ERROR;
|
|
177
|
+
}
|
|
178
|
+
const upload = await this.getUpload(id);
|
|
179
|
+
if (offset !== upload.offset) {
|
|
180
|
+
log(`[${id}] offset mismatch: requested ${offset}, actual ${upload.offset}`);
|
|
181
|
+
throw ERRORS.INVALID_OFFSET;
|
|
182
|
+
}
|
|
183
|
+
return new Promise(async (resolve, reject) => {
|
|
184
|
+
// Check if stream is destroyed after async operations
|
|
185
|
+
if (readable.destroyed) {
|
|
186
|
+
return reject(ERRORS.FILE_WRITE_ERROR);
|
|
187
|
+
}
|
|
188
|
+
let isResolved = false;
|
|
189
|
+
let hasEnded = false;
|
|
190
|
+
try {
|
|
191
|
+
let bytesReceived = 0;
|
|
192
|
+
const chunks = [];
|
|
193
|
+
// If there's existing data, download it first
|
|
194
|
+
if (upload.offset > 0) {
|
|
195
|
+
try {
|
|
196
|
+
const existingFile = await BunnyStorageSDK.file.get(this.storageZone, id);
|
|
197
|
+
const { stream: existingWebStream } = await existingFile.data();
|
|
198
|
+
const existingNodeStream = webStreamToNodeStream(existingWebStream);
|
|
199
|
+
for await (const chunk of existingNodeStream) {
|
|
200
|
+
chunks.push(chunk);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
log(`[${id}] error downloading existing file:`, error);
|
|
205
|
+
// If file doesn't exist yet, that's okay for offset 0
|
|
206
|
+
if (upload.offset > 0) {
|
|
207
|
+
isResolved = true;
|
|
208
|
+
return reject(ERRORS.FILE_WRITE_ERROR);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
// Check again if stream was destroyed during async operations
|
|
213
|
+
if (readable.destroyed) {
|
|
214
|
+
isResolved = true;
|
|
215
|
+
return reject(ERRORS.FILE_WRITE_ERROR);
|
|
216
|
+
}
|
|
217
|
+
// Read new data
|
|
218
|
+
readable.on('data', (buffer) => {
|
|
219
|
+
if (isResolved)
|
|
220
|
+
return;
|
|
221
|
+
chunks.push(buffer);
|
|
222
|
+
bytesReceived += buffer.length;
|
|
223
|
+
});
|
|
224
|
+
readable.on('end', async () => {
|
|
225
|
+
if (isResolved)
|
|
226
|
+
return;
|
|
227
|
+
hasEnded = true;
|
|
228
|
+
try {
|
|
229
|
+
// Combine all chunks
|
|
230
|
+
const combinedBuffer = Buffer.concat(chunks);
|
|
231
|
+
const newOffset = upload.offset + bytesReceived;
|
|
232
|
+
upload.offset = newOffset;
|
|
233
|
+
// Upload the combined file
|
|
234
|
+
const combinedStream = new stream.Readable({
|
|
235
|
+
read() {
|
|
236
|
+
this.push(combinedBuffer);
|
|
237
|
+
this.push(null);
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
const uploadOptions = {};
|
|
241
|
+
if (upload.metadata?.contentType) {
|
|
242
|
+
uploadOptions.contentType = upload.metadata.contentType;
|
|
243
|
+
}
|
|
244
|
+
await BunnyStorageSDK.file.upload(this.storageZone, id, nodeStreamToWebStream(combinedStream), uploadOptions);
|
|
245
|
+
// Update metadata
|
|
246
|
+
await this.#saveMetadata(upload);
|
|
247
|
+
log(`[${id}] ${newOffset} bytes written`);
|
|
248
|
+
isResolved = true;
|
|
249
|
+
resolve(newOffset);
|
|
250
|
+
}
|
|
251
|
+
catch (error) {
|
|
252
|
+
if (isResolved)
|
|
253
|
+
return;
|
|
254
|
+
log(`[${id}] error writing file:`, error);
|
|
255
|
+
isResolved = true;
|
|
256
|
+
reject(ERRORS.FILE_WRITE_ERROR);
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
readable.on('error', (error) => {
|
|
260
|
+
if (isResolved)
|
|
261
|
+
return;
|
|
262
|
+
log(`[${id}] stream error:`, error);
|
|
263
|
+
isResolved = true;
|
|
264
|
+
reject(ERRORS.FILE_WRITE_ERROR);
|
|
265
|
+
});
|
|
266
|
+
readable.on('close', () => {
|
|
267
|
+
// Only reject if stream was destroyed and we haven't received 'end' event
|
|
268
|
+
// (which means it was destroyed prematurely)
|
|
269
|
+
if (isResolved)
|
|
270
|
+
return;
|
|
271
|
+
if (readable.destroyed && !hasEnded) {
|
|
272
|
+
log(`[${id}] stream was destroyed before completion`);
|
|
273
|
+
isResolved = true;
|
|
274
|
+
reject(ERRORS.FILE_WRITE_ERROR);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
if (isResolved)
|
|
280
|
+
return;
|
|
281
|
+
log(`[${id}] write error:`, error);
|
|
282
|
+
isResolved = true;
|
|
283
|
+
reject(ERRORS.FILE_WRITE_ERROR);
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
async getUpload(id) {
|
|
288
|
+
if (!id) {
|
|
289
|
+
throw ERRORS.FILE_NOT_FOUND;
|
|
290
|
+
}
|
|
291
|
+
try {
|
|
292
|
+
const metadata = await this.#getMetadata(id);
|
|
293
|
+
const upload = metadata.file;
|
|
294
|
+
// Get the actual file size from storage
|
|
295
|
+
try {
|
|
296
|
+
const fileInfo = await BunnyStorageSDK.file.get(this.storageZone, id);
|
|
297
|
+
upload.offset = fileInfo.length;
|
|
298
|
+
}
|
|
299
|
+
catch (error) {
|
|
300
|
+
// If file doesn't exist yet, offset is 0
|
|
301
|
+
upload.offset = 0;
|
|
302
|
+
}
|
|
303
|
+
return upload;
|
|
304
|
+
}
|
|
305
|
+
catch (error) {
|
|
306
|
+
log('[BunnyStore] getUpload error', error);
|
|
307
|
+
throw ERRORS.FILE_NOT_FOUND;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
async declareUploadLength(id, upload_length) {
|
|
311
|
+
const upload = await this.getUpload(id);
|
|
312
|
+
upload.size = upload_length;
|
|
313
|
+
await this.#saveMetadata(upload);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAA;AAEhC,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAC,cAAc,EAAC,MAAM,iBAAiB,CAAA;AAE9C,OAAO,EAAC,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,SAAS,EAAC,MAAM,YAAY,CAAA;AACnE,OAAO,KAAK,eAAe,MAAM,wBAAwB,CAAA;AAEzD,MAAM,GAAG,GAAG,KAAK,CAAC,mCAAmC,CAAC,CAAA;AActD;;GAEG;AACH,SAAS,qBAAqB,CAAC,UAA2B;IACxD,OAAO,IAAI,cAAc,CAAC;QACxB,KAAK,CAAC,UAAU;YACd,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAA0B,EAAE,EAAE;gBACnD,MAAM,UAAU,GAAG,KAAK,YAAY,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,UAAU,CAAC,KAAK,CAAC,CAAA;gBAC9E,UAAU,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;YAChC,CAAC,CAAC,CAAA;YACF,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE;gBACxB,UAAU,CAAC,KAAK,EAAE,CAAA;YACpB,CAAC,CAAC,CAAA;YACF,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE;gBAC7B,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YACvB,CAAC,CAAC,CAAA;QACJ,CAAC;QACD,MAAM;YACJ,UAAU,CAAC,OAAO,EAAE,C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,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,GAAG,CAAC,8BAA8B,EAAE,KAAK,CAAC,CAAA;YAC1C,MAAM,MAAM,CAAC,cAAc,CAAA;QAC7B,CAAC;IACH,CAAC;IAED,KAAK,CAAC,mBAAmB,CAAC,EAAU,EAAE,aAAqB;QACzD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,CAAA;QAEvC,MAAM,CAAC,IAAI,GAAG,aAAa,CAAA;QAE3B,MAAM,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAA;IAClC,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/package.json",
|
|
3
|
+
"name": "@beyonk/tus-bunny-store",
|
|
4
|
+
"version": "0.1.0-beta.1",
|
|
5
|
+
"description": "Bunny.net store for @tus/server",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"exports": "./dist/index.js",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"homepage": "https://github.com/tus/tus-node-server#readme",
|
|
10
|
+
"bugs": "https://github.com/tus/tus-node-server/issues",
|
|
11
|
+
"repository": "tus/tus-node-server",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"src",
|
|
16
|
+
"!test*"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc --build",
|
|
20
|
+
"pretest": "tsc --build",
|
|
21
|
+
"test": "mocha './dist/test/*.js' --exit --timeout 30000"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@tus/utils": "^0.6.0",
|
|
25
|
+
"debug": "^4.3.4"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@bunny.net/storage-sdk": "^0.3.0",
|
|
29
|
+
"@tus/server": "^2.0.0",
|
|
30
|
+
"@types/debug": "^4.1.12",
|
|
31
|
+
"@types/mocha": "^10.0.6",
|
|
32
|
+
"@types/node": "^22.13.7",
|
|
33
|
+
"mocha": "^11.0.1",
|
|
34
|
+
"should": "^13.2.3"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@bunny.net/storage-sdk": "^0.3.0"
|
|
38
|
+
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=20.19.0"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import stream from 'node:stream'
|
|
2
|
+
import type http from 'node:http'
|
|
3
|
+
import debug from 'debug'
|
|
4
|
+
import {ReadableStream} from 'node:stream/web'
|
|
5
|
+
|
|
6
|
+
import {ERRORS, TUS_RESUMABLE, Upload, DataStore} from '@tus/utils'
|
|
7
|
+
import * as BunnyStorageSDK from '@bunny.net/storage-sdk'
|
|
8
|
+
|
|
9
|
+
const log = debug('tus-node-server:stores:bunnystore')
|
|
10
|
+
|
|
11
|
+
export type Options = {
|
|
12
|
+
/**
|
|
13
|
+
* The storage zone instance created using `BunnyStorageSDK.zone.connect_with_accesskey()`
|
|
14
|
+
*/
|
|
15
|
+
storageZone: BunnyStorageSDK.zone.StorageZone
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type MetadataValue = {
|
|
19
|
+
file: Upload
|
|
20
|
+
'tus-version': string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Convert a Node.js Readable stream to a Web ReadableStream
|
|
25
|
+
*/
|
|
26
|
+
function nodeStreamToWebStream(nodeStream: stream.Readable): ReadableStream<Uint8Array> {
|
|
27
|
+
return new ReadableStream({
|
|
28
|
+
start(controller) {
|
|
29
|
+
nodeStream.on('data', (chunk: Buffer | Uint8Array) => {
|
|
30
|
+
const uint8Array = chunk instanceof Uint8Array ? chunk : new Uint8Array(chunk)
|
|
31
|
+
controller.enqueue(uint8Array)
|
|
32
|
+
})
|
|
33
|
+
nodeStream.on('end', () => {
|
|
34
|
+
controller.close()
|
|
35
|
+
})
|
|
36
|
+
nodeStream.on('error', (err) => {
|
|
37
|
+
controller.error(err)
|
|
38
|
+
})
|
|
39
|
+
},
|
|
40
|
+
cancel() {
|
|
41
|
+
nodeStream.destroy()
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Convert a Web ReadableStream to a Node.js Readable stream
|
|
48
|
+
*/
|
|
49
|
+
function webStreamToNodeStream(webStream: ReadableStream<Uint8Array>): stream.Readable {
|
|
50
|
+
const reader = webStream.getReader()
|
|
51
|
+
const nodeStream = new stream.Readable({
|
|
52
|
+
async read() {
|
|
53
|
+
try {
|
|
54
|
+
const {done, value} = await reader.read()
|
|
55
|
+
if (done) {
|
|
56
|
+
this.push(null)
|
|
57
|
+
} else {
|
|
58
|
+
this.push(Buffer.from(value))
|
|
59
|
+
}
|
|
60
|
+
} catch (error) {
|
|
61
|
+
this.destroy(error as Error)
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
return nodeStream
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export class BunnyStore extends DataStore {
|
|
70
|
+
public storageZone: BunnyStorageSDK.zone.StorageZone
|
|
71
|
+
|
|
72
|
+
constructor(options: Options) {
|
|
73
|
+
super()
|
|
74
|
+
|
|
75
|
+
if (!options.storageZone) {
|
|
76
|
+
throw new Error('BunnyStore must have a storageZone')
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
this.storageZone = options.storageZone
|
|
80
|
+
|
|
81
|
+
this.extensions = ['creation', 'creation-with-upload', 'creation-defer-length']
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Get the path for the metadata info file
|
|
86
|
+
*/
|
|
87
|
+
#infoKey(id: string): string {
|
|
88
|
+
return `${id}.info`
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Save upload metadata to a `${file_id}.info` file
|
|
93
|
+
*/
|
|
94
|
+
async #saveMetadata(upload: Upload): Promise<void> {
|
|
95
|
+
log(`[${upload.id}] saving metadata`)
|
|
96
|
+
const metadata: MetadataValue = {
|
|
97
|
+
file: upload,
|
|
98
|
+
'tus-version': TUS_RESUMABLE,
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const metadataJson = JSON.stringify(metadata)
|
|
102
|
+
const metadataStream = new stream.Readable({
|
|
103
|
+
read() {
|
|
104
|
+
this.push(Buffer.from(metadataJson))
|
|
105
|
+
this.push(null)
|
|
106
|
+
},
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
await BunnyStorageSDK.file.upload(
|
|
110
|
+
this.storageZone,
|
|
111
|
+
this.#infoKey(upload.id),
|
|
112
|
+
nodeStreamToWebStream(metadataStream)
|
|
113
|
+
)
|
|
114
|
+
log(`[${upload.id}] metadata file saved`)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Retrieve upload metadata from `${file_id}.info`
|
|
119
|
+
*/
|
|
120
|
+
async #getMetadata(id: string): Promise<MetadataValue> {
|
|
121
|
+
try {
|
|
122
|
+
const infoFile = await BunnyStorageSDK.file.get(this.storageZone, this.#infoKey(id))
|
|
123
|
+
const {stream: webStream} = await infoFile.data()
|
|
124
|
+
const nodeStream = webStreamToNodeStream(webStream)
|
|
125
|
+
|
|
126
|
+
const chunks: Buffer[] = []
|
|
127
|
+
for await (const chunk of nodeStream) {
|
|
128
|
+
chunks.push(chunk)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const metadataJson = Buffer.concat(chunks).toString('utf-8')
|
|
132
|
+
const metadata: MetadataValue = JSON.parse(metadataJson)
|
|
133
|
+
|
|
134
|
+
// Reconstruct Upload object
|
|
135
|
+
metadata.file = new Upload({
|
|
136
|
+
id: metadata.file.id,
|
|
137
|
+
size: metadata.file.size,
|
|
138
|
+
offset: metadata.file.offset,
|
|
139
|
+
metadata: metadata.file.metadata,
|
|
140
|
+
storage: metadata.file.storage,
|
|
141
|
+
creation_date: metadata.file.creation_date,
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
return metadata
|
|
145
|
+
} catch (error) {
|
|
146
|
+
log('[BunnyStore] getMetadata error', error)
|
|
147
|
+
throw ERRORS.FILE_NOT_FOUND
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Convert the Upload object to a format that can be stored in metadata
|
|
153
|
+
*/
|
|
154
|
+
#stringifyUploadKeys(upload: Upload) {
|
|
155
|
+
return {
|
|
156
|
+
size: upload.size ?? null,
|
|
157
|
+
sizeIsDeferred: `${upload.sizeIsDeferred}`,
|
|
158
|
+
offset: upload.offset,
|
|
159
|
+
metadata: JSON.stringify(upload.metadata),
|
|
160
|
+
storage: JSON.stringify(upload.storage),
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async create(upload: Upload): Promise<Upload> {
|
|
165
|
+
log(`[${upload.id}] creating upload`)
|
|
166
|
+
if (!upload.id) {
|
|
167
|
+
throw ERRORS.FILE_NOT_FOUND
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
upload.storage = {
|
|
171
|
+
type: 'bunny',
|
|
172
|
+
path: upload.id,
|
|
173
|
+
bucket: BunnyStorageSDK.zone.name(this.storageZone),
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
upload.creation_date = new Date().toISOString()
|
|
177
|
+
|
|
178
|
+
// Create an empty file to initialize the upload
|
|
179
|
+
const emptyStream = new stream.Readable({
|
|
180
|
+
read() {
|
|
181
|
+
this.push(null)
|
|
182
|
+
},
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
const uploadOptions: BunnyStorageSDK.file.UploadOptions = {}
|
|
186
|
+
if (upload.metadata?.contentType) {
|
|
187
|
+
uploadOptions.contentType = upload.metadata.contentType
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
try {
|
|
191
|
+
await BunnyStorageSDK.file.upload(
|
|
192
|
+
this.storageZone,
|
|
193
|
+
upload.id,
|
|
194
|
+
nodeStreamToWebStream(emptyStream),
|
|
195
|
+
uploadOptions
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
await this.#saveMetadata(upload)
|
|
199
|
+
|
|
200
|
+
log(`[${upload.id}] upload created`)
|
|
201
|
+
return upload
|
|
202
|
+
} catch (error) {
|
|
203
|
+
log('[BunnyStore] create error', error)
|
|
204
|
+
throw ERRORS.FILE_WRITE_ERROR
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async read(file_id: string): Promise<stream.Readable> {
|
|
209
|
+
try {
|
|
210
|
+
const {stream: webStream} = await BunnyStorageSDK.file.download(
|
|
211
|
+
this.storageZone,
|
|
212
|
+
file_id
|
|
213
|
+
)
|
|
214
|
+
return webStreamToNodeStream(webStream)
|
|
215
|
+
} catch (error) {
|
|
216
|
+
log('[BunnyStore] read error', error)
|
|
217
|
+
throw ERRORS.FILE_NOT_FOUND
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Write data to the file, handling resumable uploads by combining existing data with new data
|
|
223
|
+
*/
|
|
224
|
+
async write(
|
|
225
|
+
readable: http.IncomingMessage | stream.Readable,
|
|
226
|
+
id: string,
|
|
227
|
+
offset: number
|
|
228
|
+
): Promise<number> {
|
|
229
|
+
if (readable.destroyed) {
|
|
230
|
+
throw ERRORS.FILE_WRITE_ERROR
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const upload = await this.getUpload(id)
|
|
234
|
+
|
|
235
|
+
if (offset !== upload.offset) {
|
|
236
|
+
log(`[${id}] offset mismatch: requested ${offset}, actual ${upload.offset}`)
|
|
237
|
+
throw ERRORS.INVALID_OFFSET
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return new Promise(async (resolve, reject) => {
|
|
241
|
+
// Check if stream is destroyed after async operations
|
|
242
|
+
if (readable.destroyed) {
|
|
243
|
+
return reject(ERRORS.FILE_WRITE_ERROR)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
let isResolved = false
|
|
247
|
+
let hasEnded = false
|
|
248
|
+
|
|
249
|
+
try {
|
|
250
|
+
let bytesReceived = 0
|
|
251
|
+
const chunks: Buffer[] = []
|
|
252
|
+
|
|
253
|
+
// If there's existing data, download it first
|
|
254
|
+
if (upload.offset > 0) {
|
|
255
|
+
try {
|
|
256
|
+
const existingFile = await BunnyStorageSDK.file.get(this.storageZone, id)
|
|
257
|
+
const {stream: existingWebStream} = await existingFile.data()
|
|
258
|
+
const existingNodeStream = webStreamToNodeStream(existingWebStream)
|
|
259
|
+
|
|
260
|
+
for await (const chunk of existingNodeStream) {
|
|
261
|
+
chunks.push(chunk)
|
|
262
|
+
}
|
|
263
|
+
} catch (error) {
|
|
264
|
+
log(`[${id}] error downloading existing file:`, error)
|
|
265
|
+
// If file doesn't exist yet, that's okay for offset 0
|
|
266
|
+
if (upload.offset > 0) {
|
|
267
|
+
isResolved = true
|
|
268
|
+
return reject(ERRORS.FILE_WRITE_ERROR)
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Check again if stream was destroyed during async operations
|
|
274
|
+
if (readable.destroyed) {
|
|
275
|
+
isResolved = true
|
|
276
|
+
return reject(ERRORS.FILE_WRITE_ERROR)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Read new data
|
|
280
|
+
readable.on('data', (buffer: Buffer) => {
|
|
281
|
+
if (isResolved) return
|
|
282
|
+
chunks.push(buffer)
|
|
283
|
+
bytesReceived += buffer.length
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
readable.on('end', async () => {
|
|
287
|
+
if (isResolved) return
|
|
288
|
+
hasEnded = true
|
|
289
|
+
try {
|
|
290
|
+
// Combine all chunks
|
|
291
|
+
const combinedBuffer = Buffer.concat(chunks)
|
|
292
|
+
const newOffset = upload.offset + bytesReceived
|
|
293
|
+
|
|
294
|
+
upload.offset = newOffset
|
|
295
|
+
|
|
296
|
+
// Upload the combined file
|
|
297
|
+
const combinedStream = new stream.Readable({
|
|
298
|
+
read() {
|
|
299
|
+
this.push(combinedBuffer)
|
|
300
|
+
this.push(null)
|
|
301
|
+
},
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
const uploadOptions: BunnyStorageSDK.file.UploadOptions = {}
|
|
305
|
+
if (upload.metadata?.contentType) {
|
|
306
|
+
uploadOptions.contentType = upload.metadata.contentType
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
await BunnyStorageSDK.file.upload(
|
|
310
|
+
this.storageZone,
|
|
311
|
+
id,
|
|
312
|
+
nodeStreamToWebStream(combinedStream),
|
|
313
|
+
uploadOptions
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
// Update metadata
|
|
317
|
+
await this.#saveMetadata(upload)
|
|
318
|
+
|
|
319
|
+
log(`[${id}] ${newOffset} bytes written`)
|
|
320
|
+
isResolved = true
|
|
321
|
+
resolve(newOffset)
|
|
322
|
+
} catch (error) {
|
|
323
|
+
if (isResolved) return
|
|
324
|
+
log(`[${id}] error writing file:`, error)
|
|
325
|
+
isResolved = true
|
|
326
|
+
reject(ERRORS.FILE_WRITE_ERROR)
|
|
327
|
+
}
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
readable.on('error', (error) => {
|
|
331
|
+
if (isResolved) return
|
|
332
|
+
log(`[${id}] stream error:`, error)
|
|
333
|
+
isResolved = true
|
|
334
|
+
reject(ERRORS.FILE_WRITE_ERROR)
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
readable.on('close', () => {
|
|
338
|
+
// Only reject if stream was destroyed and we haven't received 'end' event
|
|
339
|
+
// (which means it was destroyed prematurely)
|
|
340
|
+
if (isResolved) return
|
|
341
|
+
if (readable.destroyed && !hasEnded) {
|
|
342
|
+
log(`[${id}] stream was destroyed before completion`)
|
|
343
|
+
isResolved = true
|
|
344
|
+
reject(ERRORS.FILE_WRITE_ERROR)
|
|
345
|
+
}
|
|
346
|
+
})
|
|
347
|
+
} catch (error) {
|
|
348
|
+
if (isResolved) return
|
|
349
|
+
log(`[${id}] write error:`, error)
|
|
350
|
+
isResolved = true
|
|
351
|
+
reject(ERRORS.FILE_WRITE_ERROR)
|
|
352
|
+
}
|
|
353
|
+
})
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
async getUpload(id: string): Promise<Upload> {
|
|
357
|
+
if (!id) {
|
|
358
|
+
throw ERRORS.FILE_NOT_FOUND
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
try {
|
|
362
|
+
const metadata = await this.#getMetadata(id)
|
|
363
|
+
const upload = metadata.file
|
|
364
|
+
|
|
365
|
+
// Get the actual file size from storage
|
|
366
|
+
try {
|
|
367
|
+
const fileInfo = await BunnyStorageSDK.file.get(this.storageZone, id)
|
|
368
|
+
upload.offset = fileInfo.length
|
|
369
|
+
} catch (error) {
|
|
370
|
+
// If file doesn't exist yet, offset is 0
|
|
371
|
+
upload.offset = 0
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return upload
|
|
375
|
+
} catch (error) {
|
|
376
|
+
log('[BunnyStore] getUpload error', error)
|
|
377
|
+
throw ERRORS.FILE_NOT_FOUND
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
async declareUploadLength(id: string, upload_length: number): Promise<void> {
|
|
382
|
+
const upload = await this.getUpload(id)
|
|
383
|
+
|
|
384
|
+
upload.size = upload_length
|
|
385
|
+
|
|
386
|
+
await this.#saveMetadata(upload)
|
|
387
|
+
}
|
|
388
|
+
}
|