1
1
mirror of https://github.com/Eugeny/tabby.git synced 2024-11-23 13:13:49 +03:00
tabby/web/polyfills.ts

192 lines
5.1 KiB
TypeScript
Raw Normal View History

2021-06-19 23:52:18 +03:00
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/no-extraneous-class */
2021-06-16 00:43:02 +03: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 23:52:18 +03:00
import './polyfills.buffer'
import { Duplex } from 'stream-browserify'
2021-06-16 00:43:02 +03:00
2021-06-19 23:52:18 +03:00
export class SocketProxy extends Duplex {
socket: any
2021-06-16 00:43:02 +03:00
2021-06-19 23:52:18 +03:00
constructor (...args) {
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)))
}
connect (...args) {
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-16 00:43:02 +03:00
const mocks = {
fs: {
realpathSync: () => null,
readdir: () => null,
stat: () => null,
appendFile: () => null,
constants: {},
},
buffer: {
2021-06-19 23:52:18 +03:00
Buffer: window['Buffer'],
2021-06-16 00:43:02 +03: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 23:52:18 +03:00
net: {
Socket: SocketProxy,
},
2021-06-16 00:43:02 +03: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 23:52:18 +03:00
socksv5: {},
2021-06-16 00:43:02 +03:00
util: require('util/'),
keytar: {
getPassword: () => null,
},
2021-06-19 23:52:18 +03:00
'./crypto/build/Release/sshcrypto.node': {},
'../build/Release/cpufeatures.node': {},
2021-06-16 00:43:02 +03: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]) {
console.warn('requiring mock', path)
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 23:52:18 +03:00
module: {
paths: [],
},
__dirname: '__dirname',
setImmediate: setTimeout as any,
2021-06-16 00:43:02 +03: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')