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 e2ca559 commit a55c926
Show file tree
Hide file tree
Showing 27 changed files with 241 additions and 798 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
43 changes: 20 additions & 23 deletions lib/commands/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ module.exports = {
validate: CMD_RE,
pipeline: true,

run(session, cmd) {
async* run(session, cmd) {
let match = cmd.match(CMD_RE);
let id;

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 All @@ -31,28 +31,25 @@ module.exports = {
let by_identifier = id[0] === '<';

if (!by_identifier && !session.group.name) {
return status._412_GRP_NOT_SLCTD;
return yield status._412_GRP_NOT_SLCTD;
}

return session.server._getArticle(session, id)
.then(msg => {
if (!msg) {
if (by_identifier) return status._430_NO_ARTICLE_BY_ID;
return status._423_NO_ARTICLE_BY_NUM;
}

if (!by_identifier) session.group.current_article = msg.index;

let msg_id = session.server._buildHeaderField(session, msg, 'message-id');
let msg_index = by_identifier ? 0 : id;

return [
`${status._220_ARTICLE_FOLLOWS} ${msg_index} ${msg_id}`,
session.server._buildHead(session, msg),
'',
session.server._buildBody(session, msg),
'.'
];
});
let msg = await session.server._getArticle(session, id);

if (!msg) {
if (by_identifier) return yield status._430_NO_ARTICLE_BY_ID;
return yield status._423_NO_ARTICLE_BY_NUM;
}

if (!by_identifier) session.group.current_article = msg.index;

let msg_id = session.server._buildHeaderField(session, msg, 'message-id');
let msg_index = by_identifier ? 0 : id;

yield `${status._220_ARTICLE_FOLLOWS} ${msg_index} ${msg_id}`;
yield session.server._buildHead(session, msg);
yield '';
yield session.server._buildBody(session, msg);
yield '.';
}
};
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
33 changes: 15 additions & 18 deletions lib/commands/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ module.exports = {
validate: CMD_RE,
pipeline: true,

run(session, cmd) {
async* run(session, cmd) {
let match = cmd.match(CMD_RE);
let id;

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 All @@ -31,26 +31,23 @@ module.exports = {
let by_identifier = id[0] === '<';

if (!by_identifier && !session.group.name) {
return status._412_GRP_NOT_SLCTD;
return yield status._412_GRP_NOT_SLCTD;
}

return session.server._getArticle(session, id)
.then(msg => {
if (!msg) {
if (by_identifier) return status._430_NO_ARTICLE_BY_ID;
return status._423_NO_ARTICLE_BY_NUM;
}
let msg = await session.server._getArticle(session, id);

if (!by_identifier) session.group.current_article = msg.index;
if (!msg) {
if (by_identifier) return yield status._430_NO_ARTICLE_BY_ID;
return yield status._423_NO_ARTICLE_BY_NUM;
}

if (!by_identifier) session.group.current_article = msg.index;

let msg_id = session.server._buildHeaderField(session, msg, 'message-id');
let msg_index = by_identifier ? 0 : id;
let msg_id = session.server._buildHeaderField(session, msg, 'message-id');
let msg_index = by_identifier ? 0 : id;

return [
`${status._222_BODY_FOLLOWS} ${msg_index} ${msg_id}`,
session.server._buildBody(session, msg),
'.'
];
});
yield `${status._222_BODY_FOLLOWS} ${msg_index} ${msg_id}`;
yield session.server._buildBody(session, msg);
yield '.';
}
};
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 '.';
}
};
4 changes: 2 additions & 2 deletions lib/commands/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ module.exports = {
validate: /^DATE$/i,
pipeline: true,

run() {
* run() {
let now = new Date();

return [
yield [
status._111_DATE,
' ',
now.getUTCFullYear(),
Expand Down
6 changes: 3 additions & 3 deletions lib/commands/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ module.exports = {
head: 'GROUP',
validate: CMD_RE,

async run(session, cmd) {
async* run(session, cmd) {
let name = cmd.match(CMD_RE)[1];

let ok = await session.server._selectGroup(session, name);

if (!ok) return status._411_GRP_NOT_FOUND;
if (!ok) return yield status._411_GRP_NOT_FOUND;

let g = session.group;

return `${status._211_GRP_SELECTED} ${g.total} ${g.min_index} ${g.max_index} ${name}`;
yield `${status._211_GRP_SELECTED} ${g.total} ${g.min_index} ${g.max_index} ${name}`;
}
};
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
33 changes: 15 additions & 18 deletions lib/commands/head.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ module.exports = {
validate: CMD_RE,
pipeline: true,

run(session, cmd) {
async* run(session, cmd) {
let match = cmd.match(CMD_RE);
let id;

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 All @@ -31,26 +31,23 @@ module.exports = {
let by_identifier = id[0] === '<';

if (!by_identifier && !session.group.name) {
return status._412_GRP_NOT_SLCTD;
return yield status._412_GRP_NOT_SLCTD;
}

return session.server._getArticle(session, id)
.then(msg => {
if (!msg) {
if (by_identifier) return status._430_NO_ARTICLE_BY_ID;
return status._423_NO_ARTICLE_BY_NUM;
}
let msg = await session.server._getArticle(session, id);

if (!by_identifier) session.group.current_article = msg.index;
if (!msg) {
if (by_identifier) return yield status._430_NO_ARTICLE_BY_ID;
return yield status._423_NO_ARTICLE_BY_NUM;
}

if (!by_identifier) session.group.current_article = msg.index;

let msg_id = session.server._buildHeaderField(session, msg, 'message-id');
let msg_index = by_identifier ? 0 : id;
let msg_id = session.server._buildHeaderField(session, msg, 'message-id');
let msg_index = by_identifier ? 0 : id;

return [
`${status._221_HEAD_FOLLOWS} ${msg_index} ${msg_id}`,
session.server._buildHead(session, msg),
'.'
];
});
yield `${status._221_HEAD_FOLLOWS} ${msg_index} ${msg_id}`;
yield session.server._buildHead(session, msg);
yield '.';
}
};
Loading

0 comments on commit a55c926

Please sign in to comment.