@soham20/smart-offline-sdk 0.1.2 → 0.1.4

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/package.json CHANGED
@@ -1,17 +1,41 @@
1
1
  {
2
2
  "name": "@soham20/smart-offline-sdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Smart offline-first JavaScript SDK with intelligent caching for web applications",
5
- "main": "src/sdk/index.js",
5
+ "main": "./src/index.cjs.js",
6
+ "module": "./src/index.js",
7
+ "browser": "./src/index.js",
8
+ "types": "./src/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./src/index.js",
12
+ "require": "./src/index.cjs.js",
13
+ "browser": "./src/index.js",
14
+ "default": "./src/index.js"
15
+ },
16
+ "./client": {
17
+ "require": "./src/sdk/index.cjs"
18
+ }
19
+ },
20
+ "files": [
21
+ "src",
22
+ "smart-offline-sw.js"
23
+ ],
6
24
  "scripts": {
7
- "test": "jest",
25
+ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
8
26
  "build": "echo \"no build step\"",
9
27
  "lint": "echo \"no lint configured\""
10
28
  },
11
- "keywords": [],
29
+ "keywords": [
30
+ "offline",
31
+ "pwa",
32
+ "service-worker",
33
+ "cache",
34
+ "offline-first"
35
+ ],
12
36
  "author": "Atif Sayed",
13
37
  "license": "MIT",
14
- "type": "commonjs",
38
+ "type": "module",
15
39
  "devDependencies": {
16
40
  "jest": "^29.0.0"
17
41
  }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * SmartOffline SDK (CommonJS version)
3
+ */
4
+ function init(config = {}) {
5
+ if (typeof navigator === "undefined" || !("serviceWorker" in navigator)) {
6
+ console.warn("Service Workers not supported");
7
+ return;
8
+ }
9
+
10
+ const sdkConfig = {
11
+ pages: config.pages || [],
12
+ apis: config.apis || [],
13
+ debug: config.debug || false,
14
+ frequencyThreshold: config.frequencyThreshold ?? 3,
15
+ recencyThreshold: config.recencyThreshold ?? 24 * 60 * 60 * 1000,
16
+ maxResourceSize: config.maxResourceSize ?? Infinity,
17
+ networkQuality: config.networkQuality ?? "auto",
18
+ significance: config.significance ?? {},
19
+ };
20
+
21
+ navigator.serviceWorker.register("/smart-offline-sw.js").then(() => {
22
+ console.log("Smart Offline Service Worker registered");
23
+
24
+ navigator.serviceWorker.ready.then(() => {
25
+ if (navigator.serviceWorker.controller) {
26
+ navigator.serviceWorker.controller.postMessage({
27
+ type: "INIT_CONFIG",
28
+ payload: sdkConfig,
29
+ });
30
+
31
+ if (sdkConfig.debug) {
32
+ console.log("[SmartOffline] Config sent to SW:", sdkConfig);
33
+ }
34
+ }
35
+ });
36
+
37
+ navigator.serviceWorker.addEventListener("controllerchange", () => {
38
+ if (navigator.serviceWorker.controller) {
39
+ navigator.serviceWorker.controller.postMessage({
40
+ type: "INIT_CONFIG",
41
+ payload: sdkConfig,
42
+ });
43
+
44
+ if (sdkConfig.debug) {
45
+ console.log(
46
+ "[SmartOffline] Config sent after controllerchange:",
47
+ sdkConfig,
48
+ );
49
+ }
50
+ }
51
+ });
52
+ });
53
+ }
54
+
55
+ const SmartOffline = { init };
56
+
57
+ module.exports = { SmartOffline };
58
+ module.exports.default = SmartOffline;
package/src/index.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ export interface SmartOfflineConfig {
2
+ /** Array of page URL patterns to cache */
3
+ pages?: string[];
4
+ /** Array of API URL patterns to cache */
5
+ apis?: string[];
6
+ /** Enable debug logging */
7
+ debug?: boolean;
8
+ /** Number of accesses before resource is considered "frequent" (default: 3) */
9
+ frequencyThreshold?: number;
10
+ /** Milliseconds within which resource is considered "recent" (default: 24h) */
11
+ recencyThreshold?: number;
12
+ /** Max bytes to cache per resource; larger resources skipped (default: Infinity) */
13
+ maxResourceSize?: number;
14
+ /** Network quality setting: 'auto' | 'fast' | 'slow' (default: 'auto') */
15
+ networkQuality?: 'auto' | 'fast' | 'slow';
16
+ /** Manual priority overrides by URL pattern */
17
+ significance?: Record<string, 'high' | 'normal' | 'low'>;
18
+ }
19
+
20
+ export interface SmartOfflineSDK {
21
+ init(config?: SmartOfflineConfig): void;
22
+ }
23
+
24
+ export declare const SmartOffline: SmartOfflineSDK;
25
+ export default SmartOffline;
package/src/index.js CHANGED
@@ -18,8 +18,11 @@ function init(config = {}) {
18
18
  pages: config.pages || [],
19
19
  apis: config.apis || [],
20
20
  debug: config.debug || false,
21
-
22
-
21
+ frequencyThreshold: config.frequencyThreshold ?? 3,
22
+ recencyThreshold: config.recencyThreshold ?? 24 * 60 * 60 * 1000,
23
+ maxResourceSize: config.maxResourceSize ?? Infinity,
24
+ networkQuality: config.networkQuality ?? "auto",
25
+ significance: config.significance ?? {},
23
26
  };
