Skip to content

Commit

Permalink
Merge pull request #35 from okTurtles/update-eventsAfter
Browse files Browse the repository at this point in the history
Update eventsAfter to reflect the most recent API changes
  • Loading branch information
taoeffect authored Mar 25, 2024
2 parents d227365 + e51fd0a commit 406fec0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
36 changes: 18 additions & 18 deletions src/eventsAfter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,68 +14,68 @@ export async function eventsAfter (args: string[]): Promise<void> {

const limit = Number(parsedArgs.limit ?? defaultLimit)
if (!isArrayLength(limit)) exit('argument --limit must be a valid array length')
const [urlOrLocalPath, contractID, hash] = parsedArgs._.map(String)
const [urlOrLocalPath, contractID] = parsedArgs._.map(String)
const height = Number(parsedArgs._[2])
const src = urlOrLocalPath

try {
let messages

if (isURL(src)) {
messages = await getRemoteMessagesSince(src, contractID, hash, limit)
messages = await getRemoteMessagesSince(src, contractID, height, limit)
} else {
messages = await getMessagesSince(src, contractID, hash, limit)
messages = await getMessagesSince(src, contractID, height, limit)
}
console.log(JSON.stringify(messages.map(s => JSON.parse(s)), null, 2))
console.log(JSON.stringify(messages, null, 2))
} catch (error) {
exit(error.message)
}
}

async function getMessage (hash: string): Promise<string> {
async function getMessage (hash: string): Promise<ReturnType<typeof JSON.parse>> {
const value = await readString(hash)
if (!value) throw new Error(`no entry for ${hash}!`)
return JSON.parse(value).message
return JSON.parse(value)
}

async function getMessagesSince (src: string, contractID: string, since: string, limit: number): Promise<string[]> {
async function getMessagesSince (src: string, contractID: string, sinceHeight: number, limit: number): Promise<string[]> {
backend = await getBackend(src)

const contractHEAD: string | void = await readString(`${headPrefix}${contractID}`)
if (contractHEAD === undefined) {
throw new Deno.errors.NotFound(`contract ${contractID} doesn't exist!`)
}
const entries = []
let currentHEAD = contractHEAD
let currentHEAD = JSON.parse(contractHEAD).HEAD
let currentHeight: number
while (true) {
const entry = await getMessage(currentHEAD)
if (!entry) {
throw new Deno.errors.NotFound(`entry ${currentHEAD} no longer exists.`)
}
const head = JSON.parse(entry.head)
currentHeight = head.height
entries.push(entry)
if (currentHEAD === since) {
if (currentHeight === sinceHeight) {
break
} else {
currentHEAD = JSON.parse(entry).previousHEAD
if (currentHEAD === null) {
throw new Deno.errors.NotFound(`entry ${since} was not found in contract ${contractID}.`)
}
}
currentHEAD = head.previousHEAD
}
return entries.reverse().slice(0, limit)
}

async function getRemoteMessagesSince (src: string, contractID: string, since: string, limit: number): Promise<string[]> {
const response = await fetch(`${src}/eventsAfter/${contractID}/${since}`)
async function getRemoteMessagesSince (src: string, contractID: string, sinceHeight: number, limit: number): Promise<string[]> {
const response = await fetch(`${src}/eventsAfter/${contractID}/${sinceHeight}`)
if (!response.ok) {
// The response body may contain some useful error info if we got a Boom error response.
const bodyText = await response.text().catch(_ => '') || ``
throw new Error(`failed network request to ${src}: ${response.status} - ${response.statusText} - '${bodyText}'`)
}
const b64messages: string[] = (await response.json()).reverse()
const b64messages: string[] = await response.json()
if (b64messages.length > limit) {
b64messages.length = limit
}
return b64messages.map(b64str => JSON.parse(new TextDecoder().decode(base64.decode(b64str))).message)
return b64messages.map(b64str => JSON.parse(new TextDecoder().decode(base64.decode(b64str))))
}

async function readString (key: string): Promise<string|void> {
Expand Down
2 changes: 1 addition & 1 deletion src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function help (args?: string[]) {
chel deploy <url-or-dir-or-sqlitedb> <contract-manifest.json> [<manifest2.json> [<manifest3.json> ...]]
chel upload <url-or-dir-or-sqlitedb> <file1> [<file2> [<file3> ...]]
chel latestState <url> <contractID>
chel eventsAfter [--limit N] <url-or-dir-or-sqlitedb> <contractID> <hash>
chel eventsAfter [--limit N] <url-or-dir-or-sqlitedb> <contractID> <height>
chel eventsBefore [--limit N] <url> <contractID> <hash>
chel get <url-or-dir-or-sqlitedb> <hash>
chel hash <file>
Expand Down

0 comments on commit 406fec0

Please sign in to comment.