@platformatic/next 2.21.0-alpha.1 → 2.21.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.
@@ -38,31 +38,60 @@ export function getConnection (url) {
38
38
  }
39
39
 
40
40
  export class CacheHandler {
41
+ #standalone
41
42
  #config
42
43
  #logger
43
44
  #store
45
+ #prefix
44
46
  #subprefix
47
+ #meta
45
48
  #maxTTL
46
49
 
47
50
  constructor (options) {
48
51
  options ??= {}
49
52
 
50
- this.#config = options.config ?? globalThis.platformatic?.config?.cache
53
+ this.#standalone = options.standalone
54
+ this.#config = options.config
55
+ this.#logger = options.logger
56
+ this.#store = options.store
57
+ this.#maxTTL = options.maxTTL
58
+ this.#prefix = options.prefix
59
+ this.#subprefix = options.subprefix
60
+ this.#meta = options.meta
61
+
62
+ if (globalThis.platformatic) {
63
+ this.#config ??= globalThis.platformatic.config.cache
64
+ this.#logger ??= this.#createPlatformaticLogger()
65
+ this.#store ??= getConnection(this.#config.url)
66
+ this.#maxTTL ??= this.#config.maxTTL
67
+ this.#prefix ??= this.#config.prefix
68
+ this.#subprefix ??= this.#getPlatformaticSubprefix()
69
+ this.#meta ??= this.#getPlatformaticMeta()
70
+ } else {
71
+ this.#config ??= {}
72
+ this.#maxTTL ??= 86_400
73
+ this.#prefix ??= ''
74
+ this.#subprefix ??= ''
75
+ this.#meta ??= {}
76
+ }
51
77
 
52
78
  if (!this.#config) {
53
- throw new Error('Please provide a valid configuration.')
79
+ throw new Error('Please provide a the "config" option.')
80
+ }
81
+
82
+ if (!this.#logger) {
83
+ throw new Error('Please provide a the "logger" option.')
54
84
  }
55
85
 
56
- this.#logger = options.logger ?? this.#createLogger()
57
- this.#store = options.store ?? getConnection(this.#config.url)
58
- this.#maxTTL = options.maxTTL ?? this.#config.maxTTL ?? 86_400
59
- this.#subprefix = options.subprefix ?? this.#getSubprefix()
86
+ if (!this.#store) {
87
+ throw new Error('Please provide a the "store" option.')
88
+ }
60
89
  }
61
90
 
62
- async get (cacheKey, isRedisKey) {
91
+ async get (cacheKey, _, isRedisKey) {
63
92
  this.#logger.trace({ key: cacheKey }, 'get')
64
93
 
65
- const key = isRedisKey ? cacheKey : this.#keyFor(cacheKey, sections.values)
94
+ const key = this.#standalone || isRedisKey ? cacheKey : this.#keyFor(cacheKey, sections.values)
66
95
 
67
96
  let rawValue
68
97
  try {
@@ -106,11 +135,18 @@ export class CacheHandler {
106
135
  async set (cacheKey, value, { tags, revalidate }, isRedisKey) {
107
136
  this.#logger.trace({ key: cacheKey, value, tags, revalidate }, 'set')
108
137
 
109
- const key = isRedisKey ? cacheKey : this.#keyFor(cacheKey, sections.values)
138
+ const key = this.#standalone || isRedisKey ? cacheKey : this.#keyFor(cacheKey, sections.values)
110
139
 
111
140
  try {
112
141
  // Compute the parameters to save
113
- const data = this.#serialize({ value, tags, lastModified: Date.now(), revalidate, maxTTL: this.#maxTTL })
142
+ const data = this.#serialize({
143
+ value,
144
+ tags,
145
+ lastModified: Date.now(),
146
+ revalidate,
147
+ maxTTL: this.#maxTTL,
148
+ ...this.#meta
149
+ })
114
150
  const expire = Math.min(revalidate, this.#maxTTL)
115
151
 
116
152
  if (expire < 1) {
@@ -142,7 +178,7 @@ export class CacheHandler {
142
178
  async remove (cacheKey, isRedisKey) {
143
179
  this.#logger.trace({ key: cacheKey }, 'remove')
144
180
 
145
- const key = isRedisKey ? cacheKey : this.#keyFor(cacheKey, sections.values)
181
+ const key = this.#standalone || isRedisKey ? cacheKey : this.#keyFor(cacheKey, sections.values)
146
182
 
147
183
  let rawValue
148
184
  try {
@@ -177,7 +213,7 @@ export class CacheHandler {
177
213
  if (Array.isArray(value.tags)) {
178
214
  for (const tag of value.tags) {
179
215
  const tagsKey = this.#keyFor(tag, sections.tags)
180
- this.#store.srem(tagsKey, key, 'gt')
216
+ promises.push(this.#store.srem(tagsKey, key))
181
217
  }
182
218
  }
183
219
 
@@ -247,7 +283,7 @@ export class CacheHandler {
247
283
  await Promise.all(promises)
248
284
  }
249
285
 
250
- #createLogger () {
286
+ #createPlatformaticLogger () {
251
287
  const pinoOptions = {
252
288
  level: globalThis.platformatic?.logLevel ?? 'info'
253
289
  }
@@ -263,7 +299,7 @@ export class CacheHandler {
263
299
  return pino(pinoOptions)
264
300
  }
265
301
 
266
- #getSubprefix () {
302
+ #getPlatformaticSubprefix () {
267
303
  const root = fileURLToPath(globalThis.platformatic.root)
268
304
 
269
305
  return existsSync(resolve(root, '.next/BUILD_ID'))
@@ -271,8 +307,15 @@ export class CacheHandler {
271
307
  : 'development'
272
308
  }
273
309
 
310
+ #getPlatformaticMeta () {
311
+ return {
312
+ serviceId: globalThis.platformatic.serviceId,
313
+ workerId: globalThis.platformatic.workerId
314
+ }
315
+ }
316
+
274
317
  #keyFor (key, section) {
275
- return keyFor(this.#config.prefix, this.#subprefix, section, key)
318
+ return keyFor(this.#prefix, this.#subprefix, section, key)
276
319
  }
277
320
 
278
321
  #serialize (data) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/next",
3
- "version": "2.21.0-alpha.1",
3
+ "version": "2.21.0",
4
4
  "description": "Platformatic Next.js Stackable",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -23,9 +23,9 @@
23
23
  "iovalkey": "^0.2.1",
24
24
  "msgpackr": "^1.11.2",
25
25
  "semver": "^7.6.3",
26
- "@platformatic/basic": "2.21.0-alpha.1",
27
- "@platformatic/config": "2.21.0-alpha.1",
28
- "@platformatic/utils": "2.21.0-alpha.1"
26
+ "@platformatic/basic": "2.21.0",
27
+ "@platformatic/utils": "2.21.0",
28
+ "@platformatic/config": "2.21.0"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@fastify/reply-from": "^11.0.0",
@@ -41,8 +41,8 @@
41
41
  "react-dom": "^18.3.1",
42
42
  "typescript": "^5.5.4",
43
43
  "ws": "^8.18.0",
44
- "@platformatic/composer": "2.21.0-alpha.1",
45
- "@platformatic/service": "2.21.0-alpha.1"
44
+ "@platformatic/composer": "2.21.0",
45
+ "@platformatic/service": "2.21.0"
46
46
  },
47
47
  "scripts": {
48
48
  "test": "npm run lint && borp --concurrency=1 --no-timeout",
package/schema.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "$id": "https://schemas.platformatic.dev/@platformatic/next/2.21.0-alpha.1.json",
2
+ "$id": "https://schemas.platformatic.dev/@platformatic/next/2.21.0.json",
3
3
  "$schema": "http://json-schema.org/draft-07/schema#",
4
4
  "title": "Platformatic Next.js Stackable",
5
5
  "type": "object",