Skip to content

Commit 2e682ef

Browse files
authored
Merge pull request #18 from mojisdev/import-data
2 parents 7fc1014 + b545b62 commit 2e682ef

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@stoplight/spectral-cli": "^6.14.3",
2929
"eslint": "^9.22.0",
3030
"eslint-plugin-format": "^1.0.1",
31+
"nanotar": "^0.2.0",
3132
"tsx": "^4.19.3",
3233
"typescript": "^5.8.2",
3334
"vitest": "^3.0.9",

pnpm-lock.yaml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/copy-emoji-data.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import path from "node:path";
2+
import process from "node:process";
3+
import { unstable_startWorker } from "wrangler";
4+
5+
const root = path.resolve(import.meta.dirname, "../");
6+
7+
async function run() {
8+
const res = await fetch("https://github.com/mojisdev/emoji-data/archive/refs/heads/main.tar.gz");
9+
10+
if (!res.ok) {
11+
throw new Error(`Failed to fetch emoji-data: ${res.statusText}`);
12+
}
13+
14+
const blob = await res.blob();
15+
16+
const worker = await unstable_startWorker({
17+
config: path.join(root.toString(), "./wrangler.jsonc"),
18+
entrypoint: path.join(root.toString(), "./scripts/emoji-data-setup-app.ts"),
19+
});
20+
21+
const formData = new FormData();
22+
formData.append("file", blob, "emoji-data.tar.gz");
23+
24+
await worker.fetch("https://api.mojis.dev/upload", {
25+
method: "POST",
26+
// @ts-expect-error hmmm
27+
body: formData,
28+
});
29+
30+
await worker.dispose();
31+
}
32+
33+
run().catch((err) => {
34+
console.error(err);
35+
process.exit(1);
36+
});

scripts/emoji-data-setup-app.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { Hono } from "hono";
2+
import { HTTPException } from "hono/http-exception";
3+
import { parseTarGzip } from "nanotar";
4+
5+
const app = new Hono<{
6+
Bindings: {
7+
EMOJI_DATA: R2Bucket;
8+
};
9+
}>();
10+
11+
app.post(
12+
"/upload",
13+
async (c) => {
14+
const body = await c.req.parseBody();
15+
16+
const file = Array.isArray(body.file) ? body.file[0] : body.file;
17+
18+
if (file == null) {
19+
throw new HTTPException(400, {
20+
message: "No file uploaded",
21+
});
22+
}
23+
24+
if (typeof file === "string") {
25+
throw new HTTPException(400, {
26+
message: "invalid file uploaded",
27+
});
28+
}
29+
30+
const filePrefix = "emoji-data-main/data/";
31+
32+
const tar = await parseTarGzip(await file.arrayBuffer(), {
33+
filter(file) {
34+
if (!file.name.startsWith(filePrefix) || !file.name.endsWith(".json")) {
35+
return false;
36+
}
37+
38+
// remove the file prefix
39+
file.name = file.name.slice(filePrefix.length);
40+
41+
return true;
42+
},
43+
});
44+
45+
const promises = [];
46+
47+
for (const entry of tar) {
48+
if (entry.type !== "file") continue;
49+
const normalizedEntryName = entry.name.replace("./", "");
50+
51+
promises.push(c.env.EMOJI_DATA.put(normalizedEntryName, entry.text));
52+
}
53+
54+
c.executionCtx.waitUntil(
55+
Promise.all(promises),
56+
);
57+
58+
return c.json({
59+
message: "Files uploaded",
60+
});
61+
},
62+
);
63+
64+
console.log("started the emoji data setup app");
65+
66+
export default app;

0 commit comments

Comments
 (0)