Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
@ngx-pwa/local-storage
Advanced tools
Efficient local storage module for Angular apps and PWA: simple API based on native localStorage API, but internally stored via the asynchronous IndexedDB API for performance, and wrapped in RxJS observables to be homogeneous with other Angular modules.
Efficient local storage module for Angular apps and Progressive Wep apps (PWA):
localStorage
API, with automatic JSON stringify/parse,IndexedDB
API,Observables
,For now, Angular does not provide a local storage module, and almost every app needs some local storage. There are 2 native JavaScript APIs available:
The localStorage
API is simple to use but synchronous, so if you use it too often,
your app will soon begin to freeze.
The IndexedDB
API is asynchronous and efficient, but it's a mess to use:
you'll soon be caught by the callback hell, as it does not support Promises yet.
Mozilla has done a very great job with the localForage library:
a simple API based on native localStorage
,
but internally stored via the asynchronous IndexedDB
for performance.
But it's built in ES5 old school way and then it's a mess to include into Angular.
This module is based on the same idea as localForage, but in ES6 and additionally wrapped into RxJS Observables to be homogeneous with other Angular modules.
Install the same version as your Angular one via npm:
# For Angular 8:
npm install @ngx-pwa/local-storage@next
# For Angular 6 and Angular 7:
npm install @ngx-pwa/local-storage@6
Now you just have to inject the service where you need it:
import { LocalStorage } from '@ngx-pwa/local-storage';
@Injectable()
export class YourService {
constructor(private localStorage: LocalStorage) {}
}
Versions 4 & 5 (only) need an additional setup step explained in the old module guide.
v7 of this lib works but is not recommended due to unforeseen issues. For example, v7 doesn't support TypeScript 3.2 due to a TypeScript regression (see #64).
v8 has cleaned the mess, but for users still using Angular 7, stay on v6 of this lib as it is compatible with Angular 7 too.
If you still use the old angular-async-local-storage
package, or to update to new versions,
see the migration guides.
The API follows the native localStorage API, except it's asynchronous via RxJS Observables.
let user: User = { firstName: 'Henri', lastName: 'Bergson' };
this.localStorage.setItem('user', user).subscribe(() => {});
You can store any value, without worrying about stringifying.
To delete one item:
this.localStorage.removeItem('user').subscribe(() => {});
To delete all items:
this.localStorage.clear().subscribe(() => {});
this.localStorage.getItem<User>('user').subscribe((user) => {
user.firstName; // should be 'Henri'
});
Not finding an item is not an error, it succeeds but returns null
.
this.localStorage.getItem('notexisting').subscribe((data) => {
data; // null
});
If you tried to store undefined
, you'll get null
too, as some storages don't allow undefined
.
Note you'll only get one value: the Observable
is here for asynchronicity but is not meant to
emit again when the stored data is changed. And it's normal: if app data change, it's the role of your app
to keep track of it, not of this lib. See #16
for more context and #4
for an example.
Don't forget it's client-side storage: always check the data, as it could have been forged or deleted.
You can use a JSON Schema to validate the data. Starting with version 7, validation is now required.
this.localStorage.getItem('test', { type: 'string' })
.subscribe((user) => {
// Called if data is valid or null
}, (error) => {
// Called if data is invalid
});
See the full validation guide to see how to validate all common scenarios.
You DO NOT need to unsubscribe: the observable autocompletes (like in the HttpClient
service).
But you DO need to subscribe, even if you don't have something specific to do after writing in local storage (because it's how RxJS Observables work).
Since version 5.2, you can use these methods to auto-subscribe:
this.localStorage.setItemSubscribe('user', user);
this.localStorage.removeItemSubscribe('user');
this.localStorage.clearSubscribe();
Use these methods only if these conditions are fulfilled:
As usual, you should catch any potential error:
this.localStorage.setItem('color', 'red').subscribe(() => {
// Done
}, (error) => {
// Error
});
Could happen to anyone:
.setItem()
: storage is full (DOMException
with name 'QuotaExceededError
).setItem()
when in localStorage
fallback: error in JSON serialization because of circular references (TypeError
)Should only happen if data was corrupted or modified from outside of the lib:
.getItem()
: data invalid against your JSON schema (ValidationError
from this lib).getItem()
when in localStorage
fallback: error in JSON unserialization (SyntaxError
)indexedDB
: database store has been deleted (DOMException
with name 'NotFoundError'
)Other errors are supposed to be catched or avoided by the lib, so if you were to run into an unlisted error, please file an issue.
Map
-like operationsStarting with version >= 7.4, in addition to the classic localStorage
-like API,
this lib also provides some Map
-like methods for advanced operations:
.keys()
method.has(key)
method.size
propertySee the documentation for more info and some recipes.
If you have multiple apps on the same subdomain and you don't want to share data between them, see the prefix guide.
This lib major version is aligned to the major version of Angular. Meaning for Angular 6 you need version 6, for Angular 7 you need version 7, for Angular 8 you need version 8, and so on.
We follow Angular LTS support, meaning we support Angular 6 minimum, until November 2019.
This module supports AoT pre-compiling.
This module supports Universal server-side rendering via a mock storage.
All browsers supporting IndexedDB, ie. all current browsers : Firefox, Chrome, Opera, Safari, Edge, and IE10+.
See the browsers support guide for more details and special cases (like private browsing).
For interoperability when mixing this lib with direct usage of native APIs or other libs like localforage
(which doesn't make sense in most of cases),
see the interoperability documentation.
Changelog available here, and migration guides here.
MIT
FAQs
Efficient local storage module for Angular: simple API based on native localStorage API, but internally stored via the asynchronous IndexedDB API for performance, and wrapped in RxJS observables to be homogeneous with other Angular modules.
The npm package @ngx-pwa/local-storage receives a total of 12,417 weekly downloads. As such, @ngx-pwa/local-storage popularity was classified as popular.
We found that @ngx-pwa/local-storage demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.