jupiter-fs
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -6,2 +6,7 @@ # Changelog | ||
## [0.3.0] - 2020-08-08 | ||
### Added | ||
- Added MAX_ALLOWED_SIZE validation, maximum as 3mb files | ||
- Calculate stimated fees and fuel the binary account before process the pieces | ||
## [0.2.0] - 2020-07-25 | ||
@@ -8,0 +13,0 @@ ### Added |
@@ -36,7 +36,8 @@ "use strict"; | ||
// Quantity to found the binary client when doesnt have enought founds | ||
minimumFndrAccountBalance = minimumFndrAccountBalance || 1000000; | ||
minimumUserAccountBalance = minimumUserAccountBalance || 2000000; | ||
minimumFndrAccountBalance = minimumFndrAccountBalance || 15000000; | ||
minimumUserAccountBalance = minimumUserAccountBalance || 30000000; | ||
// Chunk size to split the file to upload | ||
// Max lengh in Jupiter is 43008 bytes per encrypted message | ||
const CHUNK_SIZE_PATTERN = /.{1,40000}/g; | ||
const MAX_ALLOWED_SIZE = 3 * 1024 * 1024; | ||
const SUBTYPE_MESSAGING_METIS_DATA = 16; | ||
@@ -90,3 +91,3 @@ const SUBTYPE_MESSAGING_METIS_METADATA = 17; | ||
} | ||
await this.checkAndFundAccount(addy.address); | ||
await this.checkAndFundAccount(addy.address, minimumFndrAccountBalance); | ||
this.binaryClient = jupiter_node_sdk_1.default({ ...addy, | ||
@@ -99,5 +100,7 @@ server: jupServer, | ||
}, | ||
async checkAndFundAccount(targetAddress) { | ||
async checkAndFundAccount(targetAddress, minBalance) { | ||
const minBalanceBI = new bignumber_js_1.default(minBalance); | ||
// Get balance for binary client | ||
const balanceJup = await this.client.getBalance(targetAddress); | ||
let remainingBalanceBI = new bignumber_js_1.default(balanceJup.unconfirmedBalanceNQT).minus(minBalance); | ||
if ( | ||
@@ -107,5 +110,7 @@ // if binary client doesnt have money or is less than minimumFndrAccountBalance | ||
!balanceJup || | ||
new bignumber_js_1.default(balanceJup).lt(new bignumber_js_1.default(this.client.nqtToJup(this.client.config.minimumFndrAccountBalance)).div(2))) { | ||
new bignumber_js_1.default(balanceJup.unconfirmedBalanceNQT).lt(minBalanceBI) || | ||
remainingBalanceBI.lt(minimumFndrAccountBalance)) { | ||
// send money to the binary client to pay fees for transactions | ||
await this.client.sendMoney(targetAddress); | ||
let amountJupToSend = (minBalance > minimumFndrAccountBalance) ? minBalance : minimumFndrAccountBalance; | ||
await this.client.sendMoney(targetAddress, amountJupToSend); | ||
} | ||
@@ -173,2 +178,11 @@ }, | ||
async writeFile(name, data, errorCallback) { | ||
if (data.length > MAX_ALLOWED_SIZE) { | ||
if (errorCallback) { | ||
errorCallback(new Error("File size not allowed")); | ||
return; | ||
} | ||
else { | ||
throw new Error("File size not allowed"); | ||
} | ||
} | ||
await this.getOrCreateBinaryAddress(); | ||
@@ -178,7 +192,7 @@ // compress the binary data before to convert to base64 | ||
const chunks = encodedFileData.match(CHUNK_SIZE_PATTERN); | ||
const expectedFees = this.binaryClient.calculateExpectedFees(chunks); | ||
await this.checkAndFundAccount(this.binaryClient.address, expectedFees); | ||
assert_1.default(chunks, `we couldn't split the data into chunks`); | ||
const dataTxns = await Promise.all(chunks.map(async (str) => { | ||
const { transaction } = await exponentialBackoff(async () => { | ||
// check if the binarclient have enought found for the transaction | ||
await this.checkAndFundAccount(this.binaryClient.address); | ||
return await this.binaryClient.storeRecord({ | ||
@@ -298,3 +312,3 @@ data: str | ||
*/ | ||
async function exponentialBackoff(promiseFunction, failureFunction = () => { }, err = null, totalAllowedBackoffTries = 10, backoffAttempt = 1) { | ||
async function exponentialBackoff(promiseFunction, failureFunction = () => { }, err = null, totalAllowedBackoffTries = 2, backoffAttempt = 1) { | ||
const backoffSecondsToWait = 2 + Math.pow(backoffAttempt, 2); | ||
@@ -301,0 +315,0 @@ if (backoffAttempt > totalAllowedBackoffTries) |
@@ -18,9 +18,12 @@ "use strict"; | ||
this.timeout(40000); | ||
// const fs = JupiterFs({ server: 'http://localhost:7876' }) | ||
assert_1.default(process.env.JUPITER_ADDRESS, 'JUPITER_ADDRESS env variable is not set'); | ||
assert_1.default(process.env.JUPITER_PASSPHRASE, 'JUPITER_PASSPHRASE env variable is not set'); | ||
// const fs = JupiterFs({ server: 'http://localhost:6876' }) | ||
const jupFs = JupiterFs_1.default({ | ||
server: 'http://localhost:6876', | ||
address: 'JUP-XTAE-VA6X-4SRT-AU89L', | ||
passphrase: 'b', | ||
server: process.env.JUPITER_SERVER || 'http://104.131.166.136:6876/test', | ||
address: process.env.JUPITER_ADDRESS, | ||
passphrase: process.env.JUPITER_PASSPHRASE, | ||
}); | ||
const testFilename = `${uuid_1.v1()}.js`; | ||
const IMAGE_TO_TEST = 'big.jpg'; | ||
describe('#newBinaryAddress()', function () { | ||
@@ -35,3 +38,2 @@ it(`should get a new JUP address from a passphrase`, async () => { | ||
const addy = await jupFs.getOrCreateBinaryAddress(); | ||
console.log(addy); | ||
assert_1.default.strictEqual(typeof addy.address === 'string', true); | ||
@@ -43,3 +45,2 @@ }); | ||
const files = await jupFs.ls(); | ||
console.log(files); | ||
assert_1.default.strictEqual(files instanceof Array, true); | ||
@@ -50,4 +51,3 @@ }); | ||
it(`should write a file to a jupiter account without error`, async () => { | ||
const fileData = await fs_1.default.promises.readFile(path_1.default.join(__dirname, '../testFiles/big.jpg'), { encoding: null }); | ||
console.log("filename " + testFilename); | ||
const fileData = await fs_1.default.promises.readFile(path_1.default.join(__dirname, '../testFiles/' + IMAGE_TO_TEST), { encoding: null }); | ||
const res = await jupFs.writeFile(testFilename, fileData, function (errorr) { | ||
@@ -62,5 +62,4 @@ console.log("erorrrrrrrrrrrrr" + errorr); | ||
it(`should get the binary data for a file specified`, async () => { | ||
console.log("filename " + testFilename); | ||
const fileData = await jupFs.getFile({ name: testFilename }); | ||
const origFileData = await fs_1.default.promises.readFile(path_1.default.join(__dirname, '../testFiles/big.jpg'), 'utf-8'); | ||
const origFileData = await fs_1.default.promises.readFile(path_1.default.join(__dirname, '../testFiles/' + IMAGE_TO_TEST), 'utf-8'); | ||
assert_1.default.strictEqual(fileData instanceof Buffer, true); | ||
@@ -71,2 +70,11 @@ assert_1.default.strictEqual(fileData.length > 0, true); | ||
}); | ||
describe('#writeFileSizeNotAllowed()', function () { | ||
it(`should write a file to a jupiter account with size not allowed`, async () => { | ||
const fileData = await fs_1.default.promises.readFile(path_1.default.join(__dirname, '../testFiles/huge.jpg'), { encoding: null }); | ||
const res = await jupFs.writeFile(testFilename, fileData, function (errorr) { | ||
assert_1.default.strictEqual(errorr.message, "File size not allowed"); | ||
}); | ||
assert_1.default.strictEqual(res === undefined, true); | ||
}); | ||
}); | ||
}); |
{ | ||
"name": "jupiter-fs", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A small file system implementation for the Jupiter blockchain.", | ||
@@ -26,3 +26,3 @@ "main": "./dist/JupiterFs.js", | ||
"dependencies": { | ||
"jupiter-node-sdk": "^0.4.0", | ||
"jupiter-node-sdk": "^0.4.1", | ||
"uuid": "^8.3.2" | ||
@@ -29,0 +29,0 @@ }, |
@@ -15,9 +15,17 @@ import assert from 'assert' | ||
// const fs = JupiterFs({ server: 'http://localhost:7876' }) | ||
const jupFs = JupiterFs({ | ||
server: 'http://localhost:6876', | ||
address: 'JUP-XTAE-VA6X-4SRT-AU89L', | ||
passphrase: 'b', | ||
}); | ||
assert(process.env.JUPITER_ADDRESS, 'JUPITER_ADDRESS env variable is not set') | ||
assert( | ||
process.env.JUPITER_PASSPHRASE, | ||
'JUPITER_PASSPHRASE env variable is not set' | ||
) | ||
// const fs = JupiterFs({ server: 'http://localhost:6876' }) | ||
const jupFs = JupiterFs({ | ||
server: process.env.JUPITER_SERVER || 'http://104.131.166.136:6876/test', | ||
address: process.env.JUPITER_ADDRESS, | ||
passphrase: process.env.JUPITER_PASSPHRASE, | ||
}) | ||
const testFilename = `${uuidv1()}.js` | ||
const IMAGE_TO_TEST = 'big.jpg'; | ||
@@ -34,3 +42,2 @@ describe('#newBinaryAddress()', function() { | ||
const addy = await jupFs.getOrCreateBinaryAddress() | ||
console.log(addy); | ||
assert.strictEqual(typeof addy.address === 'string', true) | ||
@@ -40,6 +47,5 @@ }) | ||
describe('#ls()', function() { | ||
describe('#ls()', function() { | ||
it(`should fetch a list of files for a jupiter account`, async () => { | ||
const files = await jupFs.ls() | ||
console.log(files) | ||
assert.strictEqual(files instanceof Array, true) | ||
@@ -52,6 +58,5 @@ }) | ||
const fileData = await fs.promises.readFile( | ||
path.join(__dirname, '../testFiles/big.jpg'), | ||
path.join(__dirname, '../testFiles/'+IMAGE_TO_TEST), | ||
{ encoding: null } | ||
) | ||
console.log("filename " + testFilename); | ||
const res = await jupFs.writeFile(testFilename, fileData, function(errorr: any){ | ||
@@ -65,8 +70,7 @@ console.log("erorrrrrrrrrrrrr" + errorr) | ||
describe('#getFile()', function() { | ||
describe('#getFile()', function() { | ||
it(`should get the binary data for a file specified`, async () => { | ||
console.log("filename " + testFilename); | ||
const fileData = await jupFs.getFile({ name: testFilename }) | ||
const origFileData = await fs.promises.readFile( | ||
path.join(__dirname, '../testFiles/big.jpg'), | ||
path.join(__dirname, '../testFiles/'+IMAGE_TO_TEST), | ||
'utf-8' | ||
@@ -79,2 +83,15 @@ ) | ||
}) | ||
describe('#writeFileSizeNotAllowed()', function() { | ||
it(`should write a file to a jupiter account with size not allowed`, async () => { | ||
const fileData = await fs.promises.readFile( | ||
path.join(__dirname, '../testFiles/huge.jpg'), | ||
{ encoding: null } | ||
) | ||
const res = await jupFs.writeFile(testFilename, fileData, function(errorr: any){ | ||
assert.strictEqual(errorr.message, "File size not allowed") | ||
}) | ||
assert.strictEqual(res === undefined, true) | ||
}) | ||
}) | ||
}) |
@@ -21,4 +21,4 @@ import assert from 'assert' | ||
// Quantity to found the binary client when doesnt have enought founds | ||
minimumFndrAccountBalance = minimumFndrAccountBalance || 1000000 | ||
minimumUserAccountBalance = minimumUserAccountBalance || 2000000 | ||
minimumFndrAccountBalance = minimumFndrAccountBalance || 15000000 | ||
minimumUserAccountBalance = minimumUserAccountBalance || 30000000 | ||
@@ -28,2 +28,3 @@ // Chunk size to split the file to upload | ||
const CHUNK_SIZE_PATTERN = /.{1,40000}/g; | ||
const MAX_ALLOWED_SIZE = 3 * 1024 * 1024; | ||
@@ -87,3 +88,3 @@ const SUBTYPE_MESSAGING_METIS_DATA = 16; | ||
} | ||
await this.checkAndFundAccount(addy.address) | ||
await this.checkAndFundAccount(addy.address, minimumFndrAccountBalance) | ||
this.binaryClient = JupiterClient({ ...addy, | ||
@@ -97,5 +98,9 @@ server: jupServer, | ||
async checkAndFundAccount(targetAddress: string) { | ||
async checkAndFundAccount(targetAddress: string, minBalance: number) { | ||
const minBalanceBI = new BigNumber(minBalance) | ||
// Get balance for binary client | ||
const balanceJup = await this.client.getBalance(targetAddress) | ||
let remainingBalanceBI = new BigNumber(balanceJup.unconfirmedBalanceNQT).minus(minBalance) | ||
if ( | ||
@@ -105,10 +110,9 @@ // if binary client doesnt have money or is less than minimumFndrAccountBalance | ||
!balanceJup || | ||
new BigNumber(balanceJup).lt( | ||
new BigNumber( | ||
this.client.nqtToJup(this.client.config.minimumFndrAccountBalance) | ||
).div(2) | ||
) | ||
new BigNumber(balanceJup.unconfirmedBalanceNQT).lt(minBalanceBI) || | ||
remainingBalanceBI.lt(minimumFndrAccountBalance) | ||
) { | ||
// send money to the binary client to pay fees for transactions | ||
await this.client.sendMoney(targetAddress) | ||
let amountJupToSend = (minBalance > minimumFndrAccountBalance) ? minBalance : minimumFndrAccountBalance | ||
await this.client.sendMoney(targetAddress, amountJupToSend) | ||
} | ||
@@ -200,2 +204,12 @@ }, | ||
) { | ||
if (data.length > MAX_ALLOWED_SIZE) { | ||
if (errorCallback){ | ||
errorCallback(new Error("File size not allowed")); | ||
return; | ||
} else { | ||
throw new Error("File size not allowed"); | ||
} | ||
} | ||
await this.getOrCreateBinaryAddress() | ||
@@ -206,2 +220,5 @@ | ||
const chunks = encodedFileData.match(CHUNK_SIZE_PATTERN) | ||
const expectedFees = this.binaryClient.calculateExpectedFees(chunks); | ||
await this.checkAndFundAccount(this.binaryClient.address, expectedFees) | ||
@@ -213,4 +230,2 @@ assert(chunks, `we couldn't split the data into chunks`) | ||
const { transaction } = await exponentialBackoff(async () => { | ||
// check if the binarclient have enought found for the transaction | ||
await this.checkAndFundAccount(this.binaryClient.address) | ||
return await this.binaryClient.storeRecord({ | ||
@@ -360,3 +375,3 @@ data: str | ||
err = null, | ||
totalAllowedBackoffTries = 10, | ||
totalAllowedBackoffTries = 2, | ||
backoffAttempt = 1 | ||
@@ -363,0 +378,0 @@ ): Promise<any> { |
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
8852302
16
890
6
Updatedjupiter-node-sdk@^0.4.1