Skip to content

Commit

Permalink
mvp of fastify integration
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim committed Dec 3, 2024
1 parent 6d95194 commit 4d132b2
Show file tree
Hide file tree
Showing 22 changed files with 1,040 additions and 84 deletions.
6 changes: 6 additions & 0 deletions .changeset/good-vans-hide.md
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
18 changes: 18 additions & 0 deletions example-repo/packages/fastify/.dmno/config.mts
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,
}
},
});
7 changes: 7 additions & 0 deletions example-repo/packages/fastify/.dmno/tsconfig.json
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"
]
}
24 changes: 24 additions & 0 deletions example-repo/packages/fastify/package.json
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"
}
}
4 changes: 4 additions & 0 deletions example-repo/packages/fastify/src/dmno-env.d.ts
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" />
78 changes: 78 additions & 0 deletions example-repo/packages/fastify/src/index.ts
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()
})
13 changes: 13 additions & 0 deletions example-repo/packages/fastify/tsconfig.json
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"
]
}
Loading

0 comments on commit 4d132b2

Please sign in to comment.