Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.

Commit

Permalink
fix/fetch: when fetching a MD the resourceType value was 'NFS' inst…
Browse files Browse the repository at this point in the history
…ead of 'MD' (#321)
  • Loading branch information
bochaco committed Dec 5, 2018
1 parent bdad4da commit 5daa617
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# safe_app nodejs Change Log

## [Unreleased]

### Fixed
- Experimental function `fetch` was not returning value `MD` in `resourceType` when fetching a MutableData

### SAFE libraries Dependencies
- safe_app: v0.9.0
- system_uri: v0.4.0
- deps_downloader: v0.3.0

## [0.10.0] - 26-11-2018
### Added
- Add instructions to the README for how to generate API docs locally
Expand Down
6 changes: 2 additions & 4 deletions src/web_fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,9 @@ async function fetchHelper(url) {
const content = await getContainerFromCid.call(this, publicName,
parseInt(parsedUrl.port, 10));

const resourceType = (content.type === DATA_TYPE_MD) ? DATA_TYPE_NFS : content.type;

return {
content: content.content,
resourceType,
resourceType: content.type,
parsedPath,
mimeType: content.codec
};
Expand Down Expand Up @@ -486,7 +484,7 @@ async function webFetch(url, options) {
}
const path = tokens.join('/') || `/${consts.INDEX_HTML}`;
try {
const emulation = content.emulateAs(resourceType);
const emulation = content.emulateAs(DATA_TYPE_NFS);
const { file, mimeType: fileMimeType } = await
tryDifferentPaths(emulation.fetch.bind(emulation), path);
const openedFile = await emulation.open(file, consts.pubConsts.NFS_FILE_MODE_READ);
Expand Down
65 changes: 60 additions & 5 deletions test/experimental_apis/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
const should = require('should');
const helpers = require('../helpers');
const errConst = require('../../src/error_const');
const { pubConsts: CONSTANTS } = require('../../src/consts');

const createAuthenticatedTestApp = helpers.createAuthenticatedTestApp;
const createUnregisteredTestApp = helpers.createUnregisteredTestApp;
Expand Down Expand Up @@ -48,17 +49,71 @@ describe('Experimental fetch function', () => {
const filePath = '/yumyum.html';
const { domain } = await createRandomDomain(content, filePath, '', app);
const url = `safe://${domain}${filePath}#me`;

const networkResource = await unregisteredApp.fetch(url);
should(networkResource.resourceType).equal('NFS');
should(networkResource.parsedPath).equal(filePath);
return should(networkResource.content.get(filePath)).be.fulfilled();
const nfs = networkResource.content.emulateAs('nfs');
const retrievedFile = await nfs.fetch(filePath);
const file = await nfs.open(retrievedFile, CONSTANTS.NFS_FILE_MODE_READ);
const data = await file.read(CONSTANTS.NFS_FILE_START, CONSTANTS.NFS_FILE_END);
return should(data.toString()).be.equal(content);
});

// not supported yet, it needs support for XOR-URL
it.skip('fetch a MD resource', async () => {
it('fetch a MutableData resource', async () => {
const key = 'some key string';
const value = `hello world, on ${Math.round(Math.random() * 100000)}`;
const md = await app.mutableData.newRandomPublic(16839);
await md.quickSetup({ [key]: value });
const info = await md.getNameAndTag();
should(info.xorUrl).not.be.undefined();

const networkResource = await unregisteredApp.fetch(info.xorUrl);
should(networkResource.resourceType).equal('MD');
should(networkResource.parsedPath).equal('');
const data = await networkResource.content.get(key);
return should(data.buf.toString()).be.equal(value);
});

// not supported yet, it needs support for XOR-URL
it.skip('fetch an IMD resource', async () => {
it('fetch an ImmutableData resource', async () => {
const content = `hello world, on ${Math.round(Math.random() * 100000)}`;
const idWriter = await app.immutableData.create();
await idWriter.write(content);
const cipherOpt = await app.cipherOpt.newPlainText();
const getXorUrl = true;
const immDataAddr = await idWriter.close(cipherOpt, getXorUrl);
should(immDataAddr.xorUrl).not.be.undefined();

const networkResource = await unregisteredApp.fetch(immDataAddr.xorUrl);
const data = await networkResource.content.read();
should(networkResource.resourceType).equal('IMMD');
should(networkResource.parsedPath).equal('');
return should(data.toString()).be.equal(content);
});

it('fetch an RDF resource', async () => {
const randomName = `test_${Math.round(Math.random() * 100000)}`;
const newApp = await helpers.publicNamesTestApp();
const { serviceMd } = await createRandomDomain('', '', '', newApp);
const info = await serviceMd.getNameAndTag();
should(info.xorUrl).not.be.undefined();
const profile = {
uri: `safe://mywebid.${randomName}`,
name: randomName,
nick: randomName,
website: `safe://mywebsite.${randomName}`,
image: `safe://mywebsite.${randomName}/images/myavatar`,
};
const webId = serviceMd.emulateAs('WebID');
await webId.create(profile);
const serialisedWebID = await webId.serialise();

const networkResource = await unregisteredApp.fetch(profile.uri);
should(networkResource.resourceType).equal('RDF');
should(networkResource.parsedPath).equal('');
const rdf = networkResource.content.emulateAs('rdf');
await rdf.nowOrWhenFetched();
const serialised = await rdf.serialise();
return should(serialised).be.equal(serialisedWebID);
});
});
2 changes: 2 additions & 0 deletions test/experimental_apis/rdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ describe('Experimental RDF emulation', () => {
return should(rdf.parse({}, JSON_LD_MIME_TYPE, myUri)).be.rejected();
});

it('generate a blank node', async () => should(rdf.bnode).be.not.undefined());

it('add triples and find any friend', async () => {
await md.quickSetup({});
await rdf.parse(JSON.stringify(myJsonLd), JSON_LD_MIME_TYPE, myUri);
Expand Down

0 comments on commit 5daa617

Please sign in to comment.