feat: méthode WebSocket HA pour Lovelace + vue lumières créée
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
||||
## [0.3.0] - 14/03/2017
|
||||
### Fixed
|
||||
- Fixed construction of `BrowserHeaders` with an instance of `BrowserHeaders` from a separate dependency (it's possible to have two separate BrowserHeaders classes)
|
||||
|
||||
## [0.2.0] - 09/03/2017
|
||||
### Added
|
||||
- Added `toHeaders` function
|
||||
|
||||
## [0.1.0] - 28/02/2017
|
||||
### Added
|
||||
- Initial release
|
||||
+174
@@ -0,0 +1,174 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
# browser-headers
|
||||
> Compatibility Layer for the Headers class
|
||||
|
||||
[](https://travis-ci.org/improbable-eng/js-browser-headers)
|
||||

|
||||
[](https://www.npmjs.com/package/browser-headers)
|
||||
[](LICENSE)
|
||||

|
||||
|
||||
The [Headers](https://fetch.spec.whatwg.org/#headers-class) class defined in the [fetch spec](https://fetch.spec.whatwg.org/) has been implemented slightly differently across browser vendors at the time of writing (Feb 2017).
|
||||
|
||||
This package intends to provide a wrapper for the `Headers` class to ensure a consistent API and provides headers parsing from CLRF-delimited strings.
|
||||
|
||||
This package is written in TypeScript, but is designed to be used just as easily by JavaScript projects.
|
||||
|
||||
|
||||
## Installation
|
||||
via npm:
|
||||
|
||||
```bash
|
||||
$ npm install browser-headers
|
||||
```
|
||||
|
||||
## Browser Support
|
||||
This library is tested against Chrome, Safari, Firefox, Opera, Edge, IE 10 and IE 9.
|
||||
|
||||
## API
|
||||
|
||||
```js
|
||||
import BrowserHeaders from 'browser-headers';
|
||||
|
||||
const headers = new BrowserHeaders({
|
||||
"content-type": "application/json",
|
||||
"my-header": ["value-one","value-two"]
|
||||
});
|
||||
|
||||
headers.forEach((key, values) => {
|
||||
console.log(key, values);
|
||||
});
|
||||
|
||||
// Output:
|
||||
// "content-type", ["application/json"]
|
||||
// "my-header", ["value-one","value-two"]
|
||||
```
|
||||
|
||||
The `BrowserHeaders` class has the following methods:
|
||||
|
||||
#### constructor(init: Headers | {[key: string]: string|string[]} | Map<string,string|string[]> | string | BrowserHeaders, options: {splitValues: boolean}): string[]
|
||||
`init` can be one of:
|
||||
* An instance of `Headers`
|
||||
* A CLRF-delimited string (e.g. `key-a: one\r\nkey-b: two`)
|
||||
* An instance of `BrowserHeaders`
|
||||
* An object consisting of `string->(string|string[])` (e.g. `{"key-a":["one","two"],"key-b":"three"}`)
|
||||
* A `Map<string, string|string[]>`
|
||||
|
||||
The constructor takes an additional optional `options` parameter of `{ splitValues: boolean = false }`, where
|
||||
`splitValues` defines whether the header values should be split by comma (`,`) into separate strings - this is useful
|
||||
to unify the `.append` functionality of `Headers` implementations (see the warning at the end of this README).
|
||||
`splitValues` should be used with caution and defaults to `false` because it might split what is actually a single
|
||||
logical value that contained a `,`.
|
||||
|
||||
#### .get(key: string): string[]
|
||||
Returns all of the values for that header `key` as an array
|
||||
|
||||
#### .forEach(callback: (key: string, values: string[]) => void): void
|
||||
Invokes the provided callback with each key and it's associated values as an array
|
||||
|
||||
#### .set(key: string, values: string|string[]): void
|
||||
Overwrites the `key` with the value(s) specified.
|
||||
|
||||
#### .append(key: string, values: string|string[]): void
|
||||
Appends the value(s) to specified `key`.
|
||||
|
||||
#### .delete(key: string, value: string): void
|
||||
If the `value` is specified:
|
||||
Removes the specified `value` from the `key` if it is present.
|
||||
|
||||
Otherwise:
|
||||
Removes all values for the `key` if it is present.
|
||||
|
||||
#### .has(key: string, value?: string): boolean
|
||||
If the value is specified:
|
||||
Returns true if the `key` contains the corresponding `value`.
|
||||
|
||||
Otherwise:
|
||||
Returns true if the `key` has at least one value.
|
||||
|
||||
#### .appendFromString(str: string): void
|
||||
Appends the headers defined in the provided CLRF-delimited string (e.g. `key-a: one\r\nkey-b: two`)
|
||||
|
||||
#### .toHeaders(): Headers
|
||||
Returns an instance of the browser's `Headers` class. This will throw an exception if the current browser does not have
|
||||
the `Headers` class.
|
||||
|
||||
## Warning about `.append` in native `Headers`
|
||||
The `.append` function of the `Headers` class differs significantly between browsers.
|
||||
|
||||
Some browsers concatenate the values with `", "` or just `","` and others actually maintain the individual values such that
|
||||
they can return later return an array. There is a constructor option (see above: `splitValues`) that can be enabled to
|
||||
attempt to parse these concatenated strings back into individual values.
|
||||
```js
|
||||
const headers = new Headers();
|
||||
headers.append("key-A", "one");
|
||||
headers.append("key-A", "two");
|
||||
const keyA = headers.get("key-A"); // or .getAll depending on the browser
|
||||
console.log(typeof keyA);
|
||||
console.log(keyA);
|
||||
|
||||
// Output in Edge 14:
|
||||
// string
|
||||
// one, two
|
||||
|
||||
// Output in Safari 10:
|
||||
// string
|
||||
// one,two
|
||||
|
||||
// Output in Chrome 56:
|
||||
// object
|
||||
// ["one", "two"]
|
||||
```
|
||||
+360
@@ -0,0 +1,360 @@
|
||||
(function(e, a) { for(var i in a) e[i] = a[i]; }(exports, /******/ (function(modules) { // webpackBootstrap
|
||||
/******/ // The module cache
|
||||
/******/ var installedModules = {};
|
||||
/******/
|
||||
/******/ // The require function
|
||||
/******/ function __webpack_require__(moduleId) {
|
||||
/******/
|
||||
/******/ // Check if module is in cache
|
||||
/******/ if(installedModules[moduleId]) {
|
||||
/******/ return installedModules[moduleId].exports;
|
||||
/******/ }
|
||||
/******/ // Create a new module (and put it into the cache)
|
||||
/******/ var module = installedModules[moduleId] = {
|
||||
/******/ i: moduleId,
|
||||
/******/ l: false,
|
||||
/******/ exports: {}
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Execute the module function
|
||||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
||||
/******/
|
||||
/******/ // Flag the module as loaded
|
||||
/******/ module.l = true;
|
||||
/******/
|
||||
/******/ // Return the exports of the module
|
||||
/******/ return module.exports;
|
||||
/******/ }
|
||||
/******/
|
||||
/******/
|
||||
/******/ // expose the modules object (__webpack_modules__)
|
||||
/******/ __webpack_require__.m = modules;
|
||||
/******/
|
||||
/******/ // expose the module cache
|
||||
/******/ __webpack_require__.c = installedModules;
|
||||
/******/
|
||||
/******/ // identity function for calling harmony imports with the correct context
|
||||
/******/ __webpack_require__.i = function(value) { return value; };
|
||||
/******/
|
||||
/******/ // define getter function for harmony exports
|
||||
/******/ __webpack_require__.d = function(exports, name, getter) {
|
||||
/******/ if(!__webpack_require__.o(exports, name)) {
|
||||
/******/ Object.defineProperty(exports, name, {
|
||||
/******/ configurable: false,
|
||||
/******/ enumerable: true,
|
||||
/******/ get: getter
|
||||
/******/ });
|
||||
/******/ }
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
||||
/******/ __webpack_require__.n = function(module) {
|
||||
/******/ var getter = module && module.__esModule ?
|
||||
/******/ function getDefault() { return module['default']; } :
|
||||
/******/ function getModuleExports() { return module; };
|
||||
/******/ __webpack_require__.d(getter, 'a', getter);
|
||||
/******/ return getter;
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Object.prototype.hasOwnProperty.call
|
||||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
||||
/******/
|
||||
/******/ // __webpack_public_path__
|
||||
/******/ __webpack_require__.p = "";
|
||||
/******/
|
||||
/******/ // Load entry module and return exports
|
||||
/******/ return __webpack_require__(__webpack_require__.s = 1);
|
||||
/******/ })
|
||||
/************************************************************************/
|
||||
/******/ ([
|
||||
/* 0 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var util_1 = __webpack_require__(3);
|
||||
function isBrowserHeaders(arg) {
|
||||
return typeof arg === "object" && typeof arg.headersMap === "object" && typeof arg.forEach === "function";
|
||||
}
|
||||
var BrowserHeaders = (function () {
|
||||
function BrowserHeaders(init, options) {
|
||||
if (init === void 0) { init = {}; }
|
||||
if (options === void 0) { options = { splitValues: false }; }
|
||||
var _this = this;
|
||||
this.headersMap = {};
|
||||
if (init) {
|
||||
if (typeof Headers !== "undefined" && init instanceof Headers) {
|
||||
var keys = util_1.getHeaderKeys(init);
|
||||
keys.forEach(function (key) {
|
||||
var values = util_1.getHeaderValues(init, key);
|
||||
values.forEach(function (value) {
|
||||
if (options.splitValues) {
|
||||
_this.append(key, util_1.splitHeaderValue(value));
|
||||
}
|
||||
else {
|
||||
_this.append(key, value);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
else if (isBrowserHeaders(init)) {
|
||||
init.forEach(function (key, values) {
|
||||
_this.append(key, values);
|
||||
});
|
||||
}
|
||||
else if (typeof Map !== "undefined" && init instanceof Map) {
|
||||
var asMap = init;
|
||||
asMap.forEach(function (value, key) {
|
||||
_this.append(key, value);
|
||||
});
|
||||
}
|
||||
else if (typeof init === "string") {
|
||||
this.appendFromString(init);
|
||||
}
|
||||
else if (typeof init === "object") {
|
||||
Object.getOwnPropertyNames(init).forEach(function (key) {
|
||||
var asObject = init;
|
||||
var values = asObject[key];
|
||||
if (Array.isArray(values)) {
|
||||
values.forEach(function (value) {
|
||||
_this.append(key, value);
|
||||
});
|
||||
}
|
||||
else {
|
||||
_this.append(key, values);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
BrowserHeaders.prototype.appendFromString = function (str) {
|
||||
var pairs = str.split("\r\n");
|
||||
for (var i = 0; i < pairs.length; i++) {
|
||||
var p = pairs[i];
|
||||
var index = p.indexOf(":");
|
||||
if (index > 0) {
|
||||
var key = p.substring(0, index).trim();
|
||||
var value = p.substring(index + 1).trim();
|
||||
this.append(key, value);
|
||||
}
|
||||
}
|
||||
};
|
||||
BrowserHeaders.prototype.delete = function (key, value) {
|
||||
var normalizedKey = util_1.normalizeName(key);
|
||||
if (value === undefined) {
|
||||
delete this.headersMap[normalizedKey];
|
||||
}
|
||||
else {
|
||||
var existing = this.headersMap[normalizedKey];
|
||||
if (existing) {
|
||||
var index = existing.indexOf(value);
|
||||
if (index >= 0) {
|
||||
existing.splice(index, 1);
|
||||
}
|
||||
if (existing.length === 0) {
|
||||
delete this.headersMap[normalizedKey];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
BrowserHeaders.prototype.append = function (key, value) {
|
||||
var _this = this;
|
||||
var normalizedKey = util_1.normalizeName(key);
|
||||
if (!Array.isArray(this.headersMap[normalizedKey])) {
|
||||
this.headersMap[normalizedKey] = [];
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach(function (arrayValue) {
|
||||
_this.headersMap[normalizedKey].push(util_1.normalizeValue(arrayValue));
|
||||
});
|
||||
}
|
||||
else {
|
||||
this.headersMap[normalizedKey].push(util_1.normalizeValue(value));
|
||||
}
|
||||
};
|
||||
BrowserHeaders.prototype.set = function (key, value) {
|
||||
var normalizedKey = util_1.normalizeName(key);
|
||||
if (Array.isArray(value)) {
|
||||
var normalized_1 = [];
|
||||
value.forEach(function (arrayValue) {
|
||||
normalized_1.push(util_1.normalizeValue(arrayValue));
|
||||
});
|
||||
this.headersMap[normalizedKey] = normalized_1;
|
||||
}
|
||||
else {
|
||||
this.headersMap[normalizedKey] = [util_1.normalizeValue(value)];
|
||||
}
|
||||
};
|
||||
BrowserHeaders.prototype.has = function (key, value) {
|
||||
var keyArray = this.headersMap[util_1.normalizeName(key)];
|
||||
var keyExists = Array.isArray(keyArray);
|
||||
if (!keyExists) {
|
||||
return false;
|
||||
}
|
||||
if (value !== undefined) {
|
||||
var normalizedValue = util_1.normalizeValue(value);
|
||||
return keyArray.indexOf(normalizedValue) >= 0;
|
||||
}
|
||||
else {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
BrowserHeaders.prototype.get = function (key) {
|
||||
var values = this.headersMap[util_1.normalizeName(key)];
|
||||
if (values !== undefined) {
|
||||
return values.concat();
|
||||
}
|
||||
return [];
|
||||
};
|
||||
BrowserHeaders.prototype.forEach = function (callback) {
|
||||
var _this = this;
|
||||
Object.getOwnPropertyNames(this.headersMap)
|
||||
.forEach(function (key) {
|
||||
callback(key, _this.headersMap[key]);
|
||||
}, this);
|
||||
};
|
||||
BrowserHeaders.prototype.toHeaders = function () {
|
||||
if (typeof Headers !== "undefined") {
|
||||
var headers_1 = new Headers();
|
||||
this.forEach(function (key, values) {
|
||||
values.forEach(function (value) {
|
||||
headers_1.append(key, value);
|
||||
});
|
||||
});
|
||||
return headers_1;
|
||||
}
|
||||
else {
|
||||
throw new Error("Headers class is not defined");
|
||||
}
|
||||
};
|
||||
return BrowserHeaders;
|
||||
}());
|
||||
exports.BrowserHeaders = BrowserHeaders;
|
||||
|
||||
|
||||
/***/ }),
|
||||
/* 1 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var BrowserHeaders_1 = __webpack_require__(0);
|
||||
exports.BrowserHeaders = BrowserHeaders_1.BrowserHeaders;
|
||||
|
||||
|
||||
/***/ }),
|
||||
/* 2 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
function iterateHeaders(headers, callback) {
|
||||
var iterator = headers[Symbol.iterator]();
|
||||
var entry = iterator.next();
|
||||
while (!entry.done) {
|
||||
callback(entry.value[0]);
|
||||
entry = iterator.next();
|
||||
}
|
||||
}
|
||||
exports.iterateHeaders = iterateHeaders;
|
||||
function iterateHeadersKeys(headers, callback) {
|
||||
var iterator = headers.keys();
|
||||
var entry = iterator.next();
|
||||
while (!entry.done) {
|
||||
callback(entry.value);
|
||||
entry = iterator.next();
|
||||
}
|
||||
}
|
||||
exports.iterateHeadersKeys = iterateHeadersKeys;
|
||||
|
||||
|
||||
/***/ }),
|
||||
/* 3 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var iterateHeaders_1 = __webpack_require__(2);
|
||||
function normalizeName(name) {
|
||||
if (typeof name !== "string") {
|
||||
name = String(name);
|
||||
}
|
||||
if (/[^a-z0-9\-#$%&'*+.\^_`|~]/i.test(name)) {
|
||||
throw new TypeError("Invalid character in header field name");
|
||||
}
|
||||
return name.toLowerCase();
|
||||
}
|
||||
exports.normalizeName = normalizeName;
|
||||
function normalizeValue(value) {
|
||||
if (typeof value !== "string") {
|
||||
value = String(value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
exports.normalizeValue = normalizeValue;
|
||||
function getHeaderValues(headersAsNative, key) {
|
||||
var headers = toWindowHeaders(headersAsNative);
|
||||
if (headers instanceof Headers && headers.getAll) {
|
||||
return headers.getAll(key);
|
||||
}
|
||||
var getValue = headers.get(key);
|
||||
if (getValue && typeof getValue === "string") {
|
||||
return [getValue];
|
||||
}
|
||||
return getValue;
|
||||
}
|
||||
exports.getHeaderValues = getHeaderValues;
|
||||
function toWindowHeaders(headersAsNative) {
|
||||
return headersAsNative;
|
||||
}
|
||||
function getHeaderKeys(headersAsNative) {
|
||||
var headers = toWindowHeaders(headersAsNative);
|
||||
var asMap = {};
|
||||
var keys = [];
|
||||
if (headers.keys) {
|
||||
iterateHeaders_1.iterateHeadersKeys(headers, function (key) {
|
||||
if (!asMap[key]) {
|
||||
asMap[key] = true;
|
||||
keys.push(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (headers.forEach) {
|
||||
headers.forEach(function (_, key) {
|
||||
if (!asMap[key]) {
|
||||
asMap[key] = true;
|
||||
keys.push(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
iterateHeaders_1.iterateHeaders(headers, function (entry) {
|
||||
var key = entry[0];
|
||||
if (!asMap[key]) {
|
||||
asMap[key] = true;
|
||||
keys.push(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
exports.getHeaderKeys = getHeaderKeys;
|
||||
function splitHeaderValue(str) {
|
||||
var values = [];
|
||||
var commaSpaceValues = str.split(", ");
|
||||
commaSpaceValues.forEach(function (commaSpaceValue) {
|
||||
commaSpaceValue.split(",").forEach(function (commaValue) {
|
||||
values.push(commaValue);
|
||||
});
|
||||
});
|
||||
return values;
|
||||
}
|
||||
exports.splitHeaderValue = splitHeaderValue;
|
||||
|
||||
|
||||
/***/ })
|
||||
/******/ ])));
|
||||
+371
@@ -0,0 +1,371 @@
|
||||
(function webpackUniversalModuleDefinition(root, factory) {
|
||||
if(typeof exports === 'object' && typeof module === 'object')
|
||||
module.exports = factory();
|
||||
else if(typeof define === 'function' && define.amd)
|
||||
define([], factory);
|
||||
else {
|
||||
var a = factory();
|
||||
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
|
||||
}
|
||||
})(this, function() {
|
||||
return /******/ (function(modules) { // webpackBootstrap
|
||||
/******/ // The module cache
|
||||
/******/ var installedModules = {};
|
||||
/******/
|
||||
/******/ // The require function
|
||||
/******/ function __webpack_require__(moduleId) {
|
||||
/******/
|
||||
/******/ // Check if module is in cache
|
||||
/******/ if(installedModules[moduleId]) {
|
||||
/******/ return installedModules[moduleId].exports;
|
||||
/******/ }
|
||||
/******/ // Create a new module (and put it into the cache)
|
||||
/******/ var module = installedModules[moduleId] = {
|
||||
/******/ i: moduleId,
|
||||
/******/ l: false,
|
||||
/******/ exports: {}
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Execute the module function
|
||||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
||||
/******/
|
||||
/******/ // Flag the module as loaded
|
||||
/******/ module.l = true;
|
||||
/******/
|
||||
/******/ // Return the exports of the module
|
||||
/******/ return module.exports;
|
||||
/******/ }
|
||||
/******/
|
||||
/******/
|
||||
/******/ // expose the modules object (__webpack_modules__)
|
||||
/******/ __webpack_require__.m = modules;
|
||||
/******/
|
||||
/******/ // expose the module cache
|
||||
/******/ __webpack_require__.c = installedModules;
|
||||
/******/
|
||||
/******/ // identity function for calling harmony imports with the correct context
|
||||
/******/ __webpack_require__.i = function(value) { return value; };
|
||||
/******/
|
||||
/******/ // define getter function for harmony exports
|
||||
/******/ __webpack_require__.d = function(exports, name, getter) {
|
||||
/******/ if(!__webpack_require__.o(exports, name)) {
|
||||
/******/ Object.defineProperty(exports, name, {
|
||||
/******/ configurable: false,
|
||||
/******/ enumerable: true,
|
||||
/******/ get: getter
|
||||
/******/ });
|
||||
/******/ }
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
||||
/******/ __webpack_require__.n = function(module) {
|
||||
/******/ var getter = module && module.__esModule ?
|
||||
/******/ function getDefault() { return module['default']; } :
|
||||
/******/ function getModuleExports() { return module; };
|
||||
/******/ __webpack_require__.d(getter, 'a', getter);
|
||||
/******/ return getter;
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Object.prototype.hasOwnProperty.call
|
||||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
||||
/******/
|
||||
/******/ // __webpack_public_path__
|
||||
/******/ __webpack_require__.p = "";
|
||||
/******/
|
||||
/******/ // Load entry module and return exports
|
||||
/******/ return __webpack_require__(__webpack_require__.s = 1);
|
||||
/******/ })
|
||||
/************************************************************************/
|
||||
/******/ ([
|
||||
/* 0 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var util_1 = __webpack_require__(3);
|
||||
function isBrowserHeaders(arg) {
|
||||
return typeof arg === "object" && typeof arg.headersMap === "object" && typeof arg.forEach === "function";
|
||||
}
|
||||
var BrowserHeaders = (function () {
|
||||
function BrowserHeaders(init, options) {
|
||||
if (init === void 0) { init = {}; }
|
||||
if (options === void 0) { options = { splitValues: false }; }
|
||||
var _this = this;
|
||||
this.headersMap = {};
|
||||
if (init) {
|
||||
if (typeof Headers !== "undefined" && init instanceof Headers) {
|
||||
var keys = util_1.getHeaderKeys(init);
|
||||
keys.forEach(function (key) {
|
||||
var values = util_1.getHeaderValues(init, key);
|
||||
values.forEach(function (value) {
|
||||
if (options.splitValues) {
|
||||
_this.append(key, util_1.splitHeaderValue(value));
|
||||
}
|
||||
else {
|
||||
_this.append(key, value);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
else if (isBrowserHeaders(init)) {
|
||||
init.forEach(function (key, values) {
|
||||
_this.append(key, values);
|
||||
});
|
||||
}
|
||||
else if (typeof Map !== "undefined" && init instanceof Map) {
|
||||
var asMap = init;
|
||||
asMap.forEach(function (value, key) {
|
||||
_this.append(key, value);
|
||||
});
|
||||
}
|
||||
else if (typeof init === "string") {
|
||||
this.appendFromString(init);
|
||||
}
|
||||
else if (typeof init === "object") {
|
||||
Object.getOwnPropertyNames(init).forEach(function (key) {
|
||||
var asObject = init;
|
||||
var values = asObject[key];
|
||||
if (Array.isArray(values)) {
|
||||
values.forEach(function (value) {
|
||||
_this.append(key, value);
|
||||
});
|
||||
}
|
||||
else {
|
||||
_this.append(key, values);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
BrowserHeaders.prototype.appendFromString = function (str) {
|
||||
var pairs = str.split("\r\n");
|
||||
for (var i = 0; i < pairs.length; i++) {
|
||||
var p = pairs[i];
|
||||
var index = p.indexOf(":");
|
||||
if (index > 0) {
|
||||
var key = p.substring(0, index).trim();
|
||||
var value = p.substring(index + 1).trim();
|
||||
this.append(key, value);
|
||||
}
|
||||
}
|
||||
};
|
||||
BrowserHeaders.prototype.delete = function (key, value) {
|
||||
var normalizedKey = util_1.normalizeName(key);
|
||||
if (value === undefined) {
|
||||
delete this.headersMap[normalizedKey];
|
||||
}
|
||||
else {
|
||||
var existing = this.headersMap[normalizedKey];
|
||||
if (existing) {
|
||||
var index = existing.indexOf(value);
|
||||
if (index >= 0) {
|
||||
existing.splice(index, 1);
|
||||
}
|
||||
if (existing.length === 0) {
|
||||
delete this.headersMap[normalizedKey];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
BrowserHeaders.prototype.append = function (key, value) {
|
||||
var _this = this;
|
||||
var normalizedKey = util_1.normalizeName(key);
|
||||
if (!Array.isArray(this.headersMap[normalizedKey])) {
|
||||
this.headersMap[normalizedKey] = [];
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach(function (arrayValue) {
|
||||
_this.headersMap[normalizedKey].push(util_1.normalizeValue(arrayValue));
|
||||
});
|
||||
}
|
||||
else {
|
||||
this.headersMap[normalizedKey].push(util_1.normalizeValue(value));
|
||||
}
|
||||
};
|
||||
BrowserHeaders.prototype.set = function (key, value) {
|
||||
var normalizedKey = util_1.normalizeName(key);
|
||||
if (Array.isArray(value)) {
|
||||
var normalized_1 = [];
|
||||
value.forEach(function (arrayValue) {
|
||||
normalized_1.push(util_1.normalizeValue(arrayValue));
|
||||
});
|
||||
this.headersMap[normalizedKey] = normalized_1;
|
||||
}
|
||||
else {
|
||||
this.headersMap[normalizedKey] = [util_1.normalizeValue(value)];
|
||||
}
|
||||
};
|
||||
BrowserHeaders.prototype.has = function (key, value) {
|
||||
var keyArray = this.headersMap[util_1.normalizeName(key)];
|
||||
var keyExists = Array.isArray(keyArray);
|
||||
if (!keyExists) {
|
||||
return false;
|
||||
}
|
||||
if (value !== undefined) {
|
||||
var normalizedValue = util_1.normalizeValue(value);
|
||||
return keyArray.indexOf(normalizedValue) >= 0;
|
||||
}
|
||||
else {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
BrowserHeaders.prototype.get = function (key) {
|
||||
var values = this.headersMap[util_1.normalizeName(key)];
|
||||
if (values !== undefined) {
|
||||
return values.concat();
|
||||
}
|
||||
return [];
|
||||
};
|
||||
BrowserHeaders.prototype.forEach = function (callback) {
|
||||
var _this = this;
|
||||
Object.getOwnPropertyNames(this.headersMap)
|
||||
.forEach(function (key) {
|
||||
callback(key, _this.headersMap[key]);
|
||||
}, this);
|
||||
};
|
||||
BrowserHeaders.prototype.toHeaders = function () {
|
||||
if (typeof Headers !== "undefined") {
|
||||
var headers_1 = new Headers();
|
||||
this.forEach(function (key, values) {
|
||||
values.forEach(function (value) {
|
||||
headers_1.append(key, value);
|
||||
});
|
||||
});
|
||||
return headers_1;
|
||||
}
|
||||
else {
|
||||
throw new Error("Headers class is not defined");
|
||||
}
|
||||
};
|
||||
return BrowserHeaders;
|
||||
}());
|
||||
exports.BrowserHeaders = BrowserHeaders;
|
||||
|
||||
|
||||
/***/ }),
|
||||
/* 1 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var BrowserHeaders_1 = __webpack_require__(0);
|
||||
exports.BrowserHeaders = BrowserHeaders_1.BrowserHeaders;
|
||||
|
||||
|
||||
/***/ }),
|
||||
/* 2 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
function iterateHeaders(headers, callback) {
|
||||
var iterator = headers[Symbol.iterator]();
|
||||
var entry = iterator.next();
|
||||
while (!entry.done) {
|
||||
callback(entry.value[0]);
|
||||
entry = iterator.next();
|
||||
}
|
||||
}
|
||||
exports.iterateHeaders = iterateHeaders;
|
||||
function iterateHeadersKeys(headers, callback) {
|
||||
var iterator = headers.keys();
|
||||
var entry = iterator.next();
|
||||
while (!entry.done) {
|
||||
callback(entry.value);
|
||||
entry = iterator.next();
|
||||
}
|
||||
}
|
||||
exports.iterateHeadersKeys = iterateHeadersKeys;
|
||||
|
||||
|
||||
/***/ }),
|
||||
/* 3 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var iterateHeaders_1 = __webpack_require__(2);
|
||||
function normalizeName(name) {
|
||||
if (typeof name !== "string") {
|
||||
name = String(name);
|
||||
}
|
||||
if (/[^a-z0-9\-#$%&'*+.\^_`|~]/i.test(name)) {
|
||||
throw new TypeError("Invalid character in header field name");
|
||||
}
|
||||
return name.toLowerCase();
|
||||
}
|
||||
exports.normalizeName = normalizeName;
|
||||
function normalizeValue(value) {
|
||||
if (typeof value !== "string") {
|
||||
value = String(value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
exports.normalizeValue = normalizeValue;
|
||||
function getHeaderValues(headersAsNative, key) {
|
||||
var headers = toWindowHeaders(headersAsNative);
|
||||
if (headers instanceof Headers && headers.getAll) {
|
||||
return headers.getAll(key);
|
||||
}
|
||||
var getValue = headers.get(key);
|
||||
if (getValue && typeof getValue === "string") {
|
||||
return [getValue];
|
||||
}
|
||||
return getValue;
|
||||
}
|
||||
exports.getHeaderValues = getHeaderValues;
|
||||
function toWindowHeaders(headersAsNative) {
|
||||
return headersAsNative;
|
||||
}
|
||||
function getHeaderKeys(headersAsNative) {
|
||||
var headers = toWindowHeaders(headersAsNative);
|
||||
var asMap = {};
|
||||
var keys = [];
|
||||
if (headers.keys) {
|
||||
iterateHeaders_1.iterateHeadersKeys(headers, function (key) {
|
||||
if (!asMap[key]) {
|
||||
asMap[key] = true;
|
||||
keys.push(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (headers.forEach) {
|
||||
headers.forEach(function (_, key) {
|
||||
if (!asMap[key]) {
|
||||
asMap[key] = true;
|
||||
keys.push(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
iterateHeaders_1.iterateHeaders(headers, function (entry) {
|
||||
var key = entry[0];
|
||||
if (!asMap[key]) {
|
||||
asMap[key] = true;
|
||||
keys.push(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
exports.getHeaderKeys = getHeaderKeys;
|
||||
function splitHeaderValue(str) {
|
||||
var values = [];
|
||||
var commaSpaceValues = str.split(", ");
|
||||
commaSpaceValues.forEach(function (commaSpaceValue) {
|
||||
commaSpaceValue.split(",").forEach(function (commaValue) {
|
||||
values.push(commaValue);
|
||||
});
|
||||
});
|
||||
return values;
|
||||
}
|
||||
exports.splitHeaderValue = splitHeaderValue;
|
||||
|
||||
|
||||
/***/ })
|
||||
/******/ ]);
|
||||
});
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
export interface Map<K, V> {
|
||||
clear(): void;
|
||||
delete(key: K): boolean;
|
||||
forEach(callbackfn: (value: V, index: K, map: Map<K, V>) => void, thisArg?: any): void;
|
||||
get(key: K): V | undefined;
|
||||
has(key: K): boolean;
|
||||
set(key: K, value?: V): this;
|
||||
readonly size: number;
|
||||
}
|
||||
export declare class BrowserHeaders {
|
||||
headersMap: {
|
||||
[key: string]: string[];
|
||||
};
|
||||
constructor(init?: BrowserHeaders.ConstructorArg, options?: {
|
||||
splitValues: boolean;
|
||||
});
|
||||
appendFromString(str: string): void;
|
||||
delete(key: string, value?: string): void;
|
||||
append(key: string, value: string | string[]): void;
|
||||
set(key: string, value: string | string[]): void;
|
||||
has(key: string, value?: string): boolean;
|
||||
get(key: string): string[];
|
||||
forEach(callback: (key: string, values: string[]) => void): void;
|
||||
toHeaders(): Headers;
|
||||
}
|
||||
export declare namespace BrowserHeaders {
|
||||
type HeaderObject = {
|
||||
[key: string]: string | string[];
|
||||
};
|
||||
type HeaderMap = Map<string, string | string[]>;
|
||||
type ConstructorArg = HeaderObject | HeaderMap | BrowserHeaders | Headers | string;
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
export declare interface WindowHeaders {
|
||||
get(key: string): string[];
|
||||
getAll(key: string): string[];
|
||||
has(key: string): boolean;
|
||||
delete(key: string): void;
|
||||
keys(): any;
|
||||
entries(): any;
|
||||
forEach(callback: (value: string, key: string) => void): any;
|
||||
append(key: string, value: string): void;
|
||||
set(key: string, value: string): void;
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export { BrowserHeaders } from "./BrowserHeaders";
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
import { WindowHeaders } from "./WindowHeaders";
|
||||
export declare function iterateHeaders(headers: WindowHeaders, callback: (entry: string[]) => void): void;
|
||||
export declare function iterateHeadersKeys(headers: WindowHeaders, callback: (key: string) => void): void;
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"name": "browser-headers",
|
||||
"version": "0.4.1",
|
||||
"main": "dist/browser-headers.js",
|
||||
"browser": "dist/browser-headers.umd.js",
|
||||
"types": "dist/typings/index.d.ts",
|
||||
"repository": "https://github.com/improbable-eng/js-browser-headers",
|
||||
"license": "Apache-2.0",
|
||||
"keywords": [
|
||||
"headers",
|
||||
"fetch"
|
||||
],
|
||||
"scripts": {
|
||||
"prepublish": "npm run clean && npm run build:lib",
|
||||
"clean": "rm -rf build && rm -rf lib && rm -rf dist",
|
||||
"build:test": "webpack",
|
||||
"build:lib": "npm run clean && webpack",
|
||||
"lint": "tslint -c ./tslint.json ./src/**/*.ts ./test/**/*.ts",
|
||||
"test": "npm run build:test && karma start --single-run",
|
||||
"release": "./release.sh ${npm_package_version}"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^7.0.5",
|
||||
"assert": "^1.4.1",
|
||||
"babel-core": "^6.23.1",
|
||||
"babel-loader": "^6.3.2",
|
||||
"babel-preset-es2015": "^6.13.2",
|
||||
"jasmine-core": "^2.4.1",
|
||||
"karma": "^3.0.0",
|
||||
"karma-browserstack-launcher": "^1.3.0",
|
||||
"karma-jasmine": "^1.0.2",
|
||||
"karma-sourcemap-loader": "^0.3.7",
|
||||
"ts-loader": "^2.0.1",
|
||||
"tslint": "^4.4.2",
|
||||
"typescript": "3.0.1",
|
||||
"webpack": "^2.2.1"
|
||||
},
|
||||
"dependencies": {},
|
||||
"browserify": {
|
||||
"transform": [
|
||||
"babelify"
|
||||
]
|
||||
}
|
||||
}
|
||||
+187
@@ -0,0 +1,187 @@
|
||||
import { normalizeName, normalizeValue, getHeaderValues, getHeaderKeys, splitHeaderValue } from "./util";
|
||||
|
||||
export interface Map<K, V> {
|
||||
clear(): void;
|
||||
delete(key: K): boolean;
|
||||
forEach(callbackfn: (value: V, index: K, map: Map<K, V>) => void, thisArg?: any): void;
|
||||
get(key: K): V | undefined;
|
||||
has(key: K): boolean;
|
||||
set(key: K, value?: V): this;
|
||||
readonly size: number;
|
||||
}
|
||||
|
||||
interface MapConstructor {
|
||||
new (): Map<any, any>;
|
||||
new <K, V>(entries?: [K, V][]): Map<K, V>;
|
||||
readonly prototype: Map<any, any>;
|
||||
}
|
||||
|
||||
declare const Map: MapConstructor;
|
||||
|
||||
// Declare that there is a global property named "Headers" - this might not be present at runtime
|
||||
declare const Headers: any;
|
||||
|
||||
// isBrowserHeaders is used to check if an argument is an instance of BrowserHeaders from another
|
||||
// version of the dependency.
|
||||
function isBrowserHeaders(arg: any): arg is BrowserHeaders {
|
||||
return typeof arg === "object" && typeof arg.headersMap === "object" && typeof arg.forEach === "function"
|
||||
}
|
||||
|
||||
// BrowserHeaders is a wrapper class for Headers
|
||||
export class BrowserHeaders {
|
||||
headersMap: {[key: string]: string[]};
|
||||
|
||||
constructor(init: BrowserHeaders.ConstructorArg = {}, options: {splitValues: boolean} = { splitValues: false } ) {
|
||||
this.headersMap = {};
|
||||
|
||||
if (init) {
|
||||
if (typeof Headers !== "undefined" && init instanceof Headers) {
|
||||
const keys = getHeaderKeys(init as Headers);
|
||||
keys.forEach(key => {
|
||||
const values = getHeaderValues(init as Headers, key);
|
||||
values.forEach(value => {
|
||||
if (options.splitValues) {
|
||||
this.append(key, splitHeaderValue(value));
|
||||
} else {
|
||||
this.append(key, value);
|
||||
}
|
||||
});
|
||||
});
|
||||
} else if (isBrowserHeaders(init)) {
|
||||
init.forEach((key, values) => {
|
||||
this.append(key, values)
|
||||
});
|
||||
} else if (typeof Map !== "undefined" && init instanceof Map) {
|
||||
const asMap = init as BrowserHeaders.HeaderMap;
|
||||
asMap.forEach((value: string|string[], key: string) => {
|
||||
this.append(key, value);
|
||||
});
|
||||
} else if (typeof init === "string") {
|
||||
this.appendFromString(init);
|
||||
} else if (typeof init === "object") {
|
||||
Object.getOwnPropertyNames(init).forEach(key => {
|
||||
const asObject = init as BrowserHeaders.HeaderObject;
|
||||
const values = asObject[key];
|
||||
if (Array.isArray(values)) {
|
||||
values.forEach(value => {
|
||||
this.append(key, value);
|
||||
});
|
||||
} else {
|
||||
this.append(key, values);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
appendFromString(str: string): void {
|
||||
const pairs = str.split("\r\n");
|
||||
for (let i = 0; i < pairs.length; i++) {
|
||||
const p = pairs[i];
|
||||
const index = p.indexOf(":");
|
||||
if (index > 0) {
|
||||
const key = p.substring(0, index).trim();
|
||||
const value = p.substring(index + 1).trim();
|
||||
this.append(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// delete either the key (all values) or a specific value for a key
|
||||
delete(key: string, value?: string): void {
|
||||
const normalizedKey = normalizeName(key);
|
||||
if (value === undefined) {
|
||||
delete this.headersMap[normalizedKey];
|
||||
} else {
|
||||
const existing = this.headersMap[normalizedKey];
|
||||
if (existing) {
|
||||
const index = existing.indexOf(value);
|
||||
if (index >= 0) {
|
||||
existing.splice(index, 1);
|
||||
}
|
||||
if (existing.length === 0) {
|
||||
// The last value was removed - remove the key
|
||||
delete this.headersMap[normalizedKey];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
append(key: string, value: string | string[]): void {
|
||||
const normalizedKey = normalizeName(key);
|
||||
if (!Array.isArray(this.headersMap[normalizedKey])) {
|
||||
this.headersMap[normalizedKey] = [];
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach(arrayValue => {
|
||||
this.headersMap[normalizedKey].push(normalizeValue(arrayValue));
|
||||
});
|
||||
} else {
|
||||
this.headersMap[normalizedKey].push(normalizeValue(value));
|
||||
}
|
||||
}
|
||||
|
||||
// set overrides all existing values for a key
|
||||
set(key: string, value: string | string[]): void {
|
||||
const normalizedKey = normalizeName(key);
|
||||
if (Array.isArray(value)) {
|
||||
const normalized: string[] = [];
|
||||
value.forEach(arrayValue => {
|
||||
normalized.push(normalizeValue(arrayValue));
|
||||
});
|
||||
this.headersMap[normalizedKey] = normalized;
|
||||
} else {
|
||||
this.headersMap[normalizedKey] = [normalizeValue(value)];
|
||||
}
|
||||
}
|
||||
|
||||
has(key: string, value?: string): boolean {
|
||||
const keyArray = this.headersMap[normalizeName(key)];
|
||||
const keyExists = Array.isArray(keyArray);
|
||||
if (!keyExists) {
|
||||
return false;
|
||||
}
|
||||
if (value !== undefined) {
|
||||
const normalizedValue = normalizeValue(value);
|
||||
return keyArray.indexOf(normalizedValue) >= 0;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
get(key: string): string[] {
|
||||
const values = this.headersMap[normalizeName(key)];
|
||||
if (values !== undefined) {
|
||||
return values.concat();
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
// forEach iterates through the keys and calls the callback with the key and *all* of it's values as an array
|
||||
forEach(callback: (key: string, values: string[]) => void): void {
|
||||
Object.getOwnPropertyNames(this.headersMap)
|
||||
.forEach(key => {
|
||||
callback(key, this.headersMap[key]);
|
||||
}, this);
|
||||
}
|
||||
|
||||
toHeaders(): Headers {
|
||||
if (typeof Headers !== "undefined") {
|
||||
const headers = new Headers();
|
||||
this.forEach((key, values) => {
|
||||
values.forEach(value => {
|
||||
headers.append(key, value);
|
||||
});
|
||||
});
|
||||
return headers;
|
||||
} else {
|
||||
throw new Error("Headers class is not defined");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export namespace BrowserHeaders {
|
||||
export type HeaderObject = {[key: string]: string|string[]};
|
||||
export type HeaderMap = Map<string, string|string[]>;
|
||||
export type ConstructorArg = HeaderObject | HeaderMap | BrowserHeaders | Headers | string;
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// Declare the class that *might* be present in the browser
|
||||
export declare interface WindowHeaders {
|
||||
get(key: string): string[]; // in some browsers .get returns a single string
|
||||
getAll(key: string): string[]; // some browsers don't have a .getAll
|
||||
has(key: string): boolean;
|
||||
delete(key: string): void;
|
||||
keys(): any;
|
||||
entries(): any;
|
||||
forEach(callback: (value: string, key: string) => void): any;
|
||||
append(key: string, value: string): void;
|
||||
set(key: string, value: string): void;
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export { BrowserHeaders } from "./BrowserHeaders";
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
import { WindowHeaders } from "./WindowHeaders";
|
||||
|
||||
export function iterateHeaders(headers: WindowHeaders, callback: (entry: string[]) => void): void {
|
||||
const iterator = (headers as any)[Symbol.iterator]();
|
||||
let entry = iterator.next();
|
||||
while (!entry.done) {
|
||||
callback(entry.value[0]);
|
||||
entry = iterator.next();
|
||||
}
|
||||
}
|
||||
|
||||
export function iterateHeadersKeys(headers: WindowHeaders, callback: (key: string) => void): void {
|
||||
const iterator = headers.keys();
|
||||
let entry = iterator.next();
|
||||
while (!entry.done) {
|
||||
callback(entry.value);
|
||||
entry = iterator.next();
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
import { WindowHeaders } from "./WindowHeaders";
|
||||
import { iterateHeaders, iterateHeadersKeys } from "./iterateHeaders";
|
||||
|
||||
// Declare that there is a global property named "Headers" - this might not be present at runtime
|
||||
declare const Headers: any;
|
||||
|
||||
/** @internal */
|
||||
export function normalizeName(name: any): string {
|
||||
if (typeof name !== "string") {
|
||||
name = String(name)
|
||||
}
|
||||
if (/[^a-z0-9\-#$%&'*+.\^_`|~]/i.test(name)) {
|
||||
throw new TypeError("Invalid character in header field name")
|
||||
}
|
||||
return name.toLowerCase()
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function normalizeValue(value: any): string {
|
||||
if (typeof value !== "string") {
|
||||
value = String(value)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
// getHeadersValues abstracts the difference between get() and getAll() between browsers and always returns an array
|
||||
/** @internal */
|
||||
export function getHeaderValues(headersAsNative: Headers, key: string): string[] {
|
||||
const headers = toWindowHeaders(headersAsNative);
|
||||
if (headers instanceof Headers && headers.getAll) {
|
||||
// If the headers instance has a getAll function then it will return an array
|
||||
return headers.getAll(key);
|
||||
}
|
||||
|
||||
// There is no getAll() function so get *should* return an array
|
||||
const getValue = headers.get(key);
|
||||
if (getValue && typeof getValue === "string") {
|
||||
// some .get() implementations return a string even though they don't have a .getAll() - notably Microsoft Edge
|
||||
return [getValue];
|
||||
}
|
||||
return getValue;
|
||||
}
|
||||
|
||||
// toWindowHeaders casts the native browser class to an interface that includes functions of different browser implementations
|
||||
function toWindowHeaders(headersAsNative: Headers): WindowHeaders {
|
||||
return headersAsNative as any as WindowHeaders;
|
||||
}
|
||||
|
||||
// getHeaderKeys returns an array of keys in a headers instance
|
||||
/** @internal */
|
||||
export function getHeaderKeys(headersAsNative: Headers): string[] {
|
||||
const headers = toWindowHeaders(headersAsNative);
|
||||
const asMap: {[key: string]: boolean} = {};
|
||||
const keys: string[] = [];
|
||||
|
||||
if (headers.keys) {
|
||||
iterateHeadersKeys(headers, key => {
|
||||
if (!asMap[key]) {
|
||||
// Only add the key if it hasn't been added already
|
||||
asMap[key] = true;
|
||||
keys.push(key);
|
||||
}
|
||||
});
|
||||
} else if (headers.forEach) {
|
||||
headers.forEach((_, key) => {
|
||||
if (!asMap[key]) {
|
||||
// Only add the key if it hasn't been added already
|
||||
asMap[key] = true;
|
||||
keys.push(key);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// If keys() and forEach() aren't available then fallback to iterating through headers
|
||||
iterateHeaders(headers, (entry: string[]) => {
|
||||
const key = entry[0];
|
||||
if (!asMap[key]) {
|
||||
// Only add the key if it hasn't been added already
|
||||
asMap[key] = true;
|
||||
keys.push(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function splitHeaderValue(str: string) {
|
||||
const values: string[] = [];
|
||||
const commaSpaceValues = str.split(", ");
|
||||
commaSpaceValues.forEach(commaSpaceValue => {
|
||||
commaSpaceValue.split(",").forEach(commaValue => {
|
||||
values.push(commaValue);
|
||||
});
|
||||
});
|
||||
return values;
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"alwaysStrict": true,
|
||||
"sourceMap": true,
|
||||
"declaration": true,
|
||||
"declarationDir": "typings",
|
||||
"target": "es5",
|
||||
"removeComments": true,
|
||||
"noImplicitReturns": true,
|
||||
"noImplicitAny": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"strictNullChecks": true,
|
||||
"stripInternal": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"outDir": "build",
|
||||
"noEmitOnError": true
|
||||
},
|
||||
"types": [],
|
||||
"include": [
|
||||
"src"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
{
|
||||
"rules": {
|
||||
"class-name": true,
|
||||
"comment-format": [true, "check-space"],
|
||||
"indent": [true, "spaces"],
|
||||
"no-duplicate-variable": true,
|
||||
"no-eval": true,
|
||||
"no-internal-module": true,
|
||||
"no-trailing-whitespace": true,
|
||||
"no-var-keyword": true,
|
||||
"one-line": [true, "check-open-brace", "check-whitespace"],
|
||||
"quotemark": [true, "double"],
|
||||
"semicolon": false,
|
||||
"triple-equals": [true, "allow-null-check"],
|
||||
"typedef-whitespace": [true, {
|
||||
"call-signature": "nospace",
|
||||
"index-signature": "nospace",
|
||||
"parameter": "nospace",
|
||||
"property-declaration": "nospace",
|
||||
"variable-declaration": "nospace"
|
||||
}],
|
||||
"variable-name": [true, "ban-keywords"],
|
||||
"whitespace": [true,
|
||||
"check-branch",
|
||||
"check-decl",
|
||||
"check-operator",
|
||||
"check-separator",
|
||||
"check-type"
|
||||
]
|
||||
},
|
||||
"jsRules": {
|
||||
"indent": [true, "spaces"],
|
||||
"no-duplicate-variable": true,
|
||||
"no-eval": true,
|
||||
"no-trailing-whitespace": true,
|
||||
"one-line": [true, "check-open-brace", "check-whitespace"],
|
||||
"quotemark": [true, "double"],
|
||||
"semicolon": false,
|
||||
"triple-equals": [true, "allow-null-check"],
|
||||
"variable-name": [true, "ban-keywords"],
|
||||
"whitespace": [true,
|
||||
"check-branch",
|
||||
"check-decl",
|
||||
"check-operator",
|
||||
"check-separator",
|
||||
"check-type"
|
||||
]
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
const path = require('path');
|
||||
const packageJson = require("./package.json");
|
||||
|
||||
const LIB_BASE_CONFIG = {
|
||||
entry: "./src/index.ts",
|
||||
module: {
|
||||
rules: [{
|
||||
test: /\.ts?$/,
|
||||
use: 'ts-loader',
|
||||
exclude: /node_modules/
|
||||
}]
|
||||
},
|
||||
resolve: {
|
||||
extensions: ['.ts', '.js']
|
||||
},
|
||||
};
|
||||
const DIST_DIR = path.resolve(__dirname, 'dist');
|
||||
|
||||
module.exports = [{
|
||||
name: 'lib-commonjs',
|
||||
...LIB_BASE_CONFIG,
|
||||
output: {
|
||||
filename: `${packageJson.name}.js`,
|
||||
path: DIST_DIR,
|
||||
libraryTarget: 'commonjs',
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'lib-umd',
|
||||
...LIB_BASE_CONFIG,
|
||||
output: {
|
||||
filename: `${packageJson.name}.umd.js`,
|
||||
path: DIST_DIR,
|
||||
libraryTarget: 'umd',
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'tests',
|
||||
entry: "./test/BrowserHeaders.spec.ts",
|
||||
output: {
|
||||
path: path.resolve(__dirname, 'test', 'build'),
|
||||
filename: 'integration-tests.js',
|
||||
},
|
||||
devtool: 'source-map',
|
||||
module: {
|
||||
rules: [{
|
||||
test: /\.js$/,
|
||||
include: /src|test|node_modules/,
|
||||
loader: 'babel-loader?cacheDirectory'
|
||||
},
|
||||
{
|
||||
test: /\.ts$/,
|
||||
include: /src|test|node_modules/,
|
||||
loader: "babel-loader?cacheDirectory!ts-loader"
|
||||
}
|
||||
]
|
||||
},
|
||||
plugins: [],
|
||||
resolve: {
|
||||
extensions: [".ts", ".js"]
|
||||
}
|
||||
}
|
||||
];
|
||||
Reference in New Issue
Block a user