Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: integrate link modifier #301

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/core/src/schema.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
export * as URI from './schema/uri.js'
export * as Link from './schema/link.js'
export * as DID from './schema/did.js'
export * as Text from './schema/text.js'
export * from './schema/schema.js'
export { match as link } from './schema/link.js'
export { match as did } from './schema/did.js'
export { match as uri } from './schema/uri.js'
export { match as text } from './schema/text.js'
98 changes: 0 additions & 98 deletions packages/core/src/schema/link.js

This file was deleted.

163 changes: 151 additions & 12 deletions packages/core/src/schema/schema.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import * as Schema from './type.js'
import { ok, Failure } from '../result.js'
import {
create as createLink,
parse as parseLink,
createLegacy,
isLink,
parse,
base32,
} from '../link.js'
export * from './type.js'
import * as CBOR from '../cbor.js'
import { sha256 } from 'multiformats/hashes/sha2'

export { ok }
/**
Expand All @@ -18,6 +28,9 @@ export class API {
constructor(settings) {
/** @protected */
this.settings = settings

this.codec = CBOR
this.hasher = sha256
}

toString() {
Expand Down Expand Up @@ -141,6 +154,26 @@ export class API {
schema
)
}

/**
* @template {number} [Code=number]
* @template {number} [Alg=number]
* @template {1|0} [Version=0|1]
* @param {{
* codec?: Schema.BlockCodec<Code, unknown>
* hasher?: Schema.MultihashHasher<Alg>
* version?: Version
* }} options
* @returns {Schema.LinkSchema<T, Code, Alg, Version>}
*/
link({ codec, hasher, version } = {}) {
return link({
...(codec ? { code: codec.code } : {}),
...(hasher ? { multihash: { code: hasher.code } } : {}),
...(version ? { version } : {}),
schema: this,
})
}
}

/**
Expand Down Expand Up @@ -760,7 +793,7 @@ class LessThan extends API {
/**
* @template {number} T
* @param {number} n
* @returns {Schema.Schema<T, T>}
* @returns {Schema.Reader<T, T>}
*/
export const lessThan = n => new LessThan(n)

Expand Down Expand Up @@ -789,7 +822,7 @@ class GreaterThan extends API {
/**
* @template {number} T
* @param {number} n
* @returns {Schema.Schema<T, T>}
* @returns {Schema.Reader<T, T>}
*/
export const greaterThan = n => new GreaterThan(n)

Expand Down Expand Up @@ -1063,15 +1096,15 @@ export const literal = value => new Literal(value)
/**
* @template {{[key:string]: Schema.Reader}} U
* @template [I=unknown]
* @extends {API<Schema.InferStruct<U>, I, U>}
* @extends {API<Schema.InferStruct<U>, I, {shape: U}>}
*/
class Struct extends API {
/**
* @param {I} input
* @param {U} shape
* @param {{shape: U}} settings
* @returns {Schema.ReadResult<Schema.InferStruct<U>>}
*/
readWith(input, shape) {
readWith(input, { shape }) {
if (typeof input != 'object' || input === null || Array.isArray(input)) {
return typeError({
expect: 'object',
Expand Down Expand Up @@ -1105,17 +1138,17 @@ class Struct extends API {
* @returns {Schema.MapRepresentation<Partial<Schema.InferStruct<U>>> & Schema.StructSchema}
*/
partial() {
return new Struct(
Object.fromEntries(
Object.entries(this.shape).map(([key, value]) => [key, optional(value)])
)
const shape = Object.fromEntries(
Object.entries(this.shape).map(([key, value]) => [key, optional(value)])
)

return new Struct({ shape })
}

/** @type {U} */
get shape() {
// @ts-ignore - We declared `settings` private but we access it here
return this.settings
return this.settings.shape
}

toString() {
Expand All @@ -1141,7 +1174,7 @@ class Struct extends API {
* @returns {Schema.StructSchema<U & E, I>}
*/
extend(extension) {
return new Struct({ ...this.shape, ...extension })
return new Struct({ shape: { ...this.shape, ...extension } })
}
}

Expand Down Expand Up @@ -1176,9 +1209,115 @@ export const struct = fields => {
}
}

return new Struct(/** @type {V} */ (shape))
return new Struct({ shape: /** @type {V} */ (shape) })
}

/**
* @template {unknown} [T=unknown]
* @template {number} [Code=number]
* @template {number} [Alg=number]
* @template {1|0} [Version=0|1]
* @typedef {{
* code?:Code,
* version?:Version
* multihash?: {code?: Alg, digest?: Uint8Array}
* schema?: Schema.Schema<T, unknown>
* }} LinkSettings
*/

/**
* @template {unknown} T
* @template {number} Code
* @template {number} Alg
* @template {1|0} Version
* @extends {API<Schema.Link<T, Code, Alg, Version>, unknown, LinkSettings<T, Code, Alg, Version>>}
* @implements {Schema.LinkSchema<T, Code, Alg, Version>}
*/
class LinkSchema extends API {
/**
*
* @param {unknown} cid
* @param {LinkSettings<T, Code, Alg, Version>} settings
* @returns {Schema.ReadResult<Schema.Link<T, Code, Alg, Version>>}
*/
readWith(cid, { code, multihash = {}, version }) {
if (cid == null) {
return error(`Expected link but got ${cid} instead`)
} else {
if (!isLink(cid)) {
return error(`Expected link to be a CID instead of ${cid}`)
} else {
if (code != null && cid.code !== code) {
return error(
`Expected link to be CID with 0x${code.toString(16)} codec`
)
}

if (multihash.code != null && cid.multihash.code !== multihash.code)
return error(
`Expected link to be CID with 0x${multihash.code.toString(
16
)} hashing algorithm`
)

if (version != null && cid.version !== version) {
return error(
`Expected link to be CID version ${version} instead of ${cid.version}`
)
}

const [expectDigest, actualDigest] =
multihash.digest != null
? [
base32.baseEncode(multihash.digest),
base32.baseEncode(cid.multihash.digest),
]
: ['', '']

if (expectDigest !== actualDigest) {
return error(
`Expected link with "${expectDigest}" hash digest instead of "${actualDigest}"`
)
}

return {
ok: /** @type {Schema.Link<T, any, any, any>} */ (cid),
}
}
}
}

/**
* @returns {never}
*/
link() {
throw new Error('Can not create link of link')
}

/**
* @template {string} Prefix
* @param {string} input
* @param {Schema.MultibaseDecoder<Prefix>} [base]
*/
parse(input, base) {
const link = parseLink(input, base)
return this.from(link)
}
}

/** @type {Schema.LinkSchema<unknown, number, number, 0|1>} */
export const Link = new LinkSchema({})

/**
* @template {number} Code
* @template {number} Alg
* @template {1|0} Version
* @template {unknown} T
* @param {LinkSettings<T, Code, Alg, Version>} options
* @returns {Schema.LinkSchema<T, Code, Alg, Version>}
*/
export const link = (options = {}) => new LinkSchema(options)

/**
* @template {Schema.VariantChoices} U
* @template [I=unknown]
Expand Down
Loading