Skip to content

Commit

Permalink
Upgrade to hono v4
Browse files Browse the repository at this point in the history
  • Loading branch information
dangeredwolf committed Apr 30, 2024
1 parent ad5ccfb commit beea81d
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 32 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -39,6 +39,6 @@
},
"dependencies": {
"@hono/sentry": "^1.0.1",
"hono": "^3.12.12"
"hono": "^4.2.9"
}
}
4 changes: 2 additions & 2 deletions src/caches.ts
Expand Up @@ -90,11 +90,11 @@ export const cacheMiddleware = (): MiddlewareHandler => async (c, next) => {
/* We properly state our OPTIONS when asked */
case 'OPTIONS':
c.header('allow', Constants.RESPONSE_HEADERS.allow);
c.body(null);
c.status(204);
return;
default:
c.status(405);
if (returnAsJson) return c.json('');
return c.html('');
return c.html('', 405);
}
};
3 changes: 2 additions & 1 deletion src/embed/status.ts
Expand Up @@ -9,6 +9,7 @@ import { renderVideo } from '../render/video';
import { renderInstantView } from '../render/instantview';
import { constructTwitterThread } from '../providers/twitter/conversation';
import { Experiment, experimentCheck } from '../experiments';
import { StatusCode } from 'hono/utils/http-status';

export const returnError = (c: Context, error: string): Response => {
return c.html(
Expand Down Expand Up @@ -78,7 +79,7 @@ export const handleStatus = async (

/* Catch this request if it's an API response */
if (flags?.api) {
c.status(api.code);
c.status(api.code as StatusCode);
// Add every header from Constants.API_RESPONSE_HEADERS
for (const [header, value] of Object.entries(Constants.API_RESPONSE_HEADERS)) {
c.header(header, value);
Expand Down
2 changes: 1 addition & 1 deletion src/experiments.ts
Expand Up @@ -42,7 +42,7 @@ const Experiments: { [key in Experiment]: ExperimentConfig } = {
[Experiment.IV_FORCE_THREAD_UNROLL]: {
name: 'IV force thread unroll',
description: 'Force thread unroll for Telegram Instant View',
percentage: 0.25
percentage: 1
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/providers/twitter/conversation.ts
Expand Up @@ -4,6 +4,7 @@ import { buildAPITwitterStatus } from './processor';
import { Experiment, experimentCheck } from '../../experiments';
import { isGraphQLTwitterStatus } from '../../helpers/graphql';
import { Context } from 'hono';
import { StatusCode } from 'hono/utils/http-status';

const writeDataPoint = (
c: Context,
Expand Down Expand Up @@ -579,10 +580,9 @@ export const threadAPIProvider = async (c: Context) => {

const processedResponse = await constructTwitterThread(id, true, c, undefined);

c.status(processedResponse.code);
// Add every header from Constants.API_RESPONSE_HEADERS
for (const [header, value] of Object.entries(Constants.API_RESPONSE_HEADERS)) {
c.header(header, value);
}
return c.json(processedResponse);
return c.json(processedResponse, processedResponse.code as StatusCode);
};
2 changes: 1 addition & 1 deletion src/realms/api/hit.ts
Expand Up @@ -5,7 +5,7 @@ export const linkHitRequest = async (c: Context) => {
const userAgent = c.req.header('User-Agent') || '';

if (userAgent.includes('Telegram')) {
c.status(403);
return c.text('', 403);
}
// If param `url` exists, 302 redirect to it
if (typeof c.req.query('url') === 'string') {
Expand Down
3 changes: 1 addition & 2 deletions src/realms/api/router.ts
Expand Up @@ -9,11 +9,10 @@ export const api = new Hono();

api.use('*', async (c, next) => {
if (!c.req.header('user-agent')) {
c.status(401);
return c.json({
error:
"You must identify yourself with a User-Agent header in order to use the FixTweet API. We recommend using a descriptive User-Agent header to identify your app, such as 'MyAwesomeBot/1.0 (+http://example.com/myawesomebot)'. We don't track or save what kinds of data you are pulling, but you may be blocked if you send too many requests from an unidentifiable user agent."
});
}, 401);
}
await next();
});
Expand Down
3 changes: 1 addition & 2 deletions src/realms/twitter/routes/oembed.ts
Expand Up @@ -28,7 +28,6 @@ export const oembed = async (c: Context) => {
};

c.header('content-type', 'application/json');
c.status(200);
/* Stringify and send it on its way! */
return c.text(JSON.stringify(data));
return c.text(JSON.stringify(data), 200);
};
10 changes: 3 additions & 7 deletions src/realms/twitter/routes/redirects.ts
Expand Up @@ -25,12 +25,10 @@ export const setRedirectRequest = async (c: Context) => {
/* Check that origin either does not exist or is in our domain list */
const origin = c.req.header('origin');
if (origin && !Constants.STANDARD_DOMAIN_LIST.includes(new URL(origin).hostname)) {
c.status(403);

return c.html(
Strings.MESSAGE_HTML.format({
message: `Failed to set base redirect: Your request seems to be originating from another domain, please open this up in a new tab if you are trying to set your base redirect.`
})
}), 403
);
}

Expand All @@ -46,11 +44,10 @@ export const setRedirectRequest = async (c: Context) => {
'content-security-policy',
`frame-ancestors ${Constants.STANDARD_DOMAIN_LIST.join(' ')};`
);
c.status(200);
return c.html(
Strings.MESSAGE_HTML.format({
message: `Your base redirect has been cleared. To set one, please pass along the <code>url</code> parameter.`
})
}), 200
);
}

Expand All @@ -71,11 +68,10 @@ export const setRedirectRequest = async (c: Context) => {
'content-security-policy',
`frame-ancestors ${Constants.STANDARD_DOMAIN_LIST.join(' ')};`
);
c.status(200);
return c.html(
Strings.MESSAGE_HTML.format({
message: `Your URL does not appear to be well-formed. Example: ?url=https://nitter.net`
})
}), 200
);
}

