Skip to content

Commit 11460b1

Browse files
authored
Merge pull request #773 from 0xPolygon/udpate-wrangler-pattern-deployment
fix(NO-JIRA): fix to handle options request and root access
2 parents baf70c2 + 27ca0d3 commit 11460b1

File tree

5 files changed

+98
-33
lines changed

5 files changed

+98
-33
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
node_modules/
22
build/
33
.DS_STORE
4+
.wrangler/
5+
src/wrangler_main.js

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"test": "mocha 'tests/tokenAddition'",
88
"test-all": "mocha 'tests'",
9-
"build": "rimraf build && mkdir -p build/tokenlists && node src/write.js",
9+
"build": "rimraf build && mkdir -p build/tokenlists && node src/write.js && node src/generateWorker.js",
1010
"lint": "eslint \"./src/*\"",
1111
"lint:fix": "eslint --fix \"./src/*\"",
1212
"lint-tests:fix": "eslint --fix \"./tests/*\""

src/generateWorker.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
function generateWorkerCode() {
5+
const buildDir = path.join(__dirname, "../build");
6+
const tokenListsDir = path.join(buildDir, "tokenlists");
7+
8+
// Get all JSON files in the tokenlists directory
9+
const tokenListFiles = fs.readdirSync(tokenListsDir)
10+
.filter(file => file.endsWith(".json"))
11+
.sort(); // Sort for consistent ordering
12+
13+
// Generate import statements
14+
const imports = [
15+
"import listRegistry from \"../build/listRegistry.json\";"
16+
];
17+
18+
const variableNames = [];
19+
20+
tokenListFiles.forEach((file, index) => {
21+
const baseName = file.replace(".json", "");
22+
const variableName = `tokenList${index}`;
23+
variableNames.push({ variableName, file, baseName });
24+
imports.push(`import ${variableName} from "../build/tokenlists/${file}";`);
25+
});
26+
27+
// Generate file map
28+
const fileMapEntries = [
29+
" \"/\": listRegistry,",
30+
" \"/listRegistry.json\": listRegistry,"
31+
];
32+
33+
variableNames.forEach(({ variableName, file }) => {
34+
fileMapEntries.push(` "/tokenlists/${file}": ${variableName},`);
35+
});
36+
37+
// Generate the complete worker code
38+
const workerCode = `${imports.join("\n")}
39+
40+
const corsHeaders = {
41+
"Access-Control-Allow-Origin": "*",
42+
"Access-Control-Allow-Methods": "GET, HEAD, POST, OPTIONS",
43+
"Access-Control-Allow-Headers": "Content-Type, X-Requested-With",
44+
};
45+
46+
// Auto-generated file mapping based on build directory contents
47+
const fileMap = {
48+
${fileMapEntries.join("\n")}
49+
};
50+
51+
export default {
52+
async fetch(request, env, ctx) {
53+
const url = new URL(request.url);
54+
55+
// Handle OPTIONS requests for ALL paths
56+
if (request.method === "OPTIONS") {
57+
return new Response(null, {
58+
status: 204,
59+
headers: corsHeaders
60+
});
61+
}
62+
63+
// Look up the file in our auto-generated map
64+
const fileData = fileMap[url.pathname];
65+
if (fileData) {
66+
const json = JSON.stringify(fileData, null, 2);
67+
return new Response(json, {
68+
headers: {
69+
"content-type": "application/json;charset=UTF-8",
70+
...corsHeaders,
71+
},
72+
});
73+
}
74+
75+
// File not found
76+
return new Response("Not Found", { status: 404 });
77+
},
78+
};
79+
`;
80+
81+
// Write the generated worker code
82+
const outputPath = path.join(__dirname, "wrangler_main.js");
83+
fs.writeFileSync(outputPath, workerCode);
84+
85+
console.log("✅ Generated wrangler_main.js with the following files:");
86+
console.log(" - / → listRegistry.json");
87+
console.log(" - /listRegistry.json → listRegistry.json");
88+
variableNames.forEach(({ file }) => {
89+
console.log(` - /tokenlists/${file} → tokenlists/${file}`);
90+
});
91+
}
92+
93+
// Run the generator
94+
generateWorkerCode();

src/index.js

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

wrangler.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
name = "polygon-token-list"
2+
main = "src/wrangler_main.js"
23
compatibility_date = "2024-12-30"
34
placement = { mode = "smart" }
45
preview_urls = false
56
send_metrics = false
67
workers_dev = false
78

8-
[assets]
9-
directory = "build"
10-
119
[env.staging]
1210
routes = [
1311
{ pattern = "api-polygon-tokens.staging.polygon.technology", custom_domain = true },

0 commit comments

Comments
 (0)