Skip to content

Commit

Permalink
ci: add deno fmt workflow (#118)
Browse files Browse the repository at this point in the history
* ci: add deno fmt workflow

* chore: fix name

* chore: format

---------

Co-authored-by: ascorbic <ascorbic@users.noreply.github.com>
  • Loading branch information
ascorbic and ascorbic authored Feb 7, 2024
1 parent d06651e commit b1333c7
Show file tree
Hide file tree
Showing 16 changed files with 216 additions and 176 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Format
on:
pull_request:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Deno
uses: denoland/setup-deno@v1
with:
deno-version: vx.x.x
- name: Typecheck
run: deno fmt
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: "chore: format"
branch: ${{ github.head_ref }}
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
},
"[javascriptreact]": {
"editor.defaultFormatter": "denoland.vscode-deno"
},
}
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!-- deno-fmt-ignore-file -->
# Changelog

## [3.16.0](https://github.com/ascorbic/unpic/compare/v3.15.0...v3.16.0) (2023-12-09)
Expand Down
1 change: 0 additions & 1 deletion demo/src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ import { h, render } from "preact";
import App from "./App";

render(<App />, document.getElementById("root"));

2 changes: 1 addition & 1 deletion demo/src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
"sizes": "512x512"
}
]
}
}
2 changes: 1 addition & 1 deletion demo/src/sw.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getFiles, setupPrecaching, setupRouting } from 'preact-cli/sw/';
import { getFiles, setupPrecaching, setupRouting } from "preact-cli/sw/";

setupRouting();
setupPrecaching(getFiles());
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"jsx": "react-jsx",
"jsxImportSource": "https://esm.sh/preact@10.11.2"
}
}
}
25 changes: 15 additions & 10 deletions src/transformers/astro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,33 @@ const img =
Deno.test("astro parser", () => {
const parsed = parse(img);
const expected: ParsedUrl<AstroParams> = {
base: "/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg",
base:
"/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg",
params: {
"href": "https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg"
"href":
"https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg",
},
cdn: "astro"
}
cdn: "astro",
};
assertEquals(JSON.stringify(parsed), JSON.stringify(expected));
});

Deno.test("astro parser endpoint", () => {
const parsed = parse("/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg");
const parsed = parse(
"/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg",
);
const expected: ParsedUrl<AstroParams> = {
base: "/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg",
base:
"/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg",
params: {
"href": "https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg"
"href":
"https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg",
},
cdn: "astro"
}
cdn: "astro",
};
assertEquals(JSON.stringify(parsed), JSON.stringify(expected));
});


