This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v0.10.0 - ANS and File caching (#71)
- Loading branch information
1 parent
24050d9
commit 366f6c9
Showing
16 changed files
with
141 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
"require-jsdoc": 0, | ||
"max-len": 0, | ||
"camelcase": 0, | ||
"guard-for-in": 0 | ||
"guard-for-in": 0, | ||
"prefer-spread": 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import {dir, write, remove} from 'fs-jetpack'; | ||
import {DataItemJson} from 'arweave-bundles'; | ||
import {ansBundles} from '../utility/ans.utility'; | ||
import {getDataFromChunks} from '../query/node.query'; | ||
import {tagToUTF8} from '../query/transaction.query'; | ||
|
||
export async function streamAndCacheAns(id: string): Promise<boolean> { | ||
try { | ||
dir(`${process.cwd()}/cache/tx`); | ||
|
||
const rawData = await getDataFromChunks(id); | ||
const ansTxs = await ansBundles.unbundleData(rawData.toString('utf-8')); | ||
|
||
const ansTxsConverted: Array<DataItemJson> = []; | ||
|
||
for (let i = 0; i < ansTxs.length; i++) { | ||
const ansTx = ansTxs[i]; | ||
const newAnsTx: DataItemJson = { | ||
id: ansTx.id, | ||
owner: ansTx.owner, | ||
target: ansTx.target, | ||
nonce: ansTx.nonce, | ||
data: ansTx.data, | ||
signature: ansTx.signature, | ||
tags: tagToUTF8(ansTx.tags), | ||
}; | ||
|
||
ansTxsConverted.push(newAnsTx); | ||
} | ||
|
||
write(`${process.cwd()}/cache/tx/${id}`, JSON.stringify(ansTxsConverted, null, 2)); | ||
|
||
return true; | ||
} catch (error) { | ||
remove(`${process.cwd()}/cache/tx/${id}`); | ||
console.error(`error caching data from ${id}, please note that this may be a cancelled transaction`.red.bold); | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {exists} from 'fs-jetpack'; | ||
import {streamAndCacheTx} from './stream.caching'; | ||
import {streamAndCacheAns} from './ans.caching'; | ||
|
||
export async function cacheFile(id: string) { | ||
if (exists(`${process.cwd()}/cache/tx/${id}`) === false) { | ||
await streamAndCacheTx(id); | ||
} | ||
} | ||
|
||
export async function cacheAnsFile(id: string) { | ||
if (exists(`${process.cwd()}/cache/tx/${id}`) === false) { | ||
await streamAndCacheAns(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {createWriteStream} from 'fs'; | ||
import {dir, remove} from 'fs-jetpack'; | ||
import {getTransactionOffset, getChunk} from '../query/chunk.query'; | ||
|
||
export async function streamAndCacheTx(id: string): Promise<boolean> { | ||
try { | ||
dir(`${process.cwd()}/cache/tx`); | ||
|
||
const fileStream = createWriteStream(`${process.cwd()}/cache/tx/${id}`, {flags: 'w'}); | ||
const {startOffset, endOffset} = await getTransactionOffset(id); | ||
|
||
let byte = 0; | ||
|
||
while (startOffset + byte < endOffset) { | ||
const chunk = await getChunk(startOffset + byte); | ||
byte += chunk.parsed_chunk.length; | ||
|
||
fileStream.write(Buffer.from(chunk.parsed_chunk)); | ||
} | ||
|
||
fileStream.end(); | ||
|
||
return true; | ||
} catch (error) { | ||
remove(`${process.cwd()}/cache/tx/${id}`); | ||
console.error(`error caching data from ${id}, please note that this may be a cancelled transaction`.red.bold); | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3956,6 +3956,14 @@ fs-capacitor@^2.0.4: | |
resolved "https://registry.yarnpkg.com/fs-capacitor/-/fs-capacitor-2.0.4.tgz#5a22e72d40ae5078b4fe64fe4d08c0d3fc88ad3c" | ||
integrity sha512-8S4f4WsCryNw2mJJchi46YgB6CR5Ze+4L1h8ewl9tEpL4SJ3ZO+c/bS4BWhB8bK+O3TMqhuZarTitd0S0eh2pA== | ||
|
||
fs-jetpack@^4.1.0: | ||
version "4.1.0" | ||
resolved "https://registry.yarnpkg.com/fs-jetpack/-/fs-jetpack-4.1.0.tgz#d693fcffd3cedbd8829226967866b9e89f290f0f" | ||
integrity sha512-h4nHLIcCaxnXfUWhwP+mLnar03R2DBlqicNvKJG44TJob8RV6GB8EKNwJgSaBeDAfqWhqq01y+Ao96vRwpXlPw== | ||
dependencies: | ||
minimatch "^3.0.2" | ||
rimraf "^2.6.3" | ||
|
||
fs.realpath@^1.0.0: | ||
version "1.0.0" | ||
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" | ||
|
@@ -5581,7 +5589,7 @@ minimalistic-crypto-utils@^1.0.1: | |
resolved "https://registry.yarnpkg.com/minimalistic-crypto-utils/-/minimalistic-crypto-utils-1.0.1.tgz#f6c00c1c0b082246e5c4d99dfb8c7c083b2b582a" | ||
integrity sha1-9sAMHAsIIkblxNmd+4x8CDsrWCo= | ||
|
||
[email protected], minimatch@^3.0.4: | ||
[email protected], minimatch@^3.0.2, minimatch@^3.0.4: | ||
version "3.0.4" | ||
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" | ||
integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA== | ||
|
@@ -6707,6 +6715,13 @@ rfc4648@^1.4.0: | |
resolved "https://registry.yarnpkg.com/rfc4648/-/rfc4648-1.4.0.tgz#c75b2856ad2e2d588b6ddb985d556f1f7f2a2abd" | ||
integrity sha512-3qIzGhHlMHA6PoT6+cdPKZ+ZqtxkIvg8DZGKA5z6PQ33/uuhoJ+Ws/D/J9rXW6gXodgH8QYlz2UCl+sdUDmNIg== | ||
|
||
rimraf@^2.6.3: | ||
version "2.7.1" | ||
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.7.1.tgz#35797f13a7fdadc566142c29d4f07ccad483e3ec" | ||
integrity sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w== | ||
dependencies: | ||
glob "^7.1.3" | ||
|
||
rimraf@^3.0.0, rimraf@^3.0.2: | ||
version "3.0.2" | ||
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.2.tgz#f1a5402ba6220ad52cc1282bac1ae3aa49fd061a" | ||
|