Skip to content

Commit df677ce

Browse files
committed
Use globalThis, window, or self as the global
1 parent 74d408e commit df677ce

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

index.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
const Buffer = require('buffer').Buffer
22

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+
36
function noop () {}
47

58
class Storage {
69
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')
811

912
if (!(this instanceof Storage)) return new Storage(chunkLength, opts)
1013

@@ -20,7 +23,7 @@ class Storage {
2023
this.lastChunkIndex = Math.ceil(this.length / this.chunkLength) - 1
2124
}
2225

23-
this.cachePromise = window.caches.open(this.name)
26+
this.cachePromise = global.caches.open(this.name)
2427
}
2528

2629
put (index, buf, cb = noop) {
@@ -37,9 +40,9 @@ class Storage {
3740

3841
// Even though new Response() can take buf directly, creating a Blob first
3942
// is significantly faster in Chrome and Firefox
40-
const blob = new window.Blob([buf])
43+
const blob = new global.Blob([buf])
4144

42-
const response = new window.Response(blob, {
45+
const response = new global.Response(blob, {
4346
status: 200,
4447
headers: {
4548
'Content-Type': 'application/octet-stream',
@@ -100,7 +103,7 @@ class Storage {
100103
this.closed = true
101104
this.cachePromise = null
102105

103-
window.caches.delete(this.name).then(() => {
106+
global.caches.delete(this.name).then(() => {
104107
cb(null)
105108
}, cb)
106109
}

0 commit comments

Comments
 (0)