@soham20/smart-offline-sdk 0.2.1 โ 1.0.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 +201 -35
- package/package.json +4 -3
- package/src/SmartOfflineSetup.ts +658 -0
- package/src/SmartOfflineTestUtils.ts +578 -0
- package/src/index.cjs +576 -0
- package/src/index.cjs.js +563 -97
- package/src/index.d.ts +320 -52
- package/src/index.js +781 -124
package/README.md
CHANGED
|
@@ -1,63 +1,229 @@
|
|
|
1
|
-
#
|
|
1
|
+
# SmartOffline SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A complete, reliable offline-first caching SDK for web applications. Provides intelligent priority-based caching with configurable algorithms.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Features
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
- ๐ **Easy Setup** - Single function call to initialize
|
|
8
|
+
- ๐ฏ **Priority-Based Caching** - Intelligent frequency and recency scoring
|
|
9
|
+
- ๐ **Network Aware** - Adapts caching strategy based on connection quality
|
|
10
|
+
- ๐ **Usage Tracking** - Tracks access patterns to optimize caching
|
|
11
|
+
- ๐ง **Highly Configurable** - Customize every aspect of the caching algorithm
|
|
12
|
+
- ๐ **TypeScript Support** - Full type definitions included
|
|
13
|
+
- ๐งช **Test Utilities** - Built-in testing and inspection tools
|
|
9
14
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
});
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @soham20/smart-offline-sdk
|
|
15
19
|
```
|
|
16
20
|
|
|
17
|
-
|
|
21
|
+
Or directly from GitHub:
|
|
18
22
|
|
|
19
|
-
|
|
23
|
+
```bash
|
|
24
|
+
npm install git+https://github.com/OwaisShaikh1/Hackvision2026.git
|
|
25
|
+
```
|
|
20
26
|
|
|
21
|
-
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
import { setupSmartOffline } from "@soham20/smart-offline-sdk";
|
|
22
31
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
32
|
+
// Initialize early in your app
|
|
33
|
+
await setupSmartOffline({
|
|
34
|
+
pages: ["/dashboard/*", "/products/*"],
|
|
35
|
+
apis: ["/api/v1/*"],
|
|
36
|
+
debug: true,
|
|
37
|
+
});
|
|
38
|
+
```
|
|
30
39
|
|
|
31
|
-
|
|
40
|
+
## Configuration Options
|
|
41
|
+
|
|
42
|
+
| Option | Type | Default | Description |
|
|
43
|
+
| -------------------- | --------------------------------- | ------------------------ | ------------------------------------------------------- |
|
|
44
|
+
| `pages` | `string[]` | `[]` | URL patterns for pages to cache (supports `*` wildcard) |
|
|
45
|
+
| `apis` | `string[]` | `[]` | URL patterns for API endpoints to cache |
|
|
46
|
+
| `debug` | `boolean` | `false` | Enable debug logging in console |
|
|
47
|
+
| `frequencyThreshold` | `number` | `3` | Number of accesses before URL is high priority |
|
|
48
|
+
| `recencyThreshold` | `number` | `86400000` | Time in ms for "recent" access (default: 24h) |
|
|
49
|
+
| `maxResourceSize` | `number` | `10485760` | Max bytes to cache per resource (default: 10MB) |
|
|
50
|
+
| `networkQuality` | `'auto' \| 'slow' \| 'fast'` | `'auto'` | Network quality detection mode |
|
|
51
|
+
| `significance` | `Record<string, 'high' \| 'low'>` | `{}` | Priority overrides for URL patterns |
|
|
52
|
+
| `weights` | `{ frequency, recency, size }` | `{ 1, 1, 1 }` | Weights for priority calculation |
|
|
53
|
+
| `customPriorityFn` | `Function` | `null` | Custom priority function (0-100) |
|
|
54
|
+
| `enableDetailedLogs` | `boolean` | `false` | Enable detailed event logging |
|
|
55
|
+
| `onCacheEvent` | `Function` | `undefined` | Callback for cache events |
|
|
56
|
+
| `serviceWorkerPath` | `string` | `'/smart-offline-sw.js'` | Path to service worker file |
|
|
57
|
+
| `serviceWorkerScope` | `string` | `'/'` | Service worker scope |
|
|
58
|
+
|
|
59
|
+
## Complete Example
|
|
32
60
|
|
|
33
61
|
```javascript
|
|
34
|
-
SmartOffline
|
|
35
|
-
|
|
36
|
-
|
|
62
|
+
import { setupSmartOffline, SmartOffline } from "@soham20/smart-offline-sdk";
|
|
63
|
+
|
|
64
|
+
// Full configuration example
|
|
65
|
+
const result = await setupSmartOffline({
|
|
66
|
+
// URL patterns to cache
|
|
67
|
+
pages: ["/admin/*", "/dashboard", "/products/*"],
|
|
68
|
+
apis: ["/api/v1/*", "/graphql"],
|
|
69
|
+
|
|
70
|
+
// Enable debug logging
|
|
37
71
|
debug: true,
|
|
38
72
|
|
|
39
73
|
// Priority tuning
|
|
40
|
-
frequencyThreshold: 5,
|
|
74
|
+
frequencyThreshold: 5, // 5 accesses = high priority
|
|
41
75
|
recencyThreshold: 12 * 60 * 60 * 1000, // 12 hours
|
|
42
|
-
maxResourceSize:
|
|
43
|
-
|
|
76
|
+
maxResourceSize: 5 * 1024 * 1024, // 5MB max
|
|
77
|
+
|
|
78
|
+
// Network detection
|
|
79
|
+
networkQuality: "auto",
|
|
80
|
+
|
|
81
|
+
// Manual priority overrides
|
|
44
82
|
significance: {
|
|
45
|
-
"/api/critical": "high",
|
|
46
|
-
"/api/
|
|
83
|
+
"/api/critical/*": "high", // Always cache
|
|
84
|
+
"/api/logs/*": "low", // Never cache
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
// Weight configuration
|
|
88
|
+
weights: {
|
|
89
|
+
frequency: 2, // Frequency is 2x more important
|
|
90
|
+
recency: 1,
|
|
91
|
+
size: 1,
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
// Custom priority function
|
|
95
|
+
customPriorityFn: (usage, url, config) => {
|
|
96
|
+
if (url.includes("vip")) return 100;
|
|
97
|
+
if (!usage) return 0;
|
|
98
|
+
return usage.count >= 5 ? 100 : 25;
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
// Event callback
|
|
102
|
+
onCacheEvent: (event) => {
|
|
103
|
+
console.log(`Cache event: ${event.type}`, event.url);
|
|
47
104
|
},
|
|
48
105
|
});
|
|
106
|
+
|
|
107
|
+
if (result.success) {
|
|
108
|
+
console.log("SmartOffline ready!");
|
|
109
|
+
}
|
|
49
110
|
```
|
|
50
111
|
|
|
51
|
-
##
|
|
112
|
+
## API Reference
|
|
52
113
|
|
|
53
|
-
###
|
|
114
|
+
### Main Functions
|
|
54
115
|
|
|
55
|
-
```
|
|
56
|
-
|
|
116
|
+
```javascript
|
|
117
|
+
import {
|
|
118
|
+
setupSmartOffline, // Initialize SDK (async)
|
|
119
|
+
updateConfig, // Update config at runtime
|
|
120
|
+
getConfig, // Get current configuration
|
|
121
|
+
isSmartOfflineReady, // Check if initialized
|
|
122
|
+
clearAllCache, // Clear all cached data
|
|
123
|
+
getCacheStats, // Get cache statistics
|
|
124
|
+
forceUpdate, // Force SW update
|
|
125
|
+
uninstall, // Uninstall SDK
|
|
126
|
+
} from "@soham20/smart-offline-sdk";
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Event Handling
|
|
130
|
+
|
|
131
|
+
```javascript
|
|
132
|
+
import { on, off } from "@soham20/smart-offline-sdk";
|
|
133
|
+
|
|
134
|
+
// Listen for cache events
|
|
135
|
+
on("cache", (event) => console.log("Cached:", event.url));
|
|
136
|
+
on("skip", (event) => console.log("Skipped:", event.url));
|
|
137
|
+
on("serve", (event) => console.log("Served from cache:", event.url));
|
|
138
|
+
on("error", (event) => console.log("Error:", event.url));
|
|
139
|
+
|
|
140
|
+
// Remove listener
|
|
141
|
+
off("cache", myCallback);
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### SmartOffline Object
|
|
145
|
+
|
|
146
|
+
```javascript
|
|
147
|
+
import { SmartOffline } from "@soham20/smart-offline-sdk";
|
|
148
|
+
|
|
149
|
+
// All methods available on SmartOffline object
|
|
150
|
+
SmartOffline.setup(config); // setupSmartOffline
|
|
151
|
+
SmartOffline.updateConfig(cfg); // Update configuration
|
|
152
|
+
SmartOffline.getConfig(); // Get current config
|
|
153
|
+
SmartOffline.isReady(); // Check if ready
|
|
154
|
+
SmartOffline.clearCache(); // Clear all cache
|
|
155
|
+
SmartOffline.getStats(); // Get cache stats
|
|
156
|
+
SmartOffline.forceUpdate(); // Force SW update
|
|
157
|
+
SmartOffline.uninstall(); // Clean uninstall
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Test Utilities
|
|
161
|
+
|
|
162
|
+
```javascript
|
|
163
|
+
import {
|
|
164
|
+
SmartOfflineTestSuite,
|
|
165
|
+
runSmartOfflineTests,
|
|
166
|
+
CacheInspector,
|
|
167
|
+
} from "@soham20/smart-offline-sdk";
|
|
168
|
+
|
|
169
|
+
// Run all tests
|
|
170
|
+
const results = await runSmartOfflineTests();
|
|
171
|
+
|
|
172
|
+
// Or use the test suite directly
|
|
173
|
+
const suite = new SmartOfflineTestSuite({ pages: ["/test/*"] });
|
|
174
|
+
await suite.runAll();
|
|
175
|
+
suite.printResults();
|
|
176
|
+
|
|
177
|
+
// Inspect cache
|
|
178
|
+
const inspector = new CacheInspector();
|
|
179
|
+
await inspector.showAll();
|
|
180
|
+
const data = await inspector.getAllData();
|
|
57
181
|
```
|
|
58
182
|
|
|
59
|
-
|
|
183
|
+
## Browser Console Testing
|
|
184
|
+
|
|
185
|
+
```javascript
|
|
186
|
+
// After SDK is loaded, these are available globally:
|
|
187
|
+
await runSmartOfflineTests();
|
|
188
|
+
await SmartOffline.getStats();
|
|
189
|
+
new CacheInspector().showAll();
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## How the Algorithm Works
|
|
193
|
+
|
|
194
|
+
1. **URL Pattern Matching**: Caches pages/APIs matching configured patterns
|
|
195
|
+
2. **Frequency Scoring**: URLs accessed >= `frequencyThreshold` times get higher score
|
|
196
|
+
3. **Recency Scoring**: URLs accessed within `recencyThreshold` get higher score
|
|
197
|
+
4. **Weighted Calculation**:
|
|
198
|
+
- `frequencyScore = min(100, (accessCount / threshold) * 100)`
|
|
199
|
+
- `recencyScore = max(0, 100 - (timeSinceAccess / threshold) * 100)`
|
|
200
|
+
- `weightedScore = (freqScore * freqWeight + recencyScore * recencyWeight) / totalWeight`
|
|
201
|
+
- HIGH priority if `weightedScore > 50`
|
|
202
|
+
5. **Network Awareness**: On slow networks, only HIGH priority resources are cached
|
|
203
|
+
6. **Size Limits**: Resources exceeding `maxResourceSize` are skipped
|
|
204
|
+
|
|
205
|
+
## Service Worker Setup
|
|
206
|
+
|
|
207
|
+
Copy `smart-offline-sw.js` to your public directory:
|
|
60
208
|
|
|
61
209
|
```bash
|
|
62
|
-
|
|
210
|
+
cp node_modules/@soham20/smart-offline-sdk/smart-offline-sw.js public/
|
|
63
211
|
```
|
|
212
|
+
|
|
213
|
+
## TypeScript
|
|
214
|
+
|
|
215
|
+
Full TypeScript support with exported types:
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
import type {
|
|
219
|
+
SmartOfflineConfig,
|
|
220
|
+
CacheEvent,
|
|
221
|
+
UsageData,
|
|
222
|
+
SetupResult,
|
|
223
|
+
CacheStats,
|
|
224
|
+
} from "@soham20/smart-offline-sdk";
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## License
|
|
228
|
+
|
|
229
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soham20/smart-offline-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Smart offline-first JavaScript SDK with intelligent caching for web applications",
|
|
5
|
-
"main": "./src/index.cjs
|
|
5
|
+
"main": "./src/index.cjs",
|
|
6
6
|
"module": "./src/index.js",
|
|
7
7
|
"browser": "./src/index.js",
|
|
8
8
|
"types": "./src/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"import": "./src/index.js",
|
|
12
|
-
"require": "./src/index.cjs
|
|
12
|
+
"require": "./src/index.cjs",
|
|
13
13
|
"browser": "./src/index.js",
|
|
14
|
+
"types": "./src/index.d.ts",
|
|
14
15
|
"default": "./src/index.js"
|
|
15
16
|
},
|
|
16
17
|
"./client": {
|