tabby/web/polyfills.ts

193 lines
5.2 KiB
TypeScript
Raw Normal View History

2021-06-19 20:59:15 +00:00
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
2021-06-19 20:52:18 +00:00
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/no-extraneous-class */
2021-06-15 21:43:02 +00:00
import * as angularCoreModule from '@angular/core'
import * as angularCompilerModule from '@angular/compiler'
import * as angularCommonModule from '@angular/common'
import * as angularFormsModule from '@angular/forms'
import * as angularPlatformBrowserModule from '@angular/platform-browser'
import * as angularPlatformBrowserAnimationsModule from '@angular/platform-browser/animations'
import * as angularPlatformBrowserDynamicModule from '@angular/platform-browser-dynamic'
import * as angularAnimationsModule from '@angular/animations'
import * as ngBootstrapModule from '@ng-bootstrap/ng-bootstrap'
import * as ngxToastrModule from 'ngx-toastr'
2021-06-19 20:52:18 +00:00
import './polyfills.buffer'
import { Duplex } from 'stream-browserify'
2021-06-15 21:43:02 +00:00
2021-06-19 20:52:18 +00:00
export class SocketProxy extends Duplex {
socket: any
2021-06-15 21:43:02 +00:00
2021-06-19 20:59:15 +00:00
constructor (...args: any[]) {
2021-06-19 20:52:18 +00:00
super({
allowHalfOpen: false,
})
this.socket = new window['__connector__'].Socket(...args)
this.socket.connect$.subscribe(() => this['emit']('connect'))
this.socket.data$.subscribe(data => this['emit']('data', Buffer.from(data)))
}
2021-06-19 20:59:15 +00:00
connect (...args: any[]) {
2021-06-19 20:52:18 +00:00
this.socket.connect(...args)
}
setNoDelay () { }
setTimeout () { }
_read (_size: number): void { }
_write (chunk: Buffer, _encoding: string, callback: (error?: Error | null) => void): void {
this.socket.write(chunk)
callback()
}
_destroy (error: Error|null, callback: (error: Error|null) => void): void {
this.socket.close(error)
callback(error)
}
}
2021-06-15 21:43:02 +00:00
const mocks = {
fs: {
realpathSync: () => null,
readdir: () => null,
stat: () => null,
appendFile: () => null,
constants: {},
},
buffer: {
2021-06-19 20:52:18 +00:00
Buffer: window['Buffer'],
2021-06-15 21:43:02 +00:00
},
crypto: {
...require('crypto-browserify'),
getHashes () {
return ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160']
},
timingSafeEqual (a, b) {
return a.equals(b)
},
},
events: require('events'),
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,
},
2021-06-19 20:52:18 +00:00
net: {
Socket: SocketProxy,
},
2021-06-15 21:43:02 +00:00
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: {
PreferenceManager: class { set () {} },
VT: {
ESC: {},
CSI: {},
OSC: {},
},
Terminal: class {},
Keyboard: class {},
},
lib: {
wc: {},
Storage: {
Memory: class {},
},
},
},
dns: {},
2021-06-19 20:52:18 +00:00
socksv5: {},
2021-06-15 21:43:02 +00:00
util: require('util/'),
keytar: {
getPassword: () => null,
},
2021-06-19 20:52:18 +00:00
'./crypto/build/Release/sshcrypto.node': {},
'../build/Release/cpufeatures.node': {},
2021-06-15 21:43:02 +00:00
}
const builtins = {
'@angular/core': angularCoreModule,
'@angular/compiler': angularCompilerModule,
'@angular/common': angularCommonModule,
'@angular/forms': angularFormsModule,
'@angular/platform-browser': angularPlatformBrowserModule,
'@angular/platform-browser/animations': angularPlatformBrowserAnimationsModule,
'@angular/platform-browser-dynamic': angularPlatformBrowserDynamicModule,
'@angular/animations': angularAnimationsModule,
'@ng-bootstrap/ng-bootstrap': ngBootstrapModule,
'ngx-toastr': ngxToastrModule,
deepmerge: require('deepmerge'),
rxjs: require('rxjs'),
'rxjs/operators': require('rxjs/operators'),
'js-yaml': require('js-yaml'),
'zone.js/dist/zone.js': require('zone.js/dist/zone.js'),
}
const originalRequire = window['require']
const mockRequire = path => {
if (mocks[path]) {
2021-06-19 20:59:15 +00:00
console.log(':: mock', path)
2021-06-15 21:43:02 +00:00
return mocks[path]
}
if (builtins[path]) {
return builtins[path]
}
return originalRequire(path)
}
mockRequire['resolve'] = (() => null) as any
Object.assign(window, {
require: mockRequire,
2021-06-19 20:52:18 +00:00
module: {
paths: [],
},
__dirname: '__dirname',
setImmediate: setTimeout as any,
2021-06-15 21:43:02 +00:00
})
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['ssh2'] = require('ssh2')
builtins['ssh2/lib/protocol/constants'] = require('ssh2/lib/protocol/constants')
builtins['stream'] = require('stream-browserify')