-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtypes.ts
More file actions
126 lines (104 loc) · 3.74 KB
/
types.ts
File metadata and controls
126 lines (104 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
// =============================================================================
// Core Data Unit
// =============================================================================
export type ChunkMeta = {
readonly timestamp: number;
readonly kind: "text" | "structured" | "bytes" | "signal";
readonly channel?: "stdout" | "stderr";
readonly annotations?: Readonly<Record<string, unknown>>;
};
export type Chunk<T = unknown> = {
readonly data: T;
readonly meta: ChunkMeta;
};
// =============================================================================
// Stream Primitives
// =============================================================================
export type Layer<I = unknown, O = unknown> = {
readonly name: string;
readonly transform: () => TransformStream<Chunk<I>, Chunk<O>>;
};
export type Source<T = unknown> = {
readonly name: string;
readonly readable: ReadableStream<Chunk<T>>;
};
export type Sink<T = unknown> = {
readonly name: string;
readonly writable: WritableStream<Chunk<T>>;
};
// =============================================================================
// High-Level APIs
// =============================================================================
export type ErrorContext = {
readonly error: unknown;
readonly chunkIndex: number;
readonly pendingCount: number;
readonly lastChunk: Chunk | undefined;
};
export type ErrorHandler = (context: ErrorContext) => void;
export type OutputOptions = {
readonly sink?: Sink;
readonly renderer?: import("./renderers/types.ts").Renderer;
readonly layers?: readonly Layer[];
readonly onError?: ErrorHandler;
};
export type Output = {
readonly write: (...args: import("./span.ts").SpanInput[]) => void;
readonly writeln: (...args: import("./span.ts").SpanInput[]) => void;
readonly flush: () => Promise<void>;
readonly close: () => Promise<void>;
readonly pipe: (...layers: Layer[]) => Output;
};
export type PipelineOptions = {
readonly timeout?: number;
};
export type Pipeline = {
readonly from: (source: Source) => Pipeline;
readonly through: (...layers: Layer[]) => Pipeline;
readonly to: (sink: Sink) => Pipeline;
readonly run: (options?: PipelineOptions) => Promise<void>;
readonly collect: <T = unknown>() => Promise<T[]>;
};
// =============================================================================
// Layer Definition Helper
// =============================================================================
export type LayerTransformer<I = unknown, O = unknown> = {
readonly start?: (
controller: TransformStreamDefaultController<Chunk<O>>,
) => void | Promise<void>;
readonly transform: (
chunk: Chunk<I>,
controller: TransformStreamDefaultController<Chunk<O>>,
) => void | Promise<void>;
readonly flush?: (
controller: TransformStreamDefaultController<Chunk<O>>,
) => void | Promise<void>;
};
export type LayerDefinition<I = unknown, O = unknown> = {
readonly name: string;
readonly create: () => LayerTransformer<I, O>;
};
// =============================================================================
// Errors
// =============================================================================
export class StreamError extends Error {
constructor(message: string) {
super(message);
this.name = "StreamError";
}
}
export class PipelineError extends StreamError {
public override cause?: Error;
constructor(message: string, cause?: Error) {
super(message);
this.name = "PipelineError";
this.cause = cause;
}
}
export class TimeoutError extends StreamError {
constructor(timeoutMs: number) {
super(`Pipeline timed out after ${timeoutMs}ms`);
this.name = "TimeoutError";
}
}