generated from metcoder95/lib_template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
a093f8a
commit 035c117
Showing
5 changed files
with
260 additions
and
26 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
npm run test:ci | ||
npm run test |
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 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
npm run test:ci |
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
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 |
---|---|---|
@@ -1 +1,34 @@ | ||
/// <reference types="node" /> | ||
import { FastifyPluginCallback } from 'fastify' | ||
import { FastifyError } from '@fastify/error' | ||
|
||
export interface FastifyRacingSignal extends AbortSignal { | ||
then: ( | ||
onFulfilled?: (value: AbortEvent) => void | PromiseLike<unknown>, | ||
onRejected?: (reason: Error | FastifyError) => void | PromiseLike<unknown> | ||
) => void | Promise<unknown> | ||
} | ||
|
||
export interface AbortEvent { | ||
type: 'abort' | string | ||
reason?: FastifyError | Error | ||
} | ||
|
||
export interface FastifyRacingOptions { | ||
handleError?: boolean | ||
onRequestClosed?: ((evt: AbortEvent) => void) | null | ||
} | ||
|
||
declare module 'fastify' { | ||
interface FastifyRequest { | ||
race(cb: FastifyRacingOptions['onRequestClosed']): void | ||
race(opts: Omit<FastifyRacingOptions, 'onRequestClosed'>): FastifyRacingSignal | ||
race(opts: Omit<FastifyRacingOptions, 'onRequestClosed'>): Promise<AbortEvent> | ||
race(): FastifyRacingSignal | ||
race(): Promise<AbortEvent> | ||
} | ||
} | ||
|
||
declare const FastifyRacing: FastifyPluginCallback<FastifyRacingOptions> | ||
|
||
export default FastifyRacing |
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 |
---|---|---|
@@ -1,26 +1,223 @@ | ||
/// <reference types="node" /> | ||
import { FastifyPluginCallback } from 'fastify'; | ||
import { FastifyError } from '@fastify/error'; | ||
|
||
|
||
interface AbortEvent { | ||
type: 'abort' | string; | ||
reason?: FastifyError | Error | ||
} | ||
|
||
interface FastifyRacing { | ||
handleError?: boolean; | ||
onRequestClosed?: (evt: AbortEvent) => void; | ||
} | ||
|
||
declare module 'fastify' { | ||
interface FastifyInstance { | ||
race(cb: FastifyRacing['onRequestClosed']): void | ||
race(opts: Omit<FastifyRacing, 'onRequestClosed'>): Promise<AbortEvent> | ||
race(): Promise<AbortEvent> | ||
import { expectType } from 'tsd' | ||
|
||
import fastify from 'fastify' | ||
import plugin, { AbortEvent, FastifyRacingSignal } from '..' | ||
|
||
const serverHttp = fastify() | ||
|
||
serverHttp.register(plugin) | ||
|
||
serverHttp.register(plugin, { | ||
handleError: true, | ||
onRequestClosed: null | ||
}) | ||
|
||
serverHttp.get( | ||
'/', | ||
{ | ||
preHandler: async (request, _reply) => { | ||
const signal = request.race() | ||
const signal2 = request.race({ | ||
handleError: true | ||
}) | ||
const event = await request.race() | ||
const event2 = await request.race({ | ||
handleError: true | ||
}) | ||
|
||
const asVoid = request.race(evt => { | ||
expectType<AbortEvent>(evt) | ||
}) | ||
|
||
expectType<void>(asVoid) | ||
expectType<FastifyRacingSignal>(signal) | ||
expectType<AbortEvent>(event) | ||
expectType<FastifyRacingSignal>(signal2) | ||
expectType<AbortEvent>(event2) | ||
} | ||
}, | ||
async (request, reply) => { | ||
const signal = request.race() | ||
const signal2 = request.race({ | ||
handleError: true | ||
}) | ||
const event = await request.race() | ||
const event2 = await request.race({ | ||
handleError: true | ||
}) | ||
|
||
const asVoid = request.race(evt => { | ||
expectType<AbortEvent>(evt) | ||
}) | ||
|
||
expectType<void>(asVoid) | ||
expectType<FastifyRacingSignal>(signal) | ||
expectType<AbortEvent>(event) | ||
expectType<FastifyRacingSignal>(signal2) | ||
expectType<AbortEvent>(event2) | ||
} | ||
) | ||
|
||
// -> Second level | ||
serverHttp.register( | ||
function (fastifyInstance, opts, done) { | ||
fastifyInstance.register(plugin) | ||
|
||
fastifyInstance.get( | ||
'/', | ||
{ | ||
preHandler: async (request, _reply) => { | ||
const signal = request.race() | ||
const signal2 = request.race({ | ||
handleError: true | ||
}) | ||
const event = await request.race() | ||
const event2 = await request.race({ | ||
handleError: true | ||
}) | ||
const asVoid = request.race(evt => { | ||
expectType<AbortEvent>(evt) | ||
}) | ||
|
||
expectType<void>(asVoid) | ||
expectType<FastifyRacingSignal>(signal) | ||
expectType<AbortEvent>(event) | ||
expectType<FastifyRacingSignal>(signal2) | ||
expectType<AbortEvent>(event2) | ||
} | ||
}, | ||
async (request, reply) => { | ||
const signal = request.race() | ||
const signal2 = request.race({ | ||
handleError: true | ||
}) | ||
const event = await request.race() | ||
const event2 = await request.race({ | ||
handleError: true | ||
}) | ||
|
||
const asVoid = request.race(evt => { | ||
expectType<AbortEvent>(evt) | ||
}) | ||
|
||
expectType<void>(asVoid) | ||
expectType<FastifyRacingSignal>(signal) | ||
expectType<AbortEvent>(event) | ||
expectType<FastifyRacingSignal>(signal2) | ||
expectType<AbortEvent>(event2) | ||
} | ||
) | ||
|
||
done() | ||
}, | ||
{ prefix: '/api' } | ||
) | ||
|
||
const serverHttp2 = fastify({ http2: true }) | ||
|
||
serverHttp2.register(plugin, { | ||
handleError: true, | ||
onRequestClosed: null | ||
}) | ||
|
||
serverHttp2.get( | ||
'/', | ||
{ | ||
preHandler: async (request, _reply) => { | ||
const signal = request.race() | ||
const signal2 = request.race({ | ||
handleError: true | ||
}) | ||
const event = await request.race() | ||
const event2 = await request.race({ | ||
handleError: true | ||
}) | ||
|
||
const asVoid = request.race(evt => { | ||
expectType<AbortEvent>(evt) | ||
}) | ||
|
||
expectType<void>(asVoid) | ||
expectType<FastifyRacingSignal>(signal) | ||
expectType<AbortEvent>(event) | ||
expectType<FastifyRacingSignal>(signal2) | ||
expectType<AbortEvent>(event2) | ||
} | ||
}, | ||
async (request, reply) => { | ||
const signal = request.race() | ||
const signal2 = request.race({ | ||
handleError: true | ||
}) | ||
const event = await request.race() | ||
const event2 = await request.race({ | ||
handleError: true | ||
}) | ||
|
||
const asVoid = request.race(evt => { | ||
expectType<AbortEvent>(evt) | ||
}) | ||
|
||
expectType<void>(asVoid) | ||
expectType<FastifyRacingSignal>(signal) | ||
expectType<AbortEvent>(event) | ||
expectType<FastifyRacingSignal>(signal2) | ||
expectType<AbortEvent>(event2) | ||
} | ||
} | ||
) | ||
|
||
// -> First plugin | ||
serverHttp2.register( | ||
function (fastifyInstance, opts, done) { | ||
fastifyInstance.register(plugin) | ||
|
||
fastifyInstance.get( | ||
'/', | ||
{ | ||
preHandler: async (request, _reply) => { | ||
const signal = request.race() | ||
const signal2 = request.race({ | ||
handleError: true | ||
}) | ||
const event = await request.race() | ||
const event2 = await request.race({ | ||
handleError: true | ||
}) | ||
|
||
const asVoid = request.race(evt => { | ||
expectType<AbortEvent>(evt) | ||
}) | ||
|
||
expectType<void>(asVoid) | ||
expectType<FastifyRacingSignal>(signal) | ||
expectType<AbortEvent>(event) | ||
expectType<FastifyRacingSignal>(signal2) | ||
expectType<AbortEvent>(event2) | ||
} | ||
}, | ||
async (request, reply) => { | ||
const signal = request.race() | ||
const signal2 = request.race({ | ||
handleError: true | ||
}) | ||
const event = await request.race() | ||
const event2 = await request.race({ | ||
handleError: true | ||
}) | ||
|
||
const asVoid = request.race(evt => { | ||
expectType<AbortEvent>(evt) | ||
}) | ||
|
||
declare const FastifyRacing: FastifyPluginCallback<FastifyRacing>; | ||
expectType<void>(asVoid) | ||
expectType<FastifyRacingSignal>(signal) | ||
expectType<AbortEvent>(event) | ||
expectType<FastifyRacingSignal>(signal2) | ||
expectType<AbortEvent>(event2) | ||
} | ||
) | ||
|
||
export default FastifyRacing; | ||
done() | ||
}, | ||
{ prefix: '/api' } | ||
) |