Giter VIP home page Giter VIP logo

digitalfortress-tech / localstorage-slim Goto Github PK

View Code? Open in Web Editor NEW
123.0 123.0 9.0 688 KB

An ultra slim localstorage wrapper with optional support for ttl and encryption

Home Page: https://digitalfortress.tech/js/localstorage-with-ttl-time-to-expiry/

License: MIT License

JavaScript 81.70% Makefile 1.61% TypeScript 16.70%
cryptojs encrypt-localstorage encryption localstorage localstorage-api localstorage-expire localstorage-ttl localstorage-wrapper secure-ls

localstorage-slim's People

Contributors

cdauth avatar lc-soft avatar niketpathak avatar res260 avatar rostow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

localstorage-slim's Issues

Setting secret key from env variable

I am trying to set the secret key from environment variable but I cannot do that.

// env
REACT_APP_LSKEY=123456789101112

//global setting
let key = Number(process.env.REACT_APP_LSKEY); //converting string to number
ls.config.encrypt = true;
ls.config.secret = key;

NOTE: I tried using secret key as string, still same issue hence I converted it into NUMBER.

//setting data in local storage
ls.set("foo", { bar: "baz" });

//getting data from local storage
let key = Number(process.env.REACT_APP_LSKEY);
let lsData= ls.get("foo", { secret: key });
console.log(lsData) // o/p: (empty)

Screenshot_9

If I set ls.config.secret = 50 then it is working fine but I want my secret key from env variable. Please help. Thank You.

Support for Server Side Rendering?

A small question, does localstorage-slim support Server Side Rendering with NextJS? It works fine in development but I'm not sure about production.

Support sessionStorage?

Not a bug, a feature request.

Expected Behavior

It'd be nice to be able to use sessionStorage instead of localStorage in certain cases. Obviously localStorage should remain the default, but some way to configure this module would be useful.

Current Behavior

localStorage is always used.

Possible Solution

ls.config.storage = sessionStorage

ls.get('...') // use as normal, but will now read/write from `sessionStorage` instead

The nice thing about this is it'd allow for setting storage to other values with the same interface too - which could be useful for testing or for writing a low-level wrapper for localStorage or sessionStorage.

Context

Not having this means we get lots of nice features for using localStorage like better type-safety, rich object storage, and encyrption (also ttl/expiry, although obviously that's less of a concern for sessionStorage), but we have to use the raw API or find a similar library for sessionStorage. Given the tiny change that would be needed in this library to support sessionStorage, it seems worth doing.

SessionStorage is ignored when used in localoptions object.

What happened?

If one sets the storage option as sessionStorage in localoptions object it will be ignored and localStorage will be used instead.

Example:

ls.set("testItem", "123", {storage: sessionStorage});

It will save testItem in localStorage and not in sessionStorage. It can be observed on browser dev tools.

Also, when retrieving data, the library also ignores the storage option:

ls.get("testItem", {storage: sessionStorage});

It will retrieve the object not only in sessionStorage but also in localStorage. Not sure if the last behavior is intended though.

I provided a codepen project that shows both local and session behavior.

Affected version

v2.7.0

Reproduction URL

https://codepen.io/gurgelff/pen/OJdoyxr

How to reproduce?

1 - Go to the provided codepen link
2 - Write some value in input field, like 123 (the code is already set to use sessionStorage in localconfig object).
3 - Notice that "Get from sessionStorage (native API):" returns nothing from SessionStorage.
4 - Notice also that both session and local storage using the library return some values, even though the saved value is only present in session, and not in localStorage.

Browsers

Firefox, Brave

Relevant log output

No response

Additional context

On Codepen, when using global config the issue seems resolved. But a localconfig scope is something useful that needs to be solved.

There is also an issue when used in Angular, that even with a global config it still doesn't use sessionStorage, but that is a matter for another issue.

Exceptions thrown when used in a third-party iframe with third-party cookies disabled

What happened?

I'm using the library in a cross-origin iframe while having third-party cookies disabled in Chrome.

In this situation, attempting to access the localStorage global variable results in the following exception: Uncaught DOMException: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.

This library does not catch this exception. The fallback to memory store is never reached, because that is only enabled on an exception during localStorage.getItem(), which is never reached. Furthermore, isInit is set to true before the exception is thrown, causing storage to be undefined, resulting in subsequent exceptions such as Cannot read properties of undefined (reading 'getItem').

Affected version

Latest

Reproduction URL

https://codepen.io/niketpathak/pen/WNMKzoJ

How to reproduce?

Can be reproduced with the default Codepen while having third-party cookies disabled in Chrome. Typing something into the text field will raise errors in the browser console, and the value never makes it into the storage.

Browsers

Chrome

Relevant log output

No response

Additional context

No response

Uncaught SyntaxError: Unexpected token e in JSON at position 0

If the value is a string i am getting this error on ls.get using the package as browser context
Uncaught SyntaxError: Unexpected token e in JSON at position 0

Browser: Version 96.0.4664.93 (Official Build) (64-bit)
OS: windows 10

NB: I am using the value as a string with double quote

Callback on timeout

Could you add a feature to allow for a callback function to be passed into the set() method, and this callback would be fired when the ttl is reached?

Thanks
Justin

A SyntaxError occurs during JSON parsing if the input parameter hasn't been stringified.

What happened?

When a key's value pre-exists as a string or has been previously set using the localStorage.setItem() method in local storage, a JSON parsing error arises upon invoking the ls.get() method.

Affected version

Latest

Reproduction URL

https://jsfiddle.net/j3qhtv28/5/

How to reproduce?

  1. Go to the provided reproduction link
  2. Click on 'Console'
  3. See the error

Browsers

Chrome

Relevant log output

Uncaught SyntaxError: Unexpected token 'd', "d" is not valid JSON
    at JSON.parse (<anonymous>)
    at <anonymous>:1:6

Additional context

I observed that this issue was addressed in the commit. Do you plan to merge it into the main branch soon? If not, I'm willing to submit another request to expedite the fix, as it's crucial to resolve this promptly. This is a common scenario many front-end developers might encounter when launching a new app on the default port, especially if there's a residual "token" value in the local storage not set by the ls Object. Addressing this will prevent potential confusion.

The supportLS function returns incorrect value for browsers that do not support localstorage

Expected Behavior

The supportLS function should return false when a browser doesn't support localstorage.

Current Behavior

The supportLS function always return true, as the hasLS = true assignment happens after the try/catch block, which overwrites the value of hasLS when it is false.

Steps to Reproduce (for bugs)

Not provided, but bug is obvious by looking at the code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.