utils.js 2.4 KB

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