<commit_before>/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 * @providesModule FormData
 * @flow
 */
'use strict';

type FormDataValue = any;
type FormDataNameValuePair = [string, FormDataValue];

type Headers = {[name: string]: string};
type FormDataPart = {
  string: string;
  headers: Headers;
} | {
  uri: string;
  headers: Headers;
  name?: string;
  type?: string;
};

/**
 * Polyfill for XMLHttpRequest2 FormData API, allowing multipart POST requests
 * with mixed data (string, native files) to be submitted via XMLHttpRequest.
 *
 * Example:
 *
 *   var photo = {
 *     uri: uriFromCameraRoll,
 *     type: 'image/jpeg',
 *     name: 'photo.jpg',
 *   };
 *
 *   var body = new FormData();
 *   body.append('authToken', '<PASSWORD>');
 *   body.append('photo', photo);
 *   body.append('title', 'A beautiful photo!');
 *
 *   xhr.open('POST', serverURL);
 *   xhr.send(body);
 */
class FormData {
  _parts: Array<FormDataNameValuePair>;
  _partsByKey: {[key: string]: FormDataNameValuePair};

  constructor() {
    this._parts = [];
    this._partsByKey = {};
  }

  append(key: string, value: FormDataValue) {
    var parts = this._partsByKey[key];
    if (parts) {
      // It's a bit unclear what the behaviour should be in this case.
      // The XMLHttpRequest spec doesn't specify it, while MDN says that
      // the any new values should appended to existing values. We're not
      // doing that for now -- it's tedious and doesn't seem worth the effort.
      parts[1] = value;
      return;
    }
    parts = [key, value];
    this._parts.push(parts);
    this._partsByKey[key] = parts;
  }

  getParts(): Array<FormDataPart> {
    return this._parts.map(([name, value]) => {
      var contentDisposition = 'form-data; name="' + name + '"';
      var headers: Headers = {'content-disposition': contentDisposition};
      if (typeof value === 'string') {
        return {string: value, headers};
      }

      // The body part is a "blob", which in React Native just means
      // an object with a `uri` attribute. Optionally, it can also
      // have a `name` and `type` attribute to specify filename and
      // content type (cf. web Blob interface.)
      if (typeof value.name === 'string') {
        headers['content-disposition'] += '; filename="' + value.name + '"';
      }
      if (typeof value.type === 'string') {
        headers['content-type'] = value.type;
      }
      return {...value, headers};
    });
  }
}

module.exports = FormData;<commit_msg>[ReactNative] Use FbHttp for handling http requests from React Native.<commit_after>/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 * @providesModule FormData
 * @flow
 */
'use strict';

type FormDataValue = any;
type FormDataNameValuePair = [string, FormDataValue];

type Headers = {[name: string]: string};
type FormDataPart = {
  string: string;
  headers: Headers;
} | {
  uri: string;
  headers: Headers;
  name?: string;
  type?: string;
};

/**
 * Polyfill for XMLHttpRequest2 FormData API, allowing multipart POST requests
 * with mixed data (string, native files) to be submitted via XMLHttpRequest.
 *
 * Example:
 *
 *   var photo = {
 *     uri: uriFromCameraRoll,
 *     type: 'image/jpeg',
 *     name: 'photo.jpg',
 *   };
 *
 *   var body = new FormData();
 *   body.append('authToken', '<PASSWORD>');
 *   body.append('photo', photo);
 *   body.append('title', 'A beautiful photo!');
 *
 *   xhr.open('POST', serverURL);
 *   xhr.send(body);
 */
class FormData {
  _parts: Array<FormDataNameValuePair>;
  _partsByKey: {[key: string]: FormDataNameValuePair};

  constructor() {
    this._parts = [];
    this._partsByKey = {};
  }

  append(key: string, value: FormDataValue) {
    var parts = this._partsByKey[key];
    if (parts) {
      // It's a bit unclear what the behaviour should be in this case.
      // The XMLHttpRequest spec doesn't specify it, while MDN says that
      // the any new values should appended to existing values. We're not
      // doing that for now -- it's tedious and doesn't seem worth the effort.
      parts[1] = value;
      return;
    }
    parts = [key, value];
    this._parts.push(parts);
    this._partsByKey[key] = parts;
  }

  getParts(): Array<FormDataPart> {
    return this._parts.map(([name, value]) => {
      var contentDisposition = 'form-data; name="' + name + '"';
      var headers: Headers = {'content-disposition': contentDisposition};
      if (typeof value === 'string') {
        return {string: value, headers, fieldName: name};
      }

      // The body part is a "blob", which in React Native just means
      // an object with a `uri` attribute. Optionally, it can also
      // have a `name` and `type` attribute to specify filename and
      // content type (cf. web Blob interface.)
      if (typeof value.name === 'string') {
        headers['content-disposition'] += '; filename="' + value.name + '"';
      }
      if (typeof value.type === 'string') {
        headers['content-type'] = value.type;
      }
      return {...value, headers, fieldName: name};
    });
  }
}

module.exports = FormData;
{'fraction_non_alphanumeric': 0.1354631447438872, 'fraction_numerical': 0.0021417097983223274, 'mean_word_length': 2.949260042283298, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3685857', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1669, 'n_words': 564}