Skip to content

Commit a904fe2

Browse files
ferencsaraiFerenc Sárai
andauthored
fix: act download (#576)
* feat: add download with act test * fix: download with act --------- Co-authored-by: Ferenc Sárai <[email protected]>
1 parent 6f1d002 commit a904fe2

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

src/command/download.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { MantarayNode } from '@ethersphere/bee-js'
22
import fs from 'fs'
33
import { Aggregation, LeafCommand } from 'furious-commander'
4+
import { Bytes } from '@ethersphere/bee-js'
45
import { BzzAddress, makeBzzAddress } from '../utils/bzz-address'
56
import { Download as ManifestDownload } from './manifest/download'
67
import { RootCommand } from './root-command'
@@ -14,20 +15,10 @@ export class Download extends RootCommand implements LeafCommand {
1415
public manifestDownload!: ManifestDownload
1516

1617
private address!: BzzAddress
17-
private actReqHeaders: Record<string, string> = {}
1818

1919
public async run(): Promise<void> {
2020
super.init()
2121

22-
if (this.manifestDownload.act) {
23-
this.actReqHeaders = {
24-
'Swarm-Act': 'true',
25-
'Swarm-Act-Timestamp': this.manifestDownload.actTimestamp,
26-
'Swarm-Act-History-Address': this.manifestDownload.actHistoryAddress,
27-
'Swarm-Act-Publisher': this.manifestDownload.actPublisher,
28-
}
29-
}
30-
3122
this.address = await makeBzzAddress(this.bee, this.manifestDownload.bzzUrl)
3223

3324
if (await this.isManifest()) {
@@ -39,15 +30,18 @@ export class Download extends RootCommand implements LeafCommand {
3930
}
4031

4132
private async downloadData(): Promise<void> {
42-
const downloadOptions = this.manifestDownload.act
43-
? {
44-
actPublisher: this.manifestDownload.actPublisher,
45-
actHistoryAddress: this.manifestDownload.actHistoryAddress,
46-
actTimestamp: this.manifestDownload.actTimestamp,
47-
}
48-
: undefined
33+
let response: Bytes
4934

50-
const response = await this.bee.downloadData(this.address.hash, downloadOptions)
35+
if (this.manifestDownload.act) {
36+
const responseAct = await this.bee.downloadFile(this.address.hash, this.manifestDownload.destination, {
37+
actPublisher: this.manifestDownload.actPublisher,
38+
actHistoryAddress: this.manifestDownload.actHistoryAddress,
39+
actTimestamp: this.manifestDownload.actTimestamp,
40+
})
41+
response = responseAct.data
42+
} else {
43+
response = await this.bee.downloadData(this.address.hash, undefined)
44+
}
5145

5246
if (this.manifestDownload.stdout) {
5347
process.stdout.write(response.toUtf8())

test/command/download.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { join } from 'node:path'
44
import { Upload } from '../../src/command/upload'
55
import { describeCommand, invokeTestCli } from '../utility'
66
import { getStampOption } from '../utility/stamp'
7+
import { Addresses } from '../../src/command/addresses'
78

89
function makeTmpDir(): string {
910
return mkdtempSync(join(tmpdir(), 'swarm-cli-testrun-'))
@@ -19,6 +20,28 @@ describeCommand('Test Download command', ({ consoleMessages }) => {
1920
expect(consoleMessages[0]).toContain('Hello Swarm!')
2021
})
2122

23+
it('should download with act and print to stdout', async () => {
24+
const addressesInvocation = await invokeTestCli(['addresses'])
25+
const addressesCommand = addressesInvocation.runnable as Addresses
26+
const uploadInvocation = await invokeTestCli(['upload', 'test/message.txt', '--act', ...getStampOption()])
27+
const uploadCommand = uploadInvocation.runnable as Upload
28+
const ref = uploadCommand.result.getOrThrow().toHex()
29+
const history = uploadCommand.historyAddress.getOrThrow().toHex()
30+
const publicKey = addressesCommand.nodeAddresses.publicKey.toHex()
31+
consoleMessages.length = 0
32+
await invokeTestCli([
33+
'download',
34+
ref,
35+
'--act',
36+
'--act-history-address',
37+
history,
38+
'--act-publisher',
39+
publicKey,
40+
'--stdout',
41+
])
42+
expect(consoleMessages[0]).toContain('Hello Swarm!')
43+
})
44+
2245
it('should fall back to manifest download', async () => {
2346
const tmpDir = makeTmpDir()
2447
const invocation = await invokeTestCli(['upload', 'test/testpage', ...getStampOption()])

0 commit comments

Comments
 (0)