@tweedegolf/sab-adapter-backblaze-b2 3.0.0 → 3.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.
@@ -1,4 +1,20 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
12
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
+ var m = o[Symbol.asyncIterator], i;
14
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
15
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
16
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
+ };
2
18
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
19
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
20
  };
@@ -13,21 +29,25 @@ class AdapterBackblazeB2 extends AbstractAdapter_1.AbstractAdapter {
13
29
  super(config);
14
30
  this._provider = general_1.Provider.B2;
15
31
  this._configError = null;
16
- this._client = null;
17
32
  this.authorized = false;
18
33
  this.versioning = false;
19
34
  if (typeof config !== "string") {
20
- this._config = { ...config };
35
+ this._config = Object.assign({}, config);
21
36
  }
22
37
  else {
23
38
  const { value, error } = (0, util_1.parseUrl)(config);
24
- if (error !== null) {
39
+ if (value === null) {
25
40
  this._configError = `[configError] ${error}`;
26
41
  }
27
42
  else {
28
43
  const { protocol: type, username: applicationKeyId, password: applicationKey, host: bucketName, searchParams, } = value;
44
+ if (applicationKey === null || applicationKeyId === null) {
45
+ this._configError =
46
+ 'Please provide both a value for "applicationKey" and "applicationKeyId"';
47
+ return;
48
+ }
29
49
  if (searchParams !== null) {
30
- this._config = { type, applicationKeyId, applicationKey, ...searchParams };
50
+ this._config = Object.assign({ type, applicationKeyId, applicationKey }, searchParams);
31
51
  }
32
52
  else {
33
53
  this._config = { type, applicationKeyId, applicationKey };
@@ -38,460 +58,495 @@ class AdapterBackblazeB2 extends AbstractAdapter_1.AbstractAdapter {
38
58
  }
39
59
  // console.log(this._config);
40
60
  }
41
- if (!this._config.applicationKey || !this._config.applicationKeyId) {
42
- this._configError = 'Please provide both a value for "applicationKey" and "applicationKeyId"';
61
+ try {
62
+ this._client = new backblaze_b2_1.default(this._config);
43
63
  }
44
- else {
45
- try {
46
- this._client = new backblaze_b2_1.default(this._config);
47
- }
48
- catch (e) {
49
- this._configError = `[configError] ${e.message}`;
50
- }
64
+ catch (e) {
65
+ this._configError = `[configError] ${(0, util_1.getErrorMessage)(e)}`;
51
66
  }
52
67
  if (typeof this.config.bucketName !== "undefined") {
53
68
  this._bucketName = this.config.bucketName;
54
69
  }
55
70
  }
56
71
  // util members
57
- async authorize() {
58
- if (this.authorized) {
59
- return { value: "ok", error: null };
60
- }
61
- try {
62
- const { data: _data } = await this._client.authorize();
63
- this.authorized = true;
64
- return { value: "ok", error: null };
65
- }
66
- catch (e) {
67
- return { value: null, error: e.message };
68
- }
72
+ authorize() {
73
+ return __awaiter(this, void 0, void 0, function* () {
74
+ if (this.authorized) {
75
+ return { value: "ok", error: null };
76
+ }
77
+ try {
78
+ const { data: _data } = yield this._client.authorize();
79
+ this.authorized = true;
80
+ return { value: "ok", error: null };
81
+ }
82
+ catch (e) {
83
+ return { value: null, error: (0, util_1.getErrorMessage)(e) };
84
+ }
85
+ });
69
86
  }
70
- async getBucket(name) {
71
- try {
72
- const { data } = await this._client.getBucket({ bucketName: name });
73
- if (data.buckets.length > 0) {
74
- const { bucketId, bucketName } = data.buckets[0];
75
- return { value: { id: bucketId, name: bucketName }, error: null };
87
+ getBucket(name) {
88
+ return __awaiter(this, void 0, void 0, function* () {
89
+ try {
90
+ const { data } = yield this._client.getBucket({ bucketName: name });
91
+ if (data.buckets.length > 0) {
92
+ const { bucketId, bucketName } = data.buckets[0];
93
+ return { value: { id: bucketId, name: bucketName }, error: null };
94
+ }
95
+ return { value: null, error: `Could not find bucket "${name}"` };
76
96
  }
77
- return { value: null, error: `Could not find bucket "${name}"` };
78
- }
79
- catch (e) {
80
- return { value: null, error: e.message };
81
- }
97
+ catch (e) {
98
+ return { value: null, error: (0, util_1.getErrorMessage)(e) };
99
+ }
100
+ });
82
101
  }
83
- async getUploadUrl(bucketId) {
84
- try {
85
- const { data } = await this._client.getUploadUrl({ bucketId });
86
- if (typeof data.uploadUrl === "undefined") {
87
- return { value: null, error: data.message };
102
+ getUploadUrl(bucketId) {
103
+ return __awaiter(this, void 0, void 0, function* () {
104
+ try {
105
+ const { data } = yield this._client.getUploadUrl({ bucketId });
106
+ if (typeof data.uploadUrl === "undefined") {
107
+ return { value: null, error: data.message };
108
+ }
109
+ return { value: data, error: null };
88
110
  }
89
- return { value: data, error: null };
90
- }
91
- catch (e) {
92
- return { value: null, error: e.message };
93
- }
111
+ catch (e) {
112
+ return { value: null, error: (0, util_1.getErrorMessage)(e) };
113
+ }
114
+ });
94
115
  }
95
- async getFiles(bucketName, versioning = this.versioning, numFiles = 1000) {
96
- const { value: bucket, error } = await this.getBucket(bucketName);
97
- if (error !== null) {
98
- return { value: null, error };
99
- }
100
- try {
101
- let data; //eslint-disable-line
102
- // const options: ListFileVersionsOpts = {
103
- const options = {
104
- bucketId: bucket.id,
105
- maxFileCount: numFiles,
106
- };
107
- if (versioning) {
108
- ({ data } = await this._client.listFileVersions(options));
116
+ getFiles(bucketName_1) {
117
+ return __awaiter(this, arguments, void 0, function* (bucketName, versioning = this.versioning, numFiles = 1000) {
118
+ const { value: bucket, error } = yield this.getBucket(bucketName);
119
+ if (error !== null) {
120
+ return { value: null, error };
109
121
  }
110
- else {
111
- ({ data } = await this._client.listFileNames(options));
122
+ if (bucket === null) {
123
+ return { value: null, error: `can't find bucket '${bucketName}'` };
112
124
  }
113
- return {
114
- value: data.files.map(({ fileId, fileName, contentType, contentLength }) => {
115
- return {
116
- id: fileId,
117
- name: fileName,
118
- contentType,
119
- contentLength,
120
- };
121
- }),
122
- error: null,
123
- };
124
- }
125
- catch (e) {
126
- return {
127
- value: null,
128
- error: e.message,
129
- };
130
- }
125
+ try {
126
+ let data;
127
+ // const options: ListFileVersionsOpts = {
128
+ const options = {
129
+ bucketId: bucket.id,
130
+ maxFileCount: numFiles,
131
+ };
132
+ if (versioning) {
133
+ ({ data } = yield this._client.listFileVersions(options));
134
+ }
135
+ else {
136
+ ({ data } = yield this._client.listFileNames(options));
137
+ }
138
+ return {
139
+ value: data.files.map(({ fileId, fileName, contentType, contentLength }) => {
140
+ return {
141
+ id: fileId,
142
+ name: fileName,
143
+ contentType,
144
+ contentLength,
145
+ };
146
+ }),
147
+ error: null,
148
+ };
149
+ }
150
+ catch (e) {
151
+ return { value: null, error: (0, util_1.getErrorMessage)(e) };
152
+ }
153
+ });
131
154
  }
132
- async getFile(bucketName, name) {
133
- const { value: files, error } = await this.getFiles(bucketName, false);
134
- if (error !== null) {
135
- return { value: null, error };
136
- }
137
- for (let i = 0; i < files.length; i++) {
138
- const file = files[i];
139
- if (file.name === name) {
140
- return { value: file, error: null };
155
+ getFile(bucketName, name) {
156
+ return __awaiter(this, void 0, void 0, function* () {
157
+ const { value: files, error } = yield this.getFiles(bucketName, false);
158
+ if (error !== null) {
159
+ return { value: null, error };
141
160
  }
142
- }
143
- return { value: null, error: `Could not find file '${name}' in bucket '${bucketName}'.` };
161
+ if (files === null) {
162
+ return { value: null, error: `Could not find file '${name}' in bucket '${bucketName}'.` };
163
+ }
164
+ for (let i = 0; i < files.length; i++) {
165
+ const file = files[i];
166
+ if (file.name === name) {
167
+ return { value: file, error: null };
168
+ }
169
+ }
170
+ return { value: null, error: `Could not find file '${name}' in bucket '${bucketName}'.` };
171
+ });
144
172
  }
145
173
  // protected, called by methods of public API via AbstractAdapter
146
- async _listBuckets() {
147
- const { error } = await this.authorize();
148
- if (error !== null) {
149
- return { value: null, error };
150
- }
151
- try {
152
- const { data } = await this._client.listBuckets();
153
- const value = data.buckets.map(({ bucketName }) => bucketName);
154
- return { value, error: null };
155
- }
156
- catch (e) {
157
- return { value: null, error: e };
158
- }
159
- }
160
- async _createBucket(name, options) {
161
- const { error } = await this.authorize();
162
- let bucketType = "allPrivate";
163
- if (typeof options.bucketType !== "undefined") {
164
- bucketType = options.bucketType;
165
- }
166
- else if (options.public === true) {
167
- options.bucketType = "allPublic";
168
- }
169
- if (bucketType !== "allPrivate" && bucketType !== "allPublic") {
170
- return {
171
- value: null, error: `${bucketType} is not valid: bucket type must be either 'allPrivate' or 'allPublic'`
172
- };
173
- }
174
- try {
175
- const { data } = await this._client.createBucket({
176
- ...options,
177
- bucketName: name,
178
- bucketType,
179
- });
180
- const { bucketType: _type } = data;
181
- // console.log(_type);
182
- return { value: "ok", error: null };
183
- }
184
- catch (e) {
185
- return { value: null, error: e.response.data.message };
186
- }
187
- }
188
- async _addFile(params) {
189
- const { error } = await this.authorize();
190
- if (error !== null) {
191
- return { value: null, error };
192
- }
193
- const { bucketName, targetPath } = params;
194
- const data1 = await this.getBucket(bucketName);
195
- if (data1.error !== null) {
196
- return { value: null, error: data1.error };
197
- }
198
- const { value: { id: bucketId }, } = data1;
199
- const data2 = await this.getUploadUrl(bucketId);
200
- if (data2.error !== null) {
201
- return { value: null, error: data2.error };
202
- }
203
- const { value: { uploadUrl, authorizationToken } } = data2;
204
- let { options } = params;
205
- if (typeof options === "undefined") {
206
- options = {};
207
- }
208
- try {
209
- let buffer;
210
- if (typeof params.buffer !== "undefined") {
211
- buffer = params.buffer;
212
- }
213
- else if (typeof params.stream !== "undefined") {
214
- const buffers = []; // eslint-disable-line
215
- for await (const data of params.stream) {
216
- buffers.push(data);
217
- }
218
- buffer = Buffer.concat(buffers);
219
- }
220
- const { data: _data } = await this._client.uploadFile({
221
- uploadUrl,
222
- uploadAuthToken: authorizationToken,
223
- fileName: targetPath,
224
- data: buffer,
225
- ...options,
226
- });
227
- // console.log(_data);
228
- return { value: "ok", error: null };
229
- }
230
- catch (e) {
231
- // console.log(e.toJSON());
232
- return { value: null, error: e.message };
233
- }
174
+ _listBuckets() {
175
+ return __awaiter(this, void 0, void 0, function* () {
176
+ const { error } = yield this.authorize();
177
+ if (error !== null) {
178
+ return { value: null, error };
179
+ }
180
+ try {
181
+ const { data } = yield this._client.listBuckets();
182
+ const value = data.buckets.map(({ bucketName }) => bucketName);
183
+ return { value, error: null };
184
+ }
185
+ catch (e) {
186
+ return { value: null, error: (0, util_1.getErrorMessage)(e) };
187
+ }
188
+ });
234
189
  }
235
- async _getFileAsStream(bucketName, fileName, options = { start: 0 }) {
236
- const { error } = await this.authorize();
237
- if (error !== null) {
238
- return { value: null, error };
239
- }
240
- const data = await this.getFile(bucketName, fileName);
241
- if (data.error !== null) {
242
- return { value: null, error: data.error };
243
- }
244
- const { value: file } = data;
245
- const { start, end } = options;
246
- let range = `bytes=${start}-${end}`;
247
- if (typeof start === "undefined" && typeof end === "undefined") {
248
- range = null;
249
- }
250
- else if (typeof start === "undefined") {
251
- range = `bytes=0-${end}`;
252
- }
253
- else if (typeof end === "undefined") {
254
- range = `bytes=${start}-`;
255
- }
256
- delete options.start;
257
- delete options.end;
258
- try {
259
- const { data } = await this._client.downloadFileById({
260
- fileId: file.id,
261
- responseType: "stream",
262
- axios: {
263
- headers: {
264
- "Content-Type": file.contentType,
265
- Range: range,
266
- },
267
- ...options,
268
- },
269
- });
270
- return { value: data, error: null };
271
- }
272
- catch (e) {
273
- return { value: null, error: e.message };
274
- }
190
+ _createBucket(name, options) {
191
+ return __awaiter(this, void 0, void 0, function* () {
192
+ const { error } = yield this.authorize();
193
+ if (error !== null) {
194
+ return { value: null, error };
195
+ }
196
+ let bucketType = "allPrivate";
197
+ if (options.public === true) {
198
+ options.bucketType = "allPublic";
199
+ }
200
+ if (typeof options.bucketType !== "undefined") {
201
+ bucketType = options.bucketType;
202
+ }
203
+ if (bucketType !== "allPrivate" && bucketType !== "allPublic") {
204
+ return {
205
+ value: null,
206
+ error: `Bucket type '${options.bucketType}' is not valid: must be either 'allPrivate' or 'allPublic'`,
207
+ };
208
+ }
209
+ try {
210
+ const { data } = yield this._client.createBucket(Object.assign(Object.assign({}, options), { bucketName: name, bucketType }));
211
+ const { bucketType: _type } = data;
212
+ // console.log(_type);
213
+ return { value: "ok", error: null };
214
+ }
215
+ catch (e) {
216
+ return { value: null, error: e.response.data.message };
217
+ }
218
+ });
275
219
  }
276
- async _getPublicURL(bucketName, fileName, options) {
277
- if (options.noCheck !== true) {
278
- const { value, error } = await this._bucketIsPublic(bucketName);
220
+ _addFile(params) {
221
+ return __awaiter(this, void 0, void 0, function* () {
222
+ var _a, e_1, _b, _c;
223
+ const { error } = yield this.authorize();
279
224
  if (error !== null) {
280
225
  return { value: null, error };
281
226
  }
282
- else if (value === false) {
283
- return { value: null, error: `Bucket "${bucketName}" is not public!` };
227
+ const { bucketName, targetPath } = params;
228
+ const data1 = yield this.getBucket(bucketName);
229
+ if (data1.error !== null) {
230
+ return { value: null, error: data1.error };
284
231
  }
285
- }
286
- return {
287
- value: `${this._client.downloadUrl}/file/${bucketName}/${fileName}`,
288
- error: null,
289
- };
232
+ const { value: bucket } = data1;
233
+ const { id: bucketId } = bucket;
234
+ const data2 = yield this.getUploadUrl(bucketId);
235
+ if (data2.error !== null) {
236
+ return { value: null, error: data2.error };
237
+ }
238
+ const { value: { uploadUrl, authorizationToken }, } = data2;
239
+ let { options } = params;
240
+ if (typeof options === "undefined") {
241
+ options = {};
242
+ }
243
+ try {
244
+ let buffer;
245
+ if (typeof params.buffer !== "undefined") {
246
+ buffer = params.buffer;
247
+ }
248
+ else if (typeof params.stream !== "undefined") {
249
+ const buffers = []; // eslint-disable-line
250
+ try {
251
+ for (var _d = true, _e = __asyncValues(params.stream), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
252
+ _c = _f.value;
253
+ _d = false;
254
+ const data = _c;
255
+ buffers.push(data);
256
+ }
257
+ }
258
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
259
+ finally {
260
+ try {
261
+ if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
262
+ }
263
+ finally { if (e_1) throw e_1.error; }
264
+ }
265
+ buffer = Buffer.concat(buffers);
266
+ }
267
+ if (typeof buffer === "undefined") {
268
+ return { value: null, error: "Could get file buffer" };
269
+ }
270
+ const { data: _data } = yield this._client.uploadFile(Object.assign({ uploadUrl, uploadAuthToken: authorizationToken, fileName: targetPath, data: buffer }, options));
271
+ // console.log(_data);
272
+ return { value: "ok", error: null };
273
+ }
274
+ catch (e) {
275
+ // console.log(e.toJSON());
276
+ return { value: null, error: (0, util_1.getErrorMessage)(e) };
277
+ }
278
+ });
290
279
  }
291
- async _getSignedURL(bucketName, fileName, options) {
292
- try {
293
- const data = await this.getBucket(bucketName);
280
+ _getFileAsStream(bucketName_1, fileName_1) {
281
+ return __awaiter(this, arguments, void 0, function* (bucketName, fileName, options = { start: 0 }) {
282
+ const { error } = yield this.authorize();
283
+ if (error !== null) {
284
+ return { value: null, error };
285
+ }
286
+ const data = yield this.getFile(bucketName, fileName);
294
287
  if (data.error !== null) {
295
288
  return { value: null, error: data.error };
296
289
  }
297
- const { value: { id: bucketId }, } = data;
298
- let expiresIn = 300; // 5 * 60
299
- if (typeof options.expiresIn !== "undefined") {
300
- expiresIn = Number.parseInt(options.expiresIn, 10);
301
- }
302
- const r = await this._client.getDownloadAuthorization({
303
- bucketId,
304
- fileNamePrefix: fileName,
305
- validDurationInSeconds: expiresIn
306
- });
307
- const { data: { authorizationToken } } = r;
290
+ const { value: file } = data;
291
+ if (file === null) {
292
+ return { value: null, error: `Could not find file '${fileName}' in bucket '${bucketName}'.` };
293
+ }
294
+ const { start, end } = options;
295
+ let range = `bytes=${start}-${end}`;
296
+ if (typeof start === "undefined" && typeof end === "undefined") {
297
+ range = undefined;
298
+ }
299
+ else if (typeof start === "undefined") {
300
+ range = `bytes=0-${end}`;
301
+ }
302
+ else if (typeof end === "undefined") {
303
+ range = `bytes=${start}-`;
304
+ }
305
+ delete options.start;
306
+ delete options.end;
307
+ try {
308
+ const { data } = yield this._client.downloadFileById({
309
+ fileId: file.id,
310
+ responseType: "stream",
311
+ axios: Object.assign({ headers: {
312
+ "Content-Type": file.contentType,
313
+ Range: range,
314
+ } }, options),
315
+ });
316
+ return { value: data, error: null };
317
+ }
318
+ catch (e) {
319
+ return { value: null, error: (0, util_1.getErrorMessage)(e) };
320
+ }
321
+ });
322
+ }
323
+ _getPublicURL(bucketName, fileName, _options) {
324
+ return __awaiter(this, void 0, void 0, function* () {
308
325
  return {
309
- value: `${this._client.downloadUrl}/file/${bucketName}/${fileName}?Authorization=${authorizationToken}`,
326
+ value: `${this._client.downloadUrl}/file/${bucketName}/${fileName}`,
310
327
  error: null,
311
328
  };
312
- }
313
- catch (e) {
314
- return {
315
- value: null,
316
- error: e,
317
- };
318
- }
329
+ });
319
330
  }
320
- async _removeFile(bucketName, fileName) {
321
- const { error } = await this.authorize();
322
- if (error !== null) {
323
- return { value: null, error };
324
- }
325
- const data = await this.getFiles(bucketName, true);
326
- if (error !== null) {
327
- return { value: null, error };
328
- }
329
- const { value: files } = data;
330
- const index = files.findIndex(({ name }) => name === fileName);
331
- if (index === -1) {
332
- return { value: null, error: `No file '${fileName}' found in bucket '${bucketName}'` };
333
- }
334
- if (this.versioning) {
335
- // delete the file, if the file has more versions, delete the most recent version
336
- const file = files[index];
331
+ _getSignedURL(bucketName, fileName, options) {
332
+ return __awaiter(this, void 0, void 0, function* () {
337
333
  try {
338
- await this._client.deleteFileVersion({
334
+ const data = yield this.getBucket(bucketName);
335
+ if (data.error !== null) {
336
+ return { value: null, error: data.error };
337
+ }
338
+ const { value: bucket } = data;
339
+ const { id: bucketId } = bucket;
340
+ let expiresIn = 300; // 5 * 60
341
+ if (typeof options.expiresIn !== "undefined") {
342
+ expiresIn = Number.parseInt(options.expiresIn, 10);
343
+ }
344
+ const r = yield this._client.getDownloadAuthorization({
345
+ bucketId,
346
+ fileNamePrefix: fileName,
347
+ validDurationInSeconds: expiresIn,
348
+ });
349
+ const { data: { authorizationToken }, } = r;
350
+ return {
351
+ value: `${this._client.downloadUrl}/file/${bucketName}/${fileName}?Authorization=${authorizationToken}`,
352
+ error: null,
353
+ };
354
+ }
355
+ catch (e) {
356
+ return { value: null, error: (0, util_1.getErrorMessage)(e) };
357
+ }
358
+ });
359
+ }
360
+ _removeFile(bucketName, fileName) {
361
+ return __awaiter(this, void 0, void 0, function* () {
362
+ const { error } = yield this.authorize();
363
+ if (error !== null) {
364
+ return { value: null, error };
365
+ }
366
+ const data = yield this.getFiles(bucketName, true);
367
+ if (data.value === null) {
368
+ return { value: null, error };
369
+ }
370
+ const { value: files } = data;
371
+ const index = files.findIndex(({ name }) => name === fileName);
372
+ if (index === -1) {
373
+ return { value: null, error: `No file '${fileName}' found in bucket '${bucketName}'` };
374
+ }
375
+ if (this.versioning) {
376
+ // delete the file, if the file has more versions, delete the most recent version
377
+ const file = files[index];
378
+ try {
379
+ yield this._client.deleteFileVersion({
380
+ fileId: file.id,
381
+ fileName: file.name,
382
+ });
383
+ return { value: "ok", error: null };
384
+ }
385
+ catch (e) {
386
+ return { value: null, error: (0, util_1.getErrorMessage)(e) };
387
+ }
388
+ }
389
+ else {
390
+ // delete all versions of the file
391
+ try {
392
+ yield Promise.all(files
393
+ .filter((f) => f.name === fileName)
394
+ .map(({ id: fileId, name: fileName }) => {
395
+ // console.log(fileName, fileId);
396
+ return this._client.deleteFileVersion({
397
+ fileId,
398
+ fileName,
399
+ });
400
+ }));
401
+ return { value: "ok", error: null };
402
+ }
403
+ catch (e) {
404
+ return { value: null, error: (0, util_1.getErrorMessage)(e) };
405
+ }
406
+ }
407
+ });
408
+ }
409
+ _clearBucket(name) {
410
+ return __awaiter(this, void 0, void 0, function* () {
411
+ const { value, error } = yield this.authorize();
412
+ if (error !== null) {
413
+ return { value: null, error };
414
+ }
415
+ const data = yield this.getFiles(name, true);
416
+ if (data.value === null) {
417
+ return { value: null, error: data.error };
418
+ }
419
+ const { value: files } = data;
420
+ try {
421
+ const _data = yield Promise.all(files.map((file) => this._client.deleteFileVersion({
339
422
  fileId: file.id,
340
423
  fileName: file.name,
341
- });
424
+ })));
425
+ // console.log("[clearBucket]", _data);
342
426
  return { value: "ok", error: null };
343
427
  }
344
428
  catch (e) {
345
- return { value: null, error: e.message };
429
+ return { value: null, error: (0, util_1.getErrorMessage)(e) };
430
+ }
431
+ });
432
+ }
433
+ _deleteBucket(name) {
434
+ return __awaiter(this, void 0, void 0, function* () {
435
+ const { error, value: bucket } = yield this.getBucket(name);
436
+ if (bucket === null) {
437
+ return { value: null, error: error };
346
438
  }
347
- }
348
- else {
349
- // delete all versions of the file
350
439
  try {
351
- await Promise.all(files
352
- .filter((f) => f.name === fileName)
353
- .map(({ id: fileId, name: fileName }) => {
354
- // console.log(fileName, fileId);
355
- return this._client.deleteFileVersion({
356
- fileId,
357
- fileName,
358
- });
359
- }));
440
+ yield this._client.deleteBucket({ bucketId: bucket.id });
360
441
  return { value: "ok", error: null };
361
442
  }
362
443
  catch (e) {
363
- return { value: null, error: e.message };
444
+ return { value: null, error: (0, util_1.getErrorMessage)(e) };
364
445
  }
365
- }
366
- }
367
- async _clearBucket(name) {
368
- const { value, error } = await this.authorize();
369
- if (error !== null) {
370
- return { value: null, error };
371
- }
372
- const data = await this.getFiles(name, true);
373
- if (data.error !== null) {
374
- return { value: null, error: data.error };
375
- }
376
- const { value: files } = data;
377
- try {
378
- const _data = await Promise.all(files.map((file) => this._client.deleteFileVersion({
379
- fileId: file.id,
380
- fileName: file.name,
381
- })));
382
- // console.log("[clearBucket]", _data);
383
- return { value: "ok", error: null };
384
- }
385
- catch (e) {
386
- return { value: null, error: e.message };
387
- }
446
+ });
388
447
  }
389
- async _deleteBucket(name) {
390
- const data = await this.clearBucket(name);
391
- if (data.error !== null) {
392
- return { value: null, error: data.error };
393
- }
394
- const { error, value: bucket } = await this.getBucket(name);
395
- if (error !== null) {
396
- return { value: null, error: error };
397
- }
398
- try {
399
- await this._client.deleteBucket({ bucketId: bucket.id });
400
- return { value: "ok", error: null };
401
- }
402
- catch (e) {
403
- return { value: null, error: e.message };
404
- }
405
- }
406
- async _listFiles(bucketName, numFiles) {
407
- const { error } = await this.authorize();
408
- if (error !== null) {
409
- return { value: null, error };
410
- }
411
- const data = await this.getFiles(bucketName, this.versioning, numFiles);
412
- if (data.error === null) {
413
- const { value: files } = data;
414
- return {
415
- value: files.map((f) => {
416
- return [f.name, f.contentLength];
417
- }),
418
- error: null,
419
- };
420
- }
421
- else {
422
- return { value: null, error: data.error };
423
- }
448
+ _listFiles(bucketName, numFiles) {
449
+ return __awaiter(this, void 0, void 0, function* () {
450
+ const { error } = yield this.authorize();
451
+ if (error !== null) {
452
+ return { value: null, error };
453
+ }
454
+ const data = yield this.getFiles(bucketName, this.versioning, numFiles);
455
+ if (data.value !== null) {
456
+ const { value: files } = data;
457
+ return {
458
+ value: files.map((f) => {
459
+ return [f.name, f.contentLength];
460
+ }),
461
+ error: null,
462
+ };
463
+ }
464
+ else {
465
+ return { value: null, error: data.error };
466
+ }
467
+ });
424
468
  }
425
- async _sizeOf(bucketName, fileName) {
426
- const { error } = await this.authorize();
427
- if (error !== null) {
428
- return { value: null, error };
429
- }
430
- const data = await this.getFile(bucketName, fileName);
431
- if (data.error === null) {
432
- const { value: file } = data;
433
- return { value: file.contentLength, error: null };
434
- }
435
- else {
436
- return { value: null, error: data.error };
437
- }
469
+ _sizeOf(bucketName, fileName) {
470
+ return __awaiter(this, void 0, void 0, function* () {
471
+ const { error } = yield this.authorize();
472
+ if (error !== null) {
473
+ return { value: null, error };
474
+ }
475
+ const data = yield this.getFile(bucketName, fileName);
476
+ if (data.value !== null) {
477
+ const { value: file } = data;
478
+ return { value: file.contentLength, error: null };
479
+ }
480
+ else {
481
+ return { value: null, error: data.error };
482
+ }
483
+ });
438
484
  }
439
- async _bucketExists(bucketName) {
440
- const { error } = await this.authorize();
441
- if (error !== null) {
442
- return { value: null, error };
443
- }
444
- const data = await this.getBucket(bucketName);
445
- if (data.error === null) {
446
- return { value: true, error: null };
447
- }
448
- else if (data.error.startsWith("Could not find bucket")) {
449
- return { value: false, error: null };
450
- }
451
- else {
452
- return { value: null, error: data.error };
453
- }
485
+ _bucketExists(bucketName) {
486
+ return __awaiter(this, void 0, void 0, function* () {
487
+ const { error } = yield this.authorize();
488
+ if (error !== null) {
489
+ return { value: null, error };
490
+ }
491
+ const data = yield this.getBucket(bucketName);
492
+ if (data.error === null) {
493
+ return { value: true, error: null };
494
+ }
495
+ else if (data.error.startsWith("Could not find bucket")) {
496
+ return { value: false, error: null };
497
+ }
498
+ else {
499
+ return { value: null, error: data.error };
500
+ }
501
+ });
454
502
  }
455
- async _bucketIsPublic(bucketName) {
456
- const { error } = await this.authorize();
457
- if (error !== null) {
458
- return { value: null, error };
459
- }
460
- try {
461
- const { data } = await this._client.listBuckets();
462
- const index = data.buckets.findIndex((bucket) => bucket.bucketName === bucketName);
463
- if (index === -1) {
464
- return { value: null, error: `Could not find the bucket "${bucketName}"` };
503
+ _bucketIsPublic(bucketName) {
504
+ return __awaiter(this, void 0, void 0, function* () {
505
+ const { error } = yield this.authorize();
506
+ if (error !== null) {
507
+ return { value: null, error };
465
508
  }
466
- const bucket = data.buckets[index];
467
- return { value: bucket.bucketType === "allPublic", error: null };
468
- }
469
- catch (e) {
470
- return { value: null, error: e };
471
- }
509
+ try {
510
+ const { data } = yield this._client.listBuckets();
511
+ const index = data.buckets.findIndex((bucket) => bucket.bucketName === bucketName);
512
+ if (index === -1) {
513
+ return { value: null, error: `Could not find the bucket "${bucketName}"` };
514
+ }
515
+ const bucket = data.buckets[index];
516
+ return { value: bucket.bucketType === "allPublic", error: null };
517
+ }
518
+ catch (e) {
519
+ return { value: null, error: (0, util_1.getErrorMessage)(e) };
520
+ }
521
+ });
472
522
  }
473
- async _fileExists(bucketName, fileName) {
474
- const { error, value } = await this._sizeOf(bucketName, fileName);
475
- if (error === null) {
476
- return { value: true, error: null };
477
- }
478
- else {
479
- return { value: false, error: null };
480
- }
523
+ _fileExists(bucketName, fileName) {
524
+ return __awaiter(this, void 0, void 0, function* () {
525
+ const { error, value } = yield this._sizeOf(bucketName, fileName);
526
+ if (error === null) {
527
+ return { value: true, error: null };
528
+ }
529
+ else {
530
+ return { value: false, error: null };
531
+ }
532
+ });
481
533
  }
482
- async _getPresignedUploadURL(bucketName, _fileName, _options) {
483
- try {
484
- const data = await this.getBucket(bucketName);
485
- if (data.error !== null) {
486
- return { value: null, error: data.error };
534
+ _getPresignedUploadURL(bucketName, _fileName, _options) {
535
+ return __awaiter(this, void 0, void 0, function* () {
536
+ try {
537
+ const data = yield this.getBucket(bucketName);
538
+ if (data.error !== null) {
539
+ return { value: null, error: data.error };
540
+ }
541
+ const { value: bucket } = data;
542
+ const { id: bucketId } = bucket;
543
+ const { value: { uploadUrl, authorizationToken }, } = (yield this.getUploadUrl(bucketId));
544
+ return { value: { url: uploadUrl, authToken: authorizationToken }, error: null };
487
545
  }
488
- const { value: { id: bucketId }, } = data;
489
- const { value: { uploadUrl, authorizationToken } } = await this.getUploadUrl(bucketId);
490
- return { value: { url: uploadUrl, authToken: authorizationToken }, error: null };
491
- }
492
- catch (e) {
493
- return { value: null, error: e.message };
494
- }
546
+ catch (e) {
547
+ return { value: null, error: (0, util_1.getErrorMessage)(e) };
548
+ }
549
+ });
495
550
  }
496
551
  // public
497
552
  get config() {