-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
50 lines (43 loc) · 1.02 KB
/
handler.js
File metadata and controls
50 lines (43 loc) · 1.02 KB
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
const path = require('path');
const spawn = require('child_process').spawn;
function response(view) {
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify(view)
}
}
function errorResponse(err) {
console.log(err);
return {
statusCode: 500,
body: {
error: err.message
}
}
}
module.exports.speak = (event, context, callback) => {
const defaultLanguage = 'en';
const params = JSON.parse(event.body);
const speak = spawn(path.resolve('./speak'), [
'--path=' + path.resolve('.'),
'-q',
'--ipa',
'--stdin',
`-v${ params.language.toLowerCase() || defaultLanguage }`
]);
speak.stdin.setEncoding('utf-8');
speak.stdin.write(params.text);
speak.stdin.end();
speak.stdout.on('data', (data) => {
callback(null, response({
language: params.language,
text: data.toString('utf-8').trim()
}))
});
speak.stderr.on('data', (data) => {
callback(null, errorResponse(data.toString('utf-8')));
});
};