Skip to content

Commit 260a0d6

Browse files
Migrate entrypoint-http.js to TypeScript. Source is now HttpNodeInstanceEntryPoint.ts.
1 parent 393e156 commit 260a0d6

File tree

7 files changed

+401
-203
lines changed

7 files changed

+401
-203
lines changed
Lines changed: 201 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,201 @@
1-
// Limit dependencies to core Node modules. This means the code in this file has to be very low-level and unattractive,
2-
// but simplifies things for the consumer of this module.
3-
var http = require('http');
4-
var path = require('path');
5-
var parsedArgs = parseArgs(process.argv);
6-
var requestedPortOrZero = parsedArgs.port || 0; // 0 means 'let the OS decide'
7-
8-
if (parsedArgs.watch) {
9-
autoQuitOnFileChange(process.cwd(), parsedArgs.watch.split(','));
10-
}
11-
12-
var server = http.createServer(function(req, res) {
13-
readRequestBodyAsJson(req, function(bodyJson) {
14-
var resolvedPath = path.resolve(process.cwd(), bodyJson.moduleName);
15-
var invokedModule = require(resolvedPath);
16-
var func = bodyJson.exportedFunctionName ? invokedModule[bodyJson.exportedFunctionName] : invokedModule;
17-
if (!func) {
18-
throw new Error('The module "' + resolvedPath + '" has no export named "' + bodyJson.exportedFunctionName + '"');
19-
}
20-
21-
var hasSentResult = false;
22-
var callback = function(errorValue, successValue) {
23-
if (!hasSentResult) {
24-
hasSentResult = true;
25-
if (errorValue) {
26-
res.statusCode = 500;
27-
28-
if (errorValue.stack) {
29-
res.end(errorValue.stack);
30-
} else {
31-
res.end(errorValue.toString());
32-
}
33-
} else if (typeof successValue !== 'string') {
34-
// Arbitrary object/number/etc - JSON-serialize it
35-
res.setHeader('Content-Type', 'application/json');
36-
res.end(JSON.stringify(successValue));
37-
} else {
38-
// String - can bypass JSON-serialization altogether
39-
res.setHeader('Content-Type', 'text/plain');
40-
res.end(successValue);
41-
}
42-
}
43-
};
44-
45-
// Support streamed responses
46-
Object.defineProperty(callback, 'stream', {
47-
enumerable: true,
48-
get: function() {
49-
if (!hasSentResult) {
50-
hasSentResult = true;
51-
res.setHeader('Content-Type', 'application/octet-stream');
52-
}
53-
54-
return res;
55-
}
56-
});
57-
58-
try {
59-
func.apply(null, [callback].concat(bodyJson.args));
60-
} catch (synchronousException) {
61-
callback(synchronousException, null);
62-
}
63-
});
64-
});
65-
66-
server.listen(requestedPortOrZero, 'localhost', function () {
67-
// Signal to HttpNodeHost which port it should make its HTTP connections on
68-
console.log('[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on port ' + server.address().port + '\]');
69-
70-
// Signal to the NodeServices base class that we're ready to accept invocations
71-
console.log('[Microsoft.AspNetCore.NodeServices:Listening]');
72-
});
73-
74-
function readRequestBodyAsJson(request, callback) {
75-
var requestBodyAsString = '';
76-
request
77-
.on('data', function(chunk) { requestBodyAsString += chunk; })
78-
.on('end', function() { callback(JSON.parse(requestBodyAsString)); });
79-
}
80-
81-
function autoQuitOnFileChange(rootDir, extensions) {
82-
// Note: This will only work on Windows/OS X, because the 'recursive' option isn't supported on Linux.
83-
// Consider using a different watch mechanism (though ideally without forcing further NPM dependencies).
84-
var fs = require('fs');
85-
var path = require('path');
86-
fs.watch(rootDir, { persistent: false, recursive: true }, function(event, filename) {
87-
var ext = path.extname(filename);
88-
if (extensions.indexOf(ext) >= 0) {
89-
console.log('Restarting due to file change: ' + filename);
90-
process.exit(0);
91-
}
92-
});
93-
}
94-
95-
function parseArgs(args) {
96-
// Very simplistic parsing which is sufficient for the cases needed. We don't want to bring in any external
97-
// dependencies (such as an args-parsing library) to this file.
98-
var result = {};
99-
var currentKey = null;
100-
args.forEach(function(arg) {
101-
if (arg.indexOf('--') === 0) {
102-
var argName = arg.substring(2);
103-
result[argName] = undefined;
104-
currentKey = argName;
105-
} else if (currentKey) {
106-
result[currentKey] = arg;
107-
currentKey = null;
108-
}
109-
});
110-
111-
return result;
112-
}
1+
(function(e, a) { for(var i in a) e[i] = a[i]; }(exports, /******/ (function(modules) { // webpackBootstrap
2+
/******/ // The module cache
3+
/******/ var installedModules = {};
4+
5+
/******/ // The require function
6+
/******/ function __webpack_require__(moduleId) {
7+
8+
/******/ // Check if module is in cache
9+
/******/ if(installedModules[moduleId])
10+
/******/ return installedModules[moduleId].exports;
11+
12+
/******/ // Create a new module (and put it into the cache)
13+
/******/ var module = installedModules[moduleId] = {
14+
/******/ exports: {},
15+
/******/ id: moduleId,
16+
/******/ loaded: false
17+
/******/ };
18+
19+
/******/ // Execute the module function
20+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
21+
22+
/******/ // Flag the module as loaded
23+
/******/ module.loaded = true;
24+
25+
/******/ // Return the exports of the module
26+
/******/ return module.exports;
27+
/******/ }
28+
29+
30+
/******/ // expose the modules object (__webpack_modules__)
31+
/******/ __webpack_require__.m = modules;
32+
33+
/******/ // expose the module cache
34+
/******/ __webpack_require__.c = installedModules;
35+
36+
/******/ // __webpack_public_path__
37+
/******/ __webpack_require__.p = "";
38+
39+
/******/ // Load entry module and return exports
40+
/******/ return __webpack_require__(0);
41+
/******/ })
42+
/************************************************************************/
43+
/******/ ([
44+
/* 0 */
45+
/***/ function(module, exports, __webpack_require__) {
46+
47+
module.exports = __webpack_require__(1);
48+
49+
50+
/***/ },
51+
/* 1 */
52+
/***/ function(module, exports, __webpack_require__) {
53+
54+
"use strict";
55+
// Limit dependencies to core Node modules. This means the code in this file has to be very low-level and unattractive,
56+
// but simplifies things for the consumer of this module.
57+
var http = __webpack_require__(2);
58+
var path = __webpack_require__(3);
59+
var ArgsUtil_1 = __webpack_require__(4);
60+
var AutoQuit_1 = __webpack_require__(5);
61+
// Webpack doesn't support dynamic requires for files not present at compile time, so grab a direct
62+
// reference to Node's runtime 'require' function.
63+
var dynamicRequire = eval('require');
64+
var parsedArgs = ArgsUtil_1.parseArgs(process.argv);
65+
if (parsedArgs.watch) {
66+
AutoQuit_1.autoQuitOnFileChange(process.cwd(), parsedArgs.watch.split(','));
67+
}
68+
var server = http.createServer(function (req, res) {
69+
readRequestBodyAsJson(req, function (bodyJson) {
70+
var resolvedPath = path.resolve(process.cwd(), bodyJson.moduleName);
71+
var invokedModule = dynamicRequire(resolvedPath);
72+
var func = bodyJson.exportedFunctionName ? invokedModule[bodyJson.exportedFunctionName] : invokedModule;
73+
if (!func) {
74+
throw new Error('The module "' + resolvedPath + '" has no export named "' + bodyJson.exportedFunctionName + '"');
75+
}
76+
var hasSentResult = false;
77+
var callback = function (errorValue, successValue) {
78+
if (!hasSentResult) {
79+
hasSentResult = true;
80+
if (errorValue) {
81+
res.statusCode = 500;
82+
if (errorValue.stack) {
83+
res.end(errorValue.stack);
84+
}
85+
else {
86+
res.end(errorValue.toString());
87+
}
88+
}
89+
else if (typeof successValue !== 'string') {
90+
// Arbitrary object/number/etc - JSON-serialize it
91+
res.setHeader('Content-Type', 'application/json');
92+
res.end(JSON.stringify(successValue));
93+
}
94+
else {
95+
// String - can bypass JSON-serialization altogether
96+
res.setHeader('Content-Type', 'text/plain');
97+
res.end(successValue);
98+
}
99+
}
100+
};
101+
// Support streamed responses
102+
Object.defineProperty(callback, 'stream', {
103+
enumerable: true,
104+
get: function () {
105+
if (!hasSentResult) {
106+
hasSentResult = true;
107+
res.setHeader('Content-Type', 'application/octet-stream');
108+
}
109+
return res;
110+
}
111+
});
112+
try {
113+
func.apply(null, [callback].concat(bodyJson.args));
114+
}
115+
catch (synchronousException) {
116+
callback(synchronousException, null);
117+
}
118+
});
119+
});
120+
var requestedPortOrZero = parsedArgs.port || 0; // 0 means 'let the OS decide'
121+
server.listen(requestedPortOrZero, 'localhost', function () {
122+
// Signal to HttpNodeHost which port it should make its HTTP connections on
123+
console.log('[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on port ' + server.address().port + '\]');
124+
// Signal to the NodeServices base class that we're ready to accept invocations
125+
console.log('[Microsoft.AspNetCore.NodeServices:Listening]');
126+
});
127+
function readRequestBodyAsJson(request, callback) {
128+
var requestBodyAsString = '';
129+
request
130+
.on('data', function (chunk) { requestBodyAsString += chunk; })
131+
.on('end', function () { callback(JSON.parse(requestBodyAsString)); });
132+
}
133+
134+
135+
/***/ },
136+
/* 2 */
137+
/***/ function(module, exports) {
138+
139+
module.exports = require("http");
140+
141+
/***/ },
142+
/* 3 */
143+
/***/ function(module, exports) {
144+
145+
module.exports = require("path");
146+
147+
/***/ },
148+
/* 4 */
149+
/***/ function(module, exports) {
150+
151+
"use strict";
152+
function parseArgs(args) {
153+
// Very simplistic parsing which is sufficient for the cases needed. We don't want to bring in any external
154+
// dependencies (such as an args-parsing library) to this file.
155+
var result = {};
156+
var currentKey = null;
157+
args.forEach(function (arg) {
158+
if (arg.indexOf('--') === 0) {
159+
var argName = arg.substring(2);
160+
result[argName] = undefined;
161+
currentKey = argName;
162+
}
163+
else if (currentKey) {
164+
result[currentKey] = arg;
165+
currentKey = null;
166+
}
167+
});
168+
return result;
169+
}
170+
exports.parseArgs = parseArgs;
171+
172+
173+
/***/ },
174+
/* 5 */
175+
/***/ function(module, exports, __webpack_require__) {
176+
177+
"use strict";
178+
var fs = __webpack_require__(6);
179+
var path = __webpack_require__(3);
180+
function autoQuitOnFileChange(rootDir, extensions) {
181+
// Note: This will only work on Windows/OS X, because the 'recursive' option isn't supported on Linux.
182+
// Consider using a different watch mechanism (though ideally without forcing further NPM dependencies).
183+
fs.watch(rootDir, { persistent: false, recursive: true }, function (event, filename) {
184+
var ext = path.extname(filename);
185+
if (extensions.indexOf(ext) >= 0) {
186+
console.log('Restarting due to file change: ' + filename);
187+
process.exit(0);
188+
}
189+
});
190+
}
191+
exports.autoQuitOnFileChange = autoQuitOnFileChange;
192+
193+
194+
/***/ },
195+
/* 6 */
196+
/***/ function(module, exports) {
197+
198+
module.exports = require("fs");
199+
200+
/***/ }
201+
/******/ ])));

0 commit comments

Comments
 (0)