@snowcone-app/mockup-resolver 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dev.vars.example +10 -0
- package/.env.example +9 -0
- package/DEPLOY.md +152 -0
- package/Icon/r +0 -0
- package/README.md +241 -0
- package/account123.json +27 -0
- package/bulk-upload.json +61 -0
- package/cloudflare-routes.js +21 -0
- package/dist/index.js +397 -0
- package/dist/worker.js +306 -0
- package/package.json +44 -0
- package/scripts/build-worker.js +101 -0
- package/scripts/check-service.ts +69 -0
- package/scripts/seed-local-kv.ts +180 -0
- package/scripts/seed-prod-kv.js +97 -0
- package/scripts/setup-env.sh +62 -0
- package/scripts/setup-local-dev.ts +0 -0
- package/scripts/test-mockup-resolver.ts +457 -0
- package/scripts/update-routes.js +72 -0
- package/scripts/verify-cloudflare.js +55 -0
- package/seed-local-kv.js +107 -0
- package/src/index.ts +1673 -0
- package/src/resolve.ts +240 -0
- package/src/signing.ts +42 -0
- package/test/index.test.ts +42 -0
- package/test/integration/mockup-resolver.test.ts +196 -0
- package/test/integration/worker.test.ts +228 -0
- package/test/resolve.test.ts +76 -0
- package/test/setup.ts +47 -0
- package/test-kv.js +89 -0
- package/tsconfig.json +19 -0
- package/vitest.config.ts +15 -0
- package/wrangler.jsonc +84 -0
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
import fetch from 'node-fetch'
|
|
2
|
+
|
|
3
|
+
// Test configuration
|
|
4
|
+
const BASE_URL = 'http://localhost:8102'
|
|
5
|
+
const TEST_URL = 'https://example.com/image.jpg'
|
|
6
|
+
|
|
7
|
+
// Test data from our seeded KV store
|
|
8
|
+
const TEST_API_KEYS = {
|
|
9
|
+
valid: 'test-api-key-1', // Should succeed
|
|
10
|
+
invalid: 'invalid-api-key', // Should fail
|
|
11
|
+
dev: 'dev-api-key', // Should succeed
|
|
12
|
+
allOrigins: 'test-api-key-all-origins', // New: Should succeed with any origin
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const TEST_CLIENTS = {
|
|
16
|
+
valid: {
|
|
17
|
+
clientId: 'dev_client',
|
|
18
|
+
origin: 'http://localhost:3000', // Should succeed
|
|
19
|
+
},
|
|
20
|
+
invalidOrigin: {
|
|
21
|
+
clientId: 'dev_client',
|
|
22
|
+
origin: 'http://evil-site.com', // Should fail
|
|
23
|
+
},
|
|
24
|
+
invalidClientId: {
|
|
25
|
+
clientId: 'non-existent-client',
|
|
26
|
+
origin: 'http://localhost:3000', // Should fail
|
|
27
|
+
},
|
|
28
|
+
allOrigins: {
|
|
29
|
+
clientId: 'client_all_origins',
|
|
30
|
+
origin: 'https://random-site.com', // Should succeed despite not being in allowed list
|
|
31
|
+
},
|
|
32
|
+
wildcardOrigin: {
|
|
33
|
+
clientId: 'client_1',
|
|
34
|
+
origin: 'https://test.example.com', // Should succeed due to *.example.com pattern
|
|
35
|
+
},
|
|
36
|
+
wildcardInvalidOrigin: {
|
|
37
|
+
clientId: 'client_1',
|
|
38
|
+
origin: 'https://example.com', // Should fail as it's not a subdomain
|
|
39
|
+
},
|
|
40
|
+
wildcardNestedSubdomain: {
|
|
41
|
+
clientId: 'client_1',
|
|
42
|
+
origin: 'https://foo.bar.example.com', // Should fail as nested subdomains aren't allowed
|
|
43
|
+
},
|
|
44
|
+
wildcardUppercase: {
|
|
45
|
+
clientId: 'client_1',
|
|
46
|
+
origin: 'https://TEST.EXAMPLE.COM', // Should succeed (case insensitive)
|
|
47
|
+
},
|
|
48
|
+
wildcardWithPort: {
|
|
49
|
+
clientId: 'client_1',
|
|
50
|
+
origin: 'https://test.example.com:8080', // Should succeed (ports shouldn't affect matching)
|
|
51
|
+
},
|
|
52
|
+
wildcardWithIp: {
|
|
53
|
+
clientId: 'client_2',
|
|
54
|
+
origin: 'http://192.168.1.1', // Should fail as wildcards don't work with IPs
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Check if server is running
|
|
59
|
+
async function checkServerRunning(): Promise<boolean> {
|
|
60
|
+
try {
|
|
61
|
+
// Try a simple request to see if the server is up
|
|
62
|
+
const controller = new AbortController()
|
|
63
|
+
const timeoutId = setTimeout(() => controller.abort(), 3000)
|
|
64
|
+
|
|
65
|
+
await fetch(BASE_URL, {
|
|
66
|
+
method: 'HEAD',
|
|
67
|
+
signal: controller.signal,
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
clearTimeout(timeoutId)
|
|
71
|
+
return true
|
|
72
|
+
} catch (error) {
|
|
73
|
+
console.error('\nโ ๏ธ Server check failed. Make sure the mockup resolver is running with:')
|
|
74
|
+
console.error(' pnpm run dev:local\n')
|
|
75
|
+
console.error(`Error details: ${error instanceof Error ? error.message : String(error)}\n`)
|
|
76
|
+
return false
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Test runner
|
|
81
|
+
async function runTests() {
|
|
82
|
+
console.log('๐งช Running Mockup Resolver Tests')
|
|
83
|
+
console.log('==========================\n')
|
|
84
|
+
|
|
85
|
+
// First check if server is running
|
|
86
|
+
console.log('Checking if server is running...')
|
|
87
|
+
const serverRunning = await checkServerRunning()
|
|
88
|
+
if (!serverRunning) {
|
|
89
|
+
console.log('โ Tests aborted: Server not running')
|
|
90
|
+
process.exit(1)
|
|
91
|
+
}
|
|
92
|
+
console.log('โ
Server is running\n')
|
|
93
|
+
|
|
94
|
+
let passedTests = 0
|
|
95
|
+
let failedTests = 0
|
|
96
|
+
|
|
97
|
+
// Helper function to run a test case
|
|
98
|
+
async function runTest(name: string, testFn: () => Promise<boolean>) {
|
|
99
|
+
process.stdout.write(`Testing ${name}... `)
|
|
100
|
+
try {
|
|
101
|
+
const passed = await testFn()
|
|
102
|
+
if (passed) {
|
|
103
|
+
console.log('โ
PASSED')
|
|
104
|
+
passedTests++
|
|
105
|
+
} else {
|
|
106
|
+
console.log('โ FAILED')
|
|
107
|
+
failedTests++
|
|
108
|
+
}
|
|
109
|
+
} catch (error) {
|
|
110
|
+
console.log('โ FAILED')
|
|
111
|
+
console.error(` Error: ${error instanceof Error ? error.message : String(error)}`)
|
|
112
|
+
failedTests++
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 1. Test API Key Authentication
|
|
117
|
+
await runTest('Valid API Key Authentication', async () => {
|
|
118
|
+
const response = await fetch(`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}`, {
|
|
119
|
+
headers: {
|
|
120
|
+
'x-api-key': TEST_API_KEYS.valid,
|
|
121
|
+
},
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
if (!response.ok) {
|
|
125
|
+
console.error(` Received status: ${response.status}`)
|
|
126
|
+
const body = await response.text()
|
|
127
|
+
console.error(` Response body: ${body}`)
|
|
128
|
+
return false
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const data = (await response.json()) as { signature: string }
|
|
132
|
+
return typeof data.signature === 'string' && data.signature.length > 0
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
// 2. Test Invalid API Key
|
|
136
|
+
await runTest('Invalid API Key Authentication', async () => {
|
|
137
|
+
const response = await fetch(`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}`, {
|
|
138
|
+
headers: {
|
|
139
|
+
'x-api-key': TEST_API_KEYS.invalid,
|
|
140
|
+
},
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
// Should fail with 401
|
|
144
|
+
return response.status === 401
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
// 3. Test Client ID + Origin Authentication
|
|
148
|
+
await runTest('Valid Client ID + Origin Authentication', async () => {
|
|
149
|
+
const response = await fetch(
|
|
150
|
+
`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}&clientId=${TEST_CLIENTS.valid.clientId}`,
|
|
151
|
+
{
|
|
152
|
+
headers: {
|
|
153
|
+
Origin: TEST_CLIENTS.valid.origin,
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
if (!response.ok) {
|
|
159
|
+
console.error(` Received status: ${response.status}`)
|
|
160
|
+
return false
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const data = (await response.json()) as { signature: string }
|
|
164
|
+
return typeof data.signature === 'string' && data.signature.length > 0
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
// 4. Test Invalid Origin
|
|
168
|
+
await runTest('Invalid Origin', async () => {
|
|
169
|
+
const response = await fetch(
|
|
170
|
+
`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}&clientId=${TEST_CLIENTS.invalidOrigin.clientId}`,
|
|
171
|
+
{
|
|
172
|
+
headers: {
|
|
173
|
+
Origin: TEST_CLIENTS.invalidOrigin.origin,
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
// Should fail with 401
|
|
179
|
+
return response.status === 401
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
// 5. Test Invalid Client ID
|
|
183
|
+
await runTest('Invalid Client ID', async () => {
|
|
184
|
+
const response = await fetch(
|
|
185
|
+
`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}&clientId=${
|
|
186
|
+
TEST_CLIENTS.invalidClientId.clientId
|
|
187
|
+
}`,
|
|
188
|
+
{
|
|
189
|
+
headers: {
|
|
190
|
+
Origin: TEST_CLIENTS.invalidClientId.origin,
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
// Should fail with 401
|
|
196
|
+
return response.status === 401
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
// 6. Test Missing Authentication
|
|
200
|
+
await runTest('Missing Authentication', async () => {
|
|
201
|
+
const response = await fetch(`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}`)
|
|
202
|
+
|
|
203
|
+
// Should fail with 401
|
|
204
|
+
return response.status === 401
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
// 7. Test URL Signing and Verification
|
|
208
|
+
await runTest('URL Signing and Verification', async () => {
|
|
209
|
+
// First, get a signature
|
|
210
|
+
const signResponse = await fetch(`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}`, {
|
|
211
|
+
headers: {
|
|
212
|
+
'x-api-key': TEST_API_KEYS.valid,
|
|
213
|
+
},
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
if (!signResponse.ok) {
|
|
217
|
+
console.error(` Signature request failed with status: ${signResponse.status}`)
|
|
218
|
+
return false
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const { signature } = (await signResponse.json()) as { signature: string }
|
|
222
|
+
|
|
223
|
+
// Then verify it
|
|
224
|
+
const verifyResponse = await fetch(
|
|
225
|
+
`${BASE_URL}/verify?url=${encodeURIComponent(TEST_URL)}&signature=${signature}`,
|
|
226
|
+
{
|
|
227
|
+
headers: {
|
|
228
|
+
'x-api-key': TEST_API_KEYS.valid,
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
if (!verifyResponse.ok) {
|
|
234
|
+
console.error(` Verification request failed with status: ${verifyResponse.status}`)
|
|
235
|
+
return false
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const { valid } = (await verifyResponse.json()) as { valid: boolean }
|
|
239
|
+
return valid === true
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
// 8. Test Verification with Invalid Signature
|
|
243
|
+
await runTest('Verification with Invalid Signature', async () => {
|
|
244
|
+
const verifyResponse = await fetch(
|
|
245
|
+
`${BASE_URL}/verify?url=${encodeURIComponent(TEST_URL)}&signature=deadbeef`,
|
|
246
|
+
{
|
|
247
|
+
headers: {
|
|
248
|
+
'x-api-key': TEST_API_KEYS.valid,
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
if (!verifyResponse.ok) {
|
|
254
|
+
console.error(` Verification request failed with status: ${verifyResponse.status}`)
|
|
255
|
+
return false
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const { valid } = (await verifyResponse.json()) as { valid: boolean }
|
|
259
|
+
return valid === false
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
// 9. Test Missing URL Parameter
|
|
263
|
+
await runTest('Missing URL Parameter', async () => {
|
|
264
|
+
const response = await fetch(`${BASE_URL}/sign`, {
|
|
265
|
+
headers: {
|
|
266
|
+
'x-api-key': TEST_API_KEYS.valid,
|
|
267
|
+
},
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
// Should fail with 400
|
|
271
|
+
return response.status === 400
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
// 10. Test Client with No Origin Restrictions
|
|
275
|
+
await runTest('Client with No Origin Restrictions', async () => {
|
|
276
|
+
// Test with API key authentication
|
|
277
|
+
const apiKeyResponse = await fetch(`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}`, {
|
|
278
|
+
headers: {
|
|
279
|
+
'x-api-key': TEST_API_KEYS.allOrigins,
|
|
280
|
+
Origin: TEST_CLIENTS.allOrigins.origin,
|
|
281
|
+
},
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
if (!apiKeyResponse.ok) {
|
|
285
|
+
console.error(` API Key auth failed with status: ${apiKeyResponse.status}`)
|
|
286
|
+
return false
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Test with client ID + origin authentication
|
|
290
|
+
const clientIdResponse = await fetch(
|
|
291
|
+
`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}&clientId=${TEST_CLIENTS.allOrigins.clientId}`,
|
|
292
|
+
{
|
|
293
|
+
headers: {
|
|
294
|
+
Origin: TEST_CLIENTS.allOrigins.origin,
|
|
295
|
+
},
|
|
296
|
+
},
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
if (!clientIdResponse.ok) {
|
|
300
|
+
console.error(` Client ID auth failed with status: ${clientIdResponse.status}`)
|
|
301
|
+
return false
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return true
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
// Add new test for wildcard origin patterns
|
|
308
|
+
await runTest('Wildcard Origin Pattern', async () => {
|
|
309
|
+
const response = await fetch(
|
|
310
|
+
`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}&clientId=${TEST_CLIENTS.wildcardOrigin.clientId}`,
|
|
311
|
+
{
|
|
312
|
+
headers: {
|
|
313
|
+
Origin: TEST_CLIENTS.wildcardOrigin.origin,
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
)
|
|
317
|
+
|
|
318
|
+
if (!response.ok) {
|
|
319
|
+
console.error(` Received status: ${response.status}`)
|
|
320
|
+
return false
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const data = (await response.json()) as { signature: string }
|
|
324
|
+
return typeof data.signature === 'string' && data.signature.length > 0
|
|
325
|
+
})
|
|
326
|
+
|
|
327
|
+
await runTest('Invalid Wildcard Origin (Base Domain)', async () => {
|
|
328
|
+
const response = await fetch(
|
|
329
|
+
`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}&clientId=${
|
|
330
|
+
TEST_CLIENTS.wildcardInvalidOrigin.clientId
|
|
331
|
+
}`,
|
|
332
|
+
{
|
|
333
|
+
headers: {
|
|
334
|
+
Origin: TEST_CLIENTS.wildcardInvalidOrigin.origin,
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
// Should fail with 401
|
|
340
|
+
return response.status === 401
|
|
341
|
+
})
|
|
342
|
+
|
|
343
|
+
// Add new test for nested subdomains
|
|
344
|
+
await runTest('Invalid Nested Subdomain', async () => {
|
|
345
|
+
const response = await fetch(
|
|
346
|
+
`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}&clientId=${
|
|
347
|
+
TEST_CLIENTS.wildcardNestedSubdomain.clientId
|
|
348
|
+
}`,
|
|
349
|
+
{
|
|
350
|
+
headers: {
|
|
351
|
+
Origin: TEST_CLIENTS.wildcardNestedSubdomain.origin,
|
|
352
|
+
},
|
|
353
|
+
},
|
|
354
|
+
)
|
|
355
|
+
|
|
356
|
+
// Should fail with 401
|
|
357
|
+
return response.status === 401
|
|
358
|
+
})
|
|
359
|
+
|
|
360
|
+
// Test case insensitivity
|
|
361
|
+
await runTest('Case Insensitive Wildcard Match', async () => {
|
|
362
|
+
const response = await fetch(
|
|
363
|
+
`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}&clientId=${
|
|
364
|
+
TEST_CLIENTS.wildcardUppercase.clientId
|
|
365
|
+
}`,
|
|
366
|
+
{
|
|
367
|
+
headers: {
|
|
368
|
+
Origin: TEST_CLIENTS.wildcardUppercase.origin,
|
|
369
|
+
},
|
|
370
|
+
},
|
|
371
|
+
)
|
|
372
|
+
|
|
373
|
+
return response.ok
|
|
374
|
+
})
|
|
375
|
+
|
|
376
|
+
// Test origin with port
|
|
377
|
+
await runTest('Wildcard Match with Port', async () => {
|
|
378
|
+
const response = await fetch(
|
|
379
|
+
`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}&clientId=${
|
|
380
|
+
TEST_CLIENTS.wildcardWithPort.clientId
|
|
381
|
+
}`,
|
|
382
|
+
{
|
|
383
|
+
headers: {
|
|
384
|
+
Origin: TEST_CLIENTS.wildcardWithPort.origin,
|
|
385
|
+
},
|
|
386
|
+
},
|
|
387
|
+
)
|
|
388
|
+
|
|
389
|
+
return response.ok
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
// Test IP address with wildcard
|
|
393
|
+
await runTest('IP Address with Wildcard', async () => {
|
|
394
|
+
const response = await fetch(
|
|
395
|
+
`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}&clientId=${TEST_CLIENTS.wildcardWithIp.clientId}`,
|
|
396
|
+
{
|
|
397
|
+
headers: {
|
|
398
|
+
Origin: TEST_CLIENTS.wildcardWithIp.origin,
|
|
399
|
+
},
|
|
400
|
+
},
|
|
401
|
+
)
|
|
402
|
+
|
|
403
|
+
// Should fail with 401 as wildcards don't work with IPs
|
|
404
|
+
return response.status === 401
|
|
405
|
+
})
|
|
406
|
+
|
|
407
|
+
// Add new test for CORS preflight with X-Client-ID header
|
|
408
|
+
await runTest('CORS Preflight with X-Client-ID', async () => {
|
|
409
|
+
const response = await fetch(`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}`, {
|
|
410
|
+
method: 'OPTIONS',
|
|
411
|
+
headers: {
|
|
412
|
+
Origin: TEST_CLIENTS.valid.origin,
|
|
413
|
+
'Access-Control-Request-Method': 'GET',
|
|
414
|
+
'Access-Control-Request-Headers': 'x-client-id',
|
|
415
|
+
},
|
|
416
|
+
})
|
|
417
|
+
|
|
418
|
+
if (!response.ok) {
|
|
419
|
+
console.error(` Preflight failed with status: ${response.status}`)
|
|
420
|
+
return false
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const allowedHeaders = response.headers.get('Access-Control-Allow-Headers')
|
|
424
|
+
if (!allowedHeaders || !allowedHeaders.toLowerCase().includes('x-client-id')) {
|
|
425
|
+
console.error(` X-Client-ID not in allowed headers: ${allowedHeaders}`)
|
|
426
|
+
return false
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Test actual request with X-Client-ID header
|
|
430
|
+
const actualResponse = await fetch(`${BASE_URL}/sign?url=${encodeURIComponent(TEST_URL)}`, {
|
|
431
|
+
headers: {
|
|
432
|
+
Origin: TEST_CLIENTS.valid.origin,
|
|
433
|
+
'X-Client-ID': TEST_CLIENTS.valid.clientId,
|
|
434
|
+
},
|
|
435
|
+
})
|
|
436
|
+
|
|
437
|
+
return actualResponse.ok
|
|
438
|
+
})
|
|
439
|
+
|
|
440
|
+
// Print test summary
|
|
441
|
+
console.log('\n==========================')
|
|
442
|
+
console.log(`๐ Test Summary: ${passedTests} passed, ${failedTests} failed`)
|
|
443
|
+
console.log(` Success Rate: ${Math.round((passedTests / (passedTests + failedTests)) * 100)}%`)
|
|
444
|
+
|
|
445
|
+
if (failedTests === 0) {
|
|
446
|
+
console.log('\n๐ All tests passed!')
|
|
447
|
+
} else {
|
|
448
|
+
console.log('\nโ Some tests failed. Check the logs above for details.')
|
|
449
|
+
process.exit(1)
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
// Run the tests
|
|
454
|
+
runTests().catch((error) => {
|
|
455
|
+
console.error('Error running tests:', error)
|
|
456
|
+
process.exit(1)
|
|
457
|
+
})
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Update Routes Script
|
|
3
|
+
*
|
|
4
|
+
* This script updates the router configuration by running the centralized
|
|
5
|
+
* route collection script.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const { execSync } = require('child_process')
|
|
9
|
+
const path = require('path')
|
|
10
|
+
const fs = require('fs')
|
|
11
|
+
|
|
12
|
+
// Debug info
|
|
13
|
+
console.log('๐ Updating routes with centralized route collection system...')
|
|
14
|
+
console.log(`Current directory: ${process.cwd()}`)
|
|
15
|
+
console.log(`__dirname: ${__dirname}`)
|
|
16
|
+
|
|
17
|
+
// Get the monorepo root by finding the root package.json
|
|
18
|
+
// Start from the current directory and move up until we find package.json
|
|
19
|
+
let monorepoRoot = process.cwd()
|
|
20
|
+
while (!fs.existsSync(path.join(monorepoRoot, 'package.json')) && monorepoRoot !== '/') {
|
|
21
|
+
monorepoRoot = path.dirname(monorepoRoot)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (monorepoRoot === '/') {
|
|
25
|
+
console.error('โ Could not find monorepo root. Make sure you run this script from within the monorepo.')
|
|
26
|
+
process.exit(1)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
console.log(`Monorepo root: ${monorepoRoot}`)
|
|
30
|
+
|
|
31
|
+
// Path to the collect-routes script
|
|
32
|
+
let collectRoutesScript = path.join(monorepoRoot, 'infra', 'cloudflare', 'scripts', 'collect-routes.js')
|
|
33
|
+
|
|
34
|
+
console.log(`Using collect-routes script at: ${collectRoutesScript}`)
|
|
35
|
+
|
|
36
|
+
// Check if the file exists
|
|
37
|
+
if (!fs.existsSync(collectRoutesScript)) {
|
|
38
|
+
console.error(`โ Script not found at: ${collectRoutesScript}`)
|
|
39
|
+
|
|
40
|
+
// Try to find the script in common locations
|
|
41
|
+
const possibleLocations = [
|
|
42
|
+
// Current working directory structure
|
|
43
|
+
path.join(process.cwd(), '../../../infra/cloudflare/scripts/collect-routes.js'),
|
|
44
|
+
// GitHub Actions structure
|
|
45
|
+
'/home/runner/work/snowcone-monorepo/snowcone-monorepo/infra/cloudflare/scripts/collect-routes.js',
|
|
46
|
+
// Alternative path resolution
|
|
47
|
+
path.resolve(__dirname, '../../../infra/cloudflare/scripts/collect-routes.js'),
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
console.log('Checking alternative locations:')
|
|
51
|
+
for (const location of possibleLocations) {
|
|
52
|
+
console.log(`- ${location}: ${fs.existsSync(location) ? 'FOUND' : 'not found'}`)
|
|
53
|
+
if (fs.existsSync(location)) {
|
|
54
|
+
console.log(`โ
Found script at: ${location}`)
|
|
55
|
+
collectRoutesScript = location
|
|
56
|
+
break
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
// Execute the collect-routes script
|
|
63
|
+
const result = execSync(`node ${collectRoutesScript}`, {
|
|
64
|
+
encoding: 'utf8',
|
|
65
|
+
stdio: 'inherit',
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
console.log('โ
Routes updated successfully!')
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.error(`โ Failed to update routes: ${error.message}`)
|
|
71
|
+
process.exit(1)
|
|
72
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Verify Cloudflare Credentials Script
|
|
4
|
+
*
|
|
5
|
+
* This script verifies the Cloudflare API token and account ID by making a test API call.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const { execSync } = require('child_process')
|
|
9
|
+
|
|
10
|
+
// Get environment variables
|
|
11
|
+
const apiToken = process.env.CLOUDFLARE_API_TOKEN
|
|
12
|
+
const accountId = process.env.CLOUDFLARE_ACCOUNT_ID
|
|
13
|
+
|
|
14
|
+
console.log('๐ Verifying Cloudflare credentials...')
|
|
15
|
+
|
|
16
|
+
// Check if API token is set
|
|
17
|
+
if (!apiToken) {
|
|
18
|
+
console.error('โ CLOUDFLARE_API_TOKEN is not set')
|
|
19
|
+
process.exit(1)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Check if account ID is set
|
|
23
|
+
if (!accountId) {
|
|
24
|
+
console.error('โ CLOUDFLARE_ACCOUNT_ID is not set')
|
|
25
|
+
process.exit(1)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
console.log(`โ
CLOUDFLARE_API_TOKEN is set (${apiToken.length} characters)`)
|
|
29
|
+
console.log(`โ
CLOUDFLARE_ACCOUNT_ID is set: ${accountId}`)
|
|
30
|
+
|
|
31
|
+
// Make a test API call to verify the token and account ID
|
|
32
|
+
try {
|
|
33
|
+
console.log('๐ Testing Cloudflare API access...')
|
|
34
|
+
|
|
35
|
+
// Using curl to make a simple API call
|
|
36
|
+
const command = `curl -s -X GET "https://api.cloudflare.com/client/v4/accounts/${accountId}" \\
|
|
37
|
+
-H "Authorization: Bearer ${apiToken}" \\
|
|
38
|
+
-H "Content-Type: application/json"`
|
|
39
|
+
|
|
40
|
+
const response = execSync(command, { encoding: 'utf8' })
|
|
41
|
+
const data = JSON.parse(response)
|
|
42
|
+
|
|
43
|
+
if (data.success) {
|
|
44
|
+
console.log(`โ
Successfully authenticated with Cloudflare API`)
|
|
45
|
+
console.log(` Account name: ${data.result.name}`)
|
|
46
|
+
} else {
|
|
47
|
+
console.error('โ API call failed:', data.errors)
|
|
48
|
+
process.exit(1)
|
|
49
|
+
}
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error('โ Failed to verify Cloudflare credentials:', error.message)
|
|
52
|
+
process.exit(1)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
console.log('โ
Cloudflare credentials verification completed successfully')
|
package/seed-local-kv.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
const { exec } = require('child_process')
|
|
2
|
+
const { promisify } = require('util')
|
|
3
|
+
const fs = require('fs')
|
|
4
|
+
const path = require('path')
|
|
5
|
+
|
|
6
|
+
const execAsync = promisify(exec)
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* This script seeds the local KV store with test data for development
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// Define accounts with their API keys and clientIds
|
|
13
|
+
const accounts = [
|
|
14
|
+
{
|
|
15
|
+
accountId: 'account123',
|
|
16
|
+
apiKeys: {
|
|
17
|
+
'dev-api-key': { rateLimit: 1000 },
|
|
18
|
+
'dev-api-key-2': { rateLimit: 1000 },
|
|
19
|
+
'dev-api-key-readonly': { rateLimit: 500 },
|
|
20
|
+
'test-api-key-1': { rateLimit: 2000 },
|
|
21
|
+
'test-api-key-1-readonly': { rateLimit: 500 },
|
|
22
|
+
},
|
|
23
|
+
clientIds: {
|
|
24
|
+
dev_client: {
|
|
25
|
+
rateLimit: 100,
|
|
26
|
+
},
|
|
27
|
+
client_all_origins: {
|
|
28
|
+
rateLimit: 50,
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
accountId: 'account456',
|
|
34
|
+
apiKeys: {
|
|
35
|
+
'test-api-key-2': { rateLimit: 2000 },
|
|
36
|
+
'test-api-key-2-readonly': { rateLimit: 500 },
|
|
37
|
+
'sdk-test-key-1': { rateLimit: 1000 },
|
|
38
|
+
'sdk-test-key-2': { rateLimit: 1000 },
|
|
39
|
+
},
|
|
40
|
+
clientIds: {
|
|
41
|
+
client_1: {
|
|
42
|
+
rateLimit: 200,
|
|
43
|
+
},
|
|
44
|
+
client_2: {
|
|
45
|
+
rateLimit: 300,
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
// Modified to use Miniflare format for KV data
|
|
52
|
+
async function seedData() {
|
|
53
|
+
console.log('Seeding KV store...')
|
|
54
|
+
|
|
55
|
+
// Read wrangler.toml to find namespace ID
|
|
56
|
+
console.log('Reading wrangler.toml to find namespace ID...')
|
|
57
|
+
const wranglerConfig = fs.readFileSync(path.join(__dirname, 'wrangler.toml'), 'utf8')
|
|
58
|
+
const namespaceIdMatch = wranglerConfig.match(/id\s*=\s*"([^"]+)"/)
|
|
59
|
+
const namespaceId = namespaceIdMatch ? namespaceIdMatch[1] : null
|
|
60
|
+
console.log('Found namespace ID:', namespaceId)
|
|
61
|
+
|
|
62
|
+
if (!namespaceId) {
|
|
63
|
+
throw new Error('Could not find namespace ID in wrangler.toml')
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Seed account data
|
|
67
|
+
for (const account of accounts) {
|
|
68
|
+
console.log(`\nSeeding account: ${account.accountId}`)
|
|
69
|
+
|
|
70
|
+
// Create temporary JSON file for this account
|
|
71
|
+
const tempFile = `temp_${account.accountId}.json`
|
|
72
|
+
fs.writeFileSync(tempFile, JSON.stringify(account))
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
// Use wrangler to put the data in KV
|
|
76
|
+
const command = `npx wrangler kv key put --namespace-id=${namespaceId} "${account.accountId}" --path=${tempFile}`
|
|
77
|
+
console.log('Running command:', command)
|
|
78
|
+
const { stdout, stderr } = await execAsync(command)
|
|
79
|
+
if (stdout) console.log(stdout)
|
|
80
|
+
if (stderr) console.error(stderr)
|
|
81
|
+
|
|
82
|
+
console.log(`โ
Seeded account ${account.accountId}`)
|
|
83
|
+
} catch (err) {
|
|
84
|
+
console.error(`Failed to write account ${account.accountId}:`, err)
|
|
85
|
+
} finally {
|
|
86
|
+
// Clean up temp file
|
|
87
|
+
fs.unlinkSync(tempFile)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
console.log('\nโ
KV data seeded successfully!')
|
|
92
|
+
|
|
93
|
+
console.log('\nTest with:')
|
|
94
|
+
console.log(
|
|
95
|
+
'1. API key auth: curl -H "x-api-key: test-api-key-1" "http://localhost:8102/sign?url=https://example.com/image.jpg&accountId=account123"',
|
|
96
|
+
)
|
|
97
|
+
console.log(
|
|
98
|
+
'2. Invalid auth: curl "http://localhost:8102/sign?url=https://example.com/image.jpg&accountId=account123"',
|
|
99
|
+
)
|
|
100
|
+
console.log('\nStart local server: pnpm run dev')
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Run the seeding
|
|
104
|
+
seedData().catch((error) => {
|
|
105
|
+
console.error('Failed to seed data:', error)
|
|
106
|
+
process.exit(1)
|
|
107
|
+
})
|