jupiter-fs
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -26,2 +26,3 @@ "use strict"; | ||
const assert_1 = __importDefault(require("assert")); | ||
const stream_1 = require("stream"); | ||
const uuid_1 = require("uuid"); | ||
@@ -153,3 +154,3 @@ const jupiter_node_sdk_1 = __importStar(require("jupiter-node-sdk")); | ||
*/ | ||
async getFile({ name, id }) { | ||
async getFile({ name, id }, isReadStream = false) { | ||
await this.getOrCreateBinaryAddress(); | ||
@@ -160,17 +161,36 @@ const files = await this.ls(); | ||
const dataTxns = JSON.parse(await this.client.decrypt(targetFile.txns)); | ||
const base64Strings = await Promise.all(dataTxns.map(async (txnId) => { | ||
const { data } = await this.binaryClient.request('post', '/nxt', { | ||
params: { | ||
requestType: 'readMessage', | ||
secretPhrase: encryptSecret || this.binaryClient.passphrase, | ||
transaction: txnId, | ||
}, | ||
}); | ||
if (data.errorCode > 0) | ||
throw new Error(JSON.stringify(data)); | ||
const jsonWithData = await this.binaryClient.decrypt(data.decryptedMessage); | ||
return JSON.parse(jsonWithData).data; | ||
})); | ||
const readable = new stream_1.Readable(); | ||
const getBase64Strings = async (readableStream) => { | ||
const allBase64Strings = await Promise.all(dataTxns.map(async (txnId) => { | ||
const { data } = await this.binaryClient.request('post', '/nxt', { | ||
params: { | ||
requestType: 'readMessage', | ||
secretPhrase: encryptSecret || this.binaryClient.passphrase, | ||
transaction: txnId, | ||
}, | ||
}); | ||
if (data.errorCode > 0) | ||
throw new Error(JSON.stringify(data)); | ||
const jsonWithData = await this.binaryClient.decrypt(data.decryptedMessage); | ||
const base64Chunk = JSON.parse(jsonWithData).data; | ||
if (readableStream) | ||
readableStream.push(Buffer.from(base64Chunk, 'base64')); | ||
return base64Chunk; | ||
})); | ||
if (readableStream) | ||
readableStream.push(null); | ||
return allBase64Strings; | ||
}; | ||
if (isReadStream) { | ||
readable._read = async () => { | ||
await getBase64Strings(readable); | ||
}; | ||
return readable; | ||
} | ||
const base64Strings = await getBase64Strings(); | ||
return Buffer.from(base64Strings.join(''), 'base64'); | ||
}, | ||
async getFileStream({ name, id }) { | ||
return await this.getFile({ name, id }, true); | ||
}, | ||
async newBinaryAddress() { | ||
@@ -177,0 +197,0 @@ const passphrase = jupiter_node_sdk_1.generatePassphrase(); |
{ | ||
"name": "jupiter-fs", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A small file system implementation for the Jupiter blockchain.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/JupiterFs.js", |
@@ -41,2 +41,6 @@ # jupiter-fs | ||
// you can also get the file as a Readable stream to pipe to | ||
// or be piped other streams | ||
await readStream = await jupFs.getFileStream({ name: 'myFile.txt' }) | ||
// delete the file by providing the file ID (UUID assigned originally) | ||
@@ -43,0 +47,0 @@ await jupFs.deleteFile('123e4567-e89b-42d3-a456-556642440000') |
import assert from 'assert' | ||
import { Readable } from 'stream' | ||
import { v1 as uuidv1 } from 'uuid' | ||
@@ -174,3 +175,6 @@ import JupiterClient, { generatePassphrase } from 'jupiter-node-sdk' | ||
*/ | ||
async getFile({ name, id }: any): Promise<Buffer> { | ||
async getFile( | ||
{ name, id }: any, | ||
isReadStream: boolean = false | ||
): Promise<Buffer | Readable> { | ||
await this.getOrCreateBinaryAddress() | ||
@@ -183,21 +187,46 @@ const files = await this.ls() | ||
const dataTxns = JSON.parse(await this.client.decrypt(targetFile.txns)) | ||
const base64Strings: string[] = await Promise.all( | ||
dataTxns.map(async (txnId: string) => { | ||
const { data } = await this.binaryClient.request('post', '/nxt', { | ||
params: { | ||
requestType: 'readMessage', | ||
secretPhrase: encryptSecret || this.binaryClient.passphrase, | ||
transaction: txnId, | ||
}, | ||
const readable = new Readable() | ||
const getBase64Strings = async ( | ||
readableStream?: Readable | ||
): Promise<string[]> => { | ||
const allBase64Strings: string[] = await Promise.all( | ||
dataTxns.map(async (txnId: string) => { | ||
const { data } = await this.binaryClient.request('post', '/nxt', { | ||
params: { | ||
requestType: 'readMessage', | ||
secretPhrase: encryptSecret || this.binaryClient.passphrase, | ||
transaction: txnId, | ||
}, | ||
}) | ||
if (data.errorCode > 0) throw new Error(JSON.stringify(data)) | ||
const jsonWithData = await this.binaryClient.decrypt( | ||
data.decryptedMessage | ||
) | ||
const base64Chunk = JSON.parse(jsonWithData).data | ||
if (readableStream) | ||
readableStream.push(Buffer.from(base64Chunk, 'base64')) | ||
return base64Chunk | ||
}) | ||
if (data.errorCode > 0) throw new Error(JSON.stringify(data)) | ||
const jsonWithData = await this.binaryClient.decrypt( | ||
data.decryptedMessage | ||
) | ||
return JSON.parse(jsonWithData).data | ||
}) | ||
) | ||
) | ||
if (readableStream) readableStream.push(null) | ||
return allBase64Strings | ||
} | ||
if (isReadStream) { | ||
readable._read = async () => { | ||
await getBase64Strings(readable) | ||
} | ||
return readable | ||
} | ||
const base64Strings = await getBase64Strings() | ||
return Buffer.from(base64Strings.join(''), 'base64') | ||
}, | ||
async getFileStream({ name, id }: any): Promise<Readable> { | ||
return await this.getFile({ name, id }, true) | ||
}, | ||
async newBinaryAddress() { | ||
@@ -204,0 +233,0 @@ const passphrase = generatePassphrase() |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32837
654
57