<commit_before>'use strict';

const fs = require('fs');
const path = require('path');
const { URL } = require('url');
const { Server } = require('@hapi/hapi');
const h2o2 = require('@hapi/h2o2');
const hapiPluginWebsocket = require('hapi-plugin-websocket');
const debugLog = require('./debugLog');
const LambdaContext = require('./LambdaContext.js');
const functionHelper = require('./functionHelper');
const { createUniqueId, parseQueryStringParameters } = require('./utils');
const authFunctionNameExtractor = require('./authFunctionNameExtractor');
const wsHelpers = require('./websocketHelpers');

// dummy placeholder url for the WHATWG URL constructor
// https://github.com/nodejs/node/issues/12682
// TODO move to common constants file
const BASE_URL_PLACEHOLDER = 'http://example';

module.exports = class ApiGatewayWebSocket {
  constructor(serverless, options) {
    this.service = serverless.service;
    this.log = serverless.cli.log.bind(serverless.cli);
    this.options = options;
    this.clients = new Map();
    this.actions = {};
    this.websocketsApiRouteSelectionExpression =
      serverless.service.provider.websocketsApiRouteSelectionExpression ||
      '$request.body.action';
  }

  printBlankLine() {
    console.log();
  }

  _createWebSocket() {
    // start COPY PASTE FROM HTTP SERVER CODE
    const serverOptions = {
      host: this.options.host,
      port: this.options.websocketPort,
      router: {
        stripTrailingSlash: !this.options.preserveTrailingSlash, // removes trailing slashes on incoming paths.
      },
    };

    const httpsDir = this.options.httpsProtocol;

    // HTTPS support
    if (typeof httpsDir === 'string' && httpsDir.length > 0) {
      serverOptions.tls = {
        cert: fs.readFileSync(path.resolve(httpsDir, 'cert.pem'), 'ascii'),
        key: fs.readFileSync(path.resolve(httpsDir, 'key.pem'), 'ascii'),
      };
    }

    serverOptions.state = this.options.enforceSecureCookies
      ? {
          isHttpOnly: true,
          isSameSite: false,
          isSecure: true,
        }
      : {
          isHttpOnly: false,
          isSameSite: false,
          isSecure: false,
        };

    // Hapijs server creation
    this.wsServer = new Server(serverOptions);

    this.wsServer.register(h2o2).catch((err) => this.log(err));

    // Enable CORS preflight response
    this.wsServer.ext('onPreResponse', (request, h) => {<commit_msg>Websocket: remove unused hapi proxy plugin<commit_after>'use strict';

const fs = require('fs');
const path = require('path');
const { URL } = require('url');
const { Server } = require('@hapi/hapi');
const hapiPluginWebsocket = require('hapi-plugin-websocket');
const debugLog = require('./debugLog');
const LambdaContext = require('./LambdaContext.js');
const functionHelper = require('./functionHelper');
const { createUniqueId, parseQueryStringParameters } = require('./utils');
const authFunctionNameExtractor = require('./authFunctionNameExtractor');
const wsHelpers = require('./websocketHelpers');

// dummy placeholder url for the WHATWG URL constructor
// https://github.com/nodejs/node/issues/12682
// TODO move to common constants file
const BASE_URL_PLACEHOLDER = 'http://example';

module.exports = class ApiGatewayWebSocket {
  constructor(serverless, options) {
    this.service = serverless.service;
    this.log = serverless.cli.log.bind(serverless.cli);
    this.options = options;
    this.clients = new Map();
    this.actions = {};
    this.websocketsApiRouteSelectionExpression =
      serverless.service.provider.websocketsApiRouteSelectionExpression ||
      '$request.body.action';
  }

  printBlankLine() {
    console.log();
  }

  _createWebSocket() {
    // start COPY PASTE FROM HTTP SERVER CODE
    const serverOptions = {
      host: this.options.host,
      port: this.options.websocketPort,
      router: {
        stripTrailingSlash: !this.options.preserveTrailingSlash, // removes trailing slashes on incoming paths.
      },
    };

    const httpsDir = this.options.httpsProtocol;

    // HTTPS support
    if (typeof httpsDir === 'string' && httpsDir.length > 0) {
      serverOptions.tls = {
        cert: fs.readFileSync(path.resolve(httpsDir, 'cert.pem'), 'ascii'),
        key: fs.readFileSync(path.resolve(httpsDir, 'key.pem'), 'ascii'),
      };
    }

    serverOptions.state = this.options.enforceSecureCookies
      ? {
          isHttpOnly: true,
          isSameSite: false,
          isSecure: true,
        }
      : {
          isHttpOnly: false,
          isSameSite: false,
          isSecure: false,
        };

    // Hapijs server creation
    this.wsServer = new Server(serverOptions);

    // Enable CORS preflight response
    this.wsServer.ext('onPreResponse', (request, h) => {
{'fraction_non_alphanumeric': 0.13083718973496003, 'fraction_numerical': 0.0037862852334875894, 'mean_word_length': 3.2645739910313902, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 2, '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': '784811', 'n_tokens_mistral': 1427, 'n_tokens_neox': 1386, 'n_words': 298}