Skip to content
Source
Sign inSign up

limits.ts

42 lines1.2 KB

1export const MiB = 1024 * 1024;
2export const PART_SIZE = 5 * MiB;
3export const LEASE_SECONDS = 15 * 60;
4export const GRACE_SECONDS = 10 * 60;
5​
6export const defaults = {
7 key: 16 * 1024,
8 value: 4 * MiB,
9 metadata: 5 * MiB,
10 fetch: 40 * 1024,
11 blob: 64 * MiB,
12 store: 72 * MiB,
13 headers: 8 * 1024,
14 deadlineMs: 120_000,
15};
16export type Limits = typeof defaults;
17​
18export function limitsFrom(raw: string): Limits {
19 const input: unknown = JSON.parse(raw);
20 if (!input || typeof input !== 'object' || Array.isArray(input))
21 throw new Error('Invalid limits');
22 const result = { ...defaults };
23 for (const [name, value] of Object.entries(input)) {
24 if (
25 !(name in defaults) ||
26 !Number.isSafeInteger(value) ||
27 value <= 0 ||
28 value > defaults[name as keyof Limits]
29 ) {
30 throw new Error('Limits must be positive integers no larger than the tested defaults');
31 }
32 result[name as keyof Limits] = value;
33 }
34 if (
35 result.metadata < 2 * result.key + result.value + 128 ||
36 result.fetch < 2 * result.key + 128 ||
37 result.store < result.metadata + result.blob + 1024
38 ) {
39 throw new Error('Envelope limits must accommodate field limits and framing');
40 }
41 return result;