Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
fix(publish): full coverage test and related fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 31, 2018
1 parent 6d58f33 commit b5a3446
Show file tree
Hide file tree
Showing 2 changed files with 748 additions and 13 deletions.
36 changes: 25 additions & 11 deletions publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ function publish (manifest, tarball, opts) {
return new opts.Promise(resolve => resolve()).then(() => {
validate('OSO|OOO', [manifest, tarball, opts])
if (manifest.private) {
throw new Error(
throw Object.assign(new Error(
'This package has been marked as private\n' +
"Remove the 'private' field from the package.json to publish it."
)
), { code: 'EPRIVATE' })
}
const spec = npa.resolve(manifest.name, manifest.version)
const reg = npmFetch.pickRegistry(spec, opts)
Expand All @@ -42,7 +42,10 @@ function publish (manifest, tarball, opts) {
// registry-frontdoor cares about the access level, which is only
// configurable for scoped packages
if (!spec.scope && opts.access === 'restricted') {
throw new Error("Can't restrict access to unscoped packages.")
throw Object.assign(
new Error("Can't restrict access to unscoped packages."),
{ code: 'EUNSCOPED' }
)
}

return slurpTarball(tarball, opts).then(tardata => {
Expand Down Expand Up @@ -91,7 +94,12 @@ function patchedManifest (spec, auth, base, opts) {

fixer.fixNameField(manifest, { strict: true, allowLegacyCase: true })
const version = semver.clean(manifest.version)
if (!version) { throw new Error('invalid semver: ' + manifest.version) }
if (!version) {
throw Object.assign(
new Error('invalid semver: ' + manifest.version),
{ code: 'EBADSEMVER' }
)
}
manifest.version = version
return manifest
}
Expand Down Expand Up @@ -147,23 +155,24 @@ function patchMetadata (current, newData, opts) {
return semver.clean(v, true)
}).concat(Object.keys(current.time || {}).map(v => {
if (semver.valid(v, true)) { return semver.clean(v, true) }
}).filter(v => v))
})).filter(v => v)

if (curVers.indexOf(newData.version) !== -1) {
const newVersion = Object.keys(newData.versions)[0]

if (curVers.indexOf(newVersion) !== -1) {
throw ConflictError(newData.name, newData.version)
}

const newVersion = newData.version

current.versions = current.versions || {}
current.versions[newVersion] = newData.versions[newVersion]
current._attachments = current._attachments || {}
for (var i in newData) {
switch (i) {
// objects that copy over the new stuffs
case 'dist-tags':
case 'versions':
case '_attachments':
for (var j in newData[i]) {
current[i] = current[i] || {}
current[i][j] = newData[i][j]
}
break
Expand All @@ -177,7 +186,7 @@ function patchMetadata (current, newData, opts) {
current[i] = newData[i]
}
}
const maint = JSON.parse(JSON.stringify(newData.maintainers))
const maint = newData.maintainers && JSON.parse(JSON.stringify(newData.maintainers))
newData.versions[newVersion].maintainers = maint
return current
}
Expand All @@ -187,8 +196,13 @@ function slurpTarball (tarSrc, opts) {
return opts.Promise.resolve(tarSrc)
} else if (typeof tarSrc === 'string') {
return opts.Promise.resolve(Buffer.from(tarSrc, 'base64'))
} else if (typeof tarSrc === 'object' && typeof tarSrc.pipe === 'function') {
} else if (typeof tarSrc.pipe === 'function') {
return getStream.buffer(tarSrc)
} else {
return opts.Promise.reject(Object.assign(
new Error('invalid tarball argument. Must be a Buffer, a base64 string, or a binary stream'), {
code: 'EBADTAR'
}))
}
}

Expand Down
Loading

0 comments on commit b5a3446

Please sign in to comment.