text:
---
title: "Hints"
weight: 2
type: docs
aliases:
  - /dev/table/sql/hints.html
---

# SQL Hints

{{}} {{}}

SQL hints can be used with SQL statements to alter execution plans. This chapter explains how to use hints to force various approaches.

Generally a hint can be used to:

- Enforce planner: there's no perfect planner, so it makes sense to implement hints to
allow user better control the execution;
- Append meta data(or statistics): some statistics like "table index for scan" and
"skew info of some shuffle keys" are somewhat dynamic for the query, it would be very
convenient to config them with hints because our planning metadata from the planner is very
often not that accurate;
- Operator resource constraints: for many cases, we would give a default resource
configuration for the execution operators, i.e. min parallelism or
managed memory (resource consuming UDF) or special resource requirement (GPU or SSD disk)
and so on, it would be very flexible to profile the resource with hints per query(instead of the Job).

## Dynamic Table Options
Dynamic table options allows to specify or override table options dynamically, different with static table options defined with SQL DDL or connect API,
these options can be specified flexibly in per-table scope within each query.

Thus it is very suitable to use for the ad-hoc queries in interactive terminal, for example, in the SQL-CLI,
you can specify to ignore the parse error for a CSV source just by adding a dynamic option `/*+ OPTIONS('csv.ignore-parse-errors'='true') */`.

Note: Dynamic table options default is forbidden to use because it may change the semantics of the query.
You need to set the config option `table.dynamic-table-options.enabled` to be `true` explicitly (default is false),
See the <a href="{{< ref "docs/dev/table/config" >}}">Configuration for details on how to set up the config options.

### Syntax
In order to not break the SQL compatibility, we use the Oracle style SQL hint syntax:
```sql
table_path /*+ OPTIONS(key=val [, key=val]*) */

key:
    stringLiteral
val:
    stringLiteral

```

### Examples

```sql

CREATE TABLE kafka_table1 (id BIGINT, name STRING, age INT) WITH (...);
CREATE TABLE kafka_table2 (id BIGINT, name STRING, age INT) WITH (...);

-- override table options in query source
select id, name from kafka_table1 /*+ OPTIONS('scan.startup.mode'='earliest-offset') */;

-- override table options in join
select * from
    kafka_table1 /*+ OPTIONS('scan.startup.mode'='earliest-offset') */ t1
    join
    kafka_table2 /*+ OPTIONS('scan.startup.mode'='earliest-offset') */ t2
    on t1.id = t2.id;

-- override table options for INSERT target table
insert into kafka_table1 /*+ OPTIONS('sink.partitioner'='round-robin') */ select * from kafka_table2;

```

{{< top >}}


text:
Using WYMeditor
===============

Introduction
------------

These guidelines will teach you why to use certain techniques when creating
content and how to work with them in WYMeditor. Following these will keep the
structure and meaning of your content correct - making it more flexible to
technologies like SEO1 (Search Engine Optimization), screen readers2, mobile
phone technologies, printers etc. Following these guidelines will also make
your content accessible to a wider range of people, with or without
disabilities

Guidelines
----------

**1. Use elements as intended**

    HTML is a so called markup language. It's purpose is to give the content
    meaning and structure, not visual effects. Therefore, it's important to use
    elements as intended to not break meaning, structure and compatibility to other
    technologies. If you want to style your content you've created with WYMeditor,
    you shall `learn to use CSS
    <http://www.456bereastreet.com/lab/developing_with_web_standards/full/#css>`_
    (Cascading Style Sheets).

    *1.1. Headings*

        Using a logical and proper heading structure is among one of the most
        important SEO techniques. Always use a correct hierarchy. Start with
        Heading 1, followed by Heading 2, Heading 3... etc. A proper heading
        structure will also help users to find what they are looking for, faster.

        To create a heading you simply place your marker where you want the heading
        to go and then select "Containers > Heading 1" for example.

    *1.2. Tables*

        Creating lean and accessible tables is a craft but WYMeditor makes it easy.
        Only use the Table element to arrange different kinds of data - time
        tables, statistics, member listings etc. Use the Table Header container to
        mark up headers properly. Doing this wrong will confuse users with screen
        readers when accessing tables, as headers aren't part of the data.

        To create a Table Header you simply place your marker in a table cell and
        select "Containers > Table Header".

        It's also good practice to give users the ability to do a quick overlook of
        the table's content by filling in a short description in the Caption field.
        You can do that in the dialog appearing when creating the table.

    *1.3. Lists*

        It's important to mark up lists of items the right way, using Ordered list
        or Unordered list. Doing this wrong makes it harder for users to overlook
        the content and will also confuse users with screen readers. Creating a
        list using Shift+Enter to separate items is therefore highly depreciated as
        both structure and meaning will break in the document.

        To create a list you simply use one of the two list buttons in WYMeditor's
        toolbar. Hit enter twice to "jump out of" a list.

    *1.4. Indent and outdent*

        The Indent and Outdent icons are only intended to create nested lists of
        list items (as described in 1.3). Place the marker on a list item and hit
        the Indent icon to make it go sub level. To undo this move or to make it go
        back one level, simply hit the Outdent icon.

    *1.5. Blockquotes*

        Mark up your quoted text by using the Blockquote container. Doing this the
        right way will help search engines and users with screen readers to
        understand your text and give it relevant meaning.

**2. Provide alternatives**

    *2.1. Alternative text*

        An alternative text string shall be accurate and equivalent in presenting
        the same content and function as the image. This also helps to identify the
        relevance and meaning of your image which improves SEO and accessibility
        for user with screen readers.

        You can enter the Alternative text in the image dialog, appearing when
        adding or editing an image. The only exception to leave the field empty is
        when your image is for decoration purpose only.

    *2.2. Title attribute*

        To provide additional information of a link or and image you can provide a
        Title text. This helps several other technologies to identify the relevance
        and meaning of your elements which improves SEO and accessibility for user
        with screen readers.

        You can enter the Title text in the image/link dialog, appearing when
        adding or editing an element.

**3. Write understandable**

Writing understandable is among one of the most important checkpoints in
creating accessible content. Unfortunately computers can't do anything about
this, yet. But with WYMeditor you have got the tools to make it down the right
way.

    *3.1. Descriptive headings*

        A heading element shall briefly describe the topic of the section it
        introduces making it easier to overlook both for all users. This will also
        improve the SEO aspects of your content.

    *3.2. Descriptive link texts*

        A link text shall be completely descriptive of its location, which makes it
        a lot easier to overlook and skim-read the content for all users. This will
        also improve the SEO aspects and the "link indexing" procedure for users
        with screen readers. "Click here" links are therefor highly depreciated.

Word References
---------------

**SEO (Search Engine Optimization)**
    "Search engine optimization" is a conception for everything that makes your
    content rank higher when using search engines like `Google <www.google.com>`_.

**Screen reader**
    A screen reader is a text-only web browser that dictates webpages' content
    to visually disabled peoples. In this text "screen readers" refers to all
    sorts of assistive and adoptive web technologies.

redpajama_set_name: RedPajamaGithub


text:
This folder contains tests which assert that most of the code is tree shaken away.
This is asserted by keeping gold files of all symbols which are expected to be retained.
When doing renaming it is often necessary to update the gold files, to do so use these commands:

```
yarn bazel run //packages/core/test/bundling/injection:symbol_test.accept
yarn bazel run //packages/core/test/bundling/cyclic_import:symbol_test.accept
yarn bazel run //packages/core/test/bundling/forms:symbol_test.accept
yarn bazel run //packages/core/test/bundling/hello_world:symbol_test.accept
yarn bazel run //packages/core/test/bundling/router:symbol_test.accept
yarn bazel run //packages/core/test/bundling/todo:symbol_test.accept
```

## Running all symbol tests
To run all symbol tests with one command, you can use the following scripts:

```
yarn run symbol-extractor:check
yarn run symbol-extractor:update
```


text:
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace AzureEventHubClientTool.Properties {

    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.4.0.0")]
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

        public static Settings Default {
            get {
                return defaultInstance;
            }
        }

        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("")]
        public string output {
            get {
                return ((string)(this["output"]));
            }
            set {
                this["output"] = value;
            }
        }

        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("")]
        public string input {
            get {
                return ((string)(this["input"]));
            }
            set {
                this["input"] = value;
            }
        }

        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("")]
        public string senderConnectionString {
            get {
                return ((string)(this["senderConnectionString"]));
            }
            set {
                this["senderConnectionString"] = value;
            }
        }

        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("")]
        public string receiverEvHubConnectionString {
            get {
                return ((string)(this["receiverEvHubConnectionString"]));
            }
            set {
                this["receiverEvHubConnectionString"] = value;
            }
        }

        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("")]
        public string receiverEvHubName {
            get {
                return ((string)(this["receiverEvHubName"]));
            }
            set {
                this["receiverEvHubName"] = value;
            }
        }

        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("")]
        public string receiverStorageConnectionString {
            get {
                return ((string)(this["receiverStorageConnectionString"]));
            }
            set {
                this["receiverStorageConnectionString"] = value;
            }
        }

        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("")]
        public string receiverStorageName {
            get {
                return ((string)(this["receiverStorageName"]));
            }
            set {
                this["receiverStorageName"] = value;
            }
        }

        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("")]
        public string consumerGroup
        {
            get
            {
                return ((string)(this["consumerGroup"]));
            }
            set
            {
                this["consumerGroup"] = value;
            }
        }
    }
}


text:
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.superagent = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/**
 * Check if `fn` is a function.
 *
 * @param {Function} fn
 * @return {Boolean}
 * @api private
 */
var isObject = require('./is-object');

function isFunction(fn) {
  var tag = isObject(fn) ? Object.prototype.toString.call(fn) : '';
  return tag === '[object Function]';
}

