@spoosh/plugin-gc 0.1.0-beta.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Spoosh
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.
@@ -0,0 +1,55 @@
1
+ import { SpooshPlugin } from '@spoosh/core';
2
+
3
+ type GcPluginOptions = {
4
+ /**
5
+ * Maximum age in milliseconds for cache entries.
6
+ * Entries older than this will be removed.
7
+ * @default undefined (no time-based cleanup)
8
+ */
9
+ maxAge?: number;
10
+ /**
11
+ * Maximum number of cache entries to keep.
12
+ * When exceeded, oldest entries are removed first.
13
+ * @default undefined (no size limit)
14
+ */
15
+ maxEntries?: number;
16
+ /**
17
+ * Interval in milliseconds between GC runs.
18
+ * @default 60000 (1 minute)
19
+ */
20
+ interval?: number;
21
+ };
22
+ type GcPluginExports = {
23
+ /** Manually trigger garbage collection. Returns number of entries removed. */
24
+ runGc: () => number;
25
+ };
26
+ /**
27
+ * Garbage collection plugin for automatic cache cleanup.
28
+ *
29
+ * Removes stale cache entries based on age or total count
30
+ * to prevent memory bloat in long-running applications.
31
+ *
32
+ * @param options - Plugin options
33
+ *
34
+ * @see {@link https://spoosh.dev/docs/plugins/gc | GC Plugin Documentation}
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * const plugins = [
39
+ * gcPlugin({
40
+ * maxAge: 60000, // Remove entries older than 1 minute
41
+ * maxEntries: 100, // Keep max 100 entries
42
+ * interval: 30000, // Run GC every 30 seconds
43
+ * }),
44
+ * ];
45
+ *
46
+ * // Manual GC trigger
47
+ * const { runGc } = createReactSpoosh(client);
48
+ * const removedCount = runGc();
49
+ * ```
50
+ */
51
+ declare function gcPlugin(options?: GcPluginOptions): SpooshPlugin<{
52
+ instanceApi: GcPluginExports;
53
+ }>;
54
+
55
+ export { type GcPluginExports, type GcPluginOptions, gcPlugin };
@@ -0,0 +1,55 @@
1
+ import { SpooshPlugin } from '@spoosh/core';
2
+
3
+ type GcPluginOptions = {
4
+ /**
5
+ * Maximum age in milliseconds for cache entries.
6
+ * Entries older than this will be removed.
7
+ * @default undefined (no time-based cleanup)
8
+ */
9
+ maxAge?: number;
10
+ /**
11
+ * Maximum number of cache entries to keep.
12
+ * When exceeded, oldest entries are removed first.
13
+ * @default undefined (no size limit)
14
+ */
15
+ maxEntries?: number;
16
+ /**
17
+ * Interval in milliseconds between GC runs.
18
+ * @default 60000 (1 minute)
19
+ */
20
+ interval?: number;
21
+ };
22
+ type GcPluginExports = {
23
+ /** Manually trigger garbage collection. Returns number of entries removed. */
24
+ runGc: () => number;
25
+ };
26
+ /**
27
+ * Garbage collection plugin for automatic cache cleanup.
28
+ *
29
+ * Removes stale cache entries based on age or total count
30
+ * to prevent memory bloat in long-running applications.
31
+ *
32
+ * @param options - Plugin options
33
+ *
34
+ * @see {@link https://spoosh.dev/docs/plugins/gc | GC Plugin Documentation}
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * const plugins = [
39
+ * gcPlugin({
40
+ * maxAge: 60000, // Remove entries older than 1 minute
41
+ * maxEntries: 100, // Keep max 100 entries
42
+ * interval: 30000, // Run GC every 30 seconds
43
+ * }),
44
+ * ];
45
+ *
46
+ * // Manual GC trigger
47
+ * const { runGc } = createReactSpoosh(client);
48
+ * const removedCount = runGc();
49
+ * ```
50
+ */
51
+ declare function gcPlugin(options?: GcPluginOptions): SpooshPlugin<{
52
+ instanceApi: GcPluginExports;
53
+ }>;
54
+
55
+ export { type GcPluginExports, type GcPluginOptions, gcPlugin };
package/dist/index.js ADDED
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ gcPlugin: () => gcPlugin
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // src/plugin.ts
28
+ function runGarbageCollection(stateManager, options) {
29
+ const { maxAge, maxEntries } = options;
30
+ const now = Date.now();
31
+ let removedCount = 0;
32
+ const entries = stateManager.getAllCacheEntries();
33
+ if (maxAge !== void 0) {
34
+ for (const { key, entry } of entries) {
35
+ const age = now - entry.state.timestamp;
36
+ if (age > maxAge) {
37
+ stateManager.deleteCache(key);
38
+ removedCount++;
39
+ }
40
+ }
41
+ }
42
+ if (maxEntries !== void 0) {
43
+ const currentEntries = stateManager.getAllCacheEntries();
44
+ const excess = currentEntries.length - maxEntries;
45
+ if (excess > 0) {
46
+ const sorted = currentEntries.sort(
47
+ (a, b) => a.entry.state.timestamp - b.entry.state.timestamp
48
+ );
49
+ for (let i = 0; i < excess; i++) {
50
+ const entry = sorted[i];
51
+ if (entry) {
52
+ stateManager.deleteCache(entry.key);
53
+ removedCount++;
54
+ }
55
+ }
56
+ }
57
+ }
58
+ return removedCount;
59
+ }
60
+ function gcPlugin(options = {}) {
61
+ const { interval = 6e4 } = options;
62
+ return {
63
+ name: "spoosh:gc",
64
+ operations: [],
65
+ instanceApi(context) {
66
+ const { stateManager } = context;
67
+ const runGc = () => {
68
+ return runGarbageCollection(stateManager, options);
69
+ };
70
+ setInterval(() => {
71
+ runGc();
72
+ }, interval);
73
+ return {
74
+ runGc
75
+ };
76
+ }
77
+ };
78
+ }
79
+ // Annotate the CommonJS export names for ESM import in node:
80
+ 0 && (module.exports = {
81
+ gcPlugin
82
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,55 @@
1
+ // src/plugin.ts
2
+ function runGarbageCollection(stateManager, options) {
3
+ const { maxAge, maxEntries } = options;
4
+ const now = Date.now();
5
+ let removedCount = 0;
6
+ const entries = stateManager.getAllCacheEntries();
7
+ if (maxAge !== void 0) {
8
+ for (const { key, entry } of entries) {
9
+ const age = now - entry.state.timestamp;
10
+ if (age > maxAge) {
11
+ stateManager.deleteCache(key);
12
+ removedCount++;
13
+ }
14
+ }
15
+ }
16
+ if (maxEntries !== void 0) {
17
+ const currentEntries = stateManager.getAllCacheEntries();
18
+ const excess = currentEntries.length - maxEntries;
19
+ if (excess > 0) {
20
+ const sorted = currentEntries.sort(
21
+ (a, b) => a.entry.state.timestamp - b.entry.state.timestamp
22
+ );
23
+ for (let i = 0; i < excess; i++) {
24
+ const entry = sorted[i];
25
+ if (entry) {
26
+ stateManager.deleteCache(entry.key);
27
+ removedCount++;
28
+ }
29
+ }
30
+ }
31
+ }
32
+ return removedCount;
33
+ }
34
+ function gcPlugin(options = {}) {
35
+ const { interval = 6e4 } = options;
36
+ return {
37
+ name: "spoosh:gc",
38
+ operations: [],
39
+ instanceApi(context) {
40
+ const { stateManager } = context;
41
+ const runGc = () => {
42
+ return runGarbageCollection(stateManager, options);
43
+ };
44
+ setInterval(() => {
45
+ runGc();
46
+ }, interval);
47
+ return {
48
+ runGc
49
+ };
50
+ }
51
+ };
52
+ }
53
+ export {
54
+ gcPlugin
55
+ };
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@spoosh/plugin-gc",
3
+ "version": "0.1.0-beta.0",
4
+ "description": "Garbage collection plugin for Spoosh cache with time and size-based cleanup",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/nxnom/spoosh.git",
9
+ "directory": "packages/plugin-gc"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/nxnom/spoosh/issues"
13
+ },
14
+ "homepage": "https://spoosh.dev/docs/plugins/gc",
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "keywords": [
19
+ "spoosh",
20
+ "plugin",
21
+ "gc",
22
+ "garbage-collection",
23
+ "cache",
24
+ "memory"
25
+ ],
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "import": "./dist/index.mjs",
33
+ "require": "./dist/index.js"
34
+ }
35
+ },
36
+ "peerDependencies": {
37
+ "@spoosh/core": ">=0.1.0"
38
+ },
39
+ "devDependencies": {
40
+ "@spoosh/core": "0.1.0-beta.0",
41
+ "@spoosh/test-utils": "0.1.0-beta.0"
42
+ },
43
+ "scripts": {
44
+ "dev": "tsup --watch",
45
+ "build": "tsup",
46
+ "typecheck": "tsc --noEmit",
47
+ "lint": "eslint src --max-warnings 0",
48
+ "format": "prettier --write 'src/**/*.ts'"
49
+ }
50
+ }