-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: alight link API with multiformats (#36)
* feat: alight link API with multiformats * fix: module regressions from merge
- Loading branch information
Showing
22 changed files
with
163 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
export * as Delegation from "./delegation.js" | ||
export { delegate, isLink } from "./delegation.js" | ||
export { delegate, isDelegation } from "./delegation.js" | ||
export { | ||
create as createLink, | ||
createV0 as createLegacyLink, | ||
isLink, | ||
asLink, | ||
parse as parseLink, | ||
decode as decodeLink, | ||
} from "./link.js" | ||
export * as UCAN from "@ipld/dag-ucan" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { CID } from "multiformats" | ||
import * as API from "@ucanto/interface" | ||
|
||
/** | ||
* @template {number} Code | ||
* @template {number} Alg | ||
* @param {Code} code | ||
* @param {API.MultihashDigest<Alg>} digest | ||
* @return {API.Link<unknown, Code, Alg, 1> & CID} | ||
*/ | ||
export const create = (code, digest) => | ||
/** @type {any} */ (CID.createV1(code, digest)) | ||
|
||
/** | ||
* @template {number} Alg | ||
* @param {API.MultihashDigest<Alg>} digest | ||
* @return {API.Link<unknown, 0x70, Alg, 0> & CID} | ||
*/ | ||
export const createV0 = digest => /** @type {any} */ (CID.createV0(digest)) | ||
|
||
/** | ||
* Type predicate returns true if value is the link. | ||
* | ||
* @template {API.Link<unknown, number, number, 0|1>} L | ||
* @param {unknown} value | ||
* @returns {value is (L & CID)} | ||
*/ | ||
export const isLink = value => | ||
value != null && /** @type {{asCID: unknown}} */ (value).asCID === value | ||
|
||
export const asLink = | ||
/** @type {<L extends API.Link<unknown, number, number, 0|1>>(value:L|unknown) => (L & CID)|null} */ | ||
(CID.asCID) | ||
|
||
export const parse = | ||
/** @type {<P extends string>(source:string, base?:API.MultibaseDecoder<P>) => API.Link<unknown, number, number, 0|1> & CID} */ | ||
(CID.parse) | ||
|
||
export const decode = | ||
/** @type {(bytes:Uint8Array) => API.Link<unknown, number, number, 0|1> & CID} */ | ||
CID.decode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { test, assert } from "./test.js" | ||
import { CID } from "multiformats" | ||
import * as Link from "../src/link.js" | ||
|
||
{ | ||
const dataset = [ | ||
[ | ||
"QmdpiaQ9q7n4E224syBJz4peZpAFLArwJgSXHZWH5F6DxB", | ||
"bafybeihgb7yguzxak7m4yu6pmxn3fdarzqbkshw6ovq7bxzbpilu4wl63a", | ||
"QmdpiaQ9q7n4E224syBJz4peZpAFLArwJgSXHZWH5F6DxB", | ||
], | ||
[ | ||
"bafybeiepa5hmd3vg2i2unyzrhnxnthwi2aksunykhmcaykbl2jx2u77cny", | ||
"bafybeiepa5hmd3vg2i2unyzrhnxnthwi2aksunykhmcaykbl2jx2u77cny", | ||
"QmXxyUQDxCpSuF8QrWHxsqmeCzMnHCukaBU3hnkgnYLYHj", | ||
], | ||
] | ||
|
||
for (const [input, expect, expect2] of dataset) { | ||
test(`Link.create - ${input}`, () => { | ||
const cid = CID.parse(input) | ||
const link = Link.create(cid.code, cid.multihash) | ||
assert.deepEqual(link, CID.parse(expect)) | ||
}) | ||
|
||
test(`Link.createV0 - ${input}`, () => { | ||
const cid = CID.parse(input) | ||
const link = Link.createV0(cid.multihash) | ||
assert.deepEqual(link.toString(), CID.parse(expect2).toString()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from "./server.js" | ||
export * from "./handler.js" | ||
export * from "./api.js" | ||
|
||
export { InvocationError } from "./server.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.