forked from touv/node-ezs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (145 loc) · 3.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strcit';
const assert = require('assert');
const Transform = require('stream').Transform;
class Feed {
constructor(push, done) {
this.push = push;
this.done = done;
}
write(something) {
if (something !== undefined) {
this.push(something);
}
}
end() {
this.done();
}
send(something) {
this.write(something);
this.end();
}
close() {
this.write(null);
this.end();
}
}
class Engine extends Transform {
constructor(func, params, tagname) {
super( { objectMode: true })
let self = this;
self.func = func;
self.index = 0;
self.tagname = tagname;
self.params = params || {};
self.scope = {};
}
_transform(chunk, encoding, done) {
this.index++;
if (this.tagname && chunk.tagName && this.tagname === chunk.tagName()) {
this.execWith(chunk, done);
}
else if (this.tagname && chunk.tagName && this.tagname !== chunk.tagName()) {
this.push(chunk);
done();
}
else if (this.tagname && !chunk.tagName) {
this.push(chunk);
done();
}
else {
this.execWith(chunk, done);
}
}
_flush(done) {
this.execWith(null, done);
}
execWith(chunk, done) {
let self = this;
let push = function(data) {
if (data instanceof Error) {
data = self.createError(data);
}
if (self.tagname && chunk && chunk.tagName) {
data.tagName = chunk.tagName;
}
self.push(data);
}
let feed = new Feed(push, done);
self.scope.isFirst = function() { return (self.index === 1); };
self.scope.getIndex = function() { return self.index; };
self.scope.isLast = function() { return (chunk === null); };
self.scope.getParams = function() { return self.params; };
self.scope.getParam = function(name, defval) { return self.params[name] ? self.params[name] : defval; };
try {
self.func.call(self.scope, chunk, feed)
}
catch(e) {
console.error(self.createError(e));
self.push(self.createError(e));
}
}
createError(e) {
let self = this;
let err = new Error('At index #' + self.index + ' > ' + e.stack);
// err.index = self.index;
// err.scope = self.scope;
// e.chunk = chunk; mmmm it's bad idea...
/*e.toString = function() {
return e.errmsg;
}*/
return err;
}
}
function isStatement(name) {
return typeof name === 'function';
}
function ezs(name, opts) {
if (isStatement(name)) {
return new Engine(name, opts);
}
else if (typeof name === 'string' && ezs.plugins[name]) {
return new Engine(ezs.plugins[name], opts);
}
else {
throw new Error('`' + name +'` unknown');
}
}
ezs.plugins = {};
ezs.use = function(module) {
assert.equal(typeof module, 'object');
Object.keys(module).forEach(moduleName => {
if (isStatement(module[moduleName])) {
ezs.plugins[moduleName] = module[moduleName];
}
else {
throw new Error('`' + moduleName + '` is not loaded. It\'s not a valid statement.')
}
});
return ezs;
};
ezs.tag = function(tagname, func) {
assert.equal(typeof tagname, 'string');
assert.equal(typeof func, 'function');
return new Transform({
objectMode: true,
transform(chunk, encoding, callback) {
if (func(chunk)) {
chunk.tagName = function() { return tagname; }
}
callback(null, chunk);
}
});
return ezs;
};
ezs.has = function(tagname, name, opts) {
if (isStatement(name)) {
return new Engine(name, opts, tagname);
}
else if (typeof name === 'string' && ezs.plugins[name]) {
return new Engine(ezs.plugins[name], opts, tagname);
}
else {
throw new Error('`' + name +'` unknown');
}
};
module.exports = ezs;