Skip to content

Doesn't work when the first character received is newline on JavaScript #8

@anolivetree

Description

@anolivetree

stdinGenerator() doesn't yield an empty line when the first character received is newline.

Following is the fix.

function* stdinGenerator() {
    let lineBuff = Buffer.allocUnsafe(0);
    while (true) {
        const readBuff = Buffer.allocUnsafe(1);
        try {
            if (readSync(stdinFileDescriptor, readBuff, 0, 1, null) === 0) {
                // EOF
                break;
            }
            lineBuff = Buffer.concat([lineBuff, readBuff]);
            if (readBuff[0] === NEW_LINE) {
                yield lineBuff.toString();
                lineBuff = Buffer.allocUnsafe(0);
            }
        } catch (err) {
            if (err.code === 'EAGAIN') {
                continue;
            }
            throw err;
        }
    }

    if (lineBuff.length > 0) {
        yield lineBuff.toString();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions