<commit_before>/**
 * Copyright 2018 The AMP HTML Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const minimist = require('minimist');
const {cyan, green} = require('../common/colors');
const {log} = require('../common/logging');

let serveMode = 'default';

/**
 * Returns a string representation of the server's mode.
 * @return {string}
 */
function getServeMode() {
  return serveMode;
}

/**
 * Sets the server's mode. Uses command line arguments by default, but can be
 * overridden by passing in a modeOptions object.
 * @param {!Object} modeOptions
 */
function setServeMode(modeOptions) {
  if (Object.keys(modeOptions).length == 0) {
    modeOptions = minimist(process.argv.slice(2), {string: ['rtv']});
  }

  if (modeOptions.compiled) {
    serveMode = 'compiled';
  } else if (modeOptions.esm) {
    serveMode = 'esm';
  } else if (modeOptions.cdn) {
    serveMode = 'cdn';
  } else if (modeOptions.rtv) {
    const {rtv} = modeOptions;
    if (isRtvMode(rtv)) {
      serveMode = rtv;
    } else {
      throw new Error(`Invalid rtv: ${rtv}. (Must be 15 digits long.)`);
    }
  }
}

/**
 * Logs the server's mode.
 */
function logServeMode() {
  const serveMode = getServeMode();
  if (serveMode == 'compiled') {
    log(green('Serving'), cyan('minified'), green('JS'));
  } else if (serveMode == 'esm') {
    log(green('Serving'), cyan('ESM'), green('JS'));
  } else if (serveMode == 'cdn') {
    log(green('Serving'), cyan('current prod'), green('JS'));
  } else if (isRtvMode(serveMode)) {
    log(green('Serving JS from RTV'), cyan(serveMode));
  } else {
    log(green('Serving'), cyan('unminified'), green('JS'));
  }
}

/**
 * @param {string} serveMode
 * @return {boolean}
 */
const isRtvMode = (serveMode) => {
  return /^\d{15}$/.test(serveMode);
};

/**
 * @param {string} mode
 * @param {string} file
 * @param {string=} hostName
 * @param {boolean=} inabox
 * @return {string}
 */