Deno.test("astro", async (t) => {
await t.step("should format a URL", () => {
const result = transform({
Expand Down
24 changes: 14 additions & 10 deletions src/transformers/astro.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { UrlParser, UrlTransformer, ShouldDelegateUrl } from "../types.ts";
import { ShouldDelegateUrl, UrlParser, UrlTransformer } from "../types.ts";
import {
getNumericParam,
setParamIfDefined,
toUrl,
toCanonicalUrlString,
toUrl,
} from "../utils.ts";
import { getImageCdnForUrlByDomain } from "../detect.ts";

Expand All @@ -14,10 +14,10 @@ export interface AstroParams {

export const delegateUrl: ShouldDelegateUrl = (url) => {
const parsedUrl = toUrl(url);
const searchParamHref = parsedUrl.searchParams.get("href")
const searchParamHref = parsedUrl.searchParams.get("href");
const decodedHref = typeof searchParamHref === "string"
? decodeURIComponent(searchParamHref)
: new URL(parsedUrl.pathname, parsedUrl.origin).toString()
: new URL(parsedUrl.pathname, parsedUrl.origin).toString();
const source = toCanonicalUrlString(toUrl(decodedHref));

if (!source || !source.startsWith("http")) {
Expand All @@ -35,11 +35,13 @@ export const delegateUrl: ShouldDelegateUrl = (url) => {

export const parse: UrlParser<AstroParams> = (url) => {
const parsedUrl = toUrl(url);
const searchParamHref = parsedUrl.searchParams.get("href")
const searchParamHref = parsedUrl.searchParams.get("href");
const decodedHref = typeof searchParamHref === "string"
? decodeURIComponent(searchParamHref)
: new URL(parsedUrl.pathname, parsedUrl.origin).toString()
const encodedHref = encodeURIComponent(toCanonicalUrlString(toUrl(decodedHref)));
: new URL(parsedUrl.pathname, parsedUrl.origin).toString();
const encodedHref = encodeURIComponent(
toCanonicalUrlString(toUrl(decodedHref)),
);
const width = getNumericParam(parsedUrl, "w") || undefined;
const height = getNumericParam(parsedUrl, "h") || undefined;
const format = parsedUrl.searchParams.get("f") || undefined;
Expand All @@ -56,11 +58,13 @@ export const parse: UrlParser<AstroParams> = (url) => {
};

export const transform: UrlTransformer = (
{ url: originalUrl, width, height, format, },
{ url: originalUrl, width, height, format },
) => {
const parsedUrl = toUrl(originalUrl);
const href = toCanonicalUrlString(new URL(parsedUrl.pathname, parsedUrl.origin))
const url = {searchParams: new URLSearchParams()} as URL
const href = toCanonicalUrlString(
new URL(parsedUrl.pathname, parsedUrl.origin),
);
const url = { searchParams: new URLSearchParams() } as URL;

setParamIfDefined(url, "href", href, true, true);
setParamIfDefined(url, "w", width, true, true);
Expand Down
6 changes: 4 additions & 2 deletions src/transformers/cloudflare_images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const cloudflareImagesRegex =
/https?:\/\/(?<host>[^\/]+)\/cdn-cgi\/imagedelivery\/(?<accountHash>[^\/]+)\/(?<imageId>[^\/]+)\/*(?<transformations>[^\/]+)*$/g;

const parseTransforms = (transformations: string) =>
Object.fromEntries(transformations?.split(",")?.map((t) => t.split("=")) ?? []);
Object.fromEntries(
transformations?.split(",")?.map((t) => t.split("=")) ?? [],
);

const formatUrl = (
{
Expand All @@ -25,7 +27,7 @@ const formatUrl = (
).join(",");

const pathSegments = [
host,
host,
"cdn-cgi",
"imagedelivery",
accountHash,
Expand Down
17 changes: 11 additions & 6 deletions src/transformers/cloudimage.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { assertEquals } from "https://deno.land/std@0.172.0/testing/asserts.ts";
import { ParsedUrl } from "../types.ts";
import { parse, transform, CloudimageParams } from "./cloudimage.ts";
import { CloudimageParams, parse, transform } from "./cloudimage.ts";

Deno.test("Cloudimage parser", async (t) => {
await t.step("parses a URL", () => {
const parsed = parse("https://doc.cloudimg.io/sample.li/flat1.jpg?w=450&h=200&q=90");
const parsed = parse(
"https://doc.cloudimg.io/sample.li/flat1.jpg?w=450&h=200&q=90",
);
const expected: ParsedUrl<CloudimageParams> = {
base: "https://doc.cloudimg.io/sample.li/flat1.jpg",
cdn: "cloudimage",
width: 450,
height: 200,
params: {
quality: 90
quality: 90,
},
};
assertEquals(parsed, expected);
Expand All @@ -25,7 +27,7 @@ Deno.test("Cloudimage parser", async (t) => {
width: undefined,
height: undefined,
params: {
quality: undefined
quality: undefined,
},
};
assertEquals(parsed, expected);
Expand All @@ -37,15 +39,18 @@ Deno.test("Cloudimage transformer", async (t) => {
const result = transform({
url: "https://doc.cloudimg.io/sample.li/flat1.jpg?q=90",
width: 450,
height: 200
height: 200,
});
assertEquals(
result?.toString(),
"https://doc.cloudimg.io/sample.li/flat1.jpg?q=90&w=450&h=200",
);
});
await t.step("should not set height if not provided", () => {
const result = transform({ url: "https://doc.cloudimg.io/sample.li/flat1.jpg?q=90", width: 450 });
const result = transform({
url: "https://doc.cloudimg.io/sample.li/flat1.jpg?q=90",
width: 450,
});
assertEquals(
result?.toString(),
"https://doc.cloudimg.io/sample.li/flat1.jpg?q=90&w=450",
Expand Down
8 changes: 2 additions & 6 deletions src/transformers/cloudimage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { UrlParser, UrlTransformer } from "../types.ts";
import {
getNumericParam,
setParamIfDefined,
toUrl,
} from "../utils.ts";
import { getNumericParam, setParamIfDefined, toUrl } from "../utils.ts";

export interface CloudimageParams {
quality?: number;
Expand All @@ -21,7 +17,7 @@ export const parse: UrlParser<CloudimageParams> = (url) => {
height,
base: parsedUrl.toString(),
params: {
quality
quality,
},
cdn: "cloudimage",
};
Expand Down
10 changes: 6 additions & 4 deletions src/transformers/cloudinary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const img =

const imgNoTransforms = "https://res.cloudinary.com/demo/image/upload/dog.jpg";

const imgFetchNoTransforms = "https://res.cloudinary.com/demo/image/fetch/https://mydomain.com/images/logo.jpg";
const imgFetchNoTransforms =
"https://res.cloudinary.com/demo/image/fetch/https://mydomain.com/images/logo.jpg";

const imgWithPath =
"https://res.cloudinary.com/demo/image/upload/b_rgb:FFFFFF,c_fill,dpr_2.0,f_auto,g_auto,h_600,q_auto,w_600/v1/Product%20gallery%20demo/New%20Demo%20Pages/Tshirt/tshirt1";
Expand Down Expand Up @@ -69,7 +70,8 @@ Deno.test("cloudinary parser", async (t) => {
await t.step("parses a fetch URL without transforms", () => {
const parsed = parse(imgFetchNoTransforms);
const expected: ParsedUrl<CloudinaryParams> = {
base: "https://res.cloudinary.com/demo/image/fetch/https://mydomain.com/images/logo",
base:
"https://res.cloudinary.com/demo/image/fetch/https://mydomain.com/images/logo",
cdn: "cloudinary",
format: "jpg",
width: undefined,
Expand Down Expand Up @@ -207,15 +209,15 @@ Deno.test("cloudinary transformer", async (t) => {
);
});

await t.step("transforms a fetch URL without parsed transforms", () => {
await t.step("transforms a fetch URL without parsed transforms", () => {
const result = transform({
url: imgFetchNoTransforms,
width: 100,
height: 200,
});
assertEquals(
result?.toString(),
"https://res.cloudinary.com/demo/image/fetch/w_100,h_200,c_lfill,f_auto/https://mydomain.com/images/logo.jpg"
"https://res.cloudinary.com/demo/image/fetch/w_100,h_200,c_lfill,f_auto/https://mydomain.com/images/logo.jpg",
);
});

Expand Down
15 changes: 7 additions & 8 deletions src/transformers/imageengine.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { assertEquals } from "https://deno.land/std@0.172.0/testing/asserts.ts";
import { ParsedUrl } from "../types.ts";
import { parse, transform, ImageEngineParams } from "./imageengine.ts";
import { ImageEngineParams, parse, transform } from "./imageengine.ts";

const img =
"https://blazing-fast-pics.cdn.imgeng.in/images/pic_1.jpg";
const parseImg =
"https://blazing-fast-pics.cdn.imgeng.in/images/pic_1.jpg?imgeng=/w_200/h_100/f_webp/m_box"
const transformImage =
"https://blazing-fast-pics.cdn.imgeng.in/images/pic_1.jpg?imgeng=/m_outside/f_png"
const img = "https://blazing-fast-pics.cdn.imgeng.in/images/pic_1.jpg";
const parseImg =
"https://blazing-fast-pics.cdn.imgeng.in/images/pic_1.jpg?imgeng=/w_200/h_100/f_webp/m_box";
const transformImage =
"https://blazing-fast-pics.cdn.imgeng.in/images/pic_1.jpg?imgeng=/m_outside/f_png";

Deno.test("ImageEngine parser", async (t) => {
await t.step("parses a URL", () => {
Expand Down Expand Up @@ -45,7 +44,7 @@ Deno.test("ImageEngine transformer", async (t) => {
url: img,
width: 200,
height: 100,
format: "webp"
format: "webp",
});
assertEquals(
result?.toString(),
Expand Down
Loading

0 comments on commit b1333c7

Please sign in to comment.