This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(publish): first test pass w/ bugfixes
- Loading branch information
Showing
3 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) | ||
} | ||
}) | ||
} |