imgix-url-builder 0.0.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.
@@ -0,0 +1,68 @@
1
+ import type { BuildWidthSrcSetParams } from "../buildWidthSrcSet";
2
+ import { buildSignedURL } from "./buildSignedURL";
3
+
4
+ /**
5
+ * Builds an `<img>` `srcset` attribute value for a given set of widths. It can
6
+ * also optinally apply Imgix URL API parameters to the URLs. The URLs are
7
+ * signed by appending a signature to their URL parameters. This locks the URLs
8
+ * and their parameters to the signature to prevent URL tampering.
9
+ *
10
+ * The `width` URL parameter will be applied for each `srcset` entry. If a
11
+ * `width` or `w` parameter is provided to the `params` parameter, it will be ignored.
12
+ *
13
+ * **Important**: This function should only be used in a trusted environment and
14
+ * never in a browser. The `secureURLToken` parameter is a secret and should not
15
+ * be exposed to the public.
16
+ *
17
+ * @example
18
+ *
19
+ * ```ts
20
+ * const srcset = buildSignedWidthSrcSet(
21
+ * "https://example.imgix.net/image.png",
22
+ * "example-token",
23
+ * { widths: [400, 800, 1600] },
24
+ * );
25
+ * // => https://example.imgix.net/image.png?width=400&s=def3e221c3f4c4debda091b8e49420ea 400w,
26
+ * // https://example.imgix.net/image.png?width=800&s=f12c7c39333410c10c2930b57116a943 800w,
27
+ * // https://example.imgix.net/image.png?width=1600&s=3a975b5087ab7ad2ab91fe66072fd628 1600w
28
+ * ```
29
+ *
30
+ * @example
31
+ *
32
+ * ```ts
33
+ * const srcset = buildSignedWidthSrcSet(
34
+ * "https://example.imgix.net/image.png",
35
+ * "example-token",
36
+ * {
37
+ * widths: [400, 800, 1600],
38
+ * sat: -100,
39
+ * },
40
+ * );
41
+ * // => https://example.imgix.net/image.png?width=400&sat=-100&s=def3e221c3f4c4debda091b8e49420ea 400w,
42
+ * // https://example.imgix.net/image.png?width=800&sat=-100&s=f12c7c39333410c10c2930b57116a943 800w,
43
+ * // https://example.imgix.net/image.png?width=1600&sat=-100&s=3a975b5087ab7ad2ab91fe66072fd628 1600w
44
+ * ```
45
+ *
46
+ * @param url - Full absolute URL to the Imgix image.
47
+ * @param secureURLToken - The secret secure URL token for the image's Imgix source.
48
+ * @param params - An object of Imgix URL API parameters. The `widths` parameter
49
+ * defines the resulting `srcset` widths.
50
+ *
51
+ * @returns A `srcset` attribute value for `url` with the given Imgix URL API
52
+ * parameters applied.
53
+ */
54
+ export const buildSignedWidthSrcSet = (
55
+ url: string,
56
+ secureURLToken: string,
57
+ { widths, ...params }: BuildWidthSrcSetParams,
58
+ ): string => {
59
+ return widths
60
+ .map((width) => {
61
+ return `${buildSignedURL(url, secureURLToken, {
62
+ ...params,
63
+ w: undefined,
64
+ width,
65
+ })} ${width}w`;
66
+ })
67
+ .join(", ");
68
+ };
@@ -0,0 +1,19 @@
1
+ // Re-exports of the root entry.
2
+ export { buildURL } from "../buildURL";
3
+
4
+ // Node.js-specific exports
5
+ //
6
+ // These exports contain code that should only be run in a Node.js environment.
7
+ // They may contain Node.js-specific code or should not be run in a
8
+ // client-facing environment.
9
+
10
+ export { SecureClient } from "./SecureClient";
11
+ export type { SecureClientOptions } from "./SecureClient";
12
+
13
+ export { buildSignedURL } from "./buildSignedURL";
14
+
15
+ export { buildSignedWidthSrcSet } from "./buildSignedWidthSrcSet";
16
+
17
+ export { buildSignedPixelDensitySrcSet } from "./buildSignedPixelDensitySrcSet";
18
+
19
+ export { signURL } from "./signURL";
@@ -0,0 +1,44 @@
1
+ import { createHash } from "crypto";
2
+
3
+ /**
4
+ * Signs an Imgix image URL by appending a signature to the URL parameters. This
5
+ * locks the URL and its parameters to the signature to prevent URL tampering.
6
+ *
7
+ * **Important**: This function should only be used in a trusted environment and
8
+ * never in a browser. The `secureURLToken` parameter is a secret and should not
9
+ * be exposed to the public.
10
+ *
11
+ * @example
12
+ *
13
+ * ```ts
14
+ * const url = signURL(
15
+ * "https://example.imgix.net/image.png?width=400",
16
+ * "example-token",
17
+ * );
18
+ * // => https://example.imgix.net/image.png?width=400&s=def3e221c3f4c4debda091b8e49420ea
19
+ * ```
20
+ *
21
+ * @param url - Full absolute URL to the Imgix image to sign.
22
+ * @param secureURLToken - The secret Imgix secure URL token used to generate
23
+ * the signature.
24
+ *
25
+ * @returns `url` with a signature appended via an `s` URL parameter.
26
+ *
27
+ * @see Details on securing Imgix images: https://docs.imgix.com/setup/securing-images
28
+ */
29
+ export const signURL = (url: string, secureURLToken: string): string => {
30
+ const instance = new URL(url);
31
+
32
+ // If an `s` param is passed explicitly, it will be overridden. It must
33
+ // also not be used when generating the signature.
34
+ instance.searchParams.delete("s");
35
+
36
+ // @see https://github.com/imgix/imgix-blueprint#securing-urls
37
+ const signature = createHash("md5")
38
+ .update(secureURLToken + instance.pathname + instance.search)
39
+ .digest("hex");
40
+
41
+ instance.searchParams.append("s", signature);
42
+
43
+ return instance.toString();
44
+ };