Skip to content

Commit 61635ac

Browse files
authored
fix: ipfs-companion rpc api in go-ipfs <0.13 (#2054)
Closes ipfs/ipfs-companion#1068
1 parent 0c1f1ce commit 61635ac

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

Diff for: src/daemon/config.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,26 @@ function applyDefaults (ipfsd) {
5757
writeConfigFile(ipfsd, config)
5858
}
5959

60-
function getGatewayPort (config) {
61-
let gatewayUrl = null
60+
const getRpcApiPort = (config) => getHttpPort(config.Addresses.API)
61+
const getGatewayPort = (config) => getHttpPort(config.Addresses.Gateway)
62+
function getHttpPort (addrs) {
63+
let httpUrl = null
6264

63-
if (Array.isArray(config.Addresses.Gateway)) {
64-
gatewayUrl = config.Addresses.Gateway.find(v => v.includes('127.0.0.1'))
65+
if (Array.isArray(addrs)) {
66+
httpUrl = addrs.find(v => v.includes('127.0.0.1'))
6567
} else {
66-
gatewayUrl = config.Addresses.Gateway
68+
httpUrl = addrs
6769
}
6870

69-
const gw = parseCfgMultiaddr(gatewayUrl)
71+
const gw = parseCfgMultiaddr(httpUrl)
7072
return gw.nodeAddress().port
7173
}
7274

7375
// Apply one-time updates to the config of IPFS node.
7476
// This is the place where we execute fixes and performance tweaks for existing users.
7577
function migrateConfig (ipfsd) {
7678
// Bump revision number when new migration rule is added
77-
const REVISION = 2
79+
const REVISION = 3
7880
const REVISION_KEY = 'daemonConfigRevision'
7981
const CURRENT_REVISION = store.get(REVISION_KEY, 0)
8082

@@ -92,7 +94,7 @@ function migrateConfig (ipfsd) {
9294
return
9395
}
9496

95-
if (CURRENT_REVISION <= 0) {
97+
if (CURRENT_REVISION < 1) {
9698
// Cleanup https://github.com/ipfs-shipyard/ipfs-desktop/issues/1631
9799
if (config.Discovery && config.Discovery.MDNS && config.Discovery.MDNS.enabled) {
98100
config.Discovery.MDNS.Enabled = config.Discovery.MDNS.Enabled || true
@@ -101,7 +103,7 @@ function migrateConfig (ipfsd) {
101103
}
102104
}
103105

104-
if (CURRENT_REVISION <= 1) {
106+
if (CURRENT_REVISION < 3) {
105107
const api = config.API || {}
106108
const httpHeaders = api.HTTPHeaders || {}
107109
const accessControlAllowOrigin = httpHeaders['Access-Control-Allow-Origin'] || []
@@ -117,7 +119,11 @@ function migrateConfig (ipfsd) {
117119
const addedWebUI = addURL('https://webui.ipfs.io')
118120
const addedGw = addURL(`http://webui.ipfs.io.ipns.localhost:${getGatewayPort(config)}`)
119121

120-
if (addedWebUI || addedGw) {
122+
// https://github.com/ipfs/ipfs-companion/issues/1068 in go-ipfs <0.13
123+
// TODO: remove addedApiPort after go-ipfs 0.13 ships
124+
const addedApiPort = addURL(`http://127.0.0.1:${getRpcApiPort(config)}`)
125+
126+
if (addedWebUI || addedGw || addedApiPort) {
121127
httpHeaders['Access-Control-Allow-Origin'] = accessControlAllowOrigin
122128
api.HTTPHeaders = httpHeaders
123129
config.API = api

Diff for: test/e2e/launch.e2e.test.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ test.describe.serial('Application launch', async () => {
123123
expect(config.API.HTTPHeaders['Access-Control-Allow-Origin']).toEqual([
124124
'https://127.0.0.1:4040',
125125
'https://webui.ipfs.io',
126-
'http://webui.ipfs.io.ipns.localhost:0' // ipfsd 'test' profile uses '/ip4/127.0.0.1/tcp/0'
126+
'http://webui.ipfs.io.ipns.localhost:0', // ipfsd 'test' profile uses '/ip4/127.0.0.1/tcp/0'
127+
'http://127.0.0.1:0' // // ipfsd 'test' profile uses '/ip4/127.0.0.1/tcp/0'
127128
])
128129
})
129130

@@ -143,7 +144,8 @@ test.describe.serial('Application launch', async () => {
143144
// ensure app has migrated config
144145
expect(config.API.HTTPHeaders['Access-Control-Allow-Origin']).toEqual([
145146
'https://webui.ipfs.io',
146-
'http://webui.ipfs.io.ipns.localhost:0' // ipfsd 'test' profile uses '/ip4/127.0.0.1/tcp/0'
147+
'http://webui.ipfs.io.ipns.localhost:0', // ipfsd 'test' profile uses '/ip4/127.0.0.1/tcp/0'
148+
'http://127.0.0.1:0' // // ipfsd 'test' profile uses '/ip4/127.0.0.1/tcp/0'
147149
])
148150
})
149151

@@ -163,7 +165,8 @@ test.describe.serial('Application launch', async () => {
163165
// ensure app has migrated config
164166
expect(config.API.HTTPHeaders['Access-Control-Allow-Origin']).toEqual([
165167
'https://webui.ipfs.io',
166-
'http://webui.ipfs.io.ipns.localhost:0' // ipfsd 'test' profile uses '/ip4/127.0.0.1/tcp/0'
168+
'http://webui.ipfs.io.ipns.localhost:0', // ipfsd 'test' profile uses '/ip4/127.0.0.1/tcp/0'
169+
'http://127.0.0.1:0' // // ipfsd 'test' profile uses '/ip4/127.0.0.1/tcp/0'
167170
])
168171
})
169172

0 commit comments

Comments
 (0)