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
Implement Web storage - localStorage, sessionStorage, StorageEvent #2076
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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,26 @@ | ||
"use strict"; | ||
|
||
const EventImpl = require("./Event-impl").implementation; | ||
|
||
const StorageEventInit = require("../generated/StorageEventInit"); | ||
|
||
// https://html.spec.whatwg.org/multipage/webstorage.html#the-storageevent-interface | ||
class StorageEventImpl extends EventImpl { | ||
initStorageEvent(type, bubbles, cancelable, key, oldValue, newValue, url, storageArea) { | ||
if (this._dispatchFlag) { | ||
return; | ||
} | ||
|
||
this.initEvent(type, bubbles, cancelable); | ||
this.key = key; | ||
this.oldValue = oldValue; | ||
this.newValue = newValue; | ||
this.url = url; | ||
this.storageArea = storageArea; | ||
} | ||
} | ||
StorageEventImpl.defaultInit = StorageEventInit.convert(undefined); | ||
|
||
module.exports = { | ||
implementation: StorageEventImpl | ||
}; |
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,17 @@ | ||
[Exposed=Window, | ||
Constructor(DOMString type, optional StorageEventInit eventInitDict)] | ||
interface StorageEvent : Event { | ||
readonly attribute DOMString? key; | ||
readonly attribute DOMString? oldValue; | ||
readonly attribute DOMString? newValue; | ||
readonly attribute USVString url; | ||
readonly attribute Storage? storageArea; | ||
}; | ||
|
||
dictionary StorageEventInit : EventInit { | ||
DOMString? key = null; | ||
DOMString? oldValue = null; | ||
DOMString? newValue = null; | ||
USVString url = ""; | ||
Storage? storageArea = null; | ||
}; |
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
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,98 @@ | ||
"use strict"; | ||
|
||
const DOMException = require("domexception"); | ||
const StorageEvent = require("../generated/StorageEvent"); | ||
const idlUtils = require("../generated/utils"); | ||
|
||
// https://html.spec.whatwg.org/multipage/webstorage.html#the-storage-interface | ||
class StorageImpl { | ||
constructor(args, { associatedWindow, storageArea, url, type }) { | ||
this._associatedWindow = associatedWindow; | ||
this._items = storageArea; | ||
this._url = url; | ||
this._type = type; | ||
|
||
// The spec suggests a default storage quota of 5 MB | ||
this._quota = 5000; | ||
} | ||
|
||
_dispatchStorageEvent(key, oldValue, newValue) { | ||
return this._associatedWindow._currentOriginData.windowsInSameOrigin | ||
.filter(target => target !== this._associatedWindow) | ||
.forEach(target => target.dispatchEvent(StorageEvent.create([ | ||
"storage", | ||
{ | ||
bubbles: false, | ||
cancelable: false, | ||
key, | ||
oldValue, | ||
newValue, | ||
url: this._url, | ||
storageArea: target["_" + this._type] | ||
} | ||
]))); | ||
} | ||
|
||
get length() { | ||
return this._items.size; | ||
} | ||
|
||
key(n) { | ||
if (n >= this._items.size) { | ||
return null; | ||
} | ||
return [...this._items.keys()][n]; | ||
} | ||
|
||
getItem(key) { | ||
if (this._items.has(key)) { | ||
return this._items.get(key); | ||
} | ||
return null; | ||
} | ||
|
||
setItem(key, value) { | ||
const oldValue = this._items.get(key) || null; | ||
|
||
if (oldValue === value) { | ||
return; | ||
} | ||
|
||
// Concatenate all keys and values to measure their size against the quota | ||
let itemsConcat = key + value; | ||
this._items.forEach((v, k) => { | ||
itemsConcat += v + k; | ||
}); | ||
if (Buffer.byteLength(itemsConcat) > this._quota) { | ||
throw new DOMException(`The ${this._quota} byte storage quota has been exceeded.`, "QuotaExceededError"); | ||
} | ||
|
||
setTimeout(this._dispatchStorageEvent.bind(this), 0, key, oldValue, value); | ||
|
||
this._items.set(key, value); | ||
} | ||
|
||
removeItem(key) { | ||
if (this._items.has(key)) { | ||
setTimeout(this._dispatchStorageEvent.bind(this), 0, key, this._items.get(key), null); | ||
|
||
this._items.delete(key); | ||
} | ||
} | ||
|
||
clear() { | ||
if (this._items.size > 0) { | ||
setTimeout(this._dispatchStorageEvent.bind(this), 0, null, null, null); | ||
|
||
this._items.clear(); | ||
} | ||
} | ||
|
||
get [idlUtils.supportedPropertyNames]() { | ||
return this._items.keys(); | ||
} | ||
} | ||
|
||
module.exports = { | ||
implementation: StorageImpl | ||
}; |
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,9 @@ | ||
[Exposed=Window] | ||
interface Storage { | ||
readonly attribute unsigned long length; | ||
DOMString? key(unsigned long index); | ||
[WebIDL2JSValueAsUnsupported=null] getter DOMString? getItem(DOMString key); | ||
setter void setItem(DOMString key, DOMString value); | ||
deleter void removeItem(DOMString key); | ||
void clear(); | ||
}; |
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
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.
@Zirro
As I understand, 5000 would suggest that the default storage quota is 5 KB... shouldn't that be 5_000_000 bytes?