Skip to content

Commit

Permalink
Try out async iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
rlidwka committed Aug 18, 2020
1 parent 55891b2 commit 9d9fb69
Show file tree
Hide file tree
Showing 21 changed files with 134 additions and 263 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ env:
es6: true

parserOptions:
ecmaVersion: '2017'
ecmaVersion: '2018'

rules:
accessor-pairs: 2
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ Nntp.prototype._getGroups = function (/*session, time, wildmat*/) {
/*
* Generate message headers
*/
Nntp.prototype._buildHead = function* (/*session, message*/) {
Nntp.prototype._buildHead = function (/*session, message*/) {
throw new Error('method `nntp._buildHead` is not implemented');
};


/*
* Generate message body
*/
Nntp.prototype._buildBody = function* (/*session, message*/) {
Nntp.prototype._buildBody = function (/*session, message*/) {
throw new Error('method `nntp._buildBody` is not implemented');
};

Expand Down
27 changes: 13 additions & 14 deletions lib/commands/authinfo_pass.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@ module.exports = {
head: 'AUTHINFO PASS',
validate: CMD_RE,

run(session, cmd) {
if (session.authenticated) return status._502_CMD_UNAVAILABLE;
async* run(session, cmd) {
if (session.authenticated) return yield status._502_CMD_UNAVAILABLE;

if (!session.authinfo_user) return status._482_AUTH_OUT_OF_SEQ;
if (!session.authinfo_user) return yield status._482_AUTH_OUT_OF_SEQ;

session.authinfo_pass = cmd.match(CMD_RE)[1];

return session.server._authenticate(session)
.then(success => {
if (!success) {
session.authinfo_user = null;
session.authinfo_pass = null;
return status._481_AUTH_REJECTED;
}

session.authenticated = true;
return status._281_AUTH_ACCEPTED;
});
let success = await session.server._authenticate(session);

if (!success) {
session.authinfo_user = null;
session.authinfo_pass = null;
return yield status._481_AUTH_REJECTED;
}

session.authenticated = true;
yield status._281_AUTH_ACCEPTED;
}
};
8 changes: 4 additions & 4 deletions lib/commands/authinfo_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ module.exports = {
head: 'AUTHINFO USER',
validate: CMD_RE,

run(session, cmd) {
if (session.authenticated) return status._502_CMD_UNAVAILABLE;
async* run(session, cmd) {
if (session.authenticated) return yield status._502_CMD_UNAVAILABLE;

if (!session.server.options.secure &&
!session.secure) {
return status._483_NOT_SECURE;
return yield status._483_NOT_SECURE;
}

session.authinfo_user = cmd.match(CMD_RE)[1];

return status._381_AUTH_NEED_PASS;
yield status._381_AUTH_NEED_PASS;
},

capability(session, report) {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = {
return yield status._412_GRP_NOT_SLCTD;
}

let msg = await session.server._getArticle(session, id)
let msg = await session.server._getArticle(session, id);

if (!msg) {
if (by_identifier) return yield status._430_NO_ARTICLE_BY_ID;
Expand Down
8 changes: 4 additions & 4 deletions lib/commands/capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
// Param is not used, but spec requires to support it.
validate: /^CAPABILITIES( ([a-zA-Z\-_0-9]+))?$/i,

run(session) {
* run(session) {
let report = [
[ 'VERSION', 2 ]
];
Expand All @@ -38,8 +38,8 @@ module.exports = {
}
});

return [ status._101_CAPABILITY_LIST ]
.concat(Object.keys(uniq).map(k => [ k ].concat(uniq[k]).join(' ')))
.concat([ '.' ]);
yield status._101_CAPABILITY_LIST;
yield* Object.keys(uniq).map(k => [ k ].concat(uniq[k]).join(' '));
yield '.';
}
};
50 changes: 16 additions & 34 deletions lib/commands/hdr.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
'use strict';


const pump = require('pump');
const status = require('../status');
const { Readable, Transform } = require('stream');
const status = require('../status');


const CMD_RE = /^X?HDR ([^\s]+)(?: (?:(\d{1,15})(-(\d{1,15})?)?|(<[^\s<>]+>))?)?$/i;
Expand All @@ -14,7 +12,7 @@ module.exports = {
head: 'HDR',
validate: CMD_RE,

async run(session, cmd) {
async* run(session, cmd) {
let [ , field, first, dash, last, message_id ] = cmd.match(CMD_RE);

field = field.toLowerCase();
Expand All @@ -25,7 +23,7 @@ module.exports = {
if (typeof message_id !== 'undefined') {
article = await session.server._getArticle(session, message_id);

if (!article) return status._430_NO_ARTICLE_BY_ID;
if (!article) return yield status._430_NO_ARTICLE_BY_ID;

} else if (typeof first !== 'undefined') {
first = +first;
Expand All @@ -36,18 +34,16 @@ module.exports = {
last = typeof last === 'undefined' ? session.group.max_index : +last;
}

if (!session.group.name) return status._412_GRP_NOT_SLCTD;
if (!session.group.name) return yield status._412_GRP_NOT_SLCTD;

article_stream = await session.server._getRange(session, first, last);

if (Array.isArray(article_stream)) article_stream = Readable.from(article_stream);

} else {
if (session.group.current_article <= 0) return status._420_ARTICLE_NOT_SLCTD;
if (session.group.current_article <= 0) return yield status._420_ARTICLE_NOT_SLCTD;

article = await session.server._getArticle(session, String(session.group.current_article));

if (!article) return status._420_ARTICLE_NOT_SLCTD;
if (!article) return yield status._420_ARTICLE_NOT_SLCTD;
}

function transform(msg) {
Expand All @@ -69,32 +65,18 @@ module.exports = {

if (article_stream) {
let count = 0;

let stream = new Transform({
objectMode: true,
transform(article, encoding, callback) {
if (count === 0) this.push(status._225_HEADERS_FOLLOW);
count++;
this.push(transform(article));
callback();
},
flush(callback) {
if (count === 0) this.push(status._423_NO_ARTICLE_BY_NUM);
else this.push('.');
callback();
}
});

pump(article_stream, stream);

return stream;
for await (let article of article_stream) {
if (!count++) yield status._225_HEADERS_FOLLOW;
yield transform(article);
}
if (count === 0) yield status._423_NO_ARTICLE_BY_NUM;
else yield '.';
return;
}

return [
status._225_HEADERS_FOLLOW,
transform(article),
'.'
];
yield status._225_HEADERS_FOLLOW;
yield transform(article);
yield '.';
},

capability(session, report) {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/head.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = {
if (!match[1]) {
let cursor = session.group.current_article;

if (cursor <= 0) return status._420_ARTICLE_NOT_SLCTD;
if (cursor <= 0) return yield status._420_ARTICLE_NOT_SLCTD;

id = cursor.toString();
} else {
Expand Down
30 changes: 8 additions & 22 deletions lib/commands/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
'use strict';


const pump = require('pump');
const status = require('../status');
const { Readable, Transform } = require('stream');
const status = require('../status');

const CMD_RE = /^LIST( (.+))?$/i;

Expand All @@ -15,29 +13,17 @@ module.exports = {
validate: CMD_RE,
pipeline: true,

async run(session, cmd) {
async* run(session, cmd) {
// Reject all params. All extentions are in separate files
// and detected before this one.
if (cmd.match(CMD_RE)[2]) return status._501_SYNTAX_ERROR;
if (cmd.match(CMD_RE)[2]) return yield status._501_SYNTAX_ERROR;

let groups = await session.server._getGroups(session);
yield status._215_INFO_FOLLOWS;

if (Array.isArray(groups)) groups = Readable.from(groups);
for await (let group of session.server._getGroups(session)) {
yield `${group.name} ${group.max_index} ${group.min_index} n`;
}

let stream = new Transform({
objectMode: true,
transform(group, encoding, callback) {
this.push(`${group.name} ${group.max_index} ${group.min_index} n`);
callback();
}
});

pump(groups, stream);

return [
status._215_INFO_FOLLOWS,
stream,
'.'
];
yield '.';
}
};
28 changes: 7 additions & 21 deletions lib/commands/list_active.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
'use strict';


const pump = require('pump');
const status = require('../status');
const wildmat_re = require('../wildmat');
const { Readable, Transform } = require('stream');


const CMD_RE = /^LIST ACTIVE( ([^\s]+))?$/i;
Expand All @@ -17,36 +15,24 @@ module.exports = {
validate: CMD_RE,
pipeline: true,

async run(session, cmd) {
async* run(session, cmd) {
let wildmat = null;

if (cmd.match(CMD_RE)[2]) {
try {
wildmat = wildmat_re(cmd.match(CMD_RE)[2]);
} catch (err) {
return `501 ${err.message}`;
return yield `501 ${err.message}`;
}
}

let groups = await session.server._getGroups(session, 0, wildmat);
yield status._215_INFO_FOLLOWS;

if (Array.isArray(groups)) groups = Readable.from(groups);

let stream = new Transform({
objectMode: true,
transform(group, encoding, callback) {
this.push(`${group.name} ${group.max_index} ${group.min_index} n`);
callback();
}
});

pump(groups, stream);
for await (let group of session.server._getGroups(session, 0, wildmat)) {
yield `${group.name} ${group.max_index} ${group.min_index} n`;
}

return [
status._215_INFO_FOLLOWS,
stream,
'.'
];
yield '.';
},

capability(session, report) {
Expand Down
28 changes: 7 additions & 21 deletions lib/commands/list_newsgroups.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
'use strict';


const pump = require('pump');
const status = require('../status');
const wildmat_re = require('../wildmat');
const { Readable, Transform } = require('stream');


const CMD_RE = /^LIST NEWSGROUPS( ([^\s]+))?$/i;
Expand All @@ -17,36 +15,24 @@ module.exports = {
validate: CMD_RE,
pipeline: true,

async run(session, cmd) {
async* run(session, cmd) {
let wildmat = null;

if (cmd.match(CMD_RE)[2]) {
try {
wildmat = wildmat_re(cmd.match(CMD_RE)[2]);
} catch (err) {
return `501 ${err.message}`;
return yield `501 ${err.message}`;
}
}

let groups = await session.server._getGroups(session, 0, wildmat);
yield status._215_INFO_FOLLOWS;

if (Array.isArray(groups)) groups = Readable.from(groups);

let stream = new Transform({
objectMode: true,
transform(group, encoding, callback) {
this.push(`${group.name}\t${group.description || ''}`);
callback();
}
});

pump(groups, stream);
for await (let group of session.server._getGroups(session, 0, wildmat)) {
yield `${group.name}\t${group.description || ''}`;
}

return [
status._215_INFO_FOLLOWS,
stream,
'.'
];
yield '.';
},

capability(session, report) {
Expand Down
Loading

0 comments on commit 9d9fb69

Please sign in to comment.