moved the last polyfills into we

This commit is contained in:
Eugene Pankov
2021-06-25 21:55:34 +02:00
parent 655079741a
commit 722f91da0c
2 changed files with 179 additions and 131 deletions

View File

@@ -9,3 +9,76 @@ import '../app/src/preload.scss'
// Required before other imports // Required before other imports
import './polyfills.buffer' import './polyfills.buffer'
const mocks = {}
const modules = {}
const originalRequire = window['require']
const customRequire = path => {
if (mocks[path]) {
console.log(':: mock', path)
return mocks[path]
}
if (modules[path]) {
return modules[path]
}
return originalRequire(path)
}
customRequire['resolve'] = (() => null) as any
customRequire['main'] = {
paths: [],
}
async function webRequire (url) {
console.log(`Loading ${url}`)
const e = document.createElement('script')
window['module'] = { exports: {} } as any
window['exports'] = window['module'].exports
await new Promise(resolve => {
e.onload = resolve
e.src = url
document.querySelector('head').appendChild(e)
})
return window['module'].exports
}
const Terminus = {
registerMock: (name, mod) => {
mocks[name] = mod
},
registerModule: (name, mod) => {
modules[name] = mod
},
loadPlugin: async (url) => {
const pkg = await (await fetch(url + '/package.json')).json()
url += '/' + pkg.main
const module = await webRequire(url)
Terminus.registerModule(`resources/builtin-plugins/${pkg.name}`, module)
Terminus.registerModule(pkg.name, module)
return module
},
bootstrap: (...args) => window['bootstrapTerminus'](...args),
webRequire,
}
Object.assign(window, {
require: customRequire,
module: {
paths: [],
},
Terminus,
process: {
env: { },
argv: ['terminus'],
platform: 'darwin',
on: () => null,
stdout: {},
stderr: {},
resourcesPath: 'resources',
version: '14.0.0',
nextTick: (f, ...args) => setTimeout(() => f(...args)),
cwd: () => '/',
},
global: window,
})

View File

