-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorage.ts
201 lines (162 loc) · 6.29 KB
/
Storage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { User } from "./User";
import { RemoteService, isResponse } from "./RemoteService";
import { KeeError } from "./KeeError";
let remoteService: RemoteService;
export enum StorageType {
KEE_S3 = "kee-s3"
}
export type URLlist = {ul: string, dl: string, st: string};
export class StorageItem {
public emailHashed: string; // actually userId now but backend still calls it emailHashed
public schemaVersion: number;
public id: string;
public location: string;
public type: StorageType;
public urls: URLlist;
public name: string;
public constructor (init?: Partial<StorageItem>) {
Object.assign(this, init);
}
static fromUserId (userId: string) {
const si = new StorageItem({ emailHashed: userId, schemaVersion: 1, type: StorageType.KEE_S3 });
return si;
}
static fromUserIdAndId (userId: string, id: string) {
const si = new StorageItem({ emailHashed: userId, schemaVersion: 1, type: StorageType.KEE_S3, id });
return si;
}
}
export class StorageManager {
public static init (stage: "dev"|"beta"|"prod") {
remoteService = new RemoteService(stage, "storage");
}
// any client utilising this library should perform a sanity check that ensures we're not called if
// we have no storage token but if not for any reason, the underlying request will be made without
// the necessary authentication token and a max-retry (3ish) algorithm will kick in, returning an
// authorisation error to the calling client
public static async list (user: User) {
if (!remoteService) {
return KeeError.InvalidState;
}
if (!user) {
return KeeError.InvalidRequest;
}
const storageToken = user.tokens ? user.tokens.storage : undefined;
try {
const response = await remoteService.getRequest("meta/", storageToken, () => user.refresh());
if (isResponse(response)) {
if (response.status !== 200) {
console.error("Unexpected status code");
return KeeError.Unexpected;
}
return (response.body as any[]).map(s => new StorageItem({
id: s.id,
name: s.name,
location: s.location,
type: s.type,
emailHashed: s.emailHashed,
schemaVersion: s.schemaVersion,
urls: s.urls }));
}
// We can't handle any other type of error
return response;
} catch (e) {
console.error(e);
return KeeError.Unexpected;
}
}
public static async create (user: User, name: string, emptyVault: string) {
if (!remoteService) {
return KeeError.InvalidState;
}
if (!user) {
return KeeError.InvalidRequest;
}
const storageToken = user.tokens ? user.tokens.storage : undefined;
try {
const si = StorageItem.fromUserId(user.userId);
si.name = name;
const response = await remoteService.postRequest("meta/", { si, emptyVault }, storageToken, () => user.refresh());
if (isResponse(response)) {
if (response.status !== 200) {
console.error("Unexpected status code");
return KeeError.Unexpected;
}
return response.body as StorageItem;
}
if (response === KeeError.ServerConflict) {
console.error("Tried to create non-primary DB when no primary DB exists");
return KeeError.MissingPrimaryDB;
}
if (response === KeeError.InvalidRequest) {
console.error("Mismatched email hash");
return response;
}
// We can't handle any other type of error
return response;
} catch (e) {
console.error(e);
return KeeError.Unexpected;
}
}
public static async update (user: User, si: StorageItem) {
if (!remoteService) {
return KeeError.InvalidState;
}
if (!user) {
return KeeError.InvalidRequest;
}
const storageToken = user.tokens ? user.tokens.storage : undefined;
try {
const response = await remoteService.postRequest("meta/" + si.id, si, storageToken, () => user.refresh());
if (isResponse(response)) {
if (response.status !== 200) {
console.error("Unexpected status code");
return KeeError.Unexpected;
}
return true;
}
if (response === KeeError.InvalidRequest) {
console.error("Mismatched email hash");
return response;
}
if (response === KeeError.NotFound) {
console.error("DB no longer exists. Perhaps the user ID has changed?");
return response;
}
// We can't handle any other type of error
return response;
} catch (e) {
console.error(e);
return KeeError.Unexpected;
}
}
public static async refreshItemLinks (user: User, id: string) {
if (!remoteService) {
return KeeError.InvalidState;
}
if (!user) {
return KeeError.InvalidRequest;
}
const storageToken = user.tokens ? user.tokens.storage : undefined;
try {
const response = await remoteService.getRequest("itemLinks/" + id, storageToken, () => user.refresh());
if (isResponse(response)) {
if (response.status !== 200) {
console.error("Unexpected status code");
return KeeError.Unexpected;
}
return response.body as URLlist;
}
if (response === KeeError.NotFound) {
console.error("DB no longer exists. Perhaps the user ID has changed?");
return response;
}
// We can't handle any other type of error
return response;
} catch (e) {
console.error(e);
return KeeError.Unexpected;
}
}
}