module.exports = isFunction;

},{"./is-object":2}],2:[function(require,module,exports){
/**
 * Check if `obj` is an object.
 *
 * @param {Object} obj
 * @return {Boolean}
 * @api private
 */

function isObject(obj) {
  return null !== obj && 'object' === typeof obj;
}

module.exports = isObject;

},{}],3:[function(require,module,exports){
/**
 * Module of mixed-in functions shared between node and client code
 */
var isObject = require('./is-object');

/**
 * Expose `RequestBase`.
 */

module.exports = RequestBase;

/**
 * Initialize a new `RequestBase`.
 *
 * @api public
 */

function RequestBase(obj) {
  if (obj) return mixin(obj);
}

/**
 * Mixin the prototype properties.
 *
 * @param {Object} obj
 * @return {Object}
 * @api private
 */

function mixin(obj) {
  for (var key in RequestBase.prototype) {
    obj[key] = RequestBase.prototype[key];
  }
  return obj;
}

/**
 * Clear previous timeout.
 *
 * @return {Request} for chaining
 * @api public
 */

RequestBase.prototype.clearTimeout = function _clearTimeout(){
  clearTimeout(this._timer);
  clearTimeout(this._responseTimeoutTimer);
  delete this._timer;
  delete this._responseTimeoutTimer;
  return this;
};

/**
 * Override default response body parser
 *
 * This function will be called to convert incoming data into request.body
 *
 * @param {Function}
 * @api public
 */

RequestBase.prototype.parse = function parse(fn){
  this._parser = fn;
  return this;
};

/**
 * Set format of binary response body.
 * In browser valid formats are 'blob' and 'arraybuffer',
 * which return Blob and ArrayBuffer, respectively.
 *
 * In Node all values result in Buffer.
 *
 * Examples:
 *
 *      req.get('/')
 *        .responseType('blob')
 *        .end(callback);
 *
 * @param {String} val
 * @return {Request} for chaining
 * @api public
 */

RequestBase.prototype.responseType = function(val){
  this._responseType = val;
  return this;
};

/**
 * Override default request body serializer
 *
 * This function will be called to convert data set via .send or .attach into payload to send
 *
 * @param {Function}
 * @api public
 */

RequestBase.prototype.serialize = function serialize(fn){
  this._serializer = fn;
  return this;
};

/**
 * Set timeouts.
 *
 * - response timeout is time between sending request and receiving the first byte of the response. Includes DNS and connection time.
 * - deadline is the time from start of the request to receiving response body in full. If the deadline is too short large files may not load at all on slow connections.
 *
 * Value of 0 or false means no timeout.
 *
 * @param {Number|Object} ms or {response, read, deadline}
 * @return {Request} for chaining
 * @api public
 */

RequestBase.prototype.timeout = function timeout(options){
  if (!options || 'object' !== typeof options) {
    this._timeout = options;
    this._responseTimeout = 0;
    return this;
  }

  for(var option in options) {
    switch(option) {
      case 'deadline':
        this._timeout = options.deadline;
        break;
      case 'response':
        this._responseTimeout = options.response;
        break;
      default:
        console.warn("Unknown timeout option", option);
    }
  }
  return this;
};

/**
 * Set number of retry attempts on error.
 *
 * Failed requests will be retried 'count' times if timeout or err.code >= 500.
 *
 * @param {Number} count
 * @return {Request} for chaining
 * @api public
 */

RequestBase.prototype.retry = function retry(count){
  // Default to 1 if no count passed or true
  if (arguments.length === 0 || count === true) count = 1;
  if (count <= 0) count = 0;
  this._maxRetries = count;
  this._retries = 0;
  return this;
};

/**
 * Retry request
 *
 * @return {Request} for chaining
 * @api private
 */

RequestBase.prototype._retry = function() {
  this.clearTimeout();

  // node
  if (this.req) {
    this.req = null;
    this.req = this.request();
  }

  this._aborted = false;
  this.timedout = false;

  return this._end();
};

/**
 * Promise support
 *
 * @param {Function} resolve
 * @param {Function} [reject]
 * @return {Request}
 */

RequestBase.prototype.then = function then(resolve, reject) {
  if (!this._fullfilledPromise) {
    var self = this;
    if (this._endCalled) {
      console.warn("Warning: superagent request was sent twice, because both .end() and .then() were called. Never call .end() if you use promises");
    }
    this._fullfilledPromise = new Promise(function(innerResolve, innerReject){
      self.end(function(err, res){
        if (err) innerReject(err); else innerResolve(res);
      });
    });
  }
  return this._fullfilledPromise.then(resolve, reject);
}

RequestBase.prototype.catch = function(cb) {
  return this.then(undefined, cb);
};

/**
 * Allow for extension
 */

RequestBase.prototype.use = function use(fn) {
  fn(this);
  return this;
}

RequestBase.prototype.ok = function(cb) {
  if ('function' !== typeof cb) throw Error("Callback required");
  this._okCallback = cb;
  return this;
};

RequestBase.prototype._isResponseOK = function(res) {
  if (!res) {
    return false;
  }

  if (this._okCallback) {
    return this._okCallback(res);
  }

  return res.status >= 200 && res.status < 300;
};

/**
 * Get request header `field`.
 * Case-insensitive.
 *
 * @param {String} field
 * @return {String}
 * @api public
 */

RequestBase.prototype.get = function(field){
  return this._header[field.toLowerCase()];
};

/**
 * Get case-insensitive header `field` value.
 * This is a deprecated internal API. Use `.get(field)` instead.
 *
 * (getHeader is no longer used internally by the superagent code base)
 *
 * @param {String} field
 * @return {String}
 * @api private
 * @deprecated
 */

RequestBase.prototype.getHeader = RequestBase.prototype.get;

/**
 * Set header `field` to `val`, or multiple fields with one object.
 * Case-insensitive.
 *
 * Examples:
 *
 *      req.get('/')
 *        .set('Accept', 'application/json')
 *        .set('X-API-Key', 'foobar')
 *        .end(callback);
 *
 *      req.get('/')
 *        .set({ Accept: 'application/json', 'X-API-Key': 'foobar' })
 *        .end(callback);
 *
 * @param {String|Object} field
 * @param {String} val
 * @return {Request} for chaining
 * @api public
 */

RequestBase.prototype.set = function(field, val){
  if (isObject(field)) {
    for (var key in field) {
      this.set(key, field[key]);
    }
    return this;
  }
  this._header[field.toLowerCase()] = val;
  this.header[field] = val;
  return this;
};

/**
 * Remove header `field`.
 * Case-insensitive.
 *
 * Example:
 *
 *      req.get('/')
 *        .unset('User-Agent')
 *        .end(callback);
 *
 * @param {String} field
 */
RequestBase.prototype.unset = function(field){
  delete this._header[field.toLowerCase()];
  delete this.header[field];
  return this;
};

/**
 * Write the field `name` and `val`, or multiple fields with one object
 * for "multipart/form-data" request bodies.
 *
 * ``` js
 * request.post('/upload')
 *   .field('foo', 'bar')
 *   .end(callback);
 *
 * request.post('/upload')
 *   .field({ foo: 'bar', baz: 'qux' })
 *   .end(callback);
 * ```
 *
 * @param {String|Object} name
 * @param {String|Blob|File|Buffer|fs.ReadStream} val
 * @return {Request} for chaining
 * @api public
 */
RequestBase.prototype.field = function(name, val) {

  // name should be either a string or an object.
  if (null === name ||  undefined === name) {
    throw new Error('.field(name, val) name can not be empty');
  }

  if (this._data) {
    console.error(".field() can't be used if .send() is used. Please use only .send() or only .field() & .attach()");
  }

  if (isObject(name)) {
    for (var key in name) {
      this.field(key, name[key]);
    }
    return this;
  }

  if (Array.isArray(val)) {
    for (var i in val) {
      this.field(name, val[i]);
    }
    return this;
  }

  // val should be defined now
  if (null === val || undefined === val) {
    throw new Error('.field(name, val) val can not be empty');
  }
  if ('boolean' === typeof val) {
    val = '' + val;
  }
  this._getFormData().append(name, val);
  return this;
};

/**
 * Abort the request, and clear potential timeout.
 *
 * @return {Request}
 * @api public
 */
RequestBase.prototype.abort = function(){
  if (this._aborted) {
    return this;
  }
  this._aborted = true;
  this.xhr && this.xhr.abort(); // browser
  this.req && this.req.abort(); // node
  this.clearTimeout();
  this.emit('abort');
  return this;
};

/**
 * Enable transmission of cookies with x-domain requests.
 *
 * Note that for this to work the origin must not be
 * using "Access-Control-Allow-Origin" with a wildcard,
 * and also must set "Access-Control-Allow-Credentials"
 * to "true".
 *
 * @api public
 */

RequestBase.prototype.withCredentials = function(on){
  // This is browser-only functionality. Node side is no-op.
  if(on==undefined) on = true;
  this._withCredentials = on;
  return this;
};

/**
 * Set the max redirects to `n`. Does noting in browser XHR implementation.
 *
 * @param {Number} n
 * @return {Request} for chaining
 * @api public
 */

RequestBase.prototype.redirects = function(n){
  this._maxRedirects = n;
  return this;
};

/**
 * Convert to a plain javascript object (not JSON string) of scalar properties.
 * Note as this method is designed to return a useful non-this value,
 * it cannot be chained.
 *
 * @return {Object} describing method, url, and data of this request
 * @api public
 */

RequestBase.prototype.toJSON = function(){
  return {
    method: this.method,
    url: this.url,
    data: this._data,
    headers: this._header
  };
};

/**
 * Send `data` as the request body, defaulting the `.type()` to "json" when
 * an object is given.
 *
 * Examples:
 *
 *       // manual json
 *       request.post('/user')
 *         .type('json')
 *         .send('{"name":"tj"}')
 *         .end(callback)
 *
 *       // auto json
 *       request.post('/user')
 *         .send({ name: 'tj' })
 *         .end(callback)
 *
 *       // manual x-www-form-urlencoded
 *       request.post('/user')
 *         .type('form')
 *         .send('name=tj')
 *         .end(callback)
 *
 *       // auto x-www-form-urlencoded
 *       request.post('/user')
 *         .type('form')
 *         .send({ name: 'tj' })
 *         .end(callback)
 *
 *       // defaults to x-www-form-urlencoded
 *      request.post('/user')
 *        .send('name=tobi')
 *        .send('species=ferret')
 *        .end(callback)
 *
 * @param {String|Object} data
 * @return {Request} for chaining
 * @api public
 */

RequestBase.prototype.send = function(data){
  var isObj = isObject(data);
  var type = this._header['content-type'];

  if (this._formData) {
    console.error(".send() can't be used if .attach() or .field() is used. Please use only .send() or only .field() & .attach()");
  }

  if (isObj && !this._data) {
    if (Array.isArray(data)) {
      this._data = [];
    } else if (!this._isHost(data)) {
      this._data = {};
    }
  } else if (data && this._data && this._isHost(this._data)) {
    throw Error("Can't merge these send calls");
  }

  // merge
  if (isObj && isObject(this._data)) {
    for (var key in data) {
      this._data[key] = data[key];
    }
  } else if ('string' == typeof data) {
    // default to x-www-form-urlencoded
    if (!type) this.type('form');
    type = this._header['content-type'];
    if ('application/x-www-form-urlencoded' == type) {
      this._data = this._data
        ? this._data + '&' + data
        : data;
    } else {
      this._data = (this._data || '') + data;
    }
  } else {
    this._data = data;
  }

  if (!isObj || this._isHost(data)) {
    return this;
  }

  // default to json
  if (!type) this.type('json');
  return this;
};

/**
 * Sort `querystring` by the sort function
 *
 *
 * Examples:
 *
 *       // default order
 *       request.get('/user')
 *         .query('name=Nick')
 *         .query('search=Manny')
 *         .sortQuery()
 *         .end(callback)
 *
 *       // customized sort function
 *       request.get('/user')
 *         .query('name=Nick')
 *         .query('search=Manny')
 *         .sortQuery(function(a, b){
 *           return a.length - b.length;
 *         })
 *         .end(callback)
 *
 *
 * @param {Function} sort
 * @return {Request} for chaining
 * @api public
 */

RequestBase.prototype.sortQuery = function(sort) {
  // _sort default to true but otherwise can be a function or boolean
  this._sort = typeof sort === 'undefined' ? true : sort;
  return this;
};

/**
 * Invoke callback with timeout error.
 *
 * @api private
 */

RequestBase.prototype._timeoutError = function(reason, timeout, errno){
  if (this._aborted) {
    return;
  }
  var err = new Error(reason + timeout + 'ms exceeded');
  err.timeout = timeout;
  err.code = 'ECONNABORTED';
  err.errno = errno;
  this.timedout = true;
  this.abort();
  this.callback(err);
};

RequestBase.prototype._setTimeouts = function() {
  var self = this;

  // deadline
  if (this._timeout && !this._timer) {
    this._timer = setTimeout(function(){
      self._timeoutError('Timeout of ', self._timeout, 'ETIME');
    }, this._timeout);
  }
  // response timeout
  if (this._responseTimeout && !this._responseTimeoutTimer) {
    this._responseTimeoutTimer = setTimeout(function(){
      self._timeoutError('Response timeout of ', self._responseTimeout, 'ETIMEDOUT');
    }, this._responseTimeout);
  }
}

},{"./is-object":2}],4:[function(require,module,exports){

/**
 * Module dependencies.
 */

var utils = require('./utils');

/**
 * Expose `ResponseBase`.
 */

module.exports = ResponseBase;

/**
 * Initialize a new `ResponseBase`.
 *
 * @api public
 */

function ResponseBase(obj) {
  if (obj) return mixin(obj);
}

/**
 * Mixin the prototype properties.
 *
 * @param {Object} obj
 * @return {Object}
 * @api private
 */

function mixin(obj) {
  for (var key in ResponseBase.prototype) {
    obj[key] = ResponseBase.prototype[key];
  }
  return obj;
}

/**
 * Get case-insensitive `field` value.
 *
 * @param {String} field
 * @return {String}
 * @api public
 */

ResponseBase.prototype.get = function(field){
    return this.header[field.toLowerCase()];
};

/**
 * Set header related properties:
 *
 *   - `.type` the content type without params
 *
 * A response of "Content-Type: text/plain; charset=utf-8"
 * will provide you with a `.type` of "text/plain".
 *
 * @param {Object} header
 * @api private
 */

ResponseBase.prototype._setHeaderProperties = function(header){
    // TODO: moar!
    // TODO: make this a util

    // content-type
    var ct = header['content-type'] || '';
    this.type = utils.type(ct);

    // params
    var params = utils.params(ct);
    for (var key in params) this[key] = params[key];

    this.links = {};

    // links
    try {
        if (header.link) {
            this.links = utils.parseLinks(header.link);
        }
    } catch (err) {
        // ignore
    }
};

/**
 * Set flags such as `.ok` based on `status`.
 *
 * For example a 2xx response will give you a `.ok` of __true__
 * whereas 5xx will be __false__ and `.error` will be __true__. The
 * `.clientError` and `.serverError` are also available to be more
 * specific, and `.statusType` is the class of error ranging from 1..5
 * sometimes useful for mapping respond colors etc.
 *
 * "sugar" properties are also defined for common cases. Currently providing:
 *
 *   - .noContent
 *   - .badRequest
 *   - .unauthorized
 *   - .notAcceptable
 *   - .notFound
 *
 * @param {Number} status
 * @api private
 */

ResponseBase.prototype._setStatusProperties = function(status){
    var type = status / 100 | 0;

    // status / class
    this.status = this.statusCode = status;
    this.statusType = type;

    // basics
    this.info = 1 == type;
    this.ok = 2 == type;
    this.redirect = 3 == type;
    this.clientError = 4 == type;
    this.serverError = 5 == type;
    this.error = (4 == type || 5 == type)
        ? this.toError()
        : false;

    // sugar
    this.accepted = 202 == status;
    this.noContent = 204 == status;
    this.badRequest = 400 == status;
    this.unauthorized = 401 == status;
    this.notAcceptable = 406 == status;
    this.forbidden = 403 == status;
    this.notFound = 404 == status;
};

},{"./utils":6}],5:[function(require,module,exports){
var ERROR_CODES = [
  'ECONNRESET',
  'ETIMEDOUT',
  'EADDRINFO',
  'ESOCKETTIMEDOUT'
];

/**
 * Determine if a request should be retried.
 * (Borrowed from segmentio/superagent-retry)
 *
 * @param {Error} err
 * @param {Response} [res]
 * @returns {Boolean}
 */
module.exports = function shouldRetry(err, res) {
  if (err && err.code && ~ERROR_CODES.indexOf(err.code)) return true;
  if (res && res.status && res.status >= 500) return true;
  // Superagent timeout
  if (err && 'timeout' in err && err.code == 'ECONNABORTED') return true;
  return false;
};
},{}],6:[function(require,module,exports){

/**
 * Return the mime type for the given `str`.
 *
 * @param {String} str
 * @return {String}
 * @api private
 */

exports.type = function(str){
  return str.split(/ *; */).shift();
};

/**
 * Return header field parameters.
 *
 * @param {String} str
 * @return {Object}
 * @api private
 */

exports.params = function(str){
  return str.split(/ *; */).reduce(function(obj, str){
    var parts = str.split(/ *= */);
    var key = parts.shift();
    var val = parts.shift();

    if (key && val) obj[key] = val;
    return obj;
  }, {});
};

/**
 * Parse Link header fields.
 *
 * @param {String} str
 * @return {Object}
 * @api private
 */

exports.parseLinks = function(str){
  return str.split(/ *, */).reduce(function(obj, str){
    var parts = str.split(/ *; */);
    var url = parts[0].slice(1, -1);
    var rel = parts[1].split(/ *= */)[1].slice(1, -1);
    obj[rel] = url;
    return obj;
  }, {});
};

/**
 * Strip content related fields from `header`.
 *
 * @param {Object} header
 * @return {Object} header
 * @api private
 */

exports.cleanHeader = function(header, shouldStripCookie){
  delete header['content-type'];
  delete header['content-length'];
  delete header['transfer-encoding'];
  delete header['host'];
  if (shouldStripCookie) {
    delete header['cookie'];
  }
  return header;
};
},{}],7:[function(require,module,exports){

/**
 * Expose `Emitter`.
 */

if (typeof module !== 'undefined') {
  module.exports = Emitter;
}

/**
 * Initialize a new `Emitter`.
 *
 * @api public
 */

function Emitter(obj) {
  if (obj) return mixin(obj);
};

/**
 * Mixin the emitter properties.
 *
 * @param {Object} obj
 * @return {Object}
 * @api private
 */

function mixin(obj) {
  for (var key in Emitter.prototype) {
    obj[key] = Emitter.prototype[key];
  }
  return obj;
}

/**
 * Listen on the given `event` with `fn`.
 *
 * @param {String} event
 * @param {Function} fn
 * @return {Emitter}
 * @api public
 */

Emitter.prototype.on =
Emitter.prototype.addEventListener = function(event, fn){
  this._callbacks = this._callbacks || {};
  (this._callbacks['$' + event] = this._callbacks['$' + event] || [])
    .push(fn);
  return this;
};

/**
 * Adds an `event` listener that will be invoked a single
 * time then automatically removed.
 *
 * @param {String} event
 * @param {Function} fn
 * @return {Emitter}
 * @api public
 */

Emitter.prototype.once = function(event, fn){
  function on() {
    this.off(event, on);
    fn.apply(this, arguments);
  }

  on.fn = fn;
  this.on(event, on);
  return this;
};

/**
 * Remove the given callback for `event` or all
 * registered callbacks.
 *
 * @param {String} event
 * @param {Function} fn
 * @return {Emitter}
 * @api public
 */

Emitter.prototype.off =
Emitter.prototype.removeListener =
Emitter.prototype.removeAllListeners =
Emitter.prototype.removeEventListener = function(event, fn){
  this._callbacks = this._callbacks || {};

  // all
  if (0 == arguments.length) {
    this._callbacks = {};
    return this;
  }

  // specific event
  var callbacks = this._callbacks['$' + event];
  if (!callbacks) return this;

  // remove all handlers
  if (1 == arguments.length) {
    delete this._callbacks['$' + event];
    return this;
  }

  // remove specific handler
  var cb;
  for (var i = 0; i < callbacks.length; i++) {
    cb = callbacks[i];
    if (cb === fn || cb.fn === fn) {
      callbacks.splice(i, 1);
      break;
    }
  }
  return this;
};

/**
 * Emit `event` with the given args.
 *
 * @param {String} event
 * @param {Mixed} ...
 * @return {Emitter}
 */

Emitter.prototype.emit = function(event){
  this._callbacks = this._callbacks || {};
  var args = [].slice.call(arguments, 1)
    , callbacks = this._callbacks['$' + event];

  if (callbacks) {
    callbacks = callbacks.slice(0);
    for (var i = 0, len = callbacks.length; i < len; ++i) {
      callbacks[i].apply(this, args);
    }
  }

  return this;
};

/**
 * Return array of callbacks for `event`.
 *
 * @param {String} event
 * @return {Array}
 * @api public
 */

Emitter.prototype.listeners = function(event){
  this._callbacks = this._callbacks || {};
  return this._callbacks['$' + event] || [];
};

/**
 * Check if this emitter has `event` handlers.
 *
 * @param {String} event
 * @return {Boolean}
 * @api public
 */

Emitter.prototype.hasListeners = function(event){
  return !! this.listeners(event).length;
};

},{}],8:[function(require,module,exports){
/**
 * Root reference for iframes.
 */

var root;
if (typeof window !== 'undefined') { // Browser window
  root = window;
} else if (typeof self !== 'undefined') { // Web Worker
  root = self;
} else { // Other environments
  console.warn("Using browser-only version of superagent in non-browser environment");
  root = this;
}

var Emitter = require('component-emitter');
var RequestBase = require('./request-base');
var isObject = require('./is-object');
var isFunction = require('./is-function');
var ResponseBase = require('./response-base');
var shouldRetry = require('./should-retry');

/**
 * Noop.
 */

function noop(){};

/**
 * Expose `request`.
 */

var request = exports = module.exports = function(method, url) {
  // callback
  if ('function' == typeof url) {
    return new exports.Request('GET', method).end(url);
  }

  // url first
  if (1 == arguments.length) {
    return new exports.Request('GET', method);
  }

  return new exports.Request(method, url);
}

exports.Request = Request;

/**
 * Determine XHR.
 */

request.getXHR = function () {
  if (root.XMLHttpRequest
      && (!root.location || 'file:' != root.location.protocol
          || !root.ActiveXObject)) {
    return new XMLHttpRequest;
  } else {
    try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch(e) {}
    try { return new ActiveXObject('Msxml2.XMLHTTP.6.0'); } catch(e) {}
    try { return new ActiveXObject('Msxml2.XMLHTTP.3.0'); } catch(e) {}
    try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch(e) {}
  }
  throw Error("Browser-only verison of superagent could not find XHR");
};

/**
 * Removes leading and trailing whitespace, added to support IE.
 *
 * @param {String} s
 * @return {String}
 * @api private
 */

var trim = ''.trim
  ? function(s) { return s.trim(); }
  : function(s) { return s.replace(/(^\s*|\s*$)/g, ''); };

/**
 * Serialize the given `obj`.
 *
 * @param {Object} obj
 * @return {String}
 * @api private
 */

function serialize(obj) {
  if (!isObject(obj)) return obj;
  var pairs = [];
  for (var key in obj) {
    pushEncodedKeyValuePair(pairs, key, obj[key]);
  }
  return pairs.join('&');
}

/**
 * Helps 'serialize' with serializing arrays.
 * Mutates the pairs array.
 *
 * @param {Array} pairs
 * @param {String} key
 * @param {Mixed} val
 */

function pushEncodedKeyValuePair(pairs, key, val) {
  if (val != null) {
    if (Array.isArray(val)) {
      val.forEach(function(v) {
        pushEncodedKeyValuePair(pairs, key, v);
      });
    } else if (isObject(val)) {
      for(var subkey in val) {
        pushEncodedKeyValuePair(pairs, key + '[' + subkey + ']', val[subkey]);
      }
    } else {
      pairs.push(encodeURIComponent(key)
        + '=' + encodeURIComponent(val));
    }
  } else if (val === null) {
    pairs.push(encodeURIComponent(key));
  }
}

/**
 * Expose serialization method.
 */

 request.serializeObject = serialize;

 /**
  * Parse the given x-www-form-urlencoded `str`.
  *
  * @param {String} str
  * @return {Object}
  * @api private
  */

function parseString(str) {
  var obj = {};
  var pairs = str.split('&');
  var pair;
  var pos;

  for (var i = 0, len = pairs.length; i < len; ++i) {
    pair = pairs[i];
    pos = pair.indexOf('=');
    if (pos == -1) {
      obj[decodeURIComponent(pair)] = '';
    } else {
      obj[decodeURIComponent(pair.slice(0, pos))] =
        decodeURIComponent(pair.slice(pos + 1));
    }
  }

  return obj;
}

/**
 * Expose parser.
 */

request.parseString = parseString;

/**
 * Default MIME type map.
 *
 *     superagent.types.xml = 'application/xml';
 *
 */

request.types = {
  html: 'text/html',
  json: 'application/json',
  xml: 'application/xml',
  urlencoded: 'application/x-www-form-urlencoded',
  'form': 'application/x-www-form-urlencoded',
  'form-data': 'application/x-www-form-urlencoded'
};

/**
 * Default serialization map.
 *
 *     superagent.serialize['application/xml'] = function(obj){
 *       return 'generated xml here';
 *     };
 *
 */

 request.serialize = {
   'application/x-www-form-urlencoded': serialize,
   'application/json': JSON.stringify
 };

 /**
  * Default parsers.
  *
  *     superagent.parse['application/xml'] = function(str){
  *       return { object parsed from str };
  *     };
  *
  */

request.parse = {
  'application/x-www-form-urlencoded': parseString,
  'application/json': JSON.parse
};

/**
 * Parse the given header `str` into
 * an object containing the mapped fields.
 *
 * @param {String} str
 * @return {Object}
 * @api private
 */

function parseHeader(str) {
  var lines = str.split(/\r?\n/);
  var fields = {};
  var index;
  var line;
  var field;
  var val;

  lines.pop(); // trailing CRLF

  for (var i = 0, len = lines.length; i < len; ++i) {
    line = lines[i];
    index = line.indexOf(':');
    field = line.slice(0, index).toLowerCase();
    val = trim(line.slice(index + 1));
    fields[field] = val;
  }

  return fields;
}

/**
 * Check if `mime` is json or has +json structured syntax suffix.
 *
 * @param {String} mime
 * @return {Boolean}
 * @api private
 */

function isJSON(mime) {
  return /[\/+]json\b/.test(mime);
}

/**
 * Initialize a new `Response` with the given `xhr`.
 *
 *  - set flags (.ok, .error, etc)
 *  - parse header
 *
 * Examples:
 *
 *  Aliasing `superagent` as `request` is nice:
 *
 *      request = superagent;
 *
 *  We can use the promise-like API, or pass callbacks:
 *
 *      request.get('/').end(function(res){});
 *      request.get('/', function(res){});
 *
 *  Sending data can be chained:
 *
 *      request
 *        .post('/user')
 *        .send({ name: 'tj' })
 *        .end(function(res){});
 *
 *  Or passed to `.send()`:
 *
 *      request
 *        .post('/user')
 *        .send({ name: 'tj' }, function(res){});
 *
 *  Or passed to `.post()`:
 *
 *      request
 *        .post('/user', { name: 'tj' })
 *        .end(function(res){});
 *
 * Or further reduced to a single call for simple cases:
 *
 *      request
 *        .post('/user', { name: 'tj' }, function(res){});
 *
 * @param {XMLHTTPRequest} xhr
 * @param {Object} options
 * @api private
 */

function Response(req) {
  this.req = req;
  this.xhr = this.req.xhr;
  // responseText is accessible only if responseType is '' or 'text' and on older browsers
  this.text = ((this.req.method !='HEAD' && (this.xhr.responseType === '' || this.xhr.responseType === 'text')) || typeof this.xhr.responseType === 'undefined')
     ? this.xhr.responseText
     : null;
  this.statusText = this.req.xhr.statusText;
  var status = this.xhr.status;
  // handle IE9 bug: http://stackoverflow.com/questions/10046972/msie-returns-status-code-of-1223-for-ajax-request
  if (status === 1223) {
      status = 204;
  }
  this._setStatusProperties(status);
  this.header = this.headers = parseHeader(this.xhr.getAllResponseHeaders());
  // getAllResponseHeaders sometimes falsely returns "" for CORS requests, but
  // getResponseHeader still works. so we get content-type even if getting
  // other headers fails.
  this.header['content-type'] = this.xhr.getResponseHeader('content-type');
  this._setHeaderProperties(this.header);

  if (null === this.text && req._responseType) {
    this.body = this.xhr.response;
  } else {
    this.body = this.req.method != 'HEAD'
      ? this._parseBody(this.text ? this.text : this.xhr.response)
      : null;
  }
}

ResponseBase(Response.prototype);

/**
 * Parse the given body `str`.
 *
 * Used for auto-parsing of bodies. Parsers
 * are defined on the `superagent.parse` object.
 *
 * @param {String} str
 * @return {Mixed}
 * @api private
 */

Response.prototype._parseBody = function(str){
  var parse = request.parse[this.type];
  if(this.req._parser) {
    return this.req._parser(this, str);
  }
  if (!parse && isJSON(this.type)) {
    parse = request.parse['application/json'];
  }
  return parse && str && (str.length || str instanceof Object)
    ? parse(str)
    : null;
};

/**
 * Return an `Error` representative of this response.
 *
 * @return {Error}
 * @api public
 */

Response.prototype.toError = function(){
  var req = this.req;
  var method = req.method;
  var url = req.url;

  var msg = 'cannot ' + method + ' ' + url + ' (' + this.status + ')';
  var err = new Error(msg);
  err.status = this.status;
  err.method = method;
  err.url = url;

  return err;
};

/**
 * Expose `Response`.
 */

request.Response = Response;

/**
 * Initialize a new `Request` with the given `method` and `url`.
 *
 * @param {String} method
 * @param {String} url
 * @api public
 */

function Request(method, url) {
  var self = this;
  this._query = this._query || [];
  this.method = method;
  this.url = url;
  this.header = {}; // preserves header name case
  this._header = {}; // coerces header names to lowercase
  this.on('end', function(){
    var err = null;
    var res = null;

    try {
      res = new Response(self);
    } catch(e) {
      err = new Error('Parser is unable to parse the response');
      err.parse = true;
      err.original = e;
      // issue #675: return the raw response if the response parsing fails
      if (self.xhr) {
        // ie9 doesn't have 'response' property
        err.rawResponse = typeof self.xhr.responseType == 'undefined' ? self.xhr.responseText : self.xhr.response;
        // issue #876: return the http status code if the response parsing fails
        err.status = self.xhr.status ? self.xhr.status : null;
        err.statusCode = err.status; // backwards-compat only
      } else {
        err.rawResponse = null;
        err.status = null;
      }

      return self.callback(err);
    }

    self.emit('response', res);

    var new_err;
    try {
      if (!self._isResponseOK(res)) {
        new_err = new Error(res.statusText || 'Unsuccessful HTTP response');
        new_err.original = err;
        new_err.response = res;
        new_err.status = res.status;
      }
    } catch(e) {
      new_err = e; // #985 touching res may cause INVALID_STATE_ERR on old Android
    }

    // #1000 don't catch errors from the callback to avoid double calling it
    if (new_err) {
      self.callback(new_err, res);
    } else {
      self.callback(null, res);
    }
  });
}

/**
 * Mixin `Emitter` and `RequestBase`.
 */

Emitter(Request.prototype);
RequestBase(Request.prototype);

/**
 * Set Content-Type to `type`, mapping values from `request.types`.
 *
 * Examples:
 *
 *      superagent.types.xml = 'application/xml';
 *
 *      request.post('/')
 *        .type('xml')
 *        .send(xmlstring)
 *        .end(callback);
 *
 *      request.post('/')
 *        .type('application/xml')
 *        .send(xmlstring)
 *        .end(callback);
 *
 * @param {String} type
 * @return {Request} for chaining
 * @api public
 */

Request.prototype.type = function(type){
  this.set('Content-Type', request.types[type] || type);
  return this;
};

/**
 * Set Accept to `type`, mapping values from `request.types`.
 *
 * Examples:
 *
 *      superagent.types.json = 'application/json';
 *
 *      request.get('/agent')
 *        .accept('json')
 *        .end(callback);
 *
 *      request.get('/agent')
 *        .accept('application/json')
 *        .end(callback);
 *
 * @param {String} accept
 * @return {Request} for chaining
 * @api public
 */

Request.prototype.accept = function(type){
  this.set('Accept', request.types[type] || type);
  return this;
};

/**
 * Set Authorization field value with `user` and `pass`.
 *
 * @param {String} user
 * @param {String} [pass] optional in case of using 'bearer' as type
 * @param {Object} options with 'type' property 'auto', 'basic' or 'bearer' (default 'basic')
 * @return {Request} for chaining
 * @api public
 */

Request.prototype.auth = function(user, pass, options){
  if (typeof pass === 'object' && pass !== null) { // pass is optional and can substitute for options
    options = pass;
  }
  if (!options) {
    options = {
      type: 'function' === typeof btoa ? 'basic' : 'auto',
    }
  }

  switch (options.type) {
    case 'basic':
      this.set('Authorization', 'Basic ' + btoa(user + ':' + pass));
    break;

    case 'auto':
      this.username = user;
      this.password = pass;
    break;

    case 'bearer': // usage would be .auth(accessToken, { type: 'bearer' })
      this.set('Authorization', 'Bearer ' + user);
    break;
  }
  return this;
};

/**
 * Add query-string `val`.
 *
 * Examples:
 *
 *   request.get('/shoes')
 *     .query('size=10')
 *     .query({ color: 'blue' })
 *
 * @param {Object|String} val
 * @return {Request} for chaining
 * @api public
 */

Request.prototype.query = function(val){
  if ('string' != typeof val) val = serialize(val);
  if (val) this._query.push(val);
  return this;
};

/**
 * Queue the given `file` as an attachment to the specified `field`,
 * with optional `options` (or filename).
 *
 * ``` js
 * request.post('/upload')
 *   .attach('content', new Blob(['<a id="a"><b id="b">hey!</b></a>'], { type: "text/html"}))
 *   .end(callback);
 * ```
 *
 * @param {String} field
 * @param {Blob|File} file
 * @param {String|Object} options
 * @return {Request} for chaining
 * @api public
 */

Request.prototype.attach = function(field, file, options){
  if (file) {
    if (this._data) {
      throw Error("superagent can't mix .send() and .attach()");
    }

    this._getFormData().append(field, file, options || file.name);
  }
  return this;
};

Request.prototype._getFormData = function(){
  if (!this._formData) {
    this._formData = new root.FormData();
  }
  return this._formData;
};

/**
 * Invoke the callback with `err` and `res`
 * and handle arity check.
 *
 * @param {Error} err
 * @param {Response} res
 * @api private
 */

Request.prototype.callback = function(err, res){
  // console.log(this._retries, this._maxRetries)
  if (this._maxRetries && this._retries++ < this._maxRetries && shouldRetry(err, res)) {
    return this._retry();
  }

  var fn = this._callback;
  this.clearTimeout();

  if (err) {
    if (this._maxRetries) err.retries = this._retries - 1;
    this.emit('error', err);
  }

  fn(err, res);
};

/**
 * Invoke callback with x-domain error.
 *
 * @api private
 */

Request.prototype.crossDomainError = function(){
  var err = new Error('Request has been terminated\nPossible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.');
  err.crossDomain = true;

  err.status = this.status;
  err.method = this.method;
  err.url = this.url;

  this.callback(err);
};

// This only warns, because the request is still likely to work
Request.prototype.buffer = Request.prototype.ca = Request.prototype.agent = function(){
  console.warn("This is not supported in browser version of superagent");
  return this;
};

// This throws, because it can't send/receive data as expected
Request.prototype.pipe = Request.prototype.write = function(){
  throw Error("Streaming is not supported in browser version of superagent");
};

/**
 * Compose querystring to append to req.url
 *
 * @api private
 */

Request.prototype._appendQueryString = function(){
  var query = this._query.join('&');
  if (query) {
    this.url += (this.url.indexOf('?') >= 0 ? '&' : '?') + query;
  }

  if (this._sort) {
    var index = this.url.indexOf('?');
    if (index >= 0) {
      var queryArr = this.url.substring(index + 1).split('&');
      if (isFunction(this._sort)) {
        queryArr.sort(this._sort);
      } else {
        queryArr.sort();
      }
      this.url = this.url.substring(0, index) + '?' + queryArr.join('&');
    }
  }
};

/**
 * Check if `obj` is a host object,
 * we don't want to serialize these :)
 *
 * @param {Object} obj
 * @return {Boolean}
 * @api private
 */
Request.prototype._isHost = function _isHost(obj) {
  // Native objects stringify to [object File], [object Blob], [object FormData], etc.
  return obj && 'object' === typeof obj && !Array.isArray(obj) && Object.prototype.toString.call(obj) !== '[object Object]';
}

/**
 * Initiate request, invoking callback `fn(res)`
 * with an instanceof `Response`.
 *
 * @param {Function} fn
 * @return {Request} for chaining
 * @api public
 */

Request.prototype.end = function(fn){
  if (this._endCalled) {
    console.warn("Warning: .end() was called twice. This is not supported in superagent");
  }
  this._endCalled = true;

  // store callback
  this._callback = fn || noop;

  // querystring
  this._appendQueryString();

  return this._end();
};

Request.prototype._end = function() {
  var self = this;
  var xhr = this.xhr = request.getXHR();
  var data = this._formData || this._data;

  this._setTimeouts();

  // state change
  xhr.onreadystatechange = function(){
    var readyState = xhr.readyState;
    if (readyState >= 2 && self._responseTimeoutTimer) {
      clearTimeout(self._responseTimeoutTimer);
    }
    if (4 != readyState) {
      return;
    }

    // In IE9, reads to any property (e.g. status) off of an aborted XHR will
    // result in the error "Could not complete the operation due to error c00c023f"
    var status;
    try { status = xhr.status } catch(e) { status = 0; }

    if (!status) {
      if (self.timedout || self._aborted) return;
      return self.crossDomainError();
    }
    self.emit('end');
  };

  // progress
  var handleProgress = function(direction, e) {
    if (e.total > 0) {
      e.percent = e.loaded / e.total * 100;
    }
    e.direction = direction;
    self.emit('progress', e);
  }
  if (this.hasListeners('progress')) {
    try {
      xhr.onprogress = handleProgress.bind(null, 'download');
      if (xhr.upload) {
        xhr.upload.onprogress = handleProgress.bind(null, 'upload');
      }
    } catch(e) {
      // Accessing xhr.upload fails in IE from a web worker, so just pretend it doesn't exist.
      // Reported here:
      // https://connect.microsoft.com/IE/feedback/details/837245/xmlhttprequest-upload-throws-invalid-argument-when-used-from-web-worker-context
    }
  }

  // initiate request
  try {
    if (this.username && this.password) {
      xhr.open(this.method, this.url, true, this.username, this.password);
    } else {
      xhr.open(this.method, this.url, true);
    }
  } catch (err) {
    // see #1149
    return this.callback(err);
  }

  // CORS
  if (this._withCredentials) xhr.withCredentials = true;

  // body
  if (!this._formData && 'GET' != this.method && 'HEAD' != this.method && 'string' != typeof data && !this._isHost(data)) {
    // serialize stuff
    var contentType = this._header['content-type'];
    var serialize = this._serializer || request.serialize[contentType ? contentType.split(';')[0] : ''];
    if (!serialize && isJSON(contentType)) {
      serialize = request.serialize['application/json'];
    }
    if (serialize) data = serialize(data);
  }

  // set header fields
  for (var field in this.header) {
    if (null == this.header[field]) continue;
    xhr.setRequestHeader(field, this.header[field]);
  }

  if (this._responseType) {
    xhr.responseType = this._responseType;
  }

  // send stuff
  this.emit('request', this);

  // IE11 xhr.send(undefined) sends 'undefined' string as POST payload (instead of nothing)
  // We need null here if data is undefined
  xhr.send(typeof data !== 'undefined' ? data : null);
  return this;
};

/**
 * GET `url` with optional callback `fn(res)`.
 *
 * @param {String} url
 * @param {Mixed|Function} [data] or fn
 * @param {Function} [fn]
 * @return {Request}
 * @api public
 */

request.get = function(url, data, fn){
  var req = request('GET', url);
  if ('function' == typeof data) fn = data, data = null;
  if (data) req.query(data);
  if (fn) req.end(fn);
  return req;
};

/**
 * HEAD `url` with optional callback `fn(res)`.
 *
 * @param {String} url
 * @param {Mixed|Function} [data] or fn
 * @param {Function} [fn]
 * @return {Request}
 * @api public
 */

request.head = function(url, data, fn){
  var req = request('HEAD', url);
  if ('function' == typeof data) fn = data, data = null;
  if (data) req.send(data);
  if (fn) req.end(fn);
  return req;
};

/**
 * OPTIONS query to `url` with optional callback `fn(res)`.
 *
 * @param {String} url
 * @param {Mixed|Function} [data] or fn
 * @param {Function} [fn]
 * @return {Request}
 * @api public
 */

request.options = function(url, data, fn){
  var req = request('OPTIONS', url);
  if ('function' == typeof data) fn = data, data = null;
  if (data) req.send(data);
  if (fn) req.end(fn);
  return req;
};

/**
 * DELETE `url` with optional `data` and callback `fn(res)`.
 *
 * @param {String} url
 * @param {Mixed} [data]
 * @param {Function} [fn]
 * @return {Request}
 * @api public
 */

function del(url, data, fn){
  var req = request('DELETE', url);
  if ('function' == typeof data) fn = data, data = null;
  if (data) req.send(data);
  if (fn) req.end(fn);
  return req;
};

request['del'] = del;
request['delete'] = del;

/**
 * PATCH `url` with optional `data` and callback `fn(res)`.
 *
 * @param {String} url
 * @param {Mixed} [data]
 * @param {Function} [fn]
 * @return {Request}
 * @api public
 */

request.patch = function(url, data, fn){
  var req = request('PATCH', url);
  if ('function' == typeof data) fn = data, data = null;
  if (data) req.send(data);
  if (fn) req.end(fn);
  return req;
};

/**
 * POST `url` with optional `data` and callback `fn(res)`.
 *
 * @param {String} url
 * @param {Mixed} [data]
 * @param {Function} [fn]
 * @return {Request}
 * @api public
 */

request.post = function(url, data, fn){
  var req = request('POST', url);
  if ('function' == typeof data) fn = data, data = null;
  if (data) req.send(data);
  if (fn) req.end(fn);
  return req;
};

/**
 * PUT `url` with optional `data` and callback `fn(res)`.
 *
 * @param {String} url
 * @param {Mixed|Function} [data] or fn
 * @param {Function} [fn]
 * @return {Request}
 * @api public
 */

request.put = function(url, data, fn){
  var req = request('PUT', url);
  if ('function' == typeof data) fn = data, data = null;
  if (data) req.send(data);
  if (fn) req.end(fn);
  return req;
};

},{"./is-function":1,"./is-object":2,"./request-base":3,"./response-base":4,"./should-retry":5,"component-emitter":7}]},{},[8])(8)
});


