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

Commit

Permalink
fix(publish): first test pass w/ bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 30, 2018
1 parent 74fc06f commit 74135c9
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 3 deletions.
7 changes: 4 additions & 3 deletions publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ const url = require('url')
const validate = require('aproba')

const PublishConfig = figgyPudding({
access: { default: 'restricted' },
access: {},
integrityHashes: { default: ['sha512'] },
dryRun: 'dry-run',
'dry-run': {},
force: {},
npmVersion: {},
tag: { default: 'latest' },
Promise: { default: () => Promise }
})

Expand All @@ -44,7 +45,7 @@ function publish (manifest, tarball, opts) {
throw new Error("Can't restrict access to unscoped packages.")
}

return slurpTarball(tarball).then(tardata => {
return slurpTarball(tarball, opts).then(tardata => {
const metadata = buildMetadata(
spec, auth, reg, pubManifest, tardata, opts
)
Expand Down Expand Up @@ -113,7 +114,7 @@ function buildMetadata (spec, auth, registry, manifest, tardata, opts) {
}

root.versions[ manifest.version ] = manifest
const tag = manifest.tag || this.config.defaultTag
const tag = manifest.tag || opts.tag
root['dist-tags'][tag] = manifest.version

const tbName = manifest.name + '-' + manifest.version + '.tgz'
Expand Down
76 changes: 76 additions & 0 deletions test/publish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict'

const crypto = require('crypto')
const figgyPudding = require('figgy-pudding')
const mockTar = require('./util/mock-tarball.js')
const ssri = require('ssri')
const { test } = require('tap')
const tnock = require('./util/tnock.js')

const publish = require('../publish.js')

const OPTS = figgyPudding({ registry: {} })({
registry: 'https://mock.reg/'
})

const REG = OPTS.registry

test('basic publish', t => {
const manifest = {
name: 'libnpmpublish',
version: '1.0.0',
description: 'some stuff'
}
return mockTar({
'package.json': JSON.stringify(manifest),
'index.js': 'console.log("hello world")'
}).then(tarData => {
const shasum = crypto.createHash('sha1').update(tarData).digest('hex')
const integrity = ssri.fromData(tarData, { algorithms: ['sha512'] })
const packument = {
name: 'libnpmpublish',
description: 'some stuff',
readme: '',
_id: 'libnpmpublish',
'dist-tags': {
latest: '1.0.0'
},
versions: {
'1.0.0': {
_id: 'libnpmpublish@1.0.0',
_nodeVersion: process.versions.node,
_npmVersion: '6.9.0',
name: 'libnpmpublish',
version: '1.0.0',
description: 'some stuff',
dist: {
shasum,
integrity: integrity.toString(),
tarball: `http://mock.reg/libnpmpublish/-/libnpmpublish-1.0.0.tgz`
}
}
},
_attachments: {
'libnpmpublish-1.0.0.tgz': {
'content_type': 'application/octet-stream',
data: tarData.toString('base64'),
length: tarData.length
}
}
}
const srv = tnock(t, REG)
srv.put('/libnpmpublish', body => {
t.deepEqual(body, packument, 'posted packument matches expectations')
return true
}, {
authorization: 'Bearer deadbeef'
}).reply(201, {})

return publish(manifest, tarData, OPTS.concat({
npmVersion: '6.9.0',
token: 'deadbeef'
})).then(() => {
t.ok(true, 'publish succeeded')
})
})
})
47 changes: 47 additions & 0 deletions test/util/mock-tarball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'

const BB = require('bluebird')

const getStream = require('get-stream')
const tar = require('tar-stream')
const zlib = require('zlib')

module.exports = makeTarball
function makeTarball (files, opts) {
opts = opts || {}
const pack = tar.pack()
Object.keys(files).forEach(function (filename) {
const entry = files[filename]
pack.entry({
name: (opts.noPrefix ? '' : 'package/') + filename,
type: entry.type,
size: entry.size,
mode: entry.mode,
mtime: entry.mtime || new Date(0),
linkname: entry.linkname,
uid: entry.uid,
gid: entry.gid,
uname: entry.uname,
gname: entry.gname
}, typeof files[filename] === 'string'
? files[filename]
: files[filename].data)
})
pack.finalize()
return BB.try(() => {
if (opts.stream && opts.gzip) {
const gz = zlib.createGzip()
pack.on('error', err => gz.emit('error', err)).pipe(gz)
} else if (opts.stream) {
return pack
} else {
return getStream.buffer(pack).then(ret => {
if (opts.gzip) {
return BB.fromNode(cb => zlib.gzip(ret, cb))
} else {
return ret
}
})
}
})
}

0 comments on commit 74135c9

Please sign in to comment.