File size: 8,264 Bytes
84c1942 | 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 | import * as resolve from 'resolve';
import * as path from 'path';
import { getCaller, getCallSites } from './stubs/resolve-caller';
function getConstants() {
const constants = require('constants-browserify');
constants.FS = BrowserFS.BFSRequire('fs');
return constants;
}
function updateChildren(
parent: Module | undefined,
child: Module,
scan: boolean
) {
const children = parent && parent.children;
if (children && !(scan && children.includes(child))) children.push(child);
}
export default class Module {
id: string;
exports: any;
parent: Module | undefined;
children: Module[];
filename: string | null;
loaded: boolean;
static _extensions: {
[ext: string]: (module: Module, filename: string) => void;
} = {
'.js': function(module: Module, filename: string) {
const fs = BrowserFS.BFSRequire('fs');
const content = fs.readFileSync(filename, 'utf8');
module._compile(content, filename);
},
'.json': function(module: Module, filename: string) {
const fs = BrowserFS.BFSRequire('fs');
const content = fs.readFileSync(filename, 'utf8');
try {
module.exports = JSON.parse(content);
} catch (err: any) {
err.message = filename + ': ' + err.message;
throw err;
}
},
};
static globalPaths = [];
static _cache: {
[filename: string]: Module;
} = {};
constructor(id: string, parent?: Module) {
this.id = id;
this.exports = {};
this.parent = parent;
updateChildren(parent, this, false);
this.filename = null;
this.loaded = false;
this.children = [];
}
load(filename: string) {
if (this.loaded) {
throw new Error('Loaded already');
}
this.filename = filename;
let extension = path.extname(filename) || '.js';
if (!Module._extensions[extension]) extension = '.js';
Module._extensions[extension](this, filename);
this.loaded = true;
}
require(path: string) {
return Module._load(path, this);
}
_compile(content: string, filename: string) {
const _self = this;
// remove shebang
content = content.replace(/^\#\!.*/, '');
function require(path: string) {
return _self.require(path);
}
require.cache = Module._cache;
require.resolve = function(request: string) {
return Module._resolveFilename(request, _self);
};
const global: any = {
require,
exports: _self.exports,
__filename: filename,
__dirname: path.dirname(filename),
module: _self,
};
global.global = self;
Module.evaluate(
global,
[content, `//# sourceURL=${location.origin}${filename}`].join('\n')
);
}
static evaluate(global: any, content: string) {
// Don't let the module think that we're in an AMD env.
const oldamd = (self as any).define;
(self as any).define = null;
const globalParams = Object.keys(global).join(', ');
const newCode = [`(function(${globalParams}) {`, content, `})`].join('\n');
const result = (0, eval)(newCode).apply(
this,
Object.keys(global).map(m => global[m])
);
(self as any).define = oldamd;
return result;
}
static _resolveFilename(request: string, parent: Module): string {
return resolve.sync(request, {
basedir: parent.filename ? path.dirname(parent.filename) : undefined,
extensions: ['.js', '.json'],
});
}
static createRequire(fromPath: string) {
const module = new Module(fromPath);
module.filename = fromPath;
return module._createRequire();
}
_createRequire() {
const require = (p: string) => this.require(p);
require.resolve = (request: string) => {
return Module._resolveFilename(request, this);
};
return require;
}
static _load(request: string, parent: Module, isMain?: boolean) {
if (request === 'module') {
return Module;
}
if (request === 'child_process') {
return require('./child_process');
}
if (request === 'net') {
return require('./net');
}
if (request === 'assert') {
return require('assert');
}
if (request === 'string_decoder') {
return require('string_decoder');
}
if (request === 'diagnostics') {
return require('debug');
}
if (request === '/vscode/node_modules.asar/vscode-textmate') {
return require('vscode-textmate/out/main');
}
if (request === 'zlib') {
return require('browserify-zlib');
}
if (request === 'punycode') {
return require('punycode');
}
if (request === 'execa') {
return {};
}
if (request === 'tty') {
return { isatty: () => false };
}
if (request === 'stream') {
return require('stream-browserify');
}
if (request === 'http') {
// @ts-ignore Trick the module in thinking that it's on window, it's an outdated check
self.window = self;
return require('http-browserify');
}
if (request === 'https') {
// @ts-ignore Trick the module in thinking that it's on window, it's an outdated check
self.window = self;
return require('https-browserify');
}
if (request === 'constants') {
return getConstants();
}
if (request.startsWith('vscode-extension-telemetry')) {
return {
default: class TelemetryService {
constructor(
extensionId: string,
extensionVersion: string,
key: string
) {}
sendTelemetryEvent(
eventName: string,
properties?: {
[key: string]: string;
},
measurements?: {
[key: string]: number;
}
) {}
dispose() {}
},
};
}
if (request === 'url') {
return require('url');
}
if (request === 'crypto') {
return require('crypto');
}
if (request === 'util') {
// Direct import to get the right version for VIM extensions
return require('../node_modules/util');
}
if (request === 'os') {
return require('os');
}
if (request === 'events') {
return require('events');
}
if (request === 'readline') {
return require('./readline');
}
if (request === 'stream') {
return require('stream');
}
if (request === 'https-proxy-agent') {
return {};
}
if (request === 'http-proxy-agent') {
return {};
}
if (request === 'tls') {
return {};
}
if (request === 'fs' || request === 'graceful-fs') {
const fs = BrowserFS.BFSRequire('fs');
fs.constants = getConstants();
// VIM Mode needs this just to check if there are permissions, we know that all files can be accessed
// so this way we force it to work.
fs.accessSync = (_path: any, _type: any) => {
if (fs.existsSync(_path)) {
return true;
}
throw new Error('File not found');
};
fs.access = (_path: any, cb1: any, cb: (err?: Error) => void) => {
const callback = cb || cb1;
// TODO: make this async
if (fs.existsSync(_path)) {
return callback();
}
callback(new Error('File not found'));
};
return fs;
}
// NativeModule override
if (BrowserFS.BFSRequire(request)) {
return BrowserFS.BFSRequire(request);
}
const filename = Module._resolveFilename(request, parent);
const cachedModule = Module._cache[filename];
if (cachedModule) {
updateChildren(parent, cachedModule, true);
return cachedModule.exports;
}
if (filename === 'callsites') {
return getCallSites;
}
if (filename.endsWith('resolve/lib/caller.js')) {
return getCaller;
}
const module = new Module(filename, parent);
if (isMain) {
// @ts-ignore
process.mainModule = module;
module.id = '.';
}
Module._cache[filename] = module;
let threw = true;
try {
module.load(filename);
threw = false;
} finally {
if (threw) {
delete Module._cache[filename];
console.error(module, 'had trouble loading...');
}
}
return module.exports;
}
}
|