text:
package v1

import (
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/conversion"
	"k8s.io/apimachinery/pkg/runtime"
	kapi "k8s.io/kubernetes/pkg/api"
	kapiv1 "k8s.io/kubernetes/pkg/api/v1"

	"github.com/openshift/api/quota/v1"
	internal "github.com/openshift/origin/pkg/quota/apis/quota"
)

func Convert_v1_ResourceQuotasStatusByNamespace_To_quota_ResourceQuotasStatusByNamespace(in *v1.ResourceQuotasStatusByNamespace, out *internal.ResourceQuotasStatusByNamespace, s conversion.Scope) error {
	if in == nil {
		return nil
	}

	for _, curr := range *in {
		internalStatus := &kapi.ResourceQuotaStatus{}
		kapiv1.Convert_v1_ResourceQuotaStatus_To_api_ResourceQuotaStatus(&curr.Status, internalStatus, s)

		out.Insert(curr.Namespace, *internalStatus)
	}

	return nil
}

func Convert_quota_ResourceQuotasStatusByNamespace_To_v1_ResourceQuotasStatusByNamespace(in *internal.ResourceQuotasStatusByNamespace, out *v1.ResourceQuotasStatusByNamespace, s conversion.Scope) error {
	for e := in.OrderedKeys().Front(); e != nil; e = e.Next() {
		namespace := e.Value.(string)
		status, _ := in.Get(namespace)

		versionedStatus := &corev1.ResourceQuotaStatus{}
		kapiv1.Convert_api_ResourceQuotaStatus_To_v1_ResourceQuotaStatus(&status, versionedStatus, s)

		if out == nil {
			out = &v1.ResourceQuotasStatusByNamespace{}
		}
		*out = append(*out, v1.ResourceQuotaStatusByNamespace{Namespace: namespace, Status: *versionedStatus})
	}

	return nil
}

