-
Notifications
You must be signed in to change notification settings - Fork 0
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
chore: improve local dx #19
Conversation
WalkthroughThis pull request adds comprehensive documentation to the project and enhances the setup and data processing scripts. It updates the README and package scripts to clearly define installation, usage, and emoji data management. The changes refactor the emoji data extraction process by directly handling file extraction and applying improved error handling in the worker functions. Additionally, new fields for metadata are introduced in the worker configuration to capture query details. Changes
Sequence Diagram(s)sequenceDiagram
participant Setup as Setup Script
participant FS as File System
participant API as "api.mojis.dev/upload"
Setup->>FS: Read compressed emoji data file
FS-->>Setup: Return file data (tar.gz)
Setup->>Setup: Parse file and create Blob/FormData
Setup->>API: Send POST request with emoji data
API-->>Setup: Return JSON response
Setup->>Setup: Log response and dispose worker
Possibly related PRs
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 0
🧹 Nitpick comments (12)
scripts/setup-dev/setup.ts (2)
14-15
: Add error handling for file reading operationThe file reading operation doesn't have specific error handling. If the emoji-data.tar.gz file doesn't exist, it will throw a generic error.
- const file = await readFile(path.join(root.toString(), "./node_modules/.emoji-data/emoji-data.tar.gz")); - const blob = new Blob([file]); + try { + const filePath = path.join(root.toString(), "./node_modules/.emoji-data/emoji-data.tar.gz"); + const file = await readFile(filePath); + const blob = new Blob([file]); + } catch (err) { + console.error(`Failed to read emoji data file: ${err.message}`); + throw new Error("Emoji data file not found. Run 'pnpm run copy:emoji-data' first."); + }
4-4
:❓ Verification inconclusive
Consider using the stable API if available
The script uses
unstable_startWorker
which suggests it's using an experimental API that might change in future versions of wrangler.
🏁 Script executed:
#!/bin/bash # Check if there's a stable API alternative to unstable_startWorker grep -r "startWorker" --include="*.d.ts" node_modules/wrangler/Length of output: 119
Review Verification: Verify Experimental API Usage
After checking the repository, our initial search for a stable alternative to
unstable_startWorker
in the wrangler package was inconclusive (the expected files from the wrangler dependency weren’t available). Since we couldn’t confirm a stable API option via the code search, please verify with the official wrangler documentation or your local setup to determine if a stable API has been introduced.
- File:
scripts/setup-dev/setup.ts
- Line: 4 (usage of
unstable_startWorker
)If a stable API becomes available, update the import accordingly. Otherwise, ensure that using the experimental API is safe for your workflow.
scripts/copy-emoji-data.ts (3)
22-35
: Add error handling for tar parsing operationThe tar parsing operation could fail if the blob contains invalid data, but there's no specific error handling for this operation.
const filePrefix = "emoji-data-main/data/"; -const tar = await parseTarGzip(await blob.arrayBuffer(), { - filter(file) { - if (!file.name.startsWith(filePrefix) || !file.name.endsWith(".json")) { - return false; - } - - // remove the file prefix - file.name = file.name.slice(filePrefix.length); - - return true; - }, -}); +try { + const tar = await parseTarGzip(await blob.arrayBuffer(), { + filter(file) { + if (!file.name.startsWith(filePrefix) || !file.name.endsWith(".json")) { + return false; + } + + // remove the file prefix + file.name = file.name.slice(filePrefix.length); + + return true; + }, + }); +} catch (err) { + console.error(`Failed to parse emoji data: ${err.message}`); + throw new Error("Invalid emoji data format"); +}
37-41
: Add more granular error handling for file writing operationsThe file writing operations could fail individually, but there's no specific error handling for these operations.
for await (const file of tar) { - const filePath = path.join(root.toString(), "./node_modules/.emoji-data", file.name); - await mkdir(path.dirname(filePath), { recursive: true }); - await writeFile(filePath, file.text); + try { + const filePath = path.join(root.toString(), "./node_modules/.emoji-data", file.name); + await mkdir(path.dirname(filePath), { recursive: true }); + await writeFile(filePath, file.text); + } catch (err) { + console.error(`Failed to write file ${file.name}: ${err.message}`); + // Continue with other files instead of failing the entire process + } }
43-43
: Add progress indication for better user experienceConsider adding progress indication to inform the user about the copying process, especially useful for large data sets.
+let fileCount = 0; for await (const file of tar) { const filePath = path.join(root.toString(), "./node_modules/.emoji-data", file.name); await mkdir(path.dirname(filePath), { recursive: true }); await writeFile(filePath, file.text); + fileCount++; } -console.log("emoji data copied"); +console.log(`emoji data copied: ${fileCount} files processed`);scripts/setup-dev/setup-worker.ts (1)
68-68
: Update console log message to match the new file nameThe console log message still references "emoji data setup app" despite the file being renamed to "setup-worker.ts".
-console.log("started the emoji data setup app"); +console.log("started the emoji data setup worker");README.md (6)
1-4
: Project Title and Description Clarity
The title (“# api.mojis.dev”) and the one-line description (“A RESTful API for accessing emoji data.”) clearly introduce the project. Consider adding a brief summary of key features or benefits below this description to provide new users with a quick overview of what the API offers.
5-12
: Installation Instructions Completeness
The installation section is clear and provides concise commands to clone the repository and install dependencies usingpnpm
. It may be worthwhile to note explicitly thatpnpm
is required, or include a link to the pnpm installation guide for those less familiar with it.
13-17
: Usage Command Simplicity
The usage section, with the commandpnpm run dev
, is straightforward and gives a clear starting point for running the development server. If there are any required environment variables or additional setup steps, consider mentioning them here to further improve local DX.
19-22
: Emoji Data Handling Note Enhancement
The note explains that emoji data will be copied from mojis/emoji-data into thenode_modules/.emoji-data
directory and provides an update command usingpnpm run copy:emoji-data
. Since the PR objectives mention improvements in emoji data handling—including a newsetup:emoji-data
script—it might be helpful to clarify when to use each script or add a brief reference to the new setup process.
23-26
: API Documentation Section
The API Documentation section is succinct and includes a direct link to api.mojis.dev using Scalar. For extra clarity, consider adding a brief outline of what users can expect to find in the documentation (e.g., list of endpoints, usage examples).
27-35
: Development Tools Overview
The development section effectively lists the frameworks and tools used in the project (Hono, Cloudflare Workers, Vitest, and Scalar). Given the enhancements mentioned in the PR objectives—such as changes to setup scripts likesetup:emoji-data
—you might include a brief note or link to further documentation on these new scripts to enhance the local developer experience.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
README.md
(1 hunks)package.json
(1 hunks)scripts/copy-emoji-data.ts
(2 hunks)scripts/setup-dev/setup-worker.ts
(1 hunks)scripts/setup-dev/setup.ts
(1 hunks)worker-configuration.d.ts
(2 hunks)
🔇 Additional comments (9)
worker-configuration.d.ts (1)
4619-4629
: Enhanced D1 metadata provides valuable query insightsThese additions to the
D1Meta
interface provide useful information about database queries:
served_by_region
indicates which geographical region handled the queryserved_by_primary
shows whether the primary database instance processed the querytimings.sql_duration_ms
captures the execution time, excluding network latencyThese properties will help with monitoring database performance, troubleshooting regional issues, and understanding query execution patterns in distributed environments.
scripts/setup-dev/setup.ts (3)
9-12
: LGTM! Well-structured worker initializationThe worker configuration is properly set up with the correct paths to the config file and entrypoint.
35-38
: LGTM! Proper error handling in main functionThe error handling for the main function is well-implemented, properly catching errors and exiting the process with a non-zero status code.
20-25
:❓ Verification inconclusive
Improve request error handling and remove ts-expect-error
The request doesn't check if the response was successful, and there's a TypeScript error suppression without explanation.
console.log("sending request to worker"); const res = await worker.fetch("https://api.mojis.dev/upload", { method: "POST", - // @ts-expect-error hmmm body: formData, }); + if (!res.ok) { + const text = await res.text(); + throw new Error(`Failed to upload emoji data: ${res.status} ${text}`); + }To resolve the ts-expect-error, consider using proper type annotations or creating a typed wrapper for the fetch API.
🏁 Script executed:
#!/bin/bash # Check the wrangler worker fetch method type definition grep -A 10 "fetch" --include="*.d.ts" node_modules/wrangler/Length of output: 116
Improve fetch error handling and ensure proper type annotations in setup.ts
The updated diff should:
- Remove the
@ts-expect-error
comment and replace it with proper type handling.- Add error checking to throw a descriptive error when the response isn’t successful.
- Note: The verification of the worker’s
fetch
type definition was inconclusive (the grep command failed to locate the expected files). Please manually verify or update the import of the correct type definitions (e.g., from an alternative package like@cloudflare/workers-types
if applicable) to safely remove error suppressions.console.log("sending request to worker"); const res = await worker.fetch("https://api.mojis.dev/upload", { method: "POST", - // @ts-expect-error hmmm body: formData, }); + if (!res.ok) { + const text = await res.text(); + throw new Error(`Failed to upload emoji data: ${res.status} ${text}`); + }scripts/copy-emoji-data.ts (2)
1-5
: LGTM! Good imports for file operationsAppropriate imports for file system operations and the parseTarGzip library.
10-12
: LGTM! Directory existence check and creationGood practice to check if the directory exists before attempting to create it.
scripts/setup-dev/setup-worker.ts (1)
54-64
: LGTM! Improved error handling with try-catch blockThe addition of a try-catch block greatly improves error handling during the file upload process. The response now properly indicates success or failure with an appropriate HTTP status code.
One small suggestion to make the error message more helpful:
try { await Promise.all(promises); return c.json({ message: "Files uploaded", }); } catch (err) { console.error(err); return c.json({ - message: "Failed to upload files", + message: `Failed to upload files: ${err.message}`, }, 500); }package.json (1)
10-12
: LGTM! Well-structured scripts for emoji data handlingThe updated script organization provides a clearer separation of concerns between copying and setting up emoji data. The
predev
script now properly runs both steps sequentially.README.md (1)
36-38
: License Information Accuracy
The license section clearly states that the project is published under the MIT License. This information is accurate and requires no further changes.
This pull request includes several changes to improve the setup and development process for the
api.mojis.dev
project. The most important changes include updates to theREADME.md
file, modifications to thepackage.json
scripts, and enhancements to the emoji data handling scripts.Documentation updates:
README.md
: Added sections for installation, usage, API documentation, development, and license.Script and configuration updates:
package.json
: Updated thepredev
script to include a newsetup:emoji-data
script and restructured the emoji data handling scripts.Emoji data handling improvements:
scripts/copy-emoji-data.ts
: Added checks for the existence of the emoji data directory, created the directory if it does not exist, and updated the script to parse and copy emoji data files directly. [1] [2]scripts/setup-dev/setup-worker.ts
: Renamed fromemoji-data-setup-app.ts
and added error handling for the file upload process.scripts/setup-dev/setup.ts
: Added a new script to handle the setup of emoji data for development, including starting a worker and uploading the emoji data.Summary by CodeRabbit