1
1
const Buffer = require ( 'buffer' ) . Buffer
2
2
3
+ // globalThis is the new standard global, but also support window and self (for worker contexts)
4
+ const global = typeof globalThis !== 'undefined' ? globalThis : ( typeof window !== 'undefined' ? window : self )
5
+
3
6
function noop ( ) { }
4
7
5
8
class Storage {
6
9
constructor ( chunkLength , opts = { } ) {
7
- if ( ! window || ! window . caches ) throw new Error ( 'Not supported on this platform' )
10
+ if ( ! global || ! global . caches ) throw new Error ( 'Not supported on this platform' )
8
11
9
12
if ( ! ( this instanceof Storage ) ) return new Storage ( chunkLength , opts )
10
13
@@ -20,7 +23,7 @@ class Storage {
20
23
this . lastChunkIndex = Math . ceil ( this . length / this . chunkLength ) - 1
21
24
}
22
25
23
- this . cachePromise = window . caches . open ( this . name )
26
+ this . cachePromise = global . caches . open ( this . name )
24
27
}
25
28
26
29
put ( index , buf , cb = noop ) {
@@ -37,9 +40,9 @@ class Storage {
37
40
38
41
// Even though new Response() can take buf directly, creating a Blob first
39
42
// is significantly faster in Chrome and Firefox
40
- const blob = new window . Blob ( [ buf ] )
43
+ const blob = new global . Blob ( [ buf ] )
41
44
42
- const response = new window . Response ( blob , {
45
+ const response = new global . Response ( blob , {
43
46
status : 200 ,
44
47
headers : {
45
48
'Content-Type' : 'application/octet-stream' ,
@@ -100,7 +103,7 @@ class Storage {
100
103
this . closed = true
101
104
this . cachePromise = null
102
105
103
- window . caches . delete ( this . name ) . then ( ( ) => {
106
+ global . caches . delete ( this . name ) . then ( ( ) => {
104
107
cb ( null )
105
108
} , cb )
106
109
}
0 commit comments