Skip to content
This repository was archived by the owner on Sep 28, 2021. It is now read-only.

fix: replace node buffers with uint8arrays #55

Merged
merged 2 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ stages:
- cov

node_js:
- '10'
- '12'
- 'lts/*'
- 'node'

os:
- linux
Expand All @@ -21,7 +21,6 @@ jobs:
include:
- stage: check
script:
- npx aegir build --bundlesize
- npx aegir dep-check
- npm run lint

Expand Down
22 changes: 10 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"lint": "aegir lint",
"release": "aegir release --target node",
"release-minor": "aegir release --type minor --target node",
"build": "aegir build",
"test": "aegir test -t node",
"test:node": "aegir test -t node"
},
Expand All @@ -32,26 +31,25 @@
},
"homepage": "https://github.com/ipfs/js-ipfs-http-response#readme",
"dependencies": {
"cids": "~0.8.1",
"debug": "^4.1.1",
"file-type": "^8.0.0",
"filesize": "^3.6.1",
"file-type": "^14.7.1",
"filesize": "^6.1.0",
"it-buffer": "^0.1.1",
"it-concat": "^1.0.0",
"it-reader": "^2.1.0",
"it-to-stream": "^0.1.1",
"mime-types": "^2.1.27",
"multihashes": "~0.4.19",
"multihashes": "^3.0.1",
"p-try-each": "^1.0.1"
},
"devDependencies": {
"aegir": "^22.0.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"get-stream": "^3.0.0",
"ipfs": "^0.47.0",
"ipfsd-ctl": "^1.0.2",
"it-all": "^1.0.1"
"aegir": "^25.1.0",
"cids": "^1.0.0",
"get-stream": "^6.0.0",
"ipfs": "^0.49.0",
"ipfsd-ctl": "^6.0.0",
"it-all": "^1.0.1",
"uint8arrays": "^1.1.0"
},
"contributors": [
"Vasco Santos <vasco.santos@moxy.studio>",
Expand Down
8 changes: 5 additions & 3 deletions src/utils/content-type.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict'

const fileType = require('file-type')
const { fromBuffer: fileType } = require('file-type')
const mime = require('mime-types')
const Reader = require('it-reader')

const minimumBytes = 4100

const detectContentType = async (path, source) => {
let fileSignature

Expand All @@ -12,11 +14,11 @@ const detectContentType = async (path, source) => {
if (!path.endsWith('.svg')) {
try {
const reader = Reader(source)
const { value, done } = await reader.next(fileType.minimumBytes)
const { value, done } = await reader.next(minimumBytes)

if (done) return { source: reader }

fileSignature = fileType(value.slice())
fileSignature = await fileType(value.slice())

source = (async function * () { // eslint-disable-line require-await
yield value
Expand Down
70 changes: 35 additions & 35 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')

const loadFixture = require('aegir/fixtures')
const Ctl = require('ipfsd-ctl')
const { createFactory } = require('ipfsd-ctl')
const getStream = require('get-stream')
const CID = require('cids')
const all = require('it-all')
const uint8ArrayToString = require('uint8arrays/to-string')

const { getResponse } = require('../src')
const makeWebResponseEnv = require('./utils/web-response-env')

const factory = Ctl.createFactory({
const factory = createFactory({
test: true,
type: 'proc',
ipfsModule: {
ref: require('ipfs'),
path: require.resolve('ipfs')
}
ipfsModule: require('ipfs')
})

describe('resolve file (CIDv0)', function () {
let ipfs = null
let ipfsd = null

const file = {
cid: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
Expand All @@ -37,26 +31,24 @@ describe('resolve file (CIDv0)', function () {
this.timeout(20 * 1000)
Object.assign(global, makeWebResponseEnv())

ipfsd = await factory.spawn()
const ipfsd = await factory.spawn()
ipfs = ipfsd.api

const filesAdded = await all(ipfs.add(file.data, { cidVersion: 0 }))

expect(filesAdded).to.have.length(1)

const retrievedFile = filesAdded[0]
const retrievedFile = await ipfs.add(file.data, { cidVersion: 0 })
expect(retrievedFile.cid).to.deep.equal(new CID(file.cid))
expect(retrievedFile.size, 'ipfs.add result size should not be smaller than input buffer').greaterThan(file.data.length)
})

after(() => factory.clean())

it('should resolve a CIDv0', async () => {
const res = await getResponse(ipfs, `/ipfs/${file.cid}`)

expect(res).to.exist()
expect(res.status).to.equal(200)

const contents = await getStream(res.body)
const expectedContents = loadFixture('test/fixtures/testfile.txt').toString()
const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/testfile.txt'))

expect(contents).to.equal(expectedContents)
})
Expand All @@ -78,23 +70,21 @@ describe('resolve file (CIDv1)', function () {
ipfsd = await factory.spawn()
ipfs = ipfsd.api

const filesAdded = await all(ipfs.add(file.data, { cidVersion: 1 }))

expect(filesAdded).to.have.length(1)

const retrievedFile = filesAdded[0]
const retrievedFile = await ipfs.add(file.data, { cidVersion: 1 })
expect(retrievedFile.cid).to.deep.equal(new CID(file.cid))
// expect(retrievedFile.size, 'ipfs.add result size should not be smaller than input buffer').greaterThan(file.data.length)
expect(retrievedFile.size, 'ipfs.add result size should equal input buffer').to.equal(file.data.length)
})

after(() => factory.clean())

it('should resolve a CIDv1', async () => {
const res = await getResponse(ipfs, `/ipfs/${file.cid}`)

expect(res).to.exist()
expect(res.status).to.equal(200)

const contents = await getStream(res.body)
const expectedContents = loadFixture('test/fixtures/testfile.txt').toString()
const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/testfile.txt'))

expect(contents).to.equal(expectedContents)
})
Expand Down Expand Up @@ -129,7 +119,7 @@ describe('resolve directory (CIDv0)', function () {
content('holmes.txt')
]

const res = await all(ipfs.add(dirs, { cidVersion: 0 }))
const res = await all(ipfs.addAll(dirs, { cidVersion: 0 }))
const root = res[res.length - 1]

expect(root.path).to.equal('test-folder')
Expand All @@ -139,6 +129,8 @@ describe('resolve directory (CIDv0)', function () {
expect(res[1].size, 'ipfs.add 2nd result size should not be smaller than 2nd input buffer').greaterThan(dirs[1].content.length)
})

after(() => factory.clean())

it('should return the list of files of a directory', async () => {
const res = await getResponse(ipfs, `/ipfs/${directory.cid}`, directory.cid)

Expand All @@ -150,7 +142,7 @@ describe('resolve directory (CIDv0)', function () {
const res = await getResponse(ipfs, `/ipfs/${directory.cid}/pp.txt`, directory.cid)

const contents = await getStream(res.body)
const expectedContents = loadFixture('test/fixtures/test-folder/pp.txt').toString()
const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/test-folder/pp.txt'))

expect(contents).to.equal(expectedContents)
})
Expand All @@ -159,7 +151,7 @@ describe('resolve directory (CIDv0)', function () {
const res = await getResponse(ipfs, `/ipfs/${directory.cid}/holmes.txt`, directory.cid)

const contents = await getStream(res.body)
const expectedContents = loadFixture('test/fixtures/test-folder/holmes.txt').toString()
const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/test-folder/holmes.txt'))

expect(contents).to.equal(expectedContents)
})
Expand All @@ -172,7 +164,7 @@ describe('resolve directory (CIDv1)', function () {
const directory = {
cid: 'bafybeifhimn7nu6dgmdvj6o63zegwro3yznnpfqib6kkjnagc54h46ox5q',
files: {
'pp.txt': Buffer.from(loadFixture('test/fixtures/test-folder/pp.txt')),
'pp.txt': loadFixture('test/fixtures/test-folder/pp.txt'),
'holmes.txt': loadFixture('test/fixtures/test-folder/holmes.txt')
}
}
Expand All @@ -194,14 +186,16 @@ describe('resolve directory (CIDv1)', function () {
content('holmes.txt')
]

const res = await all(ipfs.add(dirs, { cidVersion: 1 }))
const res = await all(ipfs.addAll(dirs, { cidVersion: 1 }))
const root = res[res.length - 1]
expect(root.path).to.equal('test-folder')
// expect(res[0].size, 'ipfs.files.add 1st result size should not be smaller than 1st input buffer').greaterThan(dirs[0].content.length)
// expect(res[1].size, 'ipfs.files.add 2nd result size should not be smaller than 2nd input buffer').greaterThan(dirs[1].content.length)
expect(root.cid).to.deep.equal(new CID(directory.cid))
})

after(() => factory.clean())

it('should return the list of files of a directory', async () => {
const res = await getResponse(ipfs, `/ipfs/${directory.cid}`, directory.cid)

Expand All @@ -213,7 +207,7 @@ describe('resolve directory (CIDv1)', function () {
const res = await getResponse(ipfs, `/ipfs/${directory.cid}/pp.txt`, directory.cid)

const contents = await getStream(res.body)
const expectedContents = loadFixture('test/fixtures/test-folder/pp.txt').toString()
const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/test-folder/pp.txt'))

expect(contents).to.equal(expectedContents)
})
Expand All @@ -222,7 +216,7 @@ describe('resolve directory (CIDv1)', function () {
const res = await getResponse(ipfs, `/ipfs/${directory.cid}/holmes.txt`, directory.cid)

const contents = await getStream(res.body)
const expectedContents = loadFixture('test/fixtures/test-folder/holmes.txt').toString()
const expectedContents = uint8ArrayToString(loadFixture('test/fixtures/test-folder/holmes.txt'))

expect(contents).to.equal(expectedContents)
})
Expand Down Expand Up @@ -259,13 +253,15 @@ describe('resolve web page (CIDv0)', function () {
content('index.html')
]

const res = await all(ipfs.add(dirs, { cidVersion: 0 }))
const res = await all(ipfs.addAll(dirs, { cidVersion: 0 }))
const root = res[res.length - 1]

expect(root.path).to.equal('test-site')
expect(root.cid).to.deep.equal(new CID(webpage.cid))
})

after(() => factory.clean())

it('should return the entry point of a web page when a trying to fetch a directory containing a web page', async () => {
const res = await getResponse(ipfs, `/ipfs/${webpage.cid}`, webpage.cid)

Expand Down Expand Up @@ -305,13 +301,15 @@ describe('resolve web page (CIDv1)', function () {
content('index.html')
]

const res = await all(ipfs.add(dirs, { cidVersion: 1 }))
const res = await all(ipfs.addAll(dirs, { cidVersion: 1 }))
const root = res[res.length - 1]

expect(root.path).to.equal('test-site')
expect(root.cid).to.deep.equal(new CID(webpage.cid))
})

after(() => factory.clean())

it('should return the entry point of a web page when a trying to fetch a directory containing a web page', async () => {
const res = await getResponse(ipfs, `/ipfs/${webpage.cid}`, webpage.cid)

Expand Down Expand Up @@ -356,13 +354,15 @@ describe('mime-types', () => {
content('index.html')
]

const res = await all(ipfs.add(dirs, { cidVersion: 0 }))
const res = await all(ipfs.addAll(dirs, { cidVersion: 0 }))
const root = res[res.length - 1]

expect(root.path).to.equal('test-mime-types')
expect(root.cid).to.deep.equal(new CID(webpage.cid))
})

after(() => factory.clean())

it('should return the correct mime-type for pp.txt', async () => {
const res = await getResponse(ipfs, `/ipfs/${webpage.cid}/pp.txt`, webpage.cid)

Expand Down
Loading