fix: Do not re-encode static assets. (#566)

Fix serving binary static assets.
This commit is contained in:
Ruben Verborgh 2021-01-28 23:02:32 +01:00 committed by GitHub
parent 59a7c51f60
commit c899e6c4b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 8 deletions

View File

@ -79,7 +79,7 @@ export class StaticAssetHandler extends HttpHandler {
this.logger.debug(`Serving ${request.url} via static asset ${filePath}`); this.logger.debug(`Serving ${request.url} via static asset ${filePath}`);
// Resolve when asset loading succeeds // Resolve when asset loading succeeds
const asset = createReadStream(filePath, 'utf8'); const asset = createReadStream(filePath);
return new Promise((resolve, reject): void => { return new Promise((resolve, reject): void => {
// Write a 200 response when the asset becomes readable // Write a 200 response when the asset becomes readable
asset.once('readable', (): void => { asset.once('readable', (): void => {

View File

@ -51,7 +51,7 @@ describe('a StaticAssetHandler', (): void => {
await responseEnd; await responseEnd;
expect(createReadStream).toHaveBeenCalledTimes(1); expect(createReadStream).toHaveBeenCalledTimes(1);
expect(createReadStream).toHaveBeenCalledWith('/assets/styles/bar.css', 'utf8'); expect(createReadStream).toHaveBeenCalledWith('/assets/styles/bar.css');
expect(response._getData()).toBe('file contents'); expect(response._getData()).toBe('file contents');
}); });
@ -77,7 +77,7 @@ describe('a StaticAssetHandler', (): void => {
expect(response.getHeaders()).toHaveProperty('content-type', 'text/css'); expect(response.getHeaders()).toHaveProperty('content-type', 'text/css');
expect(createReadStream).toHaveBeenCalledTimes(1); expect(createReadStream).toHaveBeenCalledTimes(1);
expect(createReadStream).toHaveBeenCalledWith('/assets/styles/bar.css', 'utf8'); expect(createReadStream).toHaveBeenCalledWith('/assets/styles/bar.css');
}); });
it('handles a request for an asset with an unknown content type.', async(): Promise<void> => { it('handles a request for an asset with an unknown content type.', async(): Promise<void> => {
@ -89,7 +89,7 @@ describe('a StaticAssetHandler', (): void => {
expect(response.getHeaders()).toHaveProperty('content-type', 'application/octet-stream'); expect(response.getHeaders()).toHaveProperty('content-type', 'application/octet-stream');
expect(createReadStream).toHaveBeenCalledTimes(1); expect(createReadStream).toHaveBeenCalledTimes(1);
expect(createReadStream).toHaveBeenCalledWith('/assets/bar.unknown', 'utf8'); expect(createReadStream).toHaveBeenCalledWith('/assets/bar.unknown');
}); });
it('throws a 404 when the asset does not exist.', async(): Promise<void> => { it('throws a 404 when the asset does not exist.', async(): Promise<void> => {
@ -142,7 +142,7 @@ describe('a StaticAssetHandler', (): void => {
expect(response.getHeaders()).toHaveProperty('content-type', 'text/css'); expect(response.getHeaders()).toHaveProperty('content-type', 'text/css');
expect(createReadStream).toHaveBeenCalledTimes(1); expect(createReadStream).toHaveBeenCalledTimes(1);
expect(createReadStream).toHaveBeenCalledWith('/assets/folders/1/abc/def.css', 'utf8'); expect(createReadStream).toHaveBeenCalledWith('/assets/folders/1/abc/def.css');
}); });
it('handles a request to a known folder URL defined with slash.', async(): Promise<void> => { it('handles a request to a known folder URL defined with slash.', async(): Promise<void> => {
@ -154,7 +154,7 @@ describe('a StaticAssetHandler', (): void => {
expect(response.getHeaders()).toHaveProperty('content-type', 'text/css'); expect(response.getHeaders()).toHaveProperty('content-type', 'text/css');
expect(createReadStream).toHaveBeenCalledTimes(1); expect(createReadStream).toHaveBeenCalledTimes(1);
expect(createReadStream).toHaveBeenCalledWith('/assets/folders/2/abc/def.css', 'utf8'); expect(createReadStream).toHaveBeenCalledWith('/assets/folders/2/abc/def.css');
}); });
it('prefers the longest path handler.', async(): Promise<void> => { it('prefers the longest path handler.', async(): Promise<void> => {
@ -166,7 +166,7 @@ describe('a StaticAssetHandler', (): void => {
expect(response.getHeaders()).toHaveProperty('content-type', 'text/css'); expect(response.getHeaders()).toHaveProperty('content-type', 'text/css');
expect(createReadStream).toHaveBeenCalledTimes(1); expect(createReadStream).toHaveBeenCalledTimes(1);
expect(createReadStream).toHaveBeenCalledWith('/assets/folders/3/abc/def.css', 'utf8'); expect(createReadStream).toHaveBeenCalledWith('/assets/folders/3/abc/def.css');
}); });
it('handles a request to a known folder URL with spaces.', async(): Promise<void> => { it('handles a request to a known folder URL with spaces.', async(): Promise<void> => {
@ -178,7 +178,7 @@ describe('a StaticAssetHandler', (): void => {
expect(response.getHeaders()).toHaveProperty('content-type', 'text/css'); expect(response.getHeaders()).toHaveProperty('content-type', 'text/css');
expect(createReadStream).toHaveBeenCalledTimes(1); expect(createReadStream).toHaveBeenCalledTimes(1);
expect(createReadStream).toHaveBeenCalledWith('/assets/folders/2/a b c/def.css', 'utf8'); expect(createReadStream).toHaveBeenCalledWith('/assets/folders/2/a b c/def.css');
}); });
it('does not handle a request to a known folder URL with parent path segments.', async(): Promise<void> => { it('does not handle a request to a known folder URL with parent path segments.', async(): Promise<void> => {