func addConversionFuncs(scheme *runtime.Scheme) error {
	return scheme.AddConversionFuncs(
		Convert_quota_ResourceQuotasStatusByNamespace_To_v1_ResourceQuotasStatusByNamespace,
		Convert_v1_ResourceQuotasStatusByNamespace_To_quota_ResourceQuotasStatusByNamespace,
	)
}


text:
@interface MOBProjectionEPSG32122 : MOBProjection

@end


text: from .a import b as a


text:
import React, { Component } from 'react';
import { Link, Match } from 'react-router';

import TopActionComponent from './TopActionComponent';
import ResultTable from './ResultTable';
import SearchBar from './SearchBar';
import NoSearchResult from './NoSearchResult';
import PostJobComponent from './PostJobComponent';

class Search extends Component{

	constructor( context, props ){
		super( context, props );

		this.state = {
			data: [],
			search_term: ""
		}
	}
	componentWillMount(){
		this.props.State.Actions.pullAccountUserData();
		this.props.State.Actions.pullSpecificJobs();
		this.props.State.Actions.pullJobs();
		this.props.State.Actions.getEmployeeBids();
		this.props.State.Actions.getMyMessages();
		this.props.State.Actions.getMySentMessages();
	}
	render() {
		var checker = ( this.props.data.length !== 0 && !this.props.data.hasOwnProperty('message') ) ?
		<ResultTable data = {this.props.data} State = {this.props.State} /> : (this.props.suggestions.length !== 0 ) ? <NoSearchResult data = {this.props.suggestions} searchAction = {this.props.searchAction}/> : ""; // checks if the array is empty

		var availableJobs = ( this.props.State.JobsAvailable.length !== 0 ) ? <ResultTable data = {this.props.State.JobsAvailable} State = {this.props.State} /> :
No Jobs With Regards To Your Profession Are Available At This Time
;
		return (

            			<TopActionComponent State = {this.props.State} AccountUser = {this.props.AccountUser} />

									<SearchBar searchAction = {this.props.searchAction}/>
									{checker}

								{(this.props.data.length === 0 ) ? availableJobs : ""}

		);
	}
}

