-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeServer.js
More file actions
202 lines (173 loc) · 5.87 KB
/
NodeServer.js
File metadata and controls
202 lines (173 loc) · 5.87 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const os = require( 'os' );
const fs = require( 'fs' );
const express = require( 'express' );
const app = express()
const { spawn } = require( "child_process" );
const OsxMode = os.platform() == 'darwin';
const Port = process.env.Port || 80; // gr: needs to be int?
const FailOnExitCode = (process.env.FailOnExitCode!=='false');
const Timeout_Default = 2 * 60;
const TimeoutSecs = process.env.TimeoutSecs || Timeout_Default;
let ImageCounter = 1;
const ErrorStatusCode = process.env.ErrorStatusCode || 500;
const StaticFilesPath = process.env.StaticFilesPath || './';
// twitter meta min 300x157
// https://developer.twitter.com/en/docs/twitter-for-websites/cards/overview/summary-card-with-large-image
const ImageWidth = process.env.ImageWidth || 40;
const ImageHeight = process.env.ImageHeight || 40;
//const ImageScale = process.env.ImageScale || 1;
//const ImageCompression = process.env.ImageCompression || 0.5;
const PopExe_Module = "./node_modules/@newchromantics/popengine/ubuntu-latest/PopEngineTestApp";
const PopExe_Osx = '/Users/graham/Library/Developer/Xcode/DerivedData/PopEngine-edqmtlsljjncjvezlgagcmlvpbob/Build/Products/Debug_JavascriptCore/PopEngine.app/Contents/MacOS/PopEngine';
const PopExe = OsxMode ? PopExe_Osx : (process.env.PopExePath || PopExe_Module);
//const PopExe = "./node_modules/@newchromantics/popengine/ubuntu-latest/PopEngineTestApp"
//const PopExe = 'D:/PopEngine/Build/PopEngineApp_Debug_x64/PopEngineApp.exe';
const PopTestPath = process.env.PopTestPath || "./PopTestImage/";
console.log(`env Port -> ${Port} (${process.env.Port})`);
console.log(`env PopExePath -> ${PopExe} (${process.env.PopExePath})`);
console.log(`env PopTestPath -> ${PopTestPath} (${process.env.PopTestPath})`);
console.log(`env TimeoutSecs -> ${TimeoutSecs} (${process.env.TimeoutSecs})`);
console.log(`env ErrorStatusCode -> ${ErrorStatusCode} (${process.env.ErrorStatusCode})`);
console.log(`env FailOnExitCode -> ${FailOnExitCode} (${process.env.FailOnExitCode})`);
console.log(`env StaticFilesPath -> ${StaticFilesPath} (${process.env.StaticFilesPath})`);
console.log(`env ImageWidth -> ${ImageWidth} (${process.env.ImageWidth})`);
console.log(`env ImageHeight -> ${ImageHeight} (${process.env.ImageHeight})`);
try
{
const AllEnv = JSON.stringify(process.env,null,'\t');
console.log(`env (all) ${AllEnv}`);
}
catch(e)
{
console.log(`env (all) error -> ${e}`);
}
/*
// Send log on timeout
app.use( ( req, res, next ) =>
{
res.setTimeout( TimeoutSecs, function ()
{
res.statusCode = ErrorStatusCode;
res.setHeader('Content-Type','text/plain');
res.end(`Request Timeout`);
} );
next();
} );
*/
function CreatePromise()
{
let Prom = {};
function RunPromise(Resolve,Reject)
{
Prom.Resolve = Resolve;
Prom.Reject = Reject;
}
Prom.Promise = new Promise(RunPromise);
let OutProm = Prom.Promise;
OutProm.Resolve = Prom.Resolve;
OutProm.Reject = Prom.Reject;
return OutProm;
}
function HexStringToRgb(HexString)
{
const r = parseInt( HexString.slice(0,2), 16 ) || 0;
const g = parseInt( HexString.slice(2,4), 16 ) || 0;
const b = parseInt( HexString.slice(4,6), 16 ) || 0;
return [r,g,b];
}
// Runs the Raymon app and sends back a zip of the data
async function RunApp(Request)
{
// extract params from the request url
// Request.path -> /hello.png
const UrlPath = Request.path.slice(1,-4); // strip / and .png
const rrggbb = HexStringToRgb(UrlPath);
const Args =
[
PopTestPath,
`ImageCounter=${ImageCounter}`,
`ImageWidth=${ImageWidth}`,
`ImageHeight=${ImageHeight}`,
`Red=${rrggbb[0]}`,
`Green=${rrggbb[1]}`,
`Blue=${rrggbb[2]}`,
];
const Raymon = spawn( PopExe, Args );
const ProcessPromise = CreatePromise();
let StdErrlog = '';
let StdOutLog = null;
function OnStdOut(Data)
{
// gr: this is confusing, you can use Buffer as a string, or as a Buffer object
// JSON.stringify is misleading, the .data isn't really there
console.log(`typeof Data.data ${typeof Data}`, JSON.stringify(Data));
if (!StdOutLog)
{
console.log('StdOutLog = Data;');
StdOutLog = Data;
}
else
{
console.log(`Buffer.concat(${StdOutLog},${Data})`);
StdOutLog = Buffer.concat([StdOutLog,Data]);
}
}
function OnStdErr(Data)
{
//console.log(`OnStdErr(typeof Data ${typeof Data})`, JSON.stringify(Data));
StdErrlog += Data;
//console.log(`stderr>${Data}`);
}
function OnError(Error)
{
ProcessPromise.Reject(Error.message);
}
function OnProcessExit(ExitCode)
{
console.log(`OnProcessExit(${ExitCode}) null=crash`);
if (ExitCode !== 0 && FailOnExitCode )
{
const Error = {};
//Error.message = `Process non-zero exit code ${ExitCode}; StdOut=${StdOutLog} StdErr=${StdErrlog}`;
Error.message = `Process non-zero exit code ${ExitCode}; StdOut.length=${StdOutLog?StdOutLog.length:'null'} StdErr=${StdErrlog}`;
OnError(Error);
return;
}
const StdOutBuffer = Buffer.from(StdOutLog);
ProcessPromise.Resolve(StdOutBuffer);
}
Raymon.on('error',OnError);
// gr: odd that these are different event names?
Raymon.stdout.on('data',OnStdOut);
Raymon.stderr.on('data',OnStdErr);
Raymon.on("close",OnProcessExit);
const Output = await ProcessPromise;
const Result = {};
Result.Mime = 'image/png';
Result.Output = Output;
return Result;
}
async function HandleGetImage(Request,Response)
{
try
{
ImageCounter++;
const Output = await RunApp(Request);
Output.StatusCode = Output.StatusCode || 200;
Output.Mime = Output.Mime || 'text/plain';
Response.statusCode = Output.StatusCode;
Response.setHeader('Content-Type',Output.Mime);
Response.end(Output.Output);
}
catch (e)
{
console.log(`RunApp error -> ${e}`);
Response.statusCode = ErrorStatusCode;
Response.setHeader('Content-Type','text/plain');
Response.end(`Error ${e}`);
}
}
app.get('/*.png',HandleGetImage);
app.get('/', function (req, res) { res.redirect('/index.html') });
app.use('/', express.static(StaticFilesPath));
app.listen( Port, () => console.log( `Server running port: ${Port}/` ) );