@dynjandi/transform-grammar
v0.3.0
Published
The URL grammar for [Dynjandi](https://dynjandi.dev)'s image CDN: the one place a variant path such as `/<file-id>/-/resize/800x/-/format/webp/` is parsed, normalised, serialised and hashed.
Readme
@dynjandi/transform-grammar
The URL grammar for Dynjandi's image CDN: the one place a variant path such as
/<file-id>/-/resize/800x/-/format/webp/ is parsed, normalised, serialised and hashed.
It is pure TypeScript with a single runtime dependency (zod), and runs in Cloudflare Workers, Node and
browsers. Code that only builds URLs can import the zod-free ./builder entry instead; see
The ./builder entry.
What it exports
| Export | Does |
|---|---|
| parseOperationChain(path) | parses a raw operations path into a NormalizedOperation[], or throws AppError |
| serializeOperationChain(operations) | the inverse: writes normalised operations back into canonical path form |
| buildVariantUrl(origin, fileId, operations) | builds a full variant URL, so a caller never assembles a path by hand. Throws AppError for the file ids . and .., which URL resolution would collapse away from the file |
| getFinalFormat(operations) | the output format the chain resolves to, or null |
| hashNormalizedOperations(operations) | the cache key for a chain: equivalent chains hash equal |
| normalizedOperationsSchema | the Zod schema for a chain of operations |
| focalSuggestionSchema | the Zod schema for the transform container's POST /focal-suggestion answer: { focalX, focalY, source, detector }. focalX and focalY are focalCoordinateSchema (fractions 0-1 from the top left, four decimals), source is "face" or "attention", and detector is FACE_DETECTOR_VERSION. FocalSuggestion is its type |
| AppError, FACE_DETECTOR_VERSION | the error type the parser throws, and the face-detector version that is part of the cache key |
Adding ./builder changed nothing in .. That second entry, @dynjandi/transform-grammar/builder, exports a subset of it
plus checkOperations:
| ./builder export | Does |
|---|---|
| buildVariantUrl, serializeOperationChain, getFinalFormat | the same functions . exports |
| checkOperations(input) | checks an unknown chain against the grammar's limits without zod. Returns { success: true, data } or { success: false, error }, where error is an AppError (status 400) carrying issues: { path, message }[]. data is rebuilt in the schema's key order, so it hashes the same as the zod-parsed chain |
| AppError, FACE_DETECTOR_VERSION | the same class and constant . exports, so instanceof AppError holds across both entries |
| the operation types | NormalizedOperation, ResizeOperation, CropOperation, FormatOperation, QualityOperation, BlurOperation, OutputFormat, CropPosition, FocalCoordinates |
./builder does not export parseOperationChain, the schemas or hashNormalizedOperations.
import { buildVariantUrl } from "@dynjandi/transform-grammar";
buildVariantUrl("https://cdn.dynjandi.dev", "<file-id>", [{ op: "resize", width: 800, height: null }]);
// https://cdn.dynjandi.dev/<file-id>/-/resize/800x/The ./builder entry
import { buildVariantUrl, checkOperations } from "@dynjandi/transform-grammar/builder";Importing anything from . bundles all of zod into a browser build, because the schemas are top-level calls
that a bundler cannot drop. ./builder never loads zod at runtime: every module it reaches is free of a zod
value import, and zod appears only in type positions, which are erased when the package is built. The types it
exports are still derived from the zod schemas, so zod stays a dependency of the package.
Measured size of a browser bundle whose entry imports buildVariantUrl and checkOperations from the built
package's ./builder export and logs both: 4,102 bytes minified and 1,530 bytes gzipped, with no zod module in
the bundle. The control is an entry that imports only buildVariantUrl from the built . export and logs it:
329,467 bytes minified and 65,467 bytes gzipped, with 79 zod modules. (checkOperations is not exported from
., so the control cannot include it.) Measured with esbuild 0.28.1 (--bundle --minify --platform=browser
--format=esm) and gzip -9 on stdin; the . figure grows with whichever zod version the consumer resolves.
checkOperations accepts and rejects the same chains as normalizedOperationsSchema, and a parity suite
runs both over generated cases to keep it so. It does not reproduce zod's error text: the issues carry this
package's own messages. A client-side check is a convenience, not validation: the CDN still validates every
request with zod.
Adding ./builder changed nothing in .: every name, type and behaviour it exported before is as it was.
The Worker and container contract
The CDN Worker parses the request path with parseOperationChain and sends the resulting
NormalizedOperation[] to the transform container as JSON, base64url-encoded, in the x-dynjandi-ops request
header. The container decodes it and validates it against normalizedOperationsSchema before applying
anything. The CDN Worker computes the cache key with hashNormalizedOperations from the parsed chain; the
container never hashes. The container and the Worker validate against the same schema, but the header's
base64url encoding is implemented in each app, not in this package.
The container has one JSON route, POST /focal-suggestion. It takes the original's bytes with no ops header
and answers 200 with a body that focalSuggestionSchema accepts. source is "face" when the point is the
centre of the union of the faces the detector found, and "attention" when none qualified and the point is the
image's most salient region. Errors on this route, as on /transform, are the flat { "error": "<message>" }.
focalSuggestionSchema is exported from . only: ./builder stays zod-free.
Published
This package is published to npm as @dynjandi/transform-grammar so that code outside this repository, such as
the public SDK, builds URLs with the real serializer instead of a copy of it. Everything it exports is public
API, including the server-side pieces listed above. A published version is permanent: npm never lets a version
number be reused.
Only dist/, this README and the LICENSE are in the published tarball.
Licence
MIT. See LICENSE.