Expand Down
3 changes: 1 addition & 2 deletions src/realms/twitter/routes/status.ts
Expand Up @@ -131,8 +131,7 @@ export const statusRequest = async (c: Context) => {
return statusResponse;
} else {
/* Somehow handleStatus sent us nothing. This should *never* happen, but we have a case for it. */
c.status(500);
return c.text(Strings.ERROR_UNKNOWN);
return c.text(Strings.ERROR_UNKNOWN, 500);
}
} else {
/* A human has clicked a fxtwitter.com/:screen_name/status/:id link!
Expand Down
3 changes: 2 additions & 1 deletion src/user.ts
Expand Up @@ -2,6 +2,7 @@ import { Context } from 'hono';
import { Constants } from './constants';
import { Strings } from './strings';
import { userAPI } from './providers/twitter/profile';
import { StatusCode } from 'hono/utils/http-status';

export const returnError = (c: Context, error: string): Response => {
return c.html(
Expand Down Expand Up @@ -29,7 +30,7 @@ export const handleProfile = async (
/* Catch this request if it's an API response */
// For now we just always return the API response while testing
if (flags?.api) {
c.status(api.code);
c.status(api.code as StatusCode);
// Add every header from Constants.API_RESPONSE_HEADERS
for (const [header, value] of Object.entries(Constants.API_RESPONSE_HEADERS)) {
c.header(header, value);
Expand Down
10 changes: 4 additions & 6 deletions src/worker.ts
Expand Up @@ -8,6 +8,7 @@ import { Constants } from './constants';
import { api } from './realms/api/router';
import { twitter } from './realms/twitter/router';
import { cacheMiddleware } from './caches';
import { StatusCode } from 'hono/utils/http-status';

const noCache = 'max-age=0, no-cache, no-store, must-revalidate';
const embeddingClientRegex =
Expand Down Expand Up @@ -89,10 +90,9 @@ app.onError((err, c) => {
if (c.req.header('User-Agent')?.match(embeddingClientRegex)) {
errorCode = 200;
}
c.status(errorCode);
c.header('cache-control', noCache);

return c.html(Strings.ERROR_HTML);
return c.html(Strings.ERROR_HTML, errorCode as StatusCode);
});

const customLogger = (message: string, ...rest: string[]) => {
Expand Down Expand Up @@ -138,12 +138,10 @@ app.all('/error', async c => {
c.header('cache-control', noCache);

if (c.req.header('User-Agent')?.match(embeddingClientRegex)) {
c.status(200);
return c.html(Strings.ERROR_HTML);
return c.html(Strings.ERROR_HTML, 200);
}
c.status(400);
/* We return it as a 200 so embedded applications can display the error */
return c.body('');
return c.body('', 400);
});

export default {
Expand Down

0 comments on commit beea81d

Please sign in to comment.