Skip to content

Commit

Permalink
Merge pull request #1 from geekypradip/indexDB-url-storage
Browse files Browse the repository at this point in the history
Support Index db url storage
  • Loading branch information
geekypradip authored Mar 16, 2024
2 parents 97015bf + 0ef565c commit c98adb7
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 14 deletions.
25 changes: 13 additions & 12 deletions lib/browser/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import BaseUpload from '../upload.js'
import NoopUrlStorage from '../noopUrlStorage.js'
import { enableDebugLog } from '../logger.js'
import DetailedError from '../error.js'

import { canStoreURLs, WebStorageUrlStorage } from './urlStorage.js'
import DefaultHttpStack from './httpStack.js'
import { enableDebugLog } from '../logger.js'
import NoopUrlStorage from '../noopUrlStorage.js'
import BaseUpload from '../upload.js'
import FileReader from './fileReader.js'
import fingerprint from './fileSignature.js'
import DefaultHttpStack from './httpStack.js'
import { canStoreURLsInIndexDB, WebIndexDBStorageUrlStorage } from './urlStorageIndexDB.js'
import { canStoreURLsInLocalStorage, WebLocalStorageUrlStorage } from './urlStorageLocalStorage.js'

const defaultOptions = {
...BaseUpload.defaultOptions,
httpStack: new DefaultHttpStack(),
fileReader: new FileReader(),
urlStorage: canStoreURLs ? new WebStorageUrlStorage() : new NoopUrlStorage(),
urlStorage:canStoreURLsInIndexDB ? new WebIndexDBStorageUrlStorage() : canStoreURLsInLocalStorage ? new WebLocalStorageUrlStorage() : new NoopUrlStorage(),

Check failure on line 15 in lib/browser/index.js

View workflow job for this annotation

GitHub Actions / Types

Do not nest ternary expressions

Check failure on line 15 in lib/browser/index.js

View workflow job for this annotation

GitHub Actions / Browserstack

Do not nest ternary expressions

Check failure on line 15 in lib/browser/index.js

View workflow job for this annotation

GitHub Actions / Node.js 18

Do not nest ternary expressions

Check failure on line 15 in lib/browser/index.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Do not nest ternary expressions

Check failure on line 15 in lib/browser/index.js

View workflow job for this annotation

GitHub Actions / Puppeteer

Do not nest ternary expressions
fingerprint,
}

Expand All @@ -35,11 +35,12 @@ const isSupported =
typeof Blob.prototype.slice === 'function'

export {
Upload,
canStoreURLs,
defaultOptions,
isSupported,
enableDebugLog,
canStoreURLsInIndexDB,
canStoreURLsInLocalStorage,
DefaultHttpStack,
defaultOptions,
DetailedError,
enableDebugLog,
isSupported,
Upload
}
118 changes: 118 additions & 0 deletions lib/browser/urlStorageIndexDB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
const isSupportIndexDB = () => {
return 'indexedDB' in window && !/iPad|iPhone|iPod/.test(navigator.platform);
};
let hasStorage = false;
try {
hasStorage = isSupportIndexDB();
} catch (e) {
if (e.code === e.SECURITY_ERR || e.code === e.QUOTA_EXCEEDED_ERR) {
hasStorage = false;
} else {
throw e;
}
}

export const canStoreURLsInIndexDB = hasStorage;

