locize-cli 11.0.0 → 12.0.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/CHANGELOG.md +12 -0
- package/LICENSE +1 -1
- package/README.md +1 -0
- package/dist/cjs/add.js +90 -0
- package/{bin/locize → dist/cjs/cli.js} +390 -670
- package/dist/cjs/combineSubkeyPreprocessor.js +155 -0
- package/dist/cjs/convertToDesiredFormat.js +205 -0
- package/dist/cjs/convertToFlatFormat.js +231 -0
- package/dist/cjs/copyVersion.js +60 -0
- package/dist/cjs/createBranch.js +59 -0
- package/dist/cjs/deleteBranch.js +89 -0
- package/dist/cjs/deleteNamespace.js +37 -0
- package/dist/cjs/download.js +376 -0
- package/dist/cjs/filterNamespaces.js +13 -0
- package/dist/cjs/format.js +156 -0
- package/dist/cjs/formats.js +33 -0
- package/dist/cjs/get.js +66 -0
- package/dist/cjs/getBranches.js +37 -0
- package/dist/cjs/getJob.js +37 -0
- package/dist/cjs/getProjectStats.js +37 -0
- package/dist/cjs/getRemoteLanguages.js +38 -0
- package/dist/cjs/getRemoteNamespace.js +125 -0
- package/dist/cjs/index.js +37 -0
- package/dist/cjs/isValidUuid.js +6 -0
- package/dist/cjs/lngs.js +215 -0
- package/dist/cjs/mapLimit.js +22 -0
- package/dist/cjs/mergeBranch.js +80 -0
- package/dist/cjs/migrate.js +239 -0
- package/dist/cjs/missing.js +162 -0
- package/dist/cjs/package.json +5 -0
- package/{parseLocalLanguage.js → dist/cjs/parseLocalLanguage.js} +135 -142
- package/dist/cjs/parseLocalLanguages.js +18 -0
- package/dist/cjs/parseLocalReference.js +11 -0
- package/dist/cjs/publishVersion.js +42 -0
- package/dist/cjs/removeUndefinedFromArrays.js +19 -0
- package/dist/cjs/removeVersion.js +42 -0
- package/dist/cjs/request.js +66 -0
- package/dist/cjs/shouldUnflatten.js +21 -0
- package/dist/cjs/sortFlatResources.js +13 -0
- package/dist/cjs/sync.js +772 -0
- package/dist/cjs/unflatten.js +81 -0
- package/dist/esm/add.js +88 -0
- package/dist/esm/cli.js +1020 -0
- package/{combineSubkeyPreprocessor.js → dist/esm/combineSubkeyPreprocessor.js} +70 -73
- package/dist/esm/convertToDesiredFormat.js +203 -0
- package/dist/esm/convertToFlatFormat.js +229 -0
- package/dist/esm/copyVersion.js +58 -0
- package/dist/esm/createBranch.js +57 -0
- package/dist/esm/deleteBranch.js +87 -0
- package/dist/esm/deleteNamespace.js +35 -0
- package/dist/esm/download.js +374 -0
- package/{filterNamespaces.js → dist/esm/filterNamespaces.js} +4 -4
- package/dist/esm/format.js +154 -0
- package/{formats.js → dist/esm/formats.js} +7 -11
- package/dist/esm/get.js +64 -0
- package/dist/esm/getBranches.js +35 -0
- package/dist/esm/getJob.js +35 -0
- package/dist/esm/getProjectStats.js +35 -0
- package/dist/esm/getRemoteLanguages.js +36 -0
- package/dist/esm/getRemoteNamespace.js +123 -0
- package/dist/esm/index.js +16 -0
- package/dist/esm/isValidUuid.js +4 -0
- package/dist/esm/lngs.js +213 -0
- package/dist/esm/mapLimit.js +20 -0
- package/dist/esm/mergeBranch.js +78 -0
- package/dist/esm/migrate.js +237 -0
- package/dist/esm/missing.js +160 -0
- package/dist/esm/parseLocalLanguage.js +194 -0
- package/dist/esm/parseLocalLanguages.js +16 -0
- package/dist/esm/parseLocalReference.js +9 -0
- package/dist/esm/publishVersion.js +40 -0
- package/{removeUndefinedFromArrays.js → dist/esm/removeUndefinedFromArrays.js} +5 -5
- package/dist/esm/removeVersion.js +40 -0
- package/dist/esm/request.js +64 -0
- package/{shouldUnflatten.js → dist/esm/shouldUnflatten.js} +7 -7
- package/dist/esm/sortFlatResources.js +11 -0
- package/dist/esm/sync.js +770 -0
- package/{unflatten.js → dist/esm/unflatten.js} +36 -34
- package/package.json +39 -18
- package/rollup.config.js +57 -0
- package/add.js +0 -105
- package/convertToDesiredFormat.js +0 -268
- package/convertToFlatFormat.js +0 -322
- package/copyVersion.js +0 -69
- package/createBranch.js +0 -61
- package/deleteBranch.js +0 -97
- package/deleteNamespace.js +0 -39
- package/download.js +0 -516
- package/format.js +0 -206
- package/get.js +0 -81
- package/getBranches.js +0 -40
- package/getJob.js +0 -40
- package/getProjectStats.js +0 -40
- package/getRemoteLanguages.js +0 -40
- package/getRemoteNamespace.js +0 -122
- package/index.js +0 -9
- package/isValidUuid.js +0 -2
- package/lngs.json +0 -211
- package/mergeBranch.js +0 -102
- package/migrate.js +0 -314
- package/missing.js +0 -169
- package/parseLocalLanguages.js +0 -22
- package/parseLocalReference.js +0 -10
- package/publishVersion.js +0 -64
- package/removeVersion.js +0 -64
- package/request.js +0 -64
- package/sortFlatResources.js +0 -9
- package/sync.js +0 -786
package/missing.js
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
const colors = require('colors')
|
|
2
|
-
const async = require('async')
|
|
3
|
-
const path = require('path')
|
|
4
|
-
const request = require('./request')
|
|
5
|
-
const formats = require('./formats')
|
|
6
|
-
const reversedFileExtensionsMap = formats.reversedFileExtensionsMap
|
|
7
|
-
const getRemoteLanguages = require('./getRemoteLanguages')
|
|
8
|
-
const parseLocalReference = require('./parseLocalReference')
|
|
9
|
-
const parseLocalLanguages = require('./parseLocalLanguages')
|
|
10
|
-
const getRemoteNamespace = require('./getRemoteNamespace')
|
|
11
|
-
|
|
12
|
-
const compareNamespace = (local, remote) => {
|
|
13
|
-
const diff = {
|
|
14
|
-
toAdd: []
|
|
15
|
-
}
|
|
16
|
-
local = local || {}
|
|
17
|
-
remote = remote || {}
|
|
18
|
-
Object.keys(local).forEach((k) => {
|
|
19
|
-
if (remote[k] === '' && local[k] === '') return
|
|
20
|
-
if (!remote[k]) {
|
|
21
|
-
diff.toAdd.push(k)
|
|
22
|
-
}
|
|
23
|
-
})
|
|
24
|
-
return diff
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const compareNamespaces = (opt, localNamespaces, cb) => {
|
|
28
|
-
async.map(localNamespaces, (ns, clb) => {
|
|
29
|
-
getRemoteNamespace(opt, ns.language, ns.namespace, (err, remoteNamespace) => {
|
|
30
|
-
if (err) return clb(err)
|
|
31
|
-
|
|
32
|
-
const diff = compareNamespace(ns.content, remoteNamespace)
|
|
33
|
-
ns.diff = diff
|
|
34
|
-
ns.remoteContent = remoteNamespace
|
|
35
|
-
clb(null, ns)
|
|
36
|
-
})
|
|
37
|
-
}, cb)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const saveMissing = (opt, lng, ns, cb) => {
|
|
41
|
-
const data = {}
|
|
42
|
-
ns.diff.toAdd.forEach((k) => { data[k] = ns.content[k] })
|
|
43
|
-
|
|
44
|
-
if (Object.keys(data).length === 0) return cb(null)
|
|
45
|
-
|
|
46
|
-
if (opt.dry) return cb(null)
|
|
47
|
-
|
|
48
|
-
const payloadKeysLimit = 1000
|
|
49
|
-
|
|
50
|
-
function send (d, clb, isRetrying) {
|
|
51
|
-
request(opt.apiEndpoint + '/missing/' + opt.projectId + '/' + opt.version + '/' + lng + '/' + ns.namespace, {
|
|
52
|
-
method: 'post',
|
|
53
|
-
body: d,
|
|
54
|
-
headers: {
|
|
55
|
-
Authorization: opt.apiKey
|
|
56
|
-
}
|
|
57
|
-
}, (err, res, obj) => {
|
|
58
|
-
if (err) return clb(err)
|
|
59
|
-
if (res.status === 504 && !isRetrying) {
|
|
60
|
-
return setTimeout(() => send(d, clb, true), 3000)
|
|
61
|
-
}
|
|
62
|
-
if (res.status >= 300 && res.status !== 412) {
|
|
63
|
-
if (obj && (obj.errorMessage || obj.message)) {
|
|
64
|
-
return clb(new Error((obj.errorMessage || obj.message)))
|
|
65
|
-
}
|
|
66
|
-
return clb(new Error(res.statusText + ' (' + res.status + ')'))
|
|
67
|
-
}
|
|
68
|
-
setTimeout(() => clb(null), 1000)
|
|
69
|
-
})
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (Object.keys(data).length > payloadKeysLimit) {
|
|
73
|
-
const tasks = []
|
|
74
|
-
const keysInObj = Object.keys(data)
|
|
75
|
-
|
|
76
|
-
while (keysInObj.length > payloadKeysLimit) {
|
|
77
|
-
(function () {
|
|
78
|
-
const pagedData = {}
|
|
79
|
-
keysInObj.splice(0, payloadKeysLimit).forEach((k) => { pagedData[k] = data[k] })
|
|
80
|
-
tasks.push((c) => send(pagedData, c))
|
|
81
|
-
})()
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (keysInObj.length === 0) return cb(null)
|
|
85
|
-
|
|
86
|
-
const finalPagedData = {}
|
|
87
|
-
keysInObj.splice(0, keysInObj.length).forEach((k) => { finalPagedData[k] = data[k] })
|
|
88
|
-
tasks.push((c) => send(finalPagedData, c))
|
|
89
|
-
|
|
90
|
-
async.series(tasks, cb)
|
|
91
|
-
return
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
send(data, cb)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const handleError = (err, cb) => {
|
|
98
|
-
if (!cb && err) {
|
|
99
|
-
console.error(colors.red(err.stack))
|
|
100
|
-
process.exit(1)
|
|
101
|
-
}
|
|
102
|
-
if (cb) cb(err)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const handleMissing = (opt, localNamespaces, cb) => {
|
|
106
|
-
if (!localNamespaces || localNamespaces.length === 0) {
|
|
107
|
-
return handleError(new Error('No local namespaces found!'))
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
compareNamespaces(opt, localNamespaces, (err, compared) => {
|
|
111
|
-
if (err) return handleError(err)
|
|
112
|
-
|
|
113
|
-
async.eachLimit(compared, Math.round(require('os').cpus().length / 2), (ns, clb) => {
|
|
114
|
-
if (!cb) {
|
|
115
|
-
if (ns.diff.toAdd.length > 0) {
|
|
116
|
-
console.log(colors.green(`adding ${ns.diff.toAdd.length} keys in ${ns.language}/${ns.namespace}...`))
|
|
117
|
-
if (opt.dry) console.log(colors.green(`would add ${ns.diff.toAdd.join(', ')} in ${ns.language}/${ns.namespace}...`))
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
saveMissing(opt, ns.language, ns, clb)
|
|
121
|
-
}, (err) => {
|
|
122
|
-
if (err) return handleError(err)
|
|
123
|
-
if (!cb) console.log(colors.green('FINISHED'))
|
|
124
|
-
if (cb) cb(null)
|
|
125
|
-
})
|
|
126
|
-
})
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const missing = (opt, cb) => {
|
|
130
|
-
if (!reversedFileExtensionsMap[opt.format]) {
|
|
131
|
-
return handleError(new Error(`${opt.format} is not a valid format!`))
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
if (opt.namespace && opt.namespace.indexOf(',') > 0) {
|
|
135
|
-
opt.namespaces = opt.namespace.split(',')
|
|
136
|
-
delete opt.namespace
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
opt.pathMaskInterpolationPrefix = opt.pathMaskInterpolationPrefix || '{{'
|
|
140
|
-
opt.pathMaskInterpolationSuffix = opt.pathMaskInterpolationSuffix || '}}'
|
|
141
|
-
opt.pathMask = opt.pathMask || `${opt.pathMaskInterpolationPrefix}language${opt.pathMaskInterpolationSuffix}${path.sep}${opt.pathMaskInterpolationPrefix}namespace${opt.pathMaskInterpolationSuffix}`
|
|
142
|
-
opt.languageFolderPrefix = opt.languageFolderPrefix || ''
|
|
143
|
-
opt.pathMask = opt.pathMask.replace(`${opt.pathMaskInterpolationPrefix}language${opt.pathMaskInterpolationSuffix}`, `${opt.languageFolderPrefix}${opt.pathMaskInterpolationPrefix}language${opt.pathMaskInterpolationSuffix}`)
|
|
144
|
-
|
|
145
|
-
getRemoteLanguages(opt, (err, remoteLanguages) => {
|
|
146
|
-
if (err) return handleError(err)
|
|
147
|
-
|
|
148
|
-
if (opt.referenceLanguageOnly && opt.language && opt.referenceLanguage !== opt.language) {
|
|
149
|
-
opt.referenceLanguage = opt.language
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (opt.referenceLanguageOnly) {
|
|
153
|
-
parseLocalReference(opt, (err, localNamespaces) => {
|
|
154
|
-
if (err) return handleError(err)
|
|
155
|
-
|
|
156
|
-
handleMissing(opt, localNamespaces, cb)
|
|
157
|
-
})
|
|
158
|
-
return
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
parseLocalLanguages(opt, remoteLanguages, (err, localNamespaces) => {
|
|
162
|
-
if (err) return handleError(err)
|
|
163
|
-
|
|
164
|
-
handleMissing(opt, localNamespaces, cb)
|
|
165
|
-
})
|
|
166
|
-
})
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
module.exports = missing
|
package/parseLocalLanguages.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
const async = require('async')
|
|
2
|
-
const parseLocalLanguage = require('./parseLocalLanguage')
|
|
3
|
-
const filterNamespaces = require('./filterNamespaces')
|
|
4
|
-
|
|
5
|
-
const parseLocalLanguages = (opt, lngs, cb) => {
|
|
6
|
-
let res = []
|
|
7
|
-
async.each(lngs, (lng, clb) => {
|
|
8
|
-
if (opt.language && (lng !== opt.language && lng !== opt.referenceLanguage)) {
|
|
9
|
-
return clb()
|
|
10
|
-
}
|
|
11
|
-
parseLocalLanguage(opt, lng, (err, nss) => {
|
|
12
|
-
if (err) return clb(err)
|
|
13
|
-
res = res.concat(filterNamespaces(opt, nss))
|
|
14
|
-
clb()
|
|
15
|
-
})
|
|
16
|
-
}, (err) => {
|
|
17
|
-
if (err) return cb(err)
|
|
18
|
-
cb(null, res)
|
|
19
|
-
})
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
module.exports = parseLocalLanguages
|
package/parseLocalReference.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
const parseLocalLanguage = require('./parseLocalLanguage')
|
|
2
|
-
const filterNamespaces = require('./filterNamespaces')
|
|
3
|
-
|
|
4
|
-
const parseLocalReference = (opt, cb) => parseLocalLanguage(opt, opt.referenceLanguage, (err, nss) => {
|
|
5
|
-
if (err) return cb(err)
|
|
6
|
-
|
|
7
|
-
cb(err, filterNamespaces(opt, nss).filter((n) => n.language === opt.referenceLanguage))
|
|
8
|
-
})
|
|
9
|
-
|
|
10
|
-
module.exports = parseLocalReference
|
package/publishVersion.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
const colors = require('colors')
|
|
2
|
-
const request = require('./request')
|
|
3
|
-
const getJob = require('./getJob')
|
|
4
|
-
|
|
5
|
-
const publishVersion = (opt, cb) => {
|
|
6
|
-
request(opt.apiEndpoint + '/publish/' + opt.projectId + '/' + opt.version + (opt.tenants ? '?tenants=true' : ''), {
|
|
7
|
-
method: 'post',
|
|
8
|
-
headers: {
|
|
9
|
-
Authorization: opt.apiKey
|
|
10
|
-
}
|
|
11
|
-
}, (err, res, obj) => {
|
|
12
|
-
if (err || (obj && (obj.errorMessage || obj.message))) {
|
|
13
|
-
if (!cb) console.log(colors.red(`publishing failed for ${opt.version}...`))
|
|
14
|
-
|
|
15
|
-
if (err) {
|
|
16
|
-
if (!cb) { console.error(colors.red(err.message)); process.exit(1) }
|
|
17
|
-
if (cb) cb(err)
|
|
18
|
-
return
|
|
19
|
-
}
|
|
20
|
-
if (obj && (obj.errorMessage || obj.message)) {
|
|
21
|
-
if (!cb) { console.error(colors.red((obj.errorMessage || obj.message))); process.exit(1) }
|
|
22
|
-
if (cb) cb(new Error((obj.errorMessage || obj.message)))
|
|
23
|
-
return
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
if (res.status >= 300) {
|
|
27
|
-
if (!cb) { console.error(colors.red(res.statusText + ' (' + res.status + ')')); process.exit(1) }
|
|
28
|
-
if (cb) cb(new Error(res.statusText + ' (' + res.status + ')'))
|
|
29
|
-
return
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (!obj || !obj.jobId) {
|
|
33
|
-
if (!cb) { console.error(colors.red('No jobId! Something went wrong!')); process.exit(1) }
|
|
34
|
-
if (cb) cb(new Error('No jobId! Something went wrong!'))
|
|
35
|
-
return
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
(function waitForJob () {
|
|
39
|
-
getJob(opt, obj.jobId, (err, job) => {
|
|
40
|
-
if (err) {
|
|
41
|
-
if (!cb) { console.error(colors.red(err.message)); process.exit(1) }
|
|
42
|
-
if (cb) cb(err)
|
|
43
|
-
return
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (job && !job.timeouted) {
|
|
47
|
-
setTimeout(waitForJob, 2000)
|
|
48
|
-
return
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (job && job.timeouted) {
|
|
52
|
-
if (!cb) { console.error(colors.red('Job timeouted!')); process.exit(1) }
|
|
53
|
-
if (cb) cb(new Error('Job timeouted!'))
|
|
54
|
-
return
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (!cb) console.log(colors.green(`publishing for ${opt.version} succesfully requested`))
|
|
58
|
-
if (cb) cb(null)
|
|
59
|
-
})
|
|
60
|
-
})()
|
|
61
|
-
})
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
module.exports = publishVersion
|
package/removeVersion.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
const colors = require('colors')
|
|
2
|
-
const request = require('./request')
|
|
3
|
-
const getJob = require('./getJob')
|
|
4
|
-
|
|
5
|
-
const removeVersion = (opt, cb) => {
|
|
6
|
-
request(opt.apiEndpoint + '/version/' + opt.projectId + '/' + opt.version, {
|
|
7
|
-
method: 'delete',
|
|
8
|
-
headers: {
|
|
9
|
-
Authorization: opt.apiKey
|
|
10
|
-
}
|
|
11
|
-
}, (err, res, obj) => {
|
|
12
|
-
if (err || (obj && (obj.errorMessage || obj.message))) {
|
|
13
|
-
if (!cb) console.log(colors.red(`remove failed for version ${opt.version}...`))
|
|
14
|
-
|
|
15
|
-
if (err) {
|
|
16
|
-
if (!cb) { console.error(colors.red(err.message)); process.exit(1) }
|
|
17
|
-
if (cb) cb(err)
|
|
18
|
-
return
|
|
19
|
-
}
|
|
20
|
-
if (obj && (obj.errorMessage || obj.message)) {
|
|
21
|
-
if (!cb) { console.error(colors.red((obj.errorMessage || obj.message))); process.exit(1) }
|
|
22
|
-
if (cb) cb(new Error((obj.errorMessage || obj.message)))
|
|
23
|
-
return
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
if (res.status >= 300) {
|
|
27
|
-
if (!cb) { console.error(colors.red(res.statusText + ' (' + res.status + ')')); process.exit(1) }
|
|
28
|
-
if (cb) cb(new Error(res.statusText + ' (' + res.status + ')'))
|
|
29
|
-
return
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (!obj || !obj.jobId) {
|
|
33
|
-
if (!cb) { console.error(colors.red('No jobId! Something went wrong!')); process.exit(1) }
|
|
34
|
-
if (cb) cb(new Error('No jobId! Something went wrong!'))
|
|
35
|
-
return
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
(function waitForJob () {
|
|
39
|
-
getJob(opt, obj.jobId, (err, job) => {
|
|
40
|
-
if (err) {
|
|
41
|
-
if (!cb) { console.error(colors.red(err.message)); process.exit(1) }
|
|
42
|
-
if (cb) cb(err)
|
|
43
|
-
return
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (job && !job.timeouted) {
|
|
47
|
-
setTimeout(waitForJob, 2000)
|
|
48
|
-
return
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (job && job.timeouted) {
|
|
52
|
-
if (!cb) { console.error(colors.red('Job timeouted!')); process.exit(1) }
|
|
53
|
-
if (cb) cb(new Error('Job timeouted!'))
|
|
54
|
-
return
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (!cb) console.log(colors.green(`remove version ${opt.version} succesfully requested`))
|
|
58
|
-
if (cb) cb(null)
|
|
59
|
-
})
|
|
60
|
-
})()
|
|
61
|
-
})
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
module.exports = removeVersion
|
package/request.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
const pkg = require('./package.json')
|
|
2
|
-
const fetch = require('node-fetch')
|
|
3
|
-
const { HttpsProxyAgent } = require('https-proxy-agent')
|
|
4
|
-
const https = require('https')
|
|
5
|
-
const CacheableLookup = require('cacheable-lookup')
|
|
6
|
-
const cacheable = new CacheableLookup()
|
|
7
|
-
cacheable.install(https.globalAgent)
|
|
8
|
-
|
|
9
|
-
const httpProxy = process.env.http_proxy || process.env.HTTP_PROXY || process.env.https_proxy || process.env.HTTPS_PROXY
|
|
10
|
-
|
|
11
|
-
const isRetriableError = (err) => {
|
|
12
|
-
return err && err.message && (
|
|
13
|
-
err.message.indexOf('ETIMEDOUT') > -1 || // on timeout retry
|
|
14
|
-
err.message.indexOf('FetchError') > -1 ||
|
|
15
|
-
err.code === 'ETIMEDOUT' ||
|
|
16
|
-
// on dns errors
|
|
17
|
-
err.message.indexOf('ENOTFOUND') > -1 ||
|
|
18
|
-
err.message.indexOf('ENODATA') > -1 ||
|
|
19
|
-
err.message.indexOf('ENOENT') > -1 // Windows: name exists, but not this record type
|
|
20
|
-
)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const isJSONResponse = (res) => res.headers.get('content-type') && res.headers.get('content-type').indexOf('json') > 0
|
|
24
|
-
|
|
25
|
-
const handleResponse = (res) => {
|
|
26
|
-
if (isJSONResponse(res)) {
|
|
27
|
-
return new Promise((resolve, reject) => res.json().then((obj) => resolve({ res, obj })).catch(reject))
|
|
28
|
-
} else {
|
|
29
|
-
return { res }
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const handleSuccessful = (callback) => (ret) => callback(null, ret.res, ret.obj)
|
|
34
|
-
|
|
35
|
-
module.exports = (url, options, callback) => {
|
|
36
|
-
if (httpProxy) {
|
|
37
|
-
const httpsProxyAgent = new HttpsProxyAgent(httpProxy)
|
|
38
|
-
cacheable.install(httpsProxyAgent)
|
|
39
|
-
options.agent = httpsProxyAgent
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
options.headers = options.headers || {}
|
|
43
|
-
options.headers['User-Agent'] = `${pkg.name}/v${pkg.version} (node/${process.version}; ${process.platform} ${process.arch})`
|
|
44
|
-
options.headers['X-User-Agent'] = options.headers['User-Agent']
|
|
45
|
-
if (options.body || options.method !== 'get') options.headers['Content-Type'] = 'application/json'
|
|
46
|
-
if (options.body) {
|
|
47
|
-
if (typeof options.body !== 'string') {
|
|
48
|
-
options.body = JSON.stringify(options.body)
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
if (options.headers['Authorization'] === undefined) delete options.headers['Authorization']
|
|
52
|
-
|
|
53
|
-
function retriableFetch (maxRetries) {
|
|
54
|
-
fetch(url, options).then(handleResponse).then(handleSuccessful(callback)).catch((err) => {
|
|
55
|
-
if (maxRetries < 1) return callback(err)
|
|
56
|
-
if (!isRetriableError(err)) return callback(err)
|
|
57
|
-
setTimeout(() => {
|
|
58
|
-
retriableFetch(--maxRetries)
|
|
59
|
-
}, 5000)
|
|
60
|
-
})
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
retriableFetch(3)
|
|
64
|
-
}
|
package/sortFlatResources.js
DELETED