-
Notifications
You must be signed in to change notification settings - Fork 319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support IndexedDB base URL storage #677
Open
geekypradip
wants to merge
10
commits into
tus:main
Choose a base branch
from
geekypradip:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f63900e
support to store url in indexDB
geekypradip 0ef565c
export canStoreURLsInIndexDB
geekypradip c98adb7
Merge pull request #1 from geekypradip/indexDB-url-storage
geekypradip 2d7c809
Update lib/browser/urlStorageIndexDB.js
geekypradip 8a998f1
rename
geekypradip 5ecb660
added new upload option
geekypradip a029786
create a new option instead of change default option for url storage
geekypradip 19f6f75
remove console and use es6 to drop bind
geekypradip 7c01f4c
Merge branch 'main' of https://github.com/geekypradip/tus-js-client i…
geekypradip 1d8333c
Merge pull request #2 from geekypradip/indexDB-url-storage
geekypradip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
const isSupportIndexedDB = () => { | ||
return 'indexedDB' in window | ||
}; | ||
|
||
let hasStorage = false; | ||
try { | ||
hasStorage = isSupportIndexedDB(); | ||
} catch (e) { | ||
if (e.code === e.SECURITY_ERR || e.code === e.QUOTA_EXCEEDED_ERR) { | ||
hasStorage = false; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
|
||
export const canStoreURLsInIndexedDB = hasStorage; | ||
|
||
export class IndexedDBUrlStorage { | ||
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 = (event) => { | ||
const db = event.target.result; | ||
if (!db.objectStoreNames.contains(this.storeName)) { | ||
db.createObjectStore(this.storeName, {keyPath: 'urlStorageKey'}); | ||
} | ||
}; | ||
openRequest.onsuccess = () => resolve(openRequest.result); | ||
openRequest.onerror = (event) => reject(event); | ||
}); | ||
} | ||
|
||
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) { | ||
throw error; | ||
} | ||
} | ||
|
||
async findAllUploads() { | ||
try { | ||
const results = await this._getAllUploadWithKeys(); | ||
return results; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
async findUploadsByFingerprint(fingerprint) { | ||
try { | ||
const allData = await this._getAllUploadWithKeys(); | ||
const results = allData.find( | ||
(data) => data.urlStorageKey.startsWith(`tus::${fingerprint}::`) | ||
); | ||
|
||
return results ? [results] : []; | ||
} catch (error) { | ||
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) { | ||
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) { | ||
throw error; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there some reason not to use
globalThis
rather thanwindow
here?