Skip to content

Commit

Permalink
v3.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
karnthis committed Sep 21, 2024
1 parent cf3479e commit f79b836
Show file tree
Hide file tree
Showing 10 changed files with 377 additions and 224 deletions.
366 changes: 185 additions & 181 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jackallabs/jackal.js",
"version": "3.1.1",
"version": "3.2.0",
"description": "Javascript library for interacting with the Jackal Chain",
"keywords": [
"jackal",
Expand Down
4 changes: 2 additions & 2 deletions src/classes/encodingHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export class EncodingHandler {
try {
const mH = pkg.meta as IFolderMetaHandler
const meta = mH.export()
console.log('saving:', meta.whoAmI)
// console.log('saving:', meta.whoAmI)

const lookup: IRootLookupMetaData = {
metaDataType: 'rootlookup',
Expand All @@ -381,7 +381,7 @@ export class EncodingHandler {
try {
const mH = pkg.meta as IFolderMetaHandler
const meta = mH.export()
console.log('saving:', meta.whoAmI)
// console.log('saving:', meta.whoAmI)

const parentAndChild = await merkleParentAndChild(`s/ulid/${mH.getUlid()}`)
return await this.storageEncodeFileTree(parentAndChild, meta, { aes: pkg.aes })
Expand Down
19 changes: 12 additions & 7 deletions src/classes/filetreeReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,21 @@ export class FiletreeReader implements IFiletreeReader {
* @returns {IChildMetaDataMap} - Contents structure.
*/
async readFolderContents (path: string, options: IReadFolderContentOptions = {}): Promise<IChildMetaDataMap> {
// console.log(path)
const {
owner = this.clientAddress,
refresh = false,
} = options
// console.log(options)
// console.log(this.bluepages[owner])
// console.log(this.yellowpages[owner])

try {
if (this.bluepages[owner][path]) {
if (refresh) {
await this.pathToLookupPostProcess(path, owner, this.yellowpages[owner][path])
}
if (this.bluepages[owner][path] && !refresh) {
return this.bluepages[owner][path]
} else {
throw new Error('No Resource Found')
await this.pathToLookupPostProcess(path, owner, this.yellowpages[owner][path])
return this.bluepages[owner][path]
}
} catch (err) {
throw warnError('filetreeReader readFolderContents()', err)
Expand Down Expand Up @@ -150,7 +153,9 @@ export class FiletreeReader implements IFiletreeReader {
async loadFolderMetaHandler (path: string): Promise<IFolderMetaHandler> {
try {
const lookup = await this.pathToLookup(path)
// console.log("lookup:", lookup)
const { file } = await this.jackalSigner.queries.fileTree.file(lookup)
// console.log("File: ", file)
const { contents } = file
const access = await this.extractEditAccess(file)
if (access) {
Expand Down Expand Up @@ -657,7 +662,7 @@ export class FiletreeReader implements IFiletreeReader {
if (this.yellowpages[ownerAddress][path]) {
return this.yellowpages[ownerAddress][path]
} else {
console.log(path)
// console.log(path)
const hexRootAddress = await merklePath(`s/ulid/${path}`)
const rootLookup = {
address: hexRootAddress,
Expand All @@ -671,7 +676,7 @@ export class FiletreeReader implements IFiletreeReader {
const lookup = (await this.decryptAndParseContents(
file,
)) as IRootLookupMetaData
console.log(lookup)
// console.log(lookup)
if (!(ownerAddress in this.redpages)) {
this.redpages[ownerAddress] = {}
}
Expand Down
2 changes: 0 additions & 2 deletions src/classes/metaHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ulid } from 'ulid'
import { chunkSize } from '@/utils/globalDefaults'
import {
intToHex,
maybeMakeThumbnail,
uintArrayToString,
} from '@/utils/converters'
import { bufferToHex } from '@/utils/hash'
Expand Down Expand Up @@ -286,7 +285,6 @@ export class FileMetaHandler implements IFileMetaHandler {
rdy.merkleRoot = new Uint8Array(tree.getRoot())
rdy.merkleHex = tree.getRootAsHex()
rdy.merkleMem = uintArrayToString(rdy.merkleRoot)
rdy.thumbnail = await maybeMakeThumbnail(source.file)
}

return new FileMetaHandler(rdy)
Expand Down
78 changes: 55 additions & 23 deletions src/classes/storageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
TJackalSigningClient,
} from '@jackallabs/jackal.js-protos'
import { PrivateKey } from 'eciesjs'
import { extractFileMetaData } from '@/utils/converters'
import { extractFileMetaData, maybeMakeThumbnail } from '@/utils/converters'
import { isItPastDate, shuffleArray, signerNotEnabled, tidyString, warnError } from '@/utils/misc'
import { FileMetaHandler, FolderMetaHandler, NullMetaHandler } from '@/classes/metaHandlers'
import { encryptionChunkSize, signatureSeed } from '@/utils/globalDefaults'
Expand Down Expand Up @@ -36,6 +36,7 @@ import {
IProviderIpSet,
IProviderPool,
IProviderUploadResponse,
IReadFolderContentOptions,
IRegisterPubKeyOptions,
IRnsHandler,
IShareOptions,
Expand Down Expand Up @@ -256,22 +257,43 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler {

/**
*
* @param {string} [path]
* @param {IReadFolderContentOptions} [options]
* @returns {Promise<void>}
*/
async loadDirectory (path?: string): Promise<void> {
async loadDirectory (options: IReadFolderContentOptions = {}): Promise<void> {
if (await this.checkLocked({ noConvert: true, signer: true })) {
throw new Error('Locked.')
}
const {
owner,
path,
refresh = true,
} = options
// console.log('path:', path)
try {
this.path = path || this.path
this.meta = await this.reader.loadFolderMetaHandler(this.path)
this.children = await this.reader.readFolderContents(this.path, { refresh: true })
this.children = await this.reader.readFolderContents(this.path, { owner, refresh })
} catch (err) {
throw warnError('storageHandler loadDirectory()', err)
}
}

/**
*
* @param {string} path
* @param {IReadFolderContentOptions} [options]
* @returns {Promise<IChildMetaDataMap>}
*/
async readDirectoryContents (path: string, options?: IReadFolderContentOptions): Promise<IChildMetaDataMap> {
if (await this.checkLocked({ noConvert: true, signer: true })) {
throw new Error('Locked.')
}
// console.log('path:', path)

return this.reader.readFolderContents(path, options)
}

/**
*
* @returns {Promise<void>}
Expand Down Expand Up @@ -347,7 +369,7 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler {
async getAvailableProviders (): Promise<string[]> {
try {
const data = await this.jackalClient.getQueries().storage.activeProviders()
console.log(data)
// console.log(data)
const final: string[] = []
for (let value of data.providers) {
final.push(value.address)
Expand All @@ -368,14 +390,14 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler {
try {
const ips: Record<string, string> = {}
for (let address of providers) {
console.log(address)
// console.log(address)
const details = await this.jackalClient
.getQueries()
.storage.provider({ address })
console.log(details)
// console.log(details)
ips[details.provider.address] = details.provider.ip
}
console.log(ips)
// console.log(ips)
return ips
} catch (err) {
throw warnError('storageHandler findProviderIps()', err)
Expand All @@ -390,11 +412,11 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler {
async loadProviderPool (providers?: IProviderIpSet): Promise<void> {
try {
providers = providers || (await this.loadProvidersFromChain())
console.log(providers)
// console.log(providers)
for (let ip of Object.values(providers)) {
console.log(ip)
// console.log(ip)
const root = ip.split('.').slice(-2).join('.')
console.log(root)
// console.log(root)
if (!(root in this.providers)) {
this.providers[root] = []
}
Expand All @@ -403,7 +425,7 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler {
ip,
failures: 0,
})
console.log(this.providers)
// console.log(this.providers)
}
} catch (err) {
throw warnError('storageHandler loadProviderPool()', err)
Expand Down Expand Up @@ -621,7 +643,7 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler {
}
foundMatch = false
}
console.log(bundle.folderMeta)
// console.log(bundle.folderMeta)
const folderMsgs = await this.existingFolderToMsgs({
meta: bundle.folderMeta,
})
Expand Down Expand Up @@ -919,6 +941,9 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler {
}
baseFile = new File(parts, name, meta)
}
if (baseFile.size == 0) {
throw new Error('File is empty')
}
return baseFile
}
} catch (err) {
Expand Down Expand Up @@ -1107,7 +1132,7 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler {
protected async loadProvidersFromChain (): Promise<IProviderIpSet> {
try {
const providers = await this.getAvailableProviders()
console.log(providers)
// console.log(providers)
return await this.findProviderIps(providers)
} catch (err) {
throw warnError('storageHandler loadProvidersFromChain()', err)
Expand Down Expand Up @@ -1198,7 +1223,7 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler {
fileFormData.set('sender', this.jklAddress)
fileFormData.set('start', startBlock.toString())

console.log('startBlock:', startBlock.toString())
// console.log('startBlock:', startBlock.toString())
return await fetch(url, { method: 'POST', body: fileFormData }).then(
async (resp): Promise<IProviderUploadResponse> => {
if (typeof resp === 'undefined' || resp === null) {
Expand Down Expand Up @@ -1365,13 +1390,17 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler {
merkle: ft.merkleRoot,
})
const [details] = files
const deletePkg = {
creator: this.jklAddress,
merkle: details.merkle,
start: details.start,
try {
const deletePkg = {
creator: this.jklAddress,
merkle: details.merkle,
start: details.start,
}
const wrapped = this.fileDeleteToMsgs(deletePkg)
msgs.push(...wrapped)
} catch (err) {
console.warn('looks like we\'re deleting a dead file...', err)
}
const wrapped = this.fileDeleteToMsgs(deletePkg)
msgs.push(...wrapped)
} else if (ft.metaDataType === 'folder') {
const data = await this.reader.readFolderContents(target)
for (let file of Object.values(data.files)) {
Expand Down Expand Up @@ -1472,13 +1501,16 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler {
const finalName = await hashAndHex(fileMeta.name + Date.now().toString())
const file = new File(encryptedArray, finalName, { type: 'text/plain' })

const thumbnail = await maybeMakeThumbnail(toProcess)

const ulid = this.readCurrentUlid()
console.log(ulid)
// console.log(ulid)
const baseMeta = await FileMetaHandler.create({
description: '',
file,
fileMeta,
location: this.readCurrentUlid(),
thumbnail,
})
return { file, meta: baseMeta, duration, aes }
} catch (err) {
Expand Down Expand Up @@ -1528,7 +1560,7 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler {
}
if (checkSet.needsProviders) {
const k = Object.keys(this.providers)
console.log(k)
// console.log(k)
const providerCount = k.length
if (providerCount === 0) {
throw true
Expand Down
13 changes: 9 additions & 4 deletions src/interfaces/classes/IStorageHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
IBroadcastOptions,
IBuyStorageOptions,
IChildMetaDataMap,
ICreateFolderOptions,
IDeleteTargetOptions,
IDownloadTracker,
Expand All @@ -8,11 +10,12 @@ import {
IFolderMetaData,
IInitStorageOptions,
IProviderIpSet,
IReadFolderContentOptions,
IRegisterPubKeyOptions,
IShareOptions,
IStagedUploadPackage,
IStorageStatus,
IWrappedEncodeObject
IWrappedEncodeObject,
} from '@/interfaces'
import type { TSharedRootMetaDataMap } from '@/types'

Expand All @@ -21,7 +24,9 @@ export interface IStorageHandler {

registerPubKey (options?: IRegisterPubKeyOptions): Promise<IWrappedEncodeObject[]>

loadDirectory (path?: string): Promise<void>
loadDirectory (options?: IReadFolderContentOptions): Promise<void>

readDirectoryContents (path: string, options?: IReadFolderContentOptions): Promise<IChildMetaDataMap>

loadShared (): Promise<void>

Expand Down Expand Up @@ -71,7 +76,7 @@ export interface IStorageHandler {

queuePublic (toQueue: File | File[], duration?: number): Promise<number>

processAllQueues (): Promise<any>
processAllQueues (options?: IBroadcastOptions): Promise<any>

getFileParticulars (filePath: string): Promise<IFileParticulars>

Expand All @@ -80,7 +85,7 @@ export interface IStorageHandler {
downloadExternalFile (
userAddress: string,
filePath: string,
trackers: IDownloadTracker
trackers: IDownloadTracker,
): Promise<File>

deleteTargets (options: IDeleteTargetOptions): Promise<IWrappedEncodeObject[]>
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/options/IReadFolderContentOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface IReadFolderContentOptions {
owner?: string
path?: string
refresh?: true
}
Loading

0 comments on commit f79b836

Please sign in to comment.