export default Search;


text:
package com.ctac.jpmc.game.conway;

/**
 *
 * an Exception to handle invalid coordinates
 *
 */
public class CoordinatesException extends RuntimeException {

	private static final long serialVersionUID = -5287403242940367050L;

	public CoordinatesException (String msg) {
		super (msg);
	}

}


text:
var tabSet = function (parent, id) {
    var res = {}, tabs = [], tabSet, header, content;
    res.tabs = tabs;

    tabSet = document.createElement('div');
    header = document.createElement('div');
    content = document.createElement('div');

    tabSet.id = id;
    tabSet.tabSet = res;//allow accesing tabset from DOM.

    tabSet.className = 'tabSet';
    header.className = 'tabHeader';
    content.className = 'tabContent';

    parent.appendChild(tabSet);
    tabSet.appendChild(header);
    tabSet.appendChild(content);

    res.addTab = function (tabTitleText, contentNode) {
        var tabTitle = document.createElement('label'),
            tabContent = document.createElement('div');
        tabs.push(tabContent);

        content.appendChild(tabContent);
        header.appendChild(tabTitle);

        tabTitle.innerHTML = tabTitleText;
        tabContent.className = tabTitleText;

        tabContent.setInactive = function () {
            tabContent.style.display = 'none';
            tabTitle.className = 'none';
        };
        tabContent.setActive = function () {
            tabContent.style.display = 'block';
            tabTitle.className = 'active';
        };

        tabTitle.onclick = function () {
            tabs.forEach(function (tab) {
                tab.setInactive();
            });
            tabContent.setActive();
        };

        if (contentNode) {
            tabContent.appendChild(contentNode);
        }
        if (tabs.indexOf(tabContent) === 0) {
             tabContent.setActive();
        } else {
             tabContent.setInactive();
        }
        return tabContent;
    };
    return res;
};