const replaceUrls = (mode, file, hostName, inabox) => {
  hostName = hostName || '';
  if (mode == 'default') {
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/v0\.js/g,
      hostName + '/dist/amp.js'
    );
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/shadow-v0\.js/g,
      hostName + '/dist/amp-shadow.js'
    );
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/amp4ads-v0\.js/g,
      hostName + '/dist/amp-inabox.js'
    );
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/video-iframe-integration-v0\.js/g,
      hostName + '/dist/video-iframe-integration.js'
    );
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/v0\/(.+?).js/g,
      hostName + '/dist/v0/$1.max.js'
    );
    if (inabox) {
      const filename = '/dist/amp-inabox.js';
      file = file.replace(/<html [^>]*>/, '<html amp4ads>');
      file = file.replace(/\/dist\/amp\.js/g, filename);
    }
  } else if (mode == 'compiled') {
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/v0\.(m?js)/g,
      hostName + '/dist/v0.$1'
    );
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/shadow-v0\.(m?js)/g,
      hostName + '/dist/shadow-v0.$1'
    );
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/amp4ads-v0\.(m?js)/g,
      hostName + '/dist/amp4ads-v0.$1'
    );
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/video-iframe-integration-v0\.(m?js)/g,
      hostName + '/dist/video-iframe-integration-v0.$1'
    );
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/v0\/(.+?)\.(m?js)/g,
      hostName + '/dist/v0/$1.$2'
    );
    file = file.replace(
      /\/dist\/v0\/examples\/(.*)\.max\.(m?js)/g,
      '/dist/v0/examples/$1.$2'<commit_msg>🏗♻️ Allow rewriting `.mjs` CDN files on local mode (#34741)<commit_after>/**
 * Copyright 2018 The AMP HTML Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const minimist = require('minimist');
const {cyan, green} = require('../common/colors');
const {log} = require('../common/logging');

let serveMode = 'default';

/**
 * Returns a string representation of the server's mode.
 * @return {string}
 */
function getServeMode() {
  return serveMode;
}

/**
 * Sets the server's mode. Uses command line arguments by default, but can be
 * overridden by passing in a modeOptions object.
 * @param {!Object} modeOptions
 */
function setServeMode(modeOptions) {
  if (Object.keys(modeOptions).length == 0) {
    modeOptions = minimist(process.argv.slice(2), {string: ['rtv']});
  }

  if (modeOptions.compiled) {
    serveMode = 'compiled';
  } else if (modeOptions.esm) {
    serveMode = 'esm';
  } else if (modeOptions.cdn) {
    serveMode = 'cdn';
  } else if (modeOptions.rtv) {
    const {rtv} = modeOptions;
    if (isRtvMode(rtv)) {
      serveMode = rtv;
    } else {
      throw new Error(`Invalid rtv: ${rtv}. (Must be 15 digits long.)`);
    }
  }
}

/**
 * Logs the server's mode.
 */
function logServeMode() {
  const serveMode = getServeMode();
  if (serveMode == 'compiled') {
    log(green('Serving'), cyan('minified'), green('JS'));
  } else if (serveMode == 'esm') {
    log(green('Serving'), cyan('ESM'), green('JS'));
  } else if (serveMode == 'cdn') {
    log(green('Serving'), cyan('current prod'), green('JS'));
  } else if (isRtvMode(serveMode)) {
    log(green('Serving JS from RTV'), cyan(serveMode));
  } else {
    log(green('Serving'), cyan('unminified'), green('JS'));
  }
}

/**
 * @param {string} serveMode
 * @return {boolean}
 */
const isRtvMode = (serveMode) => {
  return /^\d{15}$/.test(serveMode);
};

/**
 * @param {string} mode
 * @param {string} file
 * @param {string=} hostName
 * @param {boolean=} inabox
 * @return {string}
 */
const replaceUrls = (mode, file, hostName, inabox) => {
  hostName = hostName || '';
  if (mode == 'default') {
    // TODO(alanorozco): Match argv.esm in output extension/ and/or allow
    // `.mjs` to be lazily built regardless of `.mjs`
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/v0\.(m?js)/g,
      hostName + '/dist/amp.js'
    );
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/shadow-v0\.(m?js)/g,
      hostName + '/dist/amp-shadow.js'
    );
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/amp4ads-v0\.(m?js)/g,
      hostName + '/dist/amp-inabox.js'
    );
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/video-iframe-integration-v0\.(m?js)/g,
      hostName + '/dist/video-iframe-integration.js'
    );
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/v0\/(.+?)\.(m?js)/g,
      hostName + '/dist/v0/$1.max.js'
    );
    if (inabox) {
      const filename = '/dist/amp-inabox.js';
      file = file.replace(/<html [^>]*>/, '<html amp4ads>');
      file = file.replace(/\/dist\/amp\.(m?js)/g, filename);
    }
  } else if (mode == 'compiled') {
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/v0\.(m?js)/g,
      hostName + '/dist/v0.$1'
    );
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/shadow-v0\.(m?js)/g,
      hostName + '/dist/shadow-v0.$1'
    );
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/amp4ads-v0\.(m?js)/g,
      hostName + '/dist/amp4ads-v0.$1'
    );
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/video-iframe-integration-v0\.(m?js)/g,
      hostName + '/dist/video-iframe-integration-v0.$1'
    );
    file = file.replace(
      /https:\/\/cdn\.ampproject\.org\/v0\/(.+?)\.(m?js)/g,
      hostName + '/dist/v0/$1.$2'
    );
    file = file.replace(
      /\/dist\/v0\/examples\/(.*)\.max\.(m?js)/g,
      '/dist/v0/examples/$1.$2'
{'fraction_non_alphanumeric': 0.19582034407112342, 'fraction_numerical': 0.010968710310587692, 'mean_word_length': 2.7001281503630925, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7297856', 'n_tokens_mistral': 3206, 'n_tokens_neox': 3047, 'n_words': 680}