@popeindustries/lit-html-server
Version:
Efficiently render streaming lit-html templates on the server (or in a ServiceWorker!)
166 lines (152 loc) • 5.15 kB
TypeScript
export type TemplateResult = import('@popeindustries/lit-html').TemplateResult;
export type ElementRenderer = import('./element-renderer.js').ElementRenderer;
/**
* The return type of the template tag functions
*/
export type TemplateInstance = {
_$litServerTemplateInstance$: boolean;
hydratable: boolean;
id: number;
index: number;
maxIndex: number;
prefix: Buffer;
suffix: Buffer;
template: Template;
valueIndex: number;
values: Array<unknown>;
setAsRoot(type?: 'light' | 'shadow', styles?: string): void;
readChunk(options?: InternalRenderOptions): unknown;
};
/**
* A cacheable Template that stores the "strings" and "parts" associated with a
* tagged template literal invoked with "html`...`".
*/
export class Template {
digest: string;
strings: Array<Buffer>;
parts: Array<Part>;
constructor(strings: TemplateStringsArray);
}
interface TemplateResultRenderer {
push: (chunk: Buffer | null) => boolean;
destroy: (err: Error) => void;
}
type AttributeDataType = 'boolean' | 'attribute' | 'property' | 'event' | 'element';
type BooleanAttributeData = {
type: 'boolean';
length: number;
name: string;
nameBuffer: Buffer;
value?: string;
resolvedBuffer?: Buffer;
};
type AttributeOrPropertyAttributeData = {
type: 'attribute' | 'property';
length: number;
name: string;
strings?: Array<string>;
value?: string;
resolvedBuffer?: Buffer;
};
type DefaultAttributeData = {
type: 'event' | 'element';
length: number;
name: string;
value: string;
resolvedBuffer: Buffer;
};
type AttributeData = BooleanAttributeData | AttributeOrPropertyAttributeData | DefaultAttributeData;
export interface PartInfo {
type: number;
tagName: string;
name?: string;
strings?: Array<string>;
}
export enum PartType {
METADATA = 0,
ATTRIBUTE = 1,
CHILD = 2,
CUSTOMELEMENT_OPEN = 3,
CUSTOMELEMENT_CLOSE = 4,
}
interface AttributePartType {
readonly length: number;
readonly tagName: string;
readonly type: PartType.ATTRIBUTE;
resolveValue(values: Array<unknown>, options: InternalRenderOptions): unknown;
}
interface ChildPartType {
readonly tagName: string;
readonly type: PartType.CHILD;
resolveValue(value: unknown, options: InternalRenderOptions): unknown;
}
interface CustomElementOpenPartType {
readonly length: number;
readonly tagName: string;
readonly type: PartType.CUSTOMELEMENT_OPEN;
resolveValue(values: Array<unknown>, options: InternalRenderOptions): unknown;
}
interface CustomElementClosePartType {
readonly tagName: string;
readonly type: PartType.CUSTOMELEMENT_CLOSE;
resolveValue(options: InternalRenderOptions): unknown;
}
interface MetadataPartType {
readonly tagName: string;
readonly type: PartType.METADATA;
readonly value: Buffer;
resolveValue(options: InternalRenderOptions): unknown;
}
export type Part = MetadataPartType | CustomElementOpenPartType | ChildPartType | AttributePartType;
type ElementRendererConstructor = {
new (tagName: string): ElementRenderer;
matchesClass(ceClass: typeof HTMLElement, tagName: string): boolean;
};
/**
* Options supported by template render functions
*/
export type RenderOptions = {
/**
* `ElementRenderer` subclasses for rendering of web components with custom base classes
*/
elementRenderers?: Array<ElementRendererConstructor>;
};
type InternalRenderOptions = RenderOptions & {
/** The stack of nested custom elements in the tree to determine root elements */
customElementStack: Array<string>;
/** The flag to enable/disable metadata. Set when TemplateInstance.hydratable === true */
includeHydrationMetadata?: boolean;
/** The TemplateInstance.id corresponding to hydratable root, used to disable metadata when complete */
hydrationMetadataRootId?: number;
};
type RegexTagGroups = {
commentStart: string | undefined;
tagName: string | undefined;
dynamicTagName: string | undefined;
};
type RegexAttrGroups = {
attributeName: string | undefined;
spacesAndEquals: string | undefined;
quoteChar: string | undefined;
};
type RegexAttrValueGroups = {
attributeValue: string | undefined;
closingChar: string | undefined;
};
export { html, noChange, nothing, svg } from '@popeindustries/lit-html';
/**
* Renders a value, usually a `TemplateResult` returned from using the `html` tagged template literal, to a Buffer resolving Promise
*/
export function renderToBuffer(value: unknown, options?: RenderOptions): Promise<Buffer>;
/**
* Renders a value, usually a `TemplateResult` returned from using the `html` tagged template literal, to a Readable stream
*/
export function renderToNodeStream(value: unknown, options?: RenderOptions): import('stream').Readable;
/**
* Renders a value, usually a `TemplateResult` returned from using the `html` tagged template literal, to a string resolving Promise
*/
export function renderToString(value: unknown, options?: RenderOptions): Promise<string>;
/**
* Renders a value, usually a `TemplateResult` returned from using the `html` tagged template literal, to a Readable stream
*/
export function renderToWebStream(value: unknown, options?: RenderOptions): ReadableStream;