text:
@class NSData, NSString;

@interface WAPermissionInfo : NSObject <WCDBCoding>
{
    NSString *appID;
    NSData *permissionBytes;
    long long m___rowID;
}

+ (const basic_string_a490aa4c *)getWCDBPrimaryColumnName;
+ (const struct WCDBIndexHelper *)getWCDBIndexArray;
+ (unsigned long long)getWCDBIndexArrayCount;
+ (const map_0e718273 *)getFileValueTagIndexMap;
+ (id)getFileValueTypeTable;
+ (const map_0e718273 *)getPackedValueTagIndexMap;
+ (id)getPackedValueTypeTable;
+ (const map_7a576766 *)getValueNameIndexMap;
+ (id)getValueTable;
+ (id)dummyObject;
@property(nonatomic) long long __rowID; // @synthesize __rowID=m___rowID;
@property(retain, nonatomic) NSData *permissionBytes; // @synthesize permissionBytes;
@property(retain, nonatomic) NSString *appID; // @synthesize appID;
- (void).cxx_destruct;
- (const map_0e718273 *)getValueTagIndexMap;
- (id)getValueTypeTable;
- (const WCDBCondition_91e67114 *)db_permissionBytes;
- (const WCDBCondition_22fabacd *)db_appID;

// Remaining properties
@property(readonly, copy) NSString *debugDescription;
@property(readonly, copy) NSString *description;
@property(readonly) unsigned long long hash;
@property(readonly) Class superclass;

@end


text:
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                Toggle navigation

            </button>

            {%- block site_name %}
            {{ config.site_name }}
            {%- endblock %}

            {%- block site_nav %}

            {% for nav_item in nav %}
            {% if nav_item.children %}

                    {{ nav_item.title }}

                    {% for nav_item in nav_item.children %}

                            {{ nav_item.title }}

                    {% endfor %}

            {% else %}

                    {{ nav_item.title }}

            {% endif %}
            {% endfor %}

            {%- endblock %}

                {%- block search_button %} {%- endblock %}

                {%- block next_prev %}
                {%- if page and (page.next_page or page.previous_page) %}

                             Previous

                            Next

                {%- endif %}
                {%- endblock %}

                {%- block repo %}
                {% if config.repo_url %}

                        {% if config.repo_name == 'GitHub' %}

                        {% elif config.repo_name == 'Bitbucket' %}

                        {% endif %}
                        {{ config.repo_name }}

                {% endif %}
                {%- endblock %}


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Diagnostics;
using System.Windows.Data;

namespace Xceed.Wpf.DataGrid
{
  internal class SelectedCellsStorage : IEnumerable
  {
    #region CONSTRUCTORS

    internal SelectedCellsStorage( DataGridContext dataGridContext, int capacity )
    {
      // dataGridContext can be null for store that we don't want to auto-get Items from index
      m_dataGridContext = dataGridContext;
      m_list = new List<SelectionCellRangeWithItems>( capacity );
    }

    internal SelectedCellsStorage( SelectedCellsStorage collection )
    {
      m_dataGridContext = collection.m_dataGridContext;
      m_list = new List<SelectionCellRangeWithItems>( collection.m_list );
      m_cellsCount = collection.m_cellsCount;
    }

    #endregion CONSTRUCTORS

    #region PUBLIC PROPERTIES

    public int Count
    {
      get
      {
        return m_list.Count;
      }
    }

    public int CellsCount
    {
      get
      {
        return m_cellsCount;
      }
    }

    public DataGridContext DataGridContext
    {
      get
      {
        return m_dataGridContext;
      }
    }

    public SelectionCellRangeWithItems this[ int index ]
    {
      get
      {
        return m_list[ index ];
      }
      set
      {
        SelectionCellRangeWithItems oldRange = m_list[ index ];
        m_list[ index ] = value;
        m_cellsCount += value.Length - oldRange.Length;
      }
    }

    #endregion PUBLIC PROPERTIES

    #region IEnumerable

    IEnumerator IEnumerable.GetEnumerator()
    {
      return m_list.GetEnumerator();
    }

    #endregion IEnumerable

    #region PUBLIC METHODS

    public int Add( SelectionCellRangeWithItems cellRangeWithItems )
    {

      m_cellsCount += cellRangeWithItems.Length;
      SelectionRangeWithItems itemRangeWithItems = cellRangeWithItems.ItemRangeWithItems;

      SelectedItemsStorage.UpdateSelectionRangeWithItemsFromAdd(
        m_dataGridContext, itemRangeWithItems, ref itemRangeWithItems );

      m_list.Add( new SelectionCellRangeWithItems(
        itemRangeWithItems.Range, itemRangeWithItems.Items, cellRangeWithItems.ColumnRange ) );

      return m_list.Count - 1;
    }

    public void Insert( int index, SelectionCellRangeWithItems cellRangeWithItems )
    {
      Debug.Assert( !this.Contains( cellRangeWithItems.CellRange ) );
      m_cellsCount += cellRangeWithItems.Length;

      SelectionRangeWithItems itemRangeWithItems = cellRangeWithItems.ItemRangeWithItems;

      SelectedItemsStorage.UpdateSelectionRangeWithItemsFromAdd(
        m_dataGridContext, itemRangeWithItems, ref itemRangeWithItems );

      m_list.Insert(
        index, new SelectionCellRangeWithItems(
          itemRangeWithItems.Range, itemRangeWithItems.Items, cellRangeWithItems.ColumnRange ) );
    }

    public void RemoveAt( int index )
    {
      m_cellsCount -= m_list[ index ].Length;
      m_list.RemoveAt( index );
    }

    public void Clear()
    {
      m_cellsCount = 0;
      m_list.Clear();
    }

    public SelectionCellRangeWithItems[] ToArray()
    {
      return m_list.ToArray();
    }

    public bool Contains( int itemIndex, int columnIndex )
    {
      if( ( itemIndex < 0 ) || ( columnIndex < 0 ) )
        return false;

      SelectionCellRange cellRange = new SelectionCellRange( itemIndex, columnIndex );
      int count = m_list.Count;

      for( int i = 0; i < count; i++ )
      {
        if( !m_list[ i ].CellRange.Intersect( cellRange ).IsEmpty )
          return true;
      }

      return false;
    }

    public bool Contains( SelectionCellRange cellRange )
    {
      int count = m_list.Count;

      int cellRangeLength = cellRange.Length;

      // Ensure every Cells are present in the SelectedCellsStorage
      if( cellRangeLength > 1 )
      {
        SelectedCellsStorage cellStorage = new SelectedCellsStorage( null, cellRangeLength );
        cellStorage.Add( new SelectionCellRangeWithItems( cellRange.ItemRange,
          null,
          cellRange.ColumnRange ) );

        for( int i = 0; i < count; i++ )
        {
          cellStorage.Remove( m_list[ i ] );

          if( cellStorage.Count == 0 )
            return true;
        }
      }
      else
      {
        for( int i = 0; i < count; i++ )
        {
          if( !m_list[ i ].CellRange.Intersect( cellRange ).IsEmpty )
            return true;
        }
      }

      return false;
    }

