@polywrap/core-js
A TypeScript / JavaScript implementation of the WRAP standard, including all fundamental types & algorithms.
Reference
Types
CoreClient
export interface CoreClientConfig {
readonly interfaces?: ReadonlyUriMap<readonly Uri[]>;
readonly envs?: ReadonlyUriMap<WrapperEnv>;
readonly resolver: Readonly<IUriResolver<unknown>>;
}
export interface GetFileOptions {
path: string;
encoding?: "utf-8" | string;
}
export interface GetImplementationsOptions {
applyResolution?: boolean;
resolutionContext?: IUriResolutionContext;
}
export interface ValidateOptions {
abi?: boolean;
recursive?: boolean;
}
export interface CoreClient extends Invoker, UriResolverHandler<unknown> {
getConfig(): CoreClientConfig;
getInterfaces(): ReadonlyUriMap<readonly Uri[]> | undefined;
getEnvs(): ReadonlyUriMap<WrapperEnv> | undefined;
getEnvByUri(uri: Uri): WrapperEnv | undefined;
getResolver(): IUriResolver<unknown>;
getManifest(uri: Uri): Promise<Result<WrapManifest, WrapError>>;
getFile(
uri: Uri,
options: GetFileOptions
): Promise<Result<string | Uint8Array, WrapError>>;
getImplementations(
uri: Uri,
options: GetImplementationsOptions
): Promise<Result<Uri[], WrapError>>;
}
Env
export interface WrapperEnv {
readonly [k: string]: unknown;
}
Invoke
export interface InvokeOptions {
uri: Uri;
method: string;
args?: Record<string, unknown> | Uint8Array;
env?: Record<string, unknown>;
resolutionContext?: IUriResolutionContext;
}
export type InvokeResult<TData = unknown> = Result<TData, WrapError>;
export interface InvokerOptions extends InvokeOptions {
encodeResult?: boolean;
}
export interface Invoker {
invokeWrapper<TData = unknown>(
options: InvokerOptions & { wrapper: Wrapper }
): Promise<InvokeResult<TData>>;
invoke<TData = unknown>(
options: InvokerOptions
): Promise<InvokeResult<TData>>;
}
export type InvocableResult<TData = unknown> = InvokeResult<TData> & {
encoded?: boolean;
};
export interface Invocable {
invoke(
options: InvokeOptions,
invoker: Invoker
): Promise<InvocableResult<unknown>>;
}
IUriPackage
export interface IUriPackage {
uri: Uri;
package: IWrapPackage;
}
IUriRedirect
export interface IUriRedirect {
from: Uri;
to: Uri;
}
IUriWrapper
export interface IUriWrapper {
uri: Uri;
wrapper: Wrapper;
}
IWrapPackage
export interface GetManifestOptions {
noValidate?: boolean;
}
export interface IWrapPackage {
getManifest(
options?: GetManifestOptions
): Promise<Result<WrapManifest, Error>>;
createWrapper(
options?: DeserializeManifestOptions
): Promise<Result<Wrapper, Error>>;
}
MaybeAsync
export type MaybeAsync<T> = Promise<T> | T;
Uri
UriConfig
export interface UriConfig {
authority: string;
path: string;
uri: string;
}
Uri
export class Uri {
constructor
constructor(uri: string)
authority
public get authority(): string
path
public get path(): string
uri
public get uri(): string
equals
public static equals(a: Uri, b: Uri): boolean
isUri
public static isUri(value: unknown): value is Uri
isValidUri
public static isValidUri(uri: string, parsed?: UriConfig): boolean
toString
public toString(): string
toJSON
public toJSON(): string
parseUri
public static parseUri(input: string): Result<UriConfig, Error>
from
public static from(uri: Uri | string): Uri
UriResolver
export interface TryResolveUriOptions {
uri: Uri;
resolutionContext?: IUriResolutionContext;
}
export interface UriResolverHandler<TError = undefined> {
tryResolveUri(
options?: TryResolveUriOptions
): Promise<Result<UriPackageOrWrapper, TError>>;
}
Wrapper
export interface Wrapper extends Invocable {
invoke(
options: InvokeOptions,
invoker: Invoker
): Promise<InvocableResult<unknown>>;
getFile(options: GetFileOptions): Promise<Result<Uint8Array | string, Error>>;
getManifest(): WrapManifest;
}
UriResolverInterface
MaybeUriOrManifest
export interface MaybeUriOrManifest {
uri?: string | null;
manifest?: Uint8Array | null;
}
Module
tryResolveUri
tryResolveUri: async (
invoker: Invoker,
wrapper: Uri,
uri: Uri
): Promise<Result<MaybeUriOrManifest, WrapError>>
getFile
getFile: async (
invoker: Invoker,
wrapper: Uri,
path: string
): Promise<Result<Uint8Array | null, WrapError>>
Uri Resolution
IUriResolutionContext
export interface IUriResolutionContext {
isResolving(uri: Uri): boolean;
startResolving(uri: Uri): void;
stopResolving(uri: Uri): void;
trackStep<TError>(step: IUriResolutionStep<TError>): void;
getHistory(): IUriResolutionStep<unknown>[];
getResolutionPath(): Uri[];
createSubHistoryContext(): IUriResolutionContext;
createSubContext(): IUriResolutionContext;
}
IUriResolutionStep
export interface IUriResolutionStep<TError = undefined> {
sourceUri: Uri;
result: Result<UriPackageOrWrapper, TError>;
description?: string;
subHistory?: IUriResolutionStep<TError>[];
}
IUriResolver
export interface IUriResolver<TError = undefined> {
tryResolveUri(
uri: Uri,
client: CoreClient,
resolutionContext: IUriResolutionContext
): Promise<Result<UriPackageOrWrapper, TError>>;
}
UriPackageOrWrapper
export type UriValue = {
type: "uri";
uri: Uri;
};
export type UriPackageValue = IUriPackage & {
type: "package";
};
export type UriWrapperValue = IUriWrapper & {
type: "wrapper";
};
export type UriPackageOrWrapper = UriValue | UriPackageValue | UriWrapperValue;
UriResolutionContext
export class UriResolutionContext implements IUriResolutionContext {
constructor
constructor();
constructor(
resolvingUriMap: Map<string, boolean>,
resolutionPath: Set<string>
);
constructor(
resolvingUriMap: Map<string, boolean>,
history: IUriResolutionStep<unknown>[]
);
constructor(
resolvingUriMap?: Map<string, boolean>,
resolutionPathOrHistory?: Set<string> | IUriResolutionStep<unknown>[]
)