tabby/web/polyfills.ts

145 lines
4.6 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
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-29 21:57:04 +00:00
const Tabby = window['Tabby']
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,
})
2021-06-22 09:51:52 +00:00
this.socket = window['__connector__'].createSocket(...args)
2021-06-19 20:52:18 +00:00
this.socket.connect$.subscribe(() => this['emit']('connect'))
this.socket.data$.subscribe(data => this['emit']('data', Buffer.from(data)))
2021-06-22 09:51:52 +00:00
this.socket.error$.subscribe(error => this['emit']('error', error))
2021-06-19 20:52:18 +00:00
}
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
2021-06-29 21:57:04 +00:00
Tabby.registerMock('fs', {
2021-06-25 19:55:34 +00:00
realpathSync: () => null,
readdir: () => null,
stat: () => null,
appendFile: () => null,
constants: {},
})
2021-06-29 21:57:04 +00:00
Tabby.registerMock('any-promise', Promise)
Tabby.registerMock('tls', {})
Tabby.registerMock('module', {
2021-06-25 19:55:34 +00:00
globalPaths: [],
prototype: { require: window['require'] },
})
2021-06-29 21:57:04 +00:00
Tabby.registerMock('http', {
2021-06-25 19:55:34 +00:00
Agent: class {},
request: {},
})
2021-06-29 21:57:04 +00:00
Tabby.registerMock('https', {
2021-06-25 19:55:34 +00:00
Agent: class {},
request: {},
})
2021-06-29 21:57:04 +00:00
Tabby.registerMock('querystring', {})
Tabby.registerMock('tty', { isatty: () => false })
Tabby.registerMock('child_process', {})
Tabby.registerMock('readable-stream', {})
Tabby.registerMock('os', {
2021-06-25 19:55:34 +00:00
platform: () => 'web',
homedir: () => '/home',
})
2021-06-29 21:57:04 +00:00
Tabby.registerModule('buffer', {
2021-06-25 19:55:34 +00:00
Buffer: window['Buffer'],
})
2021-06-29 21:57:04 +00:00
Tabby.registerModule('crypto', {
2021-06-25 19:55:34 +00:00
...require('crypto-browserify'),
getHashes () {
return ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160']
2021-06-15 21:43:02 +00:00
},
2021-06-25 19:55:34 +00:00
timingSafeEqual (a, b) {
return a.equals(b)
2021-06-15 21:43:02 +00:00
},
2021-06-25 19:55:34 +00:00
})
2021-06-29 21:57:04 +00:00
Tabby.registerMock('dns', {})
Tabby.registerMock('@luminati-io/socksv5', {})
2021-06-29 21:57:04 +00:00
Tabby.registerMock('util', require('util/'))
Tabby.registerMock('keytar', {
2021-06-25 19:55:34 +00:00
getPassword: () => null,
})
2021-07-11 20:59:39 +00:00
Tabby.registerMock('@serialport/bindings', {})
2021-06-15 21:43:02 +00:00
2021-06-29 21:57:04 +00:00
Tabby.registerModule('net', {
2021-06-25 19:55:34 +00:00
Socket: SocketProxy,
})
2021-06-29 21:57:04 +00:00
Tabby.registerModule('events', require('events'))
Tabby.registerModule('path', require('path-browserify'))
2021-08-24 23:25:40 +00:00
Tabby.registerModule('url', {
...require('url'),
pathToFileURL: x => `file://${x}`,
})
2021-06-29 21:57:04 +00:00
Tabby.registerModule('zlib', {
2021-06-25 19:55:34 +00:00
...require('browserify-zlib'),
constants: require('browserify-zlib'),
})
2021-06-29 21:57:04 +00:00
Tabby.registerModule('assert', Object.assign(
2021-06-25 19:55:34 +00:00
require('assert'),
{
assertNotStrictEqual: () => true,
notStrictEqual: () => true,
},
))
2021-06-29 21:57:04 +00:00
Tabby.registerModule('constants', require('constants-browserify'))
Tabby.registerModule('stream', require('stream-browserify'))
2021-07-11 20:59:39 +00:00
Tabby.registerModule('readline', {
...require('readline-browserify'),
cursorTo: () => null,
clearLine: stream => stream.write('\r'),
})
2021-06-25 19:55:34 +00:00
2021-07-24 14:30:55 +00:00
Tabby.registerModule('@angular/core', require('@angular/core'))
Tabby.registerModule('@angular/cdk', require('@angular/cdk'))
Tabby.registerModule('@angular/cdk/clipboard', require('@angular/cdk/clipboard'))
Tabby.registerModule('@angular/cdk/drag-drop', require('@angular/cdk/drag-drop'))
Tabby.registerModule('@angular/compiler', require('@angular/compiler'))
Tabby.registerModule('@angular/common', require('@angular/common'))
Tabby.registerModule('@angular/forms', require('@angular/forms'))
Tabby.registerModule('@angular/platform-browser', require('@angular/platform-browser'))
Tabby.registerModule('@angular/platform-browser/animations', require('@angular/platform-browser/animations'))
Tabby.registerModule('@angular/platform-browser-dynamic', require('@angular/platform-browser-dynamic'))
Tabby.registerModule('@angular/animations', require('@angular/animations'))
Tabby.registerModule('@ng-bootstrap/ng-bootstrap', require('@ng-bootstrap/ng-bootstrap'))
Tabby.registerModule('ngx-toastr', require('ngx-toastr'))
2021-06-29 21:57:04 +00:00
Tabby.registerModule('deepmerge', require('deepmerge'))
Tabby.registerModule('rxjs', require('rxjs'))
Tabby.registerModule('rxjs/operators', require('rxjs'))
2021-06-29 21:57:04 +00:00
Tabby.registerModule('js-yaml', require('js-yaml'))
Tabby.registerModule('zone.js/dist/zone.js', require('zone.js/dist/zone.js'))
2021-06-15 21:43:02 +00:00
Object.assign(window, {
2021-06-19 20:52:18 +00:00
__dirname: '__dirname',
setImmediate: setTimeout as any,
2021-06-15 21:43:02 +00:00
})