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

Commit

Permalink
fix(api): use opts.algorithms, return true on success
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 31, 2018
1 parent b5a3446 commit 80fe34b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 68 deletions.
18 changes: 11 additions & 7 deletions publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const validate = require('aproba')

const PublishConfig = figgyPudding({
access: {},
integrityHashes: { default: ['sha512'] },
algorithms: { default: ['sha512'] },
dryRun: 'dry-run',
'dry-run': {},
force: {},
Expand All @@ -35,6 +35,8 @@ function publish (manifest, tarball, opts) {
), { code: 'EPRIVATE' })
}
const spec = npa.resolve(manifest.name, manifest.version)
// NOTE: spec is used to pick the appropriate registry/auth combo.
opts = opts.concat({ spec })
const reg = npmFetch.pickRegistry(spec, opts)
const auth = npmAuth(reg, opts)
const pubManifest = patchedManifest(spec, auth, manifest, opts)
Expand All @@ -52,24 +54,26 @@ function publish (manifest, tarball, opts) {
const metadata = buildMetadata(
spec, auth, reg, pubManifest, tardata, opts
)
return npmFetch.json(spec.escapedName, opts.concat({
return npmFetch(spec.escapedName, opts.concat({
method: 'PUT',
body: metadata
body: metadata,
ignoreBody: true
})).catch(err => {
if (err.code !== 'E409') { throw err }
return npmFetch.json(spec.escapedName, opts.concat({
query: { write: true }
})).then(
current => patchMetadata(current, metadata, opts)
).then(newMetadata => {
return npmFetch.json(spec.escapedName, opts.concat({
return npmFetch(spec.escapedName, opts.concat({
method: 'PUT',
body: newMetadata
body: newMetadata,
ignoreBody: true
}))
})
})
})
})
}).then(() => true)
}

function patchedManifest (spec, auth, base, opts) {
Expand Down Expand Up @@ -128,7 +132,7 @@ function buildMetadata (spec, auth, registry, manifest, tardata, opts) {
const tbName = manifest.name + '-' + manifest.version + '.tgz'
const tbURI = manifest.name + '/-/' + tbName
const integrity = ssri.fromData(tardata, {
algorithms: [...new Set(['sha1'].concat(opts.integrityHashes))]
algorithms: [...new Set(['sha1'].concat(opts.algorithms))]
})

manifest._id = manifest.name + '@' + manifest.version
Expand Down
4 changes: 2 additions & 2 deletions test/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ test('basic publish', t => {

return publish(manifest, tarData, OPTS.concat({
token: 'deadbeef'
})).then(() => {
t.ok(true, 'publish succeeded')
})).then(ret => {
t.ok(ret, 'publish succeeded')
})
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/unpublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ test('basic test', t => {
const srv = tnock(t, REG)
srv.get('/foo?write=true').reply(200, doc)
srv.delete(`/foo/-rev/${REV}`).reply(201)
return unpub('foo', OPTS).then(() => {
t.ok(true, 'foo was unpublished')
return unpub('foo', OPTS).then(ret => {
t.ok(ret, 'foo was unpublished')
})
})

Expand Down
118 changes: 61 additions & 57 deletions unpublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,69 +14,73 @@ const UnpublishConfig = figgyPudding({
module.exports = unpublish
function unpublish (spec, opts) {
opts = UnpublishConfig(opts)
spec = npa(spec)
const pkgUri = spec.escapedName
return npmFetch.json(pkgUri, opts.concat({
query: { write: true }
})).then(pkg => {
if (!spec.rawSpec || spec.rawSpec === '*') {
return npmFetch(`${pkgUri}/-rev/${pkg._rev}`, opts.concat({
method: 'DELETE',
ignoreBody: true
}))
} else {
const version = spec.rawSpec
const allVersions = pkg.versions || {}
const versionPublic = allVersions[version]
let dist
if (versionPublic) {
dist = allVersions[version].dist
}
delete allVersions[version]
// if it was the only version, then delete the whole package.
if (!Object.keys(allVersions).length) {
return new opts.Promise(resolve => resolve()).then(() => {
spec = npa(spec)
// NOTE: spec is used to pick the appropriate registry/auth combo.
opts = opts.concat({ spec })
const pkgUri = spec.escapedName
return npmFetch.json(pkgUri, opts.concat({
query: { write: true }
})).then(pkg => {
if (!spec.rawSpec || spec.rawSpec === '*') {
return npmFetch(`${pkgUri}/-rev/${pkg._rev}`, opts.concat({
method: 'DELETE',
ignoreBody: true
}))
} else if (versionPublic) {
const latestVer = pkg['dist-tags'].latest
Object.keys(pkg['dist-tags']).forEach(tag => {
if (pkg['dist-tags'][tag] === version) {
delete pkg['dist-tags'][tag]
}
})

if (latestVer === version) {
pkg['dist-tags'].latest = Object.keys(
allVersions
).sort(semver.compareLoose).pop()
} else {
const version = spec.rawSpec
const allVersions = pkg.versions || {}
const versionPublic = allVersions[version]
let dist
if (versionPublic) {
dist = allVersions[version].dist
}
delete allVersions[version]
// if it was the only version, then delete the whole package.
if (!Object.keys(allVersions).length) {
return npmFetch(`${pkgUri}/-rev/${pkg._rev}`, opts.concat({
method: 'DELETE',
ignoreBody: true
}))
} else if (versionPublic) {
const latestVer = pkg['dist-tags'].latest
Object.keys(pkg['dist-tags']).forEach(tag => {
if (pkg['dist-tags'][tag] === version) {
delete pkg['dist-tags'][tag]
}
})

delete pkg._revisions
delete pkg._attachments
// Update packument with removed versions
return npmFetch(`${pkgUri}/-rev/${pkg._rev}`, opts.concat({
method: 'PUT',
body: pkg,
ignoreBody: true
})).then(() => {
// Remove the tarball itself
return npmFetch.json(pkgUri, opts.concat({
query: { write: true }
})).then(({ _rev, _id }) => {
const tarballUrl = url.parse(dist.tarball).pathname.substr(1)
return npmFetch(`${tarballUrl}/-rev/${_rev}`, opts.concat({
method: 'DELETE',
ignoreBody: true
}))
if (latestVer === version) {
pkg['dist-tags'].latest = Object.keys(
allVersions
).sort(semver.compareLoose).pop()
}

delete pkg._revisions
delete pkg._attachments
// Update packument with removed versions
return npmFetch(`${pkgUri}/-rev/${pkg._rev}`, opts.concat({
method: 'PUT',
body: pkg,
ignoreBody: true
})).then(() => {
// Remove the tarball itself
return npmFetch.json(pkgUri, opts.concat({
query: { write: true }
})).then(({ _rev, _id }) => {
const tarballUrl = url.parse(dist.tarball).pathname.substr(1)
return npmFetch(`${tarballUrl}/-rev/${_rev}`, opts.concat({
method: 'DELETE',
ignoreBody: true
}))
})
})
})
}
}
}, err => {
if (err.code !== 'E404') {
throw err
}
}
}, err => {
if (err.code !== 'E404') {
throw err
}
})
})
}).then(() => true)
}

0 comments on commit 80fe34b

Please sign in to comment.