@format.dev/zip 0.0.0 → 0.1.0

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.
package/dist/web.d.mts ADDED
@@ -0,0 +1,245 @@
1
+ import { RemoteAssetFetcher, ZipOptions } from "@format.dev/types";
2
+
3
+ //#region src/types.d.ts
4
+ /** Header forms accepted for remote asset requests. Mirrors the standard `HeadersInit`. */
5
+ type RequestHeaders = Headers | Record<string, string> | [string, string][];
6
+ /** The response headers a server offers for asking later whether an asset changed. */
7
+ interface RemoteValidators {
8
+ etag?: string;
9
+ lastModified?: string;
10
+ }
11
+ interface RemoteAsset {
12
+ bytes: Uint8Array;
13
+ mimeType?: string;
14
+ validators: RemoteValidators;
15
+ }
16
+ /** A remote asset already inside an archive, as `readRemoteAssets` returns it. */
17
+ interface StoredRemoteAsset {
18
+ url: string;
19
+ bytes: Uint8Array;
20
+ }
21
+ /**
22
+ * Resolves a referenced asset path to its bytes. Returns `null` when there is
23
+ * no asset for that path. `zip()` treats this as missing (and throws, unless
24
+ * `skipMissing` is set).
25
+ *
26
+ * A resolver is the single seam through which `zip()` reaches the filesystem,
27
+ * a URL map, or any other store, which is what keeps the package portable
28
+ * across Node, workers, and browsers. `dirResolver` (Node) and `urlResolver`
29
+ * (web) are the built-ins; pass your own for anything else.
30
+ */
31
+ type AssetResolver = (relPath: string) => Promise<Uint8Array | null>;
32
+ //#endregion
33
+ //#region src/zip.d.ts
34
+
35
+ /** Entry names of an existing archive. A static-mode zip's entries are the document's known-asset set. */
36
+ declare function listZipEntries(bytes: Uint8Array): string[];
37
+ /**
38
+ * The remote assets an archive already holds, from the manifest
39
+ * {@link mergeRemoteAssets} writes, sorted by URL. Empty when the archive has
40
+ * no manifest. Lets a caller compare what was bundled with what the server
41
+ * serves now.
42
+ */
43
+ declare function readRemoteAssets(bytes: Uint8Array): StoredRemoteAsset[];
44
+ interface MergeRemoteAssetsArgs {
45
+ html: string;
46
+ /** Existing archive to merge into, or undefined to start fresh. */
47
+ zip?: Uint8Array;
48
+ options?: ZipOptions;
49
+ }
50
+ /**
51
+ * Fetch every remote (http/https) asset `html` references and merge it into an
52
+ * existing archive, alongside a manifest mapping each URL to its zip entry.
53
+ * Remote stylesheets are walked too, so a remote CSS file's fonts and images
54
+ * come along with it. Local CSS already inside the archive is also walked for
55
+ * remote references.
56
+ *
57
+ * URLs already present in the archive's manifest are not fetched again, so
58
+ * calling this repeatedly over the same archive is idempotent and cheap.
59
+ *
60
+ * @returns the merged archive bytes; the input `zip` unchanged when `html`
61
+ * references nothing remote that isn't already bundled; or `undefined` when
62
+ * there is no input archive and nothing remote to fetch.
63
+ * @throws {RemoteAssetFetchError} when the network itself is unreachable.
64
+ */
65
+ declare function mergeRemoteAssets(args: MergeRemoteAssetsArgs): Promise<Uint8Array | undefined>;
66
+ /**
67
+ * Build a ZIP archive of every asset `html` references, resolving bytes through
68
+ * `resolve`. Runs in Node, workers, and browsers (node-html-parser scans the
69
+ * HTML).
70
+ *
71
+ * @returns the archive bytes, or `undefined` when `html` references no assets.
72
+ * @throws {MissingAssetError} when `skipMissing` is unset and the resolver
73
+ * returns `null` for a referenced asset.
74
+ * @throws {UnresolvableRefError} when `skipMissing` is unset and a local ref
75
+ * names no file — an empty `src`/`href`/`url("")`, or one like `/` or `.` that
76
+ * resolves to the bundle root.
77
+ * @throws {RemoteAssetsDisabledError} when `html` references remote (http/https)
78
+ * assets and `remoteAssets` is not enabled — they would be missing from the
79
+ * archive and the PDF.
80
+ */
81
+ declare function zip(html: string, resolve: AssetResolver, options?: ZipOptions): Promise<Uint8Array | undefined>;
82
+ //#endregion
83
+ //#region src/url-resolver.d.ts
84
+ /**
85
+ * Web-standard resolver that maps asset paths to URLs and fetches them. This is
86
+ * the dynamic-mode runtime resolver: the compiled wrapper bakes in the
87
+ * `{ relPath: url }` map and fetches only the bytes a given render references.
88
+ * Returns `null` for unmapped paths. Portable — uses only `fetch`.
89
+ */
90
+ declare function urlResolver(urlMap: Record<string, string>): AssetResolver;
91
+ //#endregion
92
+ //#region src/helpers.d.ts
93
+ interface RemoteRequestParams {
94
+ url: string;
95
+ headers?: RequestHeaders;
96
+ timeoutMs?: number;
97
+ }
98
+ interface ConditionalRemoteRequestParams extends RemoteRequestParams {
99
+ validators: RemoteValidators;
100
+ }
101
+ /**
102
+ * Fetch a remote asset and return its bytes, parsed MIME type, and the
103
+ * validators the server sent, so a caller holding the bytes can later ask
104
+ * whether they changed with {@link fetchRemoteIfChanged}.
105
+ *
106
+ * Uses `fetch` + `arrayBuffer()` rather than a Node stream so the remote path
107
+ * stays portable across Node, workers, and browsers.
108
+ *
109
+ * @throws {RemoteAssetFetchError} when the network itself fails (offline, DNS,
110
+ * blocked egress, timeout). HTTP error statuses throw a plain Error — the
111
+ * server was reachable, so it's a different problem.
112
+ */
113
+ declare function fetchRemote(params: RemoteRequestParams): Promise<RemoteAsset>;
114
+ /**
115
+ * Ask the server whether a remote asset changed since `validators` were
116
+ * issued. Returns `null` on `304 Not Modified`, otherwise the full asset. A
117
+ * server that sent no validators gets an ordinary request, so this always
118
+ * downloads there.
119
+ *
120
+ * @throws {RemoteAssetFetchError} and plain HTTP errors, as {@link fetchRemote}.
121
+ */
122
+ declare function fetchRemoteIfChanged(params: ConditionalRemoteRequestParams): Promise<RemoteAsset | null>;
123
+ /**
124
+ * Probe each remote URL and return the ones that can't be loaded — a failed
125
+ * request or a non-OK status, the same "broken" definition `fetchRemote` uses.
126
+ * Collects every failure rather than stopping at the first, so callers can report
127
+ * the full set. Runs the probes concurrently.
128
+ */
129
+ declare function findBrokenRemoteAssets(params: {
130
+ urls: string[];
131
+ headers?: RequestHeaders;
132
+ timeoutMs?: number;
133
+ fetch?: RemoteAssetFetcher;
134
+ }): Promise<string[]>;
135
+ //#endregion
136
+ //#region src/scan-lite.d.ts
137
+ /**
138
+ * Scan an HTML string for the local asset references it carries, returning each
139
+ * as a bundle-root relative path. Dependency-free implementation for browser,
140
+ * worker, and edge bundles — same contract as scan.ts's `scanAssetRefs`.
141
+ */
142
+ declare const scanAssetRefs: (html: string) => string[];
143
+ //#endregion
144
+ //#region src/scan-remote.d.ts
145
+ /**
146
+ * Every remote (http/https) asset reference in an HTML string: `<img src>`,
147
+ * inline-SVG `<image>`/`<use>` hrefs, `<link rel=stylesheet>` hrefs, and
148
+ * `url()`/`@import` targets in `<style>` blocks and style attributes.
149
+ *
150
+ * Parser-backed (node-html-parser) counterpart to scan-lite's regex
151
+ * `scanRemoteRefs`; both are built from `makeScanRemoteRefs`, so they stay in
152
+ * step. Pure and IO-free — it does not fetch anything. PDF rendering never
153
+ * fetches remote references, so anything this returns will be missing from the
154
+ * output unless it's bundled (see `mergeRemoteAssets`) or removed.
155
+ */
156
+ declare const scanRemoteRefs: (html: string) => string[];
157
+ //#endregion
158
+ //#region src/errors.d.ts
159
+ /**
160
+ * Every error thrown by `@format.dev/zip` (and by compiled documents while building
161
+ * their assets zip) extends this class. The umbrella check lets callers branch
162
+ * once on "this is a zip-side failure", then narrow further by `code`.
163
+ *
164
+ * Mirrors `FormatError` from `@format.dev/client`: a single class to catch, a
165
+ * `code` field to dispatch on.
166
+ */
167
+ type FormatZipErrorCode = 'MISSING_ASSET' | 'UNRESOLVABLE_REF' | 'ASSET_MISMATCH' | 'INVALID_EXTENSION' | 'REMOTE_ASSETS_DISABLED' | 'REMOTE_ASSET_FETCH';
168
+ declare class FormatZipError extends Error {
169
+ readonly code: FormatZipErrorCode;
170
+ constructor(code: FormatZipErrorCode, message: string, options?: ErrorOptions);
171
+ }
172
+ declare class MissingAssetError extends FormatZipError {
173
+ relPath: string;
174
+ constructor(relPath: string);
175
+ }
176
+ /**
177
+ * Thrown by `zip()` when the HTML references a local asset that resolves to no
178
+ * file: an empty `src`/`href`/`url("")`, or one like `/`, `.`, or `..` that
179
+ * points at the bundle root (a directory) rather than a file. A same-document
180
+ * fragment (`url(#gradient)`) is not this: it names an element already in the
181
+ * document and is skipped silently. Reported with a warning instead of thrown
182
+ * when `skipMissing` is set.
183
+ */
184
+ declare class UnresolvableRefError extends FormatZipError {
185
+ ref: string;
186
+ constructor(ref: string);
187
+ }
188
+ interface AssetMismatchDetail {
189
+ documentName: string;
190
+ mode: 'static' | 'dynamic';
191
+ missing: string[];
192
+ known: string[];
193
+ /**
194
+ * Remote (http/https) URLs the HTML references. The render-time check attaches
195
+ * these so a later step can find broken remotes even when local assets are
196
+ * already missing — they are referenced, not yet known to be unreachable.
197
+ */
198
+ remoteRefs?: string[];
199
+ /** Remote URLs that could not be loaded, filled in once they have been probed. */
200
+ brokenRemote?: string[];
201
+ }
202
+ /**
203
+ * Thrown by a compiled document wrapper when rendered HTML references assets
204
+ * the bundle cannot supply: local files not in its known-asset set, or remote
205
+ * URLs that could not be loaded. Carries enough context for the consumer to fix
206
+ * the source: add the file to the shared assets folder or the document's
207
+ * `./assets/` folder, switch asset mode, or make the remote URL reachable.
208
+ */
209
+ declare class AssetMismatchError extends FormatZipError {
210
+ documentName: string;
211
+ mode: 'static' | 'dynamic';
212
+ missing: string[];
213
+ known: string[];
214
+ remoteRefs: string[];
215
+ brokenRemote: string[];
216
+ constructor(detail: AssetMismatchDetail);
217
+ }
218
+ declare class InvalidExtensionError extends FormatZipError {
219
+ url: string;
220
+ mimeType?: string | undefined;
221
+ constructor(url: string, mimeType?: string | undefined);
222
+ }
223
+ /**
224
+ * Thrown by `zip()` when the HTML references remote (http/https) assets and
225
+ * `remoteAssets` is not enabled. PDF generation never fetches remote
226
+ * references at render time, so a zip built without them produces a broken
227
+ * PDF — failing loudly here makes the gap visible at the source.
228
+ */
229
+ declare class RemoteAssetsDisabledError extends FormatZipError {
230
+ urls: string[];
231
+ constructor(urls: string[]);
232
+ }
233
+ /**
234
+ * Thrown when a remote asset cannot be fetched because the network itself
235
+ * failed (offline machine, DNS failure, blocked egress, timeout) — as opposed
236
+ * to the server answering with an error status. The message is written for the
237
+ * common trap: a CI runner with no outbound network access.
238
+ */
239
+ declare class RemoteAssetFetchError extends FormatZipError {
240
+ url: string;
241
+ constructor(url: string, cause?: unknown);
242
+ }
243
+ //#endregion
244
+ export { type AssetMismatchDetail, AssetMismatchError, type AssetResolver, FormatZipError, type FormatZipErrorCode, InvalidExtensionError, MissingAssetError, type RemoteAsset, RemoteAssetFetchError, type RemoteAssetFetcher, RemoteAssetsDisabledError, type RemoteValidators, type StoredRemoteAsset, UnresolvableRefError, type ZipOptions, fetchRemote, fetchRemoteIfChanged, findBrokenRemoteAssets, listZipEntries, mergeRemoteAssets, readRemoteAssets, scanAssetRefs, scanRemoteRefs, urlResolver, zip };
245
+ //# sourceMappingURL=web.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.d.mts","names":[],"sources":["../src/types.ts","../src/zip.ts","../src/url-resolver.ts","../src/helpers.ts","../src/scan-lite.ts","../src/scan-remote.ts","../src/errors.ts"],"sourcesContent":[],"mappings":";;;;KAGY,cAAA,GAAiB,UAAU;AAAvC;AAGiB,UAAA,gBAAA,CAAgB;EAKhB,IAAA,CAAA,EAAA,MAAA;EAOA,YAAA,CAAA,EAAA,MAAiB;AAelC;UAtBiB,WAAA;SACT;;EC4UQ,UAAA,ED1UH,gBC0UiB;AAU9B;AAkBC;AAwBqB,UD1XL,iBAAA,CC0XsB;EAAO,GAAA,EAAA,MAAA;EAAgC,KAAA,EDxXtE,UCwXsE;;;AAuE9E;;;;;;;;;AC3cgB,KFyBJ,aAAA,GEzBe,CAAA,OAAS,EAAyB,MAAA,EAAA,GFyBZ,OEzByB,CFyBjB,UEzBiB,GAAA,IAAA,CAAA;;;;AD4WzE;AAwBqB,iBApDN,cAAA,CAoDuB,KAAA,EApDD,UAoDC,CAAA,EAAA,MAAA,EAAA;;;;;AAuEvC;;AAA0E,iBAjH1D,gBAAA,CAiH0D,KAAA,EAjHlC,UAiHkC,CAAA,EAjHrB,iBAiHqB,EAAA;UA7FhE,qBAAA,CA6FqF;EAAR,IAAA,EAAA,MAAA;EAAO;QA1FvF;YACI;;AClXX;;;;ACR4G;AASnF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