Skip to content

Commit

Permalink
Simplify extractors
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Oct 16, 2024
1 parent 4decbf9 commit 7f4b493
Show file tree
Hide file tree
Showing 24 changed files with 247 additions and 337 deletions.
62 changes: 29 additions & 33 deletions .zed/settings.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
// Folder-specific settings
//
// For a full list of overridable settings, and general information on folder-specific settings,
// see the documentation: https://zed.dev/docs/configuring-zed#settings-files
{
"lsp": {
"deno": {
"settings": {
"deno": {
"enable": true
}
}
}
},
"languages": {
"TypeScript": {
"language_servers": [
"deno",
"!typescript-language-server",
"!vtsls",
"!eslint"
],
"formatter": "language_server"
},
"TSX": {
"language_servers": [
"deno",
"!typescript-language-server",
"!vtsls",
"!eslint"
],
"formatter": "language_server"
}
}
"lsp": {
"deno": {
"settings": {
"deno": {
"enable": true
}
}
}
},
"languages": {
"TypeScript": {
"language_servers": [
"deno",
"!typescript-language-server",
"!vtsls",
"!eslint"
],
"formatter": "language_server"
},
"TSX": {
"language_servers": [
"deno",
"!typescript-language-server",
"!vtsls",
"!eslint"
],
"formatter": "language_server"
}
}
}
17 changes: 3 additions & 14 deletions src/providers/builder.io.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OperationExtractor, Operations, URLTransformer } from "../types.ts";
import { OperationExtractor, Operations } from "../types.ts";
import {
createExtractAndGenerate,
createOperationsGenerator,
extractFromURL,
toCanonicalUrlString,
Expand Down Expand Up @@ -59,16 +60,4 @@ export const generate: URLGenerator<BuilderOperations> = (
return toCanonicalUrlString(url);
};

export const transform: URLTransformer<BuilderOperations> = (
src,
operations,
) => {
const base = extract(src);
if (!base) {
return generate(src, operations);
}
return generate(base.src, {
...base.operations,
...operations,
});
};
export const transform = createExtractAndGenerate(extract, generate);
12 changes: 4 additions & 8 deletions src/providers/bunny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
URLTransformer,
} from "../types.ts";
import {
createExtractAndGenerate,
createOperationsGenerator,
extractFromURL,
toCanonicalUrlString,
Expand Down Expand Up @@ -132,6 +133,8 @@ export const generate: URLGenerator<BunnyOperations> = (
return toCanonicalUrlString(url);
};

const extractAndGenerate = createExtractAndGenerate(extract, generate);

export const transform: URLTransformer<BunnyOperations> = (
src,
operations,
Expand All @@ -142,12 +145,5 @@ export const transform: URLTransformer<BunnyOperations> = (
Math.round(Number(height))
}`;
}
const base = extract(src);
if (!base) {
return generate(src, operations);
}
return generate(base.src, {
...base.operations,
...operations,
});
return extractAndGenerate(src, operations);
};
39 changes: 8 additions & 31 deletions src/providers/cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { getImageCdnForUrlByPath } from "../detect.ts";
import {
OperationExtractor,
Operations,
URLGenerator,
URLTransformer,
} from "../types.ts";
import { OperationExtractor, Operations, URLGenerator } from "../types.ts";
import { ImageFormat } from "../types.ts";
import {
createExtractAndGenerate,
createOperationsHandlers,
stripLeadingSlash,
toCanonicalUrlString,
Expand Down Expand Up @@ -123,40 +119,21 @@ export const extract: OperationExtractor<
CloudflareOperations,
CloudflareOptions
> = (url, options) => {
const parsedUrl = toUrl(url);
if (!parsedUrl.pathname.startsWith("/cdn-cgi/image/")) {
if (getImageCdnForUrlByPath(url) !== "cloudflare") {
return null;
}
const parsedUrl = toUrl(url);

const [, , , modifiers, ...src] = parsedUrl.pathname.split("/");
const operations = operationsParser(modifiers);
return {
src: toCanonicalUrlString(toUrl(src.join("/"))),
operations,
options: {
domain: options?.domain || parsedUrl.hostname === "n"
? undefined
: parsedUrl.hostname,
domain: options?.domain ??
(parsedUrl.hostname === "n" ? undefined : parsedUrl.hostname),
},
};
};

export const transform: URLTransformer<
CloudflareOperations,
CloudflareOptions
> = (
src,
operations,
options,
) => {
const url = toUrl(src);
if (getImageCdnForUrlByPath(url) === "cloudflare") {
const base = extract(url, options);
if (base) {
return generate(base.src, {
...base.operations,
...operations,
}, base.options);
}
}
return generate(src, operations, options);
};
export const transform = createExtractAndGenerate(extract, generate);
42 changes: 13 additions & 29 deletions src/providers/cloudimage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import {
OperationExtractor,
Operations,
URLGenerator,
URLTransformer,
} from "../types.ts";
import { createOperationsGenerator, extractFromURL, toUrl } from "../utils.ts";
import {
createExtractAndGenerate,
createOperationsGenerator,
extractFromURL,
toUrl,
} from "../utils.ts";

export interface CloudimageOperations extends Operations {
/**
Expand Down Expand Up @@ -127,6 +131,9 @@ export const generate: URLGenerator<CloudimageOperations, CloudimageOptions> = (
modifiers = {},
{ token } = {},
) => {
if (!token) {
throw new Error("Token is required for Cloudimage URLs");
}
let srcString = src.toString();
if (srcString.includes("?")) {
modifiers.ci_url_encoded = 1;
Expand All @@ -143,6 +150,9 @@ export const extract: OperationExtractor<
CloudimageOperations,
CloudimageOptions
> = (url, options = {}) => {
if (getImageCdnForUrlByDomain(url) !== "cloudimage") {
return null;
}
const result = extractFromURL(url);
if (!result) {
return null;
Expand All @@ -154,30 +164,4 @@ export const extract: OperationExtractor<
};
};

export const transform: URLTransformer<
CloudimageOperations,
CloudimageOptions
> = (
src,
operations,
options,
) => {
const url = toUrl(src);
// This is a cloudimage URL, so extract the image and operations from the URL
if (getImageCdnForUrlByDomain(url) === "cloudimage") {
const base = extract(url, options);
if (!base) {
console.error("Invalid Cloudimage URL", url.href);
return url.toString();
}
return generate(url.pathname, {
...base.operations,
...operations,
}, base.options);
}
if (!options.token) {
throw new Error("Token is required for Cloudimage URLs");
}
// This is a regular URL, so just append the operations
return generate(url, operations, options);
};
export const transform = createExtractAndGenerate(extract, generate);
11 changes: 4 additions & 7 deletions src/providers/contentful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "../types.ts";
import {
clampDimensions,
createExtractAndGenerate,
createOperationsGenerator,
extractFromURL,
toCanonicalUrlString,
Expand Down Expand Up @@ -126,18 +127,14 @@ export const generate: URLGenerator<ContentfulOperations> = (

export const extract: OperationExtractor<ContentfulOperations> = extractFromURL;

const extractAndGenerate = createExtractAndGenerate(extract, generate);

export const transform: URLTransformer<ContentfulOperations> = (
src,
operations,
) => {
const { width, height } = clampDimensions(operations, 4000, 4000);

const base = extract(src);
if (!base) {
return generate(src, operations);
}
return generate(base.src, {
...base.operations,
return extractAndGenerate(src, {
...operations,
width,
height,
Expand Down
8 changes: 4 additions & 4 deletions src/providers/contentstack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Deno.test("contentstack transform", async (t) => {
await t.step(
"should apply defaults when no operations are provided",
() => {
const result = transform(img, {});
const result = transform(img, {}, {});
assertEqualIgnoringQueryOrder(
result,
`${img}?fit=crop&auto=webp&disable=upscale`,
Expand All @@ -51,7 +51,7 @@ Deno.test("contentstack transform", async (t) => {
fit: "bounds",
auto: "avif",
disable: false,
});
}, {});
assertEqualIgnoringQueryOrder(
result,
`${img}?width=200&height=100&fit=bounds&auto=avif&disable=false`,
Expand All @@ -62,7 +62,7 @@ Deno.test("contentstack transform", async (t) => {
await t.step("should handle edge case: pjpg format", () => {
const result = transform(img, {
format: "pjpg",
});
}, {});
assertEqualIgnoringQueryOrder(
result,
`${img}?auto=webp&format=pjpg&fit=crop&disable=upscale`,
Expand All @@ -73,7 +73,7 @@ Deno.test("contentstack transform", async (t) => {
const result = transform(img, {
overlay: "overlay-image.png",
"overlay-align": "top,left",
});
}, {});
assertEqualIgnoringQueryOrder(
result,
`${img}?overlay=overlay-image.png&overlay-align=top,left&fit=crop&auto=webp&disable=upscale`,
Expand Down
19 changes: 2 additions & 17 deletions src/providers/contentstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
OperationExtractor,
Operations,
URLGenerator,
URLTransformer,
} from "../types.ts";
import {
createExtractAndGenerate,
createOperationsGenerator,
extractFromURL,
toCanonicalUrlString,
Expand Down Expand Up @@ -180,19 +180,4 @@ export const extract: OperationExtractor<
};
};

export const transform: URLTransformer<ContentstackOperations> = (
src,
operations,
) => {
const base = extract(src);
if (!base) {
return generate(src, operations);
}

const combinedOperations = {
...base.operations,
...operations,
};

return generate(base.src, combinedOperations);
};
export const transform = createExtractAndGenerate(extract, generate);
23 changes: 3 additions & 20 deletions src/providers/directus.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { OperationExtractor, Operations, URLGenerator } from "../types.ts";
import {
OperationExtractor,
Operations,
URLGenerator,
URLTransformer,
} from "../types.ts";
import {
createExtractAndGenerate,
createOperationsHandlers,
extractFromURL,
toCanonicalUrlString,
Expand Down Expand Up @@ -67,17 +63,4 @@ export const extract: OperationExtractor<DirectusOperations> = (url) => {
return base;
};

export const transform: URLTransformer<DirectusOperations> = (
src,
operations,
) => {
const base = extract(src);
if (!base) {
return generate(src, operations);
}

return generate(base.src, {
...base.operations,
...operations,
});
};
export const transform = createExtractAndGenerate(extract, generate);
Loading

0 comments on commit 7f4b493

Please sign in to comment.