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-
varhttp=require('http');
4-
varpath=require('path');
5-
varparsedArgs=parseArgs(process.argv);
6-
varrequestedPortOrZero=parsedArgs.port||0;// 0 means 'let the OS decide'
7-
8-
if(parsedArgs.watch){
9-
autoQuitOnFileChange(process.cwd(),parsedArgs.watch.split(','));
10-
}
11-
12-
varserver=http.createServer(function(req,res){
13-
readRequestBodyAsJson(req,function(bodyJson){
14-
varresolvedPath=path.resolve(process.cwd(),bodyJson.moduleName);
15-
varinvokedModule=require(resolvedPath);
16-
varfunc=bodyJson.exportedFunctionName ? invokedModule[bodyJson.exportedFunctionName] : invokedModule;
17-
if(!func){
18-
thrownewError('The module "'+resolvedPath+'" has no export named "'+bodyJson.exportedFunctionName+'"');
19-
}
20-
21-
varhasSentResult=false;
22-
varcallback=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-
}elseif(typeofsuccessValue!=='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-
returnres;
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-
functionreadRequestBodyAsJson(request,callback){
75-
varrequestBodyAsString='';
76-
request
77-
.on('data',function(chunk){requestBodyAsString+=chunk;})
78-
.on('end',function(){callback(JSON.parse(requestBodyAsString));});
79-
}
80-
81-
functionautoQuitOnFileChange(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-
varfs=require('fs');
85-
varpath=require('path');
86-
fs.watch(rootDir,{persistent: false,recursive: true},function(event,filename){
87-
varext=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-
functionparseArgs(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-
varresult={};
99-
varcurrentKey=null;
100-
args.forEach(function(arg){
101-
if(arg.indexOf('--')===0){
102-
varargName=arg.substring(2);
103-
result[argName]=undefined;
104-
currentKey=argName;
105-
}elseif(currentKey){
106-
result[currentKey]=arg;
107-
currentKey=null;
108-
}
109-
});
110-
111-
returnresult;
112-
}
1+
(function(e,a){for(variina)e[i]=a[i];}(exports,/******/(function(modules){// webpackBootstrap
2+
/******/// The module cache
3+
/******/varinstalledModules={};
4+
5+
/******/// The require function
6+
/******/function__webpack_require__(moduleId){
7+
8+
/******/// Check if module is in cache
9+
/******/if(installedModules[moduleId])
10+
/******/returninstalledModules[moduleId].exports;
11+
12+
/******/// Create a new module (and put it into the cache)
13+
/******/varmodule=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+
/******/returnmodule.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+
varhttp=__webpack_require__(2);
58+
varpath=__webpack_require__(3);
59+
varArgsUtil_1=__webpack_require__(4);
60+
varAutoQuit_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+
vardynamicRequire=eval('require');
64+
varparsedArgs=ArgsUtil_1.parseArgs(process.argv);
65+
if(parsedArgs.watch){
66+
AutoQuit_1.autoQuitOnFileChange(process.cwd(),parsedArgs.watch.split(','));
67+
}
68+
varserver=http.createServer(function(req,res){
69+
readRequestBodyAsJson(req,function(bodyJson){
70+
varresolvedPath=path.resolve(process.cwd(),bodyJson.moduleName);
71+
varinvokedModule=dynamicRequire(resolvedPath);
72+
varfunc=bodyJson.exportedFunctionName ? invokedModule[bodyJson.exportedFunctionName] : invokedModule;
73+
if(!func){
74+
thrownewError('The module "'+resolvedPath+'" has no export named "'+bodyJson.exportedFunctionName+'"');
75+
}
76+
varhasSentResult=false;
77+
varcallback=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+
elseif(typeofsuccessValue!=='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+
returnres;
110+
}
111+
});
112+
try{
113+
func.apply(null,[callback].concat(bodyJson.args));
114+
}
115+
catch(synchronousException){
116+
callback(synchronousException,null);
117+
}
118+
});
119+
});
120+
varrequestedPortOrZero=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+
functionreadRequestBodyAsJson(request,callback){
128+
varrequestBodyAsString='';
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+
functionparseArgs(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+
varresult={};
156+
varcurrentKey=null;
157+
args.forEach(function(arg){
158+
if(arg.indexOf('--')===0){
159+
varargName=arg.substring(2);
160+
result[argName]=undefined;
161+
currentKey=argName;
162+
}
163+
elseif(currentKey){
164+
result[currentKey]=arg;
165+
currentKey=null;
166+
}
167+
});
168+
returnresult;
169+
}
170+
exports.parseArgs=parseArgs;
171+
172+
173+
/***/},
174+
/* 5 */
175+
/***/function(module,exports,__webpack_require__){
176+
177+
"use strict";
178+
varfs=__webpack_require__(6);
179+
varpath=__webpack_require__(3);
180+
functionautoQuitOnFileChange(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+
varext=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)