Repository navigation
feat(server): GET /version answers the server package and its commit #2097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yyyyaaa
wants to merge
1
commit into
main
Choose a base branch
from
feat/server-version
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { svcCache } from '@pgpmjs/server-utils'; | ||
| import type { Express } from 'express'; | ||
| import { readFileSync } from 'fs'; | ||
| import { graphileCache } from 'graphile-cache'; | ||
| import { join } from 'path'; | ||
| import supertest from 'supertest'; | ||
|
|
||
| import { Server } from '../../server'; | ||
|
|
||
| const pkg = JSON.parse(readFileSync(join(__dirname, '../../../package.json'), 'utf8')); | ||
|
|
||
| const servers: Server[] = []; | ||
|
|
||
| // Port 1 refuses every connection, so a request that reaches the database or | ||
| // the routing of a host fails instead of answering. | ||
| const appWith = (commit: string | undefined): Express => { | ||
| const previous = process.env.SERVER_COMMIT; | ||
| if (commit === undefined) delete process.env.SERVER_COMMIT; | ||
| else process.env.SERVER_COMMIT = commit; | ||
| try { | ||
| const server = new Server({ pg: { host: '127.0.0.1', port: 1, database: 'version_route_test' } }); | ||
| servers.push(server); | ||
| return (server as unknown as { app: Express }).app; | ||
| } finally { | ||
| if (previous === undefined) delete process.env.SERVER_COMMIT; | ||
| else process.env.SERVER_COMMIT = previous; | ||
| } | ||
| }; | ||
|
|
||
| describe('GET /version', () => { | ||
| afterEach(async () => { | ||
| for (const server of servers.splice(0)) await server.close(); | ||
| svcCache.clear(); | ||
| graphileCache.clear(); | ||
| }); | ||
|
|
||
| it('answers the package and the commit of SERVER_COMMIT, uncached', async () => { | ||
| const res = await supertest(appWith('0123456789abcdef0123456789abcdef01234567')) | ||
| .get('/version').set('Host', 'api.localhost'); | ||
|
|
||
| expect(res.status).toBe(200); | ||
| expect(res.headers['cache-control']).toBe('no-store'); | ||
| expect(res.body).toEqual({ name: pkg.name, version: pkg.version, commit: '0123456789abcdef0123456789abcdef01234567' }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['unset', undefined], | ||
| ['empty, as an image built without the argument has it', ''] | ||
| ])('answers a null commit when SERVER_COMMIT is %s', async (_what, commit) => { | ||
| const res = await supertest(appWith(commit)).get('/version').set('Host', 'api.localhost'); | ||
|
|
||
| expect(res.status).toBe(200); | ||
| expect(res.body).toEqual({ name: pkg.name, version: pkg.version, commit: null }); | ||
| }); | ||
|
|
||
| it('answers on a host the server does not route, with a bad credential, without the database', async () => { | ||
| const res = await supertest(appWith('abc1234')) | ||
| .get('/version').set('Host', 'nowhere.invalid').set('Authorization', 'Bearer not-a-token'); | ||
|
|
||
| expect(res.status).toBe(200); | ||
| expect(res.body).toEqual({ name: pkg.name, version: pkg.version, commit: 'abc1234' }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import type { RequestHandler } from 'express'; | ||
| import { findAndRequirePackageJson } from 'find-and-require-package-json'; | ||
|
|
||
| const pkg = findAndRequirePackageJson(__dirname); | ||
|
|
||
| /** | ||
| * GET /version: the server package and the commit this process runs, so a run | ||
| * report can say what it tested. The server mounts it before routing, so it | ||
| * answers on every host without a credential or the database. | ||
| */ | ||
| export const version = (commit: string | undefined): RequestHandler => { | ||
| const body = { name: pkg.name, version: pkg.version, commit: commit || null }; | ||
| return (_req, res) => { | ||
| res.setHeader('Cache-Control', 'no-store'); | ||
| res.json(body); | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep module-scope
__dirnameout of the ESM build.graphql/server/src/middleware/version.ts:4reads__dirnamewhile the module loads.tsconfig.esm.jsondoes not exclude this file, sodist/esmkeeps a top-level__dirnamereference. The documentedmakage build-tssteps do not rewrite it. The packagemodulefield points toesm/index.js, which loadsesm/server.jsand thenesm/middleware/version.js, so the reference is evaluated on import.ES modules do not define
__dirname. A bundler that resolves themoduleentry and does not supply__dirnameproduces code that throws aReferenceErrorat import, before any request is handled. Consumers that load the package throughmain(CommonJS), or through a bundler that supplies__dirname, are not affected. The other__dirnameuse ingraphql/server/srcis inside a function body and does not run at import.Read the package metadata without evaluating
__dirnameat module load. The change must work in both the CommonJS and ESM builds. TheGET /versionresponse and thecommitvalue must not change.