i18next-http-middleware 3.10.1 → 3.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -270,6 +270,9 @@ const app = new App()
270
270
 
271
271
  ```js
272
272
  // missing keys make sure the body is parsed (i.e. with [body-parser](https://github.com/expressjs/body-parser#bodyparserjsonoptions))
273
+ // this route takes writes from anyone who can reach it: every key in the body goes to the
274
+ // backend's create (i18next-fs-backend writes it into a file) and stays in the server's memory,
275
+ // so mount it only in development or behind authentication
273
276
  app.post('/locales/add/:lng/:ns', middleware.missingKeyHandler(i18next))
274
277
  // addPath for client: http://localhost:8080/locales/add/{{lng}}/{{ns}}
275
278
 
package/cjs/index.js CHANGED
@@ -301,6 +301,8 @@ function missingKeyHandler(i18next) {
301
301
  var keySeparator = i18next.options && i18next.options.keySeparator;
302
302
  var utilsOfRequest = req.i18n && req.i18n.services && req.i18n.services.utils;
303
303
  var hasLoadedNamespace = utilsOfRequest && utilsOfRequest.hasLoadedNamespace;
304
+ var sharedNs = i18next.options && i18next.options.ns;
305
+ var nsWasKnown = !Array.isArray(sharedNs) || sharedNs.indexOf(ns) > -1;
304
306
  var saveMissingKeys = function saveMissingKeys(src) {
305
307
  if (!src || _typeof(src) !== 'object') return;
306
308
  for (var _i = 0, _Object$keys = Object.keys(src); _i < _Object$keys.length; _i++) {
@@ -308,6 +310,7 @@ function missingKeyHandler(i18next) {
308
310
  if (utils.hasUnsafeKeySegment(m, keySeparator)) continue;
309
311
  i18next.services.backendConnector.saveMissing([lng], ns, m, src[m], undefined, undefined, undefined, hasLoadedNamespace);
310
312
  }
313
+ if (!nsWasKnown && sharedNs.indexOf(ns) > -1) sharedNs.splice(sharedNs.indexOf(ns), 1);
311
314
  };
312
315
  if (typeof body === 'function') {
313
316
  var promise = body();
package/esm/index.js CHANGED
@@ -284,6 +284,8 @@ export function missingKeyHandler(i18next) {
284
284
  var keySeparator = i18next.options && i18next.options.keySeparator;
285
285
  var utilsOfRequest = req.i18n && req.i18n.services && req.i18n.services.utils;
286
286
  var hasLoadedNamespace = utilsOfRequest && utilsOfRequest.hasLoadedNamespace;
287
+ var sharedNs = i18next.options && i18next.options.ns;
288
+ var nsWasKnown = !Array.isArray(sharedNs) || sharedNs.indexOf(ns) > -1;
287
289
  var saveMissingKeys = function saveMissingKeys(src) {
288
290
  if (!src || _typeof(src) !== 'object') return;
289
291
  for (var _i = 0, _Object$keys = Object.keys(src); _i < _Object$keys.length; _i++) {
@@ -291,6 +293,7 @@ export function missingKeyHandler(i18next) {
291
293
  if (utils.hasUnsafeKeySegment(m, keySeparator)) continue;
292
294
  i18next.services.backendConnector.saveMissing([lng], ns, m, src[m], undefined, undefined, undefined, hasLoadedNamespace);
293
295
  }
296
+ if (!nsWasKnown && sharedNs.indexOf(ns) > -1) sharedNs.splice(sharedNs.indexOf(ns), 1);
294
297
  };
295
298
  if (typeof body === 'function') {
296
299
  var promise = body();
package/lib/index.js CHANGED
@@ -335,6 +335,11 @@ export function missingKeyHandler (i18next, options = {}) {
335
335
  const keySeparator = i18next.options && i18next.options.keySeparator
336
336
  const utilsOfRequest = req.i18n && req.i18n.services && req.i18n.services.utils
337
337
  const hasLoadedNamespace = utilsOfRequest && utilsOfRequest.hasLoadedNamespace
338
+ // saveMissing() writes the key to the store, which also adds its namespace to the
339
+ // shared i18next.options.ns: every request walks that list, so a namespace that
340
+ // came from this request must not stay in it
341
+ const sharedNs = i18next.options && i18next.options.ns
342
+ const nsWasKnown = !Array.isArray(sharedNs) || sharedNs.indexOf(ns) > -1
338
343
 
339
344
  // iterate only over own, non-prototype-polluting keys. The check also
340
345
  // rejects dotted variants like `__proto__.polluted` whose segments under
@@ -347,6 +352,7 @@ export function missingKeyHandler (i18next, options = {}) {
347
352
  // request) as the 8th argument; without it, it checks the newest clone
348
353
  i18next.services.backendConnector.saveMissing([lng], ns, m, src[m], undefined, undefined, undefined, hasLoadedNamespace)
349
354
  }
355
+ if (!nsWasKnown && sharedNs.indexOf(ns) > -1) sharedNs.splice(sharedNs.indexOf(ns), 1)
350
356
  }
351
357
 
352
358
  if (typeof body === 'function') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "i18next-http-middleware",
3
- "version": "3.10.1",
3
+ "version": "3.10.2",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./cjs/index.js",