@soham20/smart-offline-sdk 0.1.2 → 0.1.3

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/jest.config.js ADDED
@@ -0,0 +1,9 @@
1
+ /** @type {import('jest').Config} */
2
+ const config = {
3
+ testEnvironment: 'node',
4
+ transform: {},
5
+ moduleFileExtensions: ['js', 'cjs', 'mjs', 'json'],
6
+ testMatch: ['**/tests/**/*.test.cjs'],
7
+ };
8
+
9
+ export default config;
package/package.json CHANGED
@@ -1,17 +1,27 @@
1
1
  {
2
2
  "name": "@soham20/smart-offline-sdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
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
+ "exports": {
8
+ ".": {
9
+ "import": "./src/index.js",
10
+ "require": "./src/index.cjs.js"
11
+ },
12
+ "./client": {
13
+ "require": "./src/sdk/index.cjs"
14
+ }
15
+ },
6
16
  "scripts": {
7
- "test": "jest",
17
+ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
8
18
  "build": "echo \"no build step\"",
9
19
  "lint": "echo \"no lint configured\""
10
20
  },
11
- "keywords": [],
21
+ "keywords": ["offline", "pwa", "service-worker", "cache", "offline-first"],
12
22
  "author": "Atif Sayed",
13
23
  "license": "MIT",
14
- "type": "commonjs",
24
+ "type": "module",
15
25
  "devDependencies": {
16
26
  "jest": "^29.0.0"
17
27
  }
@@ -0,0 +1,55 @@
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("[SmartOffline] Config sent after controllerchange:", sdkConfig);
46
+ }
47
+ }
48
+ });
49
+ });
50
+ }
51
+
52
+ const SmartOffline = { init };
53
+
54
+ module.exports = { SmartOffline };
55
+ module.exports.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(() => {
File without changes
File without changes
File without changes