Skip to main content

Built and signed on GitHub Actions

Works with
This package works with Node.js, Deno, Bun
This package works with Node.js
This package works with Deno
This package works with Bun
•JSR Score94%•
License
MIT
•
Downloads1,113/wk
•
Published9 hours ago (2.4.2)

An ActivityPub/fediverse server framework

interface Context

A context.

Type Parameters

TContextData

Properties

Since 0.12.0
readonly
origin: string

The origin of the federated server, including the scheme (http:// or https://) and the host (e.g., example.com:8080).

Since 1.5.0
readonly
canonicalOrigin: string

The canonical origin of the federated server, including the scheme (http:// or https://) and the host (e.g., example.com:8080).

When the associated Federation object does not have any explicit canonical origin, it is the same as the Context.origin.

Since 0.12.0
readonly
host: string

The host of the federated server, including the hostname (e.g., example.com) and the port following a colon (e.g., :8080) if it is not the default port for the scheme.

Since 0.12.0
readonly
hostname: string

The hostname of the federated server (e.g., example.com). This is the same as the host without the port.

The user-defined data associated with the context.

The OpenTelemetry tracer provider.

Since 2.3.0
readonly
optional
meterProvider: MeterProvider

The OpenTelemetry meter provider.

The document loader for loading remote JSON-LD documents.

The context loader for loading remote JSON-LD contexts.

The FEP-ef61 portable object policy that this context applies to portable objects it dereferences. It is verifyPortableObject() with this context's document loader, context loader, and tracer provider as defaults, which the options passed to it override.

Since its name is the same as the verifyPortableObject option of property accessors, lookupObject(), and traverseCollection(), passing a context as their options applies the policy, e.g., await create.getObject(ctx). Objects parsed with a context as options, such as activities that inboxes receive, and objects that accessors fetch with a context as options also use it by default for their property accessors. It never verifies an object by itself being there: it only applies to objects that are dereferenced.

It is optional only so that custom implementations of this interface keep working; contexts that Fedify creates always have it.

The federation object that this context belongs to.

Methods

Creates a new context with the same properties as this one, but with the given data.

Builds the URI of the NodeInfo document.

Builds the URI of an actor with the given identifier.

Since 2.4.0
getPortableActorUri(
identifier: string,
authority: string
): URL

Builds the FEP-ef61 portable ID of an actor with the given identifier under the given DID authority.

The path is the same as the one Context.getActorUri builds, but the result is an ap+ef61: URI whose authority is the DID, e.g., ap+ef61://did:key:z6Mk.../users/alice. Such an actor can be served through the FEP-ef61 gateway endpoint, /.well-known/apgateway/did:key:z6Mk.../users/alice, by the same actor dispatcher.

The returned URL keeps the DID authority percent-encoded, because the URL class cannot represent the canonical form. Use formatIri() from @fedify/vocab-runtime to get the canonical string, e.g., ap+ef61://did:key:z6Mk.../users/alice.

Since 0.7.0
getObjectUri<TObject extends Object>(
cls: ConstructorWithTypeId<TObject>,
values: Record<string, string>
): URL

Builds the URI of an object with the given class and values.

Since 2.4.0
getPortableObjectUri<TObject extends Object>(
cls: ConstructorWithTypeId<TObject>,
values: Record<string, string>,
authority: string
): URL

Builds the FEP-ef61 portable ID of an object with the given class and values under the given DID authority.

The path is the same as the one Context.getObjectUri builds from the object dispatcher's path, but the result is an ap+ef61: URI whose authority is the DID, e.g., ap+ef61://did:key:z6Mk.../notes/123. Such an object can be served through the FEP-ef61 gateway endpoint, /.well-known/apgateway/did:key:z6Mk.../notes/123, by the same object dispatcher.

The returned URL keeps the DID authority percent-encoded, because the URL class cannot represent the canonical form. Use formatIri() from @fedify/vocab-runtime to get the canonical string, e.g., ap+ef61://did:key:z6Mk.../notes/123.

Since 2.4.0
getPortableInboxUri(
identifier: string,
authority: string
): URL

Builds the FEP-ef61 portable ID of an actor's inbox with the given identifier under the given DID authority.

The path is the same as the one Context.getInboxUri builds from the inbox path, but the result is an ap+ef61: URI whose authority is the DID, e.g., ap+ef61://did:key:z6Mk.../users/alice/inbox. If a portable actor has it as its inbox and lists this server in its gateways, this server accepts deliveries to the inbox through the FEP-ef61 gateway endpoint, e.g., POST /.well-known/apgateway/did:key:z6Mk.../users/alice/inbox, and dispatches them to the inbox listeners.

The returned URL keeps the DID authority percent-encoded, because the URL class cannot represent the canonical form. Use formatIri() from @fedify/vocab-runtime to get the canonical string.

Builds the URI of an actor's outbox with the given identifier.

Since 2.4.0
getPortableOutboxUri(
identifier: string,
authority: string
): URL

Builds the FEP-ef61 portable ID of an actor's outbox with the given identifier under the given DID authority.

The path is the same as the one Context.getOutboxUri builds, but the result is an ap+ef61: URI whose authority is the DID, e.g., ap+ef61://did:key:z6Mk.../users/alice/outbox. If a portable actor has it as its outbox and its ID is under the same DID, the collection is served through the FEP-ef61 gateway endpoint, e.g., /.well-known/apgateway/did:key:z6Mk.../users/alice/outbox, by the same collection dispatcher.

The returned URL keeps the DID authority percent-encoded, because the URL class cannot represent the canonical form. Use formatIri() from @fedify/vocab-runtime to get the canonical string.

Since 2.4.0
getMediaUploaderUri(identifier: string): URL

Builds the URI of an actor's media upload endpoint with the given identifier. This is the endpoint advertised under endpoints.uploadMedia in the actor document.

Builds the URI of the shared inbox.

Builds the URI of an actor's inbox with the given identifier.

Builds the URI of an actor's following collection with the given identifier.

Since 2.4.0
getPortableFollowingUri(
identifier: string,
authority: string
): URL

Builds the FEP-ef61 portable ID of an actor's following collection with the given identifier under the given DID authority.

The path is the same as the one Context.getFollowingUri builds, but the result is an ap+ef61: URI whose authority is the DID, e.g., ap+ef61://did:key:z6Mk.../users/alice/following. If a portable actor has it as its following and its ID is under the same DID, the collection is served through the FEP-ef61 gateway endpoint, e.g., /.well-known/apgateway/did:key:z6Mk.../users/alice/following, by the same collection dispatcher.

The returned URL keeps the DID authority percent-encoded, because the URL class cannot represent the canonical form. Use formatIri() from @fedify/vocab-runtime to get the canonical string.

Builds the URI of an actor's followers collection with the given identifier.

Since 2.4.0
getPortableFollowersUri(
identifier: string,
authority: string
): URL

Builds the FEP-ef61 portable ID of an actor's followers collection with the given identifier under the given DID authority.

The path is the same as the one Context.getFollowersUri builds, but the result is an ap+ef61: URI whose authority is the DID, e.g., ap+ef61://did:key:z6Mk.../users/alice/followers. If a portable actor has it as its followers and its ID is under the same DID, the collection is served through the FEP-ef61 gateway endpoint, e.g., /.well-known/apgateway/did:key:z6Mk.../users/alice/followers, by the same collection dispatcher.

The returned URL keeps the DID authority percent-encoded, because the URL class cannot represent the canonical form. Use formatIri() from @fedify/vocab-runtime to get the canonical string.

Since 0.11.0
getLikedUri(identifier: string): URL

Builds the URI of an actor's liked collection with the given identifier.

Since 2.4.0
getPortableLikedUri(
identifier: string,
authority: string
): URL

Builds the FEP-ef61 portable ID of an actor's liked collection with the given identifier under the given DID authority.

The path is the same as the one Context.getLikedUri builds, but the result is an ap+ef61: URI whose authority is the DID, e.g., ap+ef61://did:key:z6Mk.../users/alice/liked. If a portable actor has it as its liked and its ID is under the same DID, the collection is served through the FEP-ef61 gateway endpoint, e.g., /.well-known/apgateway/did:key:z6Mk.../users/alice/liked, by the same collection dispatcher.

The returned URL keeps the DID authority percent-encoded, because the URL class cannot represent the canonical form. Use formatIri() from @fedify/vocab-runtime to get the canonical string.

Since 0.11.0
getFeaturedUri(identifier: string): URL

Builds the URI of an actor's featured collection with the given identifier.

Since 2.4.0
getPortableFeaturedUri(
identifier: string,
authority: string
): URL

Builds the FEP-ef61 portable ID of an actor's featured collection with the given identifier under the given DID authority.

The path is the same as the one Context.getFeaturedUri builds, but the result is an ap+ef61: URI whose authority is the DID, e.g., ap+ef61://did:key:z6Mk.../users/alice/featured. If a portable actor has it as its featured and its ID is under the same DID, the collection is served through the FEP-ef61 gateway endpoint, e.g., /.well-known/apgateway/did:key:z6Mk.../users/alice/featured, by the same collection dispatcher.

The returned URL keeps the DID authority percent-encoded, because the URL class cannot represent the canonical form. Use formatIri() from @fedify/vocab-runtime to get the canonical string.

Since 0.11.0
getFeaturedTagsUri(identifier: string): URL

Builds the URI of an actor's featured tags collection with the given identifier.

Since 2.4.0
getPortableFeaturedTagsUri(
identifier: string,
authority: string
): URL

Builds the FEP-ef61 portable ID of an actor's featured tags collection with the given identifier under the given DID authority.

The path is the same as the one Context.getFeaturedTagsUri builds, but the result is an ap+ef61: URI whose authority is the DID, e.g., ap+ef61://did:key:z6Mk.../users/alice/tags. If a portable actor has it as its featuredTags and its ID is under the same DID, the collection is served through the FEP-ef61 gateway endpoint, e.g., /.well-known/apgateway/did:key:z6Mk.../users/alice/tags, by the same collection dispatcher.

The returned URL keeps the DID authority percent-encoded, because the URL class cannot represent the canonical form. Use formatIri() from @fedify/vocab-runtime to get the canonical string.

Since 0.9.0
parseUri(
uri: URL | null,
options?: ParseUriOptions
): ParseUriResult | null

Determines the type of the URI and extracts the associated data.

By default, only URIs on this server's origin are recognized. With the portable option, FEP-ef61 portable IDs, e.g., ap+ef61://did:key:z6Mk.../users/alice, and their compatible identifiers on any gateway, e.g., https://example.com/.well-known/apgateway/did:key:z6Mk.../users/alice, are also recognized by the same paths that the gateway endpoint routes to the dispatchers, and the result has the DID in its authority property.

The DID of a portable ID is anyone's to choose, so recognizing it does not mean that this server hosts anything for the DID. Check that authority is the DID that the application stores for the actor or object before acting on it:

const parsed = ctx.parseUri(follow.objectId, { portable: true });
if (parsed?.type !== "actor") return;
const user = await getUser(parsed.identifier);
if (user == null) return;
if (parsed.authority != null && parsed.authority !== user.did) return;

Gets the key pairs for an actor.

Since 0.4.0
getDocumentLoader(identity: { identifier: string; } | { username: string; }): Promise<DocumentLoader>

Gets an authenticated DocumentLoader for the given identity. Note that an authenticated document loader intentionally does not cache the fetched documents.

Since 0.4.0
getDocumentLoader(identity: { keyId: URL; privateKey: CryptoKey; }): DocumentLoader

Gets an authenticated DocumentLoader for the given identity. Note that an authenticated document loader intentionally does not cache the fetched documents.

Since 0.15.0
lookupObject(
identifier: string | URL,
): Promise<Object | null>

Looks up an ActivityStreams object by its URI (including acct: URIs) or a fediverse handle (e.g., @user@server or user@server).

Since 1.1.0
traverseCollection(): AsyncIterable<Object | Link>

Traverses a collection, yielding each item in the collection. If the collection is paginated, it will fetch the next page automatically.

Since 1.4.0
lookupNodeInfo(
url: URL | string,
options?: GetNodeInfoOptions & { parse?: "strict" | "best-effort"; }
): Promise<NodeInfo | undefined>

Fetches the NodeInfo document from the given URL.

Since 1.4.0
lookupNodeInfo(
url: URL | string,
options?: GetNodeInfoOptions & { parse: "none"; }
): Promise<JsonValue | undefined>

Fetches the NodeInfo document from the given URL.

Looks up a WebFinger resource.

It's almost the same as the lookupWebFinger function, but it uses the context's configuration by default.

sendActivity(
sender:
SenderKeyPair
| SenderKeyPair[]
| { identifier: string; }
| { username: string; },
recipients: Recipient | Recipient[],
activity: Activity,
): Promise<void>

Sends an activity to recipients' inboxes.

Since 0.14.0
sendActivity(
sender: { identifier: string; } | { username: string; },
recipients: "followers",
activity: Activity,
): Promise<void>

Sends an activity to the inboxes of the sender's followers.

Since 1.3.0
routeActivity(
recipient: string | null,
activity: Activity,
): Promise<boolean>

Manually routes an activity to the appropriate inbox listener.

It is useful for routing an activity that is not received from the network, or for routing an activity that is enclosed in another activity.

Note that the activity will be verified if it has Object Integrity Proofs or is equivalent to the actual remote object. If the activity is not verified, it will be rejected.

Since 2.4.0
enqueueTask<TData>(
data: TData,
): Promise<void>

Enqueues a custom background task. The payload is validated against the task's schema, serialized, and processed by the task's handler on a background worker.

Since 2.4.0
enqueueTaskMany<TData>(
payloads: readonly TData[],
): Promise<void>

Enqueues multiple payloads for a custom background task at once. Uses the queue's bulk enqueue operation when available. Without deduplication, it may fall back to parallel single enqueues when the queue does not implement bulk enqueue.

Since 1.8.0
getCollectionUri<TParam extends Record<string, string>>(
name: string | symbol,
values: TParam
): URL

Builds the URI of a collection of objects with the given name and values.

Since 2.4.0
getPortableCollectionUri<TParam extends Record<string, string>>(
name: string | symbol,
values: TParam,
authority: string
): URL

Builds the FEP-ef61 portable ID of a custom collection with the given name and values under the given DID authority.

The path is the same as the one Context.getCollectionUri builds, but the result is an ap+ef61: URI whose authority is the DID, e.g., ap+ef61://did:key:z6Mk.../users/alice/bookmarks. If the collection dispatcher maps the collection to a portable actor under the same DID with CustomCollectionCallbackSetters.mapPortableOwner(), the collection is served through the FEP-ef61 gateway endpoint, e.g., /.well-known/apgateway/did:key:z6Mk.../users/alice/bookmarks.

The returned URL keeps the DID authority percent-encoded, because the URL class cannot represent the canonical form. Use formatIri() from @fedify/vocab-runtime to get the canonical string.

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.