24
27
 
25
28
  navigator.serviceWorker.register("/smart-offline-sw.js").then(() => {
@@ -48,7 +51,7 @@ function init(config = {}) {
48
51
  if (sdkConfig.debug) {
49
52
  console.log(
50
53
  "[SmartOffline] Config sent after controllerchange:",
51
- sdkConfig
54
+ sdkConfig,
52
55
  );
53
56
  }
54
57
  }
@@ -58,5 +61,5 @@ function init(config = {}) {
58
61
 
59
62
  const SmartOffline = { init };
60
63
 
61
- export { SmartOffline }; // ✅ named export
62
- export default SmartOffline; // ✅ default export
64
+ export { SmartOffline }; // ✅ named export
65
+ export default SmartOffline; // ✅ default export
package/CONTRIBUTING.md DELETED
@@ -1,5 +0,0 @@
1
- # Contributing
2
-
3
- - Fork the repo and open a PR.
4
- - Run tests with `npm test`.
5
- - Follow the code style in existing files.
@@ -1,94 +0,0 @@
1
- # SmartOffline SDK — Project Analysis
2
-
3
- Date: 2026-01-22
4
-
5
- ---
6
-
7
- ## Project Summary
8
-
9
- SmartOffline is a JavaScript SDK that enables offline-first behavior by registering a Service Worker which caches configured pages and API responses. The repository includes a demo, basic tests, and a Node-style test SDK.
10
-
11
- Key locations:
12
-
13
- - `smart-offline-sw.js` — Service Worker implementing caching, usage tracking, and priority logic.
14
- - `src/index.js` — Browser SDK entry (`SmartOffline.init`) that registers the SW and sends configuration.
15
- - `src/usageTracker.js` — IndexedDB-based usage tracker (client-side helper).
16
- - `src/sdk/index.js` — Small Node test client (`HackvisionClient`) used by tests/examples.
17
- - `demo/` — Demo page and sample API (`demo/index.html`, `demo/api/profile.json`).
18
- - `tests/` — `tests/basic.test.js` (Jest) covering the test client.
19
-
20
- ---
21
-
22
- ## Features That Work (Implemented)
23
-
24
- - Service Worker registration via `SmartOffline.init()` (browser).
25
- - SW intercepts `GET` requests for configured pages and APIs and caches successful network responses.
26
- - Offline fallback: SW serves cached responses when fetch fails.
27
- - Usage tracking in IndexedDB (stores `count` and `lastAccessed` per URL) implemented both in `smart-offline-sw.js` and `src/usageTracker.js`.
28
- - Priority determination (high/normal) based on frequency and recency thresholds.
29
- - Demo implementation showing SDK usage and an API example.
30
- - Node test client (`HackvisionClient.echo`) and unit test (`jest`) pass.
31
- - CI workflow exists to run tests (.github/workflows/ci.yml).
32
- - Package published as `@soham20/smart-offline-sdk` (v0.1.1).
33
-
34
- ---
35
-
36
- ## Issues & Gaps (Needs Attention)
37
-
38
- - README contains unresolved merge markers. Clean and add clear quickstarts for browser and Node.
39
- - `package.json` declares `"type": "commonjs"` but `src/index.js` uses ESM-style `export` (module mismatch). This may confuse Node consumers and bundlers.
40
- - No build/bundling step: source files are published directly. This reduces compatibility across consumers (CJS/ESM/browser UMD). No `dist/` artifacts.
41
- - Service Worker behavior is not covered by automated integration tests (typical but recommended to add Playwright/Puppeteer tests for offline behavior).
42
- - Inconsistent public API surface: demo uses `SmartOffline.init()` while package `main` points to `src/sdk/index.js` (Node test client). Clarify and add a top-level entry that documents both browser and Node exports.
43
- - Privacy/consent: usage tracking stores per-URL usage in IndexedDB — document privacy implications and provide opt-out.
44
- - No cache eviction / quota management — cache can grow without limits.
45
-
46
- ---
47
-
48
- ## Short-Term Recommendations (Actionable)
49
-
50
- 1. Fix `README.md` (remove merge markers) and add clear Quickstart sections for browser and Node usage.
51
- 2. Standardize module format:
52
- - Option A: Build ESM + CJS bundles (Rollup) and set `main`/`module`/`exports` in `package.json`.
53
- - Option B: Convert `src/index.js` to CommonJS `module.exports` if targeting Node-only.
54
- 3. Add a build step that outputs `dist/` bundles (UMD for browsers, ESM and CJS for Node). Update `package.json` scripts (`build`, `prepublishOnly`).
55
- 4. Add integration tests (Playwright) that:
56
- - Serve demo, register SW, fetch a resource online, go offline, and verify cached response is served.
57
- 5. Add cache size management (LRU or simple max-entries) using usage metrics in IndexedDB.
58
- 6. Document the usage tracking behavior and add opt-out configuration (e.g., `trackUsage: false`).
59
-
60
- ---
61
-
62
- ## Mid/Large-Term Enhancements (Future)
63
-
64
- - Prefetching & background sync for high-priority resources.
65
- - Configurable stale-while-revalidate and TTL per resource.
66
- - TypeScript conversion + publishing type definitions.
67
- - Expose a small diagnostics API so apps can query cache status and usage stats.
68
- - Publish a UMD browser bundle and host on a CDN for direct `<script>` usage.
69
- - Telemetry (opt-in) and analytics for adoption + cache effectiveness metrics.
70
-
71
- ---
72
-
73
- ## Suggested Next Steps (I can implement)
74
-
75
- - Patch `README.md` to a clean quickstart (browser + Node).
76
- - Add a small build setup (Rollup) to produce `dist/` artifacts and update `package.json`.
77
- - Add a Playwright integration test for offline cache validation.
78
-
79
- If you want, I can: (pick one or more)
80
-
81
- - Fix the `README.md` now.
82
- - Add a minimal Rollup build and update `package.json`.
83
- - Add a Playwright test and a small local server script to run it.
84
-
85
- ---
86
-
87
- ## Notes & Observations
88
-
89
- - The published package name changed from `hackvision2026-sdk` (spam-detected) to scoped `@soham20/smart-offline-sdk`, which is preferable.
90
- - Tests pass (`npm test`). The test target is small (echo), so expand tests to include SDK behavior where feasible.
91
-
92
- ---
93
-
94
- _Generated by repository scan on 2026-01-22._
File without changes
File without changes
File without changes