@kishangai/cache-shuttle 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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kishan Gaiwala
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,19 @@
1
+ # Cache Shuttle 🚀
2
+
3
+ A production-grade, fault-tolerant bridging framework to seamlessly dual-write, read-through, and migrate data across conflicting storage caching tiers without server downtime.
4
+
5
+ Unlike standard multi-caching tools, **Cache Shuttle** focuses specifically on structural migration mechanics, giving you safe, chunked data synchronization utilities to transfer live data from one provider to another seamlessly.
6
+
7
+ ## Key Features
8
+
9
+ * 🔒 **RAM Explosion Prevention:** Processes large migrations in adjustable, chunked batches to keep memory overhead near zero.
10
+ * 🛡️ **Anti-Crash Isolation:** Utilizes parallel non-blocking execution routines (`Promise.allSettled`) to isolate backend network drops.
11
+ * 🔄 **Read-Through & Auto-Healing:** Resolves fast local cache misses by looking up upstream nodes and auto-healing the local tier.
12
+ * 🚚 **The Shuttle (`transferAll`):** Drain and migrate entire memory trees securely with a single line of code.
13
+
14
+ ---
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ npm install cache-shuttle
package/lib/index.js ADDED
@@ -0,0 +1,154 @@
1
+ /**
2
+ * CacheShuttle
3
+ * A production-grade framework to securely dual-write, read-through, and migrate
4
+ * data across different caching layers smoothly without server downtime.
5
+ */
6
+ class CacheShuttle {
7
+ /**
8
+ * @param {Object} options
9
+ * @param {Object} options.source - The primary/fast caching tier adapter.
10
+ * @param {Object} options.target - The secondary/persistent caching tier adapter.
11
+ * @param {number} [options.batchSize=100] - Chunks processed per cycle to prevent memory overloads.
12
+ */
13
+ constructor(options = {}) {
14
+ this._validateAdapters(options.source, options.target);
15
+
16
+ this.source = options.source;
17
+ this.target = options.target;
18
+ this.batchSize = Math.max(1, parseInt(options.batchSize, 10) || 100);
19
+ }
20
+
21
+ /**
22
+ * Defensive Validation Checklist
23
+ * Throws explicit errors instantly on startup if the user misconfigures adapters.
24
+ * @private
25
+ */
26
+ _validateAdapters(source, target) {
27
+ if (!source || !target) {
28
+ throw new Error("CacheShuttle Core Error: Provide valid 'source' and 'target' configuration blocks.");
29
+ }
30
+
31
+ const requiredSourceMethods = ['get', 'set', 'getKeys'];
32
+ for (const method of requiredSourceMethods) {
33
+ if (typeof source[method] !== 'function') {
34
+ throw new Error(`CacheShuttle Core Error: Source adapter is missing structural "${method}()" hook.`);
35
+ }
36
+ }
37
+
38
+ if (typeof target.set !== 'function') {
39
+ throw new Error('CacheShuttle Core Error: Target adapter lacks standard "set()" entry point.');
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Strategy: Dual-Write
45
+ * Executes background updates on both stores in parallel. Fully isolated to bypass system-wide breaks.
46
+ */
47
+ async set(key, value, ttlInSeconds = null) {
48
+ if (key === undefined || key === null) return false;
49
+
50
+ const results = await Promise.allSettled([
51
+ this.source.set(key, value, ttlInSeconds),
52
+ this.target.set(key, value, ttlInSeconds)
53
+ ]);
54
+
55
+ const sourceOk = results[0].status === 'fulfilled';
56
+ const targetOk = results[1].status === 'fulfilled';
57
+
58
+ if (!sourceOk) console.warn(`[CacheShuttle] Safe Warning: Outbound local write dropped for key "${key}".`);
59
+ if (!targetOk) console.warn(`[CacheShuttle] Safe Warning: Outbound upstream write dropped for key "${key}".`);
60
+
61
+ return sourceOk || targetOk;
62
+ }
63
+
64
+ /**
65
+ * Strategy: Read-Through + Self-Healing
66
+ * Returns data immediately on a fast cache hit. Resolves cache misses by pulling from target and refreshing source.
67
+ */
68
+ async get(key, ttlInSeconds = null) {
69
+ if (key === undefined || key === null) return null;
70
+
71
+ try {
72
+ // Step A: Read local fast engine
73
+ const cachedValue = await this.source.get(key);
74
+ if (cachedValue !== undefined && cachedValue !== null) {
75
+ return cachedValue;
76
+ }
77
+
78
+ // Step B: Search primary storage network layer
79
+ if (typeof this.target.get === 'function') {
80
+ const storedValue = await this.target.get(key);
81
+
82
+ if (storedValue !== undefined && storedValue !== null) {
83
+ // Asynchronously heal source node to optimize downstream loops
84
+ this.source.set(key, storedValue, ttlInSeconds).catch(err => {
85
+ console.error(`[CacheShuttle] Failed healing back-fill for "${key}":`, err.message);
86
+ });
87
+ return storedValue;
88
+ }
89
+ }
90
+ } catch (error) {
91
+ console.error(`[CacheShuttle] Read Exception encountered on "${key}":`, error.message);
92
+ }
93
+
94
+ return null;
95
+ }
96
+
97
+ /**
98
+ * Strategy: The Shuttle (Safely Batched High-Speed Core Migration)
99
+ * Converts and drains memory trees into production databases safely using segmented loops.
100
+ */
101
+ async transferAll() {
102
+ const auditLog = {
103
+ success: true,
104
+ totalKeysFound: 0,
105
+ moved: 0,
106
+ failed: 0,
107
+ errors: []
108
+ };
109
+
110
+ try {
111
+ const keys = await this.source.getKeys();
112
+ if (!Array.isArray(keys) || keys.length === 0) {
113
+ return auditLog;
114
+ }
115
+
116
+ auditLog.totalKeysFound = keys.length;
117
+
118
+ // Safe memory slicing loops: moves batchSize records at a time
119
+ for (let i = 0; i < keys.length; i += this.batchSize) {
120
+ const chunk = keys.slice(i, i + this.batchSize);
121
+
122
+ await Promise.all(chunk.map(async (key) => {
123
+ try {
124
+ const rawData = await this.source.get(key);
125
+
126
+ if (rawData !== undefined && rawData !== null) {
127
+ await this.target.set(key, rawData);
128
+ auditLog.moved++;
129
+ } else {
130
+ auditLog.failed++;
131
+ auditLog.errors.push({ key, explanation: "Empty value detected inside origin engine." });
132
+ }
133
+ } catch (chunkError) {
134
+ auditLog.failed++;
135
+ auditLog.errors.push({ key, explanation: chunkError.message });
136
+ }
137
+ }));
138
+ }
139
+
140
+ return auditLog;
141
+ } catch (fatalCrash) {
142
+ return {
143
+ success: false,
144
+ error: `Critical lifecycle break: ${fatalCrash.message}`,
145
+ totalKeysFound: auditLog.totalKeysFound,
146
+ moved: auditLog.moved,
147
+ failed: auditLog.failed,
148
+ errors: auditLog.errors
149
+ };
150
+ }
151
+ }
152
+ }
153
+
154
+ module.exports = CacheShuttle;
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@kishangai/cache-shuttle",
3
+ "version": "1.0.0",
4
+ "description": "A production-grade bridge framework to securely dual-write, read-through, and migrate records among conflicting storage cache providers seamlessly.",
5
+ "main": "lib/index.js",
6
+ "engines": {
7
+ "node": ">=14.0.0"
8
+ },
9
+ "files": [
10
+ "lib",
11
+ "README.md",
12
+ "LICENSE"
13
+ ],
14
+ "keywords": [
15
+ "cache",
16
+ "redis",
17
+ "node-cache",
18
+ "migration",
19
+ "data-transfer",
20
+ "synchronization",
21
+ "read-through"
22
+ ],
23
+ "author": "Kishan Gaiwala <gaiwalakishan@gmail.com>",
24
+ "license": "MIT",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/kishangaiwala/cache-shuttle.git"
28
+ }
29
+ }