polyfills.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
  2. /* eslint-disable @typescript-eslint/no-empty-function */
  3. /* eslint-disable @typescript-eslint/no-extraneous-class */
  4. import * as angularCoreModule from '@angular/core'
  5. import * as angularCompilerModule from '@angular/compiler'
  6. import * as angularCommonModule from '@angular/common'
  7. import * as angularFormsModule from '@angular/forms'
  8. import * as angularPlatformBrowserModule from '@angular/platform-browser'
  9. import * as angularPlatformBrowserAnimationsModule from '@angular/platform-browser/animations'
  10. import * as angularPlatformBrowserDynamicModule from '@angular/platform-browser-dynamic'
  11. import * as angularAnimationsModule from '@angular/animations'
  12. import * as ngBootstrapModule from '@ng-bootstrap/ng-bootstrap'
  13. import * as ngxToastrModule from 'ngx-toastr'
  14. import './polyfills.buffer'
  15. import { Duplex } from 'stream-browserify'
  16. export class SocketProxy extends Duplex {
  17. socket: any
  18. constructor (...args: any[]) {
  19. super({
  20. allowHalfOpen: false,
  21. })
  22. this.socket = new window['__connector__'].Socket(...args)
  23. this.socket.connect$.subscribe(() => this['emit']('connect'))
  24. this.socket.data$.subscribe(data => this['emit']('data', Buffer.from(data)))
  25. }
  26. connect (...args: any[]) {
  27. this.socket.connect(...args)
  28. }
  29. setNoDelay () { }
  30. setTimeout () { }
  31. _read (_size: number): void { }
  32. _write (chunk: Buffer, _encoding: string, callback: (error?: Error | null) => void): void {
  33. this.socket.write(chunk)
  34. callback()
  35. }
  36. _destroy (error: Error|null, callback: (error: Error|null) => void): void {
  37. this.socket.close(error)
  38. callback(error)
  39. }
  40. }
  41. const mocks = {
  42. fs: {
  43. realpathSync: () => null,
  44. readdir: () => null,
  45. stat: () => null,
  46. appendFile: () => null,
  47. constants: {},
  48. },
  49. buffer: {
  50. Buffer: window['Buffer'],
  51. },
  52. crypto: {
  53. ...require('crypto-browserify'),
  54. getHashes () {
  55. return ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160']
  56. },
  57. timingSafeEqual (a, b) {
  58. return a.equals(b)
  59. },
  60. },
  61. events: require('events'),
  62. path: require('path-browserify'),
  63. readline: {
  64. cursorTo: () => null,
  65. clearLine: stream => stream.write('\r'),
  66. },
  67. zlib: {
  68. ...require('browserify-zlib'),
  69. constants: require('browserify-zlib'),
  70. },
  71. 'any-promise': Promise,
  72. tls: { },
  73. module: {
  74. globalPaths: [],
  75. },
  76. assert: require('assert'),
  77. url: {
  78. parse: () => null,
  79. },
  80. net: {
  81. Socket: SocketProxy,
  82. },
  83. http: {
  84. Agent: class {},
  85. request: {},
  86. },
  87. https: {
  88. Agent: class {},
  89. request: {},
  90. },
  91. querystring: {},
  92. tty: { isatty: () => false },
  93. child_process: {},
  94. 'readable-stream': {},
  95. os: {
  96. platform: () => 'web',
  97. homedir: () => '/home',
  98. },
  99. constants: require('constants-browserify'),
  100. 'hterm-umdjs': {
  101. hterm: {
  102. PreferenceManager: class { set () {} },
  103. VT: {
  104. ESC: {},
  105. CSI: {},
  106. OSC: {},
  107. },
  108. Terminal: class {},
  109. Keyboard: class {},
  110. },
  111. lib: {
  112. wc: {},
  113. Storage: {
  114. Memory: class {},
  115. },
  116. },
  117. },
  118. dns: {},
  119. socksv5: {},
  120. util: require('util/'),
  121. keytar: {
  122. getPassword: () => null,
  123. },
  124. './crypto/build/Release/sshcrypto.node': {},
  125. '../build/Release/cpufeatures.node': {},
  126. }
  127. const builtins = {
  128. '@angular/core': angularCoreModule,
  129. '@angular/compiler': angularCompilerModule,
  130. '@angular/common': angularCommonModule,
  131. '@angular/forms': angularFormsModule,
  132. '@angular/platform-browser': angularPlatformBrowserModule,
  133. '@angular/platform-browser/animations': angularPlatformBrowserAnimationsModule,
  134. '@angular/platform-browser-dynamic': angularPlatformBrowserDynamicModule,
  135. '@angular/animations': angularAnimationsModule,
  136. '@ng-bootstrap/ng-bootstrap': ngBootstrapModule,
  137. 'ngx-toastr': ngxToastrModule,
  138. deepmerge: require('deepmerge'),
  139. rxjs: require('rxjs'),
  140. 'rxjs/operators': require('rxjs/operators'),
  141. 'js-yaml': require('js-yaml'),
  142. 'zone.js/dist/zone.js': require('zone.js/dist/zone.js'),
  143. }
  144. const originalRequire = window['require']
  145. const mockRequire = path => {
  146. if (mocks[path]) {
  147. console.log(':: mock', path)
  148. return mocks[path]
  149. }
  150. if (builtins[path]) {
  151. return builtins[path]
  152. }
  153. return originalRequire(path)
  154. }
  155. mockRequire['resolve'] = (() => null) as any
  156. Object.assign(window, {
  157. require: mockRequire,
  158. module: {
  159. paths: [],
  160. },
  161. __dirname: '__dirname',
  162. setImmediate: setTimeout as any,
  163. })
  164. window['require'].main = {
  165. paths: [],
  166. } as any
  167. mocks.module['prototype'] = { require: window['require'] }
  168. mocks.assert.assertNotStrictEqual = () => true
  169. mocks.assert.notStrictEqual = () => true
  170. // Late mocks and builtins
  171. builtins['ssh2'] = require('ssh2')
  172. builtins['ssh2/lib/protocol/constants'] = require('ssh2/lib/protocol/constants')
  173. builtins['stream'] = require('stream-browserify')