-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebApi.js
More file actions
151 lines (139 loc) · 4.62 KB
/
webApi.js
File metadata and controls
151 lines (139 loc) · 4.62 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
/**
* Project: Rewind Bitcoin
* Website: https://rewindbitcoin.com
*
* Author: Jose-Luis Landabaso
* Email: landabaso@gmail.com
*
* Contact Email: hello@rewindbitcoin.com
*
* License: MIT License
*
* Copyright (c) 2025 Jose-Luis Landabaso, Rewind Bitcoin
*/
import http from "http";
function writeHeadWithDefaults(res, statusCode, headers = {}, ...args) {
const defaultHeaders = res.getHeaders();
const mergedHeaders = { ...defaultHeaders, ...headers };
res.writeHead(statusCode, mergedHeaders, ...args);
}
export default function webApi(port, bees) {
const server = http.createServer(async (req, res) => {
console.log(`[info] API request: ${req.url}`);
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET");
const { pathname } = new URL(req.url, `http://${req.headers.host}`);
const pathSegments = pathname.split("/").filter(Boolean);
if (req.method === "GET") {
if (pathSegments.length === 1 && pathSegments[0] === "generate_204") {
// Route: /generate-204
console.log(`[info] Status 204`);
res.statusCode = 204;
return res.end();
}
let networkId, vaultId, command;
if (
pathSegments.length === 3 &&
pathSegments[0] === "vaults" &&
(pathSegments[2] === "get" || pathSegments[2] === "check")
) {
//If :networkId not passed, assume its bitcoin
// Route: /vaults/:vaultId/{get,check}
networkId = "bitcoin";
vaultId = pathSegments[1];
command = pathSegments[2];
} else if (
pathSegments.length === 4 &&
pathSegments[1] === "vaults" &&
(pathSegments[3] === "get" || pathSegments[3] === "check")
) {
// Route: /:networkId/vaults/:vaultId/{get,check}
networkId = pathSegments[0];
vaultId = pathSegments[2];
command = pathSegments[3];
} else {
// Invalid route
console.log(`[info] Status 404`);
res.statusCode = 404;
return res.end(`Not found: ${pathname}`);
}
// Validate networkId
const bee = bees[networkId];
if (!bee) {
console.log(`[info] Status 400`);
res.statusCode = 400;
return res.end(`Invalid networkId: ${networkId}`);
}
// Validate vaultId
if (!vaultId || !vaultId.length) {
console.log(`[info] Status 400`);
res.statusCode = 400;
return res.end("Invalid request");
}
try {
// Hypothetical call to your Bee retrieval logic
const node = await bee.get(vaultId);
if (command === "get") {
if (!node || !node.value) {
console.log(`[info] Status 404`);
res.statusCode = 404;
return res.end(`No data for ID ${vaultId}`);
} else {
console.log(`[info] Status 200`);
writeHeadWithDefaults(res, 200, {
"Content-Type": "application/octet-stream",
});
return res.end(node.value);
}
} else if (command === "check") {
if (!node || !node.value) {
console.log(`[info] Status 404`);
writeHeadWithDefaults(res, 404, {
"Content-Type": "application/json",
});
return res.end(
JSON.stringify({
exists: false,
message: `No data found for vaultId: ${vaultId}`,
}),
);
} else {
console.log(`[info] Status 200`);
writeHeadWithDefaults(res, 200, {
"Content-Type": "application/json",
});
return res.end(
JSON.stringify({
exists: true,
message: `Data exists for vaultId: ${vaultId}`,
}),
);
}
} else {
console.log(`[info] Status 500`);
res.statusCode = 500;
return res.end("Internal server error");
}
} catch (err) {
console.log(`[info] Status 500`);
console.error(err);
res.statusCode = 500;
return res.end("Internal server error");
}
} else {
// If we’re here, it’s either a wrong path or an unsupported method
console.log(`[info] Status 404`);
res.statusCode = 404;
return res.end(`Not found: ${pathname}`);
}
});
return new Promise((resolve) => {
server.listen(port, "0.0.0.0", () => {
const actualPort = server.address().port; // If port is 0, retrieve assigned one
console.log(
`[info] API mode On. Http server listening on port: ${actualPort}`,
);
resolve(server);
});
});
}