summaryrefslogtreecommitdiff
path: root/src/database.ts
diff options
context:
space:
mode:
authorNick Van Doorn <vandoorn.nick@gmail.com>2019-01-19 20:57:35 -0800
committerNick Van Doorn <vandoorn.nick@gmail.com>2019-01-19 20:57:35 -0800
commitba9af27c334ccca539aa243a081c744fdb042bc0 (patch)
tree5a234eb380ee9d36572a91d7235cc7f24a47adcf /src/database.ts
parentf7a650cd6e6e135c16a2940d7d449cfd18a0cccf (diff)
Throw error when out of space
Diffstat (limited to 'src/database.ts')
-rw-r--r--src/database.ts17
1 files changed, 16 insertions, 1 deletions
diff --git a/src/database.ts b/src/database.ts
index 71ba7fc..4e263cd 100644
--- a/src/database.ts
+++ b/src/database.ts
@@ -3,6 +3,7 @@ import { promisify } from "util";
import { DatabaseInterface } from "./database.model";
import { Context } from "./context.model";
+import { NaiveError, NaiveErrorCode as e } from "./error.model";
import { last } from "./util";
@@ -13,7 +14,8 @@ const read = promisify(readFile);
export const DEFAULT_CTX = {
logger: console.log,
- cachePath: `${__dirname}/db.json`
+ cachePath: `${__dirname}/db.json`,
+ maxDbSizeMB: 6
};
/**
@@ -108,8 +110,21 @@ export class Database implements DatabaseInterface {
}
/**
+ * Serialize the current buffer
+ * into a plain file.
+ *
+ * Change the path by injecting custom
+ * Context
+ *
+ * Throws OUT_OF_SPACE
*/
private serialize(): Promise<void> {
+ if (!this.hasSpace()) throw new NaiveError(e.OUT_OF_SPACE);
return write(this.ctx.cachePath, this.toString());
}
+
+ private hasSpace(): boolean {
+ // convert from MB to B
+ return this.toString().length <= this.ctx.maxDbSizeMB * 1024 ** 2;
+ }
}