<commit_before>/* eslint babel/semi: "error" */
import { promisifyAll } from 'bluebird';
import Redis from 'ioredis';
import {
  cloneDeep,
  compact,
  flatten,
  intersection,
  isArray,
  isFunction,
  isPlainObject,
  keyBy,
  last,
  map,
  noop,
  uniqBy,
} from 'lodash';
import IoServer from 'socket.io';
import redis_adapter from 'socket.io-redis';
import createDebug from 'debug';
import Raven from 'raven';
import config from 'config';

import { dbAdapter, Comment } from './models';
import { eventNames } from './support/PubSubAdapter';
import { List } from './support/open-lists';
import { withAuthToken } from './controllers/middlewares/with-auth-token';
import {
  HOMEFEED_MODE_FRIENDS_ALL_ACTIVITY,
  HOMEFEED_MODE_CLASSIC,
  HOMEFEED_MODE_FRIENDS_ONLY,
} from './models/timeline';
import { serializeSinglePost, serializeLike } from './serializers/v2/post';
import { serializeCommentForRealtime } from './serializers/v2/comment';
import { serializeUsersByIds } from './serializers/v2/user';

const sentryIsEnabled = 'sentryDsn' in config;
const debug = createDebug('freefeed:PubsubListener');

export default class PubsubListener {
  app;
  io;

  constructor(server, app) {
    this.app = app;

    const pubClient = new Redis({
      host: config.redis.host,
      port: config.redis.port,
      db: config.database,
    });
    const subClient = new Redis({
      host: config.redis.host,
      port: config.redis.port,
      db: config.database,
    });

    this.io = IoServer(server);
    this.io.adapter(redis_adapter({ pubClient, subClient }));

    this.io.on('error', (err) => {
      debug('socket.io error', err);
    });

    // authentication
    this.io.use(async (socket, next) => {
      try {
        socket.userId = await getAuthUserId(socket.handshake.query.token, socket);
        socket.authToken = socket.handshake.query.token;
        debug(`[socket.id=${socket.id}] auth user`, socket.userId);
      } catch (e) {
        // Can not properly return error to client so just treat user as anonymous
        socket.userId = null;
        socket.authToken = null;
        debug(`[socket.id=${socket.id}] auth error`, e.message);
      }

      return next();<commit_msg>Allow any Origin for socket.io explicitly<commit_after>/* eslint babel/semi: "error" */
import { promisifyAll } from 'bluebird';
import Redis from 'ioredis';
import {
  cloneDeep,
  compact,
  flatten,
  intersection,
  isArray,
  isFunction,
  isPlainObject,
  keyBy,
  last,
  map,
  noop,
  uniqBy,
} from 'lodash';
import IoServer from 'socket.io';
import redis_adapter from 'socket.io-redis';
import createDebug from 'debug';
import Raven from 'raven';
import config from 'config';

import { dbAdapter, Comment } from './models';
import { eventNames } from './support/PubSubAdapter';
import { List } from './support/open-lists';
import { withAuthToken } from './controllers/middlewares/with-auth-token';
import {
  HOMEFEED_MODE_FRIENDS_ALL_ACTIVITY,
  HOMEFEED_MODE_CLASSIC,
  HOMEFEED_MODE_FRIENDS_ONLY,
} from './models/timeline';
import { serializeSinglePost, serializeLike } from './serializers/v2/post';
import { serializeCommentForRealtime } from './serializers/v2/comment';
import { serializeUsersByIds } from './serializers/v2/user';

const sentryIsEnabled = 'sentryDsn' in config;
const debug = createDebug('freefeed:PubsubListener');

export default class PubsubListener {
  app;
  io;

  constructor(server, app) {
    this.app = app;

    const pubClient = new Redis({
      host: config.redis.host,
      port: config.redis.port,
      db: config.database,
    });
    const subClient = new Redis({
      host: config.redis.host,
      port: config.redis.port,
      db: config.database,
    });

    this.io = IoServer(server);
    this.io.adapter(redis_adapter({ pubClient, subClient }));

    // allow any CORS Origin
    this.io.origins((_, callback) => {
      callback(null, true);
    });

    this.io.on('error', (err) => {
      debug('socket.io error', err);
    });

    // authentication
    this.io.use(async (socket, next) => {
      try {
        socket.userId = await getAuthUserId(socket.handshake.query.token, socket);
        socket.authToken = socket.handshake.query.token;
        debug(`[socket.id=${socket.id}] auth user`, socket.userId);
      } catch (e) {
        // Can not properly return error to client so just treat user as anonymous
        socket.userId = null;
        socket.authToken = null;
        debug(`[socket.id=${socket.id}] auth error`, e.message);
      }

      return next();
{'fraction_non_alphanumeric': 0.13299626291492636, 'fraction_numerical': 0.0013189712024620796, 'mean_word_length': 3.3416030534351147, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '2093338', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1454, 'n_words': 354}