@curveball/core
Version:
Curveball is a framework writting in Typescript for Node.js
58 lines • 1.86 kB
JavaScript
import rawBody from 'raw-body';
import { Headers, Request } from '@curveball/kernel';
export class NodeRequest extends Request {
/**
* Node.js Request object
*/
inner;
constructor(inner, origin) {
super(inner.method, inner.url, origin);
this.inner = inner;
// @ts-expect-error ignoring that headers might be undefined
this.headers = new Headers(this.inner.headers);
}
rawBody(encoding, limit) {
const options = {};
if (limit) {
options.limit = limit;
}
if (encoding) {
options.encoding = encoding;
}
const length = this.headers.get('Content-Length');
if (length) {
options.length = length;
}
return rawBody(this.inner, options);
}
/**
* getStream returns a Node.js readable stream.
*
* A stream can typically only be read once.
*/
getStream() {
return this.inner;
}
/**
* Returns the IP address of the HTTP client.
*
* If trustProxy is set to true, it means this server is running behind a
* proxy, and the X-Forwarded-For header should be used instead.
*
* If trustProxy is not set, it defaults to false unless the
* CURVEBALL_TRUSTPROXY environment variable is set. Using an environment
* variable is a good idea for this as having a proxy may be environment
* dependent.
*/
ip(trustProxy) {
if (trustProxy === true || process.env.CURVEBALL_TRUSTPROXY) {
const forwardedForHeader = this.headers.get('X-Forwarded-For');
if (forwardedForHeader) {
return forwardedForHeader.split(',')[0].trim();
}
}
return this.inner.socket.remoteAddress;
}
}
export default NodeRequest;
//# sourceMappingURL=request.js.map