    public bool Contains( SelectionCellRangeWithItems cellRangeWithItemsToCompare )
    {
      int count = m_list.Count;
      SelectionCellRange cellRangeToCompare = cellRangeWithItemsToCompare.CellRange;

      int cellRangeLength = cellRangeToCompare.Length;
      // If there is more than one Cell in the range, ensure
      // the range is completely contained within this SelectedCellsStorage
      if( cellRangeLength > 1 )
      {
        SelectedCellsStorage cellStorage = new SelectedCellsStorage( null, cellRangeLength );
        cellStorage.Add( cellRangeWithItemsToCompare );

        // The range is completely contained when the store
        // is empty after removing all ranges contained within
        // this SelectedCellsStorage
        for( int i = 0; i < count; i++ )
        {
          SelectionCellRangeWithItems cellRangeWithItems = m_list[ i ];

          cellStorage.Remove( cellRangeWithItems );

          if( cellStorage.Count == 0 )
            return true;
        }
      }
      else
      {
        for( int i = 0; i < count; i++ )
        {
          SelectionCellRangeWithItems rangeWithItem = m_list[ i ];
          SelectionCellRange rangeIntersection = rangeWithItem.CellRange.Intersect( cellRangeToCompare );

          if( !rangeIntersection.IsEmpty )
          {
            if( cellRangeWithItemsToCompare.ItemRangeWithItems.IsItemsEqual(
              rangeIntersection.ItemRange, rangeWithItem.ItemRangeWithItems ) )
            {
              return true;
            }
          }
        }
      }

      return false;
    }

    public bool Remove( SelectionCellRangeWithItems cellRangeWithItemsToRemove )
    {
      if( cellRangeWithItemsToRemove.CellRange.IsEmpty )
        return true;

      bool removed = false;
      SelectionCellRange cellRangeToRemove = cellRangeWithItemsToRemove.CellRange;
      object[] itemsToRemove = cellRangeWithItemsToRemove.ItemRangeWithItems.Items;
      int itemsToRemoveCount = ( itemsToRemove == null ) ? 0 : itemsToRemove.Length;
      int itemOffsetToRemove = 0;
      bool itemRangeToRemoveIsEmpty = cellRangeToRemove.ItemRange.IsEmpty;

      do
      {
        for( int i = m_list.Count - 1; i >= 0; i-- )
        {
          SelectionCellRangeWithItems cellRangeWithItems = m_list[ i ];
          SelectionRange itemRange = cellRangeWithItems.ItemRange;
          SelectionCellRange cellRange = cellRangeWithItems.CellRange;
          SelectionRangeWithItems itemRangeWithItems = cellRangeWithItems.ItemRangeWithItems;
          SelectionCellRange cellRangeIntersection;
          object[] oldRangeItems = itemRangeWithItems.Items;

          if( itemRangeToRemoveIsEmpty )
          {
            int itemOffset = Array.IndexOf( oldRangeItems, itemsToRemove[ itemOffsetToRemove ] );

            if( itemOffset == -1 )
              continue;

            int itemIndex = itemRange.GetIndexFromItemOffset( itemOffset );

            cellRangeIntersection = new SelectionCellRange(
              new SelectionRange( itemIndex, itemIndex ),
              cellRangeWithItems.ColumnRange.Intersect( cellRangeToRemove.ColumnRange ) );
          }
          else
          {
            cellRangeIntersection = cellRange.Intersect( cellRangeToRemove );

            if( cellRangeIntersection.IsEmpty )
              continue;

            if( !itemRangeWithItems.IsItemsEqual(
              cellRangeIntersection.ItemRange, cellRangeWithItemsToRemove.ItemRangeWithItems ) )
            {
              continue;
            }
          }

          removed = true;
          m_cellsCount -= cellRangeIntersection.Length;
          SelectionCellRange[] newCellRanges = cellRange.Exclude( cellRangeIntersection );

          if( newCellRanges.Length == 0 )
          {
            m_list.RemoveAt( i );
          }
          else
          {
            SelectionCellRange newCellRange = newCellRanges[ 0 ];

            m_list[ i ] = new SelectionCellRangeWithItems(
              newCellRange.ItemRange, itemRangeWithItems.GetItems( newCellRange.ItemRange ), newCellRange.ColumnRange );

            for( int j = 1; j < newCellRanges.Length; j++ )
            {
              newCellRange = newCellRanges[ j ];

              m_list.Insert( i + j, new SelectionCellRangeWithItems(
                newCellRange.ItemRange, itemRangeWithItems.GetItems( newCellRange.ItemRange ), newCellRange.ColumnRange ) );
            }
          }
        }

        itemOffsetToRemove++;
      } while( ( itemRangeToRemoveIsEmpty ) && ( itemOffsetToRemove < itemsToRemoveCount ) );

      return removed;
    }

    public void OffsetIndex( int itemStartIndex, int offset )
    {
      // Used to offset index after an add or remove from the data source of the grid.

      SelectionRange offsetRange = new SelectionRange(
        itemStartIndex, itemStartIndex + Math.Abs( offset ) - 1 );

      for( int i = this.Count - 1; i >= 0; i-- )
      {
        SelectionCellRangeWithItems cellRangeWithItems = m_list[ i ];
        SelectionRange itemRange = cellRangeWithItems.ItemRange;
        SelectionRange columnRange = cellRangeWithItems.ColumnRange;

        if( offsetRange > itemRange )
          continue;

        SelectionRange itemRangeIntersection = itemRange.Intersect( offsetRange );
        object[] originalItems = cellRangeWithItems.ItemRangeWithItems.Items;

        if( !itemRangeIntersection.IsEmpty )
        {
          // Should only happen when adding since when we remove data from the source, we remove the
          // the range from the list.
          Debug.Assert( offset > 0 );

          // Offset the index higher than the start index of the new added item
          SelectionRange topRange;
          SelectionRange bottomRange;

          if( itemRange.StartIndex > itemRange.EndIndex )
          {
            if( itemRangeIntersection.EndIndex == itemRange.EndIndex )
            {
              SelectionRange newItemRange = new SelectionRange( itemRange.StartIndex + offset, itemRange.EndIndex + offset );
              m_list[ i ] = new SelectionCellRangeWithItems( newItemRange, originalItems, columnRange );
              continue;
            }
            else
            {
              int bottomRangeEndIndex = itemStartIndex + offset;
              bottomRange = new SelectionRange( itemStartIndex - 1, itemRange.EndIndex );
              topRange = new SelectionRange( itemRange.Length - bottomRange.Length - 1 + bottomRangeEndIndex, bottomRangeEndIndex );
            }
          }
          else
          {
            if( itemRangeIntersection.StartIndex == itemRange.StartIndex )
            {
              SelectionRange newItemRange = new SelectionRange( itemRange.StartIndex + offset, itemRange.EndIndex + offset );
              m_list[ i ] = new SelectionCellRangeWithItems( newItemRange, originalItems, columnRange );
              continue;
            }
            else
            {
              int bottomRangeStartIndex = itemStartIndex + offset;
              topRange = new SelectionRange( itemRange.StartIndex, itemStartIndex - 1 );
              bottomRange = new SelectionRange( bottomRangeStartIndex, itemRange.Length - topRange.Length - 1 + bottomRangeStartIndex );
            }
          }

          object[] topItems = null;
          object[] bottomItems = null;

          if( originalItems != null )
          {
            topItems = new object[ topRange.Length ];
            Array.Copy( originalItems, 0, topItems, 0, topItems.Length );
            bottomItems = new object[ bottomRange.Length ];
            Array.Copy( originalItems, topItems.Length, bottomItems, 0, bottomItems.Length );
          }

          m_list[ i ] = new SelectionCellRangeWithItems( topRange, topItems, columnRange );
          m_list.Insert( i + 1, new SelectionCellRangeWithItems( bottomRange, bottomItems, columnRange ) );
        }
        else
        {
          // Offset the index by the count added
          SelectionRange newItemRange = new SelectionRange( itemRange.StartIndex + offset, itemRange.EndIndex + offset );
          m_list[ i ] = new SelectionCellRangeWithItems( newItemRange, originalItems, columnRange );
        }
      }
    }

    public void OffsetIndexBasedOnSourceNewIndex( int maxOffset )
    {
      if( m_dataGridContext == null )
        throw new InvalidOperationException( "We must have a DataGridContext to find the new index." );

      CollectionView sourceItems = m_dataGridContext.Items;

      for( int i = this.Count - 1; i >= 0; i-- )
      {
        SelectionCellRangeWithItems cellRangeWithItems = m_list[ i ];
        SelectionRangeWithItems itemRangeWithItems = cellRangeWithItems.ItemRangeWithItems;
        object[] items = itemRangeWithItems.Items;

        if( items == null )
          throw new InvalidOperationException( "We should have items to find the new index." );

        object item = items[ 0 ];
        SelectionRange itemRange = itemRangeWithItems.Range;
        int startIndex = Math.Min( itemRange.StartIndex, itemRange.EndIndex );

        if( maxOffset < 0 )
        {
          for( int j = 0; j >= maxOffset; j-- )
          {
            if( object.Equals( sourceItems.GetItemAt( startIndex ), item ) )
            {
              if( j != 0 )
              {
                SelectionRange newItemRange = new SelectionRange( itemRange.StartIndex + j, itemRange.EndIndex + j );
                m_list[ i ] = new SelectionCellRangeWithItems( newItemRange, items, cellRangeWithItems.ColumnRange );
              }

              break;
            }

            startIndex--;

            if( startIndex < 0 )
              break;
          }
        }
        else
        {
          int sourceItemCount = sourceItems.Count;

          for( int j = 0; j <= maxOffset; j++ )
          {
            if( object.Equals( sourceItems.GetItemAt( startIndex ), item ) )
            {
              if( j != 0 )
              {
                SelectionRange newItemRange = new SelectionRange( itemRange.StartIndex + j, itemRange.EndIndex + j );
                m_list[ i ] = new SelectionCellRangeWithItems( newItemRange, items, cellRangeWithItems.ColumnRange );
              }

              break;
            }

            startIndex++;

            if( startIndex >= sourceItemCount )
              break;
          }
        }
      }
    }

    public List<SelectionRange> GetIntersectedColumnRanges( SelectionCellRange cellRange )
    {
      List<SelectionRange> intersectionRanges = new List<SelectionRange>();
      int count = m_list.Count;

      for( int i = 0; i < count; i++ )
      {
        SelectionCellRangeWithItems range = m_list[ i ];
        SelectionCellRange cellRangeIntersection = range.CellRange.Intersect( cellRange );

        if( !cellRangeIntersection.IsEmpty )
        {
          intersectionRanges.Add( cellRangeIntersection.ColumnRange );
        }
      }

      return intersectionRanges;
    }

    public List<SelectionCellRangeWithItems> GetIntersectedCellRangesWithItems( SelectionCellRange cellRange )
    {
      List<SelectionCellRangeWithItems> intersectionCellRanges = new List<SelectionCellRangeWithItems>();
      int count = m_list.Count;

      for( int i = 0; i < count; i++ )
      {
        SelectionCellRangeWithItems range = m_list[ i ];
        SelectionCellRange cellRangeIntersection = range.CellRange.Intersect( cellRange );

        if( !cellRangeIntersection.IsEmpty )
        {
          intersectionCellRanges.Add(
            new SelectionCellRangeWithItems(
              cellRangeIntersection.ItemRange,
              range.ItemRangeWithItems.GetItems( cellRangeIntersection.ItemRange ),
              cellRangeIntersection.ColumnRange ) );
        }
      }

      return intersectionCellRanges;
    }

