Skip to content

Commit

Permalink
Merge pull request #91 from roots-id/range_headers
Browse files Browse the repository at this point in the history
Add Range headers
  • Loading branch information
AlexAndrei98 authored Aug 22, 2023
2 parents 5cd0821 + 3bbc93e commit 57e17d1
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 48 deletions.
24 changes: 12 additions & 12 deletions examples/integration-scripts/multisig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,22 @@ async function run() {
console.log("Multisig created!")

const identifiers1 = await client1.identifiers().list()
assert.equal(identifiers1.length, 2)
assert.equal(identifiers1[0].name, "multisig")
assert.equal(identifiers1[1].name, "multisig1")
assert.equal(identifiers1.aids.length, 2)
assert.equal(identifiers1.aids[0].name, "multisig")
assert.equal(identifiers1.aids[1].name, "multisig1")

const identifiers2 = await client2.identifiers().list()
assert.equal(identifiers2.length, 2)
assert.equal(identifiers2[0].name, "multisig")
assert.equal(identifiers2[1].name, "multisig2")
assert.equal(identifiers2.aids.length, 2)
assert.equal(identifiers2.aids[0].name, "multisig")
assert.equal(identifiers2.aids[1].name, "multisig2")

const identifiers3 = await client3.identifiers().list()
assert.equal(identifiers3.length, 2)
assert.equal(identifiers3[0].name, "multisig")
assert.equal(identifiers3[1].name, "multisig3")
assert.equal(identifiers3.aids.length, 2)
assert.equal(identifiers3.aids[0].name, "multisig")
assert.equal(identifiers3.aids[1].name, "multisig3")

console.log("Client 1 identifiers:", identifiers1[0].name, identifiers1[1].name)
console.log("Client 2 identifiers:", identifiers2[0].name, identifiers2[1].name)
console.log("Client 3 identifiers:", identifiers3[0].name, identifiers3[1].name)
console.log("Client 1 identifiers:", identifiers1.aids[0].name, identifiers1.aids[1].name)
console.log("Client 2 identifiers:", identifiers2.aids[0].name, identifiers2.aids[1].name)
console.log("Client 3 identifiers:", identifiers3.aids[0].name, identifiers3.aids[1].name)

}
2 changes: 2 additions & 0 deletions examples/integration-scripts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions examples/integration-scripts/randy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ async function run() {


let aids = await client1.identifiers().list()
assert.equal(aids.length, 1)
aid = aids[0]
assert.equal(aids.aids.length, 1)
aid = aids.aids[0]
assert.equal(aid.name, 'aid1')
assert.equal(aid.prefix, icp.pre)

Expand All @@ -51,8 +51,8 @@ async function run() {
assert.deepEqual(ixn.ked['a'], [icp.pre])

aids = await client1.identifiers().list()
assert.equal(aids.length, 1)
aid = aids[0]
assert.equal(aids.aids.length, 1)
aid = aids.aids[0]

const events = client1.keyEvents()
let log = await events.get(aid["prefix"])
Expand Down
24 changes: 20 additions & 4 deletions examples/integration-scripts/salty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ async function run() {
assert.equal(icp.ked['kt'], '1')
assert.equal(icp.ked['nt'], '1')
let aids = await client1.identifiers().list()
assert.equal(aids.length, 1)
let aid = aids.pop()
assert.equal(aids.aids.length, 1)
let aid = aids.aids.pop()
assert.equal(aid.name, 'aid1')
let salt = aid.salty
assert.equal(salt.pidx, 0)
Expand All @@ -63,14 +63,30 @@ async function run() {
assert.equal(icp2.ked['kt'], '2')
assert.equal(icp2.ked['nt'], '2')
aids = await client1.identifiers().list()
assert.equal(aids.length, 2)
aid = aids[1]
assert.equal(aids.aids.length, 2)
aid = aids.aids[1]
assert.equal(aid.name, 'aid2')
salt = aid.salty
assert.equal(salt.pidx, 1)
assert.equal(salt.stem, 'signify:aid')
assert.equal(aid.prefix, icp2.pre)

await client1.identifiers().create('aid3')
aids = await client1.identifiers().list()
assert.equal(aids.aids.length, 3)
aid = aids.aids[0]
assert.equal(aid.name, 'aid1')

aids = await client1.identifiers().list(1,2)
assert.equal(aids.aids.length, 2)
aid = aids.aids[0]
assert.equal(aid.name, 'aid2')

aids = await client1.identifiers().list(2,2)
assert.equal(aids.aids.length, 1)
aid = aids.aids[0]
assert.equal(aid.name, 'aid3')

op = await client1.identifiers().rotate('aid1')
assert.equal(op['done'], true)
let ked = op['response']
Expand Down
10 changes: 5 additions & 5 deletions examples/signify-react-ts/src/test_components/Randy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function Randy() {
assert.equal(client.agent?.anchor, 'ELI7pg979AdhmvrjDeam2eAO2SR5niCgnjAJXJHtJose')
const identifiers = client.identifiers()
let aids = await identifiers.list()
assert.equal(aids.length, 0)
assert.equal(aids.aids.length, 0)

let op = await identifiers.create('aid1', {algo: Algos.randy})
assert.equal(op['done'], true)
Expand All @@ -43,8 +43,8 @@ export function Randy() {


aids = await identifiers.list()
assert.equal(aids.length, 1)
aid = aids[0]
assert.equal(aids.aids.length, 1)
aid = aids.aids[0]
assert.equal(aid.name, 'aid1')
assert.equal(aid.prefix, icp.pre)

Expand All @@ -56,8 +56,8 @@ export function Randy() {
assert.deepEqual(ixn.ked['a'], [icp.pre])

aids = await identifiers.list()
assert.equal(aids.length, 1)
aid = aids[0]
assert.equal(aids.aids.length, 1)
aid = aids.aids[0]

const events = client.keyEvents()
let log = await events.get(aid["prefix"])
Expand Down
4 changes: 2 additions & 2 deletions examples/signify-react-ts/src/test_components/Rotation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ export function Rotation() {
assert.equal(op_salt['done'], true)


let pres = await identifiers.list_identifiers()
let pres = await identifiers.list()
let aids = []
for (let pre of pres) {
let _aid = await identifiers.get_identifier(pre.name)
let _aid = await identifiers.get(pre.name)
aids.push(_aid)
}
client.rotate('1111123456789abcdefghijk', aids)
Expand Down
6 changes: 3 additions & 3 deletions examples/signify-react-ts/src/test_components/Witnesses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function Witnesses() {
const identifiers = client.identifiers()
const operations = client.operations()
let aids = await identifiers.list()
assert.equal(aids.length, 0)
assert.equal(aids.aids.length, 0)

let op = await identifiers.create('aid1', {
bran: 'canIGetAWitnessSaltGreaterThan21',
Expand Down Expand Up @@ -57,8 +57,8 @@ export function Witnesses() {
assert.equal(aid1.windexes.length, 3)

aids = await identifiers.list()
assert.equal(aids.length, 1)
const aid = aids.pop()
assert.equal(aids.aids.length, 1)
const aid = aids.aids.pop()
assert.equal(aid.prefix, icp1.pre)

setTestResult("Passed")
Expand Down
59 changes: 41 additions & 18 deletions src/keri/app/signify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Siger } from "../core/siger"
import { Prefixer } from "../core/prefixer"
import { Salter } from "../core/salter"
import { randomNonce } from "./apping"
import { parseRangeHeaders } from "../core/httping"

const DEFAULT_BOOT_URL = "http://localhost:3903"

Expand Down Expand Up @@ -176,17 +177,15 @@ export class SignifyClient {
})
if (extraHeaders !== undefined) {
extraHeaders.forEach((value, key) => {
final_headers.set(key, value)
final_headers.append(key, value)
})
}

let res = await fetch(this.url + path, {
method: method,
body: _body,
headers: final_headers
});

if (!(res.status == 200 || res.status == 202)) {
if (!(res.status == 200 || res.status == 202 || res.status == 206)) {
const error = await res.text()
throw new Error(error)
}
Expand Down Expand Up @@ -477,14 +476,29 @@ export class Identifier {
/**
* List managed identifiers
* @async
* @param {number} [start=0] Start index of list of notifications, defaults to 0
* @param {number} [end=24] End index of list of notifications, defaults to 24
* @returns {Promise<any>} A promise to the list of managed identifiers
*/
async list(): Promise<any> {
async list(start:number=0, end:number=24): Promise<any> {
let extraHeaders = new Headers()
extraHeaders.append('Range', `aids=${start}-${end}`)

let path = `/identifiers`
let data = null
let method = 'GET'
let res = await this.client.fetch(path, method, data)
return await res.json()
let res = await this.client.fetch(path, method, data, extraHeaders)

let cr = res.headers.get('content-range')
let range = parseRangeHeaders(cr,"aids")
let aids = await res.json()

return {
start: range.start,
end: range.end,
total: range.total,
aids: aids
}
}

/**
Expand Down Expand Up @@ -1704,19 +1718,28 @@ export class Notifications {
/**
* List notifications
* @async
* @param {string} last SAID of the last notification received
* @param {number} limit number of notifications to return
* @param {number} [start=0] Start index of list of notifications, defaults to 0
* @param {number} [end=24] End index of list of notifications, defaults to 24
* @returns {Promise<any>} A promise to the list of notifications
*/
async list(last?:string, limit?:number): Promise<any> {
let params = new URLSearchParams()
if (last !== undefined) {params.append('last', last)}
if (limit !== undefined) {params.append('limit', limit.toString())}

let path = `/notifications` + '?' + params.toString()
async list(start:number=0, end:number=24): Promise<any> {
let extraHeaders = new Headers()
extraHeaders.append('Range', `aids=${start}-${end}`)

let path = `/notifications`
let method = 'GET'
let res = await this.client.fetch(path, method, null)
return await res.json()
let res = await this.client.fetch(path, method, null, extraHeaders)

let cr = res.headers.get('content-range')
let range = parseRangeHeaders(cr,"notes")
let notes = await res.json()

return {
start: range.start,
end: range.end,
total: range.total,
notes: notes
}
}

/**
Expand All @@ -1738,7 +1761,7 @@ export class Notifications {
* @param {string} said SAID of the notification
* @returns {Promise<any>} A promise to the result of the deletion
*/
async delete(said:string) {
async delete(said:string): Promise<any> {
let path = `/notifications/`+ said
let method = 'DELETE'
let res = await this.client.fetch(path, method, null)
Expand Down
17 changes: 17 additions & 0 deletions src/keri/core/httping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,21 @@ export function desiginput(value: string): Array<Inputage> {

return siginputs

}
/** Parse start, end and total from HTTP Content-Range header value
* @param {string|null} header - HTTP Range header value
* @param {string} typ - type of range, e.g. "aids"
* @returns {start: number, end: number, total: number} - object with start, end and total properties
*/
export function parseRangeHeaders(header: string|null, typ: string): {start: number, end: number, total: number} {
if (header !== null) {
let data = header.replace(`${typ} `, "")
let values = data.split("/")
let rng = values[0].split("-")

return {start: parseInt(rng[0]), end: parseInt(rng[1]), total: parseInt(values[1])}
} else {
return {start: 0, end: 0, total: 0}
}

}

0 comments on commit 57e17d1

Please sign in to comment.