@foxify/http
Version:
Foxify HTTP module
174 lines • 6.61 kB
JavaScript
"use strict";
/**
* Copyright (c) 2014-2018, Project contributors.
* Copyright (c) 2015-2016, Mark Bradshaw
* Copyright (c) 2014, Walmart.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of any contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* This module is modified and optimized for Foxify specifically
*/
const mime = __importStar(require("mime-types"));
const negotiator_1 = __importDefault(require("negotiator"));
/**
* Convert extnames to mime.
*
* @param {String} type
* @return {String}
* @private
*/
const extToMime = (type) => (type.includes("/") ? type : mime.lookup(type));
class Accepts {
headers;
negotiator;
constructor(req) {
this.headers = req.headers;
this.negotiator = new negotiator_1.default(req);
}
/**
* Return accepted charsets or best fit based on `charsets`.
*
* Given `Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5`
* an array sorted by quality is returned:
*
* ["utf-8", "utf-7", "iso-8859-1"]
*/
charsets(charsets = []) {
// No charsets, return all requested charsets
if (charsets.length === 0)
return this.negotiator.charsets();
return this.negotiator.charsets(charsets)[0] || false;
}
/**
* Return accepted encodings or best fit based on `encodings`.
*
* Given `Accept-Encoding: gzip, deflate`
* an array sorted by quality is returned:
*
* ["gzip", "deflate"]
*/
encodings(encodings = []) {
// No encodings, return all requested encodings
if (encodings.length === 0)
return this.negotiator.encodings();
return this.negotiator.encodings(encodings)[0] || false;
}
/**
* Return accepted languages or best fit based on `langs`.
*
* Given `Accept-Language: en;q=0.8, es, pt`
* an array sorted by quality is returned:
*
* ["es", "pt", "en"]
*
* @param {String|Array} languages...
* @return {Array|String}
* @public
*/
languages(languages = []) {
// No languages, return all requested languages
if (languages.length === 0)
return this.negotiator.languages();
return this.negotiator.languages(languages)[0] || false;
}
/**
* Check if the given `type(s)` is acceptable, returning
* the best match when true, otherwise `undefined`, in which
* case you should respond with 406 "Not Acceptable".
*
* The `type` value may be a single mime type string
* such as "application/json", the extension name
* such as "json" or an array `["json", "html", "text/plain"]`. When a list
* or array is given the _best_ match, if any is returned.
*
* @example
* // Accept: text/html
* this.types("html");
* // => "html"
*
* @example
* // Accept: text/*, application/json
* this.types("html");
* // => "html"
* this.types("text/html");
* // => "text/html"
* this.types("json", "text");
* // => "json"
* this.types("application/json");
* // => "application/json"
*
* @example
* // Accept: text/*, application/json
* this.types("image/png");
* this.types("png");
* // => undefined
*
* @example
* // Accept: text/*;q=.5, application/json
* this.types(["html", "json"]);
* this.types("html", "json");
* // => "json"
*/
types(types = []) {
// No types, return all requested types
if (types.length === 0)
return this.negotiator.mediaTypes();
// No accept header, return first given type
if (!this.headers.accept)
return types[0];
const mimes = types.map(extToMime);
const first = this.negotiator.mediaTypes(mimes.filter(Boolean))[0];
return first ? types[mimes.indexOf(first)] : false;
}
}
exports.default = Accepts;
//# sourceMappingURL=accepts.js.map