Skip to content
Open
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
3 changes: 1 addition & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
if: ${{needs.release.outputs.releases_created}}
runs-on: ubuntu-latest
permissions:
id-token: write # Required for OIDC
id-token: write # Required for OIDC
steps:
# The logic below handles the npm publication:
- name: Checkout
Expand All @@ -56,4 +56,3 @@ jobs:

- name: Publish
run: pnpm -r publish --access=public

6 changes: 3 additions & 3 deletions packages/client/test/services/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const ok =
/** @type {<T, Args extends []|[T]>(...args:Args) => Args extends [T] ? {ok:true, value:T extends undefined ? null : T} : {ok:true, value:null}}} */ (
(value) => (value == undefined ? Ok : { ok: true, value })
value => (value == undefined ? Ok : { ok: true, value })
)

const Ok = { ok: true, value: null }
Expand All @@ -9,13 +9,13 @@ const Ok = { ok: true, value: null }
* @param {T} value
* @returns {T}
*/
export const the = (value) => value
export const the = value => value

/**
* @param {string} reason
* @returns {never}
*/
export const panic = (reason) => {
export const panic = reason => {
throw new Error(reason)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/schema/did.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class DIDBytesSchema extends Schema.API {
return Schema.error(`Expected a ${prefix} but got "${did}" instead`)
} else {
return { ok: /** @type {API.DID<Method>} */ (did) }
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/cbor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ test('encode / decode', async () => {
const o = {}
const data = {
a: o,
b: o
b: o,
}

assert.doesNotThrow(() => transcode(data))
Expand Down
12 changes: 6 additions & 6 deletions packages/core/test/delegation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,8 @@ test('delegation.attach block in capabiliy', async () => {
can: 'store/add',
with: alice.did(),
nb: {
inlineBlock: block.cid.link()
}
inlineBlock: block.cid.link(),
},
},
],
})
Expand Down Expand Up @@ -467,8 +467,8 @@ test('delegation.attach block in facts', async () => {
facts: [
{ [`${block.cid.link()}`]: block.cid.link() },
// @ts-expect-error Link has fact entry
block.cid.link()
]
block.cid.link(),
],
})

ucan.attach(block)
Expand All @@ -488,11 +488,11 @@ test('delegation.attach fails to attach block with not attached link', async ()
capabilities: [
{
can: 'store/add',
with: alice.did()
with: alice.did(),
},
],
})

