utils.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* global window, capabilities */
  2. const path = require('path')
  3. const { spawn } = require('child_process')
  4. function selectFakeFile (uppyID) {
  5. var blob = new Blob(
  6. ['data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMTIwIDEyMCI+CiAgPGNpcmNsZSBjeD0iNjAiIGN5PSI2MCIgcj0iNTAiLz4KPC9zdmc+Cg=='],
  7. { type: 'image/svg+xml' }
  8. )
  9. window[uppyID].addFile({
  10. source: 'test',
  11. name: 'test-file',
  12. type: blob.type,
  13. data: blob
  14. })
  15. }
  16. function supportsChooseFile () {
  17. // Webdriver for Safari and Edge doesn’t support .chooseFile
  18. return capabilities.browserName !== 'safari' &&
  19. capabilities.browserName !== 'MicrosoftEdge' &&
  20. capabilities.platformName !== 'Android'
  21. }
  22. function prematureExit () {
  23. throw new Error('Companion exited early')
  24. }
  25. class CompanionService {
  26. onPrepare () {
  27. this.companion = spawn('node', [
  28. path.join(__dirname, '../../packages/@uppy/companion/lib/standalone/start-server')
  29. ], {
  30. stdio: 'pipe',
  31. env: Object.assign({}, process.env, {
  32. COMPANION_DATADIR: path.join(__dirname, '../../output'),
  33. COMPANION_DOMAIN: 'localhost:3020',
  34. COMPANION_PROTOCOL: 'http',
  35. COMPANION_PORT: 3020,
  36. COMPANION_SECRET: 'test'
  37. })
  38. })
  39. return new Promise((resolve, reject) => {
  40. this.companion.on('error', reject)
  41. this.companion.stdout.on('data', (chunk) => {
  42. if (`${chunk}`.includes('Listening on')) {
  43. resolve()
  44. }
  45. })
  46. this.companion.on('error', console.error)
  47. this.companion.stderr.pipe(process.stderr)
  48. this.companion.on('exit', prematureExit)
  49. })
  50. }
  51. onComplete () {
  52. return new Promise((resolve) => {
  53. this.companion.removeListener('exit', prematureExit)
  54. this.companion.on('exit', () => resolve())
  55. this.companion.kill('SIGINT')
  56. })
  57. }
  58. }
  59. module.exports = {
  60. selectFakeFile,
  61. supportsChooseFile,
  62. CompanionService
  63. }