Socket
Socket
Sign inDemoInstall

localstorage

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.0 to 1.0.0

198

index.js

@@ -1,146 +0,94 @@

'use strict'
class LocalStorage {
constructor (namespace) {
this._namespace = namespace
this._store = window.localStorage
this._sep = '\x00'
}
function Storage(environment) {
}
static clear () {
window.localStorage.clear()
}
Storage.prototype.set = function(items, callbackFunction) {
this.base.set(items, callbackFunction)
}
_notFound (k) {
const err = new Error(`Not Found [${k}]`)
err.notFound = true
err.key = k
return err
}
Storage.prototype.get = function(keys, callbackFunction) {
this.base.get(keys, callbackFunction)
}
get (key) {
const k = [this._namespace, key].join(this._sep)
if (!this._store[k]) return [this._notFound(k)]
Storage.prototype.remove = function(keys, callbackFunction) {
this.base.remove(keys, callbackFunction)
}
try {
return [null, JSON.parse(this._store[k])]
} catch (err) {
return [err]
}
}
function StorageBase(environment) {
var _this = this
put (key, value) {
if (typeof value === 'undefined') {
return [new Error(`Invalid parameters to put, ('${key}', undefined)`)]
}
if (!environment) {
environment = 'chrome'
try {
const k = [this._namespace, key].join(this._sep)
const v = JSON.stringify(value)
const result = this._store[k] = v
return [null, result]
} catch (err) {
return [err]
}
if (environment === 'chrome') {
this.localStorage = chrome.storage.local
this.runtime = chrome.runtime
this.extension = chrome.extension
} else {
throw 'Invalid environment'
}
}
this.isBackgroundPage = (location.protocol === "chrome-extension:" &&
this.extension.getBackgroundPage() === window)
has (key) {
const k = [this._namespace, key].join(this._sep)
if (!this._store[k]) return [this._notFound(k)]
return [null, true]
}
this.storageLoadedPromise = new Promise(function(resolve, reject) {
_this.localStorage.get(null, function(data) {
_this.data = data
resolve('loaded!')
})
})
del (key) {
if (key) {
const k = [this._namespace, key].join(this._sep)
if (!this._store[k]) return [this._notFound(k)]
this.runtime.onMessage.addListener(this.proxyClient.bind(this))
}
StorageBase.prototype.proxyClient = function(request, sender, sendResponse) {
if (request.messageLocation &&
request.messageLocation !== "storage") {
return false
delete this._store[k]
return [null]
}
if (typeof(request.method) === "undefined") {
return false
}
if (this.isBackgroundPage) {
if (request.method === 'get') {
return this.get(request.key, sendResponse)
} else if (request.method === 'set') {
return this.set(request.items, sendResponse)
} else if (request.method === 'remove') {
return this.remove(request.keys, sendResponse)
}
Object.keys(window.localStorage).forEach(k => {
const ns = k.split(this._sep)[0]
if (ns === this._namespace) {
delete this._store[k]
}
})
}
search (pattern) {
if (!pattern) {
throw new Error('A pattern is required')
}
}
StorageBase.prototype.set = function(items, callbackFunction) {
var _this = this
const matchKeys = key => {
const [, _key] = key.split(this._sep)
this.storageLoadedPromise.then(function() {
_.merge(_this.data, items)
if (_this.isBackgroundPage) {
_this.localStorage.set(_this.data, callbackFunction)
} else {
if (!callbackFunction) {
callbackFunction = function() {}
}
_this.runtime.sendMessage({
method: "set",
items: items,
messageLocation: "storage"
}, callbackFunction)
}
})
if (!_key) return
if (!pattern.test(_key)) return
return true
}
return key
}
StorageBase.prototype.get = function(key, callbackFunction) {
var _this = this
this.storageLoadedPromise.then(function() {
if (typeof callbackFunction !== "function") {
return
}
if (_this.isBackgroundPage) {
if (key === null) {
// Return all data
callbackFunction(_this.data)
} else {
// Return the data for a particular key
if (!_this.data[key]) {
// If there's no hit, return null
callbackFunction(null)
} else {
callbackFunction(_this.data[key])
}
}
} else {
_this.runtime.sendMessage({
method: "get",
key: key,
messageLocation: "storage"
}, callbackFunction)
}
const makePairs = key => ({
key: key.split(this._sep)[1],
value: this._store[key]
})
return true
return [null, Object
.keys(this._store)
.filter(matchKeys)
.map(makePairs)]
}
}
StorageBase.prototype.remove = function(keys, callbackFunction) {
var _this = this
if (!(keys instanceof Array)) keys = [keys]
this.storageLoadedPromise.then(function() {
if (_this.isBackgroundPage) {
for (var i = 0; i < keys.length; i++) {
delete(_this.data[keys[i]])
}
_this.localStorage.remove(keys, callbackFunction)
} else {
if (!callbackFunction) {
callbackFunction = function() {}
}
_this.runtime.sendMessage({
method: "remove",
keys: keys,
messageLocation: "storage"
}, callbackFunction)
}
})
return true
}
Storage.prototype.base = new StorageBase()
module.exports = Storage
module.exports = LocalStorage
{
"name": "localstorage",
"version": "0.1.0",
"description": "Local storage module for Chrome",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "node test.js"
"test": "standard ."
},
"author": "Halfmoon Labs, Inc.",
"license": "MIT"
"author": "voltraco",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/voltraco/localstorage.git"
},
"bugs": {
"url": "https://github.com/voltraco/localstorage/issues"
},
"homepage": "https://github.com/voltraco/localstorage#readme",
"description": "serialize, deserialize and namespace JSON"
}

@@ -1,1 +0,29 @@

# Local Storage
# SYNOPSIS
A localStorage helper. Serialize and deserialize JSON, add namspaces, check for
keys not found.
# INSTALL
```bash
npm install voltraco/localstorage
```
# USAGE
```js
const LocalStorage = require('localstorage')
const user = new LocalStorage('user') // create the `user` namespace
user.put('quxx', { foo: 100 })
user.get('quxx') // [null, { foo: 100 }]
user.get('foo') // [ErrorNotFOund]
user.has('quxx') // [null, true]
user.has('foo') // [ErrorNotFound]
user.delete('quxx') // [null, true]
user.delete('foo') // [ErrorNotFound]
user.delete() // delete everything in the `user` namespace
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc