@dereekb/firebase 12.6.21 → 13.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.
Files changed (84) hide show
  1. package/LICENSE +1 -1
  2. package/index.cjs.js +53 -2239
  3. package/index.esm.js +34 -2216
  4. package/package.json +21 -15
  5. package/src/lib/client/storage/driver.accessor.d.ts +1 -1
  6. package/src/lib/common/firestore/query/iterator.d.ts +0 -4
  7. package/src/lib/common/firestore/snapshot/snapshot.field.d.ts +0 -6
  8. package/src/lib/common/model/model.service.d.ts +1 -1
  9. package/src/lib/common/storage/driver/accessor.d.ts +1 -1
  10. package/src/lib/common/storage/types.d.ts +3 -3
  11. package/src/lib/model/notification/notification.api.d.ts +1 -1
  12. package/src/lib/model/notification/notification.config.d.ts +5 -5
  13. package/src/lib/model/notification/notification.d.ts +15 -15
  14. package/src/lib/model/notification/notification.details.d.ts +0 -4
  15. package/src/lib/model/notification/notification.item.d.ts +1 -1
  16. package/src/lib/model/storagefile/storagefile.api.d.ts +4 -4
  17. package/src/lib/model/storagefile/storagefile.d.ts +7 -7
  18. package/src/lib/model/storagefile/storagefile.task.d.ts +1 -13
  19. package/src/lib/model/system/system.d.ts +2 -2
  20. package/test/index.cjs.js +4043 -0
  21. package/test/index.esm.js +3957 -0
  22. package/test/package.json +27 -9
  23. package/test/src/lib/client/firebase.authorized.d.ts +2 -2
  24. package/test/src/lib/client/firebase.d.ts +4 -3
  25. package/test/src/lib/client/firestore.mock.item.fixture.authorized.d.ts +2 -2
  26. package/test/src/lib/common/firebase.instance.d.ts +7 -3
  27. package/test/src/lib/common/firestore/firestore.instance.d.ts +7 -3
  28. package/test/src/lib/common/mock/mock.item.collection.fixture.d.ts +5 -2
  29. package/test/src/lib/common/mock/mock.item.storage.fixture.d.ts +4 -4
  30. package/test/src/lib/common/storage/storage.instance.d.ts +7 -3
  31. package/test/CHANGELOG.md +0 -2114
  32. package/test/README.md +0 -11
  33. package/test/src/index.js +0 -5
  34. package/test/src/index.js.map +0 -1
  35. package/test/src/lib/client/firebase.authorized.js +0 -35
  36. package/test/src/lib/client/firebase.authorized.js.map +0 -1
  37. package/test/src/lib/client/firebase.js +0 -125
  38. package/test/src/lib/client/firebase.js.map +0 -1
  39. package/test/src/lib/client/firestore.mock.item.fixture.authorized.js +0 -19
  40. package/test/src/lib/client/firestore.mock.item.fixture.authorized.js.map +0 -1
  41. package/test/src/lib/client/index.js +0 -7
  42. package/test/src/lib/client/index.js.map +0 -1
  43. package/test/src/lib/common/firebase.instance.js +0 -35
  44. package/test/src/lib/common/firebase.instance.js.map +0 -1
  45. package/test/src/lib/common/firestore/firestore.instance.js +0 -24
  46. package/test/src/lib/common/firestore/firestore.instance.js.map +0 -1
  47. package/test/src/lib/common/firestore/firestore.js +0 -67
  48. package/test/src/lib/common/firestore/firestore.js.map +0 -1
  49. package/test/src/lib/common/firestore/index.js +0 -9
  50. package/test/src/lib/common/firestore/index.js.map +0 -1
  51. package/test/src/lib/common/firestore/test.driver.accessor.js +0 -767
  52. package/test/src/lib/common/firestore/test.driver.accessor.js.map +0 -1
  53. package/test/src/lib/common/firestore/test.driver.query.js +0 -1361
  54. package/test/src/lib/common/firestore/test.driver.query.js.map +0 -1
  55. package/test/src/lib/common/firestore/test.iterator.js +0 -221
  56. package/test/src/lib/common/firestore/test.iterator.js.map +0 -1
  57. package/test/src/lib/common/index.js +0 -8
  58. package/test/src/lib/common/index.js.map +0 -1
  59. package/test/src/lib/common/mock/index.js +0 -10
  60. package/test/src/lib/common/mock/index.js.map +0 -1
  61. package/test/src/lib/common/mock/mock.item.collection.fixture.js +0 -64
  62. package/test/src/lib/common/mock/mock.item.collection.fixture.js.map +0 -1
  63. package/test/src/lib/common/mock/mock.item.id.js +0 -3
  64. package/test/src/lib/common/mock/mock.item.id.js.map +0 -1
  65. package/test/src/lib/common/mock/mock.item.js +0 -339
  66. package/test/src/lib/common/mock/mock.item.js.map +0 -1
  67. package/test/src/lib/common/mock/mock.item.query.js +0 -33
  68. package/test/src/lib/common/mock/mock.item.query.js.map +0 -1
  69. package/test/src/lib/common/mock/mock.item.service.js +0 -77
  70. package/test/src/lib/common/mock/mock.item.service.js.map +0 -1
  71. package/test/src/lib/common/mock/mock.item.storage.fixture.js +0 -40
  72. package/test/src/lib/common/mock/mock.item.storage.fixture.js.map +0 -1
  73. package/test/src/lib/common/storage/index.js +0 -7
  74. package/test/src/lib/common/storage/index.js.map +0 -1
  75. package/test/src/lib/common/storage/storage.instance.js +0 -24
  76. package/test/src/lib/common/storage/storage.instance.js.map +0 -1
  77. package/test/src/lib/common/storage/storage.js +0 -37
  78. package/test/src/lib/common/storage/storage.js.map +0 -1
  79. package/test/src/lib/common/storage/test.driver.accessor.js +0 -669
  80. package/test/src/lib/common/storage/test.driver.accessor.js.map +0 -1
  81. package/test/src/lib/index.js +0 -6
  82. package/test/src/lib/index.js.map +0 -1
  83. /package/{index.cjs.d.ts → index.d.ts} +0 -0
  84. /package/{index.esm.d.ts → test/index.d.ts} +0 -0
