@protontech/drive-sdk 0.0.11 → 0.0.13
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/dist/cache/index.d.ts +2 -0
- package/dist/cache/index.js +6 -0
- package/dist/cache/index.js.map +1 -0
- package/dist/cache/interface.d.ts +105 -0
- package/dist/cache/interface.js +3 -0
- package/dist/cache/interface.js.map +1 -0
- package/dist/cache/memoryCache.d.ts +18 -0
- package/dist/cache/memoryCache.js +78 -0
- package/dist/cache/memoryCache.js.map +1 -0
- package/dist/cache/memoryCache.test.d.ts +1 -0
- package/dist/cache/memoryCache.test.js +121 -0
- package/dist/cache/memoryCache.test.js.map +1 -0
- package/dist/crypto/hmac.d.ts +22 -0
- package/dist/crypto/hmac.js +44 -0
- package/dist/crypto/hmac.js.map +1 -0
- package/dist/crypto/utils.d.ts +2 -0
- package/dist/crypto/utils.js +35 -0
- package/dist/crypto/utils.js.map +1 -0
- package/dist/errors.d.ts +142 -0
- package/dist/errors.js +168 -0
- package/dist/errors.js.map +1 -0
- package/dist/interface/account.js +3 -0
- package/dist/interface/account.js.map +1 -0
- package/dist/interface/author.d.ts +26 -0
- package/dist/interface/author.js +3 -0
- package/dist/interface/author.js.map +1 -0
- package/dist/interface/download.d.ts +29 -0
- package/dist/interface/download.js +3 -0
- package/dist/interface/download.js.map +1 -0
- package/dist/interface/httpClient.d.ts +38 -0
- package/dist/interface/httpClient.js +3 -0
- package/dist/interface/httpClient.js.map +1 -0
- package/dist/interface/index.d.ts +1 -1
- package/dist/interface/nodes.d.ts +12 -1
- package/dist/interface/nodes.js +11 -0
- package/dist/interface/nodes.js.map +1 -1
- package/dist/interface/result.d.ts +9 -0
- package/dist/interface/result.js +11 -0
- package/dist/interface/result.js.map +1 -0
- package/dist/interface/thumbnail.d.ts +17 -0
- package/dist/interface/thumbnail.js +9 -0
- package/dist/interface/thumbnail.js.map +1 -0
- package/dist/interface/upload.d.ts +64 -0
- package/dist/interface/upload.js +3 -0
- package/dist/interface/upload.js.map +1 -0
- package/dist/internal/apiService/driveTypes.d.ts +1341 -465
- package/dist/internal/apiService/errorCodes.d.ts +30 -0
- package/dist/internal/apiService/errorCodes.js +11 -0
- package/dist/internal/apiService/errorCodes.js.map +1 -0
- package/dist/internal/apiService/errors.js +2 -2
- package/dist/internal/apiService/errors.js.map +1 -1
- package/dist/internal/apiService/observerStream.d.ts +3 -0
- package/dist/internal/apiService/observerStream.js +15 -0
- package/dist/internal/apiService/observerStream.js.map +1 -0
- package/dist/internal/apiService/transformers.js +2 -0
- package/dist/internal/apiService/transformers.js.map +1 -1
- package/dist/internal/asyncIteratorMap.d.ts +15 -0
- package/dist/internal/asyncIteratorMap.js +59 -0
- package/dist/internal/asyncIteratorMap.js.map +1 -0
- package/dist/internal/asyncIteratorMap.test.d.ts +1 -0
- package/dist/internal/asyncIteratorMap.test.js +120 -0
- package/dist/internal/asyncIteratorMap.test.js.map +1 -0
- package/dist/internal/batchLoading.d.ts +34 -0
- package/dist/internal/batchLoading.js +68 -0
- package/dist/internal/batchLoading.js.map +1 -0
- package/dist/internal/batchLoading.test.d.ts +1 -0
- package/dist/internal/batchLoading.test.js +50 -0
- package/dist/internal/batchLoading.test.js.map +1 -0
- package/dist/internal/download/controller.d.ts +8 -0
- package/dist/internal/download/controller.js +22 -0
- package/dist/internal/download/controller.js.map +1 -0
- package/dist/internal/download/queue.d.ts +5 -0
- package/dist/internal/download/queue.js +31 -0
- package/dist/internal/download/queue.js.map +1 -0
- package/dist/internal/errors.js +28 -0
- package/dist/internal/errors.js.map +1 -0
- package/dist/internal/errors.test.js +22 -0
- package/dist/internal/errors.test.js.map +1 -0
- package/dist/internal/events/interface.d.ts +47 -0
- package/dist/internal/events/interface.js +12 -0
- package/dist/internal/events/interface.js.map +1 -0
- package/dist/internal/nodes/apiService.d.ts +2 -2
- package/dist/internal/nodes/apiService.js +16 -6
- package/dist/internal/nodes/apiService.js.map +1 -1
- package/dist/internal/nodes/apiService.test.js +30 -8
- package/dist/internal/nodes/apiService.test.js.map +1 -1
- package/dist/internal/nodes/cache.js +1 -0
- package/dist/internal/nodes/cache.js.map +1 -1
- package/dist/internal/nodes/cache.test.js +1 -0
- package/dist/internal/nodes/cache.test.js.map +1 -1
- package/dist/internal/nodes/cryptoService.test.js +34 -0
- package/dist/internal/nodes/cryptoService.test.js.map +1 -1
- package/dist/internal/nodes/index.test.js +3 -1
- package/dist/internal/nodes/index.test.js.map +1 -1
- package/dist/internal/nodes/interface.d.ts +3 -1
- package/dist/internal/nodes/mediaTypes.d.ts +2 -0
- package/dist/internal/nodes/mediaTypes.js +13 -0
- package/dist/internal/nodes/mediaTypes.js.map +1 -0
- package/dist/internal/nodes/nodesAccess.js +28 -7
- package/dist/internal/nodes/nodesAccess.js.map +1 -1
- package/dist/internal/nodes/nodesAccess.test.js +7 -6
- package/dist/internal/nodes/nodesAccess.test.js.map +1 -1
- package/dist/internal/nodes/validations.d.ts +4 -0
- package/dist/internal/nodes/validations.js +21 -0
- package/dist/internal/nodes/validations.js.map +1 -0
- package/dist/internal/sharing/apiService.js +19 -2
- package/dist/internal/sharing/apiService.js.map +1 -1
- package/dist/internal/uids.d.ts +38 -0
- package/dist/internal/uids.js +85 -0
- package/dist/internal/uids.js.map +1 -0
- package/dist/internal/upload/chunkStreamReader.d.ts +13 -0
- package/dist/internal/upload/chunkStreamReader.js +46 -0
- package/dist/internal/upload/chunkStreamReader.js.map +1 -0
- package/dist/internal/upload/chunkStreamReader.test.d.ts +1 -0
- package/dist/internal/upload/chunkStreamReader.test.js +75 -0
- package/dist/internal/upload/chunkStreamReader.test.js.map +1 -0
- package/dist/internal/upload/controller.d.ts +8 -0
- package/dist/internal/upload/controller.js +25 -0
- package/dist/internal/upload/controller.js.map +1 -0
- package/dist/internal/upload/digests.d.ts +8 -0
- package/dist/internal/upload/digests.js +22 -0
- package/dist/internal/upload/digests.js.map +1 -0
- package/dist/internal/upload/fileUploader.d.ts +49 -53
- package/dist/internal/upload/fileUploader.js +91 -395
- package/dist/internal/upload/fileUploader.js.map +1 -1
- package/dist/internal/upload/fileUploader.test.js +38 -292
- package/dist/internal/upload/fileUploader.test.js.map +1 -1
- package/dist/internal/upload/index.d.ts +3 -3
- package/dist/internal/upload/index.js +20 -41
- package/dist/internal/upload/index.js.map +1 -1
- package/dist/internal/upload/manager.d.ts +1 -1
- package/dist/internal/upload/manager.js +16 -19
- package/dist/internal/upload/manager.js.map +1 -1
- package/dist/internal/upload/manager.test.js +42 -83
- package/dist/internal/upload/manager.test.js.map +1 -1
- package/dist/internal/upload/queue.d.ts +5 -0
- package/dist/internal/upload/queue.js +32 -0
- package/dist/internal/upload/queue.js.map +1 -0
- package/dist/internal/upload/streamUploader.d.ts +62 -0
- package/dist/internal/upload/streamUploader.js +441 -0
- package/dist/internal/upload/streamUploader.js.map +1 -0
- package/dist/internal/upload/streamUploader.test.d.ts +1 -0
- package/dist/internal/upload/streamUploader.test.js +358 -0
- package/dist/internal/upload/streamUploader.test.js.map +1 -0
- package/dist/internal/utils.d.ts +1 -0
- package/dist/internal/utils.js +13 -0
- package/dist/internal/utils.js.map +1 -0
- package/dist/internal/wait.d.ts +3 -0
- package/dist/internal/wait.js +28 -0
- package/dist/internal/wait.js.map +1 -0
- package/dist/internal/wait.test.d.ts +1 -0
- package/dist/internal/wait.test.js +21 -0
- package/dist/internal/wait.test.js.map +1 -0
- package/dist/protonDriveClient.d.ts +4 -4
- package/dist/protonDriveClient.js +1 -1
- package/dist/protonDriveClient.js.map +1 -1
- package/package.json +2 -2
- package/src/errors.ts +10 -4
- package/src/interface/index.ts +1 -1
- package/src/interface/nodes.ts +11 -0
- package/src/interface/upload.ts +53 -3
- package/src/internal/apiService/driveTypes.ts +1341 -465
- package/src/internal/apiService/errors.ts +3 -2
- package/src/internal/apiService/transformers.ts +2 -0
- package/src/internal/asyncIteratorMap.test.ts +150 -0
- package/src/internal/asyncIteratorMap.ts +64 -0
- package/src/internal/nodes/apiService.test.ts +36 -7
- package/src/internal/nodes/apiService.ts +19 -7
- package/src/internal/nodes/cache.test.ts +1 -0
- package/src/internal/nodes/cache.ts +1 -0
- package/src/internal/nodes/cryptoService.test.ts +38 -0
- package/src/internal/nodes/index.test.ts +3 -1
- package/src/internal/nodes/interface.ts +4 -1
- package/src/internal/nodes/nodesAccess.test.ts +7 -6
- package/src/internal/nodes/nodesAccess.ts +30 -7
- package/src/internal/sharing/apiService.ts +24 -2
- package/src/internal/upload/fileUploader.test.ts +46 -376
- package/src/internal/upload/fileUploader.ts +114 -494
- package/src/internal/upload/index.ts +26 -50
- package/src/internal/upload/manager.test.ts +45 -92
- package/src/internal/upload/manager.ts +30 -32
- package/src/internal/upload/streamUploader.test.ts +469 -0
- package/src/internal/upload/streamUploader.ts +552 -0
- package/src/protonDriveClient.ts +5 -4
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MemoryCache = void 0;
|
|
4
|
+
var memoryCache_1 = require("./memoryCache");
|
|
5
|
+
Object.defineProperty(exports, "MemoryCache", { enumerable: true, get: function () { return memoryCache_1.MemoryCache; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cache/index.ts"],"names":[],"mappings":";;;AACA,6CAA4C;AAAnC,0GAAA,WAAW,OAAA"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
export interface ProtonDriveCacheConstructor<T> {
|
|
2
|
+
/**
|
|
3
|
+
* Initialize the cache.
|
|
4
|
+
*
|
|
5
|
+
* The local database should follow document-based structure. The SDK does
|
|
6
|
+
* serialisation and data is not intended to be read by 3rd party. The SDK,
|
|
7
|
+
* however, provides also clear fields in form of tags that is used for
|
|
8
|
+
* search. Local database should have index or other structure for easier
|
|
9
|
+
* look-up.
|
|
10
|
+
*
|
|
11
|
+
* See {@link setEntity} for more details how tags are used.
|
|
12
|
+
*/
|
|
13
|
+
new (): ProtonDriveCache<T>;
|
|
14
|
+
}
|
|
15
|
+
export interface ProtonDriveCache<T> {
|
|
16
|
+
/**
|
|
17
|
+
* Re-creates the whole persistent cache.
|
|
18
|
+
*
|
|
19
|
+
* The SDK can call this when there is some inconsistency and it is better
|
|
20
|
+
* to start from scratch rather than fix it.
|
|
21
|
+
*/
|
|
22
|
+
clear(): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Adds or updates entity in the local database.
|
|
25
|
+
*
|
|
26
|
+
* The `tags` is a list of strings that should be stored properly for fast
|
|
27
|
+
* look-up.
|
|
28
|
+
*
|
|
29
|
+
* @example Usage by the SDK
|
|
30
|
+
* ```ts
|
|
31
|
+
* await cache.setEntity("node-abc42", "{ node abc42 serialised data }", ["parentUid:abc123", "sharedWithMe"] });
|
|
32
|
+
* await Array.fromAsync(cache.iterateEntitiesByTag("parentUid:abc123")); // returns ["node-abc42"]
|
|
33
|
+
* await cache.getEntity("node-abc42"); // returns "{ node abc42 serialised data }"
|
|
34
|
+
* await Array.fromAsync(cache.iterateEntities(["node-abc42"])); // returns ["{ node abc42 serialised data }"]
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example Stored data
|
|
38
|
+
* ```json
|
|
39
|
+
* {
|
|
40
|
+
* type: "node",
|
|
41
|
+
* version: 1,
|
|
42
|
+
* internal: {
|
|
43
|
+
* isStale,
|
|
44
|
+
* claimedDigests,
|
|
45
|
+
* // ...
|
|
46
|
+
* }
|
|
47
|
+
* node: {
|
|
48
|
+
* // same as node entity, here some example
|
|
49
|
+
* uid,
|
|
50
|
+
* parentUid,
|
|
51
|
+
* // ...
|
|
52
|
+
* }
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @param key - Key is internal ID controlled by the SDK. It combines type and ID of the entity.
|
|
57
|
+
* @param value - Serialised JSON object controlled by the SDK. It is not intended for use outside of the SDK.
|
|
58
|
+
* @param tags - Clear metadata about the entity used for filtering. It is intended to store efficiently for fast look-up.
|
|
59
|
+
* @throws Exception if `key` from `tags` is not one of the tag keys provided from `usedTagKeysBySDK` in constructor.
|
|
60
|
+
*/
|
|
61
|
+
setEntity(key: string, value: T, tags?: string[]): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Returns the data of the entity stored locally.
|
|
64
|
+
*
|
|
65
|
+
* @throws Exception if entity is not present.
|
|
66
|
+
*/
|
|
67
|
+
getEntity(key: string): Promise<T>;
|
|
68
|
+
/**
|
|
69
|
+
* Generator providing the data of the entities stored locally for given
|
|
70
|
+
* list of keys.
|
|
71
|
+
*
|
|
72
|
+
* No exception is thrown when data is missing.
|
|
73
|
+
*/
|
|
74
|
+
iterateEntities(keys: string[]): AsyncGenerator<EntityResult<T>>;
|
|
75
|
+
/**
|
|
76
|
+
* Generator providing the data of the entities stored locally for given
|
|
77
|
+
* filter option.
|
|
78
|
+
*
|
|
79
|
+
* No exception is thrown when data is missing.
|
|
80
|
+
*
|
|
81
|
+
* @example Usage by the SDK
|
|
82
|
+
* ```ts
|
|
83
|
+
* await cache.setEntity("node-abc42", "{ node abc42 serialised data }", { "parentUid": "abc123", "shared": "withMe" });
|
|
84
|
+
* await Array.fromAsync(cache.iterateEntitiesByTag("parentUid", "abc123")); // returns ["node-abc42"]
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @param tag - The tag, for example `parentUid:abc123`
|
|
88
|
+
*/
|
|
89
|
+
iterateEntitiesByTag(tag: string): AsyncGenerator<EntityResult<T>>;
|
|
90
|
+
/**
|
|
91
|
+
* Removes completely the entity stored locally from the database.
|
|
92
|
+
*
|
|
93
|
+
* It is no-op if entity is not present.
|
|
94
|
+
*/
|
|
95
|
+
removeEntities(keys: string[]): Promise<void>;
|
|
96
|
+
}
|
|
97
|
+
export type EntityResult<T> = {
|
|
98
|
+
key: string;
|
|
99
|
+
ok: true;
|
|
100
|
+
value: T;
|
|
101
|
+
} | {
|
|
102
|
+
key: string;
|
|
103
|
+
ok: false;
|
|
104
|
+
error: string;
|
|
105
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interface.js","sourceRoot":"","sources":["../../src/cache/interface.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ProtonDriveCache, EntityResult } from './interface';
|
|
2
|
+
/**
|
|
3
|
+
* In-memory cache implementation for Proton Drive SDK.
|
|
4
|
+
*
|
|
5
|
+
* This cache is not persistent and is intended for mostly for testing or
|
|
6
|
+
* development only. It is not recommended to use this cache in production
|
|
7
|
+
* environments.
|
|
8
|
+
*/
|
|
9
|
+
export declare class MemoryCache<T> implements ProtonDriveCache<T> {
|
|
10
|
+
private entities;
|
|
11
|
+
private entitiesByTag;
|
|
12
|
+
clear(): Promise<void>;
|
|
13
|
+
setEntity(key: string, value: T, tags?: string[]): Promise<void>;
|
|
14
|
+
getEntity(key: string): Promise<NonNullable<T>>;
|
|
15
|
+
iterateEntities(keys: string[]): AsyncGenerator<EntityResult<T>>;
|
|
16
|
+
iterateEntitiesByTag(tag: string): AsyncGenerator<EntityResult<T>>;
|
|
17
|
+
removeEntities(keys: string[]): Promise<void>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MemoryCache = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* In-memory cache implementation for Proton Drive SDK.
|
|
6
|
+
*
|
|
7
|
+
* This cache is not persistent and is intended for mostly for testing or
|
|
8
|
+
* development only. It is not recommended to use this cache in production
|
|
9
|
+
* environments.
|
|
10
|
+
*/
|
|
11
|
+
class MemoryCache {
|
|
12
|
+
entities = {};
|
|
13
|
+
entitiesByTag = {};
|
|
14
|
+
async clear() {
|
|
15
|
+
this.entities = {};
|
|
16
|
+
}
|
|
17
|
+
async setEntity(key, value, tags) {
|
|
18
|
+
this.entities[key] = value;
|
|
19
|
+
for (const tag of Object.keys(this.entitiesByTag)) {
|
|
20
|
+
const index = this.entitiesByTag[tag].indexOf(key);
|
|
21
|
+
if (index !== -1) {
|
|
22
|
+
this.entitiesByTag[tag].splice(index, 1);
|
|
23
|
+
if (this.entitiesByTag[tag].length === 0) {
|
|
24
|
+
delete this.entitiesByTag[tag];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (tags) {
|
|
29
|
+
for (const tag of tags) {
|
|
30
|
+
if (!this.entitiesByTag[tag]) {
|
|
31
|
+
this.entitiesByTag[tag] = [];
|
|
32
|
+
}
|
|
33
|
+
this.entitiesByTag[tag].push(key);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async getEntity(key) {
|
|
38
|
+
const value = this.entities[key];
|
|
39
|
+
if (!value) {
|
|
40
|
+
throw Error('Entity not found');
|
|
41
|
+
}
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
async *iterateEntities(keys) {
|
|
45
|
+
for (const key of keys) {
|
|
46
|
+
try {
|
|
47
|
+
const value = await this.getEntity(key);
|
|
48
|
+
yield { key, ok: true, value };
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
yield { key, ok: false, error: `${error}` };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async *iterateEntitiesByTag(tag) {
|
|
56
|
+
const keys = this.entitiesByTag[tag];
|
|
57
|
+
if (!keys) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
// Pass copy of keys so concurrent changes to the cache do not affect
|
|
61
|
+
// results from iterating entities.
|
|
62
|
+
yield* this.iterateEntities([...keys]);
|
|
63
|
+
}
|
|
64
|
+
async removeEntities(keys) {
|
|
65
|
+
for (const key of keys) {
|
|
66
|
+
delete this.entities[key];
|
|
67
|
+
Object.values(this.entitiesByTag).forEach((tagKeys) => {
|
|
68
|
+
const index = tagKeys.indexOf(key);
|
|
69
|
+
if (index !== -1) {
|
|
70
|
+
tagKeys.splice(index, 1);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.MemoryCache = MemoryCache;
|
|
77
|
+
;
|
|
78
|
+
//# sourceMappingURL=memoryCache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memoryCache.js","sourceRoot":"","sources":["../../src/cache/memoryCache.ts"],"names":[],"mappings":";;;AAKA;;;;;;GAMG;AACH,MAAa,WAAW;IACZ,QAAQ,GAAqB,EAAE,CAAC;IAChC,aAAa,GAAc,EAAE,CAAC;IAEtC,KAAK,CAAC,KAAK;QACP,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;IACvB,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,GAAW,EAAE,KAAQ,EAAE,IAAe;QAClD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QAE3B,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,EAAE,CAAC;YAChD,MAAM,KAAK,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YACnD,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;gBACf,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;gBACzC,IAAI,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;oBACvC,OAAO,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;gBACnC,CAAC;YACL,CAAC;QACL,CAAC;QAED,IAAI,IAAI,EAAE,CAAC;YACP,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;gBACrB,IAAI,CAAC,IAAI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AAG,IAAI,IAAI,EAAE,CAAC;YACrB,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;YAC1B,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;gBAClD,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;gBACnC,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;oBACf,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;gBAC7B,CAAC;YACL,CAAC,CAAC,CAAC;QACP,CAAC;IACL,CAAC;CACJ;AAxED,kCAwEC;AAAA,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const memoryCache_1 = require("./memoryCache");
|
|
4
|
+
describe('MemoryCache', () => {
|
|
5
|
+
let cache;
|
|
6
|
+
beforeEach(async () => {
|
|
7
|
+
cache = new memoryCache_1.MemoryCache();
|
|
8
|
+
await cache.setEntity('key1', 'value1', ['tag1:hello', 'tag2:world']);
|
|
9
|
+
await cache.setEntity('key2', 'value2', ['tag2:world']);
|
|
10
|
+
await cache.setEntity('key3', 'value3');
|
|
11
|
+
});
|
|
12
|
+
it('should store and retrieve an entity', async () => {
|
|
13
|
+
const key = 'newkey';
|
|
14
|
+
const value = 'newvalue';
|
|
15
|
+
await cache.setEntity(key, value);
|
|
16
|
+
const result = await cache.getEntity(key);
|
|
17
|
+
expect(result).toBe(value);
|
|
18
|
+
});
|
|
19
|
+
it('should update an entity with tags - remove old and add new tags', async () => {
|
|
20
|
+
const key = 'newkey';
|
|
21
|
+
await cache.setEntity(key, 'value1', ['tag1', 'tag2']);
|
|
22
|
+
await cache.setEntity(key, 'value2', ['tag2', 'tag3']);
|
|
23
|
+
const result = await cache.getEntity(key);
|
|
24
|
+
expect(result).toBe('value2');
|
|
25
|
+
const tag1 = await Array.fromAsync(cache.iterateEntitiesByTag('tag1'));
|
|
26
|
+
expect(tag1).toEqual([]);
|
|
27
|
+
const tag2 = await Array.fromAsync(cache.iterateEntitiesByTag('tag2'));
|
|
28
|
+
expect(tag2).toEqual([{ key, ok: true, value: 'value2' }]);
|
|
29
|
+
const tag3 = await Array.fromAsync(cache.iterateEntitiesByTag('tag3'));
|
|
30
|
+
expect(tag3).toEqual([{ key, ok: true, value: 'value2' }]);
|
|
31
|
+
});
|
|
32
|
+
it('should throw an error when retrieving a non-existing entity', async () => {
|
|
33
|
+
const key = 'newkey';
|
|
34
|
+
try {
|
|
35
|
+
await cache.getEntity(key);
|
|
36
|
+
fail('Should have thrown an error');
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
expect(`${error}`).toBe('Error: Entity not found');
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
it('should iterate over entities', async () => {
|
|
43
|
+
const results = [];
|
|
44
|
+
for await (const result of cache.iterateEntities(['key1', 'key2', 'key100'])) {
|
|
45
|
+
results.push(result);
|
|
46
|
+
}
|
|
47
|
+
expect(results).toEqual([
|
|
48
|
+
{ key: 'key1', ok: true, value: 'value1' },
|
|
49
|
+
{ key: 'key2', ok: true, value: 'value2' },
|
|
50
|
+
{ key: 'key100', ok: false, error: 'Error: Entity not found' },
|
|
51
|
+
]);
|
|
52
|
+
});
|
|
53
|
+
it('should iterate over entities by tag', async () => {
|
|
54
|
+
const results = [];
|
|
55
|
+
for await (const result of cache.iterateEntitiesByTag('tag2:world')) {
|
|
56
|
+
results.push(result);
|
|
57
|
+
}
|
|
58
|
+
expect(results).toEqual([
|
|
59
|
+
{ key: 'key1', ok: true, value: 'value1' },
|
|
60
|
+
{ key: 'key2', ok: true, value: 'value2' },
|
|
61
|
+
]);
|
|
62
|
+
});
|
|
63
|
+
it('should iterate over entities with multiple tags by tag', async () => {
|
|
64
|
+
const results = [];
|
|
65
|
+
for await (const result of cache.iterateEntitiesByTag('tag1:hello')) {
|
|
66
|
+
results.push(result);
|
|
67
|
+
}
|
|
68
|
+
expect(results).toEqual([
|
|
69
|
+
{ key: 'key1', ok: true, value: 'value1' },
|
|
70
|
+
]);
|
|
71
|
+
});
|
|
72
|
+
it('should iterate over entities by empty tag', async () => {
|
|
73
|
+
const results = [];
|
|
74
|
+
for await (const result of cache.iterateEntitiesByTag('nonexistent')) {
|
|
75
|
+
results.push(result);
|
|
76
|
+
}
|
|
77
|
+
expect(results).toEqual([]);
|
|
78
|
+
});
|
|
79
|
+
it('should iterate over entities with concurrent changes to the same set', async () => {
|
|
80
|
+
const iterator = cache.iterateEntities(['key1', 'key2', 'key3']);
|
|
81
|
+
const results = [];
|
|
82
|
+
const { value: { key: key1 } } = await iterator.next();
|
|
83
|
+
results.push(key1);
|
|
84
|
+
await cache.removeEntities([key1]);
|
|
85
|
+
let value = await iterator.next(); // key2
|
|
86
|
+
results.push(value.value.key);
|
|
87
|
+
value = await iterator.next(); // key3
|
|
88
|
+
results.push(value.value.key);
|
|
89
|
+
expect(results).toEqual(['key1', 'key2', 'key3']);
|
|
90
|
+
});
|
|
91
|
+
it('should remove entities', async () => {
|
|
92
|
+
await cache.removeEntities(['key1', 'key3']);
|
|
93
|
+
const results = [];
|
|
94
|
+
for await (const result of cache.iterateEntities(['key1', 'key2', 'key3'])) {
|
|
95
|
+
results.push(result);
|
|
96
|
+
}
|
|
97
|
+
expect(results).toEqual([
|
|
98
|
+
{ key: 'key1', ok: false, error: 'Error: Entity not found' },
|
|
99
|
+
{ key: 'key2', ok: true, value: 'value2' },
|
|
100
|
+
{ key: 'key3', ok: false, error: 'Error: Entity not found' },
|
|
101
|
+
]);
|
|
102
|
+
const results2 = [];
|
|
103
|
+
for await (const result of cache.iterateEntitiesByTag('tag1:hello')) {
|
|
104
|
+
results2.push(result);
|
|
105
|
+
}
|
|
106
|
+
expect(results2).toEqual([]);
|
|
107
|
+
});
|
|
108
|
+
it('should clear the cache', async () => {
|
|
109
|
+
await cache.clear();
|
|
110
|
+
const results = [];
|
|
111
|
+
for await (const result of cache.iterateEntities(['key1', 'key2', 'key3'])) {
|
|
112
|
+
results.push(result);
|
|
113
|
+
}
|
|
114
|
+
expect(results).toEqual([
|
|
115
|
+
{ key: 'key1', ok: false, error: 'Error: Entity not found' },
|
|
116
|
+
{ key: 'key2', ok: false, error: 'Error: Entity not found' },
|
|
117
|
+
{ key: 'key3', ok: false, error: 'Error: Entity not found' },
|
|
118
|
+
]);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
//# sourceMappingURL=memoryCache.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memoryCache.test.js","sourceRoot":"","sources":["../../src/cache/memoryCache.test.ts"],"names":[],"mappings":";;AACA,+CAA4C;AAE5C,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;IACzB,IAAI,KAA0B,CAAC;IAE/B,UAAU,CAAC,KAAK,IAAI,EAAE;QAClB,KAAK,GAAG,IAAI,yBAAW,EAAE,CAAC;QAE1B,MAAM,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC,CAAC;QACtE,MAAM,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC;QACxD,MAAM,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IAC5C,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,qCAAqC,EAAE,KAAK,IAAI,EAAE;QACjD,MAAM,GAAG,GAAG,QAAQ,CAAC;QACrB,MAAM,KAAK,GAAG,UAAU,CAAC;QAEzB,MAAM,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAClC,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,GAAG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AAE,yBAAyB,EAAE;SAC/D,CAAC,CAAC;QAEH,MAAM,QAAQ,GAAG,EAAE,CAAC;QACpB,IAAI,KAAK,EAAE,MAAM,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,YAAY,CAAC,EAAE,CAAC;YAClE,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC1B,CAAC;QACD,MAAM,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IACjC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,wBAAwB,EAAE,KAAK,IAAI,EAAE;QACpC,MAAM,KAAK,CAAC,KAAK,EAAE,CAAC;QAEpB,MAAM,OAAO,GAAG,EAAE,CAAC;QACnB,IAAI,KAAK,EAAE,MAAM,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC;YACzE,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACzB,CAAC;QAED,MAAM,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC;YACpB,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,yBAAyB,EAAE;YAC5D,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,yBAAyB,EAAE;YAC5D,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,yBAAyB,EAAE;SAC/D,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type HmacCryptoKey = CryptoKey;
|
|
2
|
+
type HmacKeyUsage = 'sign' | 'verify';
|
|
3
|
+
/**
|
|
4
|
+
* Import an HMAC-SHA256 key in order to use it with `signData` and `verifyData`.
|
|
5
|
+
*/
|
|
6
|
+
export declare const importHmacKey: (key: Uint8Array, keyUsage?: HmacKeyUsage[]) => Promise<HmacCryptoKey>;
|
|
7
|
+
/**
|
|
8
|
+
* Sign data using HMAC-SHA256
|
|
9
|
+
* @param key - WebCrypto secret key for signing
|
|
10
|
+
* @param data - data to sign
|
|
11
|
+
* @param additionalData - additional data to authenticate
|
|
12
|
+
*/
|
|
13
|
+
export declare const computeHmacSignature: (key: HmacCryptoKey, data: Uint8Array) => Promise<Uint8Array>;
|
|
14
|
+
/**
|
|
15
|
+
* Verify data using HMAC-SHA256
|
|
16
|
+
* @param key - WebCrypto secret key for verification
|
|
17
|
+
* @param signature - signature over data
|
|
18
|
+
* @param data - data to verify
|
|
19
|
+
* @param additionalData - additional data to authenticate
|
|
20
|
+
*/
|
|
21
|
+
export declare const verifyData: (key: HmacCryptoKey, signature: Uint8Array, data: Uint8Array) => Promise<boolean>;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.verifyData = exports.computeHmacSignature = exports.importHmacKey = void 0;
|
|
4
|
+
const HASH_ALGORITHM = 'SHA-256';
|
|
5
|
+
const KEY_LENGTH_BYTES = 32;
|
|
6
|
+
/**
|
|
7
|
+
* Import an HMAC-SHA256 key in order to use it with `signData` and `verifyData`.
|
|
8
|
+
*/
|
|
9
|
+
const importHmacKey = async (key, keyUsage = ['sign', 'verify']) => {
|
|
10
|
+
// From https://datatracker.ietf.org/doc/html/rfc2104:
|
|
11
|
+
// The key for HMAC can be of any length (keys longer than B bytes are first hashed using H).
|
|
12
|
+
// However, less than L bytes (L = 32 bytes for SHA-256) is strongly discouraged as it would
|
|
13
|
+
// decrease the security strength of the function. Keys longer than L bytes are acceptable
|
|
14
|
+
// but the extra length would not significantly increase the function strength.
|
|
15
|
+
// (A longer key may be advisable if the randomness of the key is considered weak.)
|
|
16
|
+
if (key.length < KEY_LENGTH_BYTES) {
|
|
17
|
+
throw new Error('Unexpected HMAC key size: key is too short');
|
|
18
|
+
}
|
|
19
|
+
return crypto.subtle.importKey('raw', key, { name: 'HMAC', hash: HASH_ALGORITHM }, false, keyUsage);
|
|
20
|
+
};
|
|
21
|
+
exports.importHmacKey = importHmacKey;
|
|
22
|
+
/**
|
|
23
|
+
* Sign data using HMAC-SHA256
|
|
24
|
+
* @param key - WebCrypto secret key for signing
|
|
25
|
+
* @param data - data to sign
|
|
26
|
+
* @param additionalData - additional data to authenticate
|
|
27
|
+
*/
|
|
28
|
+
const computeHmacSignature = async (key, data) => {
|
|
29
|
+
const signatureBuffer = await crypto.subtle.sign({ name: 'HMAC', hash: HASH_ALGORITHM }, key, data);
|
|
30
|
+
return new Uint8Array(signatureBuffer);
|
|
31
|
+
};
|
|
32
|
+
exports.computeHmacSignature = computeHmacSignature;
|
|
33
|
+
/**
|
|
34
|
+
* Verify data using HMAC-SHA256
|
|
35
|
+
* @param key - WebCrypto secret key for verification
|
|
36
|
+
* @param signature - signature over data
|
|
37
|
+
* @param data - data to verify
|
|
38
|
+
* @param additionalData - additional data to authenticate
|
|
39
|
+
*/
|
|
40
|
+
const verifyData = async (key, signature, data) => {
|
|
41
|
+
return crypto.subtle.verify({ name: 'HMAC', hash: HASH_ALGORITHM }, key, signature, data);
|
|
42
|
+
};
|
|
43
|
+
exports.verifyData = verifyData;
|
|
44
|
+
//# sourceMappingURL=hmac.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hmac.js","sourceRoot":"","sources":["../../src/crypto/hmac.ts"],"names":[],"mappings":";;;AAAA,MAAM,cAAc,GAAG,SAAS,CAAC;AACjC,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAK5B;;GAEG;AACI,MAAM,aAAa,GAAG,KAAK,EAC9B,GAAe,EACf,WAA2B,CAAC,MAAM,EAAE,QAAQ,CAAC,EACvB,EAAE;IACxB,sDAAsD;IACtD,6FAA6F;IAC7F,4FAA4F;IAC5F,2FAA2F;IAC3F,+EAA+E;IAC/E,mFAAmF;IACnF,IAAI,GAAG,CAAC,MAAM,GAAG,gBAAgB,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;IAClE,CAAC;IACD,OAAO,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,EAAE,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;AACxG,CAAC,CAAC;AAdW,QAAA,aAAa,iBAcxB;AAEF;;;;;GAKG;AACI,MAAM,oBAAoB,GAAG,KAAK,EAAE,GAAkB,EAAE,IAAgB,EAAE,EAAE;IAC/E,MAAM,eAAe,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,EAAE,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;IACpG,OAAO,IAAI,UAAU,CAAC,eAAe,CAAC,CAAC;AAC3C,CAAC,CAAC;AAHW,QAAA,oBAAoB,wBAG/B;AAEF;;;;;;GAMG;AACI,MAAM,UAAU,GAAG,KAAK,EAAE,GAAkB,EAAE,SAAqB,EAAE,IAAgB,EAAE,EAAE;IAC5F,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,EAAE,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;AAC9F,CAAC,CAAC;AAFW,QAAA,UAAU,cAErB"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// This file has copy-pasted utilities from CryptoProxy located in Proton web clients monorepo.
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.uint8ArrayToBase64String = uint8ArrayToBase64String;
|
|
5
|
+
exports.base64StringToUint8Array = base64StringToUint8Array;
|
|
6
|
+
function uint8ArrayToBase64String(array) {
|
|
7
|
+
return encodeBase64(arrayToBinaryString(array));
|
|
8
|
+
}
|
|
9
|
+
function base64StringToUint8Array(string) {
|
|
10
|
+
return binaryStringToArray(decodeBase64(string) || '');
|
|
11
|
+
}
|
|
12
|
+
const ifDefined = (cb) => (input) => {
|
|
13
|
+
return (input !== undefined ? cb(input) : undefined);
|
|
14
|
+
};
|
|
15
|
+
const encodeBase64 = ifDefined((input) => btoa(input).trim());
|
|
16
|
+
const decodeBase64 = ifDefined((input) => atob(input.trim()));
|
|
17
|
+
const arrayToBinaryString = (bytes) => {
|
|
18
|
+
const result = [];
|
|
19
|
+
const bs = 1 << 14;
|
|
20
|
+
const j = bytes.length;
|
|
21
|
+
for (let i = 0; i < j; i += bs) {
|
|
22
|
+
// @ts-expect-error Uint8Array treated as number[]
|
|
23
|
+
// eslint-disable-next-line prefer-spread
|
|
24
|
+
result.push(String.fromCharCode.apply(String, bytes.subarray(i, i + bs < j ? i + bs : j)));
|
|
25
|
+
}
|
|
26
|
+
return result.join('');
|
|
27
|
+
};
|
|
28
|
+
const binaryStringToArray = (str) => {
|
|
29
|
+
const result = new Uint8Array(str.length);
|
|
30
|
+
for (let i = 0; i < str.length; i++) {
|
|
31
|
+
result[i] = str.charCodeAt(i);
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/crypto/utils.ts"],"names":[],"mappings":";AAAA,+FAA+F;;AAE/F,4DAEC;AAED,4DAEC;AAND,SAAgB,wBAAwB,CAAC,KAAiB;IACtD,OAAO,YAAY,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC,CAAC;AACpD,CAAC;AAED,SAAgB,wBAAwB,CAAC,MAAc;IACnD,OAAO,mBAAmB,CAAC,YAAY,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;AAC3D,CAAC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AC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAClC,MAAM,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;IAClC,CAAC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base class for all SDK errors.
|
|
3
|
+
*
|
|
4
|
+
* This class can be used for catching all SDK errors. The error should have
|
|
5
|
+
* translated message in the `message` property that should be shown to the
|
|
6
|
+
* user without any modification.
|
|
7
|
+
*
|
|
8
|
+
* No retries should be done as that is already handled by the SDK.
|
|
9
|
+
*
|
|
10
|
+
* When SDK throws an error and it is not `ProtonDriveError`, it is unhandled error
|
|
11
|
+
* by the SDK and usually indicates bug in the SDK. Please, report it.
|
|
12
|
+
*/
|
|
13
|
+
export declare class ProtonDriveError extends Error {
|
|
14
|
+
name: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Error thrown when the operation is aborted.
|
|
18
|
+
*
|
|
19
|
+
* This error is thrown when the operation is aborted by the user.
|
|
20
|
+
* For example, by calling `abort()` on the `AbortSignal`.
|
|
21
|
+
*/
|
|
22
|
+
export declare class AbortError extends ProtonDriveError {
|
|
23
|
+
name: string;
|
|
24
|
+
constructor(message?: string);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Error thrown when the validation fails.
|
|
28
|
+
*
|
|
29
|
+
* This error is thrown when the validation of the input fails.
|
|
30
|
+
* Validation can be done on the client side or on the server side.
|
|
31
|
+
*
|
|
32
|
+
* For example, on the client, it can be thrown when the node name doesn't
|
|
33
|
+
* follow the required format, etc., while on the server side, it can be thrown
|
|
34
|
+
* when there is not enough permissions, etc.
|
|
35
|
+
*/
|
|
36
|
+
export declare class ValidationError extends ProtonDriveError {
|
|
37
|
+
name: string;
|
|
38
|
+
/**
|
|
39
|
+
* Internal API code.
|
|
40
|
+
*
|
|
41
|
+
* Use only for debugging purposes.
|
|
42
|
+
*/
|
|
43
|
+
readonly code?: number;
|
|
44
|
+
/**
|
|
45
|
+
* Additional details about the error provided by the server.
|
|
46
|
+
*/
|
|
47
|
+
readonly details?: object;
|
|
48
|
+
constructor(message: string, code?: number, details?: object);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Error thrown when the node already exists.
|
|
52
|
+
*
|
|
53
|
+
* This error is thrown when the node with the same name already exists in the
|
|
54
|
+
* parent folder. The client should ask the user to replace the existing node
|
|
55
|
+
* or choose another name. The available name is provided in the `availableName`
|
|
56
|
+
* property (that will contain original name with the index that can be used).
|
|
57
|
+
*/
|
|
58
|
+
export declare class NodeAlreadyExistsValidationError extends ValidationError {
|
|
59
|
+
name: string;
|
|
60
|
+
readonly existingNodeUid?: string;
|
|
61
|
+
constructor(message: string, code: number, existingNodeUid?: string);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Error thrown when the API call fails.
|
|
65
|
+
*
|
|
66
|
+
* This error covers both HTTP errors and API errors. SDK automatically
|
|
67
|
+
* retries the request before the error is thrown. The sepcific algorithm
|
|
68
|
+
* used for retries depends on the type of the error.
|
|
69
|
+
*
|
|
70
|
+
* Client should not retry the request when this error is thrown.
|
|
71
|
+
*/
|
|
72
|
+
export declare class ServerError extends ProtonDriveError {
|
|
73
|
+
name: string;
|
|
74
|
+
/**
|
|
75
|
+
* HTTP status code of the response.
|
|
76
|
+
*
|
|
77
|
+
* Use only for debugging purposes.
|
|
78
|
+
*/
|
|
79
|
+
readonly statusCode?: number;
|
|
80
|
+
/**
|
|
81
|
+
* Internal API code.
|
|
82
|
+
*
|
|
83
|
+
* Use only for debugging purposes.
|
|
84
|
+
*/
|
|
85
|
+
readonly code?: number;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Error thrown when the client makes too many requests to the API.
|
|
89
|
+
*
|
|
90
|
+
* SDK is configured to stay below the rate limits, but it can still happen if
|
|
91
|
+
* client is running multiple SDKs in parallel, or if the rate limits are
|
|
92
|
+
* changed on the server side.
|
|
93
|
+
*
|
|
94
|
+
* SDK automatically retries the request before the error is thrown after
|
|
95
|
+
* waiting for the required time specified by the server.
|
|
96
|
+
*
|
|
97
|
+
* Client should slow down calling SDK when this error is thrown.
|
|
98
|
+
*
|
|
99
|
+
* You can be also notified about the rate limits by the `requestsThrottled`
|
|
100
|
+
* event. See `onMessage` method on the SDK class for more details.
|
|
101
|
+
*/
|
|
102
|
+
export declare class RateLimitedError extends ServerError {
|
|
103
|
+
name: string;
|
|
104
|
+
code: number;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Error thrown when the client is not connected to the internet.
|
|
108
|
+
*
|
|
109
|
+
* Client should check the internet connection when this error is thrown.
|
|
110
|
+
*
|
|
111
|
+
* You can also be notified about the connection status by the `offline` event
|
|
112
|
+
* See `onMessage` method on the SDK class for more details.
|
|
113
|
+
*/
|
|
114
|
+
export declare class ConnectionError extends ProtonDriveError {
|
|
115
|
+
name: string;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Error thrown when the decryption fails.
|
|
119
|
+
*
|
|
120
|
+
* Client should report this error to the user and report bug report.
|
|
121
|
+
*
|
|
122
|
+
* In most cases, there is no decryption error. Every decryption error should
|
|
123
|
+
* be not exposed but set as empty value on the node, for example. But in the
|
|
124
|
+
* case of the file content, if block cannot be decrypted, decryption error
|
|
125
|
+
* is thrown.
|
|
126
|
+
*/
|
|
127
|
+
export declare class DecryptionError extends ProtonDriveError {
|
|
128
|
+
name: string;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Error thrown when the data integrity check fails.
|
|
132
|
+
*
|
|
133
|
+
* Client should report this error to the user and report bug report.
|
|
134
|
+
*
|
|
135
|
+
* For example, it can happen when hashes don't match, etc. In some cases,
|
|
136
|
+
* SDK allows to run command without verification checks for debug purposes.
|
|
137
|
+
*/
|
|
138
|
+
export declare class IntegrityError extends ProtonDriveError {
|
|
139
|
+
name: string;
|
|
140
|
+
readonly debug?: object;
|
|
141
|
+
constructor(message: string, debug?: object);
|
|
142
|
+
}
|