<commit_before>'use strict';

/**
 * Fetch a package from the place that has packages.
 *
 * The server-side renderer takes, as input, a list of js packages, in
 * addition to the name of the react component to render.  This module
 * provides the function for taking the url-path of a package and
 * actually retrieving the package.
 *
 * The server that holds the package information is hard-coded in this
 * file, to avoid letting this server execute arbitrary code.
 */

const request = require('superagent');

const cache = require("./cache");

// All files requested via /render are fetched via this hostname.
let serverHostname;

// fetchPackage takes a cacheBehavior property, which is one of these:
//    'yes': try to retrieve the object from the cache
//    'no': do not try to retrieve the object from the cache (but
//          still store it in the cache after retrieving it).
//    'ignore': do not try to retrieve the object from the cache,
//          nor try to store it in the cache.
//    'ims': retrieve the object from the cache, but use the
//          last-modified date on the result in order to do an
//          if-modified-since query.
// This variable controls the cache behavior that is used if the
// user does not pass in a value for cacheBehavior for fetchPackage().
let defaultCacheBehavior;

// How long we wait on a single http request before giving up.  Note
// that due to retries, a single fetchPackage() call can take 3 times
// as long as this.
let defaultTimeoutInMs;

// How many times we retry on 5xx error or similar, before giving up.
let numRetries;

const resetGlobals = function() {
    serverHostname = 'https://www.khanacademy.org';
    defaultCacheBehavior = 'yes';
    defaultTimeoutInMs = 1000;
    numRetries = 2;     // so 3 tries total
};


/**
 * Given an absolute path, e.g. /javascript/foo-package.js, return a
 * promise holding the package contents.
 */
const fetchPackage = function(path, cacheBehavior, triesLeftAfterThisOne) {
    if (cacheBehavior == null) {
        cacheBehavior = defaultCacheBehavior;
    }
    if (triesLeftAfterThisOne == null) {
        triesLeftAfterThisOne = numRetries;
    }

    let cachedValue;

    const url = serverHostname + path;
    if (cacheBehavior === 'ims') {
        cachedValue = cache.get(url);
        // We'll save this for making the if-modified-since query later.
    } else if (cacheBehavior === 'yes') {
        cachedValue = cache.get(url);
        if (cachedValue != null) {
            return Promise.resolve(cachedValue.text);<commit_msg>Oops, was forgetting to initialize the globals...<commit_after>'use strict';

/**
 * Fetch a package from the place that has packages.
 *
 * The server-side renderer takes, as input, a list of js packages, in
 * addition to the name of the react component to render.  This module
 * provides the function for taking the url-path of a package and
 * actually retrieving the package.
 *
 * The server that holds the package information is hard-coded in this
 * file, to avoid letting this server execute arbitrary code.
 */

const request = require('superagent');

const cache = require("./cache");

// All files requested via /render are fetched via this hostname.
let serverHostname;

// fetchPackage takes a cacheBehavior property, which is one of these:
//    'yes': try to retrieve the object from the cache
//    'no': do not try to retrieve the object from the cache (but
//          still store it in the cache after retrieving it).
//    'ignore': do not try to retrieve the object from the cache,
//          nor try to store it in the cache.
//    'ims': retrieve the object from the cache, but use the
//          last-modified date on the result in order to do an
//          if-modified-since query.
// This variable controls the cache behavior that is used if the
// user does not pass in a value for cacheBehavior for fetchPackage().
let defaultCacheBehavior;

// How long we wait on a single http request before giving up.  Note
// that due to retries, a single fetchPackage() call can take 3 times
// as long as this.
let defaultTimeoutInMs;

// How many times we retry on 5xx error or similar, before giving up.
let numRetries;

const resetGlobals = function() {
    serverHostname = 'https://www.khanacademy.org';
    defaultCacheBehavior = 'yes';
    defaultTimeoutInMs = 1000;
    numRetries = 2;     // so 3 tries total
};

resetGlobals();


/**
 * Given an absolute path, e.g. /javascript/foo-package.js, return a
 * promise holding the package contents.
 */
const fetchPackage = function(path, cacheBehavior, triesLeftAfterThisOne) {
    if (cacheBehavior == null) {
        cacheBehavior = defaultCacheBehavior;
    }
    if (triesLeftAfterThisOne == null) {
        triesLeftAfterThisOne = numRetries;
    }

    let cachedValue;

    const url = serverHostname + path;
    if (cacheBehavior === 'ims') {
        cachedValue = cache.get(url);
        // We'll save this for making the if-modified-since query later.
    } else if (cacheBehavior === 'yes') {
        cachedValue = cache.get(url);
        if (cachedValue != null) {
            return Promise.resolve(cachedValue.text);
{'fraction_non_alphanumeric': 0.08925459825750243, 'fraction_numerical': 0.003097773475314618, 'mean_word_length': 2.992272024729521, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3473560', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1371, 'n_words': 632}