@drakkar.software/sunglasses-storage-localstorage 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ import { IStorageAdapter } from '@drakkar.software/sunglasses-core';
2
+
3
+ /**
4
+ * IStorageAdapter implementation using the browser's localStorage API.
5
+ *
6
+ * Keys are namespaced with a configurable prefix (default: 'sg_') to avoid
7
+ * collisions with other libraries or application data.
8
+ *
9
+ * Works in:
10
+ * - All modern browsers
11
+ * - Node.js environments that polyfill globalThis.localStorage
12
+ */
13
+ declare class LocalStorageAdapter implements IStorageAdapter {
14
+ private readonly prefix;
15
+ constructor(prefix?: string);
16
+ read(key: string): Promise<string | null>;
17
+ write(key: string, value: string): Promise<void>;
18
+ delete(key: string): Promise<void>;
19
+ private prefixed;
20
+ }
21
+
22
+ export { LocalStorageAdapter };
@@ -0,0 +1,22 @@
1
+ import { IStorageAdapter } from '@drakkar.software/sunglasses-core';
2
+
3
+ /**
4
+ * IStorageAdapter implementation using the browser's localStorage API.
5
+ *
6
+ * Keys are namespaced with a configurable prefix (default: 'sg_') to avoid
7
+ * collisions with other libraries or application data.
8
+ *
9
+ * Works in:
10
+ * - All modern browsers
11
+ * - Node.js environments that polyfill globalThis.localStorage
12
+ */
13
+ declare class LocalStorageAdapter implements IStorageAdapter {
14
+ private readonly prefix;
15
+ constructor(prefix?: string);
16
+ read(key: string): Promise<string | null>;
17
+ write(key: string, value: string): Promise<void>;
18
+ delete(key: string): Promise<void>;
19
+ private prefixed;
20
+ }
21
+
22
+ export { LocalStorageAdapter };
package/dist/index.js ADDED
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ LocalStorageAdapter: () => LocalStorageAdapter
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/LocalStorageAdapter.ts
28
+ var LocalStorageAdapter = class {
29
+ constructor(prefix = "sg_") {
30
+ this.prefix = prefix;
31
+ }
32
+ async read(key) {
33
+ try {
34
+ return globalThis.localStorage.getItem(this.prefixed(key));
35
+ } catch {
36
+ return null;
37
+ }
38
+ }
39
+ async write(key, value) {
40
+ try {
41
+ globalThis.localStorage.setItem(this.prefixed(key), value);
42
+ } catch (err) {
43
+ if (err instanceof Error && err.name === "QuotaExceededError") {
44
+ console.warn("[SunGlasses] LocalStorageAdapter: storage quota exceeded \u2014 event data may not persist");
45
+ }
46
+ }
47
+ }
48
+ async delete(key) {
49
+ try {
50
+ globalThis.localStorage.removeItem(this.prefixed(key));
51
+ } catch {
52
+ }
53
+ }
54
+ prefixed(key) {
55
+ return `${this.prefix}${key}`;
56
+ }
57
+ };
58
+ // Annotate the CommonJS export names for ESM import in node:
59
+ 0 && (module.exports = {
60
+ LocalStorageAdapter
61
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,34 @@
1
+ // src/LocalStorageAdapter.ts
2
+ var LocalStorageAdapter = class {
3
+ constructor(prefix = "sg_") {
4
+ this.prefix = prefix;
5
+ }
6
+ async read(key) {
7
+ try {
8
+ return globalThis.localStorage.getItem(this.prefixed(key));
9
+ } catch {
10
+ return null;
11
+ }
12
+ }
13
+ async write(key, value) {
14
+ try {
15
+ globalThis.localStorage.setItem(this.prefixed(key), value);
16
+ } catch (err) {
17
+ if (err instanceof Error && err.name === "QuotaExceededError") {
18
+ console.warn("[SunGlasses] LocalStorageAdapter: storage quota exceeded \u2014 event data may not persist");
19
+ }
20
+ }
21
+ }
22
+ async delete(key) {
23
+ try {
24
+ globalThis.localStorage.removeItem(this.prefixed(key));
25
+ } catch {
26
+ }
27
+ }
28
+ prefixed(key) {
29
+ return `${this.prefix}${key}`;
30
+ }
31
+ };
32
+ export {
33
+ LocalStorageAdapter
34
+ };
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@drakkar.software/sunglasses-storage-localstorage",
3
+ "version": "0.1.0",
4
+ "description": "localStorage storage adapter for SunGlasses (web)",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "dependencies": {
19
+ "@drakkar.software/sunglasses-core": "0.2.0"
20
+ },
21
+ "devDependencies": {
22
+ "tsup": "^8.3.5",
23
+ "typescript": "^5.7.2",
24
+ "vitest": "^2.1.8",
25
+ "@drakkar.software/sunglasses-tsconfig": "0.1.0"
26
+ },
27
+ "scripts": {
28
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
29
+ "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
30
+ "typecheck": "tsc --noEmit",
31
+ "lint": "eslint src/",
32
+ "test": "vitest run",
33
+ "clean": "rm -rf dist .tsbuildinfo"
34
+ }
35
+ }