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
|
const BYTE_UNITS = ["B", "KB", "MB", "GB", "TB"] as const;
export const STORAGE_HEADROOM_RESERVE_BYTES = 50 * 1024 * 1024;
export interface StorageQuotaStatus {
quotaBytes: number | null;
usageBytes: number | null;
headroomBytes: number | null;
availableBytes: number | null;
}
export interface StorageCapacityCheckResult {
canStore: boolean;
reason: "enough-space" | "insufficient-space" | "estimate-unavailable";
availableBytes: number | null;
}
function normalizeByteValue({ value }: { value: unknown }): number | null {
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
return null;
}
return value;
}
export function formatStorageBytes({ bytes }: { bytes: number }): string {
if (!Number.isFinite(bytes) || bytes <= 0) {
return "0 B";
}
let value = bytes;
let unitIndex = 0;
while (value >= 1024 && unitIndex < BYTE_UNITS.length - 1) {
value /= 1024;
unitIndex += 1;
}
const precision = value >= 10 || unitIndex === 0 ? 0 : 1;
return `${value.toFixed(precision)} ${BYTE_UNITS[unitIndex]}`;
}
export async function readStorageQuotaStatus(): Promise<StorageQuotaStatus> {
if (
typeof navigator === "undefined" ||
!navigator.storage ||
typeof navigator.storage.estimate !== "function"
) {
return {
quotaBytes: null,
usageBytes: null,
headroomBytes: null,
availableBytes: null,
};
}
const estimate = await navigator.storage.estimate();
const quotaBytes = normalizeByteValue({ value: estimate.quota });
const usageBytes = normalizeByteValue({ value: estimate.usage });
if (quotaBytes === null || usageBytes === null) {
return {
quotaBytes,
usageBytes,
headroomBytes: null,
availableBytes: null,
};
}
const headroomBytes = Math.max(quotaBytes - usageBytes, 0);
const availableBytes = Math.max(
headroomBytes - STORAGE_HEADROOM_RESERVE_BYTES,
0,
);
return {
quotaBytes,
usageBytes,
headroomBytes,
availableBytes,
};
}
export function evaluateStorageCapacity({
requiredBytes,
quotaStatus,
}: {
requiredBytes: number;
quotaStatus: StorageQuotaStatus;
}): StorageCapacityCheckResult {
if (quotaStatus.availableBytes === null) {
return {
canStore: true,
reason: "estimate-unavailable",
availableBytes: null,
};
}
if (requiredBytes > quotaStatus.availableBytes) {
return {
canStore: false,
reason: "insufficient-space",
availableBytes: quotaStatus.availableBytes,
};
}
return {
canStore: true,
reason: "enough-space",
availableBytes: quotaStatus.availableBytes,
};
}
export class StorageQuotaExceededError extends Error {
requiredBytes: number;
constructor({ requiredBytes }: { requiredBytes: number }) {
super(
`Not enough browser storage to save a ${formatStorageBytes({ bytes: requiredBytes })} file.`,
);
this.name = "StorageQuotaExceededError";
this.requiredBytes = requiredBytes;
}
}
export function isStorageQuotaExceededError({
error,
}: {
error: unknown;
}): boolean {
if (error instanceof StorageQuotaExceededError) {
return true;
}
if (!(error instanceof Error)) {
return false;
}
return (
error.name === "QuotaExceededError" ||
error.name === "NS_ERROR_DOM_QUOTA_REACHED" ||
error.message.toLowerCase().includes("quota")
);
}
|