export class WebIndexDBStorageUrlStorage {
constructor() {
this.dbName = 'tusUrlStorage';
this.storeName = 'upload';
this.dbPromise = this.openDatabase();
}

openDatabase() {
return new Promise((resolve, reject) => {
const openRequest = indexedDB.open(this.dbName);
openRequest.onupgradeneeded = function () {

Check warning on line 27 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Types

Unexpected unnamed function 'bind'

Check warning on line 27 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Browserstack

Unexpected unnamed function 'bind'

Check warning on line 27 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Node.js 18

Unexpected unnamed function 'bind'

Check warning on line 27 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Unexpected unnamed function 'bind'

Check warning on line 27 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Puppeteer

Unexpected unnamed function 'bind'
const db = openRequest.result;
if (!db.objectStoreNames.contains(this.storeName)) {
db.createObjectStore(this.storeName, {keyPath: 'urlStorageKey'});
}
}.bind(this);
openRequest.onsuccess = function () {

Check warning on line 33 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Types

Unexpected unnamed function

Check warning on line 33 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Browserstack

Unexpected unnamed function

Check warning on line 33 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Node.js 18

Unexpected unnamed function

Check warning on line 33 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Unexpected unnamed function

Check warning on line 33 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Puppeteer

Unexpected unnamed function
resolve(openRequest.result);
};
openRequest.onerror = reject;
});
}

async _getAllUploadWithKeys() {
try {
const db = await this.dbPromise;
const transaction = db.transaction(this.storeName, 'readonly');
const store = transaction.objectStore(this.storeName);
const request = store.getAll();
const results = await new Promise((resolve, reject) => {
request.onsuccess = () => resolve(request.result);
request.onerror = reject;
});
return results.map((result) => ({
...result,
urlStorageKey: result.urlStorageKey,
}));
} catch (error) {
console.error('Error getting all uploads with keys:', error);

Check warning on line 55 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Types

Unexpected console statement

Check warning on line 55 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Browserstack

Unexpected console statement

Check warning on line 55 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Node.js 18

Unexpected console statement

Check warning on line 55 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Unexpected console statement

Check warning on line 55 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Puppeteer

Unexpected console statement
throw error;
}
}

async findAllUploads() {
try {
const results = await this._getAllUploadWithKeys();
return results;
} catch (error) {
console.error('Error finding all uploads:', error);

Check warning on line 65 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Types

Unexpected console statement

Check warning on line 65 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Browserstack

Unexpected console statement

Check warning on line 65 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Node.js 18

Unexpected console statement

Check warning on line 65 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Unexpected console statement

Check warning on line 65 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Puppeteer

Unexpected console statement
throw error;
}
}

async findUploadsByFingerprint(fingerprint) {
try {
const allData = await this._getAllUploadWithKeys();
const results = allData.find(
(data) => data.urlStorageKey.indexOf(`tus::${fingerprint}::`) === 0
);

return results ? [results] : [];
} catch (error) {
console.error('Error finding uploads by fingerprint:', error);

Check warning on line 79 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Types

Unexpected console statement

Check warning on line 79 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Browserstack

Unexpected console statement

Check warning on line 79 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Node.js 18

Unexpected console statement

Check warning on line 79 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Unexpected console statement

Check warning on line 79 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Puppeteer

Unexpected console statement
throw error;
}
}

async removeUpload(urlStorageKey) {
try {
const db = await this.dbPromise;
const transaction = db.transaction(this.storeName, 'readwrite');
const store = transaction.objectStore(this.storeName);
const request = store.delete(urlStorageKey);
await new Promise((resolve, reject) => {
request.onsuccess = resolve;
request.onerror = reject;
});
} catch (error) {
console.error('Error removing upload:', error);

Check warning on line 95 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Types

Unexpected console statement

Check warning on line 95 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Browserstack

Unexpected console statement

Check warning on line 95 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Node.js 18

Unexpected console statement

Check warning on line 95 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Unexpected console statement

Check warning on line 95 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Puppeteer

Unexpected console statement
throw error;
}
}

async addUpload(fingerprint, upload) {
try {
const id = Math.round(Math.random() * 1e12);
const key = `tus::${fingerprint}::${id}`;
const db = await this.dbPromise;
const transaction = db.transaction(this.storeName, 'readwrite');
const store = transaction.objectStore(this.storeName);
const request = store.put({urlStorageKey: key, ...upload});
await new Promise((resolve, reject) => {
request.onsuccess = () => resolve(key);
request.onerror = reject;
});
return key;
} catch (error) {
console.error('Error adding upload:', error);

Check warning on line 114 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Types

Unexpected console statement

Check warning on line 114 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Browserstack

Unexpected console statement

Check warning on line 114 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Node.js 18

Unexpected console statement

Check warning on line 114 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Unexpected console statement

Check warning on line 114 in lib/browser/urlStorageIndexDB.js

View workflow job for this annotation

GitHub Actions / Puppeteer

Unexpected console statement
throw error;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ try {
}
}

export const canStoreURLs = hasStorage
export const canStoreURLsInLocalStorage = hasStorage

export class WebStorageUrlStorage {
export class WebLocalStorageUrlStorage {
findAllUploads() {
const results = this._findEntries('tus::')
return Promise.resolve(results)
Expand Down

0 comments on commit c98adb7

Please sign in to comment.