@bleedingdev/modern-js-i18n-runtime-extensions 3.9.3-ultramodern.7 → 3.9.3-ultramodern.9
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/dist/cjs/native.js +286 -8
- package/dist/esm/native.mjs +273 -7
- package/dist/esm-node/native.mjs +273 -7
- package/dist/types/native.d.ts +13 -0
- package/package.json +12 -12
- package/src/native.ts +419 -7
package/dist/cjs/native.js
CHANGED
|
@@ -37,7 +37,11 @@ var __webpack_require__ = {};
|
|
|
37
37
|
var __webpack_exports__ = {};
|
|
38
38
|
__webpack_require__.r(__webpack_exports__);
|
|
39
39
|
__webpack_require__.d(__webpack_exports__, {
|
|
40
|
-
createNativeI18n: ()=>createNativeI18n
|
|
40
|
+
createNativeI18n: ()=>createNativeI18n,
|
|
41
|
+
createNativeI18nInstance: ()=>createNativeI18nInstance,
|
|
42
|
+
getNativeI18nRewriteSnapshot: ()=>getNativeI18nRewriteSnapshot,
|
|
43
|
+
getNativeI18nSnapshotSeed: ()=>getNativeI18nSnapshotSeed,
|
|
44
|
+
invalidateNativeI18nSnapshot: ()=>invalidateNativeI18nSnapshot
|
|
41
45
|
});
|
|
42
46
|
const external_i18next_namespaceObject = require("i18next");
|
|
43
47
|
var external_i18next_default = /*#__PURE__*/ __webpack_require__.n(external_i18next_namespaceObject);
|
|
@@ -45,6 +49,242 @@ const controller_js_namespaceObject = require("./language-sync/controller.js");
|
|
|
45
49
|
const external_requestLanguage_js_namespaceObject = require("./requestLanguage.js");
|
|
46
50
|
const external_ssrLanguageHandoff_js_namespaceObject = require("./ssrLanguageHandoff.js");
|
|
47
51
|
const external_urlRewrite_js_namespaceObject = require("./urlRewrite.js");
|
|
52
|
+
const nativeSnapshotOrigins = new WeakMap();
|
|
53
|
+
function invalidateNativeI18nSnapshot(instance) {
|
|
54
|
+
if ('object' == typeof instance && null !== instance) nativeSnapshotOrigins.delete(instance);
|
|
55
|
+
}
|
|
56
|
+
const nativeRewriteOrigins = new WeakMap();
|
|
57
|
+
function getNativeI18nRewriteSnapshot(rewrite) {
|
|
58
|
+
return 'object' == typeof rewrite && null !== rewrite ? nativeRewriteOrigins.get(rewrite)?.() : void 0;
|
|
59
|
+
}
|
|
60
|
+
function detachLocalisedUrls(value) {
|
|
61
|
+
if (void 0 === value || 'boolean' == typeof value) return value;
|
|
62
|
+
return Object.fromEntries(Object.entries(value).map(([path, languages])=>[
|
|
63
|
+
path,
|
|
64
|
+
Object.fromEntries(Object.entries(languages))
|
|
65
|
+
]));
|
|
66
|
+
}
|
|
67
|
+
function plainRecord(value) {
|
|
68
|
+
return 'object' == typeof value && null !== value && (Object.getPrototypeOf(value) === Object.prototype || null === Object.getPrototypeOf(value));
|
|
69
|
+
}
|
|
70
|
+
function detachOption(value, ancestors = new Set()) {
|
|
71
|
+
if (null === value || 'object' != typeof value) return value;
|
|
72
|
+
if (ancestors.has(value) || (Array.isArray(value) ? Object.getPrototypeOf(value) !== Array.prototype : !plainRecord(value))) throw new Error('Native i18n snapshot options must be plain acyclic data');
|
|
73
|
+
ancestors.add(value);
|
|
74
|
+
const copy = Array.isArray(value) ? [] : {};
|
|
75
|
+
for (const key of Reflect.ownKeys(value)){
|
|
76
|
+
if (Array.isArray(value) && 'length' === key) continue;
|
|
77
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
78
|
+
if ('string' != typeof key || !descriptor || !('value' in descriptor)) throw new Error('Native i18n snapshot options cannot contain accessors');
|
|
79
|
+
Object.defineProperty(copy, key, {
|
|
80
|
+
value: detachOption(descriptor.value, ancestors),
|
|
81
|
+
enumerable: descriptor.enumerable,
|
|
82
|
+
writable: true,
|
|
83
|
+
configurable: true
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
ancestors.delete(value);
|
|
87
|
+
return copy;
|
|
88
|
+
}
|
|
89
|
+
function detachInitOptions(options) {
|
|
90
|
+
const copy = detachOption(options);
|
|
91
|
+
if (!plainRecord(copy)) throw new Error('Invalid native i18n options');
|
|
92
|
+
return copy;
|
|
93
|
+
}
|
|
94
|
+
function descriptorProof(object, ignore = new Set(), ancestors = new Set()) {
|
|
95
|
+
if (ancestors.has(object)) return ()=>true;
|
|
96
|
+
ancestors.add(object);
|
|
97
|
+
const keys = Reflect.ownKeys(object).filter((key)=>'string' != typeof key || !ignore.has(key));
|
|
98
|
+
const proofs = keys.map((key)=>{
|
|
99
|
+
const descriptor = Object.getOwnPropertyDescriptor(object, key);
|
|
100
|
+
if (!descriptor || !('value' in descriptor)) throw new Error('Native i18n snapshot provenance cannot contain accessors');
|
|
101
|
+
const value = descriptor.value;
|
|
102
|
+
const nested = Array.isArray(value) || plainRecord(value) ? descriptorProof(value, new Set(), ancestors) : ()=>true;
|
|
103
|
+
return ()=>{
|
|
104
|
+
const current = Object.getOwnPropertyDescriptor(object, key);
|
|
105
|
+
return void 0 !== current && 'value' in current && current.value === value && current.enumerable === descriptor.enumerable && current.configurable === descriptor.configurable && current.writable === descriptor.writable && nested();
|
|
106
|
+
};
|
|
107
|
+
});
|
|
108
|
+
ancestors.delete(object);
|
|
109
|
+
return ()=>{
|
|
110
|
+
const currentKeys = Reflect.ownKeys(object).filter((key)=>'string' != typeof key || !ignore.has(key));
|
|
111
|
+
return currentKeys.length === keys.length && currentKeys.every((key, index)=>key === keys[index]) && proofs.every((proof)=>proof());
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function methodProof(object) {
|
|
115
|
+
const prototype = Object.getPrototypeOf(object);
|
|
116
|
+
const methods = new Map();
|
|
117
|
+
for(let current = object; current && current !== Object.prototype; current = Object.getPrototypeOf(current))for (const key of Reflect.ownKeys(current)){
|
|
118
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, key);
|
|
119
|
+
if (descriptor && 'value' in descriptor) {
|
|
120
|
+
if ('function' == typeof descriptor.value && !methods.has(key)) methods.set(key, descriptor.value);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return ()=>{
|
|
124
|
+
if (Object.getPrototypeOf(object) !== prototype) return false;
|
|
125
|
+
for (const [key, method] of methods){
|
|
126
|
+
let current = object;
|
|
127
|
+
while(current){
|
|
128
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, key);
|
|
129
|
+
if (descriptor) {
|
|
130
|
+
if (!('value' in descriptor) || descriptor.value !== method) return false;
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
current = Object.getPrototypeOf(current);
|
|
134
|
+
}
|
|
135
|
+
if (!current) return false;
|
|
136
|
+
}
|
|
137
|
+
return true;
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function ownValue(object, key) {
|
|
141
|
+
const descriptor = Object.getOwnPropertyDescriptor(object, key);
|
|
142
|
+
if (descriptor && !('value' in descriptor)) throw new Error('Native i18n snapshot provenance cannot contain accessors');
|
|
143
|
+
return descriptor?.value;
|
|
144
|
+
}
|
|
145
|
+
function semanticProof(object, fields) {
|
|
146
|
+
const proofs = fields.map((key)=>{
|
|
147
|
+
const value = ownValue(object, key);
|
|
148
|
+
const nested = Array.isArray(value) || plainRecord(value) ? descriptorProof(value, new Set([
|
|
149
|
+
'resources'
|
|
150
|
+
])) : ()=>true;
|
|
151
|
+
if (value instanceof RegExp) {
|
|
152
|
+
const source = value.source;
|
|
153
|
+
const flags = value.flags;
|
|
154
|
+
return ()=>{
|
|
155
|
+
const current = ownValue(object, key);
|
|
156
|
+
return current instanceof RegExp && current.source === source && current.flags === flags;
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
return ()=>ownValue(object, key) === value && nested();
|
|
160
|
+
});
|
|
161
|
+
return ()=>proofs.every((proof)=>proof());
|
|
162
|
+
}
|
|
163
|
+
function recordNativeSnapshotOrigin(instance, original, seed) {
|
|
164
|
+
try {
|
|
165
|
+
const useDescriptor = Object.getOwnPropertyDescriptor(instance, 'use');
|
|
166
|
+
if (!useDescriptor || !('value' in useDescriptor)) throw new Error('Native plugin registration requires an own method');
|
|
167
|
+
const use = instance.use;
|
|
168
|
+
Object.defineProperty(instance, 'use', {
|
|
169
|
+
...useDescriptor,
|
|
170
|
+
value: function(plugin) {
|
|
171
|
+
invalidateNativeI18nSnapshot(instance);
|
|
172
|
+
return use(plugin);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
const services = instance.services;
|
|
176
|
+
const store = instance.store;
|
|
177
|
+
const options = instance.options;
|
|
178
|
+
const modules = instance.modules;
|
|
179
|
+
const translator = Object.getOwnPropertyDescriptor(instance, 'translator')?.value;
|
|
180
|
+
const formatter = instance.services.formatter;
|
|
181
|
+
const formats = formatter && 'object' == typeof formatter ? Object.getOwnPropertyDescriptor(formatter, 'formats')?.value : void 0;
|
|
182
|
+
const proofs = [
|
|
183
|
+
descriptorProof(instance.options, new Set([
|
|
184
|
+
'resources'
|
|
185
|
+
])),
|
|
186
|
+
descriptorProof(instance.modules),
|
|
187
|
+
descriptorProof(services),
|
|
188
|
+
methodProof(instance),
|
|
189
|
+
methodProof(instance.store),
|
|
190
|
+
...Object.values(instance.services).filter((value)=>'object' == typeof value && null !== value).map((value)=>methodProof(value)),
|
|
191
|
+
...translator && 'object' == typeof translator ? [
|
|
192
|
+
methodProof(translator)
|
|
193
|
+
] : [],
|
|
194
|
+
...formats && 'object' == typeof formats ? [
|
|
195
|
+
descriptorProof(formats)
|
|
196
|
+
] : []
|
|
197
|
+
];
|
|
198
|
+
const serviceFields = {
|
|
199
|
+
languageUtils: [
|
|
200
|
+
'options',
|
|
201
|
+
'supportedLngs'
|
|
202
|
+
],
|
|
203
|
+
pluralResolver: [
|
|
204
|
+
'languageUtils',
|
|
205
|
+
'options'
|
|
206
|
+
],
|
|
207
|
+
interpolator: [
|
|
208
|
+
'options',
|
|
209
|
+
'escape',
|
|
210
|
+
'escapeValue',
|
|
211
|
+
'useRawValueToEscape',
|
|
212
|
+
'prefix',
|
|
213
|
+
'suffix',
|
|
214
|
+
'format',
|
|
215
|
+
'formatSeparator',
|
|
216
|
+
'unescapePrefix',
|
|
217
|
+
'unescapeSuffix',
|
|
218
|
+
'nestingPrefix',
|
|
219
|
+
'nestingSuffix',
|
|
220
|
+
'nestingOptionsSeparator',
|
|
221
|
+
'maxReplaces',
|
|
222
|
+
'alwaysFormat',
|
|
223
|
+
'regexp',
|
|
224
|
+
'regexpUnescape',
|
|
225
|
+
'nestingRegexp'
|
|
226
|
+
],
|
|
227
|
+
formatter: [
|
|
228
|
+
'formatSeparator'
|
|
229
|
+
],
|
|
230
|
+
resourceStore: [
|
|
231
|
+
'options'
|
|
232
|
+
],
|
|
233
|
+
backendConnector: [
|
|
234
|
+
'backend',
|
|
235
|
+
'store',
|
|
236
|
+
'services',
|
|
237
|
+
'options'
|
|
238
|
+
]
|
|
239
|
+
};
|
|
240
|
+
const links = Object.entries(instance.services).map(([key, value])=>{
|
|
241
|
+
if ('object' == typeof value && null !== value) proofs.push(semanticProof(value, serviceFields[key] ?? []));
|
|
242
|
+
return ()=>ownValue(services, key) === value;
|
|
243
|
+
});
|
|
244
|
+
if (translator && 'object' == typeof translator) proofs.push(semanticProof(translator, [
|
|
245
|
+
'options',
|
|
246
|
+
'resourceStore',
|
|
247
|
+
'languageUtils',
|
|
248
|
+
'pluralResolver',
|
|
249
|
+
'interpolator',
|
|
250
|
+
'backendConnector',
|
|
251
|
+
'i18nFormat',
|
|
252
|
+
'utils'
|
|
253
|
+
]));
|
|
254
|
+
nativeSnapshotOrigins.set(instance, {
|
|
255
|
+
instance,
|
|
256
|
+
original: detachInitOptions(original),
|
|
257
|
+
seed: detachInitOptions(seed),
|
|
258
|
+
unchanged: ()=>ownValue(instance, 'services') === services && ownValue(instance, 'store') === store && ownValue(instance, 'options') === options && ownValue(instance, 'modules') === modules && ownValue(services, 'formatter') === formatter && (formatter && 'object' == typeof formatter ? ownValue(formatter, 'formats') === formats : true) && Object.getOwnPropertyDescriptor(instance, 'translator')?.value === translator && (translator && 'object' == typeof translator ? ownValue(translator, 'language') === ownValue(instance, 'language') : true) && links.every((proof)=>proof()) && proofs.every((proof)=>proof())
|
|
259
|
+
});
|
|
260
|
+
} catch {}
|
|
261
|
+
}
|
|
262
|
+
function createNativeI18nInstance(initOptions) {
|
|
263
|
+
const original = detachInitOptions(initOptions);
|
|
264
|
+
const seed = {
|
|
265
|
+
...detachInitOptions(original),
|
|
266
|
+
initAsync: false,
|
|
267
|
+
partialBundledLanguages: false,
|
|
268
|
+
interpolation: {
|
|
269
|
+
escapeValue: false,
|
|
270
|
+
...original.interpolation
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
const instance = external_i18next_default().createInstance();
|
|
274
|
+
instance.init(detachInitOptions(seed));
|
|
275
|
+
if (!instance.isInitialized || instance.isInitializing) throw new Error('Native i18n instances require complete bundled resources');
|
|
276
|
+
recordNativeSnapshotOrigin(instance, original, seed);
|
|
277
|
+
return instance;
|
|
278
|
+
}
|
|
279
|
+
function getNativeI18nSnapshotSeed(instance) {
|
|
280
|
+
const origin = 'object' == typeof instance && null !== instance ? nativeSnapshotOrigins.get(instance) : void 0;
|
|
281
|
+
const services = origin ? ownValue(origin.instance, 'services') : void 0;
|
|
282
|
+
const connector = services && 'object' == typeof services ? ownValue(services, 'backendConnector') : void 0;
|
|
283
|
+
const state = connector && 'object' == typeof connector ? ownValue(connector, 'state') : void 0;
|
|
284
|
+
const pending = plainRecord(state) && Object.values(Object.getOwnPropertyDescriptors(state)).some((descriptor)=>!('value' in descriptor) || 1 === descriptor.value);
|
|
285
|
+
if (!origin || true !== ownValue(origin.instance, 'isInitialized') || true === ownValue(origin.instance, 'isInitializing') || ownValue(origin.instance, 'isLanguageChangingTo') || pending || !origin.unchanged()) throw new Error('This i18n instance requires an explicit Worker profile');
|
|
286
|
+
return detachInitOptions(origin.seed);
|
|
287
|
+
}
|
|
48
288
|
function unwrapResource(module) {
|
|
49
289
|
const resource = 'object' == typeof module && null !== module && 'default' in module ? module.default : module;
|
|
50
290
|
return resource;
|
|
@@ -77,7 +317,7 @@ function createNativeI18n(options, loaders) {
|
|
|
77
317
|
async create (language, resources) {
|
|
78
318
|
const instance = external_i18next_default().createInstance();
|
|
79
319
|
instance.use(backend);
|
|
80
|
-
|
|
320
|
+
const seed = {
|
|
81
321
|
...initOptions,
|
|
82
322
|
lng: language,
|
|
83
323
|
fallbackLng: routing.fallbackLanguage,
|
|
@@ -94,14 +334,44 @@ function createNativeI18n(options, loaders) {
|
|
|
94
334
|
escapeValue: false,
|
|
95
335
|
...initOptions.interpolation
|
|
96
336
|
}
|
|
97
|
-
}
|
|
337
|
+
};
|
|
338
|
+
let original;
|
|
339
|
+
let detachedSeed;
|
|
340
|
+
try {
|
|
341
|
+
original = detachInitOptions(initOptions);
|
|
342
|
+
detachedSeed = detachInitOptions(seed);
|
|
343
|
+
} catch {}
|
|
344
|
+
await instance.init(seed);
|
|
345
|
+
if (original && detachedSeed) recordNativeSnapshotOrigin(instance, original, detachedSeed);
|
|
98
346
|
return instance;
|
|
99
347
|
},
|
|
100
|
-
rewrite: (getLanguage)=>
|
|
101
|
-
|
|
348
|
+
rewrite: (getLanguage)=>{
|
|
349
|
+
const languages = [
|
|
350
|
+
...routing.languages
|
|
351
|
+
];
|
|
352
|
+
const localisedUrls = detachLocalisedUrls(routing.localisedUrls);
|
|
353
|
+
const rewrite = (0, external_urlRewrite_js_namespaceObject.createI18nUrlRewrite)({
|
|
354
|
+
languages,
|
|
102
355
|
getLanguage,
|
|
103
|
-
localisedUrls
|
|
104
|
-
})
|
|
356
|
+
localisedUrls
|
|
357
|
+
});
|
|
358
|
+
const unchanged = descriptorProof(rewrite);
|
|
359
|
+
nativeRewriteOrigins.set(rewrite, ()=>{
|
|
360
|
+
if (!unchanged()) throw new Error('Native i18n rewrite was modified');
|
|
361
|
+
const language = getLanguage();
|
|
362
|
+
if (!languages.includes(language)) throw new Error('Native i18n rewrite language is unsupported');
|
|
363
|
+
return {
|
|
364
|
+
language,
|
|
365
|
+
languages: [
|
|
366
|
+
...languages
|
|
367
|
+
],
|
|
368
|
+
...void 0 === localisedUrls ? {} : {
|
|
369
|
+
localisedUrls: detachLocalisedUrls(localisedUrls)
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
});
|
|
373
|
+
return rewrite;
|
|
374
|
+
},
|
|
105
375
|
handoff (language, instance) {
|
|
106
376
|
return (0, external_ssrLanguageHandoff_js_namespaceObject.createI18nSsrHandoffInlineData)({
|
|
107
377
|
language,
|
|
@@ -152,8 +422,16 @@ function createNativeI18n(options, loaders) {
|
|
|
152
422
|
};
|
|
153
423
|
}
|
|
154
424
|
exports.createNativeI18n = __webpack_exports__.createNativeI18n;
|
|
425
|
+
exports.createNativeI18nInstance = __webpack_exports__.createNativeI18nInstance;
|
|
426
|
+
exports.getNativeI18nRewriteSnapshot = __webpack_exports__.getNativeI18nRewriteSnapshot;
|
|
427
|
+
exports.getNativeI18nSnapshotSeed = __webpack_exports__.getNativeI18nSnapshotSeed;
|
|
428
|
+
exports.invalidateNativeI18nSnapshot = __webpack_exports__.invalidateNativeI18nSnapshot;
|
|
155
429
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
156
|
-
"createNativeI18n"
|
|
430
|
+
"createNativeI18n",
|
|
431
|
+
"createNativeI18nInstance",
|
|
432
|
+
"getNativeI18nRewriteSnapshot",
|
|
433
|
+
"getNativeI18nSnapshotSeed",
|
|
434
|
+
"invalidateNativeI18nSnapshot"
|
|
157
435
|
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
158
436
|
Object.defineProperty(exports, '__esModule', {
|
|
159
437
|
value: true
|
package/dist/esm/native.mjs
CHANGED
|
@@ -3,6 +3,242 @@ import { createLatestLanguageSyncBinding } from "./language-sync/controller.mjs"
|
|
|
3
3
|
import { createRequestLanguageRedirect, languageFromPathname, resolveRequestLanguage } from "./requestLanguage.mjs";
|
|
4
4
|
import { createI18nSsrHandoffInlineData, readI18nSsrHandoff } from "./ssrLanguageHandoff.mjs";
|
|
5
5
|
import { createI18nUrlRewrite } from "./urlRewrite.mjs";
|
|
6
|
+
const nativeSnapshotOrigins = new WeakMap();
|
|
7
|
+
function invalidateNativeI18nSnapshot(instance) {
|
|
8
|
+
if ('object' == typeof instance && null !== instance) nativeSnapshotOrigins.delete(instance);
|
|
9
|
+
}
|
|
10
|
+
const nativeRewriteOrigins = new WeakMap();
|
|
11
|
+
function getNativeI18nRewriteSnapshot(rewrite) {
|
|
12
|
+
return 'object' == typeof rewrite && null !== rewrite ? nativeRewriteOrigins.get(rewrite)?.() : void 0;
|
|
13
|
+
}
|
|
14
|
+
function detachLocalisedUrls(value) {
|
|
15
|
+
if (void 0 === value || 'boolean' == typeof value) return value;
|
|
16
|
+
return Object.fromEntries(Object.entries(value).map(([path, languages])=>[
|
|
17
|
+
path,
|
|
18
|
+
Object.fromEntries(Object.entries(languages))
|
|
19
|
+
]));
|
|
20
|
+
}
|
|
21
|
+
function plainRecord(value) {
|
|
22
|
+
return 'object' == typeof value && null !== value && (Object.getPrototypeOf(value) === Object.prototype || null === Object.getPrototypeOf(value));
|
|
23
|
+
}
|
|
24
|
+
function detachOption(value, ancestors = new Set()) {
|
|
25
|
+
if (null === value || 'object' != typeof value) return value;
|
|
26
|
+
if (ancestors.has(value) || (Array.isArray(value) ? Object.getPrototypeOf(value) !== Array.prototype : !plainRecord(value))) throw new Error('Native i18n snapshot options must be plain acyclic data');
|
|
27
|
+
ancestors.add(value);
|
|
28
|
+
const copy = Array.isArray(value) ? [] : {};
|
|
29
|
+
for (const key of Reflect.ownKeys(value)){
|
|
30
|
+
if (Array.isArray(value) && 'length' === key) continue;
|
|
31
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
32
|
+
if ('string' != typeof key || !descriptor || !('value' in descriptor)) throw new Error('Native i18n snapshot options cannot contain accessors');
|
|
33
|
+
Object.defineProperty(copy, key, {
|
|
34
|
+
value: detachOption(descriptor.value, ancestors),
|
|
35
|
+
enumerable: descriptor.enumerable,
|
|
36
|
+
writable: true,
|
|
37
|
+
configurable: true
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
ancestors.delete(value);
|
|
41
|
+
return copy;
|
|
42
|
+
}
|
|
43
|
+
function detachInitOptions(options) {
|
|
44
|
+
const copy = detachOption(options);
|
|
45
|
+
if (!plainRecord(copy)) throw new Error('Invalid native i18n options');
|
|
46
|
+
return copy;
|
|
47
|
+
}
|
|
48
|
+
function descriptorProof(object, ignore = new Set(), ancestors = new Set()) {
|
|
49
|
+
if (ancestors.has(object)) return ()=>true;
|
|
50
|
+
ancestors.add(object);
|
|
51
|
+
const keys = Reflect.ownKeys(object).filter((key)=>'string' != typeof key || !ignore.has(key));
|
|
52
|
+
const proofs = keys.map((key)=>{
|
|
53
|
+
const descriptor = Object.getOwnPropertyDescriptor(object, key);
|
|
54
|
+
if (!descriptor || !('value' in descriptor)) throw new Error('Native i18n snapshot provenance cannot contain accessors');
|
|
55
|
+
const value = descriptor.value;
|
|
56
|
+
const nested = Array.isArray(value) || plainRecord(value) ? descriptorProof(value, new Set(), ancestors) : ()=>true;
|
|
57
|
+
return ()=>{
|
|
58
|
+
const current = Object.getOwnPropertyDescriptor(object, key);
|
|
59
|
+
return void 0 !== current && 'value' in current && current.value === value && current.enumerable === descriptor.enumerable && current.configurable === descriptor.configurable && current.writable === descriptor.writable && nested();
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
ancestors.delete(object);
|
|
63
|
+
return ()=>{
|
|
64
|
+
const currentKeys = Reflect.ownKeys(object).filter((key)=>'string' != typeof key || !ignore.has(key));
|
|
65
|
+
return currentKeys.length === keys.length && currentKeys.every((key, index)=>key === keys[index]) && proofs.every((proof)=>proof());
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function methodProof(object) {
|
|
69
|
+
const prototype = Object.getPrototypeOf(object);
|
|
70
|
+
const methods = new Map();
|
|
71
|
+
for(let current = object; current && current !== Object.prototype; current = Object.getPrototypeOf(current))for (const key of Reflect.ownKeys(current)){
|
|
72
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, key);
|
|
73
|
+
if (descriptor && 'value' in descriptor) {
|
|
74
|
+
if ('function' == typeof descriptor.value && !methods.has(key)) methods.set(key, descriptor.value);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return ()=>{
|
|
78
|
+
if (Object.getPrototypeOf(object) !== prototype) return false;
|
|
79
|
+
for (const [key, method] of methods){
|
|
80
|
+
let current = object;
|
|
81
|
+
while(current){
|
|
82
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, key);
|
|
83
|
+
if (descriptor) {
|
|
84
|
+
if (!('value' in descriptor) || descriptor.value !== method) return false;
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
current = Object.getPrototypeOf(current);
|
|
88
|
+
}
|
|
89
|
+
if (!current) return false;
|
|
90
|
+
}
|
|
91
|
+
return true;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function ownValue(object, key) {
|
|
95
|
+
const descriptor = Object.getOwnPropertyDescriptor(object, key);
|
|
96
|
+
if (descriptor && !('value' in descriptor)) throw new Error('Native i18n snapshot provenance cannot contain accessors');
|
|
97
|
+
return descriptor?.value;
|
|
98
|
+
}
|
|
99
|
+
function semanticProof(object, fields) {
|
|
100
|
+
const proofs = fields.map((key)=>{
|
|
101
|
+
const value = ownValue(object, key);
|
|
102
|
+
const nested = Array.isArray(value) || plainRecord(value) ? descriptorProof(value, new Set([
|
|
103
|
+
'resources'
|
|
104
|
+
])) : ()=>true;
|
|
105
|
+
if (value instanceof RegExp) {
|
|
106
|
+
const source = value.source;
|
|
107
|
+
const flags = value.flags;
|
|
108
|
+
return ()=>{
|
|
109
|
+
const current = ownValue(object, key);
|
|
110
|
+
return current instanceof RegExp && current.source === source && current.flags === flags;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
return ()=>ownValue(object, key) === value && nested();
|
|
114
|
+
});
|
|
115
|
+
return ()=>proofs.every((proof)=>proof());
|
|
116
|
+
}
|
|
117
|
+
function recordNativeSnapshotOrigin(instance, original, seed) {
|
|
118
|
+
try {
|
|
119
|
+
const useDescriptor = Object.getOwnPropertyDescriptor(instance, 'use');
|
|
120
|
+
if (!useDescriptor || !('value' in useDescriptor)) throw new Error('Native plugin registration requires an own method');
|
|
121
|
+
const use = instance.use;
|
|
122
|
+
Object.defineProperty(instance, 'use', {
|
|
123
|
+
...useDescriptor,
|
|
124
|
+
value: function(plugin) {
|
|
125
|
+
invalidateNativeI18nSnapshot(instance);
|
|
126
|
+
return use(plugin);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
const services = instance.services;
|
|
130
|
+
const store = instance.store;
|
|
131
|
+
const options = instance.options;
|
|
132
|
+
const modules = instance.modules;
|
|
133
|
+
const translator = Object.getOwnPropertyDescriptor(instance, 'translator')?.value;
|
|
134
|
+
const formatter = instance.services.formatter;
|
|
135
|
+
const formats = formatter && 'object' == typeof formatter ? Object.getOwnPropertyDescriptor(formatter, 'formats')?.value : void 0;
|
|
136
|
+
const proofs = [
|
|
137
|
+
descriptorProof(instance.options, new Set([
|
|
138
|
+
'resources'
|
|
139
|
+
])),
|
|
140
|
+
descriptorProof(instance.modules),
|
|
141
|
+
descriptorProof(services),
|
|
142
|
+
methodProof(instance),
|
|
143
|
+
methodProof(instance.store),
|
|
144
|
+
...Object.values(instance.services).filter((value)=>'object' == typeof value && null !== value).map((value)=>methodProof(value)),
|
|
145
|
+
...translator && 'object' == typeof translator ? [
|
|
146
|
+
methodProof(translator)
|
|
147
|
+
] : [],
|
|
148
|
+
...formats && 'object' == typeof formats ? [
|
|
149
|
+
descriptorProof(formats)
|
|
150
|
+
] : []
|
|
151
|
+
];
|
|
152
|
+
const serviceFields = {
|
|
153
|
+
languageUtils: [
|
|
154
|
+
'options',
|
|
155
|
+
'supportedLngs'
|
|
156
|
+
],
|
|
157
|
+
pluralResolver: [
|
|
158
|
+
'languageUtils',
|
|
159
|
+
'options'
|
|
160
|
+
],
|
|
161
|
+
interpolator: [
|
|
162
|
+
'options',
|
|
163
|
+
'escape',
|
|
164
|
+
'escapeValue',
|
|
165
|
+
'useRawValueToEscape',
|
|
166
|
+
'prefix',
|
|
167
|
+
'suffix',
|
|
168
|
+
'format',
|
|
169
|
+
'formatSeparator',
|
|
170
|
+
'unescapePrefix',
|
|
171
|
+
'unescapeSuffix',
|
|
172
|
+
'nestingPrefix',
|
|
173
|
+
'nestingSuffix',
|
|
174
|
+
'nestingOptionsSeparator',
|
|
175
|
+
'maxReplaces',
|
|
176
|
+
'alwaysFormat',
|
|
177
|
+
'regexp',
|
|
178
|
+
'regexpUnescape',
|
|
179
|
+
'nestingRegexp'
|
|
180
|
+
],
|
|
181
|
+
formatter: [
|
|
182
|
+
'formatSeparator'
|
|
183
|
+
],
|
|
184
|
+
resourceStore: [
|
|
185
|
+
'options'
|
|
186
|
+
],
|
|
187
|
+
backendConnector: [
|
|
188
|
+
'backend',
|
|
189
|
+
'store',
|
|
190
|
+
'services',
|
|
191
|
+
'options'
|
|
192
|
+
]
|
|
193
|
+
};
|
|
194
|
+
const links = Object.entries(instance.services).map(([key, value])=>{
|
|
195
|
+
if ('object' == typeof value && null !== value) proofs.push(semanticProof(value, serviceFields[key] ?? []));
|
|
196
|
+
return ()=>ownValue(services, key) === value;
|
|
197
|
+
});
|
|
198
|
+
if (translator && 'object' == typeof translator) proofs.push(semanticProof(translator, [
|
|
199
|
+
'options',
|
|
200
|
+
'resourceStore',
|
|
201
|
+
'languageUtils',
|
|
202
|
+
'pluralResolver',
|
|
203
|
+
'interpolator',
|
|
204
|
+
'backendConnector',
|
|
205
|
+
'i18nFormat',
|
|
206
|
+
'utils'
|
|
207
|
+
]));
|
|
208
|
+
nativeSnapshotOrigins.set(instance, {
|
|
209
|
+
instance,
|
|
210
|
+
original: detachInitOptions(original),
|
|
211
|
+
seed: detachInitOptions(seed),
|
|
212
|
+
unchanged: ()=>ownValue(instance, 'services') === services && ownValue(instance, 'store') === store && ownValue(instance, 'options') === options && ownValue(instance, 'modules') === modules && ownValue(services, 'formatter') === formatter && (formatter && 'object' == typeof formatter ? ownValue(formatter, 'formats') === formats : true) && Object.getOwnPropertyDescriptor(instance, 'translator')?.value === translator && (translator && 'object' == typeof translator ? ownValue(translator, 'language') === ownValue(instance, 'language') : true) && links.every((proof)=>proof()) && proofs.every((proof)=>proof())
|
|
213
|
+
});
|
|
214
|
+
} catch {}
|
|
215
|
+
}
|
|
216
|
+
function createNativeI18nInstance(initOptions) {
|
|
217
|
+
const original = detachInitOptions(initOptions);
|
|
218
|
+
const seed = {
|
|
219
|
+
...detachInitOptions(original),
|
|
220
|
+
initAsync: false,
|
|
221
|
+
partialBundledLanguages: false,
|
|
222
|
+
interpolation: {
|
|
223
|
+
escapeValue: false,
|
|
224
|
+
...original.interpolation
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
const instance = i18next.createInstance();
|
|
228
|
+
instance.init(detachInitOptions(seed));
|
|
229
|
+
if (!instance.isInitialized || instance.isInitializing) throw new Error('Native i18n instances require complete bundled resources');
|
|
230
|
+
recordNativeSnapshotOrigin(instance, original, seed);
|
|
231
|
+
return instance;
|
|
232
|
+
}
|
|
233
|
+
function getNativeI18nSnapshotSeed(instance) {
|
|
234
|
+
const origin = 'object' == typeof instance && null !== instance ? nativeSnapshotOrigins.get(instance) : void 0;
|
|
235
|
+
const services = origin ? ownValue(origin.instance, 'services') : void 0;
|
|
236
|
+
const connector = services && 'object' == typeof services ? ownValue(services, 'backendConnector') : void 0;
|
|
237
|
+
const state = connector && 'object' == typeof connector ? ownValue(connector, 'state') : void 0;
|
|
238
|
+
const pending = plainRecord(state) && Object.values(Object.getOwnPropertyDescriptors(state)).some((descriptor)=>!('value' in descriptor) || 1 === descriptor.value);
|
|
239
|
+
if (!origin || true !== ownValue(origin.instance, 'isInitialized') || true === ownValue(origin.instance, 'isInitializing') || ownValue(origin.instance, 'isLanguageChangingTo') || pending || !origin.unchanged()) throw new Error('This i18n instance requires an explicit Worker profile');
|
|
240
|
+
return detachInitOptions(origin.seed);
|
|
241
|
+
}
|
|
6
242
|
function unwrapResource(module) {
|
|
7
243
|
const resource = 'object' == typeof module && null !== module && 'default' in module ? module.default : module;
|
|
8
244
|
return resource;
|
|
@@ -35,7 +271,7 @@ function createNativeI18n(options, loaders) {
|
|
|
35
271
|
async create (language, resources) {
|
|
36
272
|
const instance = i18next.createInstance();
|
|
37
273
|
instance.use(backend);
|
|
38
|
-
|
|
274
|
+
const seed = {
|
|
39
275
|
...initOptions,
|
|
40
276
|
lng: language,
|
|
41
277
|
fallbackLng: routing.fallbackLanguage,
|
|
@@ -52,14 +288,44 @@ function createNativeI18n(options, loaders) {
|
|
|
52
288
|
escapeValue: false,
|
|
53
289
|
...initOptions.interpolation
|
|
54
290
|
}
|
|
55
|
-
}
|
|
291
|
+
};
|
|
292
|
+
let original;
|
|
293
|
+
let detachedSeed;
|
|
294
|
+
try {
|
|
295
|
+
original = detachInitOptions(initOptions);
|
|
296
|
+
detachedSeed = detachInitOptions(seed);
|
|
297
|
+
} catch {}
|
|
298
|
+
await instance.init(seed);
|
|
299
|
+
if (original && detachedSeed) recordNativeSnapshotOrigin(instance, original, detachedSeed);
|
|
56
300
|
return instance;
|
|
57
301
|
},
|
|
58
|
-
rewrite: (getLanguage)=>
|
|
59
|
-
|
|
302
|
+
rewrite: (getLanguage)=>{
|
|
303
|
+
const languages = [
|
|
304
|
+
...routing.languages
|
|
305
|
+
];
|
|
306
|
+
const localisedUrls = detachLocalisedUrls(routing.localisedUrls);
|
|
307
|
+
const rewrite = createI18nUrlRewrite({
|
|
308
|
+
languages,
|
|
60
309
|
getLanguage,
|
|
61
|
-
localisedUrls
|
|
62
|
-
})
|
|
310
|
+
localisedUrls
|
|
311
|
+
});
|
|
312
|
+
const unchanged = descriptorProof(rewrite);
|
|
313
|
+
nativeRewriteOrigins.set(rewrite, ()=>{
|
|
314
|
+
if (!unchanged()) throw new Error('Native i18n rewrite was modified');
|
|
315
|
+
const language = getLanguage();
|
|
316
|
+
if (!languages.includes(language)) throw new Error('Native i18n rewrite language is unsupported');
|
|
317
|
+
return {
|
|
318
|
+
language,
|
|
319
|
+
languages: [
|
|
320
|
+
...languages
|
|
321
|
+
],
|
|
322
|
+
...void 0 === localisedUrls ? {} : {
|
|
323
|
+
localisedUrls: detachLocalisedUrls(localisedUrls)
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
});
|
|
327
|
+
return rewrite;
|
|
328
|
+
},
|
|
63
329
|
handoff (language, instance) {
|
|
64
330
|
return createI18nSsrHandoffInlineData({
|
|
65
331
|
language,
|
|
@@ -109,4 +375,4 @@ function createNativeI18n(options, loaders) {
|
|
|
109
375
|
}
|
|
110
376
|
};
|
|
111
377
|
}
|
|
112
|
-
export { createNativeI18n };
|
|
378
|
+
export { createNativeI18n, createNativeI18nInstance, getNativeI18nRewriteSnapshot, getNativeI18nSnapshotSeed, invalidateNativeI18nSnapshot };
|