Skip to content

Commit

Permalink
chore: add pages worker
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei0x309 committed Sep 18, 2024
1 parent 45a54be commit 64409c6
Show file tree
Hide file tree
Showing 9 changed files with 1,127 additions and 119 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ yarn-error.log*
.turbo
**/dist/**
**/node_modules/**

# cloudflare
.wrangler
5 changes: 4 additions & 1 deletion apps/yup-live/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
"lint": "eslint --ext .ts,.vue --ignore-path .gitignore --fix src&&prettier . -w -u",
"build-no-ssg": "vite build",
"build-ssg": "vite-ssg build",
"build-worker": "bun build ./worker/worker.ts --outfile=./dist/_worker.js",
"encrypt": "node ./scripts/build-encrypt.mjs --env-file=.env",
"preview": "vite preview",
"build": "yarn build-ssg"
"build": "yarn build-ssg && yarn build-worker"
},
"husky": {
"hooks": {
Expand All @@ -22,6 +23,7 @@
"*": "prettier -w -u"
},
"dependencies": {
"@airstack/frames": "^1.2.0",
"@ethersproject/abstract-signer": "^5.7.0",
"@noble/hashes": "^1.3.0",
"@oruga-ui/oruga-next": "^0.7.0",
Expand All @@ -46,6 +48,7 @@
"vue-router": "^4.2.5"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240909.0",
"@types/d3": "^7.4.0",
"@types/node": "^18.19.6",
"@types/video.js": "^7.3.56",
Expand Down
6 changes: 3 additions & 3 deletions apps/yup-live/src/components/content/post/crossPost.vue
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ export default defineComponent({
let searchTerm = "";
do {
searchTerm = searchString;
result = await searchChannel(searchString);
result = (await searchChannel(searchString)) as TChannel[];
} while (searchTerm !== searchString);
channels.value = result;
isChannelSearching.value = false;
Expand Down Expand Up @@ -588,7 +588,7 @@ export default defineComponent({
if (!imageFile) return;
isFileUploading.value = true;
const imageBase64 = await fileToBase64(imageFile);
const upload = await mediaUpload(store, postPlatforms.value, imageFile);
const upload = (await mediaUpload(store, postPlatforms.value, imageFile)) as any;

if (upload?.errors) {
const platforms = [] as TPlatform[];
Expand Down Expand Up @@ -637,7 +637,7 @@ export default defineComponent({
);
postPlatforms.value = postPlatforms.value.filter((p) => p !== "bsky");
}
const upload = await mediaUpload(store, postPlatforms.value, videoFile);
const upload = (await mediaUpload(store, postPlatforms.value, videoFile)) as any;
if (upload?.errors) {
const platforms = [] as TPlatform[];
for (const error of upload.errors) {
Expand Down
4 changes: 3 additions & 1 deletion apps/yup-live/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},
"types": ["@cloudflare/workers-types/2023-07-01"]
},
"include": [
"./src/**/*.js",
Expand All @@ -24,6 +25,7 @@
"./src/**/*.tsx",
"./src/**/*.vue",
"./src/**/*.vue",
"./worker/**/*.ts",
"../../packages/icons/src/**/*.vue",
"../../packages/shared/src/**/*.*",
"../../packages/components/**/*.vue"
Expand Down
19 changes: 19 additions & 0 deletions apps/yup-live/worker/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface T_FRAME_API_BODY {
untrustedData: {
fid: number
url: string
messageHash: string
timestamp: number
network: number
buttonIndex: number
inputText: string
castId: {
fid: number
hash: string
}
state: string
}
trustedData: {
messageBytes: string
}
}
18 changes: 18 additions & 0 deletions apps/yup-live/worker/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
validateFramesMessage as verifyMessageWithAirstack,
ValidateFramesMessageInput,
init
} from '@airstack/frames'

import type { T_FRAME_API_BODY } from './types'


export const verifyMessage = async (body: T_FRAME_API_BODY, AIRSTACK_KEY: string): Promise<{ valid: boolean }> => {
try {
init(AIRSTACK_KEY)
const verify = await verifyMessageWithAirstack(body as ValidateFramesMessageInput)
return { valid: verify.isValid }
} catch (e) {
return { valid: false }
}
}
43 changes: 43 additions & 0 deletions apps/yup-live/worker/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-disable no-undef */
import { verifyMessage } from './utils'
import type { T_FRAME_API_BODY } from './types'


interface Env {
ASSETS: Fetcher;
AIRSTACK_KEY: string;
}

export default {
async fetch (request, env): Promise<Response> {
const url = new URL(request.url);
if (url.pathname.startsWith('/api/miniapp')) {
// get url from query
const url = new URL(request.url);
const query = url.searchParams;
const urlToFetch = query.get('url');

// check if method is POST
if (request.method === 'POST') {
const body = await request.json() as T_FRAME_API_BODY;
verifyMessage(body, env.AIRSTACK_KEY)
}

const jsonToSend = {
"type": "form",
"title": "Yup Link",
"url": urlToFetch,
}

return new Response(JSON.stringify(jsonToSend), {
headers: {
'Content-Type': 'application/json',
},
});

}
// Otherwise, serve the static assets.
// Without this, the Worker will error and no assets will be served.
return env.ASSETS.fetch(request);
},
} satisfies ExportedHandler<Env>;
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
},
"engines": {
"node": ">=16.0.0"
},
"dependencies": {
"wrangler": "^3.78.5"
}
}
Loading

0 comments on commit 64409c6

Please sign in to comment.