-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs.jsy
44 lines (37 loc) · 1.33 KB
/
fs.jsy
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
import {bkc_hexkey_api, as_hex_key, as_u8_key} from '../_utils.jsy'
export {as_hex_key, as_u8_key}
export default bkc_with_fs
export async function bkc_with_fs(opt={}) ::
let immutable = !! opt.immutable
let _enc_r = @{} encoding: null, flag: 'r'
let _enc_w = @{} encoding: null, flag: immutable ? 'wx' : 'w'
let {stat, readFile, writeFile} = await opt.fs
let base = opt.base || './var/'
let ext = opt.ext || '.bin'
return bkc_hexkey_api @:
bkc_opt: @{} immutable
hk_has: hex_key =>
new Promise @\ resolve, reject ::
stat @ `${base}${hex_key}${ext}`,
(err) => ::
if ! err ::
resolve(1)
else if 'ENOENT' === err.code ::
resolve(0)
else reject(err)
hk_get: hex_key =>
new Promise @\ resolve, reject ::
readFile @ `${base}${hex_key}${ext}`, _enc_r,
(err, buf) => ::
if ! err ::
resolve @ Uint8Array.from(buf)
else if 'ENOENT' === err.code ::
resolve()
else reject(err)
hk_set: @\ hex_key, u8_content =>
new Promise @\ resolve, reject ::
writeFile @ `${base}${hex_key}${ext}`,
u8_content, _enc_w,
err => !err ? resolve()
: immutable && 'EEXIST' === err.code ? resolve(false)
: resolve(err)