@howells/stow-next 2.0.0 → 2.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.
@@ -0,0 +1,103 @@
1
+ // src/image-loader.ts
2
+ var STOW_DOMAIN_PATTERN = /\.stow\.sh$/;
3
+ function isStowUrl(src) {
4
+ try {
5
+ const url = new URL(src);
6
+ return STOW_DOMAIN_PATTERN.test(url.hostname);
7
+ } catch {
8
+ return false;
9
+ }
10
+ }
11
+ function buildTransformParams(width, quality, config) {
12
+ const params = new URLSearchParams();
13
+ params.set("w", width.toString());
14
+ if (config.aspectRatio) {
15
+ params.set("h", Math.round(width / config.aspectRatio).toString());
16
+ }
17
+ params.set("q", quality.toString());
18
+ if (config.fit) {
19
+ params.set("fit", config.fit);
20
+ }
21
+ if (config.gravity) {
22
+ params.set("gravity", config.gravity);
23
+ }
24
+ if (config.defaultFormat) {
25
+ params.set("f", config.defaultFormat);
26
+ }
27
+ return params;
28
+ }
29
+ function transformStowUrl(src, baseUrl, params) {
30
+ const url = new URL(src, baseUrl);
31
+ const pathname = url.pathname;
32
+ if (pathname.startsWith("/files/")) {
33
+ const transformPath = pathname.replace("/files/", "/transform/");
34
+ return `${baseUrl}${transformPath}?${params.toString()}`;
35
+ }
36
+ if (pathname.startsWith("/transform/")) {
37
+ return `${baseUrl}${pathname}?${params.toString()}`;
38
+ }
39
+ return `${src}${src.includes("?") ? "&" : "?"}${params.toString()}`;
40
+ }
41
+ function createStowLoader(config = {}) {
42
+ const {
43
+ baseUrl = "https://stow.sh",
44
+ customDomains,
45
+ defaultQuality = 75,
46
+ defaultFormat,
47
+ proxySlug,
48
+ fit,
49
+ gravity,
50
+ aspectRatio
51
+ } = config;
52
+ const customDomainSet = new Set(customDomains ?? []);
53
+ return function stowLoader2({
54
+ src,
55
+ width,
56
+ quality
57
+ }) {
58
+ const resolvedQuality = quality || defaultQuality;
59
+ const paramConfig = { defaultFormat, fit, gravity, aspectRatio };
60
+ const isAbsoluteUrl = src.startsWith("http://") || src.startsWith("https://");
61
+ if (customDomainSet.size > 0 && isAbsoluteUrl) {
62
+ const url = new URL(src);
63
+ if (customDomainSet.has(url.hostname)) {
64
+ const params = buildTransformParams(width, resolvedQuality, paramConfig);
65
+ return `${src}${src.includes("?") ? "&" : "?"}${params.toString()}`;
66
+ }
67
+ }
68
+ if (isStowUrl(src)) {
69
+ const params = buildTransformParams(width, resolvedQuality, paramConfig);
70
+ return `${src}${src.includes("?") ? "&" : "?"}${params.toString()}`;
71
+ }
72
+ if (src.startsWith(baseUrl) || src.startsWith("/files/")) {
73
+ const params = buildTransformParams(width, resolvedQuality, paramConfig);
74
+ return transformStowUrl(src, baseUrl, params);
75
+ }
76
+ if (proxySlug && (src.startsWith("http://") || src.startsWith("https://"))) {
77
+ const params = new URLSearchParams();
78
+ params.set("url", src);
79
+ params.set("w", width.toString());
80
+ if (aspectRatio) {
81
+ params.set("h", Math.round(width / aspectRatio).toString());
82
+ }
83
+ params.set("q", resolvedQuality.toString());
84
+ if (fit) {
85
+ params.set("fit", fit);
86
+ }
87
+ if (gravity) {
88
+ params.set("gravity", gravity);
89
+ }
90
+ if (defaultFormat) {
91
+ params.set("f", defaultFormat);
92
+ }
93
+ return `https://proxy.stow.sh/${proxySlug}/?${params.toString()}`;
94
+ }
95
+ return src;
96
+ };
97
+ }
98
+ var stowLoader = createStowLoader();
99
+
100
+ export {
101
+ createStowLoader,
102
+ stowLoader
103
+ };
@@ -0,0 +1,107 @@
1
+ // src/image-loader.ts
2
+ var STOW_DOMAIN_PATTERN = /\.stow\.sh$/;
3
+ function isStowUrl(src) {
4
+ try {
5
+ const url = new URL(src);
6
+ return STOW_DOMAIN_PATTERN.test(url.hostname);
7
+ } catch {
8
+ return false;
9
+ }
10
+ }
11
+ function buildTransformParams(width, quality, config) {
12
+ const params = new URLSearchParams();
13
+ params.set("w", width.toString());
14
+ if (config.aspectRatio) {
15
+ params.set("h", Math.round(width / config.aspectRatio).toString());
16
+ }
17
+ params.set("q", quality.toString());
18
+ if (config.fit) {
19
+ params.set("fit", config.fit);
20
+ }
21
+ if (config.gravity) {
22
+ params.set("gravity", config.gravity);
23
+ }
24
+ if (config.defaultFormat) {
25
+ params.set("f", config.defaultFormat);
26
+ }
27
+ return params;
28
+ }
29
+ function transformStowUrl(src, baseUrl, params) {
30
+ const url = new URL(src, baseUrl);
31
+ const pathname = url.pathname;
32
+ if (pathname.startsWith("/files/")) {
33
+ const transformPath = pathname.replace("/files/", "/transform/");
34
+ return `${baseUrl}${transformPath}?${params.toString()}`;
35
+ }
36
+ if (pathname.startsWith("/transform/")) {
37
+ return `${baseUrl}${pathname}?${params.toString()}`;
38
+ }
39
+ return `${src}${src.includes("?") ? "&" : "?"}${params.toString()}`;
40
+ }
41
+ function createStowLoader(config = {}) {
42
+ const {
43
+ baseUrl = "https://stow.sh",
44
+ customDomains,
45
+ defaultQuality = 75,
46
+ defaultFormat,
47
+ proxySlug,
48
+ fit,
49
+ gravity,
50
+ aspectRatio
51
+ } = config;
52
+ const customDomainSet = new Set(customDomains ?? []);
53
+ return function stowLoader2({
54
+ src,
55
+ width,
56
+ quality
57
+ }) {
58
+ const resolvedQuality = quality || defaultQuality;
59
+ const paramConfig = { defaultFormat, fit, gravity, aspectRatio };
60
+ const isAbsoluteUrl = src.startsWith("http://") || src.startsWith("https://");
61
+ if (customDomainSet.size > 0 && isAbsoluteUrl) {
62
+ const url = new URL(src);
63
+ if (customDomainSet.has(url.hostname)) {
64
+ const params = buildTransformParams(
65
+ width,
66
+ resolvedQuality,
67
+ paramConfig
68
+ );
69
+ return `${src}${src.includes("?") ? "&" : "?"}${params.toString()}`;
70
+ }
71
+ }
72
+ if (isStowUrl(src)) {
73
+ const params = buildTransformParams(width, resolvedQuality, paramConfig);
74
+ return `${src}${src.includes("?") ? "&" : "?"}${params.toString()}`;
75
+ }
76
+ if (src.startsWith(baseUrl) || src.startsWith("/files/")) {
77
+ const params = buildTransformParams(width, resolvedQuality, paramConfig);
78
+ return transformStowUrl(src, baseUrl, params);
79
+ }
80
+ if (proxySlug && (src.startsWith("http://") || src.startsWith("https://"))) {
81
+ const params = new URLSearchParams();
82
+ params.set("url", src);
83
+ params.set("w", width.toString());
84
+ if (aspectRatio) {
85
+ params.set("h", Math.round(width / aspectRatio).toString());
86
+ }
87
+ params.set("q", resolvedQuality.toString());
88
+ if (fit) {
89
+ params.set("fit", fit);
90
+ }
91
+ if (gravity) {
92
+ params.set("gravity", gravity);
93
+ }
94
+ if (defaultFormat) {
95
+ params.set("f", defaultFormat);
96
+ }
97
+ return `https://proxy.stow.sh/${proxySlug}/?${params.toString()}`;
98
+ }
99
+ return src;
100
+ };
101
+ }
102
+ var stowLoader = createStowLoader();
103
+
104
+ export {
105
+ createStowLoader,
106
+ stowLoader
107
+ };
@@ -57,6 +57,10 @@ interface StowLoaderConfig {
57
57
  * @default "https://stow.sh"
58
58
  */
59
59
  baseUrl?: string;
60
+ /**
61
+ * Additional hostnames to treat as Stow delivery URLs.
62
+ */
63
+ customDomains?: string[];
60
64
  /**
61
65
  * Default output format.
62
66
  */
@@ -57,6 +57,10 @@ interface StowLoaderConfig {
57
57
  * @default "https://stow.sh"
58
58
  */
59
59
  baseUrl?: string;
60
+ /**
61
+ * Additional hostnames to treat as Stow delivery URLs.
62
+ */
63
+ customDomains?: string[];
60
64
  /**
61
65
  * Default output format.
62
66
  */
@@ -66,6 +66,7 @@ function transformStowUrl(src, baseUrl, params) {
66
66
  function createStowLoader(config = {}) {
67
67
  const {
68
68
  baseUrl = "https://stow.sh",
69
+ customDomains,
69
70
  defaultQuality = 75,
70
71
  defaultFormat,
71
72
  proxySlug,
@@ -73,6 +74,7 @@ function createStowLoader(config = {}) {
73
74
  gravity,
74
75
  aspectRatio
75
76
  } = config;
77
+ const customDomainSet = new Set(customDomains ?? []);
76
78
  return function stowLoader2({
77
79
  src,
78
80
  width,
@@ -80,6 +82,18 @@ function createStowLoader(config = {}) {
80
82
  }) {
81
83
  const resolvedQuality = quality || defaultQuality;
82
84
  const paramConfig = { defaultFormat, fit, gravity, aspectRatio };
85
+ const isAbsoluteUrl = src.startsWith("http://") || src.startsWith("https://");
86
+ if (customDomainSet.size > 0 && isAbsoluteUrl) {
87
+ const url = new URL(src);
88
+ if (customDomainSet.has(url.hostname)) {
89
+ const params = buildTransformParams(
90
+ width,
91
+ resolvedQuality,
92
+ paramConfig
93
+ );
94
+ return `${src}${src.includes("?") ? "&" : "?"}${params.toString()}`;
95
+ }
96
+ }
83
97
  if (isStowUrl(src)) {
84
98
  const params = buildTransformParams(width, resolvedQuality, paramConfig);
85
99
  return `${src}${src.includes("?") ? "&" : "?"}${params.toString()}`;
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  createStowLoader,
3
3
  stowLoader
4
- } from "./chunk-WN3NWAEN.mjs";
4
+ } from "./chunk-XJA64ZFQ.mjs";
5
5
  export {
6
6
  createStowLoader,
7
7
  stowLoader
package/dist/index.js CHANGED
@@ -73,6 +73,7 @@ function transformStowUrl(src, baseUrl, params) {
73
73
  function createStowLoader(config = {}) {
74
74
  const {
75
75
  baseUrl = "https://stow.sh",
76
+ customDomains,
76
77
  defaultQuality = 75,
77
78
  defaultFormat,
78
79
  proxySlug,
@@ -80,6 +81,7 @@ function createStowLoader(config = {}) {
80
81
  gravity,
81
82
  aspectRatio
82
83
  } = config;
84
+ const customDomainSet = new Set(customDomains ?? []);
83
85
  return function stowLoader2({
84
86
  src,
85
87
  width,
@@ -87,6 +89,18 @@ function createStowLoader(config = {}) {
87
89
  }) {
88
90
  const resolvedQuality = quality || defaultQuality;
89
91
  const paramConfig = { defaultFormat, fit, gravity, aspectRatio };
92
+ const isAbsoluteUrl = src.startsWith("http://") || src.startsWith("https://");
93
+ if (customDomainSet.size > 0 && isAbsoluteUrl) {
94
+ const url = new URL(src);
95
+ if (customDomainSet.has(url.hostname)) {
96
+ const params = buildTransformParams(
97
+ width,
98
+ resolvedQuality,
99
+ paramConfig
100
+ );
101
+ return `${src}${src.includes("?") ? "&" : "?"}${params.toString()}`;
102
+ }
103
+ }
90
104
  if (isStowUrl(src)) {
91
105
  const params = buildTransformParams(width, resolvedQuality, paramConfig);
92
106
  return `${src}${src.includes("?") ? "&" : "?"}${params.toString()}`;
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  createStowLoader,
3
3
  stowLoader
4
- } from "./chunk-WN3NWAEN.mjs";
4
+ } from "./chunk-XJA64ZFQ.mjs";
5
5
 
6
6
  // src/index.ts
7
7
  import { NextResponse } from "next/server";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@howells/stow-next",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Next.js integration for Stow file storage",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -40,11 +40,11 @@
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/react": "19.2.14",
43
- "next": "16.1.6",
43
+ "next": "16.1.7",
44
44
  "react": "^19.2.4",
45
45
  "tsup": "^8.5.1",
46
46
  "typescript": "^5.9.3",
47
- "vitest": "^4.0.18",
47
+ "vitest": "^4.1.0",
48
48
  "@stow/typescript-config": "0.0.0"
49
49
  },
50
50
  "scripts": {