i accidentally deleted the repo, but recovered it. i'll start committing
it was weird. i pressed delete on a subfolder, i think one of the pages.off folders that i was using. and then, suddenly, nvim on windows 7 decided to delete every file in the directory. they weren't shred off the space time continuum, but just marked deleted. i had to pay $80 to get access to a software that could see them. bleh! just seeing all my work, a little over a week, was pretty heart shattering. but i remembered that long ago, a close friend said i could call them whenever i was feeling sad. i finally took them up on that offer. the first time i've ever called someone for emotional support. but it's ok. i got it back. and the site framework is better than ever. i'm gonna commit and push more often. the repo is private anyways.
This commit is contained in:
commit
af60d1172f
183 changed files with 19727 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
.clover
|
||||
node_modules
|
||||
|
10
deno.jsonc
Normal file
10
deno.jsonc
Normal file
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"lint": {
|
||||
"exclude": ["framework/meta"], // OLD
|
||||
"rules": {
|
||||
"exclude": [
|
||||
"no-explicit-any" // TODO
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
99
framework/assets.ts
Normal file
99
framework/assets.ts
Normal file
|
@ -0,0 +1,99 @@
|
|||
interface Loaded {
|
||||
map: BuiltAssetMap;
|
||||
buf: Buffer;
|
||||
}
|
||||
let assets: Loaded | null = null;
|
||||
|
||||
export type StaticPageId = string;
|
||||
|
||||
export async function reload() {
|
||||
const [map, buf] = await Promise.all([
|
||||
fs.readFile(".clover/static.json", "utf8"),
|
||||
fs.readFile(".clover/static.blob"),
|
||||
]);
|
||||
assets = {
|
||||
map: JSON.parse(map),
|
||||
buf,
|
||||
};
|
||||
}
|
||||
|
||||
export async function reloadSync() {
|
||||
const map = fs.readFileSync(".clover/static.json", "utf8");
|
||||
const buf = fs.readFileSync(".clover/static.blob");
|
||||
assets = {
|
||||
map: JSON.parse(map),
|
||||
buf,
|
||||
};
|
||||
}
|
||||
|
||||
export async function assetMiddleware(c: Context, next: Next) {
|
||||
if (!assets) await reload();
|
||||
const asset = assets!.map[c.req.path];
|
||||
if (asset) {
|
||||
return assetInner(c, asset, 200);
|
||||
}
|
||||
return next();
|
||||
}
|
||||
|
||||
export async function serveAsset(
|
||||
c: Context,
|
||||
id: StaticPageId,
|
||||
status: StatusCode,
|
||||
) {
|
||||
assets ?? await reload();
|
||||
return assetInner(c, assets!.map[id], status);
|
||||
}
|
||||
|
||||
export function hasAsset(id: string) {
|
||||
if (!assets) reloadSync();
|
||||
return assets!.map[id] !== undefined;
|
||||
}
|
||||
|
||||
export function etagMatches(etag: string, ifNoneMatch: string) {
|
||||
return ifNoneMatch === etag || ifNoneMatch.split(/,\s*/).indexOf(etag) > -1;
|
||||
}
|
||||
|
||||
function subarrayAsset([start, end]: View) {
|
||||
return assets!.buf.subarray(start, end);
|
||||
}
|
||||
|
||||
function assetInner(c: Context, asset: BuiltAsset, status: StatusCode) {
|
||||
const ifnonematch = c.req.header("If-None-Match");
|
||||
if (ifnonematch) {
|
||||
const etag = asset.headers.ETag;
|
||||
if (etagMatches(etag, ifnonematch)) {
|
||||
c.res = new Response(null, {
|
||||
status: 304,
|
||||
statusText: "Not Modified",
|
||||
headers: {
|
||||
ETag: etag,
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
const acceptEncoding = c.req.header("Accept-Encoding") ?? "";
|
||||
let body;
|
||||
let headers = asset.headers;
|
||||
if (acceptEncoding.includes("zstd") && asset.zstd) {
|
||||
body = subarrayAsset(asset.zstd);
|
||||
headers = {
|
||||
...asset.headers,
|
||||
"Content-Encoding": "zstd",
|
||||
};
|
||||
} else if (acceptEncoding.includes("gzip") && asset.gzip) {
|
||||
body = subarrayAsset(asset.gzip);
|
||||
headers = {
|
||||
...asset.headers,
|
||||
"Content-Encoding": "gzip",
|
||||
};
|
||||
} else {
|
||||
body = subarrayAsset(asset.raw);
|
||||
}
|
||||
c.res = new Response(body, { headers, status });
|
||||
}
|
||||
|
||||
import * as fs from "./fs.ts";
|
||||
import type { Context, Next } from "hono";
|
||||
import type { StatusCode } from "hono/utils/http-status";
|
||||
import type { BuiltAsset, BuiltAssetMap, View } from "./incremental.ts";
|
85
framework/bundle.ts
Normal file
85
framework/bundle.ts
Normal file
|
@ -0,0 +1,85 @@
|
|||
// This file implements client-side bundling, mostly wrapping esbuild.
|
||||
import process from "node:process";
|
||||
const plugins: esbuild.Plugin[] = [
|
||||
// There are currently no plugins needed by 'paperclover.net'
|
||||
];
|
||||
|
||||
export async function bundleClientJavaScript(
|
||||
referencedScripts: string[],
|
||||
extraPublicScripts: string[],
|
||||
incr: Incremental,
|
||||
dev: boolean = false,
|
||||
) {
|
||||
const entryPoints = [
|
||||
...new Set([
|
||||
...referencedScripts,
|
||||
...extraPublicScripts,
|
||||
]),
|
||||
];
|
||||
if (entryPoints.length === 0) return;
|
||||
const invalidFiles = entryPoints
|
||||
.filter((file) => !file.match(/\.client\.[tj]sx?/));
|
||||
if (invalidFiles.length > 0) {
|
||||
const cwd = process.cwd();
|
||||
throw new Error(
|
||||
"All client-side scripts should be named like '.client.ts'. Exceptions: " +
|
||||
invalidFiles.map((x) => path.join(cwd, x)).join(","),
|
||||
);
|
||||
}
|
||||
|
||||
const bundle = await esbuild.build({
|
||||
bundle: true,
|
||||
chunkNames: "/js/c.[hash]",
|
||||
entryNames: "/js/[name]",
|
||||
assetNames: "/asset/[hash]",
|
||||
entryPoints,
|
||||
format: "esm",
|
||||
minify: !dev,
|
||||
outdir: "/out!",
|
||||
plugins,
|
||||
splitting: true,
|
||||
write: false,
|
||||
});
|
||||
if (bundle.errors.length || bundle.warnings.length) {
|
||||
throw new AggregateError(
|
||||
bundle.errors.concat(bundle.warnings),
|
||||
"JS bundle failed",
|
||||
);
|
||||
}
|
||||
incr.invalidate("bundle-script");
|
||||
const publicScriptRoutes = extraPublicScripts.map((file) =>
|
||||
path.basename(file).replace(/\.client\.[tj]sx?/, "")
|
||||
);
|
||||
const promises: Promise<unknown>[] = [];
|
||||
// TODO: add a shared build hash to entrypoints, derived from all the chunk hashes.
|
||||
for (const file of bundle.outputFiles) {
|
||||
let route = file.path.replace(/^.*!/, "").replaceAll("\\", "/");
|
||||
const text = file.text;
|
||||
// Register non-chunks as script entries.
|
||||
const chunk = route.startsWith("/js/c.");
|
||||
if (!chunk) {
|
||||
route = route.replace(".client.js", ".js");
|
||||
incr.put({
|
||||
srcId: "bundle-script",
|
||||
type: "script",
|
||||
key: route.slice("/js/".length, -".js".length),
|
||||
value: text,
|
||||
});
|
||||
}
|
||||
if (chunk || publicScriptRoutes.includes(route)) {
|
||||
promises.push(incr.putAsset({
|
||||
srcId: "bundle-script",
|
||||
key: route,
|
||||
body: text,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
if (promises.length > 0) {
|
||||
await Promise.all(promises);
|
||||
}
|
||||
}
|
||||
|
||||
import * as path from "node:path";
|
||||
import * as esbuild from "esbuild";
|
||||
import { Incremental } from "./incremental.ts";
|
87
framework/css.ts
Normal file
87
framework/css.ts
Normal file
|
@ -0,0 +1,87 @@
|
|||
export interface Theme {
|
||||
bg: string;
|
||||
fg: string;
|
||||
primary?: string;
|
||||
h1?: string;
|
||||
}
|
||||
|
||||
export function stringifyTheme(theme: Theme) {
|
||||
return [
|
||||
":root {",
|
||||
"--bg: " + theme.bg + ";",
|
||||
"--fg: " + theme.fg + ";",
|
||||
theme.primary ? "--primary: " + theme.primary + ";" : null,
|
||||
"}",
|
||||
theme.h1 ? "h1 { color: " + theme.h1 + "}" : null,
|
||||
].filter(Boolean).join("\n");
|
||||
}
|
||||
|
||||
export function preprocess(css: string, theme: Theme): string {
|
||||
const keys = Object.keys(theme);
|
||||
const regex = new RegExp(
|
||||
`([{};\\n][^{};\\n]*var\\(--(${keys.join("|")})\\).*?(?=[;{}\\n]))`,
|
||||
"gs",
|
||||
);
|
||||
const regex2 = new RegExp(`var\\(--(${keys.join("|")})\\)`);
|
||||
return css.replace(
|
||||
regex,
|
||||
(_, line) =>
|
||||
line.replace(regex2, (_: string, varName: string) => theme[varName]) +
|
||||
";" + line.slice(1),
|
||||
);
|
||||
}
|
||||
|
||||
export async function bundleCssFiles(
|
||||
cssImports: string[],
|
||||
theme: Theme,
|
||||
dev: boolean = false,
|
||||
): Promise<string> {
|
||||
const plugin = {
|
||||
name: "clover",
|
||||
setup(b) {
|
||||
b.onResolve(
|
||||
{ filter: /^\$input\$$/ },
|
||||
() => ({ path: ".", namespace: "input" }),
|
||||
);
|
||||
b.onLoad(
|
||||
{ filter: /./, namespace: "input" },
|
||||
() => ({
|
||||
loader: "css",
|
||||
contents:
|
||||
cssImports.map((path) => `@import url(${JSON.stringify(path)});`)
|
||||
.join("\n") + stringifyTheme(theme),
|
||||
resolveDir: ".",
|
||||
}),
|
||||
);
|
||||
b.onLoad(
|
||||
{ filter: /\.css$/ },
|
||||
async ({ path: file }) => ({
|
||||
loader: "css",
|
||||
contents: preprocess(await fs.readFile(file, "utf-8"), theme),
|
||||
}),
|
||||
);
|
||||
},
|
||||
} satisfies Plugin;
|
||||
const build = await esbuild.build({
|
||||
bundle: true,
|
||||
entryPoints: ["$input$"],
|
||||
write: false,
|
||||
external: ["*.woff2"],
|
||||
target: ["ie11"],
|
||||
plugins: [plugin],
|
||||
minify: !dev,
|
||||
});
|
||||
const { errors, warnings, outputFiles } = build;
|
||||
if (errors.length > 0) {
|
||||
throw new AggregateError(errors, "CSS Build Failed");
|
||||
}
|
||||
if (warnings.length > 0) {
|
||||
throw new AggregateError(warnings, "CSS Build Failed");
|
||||
}
|
||||
if (outputFiles.length > 1) throw new Error("Too many output files");
|
||||
return outputFiles[0].text;
|
||||
}
|
||||
|
||||
import type { Plugin } from "esbuild";
|
||||
import * as esbuild from "esbuild";
|
||||
import * as fs from "./fs.ts";
|
10
framework/fs.ts
Normal file
10
framework/fs.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
// default
|
||||
function isScope(node, parent) {
|
||||
if ((0, _index.isBlockStatement)(node) && ((0, _index.isFunction)(parent) || (0, _index.isCatchClause)(parent))) {
|
||||
return false;
|
||||
}
|
||||
if ((0, _index.isPattern)(node) && ((0, _index.isFunction)(parent) || (0, _index.isCatchClause)(parent))) {
|
||||
return true;
|
||||
}
|
||||
return (0, _index.isScopable)(node);
|
||||
}
|
240
framework/hot.ts
Normal file
240
framework/hot.ts
Normal file
|
@ -0,0 +1,240 @@
|
|||
// This implements the ability to use TS, TSX, and more plugins
|
||||
// in Node.js. It cannot be built on the ES module loader,
|
||||
// because there is no exposed way to replace modules when
|
||||
// needed (see nodejs#49442).
|
||||
//
|
||||
// It also allows using a simple compile cache, which is used by
|
||||
// the site generator to determine when code changes.
|
||||
export const projectRoot = path.resolve(import.meta.dirname, "../");
|
||||
export const projectSrc = path.resolve(projectRoot, "src");
|
||||
|
||||
// Create a project-relative require. For convenience, it is generic-typed.
|
||||
export const load = createRequire(
|
||||
pathToFileURL(path.join(projectRoot, "run.js")).toString(),
|
||||
) as {
|
||||
<T = unknown>(id: string): T;
|
||||
extensions: NodeJS.Dict<(mod: NodeJS.Module, file: string) => unknown>;
|
||||
cache: NodeJS.Dict<NodeJS.Module>;
|
||||
resolve: (id: string, o?: { paths: string[] }) => string;
|
||||
};
|
||||
export const { cache } = load;
|
||||
|
||||
// Register extensions by overwriting `require.extensions`
|
||||
const require = load;
|
||||
const exts = require.extensions;
|
||||
exts[".ts"] = loadEsbuild;
|
||||
exts[".tsx"] = loadEsbuild;
|
||||
exts[".jsx"] = loadEsbuild;
|
||||
exts[".marko"] = loadMarko;
|
||||
exts[".mdx"] = loadMdx;
|
||||
exts[".css"] = loadCss;
|
||||
|
||||
// Intercept all module load calls to track CSS imports + file times.
|
||||
export interface FileStat {
|
||||
cssImportsRecursive: string[] | null;
|
||||
lastModified: number;
|
||||
imports: string[];
|
||||
/* Used by 'incremental.ts' */
|
||||
srcIds: string[];
|
||||
}
|
||||
let fsGraph = new Map<string, FileStat>();
|
||||
export function setFsGraph(g: Map<string, FileStat>) {
|
||||
if (fsGraph.size > 0) {
|
||||
throw new Error("Cannot restore fsGraph when it has been written into");
|
||||
}
|
||||
fsGraph = g;
|
||||
}
|
||||
export function getFsGraph() {
|
||||
return fsGraph;
|
||||
}
|
||||
|
||||
function shouldTrackPath(filename: string) {
|
||||
return !filename.includes("node_modules") &&
|
||||
!filename.includes(import.meta.dirname);
|
||||
}
|
||||
|
||||
const Module = load<typeof import("node:module")>("node:module");
|
||||
const ModulePrototypeUnderscoreCompile = Module.prototype._compile;
|
||||
Module.prototype._compile = function (
|
||||
content: string,
|
||||
filename: string,
|
||||
format: "module" | "commonjs",
|
||||
) {
|
||||
fs.writeMkdirSync(
|
||||
".clover/debug-transpilation/" +
|
||||
path.relative(projectRoot, filename).replaceAll("\\", "/").replaceAll(
|
||||
"../",
|
||||
"_/",
|
||||
).replaceAll("/", "."),
|
||||
content,
|
||||
);
|
||||
const result = ModulePrototypeUnderscoreCompile.call(
|
||||
this,
|
||||
content,
|
||||
filename,
|
||||
format,
|
||||
);
|
||||
const stat = fs.statSync(filename);
|
||||
if (shouldTrackPath(filename)) {
|
||||
const cssImportsMaybe: string[] = [];
|
||||
const imports: string[] = [];
|
||||
for (const { filename: file } of this.children) {
|
||||
const relative = path.relative(projectRoot, file);
|
||||
if (file.endsWith(".css")) cssImportsMaybe.push(relative);
|
||||
else {
|
||||
const child = fsGraph.get(relative);
|
||||
if (!child) continue;
|
||||
const { cssImportsRecursive } = child;
|
||||
if (cssImportsRecursive) cssImportsMaybe.push(...cssImportsRecursive);
|
||||
imports.push(relative);
|
||||
}
|
||||
}
|
||||
const relative = path.relative(projectRoot, filename);
|
||||
fsGraph.set(relative, {
|
||||
cssImportsRecursive: cssImportsMaybe.length > 0
|
||||
? Array.from(new Set(cssImportsMaybe))
|
||||
: null,
|
||||
imports,
|
||||
lastModified: stat.mtimeMs,
|
||||
srcIds: [],
|
||||
});
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
// Implement @/ prefix
|
||||
const ModuleUnderscoreResolveFilename = Module._resolveFilename;
|
||||
Module._resolveFilename = (...args) => {
|
||||
if (args[0].startsWith("@/")) {
|
||||
const replacedPath = "." + args[0].slice(1);
|
||||
try {
|
||||
return require.resolve(replacedPath, { paths: [projectSrc] });
|
||||
} catch (err: any) {
|
||||
if (err.code === "MODULE_NOT_FOUND" && err.requireStack.length <= 1) {
|
||||
err.message.replace(replacedPath, args[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ModuleUnderscoreResolveFilename(...args);
|
||||
};
|
||||
|
||||
function loadEsbuild(module: NodeJS.Module, filepath: string) {
|
||||
let src = fs.readFileSync(filepath, "utf8");
|
||||
return loadEsbuildCode(module, filepath, src);
|
||||
}
|
||||
|
||||
function loadEsbuildCode(module: NodeJS.Module, filepath: string, src: string) {
|
||||
if (filepath === import.meta.filename) {
|
||||
module.exports = self;
|
||||
return;
|
||||
}
|
||||
|
||||
let loader: any = "tsx";
|
||||
if (filepath.endsWith(".ts")) loader = "ts";
|
||||
else if (filepath.endsWith(".jsx")) loader = "jsx";
|
||||
else if (filepath.endsWith(".js")) loader = "js";
|
||||
if (src.includes("import.meta")) {
|
||||
src = `
|
||||
import.meta.url = ${JSON.stringify(pathToFileURL(filepath).toString())};
|
||||
import.meta.dirname = ${JSON.stringify(path.dirname(filepath))};
|
||||
import.meta.filename = ${JSON.stringify(filepath)};
|
||||
` + src;
|
||||
}
|
||||
src = esbuild.transformSync(src, {
|
||||
loader,
|
||||
format: "cjs",
|
||||
target: "esnext",
|
||||
jsx: "automatic",
|
||||
jsxImportSource: "#ssr",
|
||||
}).code;
|
||||
return module._compile(src, filepath, "commonjs");
|
||||
}
|
||||
|
||||
function loadMarko(module: NodeJS.Module, filepath: string) {
|
||||
let src = fs.readFileSync(filepath, "utf8");
|
||||
// A non-standard thing here is Clover Sitegen implements
|
||||
// its own client side scripting stuff, so it overrides
|
||||
// bare client import statements to it's own usage.
|
||||
if (src.match(/^\s*client\s+import\s+["']/m)) {
|
||||
src = src.replace(
|
||||
/^\s*client\s+import\s+("[^"]+|'[^']+)[^\n]+/m,
|
||||
"<CloverScriptInclude src=$1 />",
|
||||
) + '\nimport { Script as CloverScriptInclude } from "#sitegen";';
|
||||
}
|
||||
|
||||
src = marko.compileSync(filepath, {}).code;
|
||||
src = src.replace("marko/debug/html", "#ssr/marko");
|
||||
return loadEsbuildCode(module, filepath, src);
|
||||
}
|
||||
|
||||
function loadMdx(module: NodeJS.Module, filepath: string) {
|
||||
const input = fs.readFileSync(filepath);
|
||||
const out = mdx.compileSync(input, { jsxImportSource: "#ssr" }).value;
|
||||
const src = typeof out === "string" ? out : Buffer.from(out).toString("utf8");
|
||||
return loadEsbuildCode(module, filepath, src);
|
||||
}
|
||||
|
||||
function loadCss(module: NodeJS.Module, filepath: string) {
|
||||
module.exports = {};
|
||||
}
|
||||
|
||||
export function reloadRecursive(filepath: string) {
|
||||
filepath = path.resolve(filepath);
|
||||
const existing = cache[filepath];
|
||||
if (existing) deleteRecursive(filepath, existing);
|
||||
fsGraph.clear();
|
||||
return require(filepath);
|
||||
}
|
||||
|
||||
function deleteRecursive(id: string, module: any) {
|
||||
if (id.includes(path.sep + "node_modules" + path.sep)) {
|
||||
return;
|
||||
}
|
||||
delete cache[id];
|
||||
for (const child of module.children) {
|
||||
if (child.filename.includes("/engine/")) return;
|
||||
const existing = cache[child.filename];
|
||||
if (existing === child) deleteRecursive(child.filename, existing);
|
||||
}
|
||||
}
|
||||
|
||||
export function getCssImports(filepath: string) {
|
||||
filepath = path.resolve(filepath);
|
||||
if (!require.cache[filepath]) throw new Error(filepath + " was never loaded");
|
||||
return fsGraph.get(path.relative(projectRoot, filepath))
|
||||
?.cssImportsRecursive ?? [];
|
||||
}
|
||||
|
||||
export function resolveFrom(src: string, dest: string) {
|
||||
try {
|
||||
return createRequire(src).resolve(dest);
|
||||
} catch (err: any) {
|
||||
if (err.code === "MODULE_NOT_FOUND" && err.requireStack.length <= 1) {
|
||||
err.message = err.message.split("\n")[0] + " from '" + src + "'";
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
namespace NodeJS {
|
||||
interface Module {
|
||||
_compile(
|
||||
this: NodeJS.Module,
|
||||
content: string,
|
||||
filepath: string,
|
||||
format: "module" | "commonjs",
|
||||
): unknown;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
import * as fs from "./fs.ts";
|
||||
import * as path from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import * as esbuild from "esbuild";
|
||||
import * as marko from "@marko/compiler";
|
||||
import { createRequire } from "node:module";
|
||||
import * as mdx from "@mdx-js/mdx";
|
||||
import * as self from "./hot.ts";
|
||||
import { Buffer } from "node:buffer";
|
265
framework/incremental.ts
Normal file
265
framework/incremental.ts
Normal file
|
@ -0,0 +1,265 @@
|
|||
// `Incremental` contains multiple maps for the different parts of a site
|
||||
// build, and tracks reused items across builds. It also handles emitting and
|
||||
// updating the built site. This structure is self contained and serializable.
|
||||
//
|
||||
// Tracking is simple: Files map to one or more 'source IDs', which map to one
|
||||
// or more 'artifact'. This two layer approach allows many files (say a page +
|
||||
// all its imports) to map to the build of a page, which produces an HTML file
|
||||
// plus a list of scripts.
|
||||
|
||||
import { Buffer } from "node:buffer";
|
||||
interface ArtifactMap {
|
||||
asset: Asset;
|
||||
script: string;
|
||||
}
|
||||
type AllArtifactMaps = {
|
||||
[K in keyof ArtifactMap]: Map<string, ArtifactMap[K]>;
|
||||
};
|
||||
type ArtifactType = keyof ArtifactMap;
|
||||
|
||||
interface Asset {
|
||||
buffer: Buffer;
|
||||
headers: Record<string, string | undefined>;
|
||||
hash: string;
|
||||
}
|
||||
|
||||
export interface PutBase {
|
||||
srcTag?: string; // deprecated
|
||||
srcId: string;
|
||||
key: string;
|
||||
}
|
||||
|
||||
export interface Put<T extends ArtifactType> extends PutBase {
|
||||
type: T;
|
||||
value: ArtifactMap[T];
|
||||
}
|
||||
|
||||
export interface Output {
|
||||
type: ArtifactType;
|
||||
key: string;
|
||||
}
|
||||
|
||||
const gzip = util.promisify(zlib.gzip);
|
||||
const zstd = util.promisify(zlib.zstdCompress);
|
||||
|
||||
export class Incremental {
|
||||
/** The generated artifacts */
|
||||
out: AllArtifactMaps = {
|
||||
asset: new Map(),
|
||||
script: new Map(),
|
||||
};
|
||||
/** Compressed resources */
|
||||
compress = new Map<string, Compressed>();
|
||||
compressQueue = new Queue<CompressJob, void>({
|
||||
name: "Compress",
|
||||
maxJobs: 5,
|
||||
fn: this.compressImpl.bind(this),
|
||||
passive: true,
|
||||
getItemText: (job) => `${job.algo.toUpperCase()} ${job.label}`,
|
||||
});
|
||||
/** Tracking filesystem entries to `srcId` */
|
||||
files = new Map<string, hot.FileStat>();
|
||||
srcIds = new Map<string, Output[]>();
|
||||
|
||||
static fromSerialized() {
|
||||
}
|
||||
serialize() {
|
||||
const writer = new BufferWriter();
|
||||
|
||||
const asset = Array.from(
|
||||
this.out.asset,
|
||||
([key, { buffer, hash, headers }]) => {
|
||||
const raw = writer.write(buffer, hash);
|
||||
const { gzip: gzipBuf, zstd: zstdBuf } = this.compress.get(hash) ?? {};
|
||||
const gzip = gzipBuf ? writer.write(gzipBuf, hash) : null;
|
||||
const zstd = zstdBuf ? writer.write(zstdBuf, hash) : null;
|
||||
return [key, {
|
||||
raw,
|
||||
gzip,
|
||||
zstd,
|
||||
hash,
|
||||
headers,
|
||||
}];
|
||||
},
|
||||
);
|
||||
const script = Array.from(this.out.script);
|
||||
|
||||
const meta = Buffer.from(
|
||||
JSON.stringify({
|
||||
asset,
|
||||
script,
|
||||
}),
|
||||
"utf-8",
|
||||
);
|
||||
|
||||
const lengthBuffer = Buffer.alloc(4);
|
||||
lengthBuffer.writeUInt32LE(meta.byteLength, 0);
|
||||
|
||||
return Buffer.concat([meta, lengthBuffer, ...writer.buffers]);
|
||||
}
|
||||
|
||||
serializeToDisk(file = ".clover/incr.state") {
|
||||
const buffer = this.serialize();
|
||||
fs.writeFileSync(file, buffer);
|
||||
}
|
||||
|
||||
put<T extends ArtifactType>({
|
||||
srcId,
|
||||
type,
|
||||
key,
|
||||
value,
|
||||
}: Put<T>) {
|
||||
this.out[type].set(key, value);
|
||||
}
|
||||
|
||||
async putAsset(info: PutAsset) {
|
||||
const { body, headers, key } = info;
|
||||
const buffer = Buffer.isBuffer(body) ? body : Buffer.from(body);
|
||||
const hash = Buffer.from(await crypto.subtle.digest("sha-1", buffer))
|
||||
.toString("hex");
|
||||
const value: Asset = {
|
||||
buffer,
|
||||
headers: {
|
||||
"Content-Type": headers?.["Content-Type"] ?? mime.contentTypeFor(key),
|
||||
"ETag": JSON.stringify(hash),
|
||||
...headers,
|
||||
},
|
||||
hash,
|
||||
};
|
||||
if (!this.compress.has(hash)) {
|
||||
const label = info.key;
|
||||
this.compress.set(hash, {
|
||||
zstd: undefined,
|
||||
gzip: undefined,
|
||||
});
|
||||
await Promise.all([
|
||||
this.compressQueue.add({ label, buffer, algo: "zstd", hash }),
|
||||
this.compressQueue.add({ label, buffer, algo: "gzip", hash }),
|
||||
]);
|
||||
}
|
||||
return this.put({ ...info, type: "asset", value });
|
||||
}
|
||||
|
||||
async compressImpl({ algo, buffer, hash }: CompressJob) {
|
||||
let out;
|
||||
switch (algo) {
|
||||
case "zstd":
|
||||
out = await zstd(buffer);
|
||||
break;
|
||||
case "gzip":
|
||||
out = await gzip(buffer, { level: 9 });
|
||||
break;
|
||||
}
|
||||
let entry = this.compress.get(hash);
|
||||
if (!entry) {
|
||||
this.compress.set(
|
||||
hash,
|
||||
entry = {
|
||||
zstd: undefined,
|
||||
gzip: undefined,
|
||||
},
|
||||
);
|
||||
}
|
||||
entry![algo] = out;
|
||||
}
|
||||
|
||||
invalidate(srcId: string) {
|
||||
}
|
||||
|
||||
async wait() {
|
||||
await this.compressQueue.done({ method: "stop" });
|
||||
}
|
||||
async flush() {
|
||||
const writer = new BufferWriter();
|
||||
const asset = Object.fromEntries(
|
||||
Array.from(this.out.asset, ([key, { buffer, hash, headers }]) => {
|
||||
const raw = writer.write(buffer, hash);
|
||||
const { gzip: gzipBuf, zstd: zstdBuf } = this.compress.get(hash) ?? {};
|
||||
const gzip = gzipBuf ? writer.write(gzipBuf, hash) : null;
|
||||
const zstd = zstdBuf ? writer.write(zstdBuf, hash) : null;
|
||||
return [key, {
|
||||
raw,
|
||||
gzip,
|
||||
zstd,
|
||||
headers,
|
||||
}];
|
||||
}),
|
||||
);
|
||||
await Promise.all([
|
||||
fs.writeFile(".clover/static.json", JSON.stringify(asset)),
|
||||
fs.writeFile(".clover/static.blob", writer.get()),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
export interface PutAsset extends PutBase {
|
||||
body: string | Buffer;
|
||||
headers?: Record<string, string | undefined>;
|
||||
}
|
||||
|
||||
export interface Compressed {
|
||||
gzip?: Buffer;
|
||||
zstd?: Buffer;
|
||||
}
|
||||
|
||||
export interface CompressJob {
|
||||
algo: "zstd" | "gzip";
|
||||
buffer: Buffer;
|
||||
label: string;
|
||||
hash: string;
|
||||
}
|
||||
|
||||
class BufferWriter {
|
||||
size = 0;
|
||||
seen = new Map<string, View>();
|
||||
buffers: Buffer[] = [];
|
||||
|
||||
write(buffer: Buffer, hash: string): View {
|
||||
let view = this.seen.get(hash);
|
||||
if (view) return view;
|
||||
view = [this.size, this.size += buffer.byteLength];
|
||||
this.seen.set(hash, view);
|
||||
this.buffers.push(buffer);
|
||||
return view;
|
||||
}
|
||||
|
||||
get() {
|
||||
return Buffer.concat(this.buffers);
|
||||
}
|
||||
}
|
||||
|
||||
export type View = [start: number, end: number];
|
||||
|
||||
// Alongside this type is a byte buffer, containing all the assets.
|
||||
export interface BuiltAssetMap {
|
||||
[route: string]: BuiltAsset;
|
||||
}
|
||||
export interface BuiltAsset {
|
||||
raw: View;
|
||||
gzip: View;
|
||||
zstd: View;
|
||||
headers: Record<string, string>;
|
||||
}
|
||||
|
||||
export interface SerializedMeta {
|
||||
asset: Array<[route: string, data: {
|
||||
raw: View;
|
||||
gzip: View | null;
|
||||
zstd: View | null;
|
||||
hash: string;
|
||||
headers: Record<string, string>;
|
||||
}]>;
|
||||
script: [key: string, value: string][];
|
||||
}
|
||||
|
||||
function never(): never {
|
||||
throw new Error("Impossible");
|
||||
}
|
||||
|
||||
import * as path from "node:path";
|
||||
import * as fs from "./fs.ts";
|
||||
import * as zlib from "node:zlib";
|
||||
import * as util from "node:util";
|
||||
import { Queue } from "./queue.ts";
|
||||
import * as hot from "./hot.ts";
|
||||
import * as mime from "./mime.ts";
|
13
framework/meta/index.ts
Normal file
13
framework/meta/index.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
import { resolveMetadata } from "./merge";
|
||||
import { renderMetadata } from "./render";
|
||||
import { Metadata } from "./types";
|
||||
|
||||
export * from "./types";
|
||||
export * from "./merge";
|
||||
export * from "./render";
|
||||
|
||||
export function resolveAndRenderMetadata(
|
||||
...metadata: [Metadata, ...Metadata[]]
|
||||
) {
|
||||
return renderMetadata(resolveMetadata(...metadata));
|
||||
}
|
154
framework/meta/merge.ts
Normal file
154
framework/meta/merge.ts
Normal file
|
@ -0,0 +1,154 @@
|
|||
import { createDefaultMetadata } from "./nextjs/default-metadata";
|
||||
import { resolveAsArrayOrUndefined } from "./nextjs/generate/utils";
|
||||
import {
|
||||
resolveAlternates,
|
||||
resolveAppleWebApp,
|
||||
resolveAppLinks,
|
||||
resolveRobots,
|
||||
resolveThemeColor,
|
||||
resolveVerification,
|
||||
resolveViewport,
|
||||
} from "./nextjs/resolvers/resolve-basics";
|
||||
import { resolveIcons } from "./nextjs/resolvers/resolve-icons";
|
||||
import {
|
||||
resolveOpenGraph,
|
||||
resolveTwitter,
|
||||
} from "./nextjs/resolvers/resolve-opengraph";
|
||||
import { resolveTitle } from "./nextjs/resolvers/resolve-title";
|
||||
import type {
|
||||
Metadata,
|
||||
ResolvedMetadata,
|
||||
} from "./nextjs/types/metadata-interface";
|
||||
|
||||
type MetadataAccumulationOptions = {
|
||||
pathname: string;
|
||||
};
|
||||
|
||||
// Merge the source metadata into the resolved target metadata.
|
||||
function merge(
|
||||
target: ResolvedMetadata,
|
||||
source: Metadata | null,
|
||||
titleTemplates: {
|
||||
title?: string | null;
|
||||
twitter?: string | null;
|
||||
openGraph?: string | null;
|
||||
} = {},
|
||||
) {
|
||||
const metadataBase = source?.metadataBase || target.metadataBase;
|
||||
for (const key_ in source) {
|
||||
const key = key_ as keyof Metadata;
|
||||
|
||||
switch (key) {
|
||||
case "title": {
|
||||
target.title = resolveTitle(source.title, titleTemplates.title);
|
||||
break;
|
||||
}
|
||||
case "alternates": {
|
||||
target.alternates = resolveAlternates(source.alternates, metadataBase, {
|
||||
pathname: (source as any)._pathname ?? "/",
|
||||
});
|
||||
break;
|
||||
}
|
||||
case "openGraph": {
|
||||
target.openGraph = resolveOpenGraph(source.openGraph, metadataBase);
|
||||
if (target.openGraph) {
|
||||
target.openGraph.title = resolveTitle(
|
||||
target.openGraph.title,
|
||||
titleTemplates.openGraph,
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "twitter": {
|
||||
target.twitter = resolveTwitter(source.twitter, metadataBase);
|
||||
if (target.twitter) {
|
||||
target.twitter.title = resolveTitle(
|
||||
target.twitter.title,
|
||||
titleTemplates.twitter,
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "verification":
|
||||
target.verification = resolveVerification(source.verification);
|
||||
break;
|
||||
case "viewport": {
|
||||
target.viewport = resolveViewport(source.viewport);
|
||||
break;
|
||||
}
|
||||
case "icons": {
|
||||
target.icons = resolveIcons(source.icons);
|
||||
break;
|
||||
}
|
||||
case "appleWebApp":
|
||||
target.appleWebApp = resolveAppleWebApp(source.appleWebApp);
|
||||
break;
|
||||
case "appLinks":
|
||||
target.appLinks = resolveAppLinks(source.appLinks);
|
||||
break;
|
||||
case "robots": {
|
||||
target.robots = resolveRobots(source.robots);
|
||||
break;
|
||||
}
|
||||
case "themeColor": {
|
||||
target.themeColor = resolveThemeColor(source.themeColor);
|
||||
break;
|
||||
}
|
||||
case "archives":
|
||||
case "assets":
|
||||
case "bookmarks":
|
||||
case "keywords":
|
||||
case "authors": {
|
||||
// FIXME: type inferring
|
||||
// @ts-ignore
|
||||
target[key] = resolveAsArrayOrUndefined(source[key]) || null;
|
||||
break;
|
||||
}
|
||||
// directly assign fields that fallback to null
|
||||
case "applicationName":
|
||||
case "description":
|
||||
case "generator":
|
||||
case "creator":
|
||||
case "publisher":
|
||||
case "category":
|
||||
case "classification":
|
||||
case "referrer":
|
||||
case "colorScheme":
|
||||
case "itunes":
|
||||
case "formatDetection":
|
||||
case "manifest":
|
||||
// @ts-ignore TODO: support inferring
|
||||
target[key] = source[key] || null;
|
||||
break;
|
||||
case "other":
|
||||
target.other = Object.assign({}, target.other, source.other);
|
||||
break;
|
||||
case "metadataBase":
|
||||
target.metadataBase = metadataBase;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
export interface MetadataWithPathname extends Metadata {
|
||||
/** Set by framework author to the pathname of the page defining this metadata. */
|
||||
_pathname?: string;
|
||||
}
|
||||
|
||||
export function resolveMetadata(
|
||||
...metadata: [MetadataWithPathname, ...MetadataWithPathname[]]
|
||||
) {
|
||||
const base = createDefaultMetadata();
|
||||
for (const item of metadata) {
|
||||
merge(base, item, {
|
||||
title: base.title?.template,
|
||||
twitter: base.twitter?.title?.template,
|
||||
openGraph: base.openGraph?.title?.template,
|
||||
});
|
||||
}
|
||||
return base;
|
||||
}
|
15
framework/meta/nextjs/constants.ts
Normal file
15
framework/meta/nextjs/constants.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import type { Viewport } from "./types/extra-types";
|
||||
import type { Icons } from "./types/metadata-types";
|
||||
|
||||
export const ViewPortKeys: { [k in keyof Viewport]: string } = {
|
||||
width: "width",
|
||||
height: "height",
|
||||
initialScale: "initial-scale",
|
||||
minimumScale: "minimum-scale",
|
||||
maximumScale: "maximum-scale",
|
||||
viewportFit: "viewport-fit",
|
||||
userScalable: "user-scalable",
|
||||
interactiveWidget: "interactive-widget",
|
||||
} as const;
|
||||
|
||||
export const IconKeys: (keyof Icons)[] = ["icon", "shortcut", "apple", "other"];
|
50
framework/meta/nextjs/default-metadata.ts
Normal file
50
framework/meta/nextjs/default-metadata.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
import type { ResolvedMetadata } from "./types/metadata-interface";
|
||||
import process from "node:process";
|
||||
|
||||
export function createDefaultMetadata(): ResolvedMetadata {
|
||||
const defaultMetadataBase =
|
||||
process.env.NODE_ENV === "production" && process.env.VERCEL_URL
|
||||
? new URL(`https://${process.env.VERCEL_URL}`)
|
||||
: null;
|
||||
|
||||
return {
|
||||
viewport: "width=device-width, initial-scale=1",
|
||||
metadataBase: defaultMetadataBase,
|
||||
|
||||
// Other values are all null
|
||||
title: null,
|
||||
description: null,
|
||||
applicationName: null,
|
||||
authors: null,
|
||||
generator: null,
|
||||
keywords: null,
|
||||
referrer: null,
|
||||
themeColor: null,
|
||||
colorScheme: null,
|
||||
creator: null,
|
||||
publisher: null,
|
||||
robots: null,
|
||||
manifest: null,
|
||||
alternates: {
|
||||
canonical: null,
|
||||
languages: null,
|
||||
media: null,
|
||||
types: null,
|
||||
},
|
||||
icons: null,
|
||||
openGraph: null,
|
||||
twitter: null,
|
||||
verification: {},
|
||||
appleWebApp: null,
|
||||
formatDetection: null,
|
||||
itunes: null,
|
||||
abstract: null,
|
||||
appLinks: null,
|
||||
archives: null,
|
||||
assets: null,
|
||||
bookmarks: null,
|
||||
category: null,
|
||||
classification: null,
|
||||
other: {},
|
||||
};
|
||||
}
|
72
framework/meta/nextjs/generate/alternate.tsx
Normal file
72
framework/meta/nextjs/generate/alternate.tsx
Normal file
|
@ -0,0 +1,72 @@
|
|||
import type { ResolvedMetadata } from "../types/metadata-interface";
|
||||
|
||||
import React from "react";
|
||||
import { AlternateLinkDescriptor } from "../types/alternative-urls-types";
|
||||
|
||||
function AlternateLink({
|
||||
descriptor,
|
||||
...props
|
||||
}: {
|
||||
descriptor: AlternateLinkDescriptor;
|
||||
} & React.LinkHTMLAttributes<HTMLLinkElement>) {
|
||||
if (!descriptor.url) return null;
|
||||
return (
|
||||
<link
|
||||
{...props}
|
||||
{...(descriptor.title && { title: descriptor.title })}
|
||||
href={descriptor.url.toString()}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export function AlternatesMetadata({
|
||||
alternates,
|
||||
}: {
|
||||
alternates: ResolvedMetadata["alternates"];
|
||||
}) {
|
||||
if (!alternates) return null;
|
||||
const { canonical, languages, media, types } = alternates;
|
||||
return (
|
||||
<>
|
||||
{canonical
|
||||
? <AlternateLink rel="canonical" descriptor={canonical} />
|
||||
: null}
|
||||
{languages
|
||||
? Object.entries(languages).map(([locale, descriptors]) => {
|
||||
return descriptors?.map((descriptor, index) => (
|
||||
<AlternateLink
|
||||
rel="alternate"
|
||||
key={index}
|
||||
hrefLang={locale}
|
||||
descriptor={descriptor}
|
||||
/>
|
||||
));
|
||||
})
|
||||
: null}
|
||||
{media
|
||||
? Object.entries(media).map(([mediaName, descriptors]) =>
|
||||
descriptors?.map((descriptor, index) => (
|
||||
<AlternateLink
|
||||
rel="alternate"
|
||||
key={index}
|
||||
media={mediaName}
|
||||
descriptor={descriptor}
|
||||
/>
|
||||
))
|
||||
)
|
||||
: null}
|
||||
{types
|
||||
? Object.entries(types).map(([type, descriptors]) =>
|
||||
descriptors?.map((descriptor, index) => (
|
||||
<AlternateLink
|
||||
rel="alternate"
|
||||
key={index}
|
||||
type={type}
|
||||
descriptor={descriptor}
|
||||
/>
|
||||
))
|
||||
)
|
||||
: null}
|
||||
</>
|
||||
);
|
||||
}
|
171
framework/meta/nextjs/generate/basic.tsx
Normal file
171
framework/meta/nextjs/generate/basic.tsx
Normal file
|
@ -0,0 +1,171 @@
|
|||
import type { ResolvedMetadata } from "../types/metadata-interface";
|
||||
|
||||
import React from "react";
|
||||
import { Meta, MultiMeta } from "./meta";
|
||||
|
||||
export function BasicMetadata({ metadata }: { metadata: ResolvedMetadata }) {
|
||||
return (
|
||||
<>
|
||||
<meta charSet="utf-8" />
|
||||
{metadata.title !== null && metadata.title.absolute
|
||||
? <title>{metadata.title.absolute}</title>
|
||||
: null}
|
||||
<Meta name="description" content={metadata.description} />
|
||||
<Meta name="application-name" content={metadata.applicationName} />
|
||||
{metadata.authors
|
||||
? metadata.authors.map((author, index) => (
|
||||
<React.Fragment key={index}>
|
||||
{author.url && <link rel="author" href={author.url.toString()} />}
|
||||
<Meta name="author" content={author.name} />
|
||||
</React.Fragment>
|
||||
))
|
||||
: null}
|
||||
{metadata.manifest
|
||||
? <link rel="manifest" href={metadata.manifest.toString()} />
|
||||
: null}
|
||||
<Meta name="generator" content={metadata.generator} />
|
||||
<Meta name="keywords" content={metadata.keywords?.join(",")} />
|
||||
<Meta name="referrer" content={metadata.referrer} />
|
||||
{metadata.themeColor
|
||||
? metadata.themeColor.map((themeColor, index) => (
|
||||
<Meta
|
||||
key={index}
|
||||
name="theme-color"
|
||||
content={themeColor.color}
|
||||
media={themeColor.media}
|
||||
/>
|
||||
))
|
||||
: null}
|
||||
<Meta name="color-scheme" content={metadata.colorScheme} />
|
||||
<Meta name="viewport" content={metadata.viewport} />
|
||||
<Meta name="creator" content={metadata.creator} />
|
||||
<Meta name="publisher" content={metadata.publisher} />
|
||||
<Meta name="robots" content={metadata.robots?.basic} />
|
||||
<Meta name="googlebot" content={metadata.robots?.googleBot} />
|
||||
<Meta name="abstract" content={metadata.abstract} />
|
||||
{metadata.archives
|
||||
? metadata.archives.map((archive) => (
|
||||
<link rel="archives" href={archive} key={archive} />
|
||||
))
|
||||
: null}
|
||||
{metadata.assets
|
||||
? metadata.assets.map((asset) => (
|
||||
<link rel="assets" href={asset} key={asset} />
|
||||
))
|
||||
: null}
|
||||
{metadata.bookmarks
|
||||
? metadata.bookmarks.map((bookmark) => (
|
||||
<link rel="bookmarks" href={bookmark} key={bookmark} />
|
||||
))
|
||||
: null}
|
||||
<Meta name="category" content={metadata.category} />
|
||||
<Meta name="classification" content={metadata.classification} />
|
||||
{metadata.other
|
||||
? Object.entries(metadata.other).map(([name, content]) => (
|
||||
<Meta
|
||||
key={name}
|
||||
name={name}
|
||||
content={Array.isArray(content) ? content.join(",") : content}
|
||||
/>
|
||||
))
|
||||
: null}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export function ItunesMeta({ itunes }: { itunes: ResolvedMetadata["itunes"] }) {
|
||||
if (!itunes) return null;
|
||||
const { appId, appArgument } = itunes;
|
||||
let content = `app-id=${appId}`;
|
||||
if (appArgument) {
|
||||
content += `, app-argument=${appArgument}`;
|
||||
}
|
||||
return <meta name="apple-itunes-app" content={content} />;
|
||||
}
|
||||
|
||||
const formatDetectionKeys = [
|
||||
"telephone",
|
||||
"date",
|
||||
"address",
|
||||
"email",
|
||||
"url",
|
||||
] as const;
|
||||
export function FormatDetectionMeta({
|
||||
formatDetection,
|
||||
}: {
|
||||
formatDetection: ResolvedMetadata["formatDetection"];
|
||||
}) {
|
||||
if (!formatDetection) return null;
|
||||
let content = "";
|
||||
for (const key of formatDetectionKeys) {
|
||||
if (key in formatDetection) {
|
||||
if (content) content += ", ";
|
||||
content += `${key}=no`;
|
||||
}
|
||||
}
|
||||
return <meta name="format-detection" content={content} />;
|
||||
}
|
||||
|
||||
export function AppleWebAppMeta({
|
||||
appleWebApp,
|
||||
}: {
|
||||
appleWebApp: ResolvedMetadata["appleWebApp"];
|
||||
}) {
|
||||
if (!appleWebApp) return null;
|
||||
const { capable, title, startupImage, statusBarStyle } = appleWebApp;
|
||||
|
||||
return (
|
||||
<>
|
||||
{capable
|
||||
? <meta name="apple-mobile-web-app-capable" content="yes" />
|
||||
: null}
|
||||
<Meta name="apple-mobile-web-app-title" content={title} />
|
||||
{startupImage
|
||||
? startupImage.map((image, index) => (
|
||||
<link
|
||||
key={index}
|
||||
href={image.url}
|
||||
media={image.media}
|
||||
rel="apple-touch-startup-image"
|
||||
/>
|
||||
))
|
||||
: null}
|
||||
{statusBarStyle
|
||||
? (
|
||||
<meta
|
||||
name="apple-mobile-web-app-status-bar-style"
|
||||
content={statusBarStyle}
|
||||
/>
|
||||
)
|
||||
: null}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export function VerificationMeta({
|
||||
verification,
|
||||
}: {
|
||||
verification: ResolvedMetadata["verification"];
|
||||
}) {
|
||||
if (!verification) return null;
|
||||
|
||||
return (
|
||||
<>
|
||||
<MultiMeta
|
||||
namePrefix="google-site-verification"
|
||||
contents={verification.google}
|
||||
/>
|
||||
<MultiMeta namePrefix="y_key" contents={verification.yahoo} />
|
||||
<MultiMeta
|
||||
namePrefix="yandex-verification"
|
||||
contents={verification.yandex}
|
||||
/>
|
||||
<MultiMeta namePrefix="me" contents={verification.me} />
|
||||
{verification.other
|
||||
? Object.entries(verification.other).map(([key, value], index) => (
|
||||
<MultiMeta key={key + index} namePrefix={key} contents={value} />
|
||||
))
|
||||
: null}
|
||||
</>
|
||||
);
|
||||
}
|
62
framework/meta/nextjs/generate/icons.tsx
Normal file
62
framework/meta/nextjs/generate/icons.tsx
Normal file
|
@ -0,0 +1,62 @@
|
|||
import type { ResolvedMetadata } from "../types/metadata-interface";
|
||||
import type { Icon, IconDescriptor } from "../types/metadata-types";
|
||||
|
||||
import React from "react";
|
||||
|
||||
function IconDescriptorLink({ icon }: { icon: IconDescriptor }) {
|
||||
const { url, rel = "icon", ...props } = icon;
|
||||
|
||||
return <link rel={rel} href={url.toString()} {...props} />;
|
||||
}
|
||||
|
||||
function IconLink({ rel, icon }: { rel?: string; icon: Icon }) {
|
||||
if (typeof icon === "object" && !(icon instanceof URL)) {
|
||||
if (rel) icon.rel = rel;
|
||||
return <IconDescriptorLink icon={icon} />;
|
||||
} else {
|
||||
const href = icon.toString();
|
||||
return <link rel={rel} href={href} />;
|
||||
}
|
||||
}
|
||||
|
||||
export function IconsMetadata({ icons }: { icons: ResolvedMetadata["icons"] }) {
|
||||
if (!icons) return null;
|
||||
|
||||
const shortcutList = icons.shortcut;
|
||||
const iconList = icons.icon;
|
||||
const appleList = icons.apple;
|
||||
const otherList = icons.other;
|
||||
|
||||
return (
|
||||
<>
|
||||
{shortcutList
|
||||
? shortcutList.map((icon, index) => (
|
||||
<IconLink
|
||||
key={`shortcut-${index}`}
|
||||
rel="shortcut icon"
|
||||
icon={icon}
|
||||
/>
|
||||
))
|
||||
: null}
|
||||
{iconList
|
||||
? iconList.map((icon, index) => (
|
||||
<IconLink key={`shortcut-${index}`} rel="icon" icon={icon} />
|
||||
))
|
||||
: null}
|
||||
{appleList
|
||||
? appleList.map((icon, index) => (
|
||||
<IconLink
|
||||
key={`apple-${index}`}
|
||||
rel="apple-touch-icon"
|
||||
icon={icon}
|
||||
/>
|
||||
))
|
||||
: null}
|
||||
{otherList
|
||||
? otherList.map((icon, index) => (
|
||||
<IconDescriptorLink key={`other-${index}`} icon={icon} />
|
||||
))
|
||||
: null}
|
||||
</>
|
||||
);
|
||||
}
|
124
framework/meta/nextjs/generate/meta.tsx
Normal file
124
framework/meta/nextjs/generate/meta.tsx
Normal file
|
@ -0,0 +1,124 @@
|
|||
import React from "react";
|
||||
|
||||
export function Meta({
|
||||
name,
|
||||
property,
|
||||
content,
|
||||
media,
|
||||
}: {
|
||||
name?: string;
|
||||
property?: string;
|
||||
media?: string;
|
||||
content: string | number | URL | null | undefined;
|
||||
}): React.ReactElement | null {
|
||||
if (typeof content !== "undefined" && content !== null && content !== "") {
|
||||
return (
|
||||
<meta
|
||||
{...(name ? { name } : { property })}
|
||||
{...(media ? { media } : undefined)}
|
||||
content={typeof content === "string" ? content : content.toString()}
|
||||
/>
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
type ExtendMetaContent = Record<
|
||||
string,
|
||||
undefined | string | URL | number | boolean | null | undefined
|
||||
>;
|
||||
type MultiMetaContent =
|
||||
| (ExtendMetaContent | string | URL | number)[]
|
||||
| null
|
||||
| undefined;
|
||||
|
||||
function camelToSnake(camelCaseStr: string) {
|
||||
return camelCaseStr.replace(/([A-Z])/g, function (match) {
|
||||
return "_" + match.toLowerCase();
|
||||
});
|
||||
}
|
||||
|
||||
function getMetaKey(prefix: string, key: string) {
|
||||
// Use `twitter:image` and `og:image` instead of `twitter:image:url` and `og:image:url`
|
||||
// to be more compatible as it's a more common format
|
||||
if ((prefix === "og:image" || prefix === "twitter:image") && key === "url") {
|
||||
return prefix;
|
||||
}
|
||||
if (prefix.startsWith("og:") || prefix.startsWith("twitter:")) {
|
||||
key = camelToSnake(key);
|
||||
}
|
||||
return prefix + ":" + key;
|
||||
}
|
||||
|
||||
function ExtendMeta({
|
||||
content,
|
||||
namePrefix,
|
||||
propertyPrefix,
|
||||
}: {
|
||||
content?: ExtendMetaContent;
|
||||
namePrefix?: string;
|
||||
propertyPrefix?: string;
|
||||
}) {
|
||||
const keyPrefix = namePrefix || propertyPrefix;
|
||||
if (!content) return null;
|
||||
return (
|
||||
<React.Fragment>
|
||||
{Object.entries(content).map(([k, v], index) => {
|
||||
return typeof v === "undefined" ? null : (
|
||||
<Meta
|
||||
key={keyPrefix + ":" + k + "_" + index}
|
||||
{...(propertyPrefix && { property: getMetaKey(propertyPrefix, k) })}
|
||||
{...(namePrefix && { name: getMetaKey(namePrefix, k) })}
|
||||
content={typeof v === "string" ? v : v?.toString()}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</React.Fragment>
|
||||
);
|
||||
}
|
||||
|
||||
export function MultiMeta({
|
||||
propertyPrefix,
|
||||
namePrefix,
|
||||
contents,
|
||||
}: {
|
||||
propertyPrefix?: string;
|
||||
namePrefix?: string;
|
||||
contents?: MultiMetaContent | null;
|
||||
}) {
|
||||
if (typeof contents === "undefined" || contents === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const keyPrefix = propertyPrefix || namePrefix;
|
||||
return (
|
||||
<>
|
||||
{contents.map((content, index) => {
|
||||
if (
|
||||
typeof content === "string" ||
|
||||
typeof content === "number" ||
|
||||
content instanceof URL
|
||||
) {
|
||||
return (
|
||||
<Meta
|
||||
key={keyPrefix + "_" + index}
|
||||
{...(propertyPrefix
|
||||
? { property: propertyPrefix }
|
||||
: { name: namePrefix })}
|
||||
content={content}
|
||||
/>
|
||||
);
|
||||
} else {
|
||||
return (
|
||||
<ExtendMeta
|
||||
key={keyPrefix + "_" + index}
|
||||
namePrefix={namePrefix}
|
||||
propertyPrefix={propertyPrefix}
|
||||
content={content}
|
||||
/>
|
||||
);
|
||||
}
|
||||
})}
|
||||
</>
|
||||
);
|
||||
}
|
316
framework/meta/nextjs/generate/opengraph.tsx
Normal file
316
framework/meta/nextjs/generate/opengraph.tsx
Normal file
|
@ -0,0 +1,316 @@
|
|||
import type { ResolvedMetadata } from "../types/metadata-interface";
|
||||
import type { TwitterAppDescriptor } from "../types/twitter-types";
|
||||
|
||||
import React from "react";
|
||||
import { Meta, MultiMeta } from "./meta";
|
||||
|
||||
export function OpenGraphMetadata({
|
||||
openGraph,
|
||||
}: {
|
||||
openGraph: ResolvedMetadata["openGraph"];
|
||||
}) {
|
||||
if (!openGraph) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let typedOpenGraph;
|
||||
if ("type" in openGraph) {
|
||||
switch (openGraph.type) {
|
||||
case "website":
|
||||
typedOpenGraph = <Meta property="og:type" content="website" />;
|
||||
break;
|
||||
case "article":
|
||||
typedOpenGraph = (
|
||||
<>
|
||||
<Meta property="og:type" content="article" />
|
||||
<Meta
|
||||
property="article:published_time"
|
||||
content={openGraph.publishedTime?.toString()}
|
||||
/>
|
||||
<Meta
|
||||
property="article:modified_time"
|
||||
content={openGraph.modifiedTime?.toString()}
|
||||
/>
|
||||
<Meta
|
||||
property="article:expiration_time"
|
||||
content={openGraph.expirationTime?.toString()}
|
||||
/>
|
||||
<MultiMeta
|
||||
propertyPrefix="article:author"
|
||||
contents={openGraph.authors}
|
||||
/>
|
||||
<Meta property="article:section" content={openGraph.section} />
|
||||
<MultiMeta propertyPrefix="article:tag" contents={openGraph.tags} />
|
||||
</>
|
||||
);
|
||||
break;
|
||||
case "book":
|
||||
typedOpenGraph = (
|
||||
<>
|
||||
<Meta property="og:type" content="book" />
|
||||
<Meta property="book:isbn" content={openGraph.isbn} />
|
||||
<Meta
|
||||
property="book:release_date"
|
||||
content={openGraph.releaseDate}
|
||||
/>
|
||||
<MultiMeta
|
||||
propertyPrefix="book:author"
|
||||
contents={openGraph.authors}
|
||||
/>
|
||||
<MultiMeta propertyPrefix="book:tag" contents={openGraph.tags} />
|
||||
</>
|
||||
);
|
||||
break;
|
||||
case "profile":
|
||||
typedOpenGraph = (
|
||||
<>
|
||||
<Meta property="og:type" content="profile" />
|
||||
<Meta property="profile:first_name" content={openGraph.firstName} />
|
||||
<Meta property="profile:last_name" content={openGraph.lastName} />
|
||||
<Meta property="profile:username" content={openGraph.username} />
|
||||
<Meta property="profile:gender" content={openGraph.gender} />
|
||||
</>
|
||||
);
|
||||
break;
|
||||
case "music.song":
|
||||
typedOpenGraph = (
|
||||
<>
|
||||
<Meta property="og:type" content="music.song" />
|
||||
<Meta
|
||||
property="music:duration"
|
||||
content={openGraph.duration?.toString()}
|
||||
/>
|
||||
<MultiMeta
|
||||
propertyPrefix="music:album"
|
||||
contents={openGraph.albums}
|
||||
/>
|
||||
<MultiMeta
|
||||
propertyPrefix="music:musician"
|
||||
contents={openGraph.musicians}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
break;
|
||||
case "music.album":
|
||||
typedOpenGraph = (
|
||||
<>
|
||||
<Meta property="og:type" content="music.album" />
|
||||
<MultiMeta propertyPrefix="music:song" contents={openGraph.songs} />
|
||||
<MultiMeta
|
||||
propertyPrefix="music:musician"
|
||||
contents={openGraph.musicians}
|
||||
/>
|
||||
<Meta
|
||||
property="music:release_date"
|
||||
content={openGraph.releaseDate}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
break;
|
||||
case "music.playlist":
|
||||
typedOpenGraph = (
|
||||
<>
|
||||
<Meta property="og:type" content="music.playlist" />
|
||||
<MultiMeta propertyPrefix="music:song" contents={openGraph.songs} />
|
||||
<MultiMeta
|
||||
propertyPrefix="music:creator"
|
||||
contents={openGraph.creators}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
break;
|
||||
case "music.radio_station":
|
||||
typedOpenGraph = (
|
||||
<>
|
||||
<Meta property="og:type" content="music.radio_station" />
|
||||
<MultiMeta
|
||||
propertyPrefix="music:creator"
|
||||
contents={openGraph.creators}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
break;
|
||||
case "video.movie":
|
||||
typedOpenGraph = (
|
||||
<>
|
||||
<Meta property="og:type" content="video.movie" />
|
||||
<MultiMeta
|
||||
propertyPrefix="video:actor"
|
||||
contents={openGraph.actors}
|
||||
/>
|
||||
<MultiMeta
|
||||
propertyPrefix="video:director"
|
||||
contents={openGraph.directors}
|
||||
/>
|
||||
<MultiMeta
|
||||
propertyPrefix="video:writer"
|
||||
contents={openGraph.writers}
|
||||
/>
|
||||
<Meta property="video:duration" content={openGraph.duration} />
|
||||
<Meta
|
||||
property="video:release_date"
|
||||
content={openGraph.releaseDate}
|
||||
/>
|
||||
<MultiMeta propertyPrefix="video:tag" contents={openGraph.tags} />
|
||||
</>
|
||||
);
|
||||
break;
|
||||
case "video.episode":
|
||||
typedOpenGraph = (
|
||||
<>
|
||||
<Meta property="og:type" content="video.episode" />
|
||||
<MultiMeta
|
||||
propertyPrefix="video:actor"
|
||||
contents={openGraph.actors}
|
||||
/>
|
||||
<MultiMeta
|
||||
propertyPrefix="video:director"
|
||||
contents={openGraph.directors}
|
||||
/>
|
||||
<MultiMeta
|
||||
propertyPrefix="video:writer"
|
||||
contents={openGraph.writers}
|
||||
/>
|
||||
<Meta property="video:duration" content={openGraph.duration} />
|
||||
<Meta
|
||||
property="video:release_date"
|
||||
content={openGraph.releaseDate}
|
||||
/>
|
||||
<MultiMeta propertyPrefix="video:tag" contents={openGraph.tags} />
|
||||
<Meta property="video:series" content={openGraph.series} />
|
||||
</>
|
||||
);
|
||||
break;
|
||||
case "video.tv_show":
|
||||
typedOpenGraph = <Meta property="og:type" content="video.tv_show" />;
|
||||
break;
|
||||
case "video.other":
|
||||
typedOpenGraph = <Meta property="og:type" content="video.other" />;
|
||||
break;
|
||||
default:
|
||||
throw new Error("Invalid OpenGraph type: " + (openGraph as any).type);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Meta property="og:determiner" content={openGraph.determiner} />
|
||||
<Meta property="og:title" content={openGraph.title?.absolute} />
|
||||
<Meta property="og:description" content={openGraph.description} />
|
||||
<Meta property="og:url" content={openGraph.url?.toString()} />
|
||||
<Meta property="og:site_name" content={openGraph.siteName} />
|
||||
<Meta property="og:locale" content={openGraph.locale} />
|
||||
<Meta property="og:country_name" content={openGraph.countryName} />
|
||||
<Meta property="og:ttl" content={openGraph.ttl?.toString()} />
|
||||
<MultiMeta propertyPrefix="og:image" contents={openGraph.images} />
|
||||
<MultiMeta propertyPrefix="og:video" contents={openGraph.videos} />
|
||||
<MultiMeta propertyPrefix="og:audio" contents={openGraph.audio} />
|
||||
<MultiMeta propertyPrefix="og:email" contents={openGraph.emails} />
|
||||
<MultiMeta
|
||||
propertyPrefix="og:phone_number"
|
||||
contents={openGraph.phoneNumbers}
|
||||
/>
|
||||
<MultiMeta
|
||||
propertyPrefix="og:fax_number"
|
||||
contents={openGraph.faxNumbers}
|
||||
/>
|
||||
<MultiMeta
|
||||
propertyPrefix="og:locale:alternate"
|
||||
contents={openGraph.alternateLocale}
|
||||
/>
|
||||
{typedOpenGraph}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function TwitterAppItem({
|
||||
app,
|
||||
type,
|
||||
}: {
|
||||
app: TwitterAppDescriptor;
|
||||
type: "iphone" | "ipad" | "googleplay";
|
||||
}) {
|
||||
return (
|
||||
<>
|
||||
<Meta name={`twitter:app:name:${type}`} content={app.name} />
|
||||
<Meta name={`twitter:app:id:${type}`} content={app.id[type]} />
|
||||
<Meta
|
||||
name={`twitter:app:url:${type}`}
|
||||
content={app.url?.[type]?.toString()}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export function TwitterMetadata({
|
||||
twitter,
|
||||
}: {
|
||||
twitter: ResolvedMetadata["twitter"];
|
||||
}) {
|
||||
if (!twitter) return null;
|
||||
const { card } = twitter;
|
||||
|
||||
return (
|
||||
<>
|
||||
<Meta name="twitter:card" content={card} />
|
||||
<Meta name="twitter:site" content={twitter.site} />
|
||||
<Meta name="twitter:site:id" content={twitter.siteId} />
|
||||
<Meta name="twitter:creator" content={twitter.creator} />
|
||||
<Meta name="twitter:creator:id" content={twitter.creatorId} />
|
||||
<Meta name="twitter:title" content={twitter.title?.absolute} />
|
||||
<Meta name="twitter:description" content={twitter.description} />
|
||||
<MultiMeta namePrefix="twitter:image" contents={twitter.images} />
|
||||
{card === "player"
|
||||
? twitter.players.map((player, index) => (
|
||||
<React.Fragment key={index}>
|
||||
<Meta
|
||||
name="twitter:player"
|
||||
content={player.playerUrl.toString()}
|
||||
/>
|
||||
<Meta
|
||||
name="twitter:player:stream"
|
||||
content={player.streamUrl.toString()}
|
||||
/>
|
||||
<Meta name="twitter:player:width" content={player.width} />
|
||||
<Meta name="twitter:player:height" content={player.height} />
|
||||
</React.Fragment>
|
||||
))
|
||||
: null}
|
||||
{card === "app"
|
||||
? (
|
||||
<>
|
||||
<TwitterAppItem app={twitter.app} type="iphone" />
|
||||
<TwitterAppItem app={twitter.app} type="ipad" />
|
||||
<TwitterAppItem app={twitter.app} type="googleplay" />
|
||||
</>
|
||||
)
|
||||
: null}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export function AppLinksMeta({
|
||||
appLinks,
|
||||
}: {
|
||||
appLinks: ResolvedMetadata["appLinks"];
|
||||
}) {
|
||||
if (!appLinks) return null;
|
||||
return (
|
||||
<>
|
||||
<MultiMeta propertyPrefix="al:ios" contents={appLinks.ios} />
|
||||
<MultiMeta propertyPrefix="al:iphone" contents={appLinks.iphone} />
|
||||
<MultiMeta propertyPrefix="al:ipad" contents={appLinks.ipad} />
|
||||
<MultiMeta propertyPrefix="al:android" contents={appLinks.android} />
|
||||
<MultiMeta
|
||||
propertyPrefix="al:windows_phone"
|
||||
contents={appLinks.windows_phone}
|
||||
/>
|
||||
<MultiMeta propertyPrefix="al:windows" contents={appLinks.windows} />
|
||||
<MultiMeta
|
||||
propertyPrefix="al:windows_universal"
|
||||
contents={appLinks.windows_universal}
|
||||
/>
|
||||
<MultiMeta propertyPrefix="al:web" contents={appLinks.web} />
|
||||
</>
|
||||
);
|
||||
}
|
20
framework/meta/nextjs/generate/utils.ts
Normal file
20
framework/meta/nextjs/generate/utils.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
function resolveArray<T>(value: T): T[] {
|
||||
if (Array.isArray(value)) {
|
||||
return value;
|
||||
}
|
||||
return [value];
|
||||
}
|
||||
|
||||
function resolveAsArrayOrUndefined<T extends unknown | readonly unknown[]>(
|
||||
value: T | T[] | undefined | null,
|
||||
): undefined | T[] {
|
||||
if (typeof value === "undefined" || value === null) {
|
||||
return undefined;
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return value;
|
||||
}
|
||||
return [value];
|
||||
}
|
||||
|
||||
export { resolveArray, resolveAsArrayOrUndefined };
|
67
framework/meta/nextjs/get-metadata-route.ts
Normal file
67
framework/meta/nextjs/get-metadata-route.ts
Normal file
|
@ -0,0 +1,67 @@
|
|||
import { isMetadataRoute, isMetadataRouteFile } from "./is-metadata-route";
|
||||
import path from "../../shared/lib/isomorphic/path";
|
||||
import { djb2Hash } from "../../shared/lib/hash";
|
||||
|
||||
/*
|
||||
* If there's special convention like (...) or @ in the page path,
|
||||
* Give it a unique hash suffix to avoid conflicts
|
||||
*
|
||||
* e.g.
|
||||
* /app/open-graph.tsx -> /open-graph/route
|
||||
* /app/(post)/open-graph.tsx -> /open-graph/route-[0-9a-z]{6}
|
||||
*/
|
||||
export function getMetadataRouteSuffix(page: string) {
|
||||
let suffix = "";
|
||||
|
||||
if ((page.includes("(") && page.includes(")")) || page.includes("@")) {
|
||||
suffix = djb2Hash(page).toString(36).slice(0, 6);
|
||||
}
|
||||
return suffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map metadata page key to the corresponding route
|
||||
*
|
||||
* static file page key: /app/robots.txt -> /robots.xml -> /robots.txt/route
|
||||
* dynamic route page key: /app/robots.tsx -> /robots -> /robots.txt/route
|
||||
*
|
||||
* @param page
|
||||
* @returns
|
||||
*/
|
||||
export function normalizeMetadataRoute(page: string) {
|
||||
let route = page;
|
||||
if (isMetadataRoute(page)) {
|
||||
// Remove the file extension, e.g. /route-path/robots.txt -> /route-path
|
||||
const pathnamePrefix = page.slice(0, -(path.basename(page).length + 1));
|
||||
const suffix = getMetadataRouteSuffix(pathnamePrefix);
|
||||
|
||||
if (route === "/sitemap") {
|
||||
route += ".xml";
|
||||
}
|
||||
if (route === "/robots") {
|
||||
route += ".txt";
|
||||
}
|
||||
if (route === "/manifest") {
|
||||
route += ".webmanifest";
|
||||
}
|
||||
// Support both /<metadata-route.ext> and custom routes /<metadata-route>/route.ts.
|
||||
// If it's a metadata file route, we need to append /[id]/route to the page.
|
||||
if (!route.endsWith("/route")) {
|
||||
const isStaticMetadataFile = isMetadataRouteFile(route, [], true);
|
||||
const { dir, name: baseName, ext } = path.parse(route);
|
||||
|
||||
const isSingleRoute = page.startsWith("/sitemap") ||
|
||||
page.startsWith("/robots") ||
|
||||
page.startsWith("/manifest") ||
|
||||
isStaticMetadataFile;
|
||||
|
||||
route = path.join(
|
||||
dir,
|
||||
`${baseName}${suffix ? `-${suffix}` : ""}${ext}`,
|
||||
isSingleRoute ? "" : "[[...__metadata_id__]]",
|
||||
"route",
|
||||
);
|
||||
}
|
||||
}
|
||||
return route;
|
||||
}
|
136
framework/meta/nextjs/is-metadata-route.ts
Normal file
136
framework/meta/nextjs/is-metadata-route.ts
Normal file
|
@ -0,0 +1,136 @@
|
|||
export const STATIC_METADATA_IMAGES = {
|
||||
icon: {
|
||||
filename: "icon",
|
||||
extensions: ["ico", "jpg", "jpeg", "png", "svg"],
|
||||
},
|
||||
apple: {
|
||||
filename: "apple-icon",
|
||||
extensions: ["jpg", "jpeg", "png"],
|
||||
},
|
||||
favicon: {
|
||||
filename: "favicon",
|
||||
extensions: ["ico"],
|
||||
},
|
||||
openGraph: {
|
||||
filename: "opengraph-image",
|
||||
extensions: ["jpg", "jpeg", "png", "gif"],
|
||||
},
|
||||
twitter: {
|
||||
filename: "twitter-image",
|
||||
extensions: ["jpg", "jpeg", "png", "gif"],
|
||||
},
|
||||
} as const;
|
||||
|
||||
// Match routes that are metadata routes, e.g. /sitemap.xml, /favicon.<ext>, /<icon>.<ext>, etc.
|
||||
// TODO-METADATA: support more metadata routes with more extensions
|
||||
const defaultExtensions = ["js", "jsx", "ts", "tsx"];
|
||||
|
||||
const getExtensionRegexString = (extensions: readonly string[]) =>
|
||||
`(?:${extensions.join("|")})`;
|
||||
|
||||
// When you only pass the file extension as `[]`, it will only match the static convention files
|
||||
// e.g. /robots.txt, /sitemap.xml, /favicon.ico, /manifest.json
|
||||
// When you pass the file extension as `['js', 'jsx', 'ts', 'tsx']`, it will also match the dynamic convention files
|
||||
// e.g. /robots.js, /sitemap.tsx, /favicon.jsx, /manifest.ts
|
||||
// When `withExtension` is false, it will match the static convention files without the extension, by default it's true
|
||||
// e.g. /robots, /sitemap, /favicon, /manifest, use to match dynamic API routes like app/robots.ts
|
||||
export function isMetadataRouteFile(
|
||||
appDirRelativePath: string,
|
||||
pageExtensions: string[],
|
||||
withExtension: boolean,
|
||||
) {
|
||||
const metadataRouteFilesRegex = [
|
||||
new RegExp(
|
||||
`^[\\\\/]robots${
|
||||
withExtension
|
||||
? `\\.${getExtensionRegexString(pageExtensions.concat("txt"))}`
|
||||
: ""
|
||||
}`,
|
||||
),
|
||||
new RegExp(
|
||||
`^[\\\\/]sitemap${
|
||||
withExtension
|
||||
? `\\.${getExtensionRegexString(pageExtensions.concat("xml"))}`
|
||||
: ""
|
||||
}`,
|
||||
),
|
||||
new RegExp(
|
||||
`^[\\\\/]manifest${
|
||||
withExtension
|
||||
? `\\.${
|
||||
getExtensionRegexString(
|
||||
pageExtensions.concat("webmanifest", "json"),
|
||||
)
|
||||
}`
|
||||
: ""
|
||||
}`,
|
||||
),
|
||||
new RegExp(`^[\\\\/]favicon\\.ico$`),
|
||||
// TODO-METADATA: add dynamic routes for metadata images
|
||||
new RegExp(
|
||||
`[\\\\/]${STATIC_METADATA_IMAGES.icon.filename}${
|
||||
withExtension
|
||||
? `\\.${
|
||||
getExtensionRegexString(
|
||||
pageExtensions.concat(STATIC_METADATA_IMAGES.icon.extensions),
|
||||
)
|
||||
}`
|
||||
: ""
|
||||
}`,
|
||||
),
|
||||
new RegExp(
|
||||
`[\\\\/]${STATIC_METADATA_IMAGES.apple.filename}${
|
||||
withExtension
|
||||
? `\\.${
|
||||
getExtensionRegexString(
|
||||
pageExtensions.concat(STATIC_METADATA_IMAGES.apple.extensions),
|
||||
)
|
||||
}`
|
||||
: ""
|
||||
}`,
|
||||
),
|
||||
new RegExp(
|
||||
`[\\\\/]${STATIC_METADATA_IMAGES.openGraph.filename}${
|
||||
withExtension
|
||||
? `\\.${
|
||||
getExtensionRegexString(
|
||||
pageExtensions.concat(
|
||||
STATIC_METADATA_IMAGES.openGraph.extensions,
|
||||
),
|
||||
)
|
||||
}`
|
||||
: ""
|
||||
}`,
|
||||
),
|
||||
new RegExp(
|
||||
`[\\\\/]${STATIC_METADATA_IMAGES.twitter.filename}${
|
||||
withExtension
|
||||
? `\\.${
|
||||
getExtensionRegexString(
|
||||
pageExtensions.concat(STATIC_METADATA_IMAGES.twitter.extensions),
|
||||
)
|
||||
}`
|
||||
: ""
|
||||
}`,
|
||||
),
|
||||
];
|
||||
|
||||
return metadataRouteFilesRegex.some((r) => r.test(appDirRelativePath));
|
||||
}
|
||||
|
||||
/*
|
||||
* Remove the 'app' prefix or '/route' suffix, only check the route name since they're only allowed in root app directory
|
||||
* e.g.
|
||||
* /app/robots -> /robots
|
||||
* app/robots -> /robots
|
||||
* /robots -> /robots
|
||||
*/
|
||||
export function isMetadataRoute(route: string): boolean {
|
||||
let page = route.replace(/^\/?app\//, "").replace(/\/route$/, "");
|
||||
if (page[0] !== "/") page = "/" + page;
|
||||
|
||||
return (
|
||||
!page.endsWith("/page") &&
|
||||
isMetadataRouteFile(page, defaultExtensions, false)
|
||||
);
|
||||
}
|
58
framework/meta/nextjs/metadata.tsx
Normal file
58
framework/meta/nextjs/metadata.tsx
Normal file
|
@ -0,0 +1,58 @@
|
|||
import React from "react";
|
||||
import {
|
||||
AppleWebAppMeta,
|
||||
BasicMetadata,
|
||||
FormatDetectionMeta,
|
||||
ItunesMeta,
|
||||
VerificationMeta,
|
||||
} from "./generate/basic";
|
||||
import { AlternatesMetadata } from "./generate/alternate";
|
||||
import {
|
||||
AppLinksMeta,
|
||||
OpenGraphMetadata,
|
||||
TwitterMetadata,
|
||||
} from "./generate/opengraph";
|
||||
import { IconsMetadata } from "./generate/icons";
|
||||
import { accumulateMetadata, resolveMetadata } from "./resolve-metadata";
|
||||
import { LoaderTree } from "../../server/lib/app-dir-module";
|
||||
import { GetDynamicParamFromSegment } from "../../server/app-render/app-render";
|
||||
|
||||
// Generate the actual React elements from the resolved metadata.
|
||||
export async function MetadataTree({
|
||||
tree,
|
||||
pathname,
|
||||
searchParams,
|
||||
getDynamicParamFromSegment,
|
||||
}: {
|
||||
tree: LoaderTree;
|
||||
pathname: string;
|
||||
searchParams: { [key: string]: any };
|
||||
getDynamicParamFromSegment: GetDynamicParamFromSegment;
|
||||
}) {
|
||||
const options = {
|
||||
pathname,
|
||||
};
|
||||
const resolvedMetadata = await resolveMetadata({
|
||||
tree,
|
||||
parentParams: {},
|
||||
metadataItems: [],
|
||||
searchParams,
|
||||
getDynamicParamFromSegment,
|
||||
});
|
||||
const metadata = await accumulateMetadata(resolvedMetadata, options);
|
||||
|
||||
return (
|
||||
<>
|
||||
<BasicMetadata metadata={metadata} />
|
||||
<AlternatesMetadata alternates={metadata.alternates} />
|
||||
<ItunesMeta itunes={metadata.itunes} />
|
||||
<FormatDetectionMeta formatDetection={metadata.formatDetection} />
|
||||
<VerificationMeta verification={metadata.verification} />
|
||||
<AppleWebAppMeta appleWebApp={metadata.appleWebApp} />
|
||||
<OpenGraphMetadata openGraph={metadata.openGraph} />
|
||||
<TwitterMetadata twitter={metadata.twitter} />
|
||||
<AppLinksMeta appLinks={metadata.appLinks} />
|
||||
<IconsMetadata icons={metadata.icons} />
|
||||
</>
|
||||
);
|
||||
}
|
453
framework/meta/nextjs/resolve-metadata.ts
Normal file
453
framework/meta/nextjs/resolve-metadata.ts
Normal file
|
@ -0,0 +1,453 @@
|
|||
import type {
|
||||
Metadata,
|
||||
ResolvedMetadata,
|
||||
ResolvingMetadata,
|
||||
} from "./types/metadata-interface";
|
||||
import type { MetadataImageModule } from "../../build/webpack/loaders/metadata/types";
|
||||
import type { GetDynamicParamFromSegment } from "../../server/app-render/app-render";
|
||||
import { createDefaultMetadata } from "./default-metadata";
|
||||
import {
|
||||
resolveOpenGraph,
|
||||
resolveTwitter,
|
||||
} from "./resolvers/resolve-opengraph";
|
||||
import { resolveTitle } from "./resolvers/resolve-title";
|
||||
import { resolveAsArrayOrUndefined } from "./generate/utils";
|
||||
import { isClientReference } from "../client-reference";
|
||||
import {
|
||||
getLayoutOrPageModule,
|
||||
LoaderTree,
|
||||
} from "../../server/lib/app-dir-module";
|
||||
import { ComponentsType } from "../../build/webpack/loaders/next-app-loader";
|
||||
import { interopDefault } from "../interop-default";
|
||||
import {
|
||||
resolveAlternates,
|
||||
resolveAppleWebApp,
|
||||
resolveAppLinks,
|
||||
resolveRobots,
|
||||
resolveThemeColor,
|
||||
resolveVerification,
|
||||
resolveViewport,
|
||||
} from "./resolvers/resolve-basics";
|
||||
import { resolveIcons } from "./resolvers/resolve-icons";
|
||||
import { getTracer } from "../../server/lib/trace/tracer";
|
||||
import { ResolveMetadataSpan } from "../../server/lib/trace/constants";
|
||||
import { Twitter } from "./types/twitter-types";
|
||||
import { OpenGraph } from "./types/opengraph-types";
|
||||
import { PAGE_SEGMENT_KEY } from "../../shared/lib/constants";
|
||||
import process from "node:process";
|
||||
|
||||
type StaticMetadata = Awaited<ReturnType<typeof resolveStaticMetadata>>;
|
||||
|
||||
type MetadataResolver = (
|
||||
_parent: ResolvingMetadata,
|
||||
) => Metadata | Promise<Metadata>;
|
||||
export type MetadataItems = [
|
||||
Metadata | MetadataResolver | null,
|
||||
StaticMetadata,
|
||||
][];
|
||||
|
||||
function mergeStaticMetadata(
|
||||
metadata: ResolvedMetadata,
|
||||
staticFilesMetadata: StaticMetadata,
|
||||
) {
|
||||
if (!staticFilesMetadata) return;
|
||||
const { icon, apple, openGraph, twitter, manifest } = staticFilesMetadata;
|
||||
if (icon || apple) {
|
||||
metadata.icons = {
|
||||
icon: icon || [],
|
||||
apple: apple || [],
|
||||
};
|
||||
}
|
||||
if (twitter) {
|
||||
const resolvedTwitter = resolveTwitter(
|
||||
{ ...metadata.twitter, images: twitter } as Twitter,
|
||||
metadata.metadataBase,
|
||||
);
|
||||
metadata.twitter = resolvedTwitter;
|
||||
}
|
||||
|
||||
if (openGraph) {
|
||||
const resolvedOpenGraph = resolveOpenGraph(
|
||||
{ ...metadata.openGraph, images: openGraph } as OpenGraph,
|
||||
metadata.metadataBase,
|
||||
);
|
||||
metadata.openGraph = resolvedOpenGraph;
|
||||
}
|
||||
if (manifest) {
|
||||
metadata.manifest = manifest;
|
||||
}
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
// Merge the source metadata into the resolved target metadata.
|
||||
function merge({
|
||||
target,
|
||||
source,
|
||||
staticFilesMetadata,
|
||||
titleTemplates,
|
||||
options,
|
||||
}: {
|
||||
target: ResolvedMetadata;
|
||||
source: Metadata | null;
|
||||
staticFilesMetadata: StaticMetadata;
|
||||
titleTemplates: {
|
||||
title: string | null;
|
||||
twitter: string | null;
|
||||
openGraph: string | null;
|
||||
};
|
||||
options: MetadataAccumulationOptions;
|
||||
}) {
|
||||
// If there's override metadata, prefer it otherwise fallback to the default metadata.
|
||||
const metadataBase = typeof source?.metadataBase !== "undefined"
|
||||
? source.metadataBase
|
||||
: target.metadataBase;
|
||||
for (const key_ in source) {
|
||||
const key = key_ as keyof Metadata;
|
||||
|
||||
switch (key) {
|
||||
case "title": {
|
||||
target.title = resolveTitle(source.title, titleTemplates.title);
|
||||
break;
|
||||
}
|
||||
case "alternates": {
|
||||
target.alternates = resolveAlternates(source.alternates, metadataBase, {
|
||||
pathname: options.pathname,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case "openGraph": {
|
||||
target.openGraph = resolveOpenGraph(source.openGraph, metadataBase);
|
||||
if (target.openGraph) {
|
||||
target.openGraph.title = resolveTitle(
|
||||
target.openGraph.title,
|
||||
titleTemplates.openGraph,
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "twitter": {
|
||||
target.twitter = resolveTwitter(source.twitter, metadataBase);
|
||||
if (target.twitter) {
|
||||
target.twitter.title = resolveTitle(
|
||||
target.twitter.title,
|
||||
titleTemplates.twitter,
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "verification":
|
||||
target.verification = resolveVerification(source.verification);
|
||||
break;
|
||||
case "viewport": {
|
||||
target.viewport = resolveViewport(source.viewport);
|
||||
break;
|
||||
}
|
||||
case "icons": {
|
||||
target.icons = resolveIcons(source.icons);
|
||||
break;
|
||||
}
|
||||
case "appleWebApp":
|
||||
target.appleWebApp = resolveAppleWebApp(source.appleWebApp);
|
||||
break;
|
||||
case "appLinks":
|
||||
target.appLinks = resolveAppLinks(source.appLinks);
|
||||
break;
|
||||
case "robots": {
|
||||
target.robots = resolveRobots(source.robots);
|
||||
break;
|
||||
}
|
||||
case "themeColor": {
|
||||
target.themeColor = resolveThemeColor(source.themeColor);
|
||||
break;
|
||||
}
|
||||
case "archives":
|
||||
case "assets":
|
||||
case "bookmarks":
|
||||
case "keywords":
|
||||
case "authors": {
|
||||
// FIXME: type inferring
|
||||
// @ts-ignore
|
||||
target[key] = resolveAsArrayOrUndefined(source[key]) || null;
|
||||
break;
|
||||
}
|
||||
// directly assign fields that fallback to null
|
||||
case "applicationName":
|
||||
case "description":
|
||||
case "generator":
|
||||
case "creator":
|
||||
case "publisher":
|
||||
case "category":
|
||||
case "classification":
|
||||
case "referrer":
|
||||
case "colorScheme":
|
||||
case "itunes":
|
||||
case "formatDetection":
|
||||
case "manifest":
|
||||
// @ts-ignore TODO: support inferring
|
||||
target[key] = source[key] || null;
|
||||
break;
|
||||
case "other":
|
||||
target.other = Object.assign({}, target.other, source.other);
|
||||
break;
|
||||
case "metadataBase":
|
||||
target.metadataBase = metadataBase;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
mergeStaticMetadata(target, staticFilesMetadata);
|
||||
}
|
||||
|
||||
async function getDefinedMetadata(
|
||||
mod: any,
|
||||
props: any,
|
||||
route: string,
|
||||
): Promise<Metadata | MetadataResolver | null> {
|
||||
// Layer is a client component, we just skip it. It can't have metadata exported.
|
||||
// Return early to avoid accessing properties error for client references.
|
||||
if (isClientReference(mod)) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
(mod.generateMetadata
|
||||
? (parent: ResolvingMetadata) =>
|
||||
getTracer().trace(
|
||||
ResolveMetadataSpan.generateMetadata,
|
||||
{
|
||||
spanName: `generateMetadata ${route}`,
|
||||
attributes: {
|
||||
"next.page": route,
|
||||
},
|
||||
},
|
||||
() => mod.generateMetadata(props, parent),
|
||||
)
|
||||
: mod.metadata) || null
|
||||
);
|
||||
}
|
||||
|
||||
async function collectStaticImagesFiles(
|
||||
metadata: ComponentsType["metadata"],
|
||||
props: any,
|
||||
type: keyof NonNullable<ComponentsType["metadata"]>,
|
||||
) {
|
||||
if (!metadata?.[type]) return undefined;
|
||||
|
||||
const iconPromises = metadata[type as "icon" | "apple"].map(
|
||||
async (imageModule: (p: any) => Promise<MetadataImageModule[]>) =>
|
||||
interopDefault(await imageModule(props)),
|
||||
);
|
||||
|
||||
return iconPromises?.length > 0
|
||||
? (await Promise.all(iconPromises))?.flat()
|
||||
: undefined;
|
||||
}
|
||||
|
||||
async function resolveStaticMetadata(components: ComponentsType, props: any) {
|
||||
const { metadata } = components;
|
||||
if (!metadata) return null;
|
||||
|
||||
const [icon, apple, openGraph, twitter] = await Promise.all([
|
||||
collectStaticImagesFiles(metadata, props, "icon"),
|
||||
collectStaticImagesFiles(metadata, props, "apple"),
|
||||
collectStaticImagesFiles(metadata, props, "openGraph"),
|
||||
collectStaticImagesFiles(metadata, props, "twitter"),
|
||||
]);
|
||||
|
||||
const staticMetadata = {
|
||||
icon,
|
||||
apple,
|
||||
openGraph,
|
||||
twitter,
|
||||
manifest: metadata.manifest,
|
||||
};
|
||||
|
||||
return staticMetadata;
|
||||
}
|
||||
|
||||
// [layout.metadata, static files metadata] -> ... -> [page.metadata, static files metadata]
|
||||
export async function collectMetadata({
|
||||
tree,
|
||||
metadataItems: array,
|
||||
props,
|
||||
route,
|
||||
}: {
|
||||
tree: LoaderTree;
|
||||
metadataItems: MetadataItems;
|
||||
props: any;
|
||||
route: string;
|
||||
}) {
|
||||
const [mod, modType] = await getLayoutOrPageModule(tree);
|
||||
|
||||
if (modType) {
|
||||
route += `/${modType}`;
|
||||
}
|
||||
|
||||
const staticFilesMetadata = await resolveStaticMetadata(tree[2], props);
|
||||
const metadataExport = mod
|
||||
? await getDefinedMetadata(mod, props, route)
|
||||
: null;
|
||||
|
||||
array.push([metadataExport, staticFilesMetadata]);
|
||||
}
|
||||
|
||||
export async function resolveMetadata({
|
||||
tree,
|
||||
parentParams,
|
||||
metadataItems,
|
||||
treePrefix = [],
|
||||
getDynamicParamFromSegment,
|
||||
searchParams,
|
||||
}: {
|
||||
tree: LoaderTree;
|
||||
parentParams: { [key: string]: any };
|
||||
metadataItems: MetadataItems;
|
||||
/** Provided tree can be nested subtree, this argument says what is the path of such subtree */
|
||||
treePrefix?: string[];
|
||||
getDynamicParamFromSegment: GetDynamicParamFromSegment;
|
||||
searchParams: { [key: string]: any };
|
||||
}): Promise<MetadataItems> {
|
||||
const [segment, parallelRoutes, { page }] = tree;
|
||||
const currentTreePrefix = [...treePrefix, segment];
|
||||
const isPage = typeof page !== "undefined";
|
||||
// Handle dynamic segment params.
|
||||
const segmentParam = getDynamicParamFromSegment(segment);
|
||||
/**
|
||||
* Create object holding the parent params and current params
|
||||
*/
|
||||
const currentParams =
|
||||
// Handle null case where dynamic param is optional
|
||||
segmentParam && segmentParam.value !== null
|
||||
? {
|
||||
...parentParams,
|
||||
[segmentParam.param]: segmentParam.value,
|
||||
}
|
||||
// Pass through parent params to children
|
||||
: parentParams;
|
||||
|
||||
const layerProps = {
|
||||
params: currentParams,
|
||||
...(isPage && { searchParams }),
|
||||
};
|
||||
|
||||
await collectMetadata({
|
||||
tree,
|
||||
metadataItems,
|
||||
props: layerProps,
|
||||
route: currentTreePrefix
|
||||
// __PAGE__ shouldn't be shown in a route
|
||||
.filter((s) => s !== PAGE_SEGMENT_KEY)
|
||||
.join("/"),
|
||||
});
|
||||
|
||||
for (const key in parallelRoutes) {
|
||||
const childTree = parallelRoutes[key];
|
||||
await resolveMetadata({
|
||||
tree: childTree,
|
||||
metadataItems,
|
||||
parentParams: currentParams,
|
||||
treePrefix: currentTreePrefix,
|
||||
searchParams,
|
||||
getDynamicParamFromSegment,
|
||||
});
|
||||
}
|
||||
|
||||
return metadataItems;
|
||||
}
|
||||
|
||||
type MetadataAccumulationOptions = {
|
||||
pathname: string;
|
||||
};
|
||||
|
||||
export async function accumulateMetadata(
|
||||
metadataItems: MetadataItems,
|
||||
options: MetadataAccumulationOptions,
|
||||
): Promise<ResolvedMetadata> {
|
||||
const resolvedMetadata = createDefaultMetadata();
|
||||
const resolvers: ((value: ResolvedMetadata) => void)[] = [];
|
||||
const generateMetadataResults: (Metadata | Promise<Metadata>)[] = [];
|
||||
|
||||
let titleTemplates: {
|
||||
title: string | null;
|
||||
twitter: string | null;
|
||||
openGraph: string | null;
|
||||
} = {
|
||||
title: null,
|
||||
twitter: null,
|
||||
openGraph: null,
|
||||
};
|
||||
|
||||
// Loop over all metadata items again, merging synchronously any static object exports,
|
||||
// awaiting any static promise exports, and resolving parent metadata and awaiting any generated metadata
|
||||
|
||||
let resolvingIndex = 0;
|
||||
for (let i = 0; i < metadataItems.length; i++) {
|
||||
const [metadataExport, staticFilesMetadata] = metadataItems[i];
|
||||
let metadata: Metadata | null = null;
|
||||
if (typeof metadataExport === "function") {
|
||||
if (!resolvers.length) {
|
||||
for (let j = i; j < metadataItems.length; j++) {
|
||||
const [preloadMetadataExport] = metadataItems[j];
|
||||
// call each `generateMetadata function concurrently and stash their resolver
|
||||
if (typeof preloadMetadataExport === "function") {
|
||||
generateMetadataResults.push(
|
||||
preloadMetadataExport(
|
||||
new Promise((resolve) => {
|
||||
resolvers.push(resolve);
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const resolveParent = resolvers[resolvingIndex];
|
||||
const generatedMetadata = generateMetadataResults[resolvingIndex++];
|
||||
|
||||
// In dev we clone and freeze to prevent relying on mutating resolvedMetadata directly.
|
||||
// In prod we just pass resolvedMetadata through without any copying.
|
||||
const currentResolvedMetadata: ResolvedMetadata =
|
||||
process.env.NODE_ENV === "development"
|
||||
? Object.freeze(
|
||||
require(
|
||||
"next/dist/compiled/@edge-runtime/primitives/structured-clone",
|
||||
).structuredClone(
|
||||
resolvedMetadata,
|
||||
),
|
||||
)
|
||||
: resolvedMetadata;
|
||||
|
||||
// This resolve should unblock the generateMetadata function if it awaited the parent
|
||||
// argument. If it didn't await the parent argument it might already have a value since it was
|
||||
// called concurrently. Regardless we await the return value before continuing on to the next layer
|
||||
resolveParent(currentResolvedMetadata);
|
||||
metadata = generatedMetadata instanceof Promise
|
||||
? await generatedMetadata
|
||||
: generatedMetadata;
|
||||
} else if (metadataExport !== null && typeof metadataExport === "object") {
|
||||
// This metadataExport is the object form
|
||||
metadata = metadataExport;
|
||||
}
|
||||
|
||||
merge({
|
||||
options,
|
||||
target: resolvedMetadata,
|
||||
source: metadata,
|
||||
staticFilesMetadata,
|
||||
titleTemplates,
|
||||
});
|
||||
|
||||
// If the layout is the same layer with page, skip the leaf layout and leaf page
|
||||
// The leaf layout and page are the last two items
|
||||
if (i < metadataItems.length - 2) {
|
||||
titleTemplates = {
|
||||
title: resolvedMetadata.title?.template || null,
|
||||
openGraph: resolvedMetadata.openGraph?.title?.template || null,
|
||||
twitter: resolvedMetadata.twitter?.title?.template || null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return resolvedMetadata;
|
||||
}
|
259
framework/meta/nextjs/resolvers/resolve-basics.ts
Normal file
259
framework/meta/nextjs/resolvers/resolve-basics.ts
Normal file
|
@ -0,0 +1,259 @@
|
|||
import type {
|
||||
AlternateLinkDescriptor,
|
||||
ResolvedAlternateURLs,
|
||||
} from "../types/alternative-urls-types";
|
||||
import type { Metadata, ResolvedMetadata } from "../types/metadata-interface";
|
||||
import type { ResolvedVerification } from "../types/metadata-types";
|
||||
import type {
|
||||
FieldResolver,
|
||||
FieldResolverWithMetadataBase,
|
||||
} from "../types/resolvers";
|
||||
import type { Viewport } from "../types/extra-types";
|
||||
import path from "path";
|
||||
import { resolveAsArrayOrUndefined } from "../generate/utils";
|
||||
import { resolveUrl } from "./resolve-url";
|
||||
import { ViewPortKeys } from "../constants";
|
||||
|
||||
// Resolve with `metadataBase` if it's present, otherwise resolve with `pathname`.
|
||||
// Resolve with `pathname` if `url` is a relative path.
|
||||
function resolveAlternateUrl(
|
||||
url: string | URL,
|
||||
metadataBase: URL | null,
|
||||
pathname: string,
|
||||
) {
|
||||
if (typeof url === "string" && url.startsWith("./")) {
|
||||
url = path.resolve(pathname, url);
|
||||
} else if (url instanceof URL) {
|
||||
url = new URL(pathname, url);
|
||||
}
|
||||
|
||||
const result = metadataBase ? resolveUrl(url, metadataBase) : url;
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
export const resolveThemeColor: FieldResolver<"themeColor"> = (themeColor) => {
|
||||
if (!themeColor) return null;
|
||||
const themeColorDescriptors: ResolvedMetadata["themeColor"] = [];
|
||||
|
||||
resolveAsArrayOrUndefined(themeColor)?.forEach((descriptor) => {
|
||||
if (typeof descriptor === "string") {
|
||||
themeColorDescriptors.push({ color: descriptor });
|
||||
} else if (typeof descriptor === "object") {
|
||||
themeColorDescriptors.push({
|
||||
color: descriptor.color,
|
||||
media: descriptor.media,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return themeColorDescriptors;
|
||||
};
|
||||
|
||||
export const resolveViewport: FieldResolver<"viewport"> = (viewport) => {
|
||||
let resolved: ResolvedMetadata["viewport"] = null;
|
||||
|
||||
if (typeof viewport === "string") {
|
||||
resolved = viewport;
|
||||
} else if (viewport) {
|
||||
resolved = "";
|
||||
for (const viewportKey_ in ViewPortKeys) {
|
||||
const viewportKey = viewportKey_ as keyof Viewport;
|
||||
if (viewportKey in viewport) {
|
||||
let value = viewport[viewportKey];
|
||||
if (typeof value === "boolean") value = value ? "yes" : "no";
|
||||
if (resolved) resolved += ", ";
|
||||
resolved += `${ViewPortKeys[viewportKey]}=${value}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
return resolved;
|
||||
};
|
||||
|
||||
function resolveUrlValuesOfObject(
|
||||
obj:
|
||||
| Record<string, string | URL | AlternateLinkDescriptor[] | null>
|
||||
| null
|
||||
| undefined,
|
||||
metadataBase: ResolvedMetadata["metadataBase"],
|
||||
pathname: string,
|
||||
): null | Record<string, AlternateLinkDescriptor[]> {
|
||||
if (!obj) return null;
|
||||
|
||||
const result: Record<string, AlternateLinkDescriptor[]> = {};
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
if (typeof value === "string" || value instanceof URL) {
|
||||
result[key] = [
|
||||
{
|
||||
url: resolveAlternateUrl(value, metadataBase, pathname), // metadataBase ? resolveUrl(value, metadataBase)! : value,
|
||||
},
|
||||
];
|
||||
} else {
|
||||
result[key] = [];
|
||||
value?.forEach((item, index) => {
|
||||
const url = resolveAlternateUrl(item.url, metadataBase, pathname);
|
||||
result[key][index] = {
|
||||
url,
|
||||
title: item.title,
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function resolveCanonicalUrl(
|
||||
urlOrDescriptor: string | URL | null | AlternateLinkDescriptor | undefined,
|
||||
metadataBase: URL | null,
|
||||
pathname: string,
|
||||
): null | AlternateLinkDescriptor {
|
||||
if (!urlOrDescriptor) return null;
|
||||
|
||||
const url =
|
||||
typeof urlOrDescriptor === "string" || urlOrDescriptor instanceof URL
|
||||
? urlOrDescriptor
|
||||
: urlOrDescriptor.url;
|
||||
|
||||
// Return string url because structureClone can't handle URL instance
|
||||
return {
|
||||
url: resolveAlternateUrl(url, metadataBase, pathname),
|
||||
};
|
||||
}
|
||||
|
||||
export const resolveAlternates: FieldResolverWithMetadataBase<
|
||||
"alternates",
|
||||
{ pathname: string }
|
||||
> = (alternates, metadataBase, { pathname }) => {
|
||||
if (!alternates) return null;
|
||||
|
||||
const canonical = resolveCanonicalUrl(
|
||||
alternates.canonical,
|
||||
metadataBase,
|
||||
pathname,
|
||||
);
|
||||
const languages = resolveUrlValuesOfObject(
|
||||
alternates.languages,
|
||||
metadataBase,
|
||||
pathname,
|
||||
);
|
||||
const media = resolveUrlValuesOfObject(
|
||||
alternates.media,
|
||||
metadataBase,
|
||||
pathname,
|
||||
);
|
||||
const types = resolveUrlValuesOfObject(
|
||||
alternates.types,
|
||||
metadataBase,
|
||||
pathname,
|
||||
);
|
||||
|
||||
const result: ResolvedAlternateURLs = {
|
||||
canonical,
|
||||
languages,
|
||||
media,
|
||||
types,
|
||||
};
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
const robotsKeys = [
|
||||
"noarchive",
|
||||
"nosnippet",
|
||||
"noimageindex",
|
||||
"nocache",
|
||||
"notranslate",
|
||||
"indexifembedded",
|
||||
"nositelinkssearchbox",
|
||||
"unavailable_after",
|
||||
"max-video-preview",
|
||||
"max-image-preview",
|
||||
"max-snippet",
|
||||
] as const;
|
||||
const resolveRobotsValue: (robots: Metadata["robots"]) => string | null = (
|
||||
robots,
|
||||
) => {
|
||||
if (!robots) return null;
|
||||
if (typeof robots === "string") return robots;
|
||||
|
||||
const values: string[] = [];
|
||||
|
||||
if (robots.index) values.push("index");
|
||||
else if (typeof robots.index === "boolean") values.push("noindex");
|
||||
|
||||
if (robots.follow) values.push("follow");
|
||||
else if (typeof robots.follow === "boolean") values.push("nofollow");
|
||||
|
||||
for (const key of robotsKeys) {
|
||||
const value = robots[key];
|
||||
if (typeof value !== "undefined" && value !== false) {
|
||||
values.push(typeof value === "boolean" ? key : `${key}:${value}`);
|
||||
}
|
||||
}
|
||||
|
||||
return values.join(", ");
|
||||
};
|
||||
|
||||
export const resolveRobots: FieldResolver<"robots"> = (robots) => {
|
||||
if (!robots) return null;
|
||||
return {
|
||||
basic: resolveRobotsValue(robots),
|
||||
googleBot: typeof robots !== "string"
|
||||
? resolveRobotsValue(robots.googleBot)
|
||||
: null,
|
||||
};
|
||||
};
|
||||
|
||||
const VerificationKeys = ["google", "yahoo", "yandex", "me", "other"] as const;
|
||||
export const resolveVerification: FieldResolver<"verification"> = (
|
||||
verification,
|
||||
) => {
|
||||
if (!verification) return null;
|
||||
const res: ResolvedVerification = {};
|
||||
|
||||
for (const key of VerificationKeys) {
|
||||
const value = verification[key];
|
||||
if (value) {
|
||||
if (key === "other") {
|
||||
res.other = {};
|
||||
for (const otherKey in verification.other) {
|
||||
const otherValue = resolveAsArrayOrUndefined(
|
||||
verification.other[otherKey],
|
||||
);
|
||||
if (otherValue) res.other[otherKey] = otherValue;
|
||||
}
|
||||
} else res[key] = resolveAsArrayOrUndefined(value) as (string | number)[];
|
||||
}
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
export const resolveAppleWebApp: FieldResolver<"appleWebApp"> = (appWebApp) => {
|
||||
if (!appWebApp) return null;
|
||||
if (appWebApp === true) {
|
||||
return {
|
||||
capable: true,
|
||||
};
|
||||
}
|
||||
|
||||
const startupImages = appWebApp.startupImage
|
||||
? resolveAsArrayOrUndefined(appWebApp.startupImage)?.map((item) =>
|
||||
typeof item === "string" ? { url: item } : item
|
||||
)
|
||||
: null;
|
||||
|
||||
return {
|
||||
capable: "capable" in appWebApp ? !!appWebApp.capable : true,
|
||||
title: appWebApp.title || null,
|
||||
startupImage: startupImages,
|
||||
statusBarStyle: appWebApp.statusBarStyle || "default",
|
||||
};
|
||||
};
|
||||
|
||||
export const resolveAppLinks: FieldResolver<"appLinks"> = (appLinks) => {
|
||||
if (!appLinks) return null;
|
||||
for (const key in appLinks) {
|
||||
// @ts-ignore // TODO: type infer
|
||||
appLinks[key] = resolveAsArrayOrUndefined(appLinks[key]);
|
||||
}
|
||||
return appLinks as ResolvedMetadata["appLinks"];
|
||||
};
|
34
framework/meta/nextjs/resolvers/resolve-icons.ts
Normal file
34
framework/meta/nextjs/resolvers/resolve-icons.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import type { ResolvedMetadata } from "../types/metadata-interface";
|
||||
import type { Icon, IconDescriptor } from "../types/metadata-types";
|
||||
import type { FieldResolver } from "../types/resolvers";
|
||||
import { resolveAsArrayOrUndefined } from "../generate/utils";
|
||||
import { isStringOrURL } from "./resolve-url";
|
||||
import { IconKeys } from "../constants";
|
||||
|
||||
export function resolveIcon(icon: Icon): IconDescriptor {
|
||||
if (isStringOrURL(icon)) return { url: icon };
|
||||
else if (Array.isArray(icon)) return icon;
|
||||
return icon;
|
||||
}
|
||||
|
||||
export const resolveIcons: FieldResolver<"icons"> = (icons) => {
|
||||
if (!icons) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const resolved: ResolvedMetadata["icons"] = {
|
||||
icon: [],
|
||||
apple: [],
|
||||
};
|
||||
if (Array.isArray(icons)) {
|
||||
resolved.icon = icons.map(resolveIcon).filter(Boolean);
|
||||
} else if (isStringOrURL(icons)) {
|
||||
resolved.icon = [resolveIcon(icons)];
|
||||
} else {
|
||||
for (const key of IconKeys) {
|
||||
const values = resolveAsArrayOrUndefined(icons[key]);
|
||||
if (values) resolved[key] = values.map(resolveIcon);
|
||||
}
|
||||
}
|
||||
return resolved;
|
||||
};
|
147
framework/meta/nextjs/resolvers/resolve-opengraph.ts
Normal file
147
framework/meta/nextjs/resolvers/resolve-opengraph.ts
Normal file
|
@ -0,0 +1,147 @@
|
|||
import type { Metadata, ResolvedMetadata } from "../types/metadata-interface";
|
||||
import type {
|
||||
OpenGraph,
|
||||
OpenGraphType,
|
||||
ResolvedOpenGraph,
|
||||
} from "../types/opengraph-types";
|
||||
import type { FieldResolverWithMetadataBase } from "../types/resolvers";
|
||||
import type { ResolvedTwitterMetadata, Twitter } from "../types/twitter-types";
|
||||
import { resolveAsArrayOrUndefined } from "../generate/utils";
|
||||
import { isStringOrURL, resolveUrl } from "./resolve-url";
|
||||
|
||||
const OgTypeFields = {
|
||||
article: ["authors", "tags"],
|
||||
song: ["albums", "musicians"],
|
||||
playlist: ["albums", "musicians"],
|
||||
radio: ["creators"],
|
||||
video: ["actors", "directors", "writers", "tags"],
|
||||
basic: [
|
||||
"emails",
|
||||
"phoneNumbers",
|
||||
"faxNumbers",
|
||||
"alternateLocale",
|
||||
"audio",
|
||||
"videos",
|
||||
],
|
||||
} as const;
|
||||
|
||||
function resolveImages(
|
||||
images: Twitter["images"],
|
||||
metadataBase: ResolvedMetadata["metadataBase"],
|
||||
): NonNullable<ResolvedMetadata["twitter"]>["images"];
|
||||
function resolveImages(
|
||||
images: OpenGraph["images"],
|
||||
metadataBase: ResolvedMetadata["metadataBase"],
|
||||
): NonNullable<ResolvedMetadata["openGraph"]>["images"];
|
||||
function resolveImages(
|
||||
images: OpenGraph["images"] | Twitter["images"],
|
||||
metadataBase: ResolvedMetadata["metadataBase"],
|
||||
):
|
||||
| NonNullable<ResolvedMetadata["twitter"]>["images"]
|
||||
| NonNullable<ResolvedMetadata["openGraph"]>["images"] {
|
||||
const resolvedImages = resolveAsArrayOrUndefined(images);
|
||||
resolvedImages?.forEach((item, index, array) => {
|
||||
if (isStringOrURL(item)) {
|
||||
array[index] = {
|
||||
url: resolveUrl(item, metadataBase)!,
|
||||
};
|
||||
} else {
|
||||
// Update image descriptor url
|
||||
item.url = resolveUrl(item.url, metadataBase)!;
|
||||
}
|
||||
});
|
||||
return resolvedImages;
|
||||
}
|
||||
|
||||
function getFieldsByOgType(ogType: OpenGraphType | undefined) {
|
||||
switch (ogType) {
|
||||
case "article":
|
||||
case "book":
|
||||
return OgTypeFields.article;
|
||||
case "music.song":
|
||||
case "music.album":
|
||||
return OgTypeFields.song;
|
||||
case "music.playlist":
|
||||
return OgTypeFields.playlist;
|
||||
case "music.radio_station":
|
||||
return OgTypeFields.radio;
|
||||
case "video.movie":
|
||||
case "video.episode":
|
||||
return OgTypeFields.video;
|
||||
default:
|
||||
return OgTypeFields.basic;
|
||||
}
|
||||
}
|
||||
|
||||
export const resolveOpenGraph: FieldResolverWithMetadataBase<"openGraph"> = (
|
||||
openGraph: Metadata["openGraph"],
|
||||
metadataBase: ResolvedMetadata["metadataBase"],
|
||||
) => {
|
||||
if (!openGraph) return null;
|
||||
|
||||
const url = resolveUrl(openGraph.url, metadataBase);
|
||||
const resolved = { ...openGraph } as ResolvedOpenGraph;
|
||||
|
||||
function assignProps(og: OpenGraph) {
|
||||
const ogType = og && "type" in og ? og.type : undefined;
|
||||
const keys = getFieldsByOgType(ogType);
|
||||
for (const k of keys) {
|
||||
const key = k as keyof ResolvedOpenGraph;
|
||||
if (key in og && key !== "url") {
|
||||
const value = og[key];
|
||||
if (value) {
|
||||
const arrayValue = resolveAsArrayOrUndefined(value); /// TODO: improve typing inferring
|
||||
(resolved as any)[key] = arrayValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resolved.images = resolveImages(og.images, metadataBase);
|
||||
}
|
||||
|
||||
assignProps(openGraph);
|
||||
|
||||
resolved.url = url;
|
||||
|
||||
return resolved;
|
||||
};
|
||||
|
||||
const TwitterBasicInfoKeys = [
|
||||
"site",
|
||||
"siteId",
|
||||
"creator",
|
||||
"creatorId",
|
||||
"description",
|
||||
] as const;
|
||||
|
||||
export const resolveTwitter: FieldResolverWithMetadataBase<"twitter"> = (
|
||||
twitter,
|
||||
metadataBase,
|
||||
) => {
|
||||
if (!twitter) return null;
|
||||
const resolved = {
|
||||
...twitter,
|
||||
card: "card" in twitter ? twitter.card : "summary",
|
||||
} as ResolvedTwitterMetadata;
|
||||
for (const infoKey of TwitterBasicInfoKeys) {
|
||||
resolved[infoKey] = twitter[infoKey] || null;
|
||||
}
|
||||
resolved.images = resolveImages(twitter.images, metadataBase);
|
||||
|
||||
if ("card" in resolved) {
|
||||
switch (resolved.card) {
|
||||
case "player": {
|
||||
resolved.players = resolveAsArrayOrUndefined(resolved.players) || [];
|
||||
break;
|
||||
}
|
||||
case "app": {
|
||||
resolved.app = resolved.app || {};
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return resolved;
|
||||
};
|
39
framework/meta/nextjs/resolvers/resolve-title.ts
Normal file
39
framework/meta/nextjs/resolvers/resolve-title.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
import type { Metadata } from "../types/metadata-interface";
|
||||
import type { AbsoluteTemplateString } from "../types/metadata-types";
|
||||
|
||||
function resolveTitleTemplate(
|
||||
template: string | null | undefined,
|
||||
title: string,
|
||||
) {
|
||||
return template ? template.replace(/%s/g, title) : title;
|
||||
}
|
||||
|
||||
export function resolveTitle(
|
||||
title: Metadata["title"],
|
||||
stashedTemplate: string | null | undefined,
|
||||
): AbsoluteTemplateString {
|
||||
let resolved;
|
||||
const template = typeof title !== "string" && title && "template" in title
|
||||
? title.template
|
||||
: null;
|
||||
|
||||
if (typeof title === "string") {
|
||||
resolved = resolveTitleTemplate(stashedTemplate, title);
|
||||
} else if (title) {
|
||||
if ("default" in title) {
|
||||
resolved = resolveTitleTemplate(stashedTemplate, title.default);
|
||||
}
|
||||
if ("absolute" in title && title.absolute) {
|
||||
resolved = title.absolute;
|
||||
}
|
||||
}
|
||||
|
||||
if (title && typeof title !== "string") {
|
||||
return {
|
||||
template,
|
||||
absolute: resolved || "",
|
||||
};
|
||||
} else {
|
||||
return { absolute: resolved || title || "", template };
|
||||
}
|
||||
}
|
38
framework/meta/nextjs/resolvers/resolve-url.ts
Normal file
38
framework/meta/nextjs/resolvers/resolve-url.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
import path from "path";
|
||||
import process from "node:process";
|
||||
|
||||
function isStringOrURL(icon: any): icon is string | URL {
|
||||
return typeof icon === "string" || icon instanceof URL;
|
||||
}
|
||||
|
||||
function resolveUrl(url: null | undefined, metadataBase: URL | null): null;
|
||||
function resolveUrl(url: string | URL, metadataBase: URL | null): URL;
|
||||
function resolveUrl(
|
||||
url: string | URL | null | undefined,
|
||||
metadataBase: URL | null,
|
||||
): URL | null;
|
||||
function resolveUrl(
|
||||
url: string | URL | null | undefined,
|
||||
metadataBase: URL | null,
|
||||
): URL | null {
|
||||
if (url instanceof URL) return url;
|
||||
if (!url) return null;
|
||||
|
||||
try {
|
||||
// If we can construct a URL instance from url, ignore metadataBase
|
||||
const parsedUrl = new URL(url);
|
||||
return parsedUrl;
|
||||
} catch (_) {}
|
||||
|
||||
if (!metadataBase) {
|
||||
metadataBase = new URL(`http://localhost:${process.env.PORT || 3000}`);
|
||||
}
|
||||
|
||||
// Handle relative or absolute paths
|
||||
const basePath = metadataBase.pathname || "";
|
||||
const joinedPath = path.join(basePath, url);
|
||||
|
||||
return new URL(joinedPath, metadataBase);
|
||||
}
|
||||
|
||||
export { isStringOrURL, resolveUrl };
|
450
framework/meta/nextjs/types/alternative-urls-types.ts
Normal file
450
framework/meta/nextjs/types/alternative-urls-types.ts
Normal file
|
@ -0,0 +1,450 @@
|
|||
// Reference: https://hreflang.org/what-is-a-valid-hreflang
|
||||
|
||||
type LangCode =
|
||||
| "aa"
|
||||
| "ab"
|
||||
| "ae"
|
||||
| "af"
|
||||
| "ak"
|
||||
| "am"
|
||||
| "an"
|
||||
| "ar"
|
||||
| "as"
|
||||
| "av"
|
||||
| "ay"
|
||||
| "az"
|
||||
| "ba"
|
||||
| "be"
|
||||
| "bg"
|
||||
| "bh"
|
||||
| "bi"
|
||||
| "bm"
|
||||
| "bn"
|
||||
| "bo"
|
||||
| "br"
|
||||
| "bs"
|
||||
| "ca"
|
||||
| "ce"
|
||||
| "ch"
|
||||
| "co"
|
||||
| "cr"
|
||||
| "cs"
|
||||
| "cu"
|
||||
| "cv"
|
||||
| "cy"
|
||||
| "da"
|
||||
| "de"
|
||||
| "dv"
|
||||
| "dz"
|
||||
| "ee"
|
||||
| "el"
|
||||
| "en"
|
||||
| "eo"
|
||||
| "es"
|
||||
| "et"
|
||||
| "eu"
|
||||
| "fa"
|
||||
| "ff"
|
||||
| "fi"
|
||||
| "fj"
|
||||
| "fo"
|
||||
| "fr"
|
||||
| "fy"
|
||||
| "ga"
|
||||
| "gd"
|
||||
| "gl"
|
||||
| "gn"
|
||||
| "gu"
|
||||
| "gv"
|
||||
| "ha"
|
||||
| "he"
|
||||
| "hi"
|
||||
| "ho"
|
||||
| "hr"
|
||||
| "ht"
|
||||
| "hu"
|
||||
| "hy"
|
||||
| "hz"
|
||||
| "ia"
|
||||
| "id"
|
||||
| "ie"
|
||||
| "ig"
|
||||
| "ii"
|
||||
| "ik"
|
||||
| "io"
|
||||
| "is"
|
||||
| "it"
|
||||
| "iu"
|
||||
| "ja"
|
||||
| "jv"
|
||||
| "ka"
|
||||
| "kg"
|
||||
| "ki"
|
||||
| "kj"
|
||||
| "kk"
|
||||
| "kl"
|
||||
| "km"
|
||||
| "kn"
|
||||
| "ko"
|
||||
| "kr"
|
||||
| "ks"
|
||||
| "ku"
|
||||
| "kv"
|
||||
| "kw"
|
||||
| "ky"
|
||||
| "la"
|
||||
| "lb"
|
||||
| "lg"
|
||||
| "li"
|
||||
| "ln"
|
||||
| "lo"
|
||||
| "lt"
|
||||
| "lu"
|
||||
| "lv"
|
||||
| "mg"
|
||||
| "mh"
|
||||
| "mi"
|
||||
| "mk"
|
||||
| "ml"
|
||||
| "mn"
|
||||
| "mr"
|
||||
| "ms"
|
||||
| "mt"
|
||||
| "my"
|
||||
| "na"
|
||||
| "nb"
|
||||
| "nd"
|
||||
| "ne"
|
||||
| "ng"
|
||||
| "nl"
|
||||
| "nn"
|
||||
| "no"
|
||||
| "nr"
|
||||
| "nv"
|
||||
| "ny"
|
||||
| "oc"
|
||||
| "oj"
|
||||
| "om"
|
||||
| "or"
|
||||
| "os"
|
||||
| "pa"
|
||||
| "pi"
|
||||
| "pl"
|
||||
| "ps"
|
||||
| "pt"
|
||||
| "qu"
|
||||
| "rm"
|
||||
| "rn"
|
||||
| "ro"
|
||||
| "ru"
|
||||
| "rw"
|
||||
| "sa"
|
||||
| "sc"
|
||||
| "sd"
|
||||
| "se"
|
||||
| "sg"
|
||||
| "si"
|
||||
| "sk"
|
||||
| "sl"
|
||||
| "sm"
|
||||
| "sn"
|
||||
| "so"
|
||||
| "sq"
|
||||
| "sr"
|
||||
| "ss"
|
||||
| "st"
|
||||
| "su"
|
||||
| "sv"
|
||||
| "sw"
|
||||
| "ta"
|
||||
| "te"
|
||||
| "tg"
|
||||
| "th"
|
||||
| "ti"
|
||||
| "tk"
|
||||
| "tl"
|
||||
| "tn"
|
||||
| "to"
|
||||
| "tr"
|
||||
| "ts"
|
||||
| "tt"
|
||||
| "tw"
|
||||
| "ty"
|
||||
| "ug"
|
||||
| "uk"
|
||||
| "ur"
|
||||
| "uz"
|
||||
| "ve"
|
||||
| "vi"
|
||||
| "vo"
|
||||
| "wa"
|
||||
| "wo"
|
||||
| "xh"
|
||||
| "yi"
|
||||
| "yo"
|
||||
| "za"
|
||||
| "zh"
|
||||
| "zu"
|
||||
| "af-ZA"
|
||||
| "am-ET"
|
||||
| "ar-AE"
|
||||
| "ar-BH"
|
||||
| "ar-DZ"
|
||||
| "ar-EG"
|
||||
| "ar-IQ"
|
||||
| "ar-JO"
|
||||
| "ar-KW"
|
||||
| "ar-LB"
|
||||
| "ar-LY"
|
||||
| "ar-MA"
|
||||
| "arn-CL"
|
||||
| "ar-OM"
|
||||
| "ar-QA"
|
||||
| "ar-SA"
|
||||
| "ar-SD"
|
||||
| "ar-SY"
|
||||
| "ar-TN"
|
||||
| "ar-YE"
|
||||
| "as-IN"
|
||||
| "az-az"
|
||||
| "az-Cyrl-AZ"
|
||||
| "az-Latn-AZ"
|
||||
| "ba-RU"
|
||||
| "be-BY"
|
||||
| "bg-BG"
|
||||
| "bn-BD"
|
||||
| "bn-IN"
|
||||
| "bo-CN"
|
||||
| "br-FR"
|
||||
| "bs-Cyrl-BA"
|
||||
| "bs-Latn-BA"
|
||||
| "ca-ES"
|
||||
| "co-FR"
|
||||
| "cs-CZ"
|
||||
| "cy-GB"
|
||||
| "da-DK"
|
||||
| "de-AT"
|
||||
| "de-CH"
|
||||
| "de-DE"
|
||||
| "de-LI"
|
||||
| "de-LU"
|
||||
| "dsb-DE"
|
||||
| "dv-MV"
|
||||
| "el-CY"
|
||||
| "el-GR"
|
||||
| "en-029"
|
||||
| "en-AU"
|
||||
| "en-BZ"
|
||||
| "en-CA"
|
||||
| "en-cb"
|
||||
| "en-GB"
|
||||
| "en-IE"
|
||||
| "en-IN"
|
||||
| "en-JM"
|
||||
| "en-MT"
|
||||
| "en-MY"
|
||||
| "en-NZ"
|
||||
| "en-PH"
|
||||
| "en-SG"
|
||||
| "en-TT"
|
||||
| "en-US"
|
||||
| "en-ZA"
|
||||
| "en-ZW"
|
||||
| "es-AR"
|
||||
| "es-BO"
|
||||
| "es-CL"
|
||||
| "es-CO"
|
||||
| "es-CR"
|
||||
| "es-DO"
|
||||
| "es-EC"
|
||||
| "es-ES"
|
||||
| "es-GT"
|
||||
| "es-HN"
|
||||
| "es-MX"
|
||||
| "es-NI"
|
||||
| "es-PA"
|
||||
| "es-PE"
|
||||
| "es-PR"
|
||||
| "es-PY"
|
||||
| "es-SV"
|
||||
| "es-US"
|
||||
| "es-UY"
|
||||
| "es-VE"
|
||||
| "et-EE"
|
||||
| "eu-ES"
|
||||
| "fa-IR"
|
||||
| "fi-FI"
|
||||
| "fil-PH"
|
||||
| "fo-FO"
|
||||
| "fr-BE"
|
||||
| "fr-CA"
|
||||
| "fr-CH"
|
||||
| "fr-FR"
|
||||
| "fr-LU"
|
||||
| "fr-MC"
|
||||
| "fy-NL"
|
||||
| "ga-IE"
|
||||
| "gd-GB"
|
||||
| "gd-ie"
|
||||
| "gl-ES"
|
||||
| "gsw-FR"
|
||||
| "gu-IN"
|
||||
| "ha-Latn-NG"
|
||||
| "he-IL"
|
||||
| "hi-IN"
|
||||
| "hr-BA"
|
||||
| "hr-HR"
|
||||
| "hsb-DE"
|
||||
| "hu-HU"
|
||||
| "hy-AM"
|
||||
| "id-ID"
|
||||
| "ig-NG"
|
||||
| "ii-CN"
|
||||
| "in-ID"
|
||||
| "is-IS"
|
||||
| "it-CH"
|
||||
| "it-IT"
|
||||
| "iu-Cans-CA"
|
||||
| "iu-Latn-CA"
|
||||
| "iw-IL"
|
||||
| "ja-JP"
|
||||
| "ka-GE"
|
||||
| "kk-KZ"
|
||||
| "kl-GL"
|
||||
| "km-KH"
|
||||
| "kn-IN"
|
||||
| "kok-IN"
|
||||
| "ko-KR"
|
||||
| "ky-KG"
|
||||
| "lb-LU"
|
||||
| "lo-LA"
|
||||
| "lt-LT"
|
||||
| "lv-LV"
|
||||
| "mi-NZ"
|
||||
| "mk-MK"
|
||||
| "ml-IN"
|
||||
| "mn-MN"
|
||||
| "mn-Mong-CN"
|
||||
| "moh-CA"
|
||||
| "mr-IN"
|
||||
| "ms-BN"
|
||||
| "ms-MY"
|
||||
| "mt-MT"
|
||||
| "nb-NO"
|
||||
| "ne-NP"
|
||||
| "nl-BE"
|
||||
| "nl-NL"
|
||||
| "nn-NO"
|
||||
| "no-no"
|
||||
| "nso-ZA"
|
||||
| "oc-FR"
|
||||
| "or-IN"
|
||||
| "pa-IN"
|
||||
| "pl-PL"
|
||||
| "prs-AF"
|
||||
| "ps-AF"
|
||||
| "pt-BR"
|
||||
| "pt-PT"
|
||||
| "qut-GT"
|
||||
| "quz-BO"
|
||||
| "quz-EC"
|
||||
| "quz-PE"
|
||||
| "rm-CH"
|
||||
| "ro-mo"
|
||||
| "ro-RO"
|
||||
| "ru-mo"
|
||||
| "ru-RU"
|
||||
| "rw-RW"
|
||||
| "sah-RU"
|
||||
| "sa-IN"
|
||||
| "se-FI"
|
||||
| "se-NO"
|
||||
| "se-SE"
|
||||
| "si-LK"
|
||||
| "sk-SK"
|
||||
| "sl-SI"
|
||||
| "sma-NO"
|
||||
| "sma-SE"
|
||||
| "smj-NO"
|
||||
| "smj-SE"
|
||||
| "smn-FI"
|
||||
| "sms-FI"
|
||||
| "sq-AL"
|
||||
| "sr-BA"
|
||||
| "sr-CS"
|
||||
| "sr-Cyrl-BA"
|
||||
| "sr-Cyrl-CS"
|
||||
| "sr-Cyrl-ME"
|
||||
| "sr-Cyrl-RS"
|
||||
| "sr-Latn-BA"
|
||||
| "sr-Latn-CS"
|
||||
| "sr-Latn-ME"
|
||||
| "sr-Latn-RS"
|
||||
| "sr-ME"
|
||||
| "sr-RS"
|
||||
| "sr-sp"
|
||||
| "sv-FI"
|
||||
| "sv-SE"
|
||||
| "sw-KE"
|
||||
| "syr-SY"
|
||||
| "ta-IN"
|
||||
| "te-IN"
|
||||
| "tg-Cyrl-TJ"
|
||||
| "th-TH"
|
||||
| "tk-TM"
|
||||
| "tlh-QS"
|
||||
| "tn-ZA"
|
||||
| "tr-TR"
|
||||
| "tt-RU"
|
||||
| "tzm-Latn-DZ"
|
||||
| "ug-CN"
|
||||
| "uk-UA"
|
||||
| "ur-PK"
|
||||
| "uz-Cyrl-UZ"
|
||||
| "uz-Latn-UZ"
|
||||
| "uz-uz"
|
||||
| "vi-VN"
|
||||
| "wo-SN"
|
||||
| "xh-ZA"
|
||||
| "yo-NG"
|
||||
| "zh-CN"
|
||||
| "zh-HK"
|
||||
| "zh-MO"
|
||||
| "zh-SG"
|
||||
| "zh-TW"
|
||||
| "zu-ZA";
|
||||
|
||||
type UnmatchedLang = "x-default";
|
||||
|
||||
type HrefLang = LangCode | UnmatchedLang;
|
||||
|
||||
type Languages<T> = {
|
||||
[s in HrefLang]?: T;
|
||||
};
|
||||
|
||||
export type AlternateLinkDescriptor = {
|
||||
title?: string;
|
||||
url: string | URL;
|
||||
};
|
||||
|
||||
export type AlternateURLs = {
|
||||
canonical?: null | string | URL | AlternateLinkDescriptor;
|
||||
languages?: Languages<null | string | URL | AlternateLinkDescriptor[]>;
|
||||
media?: {
|
||||
[media: string]: null | string | URL | AlternateLinkDescriptor[];
|
||||
};
|
||||
types?: {
|
||||
[types: string]: null | string | URL | AlternateLinkDescriptor[];
|
||||
};
|
||||
};
|
||||
|
||||
export type ResolvedAlternateURLs = {
|
||||
canonical: null | AlternateLinkDescriptor;
|
||||
languages: null | Languages<AlternateLinkDescriptor[]>;
|
||||
media: null | {
|
||||
[media: string]: null | AlternateLinkDescriptor[];
|
||||
};
|
||||
types: null | {
|
||||
[types: string]: null | AlternateLinkDescriptor[];
|
||||
};
|
||||
};
|
104
framework/meta/nextjs/types/extra-types.ts
Normal file
104
framework/meta/nextjs/types/extra-types.ts
Normal file
|
@ -0,0 +1,104 @@
|
|||
// When rendering applink meta tags add a namespace tag before each array instance
|
||||
// if more than one member exists.
|
||||
// ref: https://developers.facebook.com/docs/applinks/metadata-reference
|
||||
|
||||
export type AppLinks = {
|
||||
ios?: AppLinksApple | Array<AppLinksApple>;
|
||||
iphone?: AppLinksApple | Array<AppLinksApple>;
|
||||
ipad?: AppLinksApple | Array<AppLinksApple>;
|
||||
android?: AppLinksAndroid | Array<AppLinksAndroid>;
|
||||
windows_phone?: AppLinksWindows | Array<AppLinksWindows>;
|
||||
windows?: AppLinksWindows | Array<AppLinksWindows>;
|
||||
windows_universal?: AppLinksWindows | Array<AppLinksWindows>;
|
||||
web?: AppLinksWeb | Array<AppLinksWeb>;
|
||||
};
|
||||
export type ResolvedAppLinks = {
|
||||
ios?: Array<AppLinksApple>;
|
||||
iphone?: Array<AppLinksApple>;
|
||||
ipad?: Array<AppLinksApple>;
|
||||
android?: Array<AppLinksAndroid>;
|
||||
windows_phone?: Array<AppLinksWindows>;
|
||||
windows?: Array<AppLinksWindows>;
|
||||
windows_universal?: Array<AppLinksWindows>;
|
||||
web?: Array<AppLinksWeb>;
|
||||
};
|
||||
export type AppLinksApple = {
|
||||
url: string | URL;
|
||||
app_store_id?: string | number;
|
||||
app_name?: string;
|
||||
};
|
||||
export type AppLinksAndroid = {
|
||||
package: string;
|
||||
url?: string | URL;
|
||||
class?: string;
|
||||
app_name?: string;
|
||||
};
|
||||
export type AppLinksWindows = {
|
||||
url: string | URL;
|
||||
app_id?: string;
|
||||
app_name?: string;
|
||||
};
|
||||
export type AppLinksWeb = {
|
||||
url: string | URL;
|
||||
should_fallback?: boolean;
|
||||
};
|
||||
|
||||
// Apple Itunes APp
|
||||
// https://developer.apple.com/documentation/webkit/promoting_apps_with_smart_app_banners
|
||||
export type ItunesApp = {
|
||||
appId: string;
|
||||
appArgument?: string;
|
||||
};
|
||||
|
||||
// Viewport meta structure
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTML/Viewport_meta_tag
|
||||
// intentionally leaving out user-scalable, use a string if you want that behavior
|
||||
export type Viewport = {
|
||||
width?: string | number;
|
||||
height?: string | number;
|
||||
initialScale?: number;
|
||||
minimumScale?: number;
|
||||
maximumScale?: number;
|
||||
userScalable?: boolean;
|
||||
viewportFit?: "auto" | "cover" | "contain";
|
||||
interactiveWidget?: "resizes-visual" | "resizes-content" | "overlays-content";
|
||||
};
|
||||
|
||||
// Apple Web App
|
||||
// https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html
|
||||
// https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/ConfiguringWebApplications/ConfiguringWebApplications.html
|
||||
export type AppleWebApp = {
|
||||
// default true
|
||||
capable?: boolean;
|
||||
title?: string;
|
||||
startupImage?: AppleImage | Array<AppleImage>;
|
||||
// default "default"
|
||||
statusBarStyle?: "default" | "black" | "black-translucent";
|
||||
};
|
||||
export type AppleImage = string | AppleImageDescriptor;
|
||||
export type AppleImageDescriptor = {
|
||||
url: string;
|
||||
media?: string;
|
||||
};
|
||||
|
||||
export type ResolvedAppleWebApp = {
|
||||
capable: boolean;
|
||||
title?: string | null;
|
||||
startupImage?: AppleImageDescriptor[] | null;
|
||||
statusBarStyle?: "default" | "black" | "black-translucent";
|
||||
};
|
||||
|
||||
// Format Detection
|
||||
// This is a poorly specified metadata export type that is supposed to
|
||||
// control whether the device attempts to conver text that matches
|
||||
// certain formats into links for action. The most supported example
|
||||
// is how mobile devices detect phone numbers and make them into links
|
||||
// that can initiate a phone call
|
||||
// https://www.goodemailcode.com/email-code/template.html
|
||||
export type FormatDetection = {
|
||||
telephone?: boolean;
|
||||
date?: boolean;
|
||||
address?: boolean;
|
||||
email?: boolean;
|
||||
url?: boolean;
|
||||
};
|
86
framework/meta/nextjs/types/manifest-types.ts
Normal file
86
framework/meta/nextjs/types/manifest-types.ts
Normal file
|
@ -0,0 +1,86 @@
|
|||
export type Manifest = {
|
||||
background_color?: string;
|
||||
categories?: string[];
|
||||
description?: string;
|
||||
display?: "fullscreen" | "standalone" | "minimal-ui" | "browser";
|
||||
display_override?: string[];
|
||||
icons?: {
|
||||
src: string;
|
||||
type?: string;
|
||||
sizes?: string;
|
||||
purpose?: "any" | "maskable" | "monochrome" | "badge";
|
||||
}[];
|
||||
id?: string;
|
||||
launch_handler?: {
|
||||
platform?: "windows" | "macos" | "linux";
|
||||
url?: string;
|
||||
};
|
||||
name?: string;
|
||||
orientation?:
|
||||
| "any"
|
||||
| "natural"
|
||||
| "landscape"
|
||||
| "portrait"
|
||||
| "portrait-primary"
|
||||
| "portrait-secondary"
|
||||
| "landscape-primary"
|
||||
| "landscape-secondary";
|
||||
prefer_related_applications?: boolean;
|
||||
protocol_handlers?: {
|
||||
protocol: string;
|
||||
url: string;
|
||||
title?: string;
|
||||
}[];
|
||||
related_applications?: {
|
||||
platform: string;
|
||||
url: string;
|
||||
id?: string;
|
||||
}[];
|
||||
scope?: string;
|
||||
screenshots?: {
|
||||
src: string;
|
||||
type?: string;
|
||||
sizes?: string;
|
||||
}[];
|
||||
serviceworker?: {
|
||||
src?: string;
|
||||
scope?: string;
|
||||
type?: string;
|
||||
update_via_cache?: "import" | "none" | "all";
|
||||
};
|
||||
share_target?: {
|
||||
action?: string;
|
||||
method?: "get" | "post";
|
||||
enctype?:
|
||||
| "application/x-www-form-urlencoded"
|
||||
| "multipart/form-data"
|
||||
| "text/plain";
|
||||
params?: {
|
||||
name: string;
|
||||
value: string;
|
||||
required?: boolean;
|
||||
}[];
|
||||
url?: string;
|
||||
title?: string;
|
||||
text?: string;
|
||||
files?: {
|
||||
accept?: string[];
|
||||
name?: string;
|
||||
}[];
|
||||
};
|
||||
short_name?: string;
|
||||
shortcuts?: {
|
||||
name: string;
|
||||
short_name?: string;
|
||||
description?: string;
|
||||
url: string;
|
||||
icons?: {
|
||||
src: string;
|
||||
type?: string;
|
||||
sizes?: string;
|
||||
purpose?: "any" | "maskable" | "monochrome" | "badge";
|
||||
}[];
|
||||
}[];
|
||||
start_url?: string;
|
||||
theme_color?: string;
|
||||
};
|
566
framework/meta/nextjs/types/metadata-interface.ts
Normal file
566
framework/meta/nextjs/types/metadata-interface.ts
Normal file
|
@ -0,0 +1,566 @@
|
|||
import type {
|
||||
AlternateURLs,
|
||||
ResolvedAlternateURLs,
|
||||
} from "./alternative-urls-types";
|
||||
import type {
|
||||
AppleWebApp,
|
||||
AppLinks,
|
||||
FormatDetection,
|
||||
ItunesApp,
|
||||
ResolvedAppleWebApp,
|
||||
ResolvedAppLinks,
|
||||
Viewport,
|
||||
} from "./extra-types";
|
||||
import type {
|
||||
AbsoluteTemplateString,
|
||||
Author,
|
||||
ColorSchemeEnum,
|
||||
DeprecatedMetadataFields,
|
||||
Icon,
|
||||
Icons,
|
||||
IconURL,
|
||||
ReferrerEnum,
|
||||
ResolvedIcons,
|
||||
ResolvedRobots,
|
||||
ResolvedVerification,
|
||||
Robots,
|
||||
TemplateString,
|
||||
ThemeColorDescriptor,
|
||||
Verification,
|
||||
} from "./metadata-types";
|
||||
import type { Manifest as ManifestFile } from "./manifest-types";
|
||||
import type { OpenGraph, ResolvedOpenGraph } from "./opengraph-types";
|
||||
import type { ResolvedTwitterMetadata, Twitter } from "./twitter-types";
|
||||
|
||||
/**
|
||||
* Metadata interface to describe all the metadata fields that can be set in a document.
|
||||
* @interface
|
||||
*/
|
||||
interface Metadata extends DeprecatedMetadataFields {
|
||||
/**
|
||||
* The base path and origin for absolute urls for various metadata links such as OpenGraph images.
|
||||
*/
|
||||
metadataBase?: null | URL;
|
||||
|
||||
/**
|
||||
* The document title.
|
||||
* @example
|
||||
* ```tsx
|
||||
* "My Blog"
|
||||
* <title>My Blog</title>
|
||||
*
|
||||
* { default: "Dashboard", template: "%s | My Website" }
|
||||
* <title>Dashboard | My Website</title>
|
||||
*
|
||||
* { absolute: "My Blog", template: "%s | My Website" }
|
||||
* <title>My Blog</title>
|
||||
* ```
|
||||
*/
|
||||
title?: null | string | TemplateString;
|
||||
|
||||
/**
|
||||
* The document description, and optionally the OpenGraph and twitter descriptions.
|
||||
* @example
|
||||
* ```tsx
|
||||
* "My Blog Description"
|
||||
* <meta name="description" content="My Blog Description" />
|
||||
* ```
|
||||
*/
|
||||
description?: null | string;
|
||||
|
||||
// Standard metadata names
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta/name
|
||||
|
||||
/**
|
||||
* The application name.
|
||||
* @example
|
||||
* ```tsx
|
||||
* "My Blog"
|
||||
* <meta name="application-name" content="My Blog" />
|
||||
* ```
|
||||
*/
|
||||
applicationName?: null | string;
|
||||
|
||||
/**
|
||||
* The authors of the document.
|
||||
* @example
|
||||
* ```tsx
|
||||
* [{ name: "Next.js Team", url: "https://nextjs.org" }]
|
||||
*
|
||||
* <meta name="author" content="Next.js Team" />
|
||||
* <link rel="author" href="https://nextjs.org" />
|
||||
* ```
|
||||
*/
|
||||
authors?: null | Author | Array<Author>;
|
||||
|
||||
/**
|
||||
* The generator used for the document.
|
||||
* @example
|
||||
* ```tsx
|
||||
* "Next.js"
|
||||
*
|
||||
* <meta name="generator" content="Next.js" />
|
||||
* ```
|
||||
*/
|
||||
generator?: null | string;
|
||||
|
||||
/**
|
||||
* The keywords for the document. If an array is provided, it will be flattened into a single tag with comma separation.
|
||||
* @example
|
||||
* ```tsx
|
||||
* "nextjs, react, blog"
|
||||
* <meta name="keywords" content="nextjs, react, blog" />
|
||||
*
|
||||
* ["react", "server components"]
|
||||
* <meta name="keywords" content="react, server components" />
|
||||
* ```
|
||||
*/
|
||||
keywords?: null | string | Array<string>;
|
||||
|
||||
/**
|
||||
* The referrer setting for the document.
|
||||
* @example
|
||||
* ```tsx
|
||||
* "origin"
|
||||
* <meta name="referrer" content="origin" />
|
||||
* ```
|
||||
*/
|
||||
referrer?: null | ReferrerEnum;
|
||||
|
||||
/**
|
||||
* The theme color for the document.
|
||||
* @example
|
||||
* ```tsx
|
||||
* "#000000"
|
||||
* <meta name="theme-color" content="#000000" />
|
||||
*
|
||||
* { media: "(prefers-color-scheme: dark)", color: "#000000" }
|
||||
* <meta name="theme-color" media="(prefers-color-scheme: dark)" content="#000000" />
|
||||
*
|
||||
* [
|
||||
* { media: "(prefers-color-scheme: dark)", color: "#000000" },
|
||||
* { media: "(prefers-color-scheme: light)", color: "#ffffff" }
|
||||
* ]
|
||||
* <meta name="theme-color" media="(prefers-color-scheme: dark)" content="#000000" />
|
||||
* <meta name="theme-color" media="(prefers-color-scheme: light)" content="#ffffff" />
|
||||
* ```
|
||||
*/
|
||||
themeColor?: null | string | ThemeColorDescriptor | ThemeColorDescriptor[];
|
||||
|
||||
/**
|
||||
* The color scheme for the document.
|
||||
* @example
|
||||
* ```tsx
|
||||
* "dark"
|
||||
* <meta name="color-scheme" content="dark" />
|
||||
* ```
|
||||
*/
|
||||
colorScheme?: null | ColorSchemeEnum;
|
||||
|
||||
/**
|
||||
* The viewport setting for the document.
|
||||
* @example
|
||||
* ```tsx
|
||||
* "width=device-width, initial-scale=1"
|
||||
* <meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
*
|
||||
* { width: "device-width", initialScale: 1 }
|
||||
* <meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
* ```
|
||||
*/
|
||||
viewport?: null | string | Viewport;
|
||||
|
||||
/**
|
||||
* The creator of the document.
|
||||
* @example
|
||||
* ```tsx
|
||||
* "Next.js Team"
|
||||
* <meta name="creator" content="Next.js Team" />
|
||||
* ```
|
||||
*/
|
||||
creator?: null | string;
|
||||
|
||||
/**
|
||||
* The publisher of the document.
|
||||
* @example
|
||||
*
|
||||
* ```tsx
|
||||
* "Vercel"
|
||||
* <meta name="publisher" content="Vercel" />
|
||||
* ```
|
||||
*/
|
||||
publisher?: null | string;
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta/name#other_metadata_names
|
||||
|
||||
/**
|
||||
* The robots setting for the document.
|
||||
*
|
||||
* @see https://developer.mozilla.org/en-US/docs/Glossary/Robots.txt
|
||||
* @example
|
||||
* ```tsx
|
||||
* "index, follow"
|
||||
* <meta name="robots" content="index, follow" />
|
||||
*
|
||||
* { index: false, follow: false }
|
||||
* <meta name="robots" content="noindex, nofollow" />
|
||||
* ```
|
||||
*/
|
||||
robots?: null | string | Robots;
|
||||
|
||||
/**
|
||||
* The canonical and alternate URLs for the document.
|
||||
* @example
|
||||
* ```tsx
|
||||
* { canonical: "https://example.com" }
|
||||
* <link rel="canonical" href="https://example.com" />
|
||||
*
|
||||
* { canonical: "https://example.com", hreflang: { "en-US": "https://example.com/en-US" } }
|
||||
* <link rel="canonical" href="https://example.com" />
|
||||
* <link rel="alternate" href="https://example.com/en-US" hreflang="en-US" />
|
||||
* ```
|
||||
*
|
||||
* Multiple titles example for alternate URLs except `canonical`:
|
||||
* ```tsx
|
||||
* {
|
||||
* canonical: "https://example.com",
|
||||
* types: {
|
||||
* 'application/rss+xml': [
|
||||
* { url: 'blog.rss', title: 'rss' },
|
||||
* { url: 'blog/js.rss', title: 'js title' },
|
||||
* ],
|
||||
* },
|
||||
* }
|
||||
* <link rel="canonical" href="https://example.com" />
|
||||
* <link rel="alternate" href="https://example.com/blog.rss" type="application/rss+xml" title="rss" />
|
||||
* <link rel="alternate" href="https://example.com/blog/js.rss" type="application/rss+xml" title="js title" />
|
||||
* ```
|
||||
*/
|
||||
alternates?: null | AlternateURLs;
|
||||
|
||||
/**
|
||||
* The icons for the document. Defaults to rel="icon".
|
||||
*
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel#attr-icon
|
||||
* @example
|
||||
* ```tsx
|
||||
* "https://example.com/icon.png"
|
||||
* <link rel="icon" href="https://example.com/icon.png" />
|
||||
*
|
||||
* { icon: "https://example.com/icon.png", apple: "https://example.com/apple-icon.png" }
|
||||
* <link rel="icon" href="https://example.com/icon.png" />
|
||||
* <link rel="apple-touch-icon" href="https://example.com/apple-icon.png" />
|
||||
*
|
||||
* [{ rel: "icon", url: "https://example.com/icon.png" }, { rel: "apple-touch-icon", url: "https://example.com/apple-icon.png" }]
|
||||
* <link rel="icon" href="https://example.com/icon.png" />
|
||||
* <link rel="apple-touch-icon" href="https://example.com/apple-icon.png" />
|
||||
* ```
|
||||
*/
|
||||
icons?: null | IconURL | Array<Icon> | Icons;
|
||||
|
||||
/**
|
||||
* A web application manifest, as defined in the Web Application Manifest specification.
|
||||
*
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/Manifest
|
||||
* @example
|
||||
* ```tsx
|
||||
* "https://example.com/manifest.json"
|
||||
* <link rel="manifest" href="https://example.com/manifest.json" />
|
||||
* ```
|
||||
*/
|
||||
manifest?: null | string | URL;
|
||||
|
||||
/**
|
||||
* The Open Graph metadata for the document.
|
||||
*
|
||||
* @see https://ogp.me
|
||||
* @example
|
||||
* ```tsx
|
||||
* {
|
||||
* type: "website",
|
||||
* url: "https://example.com",
|
||||
* title: "My Website",
|
||||
* description: "My Website Description",
|
||||
* siteName: "My Website",
|
||||
* images: [{
|
||||
* url: "https://example.com/og.png",
|
||||
* }],
|
||||
* }
|
||||
*
|
||||
* <meta property="og:type" content="website" />
|
||||
* <meta property="og:url" content="https://example.com" />
|
||||
* <meta property="og:site_name" content="My Website" />
|
||||
* <meta property="og:title" content="My Website" />
|
||||
* <meta property="og:description" content="My Website Description" />
|
||||
* <meta property="og:image" content="https://example.com/og.png" />
|
||||
* ```
|
||||
*/
|
||||
openGraph?: null | OpenGraph;
|
||||
|
||||
/**
|
||||
* The Twitter metadata for the document.
|
||||
* @example
|
||||
* ```tsx
|
||||
* { card: "summary_large_image", site: "@site", creator: "@creator", "images": "https://example.com/og.png" }
|
||||
*
|
||||
* <meta name="twitter:card" content="summary_large_image" />
|
||||
* <meta name="twitter:site" content="@site" />
|
||||
* <meta name="twitter:creator" content="@creator" />
|
||||
* <meta name="twitter:title" content="My Website" />
|
||||
* <meta name="twitter:description" content="My Website Description" />
|
||||
* <meta name="twitter:image" content="https://example.com/og.png" />
|
||||
* ```
|
||||
*/
|
||||
twitter?: null | Twitter;
|
||||
|
||||
/**
|
||||
* The common verification tokens for the document.
|
||||
* @example
|
||||
* ```tsx
|
||||
* { verification: { google: "1234567890", yandex: "1234567890", "me": "1234567890" } }
|
||||
* <meta name="google-site-verification" content="1234567890" />
|
||||
* <meta name="yandex-verification" content="1234567890" />
|
||||
* <meta name="me" content="@me" />
|
||||
* ```
|
||||
*/
|
||||
verification?: Verification;
|
||||
|
||||
/**
|
||||
* The Apple web app metadata for the document.
|
||||
*
|
||||
* @see https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html
|
||||
* @example
|
||||
* ```tsx
|
||||
* { capable: true, title: "My Website", statusBarStyle: "black-translucent" }
|
||||
* <meta name="apple-mobile-web-app-capable" content="yes" />
|
||||
* <meta name="apple-mobile-web-app-title" content="My Website" />
|
||||
* <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
|
||||
* ```
|
||||
*/
|
||||
appleWebApp?: null | boolean | AppleWebApp;
|
||||
|
||||
/**
|
||||
* Indicates if devices should try to interpret various formats and make actionable links out of them. For example it controles
|
||||
* if telephone numbers on mobile that can be clicked to dial or not.
|
||||
* @example
|
||||
* ```tsx
|
||||
* { telephone: false }
|
||||
* <meta name="format-detection" content="telephone=no" />
|
||||
* ```
|
||||
*/
|
||||
formatDetection?: null | FormatDetection;
|
||||
|
||||
/**
|
||||
* The metadata for the iTunes App.
|
||||
* It adds the `name="apple-itunes-app"` meta tag.
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* { app: { id: "123456789", affiliateData: "123456789", appArguments: "123456789" } }
|
||||
* <meta name="apple-itunes-app" content="app-id=123456789, affiliate-data=123456789, app-arguments=123456789" />
|
||||
* ```
|
||||
*/
|
||||
itunes?: null | ItunesApp;
|
||||
|
||||
/**
|
||||
* A brief description of what this web-page is about. Not recommended, superseded by description.
|
||||
* It adds the `name="abstract"` meta tag.
|
||||
*
|
||||
* @see https://www.metatags.org/all-meta-tags-overview/meta-name-abstract/
|
||||
* @example
|
||||
* ```tsx
|
||||
* "My Website Description"
|
||||
* <meta name="abstract" content="My Website Description" />
|
||||
* ```
|
||||
*/
|
||||
abstract?: null | string;
|
||||
|
||||
/**
|
||||
* The Facebook AppLinks metadata for the document.
|
||||
* @example
|
||||
* ```tsx
|
||||
* { ios: { appStoreId: "123456789", url: "https://example.com" }, android: { packageName: "com.example", url: "https://example.com" } }
|
||||
*
|
||||
* <meta property="al:ios:app_store_id" content="123456789" />
|
||||
* <meta property="al:ios:url" content="https://example.com" />
|
||||
* <meta property="al:android:package" content="com.example" />
|
||||
* <meta property="al:android:url" content="https://example.com" />
|
||||
* ```
|
||||
*/
|
||||
appLinks?: null | AppLinks;
|
||||
|
||||
/**
|
||||
* The archives link rel property.
|
||||
* @example
|
||||
* ```tsx
|
||||
* { archives: "https://example.com/archives" }
|
||||
* <link rel="archives" href="https://example.com/archives" />
|
||||
* ```
|
||||
*/
|
||||
archives?: null | string | Array<string>;
|
||||
|
||||
/**
|
||||
* The assets link rel property.
|
||||
* @example
|
||||
* ```tsx
|
||||
* "https://example.com/assets"
|
||||
* <link rel="assets" href="https://example.com/assets" />
|
||||
* ```
|
||||
*/
|
||||
assets?: null | string | Array<string>;
|
||||
|
||||
/**
|
||||
* The bookmarks link rel property.
|
||||
* @example
|
||||
* ```tsx
|
||||
* "https://example.com/bookmarks"
|
||||
* <link rel="bookmarks" href="https://example.com/bookmarks" />
|
||||
* ```
|
||||
*/
|
||||
bookmarks?: null | string | Array<string>; // This is technically against HTML spec but is used in wild
|
||||
|
||||
// meta name properties
|
||||
|
||||
/**
|
||||
* The category meta name property.
|
||||
* @example
|
||||
* ```tsx
|
||||
* "My Category"
|
||||
* <meta name="category" content="My Category" />
|
||||
* ```
|
||||
*/
|
||||
category?: null | string;
|
||||
|
||||
/**
|
||||
* The classification meta name property.
|
||||
* @example
|
||||
* ```tsx
|
||||
* "My Classification"
|
||||
* <meta name="classification" content="My Classification" />
|
||||
* ```
|
||||
*/
|
||||
classification?: null | string;
|
||||
|
||||
/**
|
||||
* Arbitrary name/value pairs for the document.
|
||||
*/
|
||||
other?: {
|
||||
[name: string]: string | number | Array<string | number>;
|
||||
} & DeprecatedMetadataFields;
|
||||
}
|
||||
|
||||
interface ResolvedMetadata extends DeprecatedMetadataFields {
|
||||
// origin and base path for absolute urls for various metadata links such as
|
||||
// opengraph-image
|
||||
metadataBase: null | URL;
|
||||
|
||||
// The Document title and template if defined
|
||||
title: null | AbsoluteTemplateString;
|
||||
|
||||
// The Document description, and optionally the opengraph and twitter descriptions
|
||||
description: null | string;
|
||||
|
||||
// Standard metadata names
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta/name
|
||||
applicationName: null | string;
|
||||
authors: null | Array<Author>;
|
||||
generator: null | string;
|
||||
// if you provide an array it will be flattened into a single tag with comma separation
|
||||
keywords: null | Array<string>;
|
||||
referrer: null | ReferrerEnum;
|
||||
themeColor: null | ThemeColorDescriptor[];
|
||||
colorScheme: null | ColorSchemeEnum;
|
||||
viewport: null | string;
|
||||
creator: null | string;
|
||||
publisher: null | string;
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta/name#other_metadata_names
|
||||
robots: null | ResolvedRobots;
|
||||
|
||||
// The canonical and alternate URLs for this location
|
||||
alternates: null | ResolvedAlternateURLs;
|
||||
|
||||
// Defaults to rel="icon" but the Icons type can be used
|
||||
// to get more specific about rel types
|
||||
icons: null | ResolvedIcons;
|
||||
|
||||
openGraph: null | ResolvedOpenGraph;
|
||||
|
||||
manifest: null | string | URL;
|
||||
|
||||
twitter: null | ResolvedTwitterMetadata;
|
||||
|
||||
// common verification tokens
|
||||
verification: null | ResolvedVerification;
|
||||
|
||||
// Apple web app metadata
|
||||
// https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html
|
||||
appleWebApp: null | ResolvedAppleWebApp;
|
||||
|
||||
// Should devices try to interpret various formats and make actionable links
|
||||
// out of them? The canonical example is telephone numbers on mobile that can
|
||||
// be clicked to dial
|
||||
formatDetection: null | FormatDetection;
|
||||
|
||||
// meta name="apple-itunes-app"
|
||||
itunes: null | ItunesApp;
|
||||
|
||||
// meta name="abstract"
|
||||
// A brief description of what this web-page is about.
|
||||
// Not recommended, superceded by description.
|
||||
// https://www.metatags.org/all-meta-tags-overview/meta-name-abstract/
|
||||
abstract: null | string;
|
||||
|
||||
// Facebook AppLinks
|
||||
appLinks: null | ResolvedAppLinks;
|
||||
|
||||
// link rel properties
|
||||
archives: null | Array<string>;
|
||||
assets: null | Array<string>;
|
||||
bookmarks: null | Array<string>; // This is technically against HTML spec but is used in wild
|
||||
|
||||
// meta name properties
|
||||
category: null | string;
|
||||
classification: null | string;
|
||||
|
||||
// Arbitrary name/value pairs
|
||||
other:
|
||||
| null
|
||||
| ({
|
||||
[name: string]: string | number | Array<string | number>;
|
||||
} & DeprecatedMetadataFields);
|
||||
}
|
||||
|
||||
type RobotsFile = {
|
||||
// Apply rules for all
|
||||
rules:
|
||||
| {
|
||||
userAgent?: string | string[];
|
||||
allow?: string | string[];
|
||||
disallow?: string | string[];
|
||||
crawlDelay?: number;
|
||||
}
|
||||
// Apply rules for specific user agents
|
||||
| Array<{
|
||||
userAgent: string | string[];
|
||||
allow?: string | string[];
|
||||
disallow?: string | string[];
|
||||
crawlDelay?: number;
|
||||
}>;
|
||||
sitemap?: string | string[];
|
||||
host?: string;
|
||||
};
|
||||
|
||||
type SitemapFile = Array<{
|
||||
url: string;
|
||||
lastModified?: string | Date;
|
||||
}>;
|
||||
|
||||
type ResolvingMetadata = Promise<ResolvedMetadata>;
|
||||
declare namespace MetadataRoute {
|
||||
export type Robots = RobotsFile;
|
||||
export type Sitemap = SitemapFile;
|
||||
export type Manifest = ManifestFile;
|
||||
}
|
||||
|
||||
export { Metadata, MetadataRoute, ResolvedMetadata, ResolvingMetadata };
|
155
framework/meta/nextjs/types/metadata-types.ts
Normal file
155
framework/meta/nextjs/types/metadata-types.ts
Normal file
|
@ -0,0 +1,155 @@
|
|||
/**
|
||||
* Metadata types
|
||||
*/
|
||||
|
||||
export interface DeprecatedMetadataFields {
|
||||
/**
|
||||
* Deprecated options that have a preferred method
|
||||
* @deprecated Use appWebApp to configure apple-mobile-web-app-capable which provides
|
||||
* @see https://www.appsloveworld.com/coding/iphone/11/difference-between-apple-mobile-web-app-capable-and-apple-touch-fullscreen-ipho
|
||||
*/
|
||||
"apple-touch-fullscreen"?: never;
|
||||
|
||||
/**
|
||||
* Obsolete since iOS 7.
|
||||
* @see https://web.dev/apple-touch-icon/
|
||||
* @deprecated use icons.apple or instead
|
||||
*/
|
||||
"apple-touch-icon-precomposed"?: never;
|
||||
}
|
||||
|
||||
export type TemplateString =
|
||||
| DefaultTemplateString
|
||||
| AbsoluteTemplateString
|
||||
| AbsoluteString;
|
||||
export type DefaultTemplateString = {
|
||||
default: string;
|
||||
template: string;
|
||||
};
|
||||
export type AbsoluteTemplateString = {
|
||||
absolute: string;
|
||||
template: string | null;
|
||||
};
|
||||
export type AbsoluteString = {
|
||||
absolute: string;
|
||||
};
|
||||
|
||||
export type Author = {
|
||||
// renders as <link rel="author"...
|
||||
url?: string | URL;
|
||||
// renders as <meta name="author"...
|
||||
name?: string;
|
||||
};
|
||||
|
||||
// does not include "unsafe-URL". to use this users should
|
||||
// use '"unsafe-URL" as ReferrerEnum'
|
||||
export type ReferrerEnum =
|
||||
| "no-referrer"
|
||||
| "origin"
|
||||
| "no-referrer-when-downgrade"
|
||||
| "origin-when-cross-origin"
|
||||
| "same-origin"
|
||||
| "strict-origin"
|
||||
| "strict-origin-when-cross-origin";
|
||||
|
||||
export type ColorSchemeEnum =
|
||||
| "normal"
|
||||
| "light"
|
||||
| "dark"
|
||||
| "light dark"
|
||||
| "dark light"
|
||||
| "only light";
|
||||
|
||||
type RobotsInfo = {
|
||||
// all and none will be inferred from index/follow boolean options
|
||||
index?: boolean;
|
||||
follow?: boolean;
|
||||
|
||||
/** @deprecated set index to false instead */
|
||||
noindex?: never;
|
||||
/** @deprecated set follow to false instead */
|
||||
nofollow?: never;
|
||||
|
||||
noarchive?: boolean;
|
||||
nosnippet?: boolean;
|
||||
noimageindex?: boolean;
|
||||
nocache?: boolean;
|
||||
notranslate?: boolean;
|
||||
indexifembedded?: boolean;
|
||||
nositelinkssearchbox?: boolean;
|
||||
unavailable_after?: string;
|
||||
"max-video-preview"?: number | string;
|
||||
"max-image-preview"?: "none" | "standard" | "large";
|
||||
"max-snippet"?: number;
|
||||
};
|
||||
export type Robots = RobotsInfo & {
|
||||
// if you want to specify an alternate robots just for google
|
||||
googleBot?: string | RobotsInfo;
|
||||
};
|
||||
|
||||
export type ResolvedRobots = {
|
||||
basic: string | null;
|
||||
googleBot: string | null;
|
||||
};
|
||||
|
||||
export type IconURL = string | URL;
|
||||
export type Icon = IconURL | IconDescriptor;
|
||||
export type IconDescriptor = {
|
||||
url: string | URL;
|
||||
type?: string;
|
||||
sizes?: string;
|
||||
/** defaults to rel="icon" unless superseded by Icons map */
|
||||
rel?: string;
|
||||
media?: string;
|
||||
/**
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/fetchPriority
|
||||
*/
|
||||
fetchPriority?: "high" | "low" | "auto";
|
||||
};
|
||||
|
||||
export type Icons = {
|
||||
/** rel="icon" */
|
||||
icon?: Icon | Icon[];
|
||||
/** rel="shortcut icon" */
|
||||
shortcut?: Icon | Icon[];
|
||||
/**
|
||||
* @see https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/ConfiguringWebApplications/ConfiguringWebApplications.html
|
||||
* rel="apple-touch-icon"
|
||||
*/
|
||||
apple?: Icon | Icon[];
|
||||
/** rel inferred from descriptor, defaults to "icon" */
|
||||
other?: IconDescriptor | IconDescriptor[];
|
||||
};
|
||||
|
||||
export type Verification = {
|
||||
google?: null | string | number | (string | number)[];
|
||||
yahoo?: null | string | number | (string | number)[];
|
||||
yandex?: null | string | number | (string | number)[];
|
||||
me?: null | string | number | (string | number)[];
|
||||
// if you ad-hoc additional verification
|
||||
other?: {
|
||||
[name: string]: string | number | (string | number)[];
|
||||
};
|
||||
};
|
||||
|
||||
export type ResolvedVerification = {
|
||||
google?: null | (string | number)[];
|
||||
yahoo?: null | (string | number)[];
|
||||
yandex?: null | (string | number)[];
|
||||
me?: null | (string | number)[];
|
||||
other?: {
|
||||
[name: string]: (string | number)[];
|
||||
};
|
||||
};
|
||||
|
||||
export type ResolvedIcons = {
|
||||
icon: IconDescriptor[];
|
||||
apple: IconDescriptor[];
|
||||
shortcut?: IconDescriptor[];
|
||||
other?: IconDescriptor[];
|
||||
};
|
||||
|
||||
export type ThemeColorDescriptor = {
|
||||
color: string;
|
||||
media?: string;
|
||||
};
|
267
framework/meta/nextjs/types/opengraph-types.ts
Normal file
267
framework/meta/nextjs/types/opengraph-types.ts
Normal file
|
@ -0,0 +1,267 @@
|
|||
import type { AbsoluteTemplateString, TemplateString } from "./metadata-types";
|
||||
|
||||
export type OpenGraphType =
|
||||
| "article"
|
||||
| "book"
|
||||
| "music.song"
|
||||
| "music.album"
|
||||
| "music.playlist"
|
||||
| "music.radio_station"
|
||||
| "profile"
|
||||
| "website"
|
||||
| "video.tv_show"
|
||||
| "video.other"
|
||||
| "video.movie"
|
||||
| "video.episode";
|
||||
|
||||
export type OpenGraph =
|
||||
| OpenGraphWebsite
|
||||
| OpenGraphArticle
|
||||
| OpenGraphBook
|
||||
| OpenGraphProfile
|
||||
| OpenGraphMusicSong
|
||||
| OpenGraphMusicAlbum
|
||||
| OpenGraphMusicPlaylist
|
||||
| OpenGraphRadioStation
|
||||
| OpenGraphVideoMovie
|
||||
| OpenGraphVideoEpisode
|
||||
| OpenGraphVideoTVShow
|
||||
| OpenGraphVideoOther
|
||||
| OpenGraphMetadata;
|
||||
|
||||
// update this type to reflect actual locales
|
||||
type Locale = string;
|
||||
|
||||
type OpenGraphMetadata = {
|
||||
determiner?: "a" | "an" | "the" | "auto" | "";
|
||||
title?: string | TemplateString;
|
||||
description?: string;
|
||||
emails?: string | Array<string>;
|
||||
phoneNumbers?: string | Array<string>;
|
||||
faxNumbers?: string | Array<string>;
|
||||
siteName?: string;
|
||||
locale?: Locale;
|
||||
alternateLocale?: Locale | Array<Locale>;
|
||||
images?: OGImage | Array<OGImage>;
|
||||
audio?: OGAudio | Array<OGAudio>;
|
||||
videos?: OGVideo | Array<OGVideo>;
|
||||
url?: string | URL;
|
||||
countryName?: string;
|
||||
ttl?: number;
|
||||
};
|
||||
type OpenGraphWebsite = OpenGraphMetadata & {
|
||||
type: "website";
|
||||
};
|
||||
type OpenGraphArticle = OpenGraphMetadata & {
|
||||
type: "article";
|
||||
publishedTime?: string; // datetime
|
||||
modifiedTime?: string; // datetime
|
||||
expirationTime?: string; // datetime
|
||||
authors?: null | string | URL | Array<string | URL>;
|
||||
section?: null | string;
|
||||
tags?: null | string | Array<string>;
|
||||
};
|
||||
type OpenGraphBook = OpenGraphMetadata & {
|
||||
type: "book";
|
||||
isbn?: null | string;
|
||||
releaseDate?: null | string; // datetime
|
||||
authors?: null | string | URL | Array<string | URL>;
|
||||
tags?: null | string | Array<string>;
|
||||
};
|
||||
type OpenGraphProfile = OpenGraphMetadata & {
|
||||
type: "profile";
|
||||
firstName?: null | string;
|
||||
lastName?: null | string;
|
||||
username?: null | string;
|
||||
gender?: null | string;
|
||||
};
|
||||
type OpenGraphMusicSong = OpenGraphMetadata & {
|
||||
type: "music.song";
|
||||
duration?: null | number;
|
||||
albums?: null | string | URL | OGAlbum | Array<string | URL | OGAlbum>;
|
||||
musicians?: null | string | URL | Array<string | URL>;
|
||||
};
|
||||
type OpenGraphMusicAlbum = OpenGraphMetadata & {
|
||||
type: "music.album";
|
||||
songs?: null | string | URL | OGSong | Array<string | URL | OGSong>;
|
||||
musicians?: null | string | URL | Array<string | URL>;
|
||||
releaseDate?: null | string; // datetime
|
||||
};
|
||||
type OpenGraphMusicPlaylist = OpenGraphMetadata & {
|
||||
type: "music.playlist";
|
||||
songs?: null | string | URL | OGSong | Array<string | URL | OGSong>;
|
||||
creators?: null | string | URL | Array<string | URL>;
|
||||
};
|
||||
type OpenGraphRadioStation = OpenGraphMetadata & {
|
||||
type: "music.radio_station";
|
||||
creators?: null | string | URL | Array<string | URL>;
|
||||
};
|
||||
type OpenGraphVideoMovie = OpenGraphMetadata & {
|
||||
type: "video.movie";
|
||||
actors?: null | string | URL | OGActor | Array<string | URL | OGActor>;
|
||||
directors?: null | string | URL | Array<string | URL>;
|
||||
writers?: null | string | URL | Array<string | URL>;
|
||||
duration?: null | number;
|
||||
releaseDate?: null | string; // datetime
|
||||
tags?: null | string | Array<string>;
|
||||
};
|
||||
type OpenGraphVideoEpisode = OpenGraphMetadata & {
|
||||
type: "video.episode";
|
||||
actors?: null | string | URL | OGActor | Array<string | URL | OGActor>;
|
||||
directors?: null | string | URL | Array<string | URL>;
|
||||
writers?: null | string | URL | Array<string | URL>;
|
||||
duration?: null | number;
|
||||
releaseDate?: null | string; // datetime
|
||||
tags?: null | string | Array<string>;
|
||||
series?: null | string | URL;
|
||||
};
|
||||
type OpenGraphVideoTVShow = OpenGraphMetadata & {
|
||||
type: "video.tv_show";
|
||||
};
|
||||
type OpenGraphVideoOther = OpenGraphMetadata & {
|
||||
type: "video.other";
|
||||
};
|
||||
|
||||
type OGImage = string | OGImageDescriptor | URL;
|
||||
type OGImageDescriptor = {
|
||||
url: string | URL;
|
||||
secureUrl?: string | URL;
|
||||
alt?: string;
|
||||
type?: string;
|
||||
width?: string | number;
|
||||
height?: string | number;
|
||||
};
|
||||
type OGAudio = string | OGAudioDescriptor | URL;
|
||||
type OGAudioDescriptor = {
|
||||
url: string | URL;
|
||||
secureUrl?: string | URL;
|
||||
type?: string;
|
||||
};
|
||||
type OGVideo = string | OGVideoDescriptor | URL;
|
||||
type OGVideoDescriptor = {
|
||||
url: string | URL;
|
||||
secureUrl?: string | URL;
|
||||
type?: string;
|
||||
width?: string | number;
|
||||
height?: string | number;
|
||||
};
|
||||
|
||||
export type ResolvedOpenGraph =
|
||||
| ResolvedOpenGraphWebsite
|
||||
| ResolvedOpenGraphArticle
|
||||
| ResolvedOpenGraphBook
|
||||
| ResolvedOpenGraphProfile
|
||||
| ResolvedOpenGraphMusicSong
|
||||
| ResolvedOpenGraphMusicAlbum
|
||||
| ResolvedOpenGraphMusicPlaylist
|
||||
| ResolvedOpenGraphRadioStation
|
||||
| ResolvedOpenGraphVideoMovie
|
||||
| ResolvedOpenGraphVideoEpisode
|
||||
| ResolvedOpenGraphVideoTVShow
|
||||
| ResolvedOpenGraphVideoOther
|
||||
| ResolvedOpenGraphMetadata;
|
||||
|
||||
type ResolvedOpenGraphMetadata = {
|
||||
determiner?: "a" | "an" | "the" | "auto" | "";
|
||||
title?: AbsoluteTemplateString;
|
||||
description?: string;
|
||||
emails?: Array<string>;
|
||||
phoneNumbers?: Array<string>;
|
||||
faxNumbers?: Array<string>;
|
||||
siteName?: string;
|
||||
locale?: Locale;
|
||||
alternateLocale?: Array<Locale>;
|
||||
images?: Array<OGImage>;
|
||||
audio?: Array<OGAudio>;
|
||||
videos?: Array<OGVideo>;
|
||||
url: null | URL | string;
|
||||
countryName?: string;
|
||||
ttl?: number;
|
||||
};
|
||||
type ResolvedOpenGraphWebsite = ResolvedOpenGraphMetadata & {
|
||||
type: "website";
|
||||
};
|
||||
type ResolvedOpenGraphArticle = ResolvedOpenGraphMetadata & {
|
||||
type: "article";
|
||||
publishedTime?: string; // datetime
|
||||
modifiedTime?: string; // datetime
|
||||
expirationTime?: string; // datetime
|
||||
authors?: Array<string>;
|
||||
section?: string;
|
||||
tags?: Array<string>;
|
||||
};
|
||||
type ResolvedOpenGraphBook = ResolvedOpenGraphMetadata & {
|
||||
type: "book";
|
||||
isbn?: string;
|
||||
releaseDate?: string; // datetime
|
||||
authors?: Array<string>;
|
||||
tags?: Array<string>;
|
||||
};
|
||||
type ResolvedOpenGraphProfile = ResolvedOpenGraphMetadata & {
|
||||
type: "profile";
|
||||
firstName?: string;
|
||||
lastName?: string;
|
||||
username?: string;
|
||||
gender?: string;
|
||||
};
|
||||
type ResolvedOpenGraphMusicSong = ResolvedOpenGraphMetadata & {
|
||||
type: "music.song";
|
||||
duration?: number;
|
||||
albums?: Array<OGAlbum>;
|
||||
musicians?: Array<string | URL>;
|
||||
};
|
||||
type ResolvedOpenGraphMusicAlbum = ResolvedOpenGraphMetadata & {
|
||||
type: "music.album";
|
||||
songs?: Array<string | URL | OGSong>;
|
||||
musicians?: Array<string | URL>;
|
||||
releaseDate?: string; // datetime
|
||||
};
|
||||
type ResolvedOpenGraphMusicPlaylist = ResolvedOpenGraphMetadata & {
|
||||
type: "music.playlist";
|
||||
songs?: Array<string | URL | OGSong>;
|
||||
creators?: Array<string | URL>;
|
||||
};
|
||||
type ResolvedOpenGraphRadioStation = ResolvedOpenGraphMetadata & {
|
||||
type: "music.radio_station";
|
||||
creators?: Array<string | URL>;
|
||||
};
|
||||
type ResolvedOpenGraphVideoMovie = ResolvedOpenGraphMetadata & {
|
||||
type: "video.movie";
|
||||
actors?: Array<string | URL | OGActor>;
|
||||
directors?: Array<string | URL>;
|
||||
writers?: Array<string | URL>;
|
||||
duration?: number;
|
||||
releaseDate?: string; // datetime
|
||||
tags?: Array<string>;
|
||||
};
|
||||
type ResolvedOpenGraphVideoEpisode = ResolvedOpenGraphMetadata & {
|
||||
type: "video.episode";
|
||||
actors?: Array<string | URL | OGActor>;
|
||||
directors?: Array<string | URL>;
|
||||
writers?: Array<string | URL>;
|
||||
duration?: number;
|
||||
releaseDate?: string; // datetime
|
||||
tags?: Array<string>;
|
||||
series?: string | URL;
|
||||
};
|
||||
type ResolvedOpenGraphVideoTVShow = ResolvedOpenGraphMetadata & {
|
||||
type: "video.tv_show";
|
||||
};
|
||||
type ResolvedOpenGraphVideoOther = ResolvedOpenGraphMetadata & {
|
||||
type: "video.other";
|
||||
};
|
||||
|
||||
type OGSong = {
|
||||
url: string | URL;
|
||||
disc?: number;
|
||||
track?: number;
|
||||
};
|
||||
type OGAlbum = {
|
||||
url: string | URL;
|
||||
disc?: number;
|
||||
track?: number;
|
||||
};
|
||||
type OGActor = {
|
||||
url: string | URL;
|
||||
role?: string;
|
||||
};
|
17
framework/meta/nextjs/types/resolvers.ts
Normal file
17
framework/meta/nextjs/types/resolvers.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import { Metadata, ResolvedMetadata } from "./metadata-interface";
|
||||
|
||||
export type FieldResolver<Key extends keyof Metadata> = (
|
||||
T: Metadata[Key],
|
||||
) => ResolvedMetadata[Key];
|
||||
export type FieldResolverWithMetadataBase<
|
||||
Key extends keyof Metadata,
|
||||
Options = undefined,
|
||||
> = Options extends undefined ? (
|
||||
T: Metadata[Key],
|
||||
metadataBase: ResolvedMetadata["metadataBase"],
|
||||
) => ResolvedMetadata[Key]
|
||||
: (
|
||||
T: Metadata[Key],
|
||||
metadataBase: ResolvedMetadata["metadataBase"],
|
||||
options: Options,
|
||||
) => ResolvedMetadata[Key];
|
94
framework/meta/nextjs/types/twitter-types.ts
Normal file
94
framework/meta/nextjs/types/twitter-types.ts
Normal file
|
@ -0,0 +1,94 @@
|
|||
// Reference: https://developer.twitter.com/en/docs/twitter-for-websites/cards/overview/markup
|
||||
|
||||
import type { AbsoluteTemplateString, TemplateString } from "./metadata-types";
|
||||
|
||||
export type Twitter =
|
||||
| TwitterSummary
|
||||
| TwitterSummaryLargeImage
|
||||
| TwitterPlayer
|
||||
| TwitterApp
|
||||
| TwitterMetadata;
|
||||
|
||||
type TwitterMetadata = {
|
||||
// defaults to card="summary"
|
||||
site?: string; // username for account associated to the site itself
|
||||
siteId?: string; // id for account associated to the site itself
|
||||
creator?: string; // username for the account associated to the creator of the content on the site
|
||||
creatorId?: string; // id for the account associated to the creator of the content on the site
|
||||
description?: string;
|
||||
title?: string | TemplateString;
|
||||
images?: TwitterImage | Array<TwitterImage>;
|
||||
};
|
||||
type TwitterSummary = TwitterMetadata & {
|
||||
card: "summary";
|
||||
};
|
||||
type TwitterSummaryLargeImage = TwitterMetadata & {
|
||||
card: "summary_large_image";
|
||||
};
|
||||
type TwitterPlayer = TwitterMetadata & {
|
||||
card: "player";
|
||||
players: TwitterPlayerDescriptor | Array<TwitterPlayerDescriptor>;
|
||||
};
|
||||
type TwitterApp = TwitterMetadata & {
|
||||
card: "app";
|
||||
app: TwitterAppDescriptor;
|
||||
};
|
||||
export type TwitterAppDescriptor = {
|
||||
id: {
|
||||
iphone?: string | number;
|
||||
ipad?: string | number;
|
||||
googleplay?: string;
|
||||
};
|
||||
url?: {
|
||||
iphone?: string | URL;
|
||||
ipad?: string | URL;
|
||||
googleplay?: string | URL;
|
||||
};
|
||||
name?: string;
|
||||
};
|
||||
|
||||
type TwitterImage = string | TwitterImageDescriptor | URL;
|
||||
type TwitterImageDescriptor = {
|
||||
url: string | URL;
|
||||
alt?: string;
|
||||
secureUrl?: string | URL;
|
||||
type?: string;
|
||||
width?: string | number;
|
||||
height?: string | number;
|
||||
};
|
||||
type TwitterPlayerDescriptor = {
|
||||
playerUrl: string | URL;
|
||||
streamUrl: string | URL;
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
|
||||
type ResolvedTwitterImage = {
|
||||
url: string | URL;
|
||||
alt?: string;
|
||||
secureUrl?: string | URL;
|
||||
type?: string;
|
||||
width?: string | number;
|
||||
height?: string | number;
|
||||
};
|
||||
type ResolvedTwitterSummary = {
|
||||
site: string | null;
|
||||
siteId: string | null;
|
||||
creator: string | null;
|
||||
creatorId: string | null;
|
||||
description: string | null;
|
||||
title: AbsoluteTemplateString;
|
||||
images?: Array<ResolvedTwitterImage>;
|
||||
};
|
||||
type ResolvedTwitterPlayer = ResolvedTwitterSummary & {
|
||||
players: Array<TwitterPlayerDescriptor>;
|
||||
};
|
||||
type ResolvedTwitterApp = ResolvedTwitterSummary & {
|
||||
app: TwitterAppDescriptor;
|
||||
};
|
||||
|
||||
export type ResolvedTwitterMetadata =
|
||||
| ({ card: "summary" } & ResolvedTwitterSummary)
|
||||
| ({ card: "summary_large_image" } & ResolvedTwitterSummary)
|
||||
| ({ card: "player" } & ResolvedTwitterPlayer)
|
||||
| ({ card: "app" } & ResolvedTwitterApp);
|
5
framework/meta/readme.md
Normal file
5
framework/meta/readme.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
the metadata renderer was written in 2022, and is 3700 lines of code. it
|
||||
represents the bulk of the code in the framework, which i think is wrong.
|
||||
|
||||
a bounty goes to rewriting this codebase into one or two files. merging logic is
|
||||
surely not needed, and resolution can happen in the same step as rendering.
|
565
framework/meta/render.ts
Normal file
565
framework/meta/render.ts
Normal file
|
@ -0,0 +1,565 @@
|
|||
import type { Icon, ResolvedMetadata } from "./types";
|
||||
import { escapeHTML as esc } from "./utils";
|
||||
|
||||
function Meta(name: string, content: any) {
|
||||
return `<meta name="${esc(name)}" content="${esc(content)}">`;
|
||||
}
|
||||
|
||||
function MetaProp(name: string, content: any) {
|
||||
return `<meta property="${esc(name)}" content="${esc(content)}">`;
|
||||
}
|
||||
|
||||
function MetaMedia(name: string, content: any, media: string) {
|
||||
return `<meta name="${esc(name)}" content="${esc(content)}" media="${
|
||||
esc(media)
|
||||
}">`;
|
||||
}
|
||||
|
||||
function Link(rel: string, href: any) {
|
||||
return `<link rel="${esc(rel)}" href="${esc(href)}" />`;
|
||||
}
|
||||
|
||||
function LinkMedia(rel: string, href: any, media: string) {
|
||||
return `<link rel="${esc(rel)}" href="${esc(href)}" media="${esc(media)}">`;
|
||||
}
|
||||
|
||||
const resolveUrl = (
|
||||
url: string | URL,
|
||||
) => (typeof url === "string" ? url : url.toString());
|
||||
|
||||
function IconLink(rel: string, icon: Icon) {
|
||||
if (typeof icon === "object" && !(icon instanceof URL)) {
|
||||
const { url, rel: _, ...props } = icon;
|
||||
return `<link rel="${esc(rel)}" href="${esc(resolveUrl(url))}"${
|
||||
Object.keys(props)
|
||||
.map((key) => ` ${key}="${esc(props[key])}"`)
|
||||
.join("")
|
||||
}>`;
|
||||
} else {
|
||||
const href = resolveUrl(icon);
|
||||
return Link(rel, href);
|
||||
}
|
||||
}
|
||||
|
||||
function ExtendMeta(prefix: string, content: any) {
|
||||
if (
|
||||
typeof content === "string" || typeof content === "number" ||
|
||||
content instanceof URL
|
||||
) {
|
||||
return MetaProp(prefix, content);
|
||||
} else {
|
||||
let str = "";
|
||||
for (const [prop, value] of Object.entries(content)) {
|
||||
if (value) {
|
||||
str += MetaProp(
|
||||
prefix === "og:image" && prop === "url"
|
||||
? "og:image"
|
||||
: prefix + ":" + prop,
|
||||
value,
|
||||
);
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
const formatDetectionKeys = [
|
||||
"telephone",
|
||||
"date",
|
||||
"address",
|
||||
"email",
|
||||
"url",
|
||||
] as const;
|
||||
|
||||
export function renderMetadata(meta: ResolvedMetadata): string {
|
||||
var str = "";
|
||||
|
||||
// <BasicMetadata/>
|
||||
if (meta.title?.absolute) str += `<title>${esc(meta.title.absolute)}</title>`;
|
||||
if (meta.description) str += Meta("description", meta.description);
|
||||
if (meta.applicationName) {
|
||||
str += Meta("application-name", meta.applicationName);
|
||||
}
|
||||
if (meta.authors) {
|
||||
for (var author of meta.authors) {
|
||||
if (author.url) str += Link("author", author.url);
|
||||
if (author.name) str += Meta("author", author.name);
|
||||
}
|
||||
}
|
||||
if (meta.manifest) str += Link("manifest", meta.manifest);
|
||||
if (meta.generator) str += Meta("generator", meta.generator);
|
||||
if (meta.referrer) str += Meta("referrer", meta.referrer);
|
||||
if (meta.themeColor) {
|
||||
for (var themeColor of meta.themeColor) {
|
||||
str += !themeColor.media
|
||||
? Meta("theme-color", themeColor.color)
|
||||
: MetaMedia("theme-color", themeColor.color, themeColor.media);
|
||||
}
|
||||
}
|
||||
if (meta.colorScheme) str += Meta("color-scheme", meta.colorScheme);
|
||||
if (meta.viewport) str += Meta("viewport", meta.viewport);
|
||||
if (meta.creator) str += Meta("creator", meta.creator);
|
||||
if (meta.publisher) str += Meta("publisher", meta.publisher);
|
||||
if (meta.robots?.basic) str += Meta("robots", meta.robots.basic);
|
||||
if (meta.robots?.googleBot) str += Meta("googlebot", meta.robots.googleBot);
|
||||
if (meta.abstract) str += Meta("abstract", meta.abstract);
|
||||
if (meta.archives) {
|
||||
for (var archive of meta.archives) {
|
||||
str += Link("archives", archive);
|
||||
}
|
||||
}
|
||||
if (meta.assets) {
|
||||
for (var asset of meta.assets) {
|
||||
str += Link("assets", asset);
|
||||
}
|
||||
}
|
||||
if (meta.bookmarks) {
|
||||
for (var bookmark of meta.bookmarks) {
|
||||
str += Link("bookmarks", bookmark);
|
||||
}
|
||||
}
|
||||
if (meta.category) str += Meta("category", meta.category);
|
||||
if (meta.classification) str += Meta("classification", meta.classification);
|
||||
if (meta.other) {
|
||||
for (var [name, content] of Object.entries(meta.other)) {
|
||||
if (content) {
|
||||
str += Meta(name, Array.isArray(content) ? content.join(",") : content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// <AlternatesMetadata />
|
||||
var alternates = meta.alternates;
|
||||
if (alternates) {
|
||||
if (alternates.canonical) {
|
||||
str += Link("canonical", alternates.canonical.url);
|
||||
}
|
||||
if (alternates.languages) {
|
||||
for (var [locale, urls] of Object.entries(alternates.languages)) {
|
||||
for (var { url, title } of urls) {
|
||||
str += `<link rel="alternate" hreflang="${esc(locale)}" href="${
|
||||
esc(url.toString())
|
||||
}"${title ? ` title="${esc(title)}"` : ""}>`;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (alternates.media) {
|
||||
for (var [media, urls2] of Object.entries(alternates.media)) {
|
||||
if (urls2) {
|
||||
for (var { url, title } of urls2) {
|
||||
str += `<link rel="alternate" media="${esc(media)}" href="${
|
||||
esc(url.toString())
|
||||
}"${title ? ` title="${esc(title)}"` : ""}>`;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (alternates.types) {
|
||||
for (var [type, urls2] of Object.entries(alternates.types)) {
|
||||
if (urls2) {
|
||||
for (var { url, title } of urls2) {
|
||||
str += `<link rel="alternate" type="${esc(type)}" href="${
|
||||
esc(url.toString())
|
||||
}"${title ? ` title="${esc(title)}"` : ""}>`;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// <ItunesMeta />
|
||||
if (meta.itunes) {
|
||||
str += Meta(
|
||||
"apple-itunes-app",
|
||||
`app-id=${meta.itunes.appId}${
|
||||
meta.itunes.appArgument
|
||||
? `, app-argument=${meta.itunes.appArgument}`
|
||||
: ""
|
||||
}`,
|
||||
);
|
||||
}
|
||||
|
||||
// <FormatDetectionMeta />
|
||||
if (meta.formatDetection) {
|
||||
var contentStr = "";
|
||||
for (var key of formatDetectionKeys) {
|
||||
if (key in meta.formatDetection) {
|
||||
if (contentStr) contentStr += ", ";
|
||||
contentStr += `${key}=no`;
|
||||
}
|
||||
}
|
||||
str += Meta("format-detection", contentStr);
|
||||
}
|
||||
|
||||
// <VerificationMeta />
|
||||
if (meta.verification) {
|
||||
if (meta.verification.google) {
|
||||
for (var verificationKey of meta.verification.google) {
|
||||
str += Meta("google-site-verification", verificationKey);
|
||||
}
|
||||
}
|
||||
if (meta.verification.yahoo) {
|
||||
for (var verificationKey of meta.verification.yahoo) {
|
||||
str += Meta("y_key", verificationKey);
|
||||
}
|
||||
}
|
||||
if (meta.verification.yandex) {
|
||||
for (var verificationKey of meta.verification.yandex) {
|
||||
str += Meta("yandex-verification", verificationKey);
|
||||
}
|
||||
}
|
||||
if (meta.verification.me) {
|
||||
for (var verificationKey of meta.verification.me) {
|
||||
str += Meta("me", verificationKey);
|
||||
}
|
||||
}
|
||||
if (meta.verification.other) {
|
||||
for (
|
||||
var [verificationKey2, values] of Object.entries(
|
||||
meta.verification.other,
|
||||
)
|
||||
) {
|
||||
for (var value of values) {
|
||||
str += Meta(verificationKey2, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// <AppleWebAppMeta />
|
||||
if (meta.appleWebApp) {
|
||||
const { capable, title, startupImage, statusBarStyle } = meta.appleWebApp;
|
||||
if (capable) {
|
||||
str += '<meta name="apple-mobile-web-app-capable" content="yes" />';
|
||||
}
|
||||
if (title) str += Meta("apple-mobile-web-app-title", title);
|
||||
if (startupImage) {
|
||||
for (const img of startupImage) {
|
||||
str += !img.media
|
||||
? Link("apple-touch-startup-image", img.url)
|
||||
: LinkMedia("apple-touch-startup-image", img.url, img.media);
|
||||
}
|
||||
}
|
||||
if (statusBarStyle) {
|
||||
str += Meta("apple-mobile-web-app-status-bar-style", statusBarStyle);
|
||||
}
|
||||
}
|
||||
|
||||
// <OpenGraphMetadata />
|
||||
if (meta.openGraph) {
|
||||
const og = meta.openGraph;
|
||||
if (og.determiner) str += MetaProp("og:determiner", og.determiner);
|
||||
if (og.title?.absolute) str += MetaProp("og:title", og.title.absolute);
|
||||
if (og.description) str += MetaProp("og:description", og.description);
|
||||
if (og.url) str += MetaProp("og:url", og.url.toString());
|
||||
if (og.siteName) str += MetaProp("og:site_name", og.siteName);
|
||||
if (og.locale) str += MetaProp("og:locale", og.locale);
|
||||
if (og.countryName) str += MetaProp("og:country_name", og.countryName);
|
||||
if (og.ttl) str += MetaProp("og:ttl", og.ttl);
|
||||
if (og.images) {
|
||||
for (const item of og.images) {
|
||||
str += ExtendMeta("og:image", item);
|
||||
}
|
||||
}
|
||||
if (og.videos) {
|
||||
for (const item of og.videos) {
|
||||
str += ExtendMeta("og:video", item);
|
||||
}
|
||||
}
|
||||
if (og.audio) {
|
||||
for (const item of og.audio) {
|
||||
str += ExtendMeta("og:audio", item);
|
||||
}
|
||||
}
|
||||
if (og.emails) {
|
||||
for (const item of og.emails) {
|
||||
str += ExtendMeta("og:email", item);
|
||||
}
|
||||
}
|
||||
if (og.phoneNumbers) {
|
||||
for (const item of og.phoneNumbers) {
|
||||
str += MetaProp("og:phone_number", item);
|
||||
}
|
||||
}
|
||||
if (og.faxNumbers) {
|
||||
for (const item of og.faxNumbers) {
|
||||
str += MetaProp("og:fax_number", item);
|
||||
}
|
||||
}
|
||||
if (og.alternateLocale) {
|
||||
for (const item of og.alternateLocale) {
|
||||
str += MetaProp("og:locale:alternate", item);
|
||||
}
|
||||
}
|
||||
|
||||
if ("type" in og) {
|
||||
str += MetaProp("og:type", og.type);
|
||||
switch (og.type) {
|
||||
case "website":
|
||||
break;
|
||||
case "article":
|
||||
if (og.publishedTime) {
|
||||
str += MetaProp("article:published_time", og.publishedTime);
|
||||
}
|
||||
if (og.modifiedTime) {
|
||||
str += MetaProp("article:modified_time", og.modifiedTime);
|
||||
}
|
||||
if (og.expirationTime) {
|
||||
str += MetaProp("article:expiration_time", og.expirationTime);
|
||||
}
|
||||
if (og.authors) {
|
||||
for (const item of og.authors) {
|
||||
str += MetaProp("article:author", item);
|
||||
}
|
||||
}
|
||||
if (og.section) str += MetaProp("article:section", og.section);
|
||||
if (og.tags) {
|
||||
for (const item of og.tags) {
|
||||
str += MetaProp("article:tag", item);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "book":
|
||||
if (og.isbn) str += MetaProp("book:isbn", og.isbn);
|
||||
if (og.releaseDate) {
|
||||
str += MetaProp("book:release_date", og.releaseDate);
|
||||
}
|
||||
if (og.authors) {
|
||||
for (const item of og.authors) {
|
||||
str += MetaProp("article:author", item);
|
||||
}
|
||||
}
|
||||
if (og.tags) {
|
||||
for (const item of og.tags) {
|
||||
str += MetaProp("article:tag", item);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "profile":
|
||||
if (og.firstName) str += MetaProp("profile:first_name", og.firstName);
|
||||
if (og.lastName) str += MetaProp("profile:last_name", og.lastName);
|
||||
if (og.username) str += MetaProp("profile:first_name", og.username);
|
||||
if (og.gender) str += MetaProp("profile:first_name", og.gender);
|
||||
break;
|
||||
case "music.song":
|
||||
if (og.duration) str += MetaProp("music:duration", og.duration);
|
||||
if (og.albums) {
|
||||
for (const item of og.albums) {
|
||||
str += ExtendMeta("music:albums", item);
|
||||
}
|
||||
}
|
||||
if (og.musicians) {
|
||||
for (const item of og.musicians) {
|
||||
str += MetaProp("music:musician", item);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "music.album":
|
||||
if (og.songs) {
|
||||
for (const item of og.songs) {
|
||||
str += ExtendMeta("music:song", item);
|
||||
}
|
||||
}
|
||||
if (og.musicians) {
|
||||
for (const item of og.musicians) {
|
||||
str += MetaProp("music:musician", item);
|
||||
}
|
||||
}
|
||||
if (og.releaseDate) {
|
||||
str += MetaProp("music:release_date", og.releaseDate);
|
||||
}
|
||||
break;
|
||||
case "music.playlist":
|
||||
if (og.songs) {
|
||||
for (const item of og.songs) {
|
||||
str += ExtendMeta("music:song", item);
|
||||
}
|
||||
}
|
||||
if (og.creators) {
|
||||
for (const item of og.creators) {
|
||||
str += MetaProp("music:creator", item);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "music.radio_station":
|
||||
if (og.creators) {
|
||||
for (const item of og.creators) {
|
||||
str += MetaProp("music:creator", item);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "video.movie":
|
||||
if (og.actors) {
|
||||
for (const item of og.actors) {
|
||||
str += ExtendMeta("video:actor", item);
|
||||
}
|
||||
}
|
||||
if (og.directors) {
|
||||
for (const item of og.directors) {
|
||||
str += MetaProp("video:director", item);
|
||||
}
|
||||
}
|
||||
if (og.writers) {
|
||||
for (const item of og.writers) {
|
||||
str += MetaProp("video:writer", item);
|
||||
}
|
||||
}
|
||||
if (og.duration) str += MetaProp("video:duration", og.duration);
|
||||
if (og.releaseDate) {
|
||||
str += MetaProp("video:release_date", og.releaseDate);
|
||||
}
|
||||
if (og.tags) {
|
||||
for (const item of og.tags) {
|
||||
str += MetaProp("video:tag", item);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "video.episode":
|
||||
if (og.actors) {
|
||||
for (const item of og.actors) {
|
||||
str += ExtendMeta("video:actor", item);
|
||||
}
|
||||
}
|
||||
if (og.directors) {
|
||||
for (const item of og.directors) {
|
||||
str += MetaProp("video:director", item);
|
||||
}
|
||||
}
|
||||
if (og.writers) {
|
||||
for (const item of og.writers) {
|
||||
str += MetaProp("video:writer", item);
|
||||
}
|
||||
}
|
||||
if (og.duration) str += MetaProp("video:duration", og.duration);
|
||||
if (og.releaseDate) {
|
||||
str += MetaProp("video:release_date", og.releaseDate);
|
||||
}
|
||||
if (og.tags) {
|
||||
for (const item of og.tags) {
|
||||
str += MetaProp("video:tag", item);
|
||||
}
|
||||
}
|
||||
if (og.series) str += MetaProp("video:series", og.series);
|
||||
break;
|
||||
case "video.other":
|
||||
case "video.tv_show":
|
||||
default:
|
||||
throw new Error("Invalid OpenGraph type: " + og.type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// <TwitterMetadata />
|
||||
if (meta.twitter) {
|
||||
const twitter = meta.twitter;
|
||||
|
||||
if (twitter.card) str += Meta("twitter:card", twitter.card);
|
||||
if (twitter.site) str += Meta("twitter:site", twitter.site);
|
||||
if (twitter.siteId) str += Meta("twitter:site:id", twitter.siteId);
|
||||
if (twitter.creator) str += Meta("twitter:creator", twitter.creator);
|
||||
if (twitter.creatorId) str += Meta("twitter:creator:id", twitter.creatorId);
|
||||
if (twitter.title?.absolute) {
|
||||
str += Meta("twitter:title", twitter.title.absolute);
|
||||
}
|
||||
if (twitter.description) {
|
||||
str += Meta("twitter:description", twitter.description);
|
||||
}
|
||||
if (twitter.images) {
|
||||
for (const img of twitter.images) {
|
||||
str += Meta("twitter:image", img.url);
|
||||
if (img.alt) str += Meta("twitter:image:alt", img.alt);
|
||||
}
|
||||
}
|
||||
if (twitter.card === "player") {
|
||||
for (const player of twitter.players) {
|
||||
if (player.playerUrl) str += Meta("twitter:player", player.playerUrl);
|
||||
if (player.streamUrl) {
|
||||
str += Meta("twitter:player:stream", player.streamUrl);
|
||||
}
|
||||
if (player.width) str += Meta("twitter:player:width", player.width);
|
||||
if (player.height) str += Meta("twitter:player:height", player.height);
|
||||
}
|
||||
}
|
||||
if (twitter.card === "app") {
|
||||
for (const type of ["iphone", "ipad", "googleplay"]) {
|
||||
if (twitter.app.id[type]) {
|
||||
str += Meta(`twitter:app:name:${type}`, twitter.app.name);
|
||||
str += Meta(`twitter:app:id:${type}`, twitter.app.id[type]);
|
||||
}
|
||||
if (twitter.app.url?.[type]) {
|
||||
str += Meta(`twitter:app:url:${type}`, twitter.app.url[type]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// <AppLinksMeta />
|
||||
if (meta.appLinks) {
|
||||
if (meta.appLinks.ios) {
|
||||
for (var item of meta.appLinks.ios) {
|
||||
str += ExtendMeta("al:ios", item);
|
||||
}
|
||||
}
|
||||
if (meta.appLinks.iphone) {
|
||||
for (var item of meta.appLinks.iphone) {
|
||||
str += ExtendMeta("al:iphone", item);
|
||||
}
|
||||
}
|
||||
if (meta.appLinks.ipad) {
|
||||
for (var item of meta.appLinks.ipad) {
|
||||
str += ExtendMeta("al:ipad", item);
|
||||
}
|
||||
}
|
||||
if (meta.appLinks.android) {
|
||||
for (var item2 of meta.appLinks.android) {
|
||||
str += ExtendMeta("al:android", item2);
|
||||
}
|
||||
}
|
||||
if (meta.appLinks.windows_phone) {
|
||||
for (var item3 of meta.appLinks.windows_phone) {
|
||||
str += ExtendMeta("al:windows_phone", item3);
|
||||
}
|
||||
}
|
||||
if (meta.appLinks.windows) {
|
||||
for (var item3 of meta.appLinks.windows) {
|
||||
str += ExtendMeta("al:windows", item3);
|
||||
}
|
||||
}
|
||||
if (meta.appLinks.windows_universal) {
|
||||
for (var item4 of meta.appLinks.windows_universal) {
|
||||
str += ExtendMeta("al:windows_universal", item4);
|
||||
}
|
||||
}
|
||||
if (meta.appLinks.web) {
|
||||
for (const item of meta.appLinks.web) {
|
||||
str += ExtendMeta("al:web", item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// <IconsMetadata />
|
||||
if (meta.icons) {
|
||||
if (meta.icons.shortcut) {
|
||||
for (var icon of meta.icons.shortcut) {
|
||||
str += IconLink("shortcut icon", icon);
|
||||
}
|
||||
}
|
||||
if (meta.icons.icon) {
|
||||
for (var icon of meta.icons.icon) {
|
||||
str += IconLink("icon", icon);
|
||||
}
|
||||
}
|
||||
if (meta.icons.apple) {
|
||||
for (var icon of meta.icons.apple) {
|
||||
str += IconLink("apple-touch-icon", icon);
|
||||
}
|
||||
}
|
||||
if (meta.icons.other) {
|
||||
for (var icon of meta.icons.other) {
|
||||
str += IconLink(icon.rel ?? "icon", icon);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
57
framework/meta/types.ts
Normal file
57
framework/meta/types.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
export type {
|
||||
AlternateURLs,
|
||||
ResolvedAlternateURLs,
|
||||
} from "./nextjs/types/alternative-urls-types";
|
||||
export type {
|
||||
AppleImage,
|
||||
AppleImageDescriptor,
|
||||
AppleWebApp,
|
||||
AppLinks,
|
||||
AppLinksAndroid,
|
||||
AppLinksApple,
|
||||
AppLinksWeb,
|
||||
AppLinksWindows,
|
||||
FormatDetection,
|
||||
ItunesApp,
|
||||
ResolvedAppleWebApp,
|
||||
ResolvedAppLinks,
|
||||
Viewport,
|
||||
} from "./nextjs/types/extra-types";
|
||||
export type {
|
||||
Metadata,
|
||||
ResolvedMetadata,
|
||||
ResolvingMetadata,
|
||||
} from "./nextjs/types/metadata-interface";
|
||||
export type {
|
||||
AbsoluteString,
|
||||
AbsoluteTemplateString,
|
||||
Author,
|
||||
ColorSchemeEnum,
|
||||
DefaultTemplateString,
|
||||
Icon,
|
||||
IconDescriptor,
|
||||
Icons,
|
||||
IconURL,
|
||||
ReferrerEnum,
|
||||
ResolvedIcons,
|
||||
ResolvedRobots,
|
||||
ResolvedVerification,
|
||||
Robots,
|
||||
TemplateString,
|
||||
ThemeColorDescriptor,
|
||||
Verification,
|
||||
} from "./nextjs/types/metadata-types";
|
||||
export type {
|
||||
OpenGraph,
|
||||
OpenGraphType,
|
||||
ResolvedOpenGraph,
|
||||
} from "./nextjs/types/opengraph-types";
|
||||
export type {
|
||||
FieldResolver,
|
||||
FieldResolverWithMetadataBase,
|
||||
} from "./nextjs/types/resolvers";
|
||||
export type {
|
||||
ResolvedTwitterMetadata,
|
||||
Twitter,
|
||||
TwitterAppDescriptor,
|
||||
} from "./nextjs/types/twitter-types";
|
13
framework/meta/utils.ts
Normal file
13
framework/meta/utils.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
// Extracted from @paperdave/utils/string
|
||||
declare var Bun: any;
|
||||
|
||||
export const escapeHTML: (string: string) => string = typeof Bun !== "undefined"
|
||||
? Bun.escapeHTML
|
||||
: (string: string) => {
|
||||
return string
|
||||
.replaceAll('"', """)
|
||||
.replaceAll("&", "&")
|
||||
.replaceAll("'", "'")
|
||||
.replaceAll("<", "<")
|
||||
.replaceAll(">", ">");
|
||||
};
|
30
framework/mime.ts
Normal file
30
framework/mime.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
const db = new Map(
|
||||
fs.readFileSync(path.join(import.meta.dirname, "mime.txt"), "utf8")
|
||||
.split("\n").filter(Boolean).map((line) =>
|
||||
line.split(/\s+/) as [string, string]
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* Accepts:
|
||||
* - Full file path
|
||||
* - Extension (with or without dot)
|
||||
*/
|
||||
export function contentTypeFor(file: string) {
|
||||
if (file.includes("/") || file.includes("\\")) {
|
||||
// Some file names are special cased.
|
||||
switch (path.basename(file)) {
|
||||
case "rss.xml":
|
||||
return "application/rss+xml";
|
||||
}
|
||||
|
||||
file = path.extname(file);
|
||||
}
|
||||
const dot = file.indexOf(".");
|
||||
if (dot === -1) file = "." + file;
|
||||
else if (dot > 0) file = file.slice(dot);
|
||||
return db.get(file) ?? "application/octet-stream";
|
||||
}
|
||||
|
||||
import * as fs from "./fs.ts";
|
||||
import * as path from "node:path";
|
8
framework/mime.txt
Normal file
8
framework/mime.txt
Normal file
|
@ -0,0 +1,8 @@
|
|||
.css text/css
|
||||
.html text/html; charset=utf8
|
||||
.jpeg image/jpeg
|
||||
.jpg image/jpeg
|
||||
.js text/javascript
|
||||
.json application/json
|
||||
.png image/png
|
||||
.txt text/plain
|
206
framework/queue.ts
Normal file
206
framework/queue.ts
Normal file
|
@ -0,0 +1,206 @@
|
|||
import { Progress } from "@paperclover/console/Progress";
|
||||
import { Spinner } from "@paperclover/console/Spinner";
|
||||
import * as path from "node:path";
|
||||
import process from "node:process";
|
||||
|
||||
interface QueueOptions<T, R> {
|
||||
name: string;
|
||||
fn: (item: T, spin: Spinner) => Promise<R>;
|
||||
getItemText?: (item: T) => string;
|
||||
maxJobs?: number;
|
||||
passive?: boolean;
|
||||
}
|
||||
|
||||
// Process multiple items in parallel, queue up as many.
|
||||
export class Queue<T, R> {
|
||||
#name: string;
|
||||
#fn: (item: T, spin: Spinner) => Promise<R>;
|
||||
#maxJobs: number;
|
||||
#getItemText: (item: T) => string;
|
||||
#passive: boolean;
|
||||
|
||||
#active: Spinner[] = [];
|
||||
#queue: Array<[T] | [T, (result: R) => void, (err: unknown) => void]> = [];
|
||||
|
||||
#cachedProgress: Progress<{ active: Spinner[] }> | null = null;
|
||||
#done: number = 0;
|
||||
#total: number = 0;
|
||||
#onComplete: (() => void) | null = null;
|
||||
#estimate: number | null = null;
|
||||
#errors: unknown[] = [];
|
||||
|
||||
constructor(options: QueueOptions<T, R>) {
|
||||
this.#name = options.name;
|
||||
this.#fn = options.fn;
|
||||
this.#maxJobs = options.maxJobs ?? 5;
|
||||
this.#getItemText = options.getItemText ?? defaultGetItemText;
|
||||
this.#passive = options.passive ?? false;
|
||||
}
|
||||
|
||||
get bar() {
|
||||
const cached = this.#cachedProgress;
|
||||
if (!cached) {
|
||||
const bar = this.#cachedProgress = new Progress({
|
||||
spinner: null,
|
||||
text: ({ active }) => {
|
||||
const now = performance.now();
|
||||
let text = `[${this.#done}/${this.#total}] ${this.#name}`;
|
||||
let n = 0;
|
||||
for (const item of active) {
|
||||
let itemText = "- " + item.format(now);
|
||||
text += `\n` +
|
||||
itemText.slice(0, Math.max(0, process.stdout.columns - 1));
|
||||
if (n > 10) {
|
||||
text += `\n ... + ${active.length - n} more`;
|
||||
break;
|
||||
}
|
||||
n++;
|
||||
}
|
||||
return text;
|
||||
},
|
||||
props: {
|
||||
active: [] as Spinner[],
|
||||
},
|
||||
});
|
||||
bar.value = 0;
|
||||
return bar;
|
||||
}
|
||||
return cached;
|
||||
}
|
||||
|
||||
add(args: T) {
|
||||
this.#total += 1;
|
||||
this.updateTotal();
|
||||
if (this.#active.length > this.#maxJobs) {
|
||||
const { promise, resolve, reject } = Promise.withResolvers<R>();
|
||||
this.#queue.push([args, resolve, reject]);
|
||||
return promise;
|
||||
}
|
||||
return this.#run(args);
|
||||
}
|
||||
|
||||
addMany(items: T[]) {
|
||||
this.#total += items.length;
|
||||
this.updateTotal();
|
||||
|
||||
const runNowCount = this.#maxJobs - this.#active.length;
|
||||
const runNow = items.slice(0, runNowCount);
|
||||
const runLater = items.slice(runNowCount);
|
||||
this.#queue.push(...runLater.reverse().map<[T]>((x) => [x]));
|
||||
runNow.map((item) => this.#run(item).catch(() => {}));
|
||||
}
|
||||
|
||||
async #run(args: T): Promise<R> {
|
||||
const bar = this.bar;
|
||||
const itemText = this.#getItemText(args);
|
||||
const spinner = new Spinner(itemText);
|
||||
spinner.stop();
|
||||
const active = this.#active;
|
||||
try {
|
||||
active.unshift(spinner);
|
||||
bar.props = { active };
|
||||
const result = await this.#fn(args, spinner);
|
||||
this.#done++;
|
||||
return result;
|
||||
} catch (err) {
|
||||
if (err && typeof err === "object") {
|
||||
(err as any).job = itemText;
|
||||
}
|
||||
this.#errors.push(err);
|
||||
throw err;
|
||||
} finally {
|
||||
active.splice(active.indexOf(spinner), 1);
|
||||
bar.props = { active };
|
||||
bar.value = this.#done;
|
||||
|
||||
// Process next item
|
||||
const next = this.#queue.shift();
|
||||
if (next) {
|
||||
const args = next[0];
|
||||
this.#run(args)
|
||||
.then((result) => next[1]?.(result))
|
||||
.catch((err) => next[2]?.(err));
|
||||
} else if (this.#active.length === 0) {
|
||||
if (this.#passive) {
|
||||
this.bar.stop();
|
||||
this.#cachedProgress = null;
|
||||
}
|
||||
this.#onComplete?.();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
updateTotal() {
|
||||
const bar = this.bar;
|
||||
bar.total = Math.max(this.#total, this.#estimate ?? 0);
|
||||
}
|
||||
|
||||
set estimate(e: number) {
|
||||
this.#estimate = e;
|
||||
if (this.#cachedProgress) {
|
||||
this.updateTotal();
|
||||
}
|
||||
}
|
||||
|
||||
async done(o: { method: "success" | "stop" }) {
|
||||
if (this.#active.length === 0) {
|
||||
this.#end(o);
|
||||
return;
|
||||
}
|
||||
|
||||
const { promise, resolve } = Promise.withResolvers<void>();
|
||||
this.#onComplete = resolve;
|
||||
await promise;
|
||||
this.#end(o);
|
||||
}
|
||||
|
||||
#end(
|
||||
{ method = this.#passive ? "stop" : "success" }: {
|
||||
method: "success" | "stop";
|
||||
},
|
||||
) {
|
||||
const bar = this.#cachedProgress;
|
||||
if (this.#errors.length > 0) {
|
||||
if (bar) bar.stop();
|
||||
throw new AggregateError(
|
||||
this.#errors,
|
||||
this.#errors.length + " jobs failed in '" + this.#name + "'",
|
||||
);
|
||||
}
|
||||
|
||||
if (bar) bar[method]();
|
||||
}
|
||||
}
|
||||
|
||||
const cwd = process.cwd();
|
||||
function defaultGetItemText(item: unknown) {
|
||||
let itemText = "";
|
||||
if (typeof item === "string") {
|
||||
itemText = item;
|
||||
} else if (typeof item === "object" && item !== null) {
|
||||
const { path, label, id } = item as any;
|
||||
itemText = label ?? path ?? id ?? JSON.stringify(item);
|
||||
} else {
|
||||
itemText = JSON.stringify(item);
|
||||
}
|
||||
|
||||
if (itemText.startsWith(cwd)) {
|
||||
itemText = path.relative(cwd, itemText);
|
||||
}
|
||||
return itemText;
|
||||
}
|
||||
|
||||
export class OnceMap<T> {
|
||||
private ongoing = new Map<string, Promise<T>>();
|
||||
|
||||
get(key: string, compute: () => Promise<T>) {
|
||||
if (this.ongoing.has(key)) {
|
||||
return this.ongoing.get(key)!;
|
||||
}
|
||||
|
||||
const result = compute();
|
||||
this.ongoing.set(key, result);
|
||||
result.finally(() => this.ongoing.delete(key));
|
||||
return result;
|
||||
}
|
||||
}
|
49
framework/sitegen-lib.ts
Normal file
49
framework/sitegen-lib.ts
Normal file
|
@ -0,0 +1,49 @@
|
|||
// Import this file with 'import * as sg from "#sitegen";'
|
||||
export type ScriptId = string;
|
||||
|
||||
export interface SitegenRender {
|
||||
scripts: Set<ScriptId>;
|
||||
}
|
||||
|
||||
export function initRender(): SitegenRender {
|
||||
return {
|
||||
scripts: new Set(),
|
||||
};
|
||||
}
|
||||
|
||||
export function getRender() {
|
||||
return ssr.getUserData<SitegenRender>("sitegen", () => {
|
||||
throw new Error(
|
||||
"This function can only be used in a page (static or view)",
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/** Add a client-side script to the page. */
|
||||
export function addScript(id: ScriptId) {
|
||||
const srcFile: string = util.getCallSites()
|
||||
.find((site) => !site.scriptName.startsWith(import.meta.dirname))!
|
||||
.scriptName;
|
||||
const filePath = hot.resolveFrom(srcFile, id);
|
||||
if (
|
||||
!filePath.endsWith(".client.ts") &&
|
||||
!filePath.endsWith(".client.tsx")
|
||||
) {
|
||||
throw new Error("addScript must be a .client.ts or .client.tsx");
|
||||
}
|
||||
getRender().scripts.add(filePath);
|
||||
}
|
||||
|
||||
export function Script({ src }: { src: ScriptId }) {
|
||||
if (!src) throw new Error("Missing 'src' attribute");
|
||||
addScript(src);
|
||||
return null;
|
||||
}
|
||||
|
||||
export interface Section {
|
||||
root: string;
|
||||
}
|
||||
|
||||
import * as ssr from "./engine/ssr.ts";
|
||||
import * as util from "node:util";
|
||||
import * as hot from "./hot.ts";
|
11
framework/sitegen.tsx
Normal file
11
framework/sitegen.tsx
Normal file
|
@ -0,0 +1,11 @@
|
|||
// default
|
||||
class Hub {
|
||||
getCode() {}
|
||||
getScope() {}
|
||||
addHelper() {
|
||||
throw new Error("Helpers are not supported by the default hub.");
|
||||
}
|
||||
buildError(node, msg, Error = TypeError) {
|
||||
return new Error(msg);
|
||||
}
|
||||
}
|
101
framework/sqlite.ts
Normal file
101
framework/sqlite.ts
Normal file
|
@ -0,0 +1,101 @@
|
|||
// Guard against reloads and bundler duplication.
|
||||
// @ts-ignore
|
||||
const map = globalThis[Symbol.for("clover.db")] ??= new Map<
|
||||
string,
|
||||
WrappedDatabase
|
||||
>();
|
||||
|
||||
export function getDb(file: string) {
|
||||
let db: WrappedDatabase | null = map.get(file);
|
||||
if (db) return db;
|
||||
const fileWithExt = file.includes(".") ? file : file + ".sqlite";
|
||||
db = new WrappedDatabase(
|
||||
new DatabaseSync(path.join(".clover/", fileWithExt)),
|
||||
);
|
||||
map.set(file, db);
|
||||
return db;
|
||||
}
|
||||
|
||||
export class WrappedDatabase {
|
||||
node: DatabaseSync;
|
||||
stmtTableMigrate: WeakRef<StatementSync> | null = null;
|
||||
|
||||
constructor(node: DatabaseSync) {
|
||||
this.node = node;
|
||||
this.node.exec(`
|
||||
create table if not exists clover_migrations (
|
||||
key text not null primary key,
|
||||
version integer not null
|
||||
);
|
||||
`);
|
||||
}
|
||||
|
||||
// TODO: add migration support
|
||||
// the idea is you keep `schema` as the new schema but can add
|
||||
// migrations to the mix really easily.
|
||||
table(name: string, schema: string) {
|
||||
let s = this.stmtTableMigrate?.deref();
|
||||
s ?? (this.stmtTableMigrate = new WeakRef(
|
||||
s = this.node.prepare(`
|
||||
insert or ignore into clover_migrations
|
||||
(key, version) values (?, ?);
|
||||
`),
|
||||
));
|
||||
const { changes, lastInsertRowid } = s.run(name, 1);
|
||||
console.log(changes, lastInsertRowid);
|
||||
if (changes === 1) {
|
||||
this.node.exec(schema);
|
||||
}
|
||||
}
|
||||
|
||||
prepare<Args extends unknown[] = [], Result = unknown>(
|
||||
query: string,
|
||||
): Stmt<Args, Result> {
|
||||
return new Stmt(this.node.prepare(query));
|
||||
}
|
||||
}
|
||||
|
||||
export class Stmt<Args extends unknown[] = unknown[], Row = unknown> {
|
||||
#node: StatementSync;
|
||||
#class: any | null = null;
|
||||
constructor(node: StatementSync) {
|
||||
this.#node = node;
|
||||
}
|
||||
|
||||
/** Get one row */
|
||||
get(...args: Args): Row | null {
|
||||
const item = this.#node.get(...args as any) as Row;
|
||||
if (!item) return null;
|
||||
const C = this.#class;
|
||||
if (C) Object.setPrototypeOf(item, C.prototype);
|
||||
return item;
|
||||
}
|
||||
getNonNull(...args: Args) {
|
||||
const item = this.get(...args);
|
||||
if (!item) throw new Error("Query returned no result");
|
||||
return item;
|
||||
}
|
||||
iter(...args: Args): Iterator<Row> {
|
||||
return this.array(...args)[Symbol.iterator]();
|
||||
}
|
||||
/** Get all rows */
|
||||
array(...args: Args): Row[] {
|
||||
const array = this.#node.all(...args as any) as Row[];
|
||||
const C = this.#class;
|
||||
if (C) array.forEach((item) => Object.setPrototypeOf(item, C.prototype));
|
||||
return array;
|
||||
}
|
||||
/** Return the number of changes / row ID */
|
||||
run(...args: Args) {
|
||||
return this.#node.run(...args as any);
|
||||
}
|
||||
|
||||
as<R>(Class: { new (): R }): Stmt<Args, R> {
|
||||
this.#class = Class;
|
||||
return this as any;
|
||||
}
|
||||
}
|
||||
|
||||
import { DatabaseSync, StatementSync } from "node:sqlite";
|
||||
import * as fs from "./fs.ts";
|
||||
import * as path from "node:path";
|
0
framework/typecheck.ts
Normal file
0
framework/typecheck.ts
Normal file
81
package-lock.json
generated
Normal file
81
package-lock.json
generated
Normal file
|
@ -0,0 +1,81 @@
|
|||
// DEFAULT_OPTIONS
|
||||
[object Object]
|
||||
|
||||
// strip
|
||||
function strip(jsonString, options = DEFAULT_OPTIONS) {
|
||||
if (typeof jsonString !== "string") {
|
||||
throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof jsonString}\``);
|
||||
}
|
||||
const { trailingCommas = false, whitespace = true } = options;
|
||||
const _strip = whitespace ? stripWithWhitespace : stripWithoutWhitespace;
|
||||
let isInsideString = false;
|
||||
let isInsideComment = false;
|
||||
let offset = 0;
|
||||
let buffer = "";
|
||||
let result = "";
|
||||
let commaIndex = -1;
|
||||
for (let i = 0; i < jsonString.length; i++) {
|
||||
const currentCharacter = jsonString[i];
|
||||
const nextCharacter = jsonString[i + 1];
|
||||
if (!currentCharacter) {
|
||||
continue;
|
||||
}
|
||||
if (!isInsideComment && currentCharacter === '"') {
|
||||
const escaped = isEscaped(jsonString, i);
|
||||
if (!escaped) {
|
||||
isInsideString = !isInsideString;
|
||||
}
|
||||
}
|
||||
if (isInsideString) {
|
||||
continue;
|
||||
}
|
||||
if (!isInsideComment && currentCharacter + nextCharacter === "//") {
|
||||
buffer += jsonString.slice(offset, i);
|
||||
offset = i;
|
||||
isInsideComment = singleComment;
|
||||
i++;
|
||||
} else if (isInsideComment === singleComment && currentCharacter + nextCharacter === "\r\n") {
|
||||
i++;
|
||||
isInsideComment = false;
|
||||
buffer += _strip(jsonString, offset, i);
|
||||
offset = i;
|
||||
continue;
|
||||
} else if (isInsideComment === singleComment && currentCharacter === "\n") {
|
||||
isInsideComment = false;
|
||||
buffer += _strip(jsonString, offset, i);
|
||||
offset = i;
|
||||
} else if (!isInsideComment && currentCharacter + nextCharacter === "/*") {
|
||||
buffer += jsonString.slice(offset, i);
|
||||
offset = i;
|
||||
isInsideComment = multiComment;
|
||||
i++;
|
||||
continue;
|
||||
} else if (isInsideComment === multiComment && currentCharacter + nextCharacter === "*/") {
|
||||
i++;
|
||||
isInsideComment = false;
|
||||
buffer += _strip(jsonString, offset, i + 1);
|
||||
offset = i + 1;
|
||||
continue;
|
||||
} else if (trailingCommas && !isInsideComment) {
|
||||
if (commaIndex !== -1) {
|
||||
if (currentCharacter === "}" || currentCharacter === "]") {
|
||||
buffer += jsonString.slice(offset, i);
|
||||
result += _strip(buffer, 0, 1) + buffer.slice(1);
|
||||
buffer = "";
|
||||
offset = i;
|
||||
commaIndex = -1;
|
||||
} else if (currentCharacter !== " " && currentCharacter !== " " && currentCharacter !== "\r" && currentCharacter !== "\n") {
|
||||
buffer += jsonString.slice(offset, i);
|
||||
offset = i;
|
||||
commaIndex = -1;
|
||||
}
|
||||
} else if (currentCharacter === ",") {
|
||||
result += buffer + jsonString.slice(offset, i);
|
||||
buffer = "";
|
||||
offset = i;
|
||||
commaIndex = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result + buffer + (isInsideComment ? _strip(jsonString.slice(offset)) : jsonString.slice(offset));
|
||||
}
|
25
package.json
Normal file
25
package.json
Normal file
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@hono/node-server": "^1.14.3",
|
||||
"@mdx-js/mdx": "^3.1.0",
|
||||
"@paperclover/console": "git+https://git.paperclover.net/clo/console.git",
|
||||
"esbuild": "^0.25.5",
|
||||
"hls.js": "^1.6.5",
|
||||
"hono": "^4.7.11",
|
||||
"marko": "^6.0.20"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.15.29",
|
||||
"typescript": "^5.8.3"
|
||||
},
|
||||
"imports": {
|
||||
"#sitegen": "./framework/sitegen-lib.ts",
|
||||
"#sqlite": "./framework/sqlite.ts",
|
||||
"#ssr": "./framework/engine/ssr.ts",
|
||||
"#ssr/jsx-dev-runtime": "./framework/engine/jsx-runtime.ts",
|
||||
"#ssr/jsx-runtime": "./framework/engine/jsx-runtime.ts",
|
||||
"#ssr/marko": "./framework/engine/marko-runtime.ts"
|
||||
}
|
||||
}
|
11
repl.js
Normal file
11
repl.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
// default
|
||||
(node, parent, grandparent) => {
|
||||
switch (parent.type) {
|
||||
case "MarkoTag":
|
||||
return parent.var !== node;
|
||||
case "MarkoTagBody":
|
||||
return false;
|
||||
default:
|
||||
return originalIsReferenced(node, parent, grandparent);
|
||||
}
|
||||
}
|
38
run.js
Normal file
38
run.js
Normal file
|
@ -0,0 +1,38 @@
|
|||
// This file allows using Node.js in combination with
|
||||
// available plugins. Usage: "node run <script>"
|
||||
import * as path from "node:path";
|
||||
import process from "node:process";
|
||||
|
||||
// Disable experimental warnings (Type Stripping, etc)
|
||||
{
|
||||
const { emit: originalEmit } = process;
|
||||
const warnings = ["ExperimentalWarning"];
|
||||
process.emit = function (event, error) {
|
||||
return event === "warning" && warnings.includes(error.name)
|
||||
? false
|
||||
: originalEmit.apply(process, arguments);
|
||||
};
|
||||
}
|
||||
|
||||
// Init hooks
|
||||
const hot = await import("./framework/hot.ts");
|
||||
|
||||
const console = hot.load("@paperclover/console");
|
||||
globalThis.console.log = console.info;
|
||||
globalThis.console.info = console.info;
|
||||
globalThis.console.warn = console.warn;
|
||||
globalThis.console.error = console.error;
|
||||
globalThis.console.debug = console.scoped("dbg");
|
||||
|
||||
// Load with hooks
|
||||
if (process.argv[1].startsWith(import.meta.filename.slice(0, -".js".length))) {
|
||||
if (process.argv.length == 2) {
|
||||
console.error("usage: node run <script> [...args]");
|
||||
process.exit(1);
|
||||
}
|
||||
const file = path.resolve(process.argv[2]);
|
||||
process.argv = [process.argv[0], ...process.argv.slice(2)];
|
||||
hot.load(file).main?.();
|
||||
}
|
||||
|
||||
export { hot };
|
0
src/components/Video.tsx
Normal file
0
src/components/Video.tsx
Normal file
29
src/components/video.client.ts
Normal file
29
src/components/video.client.ts
Normal file
|
@ -0,0 +1,29 @@
|
|||
import type Hls from "hls.js";
|
||||
|
||||
let cachedHls: null | Promise<typeof Hls | false> | typeof Hls | false = null;
|
||||
|
||||
const loadHls = () =>
|
||||
cachedHls ?? (cachedHls = (
|
||||
// @ts-ignore
|
||||
+"HLS POLYFILL", import("hls.js")
|
||||
).then((m, y = m.default) => cachedHls = y.isSupported() && y));
|
||||
|
||||
const createVideoPlayer = async (videoElement: HTMLVideoElement) => {
|
||||
const sources = videoElement.querySelectorAll("source");
|
||||
for (let i = 0; i < sources.length; i++) {
|
||||
const { src, type } = sources[i];
|
||||
if (videoElement.canPlayType(type)) {
|
||||
videoElement.src = src;
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === "application/x-mpegURL" && await loadHls()) {
|
||||
const hls = new (cachedHls as typeof Hls)();
|
||||
hls.loadSource(src);
|
||||
hls.attachMedia(videoElement);
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
document.querySelectorAll("video").forEach(createVideoPlayer);
|
29
src/file-viewer/backend.tsx
Normal file
29
src/file-viewer/backend.tsx
Normal file
|
@ -0,0 +1,29 @@
|
|||
// default
|
||||
function transpileNamespace(path, allowNamespaces) {
|
||||
if (path.node.declare || path.node.id.type === "StringLiteral") {
|
||||
path.remove();
|
||||
return;
|
||||
}
|
||||
if (!allowNamespaces) {
|
||||
throw path.get("id").buildCodeFrameError("Namespace not marked type-only declare." + " Non-declarative namespaces are only supported experimentally in Babel." + " To enable and review caveats see:" + " https://babeljs.io/docs/en/babel-plugin-transform-typescript");
|
||||
}
|
||||
const name = getFirstIdentifier(path.node.id).name;
|
||||
const value = handleNested(path, path.node);
|
||||
if (value === null) {
|
||||
const program = path.findParent(p => p.isProgram());
|
||||
(0, _globalTypes.registerGlobalType)(program.scope, name);
|
||||
path.remove();
|
||||
} else if (path.scope.hasOwnBinding(name)) {
|
||||
path.replaceWith(value);
|
||||
} else {
|
||||
path.scope.registerDeclaration(path.replaceWithMultiple([getDeclaration(name), value])[0]);
|
||||
}
|
||||
}
|
||||
|
||||
// getFirstIdentifier
|
||||
function getFirstIdentifier(node) {
|
||||
if (_core.types.isIdentifier(node)) {
|
||||
return node;
|
||||
}
|
||||
return getFirstIdentifier(node.left);
|
||||
}
|
36
src/file-viewer/cache.ts
Normal file
36
src/file-viewer/cache.ts
Normal file
|
@ -0,0 +1,36 @@
|
|||
// beginHiddenCallStack
|
||||
function beginHiddenCallStack(fn) {
|
||||
if (!SUPPORTED) return fn;
|
||||
return Object.defineProperty(function (...args) {
|
||||
setupPrepareStackTrace();
|
||||
return fn(...args);
|
||||
}, "name", {
|
||||
value: STOP_HIDING
|
||||
});
|
||||
}
|
||||
|
||||
// endHiddenCallStack
|
||||
function endHiddenCallStack(fn) {
|
||||
if (!SUPPORTED) return fn;
|
||||
return Object.defineProperty(function (...args) {
|
||||
return fn(...args);
|
||||
}, "name", {
|
||||
value: START_HIDING
|
||||
});
|
||||
}
|
||||
|
||||
// expectedError
|
||||
function expectedError(error) {
|
||||
if (!SUPPORTED) return;
|
||||
expectedErrors.add(error);
|
||||
return error;
|
||||
}
|
||||
|
||||
// injectVirtualStackFrame
|
||||
function injectVirtualStackFrame(error, filename) {
|
||||
if (!SUPPORTED) return;
|
||||
let frames = virtualFrames.get(error);
|
||||
if (!frames) virtualFrames.set(error, frames = []);
|
||||
frames.push(CallSite(filename));
|
||||
return error;
|
||||
}
|
91
src/file-viewer/cert.pem
Normal file
91
src/file-viewer/cert.pem
Normal file
|
@ -0,0 +1,91 @@
|
|||
// default
|
||||
function* loadPrivatePartialConfig(inputOpts) {
|
||||
if (inputOpts != null && (typeof inputOpts !== "object" || Array.isArray(inputOpts))) {
|
||||
throw new Error("Babel options must be an object, null, or undefined");
|
||||
}
|
||||
const args = inputOpts ? (0, _options.validate)("arguments", inputOpts) : {};
|
||||
const {
|
||||
envName = (0, _environment.getEnv)(),
|
||||
cwd = ".",
|
||||
root: rootDir = ".",
|
||||
rootMode = "root",
|
||||
caller,
|
||||
cloneInputAst = true
|
||||
} = args;
|
||||
const absoluteCwd = _path().resolve(cwd);
|
||||
const absoluteRootDir = resolveRootMode(_path().resolve(absoluteCwd, rootDir), rootMode);
|
||||
const filename = typeof args.filename === "string" ? _path().resolve(cwd, args.filename) : undefined;
|
||||
const showConfigPath = yield* (0, _index.resolveShowConfigPath)(absoluteCwd);
|
||||
const context = {
|
||||
filename,
|
||||
cwd: absoluteCwd,
|
||||
root: absoluteRootDir,
|
||||
envName,
|
||||
caller,
|
||||
showConfig: showConfigPath === filename
|
||||
};
|
||||
const configChain = yield* (0, _configChain.buildRootChain)(args, context);
|
||||
if (!configChain) return null;
|
||||
const merged = {
|
||||
assumptions: {}
|
||||
};
|
||||
configChain.options.forEach(opts => {
|
||||
(0, _util.mergeOptions)(merged, opts);
|
||||
});
|
||||
const options = Object.assign({}, merged, {
|
||||
targets: (0, _resolveTargets.resolveTargets)(merged, absoluteRootDir),
|
||||
cloneInputAst,
|
||||
babelrc: false,
|
||||
configFile: false,
|
||||
browserslistConfigFile: false,
|
||||
passPerPreset: false,
|
||||
envName: context.envName,
|
||||
cwd: context.cwd,
|
||||
root: context.root,
|
||||
rootMode: "root",
|
||||
filename: typeof context.filename === "string" ? context.filename : undefined,
|
||||
plugins: configChain.plugins.map(descriptor => (0, _item.createItemFromDescriptor)(descriptor)),
|
||||
presets: configChain.presets.map(descriptor => (0, _item.createItemFromDescriptor)(descriptor))
|
||||
});
|
||||
return {
|
||||
options,
|
||||
context,
|
||||
fileHandling: configChain.fileHandling,
|
||||
ignore: configChain.ignore,
|
||||
babelrc: configChain.babelrc,
|
||||
config: configChain.config,
|
||||
files: configChain.files
|
||||
};
|
||||
}
|
||||
|
||||
// loadPartialConfig
|
||||
function* loadPartialConfig(opts) {
|
||||
let showIgnoredFiles = false;
|
||||
if (typeof opts === "object" && opts !== null && !Array.isArray(opts)) {
|
||||
var _opts = opts;
|
||||
({
|
||||
showIgnoredFiles
|
||||
} = _opts);
|
||||
opts = _objectWithoutPropertiesLoose(_opts, _excluded);
|
||||
_opts;
|
||||
}
|
||||
const result = yield* loadPrivatePartialConfig(opts);
|
||||
if (!result) return null;
|
||||
const {
|
||||
options,
|
||||
babelrc,
|
||||
ignore,
|
||||
config,
|
||||
fileHandling,
|
||||
files
|
||||
} = result;
|
||||
if (fileHandling === "ignored" && !showIgnoredFiles) {
|
||||
return null;
|
||||
}
|
||||
(options.plugins || []).forEach(item => {
|
||||
if (item.value instanceof _plugin.default) {
|
||||
throw new Error("Passing cached plugin instances is not supported in " + "babel.loadPartialConfig()");
|
||||
}
|
||||
});
|
||||
return new PartialConfig(options, babelrc ? babelrc.filepath : undefined, ignore ? ignore.filepath : undefined, config ? config.filepath : undefined, fileHandling, files);
|
||||
}
|
291
src/file-viewer/cotyledon.tsx
Normal file
291
src/file-viewer/cotyledon.tsx
Normal file
|
@ -0,0 +1,291 @@
|
|||
export function Speedbump() {
|
||||
return (
|
||||
<div class="panel last">
|
||||
<div className="header">
|
||||
an interlude
|
||||
</div>
|
||||
<div className="content file-view file-view-text speedbump">
|
||||
<canvas
|
||||
style="linear-gradient(45deg, #111318, #181f20)"
|
||||
data-canvas="cotyledon"
|
||||
>
|
||||
</canvas>
|
||||
<header>
|
||||
<h1>cotyledon</h1>
|
||||
</header>
|
||||
<div id="captcha" style="display: none;">
|
||||
<p style="max-width:480px">
|
||||
please prove you're not a robot by selecting all of the images with
|
||||
four-leaf clovers, until there are only regular clovers.
|
||||
<noscript>
|
||||
this will require javascript enabled on your computer to verify
|
||||
the mouse clicks.
|
||||
</noscript>
|
||||
</p>
|
||||
<div className="enter-container">
|
||||
<div className="image-grid">
|
||||
<button>
|
||||
<img src="/captcha/image/1.jpeg" alt="a four-leaf clover" />
|
||||
</button>
|
||||
<button>
|
||||
<img src="/captcha/image/2.jpeg" alt="a four-leaf clover" />
|
||||
</button>
|
||||
<button>
|
||||
<img src="/captcha/image/3.jpeg" alt="a four-leaf clover" />
|
||||
</button>
|
||||
<button>
|
||||
<img src="/captcha/image/4.jpeg" alt="a four-leaf clover" />
|
||||
</button>
|
||||
<button>
|
||||
<img src="/captcha/image/5.jpeg" alt="a four-leaf clover" />
|
||||
</button>
|
||||
<button>
|
||||
<img src="/captcha/image/6.jpeg" alt="a four-leaf clover" />
|
||||
</button>
|
||||
<button>
|
||||
<img src="/captcha/image/7.jpeg" alt="a four-leaf clover" />
|
||||
</button>
|
||||
<button>
|
||||
<img src="/captcha/image/8.jpeg" alt="a four-leaf clover" />
|
||||
</button>
|
||||
<button>
|
||||
<img src="/captcha/image/9.jpeg" alt="a four-leaf clover" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="enter-container">
|
||||
<button id="enter2">all done</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="first">
|
||||
<p>
|
||||
this place is sacred, but dangerous. i have to keep visitors to an
|
||||
absolute minimum; you'll get dust on all the artifacts.
|
||||
</p>
|
||||
<p>
|
||||
by entering our museum, you agree not to use your camera. flash off
|
||||
isn't enough; the bits and bytes are alergic even to a camera's
|
||||
sensor
|
||||
</p>
|
||||
<p style="font-size:0.9rem;">
|
||||
(in english: please do not store downloads after you're done viewing
|
||||
them)
|
||||
</p>
|
||||
<div class="enter-container">
|
||||
<button id="enter">break my boundaries</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function Readme() {
|
||||
return (
|
||||
<div class="panel last">
|
||||
<div className="header">
|
||||
cotyledon
|
||||
</div>
|
||||
<div className="content file-view file-view-text">
|
||||
<div style="max-width: 71ch;padding:3rem;font-family:rmo,monospace">
|
||||
<p style="margin-top:0">
|
||||
welcome to the archive. if this is your first time here, i recommend
|
||||
starting in '<a href="/file/2017">2017</a>' and going
|
||||
chronologically from there. however, there is truly no wrong way to
|
||||
explore.
|
||||
</p>
|
||||
<p>
|
||||
note that there is a blanket trigger warning for everything in this
|
||||
archive: while there is nothing visually offensive, some portions of
|
||||
the text and emotions conveyed through this may hit extremely hard.
|
||||
you are warned.
|
||||
</p>
|
||||
<p>
|
||||
all file dates are real. at least as real as i could figure out.
|
||||
when i moved data across drives over my years, i accidentally had a
|
||||
few points where i stamped over all the dates with the day that
|
||||
moved the files. even fucked it up a final time in february 2025,
|
||||
while in the process of unfucking things.
|
||||
</p>
|
||||
<p>
|
||||
thankfully, my past self knew i'd want to assemble this kind of
|
||||
site, and because of that they were crazy about storing the dates of
|
||||
things inside of html, json/yaml files, and even in fucking
|
||||
databases. i'm glad it was all stored though, but jeez what a nerd.
|
||||
</p>
|
||||
<p>
|
||||
a few files were touched up for privacy, or otherwise re-encoded.
|
||||
some of them i added extra metadata.
|
||||
</p>
|
||||
<p>
|
||||
from the bottom of my heart: i hope you enjoy. it has been a
|
||||
nightmare putting this all together. technically and emotionally
|
||||
speaking. i'm glad we can put this all behind us, mark it as
|
||||
completed, and get started with the good shit.
|
||||
</p>
|
||||
<p>
|
||||
love,<br />clo
|
||||
</p>
|
||||
<br />
|
||||
<p>
|
||||
start here -> <a href="/file/2017">2017</a>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function ForEveryone() {
|
||||
// deno-fmt-ignore
|
||||
return <><div class="for_everyone">
|
||||
<p>today is my 21st birthday. april 30th, 2025.</p>
|
||||
<p>it's been nearly six months starting hormones.</p>
|
||||
<p>sometimes i feel great,</p>
|
||||
<p>sometimes i get dysphoria.</p>
|
||||
<p>with the walls around me gone</p>
|
||||
<p>that shit hits way harder than it did before.</p>
|
||||
<p>ugh..</p>
|
||||
<p>i'm glad the pain i felt is now explained,</p>
|
||||
<p>but now rendered in high definition.</p>
|
||||
<p>the smallest strands of hair on my face and belly act</p>
|
||||
<p>as sharpened nails to pierce my soul.</p>
|
||||
<p></p>
|
||||
<p>it's all a pathway to better days; the sun had risen.</p>
|
||||
<p>one little step at a time for both of us.</p>
|
||||
<p>today i quit my job. free falling, it feels so weird.</p>
|
||||
<p>like sky diving.</p>
|
||||
<p>the only thing i feel is cold wind.</p>
|
||||
<p>the only thing i see is everything,</p>
|
||||
<p>and it's beautiful.</p>
|
||||
<p>i have a month of falling before the parachute activates,</p>
|
||||
<p>gonna spend as much time of it on art as i can.</p>
|
||||
<p>that was, after all, my life plan:</p>
|
||||
<p>i wanted to make art, all the time,</p>
|
||||
<p>for everyone.</p>
|
||||
<p></p>
|
||||
<p>then you see what happened</p>
|
||||
<p>to the world and the internet.</p>
|
||||
<p>i never really got to live through that golden age,</p>
|
||||
<p>it probably sucked back then too.</p>
|
||||
<p>but now the big sites definitely stopped being fun.</p>
|
||||
<p>they slide their cold hands up my body</p>
|
||||
<p>and feel me around. it's unwelcoming, and</p>
|
||||
<p>inconsiderate to how sensitive my skin is.</p>
|
||||
<p>i'm so fucking glad i broke up with YouTube</p>
|
||||
<p>and their devilish friends.</p>
|
||||
<p>my NAS is at 5 / 24 TB</p>
|
||||
<p>and probably wont fill for the next decade.</p>
|
||||
<p></p>
|
||||
<p>it took 2 months for me to notice my body changed.</p>
|
||||
<p>that day was really nice, but it hurt a lot.</p>
|
||||
<p>a sharp, satisfying pain in my chest gave me life.</p>
|
||||
<p>learned new instincts for my arms</p>
|
||||
<p>so they'd stop poking my new shape.</p>
|
||||
<p>when i look at my face</p>
|
||||
<p>it's like a different person.</p>
|
||||
<p>she was the same as before, but completely new.</p>
|
||||
<p>something changed</p>
|
||||
<p>or i'm now used to seeing what makes me smile.</p>
|
||||
<p>regardless, whatever i see in the mirror, i smile.</p>
|
||||
<p>and, i don't hear that old name much anymore</p>
|
||||
<p>aside from nightmares. and you'll never repeat it, ok?</p>
|
||||
<p>okay.</p>
|
||||
<p></p>
|
||||
<p>been playing 'new canaan' by 'bill wurtz' on loop</p>
|
||||
<p>in the background.</p>
|
||||
<p>it kinda just feels right.</p>
|
||||
<p>especially when that verse near the end comes on.</p>
|
||||
<p></p>
|
||||
<p>more people have been allowed to visit me.</p>
|
||||
<p>my apartment used to be just for me,</p>
|
||||
<p>but the more i felt like a person</p>
|
||||
<p>the more i felt like having others over.</p>
|
||||
<p>still have to decorate and clean it a little,</p>
|
||||
<p>but it isn't a job to do alone.</p>
|
||||
<p>we dragged a giant a rug across the city one day,</p>
|
||||
<p>and it felt was like anything was possible.</p>
|
||||
<p>sometimes i have ten people visit in a day,</p>
|
||||
<p>or sometimes i focus my little eyes on just one.</p>
|
||||
<p>i never really know what i want to do</p>
|
||||
<p>until the time actually comes.</p>
|
||||
<p></p>
|
||||
{/* FILIP */}
|
||||
<p>i think about the times i was by the water with you.</p>
|
||||
<p>the sun setting warmly, icy air fell on our shoulders.</p>
|
||||
{/* NATALIE */}
|
||||
<p>and how we walked up to the top of that hill,</p>
|
||||
<p>you picked up and disposed a nail on the ground,</p>
|
||||
<p>walking the city thru places i've never been.</p>
|
||||
{/* BEN */}
|
||||
<p>or hiking through the park talking about compilers,</p>
|
||||
<p>tiring me out until i'd fall asleep in your arms.</p>
|
||||
{/* ELENA */}
|
||||
<p>and the way you held on to my hand as i woke up,</p>
|
||||
<p>noticing how i was trying to hide nightmare's tears.</p>
|
||||
<p></p>
|
||||
{/* HIGH SCHOOL */}
|
||||
<p>i remember we were yelling lyrics loudly,</p>
|
||||
<p>out of key yet cheered on because it was fun.</p>
|
||||
{/* ADVAITH/NATALIE */}
|
||||
<p>and when we all toured the big corporate office,</p>
|
||||
{/* AYU/HARRIS */}
|
||||
<p>then snuck in to some startup's office after hours;</p>
|
||||
<p>i don't remember what movie we watched.</p>
|
||||
{/* COLLEGE, DAY 1 IN EV's ROOM */}
|
||||
<p>i remember laying on the bunk bed,</p>
|
||||
<p>while the rest played a card game.</p>
|
||||
{/* MEGHAN/MORE */}
|
||||
<p>with us all laying on the rug, staring at the TV</p>
|
||||
<p>as the ending twist to that show was revealed.</p>
|
||||
<p></p>
|
||||
<p>all the moments i cherish,</p>
|
||||
<p>i love because it was always me.</p>
|
||||
<p>i didn't have to pretend,</p>
|
||||
<p>even if i didn't know who i was at the time.</p>
|
||||
<p>you all were there. for me.</p>
|
||||
<p></p>
|
||||
<p>i don't want to pretend any more</p>
|
||||
<p>i want to be myself. for everyone.</p>
|
||||
<p></p>
|
||||
<p>oh, the song ended. i thought it was on loop?</p>
|
||||
<p>it's late... can hear the crickets...</p>
|
||||
<p>and i can almost see the moon... mmmm...</p>
|
||||
<p>...nah, too much light pollution.</p>
|
||||
<p></p>
|
||||
<p>one day. one day.</p>
|
||||
<p></p>
|
||||
<p class="normal">before i go, i want to show the uncensored version of "journal about a girl", because i can trust you at least. keep in mind, i think you're one of the first people to ever see this.</p>
|
||||
</div>
|
||||
<div class="for_everyone" style="max-width:80ch;">
|
||||
<blockquote>
|
||||
<p>journal - 2024-09-14</p>
|
||||
<p>been at HackMIT today on behalf of the company. it's fun. me and zack were running around looking for people that might be good hires. he had this magic arbitrary criteria to tell "oh this person is probably cracked let's talk to them" and we go to the first one. they were a nerd, perfect. they seemed to be extremely talented with some extreme software projects.<br/>
|
||||
okay.. oof... its still clouding my mind<br/>
|
||||
i cant shake that feeling away</p>
|
||||
<p>hold on...</p>
|
||||
<p>at some point they open one of their profiles to navigate to some code, and it displays for a couple of seconds: "pronouns: she/they". i don't actually know anything about this person, but it was my perception that she is trans. their appearance, physique, and age felt similar to me, which tends makes people think you are male.</p>
|
||||
<p>but... she was having fun being herself. being a legend of identity and of her skill in computer science. winning the physics major. making cool shit at the hackathon, and probably in life. my perception of her was the exact essence of who i myself wanted to be. i was jealous of her life.</p>
|
||||
<p>i tried hard to avoid a breakdown. success. but i was feeling distant. the next hour or so was disorienting, trying not to think about it too hard. i think there was one possibly interesting person we talked to. i don't remember any of the other conversations. they were not important. but i couldn't think through them regardless.</p>
|
||||
<p>later, i decided to read some of her code. i either have a huge dislike towards the Rust programming language and/or it was not high quality code. welp, so just is a person studying. my perception was just a perception, inaccurate but impacting. i know i need to become myself, whoever that is. otherwise, i'm just going to feel this shit at higher doses. i think about this every day, and the amount of time i feel being consumed by these problems only grows.</p>
|
||||
<p>getting through it all is a lonely feeling. not because no one is around, but because i am isolated emotionally. i know other people hit these feelings, but we all are too afraid to speak up, and it's all lonely.</p>
|
||||
<p>waiting on a reply from someone from healthcare. it'll be slow, but it will be okay.</p>
|
||||
</blockquote>
|
||||
</div>
|
||||
<div class="for_everyone">
|
||||
<p class="normal">
|
||||
i've learned that even when i feel alone, it doesn't have to feel lonely. i know it's hard, dear. i know it's scary. but i promise it's possible. we're all in this together. struggling together. sacrificing together. we dedicate our lives to each you, and our art for everyone.
|
||||
</p>
|
||||
|
||||
<p class="normal" style="font-size:2rem;color:#9C91FF;font-family:times,serif;font-style:italic">
|
||||
and then we knew,<br/>
|
||||
just like paper airplanes: that we could fly...
|
||||
</p>
|
||||
<br />
|
||||
<p class="normal">
|
||||
<a href="/" style='text-decoration:underline;text-underline-offset:0.2em;'>fin.</a>
|
||||
</p>
|
||||
</div>
|
||||
</>
|
||||
}
|
||||
ForEveryone.class = "text";
|
40
src/file-viewer/extension-stats.ts
Normal file
40
src/file-viewer/extension-stats.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
// run
|
||||
function* run(config, code, ast) {
|
||||
const file = yield* (0, _normalizeFile.default)(config.passes, (0, _normalizeOpts.default)(config), code, ast);
|
||||
const opts = file.opts;
|
||||
try {
|
||||
yield* transformFile(file, config.passes);
|
||||
} catch (e) {
|
||||
var _opts$filename;
|
||||
e.message = `${(_opts$filename = opts.filename) != null ? _opts$filename : "unknown file"}: ${e.message}`;
|
||||
if (!e.code) {
|
||||
e.code = "BABEL_TRANSFORM_ERROR";
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
let outputCode, outputMap;
|
||||
try {
|
||||
if (opts.code !== false) {
|
||||
({
|
||||
outputCode,
|
||||
outputMap
|
||||
} = (0, _generate.default)(config.passes, file));
|
||||
}
|
||||
} catch (e) {
|
||||
var _opts$filename2;
|
||||
e.message = `${(_opts$filename2 = opts.filename) != null ? _opts$filename2 : "unknown file"}: ${e.message}`;
|
||||
if (!e.code) {
|
||||
e.code = "BABEL_GENERATE_ERROR";
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
return {
|
||||
metadata: file.metadata,
|
||||
options: opts,
|
||||
ast: opts.ast === true ? file.ast : null,
|
||||
code: outputCode === undefined ? null : outputCode,
|
||||
map: outputMap === undefined ? null : outputMap,
|
||||
sourceType: file.ast.program.sourceType,
|
||||
externalDependencies: (0, _deepArray.flattenToSet)(config.externalDependencies)
|
||||
};
|
||||
}
|
44
src/file-viewer/highlight-grammar/astro.plist
Normal file
44
src/file-viewer/highlight-grammar/astro.plist
Normal file
|
@ -0,0 +1,44 @@
|
|||
// default
|
||||
function normalizeOptions(config) {
|
||||
const {
|
||||
filename,
|
||||
cwd,
|
||||
filenameRelative = typeof filename === "string" ? _path().relative(cwd, filename) : "unknown",
|
||||
sourceType = "module",
|
||||
inputSourceMap,
|
||||
sourceMaps = !!inputSourceMap,
|
||||
sourceRoot = config.options.moduleRoot,
|
||||
sourceFileName = _path().basename(filenameRelative),
|
||||
comments = true,
|
||||
compact = "auto"
|
||||
} = config.options;
|
||||
const opts = config.options;
|
||||
const options = Object.assign({}, opts, {
|
||||
parserOpts: Object.assign({
|
||||
sourceType: _path().extname(filenameRelative) === ".mjs" ? "module" : sourceType,
|
||||
sourceFileName: filename,
|
||||
plugins: []
|
||||
}, opts.parserOpts),
|
||||
generatorOpts: Object.assign({
|
||||
filename,
|
||||
auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
|
||||
auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
|
||||
retainLines: opts.retainLines,
|
||||
comments,
|
||||
shouldPrintComment: opts.shouldPrintComment,
|
||||
compact,
|
||||
minified: opts.minified,
|
||||
sourceMaps,
|
||||
sourceRoot,
|
||||
sourceFileName
|
||||
}, opts.generatorOpts)
|
||||
});
|
||||
for (const plugins of config.passes) {
|
||||
for (const plugin of plugins) {
|
||||
if (plugin.manipulateOptions) {
|
||||
plugin.manipulateOptions(options, options.parserOpts);
|
||||
}
|
||||
}
|
||||
}
|
||||
return options;
|
||||
}
|
53
src/file-viewer/highlight-grammar/css.plist
Normal file
53
src/file-viewer/highlight-grammar/css.plist
Normal file
|
@ -0,0 +1,53 @@
|
|||
// default
|
||||
function* normalizeFile(pluginPasses, options, code, ast) {
|
||||
code = `${code || ""}`;
|
||||
if (ast) {
|
||||
if (ast.type === "Program") {
|
||||
ast = file(ast, [], []);
|
||||
} else if (ast.type !== "File") {
|
||||
throw new Error("AST root must be a Program or File node");
|
||||
}
|
||||
if (options.cloneInputAst) {
|
||||
ast = (0, _cloneDeep.default)(ast);
|
||||
}
|
||||
} else {
|
||||
ast = yield* (0, _index.default)(pluginPasses, options, code);
|
||||
}
|
||||
let inputMap = null;
|
||||
if (options.inputSourceMap !== false) {
|
||||
if (typeof options.inputSourceMap === "object") {
|
||||
inputMap = _convertSourceMap().fromObject(options.inputSourceMap);
|
||||
}
|
||||
if (!inputMap) {
|
||||
const lastComment = extractComments(INLINE_SOURCEMAP_REGEX, ast);
|
||||
if (lastComment) {
|
||||
try {
|
||||
inputMap = _convertSourceMap().fromComment("//" + lastComment);
|
||||
} catch (err) {
|
||||
{
|
||||
debug("discarding unknown inline input sourcemap");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!inputMap) {
|
||||
const lastComment = extractComments(EXTERNAL_SOURCEMAP_REGEX, ast);
|
||||
if (typeof options.filename === "string" && lastComment) {
|
||||
try {
|
||||
const match = EXTERNAL_SOURCEMAP_REGEX.exec(lastComment);
|
||||
const inputMapContent = _fs().readFileSync(_path().resolve(_path().dirname(options.filename), match[1]), "utf8");
|
||||
inputMap = _convertSourceMap().fromJSON(inputMapContent);
|
||||
} catch (err) {
|
||||
debug("discarding unknown file input sourcemap", err);
|
||||
}
|
||||
} else if (lastComment) {
|
||||
debug("discarding un-loadable file input sourcemap");
|
||||
}
|
||||
}
|
||||
}
|
||||
return new _file.default(options, {
|
||||
code,
|
||||
ast: ast,
|
||||
inputMap
|
||||
});
|
||||
}
|
56
src/file-viewer/highlight-grammar/diff.plist
Normal file
56
src/file-viewer/highlight-grammar/diff.plist
Normal file
|
@ -0,0 +1,56 @@
|
|||
// default
|
||||
function* parser(pluginPasses, {
|
||||
parserOpts,
|
||||
highlightCode = true,
|
||||
filename = "unknown"
|
||||
}, code) {
|
||||
try {
|
||||
const results = [];
|
||||
for (const plugins of pluginPasses) {
|
||||
for (const plugin of plugins) {
|
||||
const {
|
||||
parserOverride
|
||||
} = plugin;
|
||||
if (parserOverride) {
|
||||
const ast = parserOverride(code, parserOpts, _parser().parse);
|
||||
if (ast !== undefined) results.push(ast);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (results.length === 0) {
|
||||
return (0, _parser().parse)(code, parserOpts);
|
||||
} else if (results.length === 1) {
|
||||
yield* [];
|
||||
if (typeof results[0].then === "function") {
|
||||
throw new Error(`You appear to be using an async parser plugin, ` + `which your current version of Babel does not support. ` + `If you're using a published plugin, you may need to upgrade ` + `your @babel/core version.`);
|
||||
}
|
||||
return results[0];
|
||||
}
|
||||
throw new Error("More than one plugin attempted to override parsing.");
|
||||
} catch (err) {
|
||||
if (err.code === "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED") {
|
||||
err.message += "\nConsider renaming the file to '.mjs', or setting sourceType:module " + "or sourceType:unambiguous in your Babel config for this file.";
|
||||
}
|
||||
const {
|
||||
loc,
|
||||
missingPlugin
|
||||
} = err;
|
||||
if (loc) {
|
||||
const codeFrame = (0, _codeFrame().codeFrameColumns)(code, {
|
||||
start: {
|
||||
line: loc.line,
|
||||
column: loc.column + 1
|
||||
}
|
||||
}, {
|
||||
highlightCode
|
||||
});
|
||||
if (missingPlugin) {
|
||||
err.message = `${filename}: ` + (0, _missingPluginHelper.default)(missingPlugin[0], loc, codeFrame, filename);
|
||||
} else {
|
||||
err.message = `${filename}: ${err.message}\n\n` + codeFrame;
|
||||
}
|
||||
err.code = "BABEL_PARSE_ERROR";
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
33
src/file-viewer/highlight-grammar/dosbatch.plist
Normal file
33
src/file-viewer/highlight-grammar/dosbatch.plist
Normal file
|
@ -0,0 +1,33 @@
|
|||
// default
|
||||
function generateMissingPluginMessage(missingPluginName, loc, codeFrame, filename) {
|
||||
let helpMessage = `Support for the experimental syntax '${missingPluginName}' isn't currently enabled ` + `(${loc.line}:${loc.column + 1}):\n\n` + codeFrame;
|
||||
const pluginInfo = pluginNameMap[missingPluginName];
|
||||
if (pluginInfo) {
|
||||
const {
|
||||
syntax: syntaxPlugin,
|
||||
transform: transformPlugin
|
||||
} = pluginInfo;
|
||||
if (syntaxPlugin) {
|
||||
const syntaxPluginInfo = getNameURLCombination(syntaxPlugin);
|
||||
if (transformPlugin) {
|
||||
const transformPluginInfo = getNameURLCombination(transformPlugin);
|
||||
const sectionType = transformPlugin.name.startsWith("@babel/plugin") ? "plugins" : "presets";
|
||||
helpMessage += `\n\nAdd ${transformPluginInfo} to the '${sectionType}' section of your Babel config to enable transformation.
|
||||
If you want to leave it as-is, add ${syntaxPluginInfo} to the 'plugins' section to enable parsing.`;
|
||||
} else {
|
||||
helpMessage += `\n\nAdd ${syntaxPluginInfo} to the 'plugins' section of your Babel config ` + `to enable parsing.`;
|
||||
}
|
||||
}
|
||||
}
|
||||
const msgFilename = filename === "unknown" ? "<name of the input file>" : filename;
|
||||
helpMessage += `
|
||||
|
||||
If you already added the plugin for this syntax to your config, it's possible that your config \
|
||||
isn't being loaded.
|
||||
You can re-run Babel with the BABEL_SHOW_CONFIG_FOR environment variable to show the loaded \
|
||||
configuration:
|
||||
\tnpx cross-env BABEL_SHOW_CONFIG_FOR=${msgFilename} <your build command>
|
||||
See https://babeljs.io/docs/configuration#print-effective-configs for more info.
|
||||
`;
|
||||
return helpMessage;
|
||||
}
|
11
src/file-viewer/highlight-grammar/json.plist
Normal file
11
src/file-viewer/highlight-grammar/json.plist
Normal file
|
@ -0,0 +1,11 @@
|
|||
// default
|
||||
function _default(value) {
|
||||
if (typeof value !== "object") return value;
|
||||
{
|
||||
try {
|
||||
return deepClone(value, new Map(), true);
|
||||
} catch (_) {
|
||||
return structuredClone(value);
|
||||
}
|
||||
}
|
||||
}
|
61
src/file-viewer/highlight-grammar/lua.plist
Normal file
61
src/file-viewer/highlight-grammar/lua.plist
Normal file
|
@ -0,0 +1,61 @@
|
|||
// default
|
||||
function generateCode(pluginPasses, file) {
|
||||
const {
|
||||
opts,
|
||||
ast,
|
||||
code,
|
||||
inputMap
|
||||
} = file;
|
||||
const {
|
||||
generatorOpts
|
||||
} = opts;
|
||||
generatorOpts.inputSourceMap = inputMap == null ? void 0 : inputMap.toObject();
|
||||
const results = [];
|
||||
for (const plugins of pluginPasses) {
|
||||
for (const plugin of plugins) {
|
||||
const {
|
||||
generatorOverride
|
||||
} = plugin;
|
||||
if (generatorOverride) {
|
||||
const result = generatorOverride(ast, generatorOpts, code, _generator().default);
|
||||
if (result !== undefined) results.push(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
let result;
|
||||
if (results.length === 0) {
|
||||
result = (0, _generator().default)(ast, generatorOpts, code);
|
||||
} else if (results.length === 1) {
|
||||
result = results[0];
|
||||
if (typeof result.then === "function") {
|
||||
throw new Error(`You appear to be using an async codegen plugin, ` + `which your current version of Babel does not support. ` + `If you're using a published plugin, ` + `you may need to upgrade your @babel/core version.`);
|
||||
}
|
||||
} else {
|
||||
throw new Error("More than one plugin attempted to override codegen.");
|
||||
}
|
||||
let {
|
||||
code: outputCode,
|
||||
decodedMap: outputMap = result.map
|
||||
} = result;
|
||||
if (result.__mergedMap) {
|
||||
outputMap = Object.assign({}, result.map);
|
||||
} else {
|
||||
if (outputMap) {
|
||||
if (inputMap) {
|
||||
outputMap = (0, _mergeMap.default)(inputMap.toObject(), outputMap, generatorOpts.sourceFileName);
|
||||
} else {
|
||||
outputMap = result.map;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (opts.sourceMaps === "inline" || opts.sourceMaps === "both") {
|
||||
outputCode += "\n" + _convertSourceMap().fromObject(outputMap).toComment();
|
||||
}
|
||||
if (opts.sourceMaps === "inline") {
|
||||
outputMap = null;
|
||||
}
|
||||
return {
|
||||
outputCode,
|
||||
outputMap
|
||||
};
|
||||
}
|
17
src/file-viewer/highlight-grammar/mdx.plist
Normal file
17
src/file-viewer/highlight-grammar/mdx.plist
Normal file
|
@ -0,0 +1,17 @@
|
|||
// default
|
||||
function mergeSourceMap(inputMap, map, sourceFileName) {
|
||||
const source = sourceFileName.replace(/\\/g, "/");
|
||||
let found = false;
|
||||
const result = _remapping()(rootless(map), (s, ctx) => {
|
||||
if (s === source && !found) {
|
||||
found = true;
|
||||
ctx.source = "";
|
||||
return rootless(inputMap);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
if (typeof inputMap.sourceRoot === "string") {
|
||||
result.sourceRoot = inputMap.sourceRoot;
|
||||
}
|
||||
return Object.assign({}, result);
|
||||
}
|
0
src/file-viewer/highlight-grammar/php.plist
Normal file
0
src/file-viewer/highlight-grammar/php.plist
Normal file
676
src/file-viewer/highlight-grammar/powershell.plist
Normal file
676
src/file-viewer/highlight-grammar/powershell.plist
Normal file
File diff suppressed because one or more lines are too long
33
src/file-viewer/highlight-grammar/python.plist
Normal file
33
src/file-viewer/highlight-grammar/python.plist
Normal file
|
@ -0,0 +1,33 @@
|
|||
// loadPlugin
|
||||
function* loadPlugin(name, dirname) {
|
||||
const {
|
||||
filepath,
|
||||
loader
|
||||
} = resolvePlugin(name, dirname, yield* (0, _async.isAsync)());
|
||||
const value = yield* requireModule("plugin", loader, filepath);
|
||||
debug("Loaded plugin %o from %o.", name, dirname);
|
||||
return {
|
||||
filepath,
|
||||
value
|
||||
};
|
||||
}
|
||||
|
||||
// loadPreset
|
||||
function* loadPreset(name, dirname) {
|
||||
const {
|
||||
filepath,
|
||||
loader
|
||||
} = resolvePreset(name, dirname, yield* (0, _async.isAsync)());
|
||||
const value = yield* requireModule("preset", loader, filepath);
|
||||
debug("Loaded preset %o from %o.", name, dirname);
|
||||
return {
|
||||
filepath,
|
||||
value
|
||||
};
|
||||
}
|
||||
|
||||
// resolvePlugin
|
||||
function () { [native code] }
|
||||
|
||||
// resolvePreset
|
||||
function () { [native code] }
|
52
src/file-viewer/highlight-grammar/shell.plist
Normal file
52
src/file-viewer/highlight-grammar/shell.plist
Normal file
|
@ -0,0 +1,52 @@
|
|||
// moduleResolve
|
||||
function moduleResolve(specifier, base, conditions, preserveSymlinks) {
|
||||
const protocol = base.protocol;
|
||||
const isData = protocol === 'data:';
|
||||
const isRemote = isData || protocol === 'http:' || protocol === 'https:';
|
||||
let resolved;
|
||||
if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
|
||||
try {
|
||||
resolved = new (_url().URL)(specifier, base);
|
||||
} catch (error_) {
|
||||
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
|
||||
error.cause = error_;
|
||||
throw error;
|
||||
}
|
||||
} else if (protocol === 'file:' && specifier[0] === '#') {
|
||||
resolved = packageImportsResolve(specifier, base, conditions);
|
||||
} else {
|
||||
try {
|
||||
resolved = new (_url().URL)(specifier);
|
||||
} catch (error_) {
|
||||
if (isRemote && !_module().builtinModules.includes(specifier)) {
|
||||
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
|
||||
error.cause = error_;
|
||||
throw error;
|
||||
}
|
||||
resolved = packageResolve(specifier, base, conditions);
|
||||
}
|
||||
}
|
||||
_assert()(resolved !== undefined, 'expected to be defined');
|
||||
if (resolved.protocol !== 'file:') {
|
||||
return resolved;
|
||||
}
|
||||
return finalizeResolution(resolved, base, preserveSymlinks);
|
||||
}
|
||||
|
||||
// resolve
|
||||
function resolve(specifier, parent) {
|
||||
if (!parent) {
|
||||
throw new Error('Please pass `parent`: `import-meta-resolve` cannot ponyfill that');
|
||||
}
|
||||
try {
|
||||
return defaultResolve(specifier, {
|
||||
parentURL: parent
|
||||
}).url;
|
||||
} catch (error) {
|
||||
const exception = error;
|
||||
if ((exception.code === 'ERR_UNSUPPORTED_DIR_IMPORT' || exception.code === 'ERR_MODULE_NOT_FOUND') && typeof exception.url === 'string') {
|
||||
return exception.url;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
28
src/file-viewer/highlight-grammar/toml.plist
Normal file
28
src/file-viewer/highlight-grammar/toml.plist
Normal file
|
@ -0,0 +1,28 @@
|
|||
// transform
|
||||
function transform(code, optsOrCallback, maybeCallback) {
|
||||
let opts;
|
||||
let callback;
|
||||
if (typeof optsOrCallback === "function") {
|
||||
callback = optsOrCallback;
|
||||
opts = undefined;
|
||||
} else {
|
||||
opts = optsOrCallback;
|
||||
callback = maybeCallback;
|
||||
}
|
||||
if (callback === undefined) {
|
||||
{
|
||||
return (0, _rewriteStackTrace.beginHiddenCallStack)(transformRunner.sync)(code, opts);
|
||||
}
|
||||
}
|
||||
(0, _rewriteStackTrace.beginHiddenCallStack)(transformRunner.errback)(code, opts, callback);
|
||||
}
|
||||
|
||||
// transformAsync
|
||||
function transformAsync(...args) {
|
||||
return (0, _rewriteStackTrace.beginHiddenCallStack)(transformRunner.async)(...args);
|
||||
}
|
||||
|
||||
// transformSync
|
||||
function transformSync(...args) {
|
||||
return (0, _rewriteStackTrace.beginHiddenCallStack)(transformRunner.sync)(...args);
|
||||
}
|
28
src/file-viewer/highlight-grammar/ts.plist
Normal file
28
src/file-viewer/highlight-grammar/ts.plist
Normal file
|
@ -0,0 +1,28 @@
|
|||
// transformFromAst
|
||||
function transformFromAst(ast, code, optsOrCallback, maybeCallback) {
|
||||
let opts;
|
||||
let callback;
|
||||
if (typeof optsOrCallback === "function") {
|
||||
callback = optsOrCallback;
|
||||
opts = undefined;
|
||||
} else {
|
||||
opts = optsOrCallback;
|
||||
callback = maybeCallback;
|
||||
}
|
||||
if (callback === undefined) {
|
||||
{
|
||||
return (0, _rewriteStackTrace.beginHiddenCallStack)(transformFromAstRunner.sync)(ast, code, opts);
|
||||
}
|
||||
}
|
||||
(0, _rewriteStackTrace.beginHiddenCallStack)(transformFromAstRunner.errback)(ast, code, opts, callback);
|
||||
}
|
||||
|
||||
// transformFromAstAsync
|
||||
function transformFromAstAsync(...args) {
|
||||
return (0, _rewriteStackTrace.beginHiddenCallStack)(transformFromAstRunner.async)(...args);
|
||||
}
|
||||
|
||||
// transformFromAstSync
|
||||
function transformFromAstSync(...args) {
|
||||
return (0, _rewriteStackTrace.beginHiddenCallStack)(transformFromAstRunner.sync)(...args);
|
||||
}
|
23
src/file-viewer/highlight-grammar/tsx.plist
Normal file
23
src/file-viewer/highlight-grammar/tsx.plist
Normal file
|
@ -0,0 +1,23 @@
|
|||
// parse
|
||||
function parse(code, opts, callback) {
|
||||
if (typeof opts === "function") {
|
||||
callback = opts;
|
||||
opts = undefined;
|
||||
}
|
||||
if (callback === undefined) {
|
||||
{
|
||||
return (0, _rewriteStackTrace.beginHiddenCallStack)(parseRunner.sync)(code, opts);
|
||||
}
|
||||
}
|
||||
(0, _rewriteStackTrace.beginHiddenCallStack)(parseRunner.errback)(code, opts, callback);
|
||||
}
|
||||
|
||||
// parseAsync
|
||||
function parseAsync(...args) {
|
||||
return (0, _rewriteStackTrace.beginHiddenCallStack)(parseRunner.async)(...args);
|
||||
}
|
||||
|
||||
// parseSync
|
||||
function parseSync(...args) {
|
||||
return (0, _rewriteStackTrace.beginHiddenCallStack)(parseRunner.sync)(...args);
|
||||
}
|
10
src/file-viewer/highlight-grammar/xml.plist
Normal file
10
src/file-viewer/highlight-grammar/xml.plist
Normal file
|
@ -0,0 +1,10 @@
|
|||
// default
|
||||
(api, options, dirname) => {
|
||||
let clonedApi;
|
||||
for (const name of Object.keys(apiPolyfills)) {
|
||||
if (api[name]) continue;
|
||||
clonedApi != null ? clonedApi : clonedApi = copyApiObject(api);
|
||||
clonedApi[name] = apiPolyfills[name](clonedApi);
|
||||
}
|
||||
return builder(clonedApi != null ? clonedApi : api, options || {}, dirname);
|
||||
}
|
25
src/file-viewer/highlight-grammar/yaml.plist
Normal file
25
src/file-viewer/highlight-grammar/yaml.plist
Normal file
|
@ -0,0 +1,25 @@
|
|||
// declare
|
||||
function declare(builder) {
|
||||
return (api, options, dirname) => {
|
||||
let clonedApi;
|
||||
for (const name of Object.keys(apiPolyfills)) {
|
||||
if (api[name]) continue;
|
||||
clonedApi != null ? clonedApi : clonedApi = copyApiObject(api);
|
||||
clonedApi[name] = apiPolyfills[name](clonedApi);
|
||||
}
|
||||
return builder(clonedApi != null ? clonedApi : api, options || {}, dirname);
|
||||
};
|
||||
}
|
||||
|
||||
// declarePreset
|
||||
function declare(builder) {
|
||||
return (api, options, dirname) => {
|
||||
let clonedApi;
|
||||
for (const name of Object.keys(apiPolyfills)) {
|
||||
if (api[name]) continue;
|
||||
clonedApi != null ? clonedApi : clonedApi = copyApiObject(api);
|
||||
clonedApi[name] = apiPolyfills[name](clonedApi);
|
||||
}
|
||||
return builder(clonedApi != null ? clonedApi : api, options || {}, dirname);
|
||||
};
|
||||
}
|
17
src/file-viewer/highlight-grammar/zig.plist
Normal file
17
src/file-viewer/highlight-grammar/zig.plist
Normal file
|
@ -0,0 +1,17 @@
|
|||
// default
|
||||
(api, options, dirname) => {
|
||||
let clonedApi;
|
||||
for (const name of Object.keys(apiPolyfills)) {
|
||||
if (api[name]) continue;
|
||||
clonedApi != null ? clonedApi : clonedApi = copyApiObject(api);
|
||||
clonedApi[name] = apiPolyfills[name](clonedApi);
|
||||
}
|
||||
return builder(clonedApi != null ? clonedApi : api, options || {}, dirname);
|
||||
}
|
||||
|
||||
// defineCommonJSHook
|
||||
function defineCommonJSHook(file, hook) {
|
||||
let hooks = file.get(commonJSHooksKey);
|
||||
if (!hooks) file.set(commonJSHooksKey, hooks = []);
|
||||
hooks.push(hook);
|
||||
}
|
200
src/file-viewer/highlight.ts
Normal file
200
src/file-viewer/highlight.ts
Normal file
|
@ -0,0 +1,200 @@
|
|||
// buildDynamicImport
|
||||
function buildDynamicImport(node, deferToThen, wrapWithPromise, builder) {
|
||||
const specifier = _core.types.isCallExpression(node) ? node.arguments[0] : node.source;
|
||||
if (_core.types.isStringLiteral(specifier) || _core.types.isTemplateLiteral(specifier) && specifier.quasis.length === 0) {
|
||||
if (deferToThen) {
|
||||
return _core.template.expression.ast`
|
||||
Promise.resolve().then(() => ${builder(specifier)})
|
||||
`;
|
||||
} else return builder(specifier);
|
||||
}
|
||||
const specifierToString = _core.types.isTemplateLiteral(specifier) ? _core.types.identifier("specifier") : _core.types.templateLiteral([_core.types.templateElement({
|
||||
raw: ""
|
||||
}), _core.types.templateElement({
|
||||
raw: ""
|
||||
})], [_core.types.identifier("specifier")]);
|
||||
if (deferToThen) {
|
||||
return _core.template.expression.ast`
|
||||
(specifier =>
|
||||
new Promise(r => r(${specifierToString}))
|
||||
.then(s => ${builder(_core.types.identifier("s"))})
|
||||
)(${specifier})
|
||||
`;
|
||||
} else if (wrapWithPromise) {
|
||||
return _core.template.expression.ast`
|
||||
(specifier =>
|
||||
new Promise(r => r(${builder(specifierToString)}))
|
||||
)(${specifier})
|
||||
`;
|
||||
} else {
|
||||
return _core.template.expression.ast`
|
||||
(specifier => ${builder(specifierToString)})(${specifier})
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
// buildNamespaceInitStatements
|
||||
function buildNamespaceInitStatements(metadata, sourceMetadata, constantReexports = false, wrapReference = Lazy.wrapReference) {
|
||||
var _wrapReference;
|
||||
const statements = [];
|
||||
const srcNamespaceId = _core.types.identifier(sourceMetadata.name);
|
||||
for (const localName of sourceMetadata.importsNamespace) {
|
||||
if (localName === sourceMetadata.name) continue;
|
||||
statements.push(_core.template.statement`var NAME = SOURCE;`({
|
||||
NAME: localName,
|
||||
SOURCE: _core.types.cloneNode(srcNamespaceId)
|
||||
}));
|
||||
}
|
||||
const srcNamespace = (_wrapReference = wrapReference(srcNamespaceId, sourceMetadata.wrap)) != null ? _wrapReference : srcNamespaceId;
|
||||
if (constantReexports) {
|
||||
statements.push(...buildReexportsFromMeta(metadata, sourceMetadata, true, wrapReference));
|
||||
}
|
||||
for (const exportName of sourceMetadata.reexportNamespace) {
|
||||
statements.push((!_core.types.isIdentifier(srcNamespace) ? _core.template.statement`
|
||||
Object.defineProperty(EXPORTS, "NAME", {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
});
|
||||
` : _core.template.statement`EXPORTS.NAME = NAMESPACE;`)({
|
||||
EXPORTS: metadata.exportName,
|
||||
NAME: exportName,
|
||||
NAMESPACE: _core.types.cloneNode(srcNamespace)
|
||||
}));
|
||||
}
|
||||
if (sourceMetadata.reexportAll) {
|
||||
const statement = buildNamespaceReexport(metadata, _core.types.cloneNode(srcNamespace), constantReexports);
|
||||
statement.loc = sourceMetadata.reexportAll.loc;
|
||||
statements.push(statement);
|
||||
}
|
||||
return statements;
|
||||
}
|
||||
|
||||
// ensureStatementsHoisted
|
||||
function ensureStatementsHoisted(statements) {
|
||||
statements.forEach(header => {
|
||||
header._blockHoist = 3;
|
||||
});
|
||||
}
|
||||
|
||||
// getModuleName
|
||||
function getModuleName(rootOpts, pluginOpts) {
|
||||
var _pluginOpts$moduleId, _pluginOpts$moduleIds, _pluginOpts$getModule, _pluginOpts$moduleRoo;
|
||||
return originalGetModuleName(rootOpts, {
|
||||
moduleId: (_pluginOpts$moduleId = pluginOpts.moduleId) != null ? _pluginOpts$moduleId : rootOpts.moduleId,
|
||||
moduleIds: (_pluginOpts$moduleIds = pluginOpts.moduleIds) != null ? _pluginOpts$moduleIds : rootOpts.moduleIds,
|
||||
getModuleId: (_pluginOpts$getModule = pluginOpts.getModuleId) != null ? _pluginOpts$getModule : rootOpts.getModuleId,
|
||||
moduleRoot: (_pluginOpts$moduleRoo = pluginOpts.moduleRoot) != null ? _pluginOpts$moduleRoo : rootOpts.moduleRoot
|
||||
});
|
||||
}
|
||||
|
||||
// hasExports
|
||||
function hasExports(metadata) {
|
||||
return metadata.hasExports;
|
||||
}
|
||||
|
||||
// isModule
|
||||
function isModule(path) {
|
||||
return path.node.sourceType === "module";
|
||||
}
|
||||
|
||||
// isSideEffectImport
|
||||
function isSideEffectImport(source) {
|
||||
return source.imports.size === 0 && source.importsNamespace.size === 0 && source.reexports.size === 0 && source.reexportNamespace.size === 0 && !source.reexportAll;
|
||||
}
|
||||
|
||||
// rewriteModuleStatementsAndPrepareHeader
|
||||
function rewriteModuleStatementsAndPrepareHeader(path, {
|
||||
exportName,
|
||||
strict,
|
||||
allowTopLevelThis,
|
||||
strictMode,
|
||||
noInterop,
|
||||
importInterop = noInterop ? "none" : "babel",
|
||||
lazy,
|
||||
getWrapperPayload = Lazy.toGetWrapperPayload(lazy != null ? lazy : false),
|
||||
wrapReference = Lazy.wrapReference,
|
||||
esNamespaceOnly,
|
||||
filename,
|
||||
constantReexports = arguments[1].loose,
|
||||
enumerableModuleMeta = arguments[1].loose,
|
||||
noIncompleteNsImportDetection
|
||||
}) {
|
||||
(0, _normalizeAndLoadMetadata.validateImportInteropOption)(importInterop);
|
||||
_assert((0, _helperModuleImports.isModule)(path), "Cannot process module statements in a script");
|
||||
path.node.sourceType = "script";
|
||||
const meta = (0, _normalizeAndLoadMetadata.default)(path, exportName, {
|
||||
importInterop,
|
||||
initializeReexports: constantReexports,
|
||||
getWrapperPayload,
|
||||
esNamespaceOnly,
|
||||
filename
|
||||
});
|
||||
if (!allowTopLevelThis) {
|
||||
(0, _rewriteThis.default)(path);
|
||||
}
|
||||
(0, _rewriteLiveReferences.default)(path, meta, wrapReference);
|
||||
if (strictMode !== false) {
|
||||
const hasStrict = path.node.directives.some(directive => {
|
||||
return directive.value.value === "use strict";
|
||||
});
|
||||
if (!hasStrict) {
|
||||
path.unshiftContainer("directives", _core.types.directive(_core.types.directiveLiteral("use strict")));
|
||||
}
|
||||
}
|
||||
const headers = [];
|
||||
if ((0, _normalizeAndLoadMetadata.hasExports)(meta) && !strict) {
|
||||
headers.push(buildESModuleHeader(meta, enumerableModuleMeta));
|
||||
}
|
||||
const nameList = buildExportNameListDeclaration(path, meta);
|
||||
if (nameList) {
|
||||
meta.exportNameListName = nameList.name;
|
||||
headers.push(nameList.statement);
|
||||
}
|
||||
headers.push(...buildExportInitializationStatements(path, meta, wrapReference, constantReexports, noIncompleteNsImportDetection));
|
||||
return {
|
||||
meta,
|
||||
headers
|
||||
};
|
||||
}
|
||||
|
||||
// rewriteThis
|
||||
function rewriteThis(programPath) {
|
||||
if (!rewriteThisVisitor) {
|
||||
rewriteThisVisitor = _traverse.visitors.environmentVisitor({
|
||||
ThisExpression(path) {
|
||||
path.replaceWith(_core.types.unaryExpression("void", _core.types.numericLiteral(0), true));
|
||||
}
|
||||
});
|
||||
rewriteThisVisitor.noScope = true;
|
||||
}
|
||||
(0, _traverse.default)(programPath.node, rewriteThisVisitor);
|
||||
}
|
||||
|
||||
// wrapInterop
|
||||
function wrapInterop(programPath, expr, type) {
|
||||
if (type === "none") {
|
||||
return null;
|
||||
}
|
||||
if (type === "node-namespace") {
|
||||
return _core.types.callExpression(programPath.hub.addHelper("interopRequireWildcard"), [expr, _core.types.booleanLiteral(true)]);
|
||||
} else if (type === "node-default") {
|
||||
return null;
|
||||
}
|
||||
let helper;
|
||||
if (type === "default") {
|
||||
helper = "interopRequireDefault";
|
||||
} else if (type === "namespace") {
|
||||
helper = "interopRequireWildcard";
|
||||
} else {
|
||||
throw new Error(`Unknown interop: ${type}`);
|
||||
}
|
||||
return _core.types.callExpression(programPath.hub.addHelper(helper), [expr]);
|
||||
}
|
||||
|
||||
// getDynamicImportSource
|
||||
function getDynamicImportSource(node) {
|
||||
const [source] = node.arguments;
|
||||
return _core.types.isStringLiteral(source) || _core.types.isTemplateLiteral(source) ? source : _core.template.expression.ast`\`\${${source}}\``;
|
||||
}
|
463
src/file-viewer/mime.json
Normal file
463
src/file-viewer/mime.json
Normal file
|
@ -0,0 +1,463 @@
|
|||
|
||||
".aac": "audio/x-aac",
|
||||
".aif": "audio/x-aiff",
|
||||
".aifc": "audio/x-aiff",
|
||||
".aiff": "audio/x-aiff",
|
||||
".apk": "application/vnd.android.package-archive",
|
||||
".asm": "text/x-asm",
|
||||
".bat": "application/x-msdownload",
|
||||
".bmp": "image/bmp",
|
||||
".c": "text/x-c",
|
||||
".cc": "text/x-c",
|
||||
".class": "application/java-vm",
|
||||
".com": "application/x-msdownload",
|
||||
".conf": "text/plain",
|
||||
".cpp": "text/x-c",
|
||||
".css": "text/css",
|
||||
".cxx": "text/x-c",
|
||||
".def": "text/plain",
|
||||
".diff": "text/plain",
|
||||
".dll": "application/x-msdownload",
|
||||
".doc": "application/msword",
|
||||
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
".epub": "application/epub+zip",
|
||||
".exe": "application/x-msdownload",
|
||||
".gif": "image/gif",
|
||||
".gz": "application/x-gzip",
|
||||
".h": "text/x-c",
|
||||
".hh": "text/x-c",
|
||||
".htm": "text/html;charset=utf-8",
|
||||
".html": "text/html;charset=utf-8",
|
||||
".ico": "image/x-icon",
|
||||
".ics": "text/calendar",
|
||||
".ifb": "text/calendar",
|
||||
".iso": "application/octet-stream",
|
||||
".jar": "application/java-archive",
|
||||
".java": "text/x-java-source",
|
||||
".jpe": "image/jpeg",
|
||||
".jpeg": "image/jpeg",
|
||||
".jpg": "image/jpeg",
|
||||
".jpgv": "video/jpeg",
|
||||
".js": "application/javascript",
|
||||
".json": "application/json",
|
||||
".latex": "application/x-latex",
|
||||
".list": "text/plain",
|
||||
".log": "text/plain",
|
||||
".chat": "text/plain",
|
||||
".m4a": "audio/mp4",
|
||||
".mid": "audio/midi",
|
||||
".midi": "audio/midi",
|
||||
".mov": "video/quicktime",
|
||||
".mp3": "audio/mpeg",
|
||||
".mp4": "video/mp4",
|
||||
".mp4a": "audio/mp4",
|
||||
".mp4v": "video/mp4",
|
||||
".mpa": "video/mpeg",
|
||||
".mpe": "video/mpeg",
|
||||
".mpeg": "video/mpeg",
|
||||
".mpg": "video/mpeg",
|
||||
".mpg4": "video/mp4",
|
||||
".mpga": "audio/mpeg",
|
||||
".mpkg": "application/vnd.apple.installer+xml",
|
||||
".msi": "application/x-msdownload",
|
||||
|
||||
".nb": "application/mathematica",
|
||||
".nc": "application/x-netcdf",
|
||||
".ncx": "application/x-dtbncx+xml",
|
||||
".ngdat": "application/vnd.nokia.n-gage.data",
|
||||
".nlu": "application/vnd.neurolanguage.nlu",
|
||||
".nml": "application/vnd.enliven",
|
||||
".nnd": "application/vnd.noblenet-directory",
|
||||
".nns": "application/vnd.noblenet-sealer",
|
||||
".nnw": "application/vnd.noblenet-web",
|
||||
".npx": "image/vnd.net-fpx",
|
||||
".nsf": "application/vnd.lotus-notes",
|
||||
".nws": "message/rfc822",
|
||||
".o": "application/octet-stream",
|
||||
".oa2": "application/vnd.fujitsu.oasys2",
|
||||
".oa3": "application/vnd.fujitsu.oasys3",
|
||||
".oas": "application/vnd.fujitsu.oasys",
|
||||
".obd": "application/x-msbinder",
|
||||
".obj": "application/octet-stream",
|
||||
".oda": "application/oda",
|
||||
".odb": "application/vnd.oasis.opendocument.database",
|
||||
".odc": "application/vnd.oasis.opendocument.chart",
|
||||
".odf": "application/vnd.oasis.opendocument.formula",
|
||||
".odft": "application/vnd.oasis.opendocument.formula-template",
|
||||
".odg": "application/vnd.oasis.opendocument.graphics",
|
||||
".odi": "application/vnd.oasis.opendocument.image",
|
||||
".odp": "application/vnd.oasis.opendocument.presentation",
|
||||
".ods": "application/vnd.oasis.opendocument.spreadsheet",
|
||||
".odt": "application/vnd.oasis.opendocument.text",
|
||||
".oga": "audio/ogg",
|
||||
".ogg": "audio/ogg",
|
||||
".ogv": "video/ogg",
|
||||
".ogx": "application/ogg",
|
||||
".onepkg": "application/onenote",
|
||||
".onetmp": "application/onenote",
|
||||
".onetoc": "application/onenote",
|
||||
".onetoc2": "application/onenote",
|
||||
".opf": "application/oebps-package+xml",
|
||||
".oprc": "application/vnd.palm",
|
||||
".org": "application/vnd.lotus-organizer",
|
||||
".osf": "application/vnd.yamaha.openscoreformat",
|
||||
".osfpvg": "application/vnd.yamaha.openscoreformat.osfpvg+xml",
|
||||
".otc": "application/vnd.oasis.opendocument.chart-template",
|
||||
".otf": "application/x-font-otf",
|
||||
".otg": "application/vnd.oasis.opendocument.graphics-template",
|
||||
".oth": "application/vnd.oasis.opendocument.text-web",
|
||||
".oti": "application/vnd.oasis.opendocument.image-template",
|
||||
".otm": "application/vnd.oasis.opendocument.text-master",
|
||||
".otp": "application/vnd.oasis.opendocument.presentation-template",
|
||||
".ots": "application/vnd.oasis.opendocument.spreadsheet-template",
|
||||
".ott": "application/vnd.oasis.opendocument.text-template",
|
||||
".oxt": "application/vnd.openofficeorg.extension",
|
||||
".p": "text/x-pascal",
|
||||
".p10": "application/pkcs10",
|
||||
".p12": "application/x-pkcs12",
|
||||
".p7b": "application/x-pkcs7-certificates",
|
||||
".p7c": "application/pkcs7-mime",
|
||||
".p7m": "application/pkcs7-mime",
|
||||
".p7r": "application/x-pkcs7-certreqresp",
|
||||
".p7s": "application/pkcs7-signature",
|
||||
".pas": "text/x-pascal",
|
||||
".pbd": "application/vnd.powerbuilder6",
|
||||
".pbm": "image/x-portable-bitmap",
|
||||
".pcf": "application/x-font-pcf",
|
||||
".pcl": "application/vnd.hp-pcl",
|
||||
".pclxl": "application/vnd.hp-pclxl",
|
||||
".pct": "image/x-pict",
|
||||
".pcurl": "application/vnd.curl.pcurl",
|
||||
".pcx": "image/x-pcx",
|
||||
".pdb": "application/vnd.palm",
|
||||
".pdf": "application/pdf",
|
||||
".pfa": "application/x-font-type1",
|
||||
".pfb": "application/x-font-type1",
|
||||
".pfm": "application/x-font-type1",
|
||||
".pfr": "application/font-tdpfr",
|
||||
".pfx": "application/x-pkcs12",
|
||||
".pgm": "image/x-portable-graymap",
|
||||
".pgn": "application/x-chess-pgn",
|
||||
".pgp": "application/pgp-encrypted",
|
||||
".pic": "image/x-pict",
|
||||
".pkg": "application/octet-stream",
|
||||
".pki": "application/pkixcmp",
|
||||
".pkipath": "application/pkix-pkipath",
|
||||
".pl": "text/plain",
|
||||
".plb": "application/vnd.3gpp.pic-bw-large",
|
||||
".plc": "application/vnd.mobius.plc",
|
||||
".plf": "application/vnd.pocketlearn",
|
||||
".pls": "application/pls+xml",
|
||||
".pml": "application/vnd.ctc-posml",
|
||||
".png": "image/png",
|
||||
".pnm": "image/x-portable-anymap",
|
||||
".portpkg": "application/vnd.macports.portpkg",
|
||||
".pot": "application/vnd.ms-powerpoint",
|
||||
".potm": "application/vnd.ms-powerpoint.template.macroenabled.12",
|
||||
".potx": "application/vnd.openxmlformats-officedocument.presentationml.template",
|
||||
".ppa": "application/vnd.ms-powerpoint",
|
||||
".ppam": "application/vnd.ms-powerpoint.addin.macroenabled.12",
|
||||
".ppd": "application/vnd.cups-ppd",
|
||||
".ppm": "image/x-portable-pixmap",
|
||||
".pps": "application/vnd.ms-powerpoint",
|
||||
".ppsm": "application/vnd.ms-powerpoint.slideshow.macroenabled.12",
|
||||
".ppsx": "application/vnd.openxmlformats-officedocument.presentationml.slideshow",
|
||||
".ppt": "application/vnd.ms-powerpoint",
|
||||
".pptm": "application/vnd.ms-powerpoint.presentation.macroenabled.12",
|
||||
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
||||
".pqa": "application/vnd.palm",
|
||||
".prc": "application/x-mobipocket-ebook",
|
||||
".pre": "application/vnd.lotus-freelance",
|
||||
".prf": "application/pics-rules",
|
||||
".ps": "application/postscript",
|
||||
".psb": "application/vnd.3gpp.pic-bw-small",
|
||||
".psd": "image/vnd.adobe.photoshop",
|
||||
".psf": "application/x-font-linux-psf",
|
||||
".ptid": "application/vnd.pvi.ptid1",
|
||||
".pub": "application/x-mspublisher",
|
||||
".pvb": "application/vnd.3gpp.pic-bw-var",
|
||||
".pwn": "application/vnd.3m.post-it-notes",
|
||||
".pwz": "application/vnd.ms-powerpoint",
|
||||
".py": "text/x-python",
|
||||
".pya": "audio/vnd.ms-playready.media.pya",
|
||||
".pyc": "application/x-python-code",
|
||||
".pyo": "application/x-python-code",
|
||||
".pyv": "video/vnd.ms-playready.media.pyv",
|
||||
".qam": "application/vnd.epson.quickanime",
|
||||
".qbo": "application/vnd.intu.qbo",
|
||||
".qfx": "application/vnd.intu.qfx",
|
||||
".qps": "application/vnd.publishare-delta-tree",
|
||||
".qt": "video/quicktime",
|
||||
".qwd": "application/vnd.quark.quarkxpress",
|
||||
".qwt": "application/vnd.quark.quarkxpress",
|
||||
".qxb": "application/vnd.quark.quarkxpress",
|
||||
".qxd": "application/vnd.quark.quarkxpress",
|
||||
".qxl": "application/vnd.quark.quarkxpress",
|
||||
".qxt": "application/vnd.quark.quarkxpress",
|
||||
".ra": "audio/x-pn-realaudio",
|
||||
".ram": "audio/x-pn-realaudio",
|
||||
".rar": "application/x-rar-compressed",
|
||||
".ras": "image/x-cmu-raster",
|
||||
".rcprofile": "application/vnd.ipunplugged.rcprofile",
|
||||
".rdf": "application/rdf+xml",
|
||||
".rdz": "application/vnd.data-vision.rdz",
|
||||
".rep": "application/vnd.businessobjects",
|
||||
".res": "application/x-dtbresource+xml",
|
||||
".rgb": "image/x-rgb",
|
||||
".rif": "application/reginfo+xml",
|
||||
".rl": "application/resource-lists+xml",
|
||||
".rlc": "image/vnd.fujixerox.edmics-rlc",
|
||||
".rld": "application/resource-lists-diff+xml",
|
||||
".rm": "application/vnd.rn-realmedia",
|
||||
".rmi": "audio/midi",
|
||||
".rmp": "audio/x-pn-realaudio-plugin",
|
||||
".rms": "application/vnd.jcp.javame.midlet-rms",
|
||||
".rnc": "application/relax-ng-compact-syntax",
|
||||
".roff": "text/troff",
|
||||
".rpm": "application/x-rpm",
|
||||
".rpss": "application/vnd.nokia.radio-presets",
|
||||
".rpst": "application/vnd.nokia.radio-preset",
|
||||
".rq": "application/sparql-query",
|
||||
".rs": "application/rls-services+xml",
|
||||
".rsd": "application/rsd+xml",
|
||||
".rss": "application/rss+xml",
|
||||
".rtf": "application/rtf",
|
||||
".rtx": "text/richtext",
|
||||
".s": "text/x-asm",
|
||||
".saf": "application/vnd.yamaha.smaf-audio",
|
||||
".sbml": "application/sbml+xml",
|
||||
".sc": "application/vnd.ibm.secure-container",
|
||||
".scd": "application/x-msschedule",
|
||||
".scm": "application/vnd.lotus-screencam",
|
||||
".scq": "application/scvp-cv-request",
|
||||
".scs": "application/scvp-cv-response",
|
||||
".scurl": "text/vnd.curl.scurl",
|
||||
".sda": "application/vnd.stardivision.draw",
|
||||
".sdc": "application/vnd.stardivision.calc",
|
||||
".sdd": "application/vnd.stardivision.impress",
|
||||
".sdkd": "application/vnd.solent.sdkm+xml",
|
||||
".sdkm": "application/vnd.solent.sdkm+xml",
|
||||
".sdp": "application/sdp",
|
||||
".sdw": "application/vnd.stardivision.writer",
|
||||
".see": "application/vnd.seemail",
|
||||
".seed": "application/vnd.fdsn.seed",
|
||||
".sema": "application/vnd.sema",
|
||||
".semd": "application/vnd.semd",
|
||||
".semf": "application/vnd.semf",
|
||||
".ser": "application/java-serialized-object",
|
||||
".setpay": "application/set-payment-initiation",
|
||||
".setreg": "application/set-registration-initiation",
|
||||
".sfd-hdstx": "application/vnd.hydrostatix.sof-data",
|
||||
".sfs": "application/vnd.spotfire.sfs",
|
||||
".sgl": "application/vnd.stardivision.writer-global",
|
||||
".sgm": "text/sgml",
|
||||
".sgml": "text/sgml",
|
||||
".sh": "application/x-sh",
|
||||
".shar": "application/x-shar",
|
||||
".shf": "application/shf+xml",
|
||||
".si": "text/vnd.wap.si",
|
||||
".sic": "application/vnd.wap.sic",
|
||||
".sig": "application/pgp-signature",
|
||||
".silo": "model/mesh",
|
||||
".sis": "application/vnd.symbian.install",
|
||||
".sisx": "application/vnd.symbian.install",
|
||||
".sit": "application/x-stuffit",
|
||||
".sitx": "application/x-stuffitx",
|
||||
".skd": "application/vnd.koan",
|
||||
".skm": "application/vnd.koan",
|
||||
".skp": "application/vnd.koan",
|
||||
".skt": "application/vnd.koan",
|
||||
".sl": "text/vnd.wap.sl",
|
||||
".slc": "application/vnd.wap.slc",
|
||||
".sldm": "application/vnd.ms-powerpoint.slide.macroenabled.12",
|
||||
".sldx": "application/vnd.openxmlformats-officedocument.presentationml.slide",
|
||||
".slt": "application/vnd.epson.salt",
|
||||
".smf": "application/vnd.stardivision.math",
|
||||
".smi": "application/smil+xml",
|
||||
".smil": "application/smil+xml",
|
||||
".snd": "audio/basic",
|
||||
".snf": "application/x-font-snf",
|
||||
".so": "application/octet-stream",
|
||||
".spc": "application/x-pkcs7-certificates",
|
||||
".spf": "application/vnd.yamaha.smaf-phrase",
|
||||
".spl": "application/x-futuresplash",
|
||||
".spot": "text/vnd.in3d.spot",
|
||||
".spp": "application/scvp-vp-response",
|
||||
".spq": "application/scvp-vp-request",
|
||||
".spx": "audio/ogg",
|
||||
".src": "application/x-wais-source",
|
||||
".srx": "application/sparql-results+xml",
|
||||
".sse": "application/vnd.kodak-descriptor",
|
||||
".ssf": "application/vnd.epson.ssf",
|
||||
".ssml": "application/ssml+xml",
|
||||
".stc": "application/vnd.sun.xml.calc.template",
|
||||
".std": "application/vnd.sun.xml.draw.template",
|
||||
".stf": "application/vnd.wt.stf",
|
||||
".sti": "application/vnd.sun.xml.impress.template",
|
||||
".stk": "application/hyperstudio",
|
||||
".stl": "application/vnd.ms-pki.stl",
|
||||
".str": "application/vnd.pg.format",
|
||||
".stw": "application/vnd.sun.xml.writer.template",
|
||||
".sus": "application/vnd.sus-calendar",
|
||||
".susp": "application/vnd.sus-calendar",
|
||||
".sv4cpio": "application/x-sv4cpio",
|
||||
".sv4crc": "application/x-sv4crc",
|
||||
".svd": "application/vnd.svd",
|
||||
".svg": "image/svg+xml",
|
||||
".svgz": "image/svg+xml",
|
||||
".swa": "application/x-director",
|
||||
".swf": "application/x-shockwave-flash",
|
||||
".swi": "application/vnd.arastra.swi",
|
||||
".sxc": "application/vnd.sun.xml.calc",
|
||||
".sxd": "application/vnd.sun.xml.draw",
|
||||
".sxg": "application/vnd.sun.xml.writer.global",
|
||||
".sxi": "application/vnd.sun.xml.impress",
|
||||
".sxm": "application/vnd.sun.xml.math",
|
||||
".sxw": "application/vnd.sun.xml.writer",
|
||||
".t": "text/troff",
|
||||
".tao": "application/vnd.tao.intent-module-archive",
|
||||
".tar": "application/x-tar",
|
||||
".tcap": "application/vnd.3gpp2.tcap",
|
||||
".tcl": "application/x-tcl",
|
||||
".teacher": "application/vnd.smart.teacher",
|
||||
".tex": "application/x-tex",
|
||||
".texi": "application/x-texinfo",
|
||||
".texinfo": "application/x-texinfo",
|
||||
".text": "text/plain",
|
||||
".tfm": "application/x-tex-tfm",
|
||||
".tgz": "application/x-gzip",
|
||||
".tif": "image/tiff",
|
||||
".tiff": "image/tiff",
|
||||
".tmo": "application/vnd.tmobile-livetv",
|
||||
".torrent": "application/x-bittorrent",
|
||||
".tpl": "application/vnd.groove-tool-template",
|
||||
".tpt": "application/vnd.trid.tpt",
|
||||
".tr": "text/troff",
|
||||
".tra": "application/vnd.trueapp",
|
||||
".trm": "application/x-msterminal",
|
||||
".tsv": "text/tab-separated-values",
|
||||
".ttc": "application/x-font-ttf",
|
||||
".ttf": "application/x-font-ttf",
|
||||
".twd": "application/vnd.simtech-mindmapper",
|
||||
".twds": "application/vnd.simtech-mindmapper",
|
||||
".txd": "application/vnd.genomatix.tuxedo",
|
||||
".txf": "application/vnd.mobius.txf",
|
||||
".txt": "text/plain",
|
||||
".u32": "application/x-authorware-bin",
|
||||
".udeb": "application/x-debian-package",
|
||||
".ufd": "application/vnd.ufdl",
|
||||
".ufdl": "application/vnd.ufdl",
|
||||
".umj": "application/vnd.umajin",
|
||||
".unityweb": "application/vnd.unity",
|
||||
".uoml": "application/vnd.uoml+xml",
|
||||
".uri": "text/uri-list",
|
||||
".uris": "text/uri-list",
|
||||
".urls": "text/uri-list",
|
||||
".ustar": "application/x-ustar",
|
||||
".utz": "application/vnd.uiq.theme",
|
||||
".uu": "text/x-uuencode",
|
||||
".vcd": "application/x-cdlink",
|
||||
".vcf": "text/x-vcard",
|
||||
".vcg": "application/vnd.groove-vcard",
|
||||
".vcs": "text/x-vcalendar",
|
||||
".vcx": "application/vnd.vcx",
|
||||
".vis": "application/vnd.visionary",
|
||||
".viv": "video/vnd.vivo",
|
||||
".vor": "application/vnd.stardivision.writer",
|
||||
".vox": "application/x-authorware-bin",
|
||||
".vrml": "model/vrml",
|
||||
".vsd": "application/vnd.visio",
|
||||
".vsf": "application/vnd.vsf",
|
||||
".vss": "application/vnd.visio",
|
||||
".vst": "application/vnd.visio",
|
||||
".vsw": "application/vnd.visio",
|
||||
".vtu": "model/vnd.vtu",
|
||||
".vxml": "application/voicexml+xml",
|
||||
".w3d": "application/x-director",
|
||||
".wad": "application/x-doom",
|
||||
".wav": "audio/x-wav",
|
||||
".wax": "audio/x-ms-wax",
|
||||
".wbmp": "image/vnd.wap.wbmp",
|
||||
".wbs": "application/vnd.criticaltools.wbs+xml",
|
||||
".wbxml": "application/vnd.wap.wbxml",
|
||||
".wcm": "application/vnd.ms-works",
|
||||
".wdb": "application/vnd.ms-works",
|
||||
".wiz": "application/msword",
|
||||
".wks": "application/vnd.ms-works",
|
||||
".wm": "video/x-ms-wm",
|
||||
".wma": "audio/x-ms-wma",
|
||||
".wmd": "application/x-ms-wmd",
|
||||
".wmf": "application/x-msmetafile",
|
||||
".wml": "text/vnd.wap.wml",
|
||||
".wmlc": "application/vnd.wap.wmlc",
|
||||
".wmls": "text/vnd.wap.wmlscript",
|
||||
".wmlsc": "application/vnd.wap.wmlscriptc",
|
||||
".wmv": "video/x-ms-wmv",
|
||||
".wmx": "video/x-ms-wmx",
|
||||
".wmz": "application/x-ms-wmz",
|
||||
".wpd": "application/vnd.wordperfect",
|
||||
".wpl": "application/vnd.ms-wpl",
|
||||
".wps": "application/vnd.ms-works",
|
||||
".wqd": "application/vnd.wqd",
|
||||
".wri": "application/x-mswrite",
|
||||
".wrl": "model/vrml",
|
||||
".wsdl": "application/wsdl+xml",
|
||||
".wspolicy": "application/wspolicy+xml",
|
||||
".wtb": "application/vnd.webturbo",
|
||||
".wvx": "video/x-ms-wvx",
|
||||
".x32": "application/x-authorware-bin",
|
||||
".x3d": "application/vnd.hzn-3d-crossword",
|
||||
".xap": "application/x-silverlight-app",
|
||||
".xar": "application/vnd.xara",
|
||||
".xbap": "application/x-ms-xbap",
|
||||
".xbd": "application/vnd.fujixerox.docuworks.binder",
|
||||
".xbm": "image/x-xbitmap",
|
||||
".xdm": "application/vnd.syncml.dm+xml",
|
||||
".xdp": "application/vnd.adobe.xdp+xml",
|
||||
".xdw": "application/vnd.fujixerox.docuworks",
|
||||
".xenc": "application/xenc+xml",
|
||||
".xer": "application/patch-ops-error+xml",
|
||||
".xfdf": "application/vnd.adobe.xfdf",
|
||||
".xfdl": "application/vnd.xfdl",
|
||||
".xht": "application/xhtml+xml",
|
||||
".xhtml": "application/xhtml+xml",
|
||||
".xhvml": "application/xv+xml",
|
||||
".xif": "image/vnd.xiff",
|
||||
".xla": "application/vnd.ms-excel",
|
||||
".xlam": "application/vnd.ms-excel.addin.macroenabled.12",
|
||||
".xlb": "application/vnd.ms-excel",
|
||||
".xlc": "application/vnd.ms-excel",
|
||||
".xlm": "application/vnd.ms-excel",
|
||||
".xls": "application/vnd.ms-excel",
|
||||
".xlsb": "application/vnd.ms-excel.sheet.binary.macroenabled.12",
|
||||
".xlsm": "application/vnd.ms-excel.sheet.macroenabled.12",
|
||||
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
".xlt": "application/vnd.ms-excel",
|
||||
".xltm": "application/vnd.ms-excel.template.macroenabled.12",
|
||||
".xltx": "application/vnd.openxmlformats-officedocument.spreadsheetml.template",
|
||||
".xlw": "application/vnd.ms-excel",
|
||||
".xml": "application/xml",
|
||||
".xo": "application/vnd.olpc-sugar",
|
||||
".xop": "application/xop+xml",
|
||||
".xpdl": "application/xml",
|
||||
".xpi": "application/x-xpinstall",
|
||||
".xpm": "image/x-xpixmap",
|
||||
".xpr": "application/vnd.is-xpr",
|
||||
".xps": "application/vnd.ms-xpsdocument",
|
||||
".xpw": "application/vnd.intercon.formnet",
|
||||
".xpx": "application/vnd.intercon.formnet",
|
||||
".xsl": "application/xml",
|
||||
".xslt": "application/xslt+xml",
|
||||
".xsm": "application/vnd.syncml+xml",
|
||||
".xspf": "application/xspf+xml",
|
||||
".xul": "application/vnd.mozilla.xul+xml",
|
||||
".xvm": "application/xv+xml",
|
||||
".xvml": "application/xv+xml",
|
||||
".xwd": "image/x-xwindowdump",
|
||||
".xyz": "chemical/x-xyz",
|
||||
".zaz": "application/vnd.zzazz.deck+xml",
|
||||
".zip": "application/zip",
|
||||
".zir": "application/vnd.zul",
|
||||
".zirz": "application/vnd.zul",
|
||||
".zmm": "application/vnd.handheld-entertainment+xml"
|
||||
}
|
55
src/file-viewer/models/BlobAsset.ts
Normal file
55
src/file-viewer/models/BlobAsset.ts
Normal file
|
@ -0,0 +1,55 @@
|
|||
const db = getDb("cache.sqlite");
|
||||
db.table(
|
||||
"blob_assets",
|
||||
/* SQL */ `
|
||||
create table blob_assets (
|
||||
hash text primary key,
|
||||
refs integer not null default 0
|
||||
);
|
||||
`,
|
||||
);
|
||||
/**
|
||||
* Uncompressed files are read directly from the media store root. Compressed
|
||||
* files are stored as `<compress store>/<first 2 chars of hash>/<hash>` Since
|
||||
* multiple files can share the same hash, the number of references is tracked
|
||||
* so that when a file is deleted, the compressed data is only removed when all
|
||||
* references are gone.
|
||||
*/
|
||||
export class BlobAsset {
|
||||
/** sha1 of the contents */
|
||||
hash!: string;
|
||||
refs!: number;
|
||||
}
|
||||
|
||||
export namespace BlobAsset {
|
||||
const getQuery = cache.prepare(/* SQL */ `
|
||||
SELECT * FROM blob_assets WHERE hash = ?;
|
||||
`).as(BlobAsset);
|
||||
export function get(hash: string) {
|
||||
return getQuery.get(hash);
|
||||
}
|
||||
|
||||
const putOrIncrementQuery = cache.prepare(/* SQL */ `
|
||||
INSERT INTO blob_assets (hash, refs) VALUES (?, 1)
|
||||
ON CONFLICT(hash) DO UPDATE SET refs = refs + 1;
|
||||
`);
|
||||
export function putOrIncrement(hash: string) {
|
||||
assert(hash.length === 40);
|
||||
putOrIncrementQuery.get(hash);
|
||||
return get(hash)!;
|
||||
}
|
||||
|
||||
const decrementQuery = cache.prepare(/* SQL */ `
|
||||
UPDATE blob_assets SET refs = refs - 1 WHERE hash = ? AND refs > 0;
|
||||
`);
|
||||
const deleteQuery = cache.prepare(/* SQL */ `
|
||||
DELETE FROM blob_assets WHERE hash = ? AND refs <= 0;
|
||||
`);
|
||||
export function decrementOrDelete(hash: string) {
|
||||
assert(hash.length === 40);
|
||||
decrementQuery.run(hash);
|
||||
return deleteQuery.run(hash).changes > 0;
|
||||
}
|
||||
}
|
||||
|
||||
import { getDb } from "#sqlite";
|
0
src/file-viewer/models/FilePermissions.ts
Normal file
0
src/file-viewer/models/FilePermissions.ts
Normal file
51
src/file-viewer/models/MediaFile.ts
Normal file
51
src/file-viewer/models/MediaFile.ts
Normal file
|
@ -0,0 +1,51 @@
|
|||
import { getDb } from "#database";
|
||||
|
||||
const db = getDb("cache.sqlite");
|
||||
|
||||
db.table(/* SQL */ `
|
||||
CREATE TABLE IF NOT EXISTS permissions (
|
||||
prefix TEXT PRIMARY KEY,
|
||||
allow INTEGER NOT NULL
|
||||
);
|
||||
`);
|
||||
|
||||
export class FilePermissions {
|
||||
prefix!: string;
|
||||
/** Currently set to 1 always */
|
||||
allow!: number;
|
||||
|
||||
// -- static ops --
|
||||
|
||||
static getByPrefix(filePath: string): number {
|
||||
return (getByPrefixQuery.get(filePath))?.allow ?? 0;
|
||||
}
|
||||
static getExact(filePath: string): number {
|
||||
return (getExactQuery.get(filePath))?.allow ?? 0;
|
||||
}
|
||||
static setPermissions(dirPath: string, level: number) {
|
||||
if (level) {
|
||||
insertQuery.run(dirPath, level);
|
||||
} else {
|
||||
deleteQuery.run(dirPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const getByPrefixQuery = db.prepare(/* SQL */ `
|
||||
SELECT allow
|
||||
FROM permissions
|
||||
WHERE ? GLOB prefix || '*'
|
||||
ORDER BY LENGTH(prefix) DESC
|
||||
LIMIT 1;
|
||||
`).type<FilePermissions>();
|
||||
|
||||
const getExactQuery = db.prepare(/* SQL */ `
|
||||
SELECT allow FROM permissions WHERE ? == prefix
|
||||
`).type<FilePermissions>();
|
||||
|
||||
const insertQuery = db.prepare(/* SQL */ `
|
||||
REPLACE INTO permissions(prefix, allow) VALUES(?, ?);
|
||||
`);
|
||||
const deleteQuery = db.prepare(/* SQL */ `
|
||||
DELETE FROM permissions WHERE prefix = ?;
|
||||
`);
|
27
src/file-viewer/pages.off/file.cotyledon_enterance.tsx
Normal file
27
src/file-viewer/pages.off/file.cotyledon_enterance.tsx
Normal file
|
@ -0,0 +1,27 @@
|
|||
import { MediaFile } from "../db";
|
||||
import { useInlineScript } from "../framework/page-resources";
|
||||
import { Readme } from "../media/cotyledon";
|
||||
import { MediaPanel } from "../pages-dynamic/file_viewer";
|
||||
import "../media/files.css";
|
||||
|
||||
export const theme = {
|
||||
bg: "#312652",
|
||||
fg: "#f0f0ff",
|
||||
primary: "#fabe32",
|
||||
};
|
||||
|
||||
export default function CotyledonPage() {
|
||||
useInlineScript("canvas_cotyledon");
|
||||
useInlineScript("file_viewer");
|
||||
return (
|
||||
<div class="files ctld ctld-et">
|
||||
<MediaPanel
|
||||
file={MediaFile.getByPath("/")!}
|
||||
isLast={false}
|
||||
activeFilename={null}
|
||||
hasCotyledonCookie
|
||||
/>
|
||||
<Readme />
|
||||
</div>
|
||||
);
|
||||
}
|
43
src/file-viewer/pages.off/file.cotyledon_speedbump.tsx
Normal file
43
src/file-viewer/pages.off/file.cotyledon_speedbump.tsx
Normal file
|
@ -0,0 +1,43 @@
|
|||
// relativeImportPath
|
||||
function relativeImportPath(from, to) {
|
||||
let i = 0;
|
||||
let sepPos = -1;
|
||||
let prevSepPos = -1;
|
||||
let prevPrevSepPos = -1;
|
||||
const fromLen = from.length;
|
||||
const commonLen = Math.min(to.length, fromLen);
|
||||
for (; i < commonLen; i++) {
|
||||
const curChar = to[i];
|
||||
if (curChar !== from[i])
|
||||
break;
|
||||
if (curChar === import_path.sep) {
|
||||
prevPrevSepPos = prevSepPos;
|
||||
prevSepPos = sepPos;
|
||||
sepPos = i;
|
||||
}
|
||||
}
|
||||
if (sepPos !== -1) {
|
||||
if (hasNms(to, sepPos)) {
|
||||
return toPosix(stripNms(to, sepPos));
|
||||
}
|
||||
if (prevSepPos !== -1) {
|
||||
if (prevPrevSepPos !== -1 && to[prevSepPos + 1] === "@") {
|
||||
prevSepPos = prevPrevSepPos;
|
||||
}
|
||||
if (hasNms(to, prevSepPos)) {
|
||||
return toPosix(stripNms(to, prevSepPos));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sepPos <= 0)
|
||||
return toPosix(to);
|
||||
let back = 0;
|
||||
for (; i < fromLen; i++)
|
||||
if (from[i] === import_path.sep)
|
||||
back++;
|
||||
if (back) {
|
||||
return backSep.repeat(back) + toPosix(to.slice(sepPos + 1));
|
||||
} else {
|
||||
return `.${toPosix(to.slice(sepPos))}`;
|
||||
}
|
||||
}
|
284
src/file-viewer/redirects.ts
Normal file
284
src/file-viewer/redirects.ts
Normal file
|
@ -0,0 +1,284 @@
|
|||
// ImportInjector
|
||||
class ImportInjector {
|
||||
constructor(path, importedSource, opts) {
|
||||
this._defaultOpts = {
|
||||
importedSource: null,
|
||||
importedType: "commonjs",
|
||||
importedInterop: "babel",
|
||||
importingInterop: "babel",
|
||||
ensureLiveReference: false,
|
||||
ensureNoContext: false,
|
||||
importPosition: "before"
|
||||
};
|
||||
const programPath = path.find(p => p.isProgram());
|
||||
this._programPath = programPath;
|
||||
this._programScope = programPath.scope;
|
||||
this._hub = programPath.hub;
|
||||
this._defaultOpts = this._applyDefaults(importedSource, opts, true);
|
||||
}
|
||||
addDefault(importedSourceIn, opts) {
|
||||
return this.addNamed("default", importedSourceIn, opts);
|
||||
}
|
||||
addNamed(importName, importedSourceIn, opts) {
|
||||
_assert(typeof importName === "string");
|
||||
return this._generateImport(this._applyDefaults(importedSourceIn, opts), importName);
|
||||
}
|
||||
addNamespace(importedSourceIn, opts) {
|
||||
return this._generateImport(this._applyDefaults(importedSourceIn, opts), null);
|
||||
}
|
||||
addSideEffect(importedSourceIn, opts) {
|
||||
return this._generateImport(this._applyDefaults(importedSourceIn, opts), void 0);
|
||||
}
|
||||
_applyDefaults(importedSource, opts, isInit = false) {
|
||||
let newOpts;
|
||||
if (typeof importedSource === "string") {
|
||||
newOpts = Object.assign({}, this._defaultOpts, {
|
||||
importedSource
|
||||
}, opts);
|
||||
} else {
|
||||
_assert(!opts, "Unexpected secondary arguments.");
|
||||
newOpts = Object.assign({}, this._defaultOpts, importedSource);
|
||||
}
|
||||
if (!isInit && opts) {
|
||||
if (opts.nameHint !== undefined) newOpts.nameHint = opts.nameHint;
|
||||
if (opts.blockHoist !== undefined) newOpts.blockHoist = opts.blockHoist;
|
||||
}
|
||||
return newOpts;
|
||||
}
|
||||
_generateImport(opts, importName) {
|
||||
const isDefault = importName === "default";
|
||||
const isNamed = !!importName && !isDefault;
|
||||
const isNamespace = importName === null;
|
||||
const {
|
||||
importedSource,
|
||||
importedType,
|
||||
importedInterop,
|
||||
importingInterop,
|
||||
ensureLiveReference,
|
||||
ensureNoContext,
|
||||
nameHint,
|
||||
importPosition,
|
||||
blockHoist
|
||||
} = opts;
|
||||
let name = nameHint || importName;
|
||||
const isMod = (0, _isModule.default)(this._programPath);
|
||||
const isModuleForNode = isMod && importingInterop === "node";
|
||||
const isModuleForBabel = isMod && importingInterop === "babel";
|
||||
if (importPosition === "after" && !isMod) {
|
||||
throw new Error(`"importPosition": "after" is only supported in modules`);
|
||||
}
|
||||
const builder = new _importBuilder.default(importedSource, this._programScope, this._hub);
|
||||
if (importedType === "es6") {
|
||||
if (!isModuleForNode && !isModuleForBabel) {
|
||||
throw new Error("Cannot import an ES6 module from CommonJS");
|
||||
}
|
||||
builder.import();
|
||||
if (isNamespace) {
|
||||
builder.namespace(nameHint || importedSource);
|
||||
} else if (isDefault || isNamed) {
|
||||
builder.named(name, importName);
|
||||
}
|
||||
} else if (importedType !== "commonjs") {
|
||||
throw new Error(`Unexpected interopType "${importedType}"`);
|
||||
} else if (importedInterop === "babel") {
|
||||
if (isModuleForNode) {
|
||||
name = name !== "default" ? name : importedSource;
|
||||
const es6Default = `${importedSource}$es6Default`;
|
||||
builder.import();
|
||||
if (isNamespace) {
|
||||
builder.default(es6Default).var(name || importedSource).wildcardInterop();
|
||||
} else if (isDefault) {
|
||||
if (ensureLiveReference) {
|
||||
builder.default(es6Default).var(name || importedSource).defaultInterop().read("default");
|
||||
} else {
|
||||
builder.default(es6Default).var(name).defaultInterop().prop(importName);
|
||||
}
|
||||
} else if (isNamed) {
|
||||
builder.default(es6Default).read(importName);
|
||||
}
|
||||
} else if (isModuleForBabel) {
|
||||
builder.import();
|
||||
if (isNamespace) {
|
||||
builder.namespace(name || importedSource);
|
||||
} else if (isDefault || isNamed) {
|
||||
builder.named(name, importName);
|
||||
}
|
||||
} else {
|
||||
builder.require();
|
||||
if (isNamespace) {
|
||||
builder.var(name || importedSource).wildcardInterop();
|
||||
} else if ((isDefault || isNamed) && ensureLiveReference) {
|
||||
if (isDefault) {
|
||||
name = name !== "default" ? name : importedSource;
|
||||
builder.var(name).read(importName);
|
||||
builder.defaultInterop();
|
||||
} else {
|
||||
builder.var(importedSource).read(importName);
|
||||
}
|
||||
} else if (isDefault) {
|
||||
builder.var(name).defaultInterop().prop(importName);
|
||||
} else if (isNamed) {
|
||||
builder.var(name).prop(importName);
|
||||
}
|
||||
}
|
||||
} else if (importedInterop === "compiled") {
|
||||
if (isModuleForNode) {
|
||||
builder.import();
|
||||
if (isNamespace) {
|
||||
builder.default(name || importedSource);
|
||||
} else if (isDefault || isNamed) {
|
||||
builder.default(importedSource).read(name);
|
||||
}
|
||||
} else if (isModuleForBabel) {
|
||||
builder.import();
|
||||
if (isNamespace) {
|
||||
builder.namespace(name || importedSource);
|
||||
} else if (isDefault || isNamed) {
|
||||
builder.named(name, importName);
|
||||
}
|
||||
} else {
|
||||
builder.require();
|
||||
if (isNamespace) {
|
||||
builder.var(name || importedSource);
|
||||
} else if (isDefault || isNamed) {
|
||||
if (ensureLiveReference) {
|
||||
builder.var(importedSource).read(name);
|
||||
} else {
|
||||
builder.prop(importName).var(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (importedInterop === "uncompiled") {
|
||||
if (isDefault && ensureLiveReference) {
|
||||
throw new Error("No live reference for commonjs default");
|
||||
}
|
||||
if (isModuleForNode) {
|
||||
builder.import();
|
||||
if (isNamespace) {
|
||||
builder.default(name || importedSource);
|
||||
} else if (isDefault) {
|
||||
builder.default(name);
|
||||
} else if (isNamed) {
|
||||
builder.default(importedSource).read(name);
|
||||
}
|
||||
} else if (isModuleForBabel) {
|
||||
builder.import();
|
||||
if (isNamespace) {
|
||||
builder.default(name || importedSource);
|
||||
} else if (isDefault) {
|
||||
builder.default(name);
|
||||
} else if (isNamed) {
|
||||
builder.named(name, importName);
|
||||
}
|
||||
} else {
|
||||
builder.require();
|
||||
if (isNamespace) {
|
||||
builder.var(name || importedSource);
|
||||
} else if (isDefault) {
|
||||
builder.var(name);
|
||||
} else if (isNamed) {
|
||||
if (ensureLiveReference) {
|
||||
builder.var(importedSource).read(name);
|
||||
} else {
|
||||
builder.var(name).prop(importName);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new Error(`Unknown importedInterop "${importedInterop}".`);
|
||||
}
|
||||
const {
|
||||
statements,
|
||||
resultName
|
||||
} = builder.done();
|
||||
this._insertStatements(statements, importPosition, blockHoist);
|
||||
if ((isDefault || isNamed) && ensureNoContext && resultName.type !== "Identifier") {
|
||||
return sequenceExpression([numericLiteral(0), resultName]);
|
||||
}
|
||||
return resultName;
|
||||
}
|
||||
_insertStatements(statements, importPosition = "before", blockHoist = 3) {
|
||||
if (importPosition === "after") {
|
||||
if (this._insertStatementsAfter(statements)) return;
|
||||
} else {
|
||||
if (this._insertStatementsBefore(statements, blockHoist)) return;
|
||||
}
|
||||
this._programPath.unshiftContainer("body", statements);
|
||||
}
|
||||
_insertStatementsBefore(statements, blockHoist) {
|
||||
if (statements.length === 1 && isImportDeclaration(statements[0]) && isValueImport(statements[0])) {
|
||||
const firstImportDecl = this._programPath.get("body").find(p => {
|
||||
return p.isImportDeclaration() && isValueImport(p.node);
|
||||
});
|
||||
if ((firstImportDecl == null ? void 0 : firstImportDecl.node.source.value) === statements[0].source.value && maybeAppendImportSpecifiers(firstImportDecl.node, statements[0])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
statements.forEach(node => {
|
||||
node._blockHoist = blockHoist;
|
||||
});
|
||||
const targetPath = this._programPath.get("body").find(p => {
|
||||
const val = p.node._blockHoist;
|
||||
return Number.isFinite(val) && val < 4;
|
||||
});
|
||||
if (targetPath) {
|
||||
targetPath.insertBefore(statements);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
_insertStatementsAfter(statements) {
|
||||
const statementsSet = new Set(statements);
|
||||
const importDeclarations = new Map();
|
||||
for (const statement of statements) {
|
||||
if (isImportDeclaration(statement) && isValueImport(statement)) {
|
||||
const source = statement.source.value;
|
||||
if (!importDeclarations.has(source)) importDeclarations.set(source, []);
|
||||
importDeclarations.get(source).push(statement);
|
||||
}
|
||||
}
|
||||
let lastImportPath = null;
|
||||
for (const bodyStmt of this._programPath.get("body")) {
|
||||
if (bodyStmt.isImportDeclaration() && isValueImport(bodyStmt.node)) {
|
||||
lastImportPath = bodyStmt;
|
||||
const source = bodyStmt.node.source.value;
|
||||
const newImports = importDeclarations.get(source);
|
||||
if (!newImports) continue;
|
||||
for (const decl of newImports) {
|
||||
if (!statementsSet.has(decl)) continue;
|
||||
if (maybeAppendImportSpecifiers(bodyStmt.node, decl)) {
|
||||
statementsSet.delete(decl);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (statementsSet.size === 0) return true;
|
||||
if (lastImportPath) lastImportPath.insertAfter(Array.from(statementsSet));
|
||||
return !!lastImportPath;
|
||||
}
|
||||
}
|
||||
|
||||
// addDefault
|
||||
function addDefault(path, importedSource, opts) {
|
||||
return new _importInjector.default(path).addDefault(importedSource, opts);
|
||||
}
|
||||
|
||||
// addNamed
|
||||
function addNamed(path, name, importedSource, opts) {
|
||||
return new _importInjector.default(path).addNamed(name, importedSource, opts);
|
||||
}
|
||||
|
||||
// addNamespace
|
||||
function addNamespace(path, importedSource, opts) {
|
||||
return new _importInjector.default(path).addNamespace(importedSource, opts);
|
||||
}
|
||||
|
||||
// addSideEffect
|
||||
function addSideEffect(path, importedSource, opts) {
|
||||
return new _importInjector.default(path).addSideEffect(importedSource, opts);
|
||||
}
|
||||
|
||||
// isModule
|
||||
function isModule(path) {
|
||||
return path.node.sourceType === "module";
|
||||
}
|
1008
src/file-viewer/scan.ts
Normal file
1008
src/file-viewer/scan.ts
Normal file
File diff suppressed because it is too large
Load diff
233
src/file-viewer/scripts/canvas_2017.client.ts
Normal file
233
src/file-viewer/scripts/canvas_2017.client.ts
Normal file
|
@ -0,0 +1,233 @@
|
|||
// Vibe coded with AI
|
||||
(globalThis as any).canvas_2017 = function (canvas: HTMLCanvasElement) {
|
||||
const isStandalone = canvas.getAttribute("data-standalone") === "true";
|
||||
// Configuration interface for the checkerboard effect
|
||||
interface CheckerboardConfig {
|
||||
fps: number; // frames per second
|
||||
color1: string; // first checkerboard color
|
||||
color2: string; // second checkerboard color
|
||||
opacity: number; // opacity of each checkerboard (0-1)
|
||||
speedX1: number; // horizontal speed of first checkerboard (pixels per second)
|
||||
speedY1: number; // vertical speed of first checkerboard (pixels per second)
|
||||
speedX2: number; // horizontal speed of second checkerboard (pixels per second)
|
||||
speedY2: number; // vertical speed of second checkerboard (pixels per second)
|
||||
baseTileSize: number; // base size of checkerboard tiles
|
||||
sizeVariation: number; // maximum variation in tile size (pixels)
|
||||
sineFrequency1: number; // frequency of first sine wave for size variation
|
||||
sineFrequency2: number; // frequency of second sine wave for size variation
|
||||
sineOffset: number; // offset between the two sine waves (radians)
|
||||
rotation: number; // rotation in degrees for the entire pattern
|
||||
rotation2: number; // rotation in degrees for the entire pattern
|
||||
}
|
||||
|
||||
// Default configuration
|
||||
const config: CheckerboardConfig = {
|
||||
fps: 30,
|
||||
color1: "#1A1C17",
|
||||
color2: "#1A1C17",
|
||||
opacity: 0.3,
|
||||
speedX1: -0.02, // moving left slowly
|
||||
speedY1: -0.01, // moving up slowly
|
||||
speedX2: -0.015, // moving left (slightly slower)
|
||||
speedY2: 0.012, // moving down slowly
|
||||
baseTileSize: 200,
|
||||
sizeVariation: 1.5,
|
||||
sineFrequency1: 0.0005,
|
||||
sineFrequency2: 0.0008,
|
||||
sineOffset: Math.PI / 2, // 90 degrees offset
|
||||
rotation: 2, // 5 degree rotation
|
||||
rotation2: -2, // 5 degree rotation
|
||||
};
|
||||
|
||||
// Get the canvas context
|
||||
const ctx = canvas.getContext("2d");
|
||||
if (!ctx) {
|
||||
console.error("Could not get canvas context");
|
||||
return () => {};
|
||||
}
|
||||
|
||||
// Make canvas transparent
|
||||
if (isStandalone) {
|
||||
canvas.style.backgroundColor = "#737D60";
|
||||
} else {
|
||||
canvas.style.backgroundColor = "transparent";
|
||||
}
|
||||
|
||||
// Variables to track position and animation
|
||||
let width = canvas.width;
|
||||
let height = canvas.height;
|
||||
let animationFrameId: number;
|
||||
let lastFrameTime = 0;
|
||||
const frameInterval = 1000 / config.fps;
|
||||
|
||||
// Position offsets for the two checkerboards (centered)
|
||||
let offset1X = 0;
|
||||
let offset1Y = 0;
|
||||
let offset2X = 0;
|
||||
let offset2Y = 0;
|
||||
|
||||
// Time variable for sine wave calculation
|
||||
let time = 0;
|
||||
|
||||
// Convert rotation to radians
|
||||
const rotationRad = (config.rotation * Math.PI) / 180;
|
||||
const rotationRad2 = (config.rotation2 * Math.PI) / 180;
|
||||
|
||||
// Update canvas dimensions when resized
|
||||
const updateDimensions = () => {
|
||||
width = canvas.width = canvas.clientWidth;
|
||||
height = canvas.height = canvas.clientHeight;
|
||||
};
|
||||
|
||||
// Calculate the diagonal length of the canvas (to ensure rotation covers corners)
|
||||
const calculateDiagonal = () => {
|
||||
return Math.sqrt(width * width + height * height);
|
||||
};
|
||||
|
||||
// Draw a single checkerboard pattern scaled from center with rotation
|
||||
const drawCheckerboard = (
|
||||
offsetX: number,
|
||||
offsetY: number,
|
||||
tileSize: number,
|
||||
color1: string,
|
||||
color2: string,
|
||||
opacity: number,
|
||||
rotationRad: number,
|
||||
) => {
|
||||
ctx.globalAlpha = opacity;
|
||||
|
||||
// Get the center of the viewport
|
||||
const centerX = width / 2;
|
||||
const centerY = height / 2;
|
||||
|
||||
// Save the current transformation state
|
||||
ctx.save();
|
||||
|
||||
// Move to the center of the canvas, rotate, then move back
|
||||
ctx.translate(centerX, centerY);
|
||||
ctx.rotate(rotationRad);
|
||||
|
||||
// Calculate the number of tiles needed to cover the rotated canvas
|
||||
// We need to use the diagonal length to ensure we cover the corners when rotated
|
||||
const diagonal = calculateDiagonal();
|
||||
const tilesX = Math.ceil(diagonal / tileSize) + 6; // Added extra tiles for rotation
|
||||
const tilesY = Math.ceil(diagonal / tileSize) + 6;
|
||||
|
||||
// Calculate how many tiles fit from center to edge (in each direction)
|
||||
const halfTilesX = Math.ceil(tilesX / 2);
|
||||
const halfTilesY = Math.ceil(tilesY / 2);
|
||||
|
||||
// Adjust the offset to be relative to the center
|
||||
// The modulo ensures the pattern repeats smoothly even with scaling
|
||||
const adjustedOffsetX = offsetX % (tileSize * 2);
|
||||
const adjustedOffsetY = offsetY % (tileSize * 2);
|
||||
|
||||
// Draw the checker pattern, centered on the viewport
|
||||
for (let y = -halfTilesY; y <= halfTilesY; y++) {
|
||||
for (let x = -halfTilesX; x <= halfTilesX; x++) {
|
||||
// Determine if this tile should be colored (creating checker pattern)
|
||||
// We add a large number to ensure (x+y) is always positive for the modulo
|
||||
if ((x + y + 1000) % 2 === 0) {
|
||||
ctx.fillStyle = color1;
|
||||
} else {
|
||||
ctx.fillStyle = color2;
|
||||
}
|
||||
|
||||
// Calculate the position of this tile relative to the center
|
||||
// The adjusted offset creates the movement effect
|
||||
const posX = (x * tileSize) + adjustedOffsetX;
|
||||
const posY = (y * tileSize) + adjustedOffsetY;
|
||||
|
||||
// Draw the tile
|
||||
ctx.fillRect(
|
||||
posX - tileSize / 2,
|
||||
posY - tileSize / 2,
|
||||
tileSize,
|
||||
tileSize,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Restore the transformation state
|
||||
ctx.restore();
|
||||
};
|
||||
|
||||
// Animation loop
|
||||
const animate = (currentTime: number) => {
|
||||
animationFrameId = requestAnimationFrame(animate);
|
||||
|
||||
// Control frame rate
|
||||
if (currentTime - lastFrameTime < frameInterval) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate the time elapsed since the last frame
|
||||
const dt = currentTime - lastFrameTime;
|
||||
lastFrameTime = currentTime;
|
||||
|
||||
// Increment time for sine wave calculation
|
||||
time += dt;
|
||||
|
||||
// Update the position offsets based on speed and elapsed time
|
||||
offset1X += config.speedX1 * dt;
|
||||
offset1Y += config.speedY1 * dt;
|
||||
offset2X += config.speedX2 * dt;
|
||||
offset2Y += config.speedY2 * dt;
|
||||
|
||||
// Calculate the tile sizes using sine waves
|
||||
const tileSize1 = config.baseTileSize +
|
||||
Math.sin(time * config.sineFrequency1) * config.sizeVariation;
|
||||
const tileSize2 = config.baseTileSize +
|
||||
Math.sin(time * config.sineFrequency2 + config.sineOffset) *
|
||||
config.sizeVariation;
|
||||
|
||||
// Clear canvas
|
||||
ctx.clearRect(0, 0, width, height);
|
||||
|
||||
// Draw the two checkerboards
|
||||
drawCheckerboard(
|
||||
offset1X,
|
||||
offset1Y,
|
||||
tileSize1,
|
||||
config.color1,
|
||||
"transparent",
|
||||
config.opacity,
|
||||
rotationRad,
|
||||
);
|
||||
|
||||
drawCheckerboard(
|
||||
offset2X,
|
||||
offset2Y,
|
||||
tileSize2,
|
||||
config.color2,
|
||||
"transparent",
|
||||
config.opacity,
|
||||
rotationRad2,
|
||||
);
|
||||
|
||||
// Reset global alpha
|
||||
ctx.globalAlpha = 1.0;
|
||||
};
|
||||
|
||||
// Initialize the animation
|
||||
const init = () => {
|
||||
// Set up resize handler
|
||||
globalThis.addEventListener("resize", updateDimensions);
|
||||
|
||||
// Initial setup
|
||||
updateDimensions();
|
||||
|
||||
// Start animation
|
||||
lastFrameTime = performance.now();
|
||||
animationFrameId = requestAnimationFrame(animate);
|
||||
};
|
||||
|
||||
// Start the animation
|
||||
init();
|
||||
|
||||
// Return cleanup function
|
||||
return () => {
|
||||
globalThis.removeEventListener("resize", updateDimensions);
|
||||
cancelAnimationFrame(animationFrameId);
|
||||
};
|
||||
};
|
431
src/file-viewer/scripts/canvas_2018.client.ts
Normal file
431
src/file-viewer/scripts/canvas_2018.client.ts
Normal file
|
@ -0,0 +1,431 @@
|
|||
// This canvas is based on the maze generation algo in Tanks. This was
|
||||
// originally written in C++ as a single function in 2018, and was ported to TS
|
||||
// by Chloe in 2025 for the cotyledon canvas.
|
||||
//
|
||||
// The main difference is that this version is a visualization, rather than the
|
||||
// practical function. Instead of taking a millisecond, only 5 steps are
|
||||
// performed per second, visualizing the whole ordeal. It also isn't a playable
|
||||
// game, obviously.
|
||||
//
|
||||
// Ported with love because I care about my old self
|
||||
// She deserves the world, but instead gave it to me.
|
||||
(globalThis as any).canvas_2018 = function (canvas: HTMLCanvasElement) {
|
||||
const isStandalone = canvas.getAttribute("data-standalone") === "true";
|
||||
if (isStandalone) {
|
||||
canvas.style.backgroundColor = "#27201E";
|
||||
}
|
||||
interface Cell {
|
||||
down: boolean;
|
||||
right: boolean;
|
||||
visited: boolean;
|
||||
|
||||
cell_flash: number;
|
||||
down_flash: number;
|
||||
right_flash: number;
|
||||
}
|
||||
interface Pos {
|
||||
x: number;
|
||||
y: number;
|
||||
/** Where the wall is relative to x, y. */
|
||||
dir: "left" | "right" | "up" | "down";
|
||||
}
|
||||
interface Maze {
|
||||
grid: Grid;
|
||||
cursor: { x: number; y: number };
|
||||
lastTick: number;
|
||||
/* Pixels */
|
||||
transform: number;
|
||||
newCellsToVisit: Pos[];
|
||||
randomWallBag: Cell[];
|
||||
randomWallTarget: number;
|
||||
renderOffset: { x: number; y: number };
|
||||
done: boolean;
|
||||
}
|
||||
const hex = (color: number[]) =>
|
||||
"#" + color.map((c) => c.toString(16).padStart(2, "0")).join("");
|
||||
let cellSize: number;
|
||||
let borderThickness: number;
|
||||
const cellFlashModifier = isStandalone ? 0.4 : 0.2;
|
||||
const color = isStandalone ? "#170d0b" : "#231C1A";
|
||||
const bg = [0x27, 0x20, 0x1E];
|
||||
const wallFlashColor = [0xFF, 0xA8, 0x7A];
|
||||
const cellFlashColor = "#FFA87A";
|
||||
const updateTime = 1000 / 7;
|
||||
const randomWallBreakInterval = [6, 12]; // every 10 to 18 walls.
|
||||
function randomBetween(min: number, max: number) {
|
||||
return Math.round(
|
||||
Math.random() * (max - min),
|
||||
) + min;
|
||||
}
|
||||
function randomOf<T>(array: T[]): T {
|
||||
return array[randomBetween(0, array.length - 1)];
|
||||
}
|
||||
function randomWallTarget() {
|
||||
return randomBetween(
|
||||
randomWallBreakInterval[0],
|
||||
randomWallBreakInterval[1],
|
||||
);
|
||||
}
|
||||
|
||||
// Originally, this used a 2-dimensional array. However, I wanted to make sure
|
||||
// that the grid could be infinitely sized. This grid constructs new cells on
|
||||
// demand, as needed.
|
||||
class Grid {
|
||||
cells = new Map<number, Cell>();
|
||||
cell({ x, y }: { x: number; y: number }) {
|
||||
const k = ((x | 0) << 16) + (y | 0);
|
||||
const { cells } = this;
|
||||
let existing = this.cells.get(k);
|
||||
if (!existing) {
|
||||
existing = {
|
||||
cell_flash: 0,
|
||||
down: true,
|
||||
down_flash: 0,
|
||||
right: true,
|
||||
right_flash: 0,
|
||||
visited: false,
|
||||
};
|
||||
cells.set(k, existing);
|
||||
}
|
||||
return existing;
|
||||
}
|
||||
forAll(
|
||||
renderOffset: { x: number; y: number },
|
||||
width: number,
|
||||
height: number,
|
||||
cb: (cell: Cell, pos: { x: number; y: number }) => void,
|
||||
) {
|
||||
const { x: offsetX, y: offsetY } = renderOffset;
|
||||
const startX = Math.floor(-offsetX / cellSize);
|
||||
const startY = Math.floor(-offsetY / cellSize);
|
||||
const endX = Math.ceil((width - offsetX) / cellSize);
|
||||
const endY = Math.ceil((height - offsetY) / cellSize);
|
||||
for (let x = startX; x <= endX; x++) {
|
||||
for (let y = startY; y <= endY; y++) {
|
||||
const cellX = offsetX + x * cellSize;
|
||||
const cellY = offsetY + y * cellSize;
|
||||
cb(this.cell({ x, y }), { x: cellX, y: cellY });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ctx = canvas.getContext("2d")!;
|
||||
if (!ctx) {
|
||||
console.error("Could not get canvas context");
|
||||
return () => {};
|
||||
}
|
||||
|
||||
let width: number, height: number;
|
||||
const updateDimensions = () => {
|
||||
width = canvas.width = canvas.offsetWidth;
|
||||
height = canvas.height = canvas.offsetHeight;
|
||||
cellSize = 100;
|
||||
borderThickness = 8;
|
||||
};
|
||||
updateDimensions();
|
||||
|
||||
setTimeout(() => {
|
||||
updateDimensions();
|
||||
}, 10);
|
||||
|
||||
let maze = initMaze();
|
||||
let nextMaze: Maze | null = null;
|
||||
let completeFade = 0;
|
||||
function initMaze(): Maze {
|
||||
return {
|
||||
grid: new Grid(),
|
||||
transform: 0,
|
||||
cursor: {
|
||||
x: randomBetween(0, Math.ceil(width / cellSize)),
|
||||
y: randomBetween(0, Math.ceil(height / cellSize)),
|
||||
},
|
||||
lastTick: performance.now(),
|
||||
randomWallBag: [],
|
||||
randomWallTarget: randomWallTarget(),
|
||||
newCellsToVisit: [],
|
||||
renderOffset: { x: 0, y: 0 },
|
||||
done: false,
|
||||
};
|
||||
}
|
||||
|
||||
function isOnScreen(maze: Maze, x: number, y: number) {
|
||||
const { x: offsetX, y: offsetY } = maze.renderOffset;
|
||||
const cellX = offsetX + x * cellSize;
|
||||
const cellY = offsetY + y * cellSize;
|
||||
return (
|
||||
cellX + cellSize > 0 &&
|
||||
cellX < width &&
|
||||
cellY + cellSize > 0 &&
|
||||
cellY < height
|
||||
);
|
||||
}
|
||||
|
||||
function tick(maze: Maze, other?: Maze) {
|
||||
if (maze.done) return;
|
||||
|
||||
// The original maze algorithm broke down 4%-8% of random right facing
|
||||
// walls, and 4%-8% of down facing walls. It did this at the end.
|
||||
// To make this visual more interesting, two random walls will be broken
|
||||
// down every 12-25 cell visits. This way, the main trail is always running.
|
||||
if (maze.randomWallBag.length > maze.randomWallTarget) {
|
||||
const down: Cell = randomOf(maze.randomWallBag);
|
||||
const right: Cell = randomOf(maze.randomWallBag);
|
||||
maze.randomWallBag.forEach((cell) =>
|
||||
cell.cell_flash = Math.min(cell.cell_flash + 0.2, 1)
|
||||
);
|
||||
down.cell_flash = 1;
|
||||
down.down = false;
|
||||
down.down_flash = 1;
|
||||
right.cell_flash = 1;
|
||||
right.right = false;
|
||||
right.right_flash = 1;
|
||||
maze.randomWallBag = [];
|
||||
maze.randomWallTarget = randomWallTarget();
|
||||
return;
|
||||
}
|
||||
|
||||
// The main algorithm was simple: Have a cursor position, and move it in a
|
||||
// random direction that it had not seen before. Once it had run out of
|
||||
// options, branch off of a previous location. Only visit each cell once.
|
||||
//
|
||||
// In this visualization, cells that are too far offscreen are softly
|
||||
// treated as "visited", which is how the simulation always stays in frame.
|
||||
const current = maze.grid.cell(maze.cursor);
|
||||
current.visited = true;
|
||||
current.cell_flash = 1;
|
||||
maze.randomWallBag.push(current);
|
||||
const adjacent = ([
|
||||
{ x: maze.cursor.x + 1, y: maze.cursor.y, dir: "left" },
|
||||
{ x: maze.cursor.x - 1, y: maze.cursor.y, dir: "right" },
|
||||
{ x: maze.cursor.x, y: maze.cursor.y + 1, dir: "up" },
|
||||
{ x: maze.cursor.x, y: maze.cursor.y - 1, dir: "down" },
|
||||
] as Pos[]).filter((pos) =>
|
||||
isOnScreen(maze, pos.x, pos.y) &&
|
||||
maze.grid.cell(pos).visited === false
|
||||
);
|
||||
if (adjacent.length === 0) {
|
||||
// move cursor to a random cell that has not been visited.
|
||||
const cells = maze.newCellsToVisit.filter((pos) =>
|
||||
isOnScreen(maze, pos.x, pos.y) &&
|
||||
maze.grid.cell(pos).visited === false
|
||||
);
|
||||
if (cells.length === 0) {
|
||||
maze.done = true;
|
||||
return;
|
||||
}
|
||||
const continuePos = randomOf(cells);
|
||||
breakWall(maze, continuePos, other);
|
||||
maze.cursor = { x: continuePos.x, y: continuePos.y };
|
||||
return;
|
||||
}
|
||||
|
||||
// break a random wall
|
||||
const toBreak = randomOf(adjacent);
|
||||
breakWall(maze, toBreak, other);
|
||||
maze.cursor = { x: toBreak.x, y: toBreak.y };
|
||||
|
||||
// add the other directions to the new cells to visit.
|
||||
maze.newCellsToVisit.push(
|
||||
...adjacent.filter((pos) => pos.dir !== toBreak.dir),
|
||||
);
|
||||
}
|
||||
|
||||
function breakWall(maze: Maze, pos: Pos, other?: Maze) {
|
||||
if (pos.dir === "right") {
|
||||
const cell = maze.grid.cell(pos);
|
||||
cell.right = false;
|
||||
cell.right_flash = 1;
|
||||
if (other) cell.right = false;
|
||||
} else if (pos.dir === "down") {
|
||||
const cell = maze.grid.cell(pos);
|
||||
cell.down = false;
|
||||
cell.down_flash = 1;
|
||||
if (other) cell.down = false;
|
||||
} else if (pos.dir === "left") {
|
||||
const cell = maze.grid.cell({ x: pos.x - 1, y: pos.y });
|
||||
cell.right = false;
|
||||
cell.right_flash = 1;
|
||||
if (other) cell.right = false;
|
||||
} else if (pos.dir === "up") {
|
||||
const cell = maze.grid.cell({ x: pos.x, y: pos.y - 1 });
|
||||
cell.down = false;
|
||||
cell.down_flash = 1;
|
||||
if (other) cell.down = false;
|
||||
}
|
||||
}
|
||||
|
||||
function renderOffset(maze: Maze) {
|
||||
return { x: maze.transform, y: maze.transform };
|
||||
}
|
||||
|
||||
let animationFrameId: number;
|
||||
let last = performance.now();
|
||||
let dt: number = 0;
|
||||
|
||||
function renderMazeBorders(maze: Maze, opacity: number) {
|
||||
ctx.globalAlpha = opacity;
|
||||
maze.grid.forAll(
|
||||
maze.renderOffset,
|
||||
width,
|
||||
height,
|
||||
(cell, { x: cellX, y: cellY }) => {
|
||||
// Walls
|
||||
if (cell.right) {
|
||||
ctx.fillStyle = color;
|
||||
ctx.fillRect(
|
||||
cellX + cellSize - borderThickness / 2,
|
||||
cellY - borderThickness / 2,
|
||||
borderThickness,
|
||||
cellSize + borderThickness,
|
||||
);
|
||||
}
|
||||
if (cell.down) {
|
||||
ctx.fillStyle = color;
|
||||
ctx.fillRect(
|
||||
cellX - borderThickness / 2,
|
||||
cellY + cellSize - borderThickness / 2,
|
||||
cellSize + borderThickness,
|
||||
borderThickness,
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
ctx.globalAlpha = 1;
|
||||
}
|
||||
|
||||
function renderCellFlash(maze: Maze) {
|
||||
maze.grid.forAll(
|
||||
maze.renderOffset,
|
||||
width,
|
||||
height,
|
||||
(cell, { x: cellX, y: cellY }) => {
|
||||
// Cell flash to show visiting path.
|
||||
if (cell.cell_flash > 0) {
|
||||
cell.cell_flash = Math.max(0, cell.cell_flash - dt / 1000);
|
||||
ctx.fillStyle = cellFlashColor;
|
||||
ctx.globalAlpha = cell.cell_flash * cellFlashModifier;
|
||||
ctx.fillRect(cellX, cellY, cellSize, cellSize);
|
||||
ctx.globalAlpha = 1;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function renderBorderFlash(maze: Maze) {
|
||||
maze.grid.forAll(
|
||||
maze.renderOffset,
|
||||
width,
|
||||
height,
|
||||
(cell, { x: cellX, y: cellY }) => {
|
||||
if (cell.right_flash == 0 && cell.down_flash == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Walls
|
||||
const cellFlash = cell.cell_flash * cellFlashModifier;
|
||||
if (cell.right_flash > 0) {
|
||||
cell.right_flash = Math.max(0, cell.right_flash - dt / 500);
|
||||
ctx.fillStyle = interpolateColor(
|
||||
bg,
|
||||
wallFlashColor,
|
||||
Math.max(cell.right_flash, cellFlash),
|
||||
);
|
||||
if (cellFlash > cell.right_flash) {
|
||||
ctx.globalAlpha = cell.right_flash / cellFlash;
|
||||
}
|
||||
ctx.fillRect(
|
||||
cellX + cellSize - borderThickness / 2,
|
||||
cellY + borderThickness / 2,
|
||||
borderThickness,
|
||||
cellSize - borderThickness,
|
||||
);
|
||||
ctx.globalAlpha = 1;
|
||||
}
|
||||
if (cell.down_flash > 0) {
|
||||
if (cellFlash > cell.down_flash) {
|
||||
ctx.globalAlpha = cell.down_flash / cellFlash;
|
||||
}
|
||||
cell.down_flash = Math.max(0, cell.down_flash - dt / 500);
|
||||
ctx.fillStyle = interpolateColor(
|
||||
bg,
|
||||
wallFlashColor,
|
||||
Math.max(cell.down_flash, cellFlash),
|
||||
);
|
||||
ctx.fillRect(
|
||||
cellX + borderThickness / 2,
|
||||
cellY + cellSize - borderThickness / 2,
|
||||
cellSize - borderThickness,
|
||||
borderThickness,
|
||||
);
|
||||
ctx.globalAlpha = 1;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function render() {
|
||||
const now = performance.now();
|
||||
dt = now - last;
|
||||
maze.transform += dt * 0.005;
|
||||
maze.renderOffset = renderOffset(maze);
|
||||
if (!maze.done) {
|
||||
if (now - maze.lastTick >= updateTime) {
|
||||
tick(maze);
|
||||
maze.lastTick = now;
|
||||
|
||||
if (maze.done) {
|
||||
nextMaze = initMaze();
|
||||
nextMaze.transform = (maze.transform % cellSize) - dt * 0.005;
|
||||
nextMaze.lastTick = now;
|
||||
completeFade = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (nextMaze) {
|
||||
nextMaze.transform += dt * 0.005;
|
||||
nextMaze.renderOffset = renderOffset(nextMaze);
|
||||
if (!nextMaze.done && now - nextMaze.lastTick >= updateTime) {
|
||||
tick(nextMaze, maze);
|
||||
nextMaze.lastTick = now;
|
||||
}
|
||||
}
|
||||
last = now;
|
||||
|
||||
ctx.clearRect(0, 0, width, height);
|
||||
|
||||
renderCellFlash(maze);
|
||||
if (nextMaze) renderCellFlash(nextMaze);
|
||||
|
||||
renderMazeBorders(maze, 1);
|
||||
if (nextMaze) {
|
||||
renderMazeBorders(nextMaze, completeFade);
|
||||
completeFade += dt / 3000;
|
||||
if (completeFade >= 1) {
|
||||
maze = nextMaze;
|
||||
nextMaze = null;
|
||||
}
|
||||
}
|
||||
|
||||
renderBorderFlash(maze);
|
||||
if (nextMaze) {
|
||||
renderCellFlash(nextMaze);
|
||||
renderBorderFlash(nextMaze);
|
||||
}
|
||||
|
||||
animationFrameId = requestAnimationFrame(render);
|
||||
}
|
||||
|
||||
function interpolateColor(start: number[], end: number[], t: number) {
|
||||
return hex(start.map((s, i) => Math.round(s + (end[i] - s) * t)));
|
||||
}
|
||||
|
||||
globalThis.addEventListener("resize", updateDimensions);
|
||||
animationFrameId = requestAnimationFrame(render);
|
||||
|
||||
// cleanup function
|
||||
return () => {
|
||||
globalThis.removeEventListener("resize", updateDimensions);
|
||||
cancelAnimationFrame(animationFrameId);
|
||||
};
|
||||
};
|
109
src/file-viewer/scripts/canvas_2019.client.ts
Normal file
109
src/file-viewer/scripts/canvas_2019.client.ts
Normal file
|
@ -0,0 +1,109 @@
|
|||
// __esModule
|
||||
true
|
||||
|
||||
// assertAllowedAttributes
|
||||
function assertAllowedAttributes(path, allowed) {
|
||||
let i = 0;
|
||||
for (const attr of path.node.attributes) {
|
||||
if (attr.type === "MarkoSpreadAttribute") {
|
||||
throw path.hub.buildError(
|
||||
attr,
|
||||
`Tag does not support spread attributes.`
|
||||
);
|
||||
} else if (!allowed.includes(attr.name)) {
|
||||
throw path.hub.buildError(
|
||||
attr,
|
||||
`Tag does not support the \`${attr.name}\` attribute.`
|
||||
);
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// assertAttributesOrArgs
|
||||
function assertAttributesOrArgs(path) {
|
||||
const { node } = path;
|
||||
const args = node.arguments;
|
||||
if (args && args.length && (node.attributes.length > 0 || node.body.length)) {
|
||||
const start = args[0].loc.start;
|
||||
const end = args[args.length - 1].loc.end;
|
||||
throw path.hub.buildError(
|
||||
{ loc: { start, end } },
|
||||
"Tag does not support arguments when attributes or body present."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// assertAttributesOrSingleArg
|
||||
function assertAttributesOrSingleArg(path) {
|
||||
assertAttributesOrArgs(path);
|
||||
const args = path.node.arguments;
|
||||
if (args && args.length > 1) {
|
||||
const start = args[1].loc.start;
|
||||
const end = args[args.length - 1].loc.end;
|
||||
throw path.hub.buildError(
|
||||
{ loc: { start, end } },
|
||||
"Tag does not support multiple arguments."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// assertNoArgs
|
||||
function assertNoArgs(path) {
|
||||
const args = path.node.arguments;
|
||||
if (args && args.length) {
|
||||
const start = args[0].loc.start;
|
||||
const end = args[args.length - 1].loc.end;
|
||||
throw path.hub.buildError(
|
||||
{ loc: { start, end } },
|
||||
"Tag does not support arguments."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// assertNoAttributeTags
|
||||
function assertNoAttributeTags(path) {
|
||||
if (path.node.attributeTags.length) {
|
||||
throw path.hub.buildError(
|
||||
path.node.attributeTags[0],
|
||||
"Tag not support nested attribute tags."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// assertNoAttributes
|
||||
function assertNoAttributes(path) {
|
||||
const { attributes } = path.node;
|
||||
if (attributes.length) {
|
||||
const start = attributes[0].loc.start;
|
||||
const end = attributes[attributes.length - 1].loc.end;
|
||||
throw path.hub.buildError(
|
||||
{ loc: { start, end } },
|
||||
"Tag does not support attributes."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// assertNoParams
|
||||
function assertNoParams(path) {
|
||||
const { params } = path.node.body;
|
||||
if (params.length) {
|
||||
const start = params[0].loc.start;
|
||||
const end = params[params.length - 1].loc.end;
|
||||
throw path.hub.buildError(
|
||||
{ loc: { start, end } },
|
||||
"Tag does not support parameters."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// assertNoVar
|
||||
function assertNoVar(path) {
|
||||
if (path.node.var) {
|
||||
throw path.hub.buildError(
|
||||
path.node.var,
|
||||
"Tag does not support a variable."
|
||||
);
|
||||
}
|
||||
}
|
197
src/file-viewer/scripts/canvas_2020.client.ts
Normal file
197
src/file-viewer/scripts/canvas_2020.client.ts
Normal file
|
@ -0,0 +1,197 @@
|
|||
// Vibe coded with AI
|
||||
(globalThis as any).canvas_2020 = function (canvas: HTMLCanvasElement) {
|
||||
const isStandalone = canvas.getAttribute("data-standalone") === "true";
|
||||
// Rain effect with slanted lines
|
||||
// Configuration interface for the rain effect
|
||||
interface RainConfig {
|
||||
fps: number; // frames per second
|
||||
color: string; // color of rain particles
|
||||
angle: number; // angle in degrees
|
||||
particleDensity: number; // particles per 10000 pixels of canvas area
|
||||
speed: number; // speed of particles (pixels per frame)
|
||||
lineWidth: number; // thickness of rain lines
|
||||
lineLength: number; // length of rain lines
|
||||
}
|
||||
|
||||
// Rain particle interface
|
||||
interface RainParticle {
|
||||
x: number; // x position
|
||||
y: number; // y position
|
||||
}
|
||||
|
||||
// Default configuration
|
||||
const config: RainConfig = {
|
||||
fps: 16,
|
||||
color: isStandalone ? "#00FEFB99" : "#081F24",
|
||||
angle: -18,
|
||||
particleDensity: 1,
|
||||
speed: 400,
|
||||
lineWidth: 8,
|
||||
lineLength: 100,
|
||||
};
|
||||
|
||||
// Get the canvas context
|
||||
const ctx = canvas.getContext("2d");
|
||||
if (!ctx) {
|
||||
console.error("Could not get canvas context");
|
||||
return () => {};
|
||||
}
|
||||
|
||||
// Make canvas transparent
|
||||
if (isStandalone) {
|
||||
canvas.style.backgroundColor = "#0F252B";
|
||||
} else {
|
||||
canvas.style.backgroundColor = "transparent";
|
||||
}
|
||||
|
||||
// Calculate canvas dimensions and update when resized
|
||||
let width = canvas.width;
|
||||
let height = canvas.height;
|
||||
let particles: RainParticle[] = [];
|
||||
let animationFrameId: number;
|
||||
let lastFrameTime = 0;
|
||||
const frameInterval = 1000 / config.fps;
|
||||
|
||||
// Calculate angle in radians
|
||||
const angleRad = (config.angle * Math.PI) / 180;
|
||||
|
||||
// Update canvas dimensions and particle count when resized
|
||||
const updateDimensions = () => {
|
||||
width = canvas.width = canvas.offsetWidth;
|
||||
height = canvas.height = canvas.offsetHeight;
|
||||
|
||||
// Calculate the canvas area in pixels
|
||||
const canvasArea = width * height;
|
||||
|
||||
// Calculate target number of particles based on canvas area
|
||||
const targetParticleCount = Math.floor(
|
||||
(canvasArea / 10000) * config.particleDensity,
|
||||
);
|
||||
|
||||
// Calculate buffer for horizontal offset due to slanted angle
|
||||
const buffer = Math.abs(height * Math.tan(angleRad)) + config.lineLength;
|
||||
|
||||
// Adjust the particles array
|
||||
if (particles.length < targetParticleCount) {
|
||||
// Add more particles if needed
|
||||
for (let i = particles.length; i < targetParticleCount; i++) {
|
||||
particles.push(createParticle(true, buffer));
|
||||
}
|
||||
} else if (particles.length > targetParticleCount) {
|
||||
// Remove excess particles
|
||||
particles = particles.slice(0, targetParticleCount);
|
||||
}
|
||||
};
|
||||
|
||||
// Create a new particle
|
||||
// Added initialDistribution parameter to distribute particles across the entire canvas at startup
|
||||
const createParticle = (
|
||||
initialDistribution = false,
|
||||
buffer: number,
|
||||
): RainParticle => {
|
||||
// For initial distribution, place particles throughout the canvas
|
||||
// Otherwise start them above the canvas
|
||||
let x = Math.random() * (width + buffer * 2) - buffer;
|
||||
let y;
|
||||
|
||||
if (initialDistribution) {
|
||||
// Distribute across the entire canvas height for initial setup
|
||||
y = Math.random() * (height + config.lineLength * 2) - config.lineLength;
|
||||
} else {
|
||||
// Start new particles from above the canvas with some randomization
|
||||
y = -config.lineLength - (Math.random() * config.lineLength * 20);
|
||||
}
|
||||
|
||||
return {
|
||||
x,
|
||||
y,
|
||||
};
|
||||
};
|
||||
|
||||
// Update particle positions
|
||||
const updateParticles = () => {
|
||||
// Calculate buffer for horizontal offset due to slanted angle
|
||||
const buffer = Math.abs(height * Math.tan(angleRad)) + config.lineLength;
|
||||
|
||||
for (let i = 0; i < particles.length; i++) {
|
||||
const p = particles[i];
|
||||
|
||||
// Update position based on speed and angle
|
||||
p.x += Math.sin(angleRad) * config.speed;
|
||||
p.y += Math.cos(angleRad) * config.speed;
|
||||
|
||||
// Reset particles that go offscreen - only determined by position
|
||||
// Add extra buffer to ensure particles fully exit the visible area before resetting
|
||||
if (
|
||||
p.y > height + config.lineLength ||
|
||||
p.x < -buffer ||
|
||||
p.x > width + buffer
|
||||
) {
|
||||
particles[i] = createParticle(false, buffer);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Draw particles
|
||||
const drawParticles = () => {
|
||||
// Clear canvas
|
||||
ctx.clearRect(0, 0, width, height);
|
||||
|
||||
// Set drawing properties
|
||||
ctx.strokeStyle = config.color;
|
||||
ctx.lineWidth = config.lineWidth;
|
||||
ctx.lineCap = "square";
|
||||
|
||||
// Draw each rain line
|
||||
ctx.beginPath();
|
||||
for (const p of particles) {
|
||||
// Only draw particles that are either on screen or within a reasonable buffer
|
||||
// This is for performance reasons - we don't need to draw particles far offscreen
|
||||
if (p.y >= -config.lineLength * 2 && p.y <= height + config.lineLength) {
|
||||
const endX = p.x + Math.sin(angleRad) * config.lineLength;
|
||||
const endY = p.y + Math.cos(angleRad) * config.lineLength;
|
||||
|
||||
ctx.moveTo(p.x, p.y);
|
||||
ctx.lineTo(endX, endY);
|
||||
}
|
||||
}
|
||||
ctx.stroke();
|
||||
};
|
||||
|
||||
// Animation loop
|
||||
const animate = (currentTime: number) => {
|
||||
animationFrameId = requestAnimationFrame(animate);
|
||||
|
||||
// Control frame rate
|
||||
if (currentTime - lastFrameTime < frameInterval) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastFrameTime = currentTime;
|
||||
|
||||
updateParticles();
|
||||
drawParticles();
|
||||
};
|
||||
|
||||
// Initialize the animation
|
||||
const init = () => {
|
||||
// Set up resize handler
|
||||
globalThis.addEventListener("resize", updateDimensions);
|
||||
|
||||
// Initial setup
|
||||
updateDimensions();
|
||||
|
||||
// Start animation
|
||||
lastFrameTime = performance.now();
|
||||
animationFrameId = requestAnimationFrame(animate);
|
||||
};
|
||||
|
||||
// Start the animation
|
||||
init();
|
||||
|
||||
// Return cleanup function
|
||||
return () => {
|
||||
globalThis.removeEventListener("resize", updateDimensions);
|
||||
cancelAnimationFrame(animationFrameId);
|
||||
};
|
||||
};
|
783
src/file-viewer/scripts/canvas_2021.client.ts
Normal file
783
src/file-viewer/scripts/canvas_2021.client.ts
Normal file
|
@ -0,0 +1,783 @@
|
|||
// Vibe coded.
|
||||
(globalThis as any).canvas_2021 = function (canvas: HTMLCanvasElement) {
|
||||
const isStandalone = canvas.getAttribute("data-standalone") === "true";
|
||||
// Constants for simulation
|
||||
const PARTICLE_RADIUS = 4.5;
|
||||
const PARTICLE_DENSITY = 0.004; // Particles per pixel
|
||||
const MIN_SPEED = 0.05;
|
||||
const MAX_SPEED = 6.0;
|
||||
const FRICTION = 0.96;
|
||||
const REPULSION_STRENGTH = 0.1;
|
||||
const REPULSION_RADIUS = 50;
|
||||
const FORCE_RADIUS = 400; // Increased radius
|
||||
const FORCE_STRENGTH = 0.25;
|
||||
const FORCE_FALLOFF_EXPONENT = 3; // Higher value = sharper falloff
|
||||
const FORCE_SPACING = 10; // Pixels between force points
|
||||
const MIN_FORCE_STRENGTH = 0.05; // Minimum force strength for very slow movements
|
||||
const MAX_FORCE_STRENGTH = 0.4; // Maximum force strength for fast movements
|
||||
const MIN_SPEED_THRESHOLD = 1; // Movement speed (px/frame) that produces minimum force
|
||||
const MAX_SPEED_THRESHOLD = 20; // Movement speed that produces maximum force
|
||||
const OVERSCAN_PIXELS = 250;
|
||||
const CELL_SIZE = REPULSION_RADIUS; // For spatial hashing
|
||||
|
||||
let globalOpacity = 0;
|
||||
|
||||
if (isStandalone) {
|
||||
canvas.style.backgroundColor = "#301D02";
|
||||
} else {
|
||||
canvas.style.backgroundColor = "transparent";
|
||||
}
|
||||
|
||||
// Interfaces
|
||||
interface Particle {
|
||||
x: number;
|
||||
y: number;
|
||||
vx: number;
|
||||
vy: number;
|
||||
charge: number; // 0 to 1, affecting color
|
||||
}
|
||||
|
||||
interface Force {
|
||||
x: number;
|
||||
y: number;
|
||||
dx: number;
|
||||
dy: number;
|
||||
strength: number;
|
||||
radius: number;
|
||||
createdAt: number;
|
||||
}
|
||||
|
||||
interface SpatialHash {
|
||||
[key: string]: Particle[];
|
||||
}
|
||||
|
||||
// State
|
||||
let first = true;
|
||||
let particles: Particle[] = [];
|
||||
let forces: Force[] = [];
|
||||
let width = canvas.width;
|
||||
let height = canvas.height;
|
||||
let targetParticleCount = 0;
|
||||
let spatialHash: SpatialHash = {};
|
||||
let ctx: CanvasRenderingContext2D | null = null;
|
||||
let animationId: number | null = null;
|
||||
let isRunning = false;
|
||||
|
||||
// Mouse tracking
|
||||
let lastMousePosition: { x: number; y: number } | null = null;
|
||||
// Track position of the last created force
|
||||
let lastForcePosition: { x: number; y: number } | null = null;
|
||||
|
||||
// Keep track of previous canvas dimensions for resize logic
|
||||
let previousWidth = 0;
|
||||
let previousHeight = 0;
|
||||
|
||||
// Initialize and cleanup
|
||||
function init(): void {
|
||||
ctx = canvas.getContext("2d");
|
||||
if (!ctx) return;
|
||||
|
||||
// Set canvas to full size
|
||||
resizeCanvas();
|
||||
|
||||
// Event listeners
|
||||
globalThis.addEventListener("resize", resizeCanvas);
|
||||
document.addEventListener("mousemove", handleMouseMove);
|
||||
|
||||
// Start animation immediately
|
||||
start();
|
||||
}
|
||||
|
||||
function cleanup(): void {
|
||||
// Stop the animation
|
||||
stop();
|
||||
|
||||
// Remove event listeners
|
||||
globalThis.removeEventListener("resize", resizeCanvas);
|
||||
document.removeEventListener("mousemove", handleMouseMove);
|
||||
|
||||
// Clear arrays
|
||||
particles = [];
|
||||
forces = [];
|
||||
spatialHash = {};
|
||||
lastMousePosition = null;
|
||||
lastForcePosition = null;
|
||||
}
|
||||
|
||||
// Resize canvas and adjust particle count
|
||||
function resizeCanvas(): void {
|
||||
// Store previous dimensions
|
||||
previousWidth = width;
|
||||
previousHeight = height;
|
||||
|
||||
// Update to new dimensions
|
||||
width = globalThis.innerWidth;
|
||||
height = globalThis.innerHeight;
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
|
||||
const oldTargetCount = targetParticleCount;
|
||||
targetParticleCount = Math.floor(width * height * PARTICLE_DENSITY);
|
||||
|
||||
// Adjust particle count
|
||||
if (targetParticleCount > oldTargetCount) {
|
||||
// Add more particles if needed, but only in newly available space
|
||||
addParticles(targetParticleCount - oldTargetCount, !first);
|
||||
first = false;
|
||||
}
|
||||
// Note: Removal of excess particles happens naturally during update
|
||||
}
|
||||
|
||||
// Handle mouse movement
|
||||
function handleMouseMove(e: MouseEvent): void {
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
const currentX = e.clientX - rect.left;
|
||||
const currentY = e.clientY - rect.top;
|
||||
|
||||
// Initialize positions if this is the first movement
|
||||
if (!lastMousePosition || !lastForcePosition) {
|
||||
lastMousePosition = { x: currentX, y: currentY };
|
||||
lastForcePosition = { x: currentX, y: currentY };
|
||||
return;
|
||||
}
|
||||
|
||||
// Store current mouse position
|
||||
const mouseX = currentX;
|
||||
const mouseY = currentY;
|
||||
|
||||
// Calculate vector from last mouse position to current
|
||||
const dx = mouseX - lastMousePosition.x;
|
||||
const dy = mouseY - lastMousePosition.y;
|
||||
const distMoved = Math.sqrt(dx * dx + dy * dy);
|
||||
|
||||
// Skip if essentially no movement (avoids numerical issues)
|
||||
if (distMoved < 0.1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the vector from the last force to the current mouse position
|
||||
const forceDx = mouseX - lastForcePosition.x;
|
||||
const forceDy = mouseY - lastForcePosition.y;
|
||||
const forceDistance = Math.sqrt(forceDx * forceDx + forceDy * forceDy);
|
||||
|
||||
// Only create forces if we've moved far enough from the last force
|
||||
if (forceDistance >= FORCE_SPACING) {
|
||||
// Calculate the direction vector from last force to current mouse
|
||||
let dirX = forceDx / forceDistance;
|
||||
let dirY = forceDy / forceDistance;
|
||||
|
||||
// Calculate how many force points to create
|
||||
const numPoints = Math.floor(forceDistance / FORCE_SPACING);
|
||||
|
||||
// Calculate movement speed based on the recent movement
|
||||
const movementSpeed = distMoved; // Simple approximation of speed
|
||||
|
||||
// Scale force strength based on movement speed
|
||||
let speedFactor;
|
||||
if (movementSpeed <= MIN_SPEED_THRESHOLD) {
|
||||
speedFactor = MIN_FORCE_STRENGTH;
|
||||
} else if (movementSpeed >= MAX_SPEED_THRESHOLD) {
|
||||
speedFactor = MAX_FORCE_STRENGTH;
|
||||
} else {
|
||||
// Linear interpolation between min and max
|
||||
const t = (movementSpeed - MIN_SPEED_THRESHOLD) /
|
||||
(MAX_SPEED_THRESHOLD - MIN_SPEED_THRESHOLD);
|
||||
speedFactor = MIN_FORCE_STRENGTH +
|
||||
t * (MAX_FORCE_STRENGTH - MIN_FORCE_STRENGTH);
|
||||
}
|
||||
|
||||
// Store current force position to update incrementally
|
||||
let currentForceX = lastForcePosition.x;
|
||||
let currentForceY = lastForcePosition.y;
|
||||
|
||||
// Create evenly spaced force points along the path from last force to current mouse
|
||||
for (let i = 0; i < numPoints; i++) {
|
||||
// Calculate position for this force point
|
||||
const t = (i + 1) / numPoints;
|
||||
const fx = lastForcePosition.x + forceDx * t;
|
||||
const fy = lastForcePosition.y + forceDy * t;
|
||||
|
||||
// Create force at this position with the direction vector
|
||||
createForce(fx, fy, dirX, dirY, speedFactor);
|
||||
|
||||
// Update the last force position to this new force
|
||||
currentForceX = fx;
|
||||
currentForceY = fy;
|
||||
}
|
||||
|
||||
// Update the last force position
|
||||
lastForcePosition = { x: currentForceX, y: currentForceY };
|
||||
}
|
||||
|
||||
// Always update the last mouse position
|
||||
lastMousePosition = { x: mouseX, y: mouseY };
|
||||
}
|
||||
|
||||
// Create a new force
|
||||
function createForce(
|
||||
x: number,
|
||||
y: number,
|
||||
dx: number,
|
||||
dy: number,
|
||||
strength = FORCE_STRENGTH,
|
||||
): void {
|
||||
forces.push({
|
||||
x,
|
||||
y,
|
||||
dx,
|
||||
dy,
|
||||
strength,
|
||||
radius: 1,
|
||||
createdAt: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
// Improved particle addition with fill strategy options
|
||||
function addParticles(count: number, inNewAreaOnly: boolean = false): void {
|
||||
// Determine available space
|
||||
const minX = -OVERSCAN_PIXELS;
|
||||
const maxX = width + OVERSCAN_PIXELS;
|
||||
const minY = -OVERSCAN_PIXELS;
|
||||
const maxY = height + OVERSCAN_PIXELS;
|
||||
|
||||
// Use a grid system that guarantees uniform spacing of particles
|
||||
const gridSpacing = REPULSION_RADIUS * 0.8; // Slightly less than repulsion radius
|
||||
const gridWidth = Math.ceil((maxX - minX) / gridSpacing);
|
||||
const gridHeight = Math.ceil((maxY - minY) / gridSpacing);
|
||||
|
||||
// Track which grid cells are already occupied
|
||||
const occupiedCells: Set<string> = new Set();
|
||||
|
||||
// Mark cells occupied by existing particles
|
||||
for (const particle of particles) {
|
||||
const cellX = Math.floor((particle.x - minX) / gridSpacing);
|
||||
const cellY = Math.floor((particle.y - minY) / gridSpacing);
|
||||
|
||||
// Ensure cell coordinates are within valid range
|
||||
if (cellX >= 0 && cellX < gridWidth && cellY >= 0 && cellY < gridHeight) {
|
||||
occupiedCells.add(`${cellX},${cellY}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Create arrays of all cells and filter by placement strategy
|
||||
const allGridCells: { x: number; y: number }[] = [];
|
||||
|
||||
for (let cellY = 0; cellY < gridHeight; cellY++) {
|
||||
for (let cellX = 0; cellX < gridWidth; cellX++) {
|
||||
const cellKey = `${cellX},${cellY}`;
|
||||
if (!occupiedCells.has(cellKey)) {
|
||||
const posX = minX + (cellX + 0.5) * gridSpacing;
|
||||
const posY = minY + (cellY + 0.5) * gridSpacing;
|
||||
|
||||
// For new area only placement, filter to expanded areas
|
||||
if (inNewAreaOnly && previousWidth > 0 && previousHeight > 0) {
|
||||
const expandedRight = width > previousWidth;
|
||||
const expandedBottom = height > previousHeight;
|
||||
|
||||
const inNewRightArea = expandedRight && posX >= previousWidth &&
|
||||
posX <= width;
|
||||
const inNewBottomArea = expandedBottom && posY >= previousHeight &&
|
||||
posY <= height;
|
||||
|
||||
if (inNewRightArea || inNewBottomArea) {
|
||||
allGridCells.push({ x: cellX, y: cellY });
|
||||
}
|
||||
} else if (!inNewAreaOnly) {
|
||||
// Standard placement - add all valid cells
|
||||
allGridCells.push({ x: cellX, y: cellY });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (allGridCells.length == 0) {
|
||||
throw new Error("No cells available to place particles");
|
||||
}
|
||||
|
||||
// We now have all grid cells that match our placement criteria
|
||||
|
||||
// If we need more particles than we have available cells, we need to adjust
|
||||
// gridSpacing to fit more cells into the same space
|
||||
if (count > allGridCells.length) {
|
||||
// Retry with a smaller grid spacing
|
||||
// Proportionally reduce the grid spacing to fit the required number of particles
|
||||
const scaleFactor = Math.sqrt(allGridCells.length / count);
|
||||
const newGridSpacing = gridSpacing * scaleFactor;
|
||||
|
||||
// Clear particles and try again with new spacing
|
||||
// This is a recursive call, but with adjusted parameters that will fit
|
||||
return addParticlesWithCustomSpacing(
|
||||
count,
|
||||
inNewAreaOnly,
|
||||
newGridSpacing,
|
||||
);
|
||||
}
|
||||
|
||||
// Shuffle the available cells for random selection
|
||||
shuffleArray(allGridCells);
|
||||
|
||||
// Take the number of cells we need
|
||||
const cellsToUse = Math.min(count, allGridCells.length);
|
||||
const selectedCells = allGridCells.slice(0, cellsToUse);
|
||||
|
||||
// Create particles in selected cells
|
||||
for (const cell of selectedCells) {
|
||||
// Add jitter within the cell for natural look
|
||||
const jitterX = (Math.random() - 0.5) * gridSpacing * 0.8;
|
||||
const jitterY = (Math.random() - 0.5) * gridSpacing * 0.8;
|
||||
|
||||
// Calculate final position
|
||||
const x = minX + (cell.x + 0.5) * gridSpacing + jitterX;
|
||||
const y = minY + (cell.y + 0.5) * gridSpacing + jitterY;
|
||||
|
||||
// Create a particle at this position
|
||||
particles.push(createParticle(x, y));
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to add particles with custom grid spacing
|
||||
function addParticlesWithCustomSpacing(
|
||||
count: number,
|
||||
inNewAreaOnly: boolean,
|
||||
gridSpacing: number,
|
||||
): void {
|
||||
if (gridSpacing == 0) throw new Error("Grid spacing is 0");
|
||||
// Determine available space
|
||||
const minX = -OVERSCAN_PIXELS;
|
||||
const maxX = width + OVERSCAN_PIXELS;
|
||||
const minY = -OVERSCAN_PIXELS;
|
||||
const maxY = height + OVERSCAN_PIXELS;
|
||||
|
||||
// Create grid using the custom spacing
|
||||
const gridWidth = Math.ceil((maxX - minX) / gridSpacing);
|
||||
const gridHeight = Math.ceil((maxY - minY) / gridSpacing);
|
||||
|
||||
// Track which grid cells are already occupied
|
||||
const occupiedCells: Set<string> = new Set();
|
||||
|
||||
// Mark cells occupied by existing particles
|
||||
for (const particle of particles) {
|
||||
const cellX = Math.floor((particle.x - minX) / gridSpacing);
|
||||
const cellY = Math.floor((particle.y - minY) / gridSpacing);
|
||||
|
||||
// Ensure cell coordinates are within valid range
|
||||
if (cellX >= 0 && cellX < gridWidth && cellY >= 0 && cellY < gridHeight) {
|
||||
occupiedCells.add(`${cellX},${cellY}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Create arrays of all cells and filter by placement strategy
|
||||
const allGridCells: { x: number; y: number }[] = [];
|
||||
|
||||
for (let cellY = 0; cellY < gridHeight; cellY++) {
|
||||
for (let cellX = 0; cellX < gridWidth; cellX++) {
|
||||
const cellKey = `${cellX},${cellY}`;
|
||||
if (!occupiedCells.has(cellKey)) {
|
||||
const posX = minX + (cellX + 0.5) * gridSpacing;
|
||||
const posY = minY + (cellY + 0.5) * gridSpacing;
|
||||
|
||||
// For new area only placement, filter to expanded areas
|
||||
if (inNewAreaOnly && previousWidth > 0 && previousHeight > 0) {
|
||||
const expandedRight = width > previousWidth;
|
||||
const expandedBottom = height > previousHeight;
|
||||
|
||||
const inNewRightArea = expandedRight && posX >= previousWidth &&
|
||||
posX <= width;
|
||||
const inNewBottomArea = expandedBottom && posY >= previousHeight &&
|
||||
posY <= height;
|
||||
|
||||
if (inNewRightArea || inNewBottomArea) {
|
||||
allGridCells.push({ x: cellX, y: cellY });
|
||||
}
|
||||
} else if (!inNewAreaOnly) {
|
||||
// Standard placement - add all valid cells
|
||||
allGridCells.push({ x: cellX, y: cellY });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Shuffle the available cells for random distribution
|
||||
shuffleArray(allGridCells);
|
||||
|
||||
// Take the number of cells we need (or all if we have fewer)
|
||||
const cellsToUse = Math.min(count, allGridCells.length);
|
||||
|
||||
// Create particles in selected cells
|
||||
for (let i = 0; i < cellsToUse; i++) {
|
||||
const cell = allGridCells[i];
|
||||
|
||||
// Add jitter within the cell
|
||||
const jitterX = (Math.random() - 0.5) * gridSpacing * 0.8;
|
||||
const jitterY = (Math.random() - 0.5) * gridSpacing * 0.8;
|
||||
|
||||
// Calculate final position
|
||||
const x = minX + (cell.x + 0.5) * gridSpacing + jitterX;
|
||||
const y = minY + (cell.y + 0.5) * gridSpacing + jitterY;
|
||||
|
||||
// Create a particle at this position
|
||||
particles.push(createParticle(x, y));
|
||||
}
|
||||
}
|
||||
|
||||
// Utility to shuffle an array (Fisher-Yates algorithm)
|
||||
function shuffleArray<T>(array: T[]): void {
|
||||
for (let i = array.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[array[i], array[j]] = [array[j], array[i]];
|
||||
}
|
||||
}
|
||||
|
||||
// Simplified createParticle function that just places at a specific position
|
||||
function createParticle(x: number, y: number): Particle {
|
||||
return {
|
||||
x: x + (Math.random() * 4 - 2),
|
||||
y: y + (Math.random() * 4 - 2),
|
||||
vx: 0,
|
||||
vy: 0,
|
||||
charge: 0,
|
||||
};
|
||||
}
|
||||
|
||||
// Function to create a particle on one of the edges
|
||||
function createParticleOnEdge(): Particle {
|
||||
// Overscan bounds with fixed pixel size
|
||||
const minX = -OVERSCAN_PIXELS;
|
||||
const maxX = width + OVERSCAN_PIXELS;
|
||||
const minY = -OVERSCAN_PIXELS;
|
||||
const maxY = height + OVERSCAN_PIXELS;
|
||||
|
||||
let x: number, y: number;
|
||||
|
||||
// Place on one of the edges
|
||||
const edge = Math.floor(Math.random() * 4);
|
||||
switch (edge) {
|
||||
case 0: // Top
|
||||
x = minX + Math.random() * (maxX - minX);
|
||||
y = minY;
|
||||
break;
|
||||
case 1: // Right
|
||||
x = maxX;
|
||||
y = minY + Math.random() * (maxY - minY);
|
||||
break;
|
||||
case 2: // Bottom
|
||||
x = minX + Math.random() * (maxX - minX);
|
||||
y = maxY;
|
||||
break;
|
||||
case 3: // Left
|
||||
x = minX;
|
||||
y = minY + Math.random() * (maxY - minY);
|
||||
break;
|
||||
default:
|
||||
x = minX + Math.random() * (maxX - minX);
|
||||
y = minY + Math.random() * (maxY - minY);
|
||||
}
|
||||
|
||||
return createParticle(x, y);
|
||||
}
|
||||
|
||||
// Spatial hashing functions
|
||||
function getHashKey(x: number, y: number): string {
|
||||
const cellX = Math.floor(x / CELL_SIZE);
|
||||
const cellY = Math.floor(y / CELL_SIZE);
|
||||
return `${cellX},${cellY}`;
|
||||
}
|
||||
|
||||
function addToSpatialHash(particle: Particle): void {
|
||||
const key = getHashKey(particle.x, particle.y);
|
||||
if (!spatialHash[key]) {
|
||||
spatialHash[key] = [];
|
||||
}
|
||||
spatialHash[key].push(particle);
|
||||
}
|
||||
|
||||
function updateSpatialHash(): void {
|
||||
// Clear previous hash
|
||||
spatialHash = {};
|
||||
|
||||
// Add all particles to hash
|
||||
for (const particle of particles) {
|
||||
addToSpatialHash(particle);
|
||||
}
|
||||
}
|
||||
|
||||
function getNearbyParticles(
|
||||
x: number,
|
||||
y: number,
|
||||
radius: number,
|
||||
): Particle[] {
|
||||
const result: Particle[] = [];
|
||||
const cellRadius = Math.ceil(radius / CELL_SIZE);
|
||||
|
||||
const centerCellX = Math.floor(x / CELL_SIZE);
|
||||
const centerCellY = Math.floor(y / CELL_SIZE);
|
||||
|
||||
for (
|
||||
let cellX = centerCellX - cellRadius;
|
||||
cellX <= centerCellX + cellRadius;
|
||||
cellX++
|
||||
) {
|
||||
for (
|
||||
let cellY = centerCellY - cellRadius;
|
||||
cellY <= centerCellY + cellRadius;
|
||||
cellY++
|
||||
) {
|
||||
const key = `${cellX},${cellY}`;
|
||||
const cell = spatialHash[key];
|
||||
|
||||
if (cell) {
|
||||
result.push(...cell);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Main update function
|
||||
function update(): void {
|
||||
const now = Date.now();
|
||||
// Fixed pixel overscan
|
||||
const minX = -OVERSCAN_PIXELS;
|
||||
const maxX = width + OVERSCAN_PIXELS;
|
||||
const minY = -OVERSCAN_PIXELS;
|
||||
const maxY = height + OVERSCAN_PIXELS;
|
||||
|
||||
// Update spatial hash
|
||||
updateSpatialHash();
|
||||
|
||||
// Update forces and remove expired ones
|
||||
if (forces.length > 40) {
|
||||
forces = forces.slice(-40);
|
||||
}
|
||||
forces = forces.filter((force) => {
|
||||
force.strength *= 0.95;
|
||||
force.radius *= 0.95;
|
||||
return force.strength > 0.001;
|
||||
});
|
||||
|
||||
// Update particles
|
||||
const newParticles: Particle[] = [];
|
||||
|
||||
for (const particle of particles) {
|
||||
// Apply forces
|
||||
for (const force of forces) {
|
||||
const dx = particle.x - force.x;
|
||||
const dy = particle.y - force.y;
|
||||
const distSq = dx * dx + dy * dy;
|
||||
|
||||
const radius = force.radius * FORCE_RADIUS;
|
||||
|
||||
if (distSq < radius * radius) {
|
||||
const dist = Math.sqrt(distSq);
|
||||
|
||||
// Exponential falloff - much more concentrated at center
|
||||
// (1 - x/R)^n where n controls how sharp the falloff is
|
||||
const normalizedDist = dist / radius;
|
||||
const factor = Math.pow(1 - normalizedDist, FORCE_FALLOFF_EXPONENT);
|
||||
|
||||
// Calculate force line projection for directional effect
|
||||
// This makes particles along the force's path experience stronger effect
|
||||
const dotProduct = (dx * -force.dx) + (dy * -force.dy);
|
||||
const projectionFactor = Math.max(0, dotProduct / dist);
|
||||
|
||||
// Apply the combined factors - stronger directional bias
|
||||
const finalFactor = factor * force.strength *
|
||||
(0.1 + 0.9 * projectionFactor);
|
||||
|
||||
particle.vx += force.dx * finalFactor;
|
||||
particle.vy += force.dy * finalFactor;
|
||||
// charge for the first 100ms
|
||||
if ((now - force.createdAt) < 100) {
|
||||
particle.charge = Math.min(
|
||||
1,
|
||||
particle.charge + (finalFactor * finalFactor) * 0.2,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply repulsion from nearby particles
|
||||
const nearby = getNearbyParticles(
|
||||
particle.x,
|
||||
particle.y,
|
||||
REPULSION_RADIUS,
|
||||
);
|
||||
|
||||
for (const other of nearby) {
|
||||
if (other === particle) continue;
|
||||
|
||||
const dx = particle.x - other.x;
|
||||
const dy = particle.y - other.y;
|
||||
const distSq = dx * dx + dy * dy;
|
||||
|
||||
if (distSq < REPULSION_RADIUS * REPULSION_RADIUS && distSq > 0) {
|
||||
const dist = Math.sqrt(distSq);
|
||||
const factor = REPULSION_STRENGTH * (1 - dist / REPULSION_RADIUS);
|
||||
|
||||
const fx = dx / dist * factor;
|
||||
const fy = dy / dist * factor;
|
||||
|
||||
particle.vx += fx;
|
||||
particle.vy += fy;
|
||||
}
|
||||
}
|
||||
|
||||
// Apply friction
|
||||
particle.vx *= FRICTION;
|
||||
particle.vy *= FRICTION;
|
||||
|
||||
// Ensure minimum speed
|
||||
const speed = Math.sqrt(
|
||||
particle.vx * particle.vx + particle.vy * particle.vy,
|
||||
);
|
||||
if (speed < MIN_SPEED && speed > 0) {
|
||||
const scale = MIN_SPEED / speed;
|
||||
particle.vx *= scale;
|
||||
particle.vy *= scale;
|
||||
}
|
||||
|
||||
// Cap at maximum speed
|
||||
if (speed > MAX_SPEED) {
|
||||
const scale = MAX_SPEED / speed;
|
||||
particle.vx *= scale;
|
||||
particle.vy *= scale;
|
||||
}
|
||||
|
||||
// Update position
|
||||
particle.x += particle.vx;
|
||||
particle.y += particle.vy;
|
||||
|
||||
// Decrease charge
|
||||
particle.charge *= 0.99;
|
||||
|
||||
// Check if particle is within extended bounds
|
||||
if (
|
||||
particle.x >= minX && particle.x <= maxX &&
|
||||
particle.y >= minY && particle.y <= maxY
|
||||
) {
|
||||
// If outside screen but within overscan, keep it if we need more particles
|
||||
if (
|
||||
(particle.x < 0 || particle.x > width ||
|
||||
particle.y < 0 || particle.y > height) &&
|
||||
newParticles.length >= targetParticleCount
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
newParticles.push(particle);
|
||||
} else {
|
||||
// Out of bounds, respawn if needed
|
||||
if (newParticles.length < targetParticleCount) {
|
||||
newParticles.push(createParticleOnEdge());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add more particles if needed
|
||||
while (newParticles.length < targetParticleCount) {
|
||||
newParticles.push(createParticleOnEdge());
|
||||
}
|
||||
|
||||
particles = newParticles;
|
||||
}
|
||||
|
||||
// Render function
|
||||
const mul = isStandalone ? 0.9 : 0.5;
|
||||
const add = isStandalone ? 0.1 : 0.03;
|
||||
function render(): void {
|
||||
if (!ctx) return;
|
||||
|
||||
// Clear canvas
|
||||
ctx.clearRect(0, 0, width, height);
|
||||
|
||||
// Draw particles
|
||||
for (const particle of particles) {
|
||||
// Only draw if within canvas bounds (plus a small margin)
|
||||
if (
|
||||
particle.x >= -PARTICLE_RADIUS &&
|
||||
particle.x <= width + PARTICLE_RADIUS &&
|
||||
particle.y >= -PARTICLE_RADIUS && particle.y <= height + PARTICLE_RADIUS
|
||||
) {
|
||||
ctx.beginPath();
|
||||
ctx.arc(particle.x, particle.y, PARTICLE_RADIUS, 0, Math.PI * 2);
|
||||
|
||||
// Color based on charge
|
||||
ctx.fillStyle = "#FFCB1F";
|
||||
ctx.globalAlpha = (particle.charge * mul + add) * globalOpacity;
|
||||
ctx.fill();
|
||||
}
|
||||
}
|
||||
|
||||
// // Debug: Draw forces and falloff visualization
|
||||
// if (ctx) {
|
||||
// for (const force of forces) {
|
||||
// const R = force.radius * FORCE_RADIUS;
|
||||
|
||||
// // Draw force point
|
||||
// ctx.beginPath();
|
||||
// ctx.arc(force.x, force.y, 5, 0, Math.PI * 2);
|
||||
// ctx.fillStyle = 'rgba(255, 0, 0, 0.5)';
|
||||
// ctx.fill();
|
||||
|
||||
// // Draw force direction
|
||||
// ctx.beginPath();
|
||||
// ctx.moveTo(force.x, force.y);
|
||||
// ctx.lineTo(force.x + force.dx * 20, force.y + force.dy * 20);
|
||||
// ctx.strokeStyle = 'red';
|
||||
// ctx.stroke();
|
||||
|
||||
// // Visualize the falloff curve with rings
|
||||
// for (let i = 0; i <= 10; i++) {
|
||||
// const radius = (R * i) / 10;
|
||||
// const normalizedDist = radius / R;
|
||||
// const intensity = Math.pow(1 - normalizedDist, FORCE_FALLOFF_EXPONENT);
|
||||
|
||||
// ctx.beginPath();
|
||||
// ctx.arc(force.x, force.y, radius, 0, Math.PI * 2);
|
||||
// ctx.strokeStyle = `rgba(255, 0, 0, ${intensity * 0.2})`;
|
||||
// ctx.stroke();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
// Animation loop
|
||||
let r = Math.random();
|
||||
function animate(): void {
|
||||
globalOpacity = Math.min(1, globalOpacity + 0.03);
|
||||
update();
|
||||
render();
|
||||
|
||||
if (isRunning) {
|
||||
animationId = requestAnimationFrame(animate);
|
||||
}
|
||||
}
|
||||
|
||||
// Start/stop functions
|
||||
function start(): void {
|
||||
if (isRunning) return;
|
||||
|
||||
// Calculate target particle count based on canvas size
|
||||
targetParticleCount = Math.floor(width * height * PARTICLE_DENSITY);
|
||||
|
||||
// Clear any existing particles and create new ones with proper spacing
|
||||
particles = [];
|
||||
addParticles(targetParticleCount);
|
||||
|
||||
isRunning = true;
|
||||
animate();
|
||||
}
|
||||
|
||||
function stop(): void {
|
||||
isRunning = false;
|
||||
|
||||
if (animationId !== null) {
|
||||
cancelAnimationFrame(animationId);
|
||||
animationId = null;
|
||||
}
|
||||
}
|
||||
|
||||
init();
|
||||
return cleanup;
|
||||
};
|
160
src/file-viewer/scripts/canvas_2022.client.ts
Normal file
160
src/file-viewer/scripts/canvas_2022.client.ts
Normal file
|
@ -0,0 +1,160 @@
|
|||
(globalThis as any).canvas_2022 = function (canvas: HTMLCanvasElement) {
|
||||
const isStandalone = canvas.getAttribute("data-standalone") === "true";
|
||||
// Configuration for the grid of rotating squares
|
||||
const config = {
|
||||
gridRotation: 20, // Overall grid rotation in degrees
|
||||
squareSize: 20, // Size of each square
|
||||
spacing: 100, // Distance between square centers
|
||||
moveSpeedX: 0.01, // Horizontal movement speed (pixels per second)
|
||||
moveSpeedY: 0.01, // Vertical movement speed (pixels per second)
|
||||
squareColor: "#00220A", // Color of the squares
|
||||
squareOpacity: 1, // Opacity of the squares
|
||||
|
||||
// Function to determine square rotation based on its coordinates and time
|
||||
// Can be adjusted for different patterns
|
||||
rotationFunction: (x: number, y: number, time: number): number => {
|
||||
// Combination of spatial wave and time-based rotation
|
||||
return Math.sin(x * 0.05) * Math.cos(y * 0.05) * 180;
|
||||
},
|
||||
};
|
||||
|
||||
// Convert grid rotation to radians
|
||||
const gridRotationRad = (config.gridRotation * Math.PI) / 180;
|
||||
|
||||
// Get the canvas context
|
||||
const ctx = canvas.getContext("2d");
|
||||
if (!ctx) {
|
||||
console.error("Could not get canvas context");
|
||||
return () => {};
|
||||
}
|
||||
|
||||
// Make canvas transparent
|
||||
if (isStandalone) {
|
||||
canvas.style.backgroundColor = "#154226";
|
||||
} else {
|
||||
canvas.style.backgroundColor = "transparent";
|
||||
}
|
||||
|
||||
// Animation variables
|
||||
let width = canvas.width;
|
||||
let height = canvas.height;
|
||||
let offsetX = 0;
|
||||
let offsetY = 0;
|
||||
let time = 0;
|
||||
let animationFrameId: number;
|
||||
let lastTime = 0;
|
||||
|
||||
// Update canvas dimensions when resized
|
||||
const updateDimensions = () => {
|
||||
width = canvas.width = canvas.clientWidth;
|
||||
height = canvas.height = canvas.clientHeight;
|
||||
};
|
||||
|
||||
// Calculate the diagonal length of the canvas (to ensure rotation covers corners)
|
||||
const calculateDiagonal = () => {
|
||||
return Math.sqrt(width * width + height * height);
|
||||
};
|
||||
|
||||
// Draw a single square with rotation
|
||||
const drawSquare = (x: number, y: number, size: number, rotation: number) => {
|
||||
ctx.save();
|
||||
|
||||
// Move to the center of the square position, rotate, then draw
|
||||
ctx.translate(x, y);
|
||||
ctx.rotate((rotation * Math.PI) / 180); // Convert rotation degrees to radians
|
||||
|
||||
// Draw square centered at position
|
||||
ctx.fillRect(-size / 2, -size / 2, size, size);
|
||||
|
||||
ctx.restore();
|
||||
};
|
||||
|
||||
// Draw the entire grid of squares
|
||||
const drawGrid = () => {
|
||||
ctx.clearRect(0, 0, width, height);
|
||||
|
||||
// Set drawing properties
|
||||
ctx.fillStyle = config.squareColor;
|
||||
ctx.globalAlpha = config.squareOpacity;
|
||||
|
||||
// Save the current transformation state
|
||||
ctx.save();
|
||||
|
||||
// Move to the center of the canvas, rotate the grid, then move back
|
||||
const centerX = width / 2;
|
||||
const centerY = height / 2;
|
||||
ctx.translate(centerX, centerY);
|
||||
ctx.rotate(gridRotationRad);
|
||||
|
||||
// Calculate how much of the grid to draw based on canvas size
|
||||
const diagonal = calculateDiagonal();
|
||||
const gridSize = Math.ceil(diagonal / config.spacing) + 2;
|
||||
|
||||
// Adjust for offset to create movement
|
||||
const adjustedOffsetX = offsetX % config.spacing;
|
||||
const adjustedOffsetY = offsetY % config.spacing;
|
||||
|
||||
// Draw grid with enough squares to cover the rotated canvas
|
||||
const halfGrid = Math.ceil(gridSize / 2);
|
||||
|
||||
for (let y = -halfGrid; y <= halfGrid; y++) {
|
||||
for (let x = -halfGrid; x <= halfGrid; x++) {
|
||||
// Calculate actual position with offset
|
||||
const posX = x * config.spacing + adjustedOffsetX;
|
||||
const posY = y * config.spacing + adjustedOffsetY;
|
||||
|
||||
// Calculate square rotation based on its position and time
|
||||
const squareRotation = config.rotationFunction(posX, posY, time);
|
||||
|
||||
// Draw the square
|
||||
drawSquare(posX, posY, config.squareSize, squareRotation);
|
||||
}
|
||||
}
|
||||
|
||||
// Restore the transformation state
|
||||
ctx.restore();
|
||||
|
||||
// Reset global alpha
|
||||
ctx.globalAlpha = 1.0;
|
||||
};
|
||||
|
||||
// Animation loop
|
||||
const animate = (currentTime: number) => {
|
||||
animationFrameId = requestAnimationFrame(animate);
|
||||
|
||||
// Calculate time elapsed since last frame
|
||||
const elapsed = currentTime - lastTime;
|
||||
lastTime = currentTime;
|
||||
|
||||
// Update time variable for rotation function
|
||||
time += elapsed;
|
||||
|
||||
// Update position offsets for movement
|
||||
offsetX += config.moveSpeedX * elapsed;
|
||||
offsetY += config.moveSpeedY * elapsed;
|
||||
|
||||
// Draw the grid
|
||||
drawGrid();
|
||||
};
|
||||
|
||||
// Initialize the animation
|
||||
const init = () => {
|
||||
// Set up resize handler
|
||||
globalThis.addEventListener("resize", updateDimensions);
|
||||
|
||||
// Initial setup
|
||||
updateDimensions();
|
||||
|
||||
// Start animation
|
||||
animationFrameId = requestAnimationFrame(animate);
|
||||
};
|
||||
|
||||
// Start the animation
|
||||
init();
|
||||
|
||||
// Return cleanup function
|
||||
return () => {
|
||||
globalThis.removeEventListener("resize", updateDimensions);
|
||||
cancelAnimationFrame(animationFrameId);
|
||||
};
|
||||
};
|
197
src/file-viewer/scripts/canvas_2023.client.ts
Normal file
197
src/file-viewer/scripts/canvas_2023.client.ts
Normal file
|
@ -0,0 +1,197 @@
|
|||
(globalThis as any).canvas_2023 = function (canvas: HTMLCanvasElement) {
|
||||
const isStandalone = canvas.getAttribute("data-standalone") === "true";
|
||||
const config = {
|
||||
heartBaseSize: 50,
|
||||
heartMaxSize: 100,
|
||||
spacing: 150,
|
||||
rowSpeed: 0.1,
|
||||
heartColor: "#FF90D9",
|
||||
heartOpacity: isStandalone ? 0.5 : 0.04,
|
||||
mouseInfluenceRadius: 1000,
|
||||
heartScaleFunction: (distance: number, radius: number): number => {
|
||||
if (distance > radius) return 1;
|
||||
|
||||
const normalizedDistance = distance / radius;
|
||||
const scaleFactor = 1 +
|
||||
(1 - normalizedDistance) *
|
||||
(config.heartMaxSize / config.heartBaseSize - 1);
|
||||
|
||||
return 1 + (scaleFactor - 1) * Math.pow(1 - normalizedDistance, 2);
|
||||
},
|
||||
};
|
||||
|
||||
const heart = new Path2D(
|
||||
"M23.9451 45.3973L20.8672 42.6493C16.9551 39.0174 13.7054 35.8927 11.1181 33.275C8.53056 30.6574 6.46731 28.286 4.92839 26.1608C3.38946 24.0356 2.31772 22.1028 1.71314 20.3624C1.10856 18.6219 0.806274 16.8705 0.806274 15.1081C0.806274 11.4718 2.03118 8.42016 4.481 5.95312C6.93118 3.48608 9.93831 2.25256 13.5024 2.25256C15.5649 2.25256 17.482 2.70142 19.2536 3.59912C21.0255 4.49682 22.5893 5.80674 23.9451 7.52887C25.484 5.73346 27.1059 4.40522 28.8108 3.54416C30.5161 2.6831 32.3751 2.25256 34.3877 2.25256C38.0141 2.25256 41.0551 3.48663 43.5108 5.95477C45.9661 8.42291 47.1938 11.4758 47.1938 15.1136C47.1938 16.8712 46.8823 18.6115 46.2594 20.3343C45.6365 22.0568 44.5648 23.9807 43.0442 26.1059C41.5236 28.231 39.4721 30.6136 36.8896 33.2536C34.3068 35.8936 31.0362 39.0255 27.0779 42.6493L23.9451 45.3973ZM23.9176 38.802C27.6088 35.431 30.6339 32.5547 32.9928 30.173C35.3518 27.7913 37.2091 25.7211 38.5648 23.9624C39.9205 22.2036 40.864 20.6137 41.3953 19.1928C41.9266 17.7715 42.1923 16.4101 42.1923 15.1086C42.1923 12.8768 41.4529 11.0098 39.974 9.50748C38.4952 8.0052 36.6461 7.25406 34.4268 7.25406C32.631 7.25406 30.9572 7.6811 29.4055 8.87193C27.8537 10.0628 25.5389 13.0434 25.5389 13.0434L23.9451 15.3299L22.3512 13.0434C22.3512 13.0434 20.0643 10.2311 18.4638 9.04031C16.8634 7.84948 15.2194 7.25406 13.4991 7.25406C11.2929 7.25406 9.46857 7.98816 8.02602 9.45637C6.58383 10.9246 5.86273 12.8162 5.86273 15.1311C5.86273 16.4784 6.13644 17.8679 6.68386 19.2994C7.23127 20.731 8.18394 22.3333 9.54185 24.1064C10.8998 25.879 12.7329 27.9562 15.0413 30.3379C17.3497 32.7196 20.3084 35.5409 23.9176 38.802Z",
|
||||
);
|
||||
|
||||
const ctx = canvas.getContext("2d");
|
||||
if (!ctx) {
|
||||
console.error("Could not get canvas context");
|
||||
return () => {};
|
||||
}
|
||||
|
||||
if (isStandalone) {
|
||||
canvas.style.backgroundColor = "#2F1C21";
|
||||
} else {
|
||||
canvas.style.backgroundColor = "transparent";
|
||||
}
|
||||
|
||||
let width = canvas.width;
|
||||
let height = canvas.height;
|
||||
let animationFrameId: number;
|
||||
let lastFrameTime = 0;
|
||||
|
||||
let mouseX = width / 2;
|
||||
let mouseY = height / 2;
|
||||
|
||||
let offset = config.spacing / 2;
|
||||
|
||||
const updateDimensions = () => {
|
||||
width = canvas.width = canvas.clientWidth;
|
||||
height = canvas.height = canvas.clientHeight;
|
||||
|
||||
mouseX = width / 2;
|
||||
mouseY = height / 2;
|
||||
};
|
||||
|
||||
const drawHeart = (x: number, y: number, size: number) => {
|
||||
const scale = size / 30;
|
||||
|
||||
ctx.save();
|
||||
ctx.translate(x, y);
|
||||
ctx.scale(scale, scale);
|
||||
|
||||
ctx.fillStyle = config.heartColor;
|
||||
ctx.fill(heart);
|
||||
|
||||
ctx.restore();
|
||||
};
|
||||
|
||||
const c = 400;
|
||||
const h = 40;
|
||||
const k = solveForK(c, h);
|
||||
|
||||
const drawHeartGrid = () => {
|
||||
ctx.clearRect(0, 0, width, height);
|
||||
|
||||
ctx.globalAlpha = config.heartOpacity;
|
||||
|
||||
const numRows = Math.ceil(height / config.spacing) + 1;
|
||||
|
||||
for (let row = 0; row < numRows; row++) {
|
||||
const direction = row % 2 === 0 ? 1 : -1;
|
||||
const rowOffset = (offset * direction) % config.spacing;
|
||||
|
||||
const posYInit = row * config.spacing + config.spacing / 2;
|
||||
|
||||
for (
|
||||
let posXInit = -config.spacing + rowOffset;
|
||||
posXInit < width + config.spacing;
|
||||
posXInit += config.spacing
|
||||
) {
|
||||
const dx = (posXInit + config.heartBaseSize / 2) - mouseX;
|
||||
const dy = (posYInit + config.heartBaseSize / 2) - mouseY;
|
||||
const distance = Math.sqrt(dx * dx + dy * dy);
|
||||
|
||||
const pushIntensity = asymmetricBump(distance, h, c, k, 0.00002);
|
||||
|
||||
const pushAngle = Math.atan2(dy, dx);
|
||||
|
||||
const pushDistanceX = pushIntensity * Math.cos(pushAngle);
|
||||
const pushDistanceY = pushIntensity * Math.sin(pushAngle);
|
||||
const posX = posXInit + pushDistanceX * 1;
|
||||
const posY = posYInit + pushDistanceY * 2;
|
||||
|
||||
const scaleFactor = config.heartScaleFunction(
|
||||
distance,
|
||||
config.mouseInfluenceRadius,
|
||||
);
|
||||
const heartSize = config.heartBaseSize * scaleFactor;
|
||||
|
||||
if (
|
||||
posX > -config.heartMaxSize &&
|
||||
posX < width + config.heartMaxSize &&
|
||||
posY > -config.heartMaxSize &&
|
||||
posY < height + config.heartMaxSize
|
||||
) {
|
||||
drawHeart(posX - heartSize / 2, posY - heartSize / 2, heartSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx.globalAlpha = 1.0;
|
||||
};
|
||||
|
||||
function solveForK(c: number, k: number) {
|
||||
// input -> f(x)=h*e^{(-k*(x-c)^{2})}
|
||||
// desired result is (0, 0.45). (0, 0) is unsolvable but 0.45px will round down to 0.
|
||||
//
|
||||
// solution: -\frac{\ln\left(\frac{0.45}{h}\right)}{c^{2}}
|
||||
return -Math.log(0.45 / h) / (c * c);
|
||||
}
|
||||
|
||||
function asymmetricBump(
|
||||
x: number,
|
||||
h: number,
|
||||
c: number,
|
||||
leftK: number,
|
||||
rightK: number,
|
||||
) {
|
||||
const k = (x <= c) ? leftK : rightK;
|
||||
return h * Math.exp(-k * Math.pow(x - c, 2));
|
||||
}
|
||||
|
||||
const updateOffset = (elapsed: number) => {
|
||||
offset += config.rowSpeed * elapsed;
|
||||
|
||||
if (offset > 1000000) {
|
||||
offset -= 1000000;
|
||||
}
|
||||
};
|
||||
|
||||
const animate = (currentTime: number) => {
|
||||
animationFrameId = requestAnimationFrame(animate);
|
||||
|
||||
const elapsed = currentTime - lastFrameTime;
|
||||
lastFrameTime = currentTime;
|
||||
|
||||
updateOffset(elapsed * 0.05);
|
||||
|
||||
drawHeartGrid();
|
||||
};
|
||||
|
||||
const handleMouseMove = (event: MouseEvent) => {
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
mouseX = event.clientX - rect.left;
|
||||
mouseY = event.clientY - rect.top;
|
||||
};
|
||||
|
||||
const handleTouchMove = (event: TouchEvent) => {
|
||||
if (event.touches.length > 0) {
|
||||
event.preventDefault();
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
mouseX = event.touches[0].clientX - rect.left;
|
||||
mouseY = event.touches[0].clientY - rect.top;
|
||||
}
|
||||
};
|
||||
|
||||
const init = () => {
|
||||
globalThis.addEventListener("resize", updateDimensions);
|
||||
document.addEventListener("mousemove", handleMouseMove);
|
||||
document.addEventListener("touchmove", handleTouchMove, { passive: false });
|
||||
|
||||
updateDimensions();
|
||||
|
||||
lastFrameTime = performance.now();
|
||||
animationFrameId = requestAnimationFrame(animate);
|
||||
};
|
||||
|
||||
init();
|
||||
|
||||
return () => {
|
||||
globalThis.removeEventListener("resize", updateDimensions);
|
||||
document.removeEventListener("mousemove", handleMouseMove);
|
||||
document.removeEventListener("touchmove", handleTouchMove);
|
||||
cancelAnimationFrame(animationFrameId);
|
||||
};
|
||||
};
|
251
src/file-viewer/scripts/canvas_2024.client.ts
Normal file
251
src/file-viewer/scripts/canvas_2024.client.ts
Normal file
|
@ -0,0 +1,251 @@
|
|||
// Vibe coded with AI
|
||||
(globalThis as any).canvas_2024 = function (canvas: HTMLCanvasElement) {
|
||||
const isStandalone = canvas.getAttribute("data-standalone") === "true";
|
||||
if (isStandalone) {
|
||||
canvas.parentElement!.style.backgroundColor = "black";
|
||||
}
|
||||
|
||||
const gl = canvas.getContext("webgl", {
|
||||
alpha: true,
|
||||
premultipliedAlpha: false,
|
||||
});
|
||||
if (!gl) {
|
||||
console.error("WebGL not supported");
|
||||
return () => {};
|
||||
}
|
||||
|
||||
canvas.style.imageRendering = "pixelated";
|
||||
canvas.style.opacity = isStandalone ? "0.3" : "0.15";
|
||||
|
||||
// Resize canvas to match display size
|
||||
const resize = () => {
|
||||
const displayWidth = Math.floor(
|
||||
(canvas.clientWidth || globalThis.innerWidth) / 3,
|
||||
);
|
||||
const displayHeight = Math.floor(
|
||||
(canvas.clientHeight || globalThis.innerHeight) / 3,
|
||||
);
|
||||
|
||||
if (canvas.width !== displayWidth || canvas.height !== displayHeight) {
|
||||
canvas.width = displayWidth;
|
||||
canvas.height = displayHeight;
|
||||
gl.viewport(0, 0, canvas.width, canvas.height);
|
||||
}
|
||||
};
|
||||
resize();
|
||||
|
||||
// Vertex shader (just passes coordinates)
|
||||
const vertexShaderSource = `
|
||||
attribute vec2 a_position;
|
||||
void main() {
|
||||
gl_Position = vec4(a_position, 0.0, 1.0);
|
||||
}
|
||||
`;
|
||||
|
||||
// Fragment shader creates random noise with higher opacity to ensure visibility
|
||||
const fragmentShaderSource = `
|
||||
precision mediump float;
|
||||
uniform float u_time;
|
||||
|
||||
float noise1(float seed1,float seed2){
|
||||
return(
|
||||
fract(seed1+12.34567*
|
||||
fract(100.*(abs(seed1*0.91)+seed2+94.68)*
|
||||
fract((abs(seed2*0.41)+45.46)*
|
||||
fract((abs(seed2)+757.21)*
|
||||
fract(seed1*0.0171))))))
|
||||
* 1.0038 - 0.00185;
|
||||
}
|
||||
|
||||
float n(float seed1, float seed2, float seed3){
|
||||
float buff1 = abs(seed1+100.81) + 1000.3;
|
||||
float buff2 = abs(seed2+100.45) + 1000.2;
|
||||
float buff3 = abs(noise1(seed1, seed2)+seed3) + 1000.1;
|
||||
buff1 = (buff3*fract(buff2*fract(buff1*fract(buff2*0.146))));
|
||||
buff2 = (buff2*fract(buff2*fract(buff1+buff2*fract(buff3*0.52))));
|
||||
buff1 = noise1(buff1, buff2);
|
||||
return(buff1);
|
||||
}
|
||||
|
||||
void main() {
|
||||
float noise = n(gl_FragCoord.x, gl_FragCoord.y, u_time);
|
||||
|
||||
gl_FragColor = vec4(1.0, 0.7, 0.7, 0.8*noise);
|
||||
}
|
||||
`;
|
||||
|
||||
// Create and compile shaders
|
||||
const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexShaderSource);
|
||||
const fragmentShader = createShader(
|
||||
gl,
|
||||
gl.FRAGMENT_SHADER,
|
||||
fragmentShaderSource,
|
||||
);
|
||||
|
||||
// Check if shader creation failed
|
||||
if (!vertexShader || !fragmentShader) {
|
||||
console.error("Failed to create shaders");
|
||||
return () => {};
|
||||
}
|
||||
|
||||
// Create program and link shaders
|
||||
const program = createProgram(gl, vertexShader, fragmentShader);
|
||||
|
||||
// Check if program creation failed
|
||||
if (!program) {
|
||||
console.error("Failed to create program");
|
||||
return () => {};
|
||||
}
|
||||
|
||||
// Get attribute and uniform locations
|
||||
const positionAttributeLocation = gl.getAttribLocation(program, "a_position");
|
||||
const timeUniformLocation = gl.getUniformLocation(program, "u_time");
|
||||
|
||||
// Create a position buffer for a rectangle covering the entire canvas
|
||||
const positionBuffer = gl.createBuffer();
|
||||
if (!positionBuffer) {
|
||||
console.error("Failed to create position buffer");
|
||||
return () => {};
|
||||
}
|
||||
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
|
||||
// Rectangle that covers the entire clip space
|
||||
const positions = [
|
||||
-1.0,
|
||||
-1.0, // bottom left
|
||||
1.0,
|
||||
-1.0, // bottom right
|
||||
-1.0,
|
||||
1.0, // top left
|
||||
1.0,
|
||||
1.0, // top right
|
||||
];
|
||||
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
|
||||
|
||||
// Set up blending
|
||||
gl.enable(gl.BLEND);
|
||||
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
// Fixed 24 FPS timing
|
||||
const FPS = 24;
|
||||
const FRAME_TIME = 1000 / FPS; // ms per frame
|
||||
|
||||
// Handle animation
|
||||
let animationTimerId: number;
|
||||
let startTime = Date.now();
|
||||
let lastFrameTime = 0;
|
||||
|
||||
const render = () => {
|
||||
// Get current time
|
||||
const currentTime = Date.now();
|
||||
const deltaTime = currentTime - lastFrameTime;
|
||||
|
||||
// Skip frame if it's too early (maintain 24 FPS)
|
||||
if (deltaTime < FRAME_TIME) {
|
||||
animationTimerId = globalThis.setTimeout(render, 0); // Check again ASAP but yield to browser
|
||||
return;
|
||||
}
|
||||
|
||||
// Update last frame time, accounting for any drift
|
||||
lastFrameTime = currentTime - (deltaTime % FRAME_TIME);
|
||||
|
||||
// Resize canvas if needed
|
||||
resize();
|
||||
|
||||
// Calculate elapsed time in seconds for animation
|
||||
const elapsedTime = (currentTime - startTime) / 1000;
|
||||
|
||||
// Clear the canvas with transparent black
|
||||
gl.clearColor(0, 0, 0, 0);
|
||||
gl.clear(gl.COLOR_BUFFER_BIT);
|
||||
|
||||
// Use our shader program
|
||||
gl.useProgram(program);
|
||||
|
||||
// Set up the position attribute
|
||||
gl.enableVertexAttribArray(positionAttributeLocation);
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
|
||||
gl.vertexAttribPointer(
|
||||
positionAttributeLocation,
|
||||
2, // 2 components per vertex
|
||||
gl.FLOAT, // data type
|
||||
false, // normalize
|
||||
0, // stride (0 = compute from size and type)
|
||||
0, // offset
|
||||
);
|
||||
|
||||
// Update time uniform for animation
|
||||
gl.uniform1f(timeUniformLocation, elapsedTime);
|
||||
|
||||
// Draw the rectangle (2 triangles)
|
||||
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
||||
|
||||
// Schedule next frame (aiming for 24 FPS)
|
||||
const timeToNextFrame = Math.max(
|
||||
0,
|
||||
FRAME_TIME - (Date.now() - currentTime),
|
||||
);
|
||||
animationTimerId = globalThis.setTimeout(render, timeToNextFrame);
|
||||
};
|
||||
|
||||
// Helper function to create shaders
|
||||
function createShader(
|
||||
gl: WebGLRenderingContext,
|
||||
type: number,
|
||||
source: string,
|
||||
): WebGLShader | null {
|
||||
const shader = gl.createShader(type);
|
||||
if (!shader) {
|
||||
console.error("Failed to create shader object");
|
||||
return null;
|
||||
}
|
||||
|
||||
gl.shaderSource(shader, source);
|
||||
gl.compileShader(shader);
|
||||
|
||||
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
||||
console.error("Shader compilation error:", gl.getShaderInfoLog(shader));
|
||||
gl.deleteShader(shader);
|
||||
return null;
|
||||
}
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
// Helper function to create program and link shaders
|
||||
function createProgram(
|
||||
gl: WebGLRenderingContext,
|
||||
vertexShader: WebGLShader,
|
||||
fragmentShader: WebGLShader,
|
||||
): WebGLProgram | null {
|
||||
const program = gl.createProgram();
|
||||
if (!program) {
|
||||
console.error("Failed to create program object");
|
||||
return null;
|
||||
}
|
||||
|
||||
gl.attachShader(program, vertexShader);
|
||||
gl.attachShader(program, fragmentShader);
|
||||
gl.linkProgram(program);
|
||||
|
||||
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
||||
console.error("Program linking error:", gl.getProgramInfoLog(program));
|
||||
return null;
|
||||
}
|
||||
|
||||
return program;
|
||||
}
|
||||
|
||||
// Start the rendering with initial timestamp
|
||||
lastFrameTime = Date.now();
|
||||
render();
|
||||
|
||||
// Return cleanup function
|
||||
return () => {
|
||||
clearTimeout(animationTimerId);
|
||||
if (program) gl.deleteProgram(program);
|
||||
if (vertexShader) gl.deleteShader(vertexShader);
|
||||
if (fragmentShader) gl.deleteShader(fragmentShader);
|
||||
if (positionBuffer) gl.deleteBuffer(positionBuffer);
|
||||
};
|
||||
};
|
197
src/file-viewer/scripts/canvas_cotyledon.client.ts
Normal file
197
src/file-viewer/scripts/canvas_cotyledon.client.ts
Normal file
|
@ -0,0 +1,197 @@
|
|||
// __esModule
|
||||
true
|
||||
|
||||
// computeNode
|
||||
function computeNode(node) {
|
||||
switch (node.type) {
|
||||
case "StringLiteral":
|
||||
case "NumericLiteral":
|
||||
case "BooleanLiteral":
|
||||
return { value: node.value };
|
||||
case "RegExpLiteral":
|
||||
return { value: new RegExp(node.pattern, node.flags) };
|
||||
case "NullLiteral":
|
||||
return { value: null };
|
||||
case "Identifier":
|
||||
switch (node.name) {
|
||||
case "undefined":
|
||||
return { value: undefined };
|
||||
case "NaN":
|
||||
return { value: NaN };
|
||||
case "Infinity":
|
||||
return { value: Infinity };
|
||||
default:
|
||||
return;
|
||||
}
|
||||
case "BigIntLiteral":
|
||||
return { value: BigInt(node.value) };
|
||||
case "ParenthesizedExpression":
|
||||
return computeNode(node.expression);
|
||||
case "BinaryExpression":{
|
||||
const left = computeNode(node.left);
|
||||
if (!left) return;
|
||||
const right = computeNode(node.right);
|
||||
if (!right) return;
|
||||
switch (node.operator) {
|
||||
case "+":
|
||||
return { value: left.value + right.value };
|
||||
case "-":
|
||||
return { value: left.value - right.value };
|
||||
case "*":
|
||||
return { value: left.value * right.value };
|
||||
case "/":
|
||||
return { value: left.value / right.value };
|
||||
case "%":
|
||||
return { value: left.value % right.value };
|
||||
case "**":
|
||||
return { value: left.value ** right.value };
|
||||
case "|":
|
||||
return { value: left.value | right.value };
|
||||
case "&":
|
||||
return { value: left.value & right.value };
|
||||
case "^":
|
||||
return { value: left.value ^ right.value };
|
||||
case "<<":
|
||||
return { value: left.value << right.value };
|
||||
case ">>":
|
||||
return { value: left.value >> right.value };
|
||||
case ">>>":
|
||||
return { value: left.value >>> right.value };
|
||||
case "==":
|
||||
return { value: left.value == right.value };
|
||||
case "!=":
|
||||
return { value: left.value != right.value };
|
||||
case "===":
|
||||
return { value: left.value === right.value };
|
||||
case "!==":
|
||||
return { value: left.value !== right.value };
|
||||
case "<":
|
||||
return { value: left.value < right.value };
|
||||
case "<=":
|
||||
return { value: left.value <= right.value };
|
||||
case ">":
|
||||
return { value: left.value > right.value };
|
||||
case ">=":
|
||||
return { value: left.value >= right.value };
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
case "UnaryExpression":{
|
||||
const arg = computeNode(node.argument);
|
||||
if (!arg) return;
|
||||
switch (node.operator) {
|
||||
case "+":
|
||||
return { value: +arg.value };
|
||||
case "-":
|
||||
return { value: -arg.value };
|
||||
case "~":
|
||||
return { value: ~arg.value };
|
||||
case "!":
|
||||
return { value: !arg.value };
|
||||
case "typeof":
|
||||
return { value: typeof arg.value };
|
||||
case "void":
|
||||
return { value: void arg.value };
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
case "LogicalExpression":{
|
||||
const left = computeNode(node.left);
|
||||
if (!left) return;
|
||||
const right = computeNode(node.right);
|
||||
if (!right) return;
|
||||
switch (node.operator) {
|
||||
case "&&":
|
||||
return { value: left.value && right.value };
|
||||
case "||":
|
||||
return { value: left.value || right.value };
|
||||
case "??":
|
||||
return { value: left.value ?? right.value };
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
case "ConditionalExpression":{
|
||||
const test = computeNode(node.test);
|
||||
if (!test) return;
|
||||
const consequent = computeNode(node.consequent);
|
||||
if (!consequent) return;
|
||||
const alternate = computeNode(node.alternate);
|
||||
if (!alternate) return;
|
||||
return { value: test.value ? consequent.value : alternate.value };
|
||||
}
|
||||
case "TemplateLiteral":{
|
||||
let value = node.quasis[0].value.cooked;
|
||||
for (let i = 0; i < node.expressions.length; i++) {
|
||||
const expr = computeNode(node.expressions[i]);
|
||||
if (!expr) return;
|
||||
value += expr.value + node.quasis[i + 1].value.cooked;
|
||||
}
|
||||
return { value };
|
||||
}
|
||||
case "ObjectExpression":{
|
||||
const value = {};
|
||||
for (const prop of node.properties) {
|
||||
if (prop.decorators) return;
|
||||
switch (prop.type) {
|
||||
case "ObjectProperty":{
|
||||
let key;
|
||||
if (prop.computed) {
|
||||
const keyNode = computeNode(prop.key);
|
||||
if (!keyNode) return;
|
||||
key = keyNode.value + "";
|
||||
} else {
|
||||
switch (prop.key.type) {
|
||||
case "Identifier":
|
||||
key = prop.key.name;
|
||||
break;
|
||||
case "StringLiteral":
|
||||
key = prop.key.value;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const propValue = computeNode(prop.value);
|
||||
if (!propValue) return;
|
||||
value[key] = propValue.value;
|
||||
break;
|
||||
}
|
||||
case "SpreadElement":{
|
||||
const arg = computeNode(prop.argument);
|
||||
if (!arg) return;
|
||||
Object.assign(value, arg.value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { value };
|
||||
}
|
||||
case "ArrayExpression":{
|
||||
const value = [];
|
||||
for (const elem of node.elements) {
|
||||
if (elem) {
|
||||
if (elem.type === "SpreadElement") {
|
||||
const arg = computeNode(elem.argument);
|
||||
if (typeof arg?.value?.[Symbol.iterator] !== "function") return;
|
||||
for (const item of arg.value) {
|
||||
value.push(item);
|
||||
}
|
||||
} else {
|
||||
const elemValue = computeNode(elem);
|
||||
if (!elemValue) return;
|
||||
value.push(elemValue.value);
|
||||
}
|
||||
} else {
|
||||
value.length++;
|
||||
}
|
||||
}
|
||||
|
||||
return { value };
|
||||
}
|
||||
}
|
||||
}
|
98
src/file-viewer/server/.gitignore
vendored
Normal file
98
src/file-viewer/server/.gitignore
vendored
Normal file
|
@ -0,0 +1,98 @@
|
|||
// default
|
||||
class ImportBuilder {
|
||||
constructor(importedSource, scope, hub) {
|
||||
this._statements = [];
|
||||
this._resultName = null;
|
||||
this._importedSource = void 0;
|
||||
this._scope = scope;
|
||||
this._hub = hub;
|
||||
this._importedSource = importedSource;
|
||||
}
|
||||
done() {
|
||||
return {
|
||||
statements: this._statements,
|
||||
resultName: this._resultName
|
||||
};
|
||||
}
|
||||
import() {
|
||||
this._statements.push(importDeclaration([], stringLiteral(this._importedSource)));
|
||||
return this;
|
||||
}
|
||||
require() {
|
||||
this._statements.push(expressionStatement(callExpression(identifier("require"), [stringLiteral(this._importedSource)])));
|
||||
return this;
|
||||
}
|
||||
namespace(name = "namespace") {
|
||||
const local = this._scope.generateUidIdentifier(name);
|
||||
const statement = this._statements[this._statements.length - 1];
|
||||
_assert(statement.type === "ImportDeclaration");
|
||||
_assert(statement.specifiers.length === 0);
|
||||
statement.specifiers = [importNamespaceSpecifier(local)];
|
||||
this._resultName = cloneNode(local);
|
||||
return this;
|
||||
}
|
||||
default(name) {
|
||||
const id = this._scope.generateUidIdentifier(name);
|
||||
const statement = this._statements[this._statements.length - 1];
|
||||
_assert(statement.type === "ImportDeclaration");
|
||||
_assert(statement.specifiers.length === 0);
|
||||
statement.specifiers = [importDefaultSpecifier(id)];
|
||||
this._resultName = cloneNode(id);
|
||||
return this;
|
||||
}
|
||||
named(name, importName) {
|
||||
if (importName === "default") return this.default(name);
|
||||
const id = this._scope.generateUidIdentifier(name);
|
||||
const statement = this._statements[this._statements.length - 1];
|
||||
_assert(statement.type === "ImportDeclaration");
|
||||
_assert(statement.specifiers.length === 0);
|
||||
statement.specifiers = [importSpecifier(id, identifier(importName))];
|
||||
this._resultName = cloneNode(id);
|
||||
return this;
|
||||
}
|
||||
var(name) {
|
||||
const id = this._scope.generateUidIdentifier(name);
|
||||
let statement = this._statements[this._statements.length - 1];
|
||||
if (statement.type !== "ExpressionStatement") {
|
||||
_assert(this._resultName);
|
||||
statement = expressionStatement(this._resultName);
|
||||
this._statements.push(statement);
|
||||
}
|
||||
this._statements[this._statements.length - 1] = variableDeclaration("var", [variableDeclarator(id, statement.expression)]);
|
||||
this._resultName = cloneNode(id);
|
||||
return this;
|
||||
}
|
||||
defaultInterop() {
|
||||
return this._interop(this._hub.addHelper("interopRequireDefault"));
|
||||
}
|
||||
wildcardInterop() {
|
||||
return this._interop(this._hub.addHelper("interopRequireWildcard"));
|
||||
}
|
||||
_interop(callee) {
|
||||
const statement = this._statements[this._statements.length - 1];
|
||||
if (statement.type === "ExpressionStatement") {
|
||||
statement.expression = callExpression(callee, [statement.expression]);
|
||||
} else if (statement.type === "VariableDeclaration") {
|
||||
_assert(statement.declarations.length === 1);
|
||||
statement.declarations[0].init = callExpression(callee, [statement.declarations[0].init]);
|
||||
} else {
|
||||
_assert.fail("Unexpected type.");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
prop(name) {
|
||||
const statement = this._statements[this._statements.length - 1];
|
||||
if (statement.type === "ExpressionStatement") {
|
||||
statement.expression = memberExpression(statement.expression, identifier(name));
|
||||
} else if (statement.type === "VariableDeclaration") {
|
||||
_assert(statement.declarations.length === 1);
|
||||
statement.declarations[0].init = memberExpression(statement.declarations[0].init, identifier(name));
|
||||
} else {
|
||||
_assert.fail("Unexpected type:" + statement.type);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
read(name) {
|
||||
this._resultName = memberExpression(this._resultName, identifier(name));
|
||||
}
|
||||
}
|
4
src/file-viewer/server/CHANGES.md
Normal file
4
src/file-viewer/server/CHANGES.md
Normal file
|
@ -0,0 +1,4 @@
|
|||
// default
|
||||
function isModule(path) {
|
||||
return path.node.sourceType === "module";
|
||||
}
|
12
src/file-viewer/server/CLAUDE.md
Normal file
12
src/file-viewer/server/CLAUDE.md
Normal file
|
@ -0,0 +1,12 @@
|
|||
// default
|
||||
function rewriteThis(programPath) {
|
||||
if (!rewriteThisVisitor) {
|
||||
rewriteThisVisitor = _traverse.visitors.environmentVisitor({
|
||||
ThisExpression(path) {
|
||||
path.replaceWith(_core.types.unaryExpression("void", _core.types.numericLiteral(0), true));
|
||||
}
|
||||
});
|
||||
rewriteThisVisitor.noScope = true;
|
||||
}
|
||||
(0, _traverse.default)(programPath.node, rewriteThisVisitor);
|
||||
}
|
61
src/file-viewer/server/Cargo.lock
generated
Normal file
61
src/file-viewer/server/Cargo.lock
generated
Normal file
|
@ -0,0 +1,61 @@
|
|||
// default
|
||||
function rewriteLiveReferences(programPath, metadata, wrapReference) {
|
||||
const imported = new Map();
|
||||
const exported = new Map();
|
||||
const requeueInParent = path => {
|
||||
programPath.requeue(path);
|
||||
};
|
||||
for (const [source, data] of metadata.source) {
|
||||
for (const [localName, importName] of data.imports) {
|
||||
imported.set(localName, [source, importName, null]);
|
||||
}
|
||||
for (const localName of data.importsNamespace) {
|
||||
imported.set(localName, [source, null, localName]);
|
||||
}
|
||||
}
|
||||
for (const [local, data] of metadata.local) {
|
||||
let exportMeta = exported.get(local);
|
||||
if (!exportMeta) {
|
||||
exportMeta = [];
|
||||
exported.set(local, exportMeta);
|
||||
}
|
||||
exportMeta.push(...data.names);
|
||||
}
|
||||
const rewriteBindingInitVisitorState = {
|
||||
metadata,
|
||||
requeueInParent,
|
||||
scope: programPath.scope,
|
||||
exported
|
||||
};
|
||||
programPath.traverse(rewriteBindingInitVisitor, rewriteBindingInitVisitorState);
|
||||
const rewriteReferencesVisitorState = {
|
||||
seen: new WeakSet(),
|
||||
metadata,
|
||||
requeueInParent,
|
||||
scope: programPath.scope,
|
||||
imported,
|
||||
exported,
|
||||
buildImportReference([source, importName, localName], identNode) {
|
||||
const meta = metadata.source.get(source);
|
||||
meta.referenced = true;
|
||||
if (localName) {
|
||||
if (meta.wrap) {
|
||||
var _wrapReference;
|
||||
identNode = (_wrapReference = wrapReference(identNode, meta.wrap)) != null ? _wrapReference : identNode;
|
||||
}
|
||||
return identNode;
|
||||
}
|
||||
let namespace = _core.types.identifier(meta.name);
|
||||
if (meta.wrap) {
|
||||
var _wrapReference2;
|
||||
namespace = (_wrapReference2 = wrapReference(namespace, meta.wrap)) != null ? _wrapReference2 : namespace;
|
||||
}
|
||||
if (importName === "default" && meta.interop === "node-default") {
|
||||
return namespace;
|
||||
}
|
||||
const computed = metadata.stringSpecifiers.has(importName);
|
||||
return _core.types.memberExpression(namespace, computed ? _core.types.stringLiteral(importName) : _core.types.identifier(importName), computed);
|
||||
}
|
||||
};
|
||||
programPath.traverse(rewriteReferencesVisitor, rewriteReferencesVisitorState);
|
||||
}
|
69
src/file-viewer/server/Cargo.toml
Normal file
69
src/file-viewer/server/Cargo.toml
Normal file
|
@ -0,0 +1,69 @@
|
|||
// default
|
||||
function normalizeModuleAndLoadMetadata(programPath, exportName, {
|
||||
importInterop,
|
||||
initializeReexports = false,
|
||||
getWrapperPayload,
|
||||
esNamespaceOnly = false,
|
||||
filename
|
||||
}) {
|
||||
if (!exportName) {
|
||||
exportName = programPath.scope.generateUidIdentifier("exports").name;
|
||||
}
|
||||
const stringSpecifiers = new Set();
|
||||
nameAnonymousExports(programPath);
|
||||
const {
|
||||
local,
|
||||
sources,
|
||||
hasExports
|
||||
} = getModuleMetadata(programPath, {
|
||||
initializeReexports,
|
||||
getWrapperPayload
|
||||
}, stringSpecifiers);
|
||||
removeImportExportDeclarations(programPath);
|
||||
for (const [source, metadata] of sources) {
|
||||
const {
|
||||
importsNamespace,
|
||||
imports
|
||||
} = metadata;
|
||||
if (importsNamespace.size > 0 && imports.size === 0) {
|
||||
const [nameOfnamespace] = importsNamespace;
|
||||
metadata.name = nameOfnamespace;
|
||||
}
|
||||
const resolvedInterop = resolveImportInterop(importInterop, source, filename);
|
||||
if (resolvedInterop === "none") {
|
||||
metadata.interop = "none";
|
||||
} else if (resolvedInterop === "node" && metadata.interop === "namespace") {
|
||||
metadata.interop = "node-namespace";
|
||||
} else if (resolvedInterop === "node" && metadata.interop === "default") {
|
||||
metadata.interop = "node-default";
|
||||
} else if (esNamespaceOnly && metadata.interop === "namespace") {
|
||||
metadata.interop = "default";
|
||||
}
|
||||
}
|
||||
return {
|
||||
exportName,
|
||||
exportNameListName: null,
|
||||
hasExports,
|
||||
local,
|
||||
source: sources,
|
||||
stringSpecifiers
|
||||
};
|
||||
}
|
||||
|
||||
// hasExports
|
||||
function hasExports(metadata) {
|
||||
return metadata.hasExports;
|
||||
}
|
||||
|
||||
// isSideEffectImport
|
||||
function isSideEffectImport(source) {
|
||||
return source.imports.size === 0 && source.importsNamespace.size === 0 && source.reexports.size === 0 && source.reexportNamespace.size === 0 && !source.reexportAll;
|
||||
}
|
||||
|
||||
// validateImportInteropOption
|
||||
function validateImportInteropOption(importInterop) {
|
||||
if (typeof importInterop !== "function" && importInterop !== "none" && importInterop !== "babel" && importInterop !== "node") {
|
||||
throw new Error(`.importInterop must be one of "none", "babel", "node", or a function returning one of those values (received ${importInterop}).`);
|
||||
}
|
||||
return importInterop;
|
||||
}
|
23
src/file-viewer/server/DOCKER.md
Normal file
23
src/file-viewer/server/DOCKER.md
Normal file
|
@ -0,0 +1,23 @@
|
|||
// toGetWrapperPayload
|
||||
function toGetWrapperPayload(lazy) {
|
||||
return (source, metadata) => {
|
||||
if (lazy === false) return null;
|
||||
if ((0, _normalizeAndLoadMetadata.isSideEffectImport)(metadata) || metadata.reexportAll) return null;
|
||||
if (lazy === true) {
|
||||
return source.includes(".") ? null : "lazy";
|
||||
}
|
||||
if (Array.isArray(lazy)) {
|
||||
return !lazy.includes(source) ? null : "lazy";
|
||||
}
|
||||
if (typeof lazy === "function") {
|
||||
return lazy(source) ? "lazy" : null;
|
||||
}
|
||||
throw new Error(`.lazy must be a boolean, string array, or function`);
|
||||
};
|
||||
}
|
||||
|
||||
// wrapReference
|
||||
function wrapReference(ref, payload) {
|
||||
if (payload === "lazy") return _core.types.callExpression(ref, []);
|
||||
return null;
|
||||
}
|
40
src/file-viewer/server/Dockerfile
Normal file
40
src/file-viewer/server/Dockerfile
Normal file
|
@ -0,0 +1,40 @@
|
|||
// buildDynamicImport
|
||||
function buildDynamicImport(node, deferToThen, wrapWithPromise, builder) {
|
||||
const specifier = _core.types.isCallExpression(node) ? node.arguments[0] : node.source;
|
||||
if (_core.types.isStringLiteral(specifier) || _core.types.isTemplateLiteral(specifier) && specifier.quasis.length === 0) {
|
||||
if (deferToThen) {
|
||||
return _core.template.expression.ast`
|
||||
Promise.resolve().then(() => ${builder(specifier)})
|
||||
`;
|
||||
} else return builder(specifier);
|
||||
}
|
||||
const specifierToString = _core.types.isTemplateLiteral(specifier) ? _core.types.identifier("specifier") : _core.types.templateLiteral([_core.types.templateElement({
|
||||
raw: ""
|
||||
}), _core.types.templateElement({
|
||||
raw: ""
|
||||
})], [_core.types.identifier("specifier")]);
|
||||
if (deferToThen) {
|
||||
return _core.template.expression.ast`
|
||||
(specifier =>
|
||||
new Promise(r => r(${specifierToString}))
|
||||
.then(s => ${builder(_core.types.identifier("s"))})
|
||||
)(${specifier})
|
||||
`;
|
||||
} else if (wrapWithPromise) {
|
||||
return _core.template.expression.ast`
|
||||
(specifier =>
|
||||
new Promise(r => r(${builder(specifierToString)}))
|
||||
)(${specifier})
|
||||
`;
|
||||
} else {
|
||||
return _core.template.expression.ast`
|
||||
(specifier => ${builder(specifierToString)})(${specifier})
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
// getDynamicImportSource
|
||||
function getDynamicImportSource(node) {
|
||||
const [source] = node.arguments;
|
||||
return _core.types.isStringLiteral(source) || _core.types.isTemplateLiteral(source) ? source : _core.template.expression.ast`\`\${${source}}\``;
|
||||
}
|
10
src/file-viewer/server/README.md
Normal file
10
src/file-viewer/server/README.md
Normal file
|
@ -0,0 +1,10 @@
|
|||
// default
|
||||
function getModuleName(rootOpts, pluginOpts) {
|
||||
var _pluginOpts$moduleId, _pluginOpts$moduleIds, _pluginOpts$getModule, _pluginOpts$moduleRoo;
|
||||
return originalGetModuleName(rootOpts, {
|
||||
moduleId: (_pluginOpts$moduleId = pluginOpts.moduleId) != null ? _pluginOpts$moduleId : rootOpts.moduleId,
|
||||
moduleIds: (_pluginOpts$moduleIds = pluginOpts.moduleIds) != null ? _pluginOpts$moduleIds : rootOpts.moduleIds,
|
||||
getModuleId: (_pluginOpts$getModule = pluginOpts.getModuleId) != null ? _pluginOpts$getModule : rootOpts.getModuleId,
|
||||
moduleRoot: (_pluginOpts$moduleRoo = pluginOpts.moduleRoot) != null ? _pluginOpts$moduleRoo : rootOpts.moduleRoot
|
||||
});
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue