This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use new IPFS async/await APIs (#30)
BREAKING CHANGE: Switch to using async/await and async iterators.
- Loading branch information
1 parent
8828822
commit 68f1204
Showing
9 changed files
with
324 additions
and
388 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ stages: | |
|
||
node_js: | ||
- '10' | ||
- '12' | ||
|
||
os: | ||
- linux | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,89 @@ | ||
/* global Response */ | ||
/* global Response, Blob */ | ||
|
||
'use strict' | ||
|
||
const stream = require('stream') | ||
const toBlob = require('stream-to-blob') | ||
|
||
const debug = require('debug') | ||
const log = debug('ipfs:http:response') | ||
const toStream = require('it-to-stream') | ||
const concat = require('it-concat') | ||
const toBuffer = require('it-buffer') | ||
const log = require('debug')('ipfs:http:response') | ||
|
||
const resolver = require('./resolver') | ||
const pathUtils = require('./utils/path') | ||
const detectContentType = require('./utils/content-type') | ||
|
||
// TODO: pass path and add Etag and X-Ipfs-Path + tests | ||
const header = (status = 200, statusText = 'OK', headers = {}) => ({ | ||
const getHeader = (status = 200, statusText = 'OK', headers = {}) => ({ | ||
status, | ||
statusText, | ||
headers | ||
}) | ||
|
||
const response = async (ipfsNode, ipfsPath) => { | ||
// handle hash resolve error (simple hash, test for directory now) | ||
const handleResolveError = async (node, path, error) => { | ||
if (error) { | ||
const errorString = error.toString() | ||
|
||
if (errorString.includes('dag node is a directory')) { | ||
try { | ||
const content = await resolver.directory(node, path, error.cid) | ||
// dir render | ||
if (typeof content === 'string') { | ||
return new Response(content, header(200, 'OK', { 'Content-Type': 'text/html' })) | ||
} | ||
|
||
// redirect to dir entry point (index) | ||
return Response.redirect(pathUtils.joinURLParts(path, content[0].Name)) | ||
} catch (error) { | ||
log(error) | ||
return new Response(errorString, header(500, error.toString())) | ||
} | ||
} | ||
|
||
if (errorString.startsWith('Error: no link named')) { | ||
return new Response(errorString, header(404, errorString)) | ||
} | ||
// handle hash resolve error (simple hash, test for directory now) | ||
const handleResolveError = async (node, path, error) => { | ||
const errorString = error.toString() | ||
|
||
if (errorString.startsWith('Error: multihash length inconsistent') || errorString.startsWith('Error: Non-base58 character')) { | ||
return new Response(errorString, header(400, errorString)) | ||
if (errorString.includes('dag node is a directory')) { | ||
try { | ||
const content = await resolver.directory(node, path, error.cid) | ||
// dir render | ||
if (typeof content === 'string') { | ||
return new Response(content, getHeader(200, 'OK', { 'Content-Type': 'text/html' })) | ||
} | ||
|
||
// redirect to dir entry point (index) | ||
return Response.redirect(pathUtils.joinURLParts(path, content[0].Name)) | ||
} catch (error) { | ||
log(error) | ||
return new Response(errorString, header(500, errorString)) | ||
return new Response(errorString, getHeader(500, error.toString())) | ||
} | ||
} | ||
|
||
if (errorString.startsWith('Error: no link named')) { | ||
return new Response(errorString, getHeader(404, errorString)) | ||
} | ||
|
||
if (errorString.startsWith('Error: multihash length inconsistent') || errorString.startsWith('Error: Non-base58 character')) { | ||
return new Response(errorString, getHeader(400, errorString)) | ||
} | ||
|
||
return new Response(errorString, getHeader(500, errorString)) | ||
} | ||
|
||
const getResponse = async (ipfsNode, ipfsPath) => { | ||
// remove trailing slash for files if needed | ||
if (ipfsPath.endsWith('/')) { | ||
return Response.redirect(pathUtils.removeTrailingSlash(ipfsPath)) | ||
} | ||
|
||
try { | ||
const resolvedData = await resolver.cid(ipfsNode, ipfsPath) | ||
const { source, contentType } = await detectContentType(ipfsPath, ipfsNode.cat(resolvedData.cid)) | ||
|
||
const readableStream = ipfsNode.catReadableStream(resolvedData.cid) | ||
const responseStream = new stream.PassThrough({ highWaterMark: 1 }) | ||
readableStream.pipe(responseStream) | ||
|
||
return new Promise((resolve, reject) => { | ||
readableStream.once('error', (error) => { | ||
if (error) { | ||
log(error) | ||
return resolve(new Response(error.toString(), header(500, 'Error fetching the file'))) | ||
} | ||
}) | ||
|
||
// return only after first chunk being checked | ||
let contentTypeDetected = false | ||
readableStream.on('data', async (chunk) => { | ||
// check mime on first chunk | ||
if (contentTypeDetected) { | ||
return | ||
} | ||
|
||
contentTypeDetected = true | ||
// return Response with mime type | ||
const contentType = detectContentType(ipfsPath, chunk) | ||
|
||
if (typeof Blob === 'undefined') { | ||
return contentType | ||
? resolve(new Response(responseStream, header(200, 'OK', { 'Content-Type': contentType }))) | ||
: resolve(new Response(responseStream, header())) | ||
} | ||
|
||
try { | ||
const blob = await toBlob(responseStream) | ||
|
||
return contentType | ||
? resolve(new Response(blob, header(200, 'OK', { 'Content-Type': contentType }))) | ||
: resolve(new Response(blob, header())) | ||
} catch (err) { | ||
return resolve(new Response(err.toString(), header(500, 'Error fetching the file'))) | ||
} | ||
}) | ||
}) | ||
if (typeof Blob === 'undefined') { | ||
const responseStream = toStream.readable(toBuffer(source)) | ||
|
||
return contentType | ||
? new Response(responseStream, getHeader(200, 'OK', { 'Content-Type': contentType })) | ||
: new Response(responseStream, getHeader()) | ||
} | ||
|
||
try { | ||
const data = await concat(source) | ||
const blob = new Blob([data.slice()]) | ||
|
||
return contentType | ||
? new Response(blob, getHeader(200, 'OK', { 'Content-Type': contentType })) | ||
: new Response(blob, getHeader()) | ||
} catch (err) { | ||
return new Response(err.toString(), getHeader(500, 'Error fetching the file')) | ||
} | ||
} catch (error) { | ||
log(error) | ||
return handleResolveError(ipfsNode, ipfsPath, error) | ||
} | ||
} | ||
|
||
module.exports = { | ||
getResponse: response, | ||
resolver: resolver | ||
getResponse, | ||
resolver | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.