Skip to content

Commit

Permalink
fix: broken dnslink at /ipns/ipfs.io/blog/foo (#844)
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel authored Feb 12, 2020
1 parent 4fcdfb2 commit 5da02c9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
10 changes: 10 additions & 0 deletions add-on/src/lib/ipfs-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,16 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
const state = getState()
if (!state.active) return
if (request.statusCode === 200) return // finish if no error to recover from

// Seamlessly fix canonical link when DNSLink breaks ipfs.io/blog/*
/// https://github.com/ipfs/blog/issues/360
if (request.type === 'main_frame' &&
request.statusCode === 404 &&
request.url.match(/\/\/[^/]+\/ipns\/ipfs\.io\/blog\//)) {
log('onCompleted: fixing /ipns/ipfs.io/blog → /ipns/blog.ipfs.io')
return browser.tabs.update({ url: request.url.replace('/ipns/ipfs.io/blog', '/ipns/blog.ipfs.io') })
}

let redirectUrl
if (isRecoverable(request, state, ipfsPathValidator)) {
// if subdomain request redirect to default public subdomain url
Expand Down
30 changes: 27 additions & 3 deletions test/functional/lib/ipfs-request-workarounds.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'
const { describe, it, before, beforeEach, after } = require('mocha')
const { expect } = require('chai')
const { expect, assert } = require('chai')
const { URL } = require('url') // URL implementation with support for .origin attribute
const browser = require('sinon-chrome')
const { initState } = require('../../../add-on/src/lib/state')
const { createRuntimeChecks } = require('../../../add-on/src/lib/runtime-checks')
const { createRequestModifier } = require('../../../add-on/src/lib/ipfs-request')
const createDnslinkResolver = require('../../../add-on/src/lib/dnslink')
const createDNSLinkResolver = require('../../../add-on/src/lib/dnslink')
const { createIpfsPathValidator } = require('../../../add-on/src/lib/ipfs-path')
const { optionDefaults } = require('../../../add-on/src/lib/options')

Expand All @@ -33,7 +33,7 @@ describe('modifyRequest processing', function () {
state = initState(optionDefaults)
getState = () => state
const getIpfs = () => {}
dnslinkResolver = createDnslinkResolver(getState)
dnslinkResolver = createDNSLinkResolver(getState)
runtime = Object.assign({}, await createRuntimeChecks(browser)) // make it mutable for tests
ipfsPathValidator = createIpfsPathValidator(getState, getIpfs, dnslinkResolver)
modifyRequest = createRequestModifier(getState, dnslinkResolver, ipfsPathValidator, runtime)
Expand Down Expand Up @@ -159,6 +159,30 @@ describe('modifyRequest processing', function () {
})
})

// We've moved blog to blog.ipfs.io but links to ipfs.io/blog/*
// are still around due to the way Discourse integration was done for comments.
// https://github.com/ipfs/blog/issues/360
describe('a failed main_frame request to /ipns/ipfs.io/blog', function () {
it('should be updated to /ipns/blog.ipfs.io', async function () {
const brokenDNSLinkUrl = 'http://example.com/ipns/ipfs.io/blog/some-post'
const fixedDNSLinkUrl = 'http://example.com/ipns/blog.ipfs.io/some-post'
// ensure clean modifyRequest
runtime = Object.assign({}, await createRuntimeChecks(browser)) // make it mutable for tests
modifyRequest = createRequestModifier(getState, dnslinkResolver, ipfsPathValidator, runtime)
// test
const request = {
statusCode: 404,
type: 'main_frame',
url: brokenDNSLinkUrl
}
browser.tabs.update.flush()
assert.ok(browser.tabs.update.notCalled)
modifyRequest.onCompleted(request)
assert.ok(browser.tabs.update.withArgs({ url: fixedDNSLinkUrl }).calledOnce)
browser.tabs.update.flush()
})
})

after(function () {
delete global.URL
delete global.browser
Expand Down

0 comments on commit 5da02c9

Please sign in to comment.