Skip to content

Commit c7f813f

Browse files
committed
Remove noop storage fallback when Storage is not available
1 parent 7a7ea77 commit c7f813f

File tree

4 files changed

+132
-180
lines changed

4 files changed

+132
-180
lines changed

CHANGELOG.md

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,72 @@
11
# CHANGELOG
22

3-
## v3.0.0 (2025-06-XX)
3+
## v3.0.0 (2025-06-18)
44

55
### BREAKING CHANGES
66

7-
- The `WebStorage` module is now exclusively available as an ES module (ESM), aligning with the modern JavaScript module standard. Additionally, it is no longer the default export — you must import it using a named import.
8-
**v2.x.x**
9-
```js
10-
import WebStorage from '@georapbox/web-storage';
11-
```
12-
13-
**v3.x.x**
14-
```js
15-
import { WebStorage } from '@georapbox/web-storage';
16-
```
17-
- All API methods now return `[value, error]` tuple-like values instead of accepting error callbacks.
18-
This allows developers to handle errors in a clean, synchronous style without using `try/catch` or providing callbacks. For example:
19-
20-
**v2.x.x**
21-
```js
22-
const value = storage.getItem('key', value, (err) => {
23-
console.error(err);
24-
});
25-
```
26-
27-
**v3.x.x**
28-
```js
29-
const [value, error] = storage.getItem('key', value);
30-
31-
if (error) {
32-
console.error(error);
33-
}
34-
```
35-
- Removed support for error callback functions in all methods (`getItem`, `setItem`, `removeItem`, `clear`, `keys`, `length`, `iterate`). Errors must now be handled via the returned tuple.
36-
- Internal `_keyPrefix` and `_driver` fields are now private class fields (`#keyPrefix`, `#driver`). They are no longer accessible outside the class.
7+
#### Switch to ES modules and named exports
8+
9+
The `WebStorage` module is now exclusively available as an ES module (ESM), aligning with the modern JavaScript module standard. Additionally, it is no longer the default export — you must import it using a named import.
10+
11+
**v2.x.x**
12+
```js
13+
import WebStorage from '@georapbox/web-storage';
14+
```
15+
16+
**v3.x.x**
17+
```js
18+
import { WebStorage } from '@georapbox/web-storage';
19+
```
20+
21+
#### API methods return [value, error] tuples
3722

38-
## NEW FEATURES
23+
All API methods now return `[value, error]` tuple-like values instead of accepting error callbacks. This allows developers to handle errors in a clean, synchronous style without using `try/catch` or providing callbacks. For example:
3924

40-
- Export type declaration files (`.d.ts`) for TypeScript users, ensuring better type safety and autocompletion support in TypeScript projects.
25+
**v2.x.x**
26+
```js
27+
const value = storage.getItem('key', value, (err) => {
28+
console.error(err);
29+
});
30+
```
31+
32+
**v3.x.x**
33+
```js
34+
const [value, error] = storage.getItem('key', value);
35+
36+
if (error) {
37+
console.error(error);
38+
}
39+
```
40+
41+
#### Removed noop storage fallback
42+
43+
In previous versions, if `localStorage` or `sessionStorage` was unavailable (e.g., due to privacy settings or Safari private mode), a silent in-memory fallback was used that mimicked the Storage API. This allowed methods like `setItem()` to return success even though no actual data was stored.
44+
45+
This behavior has been removed to improve transparency and correctness. As of v3.0.0:
46+
47+
- No fallback is used.
48+
- Errors are captured and returned via the `[_, error]` tuple-like value.
49+
- Developers can use `WebStorage.isAvailable()` for feature detection, or gracefully handle errors based on method output.
50+
51+
This ensures failures are explicit and prevents false assumptions about persistence.
52+
53+
### NEW FEATURES
54+
55+
#### Type declarations for TypeScript
56+
57+
Export type declaration files (`.d.ts`) for TypeScript users, ensuring better type safety and autocompletion support in TypeScript projects.
4158

