-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
185 lines (167 loc) · 4.99 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Generated by CoffeeScript 1.4.0
(function() {
var buffer_request;
exports.Block = (function() {
function Block(def, handler) {
if (typeof def === "function") {
this.def = {};
def.apply(this);
} else {
this.def = def || {};
this.handlerFn = handler;
}
}
Block.prototype.name = function(name) {
this.def.name = name;
return this;
};
Block.prototype.description = function(description) {
this.def.description = description;
return this;
};
Block.prototype.url = function(url) {
this.def.url = url;
return this;
};
Block.prototype.input = function() {
return this._io("inputs", arguments);
};
Block.prototype.output = function() {
return this._io("outputs", arguments);
};
Block.prototype._io = function(type, args) {
if (!this.def[type]) {
this.def[type] = [];
}
if (typeof args[0] === "object") {
this.def[type].push(args[0]);
} else {
this.def[type].push({
name: args[0],
type: args[1],
description: args[2]
});
}
return this;
};
Block.prototype.handle = function(handler) {
this.handlerFn = handler;
return this;
};
Block.prototype._validateInputs = function(inputs) {
var errors, input, value, _i, _len, _ref;
errors = [];
_ref = this.def.inputs;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
input = _ref[_i];
if (!(input.name !== "*")) {
continue;
}
value = inputs[input.name];
if (!(value != null) && input.required !== false) {
errors.push("Input '" + input.name + "' is required.");
}
if ((value != null) && typeof value !== input.type) {
errors.push("Input '" + input.name + "' must be a " + input.type + ".");
}
}
if (errors.length !== 0) {
throw errors.join("\n");
}
};
Block.prototype._handleRequest = function(request, callback) {
var done, inputs;
inputs = request.inputs[0];
this._validateInputs(inputs);
done = function(err, outputs) {
if (!Array.isArray(outputs)) {
outputs = [outputs];
}
return callback(err, {
outputs: outputs
});
};
if (this.handlerFn.length < 2) {
try {
return done(null, this.handlerFn(inputs));
} catch (err) {
return done(err);
}
} else {
return this.handlerFn(inputs, done);
}
};
Block.prototype.nodeHandler = function() {
var _this = this;
return function(req, res) {
return _this._nodeHandler(req, res);
};
};
Block.prototype._nodeHandler = function(req, res) {
var corsHeaders,
_this = this;
corsHeaders = function(res) {
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
res.setHeader("Access-Control-Allow-Methods", "OPTIONS, POST");
return res.setHeader("Access-Control-Allow-Origin", "*");
};
switch (req.method.toUpperCase()) {
case "OPTIONS":
corsHeaders(res);
res.writeHead(200, "OK", {
"Content-Type": "application/json; charset=utf-8"
});
return res.end(JSON.stringify(this.def, null, 2));
case "POST":
corsHeaders(res);
return buffer_request(req, function(err, body) {
var request;
try {
if (err) {
throw err;
}
request = JSON.parse(body);
return _this._handleRequest(request, function(err, response) {
if (err) {
res.writeHead(500, "Internal Server Error");
return res.end();
} else {
res.writeHead(200, "OK", {
"Content-Type": "application/json; charset=utf-8"
});
return res.end(JSON.stringify(response, null, 2));
}
});
} catch (err) {
res.writeHead(400, "Bad Request");
return res.end(String(err));
}
});
default:
res.writeHead(405, "Method Not Allowed");
return res.end();
}
};
Block.prototype.listen = function(port) {
var _ref;
port = (_ref = port != null ? port : process.env.PORT) != null ? _ref : 3000;
console.log("Starting '" + this.def.name + "' WebPipe block on port " + port);
return require("http").createServer(this.nodeHandler()).listen(port);
};
return Block;
})();
buffer_request = function(req, callback) {
var buffer;
buffer = "";
req.setEncoding("utf-8");
req.on("error", function(err) {
return callback(err);
});
req.on("data", function(data) {
return buffer += data;
});
return req.on("end", function() {
return callback(null, buffer);
});
};
}).call(this);