From 5fcd313e2f0d640de472a7689261a7a68fac0dea Mon Sep 17 00:00:00 2001 From: David Humphrey Date: Sat, 17 May 2014 15:18:32 -0400 Subject: [PATCH] Fix issue #56: Support Filer in node.js as an fs alternative --- lib/intercom.js | 17 ++++++++++++----- src/providers/indexeddb.js | 10 ++++++---- src/providers/websql.js | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/intercom.js b/lib/intercom.js index 6c3bae8..dfe52e0 100644 --- a/lib/intercom.js +++ b/lib/intercom.js @@ -31,7 +31,8 @@ define(function(require) { } var localStorage = (function(window) { - if (typeof window.localStorage === 'undefined') { + if (typeof window === 'undefined' || + typeof window.localStorage === 'undefined') { return { getItem : function() {}, setItem : function() {}, @@ -53,6 +54,12 @@ define(function(require) { var storageHandler = function() { self._onStorageEvent.apply(self, arguments); }; + + // If we're in node.js, skip event registration + if (typeof window === 'undefined' || typeof document === 'undefined') { + return; + } + if (document.attachEvent) { document.attachEvent('onstorage', storageHandler); } else { @@ -80,7 +87,7 @@ define(function(require) { self._on('storage', lock); listening = true; } - waitTimer = window.setTimeout(lock, WAIT); + waitTimer = setTimeout(lock, WAIT); return; } executed = true; @@ -95,7 +102,7 @@ define(function(require) { self._off('storage', lock); } if (waitTimer) { - window.clearTimeout(waitTimer); + clearTimeout(waitTimer); } localStorage.removeItem(INDEX_LOCK); } @@ -240,7 +247,7 @@ define(function(require) { localStorage.setItem(INDEX_EMIT, data); self.trigger(name, message); - window.setTimeout(function() { + setTimeout(function() { self._cleanup_emit(); }, 50); }); @@ -277,7 +284,7 @@ define(function(require) { localStorage.setItem(INDEX_ONCE, JSON.stringify(data)); fn(); - window.setTimeout(function() { + setTimeout(function() { self._cleanup_once(); }, 50); }); diff --git a/src/providers/indexeddb.js b/src/providers/indexeddb.js index 4975d60..8a42f87 100644 --- a/src/providers/indexeddb.js +++ b/src/providers/indexeddb.js @@ -2,10 +2,12 @@ define(function(require) { var FILE_SYSTEM_NAME = require('src/constants').FILE_SYSTEM_NAME; var FILE_STORE_NAME = require('src/constants').FILE_STORE_NAME; - var indexedDB = window.indexedDB || - window.mozIndexedDB || - window.webkitIndexedDB || - window.msIndexedDB; + var indexedDB = (function(window) { + return window.indexedDB || + window.mozIndexedDB || + window.webkitIndexedDB || + window.msIndexedDB; + }(this)); var IDB_RW = require('src/constants').IDB_RW; var IDB_RO = require('src/constants').IDB_RO; diff --git a/src/providers/websql.js b/src/providers/websql.js index c765f8a..67ed6b1 100644 --- a/src/providers/websql.js +++ b/src/providers/websql.js @@ -98,7 +98,7 @@ define(function(require) { this.db = null; } WebSQL.isSupported = function() { - return !!window.openDatabase; + return typeof window === 'undefined' ? false : !!window.openDatabase; }; WebSQL.prototype.open = function(callback) {