This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
demo-server.js
155 lines (150 loc) · 4.93 KB
/
demo-server.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
/**
* Copyright 2012 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var http = require('http');
var fs = require('fs');
var SOURCE_DIR = __dirname + '/test/fixtures/sample-module';
var SOURCEMAP_PREFIX = '/_sourcemap';
var SOURCEMAP_PATH_PREFIX_REGEX = /^\/_sourcemap\//;
var ORIGINAL_SOURCE_PATH_PREFIX = 'http://127.0.0.1:1337/_js';
var ORIGINAL_SOURCE_PATH_PREFIX_REGEX = /^\/_js\//;
require('./module-server').from(SOURCE_DIR + '/../build',
SOURCE_DIR + '/module-graph.json', run);
function run(err, moduleServer) {
if (err) {
throw err;
}
function jsForPath(path, isSourceMapRequest, onJs) {
path = path.replace(/^\//, '');
var parts = path.split(/\//);
var modules = decodeURIComponent(parts.shift()).split(/,/);
var options = {};
parts.forEach(function(part) {
var pair = part.split(/=/);
options[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
});
var exclude = null;
if(options.exm) {
exclude = options.exm.split(/,/);
}
return moduleServer(modules, exclude, onJs, {
createSourceMap: isSourceMapRequest,
sourceMapSourceRootUrlPrefix: ORIGINAL_SOURCE_PATH_PREFIX,
debug: true,
onLog: function() {
console.log(arguments);
}
});
}
http.createServer(function (req, res) {
var url = require('url').parse(req.url);
// Load static files for demo
switch(url.pathname) {
case "/demo.html":
fs.readFile(__dirname + '/clients/test/demo.html', 'utf8', function (err, html) {
if(err) {
throw err;
} else {
res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': html.length
});
res.end(html, 'utf8');
}
});
return;
case "/third-party/LABjs/LAB.src.js":
fs.readFile(__dirname + '/clients/third-party/LABjs/LAB.src.js', 'utf8', function (err, js) {
if(err) {
throw err;
} else {
res.writeHead(200, {
'Content-Type': 'application/javascript',
});
res.end(js, 'utf8');
}
});
return;
case "/module-client.js":
fs.readFile(__dirname + '/clients/module-client.js', 'utf8', function (err, js) {
if(err) {
throw err;
} else {
res.writeHead(200, {
'Content-Type': 'application/javascript',
});
res.end(js, 'utf8');
}
});
return;
}
console.log('Path ' + url.pathname);
if (ORIGINAL_SOURCE_PATH_PREFIX_REGEX.test(url.pathname)) {
var filename = SOURCE_DIR + '/' + url.pathname
.replace(ORIGINAL_SOURCE_PATH_PREFIX_REGEX, '');
fs.readFile(filename, 'utf8', function(err, js) {
if (err) {
throw err;
} else {
res.writeHead(200, {
'Content-Type': 'application/javascript',
'Content-Length': js.length,
'Pragma': 'no-cache'
});
res.end(js, 'utf8');
}
});
return;
}
var isSourceMapRequest = false;
if (SOURCEMAP_PATH_PREFIX_REGEX.test(url.pathname)) {
isSourceMapRequest = true;
url.pathname = url.pathname.replace(SOURCEMAP_PATH_PREFIX_REGEX, '/');
}
jsForPath(url.pathname, isSourceMapRequest, function(err, length, js,
sourceMap) {
if (err) {
console.log('Error', err);
if (err.statusCode) {
res.writeHead(err.statusCode, {'Content-Type': 'text/plain'});
res.end(err.message)
} else {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('Internal server error');
}
} else {
if (isSourceMapRequest) {
var map = JSON.stringify(sourceMap, null, ' ');
res.writeHead(200, {
'Content-Type': 'application/json',
'Content-Length': map.length,
'Pragma': 'no-cache'
});
res.end(map, 'utf8');
} else {
var mapUrl = SOURCEMAP_PREFIX + url.pathname;
res.writeHead(200, {
'Content-Type': 'application/javascript',
'Content-Length': length,
'SourceMap': mapUrl,
'X-SourceMap': mapUrl
});
res.end(js, 'utf8');
}
}
});
}).listen(1337, '127.0.0.1');
console.log('Module server running at http://127.0.0.1:1337/');
}