-
Notifications
You must be signed in to change notification settings - Fork 18
/
storage.js
54 lines (52 loc) · 1.88 KB
/
storage.js
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
const { coalesce } = require('extant')
const assert = require('assert')
class Storage {
static options (options) {
if (options.checksum == null) {
options.checksum = (() => '0')
}
assert(options.extractor != null)
options.serializer = function () {
const serializer = coalesce(options.serializer, 'json')
switch (serializer) {
case 'json':
return {
parts: {
serialize: function (parts) {
return parts.map(part => Buffer.from(JSON.stringify(part)))
},
deserialize: function (parts) {
return parts.map(part => JSON.parse(part.toString()))
}
},
key: {
serialize: function (key) {
if (key == null) {
throw new Error
}
return [ Buffer.from(JSON.stringify(key)) ]
},
deserialize: function (parts) {
return JSON.parse(parts[0].toString())
}
}
}
case 'buffer':
return {
parts: {
serialize: function (parts) { return parts },
deserialize: function (parts) { return parts }
},
key: {
serialize: function (part) { return [ part ] },
deserialize: function (parts) { return parts[0] }
}
}
default:
return serializer
}
} ()
return options
}
}
module.exports = Storage