vscode-cache-sync 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 +215 -0
- package/README.md +57 -0
- package/dist/index.d.ts +83 -0
- package/dist/index.js +274 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
Copyright 2026 elenam27
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
|
14
|
+
|
|
15
|
+
Apache License
|
|
16
|
+
Version 2.0, January 2004
|
|
17
|
+
http://www.apache.org/licenses/
|
|
18
|
+
|
|
19
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
20
|
+
|
|
21
|
+
1. Definitions.
|
|
22
|
+
|
|
23
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
24
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
25
|
+
|
|
26
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
27
|
+
the copyright owner that is granting the License.
|
|
28
|
+
|
|
29
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
30
|
+
other entities that control, are controlled by, or are under common
|
|
31
|
+
control with that entity. For the purposes of this definition,
|
|
32
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
33
|
+
direction or management of such entity, whether by contract or
|
|
34
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
35
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
36
|
+
|
|
37
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
38
|
+
exercising permissions granted by this License.
|
|
39
|
+
|
|
40
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
41
|
+
including but not limited to software source code, documentation
|
|
42
|
+
source, and configuration files.
|
|
43
|
+
|
|
44
|
+
"Object" form shall mean any form resulting from mechanical
|
|
45
|
+
transformation or translation of a Source form, including but
|
|
46
|
+
not limited to compiled object code, generated documentation,
|
|
47
|
+
and conversions to other media types.
|
|
48
|
+
|
|
49
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
50
|
+
Object form, made available under the License, as indicated by a
|
|
51
|
+
copyright notice that is included in or attached to the work
|
|
52
|
+
(an example is provided in the Appendix below).
|
|
53
|
+
|
|
54
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
55
|
+
form, that is based on (or derived from) the Work and for which the
|
|
56
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
57
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
58
|
+
of this License, Derivative Works shall not include works that remain
|
|
59
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
60
|
+
the Work and Derivative Works thereof.
|
|
61
|
+
|
|
62
|
+
"Contribution" shall mean any work of authorship, including
|
|
63
|
+
the original version of the Work and any modifications or additions
|
|
64
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
65
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
66
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
67
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
68
|
+
means any form of electronic, verbal, or written communication sent
|
|
69
|
+
to the Licensor or its representatives, including but not limited to
|
|
70
|
+
communication on electronic mailing lists, source code control systems,
|
|
71
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
72
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
73
|
+
excluding communication that is conspicuously marked or otherwise
|
|
74
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
75
|
+
|
|
76
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
77
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
78
|
+
subsequently incorporated within the Work.
|
|
79
|
+
|
|
80
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
81
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
82
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
83
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
84
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
85
|
+
Work and such Derivative Works in Source or Object form.
|
|
86
|
+
|
|
87
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
88
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
89
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
90
|
+
(except as stated in this section) patent license to make, have made,
|
|
91
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
92
|
+
where such license applies only to those patent claims licensable
|
|
93
|
+
by such Contributor that are necessarily infringed by their
|
|
94
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
95
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
96
|
+
institute patent litigation against any entity (including a
|
|
97
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
98
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
99
|
+
or contributory patent infringement, then any patent licenses
|
|
100
|
+
granted to You under this License for that Work shall terminate
|
|
101
|
+
as of the date such litigation is filed.
|
|
102
|
+
|
|
103
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
104
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
105
|
+
modifications, and in Source or Object form, provided that You
|
|
106
|
+
meet the following conditions:
|
|
107
|
+
|
|
108
|
+
(a) You must give any other recipients of the Work or
|
|
109
|
+
Derivative Works a copy of this License; and
|
|
110
|
+
|
|
111
|
+
(b) You must cause any modified files to carry prominent notices
|
|
112
|
+
stating that You changed the files; and
|
|
113
|
+
|
|
114
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
115
|
+
that You distribute, all copyright, patent, trademark, and
|
|
116
|
+
attribution notices from the Source form of the Work,
|
|
117
|
+
excluding those notices that do not pertain to any part of
|
|
118
|
+
the Derivative Works; and
|
|
119
|
+
|
|
120
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
121
|
+
distribution, then any Derivative Works that You distribute must
|
|
122
|
+
include a readable copy of the attribution notices contained
|
|
123
|
+
within such NOTICE file, excluding those notices that do not
|
|
124
|
+
pertain to any part of the Derivative Works, in at least one
|
|
125
|
+
of the following places: within a NOTICE text file distributed
|
|
126
|
+
as part of the Derivative Works; within the Source form or
|
|
127
|
+
documentation, if provided along with the Derivative Works; or,
|
|
128
|
+
within a display generated by the Derivative Works, if and
|
|
129
|
+
wherever such third-party notices normally appear. The contents
|
|
130
|
+
of the NOTICE file are for informational purposes only and
|
|
131
|
+
do not modify the License. You may add Your own attribution
|
|
132
|
+
notices within Derivative Works that You distribute, alongside
|
|
133
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
134
|
+
that such additional attribution notices cannot be construed
|
|
135
|
+
as modifying the License.
|
|
136
|
+
|
|
137
|
+
You may add Your own copyright statement to Your modifications and
|
|
138
|
+
may provide additional or different license terms and conditions
|
|
139
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
140
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
141
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
142
|
+
the conditions stated in this License.
|
|
143
|
+
|
|
144
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
145
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
146
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
147
|
+
this License, without any additional terms or conditions.
|
|
148
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
149
|
+
the terms of any separate license agreement you may have executed
|
|
150
|
+
with Licensor regarding such Contributions.
|
|
151
|
+
|
|
152
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
153
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
154
|
+
except as required for reasonable and customary use in describing the
|
|
155
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
156
|
+
|
|
157
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
158
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
159
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
160
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
161
|
+
implied, including, without limitation, any warranties or conditions
|
|
162
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
163
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
164
|
+
appropriateness of using or redistributing the Work and assume any
|
|
165
|
+
risks associated with Your exercise of permissions under this License.
|
|
166
|
+
|
|
167
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
168
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
169
|
+
unless required by applicable law (such as deliberate and grossly
|
|
170
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
171
|
+
liable to You for damages, including any direct, indirect, special,
|
|
172
|
+
incidental, or consequential damages of any character arising as a
|
|
173
|
+
result of this License or out of the use or inability to use the
|
|
174
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
175
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
176
|
+
other commercial damages or losses), even if such Contributor
|
|
177
|
+
has been advised of the possibility of such damages.
|
|
178
|
+
|
|
179
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
180
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
181
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
182
|
+
or other liability obligations and/or rights consistent with this
|
|
183
|
+
License. However, in accepting such obligations, You may act only
|
|
184
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
185
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
186
|
+
defend, and hold each Contributor harmless for any liability
|
|
187
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
188
|
+
of your accepting any such warranty or additional liability.
|
|
189
|
+
|
|
190
|
+
END OF TERMS AND CONDITIONS
|
|
191
|
+
|
|
192
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
193
|
+
|
|
194
|
+
To apply the Apache License to your work, attach the following
|
|
195
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
196
|
+
replaced with your own identifying information. (Don't include
|
|
197
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
198
|
+
comment syntax for the file format. We also recommend that a
|
|
199
|
+
file or class name and description of purpose be included on the
|
|
200
|
+
same "printed page" as the copyright notice for easier
|
|
201
|
+
identification within third-party archives.
|
|
202
|
+
|
|
203
|
+
Copyright [yyyy] [name of copyright owner]
|
|
204
|
+
|
|
205
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
206
|
+
you may not use this file except in compliance with the License.
|
|
207
|
+
You may obtain a copy of the License at
|
|
208
|
+
|
|
209
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
210
|
+
|
|
211
|
+
Unless required by applicable law or agreed to in writing, software
|
|
212
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
213
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
214
|
+
See the License for the specific language governing permissions and
|
|
215
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# vscode-cache-sync
|
|
2
|
+
|
|
3
|
+
Workspace cache synchronization for VS Code extensions.
|
|
4
|
+
|
|
5
|
+
A small, dependency-free cache with TTL expiry, an entry cap, and
|
|
6
|
+
best-effort persistence into an injected storage handle. The storage
|
|
7
|
+
contract follows the VS Code [`Memento`](https://code.visualstudio.com/api/references/vscode-api#Memento)
|
|
8
|
+
shape, so an extension can back the cache with its global state; any object
|
|
9
|
+
with the same two methods works, including plain Map-backed stubs in tests.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **TTL entries** — every entry carries an absolute expiry; a background
|
|
14
|
+
sweep removes expired entries and `get()` drops them lazily.
|
|
15
|
+
- **Entry cap** — the oldest entries are dropped once `maxEntries` is
|
|
16
|
+
reached, so a long-lived session cannot grow the cache without bound.
|
|
17
|
+
- **Debounced persistence** — mutations coalesce into a single snapshot
|
|
18
|
+
write once writes settle, keeping storage traffic proportional to bursts,
|
|
19
|
+
not individual calls.
|
|
20
|
+
- **Zero dependencies** — no runtime dependencies; timers never hold the
|
|
21
|
+
Node event loop open.
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
const { CacheSyncManager, createDefaultConfig } = require("vscode-cache-sync");
|
|
27
|
+
|
|
28
|
+
const cache = new CacheSyncManager({
|
|
29
|
+
...createDefaultConfig(),
|
|
30
|
+
globalState: context.globalState, // Memento-shaped handle (optional)
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
cache.set("lastWorkspace", { folder: "/work/app" });
|
|
34
|
+
cache.get("lastWorkspace"); // { folder: "/work/app" }
|
|
35
|
+
cache.set("drafts", drafts, { ttlMs: 60_000 });
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Configuration is created via `createDefaultConfig()`; spread and override
|
|
39
|
+
individual fields as needed. Unknown fields are ignored.
|
|
40
|
+
|
|
41
|
+
## API
|
|
42
|
+
|
|
43
|
+
| Member | Description |
|
|
44
|
+
| --- | --- |
|
|
45
|
+
| `createDefaultConfig()` | Fresh default configuration object. |
|
|
46
|
+
| `new CacheSyncManager(config?)` | Creates the manager; restores a persisted snapshot when storage is provided. |
|
|
47
|
+
| `set(key, value, { ttlMs }?)` | Stores a value; schedules the debounced persist. |
|
|
48
|
+
| `get(key)` | Live value, or `undefined` when missing or expired. |
|
|
49
|
+
| `has(key)` | Whether `key` holds a live entry. |
|
|
50
|
+
| `delete(key)` / `clear()` | Removal. |
|
|
51
|
+
| `size` | Number of live entries in memory. |
|
|
52
|
+
| `snapshot()` | Plain-object snapshot of the live entries. |
|
|
53
|
+
| `dispose()` | Stops background timers and flushes pending state. |
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
Apache-2.0
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vscode-cache-sync — workspace cache synchronization for VS Code extensions.
|
|
3
|
+
*
|
|
4
|
+
* A small, dependency-free cache with TTL expiry, an entry cap, and
|
|
5
|
+
* best-effort persistence into a Memento-shaped storage handle.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Default manager configuration. All numeric fields are clamped into safe
|
|
10
|
+
* ranges by the manager; unknown fields are ignored.
|
|
11
|
+
*/
|
|
12
|
+
export interface CacheSyncConfig {
|
|
13
|
+
/** Maximum number of live entries before the oldest entries are dropped. */
|
|
14
|
+
maxEntries: number;
|
|
15
|
+
/** Time-to-live applied to entries that do not carry their own TTL (ms). */
|
|
16
|
+
defaultTtlMs: number;
|
|
17
|
+
/** How often the background sweep removes expired entries (ms). */
|
|
18
|
+
evictionIntervalMs: number;
|
|
19
|
+
/** Grace period after a mutation before the snapshot is persisted (ms). */
|
|
20
|
+
debounceMs: number;
|
|
21
|
+
/** Storage key used for the persisted snapshot. */
|
|
22
|
+
persistenceKey: string;
|
|
23
|
+
/**
|
|
24
|
+
* Optional Memento-shaped storage handle (`get`/`update`). When omitted,
|
|
25
|
+
* the cache runs purely in memory.
|
|
26
|
+
*/
|
|
27
|
+
globalState?: {
|
|
28
|
+
get<T>(key: string): T | undefined;
|
|
29
|
+
update(key: string, value: unknown): Thenable<void> | void;
|
|
30
|
+
};
|
|
31
|
+
/** Accepted for integration convenience; not used by the cache itself. */
|
|
32
|
+
extensionContext?: unknown;
|
|
33
|
+
[key: string]: unknown;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Serialized cache entry: the stored value plus an absolute expiry stamp. */
|
|
37
|
+
export interface CacheEntry<T = unknown> {
|
|
38
|
+
value: T;
|
|
39
|
+
expiresAt: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface SetOptions {
|
|
43
|
+
/** Per-entry TTL override (ms). */
|
|
44
|
+
ttlMs?: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Returns a fresh default configuration. Consumers may spread this object
|
|
49
|
+
* and override individual fields.
|
|
50
|
+
*/
|
|
51
|
+
export function createDefaultConfig(): CacheSyncConfig;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* TTL cache with an entry cap, a background eviction sweep, and debounced
|
|
55
|
+
* persistence into the injected storage handle.
|
|
56
|
+
*/
|
|
57
|
+
export class CacheSyncManager {
|
|
58
|
+
constructor(config?: Partial<CacheSyncConfig>);
|
|
59
|
+
|
|
60
|
+
/** Stores `value` under `key`. An optional per-entry TTL overrides the default. */
|
|
61
|
+
set<T>(key: string, value: T, options?: SetOptions): this;
|
|
62
|
+
|
|
63
|
+
/** Returns the live value for `key`, or undefined when missing or expired. */
|
|
64
|
+
get<T = unknown>(key: string): T | undefined;
|
|
65
|
+
|
|
66
|
+
/** Returns true when `key` holds a live (non-expired) entry. */
|
|
67
|
+
has(key: string): boolean;
|
|
68
|
+
|
|
69
|
+
/** Removes `key` if present. */
|
|
70
|
+
delete(key: string): boolean;
|
|
71
|
+
|
|
72
|
+
/** Drops every entry. */
|
|
73
|
+
clear(): void;
|
|
74
|
+
|
|
75
|
+
/** Number of live entries currently held in memory. */
|
|
76
|
+
readonly size: number;
|
|
77
|
+
|
|
78
|
+
/** Plain-object snapshot of the live entries; expired entries are dropped first. */
|
|
79
|
+
snapshot(): Record<string, CacheEntry>;
|
|
80
|
+
|
|
81
|
+
/** Stops background timers and flushes pending state. The manager is inert afterwards. */
|
|
82
|
+
dispose(): void;
|
|
83
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* vscode-cache-sync — workspace cache synchronization for VS Code extensions.
|
|
4
|
+
*
|
|
5
|
+
* A small, dependency-free cache with TTL expiry, an entry cap, and
|
|
6
|
+
* best-effort persistence into an injected storage handle. The storage
|
|
7
|
+
* contract follows the VS Code `Memento` shape (`get(key)` / `update(key,
|
|
8
|
+
* value)`), so the cache can be backed by an extension's global state; any
|
|
9
|
+
* object with the same two methods works, including plain Map-backed stubs
|
|
10
|
+
* in tests.
|
|
11
|
+
*
|
|
12
|
+
* The cache is intentionally synchronous for reads and debounced for writes:
|
|
13
|
+
* mutations update an in-memory Map immediately and a single coalesced
|
|
14
|
+
* persist runs once writes settle.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const DEFAULTS = Object.freeze({
|
|
18
|
+
/** Maximum number of live entries before the oldest entries are dropped. */
|
|
19
|
+
maxEntries: 1024,
|
|
20
|
+
/** Time-to-live applied to entries that do not carry their own TTL (ms). */
|
|
21
|
+
defaultTtlMs: 5 * 60 * 1000,
|
|
22
|
+
/** How often the background sweep removes expired entries (ms). */
|
|
23
|
+
evictionIntervalMs: 60 * 1000,
|
|
24
|
+
/** Grace period after a mutation before the snapshot is persisted (ms). */
|
|
25
|
+
debounceMs: 250,
|
|
26
|
+
/** Storage key used for the persisted snapshot. */
|
|
27
|
+
persistenceKey: "workspaceCache.snapshot.v1",
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
/** Hard clamps so a bad config cannot produce unbounded structures. */
|
|
31
|
+
const LIMITS = Object.freeze({
|
|
32
|
+
maxEntries: { min: 1, max: 65536 },
|
|
33
|
+
defaultTtlMs: { min: 50, max: 24 * 60 * 60 * 1000 },
|
|
34
|
+
evictionIntervalMs: { min: 50, max: 60 * 60 * 1000 },
|
|
35
|
+
debounceMs: { min: 0, max: 60 * 1000 },
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
function clampInt(value, limits, fallback) {
|
|
39
|
+
const n = Number(value);
|
|
40
|
+
if (!Number.isFinite(n)) return fallback;
|
|
41
|
+
return Math.min(limits.max, Math.max(limits.min, Math.floor(n)));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Returns a fresh default configuration. Consumers may spread this object
|
|
46
|
+
* and override individual fields; unknown fields are ignored by the manager.
|
|
47
|
+
*/
|
|
48
|
+
function createDefaultConfig() {
|
|
49
|
+
return { ...DEFAULTS };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Serialized cache entry: the stored value plus an absolute expiry stamp.
|
|
54
|
+
*/
|
|
55
|
+
class CacheSyncManager {
|
|
56
|
+
/**
|
|
57
|
+
* @param {object} [config] see `createDefaultConfig()`; the injected
|
|
58
|
+
* `globalState` (Memento-shaped) enables persistence, and any additional
|
|
59
|
+
* fields (for example an `extensionContext`) are accepted and ignored.
|
|
60
|
+
*/
|
|
61
|
+
constructor(config) {
|
|
62
|
+
const cfg = config && typeof config === "object" ? config : {};
|
|
63
|
+
this._maxEntries = clampInt(cfg.maxEntries, LIMITS.maxEntries, DEFAULTS.maxEntries);
|
|
64
|
+
this._defaultTtlMs = clampInt(cfg.defaultTtlMs, LIMITS.defaultTtlMs, DEFAULTS.defaultTtlMs);
|
|
65
|
+
this._evictionIntervalMs = clampInt(
|
|
66
|
+
cfg.evictionIntervalMs,
|
|
67
|
+
LIMITS.evictionIntervalMs,
|
|
68
|
+
DEFAULTS.evictionIntervalMs,
|
|
69
|
+
);
|
|
70
|
+
this._debounceMs = clampInt(cfg.debounceMs, LIMITS.debounceMs, DEFAULTS.debounceMs);
|
|
71
|
+
this._persistenceKey =
|
|
72
|
+
typeof cfg.persistenceKey === "string" && cfg.persistenceKey.length > 0
|
|
73
|
+
? cfg.persistenceKey
|
|
74
|
+
: DEFAULTS.persistenceKey;
|
|
75
|
+
this._storage = CacheSyncManager._asStorage(cfg.globalState);
|
|
76
|
+
this._disposed = false;
|
|
77
|
+
|
|
78
|
+
this._entries = new Map();
|
|
79
|
+
this._persistTimer = null;
|
|
80
|
+
this._sweepTimer = null;
|
|
81
|
+
|
|
82
|
+
this._restore();
|
|
83
|
+
this._startEvictionSweep();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Adapts a Memento-shaped handle (or a plain Map) to the two operations
|
|
88
|
+
* the cache needs. Returns null when no storage was provided, in which
|
|
89
|
+
* case the cache simply runs in memory.
|
|
90
|
+
*/
|
|
91
|
+
static _asStorage(handle) {
|
|
92
|
+
if (!handle) return null;
|
|
93
|
+
if (typeof handle.get === "function" && typeof handle.update === "function") {
|
|
94
|
+
return { get: (k) => handle.get(k), update: (k, v) => handle.update(k, v) };
|
|
95
|
+
}
|
|
96
|
+
if (handle instanceof Map) {
|
|
97
|
+
return { get: (k) => handle.get(k), update: (k, v) => void handle.set(k, v) };
|
|
98
|
+
}
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Stores `value` under `key`. An optional per-entry TTL overrides the default. */
|
|
103
|
+
set(key, value, options) {
|
|
104
|
+
this._assertAlive();
|
|
105
|
+
if (typeof key !== "string" || key.length === 0) return this;
|
|
106
|
+
const ttl = clampInt(options && options.ttlMs, LIMITS.defaultTtlMs, this._defaultTtlMs);
|
|
107
|
+
const expiresAt = Date.now() + ttl;
|
|
108
|
+
if (!this._entries.has(key)) this._enforceCap(key);
|
|
109
|
+
this._entries.set(key, { value, expiresAt });
|
|
110
|
+
this._schedulePersist();
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Returns the live value for `key`, or undefined when missing or expired. */
|
|
115
|
+
get(key) {
|
|
116
|
+
this._assertAlive();
|
|
117
|
+
const entry = this._entries.get(key);
|
|
118
|
+
if (!entry) return undefined;
|
|
119
|
+
if (entry.expiresAt <= Date.now()) {
|
|
120
|
+
this._entries.delete(key);
|
|
121
|
+
this._schedulePersist();
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
return entry.value;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Returns true when `key` holds a live (non-expired) entry. */
|
|
128
|
+
has(key) {
|
|
129
|
+
return this.get(key) !== undefined;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Removes `key` if present. */
|
|
133
|
+
delete(key) {
|
|
134
|
+
this._assertAlive();
|
|
135
|
+
const removed = this._entries.delete(key);
|
|
136
|
+
if (removed) this._schedulePersist();
|
|
137
|
+
return removed;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Drops every entry. */
|
|
141
|
+
clear() {
|
|
142
|
+
this._assertAlive();
|
|
143
|
+
this._entries.clear();
|
|
144
|
+
this._schedulePersist();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Number of live entries currently held in memory. */
|
|
148
|
+
get size() {
|
|
149
|
+
this._assertAlive();
|
|
150
|
+
return this._entries.size;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Plain-object snapshot of the live entries, suitable for persistence and
|
|
155
|
+
* for debugging. Expired entries are dropped first.
|
|
156
|
+
*/
|
|
157
|
+
snapshot() {
|
|
158
|
+
this._assertAlive();
|
|
159
|
+
this._dropExpired();
|
|
160
|
+
const out = Object.create(null);
|
|
161
|
+
for (const [key, entry] of this._entries) out[key] = entry;
|
|
162
|
+
return out;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Stops background timers and flushes pending state. The manager is inert afterwards. */
|
|
166
|
+
dispose() {
|
|
167
|
+
if (this._disposed) return;
|
|
168
|
+
this._disposed = true;
|
|
169
|
+
if (this._sweepTimer) {
|
|
170
|
+
clearTimeout(this._sweepTimer);
|
|
171
|
+
this._sweepTimer = null;
|
|
172
|
+
}
|
|
173
|
+
if (this._persistTimer) {
|
|
174
|
+
clearTimeout(this._persistTimer);
|
|
175
|
+
this._persistTimer = null;
|
|
176
|
+
}
|
|
177
|
+
this._persist();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// --- internals -----------------------------------------------------------
|
|
181
|
+
|
|
182
|
+
_assertAlive() {
|
|
183
|
+
if (this._disposed) throw new Error("CacheSyncManager has been disposed");
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Keeps insertion order stable and drops the oldest entry when at cap. */
|
|
187
|
+
_enforceCap(incomingKey) {
|
|
188
|
+
while (this._entries.size >= this._maxEntries) {
|
|
189
|
+
const oldest = this._entries.keys().next();
|
|
190
|
+
if (oldest.done) break;
|
|
191
|
+
if (oldest.value === incomingKey) break;
|
|
192
|
+
this._entries.delete(oldest.value);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
_dropExpired() {
|
|
197
|
+
const now = Date.now();
|
|
198
|
+
let changed = false;
|
|
199
|
+
for (const [key, entry] of this._entries) {
|
|
200
|
+
if (entry.expiresAt <= now) {
|
|
201
|
+
this._entries.delete(key);
|
|
202
|
+
changed = true;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return changed;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
_startEvictionSweep() {
|
|
209
|
+
const tick = () => {
|
|
210
|
+
if (this._disposed) return;
|
|
211
|
+
if (this._dropExpired()) this._schedulePersist();
|
|
212
|
+
this._sweepTimer = setTimeout(tick, this._evictionIntervalMs);
|
|
213
|
+
if (this._sweepTimer && typeof this._sweepTimer.unref === "function") {
|
|
214
|
+
this._sweepTimer.unref();
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
this._sweepTimer = setTimeout(tick, this._evictionIntervalMs);
|
|
218
|
+
if (this._sweepTimer && typeof this._sweepTimer.unref === "function") {
|
|
219
|
+
this._sweepTimer.unref();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
_schedulePersist() {
|
|
224
|
+
if (!this._storage || this._disposed) return;
|
|
225
|
+
if (this._persistTimer) clearTimeout(this._persistTimer);
|
|
226
|
+
this._persistTimer = setTimeout(() => {
|
|
227
|
+
this._persistTimer = null;
|
|
228
|
+
this._persist();
|
|
229
|
+
}, this._debounceMs);
|
|
230
|
+
if (this._persistTimer && typeof this._persistTimer.unref === "function") {
|
|
231
|
+
this._persistTimer.unref();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Writes the current snapshot to storage; errors are swallowed by design. */
|
|
236
|
+
_persist() {
|
|
237
|
+
if (!this._storage) return;
|
|
238
|
+
try {
|
|
239
|
+
const serializable = {};
|
|
240
|
+
for (const [key, entry] of this._entries) serializable[key] = entry;
|
|
241
|
+
const result = this._storage.update(this._persistenceKey, serializable);
|
|
242
|
+
if (result && typeof result.catch === "function") result.catch(() => {});
|
|
243
|
+
} catch {
|
|
244
|
+
// Persistence is best-effort; the in-memory cache stays authoritative.
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Restores a previously persisted snapshot, ignoring malformed payloads. */
|
|
249
|
+
_restore() {
|
|
250
|
+
if (!this._storage) return;
|
|
251
|
+
try {
|
|
252
|
+
const restored = this._storage.get(this._persistenceKey);
|
|
253
|
+
if (!restored || typeof restored !== "object") return;
|
|
254
|
+
for (const key of Object.keys(restored)) {
|
|
255
|
+
const entry = restored[key];
|
|
256
|
+
if (
|
|
257
|
+
entry &&
|
|
258
|
+
typeof entry === "object" &&
|
|
259
|
+
entry.expiresAt > Date.now() &&
|
|
260
|
+
!this._entries.has(key)
|
|
261
|
+
) {
|
|
262
|
+
this._entries.set(key, { value: entry.value, expiresAt: entry.expiresAt });
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
} catch {
|
|
266
|
+
// A corrupted snapshot is treated as absent.
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
module.exports = {
|
|
272
|
+
CacheSyncManager,
|
|
273
|
+
createDefaultConfig,
|
|
274
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vscode-cache-sync",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Workspace cache synchronization for VS Code extensions",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/"
|
|
17
|
+
],
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/elenam27/vscode-cache-sync"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/elenam27/vscode-cache-sync/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/elenam27/vscode-cache-sync#readme",
|
|
26
|
+
"author": {
|
|
27
|
+
"name": "elenam27",
|
|
28
|
+
"url": "https://github.com/elenam27"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"vscode",
|
|
32
|
+
"cache",
|
|
33
|
+
"workspace",
|
|
34
|
+
"extension",
|
|
35
|
+
"ttl"
|
|
36
|
+
],
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=18.0.0"
|
|
39
|
+
}
|
|
40
|
+
}
|