Skip to content

fix(fastify) New files requires a server restart to be served #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/.idea
/.awcache
/.vscode
/.history

# misc
npm-debug.log
Expand Down
72 changes: 72 additions & 0 deletions lib/filters/not-found-excepion.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as fs from 'fs';
import { validatePath } from '../utils/validate-path.util';
import {
ArgumentsHost,
Catch,
HttpException,
NotFoundException
} from '@nestjs/common';
import { ServeStaticModuleOptions } from '../interfaces/serve-static-options.interface';
import { AbstractLoader } from '../loaders/abstract.loader';
import {
DEFAULT_RENDER_PATH,
DEFAULT_ROOT_PATH
} from '../serve-static.constants';
import { BaseExceptionFilter, HttpAdapterHost } from '@nestjs/core';
import { wildcardToRegExp } from '../utils/wilcard-to-reg-exp.util';

@Catch(NotFoundException)
export class NotFoundExceptionFilter extends BaseExceptionFilter {
constructor(
protected httpAdapterHost: HttpAdapterHost,
private loader: AbstractLoader,
private optionsArr: ServeStaticModuleOptions[]
) {
super(httpAdapterHost.httpAdapter);
}

catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const request = ctx.getRequest();

const opts = this.isRenderPath(request);

if( opts === undefined ){
return super.catch(exception, host);
}

opts.renderPath = opts.renderPath || DEFAULT_RENDER_PATH;
const clientPath = opts.rootPath || DEFAULT_ROOT_PATH;
const indexFilePath = this.loader.getIndexFilePath(clientPath);

const stream = fs.createReadStream(indexFilePath);
if (opts.serveStaticOptions && opts.serveStaticOptions.setHeaders) {
const stat = fs.statSync(indexFilePath);
opts.serveStaticOptions.setHeaders(response, indexFilePath, stat);
}
response.type('text/html').send(stream);
}

private isRenderPath(request): ServeStaticModuleOptions | undefined {
return this.optionsArr.find( opts => {
let renderPath: string | RegExp = opts.renderPath || DEFAULT_RENDER_PATH;

if( opts.serveRoot ) {
renderPath =
typeof opts.serveRoot === 'string'
? opts.serveRoot + validatePath(renderPath as string)
: opts.serveRoot;
}

const re = renderPath instanceof RegExp ? renderPath : wildcardToRegExp(renderPath);
const queryParamsIndex = request.url.indexOf('?');
const pathname =
queryParamsIndex >= 0
? request.url.slice(0, queryParamsIndex)
: request.url;

return re.exec(pathname) ? true : false;
});
}
}
26 changes: 1 addition & 25 deletions lib/loaders/fastify.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,17 @@ export class FastifyLoader extends AbstractLoader {
options.renderPath = options.renderPath || DEFAULT_RENDER_PATH;

const clientPath = options.rootPath || DEFAULT_ROOT_PATH;
const indexFilePath = this.getIndexFilePath(clientPath);

if (options.serveRoot) {
app.register(fastifyStatic, {
root: clientPath,
...(options.serveStaticOptions || {}),
wildcard: false,
prefix: options.serveRoot
});

const renderPath =
typeof options.serveRoot === 'string'
? options.serveRoot + validatePath(options.renderPath as string)
: options.serveRoot;

app.get(renderPath, (req: any, res: any) => {
const stream = fs.createReadStream(indexFilePath);
res.type('text/html').send(stream);
});
} else {
app.register(fastifyStatic, {
root: clientPath,
...(options.serveStaticOptions || {}),
wildcard: false
});
app.get(options.renderPath, (req: any, res: any) => {
const stream = fs.createReadStream(indexFilePath);
if (
options.serveStaticOptions &&
options.serveStaticOptions.setHeaders
) {
const stat = fs.statSync(indexFilePath);
options.serveStaticOptions.setHeaders(res, indexFilePath, stat);
}
res.type('text/html').send(stream);
...(options.serveStaticOptions || {})
});
}
});
Expand Down
13 changes: 12 additions & 1 deletion lib/serve-static.providers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Provider } from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';
import { APP_FILTER, HttpAdapterHost } from '@nestjs/core';
import { NotFoundExceptionFilter } from './filters/not-found-excepion.filter';
import { ServeStaticModuleOptions } from './interfaces/serve-static-options.interface';
import { AbstractLoader } from './loaders/abstract.loader';
import { ExpressLoader } from './loaders/express.loader';
import { FastifyLoader } from './loaders/fastify.loader';
import { NoopLoader } from './loaders/noop.loader';
import { SERVE_STATIC_MODULE_OPTIONS } from './serve-static.constants';

export const serveStaticProviders: Provider[] = [
{
Expand All @@ -23,5 +26,13 @@ export const serveStaticProviders: Provider[] = [
return new ExpressLoader();
},
inject: [HttpAdapterHost]
},
{
provide: APP_FILTER,
useFactory: (httpAdapterHost: HttpAdapterHost, loader: AbstractLoader, opts: ServeStaticModuleOptions[]) => {
if( loader instanceof FastifyLoader )
return new NotFoundExceptionFilter(httpAdapterHost, loader, opts);
},
inject: [HttpAdapterHost, AbstractLoader, SERVE_STATIC_MODULE_OPTIONS]
}
];
5 changes: 5 additions & 0 deletions lib/utils/reg-escape.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Escapes all characters in the given string
*/
export const regExpEscape = (s: string) =>
s.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
11 changes: 11 additions & 0 deletions lib/utils/wilcard-to-reg-exp.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { regExpEscape } from "./reg-escape.util";

/**
* Creates a RegExp from the given string, allowing wildcards.
*
* "*" will be converted to ".*"
* "?" will be converted to "."
*
* Escapes the rest of the characters
*/
export const wildcardToRegExp = (s: string) => new RegExp('^' + s.split(/\*+/).map(regExpEscape).join('.*').replace(/\\\?/g, '.') + '$');