Skip to content

Commit

Permalink
re-initialized the git
Browse files Browse the repository at this point in the history
  • Loading branch information
rajneesh069 committed Jun 23, 2024
0 parents commit e27677a
Show file tree
Hide file tree
Showing 13 changed files with 2,029 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DATABASE_URL="prisma_accelerate_link"
DIRECT_URL="postgres_db_url"
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# prod
dist/

# dev
.yarn/
!.yarn/releases
.vscode/*
!.vscode/launch.json
!.vscode/*.code-snippets
.idea/workspace.xml
.idea/usage.statistics.xml
.idea/shelf

# deps
node_modules/
.wrangler

# env
.env
.env.production
.dev.vars
wrangler.toml

# logs
logs/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

# misc
.DS_Store
213 changes: 213 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
## Some info about EDGE networks and Cloudflare Workers

### Edge Computing

**Edge Computing** refers to the practice of processing data closer to where it is generated (i.e., at the "edge" of the network), rather than in a centralized data center. This reduces latency and bandwidth usage, leading to faster response times and more efficient processing.

### Methods Using Edge Computing

1. **CDNs (Content Delivery Networks)**: Distribute content closer to the user to improve load times and reduce latency (e.g., Cloudflare, Akamai).
2. **Edge Functions**: Small units of code that run at the edge of the network, near the user (e.g., Cloudflare Workers, Vercel Edge Functions).
3. **IoT (Internet of Things)**: Devices process data locally or at a nearby gateway rather than sending all data to a central server.
4. **Fog Computing**: Extends cloud computing to the edge of the network, providing compute, storage, and networking services between end devices and cloud data centers.
5. **MEC (Multi-access Edge Computing)**: Provides IT service environment and cloud computing capabilities at the edge of the cellular network, closer to mobile users.
6. **Edge Caching**: Stores frequently accessed data at the edge to improve access times and reduce load on the central server.

### Why Some Packages Don't Work on Edge

1. **Node.js Modules**: Edge environments often don't support all Node.js modules, especially those related to file system operations, process management, or other server-specific functionalities.
2. **Resource Constraints**: Edge environments have limits on memory, CPU, and execution time, which can affect packages that are resource-intensive.
3. **Compatibility**: Some packages are designed with assumptions about the environment they will run in, which might not hold true in a constrained Edge environment.

### JWT and Edge Networks

#### Why JWT Might Not Work on Edge Networks

1. **Dependencies and Libraries**: Edge environments, like those provided by Next.js's Edge Functions, often have limitations on the libraries and dependencies you can use. They are designed to be lightweight and may not support all Node.js modules.
2. **Environment Constraints**: Edge environments are typically more restrictive in terms of execution capabilities, available resources, and execution time. Libraries that rely on certain Node.js built-in modules or features may not work as expected.

3. **Security Concerns**: Edge environments are designed to run code closer to the user, which can introduce security concerns. JWT libraries often rely on cryptographic functions, and the availability or security of these functions might be restricted in an Edge environment.

#### Using `jose` on Edge Networks

`jose` is a JavaScript library for JSON Web Tokens (JWTs) and related standards (JWS, JWE, etc.). It is often used in Edge environments because it is lightweight and designed to work without relying on Node.js-specific modules.

Example of using `jose` in a Next.js Edge Function:

```javascript
import { jwtVerify } from "jose";

export default async function handler(req) {
const token = req.headers.get("Authorization")?.split(" ")[1];

if (!token) {
return new Response("Unauthorized", { status: 401 });
}

try {
const { payload } = await jwtVerify(
token,
new TextEncoder().encode("your-SECRET-key")
);
return new Response(JSON.stringify(payload), { status: 200 });
} catch (error) {
return new Response("Unauthorized", { status: 401 });
}
}

export const config = {
runtime: "edge",
};
```
**Hono.js** is a lightweight, fast web framework for Cloudflare Workers. It simplifies the process of building serverless applications by providing a familiar routing and middleware structure.
### Example Hono.js Code for Cloudflare Workers
Below is an example of how you might use Hono.js with Cloudflare Workers to create a simple API that includes JWT authentication and some basic routing.
#### Setting Up Cloudflare Workers
1. **Install Wrangler**: The command-line tool to manage Cloudflare Workers.
```sh
npm install -g wrangler
```
2. **Initialize a New Worker**:
```sh
wrangler init my-worker
cd my-worker
```
3. **Install Hono.js**:
```sh
npm install hono jose
```
4. **Edit `wrangler.toml`**:
Make sure your `wrangler.toml` file is configured properly for your project.
5. **Edit `src/index.ts`** (or `src/index.js` if you're not using TypeScript):
#### Example `src/index.ts`
```typescript
import { Hono } from "hono";
import { jwt } from "hono/jwt";
import { jwtVerify } from "jose";

const app = new Hono();

// Middleware to verify JWT
const verifyJWT = async (ctx, next) => {
const token = ctx.req.headers.get("Authorization")?.split(" ")[1];
if (!token) {
return ctx.text("Unauthorized", 401);
}

try {
const { payload } = await jwtVerify(
token,
new TextEncoder().encode("your-SECRET-key")
);
ctx.set("user", payload);
await next();
} catch (error) {
return ctx.text("Unauthorized", 401);
}
};

// Public route
app.get("/", (ctx) => {
return ctx.text("Hello, World!");
});

// Protected route
app.get("/protected", verifyJWT, (ctx) => {
const user = ctx.get("user");
return ctx.json({ message: "This is a protected route", user });
});

// Start the Hono app
export default app;
```
#### Deploying to Cloudflare Workers
1. **Build and Deploy**:
```sh
wrangler publish
```
### Explanation
1. **Imports**: Import necessary modules from Hono and jose.
2. **App Initialization**: Initialize a new Hono application.
3. **JWT Middleware**: Create a middleware function to verify JWT tokens. It extracts the token from the `Authorization` header, verifies it using `jose`, and attaches the payload to the context.
4. **Routes**:
- **Public Route**: A simple public route that returns "Hello, World!".
- **Protected Route**: A route protected by the JWT middleware. It returns a message and the user information extracted from the JWT.
5. **Deployment**: Use Wrangler to build and deploy your worker to Cloudflare's edge network.
### What Cloudflare Workers Do
- **Run JavaScript at the Edge**: Execute code closer to the end user to reduce latency and improve performance.
- **Serverless**: No need to manage servers or infrastructure; Cloudflare handles scaling and availability.
- **Flexible**: Can handle various tasks such as serving static assets, modifying requests/responses, and running API endpoints.
### Why Use Edge Computing with Cloudflare Workers
- **Performance**: Reduced latency by running code closer to users.
- **Scalability**: Automatic scaling without the need for manual intervention.
- **Cost Efficiency**: Pay only for what you use, without the need for maintaining servers.
### Summary
- **Edge Computing** reduces latency by processing data closer to the user.
- **CDNs and Edge Functions** are popular examples of edge computing.
- Some libraries, like those for JWTs, may not work in Edge environments due to dependencies on Node.js modules.
- `jose` is a lightweight JWT library compatible with Edge environments.
- Edge environments have constraints that affect the compatibility of some Node.js packages.
Cloudflare Workers provide a serverless execution environment that allows you to run JavaScript code at the edge of the Cloudflare network. This means your code can run closer to your users, reducing latency and improving performance. Workers are often used for tasks like:
- Serving static content
- Manipulating HTTP requests and responses
- Implementing custom logic for routing and load balancing
- API gateways
- Authentication and authorization
- Handling serverless functions
#### Issues with JWT on Edge Networks
- **Dependencies and Libraries**: Edge environments may not support all Node.js modules, leading to compatibility issues.
- **Environment Constraints**: Limited resources and execution capabilities can prevent some JWT libraries from working.
- **Security Concerns**: Cryptographic functions required by JWT libraries might be restricted in Edge environments.
### Edge Computing and Its Methods
#### Edge Computing vs. CDN
- **Edge Computing**: Processing data closer to where it is generated, reducing latency and bandwidth usage.
- **CDN (Content Delivery Network)**: Distributes content closer to the user to improve load times and reduce latency.
#### Methods Using Edge Computing
1. **CDNs**: e.g., Cloudflare, Akamai.
2. **Edge Functions**: e.g., Cloudflare Workers, Vercel Edge Functions.
3. **IoT (Internet of Things)**: Local processing on devices or nearby gateways.
4. **Fog Computing**: Extends cloud services to the network edge.
5. **MEC (Multi-access Edge Computing)**: Provides cloud capabilities at the edge of cellular networks.
6. **Edge Caching**: Stores frequently accessed data at the edge.
### Cloudflare Workers and Hono.js
#### Cloudflare Workers
- **Serverless Execution**: Runs JavaScript at the edge, closer to users, improving performance and reducing latency.
- **Use Cases**: Serving static content, modifying HTTP requests/responses, API gateways, authentication, etc.
- **Benefits**: Scalability, cost efficiency, and reduced latency.
- **Hono.js** is a lightweight framework for Cloudflare Workers.
Loading

0 comments on commit e27677a

Please sign in to comment.