-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(/api/unicode/names): add unicode codepoint to name service #424
Open
sidvishnoi
wants to merge
2
commits into
main
Choose a base branch
from
unicode-names
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import express from "express"; | ||
import unicode from "./unicode/index.js"; | ||
|
||
const router = express.Router({ mergeParams: true }); | ||
|
||
router.use("/unicode", unicode); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import path from "node:path"; | ||
|
||
import express from "express"; | ||
import cors from "cors"; | ||
|
||
import { env, ms } from "../../../utils/misc.js"; | ||
|
||
import namesRoute from "./names.js"; | ||
import updateRoute from "./update.js"; | ||
|
||
const DATA_DIR = env("DATA_DIR"); | ||
|
||
const router = express.Router({ mergeParams: true }); | ||
|
||
router | ||
.options("/names", cors({ methods: ["POST", "GET"], maxAge: ms("1day") })) | ||
.post("/names", express.json({ limit: "2mb" }), cors(), namesRoute); | ||
router.post("/update", updateRoute); | ||
router.use("/data", express.static(path.join(DATA_DIR, "unicode"))); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import path from "node:path"; | ||
import { tmpdir } from "node:os"; | ||
import { createReadStream, createWriteStream } from "node:fs"; | ||
import { mkdir, rm } from "node:fs/promises"; | ||
import { Readable } from "node:stream"; | ||
import { ReadableStream } from "node:stream/web"; | ||
import { finished } from "node:stream/promises"; | ||
import { createInterface } from "node:readline/promises"; | ||
|
||
import { env } from "../../../../utils/misc.js"; | ||
|
||
const DATA_DIR = env("DATA_DIR"); | ||
|
||
export const INPUT_DATA_SOURCE = `https://unicode.org/Public/UNIDATA/UnicodeData.txt`; | ||
const OUT_DIR_BASE = path.join(DATA_DIR, "unicode"); | ||
const OUT_FILE_BY_CODEPOINT = path.resolve( | ||
OUT_DIR_BASE, | ||
"./codepoint-to-name.json", | ||
); | ||
|
||
const defaultOptions = { forceUpdate: false }; | ||
type Options = typeof defaultOptions; | ||
|
||
export default async function main(options: Partial<Options> = {}) { | ||
options = { ...defaultOptions, ...options } as Options; | ||
const hasUpdated = await updateInputSource(); | ||
if (!hasUpdated && !options.forceUpdate) { | ||
console.log("Nothing to update"); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// download file and convert its data to JSON | ||
async function updateInputSource() { | ||
await mkdir(OUT_DIR_BASE, { recursive: true }); | ||
|
||
const namesJs = path.join(tmpdir(), "unicode-all-names.js"); | ||
await rm(namesJs, { force: true }); | ||
await rm(OUT_FILE_BY_CODEPOINT, { force: true }); | ||
|
||
console.log(`Downloading`, INPUT_DATA_SOURCE, "to", namesJs); | ||
await downloadFile(INPUT_DATA_SOURCE, namesJs); | ||
|
||
console.log("Converting to JSON and writing to", OUT_FILE_BY_CODEPOINT); | ||
const rl = createInterface({ | ||
input: createReadStream(namesJs), | ||
crlfDelay: Infinity, | ||
}); | ||
const dest = createWriteStream(OUT_FILE_BY_CODEPOINT, { flags: "a" }); | ||
dest.write("[\n"); | ||
for await (const line of rl) { | ||
const parsed = parseLine(line); | ||
if (!parsed) continue; | ||
dest.write(JSON.stringify(parsed) + ",\n"); | ||
} | ||
dest.write(`["null", {"name": ""}]`); | ||
dest.write("\n]\n"); | ||
await new Promise(resolve => dest.end(resolve)); | ||
|
||
console.log("Wrote to", OUT_FILE_BY_CODEPOINT); | ||
await rm(namesJs, { force: true }); | ||
|
||
return true; | ||
} | ||
|
||
// Parse a line based on https://www.unicode.org/Public/5.1.0/ucd/UCD.html#UnicodeData.txt | ||
// e.g. 0001;<control>;Cc;0;BN;;;;;N;START OF HEADING;;;; | ||
// -> 0001 -> {name: "[control]", generalCategory: "Cc", ...} | ||
function parseLine(line: string) { | ||
if (line.startsWith("#")) { | ||
return null; // comments | ||
} | ||
|
||
const parts = line.split(";"); | ||
const codepoint = parts[0]; | ||
const name = parts[1].replace(/[<>]/g, s => (s === "<" ? "[" : "]")); | ||
return [codepoint, { name }] as const; | ||
} | ||
|
||
async function downloadFile(url: string, destination: string) { | ||
const res = await fetch(url); | ||
await mkdir(path.dirname(destination), { recursive: true }); | ||
const outStream = createWriteStream(destination, { flags: "wx" }); | ||
await finished( | ||
Readable.fromWeb(res.body as ReadableStream<any>).pipe(outStream), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Store } from "./store.js"; | ||
|
||
export const store = new Store(); | ||
|
||
export type { Store }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import path from "path"; | ||
import { readFileSync } from "fs"; | ||
|
||
import { env } from "../../../../utils/misc.js"; | ||
import { INPUT_DATA_SOURCE } from "./scraper.js"; | ||
|
||
export class Store { | ||
version = -1; | ||
private codepointToName: Map<string, { name: string }> = new Map(); | ||
|
||
constructor() { | ||
this.fill(); | ||
} | ||
|
||
/** Fill the store with its contents from the filesystem. */ | ||
fill() { | ||
this.codepointToName = new Map(readJson("codepoint-to-name.json")); | ||
this.version = Date.now(); | ||
} | ||
|
||
getNameByHexCodePoint(hex: string) { | ||
return this.codepointToName.get(hex) ?? null; | ||
} | ||
|
||
get dataSource() { | ||
return INPUT_DATA_SOURCE; | ||
} | ||
} | ||
|
||
function readJson(filename: string) { | ||
const DATA_DIR = env("DATA_DIR"); | ||
const dataFile = path.resolve(DATA_DIR, `./unicode/${filename}`); | ||
const text = readFileSync(dataFile, "utf8"); | ||
return JSON.parse(text); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { Request, Response } from "express"; | ||
|
||
import { store, type Store } from "./lib/store-init.js"; | ||
|
||
interface Query { | ||
/** Codepoint as hex */ | ||
hex: string; | ||
} | ||
interface Result { | ||
name: string; | ||
} | ||
|
||
type Options = Record<never, never>; | ||
|
||
interface RequestBody { | ||
queries: Query[]; | ||
options?: Options; | ||
} | ||
type IRequest = Request<never, any, RequestBody>; | ||
|
||
interface ResponseData { | ||
data: Array<{ query: Query; result: Result | null }>; | ||
metadata: { lastParsedAt: string; dataSource: string }; | ||
} | ||
|
||
export default function route(req: IRequest, res: Response) { | ||
const { options = {}, queries = [] } = req.body; | ||
const data: ResponseData["data"] = queries.map(query => ({ | ||
query, | ||
result: search(query, store, options), | ||
})); | ||
|
||
Object.assign(res.locals, { | ||
errors: getErrorCount(data), | ||
queries: queries.length, | ||
}); | ||
|
||
const result: ResponseData = { | ||
data, | ||
metadata: { | ||
lastParsedAt: store.version.toString(), | ||
dataSource: store.dataSource, | ||
}, | ||
}; | ||
res.json(result); | ||
} | ||
|
||
function search(query: Query, store: Store, _options: Options): Result | null { | ||
if (query.hex) { | ||
query.hex = query.hex.toUpperCase().padStart(4, "0"); | ||
const data = store.getNameByHexCodePoint(query.hex); | ||
return data; | ||
} | ||
return null; | ||
} | ||
|
||
function getErrorCount(results: ResponseData["data"]) { | ||
return results.filter(({ result }) => !result).length; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import path from "path"; | ||
import { legacyDirname } from "../../../utils/misc.js"; | ||
import { BackgroundTaskQueue } from "../../../utils/background-task-queue.js"; | ||
import { store } from "./lib/store-init.js"; | ||
import type { Request, Response } from "express"; | ||
|
||
const workerFile = path.join(legacyDirname(import.meta), "update.worker.js"); | ||
const taskQueue = new BackgroundTaskQueue<typeof import("./update.worker.js")>( | ||
workerFile, | ||
"unicode_update", | ||
); | ||
|
||
export default async function route(req: Request, res: Response) { | ||
const job = taskQueue.add({}); | ||
try { | ||
const { updated } = await job.run(); | ||
if (updated) { | ||
store.fill(); | ||
} | ||
} catch { | ||
res.status(500); | ||
} finally { | ||
res.locals.job = job.id; | ||
res.send(job.id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import unicodeScraper from "./lib/scraper.js"; | ||
|
||
interface Input {} | ||
|
||
export default async function unicodeUpdate(_input: Input) { | ||
const updated = await unicodeScraper(); | ||
return { updated }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or something similar... just for a bit more context.