/
index.js
165 lines (143 loc) · 5.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { defaults, debounce, getPath, setPath, pushPath } from './utils.js'
import { readFile, readFileSync } from './readFile.js'
import { writeFile, removeFile } from './writeFile.js'
const getDefaults = () => {
return {
loadPath: '/locales/{{lng}}/{{ns}}.json',
addPath: '/locales/{{lng}}/{{ns}}.missing.json',
ident: 2,
parse: JSON.parse,
stringify: JSON.stringify
// expirationTime: 60 * 60 * 1000
}
}
class Backend {
constructor (services, options = {}, allOptions = {}) {
this.services = services
this.options = options
this.allOptions = allOptions
this.type = 'backend'
this.init(services, options, allOptions)
}
init (services, options = {}, allOptions = {}) {
this.services = services
this.options = defaults(options, this.options || {}, getDefaults())
this.allOptions = allOptions
this.queuedWrites = {}
this.debouncedWrite = debounce(this.write, 250)
}
read (language, namespace, callback) {
let loadPath = this.options.loadPath
if (typeof this.options.loadPath === 'function') {
loadPath = this.options.loadPath(language, namespace)
}
const filename = this.services.interpolator.interpolate(loadPath, { lng: language, ns: namespace })
if (this.allOptions.initImmediate === false) {
try {
const { data, stat } = readFileSync(filename, this.options)
if (this.options.expirationTime && stat && stat.mtime && stat.mtime.getTime() + this.options.expirationTime < Date.now()) {
this.removeFile(language, namespace)
return callback(new Error('File expired!'), false) // no retry
}
callback(null, data)
} catch (err) {
callback(err, false) // no retry
}
return
}
readFile(filename, this.options)
.then(({ data, stat }) => {
if (this.options.expirationTime && stat && stat.mtime && stat.mtime.getTime() + this.options.expirationTime < Date.now()) {
this.removeFile(language, namespace)
return callback(new Error('File expired!'), false) // no retry
}
callback(null, data)
})
.catch((err) => callback(err, false)) // no retry
}
create (languages, namespace, key, fallbackValue, callback) {
if (typeof callback !== 'function') callback = () => {}
if (typeof languages === 'string') languages = [languages]
let todo = languages.length
const done = () => {
if (!--todo) callback()
}
languages.forEach((lng) => {
// eslint-disable-next-line no-useless-call
this.queue.call(this, lng, namespace, key, fallbackValue, done)
})
}
// this way i18next-fs-backend can be used as cache layer in combination with i18next-chained-backend
save (language, namespace, data, callback) {
if (!callback) callback = () => {}
const keys = Object.keys(data)
let todo = keys.length
const done = () => {
if (!--todo) callback()
}
keys.forEach((key) => {
// eslint-disable-next-line no-useless-call
this.queue.call(this, language, namespace, key, data[key], done)
})
}
removeFile (language, namespace) {
let addPath = this.options.addPath
if (typeof this.options.addPath === 'function') {
addPath = this.options.addPath(language, namespace)
}
const filename = this.services.interpolator.interpolate(addPath, { lng: language, ns: namespace })
removeFile(filename, this.options)
.then(() => {})
.catch(() => {})
}
write () {
for (const lng in this.queuedWrites) {
const namespaces = this.queuedWrites[lng]
if (lng !== 'locks') {
for (const ns in namespaces) {
this.writeFile(lng, ns)
}
}
}
}
writeFile (lng, namespace) {
const lock = getPath(this.queuedWrites, ['locks', lng, namespace])
if (lock) return
let addPath = this.options.addPath
if (typeof this.options.addPath === 'function') {
addPath = this.options.addPath(lng, namespace)
}
const filename = this.services.interpolator.interpolate(addPath, { lng, ns: namespace })
const missings = getPath(this.queuedWrites, [lng, namespace])
setPath(this.queuedWrites, [lng, namespace], [])
if (missings.length) {
// lock
setPath(this.queuedWrites, ['locks', lng, namespace], true)
const proceed = ({ data }) => {
missings.forEach((missing) => {
const path = this.allOptions.keySeparator === false ? [missing.key] : (missing.key.split(this.allOptions.keySeparator || '.'))
setPath(data, path, missing.fallbackValue)
})
const proceedWrite = () => {
// unlock
setPath(this.queuedWrites, ['locks', lng, namespace], false)
missings.forEach((missing) => {
if (missing.callback) missing.callback()
})
// rerun
this.debouncedWrite()
}
writeFile(filename, data, this.options)
.then(proceedWrite)
.catch(proceedWrite)
}
readFile(filename, this.options).then(proceed).catch(() => proceed({ data: {} }))
}
}
queue (lng, namespace, key, fallbackValue, callback) {
pushPath(this.queuedWrites, [lng, namespace], { key, fallbackValue: fallbackValue || '', callback })
this.debouncedWrite()
}
}
Backend.type = 'backend'
export default Backend