Skip to content
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

Refactor handling of response's 'end' in forwarder #301

Closed
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
43 changes: 37 additions & 6 deletions lib/forwarder.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ import { removeConfig } from '../lib/config.js';
* @import { Resolver} from '../lib/resolver.js'
*/

/* Maximum number of characters of all eslint exit codes */
const EXIT_CODE_MAX_LENGTH = 2;

/* The string sent by our daemon upon termination */
const DAEMON_EXIT_TOKEN = 'EXIT';

const DAEMON_TERMINATION_CODE_MAX_LENGTH =
DAEMON_EXIT_TOKEN.length + EXIT_CODE_MAX_LENGTH;
const DAEMON_TERMINATION_CODE_REGEXP = new RegExp(
`${DAEMON_EXIT_TOKEN}([0-9]{1,${EXIT_CODE_MAX_LENGTH}})`
);

/**
* @param {Resolver} resolver
* @param {Config} config
Expand Down Expand Up @@ -36,19 +48,38 @@ export async function forwardToDaemon(resolver, config) {
let chunk = '';
while ((chunk = socket.read()) !== null) {
content += chunk;
if (content.length > 5) {
process.stdout.write(content.substring(0, content.length - 5));
content = content.substring(content.length - 5);
if (content.length > DAEMON_TERMINATION_CODE_MAX_LENGTH) {
const message_length =
content.length - DAEMON_TERMINATION_CODE_MAX_LENGTH;
// write everything we are sure doesn't contain the termination code:
process.stdout.write(content.substring(0, message_length));
// keep only what we haven't written yet:
content = content.substring(message_length);
}
}
})
.on('end', () => {
if (content.startsWith('EXIT')) {
process.exitCode = Number(content.slice(4));
} else {
// search the end of 'content' for the termination code:
const endOfContent = content.slice(-DAEMON_TERMINATION_CODE_MAX_LENGTH);
const match = endOfContent.match(DAEMON_TERMINATION_CODE_REGEXP);

if (!match) {
process.stdout.write(content);
console.error('eslint_d: unexpected response');
process.exitCode = 1;
return;
}

// write everything but the termination code:
content = content.slice(
0,
-DAEMON_TERMINATION_CODE_MAX_LENGTH + (match.index || 0)
);

process.exitCode = Number(match[1]);

if (content) {
process.stdout.write(content);
}
})
.on('error', async (err) => {
Expand Down
56 changes: 49 additions & 7 deletions lib/forwarder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('lib/forwarder', () => {
assert.calledWith(socket.write, text);
});

it('forwards socket response to stdout, except for the last 5 characters', () => {
it('forwards socket response to stdout', () => {
const chunks = ['response ', 'from daemon'];
sinon.replace(
socket,
Expand All @@ -96,10 +96,12 @@ describe('lib/forwarder', () => {

forwardToDaemon(resolver, config);
socket.on.firstCall.callback(); // readable
socket.on.secondCall.callback(); // end

assert.calledTwice(process.stdout.write);
assert.calledWith(process.stdout.write, 'resp');
assert.calledWith(process.stdout.write, 'onse from d');
assert.calledThrice(process.stdout.write);
assert.calledWith(process.stdout.write, 'res');
assert.calledWith(process.stdout.write, 'ponse from ');
assert.calledWith(process.stdout.write, 'daemon');
});

it('handles EXIT0 from response', () => {
Expand All @@ -115,7 +117,8 @@ describe('lib/forwarder', () => {
socket.on.firstCall.callback(); // readable
socket.on.secondCall.callback(); // end

assert.calledOnceWith(process.stdout.write, 'response from daemon');
assert.calledWith(process.stdout.write, 'response from daemo');
assert.calledWith(process.stdout.write, 'n');
assert.equals(process.exitCode, 0);
refute.called(console.error);
});
Expand All @@ -133,7 +136,46 @@ describe('lib/forwarder', () => {
socket.on.firstCall.callback(); // readable
socket.on.secondCall.callback(); // end

assert.calledWith(process.stdout.write, 'response from daemo');
assert.calledWith(process.stdout.write, 'n');
assert.equals(process.exitCode, 1);
refute.called(console.error);
});

it('handles EXIT12 from response', () => {
const chunks = ['response from daemonEXIT12'];
sinon.replace(
socket,
'read',
sinon.fake(() => (chunks.length ? chunks.shift() : null))
);
sinon.replace(process.stdout, 'write', sinon.fake());

forwardToDaemon(resolver, config);
socket.on.firstCall.callback(); // readable
socket.on.secondCall.callback(); // end

assert.calledWith(process.stdout.write, 'response from daemon');
assert.equals(process.exitCode, 12);
refute.called(console.error);
});

it('handles EXIT1 inside response', () => {
const chunks = ['response EXIT1', ' from daemonEXIT1'];
sinon.replace(
socket,
'read',
sinon.fake(() => (chunks.length ? chunks.shift() : null))
);
sinon.replace(process.stdout, 'write', sinon.fake());

forwardToDaemon(resolver, config);
socket.on.firstCall.callback(); // readable
socket.on.secondCall.callback(); // end

assert.calledWith(process.stdout.write, 'response');
assert.calledWith(process.stdout.write, ' EXIT1 from daemo');
assert.calledWith(process.stdout.write, 'n');
assert.equals(process.exitCode, 1);
refute.called(console.error);
});
Expand All @@ -151,8 +193,8 @@ describe('lib/forwarder', () => {
socket.on.firstCall.callback(); // readable
socket.on.secondCall.callback(); // end

assert.calledWith(process.stdout.write, 'response from d');
assert.calledWith(process.stdout.write, 'aemon');
assert.calledWith(process.stdout.write, 'response from ');
assert.calledWith(process.stdout.write, 'daemon');
assert.equals(process.exitCode, 1);
assert.calledOnceWith(console.error, 'eslint_d: unexpected response');
});
Expand Down
Loading