mirror of
https://github.com/nodejs/node.git
synced 2025-05-02 14:56:19 +00:00

This change removes `common.noop` from the Node.js internal testing common module. Over the last few weeks, I've grown to dislike the `common.noop` abstraction. First, new (and experienced) contributors are unaware of it and so it results in a large number of low-value nits on PRs. It also increases the number of things newcomers and infrequent contributors have to be aware of to be effective on the project. Second, it is confusing. Is it a singleton/property or a getter? Which should be expected? This can lead to subtle and hard-to-find bugs. (To my knowledge, none have landed on master. But I also think it's only a matter of time.) Third, the abstraction is low-value in my opinion. What does it really get us? A case could me made that it is without value at all. Lastly, and this is minor, but the abstraction is wordier than not using the abstraction. `common.noop` doesn't save anything over `() => {}`. So, I propose removing it. PR-URL: https://github.com/nodejs/node/pull/12822 Reviewed-By: Teddy Katz <teddy.katz@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com>
88 lines
2.0 KiB
JavaScript
88 lines
2.0 KiB
JavaScript
'use strict';
|
|
// Run this program with valgrind or efence with --expose_gc to expose the
|
|
// problem.
|
|
|
|
// Flags: --expose_gc
|
|
|
|
require('../common');
|
|
const assert = require('assert');
|
|
const HTTPParser = process.binding('http_parser').HTTPParser;
|
|
|
|
const kOnHeaders = HTTPParser.kOnHeaders | 0;
|
|
const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
|
|
const kOnBody = HTTPParser.kOnBody | 0;
|
|
const kOnMessageComplete = HTTPParser.kOnMessageComplete | 0;
|
|
|
|
let headersComplete = 0;
|
|
let messagesComplete = 0;
|
|
|
|
function flushPool() {
|
|
Buffer.allocUnsafe(Buffer.poolSize - 1);
|
|
global.gc();
|
|
}
|
|
|
|
function demoBug(part1, part2) {
|
|
flushPool();
|
|
|
|
const parser = new HTTPParser('REQUEST');
|
|
|
|
parser.headers = [];
|
|
parser.url = '';
|
|
|
|
parser[kOnHeaders] = function(headers, url) {
|
|
parser.headers = parser.headers.concat(headers);
|
|
parser.url += url;
|
|
};
|
|
|
|
parser[kOnHeadersComplete] = function(info) {
|
|
headersComplete++;
|
|
console.log('url', info.url);
|
|
};
|
|
|
|
parser[kOnBody] = () => {};
|
|
|
|
parser[kOnMessageComplete] = function() {
|
|
messagesComplete++;
|
|
};
|
|
|
|
|
|
// We use a function to eliminate references to the Buffer b
|
|
// We want b to be GCed. The parser will hold a bad reference to it.
|
|
(function() {
|
|
const b = Buffer.from(part1);
|
|
flushPool();
|
|
|
|
console.log('parse the first part of the message');
|
|
parser.execute(b, 0, b.length);
|
|
})();
|
|
|
|
flushPool();
|
|
|
|
(function() {
|
|
const b = Buffer.from(part2);
|
|
|
|
console.log('parse the second part of the message');
|
|
parser.execute(b, 0, b.length);
|
|
parser.finish();
|
|
})();
|
|
|
|
flushPool();
|
|
}
|
|
|
|
|
|
demoBug('POST /1', '/22 HTTP/1.1\r\n' +
|
|
'Content-Type: text/plain\r\n' +
|
|
'Content-Length: 4\r\n\r\n' +
|
|
'pong');
|
|
|
|
demoBug('POST /1/22 HTTP/1.1\r\n' +
|
|
'Content-Type: tex', 't/plain\r\n' +
|
|
'Content-Length: 4\r\n\r\n' +
|
|
'pong');
|
|
|
|
process.on('exit', function() {
|
|
assert.strictEqual(2, headersComplete);
|
|
assert.strictEqual(2, messagesComplete);
|
|
console.log('done!');
|
|
});
|