4259
### INTERNAL CHANGES
4360

44-
- Rewrite to use native class private fields.
45-
- Internal noopStorage fallback now fully conforms to the Storage interface.
61+
- Update Node.js version requirement and dev dependencies to the latest versions.
4662
- Drop Jest in favor of @web/test-runner and Playwright for testing.
4763
- Drop rollup in favor of esbuild for bundling.
48-
- Update Node.js version requirement to 22.x.x.
49-
- Update dependencies to their latest versions.
5064

5165
## v2.1.0 (2021-01-26)
5266

5367
- Generate minified versions for ESM and CommonJS exported bubdles.
5468

55-
### Internal changes
69+
### INTERNAL CHANGES
5670

5771
- Replace Mocha with Jest as testing framework.
5872
- Replace Travis CI with Github actions.
@@ -79,7 +93,7 @@
7993
- `WebStorage.isAvailable` static method, as of v2.x, accepts "localStorage" or "sessionStorage" strings as arguments.
8094
- On initialization the library **throws** if `driver` option is anything other than "localStorage" or "sessionStorage" and if `keyPrefix` option is not of type `String`.
8195

82-
### OTHER CHANGES
96+
### INTERNAL CHANGES
8397

8498
- Keep `devDependencies` up to date.
8599

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,7 @@ WebStorage.isAvailable('localStorage');
8585

8686
### setItem(key, value)
8787

88-
Saves an item to storage with the specified key. You can store items of any of the following data types as long as data can be serialized to JSON.
89-
90-
- String
91-
- Number
92-
- Array
93-
- Object
88+
Saves an item to storage with the specified key. Any value that can be serialized to JSON can be stored, including objects, arrays, strings, numbers, and booleans.
9489

9590
**Throws:** `TypeError` - Throws if `key` is not a string.
9691
**Returns:** `[boolean, Error | null]` - Returns an array with two elements: the first is `true` if the item was saved successfully, or `false` if it was not, and the second is `null` if no error occurred, or an `Error` object if an error occurred.

src/web-storage.js

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,12 @@ function removePrefix(str, prefix) {
4040
return str;
4141
}
4242

43-
/**
44-
* Creates a no-operation fallback implementation of the Web Storage API.
45-
*
46-
* This function returns an object that conforms to the `Storage` interface
47-
* but performs no actual storage operations. It is intended for use in
48-
* environments where `localStorage` or `sessionStorage` is unavailable or restricted.
49-
*
50-
* Behavior:
51-
* - `getItem` and `key` always return `null`
52-
* - `setItem`, `removeItem`, and `clear` do nothing and return `undefined`
53-
* - `length` is always `0`
54-
*
55-
* This allows safe use of the storage API without requiring availability checks or try/catch blocks.
56-
*
57-
* @returns {Storage} A noop-compatible storage object that safely mimics the `Storage` API
58-
*/
59-
function createNoopStorage() {
60-
return {
61-
getItem: () => null,
62-
setItem: () => undefined,
63-
removeItem: () => undefined,
64-
key: () => null,
65-
clear: () => undefined,
66-
length: 0
67-
};
68-
}
69-
7043
class WebStorage {
7144
/** @type {Storage} */
7245
#driver;
7346

7447
/** @type {string} */
75-
#keyPrefix;
48+
#keyPrefix = '';
7649

7750
/**
7851
* Creates a new instance of WebStorage.
@@ -93,7 +66,7 @@ class WebStorage {
9366
throw new TypeError('The "keyPrefix" option must be a string.');
9467
}
9568

96-
this.#driver = WebStorage.isAvailable(opts.driver) ? window[opts.driver] : createNoopStorage();
69+
this.#driver = window[opts.driver];
9770
this.#keyPrefix = opts.keyPrefix.trim();
9871
}
9972

0 commit comments

Comments
 (0)