Web integration for Connect-Like Resources
npm install @travetto/web-connectInstall: @travetto/web-connect
``bash
npm install @travetto/web-connect
yarn add @travetto/web-connect
`
This module provides basic integration for calling connect related middleware with Web API. This logic is not intended to be exhaustive, but intended to provide a quick bridge. This only consumer of this is Web Auth Passport as it needs to bind the WebRequest and WebResponse to standard contracts for passport.
This module is already most likely compatible with quite a bit of middleware, but will fail under any of the following conditions:
* The calling code expects the request or response to be a proper EventEmitter, beyond .on('end', callback)
* The calling code expects the request/response sockets to be live.
* The calling code modifies the shape of the objects (e.g. rewrites the close method on response).
Barring these exceptions, gaps will be filled in as more use cases arise. The above exceptions are non-negotiable as they are are enforced by the invocation method defined by Web API.
Code: Example of using the Connect Adaptor with Passport
`typescript
async authenticate(input: object, ctx: WebFilterContext): Promise
const requestOptions = this.#passportOptions(ctx);
const options = {
session: this.session,
failWithError: true,
...requestOptions,
state: PassportUtil.enhanceState(ctx, requestOptions.state)
};
const user = await WebConnectUtil.invoke
passport.authenticate(this.#strategyName, options, next)(request, response)
);
if (user) {
delete user._raw;
delete user._json;
delete user.source;
return this.#toPrincipal(user, this.#strategyName);
}
}
}
``