@nuxt/scripts 1.0.0-rc.6 → 1.0.0-rc.8
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.
- package/bin/cli.mjs +2 -0
- package/dist/cli.d.mts +2 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.mjs +50 -0
- package/dist/devtools-client/200.html +1 -1
- package/dist/devtools-client/404.html +1 -1
- package/dist/devtools-client/_nuxt/{B7jHEBMT.js → B4uHpJPz.js} +1 -1
- package/dist/devtools-client/_nuxt/{BaiqmiV1.js → BBS9G2Kb.js} +1 -1
- package/dist/devtools-client/_nuxt/{qfgRjj7S.js → CQR4zIAm.js} +1 -1
- package/dist/devtools-client/_nuxt/{Chi3DhDl.js → Cxq4HLPL.js} +1 -1
- package/dist/devtools-client/_nuxt/{CLjOeO0G.js → DCBsJT4N.js} +1 -1
- package/dist/devtools-client/_nuxt/{HLYrIHDq.js → DTxy5P8N.js} +18 -18
- package/dist/devtools-client/_nuxt/{BjmZlwuw.js → DvZScWzI.js} +1 -1
- package/dist/devtools-client/_nuxt/builds/latest.json +1 -1
- package/dist/devtools-client/_nuxt/builds/meta/484f72b9-a019-4127-8ab9-c10e92624094.json +1 -0
- package/dist/devtools-client/_nuxt/error-404.d44aGwWI.css +1 -0
- package/dist/devtools-client/_nuxt/error-500.NthMfIEt.css +1 -0
- package/dist/devtools-client/docs/index.html +1 -1
- package/dist/devtools-client/first-party/index.html +1 -1
- package/dist/devtools-client/index.html +1 -1
- package/dist/devtools-client/registry/index.html +1 -1
- package/dist/module.d.mts +51 -2
- package/dist/module.d.ts +51 -2
- package/dist/module.json +1 -1
- package/dist/module.mjs +80 -9
- package/dist/registry.mjs +10 -10
- package/dist/runtime/server/bluesky-embed.js +3 -2
- package/dist/runtime/server/google-maps-geocode-proxy.js +4 -3
- package/dist/runtime/server/google-static-maps-proxy.js +4 -3
- package/dist/runtime/server/gravatar-proxy.js +4 -3
- package/dist/runtime/server/instagram-embed.d.ts +1 -16
- package/dist/runtime/server/instagram-embed.js +5 -116
- package/dist/runtime/server/proxy-handler.js +1 -2
- package/dist/runtime/server/utils/image-proxy.js +3 -2
- package/dist/runtime/server/utils/instagram-embed.d.ts +16 -0
- package/dist/runtime/server/utils/instagram-embed.js +152 -0
- package/dist/runtime/server/utils/sign.d.ts +109 -0
- package/dist/runtime/server/utils/sign.js +88 -0
- package/dist/runtime/server/utils/withSigning.d.ts +23 -0
- package/dist/runtime/server/utils/withSigning.js +18 -0
- package/dist/runtime/server/x-embed.js +3 -2
- package/dist/runtime/types.d.ts +9 -1
- package/dist/types.d.mts +2 -2
- package/package.json +6 -2
- package/dist/devtools-client/_nuxt/builds/meta/640f0a39-e659-4a31-8b8d-adbd9af52f1e.json +0 -1
- package/dist/devtools-client/_nuxt/error-404.Dwj0Wlzm.css +0 -1
- package/dist/devtools-client/_nuxt/error-500.B4wHUYBa.css +0 -1
|
@@ -1,40 +1,11 @@
|
|
|
1
1
|
import { createError, defineEventHandler, getQuery, setHeader } from "h3";
|
|
2
2
|
import { $fetch } from "ofetch";
|
|
3
3
|
import { ELEMENT_NODE, parse, renderSync, TEXT_NODE, walkSync } from "ultrahtml";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export
|
|
7
|
-
export const STATIC_CDN_RE = /https:\/\/static\.cdninstagram\.com[^"'\s),]+/g;
|
|
8
|
-
export const LOOKASIDE_RE = /https:\/\/lookaside\.instagram\.com[^"'\s),]+/g;
|
|
9
|
-
export const INSTAGRAM_IMAGE_HOSTS = ["scontent.cdninstagram.com", "lookaside.instagram.com"];
|
|
10
|
-
export const INSTAGRAM_ASSET_HOST = "static.cdninstagram.com";
|
|
11
|
-
const CHARSET_RE = /@charset\s[^;]+;/gi;
|
|
12
|
-
const IMPORT_RE = /@import\s[^;]+;/gi;
|
|
13
|
-
const WHITESPACE_RE = /\s/;
|
|
4
|
+
import { rewriteUrl, rewriteUrlsInText, RSRC_RE, scopeCss } from "./utils/instagram-embed.js";
|
|
5
|
+
import { withSigning } from "./utils/withSigning.js";
|
|
6
|
+
export { proxyAssetUrl, proxyImageUrl, rewriteUrl, rewriteUrlsInText, scopeCss } from "./utils/instagram-embed.js";
|
|
14
7
|
const EMBED_INSTAGRAM_SUFFIX_RE = /\/embed\/instagram$/;
|
|
15
|
-
const AT_RULE_NAME_RE = /@([\w-]+)/;
|
|
16
|
-
const MULTI_SPACE_RE = /\s+/g;
|
|
17
8
|
const SRCSET_SPLIT_RE = /\s+/;
|
|
18
|
-
export function proxyImageUrl(url, prefix = "/_scripts") {
|
|
19
|
-
return `${prefix}/embed/instagram-image?url=${encodeURIComponent(url.replace(AMP_RE, "&"))}`;
|
|
20
|
-
}
|
|
21
|
-
export function proxyAssetUrl(url, prefix = "/_scripts") {
|
|
22
|
-
return `${prefix}/embed/instagram-asset?url=${encodeURIComponent(url.replace(AMP_RE, "&"))}`;
|
|
23
|
-
}
|
|
24
|
-
export function rewriteUrl(url, prefix = "/_scripts") {
|
|
25
|
-
try {
|
|
26
|
-
const parsed = new URL(url);
|
|
27
|
-
if (parsed.hostname === INSTAGRAM_ASSET_HOST)
|
|
28
|
-
return proxyAssetUrl(url, prefix);
|
|
29
|
-
if (INSTAGRAM_IMAGE_HOSTS.some((h) => parsed.hostname === h || parsed.hostname.endsWith(`.cdninstagram.com`)))
|
|
30
|
-
return proxyImageUrl(url, prefix);
|
|
31
|
-
} catch {
|
|
32
|
-
}
|
|
33
|
-
return url;
|
|
34
|
-
}
|
|
35
|
-
export function rewriteUrlsInText(text, prefix = "/_scripts") {
|
|
36
|
-
return text.replace(SCONTENT_RE, (m) => proxyImageUrl(m, prefix)).replace(STATIC_CDN_RE, (m) => proxyAssetUrl(m, prefix)).replace(LOOKASIDE_RE, (m) => proxyImageUrl(m, prefix));
|
|
37
|
-
}
|
|
38
9
|
function removeNode(node) {
|
|
39
10
|
node.type = TEXT_NODE;
|
|
40
11
|
node.value = "";
|
|
@@ -42,89 +13,7 @@ function removeNode(node) {
|
|
|
42
13
|
node.attributes = {};
|
|
43
14
|
node.children = [];
|
|
44
15
|
}
|
|
45
|
-
export
|
|
46
|
-
let result = css.replace(CHARSET_RE, "");
|
|
47
|
-
result = result.replace(IMPORT_RE, "");
|
|
48
|
-
return processRules(result, scopeSelector);
|
|
49
|
-
}
|
|
50
|
-
function processRules(css, scopeSelector) {
|
|
51
|
-
const output = [];
|
|
52
|
-
let i = 0;
|
|
53
|
-
while (i < css.length) {
|
|
54
|
-
while (i < css.length && WHITESPACE_RE.test(css[i])) i++;
|
|
55
|
-
if (i >= css.length)
|
|
56
|
-
break;
|
|
57
|
-
if (css[i] === "@") {
|
|
58
|
-
const atRule = extractAtRule(css, i);
|
|
59
|
-
if (atRule) {
|
|
60
|
-
const atName = atRule.content.match(AT_RULE_NAME_RE)?.[1]?.toLowerCase();
|
|
61
|
-
if (atName === "media" || atName === "supports" || atName === "layer") {
|
|
62
|
-
const braceStart = atRule.content.indexOf("{");
|
|
63
|
-
const innerCss = atRule.content.slice(braceStart + 1, -1);
|
|
64
|
-
const scopedInner = processRules(innerCss, scopeSelector);
|
|
65
|
-
output.push(`${atRule.content.slice(0, braceStart + 1) + scopedInner}}`);
|
|
66
|
-
} else if (atName === "keyframes" || atName === "-webkit-keyframes" || atName === "font-face") {
|
|
67
|
-
output.push(atRule.content);
|
|
68
|
-
}
|
|
69
|
-
i = atRule.end;
|
|
70
|
-
continue;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
const bracePos = css.indexOf("{", i);
|
|
74
|
-
if (bracePos === -1)
|
|
75
|
-
break;
|
|
76
|
-
const selector = css.slice(i, bracePos).trim();
|
|
77
|
-
const block = extractBlock(css, bracePos);
|
|
78
|
-
if (!block)
|
|
79
|
-
break;
|
|
80
|
-
i = block.end;
|
|
81
|
-
if (!selector)
|
|
82
|
-
continue;
|
|
83
|
-
const selectors = selector.split(",").map((s) => s.trim());
|
|
84
|
-
const filteredSelectors = selectors.filter((s) => {
|
|
85
|
-
const normalized = s.replace(MULTI_SPACE_RE, " ").trim().toLowerCase();
|
|
86
|
-
return normalized !== ":root" && normalized !== "html" && normalized !== "body" && !normalized.startsWith(":root ") && !normalized.startsWith("html ") && !normalized.startsWith("body ") && normalized !== "html, body";
|
|
87
|
-
});
|
|
88
|
-
if (filteredSelectors.length === 0)
|
|
89
|
-
continue;
|
|
90
|
-
const scopedSelectors = filteredSelectors.map((s) => {
|
|
91
|
-
return `${scopeSelector} ${s}`;
|
|
92
|
-
});
|
|
93
|
-
output.push(`${scopedSelectors.join(", ")} ${block.content}`);
|
|
94
|
-
}
|
|
95
|
-
return output.join("\n");
|
|
96
|
-
}
|
|
97
|
-
function extractAtRule(css, start) {
|
|
98
|
-
const bracePos = css.indexOf("{", start);
|
|
99
|
-
const semiPos = css.indexOf(";", start);
|
|
100
|
-
if (semiPos !== -1 && (bracePos === -1 || semiPos < bracePos)) {
|
|
101
|
-
return { content: css.slice(start, semiPos + 1), end: semiPos + 1 };
|
|
102
|
-
}
|
|
103
|
-
if (bracePos === -1)
|
|
104
|
-
return null;
|
|
105
|
-
const block = extractBlock(css, bracePos);
|
|
106
|
-
if (!block)
|
|
107
|
-
return null;
|
|
108
|
-
return {
|
|
109
|
-
content: css.slice(start, bracePos) + block.content,
|
|
110
|
-
end: block.end
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
function extractBlock(css, openBrace) {
|
|
114
|
-
let depth = 0;
|
|
115
|
-
for (let j = openBrace; j < css.length; j++) {
|
|
116
|
-
if (css[j] === "{") {
|
|
117
|
-
depth++;
|
|
118
|
-
} else if (css[j] === "}") {
|
|
119
|
-
depth--;
|
|
120
|
-
if (depth === 0) {
|
|
121
|
-
return { content: css.slice(openBrace, j + 1), end: j + 1 };
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
export default defineEventHandler(async (event) => {
|
|
16
|
+
export default withSigning(defineEventHandler(async (event) => {
|
|
128
17
|
const handlerPath = event.path?.split("?")[0] || "";
|
|
129
18
|
const prefix = handlerPath.replace(EMBED_INSTAGRAM_SUFFIX_RE, "") || "/_scripts";
|
|
130
19
|
const query = getQuery(event);
|
|
@@ -229,4 +118,4 @@ ${combinedCss}</style>${bodyHtml}</div>`;
|
|
|
229
118
|
setHeader(event, "Content-Type", "text/html");
|
|
230
119
|
setHeader(event, "Cache-Control", "public, max-age=600, s-maxage=600");
|
|
231
120
|
return result;
|
|
232
|
-
});
|
|
121
|
+
}));
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { useRuntimeConfig } from "#imports";
|
|
2
1
|
import { createError, defineEventHandler, getHeaders, getQuery, getRequestIP, getRequestWebStream, readBody, setResponseHeader } from "h3";
|
|
3
|
-
import { useNitroApp } from "nitropack/runtime";
|
|
2
|
+
import { useNitroApp, useRuntimeConfig } from "nitropack/runtime";
|
|
4
3
|
import {
|
|
5
4
|
anonymizeIP,
|
|
6
5
|
mergePrivacy,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createError, defineEventHandler, getQuery, setHeader } from "h3";
|
|
2
2
|
import { $fetch } from "ofetch";
|
|
3
|
+
import { withSigning } from "./withSigning.js";
|
|
3
4
|
const AMP_RE = /&/g;
|
|
4
5
|
export function createImageProxyHandler(config) {
|
|
5
6
|
const {
|
|
@@ -10,7 +11,7 @@ export function createImageProxyHandler(config) {
|
|
|
10
11
|
followRedirects = true,
|
|
11
12
|
decodeAmpersands = false
|
|
12
13
|
} = config;
|
|
13
|
-
return defineEventHandler(async (event) => {
|
|
14
|
+
return withSigning(defineEventHandler(async (event) => {
|
|
14
15
|
const query = getQuery(event);
|
|
15
16
|
let url = query.url;
|
|
16
17
|
if (decodeAmpersands && url)
|
|
@@ -66,5 +67,5 @@ export function createImageProxyHandler(config) {
|
|
|
66
67
|
setHeader(event, "Content-Type", response.headers.get("content-type") || contentType);
|
|
67
68
|
setHeader(event, "Cache-Control", `public, max-age=${cacheMaxAge}, s-maxage=${cacheMaxAge}`);
|
|
68
69
|
return response._data;
|
|
69
|
-
});
|
|
70
|
+
}));
|
|
70
71
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const RSRC_RE: RegExp;
|
|
2
|
+
export declare const AMP_RE: RegExp;
|
|
3
|
+
export declare const SCONTENT_RE: RegExp;
|
|
4
|
+
export declare const STATIC_CDN_RE: RegExp;
|
|
5
|
+
export declare const LOOKASIDE_RE: RegExp;
|
|
6
|
+
export declare const INSTAGRAM_IMAGE_HOSTS: string[];
|
|
7
|
+
export declare const INSTAGRAM_ASSET_HOST = "static.cdninstagram.com";
|
|
8
|
+
export declare function proxyImageUrl(url: string, prefix?: string): string;
|
|
9
|
+
export declare function proxyAssetUrl(url: string, prefix?: string): string;
|
|
10
|
+
export declare function rewriteUrl(url: string, prefix?: string): string;
|
|
11
|
+
export declare function rewriteUrlsInText(text: string, prefix?: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* Scope CSS rules under a parent selector and strip global/page-level rules.
|
|
14
|
+
* Removes :root, html, body selectors and @charset/@import at-rules.
|
|
15
|
+
*/
|
|
16
|
+
export declare function scopeCss(css: string, scopeSelector: string): string;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
export const RSRC_RE = /url\(\/rsrc\.php([^)]+)\)/g;
|
|
2
|
+
export const AMP_RE = /&/g;
|
|
3
|
+
export const SCONTENT_RE = /https:\/\/scontent[^"'\s),]+\.cdninstagram\.com[^"'\s),]+/g;
|
|
4
|
+
export const STATIC_CDN_RE = /https:\/\/static\.cdninstagram\.com[^"'\s),]+/g;
|
|
5
|
+
export const LOOKASIDE_RE = /https:\/\/lookaside\.instagram\.com[^"'\s),]+/g;
|
|
6
|
+
export const INSTAGRAM_IMAGE_HOSTS = ["scontent.cdninstagram.com", "lookaside.instagram.com"];
|
|
7
|
+
export const INSTAGRAM_ASSET_HOST = "static.cdninstagram.com";
|
|
8
|
+
const CHARSET_RE = /@charset\s[^;]+;/gi;
|
|
9
|
+
const IMPORT_RE = /@import\s[^;]+;/gi;
|
|
10
|
+
const WHITESPACE_RE = /\s/;
|
|
11
|
+
const AT_RULE_NAME_RE = /@([\w-]+)/;
|
|
12
|
+
const MULTI_SPACE_RE = /\s+/g;
|
|
13
|
+
export function proxyImageUrl(url, prefix = "/_scripts") {
|
|
14
|
+
return `${prefix}/embed/instagram-image?url=${encodeURIComponent(url.replace(AMP_RE, "&"))}`;
|
|
15
|
+
}
|
|
16
|
+
export function proxyAssetUrl(url, prefix = "/_scripts") {
|
|
17
|
+
return `${prefix}/embed/instagram-asset?url=${encodeURIComponent(url.replace(AMP_RE, "&"))}`;
|
|
18
|
+
}
|
|
19
|
+
export function rewriteUrl(url, prefix = "/_scripts") {
|
|
20
|
+
try {
|
|
21
|
+
const parsed = new URL(url);
|
|
22
|
+
if (parsed.hostname === INSTAGRAM_ASSET_HOST)
|
|
23
|
+
return proxyAssetUrl(url, prefix);
|
|
24
|
+
if (INSTAGRAM_IMAGE_HOSTS.some((h) => parsed.hostname === h || parsed.hostname.endsWith(`.cdninstagram.com`)))
|
|
25
|
+
return proxyImageUrl(url, prefix);
|
|
26
|
+
} catch {
|
|
27
|
+
}
|
|
28
|
+
return url;
|
|
29
|
+
}
|
|
30
|
+
export function rewriteUrlsInText(text, prefix = "/_scripts") {
|
|
31
|
+
return text.replace(SCONTENT_RE, (m) => proxyImageUrl(m, prefix)).replace(STATIC_CDN_RE, (m) => proxyAssetUrl(m, prefix)).replace(LOOKASIDE_RE, (m) => proxyImageUrl(m, prefix));
|
|
32
|
+
}
|
|
33
|
+
export function scopeCss(css, scopeSelector) {
|
|
34
|
+
let result = css.replace(CHARSET_RE, "");
|
|
35
|
+
result = result.replace(IMPORT_RE, "");
|
|
36
|
+
return processRules(result, scopeSelector);
|
|
37
|
+
}
|
|
38
|
+
function processRules(css, scopeSelector) {
|
|
39
|
+
const output = [];
|
|
40
|
+
let i = 0;
|
|
41
|
+
while (i < css.length) {
|
|
42
|
+
while (i < css.length && WHITESPACE_RE.test(css[i])) i++;
|
|
43
|
+
if (i >= css.length)
|
|
44
|
+
break;
|
|
45
|
+
if (css[i] === "@") {
|
|
46
|
+
const atRule = extractAtRule(css, i);
|
|
47
|
+
if (atRule) {
|
|
48
|
+
const atName = atRule.content.match(AT_RULE_NAME_RE)?.[1]?.toLowerCase();
|
|
49
|
+
if (atName === "media" || atName === "supports" || atName === "layer") {
|
|
50
|
+
const braceStart = atRule.content.indexOf("{");
|
|
51
|
+
if (braceStart === -1) {
|
|
52
|
+
output.push(atRule.content);
|
|
53
|
+
} else {
|
|
54
|
+
const innerCss = atRule.content.slice(braceStart + 1, -1);
|
|
55
|
+
const scopedInner = processRules(innerCss, scopeSelector);
|
|
56
|
+
output.push(`${atRule.content.slice(0, braceStart + 1)}${scopedInner}}`);
|
|
57
|
+
}
|
|
58
|
+
} else if (atName === "keyframes" || atName === "-webkit-keyframes" || atName === "font-face") {
|
|
59
|
+
output.push(atRule.content);
|
|
60
|
+
}
|
|
61
|
+
i = atRule.end;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const bracePos = css.indexOf("{", i);
|
|
66
|
+
if (bracePos === -1)
|
|
67
|
+
break;
|
|
68
|
+
const selector = css.slice(i, bracePos).trim();
|
|
69
|
+
const block = extractBlock(css, bracePos);
|
|
70
|
+
if (!block)
|
|
71
|
+
break;
|
|
72
|
+
i = block.end;
|
|
73
|
+
if (!selector)
|
|
74
|
+
continue;
|
|
75
|
+
const selectors = splitTopLevel(selector, ",").map((s) => s.trim());
|
|
76
|
+
const filteredSelectors = selectors.filter((s) => {
|
|
77
|
+
const normalized = s.replace(MULTI_SPACE_RE, " ").trim().toLowerCase();
|
|
78
|
+
return normalized !== ":root" && normalized !== "html" && normalized !== "body" && !normalized.startsWith(":root ") && !normalized.startsWith("html ") && !normalized.startsWith("body ") && normalized !== "html, body";
|
|
79
|
+
});
|
|
80
|
+
if (filteredSelectors.length === 0)
|
|
81
|
+
continue;
|
|
82
|
+
const scopedSelectors = filteredSelectors.map((s) => `${scopeSelector} ${s}`);
|
|
83
|
+
output.push(`${scopedSelectors.join(", ")} ${block.content}`);
|
|
84
|
+
}
|
|
85
|
+
return output.join("\n");
|
|
86
|
+
}
|
|
87
|
+
function extractAtRule(css, start) {
|
|
88
|
+
const bracePos = css.indexOf("{", start);
|
|
89
|
+
const semiPos = css.indexOf(";", start);
|
|
90
|
+
if (semiPos !== -1 && (bracePos === -1 || semiPos < bracePos)) {
|
|
91
|
+
return { content: css.slice(start, semiPos + 1), end: semiPos + 1 };
|
|
92
|
+
}
|
|
93
|
+
if (bracePos === -1)
|
|
94
|
+
return null;
|
|
95
|
+
const block = extractBlock(css, bracePos);
|
|
96
|
+
if (!block)
|
|
97
|
+
return null;
|
|
98
|
+
return {
|
|
99
|
+
content: css.slice(start, bracePos) + block.content,
|
|
100
|
+
end: block.end
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function splitTopLevel(input, separator) {
|
|
104
|
+
const parts = [];
|
|
105
|
+
let depth = 0;
|
|
106
|
+
let quote = null;
|
|
107
|
+
let start = 0;
|
|
108
|
+
for (let i = 0; i < input.length; i++) {
|
|
109
|
+
const ch = input[i];
|
|
110
|
+
if (quote) {
|
|
111
|
+
if (ch === "\\") {
|
|
112
|
+
i++;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (ch === quote)
|
|
116
|
+
quote = null;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (ch === '"' || ch === "'") {
|
|
120
|
+
quote = ch;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (ch === "(" || ch === "[") {
|
|
124
|
+
depth++;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (ch === ")" || ch === "]") {
|
|
128
|
+
depth--;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (ch === separator && depth === 0) {
|
|
132
|
+
parts.push(input.slice(start, i));
|
|
133
|
+
start = i + 1;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
parts.push(input.slice(start));
|
|
137
|
+
return parts;
|
|
138
|
+
}
|
|
139
|
+
function extractBlock(css, openBrace) {
|
|
140
|
+
let depth = 0;
|
|
141
|
+
for (let j = openBrace; j < css.length; j++) {
|
|
142
|
+
if (css[j] === "{") {
|
|
143
|
+
depth++;
|
|
144
|
+
} else if (css[j] === "}") {
|
|
145
|
+
depth--;
|
|
146
|
+
if (depth === 0) {
|
|
147
|
+
return { content: css.slice(openBrace, j + 1), end: j + 1 };
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HMAC URL signing for proxy endpoints.
|
|
3
|
+
*
|
|
4
|
+
* ## Why
|
|
5
|
+
*
|
|
6
|
+
* Proxy endpoints like `/_scripts/proxy/google-static-maps` inject a server-side
|
|
7
|
+
* API key and forward requests to third-party services. Without signing, anyone
|
|
8
|
+
* can call these endpoints with arbitrary parameters and burn the site owner's
|
|
9
|
+
* API quota. Signing ensures only URLs generated server-side (during SSR/prerender
|
|
10
|
+
* or via the `/_scripts/sign` endpoint) are accepted.
|
|
11
|
+
*
|
|
12
|
+
* ## How
|
|
13
|
+
*
|
|
14
|
+
* 1. The module stores a deterministic secret in `runtimeConfig.nuxt-scripts.proxySecret`
|
|
15
|
+
* (env: `NUXT_SCRIPTS_PROXY_SECRET`).
|
|
16
|
+
* 2. URLs are canonicalized (sort query keys, strip `sig`) and signed with HMAC-SHA256.
|
|
17
|
+
* 3. The first 16 hex chars (64 bits) of the digest is appended as `?sig=<hex>`.
|
|
18
|
+
* 4. Endpoints wrapped with `withSigning()` verify the sig against the current request.
|
|
19
|
+
*
|
|
20
|
+
* A 64-bit signature is enough to defeat brute force for this threat model
|
|
21
|
+
* (a billion guesses gives a ~5% hit rate at 2^64). Longer signatures bloat
|
|
22
|
+
* prerendered HTML for no practical gain.
|
|
23
|
+
*/
|
|
24
|
+
import type { H3Event } from 'h3';
|
|
25
|
+
/** Query param name for the signature. Chosen to be unlikely to collide with upstream APIs. */
|
|
26
|
+
export declare const SIG_PARAM = "sig";
|
|
27
|
+
/** Length of the hex signature (16 chars = 64 bits). */
|
|
28
|
+
export declare const SIG_LENGTH = 16;
|
|
29
|
+
/**
|
|
30
|
+
* Canonicalize a query object into a deterministic string suitable for HMAC input.
|
|
31
|
+
*
|
|
32
|
+
* Rules:
|
|
33
|
+
* - The `sig` param is stripped (it can't sign itself).
|
|
34
|
+
* - `undefined` and `null` values are skipped (mirrors `ufo.withQuery`).
|
|
35
|
+
* - Keys are sorted alphabetically so order-independent reconstruction works.
|
|
36
|
+
* - Arrays expand to repeated keys (e.g. `markers=a&markers=b`), matching how
|
|
37
|
+
* `ufo.withQuery` serializes array-valued params.
|
|
38
|
+
* - Objects are JSON-stringified (rare, but consistent with `ufo.withQuery`).
|
|
39
|
+
* - Encoding uses `encodeURIComponent` for both keys and values so the canonical
|
|
40
|
+
* form matches what shows up on the wire.
|
|
41
|
+
*
|
|
42
|
+
* The resulting string is stable across server/client and different JS runtimes
|
|
43
|
+
* because it does not depend on `URLSearchParams` insertion order.
|
|
44
|
+
*/
|
|
45
|
+
export declare function canonicalizeQuery(query: Record<string, unknown>): string;
|
|
46
|
+
/**
|
|
47
|
+
* Sign a path + query using HMAC-SHA256 and return the 16-char hex digest.
|
|
48
|
+
*
|
|
49
|
+
* The HMAC input is `${path}?${canonicalQuery}` so that the same query signed
|
|
50
|
+
* against a different endpoint yields a different signature (prevents cross-
|
|
51
|
+
* endpoint signature reuse).
|
|
52
|
+
*
|
|
53
|
+
* `path` should be the URL path without query string (e.g. `/_scripts/proxy/google-static-maps`).
|
|
54
|
+
* Callers should not include origin / host since the signing contract is path-relative.
|
|
55
|
+
*/
|
|
56
|
+
export declare function signProxyUrl(path: string, query: Record<string, unknown>, secret: string): string;
|
|
57
|
+
/**
|
|
58
|
+
* Build a fully-formed signed URL (path + query + sig).
|
|
59
|
+
*
|
|
60
|
+
* This is the primary helper for code paths that need to emit a proxy URL
|
|
61
|
+
* (SSR components, server-side URL rewriters like instagram-embed).
|
|
62
|
+
*/
|
|
63
|
+
export declare function buildSignedProxyUrl(path: string, query: Record<string, unknown>, secret: string): string;
|
|
64
|
+
/** Query param name for the page token. */
|
|
65
|
+
export declare const PAGE_TOKEN_PARAM = "_pt";
|
|
66
|
+
/** Query param name for the page token timestamp. */
|
|
67
|
+
export declare const PAGE_TOKEN_TS_PARAM = "_ts";
|
|
68
|
+
/** Default max age for page tokens in seconds (1 hour). */
|
|
69
|
+
export declare const PAGE_TOKEN_MAX_AGE = 3600;
|
|
70
|
+
/**
|
|
71
|
+
* Generate a page token that authorizes client-side proxy requests.
|
|
72
|
+
*
|
|
73
|
+
* Embedded in the SSR payload so the browser can attach it to reactive proxy
|
|
74
|
+
* URL updates without needing a `/sign` round-trip. The token is scoped to
|
|
75
|
+
* a timestamp and expires after `PAGE_TOKEN_MAX_AGE` seconds.
|
|
76
|
+
*
|
|
77
|
+
* Construction: first 16 hex chars of `HMAC(secret, "proxy-access:<timestamp>")`.
|
|
78
|
+
*/
|
|
79
|
+
export declare function generateProxyToken(secret: string, timestamp: number): string;
|
|
80
|
+
/**
|
|
81
|
+
* Verify a page token against the current time.
|
|
82
|
+
*
|
|
83
|
+
* Returns `true` if the token matches the HMAC for the given timestamp AND
|
|
84
|
+
* the timestamp is within `maxAge` seconds of `now`.
|
|
85
|
+
*/
|
|
86
|
+
export declare function verifyProxyToken(token: string, timestamp: number, secret: string, maxAge?: number, now?: number): boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Verify a request against either a URL signature or a page token.
|
|
89
|
+
*
|
|
90
|
+
* Two verification modes, checked in order:
|
|
91
|
+
*
|
|
92
|
+
* 1. **URL signature** (`sig` param): the exact URL was signed server-side
|
|
93
|
+
* during SSR/prerender. Locked to the specific path + query params.
|
|
94
|
+
*
|
|
95
|
+
* 2. **Page token** (`_pt` + `_ts` params): the client received a short-lived
|
|
96
|
+
* token during SSR and is making a reactive proxy request with new params.
|
|
97
|
+
* Valid for any params on the target path, but expires after `maxAge`.
|
|
98
|
+
*
|
|
99
|
+
* Returns `false` if neither mode validates.
|
|
100
|
+
*/
|
|
101
|
+
export declare function verifyProxyRequest(event: H3Event, secret: string, maxAge?: number): boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Constant-time string comparison.
|
|
104
|
+
*
|
|
105
|
+
* Both inputs are expected to be equal-length hex strings. The loop runs over
|
|
106
|
+
* the longer length so an early-exit on length mismatch doesn't leak the
|
|
107
|
+
* expected length (though both are fixed at `SIG_LENGTH` in practice).
|
|
108
|
+
*/
|
|
109
|
+
export declare function constantTimeEqual(a: string, b: string): boolean;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { createHmac } from "node:crypto";
|
|
2
|
+
import { getQuery } from "h3";
|
|
3
|
+
export const SIG_PARAM = "sig";
|
|
4
|
+
export const SIG_LENGTH = 16;
|
|
5
|
+
export function canonicalizeQuery(query) {
|
|
6
|
+
const keys = Object.keys(query).filter((k) => k !== SIG_PARAM && query[k] !== void 0 && query[k] !== null).sort();
|
|
7
|
+
const parts = [];
|
|
8
|
+
for (const key of keys) {
|
|
9
|
+
const value = query[key];
|
|
10
|
+
const encodedKey = encodeURIComponent(key);
|
|
11
|
+
if (Array.isArray(value)) {
|
|
12
|
+
for (const item of value) {
|
|
13
|
+
if (item === void 0 || item === null)
|
|
14
|
+
continue;
|
|
15
|
+
parts.push(`${encodedKey}=${encodeURIComponent(serializeValue(item))}`);
|
|
16
|
+
}
|
|
17
|
+
} else {
|
|
18
|
+
parts.push(`${encodedKey}=${encodeURIComponent(serializeValue(value))}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return parts.join("&");
|
|
22
|
+
}
|
|
23
|
+
function serializeValue(value) {
|
|
24
|
+
if (typeof value === "string")
|
|
25
|
+
return value;
|
|
26
|
+
if (typeof value === "object")
|
|
27
|
+
return JSON.stringify(value);
|
|
28
|
+
return String(value);
|
|
29
|
+
}
|
|
30
|
+
export function signProxyUrl(path, query, secret) {
|
|
31
|
+
const canonical = canonicalizeQuery(query);
|
|
32
|
+
const input = canonical ? `${path}?${canonical}` : path;
|
|
33
|
+
return createHmac("sha256", secret).update(input).digest("hex").slice(0, SIG_LENGTH);
|
|
34
|
+
}
|
|
35
|
+
export function buildSignedProxyUrl(path, query, secret) {
|
|
36
|
+
const sig = signProxyUrl(path, query, secret);
|
|
37
|
+
const canonical = canonicalizeQuery(query);
|
|
38
|
+
const queryString = canonical ? `${canonical}&${SIG_PARAM}=${sig}` : `${SIG_PARAM}=${sig}`;
|
|
39
|
+
return `${path}?${queryString}`;
|
|
40
|
+
}
|
|
41
|
+
export const PAGE_TOKEN_PARAM = "_pt";
|
|
42
|
+
export const PAGE_TOKEN_TS_PARAM = "_ts";
|
|
43
|
+
export const PAGE_TOKEN_MAX_AGE = 3600;
|
|
44
|
+
export function generateProxyToken(secret, timestamp) {
|
|
45
|
+
return createHmac("sha256", secret).update(`proxy-access:${timestamp}`).digest("hex").slice(0, SIG_LENGTH);
|
|
46
|
+
}
|
|
47
|
+
export function verifyProxyToken(token, timestamp, secret, maxAge = PAGE_TOKEN_MAX_AGE, now = Math.floor(Date.now() / 1e3)) {
|
|
48
|
+
if (!token || !secret || typeof timestamp !== "number")
|
|
49
|
+
return false;
|
|
50
|
+
if (token.length !== SIG_LENGTH)
|
|
51
|
+
return false;
|
|
52
|
+
const age = now - timestamp;
|
|
53
|
+
if (age > maxAge || age < -60)
|
|
54
|
+
return false;
|
|
55
|
+
const expected = generateProxyToken(secret, timestamp);
|
|
56
|
+
return constantTimeEqual(expected, token);
|
|
57
|
+
}
|
|
58
|
+
export function verifyProxyRequest(event, secret, maxAge) {
|
|
59
|
+
if (!secret)
|
|
60
|
+
return false;
|
|
61
|
+
const query = getQuery(event);
|
|
62
|
+
const rawSig = query[SIG_PARAM];
|
|
63
|
+
const sig = Array.isArray(rawSig) ? rawSig[0] : rawSig;
|
|
64
|
+
if (typeof sig === "string" && sig.length === SIG_LENGTH) {
|
|
65
|
+
const path = (event.path || "").split("?")[0] || "";
|
|
66
|
+
const expected = signProxyUrl(path, query, secret);
|
|
67
|
+
if (constantTimeEqual(expected, sig))
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
const rawToken = query[PAGE_TOKEN_PARAM];
|
|
71
|
+
const rawTs = query[PAGE_TOKEN_TS_PARAM];
|
|
72
|
+
const token = Array.isArray(rawToken) ? rawToken[0] : rawToken;
|
|
73
|
+
const ts = Array.isArray(rawTs) ? rawTs[0] : rawTs;
|
|
74
|
+
if (typeof token === "string" && ts !== void 0) {
|
|
75
|
+
const timestamp = Number(ts);
|
|
76
|
+
if (!Number.isNaN(timestamp))
|
|
77
|
+
return verifyProxyToken(token, timestamp, secret, maxAge);
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
export function constantTimeEqual(a, b) {
|
|
82
|
+
if (a.length !== b.length)
|
|
83
|
+
return false;
|
|
84
|
+
let diff = 0;
|
|
85
|
+
for (let i = 0; i < a.length; i++)
|
|
86
|
+
diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
87
|
+
return diff === 0;
|
|
88
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Middleware wrapper that enforces HMAC signature verification on a proxy handler.
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* ```ts
|
|
6
|
+
* export default withSigning(defineEventHandler(async (event) => {
|
|
7
|
+
* // ... handler logic
|
|
8
|
+
* }))
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* Behavior:
|
|
12
|
+
* - Reads `runtimeConfig.nuxt-scripts.proxySecret` (server-only).
|
|
13
|
+
* - If no secret is configured: passes through (signing not yet enabled).
|
|
14
|
+
* This allows shipping handler wiring before components emit signed URLs.
|
|
15
|
+
* Once `NUXT_SCRIPTS_PROXY_SECRET` is set, verification is enforced.
|
|
16
|
+
* - If a secret IS configured and the request's signature is invalid: 403.
|
|
17
|
+
* - Otherwise, delegates to the wrapped handler.
|
|
18
|
+
*
|
|
19
|
+
* The outer wrapper runs before any handler logic, so unauthorized requests
|
|
20
|
+
* never reach the upstream fetch and cannot consume API quota.
|
|
21
|
+
*/
|
|
22
|
+
import type { EventHandler, EventHandlerRequest, EventHandlerResponse } from 'h3';
|
|
23
|
+
export declare function withSigning<Req extends EventHandlerRequest = EventHandlerRequest, Res extends EventHandlerResponse = EventHandlerResponse>(handler: EventHandler<Req, Res>): EventHandler<Req, Res>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createError, defineEventHandler } from "h3";
|
|
2
|
+
import { useRuntimeConfig } from "nitropack/runtime";
|
|
3
|
+
import { verifyProxyRequest } from "./sign.js";
|
|
4
|
+
export function withSigning(handler) {
|
|
5
|
+
return defineEventHandler(async (event) => {
|
|
6
|
+
const runtimeConfig = useRuntimeConfig(event);
|
|
7
|
+
const secret = runtimeConfig["nuxt-scripts"]?.proxySecret;
|
|
8
|
+
if (!secret)
|
|
9
|
+
return handler(event);
|
|
10
|
+
if (!verifyProxyRequest(event, secret)) {
|
|
11
|
+
throw createError({
|
|
12
|
+
statusCode: 403,
|
|
13
|
+
statusMessage: "Invalid signature"
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return handler(event);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { createError, defineEventHandler, getQuery, setHeader } from "h3";
|
|
2
2
|
import { $fetch } from "ofetch";
|
|
3
|
+
import { withSigning } from "./utils/withSigning.js";
|
|
3
4
|
const TWEET_ID_RE = /^\d+$/;
|
|
4
|
-
export default defineEventHandler(async (event) => {
|
|
5
|
+
export default withSigning(defineEventHandler(async (event) => {
|
|
5
6
|
const query = getQuery(event);
|
|
6
7
|
const tweetId = query.id;
|
|
7
8
|
if (!tweetId || !TWEET_ID_RE.test(tweetId)) {
|
|
@@ -29,4 +30,4 @@ export default defineEventHandler(async (event) => {
|
|
|
29
30
|
setHeader(event, "Content-Type", "application/json");
|
|
30
31
|
setHeader(event, "Cache-Control", "public, max-age=600, s-maxage=600");
|
|
31
32
|
return tweetData;
|
|
32
|
-
});
|
|
33
|
+
}));
|
package/dist/runtime/types.d.ts
CHANGED
|
@@ -235,7 +235,7 @@ export type BuiltInRegistryScriptKey = 'bingUet' | 'blueskyEmbed' | 'carbonAds'
|
|
|
235
235
|
* Includes both built-in and augmented keys.
|
|
236
236
|
*/
|
|
237
237
|
export type RegistryScriptKey = Exclude<keyof ScriptRegistry, `${string}-npm`>;
|
|
238
|
-
type RegistryConfigInput<T> = [T] extends [true] ? Record<string, never> : T;
|
|
238
|
+
type RegistryConfigInput<T> = 0 extends 1 & T ? Record<string, any> : [T] extends [true] ? Record<string, never> : T;
|
|
239
239
|
export type NuxtConfigScriptRegistryEntry<T> = true | false | 'mock' | (RegistryConfigInput<T> & {
|
|
240
240
|
trigger?: NuxtUseScriptOptionsSerializable['trigger'] | false;
|
|
241
241
|
proxy?: boolean;
|
|
@@ -268,6 +268,14 @@ export interface RegistryScriptServerHandler {
|
|
|
268
268
|
route: string;
|
|
269
269
|
handler: string;
|
|
270
270
|
middleware?: boolean;
|
|
271
|
+
/**
|
|
272
|
+
* Whether this handler verifies HMAC signatures via `withSigning()`.
|
|
273
|
+
*
|
|
274
|
+
* When any enabled script registers a handler with `requiresSigning: true`,
|
|
275
|
+
* the module enforces that `NUXT_SCRIPTS_PROXY_SECRET` is set in production,
|
|
276
|
+
* and the `/_scripts/sign` endpoint will accept this route as a signable path.
|
|
277
|
+
*/
|
|
278
|
+
requiresSigning?: boolean;
|
|
271
279
|
}
|
|
272
280
|
/**
|
|
273
281
|
* Declares what optimization modes a script supports and what's active by default.
|
package/dist/types.d.mts
CHANGED
|
@@ -6,6 +6,6 @@ declare module '@nuxt/schema' {
|
|
|
6
6
|
|
|
7
7
|
export { type FirstPartyPrivacy } from '../dist/runtime/types.js'
|
|
8
8
|
|
|
9
|
-
export { type applyAutoInject, default, type isProxyDisabled } from './module.mjs'
|
|
9
|
+
export { type applyAutoInject, default, type isProxyDisabled, type resolveProxySecret } from './module.mjs'
|
|
10
10
|
|
|
11
|
-
export { type ModuleHooks, type ModuleOptions } from './module.mjs'
|
|
11
|
+
export { type ModuleHooks, type ModuleOptions, type ResolvedProxySecret } from './module.mjs'
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nuxt/scripts",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.0-rc.
|
|
4
|
+
"version": "1.0.0-rc.8",
|
|
5
5
|
"description": "Load third-party scripts with better performance, privacy and DX in Nuxt Apps.",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Harlan Wilton",
|
|
@@ -45,7 +45,11 @@
|
|
|
45
45
|
]
|
|
46
46
|
}
|
|
47
47
|
},
|
|
48
|
+
"bin": {
|
|
49
|
+
"nuxt-scripts": "./bin/cli.mjs"
|
|
50
|
+
},
|
|
48
51
|
"files": [
|
|
52
|
+
"bin",
|
|
49
53
|
"dist"
|
|
50
54
|
],
|
|
51
55
|
"build": {
|
|
@@ -108,7 +112,7 @@
|
|
|
108
112
|
"magic-string": "^0.30.21",
|
|
109
113
|
"ofetch": "^1.5.1",
|
|
110
114
|
"ohash": "^2.0.11",
|
|
111
|
-
"oxc-parser": "^0.
|
|
115
|
+
"oxc-parser": "^0.125.0",
|
|
112
116
|
"oxc-walker": "^0.7.0",
|
|
113
117
|
"pathe": "^2.0.3",
|
|
114
118
|
"pkg-types": "^2.3.0",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"id":"640f0a39-e659-4a31-8b8d-adbd9af52f1e","timestamp":1775787703990,"prerendered":[]}
|