const block = await getBlock({ test: 'inlineBlock' })
assert.throws(() => ucan.attach(block))
})
})
41 changes: 32 additions & 9 deletions packages/core/test/extra-schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,11 @@ test('URI.from', () => {
Uint8Array.from([1, 2, 3]),
/Unable to parse bytes as did:/,
],
[{ method: 'echo' }, DIDTools.parse('did:echo:hello'), { ok: 'did:echo:hello' }],
[
{ method: 'echo' },
DIDTools.parse('did:echo:hello'),
{ ok: 'did:echo:hello' },
],
[
{ method: 'foo' },
DIDTools.parse('did:echo:hello'),
Expand Down Expand Up @@ -500,11 +504,16 @@ test('URI.from', () => {
[undefined, /Expected value of type Uint8Array instead got undefined/],
[null, /Expected value of type Uint8Array instead got null/],
[Uint8Array.from([1, 2, 3]), /Unable to decode bytes as DID:/],
[DIDTools.parse('did:echo:1'), { ok: new Uint8Array([157, 26, 101, 99, 104, 111, 58, 49]) }],
[
DIDTools.parse('did:echo:1'),
{ ok: new Uint8Array([157, 26, 101, 99, 104, 111, 58, 49]) },
],
]

for (const [input, out] of dataset) {
test(`Principal.read(${input == null ? input : `Uint8Array([${input}])`})`, () => {
test(`Principal.read(${
input == null ? input : `Uint8Array([${input}])`
})`, () => {
matchResult(Principal.read(input), out)
})
}
Expand All @@ -531,7 +540,11 @@ test('URI.from', () => {
[
{ method: 'echo' },
DIDTools.parse('did:echo:hello'),
{ ok: new Uint8Array([157, 26, 101, 99, 104, 111, 58, 104, 101, 108, 108, 111]) }
{
ok: new Uint8Array([
157, 26, 101, 99, 104, 111, 58, 104, 101, 108, 108, 111,
]),
},
],
[
{ method: 'foo' },
Expand All @@ -541,7 +554,9 @@ test('URI.from', () => {
]

for (const [options, input, out] of dataset) {
test(`Principal.match({ method: ${options.method == null ? options.method : `"${options.method}"`} }).read(${input == null ? input : `Uint8Array([${input}])`})`, () => {
test(`Principal.match({ method: ${
options.method == null ? options.method : `"${options.method}"`
} }).read(${input == null ? input : `Uint8Array([${input}])`})`, () => {
matchResult(Principal.match(options).read(input), out)
})
}
Expand All @@ -555,7 +570,7 @@ test('URI.from', () => {
[
{},
DIDTools.parse('did:echo:bar'),
{ ok: new Uint8Array([157, 26, 101, 99, 104, 111, 58, 98, 97, 114]) }
{ ok: new Uint8Array([157, 26, 101, 99, 104, 111, 58, 98, 97, 114]) },
],
[{ method: 'echo' }, undefined, { ok: undefined }],
[
Expand All @@ -576,8 +591,14 @@ test('URI.from', () => {
]

for (const [options, input, out] of dataset) {
test(`Principal.match({ method: ${options.method == null ? options.method : `"${options.method}"`} }).optional().read(${input == null ? input : `Uint8Array([${input}])`})`, () => {
const schema = options.method ? Principal.match(options) : Principal.principal()
test(`Principal.match({ method: ${
options.method == null ? options.method : `"${options.method}"`
} }).optional().read(${
input == null ? input : `Uint8Array([${input}])`
})`, () => {
const schema = options.method
? Principal.match(options)
: Principal.principal()
matchResult(schema.optional().read(input), out)
})
}
Expand All @@ -596,7 +617,9 @@ test('URI.from', () => {
],
]
for (const [did, errorExpectation] of dataset) {
test(`Principal.from(${did == null ? did : `Uint8Array([${did}])`})`, () => {
test(`Principal.from(${
did == null ? did : `Uint8Array([${did}])`
})`, () => {
let error
try {
Principal.from(did)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export async function getBlock(value) {
return await Block.encode({
value,
codec,
hasher
hasher,
})
}
8 changes: 5 additions & 3 deletions packages/interface/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
Revoked,
InferCapability,
Authorization,
Reader
Reader,
} from './capability.js'
import type * as Transport from './transport.js'
import type { Tuple, Block } from './transport.js'
Expand Down Expand Up @@ -978,7 +978,9 @@ export interface HTTPError {
/**
* Options for UCAN validation.
*/
export interface ValidatorOptions extends PrincipalResolver, Partial<AuthorityProver> {
export interface ValidatorOptions
extends PrincipalResolver,
Partial<AuthorityProver> {
/**
* Schema allowing invocations to be accepted for audiences other than the
* service itself.
Expand Down Expand Up @@ -1092,7 +1094,7 @@ export interface PrincipalParser {
*/
export interface PrincipalResolver {
resolveDIDKey?: (
did: UCAN.DID,
did: UCAN.DID
) => Await<Result<DIDKey[], DIDKeyResolutionError>>
}

Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export const provideAdvanced =
// If audience schema is not provided we expect the audience to match
// the server id. Users could pass `schema.string()` if they want to accept
// any audience.
const audienceSchema = audience || options.audience || Schema.literal(options.id.did())
const audienceSchema =
audience || options.audience || Schema.literal(options.id.did())
const result = audienceSchema.read(invocation.audience.did())
if (result.error) {
return { error: new InvalidAudience({ cause: result.error }) }
Expand Down
11 changes: 7 additions & 4 deletions packages/server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,18 @@ export const handle = async (server, request) => {
}
} else {
const { encoder, decoder } = selection.ok
let message;
let message
try {
message = await decoder.decode(request)
} catch (err) {
const errorMessage = err instanceof Error ? err.message : 'Unable to decode request'
const errorMessage =
err instanceof Error ? err.message : 'Unable to decode request'
return {
status: 400,
headers: { 'Content-Type': 'text/plain' },
body: new TextEncoder().encode(`Bad request: Malformed payload - ${errorMessage}`),
body: new TextEncoder().encode(
`Bad request: Malformed payload - ${errorMessage}`
),
}
}
const result = await execute(message, server)
Expand Down Expand Up @@ -198,4 +201,4 @@ export const resolve = (service, path) => {
}
}
return target
}
}
30 changes: 17 additions & 13 deletions packages/server/test/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,10 @@ test('did:web principal resolve', async () => {
},
codec: CAR.inbound,
id: w3,
resolveDIDKey: did => did === account.did()
? Server.ok([bob.did()])
: Server.error(new DIDResolutionError(did)),
resolveDIDKey: did =>
did === account.did()
? Server.ok([bob.did()])
: Server.error(new DIDResolutionError(did)),
validateAuthorization: () => ({ ok: {} }),
})

Expand Down Expand Up @@ -402,7 +403,7 @@ test('alternative audience', async () => {
id: service,
audience: Schema.or(
Schema.literal('did:web:web3.storage'),
Schema.literal(alias.did()),
Schema.literal(alias.did())
),
validateAuthorization: () => ({ ok: {} }),
})
Expand Down Expand Up @@ -604,9 +605,9 @@ test('should return 400 Bad Request for malformed payloads', async () => {
const malformedRequest = {
headers: {
'content-type': CAR.contentType,
'accept': CAR.contentType
accept: CAR.contentType,
},
body: new Uint8Array([1, 2, 3])
body: new Uint8Array([1, 2, 3]),
}

const response = await server.request(malformedRequest)
Expand All @@ -628,26 +629,29 @@ test('should return 400 Bad Request for non-Error decoder failures', async () =>
decoder: {
decode: async () => {
throw 'Not an Error instance'
}
}
}
})
},
},
},
}),
},
validateAuthorization: () => ({ ok: {} }),
})

const malformedRequest = {
headers: {
'content-type': CAR.contentType,
'accept': CAR.contentType
accept: CAR.contentType,
},
body: new Uint8Array([1, 2, 3])
body: new Uint8Array([1, 2, 3]),
}

const response = await server.request(malformedRequest)

assert.equal(response.status, 400)
assert.deepEqual(response.headers, { 'Content-Type': 'text/plain' })
const errorMessage = new TextDecoder().decode(response.body)
assert.match(errorMessage, /Bad request: Malformed payload - Unable to decode request/)
assert.match(
errorMessage,
/Bad request: Malformed payload - Unable to decode request/
)
})
5 changes: 4 additions & 1 deletion packages/transport/src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ class Channel {

const buffer = response.ok
? await response.arrayBuffer()
: HTTPError.throw(`HTTP Request failed. ${this.method} ${this.url.href} → ${response.status}`, response)
: HTTPError.throw(
`HTTP Request failed. ${this.method} ${this.url.href} → ${response.status}`,
response
)

return {
headers: response.headers.entries
Expand Down
7 changes: 5 additions & 2 deletions packages/transport/test/https.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ test('headers from http channel are passed to fetch along with the request heade
headers: { 'x-client': 'abc' },
})

const requestHeaders = { 'x-test': 'test-value', 'content-type': 'text/plain' }

const requestHeaders = {
'x-test': 'test-value',
'content-type': 'text/plain',
}

await channel.request({
headers: requestHeaders,
body: UTF8.encode('ping'),
Expand Down
2 changes: 1 addition & 1 deletion packages/transport/test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @param {AsyncIterable<T>|Iterable<T>} iterable
* @returns {Promise<T[]>}
*/
export const collect = async (iterable) => {
export const collect = async iterable => {
const result = []
for await (const item of iterable) {
result.push(item)
Expand Down
2 changes: 1 addition & 1 deletion packages/validator/src/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export class Unauthorized extends Failure {
failedProofs,
}) {
super()
this.name = /** @type {const} */ ('Unauthorized')
this.name = /** @type {const} */ ('Unauthorized')
this.capability = capability
this.delegationErrors = delegationErrors
this.unknownCapabilities = unknownCapabilities
Expand Down
Loading
Loading