Skip to content

Commit

Permalink
ci: add test and ci (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn authored Jul 18, 2024
1 parent 286f14e commit 27e7699
Show file tree
Hide file tree
Showing 29 changed files with 838 additions and 610 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
root = true

[*]
indent_style = tab
indent_style = space
tab_width = 2
end_of_line = lf
charset = utf-8
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: CI

on:
pull_request:
push:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: setup pnpm
uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: pnpm install
- run: pnpm run test
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pnpm-lock.yaml
6 changes: 0 additions & 6 deletions .prettierrc

This file was deleted.

56 changes: 31 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
{
"name": "@affine/workers",
"version": "0.1.0",
"private": true,
"author": "DarkSky <[email protected]>",
"license": "MPL-2.0",
"type": "module",
"scripts": {
"dev": "pnpm --filter @affine/worker dev",
"deploy": "pnpm run --filter @affine/worker deploy",
"format": "pnpm prettier --write .",
"create-d1": "wrangler d1 create affine-worker",
"create-d1-schema": "pnpm run -r migrate:online"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20231218.0",
"better-sqlite3": "^8.7.0",
"prettier": "3.1.1",
"typescript": "^5.3.3"
},
"packageManager": "[email protected]",
"pnpm": {
"overrides": {
"side-channel": "npm:@nolyfill/side-channel@latest"
}
}
"name": "@affine/workers",
"version": "0.1.0",
"private": true,
"author": "DarkSky <[email protected]>",
"license": "MPL-2.0",
"type": "module",
"scripts": {
"dev": "pnpm --filter @affine/worker dev",
"deploy": "pnpm run --filter @affine/worker deploy",
"format": "pnpm prettier --write .",
"create-d1": "wrangler d1 create affine-worker",
"create-d1-schema": "pnpm run -r migrate:online",
"test": "pnpm test --recursive"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20231218.0",
"better-sqlite3": "^8.7.0",
"prettier": "3.1.1",
"typescript": "^5.3.3"
},
"packageManager": "[email protected]",
"prettier": {
"printWidth": 140,
"singleQuote": true,
"semi": true
},
"pnpm": {
"overrides": {
"side-channel": "npm:@nolyfill/side-channel@latest"
}
}
}
15 changes: 15 additions & 0 deletions packages/image-proxy/__test__/image-proxy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { test } from 'node:test';
import assert from 'node:assert';

import { imageProxy } from '../src/index.js';

await test('image-proxy', async () => {
const res = await imageProxy(
new Request('https://affine.pro', {
headers: {
origin: 'https://google.com',
},
}),
);
assert.strictEqual(res.status, 404, "Should return 404 if origin isn't allowed");
});
34 changes: 18 additions & 16 deletions packages/image-proxy/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
{
"name": "@affine/image-proxy",
"version": "0.0.1",
"author": "DarkSky <[email protected]>",
"license": "MPL-2.0",
"main": "src/index.ts",
"type": "module",
"private": true,
"scripts": {},
"devDependencies": {
"@affine/utils": "workspace:*",
"@cloudflare/workers-types": "^4.20231218.0",
"@types/node": "^20.10.5",
"itty-router": "4.0.25",
"tldts": "^6.1.1",
"typescript": "^5.3.3"
}
"name": "@affine/image-proxy",
"version": "0.0.1",
"author": "DarkSky <[email protected]>",
"license": "MPL-2.0",
"main": "src/index.ts",
"type": "module",
"private": true,
"scripts": {
"test": "node --import @oxc-node/core/register --test __test__/**"
},
"devDependencies": {
"@affine/utils": "workspace:*",
"@cloudflare/workers-types": "^4.20231218.0",
"@oxc-node/core": "^0.0.10",
"@types/node": "^20.10.5",
"tldts": "^6.1.1",
"typescript": "^5.3.3"
}
}
81 changes: 40 additions & 41 deletions packages/image-proxy/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
import { cloneHeader, fixUrl, isOriginAllowed, isRefererAllowed, log, respBadRequest, respNotFound } from '@affine/utils';
import type { IRequest } from 'itty-router';

export async function imageProxy(request: IRequest) {
const origin = request.headers.get('Origin') ?? '';
const referer = request.headers.get('Referer') ?? '';
if (!isOriginAllowed(origin) && !isRefererAllowed(referer)) {
log('Invalid Origin', 'ERROR', { origin, referer });
return respNotFound();
}
export async function imageProxy(request: Request) {
const origin = request.headers.get('Origin') ?? '';
const referer = request.headers.get('Referer') ?? '';
if (!isOriginAllowed(origin) && !isRefererAllowed(referer)) {
log('Invalid Origin', 'ERROR', { origin, referer });
return respNotFound();
}

const url = new URL(request.url);
const imageURL = url.searchParams.get('url');
if (!imageURL) {
return respBadRequest('Missing "url" parameter');
}
const url = new URL(request.url);
const imageURL = url.searchParams.get('url');
if (!imageURL) {
return respBadRequest('Missing "url" parameter');
}

const targetURL = fixUrl(imageURL);
if (!targetURL) {
log('Invalid URL', 'ERROR', { origin, url: imageURL });
return respBadRequest('Invalid URL', { allowOrigin: origin });
}
const targetURL = fixUrl(imageURL);
if (!targetURL) {
log('Invalid URL', 'ERROR', { origin, url: imageURL });
return respBadRequest('Invalid URL', { allowOrigin: origin });
}

const imageRequest = new Request(targetURL.toString(), {
method: 'GET',
headers: cloneHeader(request.headers),
});
const imageRequest = new Request(targetURL.toString(), {
method: 'GET',
headers: cloneHeader(request.headers),
});

const accept = request.headers.get('accept');
const response = await fetch(imageRequest, {
cf: {
image: {
fit: 'scale-down',
width: 1280,
format: accept && /image\/avif/.test(accept) ? 'avif' : 'webp',
},
},
});
const modifiedResponse = new Response(response.body);
modifiedResponse.headers.set('Access-Control-Allow-Origin', request.headers.get('Origin') ?? 'null');
modifiedResponse.headers.set('Vary', 'Origin');
modifiedResponse.headers.set('Access-Control-Allow-Methods', 'GET');
const contentType = response.headers.get('Content-Type');
contentType && modifiedResponse.headers.set('Content-Type', contentType);
const contentDisposition = response.headers.get('Content-Disposition');
contentDisposition && modifiedResponse.headers.set('Content-Disposition', contentDisposition);
return modifiedResponse;
const accept = request.headers.get('accept');
const response = await fetch(imageRequest, {
cf: {
image: {
fit: 'scale-down',
width: 1280,
format: accept && /image\/avif/.test(accept) ? 'avif' : 'webp',
},
},
});
const modifiedResponse = new Response(response.body);
modifiedResponse.headers.set('Access-Control-Allow-Origin', request.headers.get('Origin') ?? 'null');
modifiedResponse.headers.set('Vary', 'Origin');
modifiedResponse.headers.set('Access-Control-Allow-Methods', 'GET');
const contentType = response.headers.get('Content-Type');
contentType && modifiedResponse.headers.set('Content-Type', contentType);
const contentDisposition = response.headers.get('Content-Disposition');
contentDisposition && modifiedResponse.headers.set('Content-Disposition', contentDisposition);
return modifiedResponse;
}
12 changes: 6 additions & 6 deletions packages/image-proxy/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src/"
},
"include": ["./src"],
"references": []
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src/"
},
"include": ["./src"],
"references": []
}
34 changes: 17 additions & 17 deletions packages/link-preview/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ A worker used to parse page metadata based on Open Graph Protocol.

