Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private async hasEnoughQuota(): Promise {
if ('storage' in navigator && 'estimate' in navigator.storage) {
const {quota, usage} = await navigator.storage.estimate();
if (typeof quota === 'number' && typeof usage === 'number') {
const diskIsFull = usage >= quota;
if (diskIsFull) {
const errorMessage = `Out of disk space. Using "${usage}" out of "${quota}" bytes.`;
return Promise.reject(new StoreEngineError.LowDiskSpaceError(errorMessage));
}
}
}
return Promise.resolve();
}
private mapDatabaseError(error: Dexie.DexieError, tableName: string, primaryKey: string): Error {
const isAlreadyExisting = error instanceof Dexie.ConstraintError;
/** @see https://github.com/dfahlander/Dexie.js/issues/776 */
const hasNotEnoughDiskSpace =
error.name === Dexie.errnames.QuotaExceeded || (error.inner && error.inner.name === Dexie.errnames.QuotaExceeded);
if (isAlreadyExisting) {
const message = `Record "${primaryKey}" already exists in "${tableName}". You need to delete the record first if you want to overwrite it.`;
return new StoreEngineError.RecordAlreadyExistsError(message);
} else if (hasNotEnoughDiskSpace) {
const message = `Cannot save "${primaryKey}" in "${tableName}" because there is low disk space.`;
return new StoreEngineError.LowDiskSpaceError(message);
} else {
return error;
}
}
}
private async hasEnoughQuota(): Promise {
if ('storage' in navigator && 'estimate' in navigator.storage) {
const {quota, usage} = await navigator.storage.estimate();
if (typeof quota === 'number' && typeof usage === 'number') {
const diskIsFull = usage >= quota;
if (diskIsFull) {
const errorMessage = `Out of disk space. Using "${usage}" out of "${quota}" bytes.`;
throw new StoreEngineError.LowDiskSpaceError(errorMessage);
}
}
}
}
private mapDatabaseError(
error: Dexie.DexieError,
tableName: string,
primaryKey: PrimaryKey,
): Error {
const isAlreadyExisting = error instanceof Dexie.ConstraintError;
/** @see https://github.com/dfahlander/Dexie.js/issues/776 */
const hasNotEnoughDiskSpace =
error.name === Dexie.errnames.QuotaExceeded || error.inner?.name === Dexie.errnames.QuotaExceeded;
if (isAlreadyExisting) {
const message = `Record "${primaryKey}" already exists in "${tableName}". You need to delete the record first if you want to overwrite it.`;
return new StoreEngineError.RecordAlreadyExistsError(message);
} else if (hasNotEnoughDiskSpace) {
const message = `Cannot save "${primaryKey}" in "${tableName}" because there is low disk space.`;
return new StoreEngineError.LowDiskSpaceError(message);
} else {
return error;
}
}