-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathcode-web.js
executable file
·358 lines (311 loc) · 10.1 KB
/
code-web.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env node
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// @ts-check
const http = require('http');
const url = require('url');
const fs = require('fs');
const path = require('path');
const util = require('util');
const opn = require('opn');
const minimist = require('minimist');
const webpack = require('webpack');
const APP_ROOT = path.dirname(__dirname);
const EXTENSIONS_ROOT = path.join(APP_ROOT, 'extensions');
const WEB_MAIN = path.join(APP_ROOT, 'src', 'vs', 'code', 'browser', 'workbench', 'workbench-dev.html');
const args = minimist(process.argv, {
boolean: [
'watch',
'no-launch',
'help'
],
string: [
'scheme',
'host',
'port',
'local_port'
],
});
if (args.help) {
console.log(
'yarn web [options]\n' +
' --watch Watch extensions that require browser specific builds\n' +
' --no-launch Do not open VSCode web in the browser\n' +
' --scheme Protocol (https or http)\n' +
' --host Remote host\n' +
' --port Remote/Local port\n' +
' --local_port Local port override\n' +
' --help\n' +
'[Example]\n' +
' yarn web --scheme https --host example.com --port 8080 --local_port 30000'
);
process.exit(0);
}
const PORT = args.port || process.env.PORT || 8080;
const LOCAL_PORT = args.local_port || process.env.LOCAL_PORT || PORT;
const SCHEME = args.scheme || process.env.VSCODE_SCHEME || 'http';
const HOST = args.host || 'localhost';
const AUTHORITY = process.env.VSCODE_AUTHORITY || `${HOST}:${PORT}`;
const exists = (path) => util.promisify(fs.exists)(path);
const readFile = (path) => util.promisify(fs.readFile)(path);
const CharCode_PC = '%'.charCodeAt(0);
async function initialize() {
const extensionFolders = await util.promisify(fs.readdir)(EXTENSIONS_ROOT);
const staticExtensions = [];
const webpackConfigs = [];
await Promise.all(extensionFolders.map(async extensionFolder => {
const packageJSONPath = path.join(EXTENSIONS_ROOT, extensionFolder, 'package.json');
if (await exists(packageJSONPath)) {
try {
const packageJSON = JSON.parse((await readFile(packageJSONPath)).toString());
if (packageJSON.main && !packageJSON.browser) {
return; // unsupported
}
if (packageJSON.browser) {
packageJSON.main = packageJSON.browser;
const webpackConfigPath = path.join(EXTENSIONS_ROOT, extensionFolder, 'extension-browser.webpack.config.js');
if ((await exists(webpackConfigPath))) {
const configOrFnOrArray = require(webpackConfigPath);
function addConfig(configOrFn) {
if (typeof configOrFn === 'function') {
webpackConfigs.push(configOrFn({}, {}));
} else {
webpackConfigs.push(configOrFn);
}
}
if (Array.isArray(configOrFnOrArray)) {
configOrFnOrArray.forEach(addConfig);
} else {
addConfig(configOrFnOrArray);
}
}
}
const packageNlsPath = path.join(EXTENSIONS_ROOT, extensionFolder, 'package.nls.json');
if (await exists(packageNlsPath)) {
const packageNls = JSON.parse((await readFile(packageNlsPath)).toString());
const translate = (obj) => {
for (let key in obj) {
const val = obj[key];
if (Array.isArray(val)) {
val.forEach(translate);
} else if (val && typeof val === 'object') {
translate(val);
} else if (typeof val === 'string' && val.charCodeAt(0) === CharCode_PC && val.charCodeAt(val.length - 1) === CharCode_PC) {
const translated = packageNls[val.substr(1, val.length - 2)];
if (translated) {
obj[key] = translated;
}
}
}
};
translate(packageJSON);
}
packageJSON.extensionKind = ['web']; // enable for Web
staticExtensions.push({
packageJSON,
extensionLocation: { scheme: SCHEME, authority: AUTHORITY, path: `/static-extension/${extensionFolder}` }
});
} catch (e) {
console.log(e);
}
}
}));
return new Promise((resolve, reject) => {
if (args.watch) {
webpack(webpackConfigs).watch({}, (err, stats) => {
if (err) {
console.log(err);
reject();
} else {
console.log(stats.toString());
resolve(staticExtensions);
}
});
} else {
webpack(webpackConfigs).run((err, stats) => {
if (err) {
console.log(err);
reject();
} else {
console.log(stats.toString());
resolve(staticExtensions);
}
});
}
});
}
const staticExtensionsPromise = initialize();
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
const pathname = parsedUrl.pathname;
try {
if (pathname === '/favicon.ico') {
// favicon
return serveFile(req, res, path.join(APP_ROOT, 'resources', 'win32', 'code.ico'));
}
if (pathname === '/manifest.json') {
// manifest
res.writeHead(200, { 'Content-Type': 'application/json' });
return res.end(JSON.stringify({
'name': 'Code Web - OSS',
'short_name': 'Code Web - OSS',
'start_url': '/',
'lang': 'en-US',
'display': 'standalone'
}));
}
if (/^\/static\//.test(pathname)) {
// static requests
return handleStatic(req, res, parsedUrl);
}
if (/^\/static-extension\//.test(pathname)) {
// static extension requests
return handleStaticExtension(req, res, parsedUrl);
}
if (pathname === '/') {
// main web
return handleRoot(req, res);
}
return serveError(req, res, 404, 'Not found.');
} catch (error) {
console.error(error.toString());
return serveError(req, res, 500, 'Internal Server Error.');
}
});
server.listen(LOCAL_PORT, () => {
if (LOCAL_PORT !== PORT) {
console.log(`Operating location at http://0.0.0.0:${LOCAL_PORT}`);
}
console.log(`Web UI available at ${SCHEME}://${AUTHORITY}`);
});
server.on('error', err => {
console.error(`Error occurred in server:`);
console.error(err);
});
/**
* @param {import('http').IncomingMessage} req
* @param {import('http').ServerResponse} res
* @param {import('url').UrlWithParsedQuery} parsedUrl
*/
function handleStatic(req, res, parsedUrl) {
// Strip `/static/` from the path
const relativeFilePath = path.normalize(decodeURIComponent(parsedUrl.pathname.substr('/static/'.length)));
return serveFile(req, res, path.join(APP_ROOT, relativeFilePath));
}
/**
* @param {import('http').IncomingMessage} req
* @param {import('http').ServerResponse} res
* @param {import('url').UrlWithParsedQuery} parsedUrl
*/
function handleStaticExtension(req, res, parsedUrl) {
// Strip `/static-extension/` from the path
const relativeFilePath = path.normalize(decodeURIComponent(parsedUrl.pathname.substr('/static-extension/'.length)));
const filePath = path.join(EXTENSIONS_ROOT, relativeFilePath);
return serveFile(req, res, filePath);
}
/**
* @param {import('http').IncomingMessage} req
* @param {import('http').ServerResponse} res
*/
async function handleRoot(req, res) {
const match = req.url && req.url.match(/\?([^#]+)/);
let ghPath;
if (match) {
const qs = new URLSearchParams(match[1]);
ghPath = qs.get('gh');
if (ghPath && !ghPath.startsWith('/')) {
ghPath = '/' + ghPath;
}
}
const staticExtensions = await staticExtensionsPromise;
const webConfiguration = escapeAttribute(JSON.stringify({
staticExtensions, folderUri: ghPath
? { scheme: 'github', authority: 'github.com', path: ghPath }
: { scheme: 'memfs', path: `/sample-folder` }
}));
const data = (await util.promisify(fs.readFile)(WEB_MAIN)).toString()
.replace('{{WORKBENCH_WEB_CONFIGURATION}}', () => webConfiguration) // use a replace function to avoid that regexp replace patterns ($&, $0, ...) are applied
.replace('{{WEBVIEW_ENDPOINT}}', '')
.replace('{{REMOTE_USER_DATA_URI}}', '');
res.writeHead(200, { 'Content-Type': 'text/html' });
return res.end(data);
}
/**
* @param {string} value
*/
function escapeAttribute(value) {
return value.replace(/"/g, '"');
}
/**
* @param {import('http').IncomingMessage} req
* @param {import('http').ServerResponse} res
* @param {string} errorMessage
*/
function serveError(req, res, errorCode, errorMessage) {
res.writeHead(errorCode, { 'Content-Type': 'text/plain' });
res.end(errorMessage);
}
const textMimeType = {
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.svg': 'image/svg+xml',
};
const mapExtToMediaMimes = {
'.bmp': 'image/bmp',
'.gif': 'image/gif',
'.ico': 'image/x-icon',
'.jpe': 'image/jpg',
'.jpeg': 'image/jpg',
'.jpg': 'image/jpg',
'.png': 'image/png',
'.tga': 'image/x-tga',
'.tif': 'image/tiff',
'.tiff': 'image/tiff',
'.woff': 'application/font-woff'
};
/**
* @param {string} forPath
*/
function getMediaMime(forPath) {
const ext = path.extname(forPath);
return mapExtToMediaMimes[ext.toLowerCase()];
}
/**
* @param {import('http').IncomingMessage} req
* @param {import('http').ServerResponse} res
* @param {string} filePath
*/
async function serveFile(req, res, filePath, responseHeaders = Object.create(null)) {
try {
// Sanity checks
filePath = path.normalize(filePath); // ensure no "." and ".."
if (filePath.indexOf(`${APP_ROOT}${path.sep}`) !== 0) {
// invalid location outside of APP_ROOT
return serveError(req, res, 400, `Bad request.`);
}
const stat = await util.promisify(fs.stat)(filePath);
// Check if file modified since
const etag = `W/"${[stat.ino, stat.size, stat.mtime.getTime()].join('-')}"`; // weak validator (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)
if (req.headers['if-none-match'] === etag) {
res.writeHead(304);
return res.end();
}
// Headers
responseHeaders['Content-Type'] = textMimeType[path.extname(filePath)] || getMediaMime(filePath) || 'text/plain';
responseHeaders['Etag'] = etag;
res.writeHead(200, responseHeaders);
// Data
fs.createReadStream(filePath).pipe(res);
} catch (error) {
console.error(error.toString());
res.writeHead(404, { 'Content-Type': 'text/plain' });
return res.end('Not found');
}
}
if (args.launch !== false) {
opn(`${SCHEME}://${HOST}:${PORT}`);
}