@soham20/smart-offline-sdk 0.1.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/.github/workflows/ci.yml +17 -0
- package/CHANGELOG.md +6 -0
- package/CONTRIBUTING.md +5 -0
- package/LICENSE +21 -0
- package/README.md +14 -0
- package/demo/api/profile.json +5 -0
- package/demo/index.html +32 -0
- package/examples/example.js +7 -0
- package/package.json +18 -0
- package/smart-offline-sw.js +104 -0
- package/src/index.js +47 -0
- package/src/sdk/index.js +11 -0
- package/tests/basic.test.js +7 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on: [push, pull_request]
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
test:
|
|
7
|
+
runs-on: windows-latest
|
|
8
|
+
strategy:
|
|
9
|
+
matrix:
|
|
10
|
+
node-version: [18.x, 20.x]
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v4
|
|
13
|
+
- uses: actions/setup-node@v4
|
|
14
|
+
with:
|
|
15
|
+
node-version: ${{ matrix.node-version }}
|
|
16
|
+
- run: npm ci
|
|
17
|
+
- run: npm test
|
package/CHANGELOG.md
ADDED
package/CONTRIBUTING.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Hackvision2026 SDK (JavaScript)
|
|
2
|
+
|
|
3
|
+
This repository contains the JavaScript SDK for Hackvision2026.
|
|
4
|
+
|
|
5
|
+
Quickstart
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
const { HackvisionClient } = require('./src');
|
|
9
|
+
|
|
10
|
+
const client = new HackvisionClient({ apiKey: process.env.HACKVISION_API_KEY });
|
|
11
|
+
client.echo('hello').then(console.log);
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
See `examples/` for a runnable example.
|
package/demo/index.html
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>Smart Offline SDK Demo</title>
|
|
5
|
+
</head>
|
|
6
|
+
<body>
|
|
7
|
+
<h1>Smart Offline SDK Demo</h1>
|
|
8
|
+
<p>If this page reloads offline, the SDK works.</p>
|
|
9
|
+
|
|
10
|
+
<script type="module">
|
|
11
|
+
import { SmartOffline } from "../src/index.js";
|
|
12
|
+
|
|
13
|
+
SmartOffline.init({
|
|
14
|
+
pages: ["/demo/index.html","/demo/api/profile.json"],
|
|
15
|
+
apis: ["https://jsonplaceholder.typicode.com/posts"],
|
|
16
|
+
debug: true
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
fetch("https://jsonplaceholder.typicode.com/posts")
|
|
20
|
+
.then(res => res.json())
|
|
21
|
+
.then(data => {
|
|
22
|
+
document.body.innerHTML += `<pre>${data}</pre>`;
|
|
23
|
+
})
|
|
24
|
+
.catch(err => {
|
|
25
|
+
document.body.innerHTML += "<p>API failed</p>";
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
</script>
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
</body>
|
|
32
|
+
</html>
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@soham20/smart-offline-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Smart offline-first JavaScript SDK with intelligent caching for web applications",
|
|
5
|
+
"main": "src/sdk/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "jest",
|
|
8
|
+
"build": "echo \"no build step\"",
|
|
9
|
+
"lint": "echo \"no lint configured\""
|
|
10
|
+
},
|
|
11
|
+
"keywords": [],
|
|
12
|
+
"author": "Atif Sayed",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"type": "commonjs",
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"jest": "^29.0.0"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
const CACHE_NAME = "smart-offline-cache-v1";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* SDK configuration received from SmartOffline.init()
|
|
5
|
+
*/
|
|
6
|
+
let SDK_CONFIG = {
|
|
7
|
+
pages: [],
|
|
8
|
+
apis: [],
|
|
9
|
+
debug: false,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Receive config from SDK
|
|
14
|
+
*/
|
|
15
|
+
self.addEventListener("message", (event) => {
|
|
16
|
+
if (event.data && event.data.type === "INIT_CONFIG") {
|
|
17
|
+
SDK_CONFIG = event.data.payload;
|
|
18
|
+
|
|
19
|
+
if (SDK_CONFIG.debug) {
|
|
20
|
+
console.log("[SmartOffline] Config received:", SDK_CONFIG);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Install event
|
|
27
|
+
*/
|
|
28
|
+
self.addEventListener("install", (event) => {
|
|
29
|
+
self.skipWaiting(); // 🔥 REQUIRED
|
|
30
|
+
event.waitUntil(caches.open(CACHE_NAME));
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Activate event
|
|
35
|
+
*/
|
|
36
|
+
self.addEventListener("activate", (event) => {
|
|
37
|
+
event.waitUntil(
|
|
38
|
+
Promise.all([
|
|
39
|
+
self.clients.claim(), // 🔥 REQUIRED
|
|
40
|
+
caches.keys().then((cacheNames) =>
|
|
41
|
+
Promise.all(
|
|
42
|
+
cacheNames.map((name) => {
|
|
43
|
+
if (name !== CACHE_NAME) {
|
|
44
|
+
return caches.delete(name);
|
|
45
|
+
}
|
|
46
|
+
}),
|
|
47
|
+
),
|
|
48
|
+
),
|
|
49
|
+
]),
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Fetch event – SMART logic
|
|
56
|
+
*/
|
|
57
|
+
self.addEventListener("fetch", (event) => {
|
|
58
|
+
const request = event.request;
|
|
59
|
+
|
|
60
|
+
if (request.method !== "GET") return;
|
|
61
|
+
|
|
62
|
+
console.log("[SW] Intercepted:", request.url);
|
|
63
|
+
|
|
64
|
+
const isPage = SDK_CONFIG.pages.some((p) =>
|
|
65
|
+
request.url.includes(p)
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
const isAPI = SDK_CONFIG.apis.some((a) =>
|
|
69
|
+
request.url.includes(a)
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
if (!isPage && !isAPI) return;
|
|
73
|
+
|
|
74
|
+
event.respondWith(
|
|
75
|
+
fetch(request)
|
|
76
|
+
.then((response) => {
|
|
77
|
+
const clone = response.clone();
|
|
78
|
+
const cacheKey = request.url;
|
|
79
|
+
|
|
80
|
+
caches.open(CACHE_NAME).then((cache) => {
|
|
81
|
+
cache.put(cacheKey, clone);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
if (SDK_CONFIG.debug) {
|
|
85
|
+
console.log(
|
|
86
|
+
`[SmartOffline] Cached ${isAPI ? "API" : "PAGE"}:`,
|
|
87
|
+
cacheKey
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return response;
|
|
92
|
+
})
|
|
93
|
+
.catch(() => {
|
|
94
|
+
if (SDK_CONFIG.debug) {
|
|
95
|
+
console.log(
|
|
96
|
+
`[SmartOffline] Served from cache ${isAPI ? "API" : "PAGE"}:`,
|
|
97
|
+
request.url
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
return caches.match(request.url);
|
|
101
|
+
}),
|
|
102
|
+
);
|
|
103
|
+
});
|
|
104
|
+
|
package/src/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
function init(config = {}) {
|
|
2
|
+
if (!("serviceWorker" in navigator)) {
|
|
3
|
+
console.warn("Service Workers not supported");
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const sdkConfig = {
|
|
8
|
+
pages: config.pages || [],
|
|
9
|
+
apis: config.apis || [],
|
|
10
|
+
debug: config.debug || false,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
navigator.serviceWorker.register("/smart-offline-sw.js").then((registration) => {
|
|
14
|
+
console.log("Smart Offline Service Worker registered");
|
|
15
|
+
|
|
16
|
+
// Wait for SW to be ready before sending config
|
|
17
|
+
navigator.serviceWorker.ready.then(() => {
|
|
18
|
+
// Send config to active service worker
|
|
19
|
+
if (navigator.serviceWorker.controller) {
|
|
20
|
+
navigator.serviceWorker.controller.postMessage({
|
|
21
|
+
type: "INIT_CONFIG",
|
|
22
|
+
payload: sdkConfig,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
if (sdkConfig.debug) {
|
|
26
|
+
console.log("[SmartOffline] Config sent to SW:", sdkConfig);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Handle new SW taking control (on first install)
|
|
32
|
+
navigator.serviceWorker.addEventListener("controllerchange", () => {
|
|
33
|
+
if (navigator.serviceWorker.controller) {
|
|
34
|
+
navigator.serviceWorker.controller.postMessage({
|
|
35
|
+
type: "INIT_CONFIG",
|
|
36
|
+
payload: sdkConfig,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
if (sdkConfig.debug) {
|
|
40
|
+
console.log("[SmartOffline] Config sent after controllerchange:", sdkConfig);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const SmartOffline = { init };
|
package/src/sdk/index.js
ADDED