@platformatic/runtime 2.30.0 → 2.30.1-alpha.1
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/config.d.ts +1 -1
- package/lib/worker/http-cache.js +70 -4
- package/lib/worker/main.js +4 -3
- package/package.json +16 -15
- package/schema.json +1 -1
package/config.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* and run json-schema-to-typescript to regenerate this file.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
export type
|
|
8
|
+
export type HttpsSchemasPlatformaticDevPlatformaticRuntime2301Alpha1Json = {
|
|
9
9
|
[k: string]: unknown;
|
|
10
10
|
} & {
|
|
11
11
|
$schema?: string;
|
package/lib/worker/http-cache.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
const { randomUUID } = require('node:crypto')
|
|
3
4
|
const { Readable, Writable } = require('node:stream')
|
|
5
|
+
const { interceptors } = require('undici')
|
|
4
6
|
const { kITC } = require('./symbols')
|
|
5
7
|
|
|
8
|
+
const kCacheIdHeader = Symbol('cacheIdHeader')
|
|
9
|
+
const CACHE_ID_HEADER = 'x-plt-http-cache-id'
|
|
10
|
+
|
|
6
11
|
const noop = () => {}
|
|
7
12
|
|
|
8
13
|
class RemoteCacheStore {
|
|
@@ -56,13 +61,19 @@ class RemoteCacheStore {
|
|
|
56
61
|
}
|
|
57
62
|
}
|
|
58
63
|
|
|
59
|
-
createWriteStream (
|
|
64
|
+
createWriteStream (key, value) {
|
|
65
|
+
const cacheEntryId = value.headers?.[kCacheIdHeader]
|
|
66
|
+
if (cacheEntryId) {
|
|
67
|
+
key = { ...key, id: cacheEntryId }
|
|
68
|
+
value.headers = { ...value.headers, [CACHE_ID_HEADER]: cacheEntryId }
|
|
69
|
+
}
|
|
70
|
+
|
|
60
71
|
const itc = globalThis[kITC]
|
|
61
72
|
if (!itc) throw new Error('Cannot write to cache without an ITC instance')
|
|
62
73
|
|
|
63
74
|
let payload = ''
|
|
64
75
|
|
|
65
|
-
|
|
76
|
+
key = this.#sanitizeRequest(key)
|
|
66
77
|
|
|
67
78
|
return new Writable({
|
|
68
79
|
write (chunk, encoding, callback) {
|
|
@@ -70,7 +81,7 @@ class RemoteCacheStore {
|
|
|
70
81
|
callback()
|
|
71
82
|
},
|
|
72
83
|
final (callback) {
|
|
73
|
-
itc.send('setHttpCacheValue', { request, response, payload })
|
|
84
|
+
itc.send('setHttpCacheValue', { request: key, response: value, payload })
|
|
74
85
|
.then(() => callback())
|
|
75
86
|
.catch((err) => callback(err))
|
|
76
87
|
}
|
|
@@ -96,4 +107,59 @@ class RemoteCacheStore {
|
|
|
96
107
|
}
|
|
97
108
|
}
|
|
98
109
|
|
|
99
|
-
|
|
110
|
+
const httpCacheInterceptor = (interceptorOpts) => {
|
|
111
|
+
const originalInterceptor = interceptors.cache(interceptorOpts)
|
|
112
|
+
|
|
113
|
+
// AsyncLocalStorage that contains a client http request span
|
|
114
|
+
// Exists only when the nodejs stackable telemetry is enabled
|
|
115
|
+
const clientSpansAls = globalThis.platformatic.clientSpansAls
|
|
116
|
+
|
|
117
|
+
return (originalDispatch) => {
|
|
118
|
+
const dispatch = (opts, handler) => {
|
|
119
|
+
const originOnResponseStart = handler.onResponseStart.bind(handler)
|
|
120
|
+
handler.onResponseStart = (ac, statusCode, headers, statusMessage) => {
|
|
121
|
+
// Setting a potentially cache entry id when cache miss happens
|
|
122
|
+
headers[kCacheIdHeader] = randomUUID()
|
|
123
|
+
return originOnResponseStart(ac, statusCode, headers, statusMessage)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return originalDispatch(opts, handler)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const dispatcher = originalInterceptor(dispatch)
|
|
130
|
+
|
|
131
|
+
return (opts, handler) => {
|
|
132
|
+
const originOnResponseStart = handler.onResponseStart.bind(handler)
|
|
133
|
+
handler.onResponseStart = (ac, statusCode, headers, statusMessage) => {
|
|
134
|
+
const cacheEntryId = headers[kCacheIdHeader] ?? headers[CACHE_ID_HEADER]
|
|
135
|
+
const isCacheHit = headers.age !== undefined
|
|
136
|
+
|
|
137
|
+
if (cacheEntryId) {
|
|
138
|
+
// Setting a cache id header on cache hit
|
|
139
|
+
headers[CACHE_ID_HEADER] = cacheEntryId
|
|
140
|
+
delete headers[kCacheIdHeader]
|
|
141
|
+
|
|
142
|
+
if (clientSpansAls) {
|
|
143
|
+
try {
|
|
144
|
+
const { span } = clientSpansAls.getStore()
|
|
145
|
+
if (span) {
|
|
146
|
+
span.setAttribute('http.cache.id', cacheEntryId)
|
|
147
|
+
span.setAttribute('http.cache.hit', isCacheHit.toString())
|
|
148
|
+
}
|
|
149
|
+
} catch (err) {
|
|
150
|
+
interceptorOpts.logger.error(err, 'Error setting cache id on span')
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return originOnResponseStart(ac, statusCode, headers, statusMessage)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (!clientSpansAls) {
|
|
158
|
+
return dispatcher(opts, handler)
|
|
159
|
+
}
|
|
160
|
+
return clientSpansAls.run({ span: null }, () => dispatcher(opts, handler))
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
module.exports = { RemoteCacheStore, httpCacheInterceptor }
|
package/lib/worker/main.js
CHANGED
|
@@ -16,7 +16,7 @@ const { fetch, setGlobalDispatcher, getGlobalDispatcher, Agent } = require('undi
|
|
|
16
16
|
const { wire } = require('undici-thread-interceptor')
|
|
17
17
|
const undici = require('undici')
|
|
18
18
|
|
|
19
|
-
const RemoteCacheStore = require('./http-cache')
|
|
19
|
+
const { RemoteCacheStore, httpCacheInterceptor } = require('./http-cache')
|
|
20
20
|
const { PlatformaticApp } = require('./app')
|
|
21
21
|
const { setupITC } = require('./itc')
|
|
22
22
|
const { loadInterceptors } = require('./interceptors')
|
|
@@ -158,7 +158,7 @@ async function main () {
|
|
|
158
158
|
if (config.httpCache) {
|
|
159
159
|
setGlobalDispatcher(
|
|
160
160
|
getGlobalDispatcher().compose(
|
|
161
|
-
|
|
161
|
+
httpCacheInterceptor({
|
|
162
162
|
store: new RemoteCacheStore({
|
|
163
163
|
onRequest: (opts) => {
|
|
164
164
|
globalThis.platformatic?.onHttpCacheRequest?.(opts)
|
|
@@ -171,7 +171,8 @@ async function main () {
|
|
|
171
171
|
},
|
|
172
172
|
logger: globalThis.platformatic.logger
|
|
173
173
|
}),
|
|
174
|
-
methods: config.httpCache.methods ?? ['GET', 'HEAD']
|
|
174
|
+
methods: config.httpCache.methods ?? ['GET', 'HEAD'],
|
|
175
|
+
logger: globalThis.platformatic.logger
|
|
175
176
|
})
|
|
176
177
|
)
|
|
177
178
|
)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/runtime",
|
|
3
|
-
"version": "2.30.
|
|
3
|
+
"version": "2.30.1-alpha.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
@@ -35,18 +35,19 @@
|
|
|
35
35
|
"typescript": "^5.5.4",
|
|
36
36
|
"undici-oidc-interceptor": "^0.5.0",
|
|
37
37
|
"why-is-node-running": "^2.2.2",
|
|
38
|
-
"@platformatic/composer": "2.30.
|
|
39
|
-
"@platformatic/db": "2.30.
|
|
40
|
-
"@platformatic/
|
|
41
|
-
"@platformatic/
|
|
42
|
-
"@platformatic/
|
|
43
|
-
"@platformatic/sql-
|
|
38
|
+
"@platformatic/composer": "2.30.1-alpha.1",
|
|
39
|
+
"@platformatic/db": "2.30.1-alpha.1",
|
|
40
|
+
"@platformatic/service": "2.30.1-alpha.1",
|
|
41
|
+
"@platformatic/sql-graphql": "2.30.1-alpha.1",
|
|
42
|
+
"@platformatic/node": "2.30.1-alpha.1",
|
|
43
|
+
"@platformatic/sql-mapper": "2.30.1-alpha.1"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@fastify/accepts": "^5.0.0",
|
|
47
47
|
"@fastify/error": "^4.0.0",
|
|
48
48
|
"@fastify/websocket": "^11.0.0",
|
|
49
49
|
"@hapi/topo": "^6.0.2",
|
|
50
|
+
"@opentelemetry/api": "^1.8.0",
|
|
50
51
|
"@platformatic/http-metrics": "^0.2.1",
|
|
51
52
|
"@platformatic/undici-cache-memory": "^0.8.1",
|
|
52
53
|
"@watchable/unpromise": "^1.0.2",
|
|
@@ -71,15 +72,15 @@
|
|
|
71
72
|
"tail-file-stream": "^0.2.0",
|
|
72
73
|
"thread-cpu-usage": "^0.2.0",
|
|
73
74
|
"undici": "^7.0.0",
|
|
74
|
-
"undici-thread-interceptor": "^0.10.
|
|
75
|
+
"undici-thread-interceptor": "^0.10.3",
|
|
75
76
|
"ws": "^8.16.0",
|
|
76
|
-
"@platformatic/basic": "2.30.
|
|
77
|
-
"@platformatic/config": "2.30.
|
|
78
|
-
"@platformatic/
|
|
79
|
-
"@platformatic/telemetry": "2.30.
|
|
80
|
-
"@platformatic/
|
|
81
|
-
"@platformatic/
|
|
82
|
-
"@platformatic/utils": "2.30.
|
|
77
|
+
"@platformatic/basic": "2.30.1-alpha.1",
|
|
78
|
+
"@platformatic/config": "2.30.1-alpha.1",
|
|
79
|
+
"@platformatic/generators": "2.30.1-alpha.1",
|
|
80
|
+
"@platformatic/telemetry": "2.30.1-alpha.1",
|
|
81
|
+
"@platformatic/ts-compiler": "2.30.1-alpha.1",
|
|
82
|
+
"@platformatic/itc": "2.30.1-alpha.1",
|
|
83
|
+
"@platformatic/utils": "2.30.1-alpha.1"
|
|
83
84
|
},
|
|
84
85
|
"scripts": {
|
|
85
86
|
"test": "npm run lint && borp --concurrency=1 --timeout=300000 && tsd",
|
package/schema.json
CHANGED