From 9d5733c0deb7b8befc42a0b7c2599001554e83e1 Mon Sep 17 00:00:00 2001 From: WenheLI Date: Mon, 6 Jan 2020 18:04:45 +0800 Subject: [PATCH] fix save error --- tfjs-core/src/io/indexed_db.ts | 2 +- tfjs-core/src/io/local_storage.ts | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tfjs-core/src/io/indexed_db.ts b/tfjs-core/src/io/indexed_db.ts index 2ce5a8ce0f8..35fada1ffcc 100644 --- a/tfjs-core/src/io/indexed_db.ts +++ b/tfjs-core/src/io/indexed_db.ts @@ -57,7 +57,7 @@ function getIndexedDBFactory(): IDBFactory { 'is not a web browser.'); } // tslint:disable-next-line:no-any - const theWindow: any = window; + const theWindow: any = window || self; const factory = theWindow.indexedDB || theWindow.mozIndexedDB || theWindow.webkitIndexedDB || theWindow.msIndexedDB || theWindow.shimIndexedDB; diff --git a/tfjs-core/src/io/local_storage.ts b/tfjs-core/src/io/local_storage.ts index 50adc721444..beab8139026 100644 --- a/tfjs-core/src/io/local_storage.ts +++ b/tfjs-core/src/io/local_storage.ts @@ -38,6 +38,7 @@ const MODEL_METADATA_SUFFIX = 'model_metadata'; */ export function purgeLocalStorageArtifacts(): string[] { if (!env().getBool('IS_BROWSER') || + typeof window === 'undefined' || typeof window.localStorage === 'undefined') { throw new Error( 'purgeLocalStorageModels() cannot proceed because local storage is ' + @@ -119,7 +120,8 @@ export class BrowserLocalStorage implements IOHandler { constructor(modelPath: string) { if (!env().getBool('IS_BROWSER') || - typeof window.localStorage === 'undefined') { + typeof window === 'undefined' || + typeof window.localStorage === 'undefined') { // TODO(cais): Add more info about what IOHandler subtypes are // available. // Maybe point to a doc page on the web and/or automatically determine @@ -307,6 +309,7 @@ export class BrowserLocalStorageManager implements ModelStoreManager { env().getBool('IS_BROWSER'), () => 'Current environment is not a web browser'); assert( + typeof window === 'undefined' || typeof window.localStorage !== 'undefined', () => 'Current browser does not appear to support localStorage'); this.LS = window.localStorage;