-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathindex.ts
More file actions
221 lines (182 loc) · 6.63 KB
/
index.ts
File metadata and controls
221 lines (182 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import Bun, { redis, sql } from "bun";
const GOOGLE_API_KEY = process.env.GOOGLE_API_KEY;
const ALLOWED_QUERY_PARAMETERS = {
raw: ["true", "false"],
};
// In-memory cache for pending requests to prevent thundering herd
const pendingRequests = new Map<string, Promise<string>>();
const BASE_HEADERS = {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers":
"Origin, X-Requested-With, Content-Type, Accept",
};
const server = Bun.serve({
port: process.env.PORT || 3000,
routes: {
"/": Response.redirect("https://github.com/benborgers/opensheet#readme"),
"/up": new Response("ok"),
"/:id/:sheet": async (request) => {
const { id, sheet: sheetParam } = request.params;
const url = new URL(request.url);
// Random cache duration between 30-60 seconds to prevent cache stampedes
const cacheDuration = Math.floor(Math.random() * 31) + 30; // 30 to 60 seconds
const HEADERS = {
...BASE_HEADERS,
"Cache-Control": `public, max-age=${cacheDuration}, s-maxage=${cacheDuration}`,
};
const queryParams = Object.fromEntries(url.searchParams.entries());
// This prevents use of extra query parameters like ?v= to bust the cache early.
if (
Object.keys(queryParams).some(
(key) => !(key in ALLOWED_QUERY_PARAMETERS)
)
) {
return error(
`Invalid query parameters. Allowed parameters: ${Object.keys(
ALLOWED_QUERY_PARAMETERS
)
.map((key) => `\`${key}\``)
.join(", ")}. Your request was: ${request.url}`,
400
);
}
// This prevents the use of the `raw` parameter to bust the cache.
for (const [key, value] of Object.entries(queryParams)) {
if (key in ALLOWED_QUERY_PARAMETERS) {
const allowedValues = ALLOWED_QUERY_PARAMETERS[key];
if (!allowedValues.includes(value)) {
return error(`Invalid value for query parameter \`${key}\``, 400);
}
}
}
const useUnformattedValues = queryParams.raw === "true";
const cacheKey = request.url;
const cachedResponse = await redis.get(cacheKey);
if (cachedResponse) {
return new Response(cachedResponse, {
headers: HEADERS,
});
}
// Check if there's already a pending request for this cache key
// This prevents multiple simultaneous requests from all hitting the Google API
if (pendingRequests.has(cacheKey)) {
try {
const responseData = await pendingRequests.get(cacheKey)!;
return new Response(responseData, {
headers: HEADERS,
});
} catch (e) {
return error(e instanceof Error ? e.message : String(e));
}
}
// Create a promise for fetching this data and store it
const fetchPromise = (async () => {
try {
if (Math.random() < 0.01) {
const now = new Date();
const hour = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
now.getHours()
).toISOString();
await sql`
INSERT INTO analytics (hour, sheet_id, count)
VALUES (${hour}, ${id}, 1)
ON CONFLICT (hour, sheet_id)
DO UPDATE SET count = analytics.count + 1
`;
}
let sheet = decodeURIComponent(sheetParam.replace(/\+/g, " "));
if (!isNaN(sheet as any)) {
if (parseInt(sheet) === 0) {
throw new Error("For this API, sheet numbers start at 1");
}
const metadataCacheKey = `metadata:${id}`;
const cachedMetadata = await redis.get(metadataCacheKey);
let sheetData;
if (cachedMetadata) {
sheetData = JSON.parse(cachedMetadata);
} else {
sheetData = await (
await fetch(
`https://sheets.googleapis.com/v4/spreadsheets/${id}?key=${GOOGLE_API_KEY}`
)
).json();
if (sheetData.error) {
throw new Error(sheetData.error.message);
}
await redis.set(metadataCacheKey, JSON.stringify(sheetData));
await redis.expire(metadataCacheKey, 300);
}
const sheetIndex = parseInt(sheet) - 1;
const sheetWithThisIndex = sheetData.sheets[sheetIndex];
if (!sheetWithThisIndex) {
throw new Error(`There is no sheet number ${sheet}`);
}
sheet = sheetWithThisIndex.properties.title;
}
const apiUrl = new URL(
`https://sheets.googleapis.com/v4/spreadsheets/${id}/values/${encodeURIComponent(
sheet
)}`
);
apiUrl.searchParams.set("key", GOOGLE_API_KEY!);
if (useUnformattedValues)
apiUrl.searchParams.set("valueRenderOption", "UNFORMATTED_VALUE");
const result = await (await fetch(apiUrl)).json();
if (result.error) {
throw new Error(result.error.message);
}
const rows: any[] = [];
const rawRows = result.values || [];
const headers = rawRows.shift();
rawRows.forEach((row: any[]) => {
const rowData: any = {};
row.forEach((item, index) => {
rowData[headers[index]] = item;
});
rows.push(rowData);
});
const responseData = JSON.stringify(rows);
await redis.set(cacheKey, responseData);
await redis.expire(cacheKey, cacheDuration);
return responseData;
} finally {
// Always remove from pending requests when done
pendingRequests.delete(cacheKey);
}
})();
pendingRequests.set(cacheKey, fetchPromise);
try {
const responseData = await fetchPromise;
return new Response(responseData, {
headers: HEADERS,
});
} catch (e) {
return error(e instanceof Error ? e.message : String(e));
}
},
},
fetch() {
return error("URL format is /spreadsheet_id/sheet_name", 404);
},
});
console.log(`Server running on http://localhost:${server.port}`);
const error = (message: string, status = 400) => {
console.log(status, message);
return new Response(
JSON.stringify({
error: message,
documentation: "https://github.com/benborgers/opensheet#readme",
}),
{
status: status,
headers: {
...BASE_HEADERS,
"Cache-Control": "public, max-age=30, s-maxage=30",
},
}
);
};