@@ -15,6 +15,7 @@ import * as ngxToastrModule from 'ngx-toastr'
import './polyfills.buffer' import './polyfills.buffer'
import { Duplex } from 'stream-browserify' import { Duplex } from 'stream-browserify'
const Terminus = window['Terminus']
export class SocketProxy extends Duplex { export class SocketProxy extends Duplex {
socket: any socket: any
@@ -50,18 +51,47 @@ export class SocketProxy extends Duplex {
} }
} }
const mocks = { Terminus.registerMock('fs', {
fs: {
realpathSync: () => null, realpathSync: () => null,
readdir: () => null, readdir: () => null,
stat: () => null, stat: () => null,
appendFile: () => null, appendFile: () => null,
constants: {}, constants: {},
}, })
buffer: { Terminus.registerMock('readline', {
cursorTo: () => null,
clearLine: stream => stream.write('\r'),
})
Terminus.registerMock('any-promise', Promise)
Terminus.registerMock('tls', {})
Terminus.registerMock('module', {
globalPaths: [],
prototype: { require: window['require'] },
})
Terminus.registerMock('url', {
parse: () => null,
})
Terminus.registerMock('http', {
Agent: class {},
request: {},
})
Terminus.registerMock('https', {
Agent: class {},
request: {},
})
Terminus.registerMock('querystring', {})
Terminus.registerMock('tty', { isatty: () => false })
Terminus.registerMock('child_process', {})
Terminus.registerMock('readable-stream', {})
Terminus.registerMock('os', {
platform: () => 'web',
homedir: () => '/home',
})
Terminus.registerModule('buffer', {
Buffer: window['Buffer'], Buffer: window['Buffer'],
}, })
crypto: { Terminus.registerModule('crypto', {
...require('crypto-browserify'), ...require('crypto-browserify'),
getHashes () { getHashes () {
return ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160'] return ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160']
@@ -69,47 +99,8 @@ const mocks = {
timingSafeEqual (a, b) { timingSafeEqual (a, b) {
return a.equals(b) return a.equals(b)
}, },
}, })
events: require('events'), Terminus.registerMock('hterm-umdjs', {
path: require('path-browserify'),
readline: {
cursorTo: () => null,
clearLine: stream => stream.write('\r'),
},
zlib: {
...require('browserify-zlib'),
constants: require('browserify-zlib'),
},
'any-promise': Promise,
tls: { },
module: {
globalPaths: [],
},
assert: require('assert'),
url: {
parse: () => null,
},
net: {
Socket: SocketProxy,
},
http: {
Agent: class {},
request: {},
},
https: {
Agent: class {},
request: {},
},
querystring: {},
tty: { isatty: () => false },
child_process: {},
'readable-stream': {},
os: {
platform: () => 'web',
homedir: () => '/home',
},
constants: require('constants-browserify'),
'hterm-umdjs': {
hterm: { hterm: {
PreferenceManager: class { set () {} }, PreferenceManager: class { set () {} },
VT: { VT: {
@@ -126,66 +117,50 @@ const mocks = {
Memory: class {}, Memory: class {},
}, },
}, },
}, })
dns: {}, Terminus.registerMock('dns', {})
socksv5: {}, Terminus.registerMock('socksv5', {})
util: require('util/'), Terminus.registerMock('util', require('util/'))
keytar: { Terminus.registerMock('keytar', {
getPassword: () => null, getPassword: () => null,
})
Terminus.registerModule('net', {
Socket: SocketProxy,
})
Terminus.registerModule('events', require('events'))
Terminus.registerModule('path', require('path-browserify'))
Terminus.registerModule('zlib', {
...require('browserify-zlib'),
constants: require('browserify-zlib'),
})
Terminus.registerModule('assert', Object.assign(
require('assert'),
{
assertNotStrictEqual: () => true,
notStrictEqual: () => true,
}, },
'./crypto/build/Release/sshcrypto.node': {}, ))
'../build/Release/cpufeatures.node': {}, Terminus.registerModule('constants', require('constants-browserify'))
} Terminus.registerModule('stream', require('stream-browserify'))
const builtins = { Terminus.registerModule('@angular/core', angularCoreModule)
'@angular/core': angularCoreModule, Terminus.registerModule('@angular/compiler', angularCompilerModule)
'@angular/compiler': angularCompilerModule, Terminus.registerModule('@angular/common', angularCommonModule)
'@angular/common': angularCommonModule, Terminus.registerModule('@angular/forms', angularFormsModule)
'@angular/forms': angularFormsModule, Terminus.registerModule('@angular/platform-browser', angularPlatformBrowserModule)
'@angular/platform-browser': angularPlatformBrowserModule, Terminus.registerModule('@angular/platform-browser/animations', angularPlatformBrowserAnimationsModule)
'@angular/platform-browser/animations': angularPlatformBrowserAnimationsModule, Terminus.registerModule('@angular/platform-browser-dynamic', angularPlatformBrowserDynamicModule)
'@angular/platform-browser-dynamic': angularPlatformBrowserDynamicModule, Terminus.registerModule('@angular/animations', angularAnimationsModule)
'@angular/animations': angularAnimationsModule, Terminus.registerModule('@ng-bootstrap/ng-bootstrap', ngBootstrapModule)
'@ng-bootstrap/ng-bootstrap': ngBootstrapModule, Terminus.registerModule('ngx-toastr', ngxToastrModule)
'ngx-toastr': ngxToastrModule, Terminus.registerModule('deepmerge', require('deepmerge'))
deepmerge: require('deepmerge'), Terminus.registerModule('rxjs', require('rxjs'))
rxjs: require('rxjs'), Terminus.registerModule('rxjs/operators', require('rxjs/operators'))
'rxjs/operators': require('rxjs/operators'), Terminus.registerModule('js-yaml', require('js-yaml'))
'js-yaml': require('js-yaml'), Terminus.registerModule('zone.js/dist/zone.js', require('zone.js/dist/zone.js'))
'zone.js/dist/zone.js': require('zone.js/dist/zone.js'),
}
const originalRequire = window['require']
const mockRequire = path => {
if (mocks[path]) {
console.log(':: mock', path)
return mocks[path]
}
if (builtins[path]) {
return builtins[path]
}
return originalRequire(path)
}
mockRequire['resolve'] = (() => null) as any
Object.assign(window, { Object.assign(window, {
require: mockRequire,
module: {
paths: [],
},
__dirname: '__dirname', __dirname: '__dirname',
setImmediate: setTimeout as any, setImmediate: setTimeout as any,
}) })
window['require'].main = {
paths: [],
} as any
mocks.module['prototype'] = { require: window['require'] }
mocks.assert.assertNotStrictEqual = () => true
mocks.assert.notStrictEqual = () => true
// Late mocks and builtins
builtins['stream'] = require('stream-browserify')