layercache 1.0.0 → 1.0.2

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/dist/cli.cjs ADDED
@@ -0,0 +1,228 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+
31
+ // src/cli.ts
32
+ var cli_exports = {};
33
+ __export(cli_exports, {
34
+ main: () => main
35
+ });
36
+ module.exports = __toCommonJS(cli_exports);
37
+ var import_ioredis = __toESM(require("ioredis"), 1);
38
+
39
+ // src/invalidation/PatternMatcher.ts
40
+ var PatternMatcher = class {
41
+ static matches(pattern, value) {
42
+ const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&");
43
+ const regex = new RegExp(`^${escaped.replace(/\*/g, ".*").replace(/\?/g, ".")}$`);
44
+ return regex.test(value);
45
+ }
46
+ };
47
+
48
+ // src/invalidation/RedisTagIndex.ts
49
+ var RedisTagIndex = class {
50
+ client;
51
+ prefix;
52
+ scanCount;
53
+ constructor(options) {
54
+ this.client = options.client;
55
+ this.prefix = options.prefix ?? "layercache:tag-index";
56
+ this.scanCount = options.scanCount ?? 100;
57
+ }
58
+ async touch(key) {
59
+ await this.client.sadd(this.knownKeysKey(), key);
60
+ }
61
+ async track(key, tags) {
62
+ const keyTagsKey = this.keyTagsKey(key);
63
+ const existingTags = await this.client.smembers(keyTagsKey);
64
+ const pipeline = this.client.pipeline();
65
+ pipeline.sadd(this.knownKeysKey(), key);
66
+ for (const tag of existingTags) {
67
+ pipeline.srem(this.tagKeysKey(tag), key);
68
+ }
69
+ pipeline.del(keyTagsKey);
70
+ if (tags.length > 0) {
71
+ pipeline.sadd(keyTagsKey, ...tags);
72
+ for (const tag of new Set(tags)) {
73
+ pipeline.sadd(this.tagKeysKey(tag), key);
74
+ }
75
+ }
76
+ await pipeline.exec();
77
+ }
78
+ async remove(key) {
79
+ const keyTagsKey = this.keyTagsKey(key);
80
+ const existingTags = await this.client.smembers(keyTagsKey);
81
+ const pipeline = this.client.pipeline();
82
+ pipeline.srem(this.knownKeysKey(), key);
83
+ pipeline.del(keyTagsKey);
84
+ for (const tag of existingTags) {
85
+ pipeline.srem(this.tagKeysKey(tag), key);
86
+ }
87
+ await pipeline.exec();
88
+ }
89
+ async keysForTag(tag) {
90
+ return this.client.smembers(this.tagKeysKey(tag));
91
+ }
92
+ async matchPattern(pattern) {
93
+ const matches = [];
94
+ let cursor = "0";
95
+ do {
96
+ const [nextCursor, keys] = await this.client.sscan(
97
+ this.knownKeysKey(),
98
+ cursor,
99
+ "MATCH",
100
+ pattern,
101
+ "COUNT",
102
+ this.scanCount
103
+ );
104
+ cursor = nextCursor;
105
+ matches.push(...keys.filter((key) => PatternMatcher.matches(pattern, key)));
106
+ } while (cursor !== "0");
107
+ return matches;
108
+ }
109
+ async clear() {
110
+ const indexKeys = await this.scanIndexKeys();
111
+ if (indexKeys.length === 0) {
112
+ return;
113
+ }
114
+ await this.client.del(...indexKeys);
115
+ }
116
+ async scanIndexKeys() {
117
+ const matches = [];
118
+ let cursor = "0";
119
+ const pattern = `${this.prefix}:*`;
120
+ do {
121
+ const [nextCursor, keys] = await this.client.scan(cursor, "MATCH", pattern, "COUNT", this.scanCount);
122
+ cursor = nextCursor;
123
+ matches.push(...keys);
124
+ } while (cursor !== "0");
125
+ return matches;
126
+ }
127
+ knownKeysKey() {
128
+ return `${this.prefix}:keys`;
129
+ }
130
+ keyTagsKey(key) {
131
+ return `${this.prefix}:key:${encodeURIComponent(key)}`;
132
+ }
133
+ tagKeysKey(tag) {
134
+ return `${this.prefix}:tag:${encodeURIComponent(tag)}`;
135
+ }
136
+ };
137
+
138
+ // src/cli.ts
139
+ async function main(argv = process.argv.slice(2)) {
140
+ const args = parseArgs(argv);
141
+ if (!args.command || !args.redisUrl) {
142
+ printUsage();
143
+ process.exitCode = 1;
144
+ return;
145
+ }
146
+ const redis = new import_ioredis.default(args.redisUrl);
147
+ try {
148
+ if (args.command === "stats") {
149
+ const keys = await scanKeys(redis, args.pattern ?? "*");
150
+ process.stdout.write(`${JSON.stringify({ totalKeys: keys.length, pattern: args.pattern ?? "*" }, null, 2)}
151
+ `);
152
+ return;
153
+ }
154
+ if (args.command === "keys") {
155
+ const keys = await scanKeys(redis, args.pattern ?? "*");
156
+ process.stdout.write(`${keys.join("\n")}
157
+ `);
158
+ return;
159
+ }
160
+ if (args.command === "invalidate") {
161
+ if (args.tag) {
162
+ const tagIndex = new RedisTagIndex({ client: redis, prefix: args.tagIndexPrefix ?? "layercache:tag-index" });
163
+ const keys2 = await tagIndex.keysForTag(args.tag);
164
+ if (keys2.length > 0) {
165
+ await redis.del(...keys2);
166
+ }
167
+ process.stdout.write(`${JSON.stringify({ deletedKeys: keys2.length, tag: args.tag }, null, 2)}
168
+ `);
169
+ return;
170
+ }
171
+ const keys = await scanKeys(redis, args.pattern ?? "*");
172
+ if (keys.length > 0) {
173
+ await redis.del(...keys);
174
+ }
175
+ process.stdout.write(`${JSON.stringify({ deletedKeys: keys.length, pattern: args.pattern ?? "*" }, null, 2)}
176
+ `);
177
+ return;
178
+ }
179
+ printUsage();
180
+ process.exitCode = 1;
181
+ } finally {
182
+ redis.disconnect();
183
+ }
184
+ }
185
+ function parseArgs(argv) {
186
+ const [command, ...rest] = argv;
187
+ const parsed = { command };
188
+ for (let index = 0; index < rest.length; index += 1) {
189
+ const token = rest[index];
190
+ const value = rest[index + 1];
191
+ if (token === "--redis") {
192
+ parsed.redisUrl = value;
193
+ index += 1;
194
+ } else if (token === "--pattern") {
195
+ parsed.pattern = value;
196
+ index += 1;
197
+ } else if (token === "--tag") {
198
+ parsed.tag = value;
199
+ index += 1;
200
+ } else if (token === "--tag-index-prefix") {
201
+ parsed.tagIndexPrefix = value;
202
+ index += 1;
203
+ }
204
+ }
205
+ return parsed;
206
+ }
207
+ async function scanKeys(redis, pattern) {
208
+ const keys = [];
209
+ let cursor = "0";
210
+ do {
211
+ const [nextCursor, batch] = await redis.scan(cursor, "MATCH", pattern, "COUNT", 100);
212
+ cursor = nextCursor;
213
+ keys.push(...batch);
214
+ } while (cursor !== "0");
215
+ return keys;
216
+ }
217
+ function printUsage() {
218
+ process.stdout.write(
219
+ "Usage:\n layercache stats --redis <url> [--pattern <glob>]\n layercache keys --redis <url> [--pattern <glob>]\n layercache invalidate --redis <url> [--pattern <glob> | --tag <tag>] [--tag-index-prefix <prefix>]\n"
220
+ );
221
+ }
222
+ if (process.argv[1]?.includes("cli.")) {
223
+ void main();
224
+ }
225
+ // Annotate the CommonJS export names for ESM import in node:
226
+ 0 && (module.exports = {
227
+ main
228
+ });
package/dist/cli.d.cts ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ declare function main(argv?: string[]): Promise<void>;
3
+
4
+ export { main };
package/dist/cli.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ declare function main(argv?: string[]): Promise<void>;
3
+
4
+ export { main };
package/dist/cli.js ADDED
@@ -0,0 +1,96 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ RedisTagIndex
4
+ } from "./chunk-IILH5XTS.js";
5
+
6
+ // src/cli.ts
7
+ import Redis from "ioredis";
8
+ async function main(argv = process.argv.slice(2)) {
9
+ const args = parseArgs(argv);
10
+ if (!args.command || !args.redisUrl) {
11
+ printUsage();
12
+ process.exitCode = 1;
13
+ return;
14
+ }
15
+ const redis = new Redis(args.redisUrl);
16
+ try {
17
+ if (args.command === "stats") {
18
+ const keys = await scanKeys(redis, args.pattern ?? "*");
19
+ process.stdout.write(`${JSON.stringify({ totalKeys: keys.length, pattern: args.pattern ?? "*" }, null, 2)}
20
+ `);
21
+ return;
22
+ }
23
+ if (args.command === "keys") {
24
+ const keys = await scanKeys(redis, args.pattern ?? "*");
25
+ process.stdout.write(`${keys.join("\n")}
26
+ `);
27
+ return;
28
+ }
29
+ if (args.command === "invalidate") {
30
+ if (args.tag) {
31
+ const tagIndex = new RedisTagIndex({ client: redis, prefix: args.tagIndexPrefix ?? "layercache:tag-index" });
32
+ const keys2 = await tagIndex.keysForTag(args.tag);
33
+ if (keys2.length > 0) {
34
+ await redis.del(...keys2);
35
+ }
36
+ process.stdout.write(`${JSON.stringify({ deletedKeys: keys2.length, tag: args.tag }, null, 2)}
37
+ `);
38
+ return;
39
+ }
40
+ const keys = await scanKeys(redis, args.pattern ?? "*");
41
+ if (keys.length > 0) {
42
+ await redis.del(...keys);
43
+ }
44
+ process.stdout.write(`${JSON.stringify({ deletedKeys: keys.length, pattern: args.pattern ?? "*" }, null, 2)}
45
+ `);
46
+ return;
47
+ }
48
+ printUsage();
49
+ process.exitCode = 1;
50
+ } finally {
51
+ redis.disconnect();
52
+ }
53
+ }
54
+ function parseArgs(argv) {
55
+ const [command, ...rest] = argv;
56
+ const parsed = { command };
57
+ for (let index = 0; index < rest.length; index += 1) {
58
+ const token = rest[index];
59
+ const value = rest[index + 1];
60
+ if (token === "--redis") {
61
+ parsed.redisUrl = value;
62
+ index += 1;
63
+ } else if (token === "--pattern") {
64
+ parsed.pattern = value;
65
+ index += 1;
66
+ } else if (token === "--tag") {
67
+ parsed.tag = value;
68
+ index += 1;
69
+ } else if (token === "--tag-index-prefix") {
70
+ parsed.tagIndexPrefix = value;
71
+ index += 1;
72
+ }
73
+ }
74
+ return parsed;
75
+ }
76
+ async function scanKeys(redis, pattern) {
77
+ const keys = [];
78
+ let cursor = "0";
79
+ do {
80
+ const [nextCursor, batch] = await redis.scan(cursor, "MATCH", pattern, "COUNT", 100);
81
+ cursor = nextCursor;
82
+ keys.push(...batch);
83
+ } while (cursor !== "0");
84
+ return keys;
85
+ }
86
+ function printUsage() {
87
+ process.stdout.write(
88
+ "Usage:\n layercache stats --redis <url> [--pattern <glob>]\n layercache keys --redis <url> [--pattern <glob>]\n layercache invalidate --redis <url> [--pattern <glob> | --tag <tag>] [--tag-index-prefix <prefix>]\n"
89
+ );
90
+ }
91
+ if (process.argv[1]?.includes("cli.")) {
92
+ void main();
93
+ }
94
+ export {
95
+ main
96
+ };