Skip to content

Commit

Permalink
Merge pull request #117 from saul-jb/feat/abortable-requests
Browse files Browse the repository at this point in the history
feat: Abortable Requests
  • Loading branch information
tabcat authored Apr 3, 2024
2 parents 9214d76 + d8f489d commit 009ed70
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 20 deletions.
7 changes: 4 additions & 3 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-tsdoc": "^0.2.17",
"interface-datastore": "^8.2.10",
"interface-store": "^5.1.8",
"ipns": "^8.0.0",
"level": "^8.0.0",
"libp2p": "^1.1.1",
Expand Down
7 changes: 4 additions & 3 deletions src/entry/basal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
AsEntry
} from '../interface.js'
import type { IdentityInstance } from '@/identity/interface.js'
import type { AbortOptions } from 'interface-store'
import type { CID } from 'multiformats/cid'
import type { BlockView } from 'multiformats/interface'
import { encodeCbor, decodeCbor } from '@/utils/block.js'
Expand Down Expand Up @@ -91,11 +92,11 @@ const asEntry = async (entry: AsEntry<unknown>): Promise<Entry | null> => {
return new Entry({ block: asSigned, data, identity })
}

const fetch = async ({ blockstore, identity, cid }: Fetch): Promise<Entry> => {
const bytes = await blockstore.get(cid)
const fetch = async ({ blockstore, identity, cid }: Fetch, options?: AbortOptions): Promise<Entry> => {
const bytes = await blockstore.get(cid, options)
const block = await decodeCbor<SignedEntry>(bytes)
const { auth } = block.value
const identityInstance = await identity.fetch({ blockstore, auth })
const identityInstance = await identity.fetch({ blockstore, auth }, options)

const entry = await asEntry({ block, identity: identityInstance })

Expand Down
4 changes: 2 additions & 2 deletions src/entry/interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { IdentityInstance, IdentityComponent } from '@/identity/interface.js'
import type { ComponentProtocol } from '@/interface.js'
import type { Blockstore } from 'interface-blockstore'
import type { AbortOptions } from 'interface-store'
import type { CID } from 'multiformats/cid'
import type { BlockView } from 'multiformats/interface'
import { HLDB_PREFIX } from '@/utils/constants.js'
Expand All @@ -26,14 +27,13 @@ export interface Fetch {
blockstore: Blockstore
identity: IdentityComponent<any>
cid: CID
timeout?: number
}

export type AsEntry<Value> = Pick<EntryInstance<Value>, 'block' | 'identity'>

export interface EntryComponent<T extends EntryInstance<unknown> = EntryInstance<unknown>, P extends string = string> extends ComponentProtocol<P> {
create(create: Create): Promise<T>
fetch(fetch: Fetch): Promise<T>
fetch(fetch: Fetch, options?: AbortOptions): Promise<T>
asEntry(entry: AsEntry<unknown>): Promise<T | null>
verify(entry: AsEntry<unknown>): Promise<boolean>
}
Expand Down
5 changes: 3 additions & 2 deletions src/identity/basal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
Import
} from '../interface.js'
import type { PrivateKey, PublicKey } from '@libp2p/interface/keys'
import type { AbortOptions } from 'interface-store'
import type { CID } from 'multiformats/cid'
import type { BlockView } from 'multiformats/interface'
import { decodeCbor, encodeCbor } from '@/utils/block.js'
Expand Down Expand Up @@ -132,8 +133,8 @@ const get = async ({ name, identities, keychain }: Get): Promise<Identity> => {
}
}

const fetch = async ({ blockstore, auth: cid }: Fetch): Promise<Identity> => {
const bytes = await blockstore.get(cid)
const fetch = async ({ blockstore, auth: cid }: Fetch, options?: AbortOptions): Promise<Identity> => {
const bytes = await blockstore.get(cid, options)
const block = await decodeCbor<IdentityValue>(bytes)

const identity = asIdentity({ block })
Expand Down
3 changes: 2 additions & 1 deletion src/identity/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ComponentProtocol } from '@/interface.js'
import type { Keychain } from '@libp2p/keychain'
import type { Blockstore } from 'interface-blockstore'
import type { Datastore } from 'interface-datastore'
import type { AbortOptions } from 'interface-store'
import type { CID } from 'multiformats/cid'
import type { BlockView } from 'multiformats/interface'
import { HLDB_PREFIX } from '@/utils/constants.js'
Expand Down Expand Up @@ -44,7 +45,7 @@ export interface IdentityInstance<Value> {
export interface IdentityComponent<T extends IdentityInstance<unknown> = IdentityInstance<unknown>, P extends string = string> extends ComponentProtocol<P> {
gen(gen: Gen): Promise<T>
get(get: Get): Promise<T>
fetch(fetch: Fetch): Promise<T>
fetch(fetch: Fetch, options?: AbortOptions): Promise<T>
asIdentity(asIdentity: AsIdentity<unknown>): T | null
import(imp: Import): Promise<T>
export(exp: Export): Promise<Uint8Array>
Expand Down
6 changes: 4 additions & 2 deletions src/manifest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
ManifestData,
Protocol
} from './interface.js'
import type { AbortOptions } from 'interface-store'
import type { BlockView } from 'multiformats/interface'
import { decodeCbor, encodeCbor } from '@/utils/block.js'
// import type { FetchOptions } from '@/utils/types.js'
Expand Down Expand Up @@ -77,9 +78,10 @@ export class Manifest {
* @returns
*/
static async fetch (
{ blockstore, address }: Fetch
{ blockstore, address }: Fetch,
options?: AbortOptions
): Promise<Manifest> {
const bytes = await blockstore.get(address.cid)
const bytes = await blockstore.get(address.cid, options)
const block = await decodeCbor<ManifestData>(bytes)
const manifest = this.asManifest({ block })

Expand Down
15 changes: 8 additions & 7 deletions src/welo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type { ReplicatorModule } from '@/replicator/interface.js'
import type { Keychain } from '@libp2p/keychain'
import type { Blockstore } from 'interface-blockstore'
import type { Datastore } from 'interface-datastore'
import type { AbortOptions } from 'interface-store'
import { Manifest, Address } from '@/manifest/index.js'
import { DATABASE_NAMESPACE, IDENTITY_NAMESPACE } from '@/utils/constants.js'
import { cidstring } from '@/utils/index.js'
Expand Down Expand Up @@ -108,17 +109,17 @@ export class Welo extends Playable {
*
* Options are shallow merged with {@link defaultManifest}.
*
* @param options - Override defaults used to create the manifest.
* @param settings - Override defaults used to create the manifest.
* @returns
*/
async determine (options: Determine): Promise<Manifest> {
async determine (settings: Determine, options?: AbortOptions): Promise<Manifest> {
const manifestObj: ManifestData = {
...this.getDefaultManifest(options.name),
...options
...this.getDefaultManifest(settings.name),
...settings
}

const manifest = await Manifest.create(manifestObj)
await this.blockstore.put(manifest.block.cid, manifest.block.bytes)
await this.blockstore.put(manifest.block.cid, manifest.block.bytes, options)

try {
this.getComponents(manifest)
Expand All @@ -138,8 +139,8 @@ export class Welo extends Playable {
* @param address - the Address of the Manifest to fetch
* @returns
*/
async fetch (address: Address): Promise<Manifest> {
return Manifest.fetch({ blockstore: this.blockstore, address })
async fetch (address: Address, options?: AbortOptions): Promise<Manifest> {
return Manifest.fetch({ blockstore: this.blockstore, address }, options)
}

/**
Expand Down

0 comments on commit 009ed70

Please sign in to comment.