<commit_before>var fs = require('fs')
  , expect = require('chai').expect
  , async = require('async')
  , checksum = require('checksum')
  , ss = require('../../')
  , support = require('./support')
  , client = support.client
  , port = 8888;


describe('socket.io-stream', function() {
  beforeEach(function(done) {
    support.startServer(this, done);
  });
  afterEach(function(done) {
    support.stopServer(this, done);
  });

  describe('streaming', function() {
    var filename = __dirname + '/resources/frog.jpg'
      , _filename = filename + '.tmp';

    afterEach(function(done) {
      fs.unlink(_filename, done);
    });

    describe('emit to server', function() {
      it('should be able to send a file to the server', function(done) {
        this.io.of('/foo').on('connection', function(socket) {
          ss(socket).on('file', function(stream, data, callback) {
            expect(data.name).to.eql(filename);

            var dst = fs.createWriteStream(_filename);
            // use 'close' since 'finish' is not supported on the old Stream.
            dst.on('close', callback);
            stream.pipe(dst);
          });
        });

        var socket = client('/foo');
        socket.on('connect', function() {
          var stream = ss.createStream();

          ss(socket).emit('file', stream, {name: filename}, function() {
            async.map([filename, _filename], checksum.file, function(err, sums) {
              // check if two files are equal.
              expect(sums[0]).to.eql(sums[1]);
              done();
            });
          });
          fs.createReadStream(filename).pipe(stream);
        });
      });

      it('should be able to send a file to the client', function(done) {
        this.io.of('/foo').on('connection', function(socket) {
          ss(socket).on('file', function(stream, data) {
            expect(data.name).to.eql(filename);
            fs.createReadStream(filename).pipe(stream);
          });
        });

        var socket = client('/foo');
        socket.on('connect', function() {
          var stream = ss.createStream();
          ss(socket).emit('file', stream, {name: filename});

          var dst = fs.createWriteStream(_filename);
          dst.on('close', function() {
            async.map([filename, _filename], checksum.file, function(err, sums) {
              expect(sums[0]).to.eql(sums[1]);
              done();
            });
          });
          stream.pipe(dst);
        });
      });

      it('should be able to send back a file', function(done) {
        this.io.of('/foo').on('connection', function(socket) {
          ss(socket).on('file', function(stream, data) {
            expect(data.name).to.eql(filename);
            stream.pipe(stream);
          });
        });

        var socket = client('/foo');
        socket.on('connect', function() {
          var stream = ss.createStream();
          ss(socket).emit('file', stream, {name: filename});

          var dst = fs.createWriteStream(_filename);
          dst.on('close', function() {
            async.map([filename, _filename], checksum.file, function(err, sums) {
              expect(sums[0]).to.eql(sums[1]);
              done();
            });
          });
          fs.createReadStream(filename).pipe(stream).pipe(dst);
        });
      });<commit_msg>tweak<commit_after>var fs = require('fs')
  , expect = require('chai').expect
  , async = require('async')
  , checksum = require('checksum')
  , ss = require('../../')
  , support = require('./support')
  , client = support.client
  , port = 8888;


describe('socket.io-stream', function() {
  beforeEach(function(done) {
    support.startServer(this, done);
  });
  afterEach(function(done) {
    support.stopServer(this, done);
  });

  describe('streaming', function() {
    var filename = __dirname + '/resources/frog.jpg'
      , _filename = filename + '.tmp';

    afterEach(function(done) {
      fs.unlink(_filename, done);
    });

    describe('emit to server', function() {
      it('should be able to send a file to the server', function(done) {
        this.io.of('/foo').on('connection', function(socket) {
          ss(socket).on('file', function(stream, data, callback) {
            expect(data.name).to.eql(filename);

            var dst = fs.createWriteStream(_filename);
            // use 'close' since 'finish' is not supported on the old Stream.
            dst.on('close', callback);
            stream.pipe(dst);
          });
        });

        var socket = client('/foo');
        socket.on('connect', function() {
          var stream = ss.createStream();

          ss(socket).emit('file', stream, {name: filename}, function() {
            async.map([filename, _filename], checksum.file, function(err, sums) {
              // check if two files are equal.
              expect(sums[0]).to.eql(sums[1]);
              done();
            });
          });
          fs.createReadStream(filename).pipe(stream);
        });
      });

      it('should be able to send a file to the client', function(done) {
        this.io.of('/foo').on('connection', function(socket) {
          ss(socket).on('file', function(stream, data) {
            expect(data.name).to.eql(filename);
            fs.createReadStream(filename).pipe(stream);
          });
        });

        var socket = client('/foo');
        socket.on('connect', function() {
          var stream = ss.createStream();
          ss(socket).emit('file', stream, {name: filename});

          var dst = fs.createWriteStream(_filename);
          dst.on('close', function() {
            async.map([filename, _filename], checksum.file, function(err, sums) {
              expect(sums[0]).to.eql(sums[1]);
              done();
            });
          });
          stream.pipe(dst);
        });
      });

      it('should be able to send back a file', function(done) {
        this.io.of('/foo').on('connection', function(socket) {
          ss(socket, {allowHalfOpen: true}).on('file', function(stream, data) {
            expect(data.name).to.eql(filename);
            stream.pipe(stream);
          });
        });

        var socket = client('/foo');
        socket.on('connect', function() {
          var stream = ss.createStream();
          ss(socket).emit('file', stream, {name: filename});

          var dst = fs.createWriteStream(_filename);
          dst.on('close', function() {
            async.map([filename, _filename], checksum.file, function(err, sums) {
              expect(sums[0]).to.eql(sums[1]);
              done();
            });
          });
          fs.createReadStream(filename).pipe(stream).pipe(dst);
        });
      });
{'fraction_non_alphanumeric': 0.1700283878679217, 'fraction_numerical': 0.0029881966233378156, 'mean_word_length': 2.097639981490051, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7555610', 'n_tokens_mistral': 1914, 'n_tokens_neox': 1872, 'n_words': 376}