fix KV emulation ttl

This commit is contained in:
41666 2020-12-13 21:58:34 -05:00
parent 961cdab975
commit e25b9c96c6

View file

@ -26,16 +26,16 @@ class KVShim {
makeValue(value, expirationTtl) { makeValue(value, expirationTtl) {
if (!expirationTtl) { if (!expirationTtl) {
return { return JSON.stringify({
value, value,
expires: false, expires: false,
}; });
} }
return { return JSON.stringify({
value: JSON.stringify(value), value,
expires: Date.now() + 1000 * expirationTtl, expires: Date.now() + 1000 * expirationTtl,
}; });
} }
validate(value) { validate(value) {
@ -43,7 +43,7 @@ class KVShim {
return false; return false;
} }
if (value.expires < Date.now()) { if (value.expires && value.expires < Date.now()) {
return false; return false;
} }
@ -51,7 +51,7 @@ class KVShim {
} }
async get(key, type = 'text') { async get(key, type = 'text') {
const result = await this.level.get(key); const result = JSON.parse(await this.level.get(key));
if (!this.validate(result)) { if (!this.validate(result)) {
return null; return null;
@ -75,7 +75,7 @@ class KVShim {
hasWarned = true; hasWarned = true;
} }
return this.level.put(key, this.makeValue(value, expirationTtl)); return await this.level.put(key, this.makeValue(value, expirationTtl));
} }
async delete(key) { async delete(key) {