Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

i18next-http-middleware

Package Overview
Dependencies
Maintainers
2
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

i18next-http-middleware - npm Package Compare versions

Comparing version 3.2.1 to 3.2.2

40

cjs/httpFunctions.js

@@ -7,3 +7,2 @@ "use strict";

exports.setUrl = exports.setStatus = exports.setHeader = exports.setContentType = exports.send = exports.getUrl = exports.getSession = exports.getQuery = exports.getPath = exports.getParams = exports.getOriginalUrl = exports.getHeaders = exports.getHeader = exports.getCookies = exports.getBody = exports.extendOptionsWithDefaults = void 0;
var getPath = function getPath(req) {

@@ -15,5 +14,3 @@ if (req.path) return req.path;

};
exports.getPath = getPath;
var getUrl = function getUrl(req) {

@@ -25,5 +22,3 @@ if (req.url && req.url.href) return req.url.href;

};
exports.getUrl = getUrl;
var setUrl = function setUrl(req, url) {

@@ -34,8 +29,5 @@ if (req.url) {

}
console.log('no possibility found to get url');
};
exports.setUrl = setUrl;
var getOriginalUrl = function getOriginalUrl(req) {

@@ -46,5 +38,3 @@ if (req.originalUrl) return req.originalUrl;

};
exports.getOriginalUrl = getOriginalUrl;
var getQuery = function getQuery(req) {

@@ -54,3 +44,2 @@ if (req.query && typeof req.query.entries === 'function' && typeof Object.fromEntries === 'function' && typeof req.query[Symbol.iterator] === 'function') {

}
if (req.query) return req.query;

@@ -65,5 +54,3 @@ if (req.searchParams) return req.searchParams;

};
exports.getQuery = getQuery;
var getParams = function getParams(req) {

@@ -76,5 +63,3 @@ if (req.params) return req.params;

};
exports.getParams = getParams;
var getHeaders = function getHeaders(req) {

@@ -84,8 +69,5 @@ if (req.headers) return req.headers;

};
exports.getHeaders = getHeaders;
var getCookies = function getCookies(req) {
if (req.cookies) return req.cookies;
if (getHeaders(req)) {

@@ -100,8 +82,5 @@ var list = {};

}
console.log('no possibility found to get cookies');
};
exports.getCookies = getCookies;
var getBody = function getBody(req) {

@@ -111,3 +90,2 @@ if (req.ctx && typeof req.ctx.body === 'function') {

}
if (req.ctx && req.ctx.body) return req.ctx.body;

@@ -120,5 +98,3 @@ if (req.json) return req.json;

};
exports.getBody = getBody;
var getHeader = function getHeader(res, name) {

@@ -131,5 +107,3 @@ if (res.getHeader) return res.getHeader(name);

};
exports.getHeader = getHeader;
var setHeader = function setHeader(res, name, value) {

@@ -139,16 +113,11 @@ if (res._headerSent || res.headersSent) return;

if (typeof res.header === 'function') return res.header(name, value);
if (res.responseHeaders && typeof res.responseHeaders.set === 'function') {
return res.responseHeaders.set(name, value);
}
if (res.headers && typeof res.headers.set === 'function') {
return res.headers.set(name, value);
}
console.log('no possibility found to set header');
};
exports.setHeader = setHeader;
var setContentType = function setContentType(res, type) {

@@ -159,5 +128,3 @@ if (typeof res.contentType === 'function') return res.contentType(type);

};
exports.setContentType = setContentType;
var setStatus = function setStatus(res, code) {

@@ -168,5 +135,3 @@ if (typeof res.status === 'function') return res.status(code);

};
exports.setStatus = setStatus;
var send = function send(res, body) {

@@ -176,5 +141,3 @@ if (typeof res.send === 'function') return res.send(body);

};
exports.send = send;
var getSession = function getSession(req) {

@@ -185,5 +148,3 @@ if (req.session) return req.session;

};
exports.getSession = getSession;
var extendOptionsWithDefaults = function extendOptionsWithDefaults() {

@@ -208,3 +169,2 @@ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

};
exports.extendOptionsWithDefaults = extendOptionsWithDefaults;

69