```ts
interface ResponseData {
url: string;
title?: string;
siteName?: string;
description?: string;
images?: string[];
mediaType?: string;
contentType?: string;
charset?: string;
videos?: string[];
favicons?: string[];
url: string;
title?: string;
siteName?: string;
description?: string;
images?: string[];
mediaType?: string;
contentType?: string;
charset?: string;
videos?: string[];
favicons?: string[];
}
```

Expand All @@ -22,13 +22,13 @@ interface ResponseData {
```ts
const url = 'https://github.com/toeverything/affine-workers';
const response = await fetch('https://affine-worker.toeverything.workers.dev/api/linkPreview', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
url,
}),
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
url,
}),
});
const data = await response.json();
```
27 changes: 13 additions & 14 deletions packages/link-preview/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
{
"name": "@affine/link-preview",
"version": "0.0.1",
"author": "Flrande <[email protected]>",
"license": "MPL-2.0",
"main": "src/index.ts",
"type": "module",
"private": true,
"devDependencies": {
"@affine/utils": "workspace:*",
"@cloudflare/workers-types": "^4.20231218.0",
"@types/node": "^20.10.5",
"itty-router": "4.0.25",
"typescript": "^5.3.3"
}
"name": "@affine/link-preview",
"version": "0.0.1",
"author": "Flrande <[email protected]>",
"license": "MPL-2.0",
"main": "src/index.ts",
"type": "module",
"private": true,
"devDependencies": {
"@affine/utils": "workspace:*",
"@cloudflare/workers-types": "^4.20231218.0",
"@types/node": "^20.10.5",
"typescript": "^5.3.3"
}
}
Loading

0 comments on commit 27e7699

Please sign in to comment.