@@ -1,669 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.describeFirebaseStorageAccessorDriverTests = describeFirebaseStorageAccessorDriverTests;
4
- const test_1 = require("@dereekb/util/test");
5
- const util_1 = require("@dereekb/util");
6
- const firebase_1 = require("@dereekb/firebase");
7
- const stream_1 = require("stream");
8
- const fs_1 = require("fs");
9
- /**
10
- * Describes accessor driver tests, using a MockItemCollectionFixture.
11
- *
12
- * @param f
13
- */
14
- function describeFirebaseStorageAccessorDriverTests(f) {
15
- describe('FirebaseStorageAccessor', () => {
16
- describe('file()', () => {
17
- const secondBucket = 'second-bucket';
18
- const doesNotExistFilePath = 'test.png';
19
- let doesNotExistFile;
20
- const existsFilePath = 'test/exists.txt';
21
- const existsFileContent = 'Hello! \ud83d\ude0a';
22
- const existsFileContentType = 'text/plain';
23
- let existsFile;
24
- let secondBucketTarget;
25
- beforeEach(async () => {
26
- doesNotExistFile = f.storageContext.file(doesNotExistFilePath);
27
- existsFile = f.storageContext.file(existsFilePath);
28
- await existsFile.upload(existsFileContent, { stringFormat: 'raw', contentType: existsFileContentType }); // re-upload for each test
29
- // delete the does not exist file and second bucket target if it exists
30
- await doesNotExistFile.delete().catch(() => null);
31
- secondBucketTarget = f.storageContext.file({ bucketId: secondBucket, pathString: doesNotExistFilePath });
32
- await secondBucketTarget.delete().catch(() => null);
33
- });
34
- describe('uploading', () => {
35
- let uploadFile;
36
- beforeEach(() => {
37
- uploadFile = f.storageContext.file('upload.txt');
38
- });
39
- describe('upload()', () => {
40
- describe('string types', () => {
41
- (0, test_1.itShouldFail)('if stringFormat is not defined in the options', async () => {
42
- const contentType = 'text/plain';
43
- const data = existsFileContent;
44
- await (0, test_1.expectFail)(() => uploadFile.upload(data, { contentType }));
45
- });
46
- it('should upload a raw UTF-16 string.', async () => {
47
- const contentType = 'text/plain';
48
- const data = existsFileContent;
49
- await uploadFile.upload(data, { stringFormat: 'raw', contentType });
50
- const metadata = await uploadFile.getMetadata();
51
- expect(metadata.contentType).toBe(contentType);
52
- const result = await uploadFile.getBytes();
53
- expect(result).toBeDefined();
54
- const decoded = Buffer.from(result).toString('utf-8');
55
- expect(decoded).toBe(data);
56
- });
57
- it('should upload a base64 string.', async () => {
58
- const bytes = await existsFile.getBytes();
59
- const data = Buffer.from(bytes).toString('base64');
60
- const contentType = 'text/plain';
61
- await uploadFile.upload(data, { stringFormat: 'base64', contentType });
62
- const metadata = await uploadFile.getMetadata();
63
- expect(metadata.contentType).toBe(contentType);
64
- const result = await uploadFile.getBytes();
65
- expect(result).toBeDefined();
66
- const decoded = Buffer.from(result).toString('utf-8');
67
- expect(decoded).toBe(existsFileContent);
68
- });
69
- it('should upload a base64url string.', async () => {
70
- const bytes = await existsFile.getBytes();
71
- const data = Buffer.from(bytes).toString('base64url');
72
- const contentType = 'text/plain';
73
- await uploadFile.upload(data, { stringFormat: 'base64url', contentType });
74
- const metadata = await uploadFile.getMetadata();
75
- expect(metadata.contentType).toBe(contentType);
76
- const result = await uploadFile.getBytes();
77
- expect(result).toBeDefined();
78
- const decoded = Buffer.from(result).toString('utf-8');
79
- expect(decoded).toBe(existsFileContent);
80
- });
81
- });
82
- describe('data types', () => {
83
- // NOTE: We can really only test how a NodeJS environment will behave here.
84
- it('should upload a Uint8Array', async () => {
85
- const dataBuffer = Buffer.from(existsFileContent, 'utf-8');
86
- const data = new Uint8Array(dataBuffer);
87
- const contentType = 'text/plain';
88
- await uploadFile.upload(data, { contentType });
89
- const metadata = await uploadFile.getMetadata();
90
- expect(metadata.contentType).toBe(contentType);
91
- });
92
- it('should upload a Buffer', async () => {
93
- const buffer = Buffer.from(existsFileContent, 'utf-8');
94
- const contentType = 'text/plain';
95
- await uploadFile.upload(buffer, { contentType });
96
- const metadata = await uploadFile.getMetadata();
97
- expect(metadata.contentType).toBe(contentType);
98
- });
99
- it('should upload a Blob', async () => {
100
- const buffer = Buffer.from(existsFileContent, 'utf-8');
101
- const data = new Uint8Array(buffer);
102
- const blob = data.buffer; // blob-like
103
- const contentType = 'text/plain';
104
- await uploadFile.upload(blob, { contentType });
105
- const metadata = await uploadFile.getMetadata();
106
- expect(metadata.contentType).toBe(contentType);
107
- });
108
- // NOTE: File extends Blob, so above test should cover it ok.
109
- });
110
- // TODO(TEST): Test uploading other types.
111
- describe('custom metadata', () => {
112
- it('should upload custom metadata via customMetadata', async () => {
113
- const customMetadataKey = 'x-amz-meta-custom-key';
114
- const customMetadataValue = 'custom-value';
115
- const customMetadata = {
116
- [customMetadataKey]: customMetadataValue
117
- };
118
- const contentType = 'text/plain';
119
- const data = existsFileContent;
120
- await uploadFile.upload(data, { stringFormat: 'raw', contentType, customMetadata });
121
- const metadata = await uploadFile.getMetadata();
122
- expect(metadata.customMetadata).toEqual(customMetadata);
123
- });
124
- it('should upload custom metadata via metadata', async () => {
125
- const customMetadataKey = 'x-amz-meta-custom-key';
126
- const customMetadataValue = 'custom-value';
127
- const customMetadata = {
128
- [customMetadataKey]: customMetadataValue
129
- };
130
- const contentType = 'text/plain';
131
- const data = existsFileContent;
132
- await uploadFile.upload(data, { stringFormat: 'raw', contentType, metadata: { customMetadata } });
133
- const metadata = await uploadFile.getMetadata();
134
- expect(metadata.customMetadata).toEqual(customMetadata);
135
- });
136
- it('should upload the merged custom metadatas', async () => {
137
- const customMetadataAKey = 'x-amz-meta-custom-key';
138
- const customMetadataAValue = '1';
139
- const customMetadataBKey = 'x-axx-meta-custom-key';
140
- const customMetadataBValue = 'true';
141
- const customMetadataA = {
142
- [customMetadataAKey]: customMetadataAValue
143
- };
144
- const customMetadataB = {
145
- [customMetadataBKey]: customMetadataBValue
146
- };
147
- const contentType = 'text/plain';
148
- const data = existsFileContent;
149
- await uploadFile.upload(data, { stringFormat: 'raw', contentType, customMetadata: customMetadataA, metadata: { customMetadata: customMetadataB } });
150
- const metadata = await uploadFile.getMetadata();
151
- expect(metadata.customMetadata).toEqual({ ...customMetadataA, ...customMetadataB });
152
- });
153
- });
154
- });
155
- describe('uploadStream()', () => {
156
- it('should upload a string using a WritableStream', async () => {
157
- if (uploadFile.uploadStream != null) {
158
- const contentType = 'text/plain';
159
- const data = existsFileContent;
160
- const stream = uploadFile.uploadStream();
161
- await (0, util_1.useCallback)((cb) => stream.write(data, 'utf-8', cb));
162
- await (0, util_1.useCallback)((cb) => stream.end(cb));
163
- const exists = await uploadFile.exists();
164
- expect(exists).toBe(true);
165
- const metadata = await uploadFile.getMetadata();
166
- expect(metadata.contentType).toBe(contentType);
167
- const result = await uploadFile.getBytes();
168
- expect(result).toBeDefined();
169
- const decoded = Buffer.from(result).toString('utf-8');
170
- expect(decoded).toBe(data);
171
- }
172
- });
173
- it('should upload a string using a stream using a WritableStream', async () => {
174
- if (uploadFile.uploadStream != null) {
175
- const myText = 'This is a test string.';
176
- // Create a readable stream from the string
177
- const readableStream = stream_1.Readable.from(myText, { encoding: 'utf-8' });
178
- await (0, firebase_1.uploadFileWithStream)(uploadFile, readableStream);
179
- const exists = await uploadFile.exists();
180
- expect(exists).toBe(true);
181
- const metadata = await uploadFile.getMetadata();
182
- expect(metadata.contentType).toBe('text/plain');
183
- const result = await uploadFile.getBytes();
184
- expect(result).toBeDefined();
185
- const decoded = Buffer.from(result).toString('utf-8');
186
- expect(decoded).toBe(myText);
187
- }
188
- });
189
- it('should upload a png using a stream using a WritableStream', async () => {
190
- if (uploadFile.uploadStream != null) {
191
- const testFilePath = `${__dirname}/assets/testpng.png`;
192
- const contentType = 'image/png';
193
- const testFileStream = (0, fs_1.createReadStream)(testFilePath, {});
194
- await (0, firebase_1.uploadFileWithStream)(uploadFile, testFileStream, { contentType });
195
- const exists = await uploadFile.exists();
196
- expect(exists).toBe(true);
197
- const metadata = await uploadFile.getMetadata();
198
- expect(metadata.contentType).toBe(contentType);
199
- const result = await uploadFile.getBytes();
200
- expect(result).toBeDefined();
201
- }
202
- });
203
- });
204
- });
205
- describe('copy()', () => {
206
- it('should copy the file to a new location in the same bucket.', async () => {
207
- if (existsFile.copy != null) {
208
- const exists = await doesNotExistFile.exists();
209
- expect(exists).toBe(false);
210
- const targetPath = doesNotExistFile.storagePath;
211
- const result = await existsFile.copy(targetPath);
212
- expect(result.storagePath.pathString).toBe(targetPath.pathString);
213
- const doesNotExistFileExists = await doesNotExistFile.exists();
214
- expect(doesNotExistFileExists).toBe(true);
215
- const existsStillExists = await existsFile.exists();
216
- expect(existsStillExists).toBe(true); // original still exists
217
- }
218
- });
219
- it('should copy the file to a new location to a different bucket.', async () => {
220
- if (existsFile.copy != null) {
221
- const secondBucket = {
222
- bucketId: 'second-bucket',
223
- pathString: secondBucketTarget.storagePath.pathString
224
- };
225
- const targetFile = f.storageContext.file(secondBucket);
226
- const exists = await targetFile.exists();
227
- expect(exists).toBe(false);
228
- const targetPath = targetFile.storagePath;
229
- const result = await existsFile.copy(targetPath);
230
- expect(result.storagePath.pathString).toBe(targetPath.pathString);
231
- const targetFileExists = await targetFile.exists();
232
- expect(targetFileExists).toBe(true);
233
- const doesNotExistExists = await doesNotExistFile.exists();
234
- expect(doesNotExistExists).toBe(false); // on a different bucket
235
- const existsStillExists = await existsFile.exists();
236
- expect(existsStillExists).toBe(true); // original still exists
237
- }
238
- });
239
- });
240
- describe('move()', () => {
241
- it('should move the file to a new location in the same bucket.', async () => {
242
- if (existsFile.move != null) {
243
- const exists = await doesNotExistFile.exists();
244
- expect(exists).toBe(false);
245
- const targetPath = doesNotExistFile.storagePath;
246
- const result = await existsFile.move(targetPath);
247
- expect(result.storagePath.pathString).toBe(targetPath.pathString);
248
- const doesNotExistExists = await doesNotExistFile.exists();
249
- expect(doesNotExistExists).toBe(true);
250
- const existsStillExists = await existsFile.exists();
251
- expect(existsStillExists).toBe(false); // check was moved
252
- }
253
- });
254
- it('should move the file to a new location to a different bucket.', async () => {
255
- if (existsFile.move != null) {
256
- const secondBucket = {
257
- bucketId: 'second-bucket',
258
- pathString: doesNotExistFile.storagePath.pathString
259
- };
260
- const targetFile = f.storageContext.file(secondBucket);
261
- const exists = await targetFile.exists();
262
- expect(exists).toBe(false);
263
- const targetPath = targetFile.storagePath;
264
- const result = await existsFile.move(targetPath);
265
- expect(result.storagePath.pathString).toBe(targetPath.pathString);
266
- const targetFileExists = await targetFile.exists();
267
- expect(targetFileExists).toBe(true);
268
- const doesNotExistStillDoesNotExists = await doesNotExistFile.exists();
269
- expect(doesNotExistStillDoesNotExists).toBe(false);
270
- const existsStillExists = await existsFile.exists();
271
- expect(existsStillExists).toBe(false); // check was moved
272
- }
273
- });
274
- });
275
- describe('exists()', () => {
276
- it('should return true if the file exists.', async () => {
277
- const result = await existsFile.exists();
278
- expect(result).toBe(true);
279
- });
280
- it('should return false if the file exists.', async () => {
281
- const result = await doesNotExistFile.exists();
282
- expect(result).toBe(false);
283
- });
284
- });
285
- describe('getMetadata()', () => {
286
- (0, test_1.itShouldFail)('if the file does not exist.', async () => {
287
- await (0, test_1.expectFail)(() => doesNotExistFile.getMetadata());
288
- });
289
- it('should return the metadata.', async () => {
290
- const result = await existsFile.getMetadata();
291
- expect(result.bucket).toBe(existsFile.storagePath.bucketId);
292
- expect(result.fullPath).toBe(existsFilePath);
293
- expect(typeof result.size).toBe('number');
294
- expect(result.size).toBeGreaterThan(0);
295
- expect(result.contentType).toBe(existsFileContentType);
296
- expect(result).toBeDefined();
297
- });
298
- });
299
- describe('setMetadata()', () => {
300
- (0, test_1.itShouldFail)('if the file does not exist.', async () => {
301
- await (0, test_1.expectFail)(() => doesNotExistFile.setMetadata({}));
302
- });
303
- it('should replace the content type field.', async () => {
304
- const currentMetadata = await existsFile.getMetadata();
305
- expect(currentMetadata.contentType).toBe(existsFileContentType);
306
- const nextContentType = 'application/json';
307
- const result = await existsFile.setMetadata({
308
- contentType: nextContentType
309
- });
310
- expect(result.contentType).toBe(nextContentType);
311
- const updatedMetadata = await existsFile.getMetadata();
312
- expect(updatedMetadata.contentType).toBe(nextContentType);
313
- });
314
- it('should replace the metadata for only the provided fields.', async () => {
315
- const currentMetadata = await existsFile.getMetadata();
316
- expect(currentMetadata.contentType).toBe(existsFileContentType);
317
- const customMetadataA = {
318
- foo: 'bar'
319
- };
320
- const result = await existsFile.setMetadata({
321
- contentType: undefined, // should not change
322
- customMetadata: customMetadataA
323
- });
324
- expect(result.contentType).toBe(existsFileContentType);
325
- expect(result.customMetadata).toEqual(customMetadataA);
326
- const updatedMetadata = await existsFile.getMetadata();
327
- expect(updatedMetadata.contentType).toBe(existsFileContentType);
328
- expect(updatedMetadata.customMetadata).toEqual(customMetadataA);
329
- // update again. All custom metadata is replaced
330
- const customMetadataB = {
331
- foo: 'baz'
332
- };
333
- const result2 = await existsFile.setMetadata({
334
- customMetadata: customMetadataB
335
- });
336
- expect(result2.contentType).toBe(existsFileContentType);
337
- expect(result2.customMetadata).toEqual(customMetadataB);
338
- const updatedMetadata2 = await existsFile.getMetadata();
339
- expect(updatedMetadata2.contentType).toBe(existsFileContentType);
340
- expect(updatedMetadata2.customMetadata).toEqual(customMetadataB);
341
- });
342
- });
343
- describe('getBytes()', () => {
344
- (0, test_1.itShouldFail)('if the file does not exist.', async () => {
345
- await (0, test_1.expectFail)(() => doesNotExistFile.getBytes());
346
- });
347
- it('should download the file.', async () => {
348
- const result = await existsFile.getBytes();
349
- expect(result).toBeDefined();
350
- const decoded = Buffer.from(result).toString('utf-8');
351
- expect(decoded).toBe(existsFileContent);
352
- });
353
- describe('with maxDownloadSizeBytes configuration', () => {
354
- it('should download up to the maxDownloadSizeBytes number of bytes', async () => {
355
- const charactersToTake = 5;
356
- const result = await existsFile.getBytes(charactersToTake); // each normal utf-8 character is 1 byte
357
- expect(result).toBeDefined();
358
- const decoded = Buffer.from(result).toString('utf-8');
359
- expect(decoded).toBe(existsFileContent.substring(0, charactersToTake));
360
- });
361
- });
362
- });
363
- describe('getStream()', () => {
364
- it('should download the file.', async () => {
365
- if (existsFile.getStream != null) {
366
- // only test if the driver/file has getStream available
367
- const stream = existsFile.getStream();
368
- expect(stream).toBeDefined();
369
- const buffer = await (0, util_1.readableStreamToBuffer)(stream);
370
- const decoded = buffer.toString('utf-8');
371
- expect(decoded).toBe(existsFileContent);
372
- }
373
- });
374
- });
375
- describe('getDownloadUrl()', () => {
376
- (0, test_1.itShouldFail)('if the file does not exist.', async () => {
377
- const doesNotExistFileExists = await doesNotExistFile.exists();
378
- expect(doesNotExistFileExists).toBe(false);
379
- await (0, test_1.expectFail)(() => doesNotExistFile.getDownloadUrl());
380
- });
381
- it('should return the download url.', async () => {
382
- const result = await existsFile.getDownloadUrl();
383
- expect(result).toBeDefined();
384
- expect(typeof result).toBe('string');
385
- });
386
- });
387
- // Cannot be tested, will throw "Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information."
388
- describe('getSignedUrl()', () => {
389
- it('should return the signed read url.', async () => {
390
- if (existsFile.getSignedUrl) {
391
- const result = await existsFile.getSignedUrl({});
392
- expect(result).toBeDefined();
393
- expect(typeof result).toBe('string');
394
- }
395
- });
396
- });
397
- describe('makePublic()', () => {
398
- beforeEach(async () => {
399
- await existsFile.delete();
400
- await existsFile.upload(existsFileContent, { stringFormat: 'raw', contentType: existsFileContentType }); // re-upload for each test
401
- });
402
- it('should make the file public.', async () => {
403
- if (existsFile.makePublic && existsFile.isPublic && existsFile.getAcls) {
404
- // TODO: firestore emulator files seem to always be public and ACLs do not change?
405
- // let isPublic = await existsFile.isPublic();
406
- // expect(isPublic).toBe(false);
407
- // TODO: Not implemented in the emulator properly either
408
- // const acls = await existsFile.getAcls();
409
- // console.log({ acls });
410
- await existsFile.makePublic(true);
411
- // TODO: doesn't really test it properly since true is always returned by the emulator...
412
- const isPublic = await existsFile.isPublic();
413
- expect(isPublic).toBe(true);
414
- // TODO: Not implemented in the emulator
415
- // await existsFile.makePublic(false);
416
- // isPublic = await existsFile.isPublic();
417
- // expect(isPublic).toBe(false);
418
- }
419
- });
420
- });
421
- // TODO: getAcls() and related functions cannot be tested in the emulator currently
422
- describe('delete()', () => {
423
- (0, test_1.itShouldFail)('if the file does not exist.', async () => {
424
- await (0, test_1.expectFail)(() => doesNotExistFile.delete());
425
- });
426
- it('should delete the file at the path.', async () => {
427
- await existsFile.delete();
428
- const result = await existsFile.exists();
429
- expect(result).toBe(false);
430
- });
431
- describe('ignoreNotFound=true', () => {
432
- it('should not throw an error if the file does not exist.', async () => {
433
- await doesNotExistFile.delete({ ignoreNotFound: true });
434
- });
435
- });
436
- });
437
- });
438
- describe('folder()', () => {
439
- const doesNotExistFolderPath = '/doesnotexist/';
440
- let doesNotExistFolder;
441
- const existsFolderPath = '/test/two/';
442
- let existsFolder;
443
- const existsFileName = 'exists.txt';
444
- const existsFilePath = existsFolderPath + existsFileName;
445
- const existsFileContent = 'Hello! \ud83d\ude0a';
446
- let existsFile;
447
- beforeEach(async () => {
448
- doesNotExistFolder = f.storageContext.folder(doesNotExistFolderPath);
449
- existsFolder = f.storageContext.folder(existsFolderPath);
450
- existsFile = f.storageContext.file(existsFilePath);
451
- await existsFile.upload(existsFileContent, { stringFormat: 'raw', contentType: 'text/plain' });
452
- });
453
- describe('exists()', () => {
454
- it('should return false if there are no items in the folder.', async () => {
455
- const exists = await doesNotExistFolder.exists();
456
- expect(exists).toBe(false);
457
- });
458
- it('should return true if there are items in the folder.', async () => {
459
- const exists = await existsFolder.exists();
460
- expect(exists).toBe(true);
461
- });
462
- });
463
- describe('list()', () => {
464
- const existsBFileName = 'a.txt';
465
- const existsBFilePath = existsFolderPath + existsBFileName;
466
- const existsCFolderPath = existsFolderPath + 'c/';
467
- const existsCFilePath = existsCFolderPath + 'c.txt';
468
- const otherFolderPath = '/other/';
469
- const otherFolderFilePath = otherFolderPath + 'other.txt';
470
- beforeEach(async () => {
471
- await f.storageContext.file(existsBFilePath).upload(existsFileContent, { stringFormat: 'raw', contentType: 'text/plain' });
472
- await f.storageContext.file(existsCFilePath).upload(existsFileContent, { stringFormat: 'raw', contentType: 'text/plain' });
473
- await f.storageContext.file(otherFolderFilePath).upload(existsFileContent, { stringFormat: 'raw', contentType: 'text/plain' });
474
- });
475
- describe('options', () => {
476
- describe('listAll', () => {
477
- describe('=false/unset', () => {
478
- it('should list all the direct files and folders that exist on the test path.', async () => {
479
- const result = await existsFolder.list();
480
- expect(result).toBeDefined();
481
- const files = result.files();
482
- expect(files.length).toBe(2);
483
- const fileNames = new Set(files.map((x) => x.name));
484
- expect(fileNames).toContain(existsFileName);
485
- expect(fileNames).toContain(existsBFileName);
486
- const folders = result.folders();
487
- expect(folders.length).toBe(1);
488
- const folderNames = new Set(folders.map((x) => x.name));
489
- expect(folderNames).toContain('c');
490
- });
491
- it('should list all the direct folders that exist at the root.', async () => {
492
- const rootFolder = await f.storageContext.folder('/');
493
- const result = await rootFolder.list();
494
- expect(result).toBeDefined();
495
- const files = result.files();
496
- expect(files.length).toBe(0); // files are under /test/ and /other/
497
- const folders = result.folders();
498
- expect(folders.length).toBe(2);
499
- const names = new Set(folders.map((x) => x.name));
500
- expect(names).toContain('test');
501
- expect(names).toContain('other');
502
- });
503
- });
504
- describe('=true', () => {
505
- it('should list all files and folders that exist on the test path.', async () => {
506
- const result = await existsFolder.list({ includeNestedResults: true });
507
- expect(result).toBeDefined();
508
- const files = result.files();
509
- expect(files.length).toBe(3);
510
- const filePaths = new Set(files.map((x) => `${util_1.SLASH_PATH_SEPARATOR}${x.storagePath.pathString}`));
511
- expect(filePaths).toContain(existsFilePath);
512
- expect(filePaths).toContain(existsBFilePath);
513
- expect(filePaths).toContain(existsCFilePath);
514
- expect(filePaths).not.toContain(otherFolderFilePath);
515
- // folders are not counted/returned
516
- const folders = result.folders();
517
- expect(folders.length).toBe(0);
518
- });
519
- it('should list all the folders that exist at the root.', async () => {
520
- const rootFolder = await f.storageContext.folder('/');
521
- const result = await rootFolder.list({ includeNestedResults: true });
522
- expect(result).toBeDefined();
523
- const files = result.files();
524
- expect(files.length).toBe(4); // all created files
525
- const folders = result.folders();
526
- expect(folders.length).toBe(0);
527
- });
528
- describe('maxResults', () => {
529
- it('should limit the number of results returned.', async () => {
530
- const rootFolder = await f.storageContext.folder('/');
531
- const limit = 2;
532
- const result = await rootFolder.list({ includeNestedResults: true, maxResults: limit });
533
- expect(result).toBeDefined();
534
- if (f.storageContext.drivers.storageAccessorDriver.type === 'server') {
535
- // Currently only the server can properly limit the number of results returned.
536
- // The client-side will limit the results somewhat, but if folders are returned then it will return the results of those folders as well.
537
- const files = result.files();
538
- expect(files.length).toBe(limit);
539
- const nextPage = await result.next();
540
- const nextPageFiles = nextPage.files();
541
- expect(nextPageFiles.length).toBe(limit);
542
- }
543
- const folders = result.folders();
544
- expect(folders.length).toBe(0);
545
- });
546
- });
547
- });
548
- });
549
- });
550
- describe('file()', () => {
551
- it('should return the file for the result.', async () => {
552
- const result = await existsFolder.list();
553
- expect(result).toBeDefined();
554
- const files = result.files();
555
- const fileResult = files.find((x) => x.name === existsFileName);
556
- const file = fileResult.file();
557
- const exists = await file.exists();
558
- expect(exists).toBe(true);
559
- });
560
- });
561
- describe('folder()', () => {
562
- it('should return the folder for the result.', async () => {
563
- const rootFolder = await f.storageContext.folder('/');
564
- const result = await rootFolder.list();
565
- expect(result).toBeDefined();
566
- const folders = result.folders();
567
- const folderResult = folders.find((x) => x.name === 'test');
568
- const folder = folderResult.folder();
569
- const exists = await folder.exists();
570
- expect(exists).toBe(true);
571
- });
572
- });
573
- describe('next()', () => {
574
- it('should return the next set of results.', async () => {
575
- const maxResults = 1;
576
- const rootFolder = await f.storageContext.folder(existsFolderPath);
577
- const result = await rootFolder.list({ maxResults });
578
- expect(result).toBeDefined();
579
- const files = result.files();
580
- expect(files.length).toBe(maxResults);
581
- const next = await result.next();
582
- expect(next).toBeDefined();
583
- const nextFiles = next.files();
584
- expect(nextFiles.length).toBe(maxResults);
585
- expect(nextFiles[0].storagePath.pathString).not.toBe(files[0].storagePath.pathString);
586
- expect(next.hasNext).toBe(false);
587
- });
588
- (0, test_1.itShouldFail)('if next() is called and hasNext was false.', async () => {
589
- const rootFolder = await f.storageContext.folder(existsFolderPath);
590
- const result = await rootFolder.list({});
591
- expect(result.hasNext).toBe(false);
592
- await (0, test_1.expectFail)(() => result.next());
593
- });
594
- });
595
- describe('maxResults', () => {
596
- it('should respect the max results.', async () => {
597
- const maxResults = 1;
598
- const rootFolder = await f.storageContext.folder(existsFolderPath);
599
- const result = await rootFolder.list({ maxResults });
600
- expect(result).toBeDefined();
601
- const files = result.files();
602
- expect(files.length).toBe(maxResults);
603
- const folders = result.folders();
604
- expect(folders.length).toBe(1);
605
- const names = new Set(folders.map((x) => x.name));
606
- expect(names).toContain('c');
607
- });
608
- it('prefixes/folders are unaffected by maxResults.', async () => {
609
- const maxResults = 1;
610
- const rootFolder = await f.storageContext.folder('/');
611
- const result = await rootFolder.list({ maxResults });
612
- expect(result).toBeDefined();
613
- const files = result.files();
614
- expect(files.length).toBe(0); // files are under /test/ and /other/
615
- const folders = result.folders();
616
- expect(folders.length).toBe(2);
617
- const names = new Set(folders.map((x) => x.name));
618
- expect(names).toContain('test');
619
- expect(names).toContain('other');
620
- });
621
- });
622
- describe('utilities', () => {
623
- describe('iterateStorageListFilesByEachFile()', () => {
624
- it('should iterate through all the files in the current folder one at a time', async () => {
625
- const visitedFiles = [];
626
- const result = await (0, firebase_1.iterateStorageListFilesByEachFile)({
627
- folder: existsFolder,
628
- readItemsFromPageResult: (x) => x.result.files(),
629
- iterateEachPageItem: async (file) => {
630
- visitedFiles.push(file);
631
- }
632
- });
633
- const visitedFilePathStrings = visitedFiles.map((x) => `${util_1.SLASH_PATH_SEPARATOR}${x.storagePath.pathString}`);
634
- expect(visitedFilePathStrings).toContain(existsFilePath);
635
- expect(visitedFilePathStrings).toContain(existsBFilePath);
636
- expect(visitedFilePathStrings).not.toContain(existsCFilePath);
637
- expect(visitedFilePathStrings).not.toContain(otherFolderFilePath);
638
- expect(result).toBeDefined();
639
- expect(result.totalItemsLoaded).toBe(2);
640
- expect(result.totalItemsVisited).toBe(visitedFiles.length);
641
- });
642
- describe('includeNestedResults=true', () => {
643
- it('should iterate through all the files and nested files under the current folder one at a time', async () => {
644
- const visitedFiles = [];
645
- const result = await (0, firebase_1.iterateStorageListFilesByEachFile)({
646
- folder: existsFolder,
647
- includeNestedResults: true,
648
- readItemsFromPageResult: (x) => x.result.files(),
649
- iterateEachPageItem: async (file) => {
650
- visitedFiles.push(file);
651
- }
652
- });
653
- const visitedFilePathStrings = visitedFiles.map((x) => `${util_1.SLASH_PATH_SEPARATOR}${x.storagePath.pathString}`);
654
- expect(result).toBeDefined();
655
- expect(result.totalItemsLoaded).toBe(3);
656
- expect(result.totalItemsVisited).toBe(visitedFiles.length);
657
- expect(visitedFilePathStrings).toContain(existsFilePath);
658
- expect(visitedFilePathStrings).toContain(existsBFilePath);
659
- expect(visitedFilePathStrings).toContain(existsCFilePath);
660
- expect(visitedFilePathStrings).not.toContain(otherFolderFilePath);
661
- });
662
- });
663
- });
664
- });
665
- });
666
- });
667
- });
668
- }
669
- //# sourceMappingURL=test.driver.accessor.js.map