-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
286f14e
commit 27e7699
Showing
29 changed files
with
838 additions
and
610 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pnpm-lock.yaml |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.