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QAAQ,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,KAE9D,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;WACzE,MAAM;+BACkB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC;;;;gBAA/B,MAAM;gBAAE,IAAI;;AACnB,gBAAI,IAAI,KAAK,QAAQ,EACnB,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,KAC1B,IAAI,IAAI,KAAK,UAAU,EAC1B,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,KAE/B,MAAM,IAAI,KAAK,0BAAwB,IAAI,CAAG,CAAC;WAClD;SACF;;;;;;;;;;;;;;;KACF;;;SAxGG,QAAQ;;;AA4Gd,MAAM,CAAC,OAAO,GAAG,QAAQ,CAAC","file":"storage.js","sourcesContent":["// See [Web Storage](http://dev.w3.org/html5/webstorage/)\nconst DOM = require('./dom');\n\n\n// Implementation of the StorageEvent.\nclass StorageEvent extends DOM.Event {\n\n  constructor(storage, url, key, oldValue, newValue) {\n    super('storage');\n    this._storage   = storage;\n    this._url       = url;\n    this._key       = key;\n    this._oldValue  = oldValue;\n    this._newValue  = newValue;\n  }\n\n  get url() {\n    return this._url;\n  }\n  get storageArea() {\n    return this._storage;\n  }\n  get key() {\n    return this._key;\n  }\n  get oldValue() {\n    return this._oldValue;\n  }\n  get newValue() {\n    return this._newValue;\n  }\n}\n\n\n// Storage area. The storage area is shared by multiple documents of the same\n// origin. For session storage, they must also share the same browsing context.\nclass StorageArea {\n\n  constructor() {\n    this._items     = new Map()\n    this._storages  = [];\n  }\n\n  // Fire a storage event. Fire in all documents that share this storage area,\n  // except for the source document.\n  _fire(source, key, oldValue, newValue) {\n    for (let [storage, window] of this._storages) {\n      if (storage === source)\n        continue;\n      const event = new StorageEvent(storage, window.location.href, key, oldValue, newValue);\n      window.dispatchEvent(event);\n    }\n  }\n\n  // Return number of key/value pairs.\n  get length() {\n    return this._items.size;\n  }\n\n  // Get key by ordinal position.\n  key(index) {\n    const iterator = this._items.keys();\n    let   next = iterator.next();\n    for (let i = 0; i < index; ++i)\n      next = iterator.next();\n    return next.value;\n  }\n\n  // Get value from key\n  get(key) {\n    return this._items.has(key) ? this._items.get(key) : null;\n  }\n\n  // Set the value of a key. We also need the source storage (so we don't send\n  // it a storage event).\n  set(source, key, value) {\n    const oldValue = this._items.get(key);\n    this._items.set(key, value);\n    this._fire(source, key, oldValue, value);\n  }\n\n  // Remove the value at the key. We also need source storage (see set above).\n  remove(source, key) {\n    const oldValue = this._items.get(key);\n    this._items.delete(key);\n    this._fire(source, key, oldValue);\n  }\n\n  // Remove all values. We also need source storage (see set above).\n  clear(source) {\n    this._items.clear();\n    this._fire(source);\n  }\n\n  get pairs() {\n    return [...this._items];\n  }\n\n  toString() {\n    return this._items.toString();\n  }\n\n  // Associate local/sessionStorage and window with this storage area. Used when firing events.\n  associate(storage, window) {\n    this._storages.push([storage, window]);\n  }\n\n}\n\n\n// Implementation of the Storage interface, used by local and session storage.\nclass Storage {\n\n  constructor(area) {\n    this._area = area;\n  }\n\n  // ### storage.length => Number\n  //\n  // Returns the number of key/value pairs in this storage.\n  get length() {\n    return this._area.length;\n  }\n\n  // ### storage.key(index) => String\n  //\n  // Returns the key at this position.\n  key(index) {\n    return this._area.key(index);\n  }\n\n  // ### storage.getItem(key) => Object\n  //\n  // Returns item by key.\n  getItem(key) {\n    return this._area.get(key.toString());\n  }\n\n  // ### storage.setItem(key, Object)\n  //\n  // Add item or change value of existing item.\n  setItem(key, value) {\n    this._area.set(this, key.toString(), value);\n  }\n\n  // ### storage.removeItem(key)\n  //\n  // Remove item.\n  removeItem(key) {\n    this._area.remove(this, key.toString());\n  }\n\n  // ### storage.clear()\n  //\n  // Remove all items.\n  clear() {\n    this._area.clear(this);\n  }\n\n  // Dump to a string, useful for debugging.\n  dump(output = process.stdout) {\n    return this._area.dump(output);\n  }\n\n}\n\n\n\n\n// Combined local/session storage.\nclass Storages {\n\n  constructor() {\n    this._locals    = new Map();\n    this._sessions  = new Map();\n  }\n\n  // Return local Storage based on the document origin (hostname/port).\n  local(host) {\n    if (!this._locals.has(host))\n      this._locals.set(host, new StorageArea());\n    return new Storage(this._locals.get(host));\n  }\n\n  // Return session Storage based on the document origin (hostname/port).\n  session(host) {\n    if (!this._sessions.has(host))\n      this._sessions.set(host, new StorageArea());\n    return new Storage(this._sessions.get(host));\n  }\n\n  // Extend window with local/session storage support.\n  extend(window) {\n    const storages = this;\n    window.StorageEvent = StorageEvent;\n    Object.defineProperties(window, {\n      localStorage: {\n        get() {\n          const { document } = this;\n          if (!document._localStorage)\n            document._localStorage = storages.local(document.location.host);\n          return document._localStorage;\n        }\n      },\n\n      sessionStorage: {\n        get() {\n          const { document } = this;\n          if (!document._sessionStorage)\n            document._sessionStorage = storages.session(document.location.host);\n          return document._sessionStorage;\n        }\n      }\n    });\n  }\n\n  // Used to dump state to console (debuggin)\n  dump(output = process.stdout) {\n    for (let [domain, area] of this._locals) {\n      output.write(`${domain} local:\\n`);\n      for (let [name, value] of area.pairs)\n        output.write(`  ${name} = ${value}\\n`);\n    }\n    for (let [domain, area] of this._sessions) {\n      output.push(`${domain} session:\\n`);\n      for (let [name, value] of area.pairs)\n        output.write(`  ${name} = ${value}\\n`);\n    }\n  }\n\n  // browser.saveStorage uses this\n  save() {\n    const serialized = [`# Saved on ${new Date().toISOString()}`];\n    for (let [domain, area] of this._locals) {\n      let pairs = area.pairs;\n      if (pairs.length) {\n        serialized.push(`${domain} local:`);\n        for (let [name, value] of area.pairs)\n          serialized.push(`  ${escape(name)} = ${escape(value)}`);\n      }\n    }\n    for (let [domain, area] of this._sessions) {\n      let pairs = area.pairs;\n      if (pairs.length) {\n        serialized.push(`${domain} session:`);\n        for (let [name, value] of area.pairs)\n          serialized.push(`  ${escape(name)} = ${escape(value)}`);\n      }\n    }\n    return serialized.join(`\\n`) + `\\n`;\n  }\n\n  // browser.loadStorage uses this\n  load(serialized) {\n    let storage = null;\n    for (let item of serialized.split(/\\n+/)) {\n      if (item[0] === '#' || item === '')\n        continue;\n      if (item[0] === ' ') {\n        const [key, value] = item.split('=');\n        if (storage)\n          storage.setItem(unescape(key.trim()), unescape(value.trim()));\n        else\n          throw new Error('Must specify storage type using local: or session:');\n      } else {\n        const [domain, type] = item.split(' ');\n        if (type === 'local:')\n          storage = this.local(domain);\n        else if (type === 'session:')\n          storage = this.session(domain);\n        else\n          throw new Error(`Unkown storage type ${type}`);\n      }\n    }\n  }\n}\n\n\nmodule.exports = Storages;\n\n"]}