@hugex/artifact 0.11.0 → 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,6 +28,8 @@ export declare class ArtifactDO extends DurableObject<ArtifactEnv> implements Ar
28
28
  private oraRetryAt;
29
29
  /** `expiresAt` of the salt whose alarm was successfully armed. Null means the next view must try again. */
30
30
  private saltAlarmExpiresAt;
31
+ /** One Browser Run session at a time for this artifact. */
32
+ private thumbRunning;
31
33
  constructor(ctx: DurableObjectState, env: ArtifactEnv);
32
34
  alarm(): Promise<void>;
33
35
  create(input: CreateArtifactInput): Promise<{
@@ -93,6 +95,13 @@ export declare class ArtifactDO extends DurableObject<ArtifactEnv> implements Ar
93
95
  }): Promise<{
94
96
  removed: boolean;
95
97
  } | null>;
98
+ /** Encola la captura de la versión activa. Si ya está `ready` en hx-core, no recaptura, salvo `force`. */
99
+ enqueueThumbnail(input?: {
100
+ force?: boolean;
101
+ }): Promise<{
102
+ queued: boolean;
103
+ version: string | null;
104
+ } | null>;
96
105
  stats(opts?: {
97
106
  days?: number;
98
107
  }): Promise<ArtifactStats | null>;
@@ -229,6 +238,19 @@ export declare class ArtifactDO extends DurableObject<ArtifactEnv> implements Ar
229
238
  private indexEntry;
230
239
  private readMeta;
231
240
  private toPublic;
241
+ private afterActivation;
242
+ /**
243
+ * Valida el token firmado (un solo uso) o la cookie de esa visita.
244
+ * `reject` es un token presente pero inválido. `null` es una visita normal.
245
+ */
246
+ private resolveCapture;
247
+ private thumbSecret;
248
+ private issueCaptureToken;
249
+ private capturePageUrl;
250
+ /** @returns whether a capture was queued (false when it was already ready or core is unset). */
251
+ private scheduleThumb;
252
+ private drainThumbs;
253
+ private captureVersion;
232
254
  private secure;
233
255
  private toVersion;
234
256
  private fileMetaFor;
