New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@fuels/local-storage

Package Overview
Dependencies
Maintainers
0
Versions
98
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fuels/local-storage - npm Package Compare versions

Comparing version 0.25.1 to 0.26.0-main-5a9cf49d6b39cb2fb9e9cb5465b117fdab7bf954

6

CHANGELOG.md
# @fuels/local-storage
## 0.26.0-main-5a9cf49d6b39cb2fb9e9cb5465b117fdab7bf954
### Minor Changes
- Check for localStorage availability, by [@nelitow](https://github.com/nelitow) (See [#132](https://github.com/FuelLabs/fuels-npm-packs/pull/132))
## 0.25.1

@@ -4,0 +10,0 @@

2

dist/index.d.ts

@@ -6,3 +6,5 @@ import { EventEmitter } from 'events';

private emitter;
private isAvailable;
constructor(prefix: string, emitter?: EventEmitter);
private checkLocalStorageAvailability;
subscribe: (listener: <T extends unknown[]>(...args: T) => void) => () => void;

@@ -9,0 +11,0 @@ setItem: <T>(key: string, value: T) => void;

@@ -18,6 +18,13 @@ 'use strict';

this.setItem = (key, value) => {
localStorage.setItem(this.createKey(key), JSON.stringify(value));
this.dispatchChange(key, value);
if (!this.isAvailable)
return;
try {
localStorage.setItem(this.createKey(key), JSON.stringify(value));
this.dispatchChange(key, value);
} catch (error) {
}
};
this.getItem = (key) => {
if (!this.isAvailable)
return null;
try {

@@ -31,12 +38,35 @@ const data = localStorage.getItem(this.createKey(key));

this.clear = () => {
Object.keys(localStorage).filter((key) => key.startsWith(this.prefix)).forEach((key) => localStorage.removeItem(key));
this.dispatchChange();
if (!this.isAvailable)
return;
try {
Object.keys(localStorage).filter((key) => key.startsWith(this.prefix)).forEach((key) => localStorage.removeItem(key));
this.dispatchChange();
} catch (error) {
}
};
this.removeItem = (key) => {
localStorage.removeItem(this.createKey(key));
this.dispatchChange();
if (!this.isAvailable)
return;
try {
localStorage.removeItem(this.createKey(key));
this.dispatchChange();
} catch (error) {
}
};
this.prefix = prefix;
this.emitter = emitter ?? new events.EventEmitter();
this.isAvailable = this.checkLocalStorageAvailability();
}
checkLocalStorageAvailability() {
try {
if (typeof window === "undefined" || !window.localStorage)
return false;
const testKey = "__storage_test__";
localStorage.setItem(testKey, testKey);
localStorage.removeItem(testKey);
return true;
} catch {
return false;
}
}
// ---------------------------------------------------------------------------

@@ -43,0 +73,0 @@ // Private methods

@@ -6,3 +6,5 @@ /// <reference types="node" />

private emitter;
private isAvailable;
constructor(prefix: string, emitter?: EventEmitter);
private checkLocalStorageAvailability;
subscribe: (listener: <T extends unknown[]>(...args: T) => void) => () => void;

@@ -9,0 +11,0 @@ setItem: <T>(key: string, value: T) => void;

8

package.json
{
"name": "@fuels/local-storage",
"version": "0.25.1",
"version": "0.26.0-main-5a9cf49d6b39cb2fb9e9cb5465b117fdab7bf954",
"license": "Apache-2.0",

@@ -14,5 +14,5 @@ "main": "dist/index.js",

"jest-localstorage-mock": "^2.4.26",
"@fuels/ts-config": "0.25.1",
"@fuels/jest": "0.25.1",
"@fuels/tsup-config": "0.25.1"
"@fuels/jest": "0.26.0-main-5a9cf49d6b39cb2fb9e9cb5465b117fdab7bf954",
"@fuels/ts-config": "0.26.0-main-5a9cf49d6b39cb2fb9e9cb5465b117fdab7bf954",
"@fuels/tsup-config": "0.26.0-main-5a9cf49d6b39cb2fb9e9cb5465b117fdab7bf954"
},

@@ -19,0 +19,0 @@ "scripts": {

@@ -6,2 +6,3 @@ import { EventEmitter } from 'events';

private emitter!: EventEmitter;
private isAvailable: boolean;

@@ -11,4 +12,17 @@ constructor(prefix: string, emitter?: EventEmitter) {

this.emitter = emitter ?? new EventEmitter();
this.isAvailable = this.checkLocalStorageAvailability();
}
private checkLocalStorageAvailability(): boolean {
try {
if (typeof window === 'undefined' || !window.localStorage) return false;
const testKey = '__storage_test__';
localStorage.setItem(testKey, testKey);
localStorage.removeItem(testKey);
return true;
} catch {
return false;
}
}
subscribe = (listener: <T extends unknown[]>(...args: T) => void) => {

@@ -23,7 +37,13 @@ if (!this.emitter) return () => {};

setItem = <T>(key: string, value: T) => {
localStorage.setItem(this.createKey(key), JSON.stringify(value));
this.dispatchChange(key, value);
if (!this.isAvailable) return;
try {
localStorage.setItem(this.createKey(key), JSON.stringify(value));
this.dispatchChange(key, value);
} catch (error) {
// Silently fail if localStorage is not available or quota is exceeded
}
};
getItem = <T>(key: string): T | null => {
if (!this.isAvailable) return null;
try {

@@ -38,11 +58,21 @@ const data = localStorage.getItem(this.createKey(key));

clear = () => {
Object.keys(localStorage)
.filter((key) => key.startsWith(this.prefix))
.forEach((key) => localStorage.removeItem(key));
this.dispatchChange();
if (!this.isAvailable) return;
try {
Object.keys(localStorage)
.filter((key) => key.startsWith(this.prefix))
.forEach((key) => localStorage.removeItem(key));
this.dispatchChange();
} catch (error) {
// Silently fail if localStorage operations fail
}
};
removeItem = (key: string) => {
localStorage.removeItem(this.createKey(key));
this.dispatchChange();
if (!this.isAvailable) return;
try {
localStorage.removeItem(this.createKey(key));
this.dispatchChange();
} catch (error) {
// Silently fail if localStorage operations fail
}
};

@@ -49,0 +79,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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