This repository was archived by the owner on Sep 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
refactor: use new IPFS async/await APIs #30
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -36,19 +36,21 @@ | |
"debug": "^4.1.1", | ||
"file-type": "^8.0.0", | ||
"filesize": "^3.6.1", | ||
"get-stream": "^3.0.0", | ||
"ipfs-unixfs": "~0.1.16", | ||
"it-concat": "^1.0.0", | ||
"it-reader": "^2.1.0", | ||
"it-to-stream": "^0.1.1", | ||
"mime-types": "^2.1.21", | ||
"multihashes": "~0.4.14", | ||
"p-try-each": "^1.0.1", | ||
"stream-to-blob": "^2.0.0" | ||
"p-try-each": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"aegir": "^18.0.3", | ||
"aegir": "^20.5.0", | ||
"chai": "^4.2.0", | ||
"dirty-chai": "^2.0.1", | ||
"ipfs": "0.36.0", | ||
"ipfsd-ctl": "~0.42.2" | ||
"get-stream": "^3.0.0", | ||
"ipfs": "github:ipfs/js-ipfs#refactor/async-await-roundup", | ||
"ipfsd-ctl": "^1.0.2", | ||
"it-all": "^1.0.1" | ||
}, | ||
"contributors": [ | ||
"Alex Potsides <[email protected]>", | ||
|
This file contains hidden or 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 hidden or 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,92 @@ | ||
/* 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 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'))) | ||
if (typeof Blob === 'undefined') { | ||
const responseStream = toStream.readable((async function * () { | ||
for await (const chunk of source) { | ||
yield chunk.slice() // Convert BufferList to Buffer | ||
} | ||
}) | ||
|
||
// 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())) | ||
} | ||
return contentType | ||
? new Response(responseStream, getHeader(200, 'OK', { 'Content-Type': contentType })) | ||
: new Response(responseStream, getHeader()) | ||
} | ||
|
||
try { | ||
const blob = await toBlob(responseStream) | ||
try { | ||
const data = await concat(source) | ||
const blob = new Blob([data.slice()]) | ||
|
||
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'))) | ||
} | ||
}) | ||
}) | ||
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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.