libp2r2p 0.8.0 → 0.10.0
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 +72 -45
- package/base16/index.js +6 -3
- package/base36/index.js +12 -8
- package/base62/index.js +15 -11
- package/base64/index.js +18 -2
- package/base93/index.js +19 -7
- package/content-key/event/index.js +40 -12
- package/double-dh/index.js +21 -6
- package/ecdh/index.js +12 -1
- package/error/index.js +32 -0
- package/event/helpers/serialize.js +31 -0
- package/event/index.js +116 -0
- package/idb/index.js +5 -3
- package/idb-queue/index.js +21 -20
- package/index.js +10 -1
- package/key/index.js +31 -18
- package/kind/index.js +244 -0
- package/nip04/index.js +47 -0
- package/nip05/index.js +61 -0
- package/nip19/index.js +176 -43
- package/nip44/helpers.js +95 -0
- package/nip44/index.js +14 -0
- package/nip44-v3/index.js +35 -16
- package/nip46/helpers/frame.js +6 -6
- package/nip46/helpers/url.js +8 -6
- package/nip46/services/bunker-signer.js +7 -6
- package/nip46/services/client.js +8 -7
- package/nip46/services/server-session.js +8 -7
- package/nip46/services/transport.js +9 -8
- package/nip96/index.js +285 -0
- package/nip98/index.js +56 -0
- package/nwt/index.js +241 -0
- package/package.json +22 -5
- package/private-channel/helpers/chunks.js +7 -6
- package/private-channel/helpers/event.js +5 -4
- package/private-channel/index.js +63 -61
- package/private-channel/services/received-chunks.js +4 -3
- package/private-message/index.js +32 -31
- package/private-messenger/index.js +23 -22
- package/private-messenger/recovery/index.js +15 -14
- package/private-messenger/services/channel-state.js +2 -1
- package/relay/helpers/hll.js +3 -1
- package/relay/services/query.js +3 -3
- package/relay/services/relay-connection.js +222 -121
- package/relay/services/relay-pool.js +13 -10
- package/temporary-storage/index.js +3 -1
- package/url/index.js +131 -0
- package/web-storage-queue/index.js +8 -6
- package/i18n/index.js +0 -235
package/i18n/index.js
DELETED
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
const DEFAULT_LOCALE = 'en'
|
|
2
|
-
const INTERPOLATION_RE = /{{\s*([A-Za-z0-9_.-]+)\s*}}/g
|
|
3
|
-
|
|
4
|
-
function assertLocales (locales) {
|
|
5
|
-
if (!locales || typeof locales !== 'object' || Array.isArray(locales)) {
|
|
6
|
-
throw new TypeError('locales should be an object')
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function placeholderSignature (value) {
|
|
11
|
-
return [...String(value).matchAll(INTERPOLATION_RE)]
|
|
12
|
-
.map(match => match[1])
|
|
13
|
-
.sort()
|
|
14
|
-
.join(',')
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function validateTranslationValue (key, locale, value, expectedPlaceholders) {
|
|
18
|
-
if (typeof value === 'string') {
|
|
19
|
-
if (placeholderSignature(value) !== expectedPlaceholders) {
|
|
20
|
-
throw new Error(`placeholder mismatch for "${key}" (${locale})`)
|
|
21
|
-
}
|
|
22
|
-
return
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (!value || typeof value !== 'object' || Array.isArray(value) || typeof value.other !== 'string') {
|
|
26
|
-
throw new TypeError(`translation for "${key}" (${locale}) should be a string or plural object with an other form`)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
for (const form of Object.values(value)) {
|
|
30
|
-
if (typeof form !== 'string') {
|
|
31
|
-
throw new TypeError(`plural forms for "${key}" (${locale}) should be strings`)
|
|
32
|
-
}
|
|
33
|
-
if (placeholderSignature(form) !== expectedPlaceholders) {
|
|
34
|
-
throw new Error(`placeholder mismatch for "${key}" (${locale})`)
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function validateLocales (locales, options = {}) {
|
|
40
|
-
assertLocales(locales)
|
|
41
|
-
if (!options || typeof options !== 'object' || Array.isArray(options)) {
|
|
42
|
-
throw new TypeError('validation options should be an object')
|
|
43
|
-
}
|
|
44
|
-
const {
|
|
45
|
-
requiredLocales = [],
|
|
46
|
-
referenceLocale = DEFAULT_LOCALE,
|
|
47
|
-
requireReferenceKey = false
|
|
48
|
-
} = options
|
|
49
|
-
if (!Array.isArray(requiredLocales) || requiredLocales.some(locale => typeof locale !== 'string' || !locale)) {
|
|
50
|
-
throw new TypeError('requiredLocales should be an array of non-empty strings')
|
|
51
|
-
}
|
|
52
|
-
if (typeof referenceLocale !== 'string' || !referenceLocale) {
|
|
53
|
-
throw new TypeError('referenceLocale should be a non-empty string')
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
for (const [key, translations] of Object.entries(locales)) {
|
|
57
|
-
if (!translations || typeof translations !== 'object' || Array.isArray(translations)) {
|
|
58
|
-
throw new TypeError(`translations for "${key}" should be an object`)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
for (const locale of requiredLocales) {
|
|
62
|
-
if (!Object.prototype.hasOwnProperty.call(translations, locale)) {
|
|
63
|
-
throw new Error(`missing translation for "${key}" (${locale})`)
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const expectedPlaceholders = placeholderSignature(key)
|
|
68
|
-
for (const [locale, value] of Object.entries(translations)) {
|
|
69
|
-
validateTranslationValue(key, locale, value, expectedPlaceholders)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (requireReferenceKey) {
|
|
73
|
-
if (!Object.prototype.hasOwnProperty.call(translations, referenceLocale)) {
|
|
74
|
-
throw new Error(`missing reference translation for "${key}" (${referenceLocale})`)
|
|
75
|
-
}
|
|
76
|
-
const reference = translations[referenceLocale]
|
|
77
|
-
const referenceValue = typeof reference === 'string' ? reference : reference.other
|
|
78
|
-
if (referenceValue !== key) {
|
|
79
|
-
throw new Error(`reference translation should match key "${key}" (${referenceLocale})`)
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return locales
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function canonicalizeLocale (locale) {
|
|
88
|
-
if (typeof locale !== 'string' || !locale.trim()) return null
|
|
89
|
-
const value = locale.trim().replace(/_/g, '-')
|
|
90
|
-
try {
|
|
91
|
-
return Intl.getCanonicalLocales(value)[0] ?? null
|
|
92
|
-
} catch {
|
|
93
|
-
return null
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function getIntlLocale () {
|
|
98
|
-
try {
|
|
99
|
-
return Intl.DateTimeFormat().resolvedOptions().locale
|
|
100
|
-
} catch {
|
|
101
|
-
return null
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function getNavigator () {
|
|
106
|
-
try {
|
|
107
|
-
return globalThis.navigator
|
|
108
|
-
} catch {
|
|
109
|
-
return null
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export function getCurrentDeviceLocale () {
|
|
114
|
-
const navigator = getNavigator()
|
|
115
|
-
const candidates = [
|
|
116
|
-
getIntlLocale(),
|
|
117
|
-
navigator?.language,
|
|
118
|
-
navigator?.languages?.[0],
|
|
119
|
-
DEFAULT_LOCALE
|
|
120
|
-
]
|
|
121
|
-
|
|
122
|
-
for (const candidate of candidates) {
|
|
123
|
-
const locale = canonicalizeLocale(candidate)
|
|
124
|
-
if (locale) return locale
|
|
125
|
-
}
|
|
126
|
-
return DEFAULT_LOCALE
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function localeLanguage (locale) {
|
|
130
|
-
try {
|
|
131
|
-
return new Intl.Locale(locale).language
|
|
132
|
-
} catch {
|
|
133
|
-
return locale.split('-')[0].toLowerCase()
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function preferredChineseLocale (locale) {
|
|
138
|
-
if (localeLanguage(locale) !== 'zh') return null
|
|
139
|
-
try {
|
|
140
|
-
const { script, region } = new Intl.Locale(locale)
|
|
141
|
-
if (script === 'Hant' || ['TW', 'HK', 'MO'].includes(region)) return 'zh-TW'
|
|
142
|
-
return 'zh-CN'
|
|
143
|
-
} catch {
|
|
144
|
-
return /(?:^|-)(?:hant|tw|hk|mo)(?:-|$)/i.test(locale) ? 'zh-TW' : 'zh-CN'
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
function localeCandidates (translations, requestedLocale, fallbackLocale) {
|
|
149
|
-
const keys = Object.keys(translations)
|
|
150
|
-
const canonicalKeys = new Map()
|
|
151
|
-
for (const key of keys) {
|
|
152
|
-
const canonical = canonicalizeLocale(key)
|
|
153
|
-
if (canonical && !canonicalKeys.has(canonical.toLowerCase())) {
|
|
154
|
-
canonicalKeys.set(canonical.toLowerCase(), { key, canonical })
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const result = []
|
|
159
|
-
const seen = new Set()
|
|
160
|
-
const add = locale => {
|
|
161
|
-
const canonical = canonicalizeLocale(locale)
|
|
162
|
-
const match = canonical && canonicalKeys.get(canonical.toLowerCase())
|
|
163
|
-
if (match && !seen.has(match.key)) {
|
|
164
|
-
seen.add(match.key)
|
|
165
|
-
result.push(match)
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
const addCompatible = locale => {
|
|
169
|
-
const canonical = canonicalizeLocale(locale)
|
|
170
|
-
if (!canonical) return
|
|
171
|
-
add(canonical)
|
|
172
|
-
add(preferredChineseLocale(canonical))
|
|
173
|
-
const language = localeLanguage(canonical)
|
|
174
|
-
const match = [...canonicalKeys.values()].find(candidate => localeLanguage(candidate.canonical) === language)
|
|
175
|
-
if (match) add(match.canonical)
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
addCompatible(requestedLocale)
|
|
179
|
-
addCompatible(fallbackLocale)
|
|
180
|
-
addCompatible(DEFAULT_LOCALE)
|
|
181
|
-
return result
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
function selectPlural (forms, locale, values) {
|
|
185
|
-
if (!forms || typeof forms !== 'object' || Array.isArray(forms)) return null
|
|
186
|
-
const count = Number(values?.count)
|
|
187
|
-
if (!Number.isFinite(count)) return null
|
|
188
|
-
|
|
189
|
-
let category = 'other'
|
|
190
|
-
try {
|
|
191
|
-
category = new Intl.PluralRules(locale).select(count)
|
|
192
|
-
} catch {}
|
|
193
|
-
const value = forms[category] ?? forms.other
|
|
194
|
-
return typeof value === 'string' ? value : null
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
function selectTemplate (translations, requestedLocale, fallbackLocale, values) {
|
|
198
|
-
if (!translations || typeof translations !== 'object' || Array.isArray(translations)) return null
|
|
199
|
-
for (const { key, canonical } of localeCandidates(translations, requestedLocale, fallbackLocale)) {
|
|
200
|
-
const value = translations[key]
|
|
201
|
-
if (typeof value === 'string') return value
|
|
202
|
-
const plural = selectPlural(value, canonical, values)
|
|
203
|
-
if (plural !== null) return plural
|
|
204
|
-
}
|
|
205
|
-
return null
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
function interpolate (template, values) {
|
|
209
|
-
const source = String(template)
|
|
210
|
-
if (!values || typeof values !== 'object') return source
|
|
211
|
-
return source.replace(INTERPOLATION_RE, (token, name) => (
|
|
212
|
-
Object.prototype.hasOwnProperty.call(values, name)
|
|
213
|
-
? String(values[name])
|
|
214
|
-
: token
|
|
215
|
-
))
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
export function getT (locales, {
|
|
219
|
-
locale = getCurrentDeviceLocale(),
|
|
220
|
-
fallbackLocale = DEFAULT_LOCALE,
|
|
221
|
-
validation
|
|
222
|
-
} = {}) {
|
|
223
|
-
assertLocales(locales)
|
|
224
|
-
if (validation !== undefined) validateLocales(locales, validation)
|
|
225
|
-
|
|
226
|
-
const requestedLocale = canonicalizeLocale(locale) ?? DEFAULT_LOCALE
|
|
227
|
-
const canonicalFallback = canonicalizeLocale(fallbackLocale) ?? DEFAULT_LOCALE
|
|
228
|
-
|
|
229
|
-
return function t (key, values) {
|
|
230
|
-
if (typeof key !== 'string') throw new TypeError('translation key should be a string')
|
|
231
|
-
const translations = Object.prototype.hasOwnProperty.call(locales, key) ? locales[key] : null
|
|
232
|
-
const template = selectTemplate(translations, requestedLocale, canonicalFallback, values) ?? key
|
|
233
|
-
return interpolate(template, values)
|
|
234
|
-
}
|
|
235
|
-
}
|