-
Notifications
You must be signed in to change notification settings - Fork 1
/
expiration.ts
275 lines (262 loc) · 7.59 KB
/
expiration.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { Scheduler } from 'convex/server';
import { Doc } from './convex/_generated/dataModel';
import { DatabaseReader } from './convex/_generated/server';
import { api } from './convex/_generated/api';
import { ConvexError } from 'convex/values';
export const expirationOptions = [
'after one access',
// 'after 20 seconds', // for testing
'after ten accesses',
'after five minutes',
'after one week',
'never',
];
export const directExpirationOption = 'manually expired';
export const failedAccesses = 'too many failed accesses';
export const maxAccessFailures = 4;
type Expiration = {
never?: boolean;
manual?: boolean;
afterDuration?: number;
afterAccessCount?: number;
afterFailedAccesses?: boolean;
};
export function optionToExpiration(option: string): Expiration {
switch (option) {
case 'after one access':
return { afterAccessCount: 1 };
case 'after ten accesses':
return { afterAccessCount: 10 };
case 'after 20 seconds':
return { afterDuration: 20 * 1000 };
case 'after five minutes':
return { afterDuration: 5 * 60 * 1000 };
case 'after one week':
return { afterDuration: 7 * 24 * 60 * 60 * 1000 };
case 'never':
return { never: true };
case directExpirationOption:
return { manual: true };
case failedAccesses:
return { afterFailedAccesses: true };
default:
throw Error(`unrecognized expiration '${option}'`);
}
}
export async function countAccesses(
db: DatabaseReader,
name: string
): Promise<number> {
const allAccesses = await db
.query('accesses')
.withIndex('by_name_and_key', (q) => q.eq('name', name))
.collect();
return allAccesses.length;
}
export async function latestAccessTime(
db: DatabaseReader,
name: string
): Promise<number> {
const allAccesses = await db
.query('accesses')
.withIndex('by_name_and_key', (q) => q.eq('name', name))
.collect();
const largestCreationTime = Math.max(
...allAccesses.map((access) => {
return access._creationTime;
})
);
return largestCreationTime;
}
export async function countInvalidAccesses(
db: DatabaseReader,
name: string
): Promise<number> {
const allAccesses = await db
.query('accessFailures')
.withIndex('by_name_and_key', (q) => q.eq('name', name))
.collect();
return allAccesses.length;
}
export async function getValidWhisper(
db: DatabaseReader,
name: string,
newAccess: boolean
): Promise<Doc<'whispers'>> {
const whisperDoc = await db
.query('whispers')
.withIndex('by_name', (q) => q.eq('name', name))
.unique();
const creation = whisperDoc!._creationTime;
const expiration = optionToExpiration(whisperDoc!.expiration);
if (expiration.never) {
return whisperDoc!;
} else if (expiration.manual) {
throw new ConvexError(`manually expired`);
} else if (expiration.afterAccessCount) {
if (
newAccess &&
(await countAccesses(db, name)) >= expiration.afterAccessCount
) {
throw new ConvexError(
`already accessed ${expiration.afterAccessCount} time${
expiration.afterAccessCount === 1 ? '' : 's'
}`
);
}
} else if (expiration.afterDuration) {
const after = creation + expiration.afterDuration;
if (after < new Date().getTime()) {
throw new ConvexError(
`expired after ${printDuration(expiration.afterDuration)}`
);
}
} else if (expiration.afterFailedAccesses) {
throw new ConvexError(`expired after ${maxAccessFailures} failed accesses`);
} else {
throw Error('developer error');
}
return whisperDoc!;
}
function splitBase(
n: number,
biggest: string,
bases: [number, string][]
): [number, string][] {
let inBase: [number, string][] = [];
for (let i = bases.length - 1; i >= 0; i--) {
const base = bases[i][0];
const baseName = bases[i][1];
const count = Math.floor(n / base);
const remainder = n % base;
inBase = [[remainder, baseName], ...inBase];
n = count;
}
inBase = [[n, biggest], ...inBase];
return inBase;
}
function printDuration(duration: number): string {
let inBase = splitBase(duration, 'day', [
[24, 'hour'],
[60, 'minute'],
[60, 'second'],
[1000, 'millisecond'],
]);
let asStrings = inBase
.map(([count, name]) => {
if (count === 0 || name === 'millisecond') {
return '';
}
if (count === 1) {
return `1 ${name}`;
}
return `${count} ${name}s`;
})
.filter((s) => s.length > 0);
if (asStrings.length === 0) {
return '0 seconds';
}
return asStrings.join(', ');
}
export async function whenShouldDelete(
db: DatabaseReader,
name: string
): Promise<Date | null> {
const whisperDoc = await db
.query('whispers')
.withIndex('by_name', (q) => q.eq('name', name))
.unique();
const creation = whisperDoc!._creationTime;
const currentTime = new Date().getTime();
const expiration = optionToExpiration(whisperDoc!.expiration);
if (expiration.never) {
return null;
} else if (expiration.manual || expiration.afterFailedAccesses) {
return new Date();
} else if (expiration.afterAccessCount) {
const accesses = await countAccesses(db, name);
if (accesses >= expiration.afterAccessCount) {
// In case the new access caused the secret to expire, give everyone with
// access keys a day to readSecret, then delete it.
const lastAccessTime = await latestAccessTime(db, name);
return new Date(lastAccessTime + 24 * 60 * 60 * 1000);
} else {
return null;
}
} else if (expiration.afterDuration) {
const after = creation + expiration.afterDuration;
if (after < currentTime) {
return new Date();
} else {
return new Date(after);
}
}
throw Error('developer error');
}
export async function readExpiration(
db: DatabaseReader,
name: string
): Promise<[string, number | null, boolean]> {
const whisperDoc = await db
.query('whispers')
.withIndex('by_name', (q) => q.eq('name', name))
.unique();
const creation = whisperDoc!._creationTime;
const expiration = optionToExpiration(whisperDoc!.expiration);
if (expiration.never) {
return ['will never expire', null, false];
} else if (expiration.manual) {
return ['manually expired', null, true];
} else if (expiration.afterFailedAccesses) {
return ['too many failed accesses', null, true];
} else if (expiration.afterAccessCount) {
const accesses = await countAccesses(db, name);
if (accesses >= expiration.afterAccessCount) {
return [
`expired after ${accesses} access${accesses === 1 ? '' : 'es'}`,
null,
true,
];
} else {
const remaining = expiration.afterAccessCount - accesses;
return [
`will expire after ${remaining} more access${
remaining === 1 ? '' : 'es'
}`,
null,
false,
];
}
} else if (expiration.afterDuration) {
const after = creation + expiration.afterDuration;
const currentTime = new Date().getTime();
if (after < currentTime) {
return [
`expired ${printDuration(currentTime - after)} ago`,
currentTime + 1000,
true,
];
} else {
return [
`will expire in ${printDuration(after - currentTime)}`,
currentTime + 1000,
false,
];
}
}
throw Error('developer error');
}
export async function scheduleDeletion(
scheduler: Scheduler,
db: DatabaseReader,
whisperName: string,
creatorKey: string
) {
const expireTime = await whenShouldDelete(db, whisperName);
if (expireTime) {
await scheduler.runAt(expireTime, api.deleteExpired.default, {
whisperName,
creatorKey,
});
}
}