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

@jmondi/browser-storage

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jmondi/browser-storage - npm Package Compare versions

Comparing version 1.3.0 to 1.4.0

36

esm/index.d.ts

@@ -1,23 +0,16 @@

export interface StorageSerializer {
export type StorageSerializer = {
parse<T = unknown>(value: string): T;
stringify<T = unknown>(value: T): string;
}
};
export type Adapter = Pick<Storage, "clear" | "getItem" | "removeItem" | "setItem">;
export type StorageConfig = {
prefix?: string;
serializer?: StorageSerializer;
adapter?: Adapter;
};
export declare class MemoryStorageProvider implements Storage {
private storage;
clear(): void;
getItem(key: string): string | null;
key(_: number): string | null;
removeItem(key: string): void;
setItem(key: string, value: string): void;
get length(): number;
}
export declare abstract class AbstractStorage {
readonly adapter: Storage;
export declare class BrowserStorage {
readonly adapter: Adapter;
readonly prefix: string;
readonly serializer: StorageSerializer;
constructor({ prefix, serializer }?: StorageConfig);
constructor(config?: StorageConfig);
get<T>(key: string): T | null;

@@ -30,7 +23,14 @@ set(key: string, value?: unknown): boolean;

}
export declare class LocalStorage extends AbstractStorage {
readonly adapter: Storage;
export declare class LocalStorage extends BrowserStorage {
constructor(config?: Omit<StorageConfig, "adapter">);
}
export declare class SessionStorage extends AbstractStorage {
readonly adapter: Storage;
export declare class SessionStorage extends BrowserStorage {
constructor(config?: Omit<StorageConfig, "adapter">);
}
export declare class MemoryStorageProvider implements Adapter {
private storage;
clear(): void;
getItem(key: string): string | null;
removeItem(key: string): void;
setItem(key: string, value: string): void;
}

@@ -1,31 +0,3 @@

export class MemoryStorageProvider {
constructor() {
Object.defineProperty(this, "storage", {
enumerable: true,
configurable: true,
writable: true,
value: new Map()
});
}
clear() {
this.storage.clear();
}
getItem(key) {
return this.storage.get(key) ?? null;
}
key(_) {
return null;
}
removeItem(key) {
this.storage.delete(key);
}
setItem(key, value) {
this.storage.set(key, value);
}
get length() {
return this.storage.size;
}
}
export class AbstractStorage {
constructor({ prefix = "", serializer = JSON } = {}) {
export class BrowserStorage {
constructor(config = {}) {
Object.defineProperty(this, "adapter", {

@@ -35,3 +7,3 @@ enumerable: true,

writable: true,
value: new MemoryStorageProvider()
value: void 0
});

@@ -50,4 +22,5 @@ Object.defineProperty(this, "prefix", {

});
this.prefix = prefix;
this.serializer = serializer;
this.adapter = config.adapter ?? new MemoryStorageProvider();
this.prefix = config.prefix ?? "";
this.serializer = config.serializer ?? JSON;
}

@@ -62,3 +35,3 @@ get(key) {

}
catch (e) { }
catch { }
return false;

@@ -90,27 +63,38 @@ }

}
catch (e) { }
catch (e) {
}
return item ?? null;
}
}
export class LocalStorage extends AbstractStorage {
constructor() {
super(...arguments);
Object.defineProperty(this, "adapter", {
enumerable: true,
configurable: true,
writable: true,
value: globalThis.localStorage
});
export class LocalStorage extends BrowserStorage {
constructor(config = {}) {
super({ ...config, adapter: globalThis.localStorage });
}
}
export class SessionStorage extends AbstractStorage {
export class SessionStorage extends BrowserStorage {
constructor(config = {}) {
super({ ...config, adapter: globalThis.sessionStorage });
}
}
export class MemoryStorageProvider {
constructor() {
super(...arguments);
Object.defineProperty(this, "adapter", {
Object.defineProperty(this, "storage", {
enumerable: true,
configurable: true,
writable: true,
value: globalThis.sessionStorage
value: new Map()
});
}
clear() {
this.storage.clear();
}
getItem(key) {
return this.storage.get(key) ?? null;
}
removeItem(key) {
this.storage.delete(key);
}
setItem(key, value) {
this.storage.set(key, value);
}
}

@@ -5,3 +5,3 @@ {

"name": "@jmondi/browser-storage",
"version": "1.3.0",
"version": "1.4.0",
"description": "Utilities for local and session browser storage.",

@@ -8,0 +8,0 @@ "keywords": [

@@ -1,2 +0,2 @@

# DateDuration
# @jmondi/browser-storage

@@ -20,3 +20,7 @@ An abstracted storage library for **browser** applications that interfaces with localStorage, sessionStorage, in-memory storage, or any custom serializer. It provides serialization capabilities with optional key prefixing for better storage management.

```ts
import { DateDuration } from "https://deno.land/x/browser_storage";
import {
LocaleStorage,
SessionStorage,
BrowserStorage
} from "https://deno.land/x/browser_storage"
```

@@ -73,6 +77,17 @@

```ts
const storage = new LocalStorage({
import { BrowserStorage } from "./index.ts";
const localStorage = new LocalStorage({
prefix: 'app_', // Optional. Defaults to "".
serializer: JSON // Optional. Defaults to JSON.
serializer: JSON, // Optional. Defaults to JSON.
});
const sessionStorage = new SessionStorage({
prefix: 'app_', // Optional. Defaults to "".
serializer: JSON, // Optional. Defaults to JSON.
});
const browserStorage = new BrowserStorage({
prefix: 'app_', // Optional. Defaults to "".
serializer: JSON, // Optional. Defaults to JSON.
adapter: Adapter, // Optional. Defaults to an InMemoryStorageProvider.
});
```

@@ -82,33 +97,29 @@

Underneath, both `LocalStorage` and `SessionStorage` extend the `AbstractStorage` class, which operates over an arbitrary storage adapter. This design enables you to extend `AbstractStorage` to interface with any custom storage provider of your choice.
The BrowserStorage class gives you the option to use a custom storage adapter.
Underneath, both `LocalStorage` and `SessionStorage` extend the `BrowserStorage` class, which operates over an arbitrary storage adapter. This design enables you to extend `BrowserStorage` to interface with any custom storage provider of your choice.
For a custom storage provider to work correctly, it needs to adhere to the browser's [Storage interface](https://developer.mozilla.org/en-US/docs/Web/API/Storage) – that is, it must implement methods such as `getItem`, `setItem`, `removeItem`, and `clear`, along with the `length` property. As an example, the provided `MemoryStorageProvider` class is a valid storage provider that stores data in an in-memory JavaScript map.
```ts
export class MemoryStorageProvider implements Storage {
private storage = new Map<string, string | null>();
import { type Adapter } from "@jmondi/browser-storage";
import Cookies from "js-cookie";
export class CookieStorage implements Adapter {
clear(): void {
this.storage.clear();
Cookies.clear();
}
getItem(key: string): string | null {
return this.storage.get(key) ?? null;
return Cookies.get(key) ?? null;
}
key(_: number): string | null {
return null;
}
removeItem(key: string): void {
this.storage.delete(key);
Cookies.remove(key);
}
setItem(key: string, value: string): void {
this.storage.set(key, value);
Cookies.set(key, value);
}
get length() {
return this.storage.size;
}
}

@@ -115,0 +126,0 @@ ```

@@ -1,23 +0,16 @@

export interface StorageSerializer {
export type StorageSerializer = {
parse<T = unknown>(value: string): T;
stringify<T = unknown>(value: T): string;
}
};
export type Adapter = Pick<Storage, "clear" | "getItem" | "removeItem" | "setItem">;
export type StorageConfig = {
prefix?: string;
serializer?: StorageSerializer;
adapter?: Adapter;
};
export declare class MemoryStorageProvider implements Storage {
private storage;
clear(): void;
getItem(key: string): string | null;
key(_: number): string | null;
removeItem(key: string): void;
setItem(key: string, value: string): void;
get length(): number;
}
export declare abstract class AbstractStorage {
readonly adapter: Storage;
export declare class BrowserStorage {
readonly adapter: Adapter;
readonly prefix: string;
readonly serializer: StorageSerializer;
constructor({ prefix, serializer }?: StorageConfig);
constructor(config?: StorageConfig);
get<T>(key: string): T | null;

@@ -30,7 +23,14 @@ set(key: string, value?: unknown): boolean;

}
export declare class LocalStorage extends AbstractStorage {
readonly adapter: Storage;
export declare class LocalStorage extends BrowserStorage {
constructor(config?: Omit<StorageConfig, "adapter">);
}
export declare class SessionStorage extends AbstractStorage {
readonly adapter: Storage;
export declare class SessionStorage extends BrowserStorage {
constructor(config?: Omit<StorageConfig, "adapter">);
}
export declare class MemoryStorageProvider implements Adapter {
private storage;
clear(): void;
getItem(key: string): string | null;
removeItem(key: string): void;
setItem(key: string, value: string): void;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SessionStorage = exports.LocalStorage = exports.AbstractStorage = exports.MemoryStorageProvider = void 0;
class MemoryStorageProvider {
constructor() {
Object.defineProperty(this, "storage", {
enumerable: true,
configurable: true,
writable: true,
value: new Map()
});
}
clear() {
this.storage.clear();
}
getItem(key) {
return this.storage.get(key) ?? null;
}
key(_) {
return null;
}
removeItem(key) {
this.storage.delete(key);
}
setItem(key, value) {
this.storage.set(key, value);
}
get length() {
return this.storage.size;
}
}
exports.MemoryStorageProvider = MemoryStorageProvider;
class AbstractStorage {
constructor({ prefix = "", serializer = JSON } = {}) {
exports.MemoryStorageProvider = exports.SessionStorage = exports.LocalStorage = exports.BrowserStorage = void 0;
class BrowserStorage {
constructor(config = {}) {
Object.defineProperty(this, "adapter", {

@@ -39,3 +10,3 @@ enumerable: true,

writable: true,
value: new MemoryStorageProvider()
value: void 0
});

@@ -54,4 +25,5 @@ Object.defineProperty(this, "prefix", {

});
this.prefix = prefix;
this.serializer = serializer;
this.adapter = config.adapter ?? new MemoryStorageProvider();
this.prefix = config.prefix ?? "";
this.serializer = config.serializer ?? JSON;
}

@@ -66,3 +38,3 @@ get(key) {

}
catch (e) { }
catch { }
return false;

@@ -94,30 +66,42 @@ }

}
catch (e) { }
catch (e) {
}
return item ?? null;
}
}
exports.AbstractStorage = AbstractStorage;
class LocalStorage extends AbstractStorage {
constructor() {
super(...arguments);
Object.defineProperty(this, "adapter", {
enumerable: true,
configurable: true,
writable: true,
value: globalThis.localStorage
});
exports.BrowserStorage = BrowserStorage;
class LocalStorage extends BrowserStorage {
constructor(config = {}) {
super({ ...config, adapter: globalThis.localStorage });
}
}
exports.LocalStorage = LocalStorage;
class SessionStorage extends AbstractStorage {
class SessionStorage extends BrowserStorage {
constructor(config = {}) {
super({ ...config, adapter: globalThis.sessionStorage });
}
}
exports.SessionStorage = SessionStorage;
class MemoryStorageProvider {
constructor() {
super(...arguments);
Object.defineProperty(this, "adapter", {
Object.defineProperty(this, "storage", {
enumerable: true,
configurable: true,
writable: true,
value: globalThis.sessionStorage
value: new Map()
});
}
clear() {
this.storage.clear();
}
getItem(key) {
return this.storage.get(key) ?? null;
}
removeItem(key) {
this.storage.delete(key);
}
setItem(key, value) {
this.storage.set(key, value);
}
}
exports.SessionStorage = SessionStorage;
exports.MemoryStorageProvider = MemoryStorageProvider;
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