Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/command/feed/feed-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { getWalletFromIdentity, pickIdentity } from '../../service/identity'
import { Identity } from '../../service/identity/types'
import { printStamp } from '../../service/stamp'
import { topicProperties, topicStringProperties } from '../../utils/option'
import { printQRCodeWithLabel } from '../../utils/qr'
import { createSpinner } from '../../utils/spinner'
import { createKeyValue } from '../../utils/text'
import { publicUrl } from '../../utils/url'
import { RootCommand } from '../root-command'
import { VerbosityLevel } from '../root-command/command-log'
import { printQRCodeWithLabel } from '../../utils/qr'

interface FeedInfo {
reference: Reference
Expand Down Expand Up @@ -57,7 +58,7 @@ export class FeedCommand extends RootCommand {
this.console.log(createKeyValue('Feed Manifest URL', manifestUrl))

if (this.qr) {
printQRCodeWithLabel(manifestUrl, 'QR for Manifest URL', this.console)
printQRCodeWithLabel(publicUrl(manifestUrl), 'QR for Manifest URL', this.console)
}

this.console.quiet(manifest.toHex())
Expand Down
3 changes: 2 additions & 1 deletion src/command/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { stampProperties } from '../utils/option'
import { printQRCodeWithLabel } from '../utils/qr'
import { createSpinner } from '../utils/spinner'
import { createKeyValue, deprecationWarningText, warningSymbol, warningText } from '../utils/text'
import { publicUrl } from '../utils/url'
import { RootCommand } from './root-command'
import { VerbosityLevel } from './root-command/command-log'

Expand Down Expand Up @@ -232,7 +233,7 @@ export class Upload extends RootCommand implements LeafCommand {
}

if (this.qr) {
printQRCodeWithLabel(url, 'QR for URL', this.console)
printQRCodeWithLabel(publicUrl(url), 'QR for URL', this.console)
}

if (this.shareWith) {
Expand Down
15 changes: 15 additions & 0 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function publicUrl(url: string): string {
let publicUrl: string = url

const isLocal = ['localhost', '127.0.0.1', '::1'].includes(new URL(url).hostname)

if (isLocal) {
publicUrl = Object.assign(new URL(url), {
protocol: 'https:',
host: 'api.gateway.ethswarm.org',
port: '',
}).toString()
}

return publicUrl
}
27 changes: 27 additions & 0 deletions test/command/feed.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { randomUUID } from 'crypto'
import QRCode from 'qrcode'
import { Upload } from '../../src/command/upload'
import { toBeQRCode, toMatchLinesInOrder } from '../custom-matcher'
import { describeCommand, invokeTestCli } from '../utility'
Expand Down Expand Up @@ -112,6 +113,32 @@ describeCommand(
expect(hasMessageContaining('QR for Manifest URL:')).toBeTruthy()
expect(consoleMessages[13]).toBeQRCode()
})

describe('when the URL is local', () => {
it('should change the URL to use gateway', async () => {
const stampBatchId = await getOrBuyStamp()
// create identity
const identityName = randomUUID()
await invokeTestCli(['identity', 'create', identityName, '--password', 'test'])
jest.spyOn(QRCode, 'toString')
await invokeTestCli([
'feed',
'upload',
'--identity',
identityName,
'--password',
'test',
'--stamp',
stampBatchId.toHex(),
'--qr',
`${__dirname}/../testpage/images/swarm.png`,
])
expect(QRCode.toString).toHaveBeenCalledWith(
expect.stringContaining('https://api.gateway.ethswarm.org/bzz/'),
{ type: 'terminal', small: true },
)
})
})
})

it.skip('should increment number of updates for sequence feeds', async () => {
Expand Down
12 changes: 12 additions & 0 deletions test/command/upload.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { System } from 'cafe-utility'
import { existsSync, unlinkSync, writeFileSync } from 'fs'
import { LeafCommand } from 'furious-commander'
import QRCode from 'qrcode'
import type { Upload } from '../../src/command/upload'
import { toBeQRCode, toMatchLinesInOrder } from '../custom-matcher'
import { describeCommand, invokeTestCli } from '../utility'
Expand Down Expand Up @@ -212,6 +213,17 @@ describeCommand(
expect(hasMessageContaining('QR for URL:')).toBeTruthy()
expect(getLastMessage()).toBeQRCode()
})

describe('when the URL is local', () => {
it('should change the URL to use gateway', async () => {
jest.spyOn(QRCode, 'toString')
await invokeTestCli(['upload', 'test/message.txt', '--qr', ...getStampOption()])
expect(QRCode.toString).toHaveBeenCalledWith(
expect.stringContaining('https://api.gateway.ethswarm.org/bzz/'),
{ type: 'terminal', small: true },
)
})
})
})

describe('when using stdin and bee-api-url is a gateway', () => {
Expand Down
Loading