Skip to content

Commit

Permalink
sparql-proxy: configure server-timing
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicm67 committed Jun 12, 2024
1 parent 1a04ad8 commit f0452b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-papayas-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zazuko/trifid-plugin-sparql-proxy": minor
---

Returns `Server-Timing` as response header containing the duration of the request to the configured endpoint.
6 changes: 6 additions & 0 deletions packages/sparql-proxy/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-check

import { Readable } from 'node:stream'
import { performance } from 'node:perf_hooks'
import { sparqlGetRewriteConfiguration } from 'trifid-core'
import replaceStream from 'string-replace-stream'

Expand Down Expand Up @@ -155,11 +156,15 @@ const factory = async (trifid) => {
if (authorizationHeader) {
headers.Authorization = authorizationHeader
}

const start = performance.now()
const response = await fetch(options.endpointUrl, {
method: 'POST',
headers,
body: new URLSearchParams({ query }),
})
const end = performance.now()
const duration = end - start

const contentType = response.headers.get('content-type')

Expand All @@ -175,6 +180,7 @@ const factory = async (trifid) => {

return reply
.status(response.status)
.header('Server-Timing', `sparql-proxy;dur=${duration};desc="Querying the endpoint"`)
.header('content-type', contentType)
.send(responseStream)
} catch (error) {
Expand Down

0 comments on commit f0452b1

Please sign in to comment.