f7eebdfb540e7a39e1c006112140675045e2dfb0.svn-base
11.5 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
var semver = require("semver")
var validateLicense = require('validate-npm-package-license');
var hostedGitInfo = require("hosted-git-info")
var isBuiltinModule = require("resolve").isCore
var depTypes = ["dependencies","devDependencies","optionalDependencies"]
var extractDescription = require("./extract_description")
var url = require("url")
var typos = require("./typos.json")
var fixer = module.exports = {
// default warning function
warn: function() {},
fixRepositoryField: function(data) {
if (data.repositories) {
this.warn("repositories");
data.repository = data.repositories[0]
}
if (!data.repository) return this.warn("missingRepository")
if (typeof data.repository === "string") {
data.repository = {
type: "git",
url: data.repository
}
}
var r = data.repository.url || ""
if (r) {
var hosted = hostedGitInfo.fromUrl(r)
if (hosted) {
r = data.repository.url
= hosted.getDefaultRepresentation() == "shortcut" ? hosted.https() : hosted.toString()
}
}
if (r.match(/github.com\/[^\/]+\/[^\/]+\.git\.git$/)) {
this.warn("brokenGitUrl", r)
}
}
, fixTypos: function(data) {
Object.keys(typos.topLevel).forEach(function (d) {
if (data.hasOwnProperty(d)) {
this.warn("typo", d, typos.topLevel[d])
}
}, this)
}
, fixScriptsField: function(data) {
if (!data.scripts) return
if (typeof data.scripts !== "object") {
this.warn("nonObjectScripts")
delete data.scripts
return
}
Object.keys(data.scripts).forEach(function (k) {
if (typeof data.scripts[k] !== "string") {
this.warn("nonStringScript")
delete data.scripts[k]
} else if (typos.script[k] && !data.scripts[typos.script[k]]) {
this.warn("typo", k, typos.script[k], "scripts")
}
}, this)
}
, fixFilesField: function(data) {
var files = data.files
if (files && !Array.isArray(files)) {
this.warn("nonArrayFiles")
delete data.files
} else if (data.files) {
data.files = data.files.filter(function(file) {
if (!file || typeof file !== "string") {
this.warn("invalidFilename", file)
return false
} else {
return true
}
}, this)
}
}
, fixBinField: function(data) {
if (!data.bin) return;
if (typeof data.bin === "string") {
var b = {}
var match
if (match = data.name.match(/^@[^/]+[/](.*)$/)) {
b[match[1]] = data.bin
} else {
b[data.name] = data.bin
}
data.bin = b
}
}
, fixManField: function(data) {
if (!data.man) return;
if (typeof data.man === "string") {
data.man = [ data.man ]
}
}
, fixBundleDependenciesField: function(data) {
var bdd = "bundledDependencies"
var bd = "bundleDependencies"
if (data[bdd] && !data[bd]) {
data[bd] = data[bdd]
delete data[bdd]
}
if (data[bd] && !Array.isArray(data[bd])) {
this.warn("nonArrayBundleDependencies")
delete data[bd]
} else if (data[bd]) {
data[bd] = data[bd].filter(function(bd) {
if (!bd || typeof bd !== 'string') {
this.warn("nonStringBundleDependency", bd)
return false
} else {
if (!data.dependencies) {
data.dependencies = {}
}
if (!data.dependencies.hasOwnProperty(bd)) {
this.warn("nonDependencyBundleDependency", bd)
data.dependencies[bd] = "*"
}
return true
}
}, this)
}
}
, fixDependencies: function(data, strict) {
var loose = !strict
objectifyDeps(data, this.warn)
addOptionalDepsToDeps(data, this.warn)
this.fixBundleDependenciesField(data)
;['dependencies','devDependencies'].forEach(function(deps) {
if (!(deps in data)) return
if (!data[deps] || typeof data[deps] !== "object") {
this.warn("nonObjectDependencies", deps)
delete data[deps]
return
}
Object.keys(data[deps]).forEach(function (d) {
var r = data[deps][d]
if (typeof r !== 'string') {
this.warn("nonStringDependency", d, JSON.stringify(r))
delete data[deps][d]
}
var hosted = hostedGitInfo.fromUrl(data[deps][d])
if (hosted) data[deps][d] = hosted.toString()
}, this)
}, this)
}
, fixModulesField: function (data) {
if (data.modules) {
this.warn("deprecatedModules")
delete data.modules
}
}
, fixKeywordsField: function (data) {
if (typeof data.keywords === "string") {
data.keywords = data.keywords.split(/,\s+/)
}
if (data.keywords && !Array.isArray(data.keywords)) {
delete data.keywords
this.warn("nonArrayKeywords")
} else if (data.keywords) {
data.keywords = data.keywords.filter(function(kw) {
if (typeof kw !== "string" || !kw) {
this.warn("nonStringKeyword");
return false
} else {
return true
}
}, this)
}
}
, fixVersionField: function(data, strict) {
// allow "loose" semver 1.0 versions in non-strict mode
// enforce strict semver 2.0 compliance in strict mode
var loose = !strict
if (!data.version) {
data.version = ""
return true
}
if (!semver.valid(data.version, loose)) {
throw new Error('Invalid version: "'+ data.version + '"')
}
data.version = semver.clean(data.version, loose)
return true
}
, fixPeople: function(data) {
modifyPeople(data, unParsePerson)
modifyPeople(data, parsePerson)
}
, fixNameField: function(data, options) {
if (typeof options === "boolean") options = {strict: options}
else if (typeof options === "undefined") options = {}
var strict = options.strict
if (!data.name && !strict) {
data.name = ""
return
}
if (typeof data.name !== "string") {
throw new Error("name field must be a string.")
}
if (!strict)
data.name = data.name.trim()
ensureValidName(data.name, strict, options.allowLegacyCase)
if (isBuiltinModule(data.name))
this.warn("conflictingName", data.name)
}
, fixDescriptionField: function (data) {
if (data.description && typeof data.description !== 'string') {
this.warn("nonStringDescription")
delete data.description
}
if (data.readme && !data.description)
data.description = extractDescription(data.readme)
if(data.description === undefined) delete data.description;
if (!data.description) this.warn("missingDescription")
}
, fixReadmeField: function (data) {
if (!data.readme) {
this.warn("missingReadme")
data.readme = "ERROR: No README data found!"
}
}
, fixBugsField: function(data) {
if (!data.bugs && data.repository && data.repository.url) {
var hosted = hostedGitInfo.fromUrl(data.repository.url)
if(hosted && hosted.bugs()) {
data.bugs = {url: hosted.bugs()}
}
}
else if(data.bugs) {
var emailRe = /^.+@.*\..+$/
if(typeof data.bugs == "string") {
if(emailRe.test(data.bugs))
data.bugs = {email:data.bugs}
else if(url.parse(data.bugs).protocol)
data.bugs = {url: data.bugs}
else
this.warn("nonEmailUrlBugsString")
}
else {
bugsTypos(data.bugs, this.warn)
var oldBugs = data.bugs
data.bugs = {}
if(oldBugs.url) {
if(typeof(oldBugs.url) == "string" && url.parse(oldBugs.url).protocol)
data.bugs.url = oldBugs.url
else
this.warn("nonUrlBugsUrlField")
}
if(oldBugs.email) {
if(typeof(oldBugs.email) == "string" && emailRe.test(oldBugs.email))
data.bugs.email = oldBugs.email
else
this.warn("nonEmailBugsEmailField")
}
}
if(!data.bugs.email && !data.bugs.url) {
delete data.bugs
this.warn("emptyNormalizedBugs")
}
}
}
, fixHomepageField: function(data) {
if (!data.homepage && data.repository && data.repository.url) {
var hosted = hostedGitInfo.fromUrl(data.repository.url)
if (hosted && hosted.docs()) data.homepage = hosted.docs()
}
if (!data.homepage) return
if(typeof data.homepage !== "string") {
this.warn("nonUrlHomepage")
return delete data.homepage
}
if(!url.parse(data.homepage).protocol) {
data.homepage = "http://" + data.homepage
}
}
, fixLicenseField: function(data) {
if (!data.license) {
return this.warn("missingLicense")
} else{
if (
typeof(data.license) !== 'string' ||
data.license.length < 1 ||
data.license.trim() === ''
) {
this.warn("invalidLicense")
} else {
if (!validateLicense(data.license).validForNewPackages)
this.warn("invalidLicense")
}
}
}
}
function isValidScopedPackageName(spec) {
if (spec.charAt(0) !== '@') return false
var rest = spec.slice(1).split('/')
if (rest.length !== 2) return false
return rest[0] && rest[1] &&
rest[0] === encodeURIComponent(rest[0]) &&
rest[1] === encodeURIComponent(rest[1])
}
function isCorrectlyEncodedName(spec) {
return !spec.match(/[\/@\s\+%:]/) &&
spec === encodeURIComponent(spec)
}
function ensureValidName (name, strict, allowLegacyCase) {
if (name.charAt(0) === "." ||
!(isValidScopedPackageName(name) || isCorrectlyEncodedName(name)) ||
(strict && (!allowLegacyCase) && name !== name.toLowerCase()) ||
name.toLowerCase() === "node_modules" ||
name.toLowerCase() === "favicon.ico") {
throw new Error("Invalid name: " + JSON.stringify(name))
}
}
function modifyPeople (data, fn) {
if (data.author) data.author = fn(data.author)
;["maintainers", "contributors"].forEach(function (set) {
if (!Array.isArray(data[set])) return;
data[set] = data[set].map(fn)
})
return data
}
function unParsePerson (person) {
if (typeof person === "string") return person
var name = person.name || ""
var u = person.url || person.web
var url = u ? (" ("+u+")") : ""
var e = person.email || person.mail
var email = e ? (" <"+e+">") : ""
return name+email+url
}
function parsePerson (person) {
if (typeof person !== "string") return person
var name = person.match(/^([^\(<]+)/)
var url = person.match(/\(([^\)]+)\)/)
var email = person.match(/<([^>]+)>/)
var obj = {}
if (name && name[0].trim()) obj.name = name[0].trim()
if (email) obj.email = email[1];
if (url) obj.url = url[1];
return obj
}
function addOptionalDepsToDeps (data, warn) {
var o = data.optionalDependencies
if (!o) return;
var d = data.dependencies || {}
Object.keys(o).forEach(function (k) {
d[k] = o[k]
})
data.dependencies = d
}
function depObjectify (deps, type, warn) {
if (!deps) return {}
if (typeof deps === "string") {
deps = deps.trim().split(/[\n\r\s\t ,]+/)
}
if (!Array.isArray(deps)) return deps
warn("deprecatedArrayDependencies", type)
var o = {}
deps.filter(function (d) {
return typeof d === "string"
}).forEach(function(d) {
d = d.trim().split(/(:?[@\s><=])/)
var dn = d.shift()
var dv = d.join("")
dv = dv.trim()
dv = dv.replace(/^@/, "")
o[dn] = dv
})
return o
}
function objectifyDeps (data, warn) {
depTypes.forEach(function (type) {
if (!data[type]) return;
data[type] = depObjectify(data[type], type, warn)
})
}
function bugsTypos(bugs, warn) {
if (!bugs) return
Object.keys(bugs).forEach(function (k) {
if (typos.bugs[k]) {
warn("typo", k, typos.bugs[k], "bugs")
bugs[typos.bugs[k]] = bugs[k]
delete bugs[k]
}
})
}