Skip to content

Commit

Permalink
Remove special handling of HEAD requests (#8416)
Browse files Browse the repository at this point in the history
Co-authored-by: Niklas Mischkulnig <[email protected]>
  • Loading branch information
alukach and mischnic authored Sep 24, 2022
1 parent 828a845 commit de15695
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
27 changes: 27 additions & 0 deletions packages/core/integration-tests/test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
overlayFS,
ncp,
request as get,
requestRaw as getRaw,
} from '@parcel/test-utils';
import https from 'https';
import getPort from 'get-port';
Expand Down Expand Up @@ -58,6 +59,32 @@ describe('server', function () {
assert.equal(data, distFile);
});

it('should include content length for HEAD requests', async function () {
let port = await getPort();
let b = bundler(path.join(__dirname, '/integration/commonjs/index.js'), {
defaultTargetOptions: {
distDir,
},
config,
serveOptions: {
https: false,
port: port,
host: 'localhost',
},
});

subscription = await b.watch();
await getNextBuild(b);

let result = await getRaw('/index.js', port, {method: 'HEAD'});
let distFile = await outputFS.readFile(path.join(distDir, 'index.js'));
assert.strictEqual(
result.res.headers['content-length'],
String(distFile.byteLength),
);
assert.strictEqual(result.data, '');
});

it('should serve source files', async function () {
let port = await getPort();
let inputPath = path.join(__dirname, '/integration/commonjs/index.js');
Expand Down
35 changes: 35 additions & 0 deletions packages/core/test-utils/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
} from '@parcel/types';
import type {FileSystem} from '@parcel/fs';
import type WorkerFarm from '@parcel/workers';
import type {IncomingMessage} from 'http';

import invariant from 'assert';
import util from 'util';
Expand Down Expand Up @@ -1167,6 +1168,40 @@ export async function assertNoFilePathInCache(
}
}

export function requestRaw(
file: string,
port: number,
options: ?requestOptions,
client: typeof http | typeof https = http,
): Promise<{|res: IncomingMessage, data: string|}> {
return new Promise((resolve, reject) => {
client
// $FlowFixMe
.request(
{
hostname: 'localhost',
port: port,
path: file,
rejectUnauthorized: false,
...options,
},
(res: IncomingMessage) => {
res.setEncoding('utf8');
let data = '';
res.on('data', c => (data += c));
res.on('end', () => {
if (res.statusCode !== 200) {
return reject({res, data});
}

resolve({res, data});
});
},
)
.end();
});
}

export function request(
file: string,
port: number,
Expand Down
5 changes: 0 additions & 5 deletions packages/reporters/dev-server/src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,6 @@ export default class Server {
return next(req, res);
}

if (req.method === 'HEAD') {
res.end();
return;
}

if (fresh(req.headers, {'last-modified': stat.mtime.toUTCString()})) {
res.statusCode = 304;
res.end();
Expand Down

0 comments on commit de15695

Please sign in to comment.