memorio 4.1.6 → 4.2.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.
- package/README.md +16 -57
- package/docs/README.md +16 -57
- package/docs/markdown/PLATFORM.md +10 -9
- package/docs/markdown/SECURITY.md +15 -10
- package/index.cjs +436 -385
- package/index.js +436 -385
- package/package.json +1 -1
- package/types/memorio.d.ts +1 -0
package/README.md
CHANGED
|
@@ -59,12 +59,24 @@ Just import it and start storing.
|
|
|
59
59
|
|
|
60
60
|
---
|
|
61
61
|
|
|
62
|
-
##
|
|
62
|
+
## Installation options
|
|
63
63
|
|
|
64
64
|
```bash
|
|
65
|
+
# npm
|
|
65
66
|
npm i memorio
|
|
67
|
+
|
|
68
|
+
# pnpm
|
|
69
|
+
pnpm add memorio
|
|
70
|
+
|
|
71
|
+
# yarn
|
|
72
|
+
yarn add memorio
|
|
73
|
+
|
|
74
|
+
# React peer dep (optional, React ≥ 16.8)
|
|
75
|
+
npm i react react-dom
|
|
66
76
|
```
|
|
67
77
|
|
|
78
|
+
## **Setup**
|
|
79
|
+
|
|
68
80
|
```typescript
|
|
69
81
|
// memorio/index.ts — import once at your app entry point
|
|
70
82
|
import 'memorio'
|
|
@@ -113,7 +125,7 @@ state.lock() // freeze everything
|
|
|
113
125
|
state.unlock() // unfreeze
|
|
114
126
|
```
|
|
115
127
|
|
|
116
|
-
### `store` —
|
|
128
|
+
### `store` — survives refresh
|
|
117
129
|
|
|
118
130
|
```javascript
|
|
119
131
|
store.set('preferences', { theme: 'dark' })
|
|
@@ -124,7 +136,7 @@ console.debug(store.size(), 'chars stored')
|
|
|
124
136
|
console.debug(store.isPersistent) // true → real localStorage
|
|
125
137
|
```
|
|
126
138
|
|
|
127
|
-
### `session` —
|
|
139
|
+
### `session` — dies with tab
|
|
128
140
|
|
|
129
141
|
```javascript
|
|
130
142
|
session.set('token', 'user-abc-123')
|
|
@@ -140,7 +152,7 @@ const result = cache.get('temp') // undefined or the value
|
|
|
140
152
|
cache.clear() // empty it all
|
|
141
153
|
```
|
|
142
154
|
|
|
143
|
-
### `idb` —
|
|
155
|
+
### `idb` — structured & typed
|
|
144
156
|
|
|
145
157
|
```javascript
|
|
146
158
|
await idb.db.create('my-db')
|
|
@@ -236,20 +248,6 @@ Memorio runs in every JavaScript environment, with automatic fallbacks.
|
|
|
236
248
|
|
|
237
249
|
> **Why memory fallbacks on the server?** There is no browser. `store` and `session` gracefully fall back to `Map`. You still get the same API. Same `state`, same `cache`, same `useObserver`. No extra config required.
|
|
238
250
|
|
|
239
|
-
### Platform detection
|
|
240
|
-
|
|
241
|
-
Access via `memorio.*` after `import 'memorio'`:
|
|
242
|
-
|
|
243
|
-
```javascript
|
|
244
|
-
memorio.isBrowser() // true in Chrome, Firefox, Safari
|
|
245
|
-
memorio.isNode() // true in Node.js
|
|
246
|
-
memorio.isDeno() // true in Deno
|
|
247
|
-
memorio.isEdge() // true in Cloudflare Workers, Vercel Edge
|
|
248
|
-
|
|
249
|
-
const c = memorio.getCapabilities()
|
|
250
|
-
// { platform: 'browser', hasLocalStorage: true, hasIndexedDB: true, ... }
|
|
251
|
-
```
|
|
252
|
-
|
|
253
251
|
---
|
|
254
252
|
|
|
255
253
|
## Security
|
|
@@ -263,45 +261,6 @@ const c = memorio.getCapabilities()
|
|
|
263
261
|
|
|
264
262
|
---
|
|
265
263
|
|
|
266
|
-
## Installation options
|
|
267
|
-
|
|
268
|
-
```bash
|
|
269
|
-
# npm
|
|
270
|
-
npm i memorio
|
|
271
|
-
|
|
272
|
-
# pnpm
|
|
273
|
-
pnpm add memorio
|
|
274
|
-
|
|
275
|
-
# yarn
|
|
276
|
-
yarn add memorio
|
|
277
|
-
|
|
278
|
-
# React peer dep (optional, React ≥ 16.8)
|
|
279
|
-
npm i react react-dom
|
|
280
|
-
```
|
|
281
|
-
|
|
282
|
-
---
|
|
283
|
-
|
|
284
|
-
## Testing
|
|
285
|
-
|
|
286
|
-
```
|
|
287
|
-
95 tests · 8 suites · all passing
|
|
288
|
-
|
|
289
|
-
basic | state | store | session | cache | idb | observer | useObserver
|
|
290
|
-
```
|
|
291
|
-
|
|
292
|
-
| Suite | Tests | Status |
|
|
293
|
-
|-------|-------|--------|
|
|
294
|
-
| Basic | 7 | ✅ |
|
|
295
|
-
| State | 24 | ✅ |
|
|
296
|
-
| Store | 17 | ✅ |
|
|
297
|
-
| Session | 12 | ✅ |
|
|
298
|
-
| IDB | 7 | ✅ |
|
|
299
|
-
| Cache | 5 | ✅ |
|
|
300
|
-
| Observer | 12 | ✅ |
|
|
301
|
-
| useObserver | 12 | ✅ |
|
|
302
|
-
|
|
303
|
-
---
|
|
304
|
-
|
|
305
264
|
## License
|
|
306
265
|
|
|
307
266
|
MIT © [Dario Passariello](https://dario.passariello.ca)
|
package/docs/README.md
CHANGED
|
@@ -59,12 +59,24 @@ Just import it and start storing.
|
|
|
59
59
|
|
|
60
60
|
---
|
|
61
61
|
|
|
62
|
-
##
|
|
62
|
+
## Installation options
|
|
63
63
|
|
|
64
64
|
```bash
|
|
65
|
+
# npm
|
|
65
66
|
npm i memorio
|
|
67
|
+
|
|
68
|
+
# pnpm
|
|
69
|
+
pnpm add memorio
|
|
70
|
+
|
|
71
|
+
# yarn
|
|
72
|
+
yarn add memorio
|
|
73
|
+
|
|
74
|
+
# React peer dep (optional, React ≥ 16.8)
|
|
75
|
+
npm i react react-dom
|
|
66
76
|
```
|
|
67
77
|
|
|
78
|
+
## **Setup**
|
|
79
|
+
|
|
68
80
|
```typescript
|
|
69
81
|
// memorio/index.ts — import once at your app entry point
|
|
70
82
|
import 'memorio'
|
|
@@ -113,7 +125,7 @@ state.lock() // freeze everything
|
|
|
113
125
|
state.unlock() // unfreeze
|
|
114
126
|
```
|
|
115
127
|
|
|
116
|
-
### `store` —
|
|
128
|
+
### `store` — survives refresh
|
|
117
129
|
|
|
118
130
|
```javascript
|
|
119
131
|
store.set('preferences', { theme: 'dark' })
|
|
@@ -124,7 +136,7 @@ console.debug(store.size(), 'chars stored')
|
|
|
124
136
|
console.debug(store.isPersistent) // true → real localStorage
|
|
125
137
|
```
|
|
126
138
|
|
|
127
|
-
### `session` —
|
|
139
|
+
### `session` — dies with tab
|
|
128
140
|
|
|
129
141
|
```javascript
|
|
130
142
|
session.set('token', 'user-abc-123')
|
|
@@ -140,7 +152,7 @@ const result = cache.get('temp') // undefined or the value
|
|
|
140
152
|
cache.clear() // empty it all
|
|
141
153
|
```
|
|
142
154
|
|
|
143
|
-
### `idb` —
|
|
155
|
+
### `idb` — structured & typed
|
|
144
156
|
|
|
145
157
|
```javascript
|
|
146
158
|
await idb.db.create('my-db')
|
|
@@ -236,20 +248,6 @@ Memorio runs in every JavaScript environment, with automatic fallbacks.
|
|
|
236
248
|
|
|
237
249
|
> **Why memory fallbacks on the server?** There is no browser. `store` and `session` gracefully fall back to `Map`. You still get the same API. Same `state`, same `cache`, same `useObserver`. No extra config required.
|
|
238
250
|
|
|
239
|
-
### Platform detection
|
|
240
|
-
|
|
241
|
-
Access via `memorio.*` after `import 'memorio'`:
|
|
242
|
-
|
|
243
|
-
```javascript
|
|
244
|
-
memorio.isBrowser() // true in Chrome, Firefox, Safari
|
|
245
|
-
memorio.isNode() // true in Node.js
|
|
246
|
-
memorio.isDeno() // true in Deno
|
|
247
|
-
memorio.isEdge() // true in Cloudflare Workers, Vercel Edge
|
|
248
|
-
|
|
249
|
-
const c = memorio.getCapabilities()
|
|
250
|
-
// { platform: 'browser', hasLocalStorage: true, hasIndexedDB: true, ... }
|
|
251
|
-
```
|
|
252
|
-
|
|
253
251
|
---
|
|
254
252
|
|
|
255
253
|
## Security
|
|
@@ -263,45 +261,6 @@ const c = memorio.getCapabilities()
|
|
|
263
261
|
|
|
264
262
|
---
|
|
265
263
|
|
|
266
|
-
## Installation options
|
|
267
|
-
|
|
268
|
-
```bash
|
|
269
|
-
# npm
|
|
270
|
-
npm i memorio
|
|
271
|
-
|
|
272
|
-
# pnpm
|
|
273
|
-
pnpm add memorio
|
|
274
|
-
|
|
275
|
-
# yarn
|
|
276
|
-
yarn add memorio
|
|
277
|
-
|
|
278
|
-
# React peer dep (optional, React ≥ 16.8)
|
|
279
|
-
npm i react react-dom
|
|
280
|
-
```
|
|
281
|
-
|
|
282
|
-
---
|
|
283
|
-
|
|
284
|
-
## Testing
|
|
285
|
-
|
|
286
|
-
```
|
|
287
|
-
95 tests · 8 suites · all passing
|
|
288
|
-
|
|
289
|
-
basic | state | store | session | cache | idb | observer | useObserver
|
|
290
|
-
```
|
|
291
|
-
|
|
292
|
-
| Suite | Tests | Status |
|
|
293
|
-
|-------|-------|--------|
|
|
294
|
-
| Basic | 7 | ✅ |
|
|
295
|
-
| State | 24 | ✅ |
|
|
296
|
-
| Store | 17 | ✅ |
|
|
297
|
-
| Session | 12 | ✅ |
|
|
298
|
-
| IDB | 7 | ✅ |
|
|
299
|
-
| Cache | 5 | ✅ |
|
|
300
|
-
| Observer | 12 | ✅ |
|
|
301
|
-
| useObserver | 12 | ✅ |
|
|
302
|
-
|
|
303
|
-
---
|
|
304
|
-
|
|
305
264
|
## License
|
|
306
265
|
|
|
307
266
|
MIT © [Dario Passariello](https://dario.passariello.ca)
|
|
@@ -125,9 +125,8 @@ session.isPersistent; // false - data lost on restart
|
|
|
125
125
|
Each instance/session gets unique storage keys to prevent conflicts:
|
|
126
126
|
|
|
127
127
|
```javascript
|
|
128
|
-
//
|
|
129
|
-
//
|
|
130
|
-
// session: "memorio_session_[sessionId]_key"
|
|
128
|
+
// Without context: "memorio_store_[sessionId]_key"
|
|
129
|
+
// With context: "[contextName]-key"
|
|
131
130
|
```
|
|
132
131
|
|
|
133
132
|
This ensures:
|
|
@@ -146,7 +145,11 @@ For server-side applications handling multiple tenants (e.g., different users/re
|
|
|
146
145
|
|
|
147
146
|
```javascript
|
|
148
147
|
// Create isolated context for a user/session
|
|
149
|
-
const ctx = memorio.
|
|
148
|
+
const ctx = memorio.isolate('user-123');
|
|
149
|
+
|
|
150
|
+
// Keys in store/session are prefixed with context name
|
|
151
|
+
// store: "user-123-key"
|
|
152
|
+
// session: "user-123-key"
|
|
150
153
|
|
|
151
154
|
// Use context's isolated storage
|
|
152
155
|
ctx.state.user = { name: 'Isolated User' };
|
|
@@ -167,11 +170,10 @@ console.debug(contexts); // ['user-123', 'user-456', ...]
|
|
|
167
170
|
|
|
168
171
|
// Delete a context (cleanup)
|
|
169
172
|
memorio.deleteContext('user-123');
|
|
170
|
-
|
|
171
|
-
// Shorthand for createContext
|
|
172
|
-
const ctx2 = memorio.isolate('tenant-A');
|
|
173
173
|
```
|
|
174
174
|
|
|
175
|
+
> **Note**: `memorio.isolate('name')` is a shorthand alias for creating isolated contexts.
|
|
176
|
+
|
|
175
177
|
### Context Use Cases
|
|
176
178
|
|
|
177
179
|
#### 1. Per-Request Isolation (Express/Fastify)
|
|
@@ -246,10 +248,9 @@ Same as browser - localStorage and sessionStorage are available.
|
|
|
246
248
|
|
|
247
249
|
| Function | Returns | Description |
|
|
248
250
|
|----------|---------|-------------|
|
|
249
|
-
| `memorio.
|
|
251
|
+
| `memorio.isolate(name?)` | `Context` | Create isolated context |
|
|
250
252
|
| `memorio.listContexts()` | `string[]` | List all context IDs |
|
|
251
253
|
| `memorio.deleteContext(id)` | `boolean` | Delete a context |
|
|
252
|
-
| `memorio.isolate(name?)` | `Context` | Alias for createContext |
|
|
253
254
|
|
|
254
255
|
### Properties
|
|
255
256
|
|
|
@@ -108,18 +108,23 @@ Each session gets a unique namespace to prevent data leakage:
|
|
|
108
108
|
|
|
109
109
|
### Isolation Mechanism
|
|
110
110
|
|
|
111
|
-
| Component |
|
|
112
|
-
|
|
113
|
-
| Session ID | `crypto.randomUUID()` |
|
|
114
|
-
| Store Keys | `memorio_store_[uuid]
|
|
115
|
-
| Session Keys | `memorio_session_[uuid]
|
|
116
|
-
| State | In-memory (per-instance) |
|
|
111
|
+
| Component | Without Context | With Context |
|
|
112
|
+
|-----------|-----------------|--------------|
|
|
113
|
+
| Session ID | `crypto.randomUUID()` | Context name |
|
|
114
|
+
| Store Keys | `memorio_store_[uuid]-keyname` | `[contextName]-keyname` |
|
|
115
|
+
| Session Keys | `memorio_session_[uuid]-keyname` | `[contextName]-keyname` |
|
|
116
|
+
| State | In-memory (per-instance) | In-memory (per-instance) |
|
|
117
117
|
|
|
118
118
|
### Key Prefix Format
|
|
119
119
|
|
|
120
120
|
```
|
|
121
|
-
|
|
122
|
-
|
|
121
|
+
// Without context:
|
|
122
|
+
memorio_store-[session-uuid]-username
|
|
123
|
+
memorio_session-[session-uuid]-auth-token
|
|
124
|
+
|
|
125
|
+
// With context (createContext('user-123')):
|
|
126
|
+
user-123-username
|
|
127
|
+
user-123-auth-token
|
|
123
128
|
```
|
|
124
129
|
|
|
125
130
|
### Cross-Session Protection
|
|
@@ -138,8 +143,8 @@ For server-side applications, contexts provide complete data isolation:
|
|
|
138
143
|
|
|
139
144
|
```typescript
|
|
140
145
|
// Create isolated context per tenant
|
|
141
|
-
const tenantA = memorio.
|
|
142
|
-
const tenantB = memorio.
|
|
146
|
+
const tenantA = memorio.isolate('tenant-A')
|
|
147
|
+
const tenantB = memorio.isolate('tenant-B')
|
|
143
148
|
|
|
144
149
|
// Each context has completely separate storage
|
|
145
150
|
tenantA.state.secret = 'Tenant A data' // Isolated
|