cjs/index.js
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {

@@ -16,24 +15,13 @@ value: true

exports.plugin = plugin;
var utils = _interopRequireWildcard(require("./utils.js"));
var _LanguageDetector = _interopRequireDefault(require("./LanguageDetector.js"));
var _httpFunctions = require("./httpFunctions.js");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var LanguageDetector = _LanguageDetector.default;
exports.LanguageDetector = LanguageDetector;
var checkForCombinedReqRes = function checkForCombinedReqRes(req, res, next) {

@@ -51,3 +39,2 @@ if (!res) {

}
return {

@@ -59,3 +46,2 @@ req: req,

};
function handle(i18next) {

@@ -66,6 +52,5 @@ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

var _checkForCombinedReqR = checkForCombinedReqRes(rq, rs, n),
req = _checkForCombinedReqR.req,
res = _checkForCombinedReqR.res,
next = _checkForCombinedReqR.next;
req = _checkForCombinedReqR.req,
res = _checkForCombinedReqR.res,
next = _checkForCombinedReqR.next;
if (typeof options.ignoreRoutes === 'function') {

@@ -77,3 +62,2 @@ if (options.ignoreRoutes(req, res, options, i18next)) {

var ignores = options.ignoreRoutes instanceof Array && options.ignoreRoutes || [];
for (var i = 0; i < ignores.length; i++) {

@@ -83,3 +67,2 @@ if (options.getPath(req).indexOf(ignores[i]) > -1) return next();

}
var i18n = i18next.cloneInstance({

@@ -91,3 +74,2 @@ initImmediate: false

if (options.attachLocals) res.locals = res.locals || {};
if (res.locals) {

@@ -97,9 +79,6 @@ res.locals.language = lng;

}
if (lng && options.getHeader(res, 'Content-Language') !== lng) {
options.setHeader(res, 'Content-Language', lng);
}
req.languages = i18next.services.languageUtils.toResolveHierarchy(lng);
if (i18next.services.languageDetector) {

@@ -110,3 +89,2 @@ i18next.services.languageDetector.cacheUserLanguage(req, res, lng);

var lng = req.lng;
if (!lng && i18next.services.languageDetector) {

@@ -117,20 +95,18 @@ lng = i18next.services.languageDetector.detect(req, res);

req.language = req.locale = req.lng = lng;
if (lng && options.getHeader(res, 'Content-Language') !== lng) {
options.setHeader(res, 'Content-Language', lng);
}
req.languages = i18next.services.languageUtils.toResolveHierarchy(lng);
req.languages = i18next.services.languageUtils.toResolveHierarchy(lng);
i18n.changeLanguage(lng || i18next.options.fallbackLng[0]);
if (req.i18nextLookupName === 'path' && options.removeLngFromUrl) {
options.setUrl(req, utils.removeLngFromUrl(options.getUrl(req), i18next.services.languageDetector.options.lookupFromPathIndex));
}
var t = i18n.t.bind(i18n);
var exists = i18n.exists.bind(i18n);
req.i18n = i18n;
req.t = t;
if (options.attachLocals) res.locals = res.locals || {};
if (res.locals) {

@@ -143,7 +119,5 @@ res.locals.t = t;

}
if (i18next.services.languageDetector) {
i18next.services.languageDetector.cacheUserLanguage(req, res, lng);
}
if (!req.lng) return next();

@@ -155,3 +129,2 @@ i18next.loadLanguages(req.lng, function () {

}
function plugin(instance, options, next) {

@@ -165,3 +138,2 @@ options.attachLocals = true;

}
var hapiPlugin = {

@@ -183,3 +155,2 @@ name: 'i18next-http-middleware',

plugin[Symbol.for('skip-override')] = true;
function getResourcesHandler(i18next) {

@@ -191,5 +162,4 @@ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

var _checkForCombinedReqR2 = checkForCombinedReqRes(rq, rs),
req = _checkForCombinedReqR2.req,
res = _checkForCombinedReqR2.res;
req = _checkForCombinedReqR2.req,
res = _checkForCombinedReqR2.res;
if (!i18next.services.backendConnector) {

@@ -199,6 +169,4 @@ options.setStatus(res, 404);

}
var resources = {};
options.setContentType(res, 'application/json');
if (options.cache !== undefined ? options.cache : typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'production') {

@@ -211,5 +179,5 @@ options.setHeader(res, 'Cache-Control', 'public, max-age=' + maxAge);

}
var languages = options.getQuery(req)[options.lngParam || 'lng'] ? options.getQuery(req)[options.lngParam || 'lng'].split(' ') : [];
var namespaces = options.getQuery(req)[options.nsParam || 'ns'] ? options.getQuery(req)[options.nsParam || 'ns'].split(' ') : [];
namespaces.forEach(function (ns) {

@@ -230,3 +198,2 @@ if (i18next.options.ns && i18next.options.ns.indexOf(ns) < 0) {

}
function missingKeyHandler(i18next) {

@@ -237,8 +204,6 @@ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

var _checkForCombinedReqR3 = checkForCombinedReqRes(rq, rs),
req = _checkForCombinedReqR3.req,
res = _checkForCombinedReqR3.res;
req = _checkForCombinedReqR3.req,
res = _checkForCombinedReqR3.res;
var lng = options.getParams(req)[options.lngParam || 'lng'];
var ns = options.getParams(req)[options.nsParam || 'ns'];
if (!i18next.services.backendConnector) {

@@ -248,8 +213,5 @@ options.setStatus(res, 404);

}
var body = options.getBody(req);
if (typeof body === 'function') {
var promise = body();
if (promise && typeof promise.then === 'function') {

@@ -261,3 +223,2 @@ return new Promise(function (resolve) {

}
resolve(options.send(res, 'ok'));

@@ -268,11 +229,8 @@ });

}
for (var m in body) {
i18next.services.backendConnector.saveMissing([lng], ns, m, body[m]);
}
return options.send(res, 'ok');
};
}
function addRoute(i18next, route, lngs, app, verb, fc) {

@@ -285,10 +243,7 @@ if (typeof verb === 'function') {

var callbacks = [fc].concat(Array.prototype.slice.call(arguments, 6));
for (var i = 0, li = lngs.length; i < li; i++) {
var parts = String(route).split('/');
var locRoute = [];
for (var y = 0, ly = parts.length; y < ly; y++) {
var part = parts[y];
if (part === ':lng') {

@@ -304,3 +259,2 @@ locRoute.push(lngs[i]);

}
var routes = [locRoute.join('/')];

@@ -310,3 +264,2 @@ app[verb || 'get'].apply(app, routes.concat(callbacks));

}
var _default = {

@@ -313,0 +266,0 @@ plugin: plugin,

"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {

@@ -9,29 +8,15 @@ value: true

exports.default = void 0;
var utils = _interopRequireWildcard(require("./utils.js"));
var _cookie = _interopRequireDefault(require("./languageLookups/cookie.js"));
var _querystring = _interopRequireDefault(require("./languageLookups/querystring.js"));
var _path = _interopRequireDefault(require("./languageLookups/path.js"));
var _header = _interopRequireDefault(require("./languageLookups/header.js"));
var _session = _interopRequireDefault(require("./languageLookups/session.js"));
var _httpFunctions = require("./httpFunctions.js");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function getDefaults() {

@@ -49,3 +34,2 @@ return (0, _httpFunctions.extendOptionsWithDefaults)({

}
var LanguageDetector = function () {

@@ -55,5 +39,3 @@ function LanguageDetector(services) {

var allOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
_classCallCheck(this, LanguageDetector);
this.type = 'languageDetector';

@@ -63,3 +45,2 @@ this.detectors = {};

}
_createClass(LanguageDetector, [{

@@ -88,3 +69,2 @@ key: "init",

var _this = this;
if (arguments.length < 2) return;

@@ -95,5 +75,3 @@ if (!detectionOrder) detectionOrder = this.options.order;

if (found || !_this.detectors[detectorName]) return;
var detections = _this.detectors[detectorName].lookup(req, res, _this.options);
if (!detections) return;

@@ -104,6 +82,4 @@ if (!Array.isArray(detections)) detections = [detections];

});
if (_this.services.languageUtils.getBestMatchFromCodes) {
found = _this.services.languageUtils.getBestMatchFromCodes(detections);
if (found) {

@@ -118,3 +94,2 @@ if (_this.options.ignoreCase) {

}
if (found) req.i18nextLookupName = detectorName;

@@ -131,3 +106,2 @@ } else {

if (!fallbacks) fallbacks = [];
if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {

@@ -139,3 +113,2 @@ found = fallbacks[0];

}
;

@@ -148,3 +121,2 @@ return found;

var _this2 = this;
if (arguments.length < 3) return;

@@ -156,3 +128,2 @@ if (!caches) caches = this.options.caches;

_this2.detectors[cacheName].cacheUserLanguage(req, res, lng, _this2.options);
res.cachedUserLanguage = lng;

@@ -163,6 +134,4 @@ }

}]);
return LanguageDetector;
}();
LanguageDetector.type = 'languageDetector';

@@ -169,0 +138,0 @@ var _default = LanguageDetector;

@@ -8,3 +8,2 @@ "use strict";

var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
var serializeCookie = function serializeCookie(name, val, options) {

@@ -15,3 +14,2 @@ var opt = options || {};

var str = name + '=' + value;
if (opt.maxAge > 0) {

@@ -22,3 +20,2 @@ var maxAge = opt.maxAge - 0;

}
if (opt.domain) {

@@ -28,6 +25,4 @@ if (!fieldContentRegExp.test(opt.domain)) {

}
str += '; Domain=' + opt.domain;
}
if (opt.path) {

@@ -37,6 +32,4 @@ if (!fieldContentRegExp.test(opt.path)) {

}
str += '; Path=' + opt.path;
}
if (opt.expires) {

@@ -46,12 +39,8 @@ if (typeof opt.expires.toUTCString !== 'function') {

}
str += '; Expires=' + opt.expires.toUTCString();
}
if (opt.httpOnly) str += '; HttpOnly';
if (opt.secure) str += '; Secure';
if (opt.sameSite) {
var sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite;
switch (sameSite) {

@@ -61,15 +50,11 @@ case true:

break;
case 'lax':
str += '; SameSite=Lax';
break;
case 'strict':
str += '; SameSite=Strict';
break;
case 'none':
str += '; SameSite=None';
break;
default:

@@ -79,6 +64,4 @@ throw new TypeError('option sameSite is invalid');

}
return str;
};
var _default = {

@@ -88,3 +71,2 @@ name: 'cookie',

var found;
if (options.lookupCookie && typeof req !== 'undefined') {

@@ -94,3 +76,2 @@ var cookies = options.getCookies(req);

}
return found;

@@ -100,6 +81,4 @@ },

var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
if (options.lookupCookie && req !== 'undefined') {
var expirationDate = options.cookieExpirationDate;
if (!expirationDate) {

@@ -109,3 +88,2 @@ expirationDate = new Date();

}
var cookieOptions = {

@@ -112,0 +90,0 @@ expires: expirationDate,

@@ -12,3 +12,2 @@ "use strict";

var found;
if (typeof req !== 'undefined') {

@@ -19,6 +18,4 @@ var headers = options.getHeaders(req);

var acceptLanguage = options.lookupHeader ? headers[options.lookupHeader] : headers['accept-language'];
if (acceptLanguage) {
var lookupRegex = /(([a-z]{2})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi;
var lookupRegex = /(([a-z]{2,3})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi;
if (acceptLanguage.indexOf('-') > 0) {

@@ -28,5 +25,4 @@ var foundSpecialCase = specialCases.find(function (s) {

});
if (foundSpecialCase) lookupRegex = /(([a-z]{2})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi;
if (foundSpecialCase) lookupRegex = /(([a-z]{2,3})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi;
}
var lngs = [];

@@ -36,6 +32,4 @@ var i;

var rgx = options.lookupHeaderRegex || lookupRegex;
do {
m = rgx.exec(acceptLanguage);
if (m) {

@@ -45,3 +39,2 @@ var lng = m[1];

var q = Number(weight);
if (lng && !isNaN(q)) {

@@ -55,15 +48,11 @@ lngs.push({

} while (m);
lngs.sort(function (a, b) {
return b.q - a.q;
});
for (i = 0; i < lngs.length; i++) {
locales.push(lngs[i].lng);
}
if (locales.length) found = locales;
}
}
return found;

@@ -70,0 +59,0 @@ }

@@ -11,19 +11,14 @@ "use strict";

var found;
if (req === undefined) {
return found;
}
if (options.lookupPath !== undefined && req.params) {
found = options.getParams(req)[options.lookupPath];
}
if (!found && typeof options.lookupFromPathIndex === 'number' && options.getOriginalUrl(req)) {
var path = options.getOriginalUrl(req).split('?')[0];
var parts = path.split('/');
if (parts[0] === '') {
parts.shift();
}
if (parts.length > options.lookupFromPathIndex) {

@@ -33,3 +28,2 @@ found = parts[options.lookupFromPathIndex];

}
return found;

@@ -36,0 +30,0 @@ }

@@ -11,3 +11,2 @@ "use strict";

var found;
if (options.lookupQuerystring !== undefined && typeof req !== 'undefined') {

@@ -17,6 +16,4 @@ if (options.getQuery(req)) {

}
if (!found && options.getUrl(req) && options.getUrl(req).indexOf('?')) {
var lastPartOfUri = options.getUrl(req).substring(options.getUrl(req).indexOf('?'));
if (typeof URLSearchParams !== 'undefined') {

@@ -27,6 +24,4 @@ var urlParams = new URLSearchParams(lastPartOfUri);

var indexOfQsStart = lastPartOfUri.indexOf("".concat(options.lookupQuerystring, "="));
if (indexOfQsStart > -1) {
var restOfUri = lastPartOfUri.substring(options.lookupQuerystring.length + 2);
if (restOfUri.indexOf('&') < 0) {

@@ -41,3 +36,2 @@ found = restOfUri;

}
return found;

@@ -44,0 +38,0 @@ }

@@ -7,5 +7,3 @@ "use strict";

exports.default = void 0;
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
var _default = {

@@ -15,7 +13,5 @@ name: 'session',

var found;
if (options.lookupSession !== undefined && _typeof(req) && options.getSession(req)) {
found = options.getSession(req)[options.lookupSession];
}
return found;

@@ -25,3 +21,2 @@ },

var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
if (options.lookupSession && req && options.getSession(req)) {

@@ -28,0 +23,0 @@ options.getSession(req)[options.lookupSession] = lng;

@@ -10,3 +10,2 @@ "use strict";

exports.setPath = setPath;
function setPath(object, path, newValue) {

@@ -16,6 +15,4 @@ var stack;

if (typeof path === 'string') stack = path.split('.');
while (stack.length > 1) {
var _key = stack.shift();
if (_key.indexOf('###') > -1) _key = _key.replace(/###/g, '.');

@@ -25,3 +22,2 @@ if (!object[_key]) object[_key] = {};

}
var key = stack.shift();

@@ -31,7 +27,5 @@ if (key.indexOf('###') > -1) key = key.replace(/###/g, '.');

}
var arr = [];
var each = arr.forEach;
var slice = arr.slice;
function defaults(obj) {

@@ -47,3 +41,2 @@ each.call(slice.call(arguments, 1), function (source) {

}
function extend(obj) {

@@ -59,7 +52,5 @@ each.call(slice.call(arguments, 1), function (source) {

}
function removeLngFromUrl(url, lookupFromPathIndex) {
var first = '';
var pos = lookupFromPathIndex;
if (url[0] === '/') {

@@ -66,0 +57,0 @@ pos++;

@@ -18,3 +18,2 @@ export var getPath = function getPath(req) {

}
console.log('no possibility found to get url');

@@ -31,3 +30,2 @@ };

}
if (req.query) return req.query;

@@ -55,3 +53,2 @@ if (req.searchParams) return req.searchParams;

if (req.cookies) return req.cookies;
if (getHeaders(req)) {

@@ -66,3 +63,2 @@ var list = {};

}
console.log('no possibility found to get cookies');

@@ -74,3 +70,2 @@ };

}
if (req.ctx && req.ctx.body) return req.ctx.body;

@@ -94,11 +89,8 @@ if (req.json) return req.json;

if (typeof res.header === 'function') return res.header(name, value);
if (res.responseHeaders && typeof res.responseHeaders.set === 'function') {
return res.responseHeaders.set(name, value);
}
if (res.headers && typeof res.headers.set === 'function') {
return res.headers.set(name, value);
}
console.log('no possibility found to set header');

@@ -105,0 +97,0 @@ };

55

esm/index.js
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import * as utils from './utils.js';

@@ -11,3 +8,2 @@ import LD from './LanguageDetector.js';

export var LanguageDetector = LD;
var checkForCombinedReqRes = function checkForCombinedReqRes(req, res, next) {

@@ -25,3 +21,2 @@ if (!res) {

}
return {

@@ -33,3 +28,2 @@ req: req,

};
export function handle(i18next) {

@@ -40,6 +34,5 @@ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

var _checkForCombinedReqR = checkForCombinedReqRes(rq, rs, n),
req = _checkForCombinedReqR.req,
res = _checkForCombinedReqR.res,
next = _checkForCombinedReqR.next;
req = _checkForCombinedReqR.req,
res = _checkForCombinedReqR.res,
next = _checkForCombinedReqR.next;
if (typeof options.ignoreRoutes === 'function') {

@@ -51,3 +44,2 @@ if (options.ignoreRoutes(req, res, options, i18next)) {

var ignores = options.ignoreRoutes instanceof Array && options.ignoreRoutes || [];
for (var i = 0; i < ignores.length; i++) {

@@ -57,3 +49,2 @@ if (options.getPath(req).indexOf(ignores[i]) > -1) return next();

}
var i18n = i18next.cloneInstance({

@@ -65,3 +56,2 @@ initImmediate: false

if (options.attachLocals) res.locals = res.locals || {};
if (res.locals) {

@@ -71,9 +61,6 @@ res.locals.language = lng;

}
if (lng && options.getHeader(res, 'Content-Language') !== lng) {
options.setHeader(res, 'Content-Language', lng);
}
req.languages = i18next.services.languageUtils.toResolveHierarchy(lng);
if (i18next.services.languageDetector) {

@@ -84,3 +71,2 @@ i18next.services.languageDetector.cacheUserLanguage(req, res, lng);

var lng = req.lng;
if (!lng && i18next.services.languageDetector) {

@@ -91,20 +77,18 @@ lng = i18next.services.languageDetector.detect(req, res);

req.language = req.locale = req.lng = lng;
if (lng && options.getHeader(res, 'Content-Language') !== lng) {
options.setHeader(res, 'Content-Language', lng);
}
req.languages = i18next.services.languageUtils.toResolveHierarchy(lng);
req.languages = i18next.services.languageUtils.toResolveHierarchy(lng);
i18n.changeLanguage(lng || i18next.options.fallbackLng[0]);
if (req.i18nextLookupName === 'path' && options.removeLngFromUrl) {
options.setUrl(req, utils.removeLngFromUrl(options.getUrl(req), i18next.services.languageDetector.options.lookupFromPathIndex));
}
var t = i18n.t.bind(i18n);
var exists = i18n.exists.bind(i18n);
req.i18n = i18n;
req.t = t;
if (options.attachLocals) res.locals = res.locals || {};
if (res.locals) {

@@ -117,7 +101,5 @@ res.locals.t = t;

}
if (i18next.services.languageDetector) {
i18next.services.languageDetector.cacheUserLanguage(req, res, lng);
}
if (!req.lng) return next();

@@ -158,5 +140,4 @@ i18next.loadLanguages(req.lng, function () {

var _checkForCombinedReqR2 = checkForCombinedReqRes(rq, rs),
req = _checkForCombinedReqR2.req,
res = _checkForCombinedReqR2.res;
req = _checkForCombinedReqR2.req,
res = _checkForCombinedReqR2.res;
if (!i18next.services.backendConnector) {

@@ -166,6 +147,4 @@ options.setStatus(res, 404);

}
var resources = {};
options.setContentType(res, 'application/json');
if (options.cache !== undefined ? options.cache : typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'production') {

@@ -178,5 +157,5 @@ options.setHeader(res, 'Cache-Control', 'public, max-age=' + maxAge);

}
var languages = options.getQuery(req)[options.lngParam || 'lng'] ? options.getQuery(req)[options.lngParam || 'lng'].split(' ') : [];
var namespaces = options.getQuery(req)[options.nsParam || 'ns'] ? options.getQuery(req)[options.nsParam || 'ns'].split(' ') : [];
namespaces.forEach(function (ns) {

@@ -202,8 +181,6 @@ if (i18next.options.ns && i18next.options.ns.indexOf(ns) < 0) {

var _checkForCombinedReqR3 = checkForCombinedReqRes(rq, rs),
req = _checkForCombinedReqR3.req,
res = _checkForCombinedReqR3.res;
req = _checkForCombinedReqR3.req,
res = _checkForCombinedReqR3.res;
var lng = options.getParams(req)[options.lngParam || 'lng'];
var ns = options.getParams(req)[options.nsParam || 'ns'];
if (!i18next.services.backendConnector) {

@@ -213,8 +190,5 @@ options.setStatus(res, 404);

}
var body = options.getBody(req);
if (typeof body === 'function') {
var promise = body();
if (promise && typeof promise.then === 'function') {

@@ -226,3 +200,2 @@ return new Promise(function (resolve) {

}
resolve(options.send(res, 'ok'));

@@ -233,7 +206,5 @@ });

}
for (var m in body) {
i18next.services.backendConnector.saveMissing([lng], ns, m, body[m]);
}
return options.send(res, 'ok');

@@ -249,10 +220,7 @@ };

var callbacks = [fc].concat(Array.prototype.slice.call(arguments, 6));
for (var i = 0, li = lngs.length; i < li; i++) {
var parts = String(route).split('/');
var locRoute = [];
for (var y = 0, ly = parts.length; y < ly; y++) {
var part = parts[y];
if (part === ':lng') {

@@ -268,3 +236,2 @@ locRoute.push(lngs[i]);

}
var routes = [locRoute.join('/')];

@@ -271,0 +238,0 @@ app[verb || 'get'].apply(app, routes.concat(callbacks));

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
import * as utils from './utils.js';

@@ -14,3 +11,2 @@ import cookieLookup from './languageLookups/cookie.js';

import { extendOptionsWithDefaults } from './httpFunctions.js';
function getDefaults() {

@@ -28,3 +24,2 @@ return extendOptionsWithDefaults({

}
var LanguageDetector = function () {

@@ -34,5 +29,3 @@ function LanguageDetector(services) {

var allOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
_classCallCheck(this, LanguageDetector);
this.type = 'languageDetector';

@@ -42,3 +35,2 @@ this.detectors = {};

}
_createClass(LanguageDetector, [{

@@ -67,3 +59,2 @@ key: "init",

var _this = this;
if (arguments.length < 2) return;

@@ -74,5 +65,3 @@ if (!detectionOrder) detectionOrder = this.options.order;

if (found || !_this.detectors[detectorName]) return;
var detections = _this.detectors[detectorName].lookup(req, res, _this.options);
if (!detections) return;

@@ -83,6 +72,4 @@ if (!Array.isArray(detections)) detections = [detections];

});
if (_this.services.languageUtils.getBestMatchFromCodes) {
found = _this.services.languageUtils.getBestMatchFromCodes(detections);
if (found) {

@@ -97,3 +84,2 @@ if (_this.options.ignoreCase) {

}
if (found) req.i18nextLookupName = detectorName;

@@ -110,3 +96,2 @@ } else {

if (!fallbacks) fallbacks = [];
if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {

@@ -118,3 +103,2 @@ found = fallbacks[0];

}
;

@@ -127,3 +111,2 @@ return found;

var _this2 = this;
if (arguments.length < 3) return;

@@ -135,3 +118,2 @@ if (!caches) caches = this.options.caches;

_this2.detectors[cacheName].cacheUserLanguage(req, res, lng, _this2.options);
res.cachedUserLanguage = lng;

@@ -142,7 +124,5 @@ }

}]);
return LanguageDetector;
}();
LanguageDetector.type = 'languageDetector';
export default LanguageDetector;

@@ -0,3 +1,3 @@

var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
var serializeCookie = function serializeCookie(name, val, options) {

@@ -8,3 +8,2 @@ var opt = options || {};

var str = name + '=' + value;
if (opt.maxAge > 0) {

@@ -15,3 +14,2 @@ var maxAge = opt.maxAge - 0;

}
if (opt.domain) {

@@ -21,6 +19,4 @@ if (!fieldContentRegExp.test(opt.domain)) {

}
str += '; Domain=' + opt.domain;
}
if (opt.path) {

@@ -30,6 +26,4 @@ if (!fieldContentRegExp.test(opt.path)) {

}
str += '; Path=' + opt.path;
}
if (opt.expires) {

@@ -39,12 +33,8 @@ if (typeof opt.expires.toUTCString !== 'function') {

}
str += '; Expires=' + opt.expires.toUTCString();
}
if (opt.httpOnly) str += '; HttpOnly';
if (opt.secure) str += '; Secure';
if (opt.sameSite) {
var sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite;
switch (sameSite) {

@@ -54,15 +44,11 @@ case true:

break;
case 'lax':
str += '; SameSite=Lax';
break;
case 'strict':
str += '; SameSite=Strict';
break;
case 'none':
str += '; SameSite=None';
break;
default:

@@ -72,6 +58,4 @@ throw new TypeError('option sameSite is invalid');

}
return str;
};
export default {

@@ -81,3 +65,2 @@ name: 'cookie',

var found;
if (options.lookupCookie && typeof req !== 'undefined') {

@@ -87,3 +70,2 @@ var cookies = options.getCookies(req);

}
return found;

@@ -93,6 +75,4 @@ },

var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
if (options.lookupCookie && req !== 'undefined') {
var expirationDate = options.cookieExpirationDate;
if (!expirationDate) {

@@ -102,3 +82,2 @@ expirationDate = new Date();

}
var cookieOptions = {

@@ -105,0 +84,0 @@ expires: expirationDate,

@@ -6,3 +6,2 @@ var specialCases = ['hans', 'hant', 'latn', 'cyrl', 'cans', 'mong', 'arab'];

var found;
if (typeof req !== 'undefined') {

@@ -13,6 +12,4 @@ var headers = options.getHeaders(req);

var acceptLanguage = options.lookupHeader ? headers[options.lookupHeader] : headers['accept-language'];
if (acceptLanguage) {
var lookupRegex = /(([a-z]{2})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi;
var lookupRegex = /(([a-z]{2,3})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi;
if (acceptLanguage.indexOf('-') > 0) {

@@ -22,5 +19,4 @@ var foundSpecialCase = specialCases.find(function (s) {

});
if (foundSpecialCase) lookupRegex = /(([a-z]{2})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi;
if (foundSpecialCase) lookupRegex = /(([a-z]{2,3})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi;
}
var lngs = [];

@@ -30,6 +26,4 @@ var i;

var rgx = options.lookupHeaderRegex || lookupRegex;
do {
m = rgx.exec(acceptLanguage);
if (m) {

@@ -39,3 +33,2 @@ var lng = m[1];

var q = Number(weight);
if (lng && !isNaN(q)) {

@@ -49,17 +42,13 @@ lngs.push({

} while (m);
lngs.sort(function (a, b) {
return b.q - a.q;
});
for (i = 0; i < lngs.length; i++) {
locales.push(lngs[i].lng);
}
if (locales.length) found = locales;
}
}
return found;
}
};

@@ -5,19 +5,14 @@ export default {

var found;
if (req === undefined) {
return found;
}
if (options.lookupPath !== undefined && req.params) {
found = options.getParams(req)[options.lookupPath];
}
if (!found && typeof options.lookupFromPathIndex === 'number' && options.getOriginalUrl(req)) {
var path = options.getOriginalUrl(req).split('?')[0];
var parts = path.split('/');
if (parts[0] === '') {
parts.shift();
}
if (parts.length > options.lookupFromPathIndex) {

@@ -27,5 +22,4 @@ found = parts[options.lookupFromPathIndex];

}
return found;
}
};

@@ -5,3 +5,2 @@ export default {

var found;
if (options.lookupQuerystring !== undefined && typeof req !== 'undefined') {

@@ -11,6 +10,4 @@ if (options.getQuery(req)) {

}
if (!found && options.getUrl(req) && options.getUrl(req).indexOf('?')) {
var lastPartOfUri = options.getUrl(req).substring(options.getUrl(req).indexOf('?'));
if (typeof URLSearchParams !== 'undefined') {

@@ -21,6 +18,4 @@ var urlParams = new URLSearchParams(lastPartOfUri);

var indexOfQsStart = lastPartOfUri.indexOf("".concat(options.lookupQuerystring, "="));
if (indexOfQsStart > -1) {
var restOfUri = lastPartOfUri.substring(options.lookupQuerystring.length + 2);
if (restOfUri.indexOf('&') < 0) {

@@ -35,5 +30,4 @@ found = restOfUri;

}
return found;
}
};
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
export default {

@@ -7,7 +6,5 @@ name: 'session',

var found;
if (options.lookupSession !== undefined && _typeof(req) && options.getSession(req)) {
found = options.getSession(req)[options.lookupSession];
}
return found;

@@ -17,3 +14,2 @@ },

var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
if (options.lookupSession && req && options.getSession(req)) {

@@ -20,0 +16,0 @@ options.getSession(req)[options.lookupSession] = lng;

@@ -5,6 +5,4 @@ export function setPath(object, path, newValue) {

if (typeof path === 'string') stack = path.split('.');
while (stack.length > 1) {
var _key = stack.shift();
if (_key.indexOf('###') > -1) _key = _key.replace(/###/g, '.');

@@ -14,3 +12,2 @@ if (!object[_key]) object[_key] = {};

}
var key = stack.shift();

@@ -46,3 +43,2 @@ if (key.indexOf('###') > -1) key = key.replace(/###/g, '.');

var pos = lookupFromPathIndex;
if (url[0] === '/') {

@@ -49,0 +45,0 @@ pos++;

@@ -17,6 +17,6 @@ const specialCases = ['hans', 'hant', 'latn', 'cyrl', 'cans', 'mong', 'arab']

if (acceptLanguage) {
let lookupRegex = /(([a-z]{2})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi
let lookupRegex = /(([a-z]{2,3})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi
if (acceptLanguage.indexOf('-') > 0) {
const foundSpecialCase = specialCases.find((s) => acceptLanguage.toLowerCase().indexOf(`-${s}`) > 0)
if (foundSpecialCase) lookupRegex = /(([a-z]{2})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi
if (foundSpecialCase) lookupRegex = /(([a-z]{2,3})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi
}

@@ -23,0 +23,0 @@ const lngs = []; let i; let m

{
"name": "i18next-http-middleware",
"version": "3.2.1",
"version": "3.2.2",
"private": false,

@@ -24,23 +24,23 @@ "type": "module",

"devDependencies": {
"@babel/cli": "7.16.0",
"@babel/core": "7.16.5",
"@babel/preset-env": "7.16.5",
"@hapi/hapi": "^20.2.1",
"@types/express-serve-static-core": "^4.17.27",
"@babel/cli": "7.19.3",
"@babel/core": "7.19.6",
"@babel/preset-env": "7.19.4",
"@hapi/hapi": "^20.2.2",
"@types/express-serve-static-core": "^4.17.31",
"babel-plugin-add-module-exports": "1.0.4",
"eslint": "7.29.0",
"eslint-config-standard": "16.0.3",
"eslint-plugin-import": "2.25.3",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-promise": "5.1.0",
"eslint": "8.26.0",
"eslint-config-standard": "17.0.0",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-n": "15.3.0",
"eslint-plugin-promise": "6.1.1",
"eslint-plugin-require-path-exists": "1.1.9",
"eslint-plugin-standard": "5.0.0",
"expect.js": "0.3.1",
"express": "4.17.2",
"fastify": "3.25.2",
"i18next": "21.6.3",
"mocha": "9.1.3",
"supertest": "6.1.6",
"tsd": "0.19.0",
"uglify-js": "3.14.5"
"express": "4.18.2",
"fastify": "4.9.2",
"i18next": "22.0.2",
"mocha": "10.1.0",
"supertest": "6.3.1",
"tsd": "0.24.1",
"uglify-js": "3.17.4"
},

@@ -47,0 +47,0 @@ "description": "i18next-http-middleware is a middleware to be used with Node.js web frameworks like express or Fastify and also for Deno.",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc