Friends function
All checks were successful
Lint / lint (push) Successful in 21s

This commit is contained in:
Gabriel Simmer 2024-04-15 23:49:41 +01:00
parent 4ac67b96ab
commit 3bd6a6586b
Signed by: arch
SSH key fingerprint: SHA256:m3OEcdtrnBpMX+2BDGh/byv3hrCekCLzDYMdvGEKPPQ
10 changed files with 3261 additions and 0 deletions

12
friends/.editorconfig Normal file
View file

@ -0,0 +1,12 @@
# http://editorconfig.org
root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.yml]
indent_style = space

172
friends/.gitignore vendored Normal file
View file

@ -0,0 +1,172 @@
# Logs
logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Runtime data
pids
_.pid
_.seed
\*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
\*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
\*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
\*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*
# wrangler project
.dev.vars
.wrangler/

6
friends/.prettierrc Normal file
View file

@ -0,0 +1,6 @@
{
"printWidth": 140,
"singleQuote": true,
"semi": true,
"useTabs": true
}

2930
friends/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

16
friends/package.json Normal file
View file

@ -0,0 +1,16 @@
{
"name": "friends",
"version": "0.0.0",
"private": true,
"scripts": {
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"start": "wrangler dev",
"test": "vitest"
},
"devDependencies": {
"wrangler": "^3.0.0",
"vitest": "1.3.0",
"@cloudflare/vitest-pool-workers": "^0.1.0"
}
}

20
friends/src/index.html Normal file
View file

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="I'm a silly dog">
<title>friends of the arch.dog</title>
<link rel="stylesheet" href="/styles.css">
</head>
<body>
<main><div id="error"></div>
<header>
<img src="/avatar.png" alt="A cute golden retriever with a stylish blue hairstyle and glasses">
<h1>Friends of Arch</h1>
</header>
<p>A collection of wonderful beans across the internet.</p>
<div id="friends"></div>
</main>
</body>
</html>

54
friends/src/index.js Normal file
View file

@ -0,0 +1,54 @@
import friendsPage from "./index.html";
export default {
async fetch(request, env, ctx) {
const renderFriends = async (error) => {
const friends = await env.FRIENDS.list();
const friendList = await Promise.all(friends.keys.map(async f => {
if (f.metadata && f.metadata.noImage) {
const value = await env.FRIENDS.get(f.name);
return `<a href="https://${f.name}"><p>${value}</p></a>`
}
return `<a href="https://${f.name}"><img src="./${f.name}" alt="${f.name}"></a>`
}));
return new HTMLRewriter()
.on("#friends", {
element(element) {
element.setInnerContent(friendList.join("\n"), { html: true });
},
})
.on("#error", {
element(element) {
if (error) {
element.setInnerContent(error);
} else {
element.remove();
}
},
})
.transform(
new Response(friendsPage, { headers: { "Content-Type": "text/html" } }),
);
};
const url = new URL(request.url);
const { pathname } = url;
const friend = pathname.replace("/friends", "", 1);
switch (friend) {
case "":
return Response.redirect("https://arch.dog/friends/", 301);
case "/":
return await renderFriends()
default:
const { value, metadata } = await env.FRIENDS.getWithMetadata(friend.replace("/", "", 1), { type: "stream" });
if (value === null) {
return new Response("not found", {status: 404});
}
return new Response(value, {});
}
},
};

View file

@ -0,0 +1,20 @@
import { env, createExecutionContext, waitOnExecutionContext, SELF } from "cloudflare:test";
import { describe, it, expect } from "vitest";
import worker from "../src";
describe("Hello World worker", () => {
it("responds with Hello World! (unit style)", async () => {
const request = new Request("http://example.com");
// Create an empty context to pass to `worker.fetch()`.
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
// Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions
await waitOnExecutionContext(ctx);
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
});
it("responds with Hello World! (integration style)", async () => {
const response = await SELF.fetch("http://example.com");
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
});
});

11
friends/vitest.config.js Normal file
View file

@ -0,0 +1,11 @@
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
export default defineWorkersConfig({
test: {
poolOptions: {
workers: {
wrangler: { configPath: "./wrangler.toml" },
},
},
},
});

20
friends/wrangler.toml Normal file
View file

@ -0,0 +1,20 @@
#:schema node_modules/wrangler/config-schema.json
name = "friends"
main = "src/index.js"
compatibility_date = "2024-04-05"
compatibility_flags = ["nodejs_compat"]
workers_dev = false
[limits]
cpu_ms = 10
kv_namespaces = [
{ binding = "FRIENDS", id = "4c7b1469a4cb4050b0c8b8b41a7396aa", preview_id = "4c7b1469a4cb4050b0c8b8b41a7396aa" }
]
routes = [
{ pattern = "arch.dog/friends", zone_name = "arch.dog" },
{ pattern = "arch.dog/friends/*", zone_name = "arch.dog" }
]