-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d95194
commit 4d132b2
Showing
22 changed files
with
1,040 additions
and
84 deletions.
There are no files selected for viewing
This file contains 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,6 @@ | ||
--- | ||
"@dmno/fastify-integration": patch | ||
"dmno": patch | ||
--- | ||
|
||
initial version of fastify integration |
This file contains 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,18 @@ | ||
import { DmnoBaseTypes, defineDmnoService } from 'dmno'; | ||
|
||
export default defineDmnoService({ | ||
pick: [], | ||
settings: { | ||
preventClientLeaks: true | ||
}, | ||
schema: { | ||
PORT: { | ||
extends: DmnoBaseTypes.port, | ||
value: 3001 | ||
}, | ||
SOME_SECRET: { | ||
value: 'shhh-dont-leak-me', | ||
sensitive: true, | ||
} | ||
}, | ||
}); |
This file contains 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,7 @@ | ||
{ | ||
"extends": "dmno/tsconfigs/dmno-folder", | ||
"include": [ | ||
"./**/*.mts", | ||
"./.typegen/global.d.ts" | ||
] | ||
} |
This file contains 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,24 @@ | ||
{ | ||
"name": "fastify", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "dmno run -w -- vite-node --watch src/index.ts" | ||
}, | ||
"keywords": [ ], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"dmno": "link:../../../packages/core", | ||
"@dmno/fastify-integration": "link:../../../packages/integrations/fastify", | ||
"fastify": "^5.1.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.12.7", | ||
"fastify-tsconfig": "^2.0.0", | ||
"vite": "^5", | ||
"vite-node": "^2.1.8" | ||
} | ||
} |
This file contains 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,4 @@ | ||
// inject DMNO_CONFIG global | ||
/// <reference types="../.dmno/.typegen/global.d.ts" /> | ||
// inject DMNO_PUBLIC_CONFIG global | ||
/// <reference types="../.dmno/.typegen/global-public.d.ts" /> |
This file contains 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,78 @@ | ||
import Fastify from 'fastify'; | ||
import { dmnoFastifyPlugin } from '@dmno/fastify-integration'; | ||
|
||
const fastify = Fastify({ | ||
logger: true, | ||
forceCloseConnections: true, // needed for vite-node HMR | ||
}) | ||
|
||
// register our DMNO fastify plugin | ||
fastify.register(dmnoFastifyPlugin); | ||
|
||
// ~ ROUTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
fastify.get('/', function (request, reply) { | ||
console.log('`console.log` tests ---------------------------------------') | ||
console.log('log a secret:', DMNO_CONFIG.SOME_SECRET); | ||
console.log('log a secret in array:', [DMNO_CONFIG.SOME_SECRET]); | ||
console.log('log a secret in obj:', { secret: DMNO_CONFIG.SOME_SECRET }); | ||
|
||
console.log('`request.log` tests ---------------------------------------') | ||
request.log.info(DMNO_CONFIG.SOME_SECRET, 'asdf'); // added as `msg` | ||
request.log.info([DMNO_CONFIG.SOME_SECRET]); // added as `0` | ||
request.log.info({ secret: DMNO_CONFIG.SOME_SECRET }); // overwrites keys on the obj | ||
|
||
reply.send({ | ||
hello: 'world', | ||
demos: [ | ||
`http://${request.host}/leak`, | ||
`http://${request.host}/intercept` | ||
] | ||
}) | ||
}); | ||
|
||
fastify.get('/leak', function (request, reply) { | ||
// leak the secret as part of the response | ||
reply.send({ | ||
superSecretKey: DMNO_CONFIG.SOME_SECRET, | ||
message: 'this endpoint should throw due to leaking a secret in the response', | ||
}) | ||
}); | ||
|
||
fastify.get('/intercept', async function (request, reply) { | ||
// send a secret to a domain that is not part of the allow list | ||
await fetch('https://api.sampleapis.com/coffee/hot', { | ||
headers: { | ||
'x-auth': DMNO_CONFIG.SOME_SECRET | ||
} | ||
}); | ||
|
||
reply.send({ message: 'this endpoint should throw due to a leaked secret' }); | ||
}); | ||
|
||
// ~ START THE SERVER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
fastify.listen({ port: DMNO_CONFIG.PORT }, function (err, address) { | ||
if (err) { | ||
fastify.log.error(err) | ||
process.exit(1) | ||
} | ||
|
||
console.log(`Server now listening @ ${address.replace('[::1]', 'localhost')}`); | ||
}) | ||
|
||
|
||
// ~ Live reload / HMR using vite-node ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
if (import.meta.hot) { | ||
import.meta.hot.on("vite:beforeFullReload", async () => { | ||
await fastify.close(); | ||
|
||
}); | ||
import.meta.hot.dispose(() => { | ||
console.log('hot.dispose'); | ||
fastify.close() | ||
}); | ||
} | ||
|
||
process.on('exit', () => { | ||
console.log('process.exit'); | ||
fastify.close() | ||
}) |
This file contains 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,13 @@ | ||
{ | ||
"extends": "fastify-tsconfig", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"sourceMap": true, | ||
"composite": true, | ||
"types": [ "vite/client" ], | ||
"strict": true | ||
}, | ||
"include": [ | ||
"src/**/*.ts" | ||
] | ||
} |
Oops, something went wrong.