Skip to content

Commit

Permalink
fixes #7
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina committed Apr 23, 2024
1 parent 14f4f1a commit 57cfd73
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 4 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,45 @@ export default function doWork () {
}
```

### enterWith

If you need to alter the current asynchronous context, you can use the `enterWith` helper.

```js
import { before, describe, it } from "node:test";
import Fastify from "fastify";
import { app, start } from "fastify-asyncforge";
import assert from "node:assert/strict";

let fastify;

async function build(config) {
const server = await Fastify();

server.decorate("config", config);
await start(server);
console.log("config from memo", app().config);

return server;
}

describe("support exiting from a context", () => {
before(async () => {
fastify = await build({ foo: "bar" });
});

it("throws", () => {
assert.throws(app);
});

it("does not throw using enterWith", () => {
fastify.enterWith();
assert.equal(app(), fastify);
});
});
```


## License

MIT
19 changes: 15 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@
const fp = require('fastify-plugin')
const { memo, setAll } = require('asyncforge')

const app = memo()
const request = memo()
const reply = memo()
const logger = memo()
const app = memo('fastify.app')
const request = memo('fastify.request')
const reply = memo('fastify.reply')
const logger = memo('fastify.logger')

const fastifyAsyncForge = fp(function (fastify, opts, next) {
fastify.decorate('enterWith', function () {
try {
app()
} catch {
setAll({
[app.key]: this,
[logger.key]: this.log
})
}
})

fastify.addHook('onRequest', async function (req, res) {
setAll({
[app.key]: this,
Expand Down
29 changes: 29 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,32 @@ test('basic helpers without start', async (t) => {

await p.complete
})

test('fastify.enterWith', async (t) => {
const p = tspl(t, { plan: 7 })
const fastify = Fastify()

await fastify.register(fastifyAsyncForge)

fastify.enterWith()

p.strictEqual(logger(), fastify.log)
p.strictEqual(app(), fastify)

fastify.get('/', async function (_request, _reply) {
p.strictEqual(app(), this)
p.strictEqual(request(), _request)
p.strictEqual(reply(), _reply)
p.strictEqual(logger(), _request.log)
return { hello: 'world' }
})

const res = await fastify.inject({
method: 'GET',
url: '/'
})

p.strictEqual(res.statusCode, 200)

await p.complete
})
30 changes: 30 additions & 0 deletions test/issue-7.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { before, describe, it } from 'node:test'
import Fastify from 'fastify'
import { app, start } from '../index.js'
import assert from 'node:assert/strict'

let fastify

async function build (config) {
const server = await Fastify()

server.decorate('config', config)
await start(server)

return server
}

describe('support exiting from a context', () => {
before(async () => {
fastify = await build({ foo: 'bar' })
})

it('throws', () => {
assert.throws(app)
})

it('does not throw using enterWith', () => {
fastify.enterWith()
assert.equal(app(), fastify)
})
})

0 comments on commit 57cfd73

Please sign in to comment.