Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests #17

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .codesandbox/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
// These tasks will run in order when initializing your CodeSandbox project.
"setupTasks": [
{
"name": "Install node 18",
"command": "nvm install 18"
},
{
"name": "Install Dependencies",
"command": "yarn install"
}
],

// These tasks can be run from CodeSandbox. Running one will open a log in the app.
"tasks": {
"d": {
"name": "d",
"command": "yarn d",
"runAtStart": false
},
"start": {
"name": "start",
"command": "yarn start",
"runAtStart": true
},
"build": {
"name": "build",
"command": "yarn build",
"runAtStart": false
},
"preview": {
"name": "preview",
"command": "yarn preview",
"runAtStart": false
},
"test": {
"name": "test",
"command": "yarn test",
"runAtStart": false
},
"coverage": {
"name": "coverage",
"command": "yarn coverage",
"runAtStart": false
}
}
}
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"import/resolver": {
"typescript": true,
"node": true
},
"jest": {
"version": 27
}
},
"rules": {
Expand Down
7 changes: 7 additions & 0 deletions api/__tests__/__snapshots__/image2data.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Vitest Snapshot v1

exports[`api/image2data > should generate correct string 1`] = `
{
"dataUrl": "data:text/html; charset=UTF-8;base64,PCFET0NUWVBFIGh0bWw+CjwhLS1baWYgbHQgSUUgN10+IDxodG1sIGNsYXNzPSJuby1qcyBpZTYgb2xkaWUiIGxhbmc9ImVuLVVTIj4gPCFbZW5kaWZdLS0+CjwhLS1baWYg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BiJiYoYi5jbGFzc0xpc3QucmVtb3ZlKCJoaWRkZW4iKSxjLmFkZEV2ZW50TGlzdGVuZXIoImNsaWNrIixmdW5jdGlvbigpe2MuY2xhc3NMaXN0LmFkZCgiaGlkZGVuIik7YS5nZXRFbGVtZW50QnlJZCgiY2YtZm9vdGVyLWlwIikuY2xhc3NMaXN0LnJlbW92ZSgiaGlkZGVuIil9KSl9dmFyIGE9ZG9jdW1lbnQ7ZG9jdW1lbnQuYWRkRXZlbnRMaXN0ZW5lciYmYS5hZGRFdmVudExpc3RlbmVyKCJET01Db250ZW50TG9hZGVkIixkKX0pKCk7PC9zY3JpcHQ+CjwvZGl2PjwhLS0gLy5lcnJvci1mb290ZXIgLS0+CgoKICAgIDwvZGl2PjwhLS0gLyNjZi1lcnJvci1kZXRhaWxzIC0tPgogIDwvZGl2PjwhLS0gLyNjZi13cmFwcGVyIC0tPgoKICA8c2NyaXB0PgogIHdpbmRvdy5fY2ZfdHJhbnNsYXRpb24gPSB7fTsKICAKICAKPC9zY3JpcHQ+Cgo8L2JvZHk+CjwvaHRtbD4K",
}
`;
34 changes: 34 additions & 0 deletions api/__tests__/image2data.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { kill as killChild } from "node:process";
import { join } from "node:path";

// @ts-ignore
import { startDevServer } from "@vercel/node";

describe("api/image2data", () => {
let pid: number;
let port: number;

beforeAll(async () => {
const devServer: { pid: number; port: number } = await startDevServer({
entrypoint: "image2data.ts",
workPath: join(__dirname, ".."),
config: {},
});

console.log(__dirname, devServer);

({ pid, port } = devServer);
});

afterAll(() => {
killChild(pid);
});

it("should generate correct string", async () => {
const data = await fetch(
`http://localhost:${port}/api/image2data?url=https://via.placeholder.com/150`
);

await expect(data.json()).resolves.toMatchSnapshot();
});
});
11 changes: 9 additions & 2 deletions api/image2data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ServerResponse } from 'node:http';

import type { VercelRequest } from '@vercel/node';
import { fileTypeFromBuffer } from 'file-type'

interface RequestData {
url?: string;
Expand All @@ -21,8 +22,14 @@ export const enum Image2DataError {
export type ImageResponseData = { dataUrl: string } | { error: string };

export async function downloadAndEncode(url: URL) {
const imageResp = await fetch(url);
const image = Buffer.from(await imageResp.arrayBuffer()).toString('base64');
const imageResp = await fetch(url, { headers: { 'accept': 'image/*' } });
const imageBuffer = Buffer.from(await imageResp.arrayBuffer());
const imageType = await fileTypeFromBuffer(imageBuffer)
const image = imageBuffer.toString('base64');

console.log(imageType, imageResp.type, imageResp.headers.get('content-type'));

// TODO: get file type - https://via.placeholder.com/150 has no content-type header
return `data:${imageResp.headers.get('content-type')};base64,${image}`;
}

Expand Down
3 changes: 2 additions & 1 deletion api/tunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export default async function handler(req: Request) {
const { host, pathname } = new URL(header.dsn);
const projectId = pathname.substring(1);

if (!host.endsWith(sentryHost)) {
// todo: tool
if (host !== sentryHost && !host.endsWith(`.${sentryHost}`)) {
throw new Error(`invalid host: ${host}`);
}

Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
"@reduxjs/toolkit": "^1.9.1",
"@sentry/react": "^7.30.0",
"@sentry/tracing": "^7.30.0",
"@vercel/node": "^2.8.6",
"@vercel/node": "^2.8.13",
"cheerio": "1.0.0-rc.12",
"file-type": "^18.2.0",
"i18next": "^22.4.9",
"i18next-browser-languagedetector": "^7.0.1",
"i18next-http-backend": "^2.1.1",
Expand All @@ -41,6 +42,8 @@
"eslint": "^8.31.0",
"jest-extended": "^3.2.3",
"jsdom": "^21.0.0",
"nock": "^13.3.0",
"path-to-regexp": "^6.2.1",
"typescript": "^4.9.4",
"vite": "^4.0.4",
"vite-plugin-pwa": "^0.14.1",
Expand All @@ -53,6 +56,7 @@
"build": "tsc && vite build",
"preview": "vite preview",
"test": "vitest",
"coverage": "vitest run --coverage"
"coverage": "vitest run --coverage",
"lint": "eslint \"src/**\" \"api/**\""
}
}
Loading