Skip to content

Commit d834d8f

Browse files
authored
feat: reduce vercel function duration (#226)
* feat: reduce vercel function duration * chore: bump packages version * chore: bump packages version
1 parent c2fa12c commit d834d8f

16 files changed

Lines changed: 4919 additions & 1251 deletions

.github/workflows/test-and-lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
api_key: ${{ secrets.foresight_api_key }}
1818

1919
- uses: actions/checkout@v4
20-
- name: Use Node.js 20
20+
- name: Use Node.js 22
2121
uses: actions/setup-node@v4
2222
with:
2323
node-version-file: '.node-version'
@@ -40,7 +40,7 @@ jobs:
4040
runs-on: ubuntu-latest
4141
steps:
4242
- uses: actions/checkout@v4
43-
- name: Use Node.js 20
43+
- name: Use Node.js 22
4444
uses: actions/setup-node@v4
4545
with:
4646
node-version-file: '.node-version'

.node-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20.12.2
1+
22.13.0

api/cards/most-commit-language.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ export default async (req: VercelRequest, res: VercelResponse) => {
3131
try {
3232
const cardSVG = await getCommitsLanguageSVGWithThemeName(username, theme, excludeArr);
3333
res.setHeader('Content-Type', 'image/svg+xml');
34+
res.setHeader('Cache-Control', 'public, max-age=14400, s-maxage=86400');
3435
res.send(cardSVG);
3536
return;
3637
} catch (err: any) {
3738
console.log(err.message);
3839
// We update github token and try again, until getNextGitHubToken throw an Error
39-
changToNextGitHubToken(tokenIndex);
40-
tokenIndex += 1;
40+
if (err.response && (err.response.status === 403 || err.response.status === 401)) {
41+
changToNextGitHubToken(tokenIndex);
42+
tokenIndex += 1;
43+
} else {
44+
throw err;
45+
}
4146
}
4247
}
4348
} catch (err: any) {

api/cards/productive-time.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@ export default async (req: VercelRequest, res: VercelResponse) => {
2323
try {
2424
const cardSVG = await getProductiveTimeSVGWithThemeName(username, theme, Number(utcOffset));
2525
res.setHeader('Content-Type', 'image/svg+xml');
26+
res.setHeader('Cache-Control', 'public, max-age=14400, s-maxage=86400');
2627
res.send(cardSVG);
2728
return;
2829
} catch (err: any) {
2930
console.log(err.message);
3031
// We update github token and try again, until getNextGitHubToken throw an Error
31-
changToNextGitHubToken(tokenIndex);
32-
tokenIndex += 1;
32+
if (err.response && (err.response.status === 403 || err.response.status === 401)) {
33+
changToNextGitHubToken(tokenIndex);
34+
tokenIndex += 1;
35+
} else {
36+
throw err;
37+
}
3338
}
3439
}
3540
} catch (err: any) {

api/cards/profile-details.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ export default async (req: VercelRequest, res: VercelResponse) => {
1919
try {
2020
const cardSVG = await getProfileDetailsSVGWithThemeName(username, theme);
2121
res.setHeader('Content-Type', 'image/svg+xml');
22+
res.setHeader('Cache-Control', 'public, max-age=14400, s-maxage=86400');
2223
res.send(cardSVG);
2324
return;
2425
} catch (err: any) {
2526
console.log(err.message);
2627
// We update github token and try again, until getNextGitHubToken throw an Error
27-
changToNextGitHubToken(tokenIndex);
28-
tokenIndex += 1;
28+
if (err.response && (err.response.status === 403 || err.response.status === 401)) {
29+
changToNextGitHubToken(tokenIndex);
30+
tokenIndex += 1;
31+
} else {
32+
throw err;
33+
}
2934
}
3035
}
3136
} catch (err: any) {

api/cards/repos-per-language.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ export default async (req: VercelRequest, res: VercelResponse) => {
3131
try {
3232
const cardSVG = await getReposPerLanguageSVGWithThemeName(username, theme, excludeArr);
3333
res.setHeader('Content-Type', 'image/svg+xml');
34+
res.setHeader('Cache-Control', 'public, max-age=14400, s-maxage=86400');
3435
res.send(cardSVG);
3536
return;
3637
} catch (err: any) {
3738
console.log(err.message);
3839
// We update github token and try again, until getNextGitHubToken throw an Error
39-
changToNextGitHubToken(tokenIndex);
40-
tokenIndex += 1;
40+
if (err.response && (err.response.status === 403 || err.response.status === 401)) {
41+
changToNextGitHubToken(tokenIndex);
42+
tokenIndex += 1;
43+
} else {
44+
throw err;
45+
}
4146
}
4247
}
4348
} catch (err: any) {

api/cards/stats.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ export default async (req: VercelRequest, res: VercelResponse) => {
1919
try {
2020
const cardSVG = await getStatsSVGWithThemeName(username, theme);
2121
res.setHeader('Content-Type', 'image/svg+xml');
22+
res.setHeader('Cache-Control', 'public, max-age=14400, s-maxage=86400');
2223
res.send(cardSVG);
2324
return;
2425
} catch (err: any) {
2526
console.log(err.message);
2627
// We update github token and try again, until getNextGitHubToken throw an Error
27-
changToNextGitHubToken(tokenIndex);
28-
tokenIndex += 1;
28+
if (err.response && (err.response.status === 403 || err.response.status === 401)) {
29+
changToNextGitHubToken(tokenIndex);
30+
tokenIndex += 1;
31+
} else {
32+
throw err;
33+
}
2934
}
3035
}
3136
} catch (err: any) {

babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: [['@babel/preset-env', { targets: { node: 'current' } }]],
3+
};

jest.config.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
collectCoverage: true,
5+
clearMocks: true,
6+
moduleFileExtensions: ['js', 'ts'],
7+
transform: {
8+
'^.+\\.ts$': 'ts-jest',
9+
'^.+\\.js$': 'babel-jest' // Ensure JS files in node_modules can be transformed
10+
},
11+
setupFilesAfterEnv: [],
12+
testMatch: ['**/*.test.ts'],
13+
verbose: true,
14+
collectCoverageFrom: ['**/*.{ts,jx}', '!**/node_modules/**', '!**/dist/**', '!**/lib/**'],
15+
moduleNameMapper: {
16+
'^axios$': path.join(__dirname, 'node_modules/axios/dist/node/axios.cjs')
17+
},
18+
// Tell Jest to compile d3 and its dependencies (which are ESM)
19+
transformIgnorePatterns: [
20+
'node_modules/(?!d3|d3-.*|internmap|delaunator|robust-predicates|axios)'
21+
]
22+
};

jest.config.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)