Skip to content

Commit

Permalink
type definitions, fixed small bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzha0 committed Jan 23, 2022
1 parent 1133ee8 commit ed50d68
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 11 deletions.
62 changes: 62 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
declare module "ar-wrapper" {
type Arweave = import("arweave")
type LRUMap = import("lru_map").LRUMap<string, Document>

interface BlockStatusI {
status: number
confirmed: {
block_height: number
block_indep_hash: string
number_of_confirmations: number
}
}

export interface BlockDocument {
name: string
content: string
version: number
tags: Record<string, string>
}

export interface OptionsI {
host: string
port: number
protocol: string
timeout: number
logging: boolean
}

export const DEFAULT_OPTIONS: OptionsI

export class Document {
txID: string
client: ArweaveClient
posted: boolean
timestamp: string

name: string
content: any
version: number
tags: Record<string, string>

constructor(parentClient: ArweaveClient, name: string, content: any, tags: Record<string, string>, version?: number)
data(): BlockDocument

update(content: any): Promise<Document>
bumpTimestamp(dateMs: number)
}

export class ArweaveClient {
adminAddr: string
client: Arweave
cache: LRUMap

constructor(adminAddress: string, keyFile: string, cacheSize?: number, options?: OptionsI)
isCached(documentName: string, desiredVersion?: number): boolean
addDocument(name: string, content: any, tags: Record<string, string>): Promise<Document>
updateDocument(document: Document): Promise<Document>
pollForConfirmation(txId: string, maxRetries?: number): Promise<BlockStatusI>
getDocumentByName(name: string, version?: number, maxRetries?: number, verifiedOnly?: boolean): Promise<Document>
getDocumentByName(txId: string, maxRetries?: number, verifiedOnly?: boolean): Promise<Document>
}
}
23 changes: 12 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ class ArweaveClient {
// Construct a new client given the address of the admin account,
// keys to the wallet, and a set of options for connecting to an Arweave network.
// Options are identical to the ones supported by the official `arweave-js` library
constructor(adminAddress, key, cache_size = 500, options = DEFAULT_OPTIONS) {
this.#key = key
constructor(adminAddress, keyFile, cacheSize = 500, options = DEFAULT_OPTIONS) {
this.#key = keyFile
this.adminAddr = adminAddress
this.client = ArweaveLib.init(options)
this.cache = new LRUMap(500)
this.cache = new LRUMap(cacheSize)
}

// Internal function for adding single document to permaweb
Expand Down Expand Up @@ -124,13 +124,6 @@ class ArweaveClient {
return doc
}

// Add a new document
async addDocument(name, content, tags) {
// create document + transaction
const doc = new Document(this, name, content, tags)
return this.#insert(doc)
}

// Internal function to see if given document is cached.
// Optionally define desired version to match against.
isCached(documentName, desiredVersion) {
Expand All @@ -144,6 +137,13 @@ class ArweaveClient {
return cached.synced && versionMatch
}

// Add a new document
async addDocument(name, content, tags) {
// create document + transaction
const doc = new Document(this, name, content, tags)
return this.#insert(doc)
}

// Update existing document object and send to chain
async updateDocument(document) {
// check if cache has latest version of document
Expand Down Expand Up @@ -192,6 +192,7 @@ class ArweaveClient {
})
}

// TODO: handle pagination/cursor here
return {
query: `
query {
Expand Down Expand Up @@ -249,7 +250,7 @@ class ArweaveClient {
}

// fetch document, update cache
const doc = await this.getDocumentByTxId(txId)
const doc = await this.getDocumentByTxId(txId, maxRetries)
this.cache.set(doc.name, doc)
return doc
}
Expand Down

0 comments on commit ed50d68

Please sign in to comment.