Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Web storage - localStorage, sessionStorage, StorageEvent
- Loading branch information
Showing
10 changed files
with
223 additions
and
4 deletions.
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; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Zirro
Author
Member
|
||
} | ||
|
||
_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
As I understand, 5000 would suggest that the default storage quota is 5 KB, right?
Shouldn't that be 5_000_000 bytes?