Skip to content

Commit

Permalink
Enable OPFS for Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
kevodwyer committed Jul 25, 2023
1 parent 690f150 commit 7b310e1
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions vendor/priors/gwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,22 +463,15 @@ async function* getFileStatsRecursively(entry) { //https://developer.mozilla.org
}
}
/*
as of March 2023 OPFS works in Chrome and Firefox desktop
Safari doesn't implement FileSystemFileHandle.createWritable() - https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle
mobile browsers - not confirmed to work and difficult to debug.
*/
function isOPFSAvailable() {
let future = peergos.shared.util.Futures.incomplete();
try {
let isSafari =
/constructor/i.test(window.HTMLElement) ||
(function (p) {
return p.toString() === "[object SafariRemoteNotification]";
})(!window["safari"] || safari.pushNotification);
let isMobile = /Mobi|Android/i.test(navigator.userAgent); // https://stackoverflow.com/a/24600597
let isLinuxOnFirefox = navigator.userAgent.search('Linux')!==-1 && navigator.userAgent.search('X11')!==-1
&& navigator.userAgent.toLowerCase().indexOf("firefox") > -1;
if (!isSafari && !isMobile && !isLinuxOnFirefox) {
if (!isMobile && !isLinuxOnFirefox) {
navigator.storage.getDirectory().then(root => {
if (rootDirectory == null) {
rootDirectory = root;
Expand All @@ -498,7 +491,6 @@ function isOPFSAvailable() {
return future;
}
//Firefox private mode does not support IndexedDB. https://bugzilla.mozilla.org/show_bug.cgi?id=781982
//Safari does not support StorageManager.estimate(). https://developer.mozilla.org/en-US/docs/Web/API/StorageManager/estimate
function isIndexedDBAvailable() {
let future = peergos.shared.util.Futures.incomplete();
if (navigator.userAgent.toLowerCase().indexOf("firefox") > -1){
Expand Down Expand Up @@ -526,6 +518,7 @@ var batStoreCache;
var accountStoreCache;
var pkiStoreCache;
var rootKeyCache;
let SAFARI_CACHE_SIZE = 1024 * 1024 * 200;

function bindCacheStore(storeCache) {
blockStoreCache = storeCache;
Expand All @@ -541,13 +534,17 @@ function getCurrentDesiredCacheSize() {
}
function getDesiredCacheSize() {
let future = peergos.shared.util.Futures.incomplete();
getIDBKV("desiredSize", blockStoreCache.cacheDesiredSizeStore).then((val) => {
if (val == null) {
future.complete(-1);
} else {
future.complete(val);
}
});
if (isSafariTest()) {
future.complete(SAFARI_CACHE_SIZE);
} else {
getIDBKV("desiredSize", blockStoreCache.cacheDesiredSizeStore).then((val) => {
if (val == null) {
future.complete(-1);
} else {
future.complete(val);
}
});
}
return future;
}
function setDesiredCacheSize(desiredSize) {
Expand Down Expand Up @@ -610,12 +607,20 @@ function getBrowserStorageUsage() {
return prom;
}
}

function isSafariTest() {
let test =
/constructor/i.test(window.HTMLElement) ||
(function (p) {
return p.toString() === "[object SafariRemoteNotification]";
})(!window["safari"] || safari.pushNotification);
return test;
}
function getBrowserStorageQuota() {
if (navigator.storage && navigator.storage.estimate) {
return navigator.storage.estimate().then(quota => quota.quota);
} else {
let prom = new Promise(function(resolve, reject) { resolve(0)});

let prom = new Promise(function(resolve, reject) { resolve(isSafariTest() ? SAFARI_CACHE_SIZE : 0)});
return prom;
}
}
Expand Down

0 comments on commit 7b310e1

Please sign in to comment.