Skip to content

Commit

Permalink
Rename PUBLISH to POST, fix loading raw content hashes, replace resol…
Browse files Browse the repository at this point in the history
…ve header with querystring
  • Loading branch information
RangerMauve committed Jul 19, 2021
1 parent 92c81ac commit fd9e37e
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 88 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,5 @@ dist

# Package stuff, bleh
package-lock.json

.test-repo/
54 changes: 29 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const parseRange = require('range-parser')
const mime = require('mime/lite')
const CID = require('cids')

const SUPPORTED_METHODS = ['GET', 'HEAD', 'PUBLISH', 'POST']
const SUPPORTED_METHODS = ['GET', 'HEAD', 'POST']

function makePotentialPaths (path) {
return [
Expand All @@ -16,7 +16,7 @@ function makePotentialPaths (path) {

module.exports = function makeIPFSFetch ({ ipfs }) {
return makeFetch(async ({ url, headers: reqHeaders, method, signal, body }) => {
const { hostname, pathname, protocol } = new URL(url)
const { hostname, pathname, protocol, searchParams } = new URL(url)
let ipfsPath = hostname ? hostname + pathname : pathname.slice(1)

const headers = {}
Expand Down Expand Up @@ -91,7 +91,7 @@ module.exports = function makeIPFSFetch ({ ipfs }) {
}

try {
if (method === 'POST') {
if (method === 'POST' && protocol === 'ipfs:') {
// Node.js and browsers handle pathnames differently for IPFS URLs
const path = ensureSlash(stripLeadingSlash(ipfsPath))
const { cid } = await ipfs.add({
Expand Down Expand Up @@ -135,22 +135,19 @@ module.exports = function makeIPFSFetch ({ ipfs }) {

let data = null

const stats = await collect(ipfs.ls(ipfsPath, { signal }))
const files = stats.map(({ name, type }) => (type === 'dir') ? `${name}/` : name)
try {
const stats = await collect(ipfs.ls(ipfsPath, { signal }))
const files = stats.map(({ name, type }) => (type === 'dir') ? `${name}/` : name)

if (files.includes('index.html')) {
const resolveStrategy = reqHeaders['X-Resolve'] || reqHeaders['x-resolve']
if (resolveStrategy !== 'none') {
ipfsPath += 'index.html'
return serveFile()
if (files.includes('index.html')) {
if (!searchParams.has('noResolve')) {
return serveFile()
}
}
}
if ((reqHeaders.Accept || reqHeaders.accept) === 'application/json') {
const json = JSON.stringify(files, null, '\t')
headers['Content-Type'] = 'application/json; charset=utf-8'
data = json
} else {
const page = `

const accept = reqHeaders.Accept || reqHeaders.accept
if (accept && accept.includes('text/html')) {
const page = `
<!DOCTYPE html>
<title>${url}</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
Expand All @@ -161,22 +158,29 @@ module.exports = function makeIPFSFetch ({ ipfs }) {
`).join('')}
</ul>
`
headers['Content-Type'] = 'text/html; charset=utf-8'
data = page
}
headers['Content-Type'] = 'text/html; charset=utf-8'
data = page
} else {
const json = JSON.stringify(files, null, '\t')
headers['Content-Type'] = 'application/json; charset=utf-8'
data = json
}

return {
statusCode: 200,
headers,
data: intoAsyncIterable(data)
return {
statusCode: 200,
headers,
data: intoAsyncIterable(data)
}
} catch {
return serveFile()
}
} else {
if (protocol === 'ipns:') {
await resolveIPNS()
}
return serveFile()
}
} else if (method === 'PUBLISH' && protocol === 'ipns:') {
} else if (method === 'POST' && protocol === 'ipns:') {
const keyName = stripSlash(ipfsPath)
const rawValue = await collectString(body)
const value = rawValue.replace(/^ipfs:\/\//, '/ipfs/').replace(/^ipns:\/\//, '/ipns/')
Expand Down
Loading

0 comments on commit fd9e37e

Please sign in to comment.