@aryanbansal-launch/edge-utils 0.1.8 β†’ 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -85,7 +85,7 @@ export default async function handler(request, context) {
85
85
  `.trim();
86
86
 
87
87
  async function init() {
88
- console.log(`\n${colors.bright}${colors.cyan}πŸš€ Edge Utilities: Contentstack Launch Setup${colors.reset}\n`);
88
+ console.log(`\n${colors.bright}${colors.cyan}πŸš€ create-launch-edge: Contentstack Launch Initializer${colors.reset}\n`);
89
89
 
90
90
  let actionsTaken = 0;
91
91
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aryanbansal-launch/edge-utils",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "repository": {
@@ -13,7 +13,7 @@
13
13
  },
14
14
  "main": "dist/index.js",
15
15
  "bin": {
16
- "launch-init": "./bin/launch-init.js",
16
+ "create-launch-edge": "./bin/launch-init.js",
17
17
  "launch-config": "./bin/launch-config.js"
18
18
  },
19
19
  "exports": {
package/readme.md CHANGED
@@ -3,133 +3,133 @@
3
3
  [![npm version](https://img.shields.io/npm/v/@aryanbansal-launch/edge-utils.svg)](https://www.npmjs.com/package/@aryanbansal-launch/edge-utils)
4
4
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
5
 
6
- A lightweight, high-performance toolkit specifically designed for **Contentstack Launch Edge Functions**. Speed up your development with production-ready utilities for security, authentication, routing, and Next.js compatibilityβ€”all optimized to run at the edge.
7
-
8
- ---
9
-
10
- ## ✨ Features
11
-
12
- - πŸ›‘οΈ **Security First**: Block AI crawlers and manage IP access with ease.
13
- - πŸ” **Edge Auth**: Implement Basic Auth directly at the edge for specific hostnames.
14
- - πŸ“ **Geo-Aware**: Easily extract location data from request headers.
15
- - βš›οΈ **Next.js Ready**: Built-in fixes for RSC header issues on Launch proxies.
16
- - πŸš€ **Cache Priming**: Easily manage cache warming URLs via CLI.
17
- - πŸ”€ **Smart Routing**: Declarative redirects based on path and method.
18
- - ⚑ **Zero Dependencies**: Lightweight and optimized for edge runtime limits.
6
+ A comprehensive toolkit for [Contentstack Launch](https://www.contentstack.com/docs/developers/launch) (the high-performance frontend hosting service by [Contentstack](https://www.contentstack.com/)). This library provides production-ready utilities to simplify [Edge Functions](https://www.contentstack.com/docs/developers/launch/edge-functions) development, security, and performance optimization.
19
7
 
20
8
  ---
21
9
 
22
10
  ## ⚑ Quick Start (Recommended)
23
11
 
24
- Set up your entire edge environment in seconds with our automated CLI tool.
12
+ Bootstrap your edge environment in seconds using our automated initializer. Run this command from your **project root**:
25
13
 
26
- ### 1. Install
27
14
  ```bash
15
+ # Install the package
28
16
  npm install @aryanbansal-launch/edge-utils
29
- ```
30
17
 
31
- ### 2. Initialize
32
- Run this command from your **project root**:
33
- ```bash
34
- npx launch-init
18
+ # Initialize Edge Functions
19
+ npx create-launch-edge
35
20
  ```
36
- This will automatically create the `functions/` directory and a boilerplate `[proxy].edge.js` handler for you.
37
21
 
38
- ### 3. Configure (Optional)
39
- Manage your `launch.json` ([Redirects](https://www.contentstack.com/docs/developers/launch/edge-url-redirects), [Rewrites](https://www.contentstack.com/docs/developers/launch/edge-url-rewrites), and [Cache Priming](https://www.contentstack.com/docs/developers/launch/cache-priming)) interactively:
40
- ```bash
41
- npx launch-config
42
- ```
22
+ This command will automatically create the `functions/` directory and a production-ready `[proxy].edge.js` boilerplate handler.
23
+
24
+ ---
25
+
26
+ ## ✨ Features & Deep Dive
27
+
28
+ ### πŸ›‘οΈ Security & Access Control
29
+ - **[Block AI Crawlers](https://www.contentstack.com/docs/developers/launch/blocking-ai-crawlers)**: Automatically detects and rejects requests from known scrapers (GPTBot, ClaudeBot, etc.) to protect your content and server resources.
30
+ - **[Restricted Default Domains](https://www.contentstack.com/docs/developers/launch/blocking-default-launch-domains-from-google-search)**: By default, Launch provides a `*.contentstackapps.com` domain. This utility forces visitors to your custom domain, which is essential for SEO (preventing duplicate content) and professional branding.
31
+ - **[IP Access Control](https://www.contentstack.com/docs/developers/launch/ip-based-access-control-using-edge-functions)**: Create a lightweight firewall at the edge to whitelist internal teams or block malicious IPs before they hit your application logic.
32
+ - **[Edge Auth](https://www.contentstack.com/docs/developers/launch/password-protection-for-environments)**: Implement [Basic Authentication](https://www.contentstack.com/docs/developers/launch/password-protection-for-environments) directly at the edge to protect staging environments or specific paths. (Note: Hashing is recommended for production environments).
33
+
34
+ ### βš›οΈ Next.js Optimization
35
+ - **[RSC Header Fix](https://www.contentstack.com/docs/developers/launch/handling-nextjs-rsc-issues-on-launch)**: Next.js React Server Components (RSC) use a special `rsc` header. Sometimes, proxies or caches can incorrectly serve RSC data when a full page load is expected. This utility detects these edge cases and strips the header to ensure the correct response type is served.
36
+
37
+ ### πŸ“ Performance & Geo-Awareness
38
+ - **[Geo-Location Access](https://www.contentstack.com/docs/developers/launch/geolocation-headers-in-launch)**: Contentstack Launch injects geography data into request headers. This utility parses those headers into a clean object (`country`, `city`, `region`, etc.), enabling you to personalize content or restrict features based on user location.
39
+ - **[Cache Priming](https://www.contentstack.com/docs/developers/launch/cache-priming)**: Use the `launch-config` CLI to pre-load critical URLs into the edge cache, eliminating "cold start" latency for your first visitors after a deployment.
40
+
41
+ ### πŸ”€ Smart Routing
42
+ - **Declarative Redirects**: Handle complex, logic-based redirects at runtime.
43
+ - **Runtime vs Config**:
44
+ - Use **`launch.json`** ([Static Redirects](https://www.contentstack.com/docs/developers/launch/edge-url-redirects)) for high-performance, simple path-to-path mapping.
45
+ - Use **`redirectIfMatch`** (this library) for dynamic redirects that require logic, such as checking cookies, headers, or geo-location before redirecting.
43
46
 
44
47
  ---
45
48
 
46
- ## πŸ› οΈ Usage Example
49
+ ## πŸ› οΈ Detailed Usage Example
47
50
 
48
- Once initialized, your `functions/[proxy].edge.js` will look like a powerful middleware chain:
51
+ Your `functions/[proxy].edge.js` acts as a **middleware chain**. You can layer these utilities to create complex edge logic:
49
52
 
50
53
  ```javascript
51
54
  import {
52
- jsonResponse,
53
- passThrough,
54
- redirectIfMatch,
55
- protectWithBasicAuth,
56
- ipAccessControl,
57
- blockAICrawlers,
58
55
  blockDefaultDomains,
56
+ handleNextJS_RSC,
57
+ blockAICrawlers,
58
+ ipAccessControl,
59
+ protectWithBasicAuth,
60
+ redirectIfMatch,
59
61
  getGeoHeaders,
60
- handleNextJS_RSC
62
+ passThrough
61
63
  } from "@aryanbansal-launch/edge-utils";
62
64
 
63
65
  export default async function handler(request, context) {
64
- // 1. πŸ›‘οΈ Block access via default Launch domains
65
- const defaultDomainResponse = blockDefaultDomains(request);
66
- if (defaultDomainResponse) return defaultDomainResponse;
67
-
68
- // 2. βš›οΈ Fix Next.js RSC issues for specific paths
69
- const rscResponse = await handleNextJS_RSC(request, {
66
+ // 1. πŸ›‘οΈ Force Custom Domain (SEO Best Practice)
67
+ // Blocks access via *.contentstackapps.com
68
+ const domainCheck = blockDefaultDomains(request);
69
+ if (domainCheck) return domainCheck;
70
+
71
+ // 2. βš›οΈ Fix Next.js RSC Header issues
72
+ // Prevents "JSON-only" responses on page refreshes
73
+ const rscCheck = await handleNextJS_RSC(request, {
70
74
  affectedPaths: ["/shop", "/about"]
71
75
  });
72
- if (rscResponse) return rscResponse;
76
+ if (rscCheck) return rscCheck;
73
77
 
74
- // 3. πŸ€– Block AI bots immediately
75
- const botResponse = blockAICrawlers(request);
76
- if (botResponse) return botResponse;
78
+ // 3. πŸ€– Block Aggressive Bots
79
+ const botCheck = blockAICrawlers(request);
80
+ if (botCheck) return botCheck;
77
81
 
78
- // 4. 🧱 IP Whitelisting
79
- const ipResponse = ipAccessControl(request, { allow: ["203.0.113.10"] });
80
- if (ipResponse) return ipResponse;
82
+ // 4. 🧱 Firewall
83
+ const ipCheck = ipAccessControl(request, { allow: ["203.0.113.10"] });
84
+ if (ipCheck) return ipCheck;
81
85
 
82
- // 5. πŸ” Domain-specific Basic Auth (e.g., for staging)
83
- const authResponse = await protectWithBasicAuth(request, {
86
+ // 5. πŸ” Password Protection
87
+ const auth = await protectWithBasicAuth(request, {
84
88
  hostnameIncludes: "staging.myapp.com",
85
89
  username: "admin",
86
90
  password: "securepassword123"
87
91
  });
88
- if (authResponse && authResponse.status === 401) return authResponse;
92
+ if (auth && auth.status === 401) return auth;
89
93
 
90
- // 6. πŸ”€ SEO-friendly Redirects
91
- const redirectResponse = redirectIfMatch(request, {
92
- path: "/legacy-url",
93
- to: "/modern-url",
94
+ // 6. πŸ”€ Logic-Based Redirects
95
+ const redirect = redirectIfMatch(request, {
96
+ path: "/legacy-page",
97
+ to: "/new-page",
94
98
  status: 301
95
99
  });
96
- if (redirectResponse) return redirectResponse;
100
+ if (redirect) return redirect;
97
101
 
98
- // 7. πŸ“ Geo-Location Access
102
+ // 7. πŸ“ Personalization
99
103
  const geo = getGeoHeaders(request);
100
- console.log(`Request from ${geo.city}, ${geo.country}`);
104
+ if (geo.country === "UK") {
105
+ // Custom logic for UK visitors...
106
+ }
101
107
 
102
- // 8. πŸš€ Pass through to origin
108
+ // 8. πŸš€ Pass through to Origin
103
109
  return passThrough(request);
104
110
  }
105
111
  ```
106
112
 
107
113
  ---
108
114
 
109
- ## πŸ“– API Reference
110
-
111
- ### πŸ›‘οΈ Security
112
- - **`blockAICrawlers(request, bots?)`**: Detects and blocks known AI crawlers (GPTBot, ClaudeBot, etc.) based on the User-Agent.
113
- - **`blockDefaultDomains(request, { domainToBlock? })`**: Prevents users from accessing your site via the default `*.contentstackapps.com` domains, forcing them to use your custom domain.
114
- - **`ipAccessControl(request, { allow?, deny? })`**: A simple firewall to whitelist or blacklist specific IP addresses at the edge.
115
-
116
- ### πŸ” Authentication
117
- - **`protectWithBasicAuth(request, options)`**: Prompt for credentials based on hostname.
115
+ ## βš™οΈ Configuration CLI
118
116
 
119
- ### πŸ”€ Redirection
120
- - **`redirectIfMatch(request, options)`**: Perform SEO-friendly redirects at the edge.
117
+ Manage your `launch.json` file interactively to handle bulk settings:
121
118
 
122
- ### πŸ“ Geo Location
123
- - **`getGeoHeaders(request)`**: Returns an object with `country`, `region`, `city`, `latitude`, `longitude`.
119
+ ```bash
120
+ npx launch-config
121
+ ```
124
122
 
125
- ### βš›οΈ Next.js
126
- - **`handleNextJS_RSC(request, { affectedPaths })`**: Resolves RSC header issues on Contentstack Launch.
123
+ ### Supported Settings:
124
+ - **Bulk Redirects**: Add multiple sources and destinations easily.
125
+ - **Rewrites**: Map internal paths to external APIs or micro-services.
126
+ - **Cache Priming**: Add a comma-separated list of URLs to warm up the CDN.
127
127
 
128
128
  ---
129
129
 
130
130
  ## 🌐 Platform Support
131
131
 
132
- This library is exclusively optimized for **[Contentstack Launch](https://www.contentstack.com/docs/developers/launch)**.
132
+ This library is exclusively optimized for **[Contentstack Launch](https://www.contentstack.com/docs/developers/launch)**. It assumes an environment where `Request`, `Response`, and standard Edge Global APIs are available.
133
133
 
134
134
  ---
135
135