@aryanbansal-launch/edge-utils 0.1.7 β†’ 0.1.9

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
 
@@ -1,6 +1,3 @@
1
- /**
2
- * Generates a standard launch.json configuration object.
3
- */
4
1
  export function generateLaunchConfig(options) {
5
2
  return {
6
3
  redirects: options.redirects || [],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aryanbansal-launch/edge-utils",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
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
@@ -1,132 +1,128 @@
1
1
  # πŸš€ Edge Utils for Contentstack Launch
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/@launch/edge-utils.svg)](https://www.npmjs.com/package/@aryanbansal-launch/edge-utils)
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, Rewrites, and 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
+
33
+ ### βš›οΈ Next.js Optimization
34
+ - **[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.
35
+
36
+ ### πŸ“ Performance & Geo-Awareness
37
+ - **[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.
38
+ - **[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.
39
+
40
+ ### πŸ”€ Smart Routing
41
+ - **Declarative Redirects**: Handle complex, logic-based redirects at runtime.
42
+ - **Runtime vs Config**:
43
+ - Use **`launch.json`** ([Static Redirects](https://www.contentstack.com/docs/developers/launch/edge-url-redirects)) for high-performance, simple path-to-path mapping.
44
+ - Use **`redirectIfMatch`** (this library) for dynamic redirects that require logic, such as checking cookies, headers, or geo-location before redirecting.
43
45
 
44
46
  ---
45
47
 
46
- ## πŸ› οΈ Usage Example
48
+ ## πŸ› οΈ Detailed Usage Example
47
49
 
48
- Once initialized, your `functions/[proxy].edge.js` will look like a powerful middleware chain:
50
+ Your `functions/[proxy].edge.js` acts as a **middleware chain**. You can layer these utilities to create complex edge logic:
49
51
 
50
52
  ```javascript
51
53
  import {
52
- jsonResponse,
53
- passThrough,
54
- redirectIfMatch,
55
- protectWithBasicAuth,
56
- ipAccessControl,
57
- blockAICrawlers,
58
54
  blockDefaultDomains,
55
+ handleNextJS_RSC,
56
+ blockAICrawlers,
57
+ ipAccessControl,
58
+ redirectIfMatch,
59
59
  getGeoHeaders,
60
- handleNextJS_RSC
60
+ passThrough
61
61
  } from "@aryanbansal-launch/edge-utils";
62
62
 
63
63
  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, {
64
+ // 1. πŸ›‘οΈ Force Custom Domain (SEO Best Practice)
65
+ // Blocks access via *.contentstackapps.com
66
+ const domainCheck = blockDefaultDomains(request);
67
+ if (domainCheck) return domainCheck;
68
+
69
+ // 2. βš›οΈ Fix Next.js RSC Header issues
70
+ // Prevents "JSON-only" responses on page refreshes
71
+ const rscCheck = await handleNextJS_RSC(request, {
70
72
  affectedPaths: ["/shop", "/about"]
71
73
  });
72
- if (rscResponse) return rscResponse;
73
-
74
- // 2. πŸ›‘οΈ Block AI bots immediately
75
- const botResponse = blockAICrawlers(request);
76
- if (botResponse) return botResponse;
74
+ if (rscCheck) return rscCheck;
77
75
 
78
- // 3. 🧱 IP Whitelisting
79
- const ipResponse = ipAccessControl(request, { allow: ["203.0.113.10"] });
80
- if (ipResponse) return ipResponse;
76
+ // 3. πŸ€– Block Aggressive Bots
77
+ const botCheck = blockAICrawlers(request);
78
+ if (botCheck) return botCheck;
81
79
 
82
- // 4. πŸ” Domain-specific Basic Auth (e.g., for staging)
83
- const authResponse = await protectWithBasicAuth(request, {
84
- hostnameIncludes: "staging.myapp.com",
85
- username: "admin",
86
- password: "securepassword123"
87
- });
88
- if (authResponse && authResponse.status === 401) return authResponse;
80
+ // 4. 🧱 Firewall
81
+ const ipCheck = ipAccessControl(request, { allow: ["203.0.113.10"] });
82
+ if (ipCheck) return ipCheck;
89
83
 
90
- // 5. πŸ”€ SEO-friendly Redirects
91
- const redirectResponse = redirectIfMatch(request, {
92
- path: "/legacy-url",
93
- to: "/modern-url",
84
+ // 5. πŸ”€ Logic-Based Redirects
85
+ const redirect = redirectIfMatch(request, {
86
+ path: "/legacy-page",
87
+ to: "/new-page",
94
88
  status: 301
95
89
  });
96
- if (redirectResponse) return redirectResponse;
90
+ if (redirect) return redirect;
97
91
 
98
- // 6. πŸ“ Geo-Location Access
92
+ // 6. πŸ“ Personalization
99
93
  const geo = getGeoHeaders(request);
100
- console.log(`Request from ${geo.city}, ${geo.country}`);
94
+ if (geo.country === "UK") {
95
+ // Custom logic for UK visitors...
96
+ }
101
97
 
102
- // 7. πŸš€ Pass through to origin
98
+ // 7. πŸš€ Pass through to Origin
103
99
  return passThrough(request);
104
100
  }
105
101
  ```
106
102
 
107
103
  ---
108
104
 
109
- ## πŸ“– API Reference
105
+ ## βš™οΈ Configuration CLI
106
+
107
+ Manage your `launch.json` file interactively to handle bulk settings:
110
108
 
111
- ### πŸ›‘οΈ Security
112
- - **`blockAICrawlers(request, bots?)`**: Blocks common AI crawlers.
113
- - **`blockDefaultDomains(request, { domainToBlock? })`**: Blocks access via default Contentstack Launch domains (e.g., `contentstackapps.com`).
114
- - **`ipAccessControl(request, { allow?, deny? })`**: Simple IP-based firewall.
109
+ ```bash
110
+ npx launch-config
111
+ ```
115
112
 
116
- ### πŸ” Authentication
117
- - **`protectWithBasicAuth(request, options)`**: Prompt for credentials based on hostname.
113
+ ### Supported Settings:
114
+ - **Bulk Redirects**: Add multiple sources and destinations easily.
115
+ - **Rewrites**: Map internal paths to external APIs or micro-services.
116
+ - **Cache Priming**: Add a comma-separated list of URLs to warm up the CDN.
118
117
 
119
- ### πŸ”€ Redirection
120
- - **`redirectIfMatch(request, options)`**: Perform SEO-friendly redirects at the edge.
118
+ ---
121
119
 
122
- ### πŸ“ Geo Location
123
- - **`getGeoHeaders(request)`**: Returns an object with `country`, `region`, `city`, `latitude`, `longitude`.
120
+ ## 🌐 Platform Support
124
121
 
125
- ### βš›οΈ Next.js
126
- - **`handleNextJS_RSC(request, { affectedPaths })`**: Resolves RSC header issues on Contentstack Launch.
122
+ 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.
127
123
 
128
124
  ---
129
125
 
130
- ## 🌐 Platform Support
126
+ ## πŸ“„ License
131
127
 
132
- This library is exclusively optimized for **[Contentstack Launch](https://www.contentstack.com/docs/developers/launch)**.
128
+ Distributed under the MIT License. See `LICENSE` for more information.