    #endregion PUBLIC METHODS

    private List<SelectionCellRangeWithItems> m_list;
    private DataGridContext m_dataGridContext;
    private int m_cellsCount;
  }
}


text:
import React from 'react';
import evalDependsOn from '../../../fields/utils/evalDependsOn';

module.exports = React.createClass({
	displayName: 'FormHeading',
	propTypes: {
		options: React.PropTypes.object
	},
	render () {
		if (!evalDependsOn(this.props.options.dependsOn, this.props.options.values)) {
			return null;
		}
		return
{this.props.content}
;
	},
});


text:
<!DOCTYPE html>

  <meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<meta name='description' content='TLS management on AWS'>
<meta name='theme-color' content='#ffcd00'>

<meta property='og:title' content='Automate TLS management on AWS with LetsEncrypt » INGE4PRES RATIONAL THOUGHTS'>
<meta property='og:description' content='TLS management on AWS'>
<meta property='og:url' content='http://inge.4pr.es/2016/12/03/2016-12-03-automate-tls-management-on-aws-with-letsencrypt/'>
<meta property='og:site_name' content='INGE4PRES RATIONAL THOUGHTS'>
<meta property='og:type' content='article'><meta property='article:publisher' content='358809487873346'><meta property='article:section' content='post'><meta property='article:tag' content='aws'><meta property='article:tag' content='tls'><meta property='article:tag' content='letsencrypt'><meta property='article:published_time' content='2016-12-03T15:34:48+01:00'/><meta property='article:modified_time' content='2016-12-03T15:34:48+01:00'/><meta property='fb:app_id' content='1845367892442473'><meta property='fb:admins' content='francesco.inge'><meta name='twitter:card' content='summary'><meta name='twitter:site' content='@inge4pres'>

<meta name="generator" content="Hugo 0.82.0" />

  <title>Automate TLS management on AWS with LetsEncrypt » INGE4PRES RATIONAL THOUGHTS</title>
  <link rel='canonical' href='http://inge.4pr.es/2016/12/03/2016-12-03-automate-tls-management-on-aws-with-letsencrypt/'>

  <link rel='icon' href='/favicon.ico'>
<link rel='stylesheet' href='/assets/css/main.ab98e12b.css'>

  Skip to Main Menu

      Francesco Gualazzi

    infrastructure-aware developer

    Skip to Content

<button id='sidebar-toggler' class='sidebar-toggler' aria-controls='sidebar'>
  Toggle Sidebar
  <svg class='icon' xmlns='http://www.w3.org/2000/svg' viewbox='0 0 24 24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' aria-hidden='true'>

  <line x1="3" y1="12" x2="21" y2="12" />
  <line x1="3" y1="6" x2="21" y2="6" />
  <line x1="3" y1="18" x2="21" y2="18" />

</svg>

  <svg class='icon' xmlns='http://www.w3.org/2000/svg' viewbox='0 0 24 24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' aria-hidden='true'>

  <line x1="18" y1="6" x2="6" y2="18" />
  <line x1="6" y1="6" x2="18" y2="18" />

</svg>

</button>

        /

        /about

        /resume

INGE4PRES RATIONAL THOUGHTS

Growing knowledge one post at a time

Automate TLS management on AWS with LetsEncrypt

TLS management on AWS

  <svg class='icon' xmlns='http://www.w3.org/2000/svg' viewbox='0 0 24 24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' aria-hidden='true'>

  <rect x="3" y="4" width="18" height="18" rx="2" ry="2"/>
  <line x1="16" y1="2" x2="16" y2="6"/>
  <line x1="8" y1="2" x2="8" y2="6"/>
  <line x1="3" y1="10" x2="21" y2="10"/>

</svg>
Posted on
  <time class='entry-date' datetime='2016-12-03T15:34:48+01:00'>3 Dec 2016</time>

<svg class='icon' xmlns='http://www.w3.org/2000/svg' viewbox='0 0 24 24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' aria-hidden='true'>

  <circle cx="12" cy="12" r="10"/>
  <polyline points="12 6 12 12 15 15"/>

</svg>
3 mins read

Letsencrypt is cool: automated, free TLS certificates for everybody! They are sponsored mainly by internet corps and they started a crowd-funding campaign to avoid the influence of this corps in the future of the project. I recently moved the blog to hugo on AWS and I’m now porting the TLS management scripts I wrote a while ago on AWS: this is a nice exercise to give a proper TLS automation valid for everyone on AWS.

I used Terraform, the AWS CLI and the amazing (although still in beta) lego; the idea is to spin up an EC2 instance every month to query Letsencrypt to renew a  certificate I already provisioned; the instance will have permissions to update the certificate using AWS Certificate Manager and other services like API Gateway custom domain names and Cloudfront are already bound to use ACM certificate of the domain. I tried and make the Terraform code as abstract and reusable as possible: cloudfront distribution id, domain name and issuer mail are all variable configurable via config file or at command line.

Setting up the environment

I prepared the TLS certificate and key running first lego once on my local box, using the DNS challenge against AWS Route53, lego will use Letsencrypt ACME secret and put it into a TXT record for the domain to be validated so that Letsencrypt will know I am the owner of the domain. You can read more on Letsencrypt domain ownership validation here.

After the lego account is created, generally in $HOME/.lego, you will find private key and certificate is created for the domain(s); the certificate is a bundle of all the SAN submitted in the request. Then how to automate all of this on AWS?

Designing automation

The idea is that every service that needs TLS encryption will be able to fetch the certificate from an AWS service. This was not possible until AWS Certificate Manager was released a while ago: ACM will create or store a certificate to be used in other AWS services via the AWS API. No more magic to spread certificates via S3 or other tricks, you now have an awscli command!
So here I chose to use Terraform to bootstrap all the infrastructure to run lego and run Letsencrypt automatically via AWS Autoscaling group.

I first imported the certificate created with lego in ACM in us-east-1 region (N. Virginia): this is due to API Gateway limitations to be able to integrate natively with ACM only in us-east-1. I get the certificate ARN for the certificate just uploaded and I run terraform plan -var 'cf_distribution_id=EX4MPL3D15TR0' -var 'certificate_arn=...'.
Once completed I can see a new Autoscaling group with scheduled policies, a launch configuration that starting from the base Amazon AMI with a user-data script at each boot will:

download and install lego binary

sync the TLS account with the S3 bucket created

split the certificate from the chain, as they need to be uploaded separately

call the acm subcommand of awscli to update the certificate

This is amazing, I can now forget about TLS management for all of my websites!

<svg class='icon' xmlns='http://www.w3.org/2000/svg' viewbox='0 0 24 24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' aria-hidden='true'>

    <path d="M22,19a2,2,0,0,1-2,2H4a2,2,0,0,1-2-2V5A2,2,0,0,1,4,3H9l2,3h9a2,2,0,0,1,2,2Z"/>

</svg>
Categories: tech

<svg class='icon' xmlns='http://www.w3.org/2000/svg' viewbox='0 0 24 24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' aria-hidden='true'>

  <path d="M20.59,13.41l-7.17,7.17a2,2,0,0,1-2.83,0L2,12V2H12l8.59,8.59A2,2,0,0,1,20.59,13.41Z"/>
  <line x1="7" y1="7" x2="7" y2="7"/>

</svg>
Tags: aws, tls, letsencrypt

        <svg class='icon' xmlns='http://www.w3.org/2000/svg' viewbox='0 0 24 24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' aria-hidden='true'>

  <line x1="20" y1="12" x2="4" y2="12"/>
  <polyline points="10 18 4 12 10 6"/>

</svg>
 Previous
        Previous post: 4pres goes #serverless

        Next post: DevOps: you're doing it wrongNext <svg class='icon' xmlns='http://www.w3.org/2000/svg' viewbox='0 0 24 24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' aria-hidden='true'>

  <line x1="4" y1="12" x2="20" y2="12"/>
  <polyline points="14 6 20 12 14 18"/>

</svg>

Please enable JavaScript to view the comments powered by Disqus.
comments powered by Disqus

Stay Tuned

          Open Github account in new tab<svg class='icon' xmlns='http://www.w3.org/2000/svg' viewbox='0 0 24 24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' aria-hidden='true'>

      <path d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22"/>

</svg>

          Open Gitlab account in new tab<svg class='icon' xmlns='http://www.w3.org/2000/svg' viewbox='0 0 24 24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' aria-hidden='true'>

    <title>GitLab icon</title>  <path d="M22.65 14.39L12 22.13 1.35 14.39a.84.84 0 0 1-.3-.94l1.22-3.78 2.44-7.51A.42.42 0 0 1 4.82 2a.43.43 0 0 1 .58 0 .42.42 0 0 1 .11.18l2.44 7.49h8.1l2.44-7.51A.42.42 0 0 1 18.6 2a.43.43 0 0 1 .58 0 .42.42 0 0 1 .11.18l2.44 7.51L23 13.45a.84.84 0 0 1-.35.94z"/>
</svg>

          Open Twitter account in new tab<svg class='icon' xmlns='http://www.w3.org/2000/svg' viewbox='0 0 24 24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' aria-hidden='true'>

  <title>Twitter icon</title>
  <path d="M23 3a10.9 10.9 0 0 1-3.14 1.53 4.48 4.48 0 0 0-7.86 3v1A10.66 10.66 0 0 1 3 4s-4 9 5 13a11.64 11.64 0 0 1-7 2c9 5 20 0 20-11.5a4.5 4.5 0 0 0-.08-.83A7.72 7.72 0 0 0 23 3z"></path>

</svg>

          Contact via Email<svg class='icon' xmlns='http://www.w3.org/2000/svg' viewbox='0 0 24 24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' aria-hidden='true'>

    <path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"></path><polyline points="22,6 12,13 2,6"></polyline>
</svg>

          Open Linkedin account in new tab<svg class='icon' xmlns='http://www.w3.org/2000/svg' viewbox='0 0 24 24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' aria-hidden='true'>

    <path d="M16 8a6 6 0 0 1 6 6v7h-4v-7a2 2 0 0 0-2-2 2 2 0 0 0-2 2v7h-4v-7a6 6 0 0 1 6-6z"></path><rect x="2" y="9" width="4" height="12"></rect><circle cx="4" cy="4" r="2"></circle>
</svg>

          Open Instagram account in new tab<svg class='icon' xmlns='http://www.w3.org/2000/svg' viewbox='0 0 24 24' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' aria-hidden='true'>

    <rect x="2" y="2" width="20" height="20" rx="5" ry="5"></rect><path d="M16 11.37A4 4 0 1 1 12.63 8 4 4 0 0 1 16 11.37z"></path><line x1="17.5" y1="6.5" x2="17.51" y2="6.5"></line>
</svg>

inge4pres © 2012-2021 Francesco Gualazzi