forked from mathiasvr/cache-storage-chunk-store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (88 loc) · 3.26 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const Buffer = require('buffer').Buffer
// globalThis is the new standard global, but also support window and self (for worker contexts)
const global = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== 'undefined' ? window : self)
function noop () {}
class Storage {
constructor (chunkLength, opts = {}) {
if (!global || !global.caches) throw new Error('Not supported on this platform')
if (!(this instanceof Storage)) return new Storage(chunkLength, opts)
this.chunkLength = Number(chunkLength)
if (!this.chunkLength) { throw new Error('First argument must be a chunk length') }
this.closed = false
this.length = Number(opts.length) || Infinity
this.name = opts.name || 'CacheStorageChunkStore'
if (this.length !== Infinity) {
this.lastChunkLength = this.length % this.chunkLength || this.chunkLength
this.lastChunkIndex = Math.ceil(this.length / this.chunkLength) - 1
}
this.cachePromise = global.caches.open(this.name)
}
put (index, buf, cb = noop) {
if (this.closed) return nextTick(cb, new Error('Storage is closed'))
const isLastChunk = index === this.lastChunkIndex
if (isLastChunk && buf.length !== this.lastChunkLength) {
return nextTick(cb, new Error('Last chunk length must be ' + this.lastChunkLength))
}
if (!isLastChunk && buf.length !== this.chunkLength) {
return nextTick(cb, new Error('Chunk length must be ' + this.chunkLength))
}
// Even though new Response() can take buf directly, creating a Blob first
// is significantly faster in Chrome and Firefox
const blob = new global.Blob([buf])
const response = new global.Response(blob, {
status: 200,
headers: {
'Content-Type': 'application/octet-stream',
'Content-Length': buf.length
}
})
this.cachePromise.then((cache) => {
cache
.put('/index/' + index, response)
.then(() => cb(null), cb)
}, cb)
}
get (index, opts, cb = noop) {
if (typeof opts === 'function') {
cb = opts
opts = null
}
if (this.closed) return nextTick(cb, new Error('Storage is closed'))
this.cachePromise.then((cache) => {
cache.match('/index/' + index).then((response) => {
if (!response) {
const err = new Error('Chunk not found')
err.notFound = true
return cb(err)
}
response.arrayBuffer().then((arrayBuffer) => {
const buf = Buffer.from(arrayBuffer)
if (!opts) return cb(null, buf)
const offset = opts.offset || 0
const len = opts.length || (buf.length - offset)
if (offset === 0 && len === buf.length) {
return cb(null, buf)
}
return cb(null, buf.slice(offset, len + offset))
}, cb)
}, cb)
}, cb)
}
close (cb = noop) {
if (this.closed) return nextTick(cb, new Error('Storage is closed'))
this.closed = true
nextTick(cb, null)
}
destroy (cb = noop) {
if (this.closed) return nextTick(cb, new Error('Storage is closed'))
this.closed = true
this.cachePromise = null
global.caches.delete(this.name).then(() => {
cb(null)
}, cb)
}
}
function nextTick (cb, err, val) {
queueMicrotask(() => cb(err, val))
}
module.exports = Storage