-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
240 lines (205 loc) · 7.25 KB
/
index.js
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const express = require("express");
const app = express();
const cors = require("cors");
const datalayerCache = require("chia-datalayer-kv-cache");
const wallet = require("./rpcs/wallet");
const hexUtils = require("./utils/hex-utils");
const defaultConfig = require("./utils/defaultConfig");
const {
isValidJSON,
isBase64Image,
getFileExtension,
mimeTypes,
} = require("./utils/api-utils");
app.use(cors());
const multipartCache = {};
let config = defaultConfig;
datalayerCache.configure({
full_node_host: config.FULL_NODE_HOST,
datalayer_host: config.DATALAYER_HOST,
wallet_host: config.WALLET_HOST,
certificate_folder_path: config.CERTIFICATE_FOLDER_PATH,
default_wallet_id: config.DEFAULT_WALLET_ID,
});
function configure(newConfig) {
config = { ...config, ...newConfig };
datalayerCache.configure({
full_node_host: config.FULL_NODE_HOST,
datalayer_host: config.DATALAYER_HOST,
wallet_host: config.WALLET_HOST,
certificate_folder_path: config.CERTIFICATE_FOLDER_PATH,
default_wallet_id: config.DEFAULT_WALLET_ID,
});
}
app.get("/", async (req, res) => {
return res.json({
message: "Welcome to the Chia DataLayer Web2 Gateway",
endpoints: {
"/.well-known": "Returns the public deposit address of the node",
"/:storeId": "Returns all keys in the store",
"/:storeId/:key": "Returns the value of the key in the store",
},
});
});
app.get("/.well-known", async (req, res) => {
// Ideally we want to check the balance of the the current address and
// only return it if its zero, if its not zero we want to get the next unused address and
// return that. But I need to research how to check the balance of a single address.
const publicAddress = await wallet.getPublicAddress(config);
res.json({
xch_address: publicAddress,
donation_address:
"xch17edp36nd9m5jfcq2sa5qp25ekrrfguvpx05zce35pf65mlvfn4gqyl0434",
});
});
app.get("/:storeId/*", async (req, res) => {
const storeId = req.params.storeId;
let key = req.params[0];
// Remove everything after '#'
if (key.includes("#")) {
key = key.split("#")[0];
}
// Remove trailing slash
if (key.endsWith("/")) {
key = key.slice(0, -1);
}
try {
// A referrer indicates that the user is trying to access the store from a website
// we want to redirect them so that the URL includes the storeId in the path
const refererUrl = req.headers.referer;
if (refererUrl && !refererUrl.includes(storeId)) {
if (key.startsWith("/")) {
key = key.slice(1);
}
res.location(`${refererUrl}/${storeId}/${key}`);
res.status(301).end();
return;
}
const hexKey = hexUtils.encodeHex(key);
const dataLayerResponse = await datalayerCache.getValue({
id: storeId,
key: hexKey,
});
if (!dataLayerResponse) {
throw new Error(`Key not found ${key}`);
}
const value = hexUtils.decodeHex(dataLayerResponse.value);
const fileExtension = getFileExtension(key);
if (isValidJSON(value) && JSON.parse(value)?.type === "multipart") {
const mimeType = mimeTypes[fileExtension] || "application/octet-stream";
let multipartFileNames = JSON.parse(value).parts;
const cacheKey = multipartFileNames.sort().join(",");
multipartFileNames = multipartFileNames.sort((a, b) => {
const numberA = parseInt(a.split(".part")[1]);
const numberB = parseInt(b.split(".part")[1]);
return numberA - numberB;
});
if (multipartCache[cacheKey]) {
console.log("Serving from cache");
res.setHeader("Content-Type", mimeType);
return res.end(multipartCache[cacheKey]);
}
const hexPartsPromises = multipartFileNames.map((fileName) => {
console.log(`Stitching ${fileName}`);
const hexKey = hexUtils.encodeHex(fileName);
return datalayerCache.getValue({
id: storeId,
key: hexKey,
});
});
const dataLayerResponses = await Promise.all(hexPartsPromises);
const hexParts = dataLayerResponses.map((response) => response.value);
const resultHex = hexParts.join("");
const resultBuffer = Buffer.from(resultHex, "hex");
multipartCache[cacheKey] = resultBuffer;
res.setHeader("Content-Type", mimeType);
return res.end(resultBuffer);
} else if (fileExtension) {
const mimeType = mimeTypes[fileExtension] || "application/octet-stream";
res.setHeader("Content-Type", mimeType);
return res.send(value);
} else if (isValidJSON(value)) {
return res.json(JSON.parse(value));
} else if (isBase64Image(value)) {
const base64Image = value.split(";base64,").pop();
const imageBuffer = Buffer.from(base64Image, "base64");
const mimeType = value.match(/[^:]\w+\/[\w-+\d.]+(?=;|,)/)[0];
res.type(mimeType);
return res.send(imageBuffer);
} else {
return res.send(value);
}
} catch (error) {
console.log(error);
// If the key is not found and the store is empty we want to redirect the user to the store
// This adds support for SPA's hosted on datalayer
res.location(`/${storeId}`);
res.status(301).end();
}
});
app.get("/:storeId", async (req, res) => {
try {
let { storeId } = req.params;
const { showkeys } = req.query;
if (storeId.endsWith("/")) {
storeId = storeId.slice(0, -1);
}
// A referrer indicates that the user is trying to access the store from a website
// we want to redirect them so that the URL includes the storeId in the path
const refererUrl = req.headers.referer;
if (refererUrl && !refererUrl.includes(storeId)) {
if (storeId.startsWith("/")) {
storeId = storeId.slice(1);
}
res.location(`${refererUrl}/${storeId}`);
res.status(301).end();
return;
}
const dataLayerResponse = await datalayerCache.getKeys({
id: storeId,
});
if (dataLayerResponse.error) {
res.status(400).json({
error: 'Store has no data',
});
}
const apiResponse = dataLayerResponse.keys.map((key) =>
hexUtils.decodeHex(key)
);
// If index.html is in the store treat this endpoint like a website
if (apiResponse.length && apiResponse.includes("index.html") && !showkeys) {
const hexKey = hexUtils.encodeHex("index.html");
const dataLayerResponse = await datalayerCache.getValue({
id: storeId,
key: hexKey,
});
let value = hexUtils.decodeHex(dataLayerResponse.value);
// Add the base tag
const baseTag = `<base href="/${storeId}/">`;
value = value.replace("<head>", `<head>\n ${baseTag}`);
// Set Content-Type to HTML and send the decoded value
res.setHeader("Content-Type", "text/html");
return res.send(value);
}
return res.json(apiResponse);
} catch (error) {
console.log(error);
res.status(500).json({
error: "Can not retrieve data or it doesnt exist on this node",
});
}
});
function start() {
console.log(
`Starting web2 gateway server on port ${config.WEB2_GATEWAY_PORT}`
);
app.listen(config.WEB2_GATEWAY_PORT, config.WEB2_BIND_ADDRESS, () => {
console.log(
`DataLayer Web2 Gateway Server running, go to http://localhost:${config.WEB2_GATEWAY_PORT} to view your datalayer`
);
});
}
module.exports = {
start,
configure,
};