Wednesday 19 April 2017

Chrome Native Messaging - Hanging Child Process

I'm trying to make an extension that uses chrome native messaging to communicate with youtube-dl using a node.js host script. I've been able to successfully parse the stdin from the extension & also been able to run a child process (i.e. touch file.dat), but when I try to exec/spawn youtube-dl it hangs on the command. I've tried the host script independently of chrome native input and it works fine. I think the problem may have something to do with 1MB limitations on buffer size of chrome native messaging. Is there a way around reading the buffer?

#! /usr/bin/env node
"use strict";
const fs = require('fs');
const exec = require('child_process').execSync;

const dlPath = '/home/toughluck/Music';

let first = true;
let buffers = [];
process.stdin.on('readable', () => {
  let chunk = process.stdin.read();
  if (chunk !== null) {
    if (first) {
      chunk = chunk.slice(4);
      first = false;
    }
    buffers.push(chunk);
  }
});
process.stdin.on('end', () => {
  const res = Buffer.concat(buffers);
  const url = JSON.parse(res).url;
  const outTemplate = `${dlPath}/%(title)s.%(ext)s`;
  const cmdOptions = {
    shell: '/bin/bash'
  };
  const cmd = `youtube-dl --extract-audio --audio-format mp3 -o \"${outTemplate}\" ${url}`;
  // const args = ['--extract-audio', '--audio-format', 'mp3', '-o', outTemplate, url];
  // const cmd2 = 'youtube-dl';

  process.stderr.write('Suck it chrome');
  process.stderr.write('stderr doesnt stop host');

  exec(cmd, cmdOptions, (err, stdout, stderr) => {
    if (err) throw err;
    process.stderr.write(stdout);
    process.stderr.write(stderr);
  });

  process.stderr.write('\n Okay....');
});

The full codebase can be found at https://github.com/wrleskovec/chrome-youtube-mp3-dl



via wrleskovec

No comments:

Post a Comment