|
1 | 1 | 'use strict'
|
2 | 2 |
|
| 3 | +const events = require('events') |
3 | 4 | const fs = require('@npmcli/fs')
|
| 5 | +const Minipass = require('minipass') |
4 | 6 | const path = require('path')
|
5 | 7 | const rimraf = require('rimraf')
|
6 | 8 | const ssri = require('ssri')
|
@@ -32,6 +34,62 @@ t.test('basic put', (t) => {
|
32 | 34 | })
|
33 | 35 | })
|
34 | 36 |
|
| 37 | +t.test('basic put, providing external integrity emitter', async (t) => { |
| 38 | + const CACHE = t.testdir() |
| 39 | + const CONTENT = 'foobarbaz' |
| 40 | + const INTEGRITY = ssri.fromData(CONTENT) |
| 41 | + |
| 42 | + const write = t.mock('../../lib/content/write.js', { |
| 43 | + ssri: { |
| 44 | + ...ssri, |
| 45 | + integrityStream: () => { |
| 46 | + throw new Error('Should not be called') |
| 47 | + }, |
| 48 | + }, |
| 49 | + }) |
| 50 | + |
| 51 | + const source = new Minipass().end(CONTENT) |
| 52 | + |
| 53 | + const tee = new Minipass() |
| 54 | + |
| 55 | + const integrityStream = ssri.integrityStream() |
| 56 | + // since the integrityStream is not going anywhere, we need to manually resume it |
| 57 | + // otherwise it'll get stuck in paused mode and will never process any data events |
| 58 | + integrityStream.resume() |
| 59 | + const integrityStreamP = Promise.all([ |
| 60 | + events.once(integrityStream, 'integrity').then((res) => res[0]), |
| 61 | + events.once(integrityStream, 'size').then((res) => res[0]), |
| 62 | + ]) |
| 63 | + |
| 64 | + const contentStream = write.stream(CACHE, { integrityEmitter: integrityStream }) |
| 65 | + const contentStreamP = Promise.all([ |
| 66 | + events.once(contentStream, 'integrity').then((res) => res[0]), |
| 67 | + events.once(contentStream, 'size').then((res) => res[0]), |
| 68 | + contentStream.promise(), |
| 69 | + ]) |
| 70 | + |
| 71 | + tee.pipe(integrityStream) |
| 72 | + tee.pipe(contentStream) |
| 73 | + source.pipe(tee) |
| 74 | + |
| 75 | + const [ |
| 76 | + [ssriIntegrity, ssriSize], |
| 77 | + [contentIntegrity, contentSize], |
| 78 | + ] = await Promise.all([ |
| 79 | + integrityStreamP, |
| 80 | + contentStreamP, |
| 81 | + ]) |
| 82 | + |
| 83 | + t.equal(ssriSize, CONTENT.length, 'ssri got the right size') |
| 84 | + t.equal(contentSize, CONTENT.length, 'content got the right size') |
| 85 | + t.same(ssriIntegrity, INTEGRITY, 'ssri got the right integrity') |
| 86 | + t.same(contentIntegrity, INTEGRITY, 'content got the right integrity') |
| 87 | + |
| 88 | + const cpath = contentPath(CACHE, ssriIntegrity) |
| 89 | + t.ok(fs.lstatSync(cpath).isFile(), 'content inserted as a single file') |
| 90 | + t.equal(fs.readFileSync(cpath, 'utf8'), CONTENT, 'contents are identical to inserted content') |
| 91 | +}) |
| 92 | + |
35 | 93 | t.test("checks input digest doesn't match data", (t) => {
|
36 | 94 | const CONTENT = 'foobarbaz'
|
37 | 95 | const integrity = ssri.fromData(CONTENT)
|
|
0 commit comments