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

Commit

Permalink
Merge 6c3ea99 into e3d60a7
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterlester committed Nov 21, 2018
2 parents e3d60a7 + 6c3ea99 commit 1a0cad7
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Experimental WebID emulation layer which provides an abstraction to generate and manipulate WebID profile documents on the SAFE network.
- Experimental Web API which provides utilities to manage public names and subNames, get the list of owned WebIDs, get list of owned public names, etc.
- Rename `isMockBuild` function to `appIsMock` to match the renaming in safe_app lib v0.9.0.
- Constant to automatically obtain correct file version for updates and deletes

### Changed
- Dependency downloader configuration to download both mock and prod libs without `NODE_ENV=dev`
Expand Down
22 changes: 15 additions & 7 deletions src/api/emulations/nfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,12 @@ class NFS {

/**
* Replace a path with a new file. Directly commit to the network.
*
* CONSTANTS.GET_NEXT_VERSION: Applies update to next file version.
*
* @param {(String|Buffer)} fileName - the path to store the file under
* @param {File} file - the file to serialise and store
* @param {Number} version - the version successor number, to ensure you
* @param {Number|CONSTANTS.GET_NEXT_VERSION} version - the version successor number, to ensure you
are overwriting the right one
* @param {String|Buffer} userMetadata - optional parameter for updating user metadata
* @returns {Promise<File>} - the same file
Expand All @@ -253,20 +256,25 @@ class NFS {
fileMeta.user_metadata_len = userMetadata.length;
fileMeta.user_metadata_cap = userMetadataPtr.length;
}
const fileContext = file;
return lib.dir_update_file(this.mData.app.connection, this.mData.ref, fileName,
file.ref.ref(), version)
.then(() => { file.version = version; }) // eslint-disable-line no-param-reassign
.then(() => file);
fileContext.ref.ref(), version)
.then((newVersion) => {
fileContext.version = newVersion;
})
.then(() => fileContext);
}

/**
* Delete a file from path. Directly commit to the network.
* @param {(String|Buffer)} fileName
* @param {Number} version
* @returns {Promise}
* @param {Number|CONSTANTS.GET_NEXT_VERSION} version - the version successor number, to ensure you
are deleting the right one
* @returns {Promise<Number>} - version of deleted file
*/
delete(fileName, version) {
return lib.dir_delete_file(this.mData.app.connection, this.mData.ref, fileName, version);
return lib.dir_delete_file(this.mData.app.connection, this.mData.ref, fileName, version)
.then((newVersion) => newVersion);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ const LIB_LOCATION_PROD = 'prod';
* This can be used when invoking the `put` function of the MutableData API to
* signal that it should be committed to the network with an empty set of permissions.
*
* @param {Number} GET_NEXT_VERSION Gets next correct file version.
* This constant may be used in place of the version argument when
* invoking `update` function of the NFS API to automatically obtain correct file version.
*
*/
const pubConsts = {
NFS_FILE_MODE_OVERWRITE: 1,
Expand All @@ -90,6 +94,7 @@ const pubConsts = {
MD_METADATA_KEY: '_metadata',
MD_ENTRIES_EMPTY: 0,
MD_PERMISSION_EMPTY: 0,
GET_NEXT_VERSION: 0
};

const SAFE_APP_LIB_FILENAME = {
Expand Down
4 changes: 2 additions & 2 deletions src/native/_nfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ module.exports = {
api: {
dir_fetch_file: h.Promisified(toMDataInfo, [FilePtr, t.u64], readFileInfo),
dir_insert_file: h.Promisified(toMDataInfo, []),
dir_update_file: h.Promisified(toMDataInfo, []),
dir_delete_file: h.Promisified(toMDataInfo, []),
dir_update_file: h.Promisified(toMDataInfo, [t.u64]),
dir_delete_file: h.Promisified(toMDataInfo, [t.u64]),
file_open: h.Promisified(toMDataInfo, FileContextHandle),
file_size: h.Promisified(null, [t.u64]),
file_read: h.Promisified(null, [t.u8Pointer, t.usize], h.asBuffer),
Expand Down
3 changes: 2 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ describe('Smoke testing', () => {
USER_ANYONE: 0,
MD_METADATA_KEY: '_metadata',
MD_ENTRIES_EMPTY: 0,
MD_PERMISSION_EMPTY: 0
MD_PERMISSION_EMPTY: 0,
GET_NEXT_VERSION: 0
};
return should(CONSTANTS).be.eql(expectedConsts);
});
Expand Down
13 changes: 13 additions & 0 deletions test/nfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,23 @@ describe('NFS emulation', () => {
should(file.userMetadata.toString()).be.equal(userMetadata);
const fileVersion = file.version;
file = await nfs.update('hello.txt', file, fileVersion + 1, 'text/javascript');
should(file.version).be.equal(1);
return should(file.userMetadata.toString())
.be.equal('text/javascript');
});

it('automatically obtains correct file version when passing constant', async () => {
const m = await app.mutableData.newRandomPublic(TYPE_TAG);
await m.quickSetup({});
const nfs = await m.emulateAs('nfs');
const userMetadata = 'text/plain';
let file = await nfs.create('hello, SAFE world!');
file = await nfs.insert('hello.txt', file, userMetadata);
should(file.version).be.equal(0);
file = await nfs.update('hello.txt', file, CONSTANTS.GET_NEXT_VERSION, 'text/javascript');
return should(file.version).be.equal(1);
});

it('throws error if invalid user metadata type is passed while updating file', async () => {
const mData = await app.mutableData.newRandomPublic(TYPE_TAG);
await mData.quickSetup({});
Expand Down

0 comments on commit 1a0cad7

Please sign in to comment.