@@ -0,0 +1,116 @@
1
+ export declare const CAPTURE_TTL_MS = 60000;
2
+ export declare const THUMB_MAX_BYTES: number;
3
+ export declare const THUMB_WIDTH = 640;
4
+ export declare const THUMB_HEIGHT = 400;
5
+ export declare const CAPTURE_QUERY = "__hx_capture";
6
+ export declare const CAPTURE_COOKIE = "__hx_capture";
7
+ export type ThumbFail = "timeout" | "navigation" | "no_browser" | "too_large" | "delivery";
8
+ export type ThumbStatusName = "pending" | "ready" | "failed";
9
+ export interface ThumbCoreEnv {
10
+ HX_CORE_URL?: string;
11
+ HX_API_KEY?: string;
12
+ TENANT?: string;
13
+ }
14
+ export declare function coreConfigured(env: ThumbCoreEnv): boolean;
15
+ /** Tras el intento `attempt` (0 = el primero), espera o se rinde. Dos reintentos: 1 min y 10 min. */
16
+ export declare function thumbRetry(attempt: number): {
17
+ delayMs: number;
18
+ } | {
19
+ failed: true;
20
+ };
21
+ export declare function randomHex(bytes?: number): string;
22
+ export declare function signCaptureToken(secret: string, uuid: string, version: string, exp: number, nonce: string): Promise<string>;
23
+ export declare function verifyCaptureToken(secret: string, token: string, uuid: string): Promise<{
24
+ version: string;
25
+ exp: number;
26
+ nonce: string;
27
+ } | null>;
28
+ export declare function captureCookieHeader(token: string): string;
29
+ export declare function readCaptureCookie(header: string | null): string | null;
30
+ export declare function getThumbStatus(env: ThumbCoreEnv, uuid: string, version: string): Promise<ThumbStatusName | null>;
31
+ export declare function putThumbStatus(env: ThumbCoreEnv, uuid: string, version: string, body: {
32
+ status: "pending";
33
+ force?: boolean;
34
+ } | {
35
+ status: "failed";
36
+ reason: ThumbFail;
37
+ force?: boolean;
38
+ }): Promise<boolean>;
39
+ export declare function putThumbImage(env: ThumbCoreEnv, uuid: string, version: string, bytes: Uint8Array): Promise<{
40
+ ok: true;
41
+ } | {
42
+ ok: false;
43
+ reason: "too_large" | "delivery";
44
+ detail: string;
45
+ }>;
46
+ export interface ThumbBrowser {
47
+ newContext(opts: {
48
+ viewport: {
49
+ width: number;
50
+ height: number;
51
+ };
52
+ deviceScaleFactor: number;
53
+ colorScheme: "light";
54
+ }): Promise<{
55
+ newPage(): Promise<ThumbPage>;
56
+ close?: () => Promise<void>;
57
+ }>;
58
+ close(): Promise<void>;
59
+ }
60
+ export interface ThumbRequestFailed {
61
+ url(): string;
62
+ failure(): {
63
+ errorText?: string;
64
+ } | null;
65
+ }
66
+ export interface ThumbResponse {
67
+ status(): number;
68
+ url(): string;
69
+ }
70
+ export interface ThumbPage {
71
+ goto(url: string, opts: {
72
+ waitUntil: "load";
73
+ timeout: number;
74
+ }): Promise<unknown>;
75
+ waitForTimeout(ms: number): Promise<unknown>;
76
+ waitForLoadState?(state: "networkidle", opts: {
77
+ timeout: number;
78
+ }): Promise<unknown>;
79
+ on?(event: "requestfailed", handler: (request: ThumbRequestFailed) => void): void;
80
+ on?(event: "response", handler: (response: ThumbResponse) => void): void;
81
+ on?(event: "console", handler: (message: {
82
+ type(): string;
83
+ text(): string;
84
+ }) => void): void;
85
+ evaluate?(fn: () => void): Promise<unknown>;
86
+ context(): {
87
+ newCDPSession(page: ThumbPage): Promise<{
88
+ send(method: string, params?: unknown): Promise<{
89
+ data?: string;
90
+ }>;
91
+ }>;
92
+ };
93
+ }
94
+ export interface CaptureShot {
95
+ ok: true;
96
+ bytes: Uint8Array;
97
+ width: number;
98
+ height: number;
99
+ detail: string;
100
+ }
101
+ export interface CaptureMiss {
102
+ ok: false;
103
+ reason: ThumbFail;
104
+ detail: string;
105
+ }
106
+ /** Width and height from a WebP RIFF header. Browser Run ignores `deviceScaleFactor`. */
107
+ export declare function webpSize(bytes: Uint8Array): {
108
+ width: number;
109
+ height: number;
110
+ } | null;
111
+ export declare function captureThumbnail(opts: {
112
+ url: string;
113
+ binding: unknown;
114
+ launch?: (binding: unknown) => Promise<ThumbBrowser>;
115
+ deadlineMs?: number;
116
+ }): Promise<CaptureShot | CaptureMiss>;
@@ -53,6 +53,11 @@ export interface ArtifactEnv {
53
53
  * Without it, granting a Worker fails; list and revoke still work.
54
54
  */
55
55
  HX_CORE_URL?: string;
56
+ /**
57
+ * Browser Run binding. Required to capture a version thumbnail. Without it the
58
+ * capture is marked `failed` / `no_browser` and the artifact keeps working.
59
+ */
60
+ BROWSER?: unknown;
56
61
  }
57
62
  /** Target of the same-origin agent route `<base>/__hx/agent/<flowId>/<sessionId>`. */
58
63
  export interface ArtifactAgentProxyTarget {
@@ -507,6 +512,16 @@ export interface ArtifactRpc {
507
512
  }): Promise<{
508
513
  removed: boolean;
509
514
  } | null>;
515
+ /**
516
+ * Queues a capture of the active version. Already-`ready` thumbnails are not recaptured.
517
+ * Null when the artifact does not exist.
518
+ */
519
+ enqueueThumbnail(input?: {
520
+ force?: boolean;
521
+ }): Promise<{
522
+ queued: boolean;
523
+ version: string | null;
524
+ } | null>;
510
525
  }
511
526
  /** RPC surface of ArtifactIndexDO (stub = env.ARTIFACT_INDEX.get(env.ARTIFACT_INDEX.idFromName('index'))). */
512
527
  export interface ArtifactIndexRpc {