Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

safer-web-storage

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

safer-web-storage

Handle all exception when storage is not available due to user's browser settings. Swap to in-memory storage if sessionStorage is not available.

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

safer-web-storage

Browsers that support localStorage and sessionStorage will have a property on the window object named respectively. However, for various reasons, just asserting that property exists may throw exceptions. If it does exist, that is still no guarantee that localStorage is actually available, as various browsers offer settings that disable storages. So a browser may support localStorage, but not make it available to the scripts on the page. One example of that is Safari, which in Private Browsing mode gives us an empty localStorage object with a quota of zero, effectively making it unusable.

Installation and Usage

npm install safer-web-storage
import * as SafeStorage from 'safer-web-storage'

const safeLocalStorage = SafeStorage.createSafeLocalStorage()
const safeSessionStorage = SafeStorage.createSafeSessionStorage()

safeLocalStorage.getItem('apples')
safeSessionStorage.seItem('pineapples', 20)

If either window.sessionStorage or its methods are not accessible, it swaps to in-memory storage.

This wrapper supports all methods and properties of Web Storage API:

  • getItem
  • setItem
  • removeItem
  • key
  • clear
  • length

API

createSafeLocalStorage(options) => Storage

createSafeSessionStorage(options) => Storage

You can pass the following properties to options:

NameTypeDefaultDescription
errorMessageStringLooks like you've disabled <StorageType>. Enable it to avoid this warning.Error message printed to the browser console when storage is not available

Storage public API

NameTypeDetails
isNativeStorageUsedgetterReturns Boolean
isInMemoryStorageUsedgetterReturns Boolean

Development

To test the library in a browser run:

npm install
npm start

Publish

npm ci
npm test
npm run build
npm version [patch|minor|major]
npm publish

Keywords

FAQs

Package last updated on 24 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc