@aryanbansal-launch/edge-utils 0.1.6 → 0.1.7

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.
@@ -0,0 +1,123 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fs from 'fs';
4
+ import path from 'path';
5
+ import readline from 'readline';
6
+
7
+ const colors = {
8
+ reset: '\x1b[0m',
9
+ bright: '\x1b[1m',
10
+ green: '\x1b[32m',
11
+ yellow: '\x1b[33m',
12
+ cyan: '\x1b[36m',
13
+ blue: '\x1b[34m',
14
+ red: '\x1b[31m'
15
+ };
16
+
17
+ const rl = readline.createInterface({
18
+ input: process.stdin,
19
+ output: process.stdout
20
+ });
21
+
22
+ const question = (query) =>
23
+ new Promise((resolve) => rl.question(query, resolve));
24
+
25
+ async function run() {
26
+ console.log(`\n${colors.bright}${colors.cyan}🚀 Launch Configuration Generator${colors.reset}\n`);
27
+
28
+ const packageJsonPath = path.join(process.cwd(), 'package.json');
29
+ if (!fs.existsSync(packageJsonPath)) {
30
+ console.log(`${colors.red}❌ Error: Root directory not detected.${colors.reset}`);
31
+ process.exit(1);
32
+ }
33
+
34
+ const launchJsonPath = path.join(process.cwd(), 'launch.json');
35
+ let config = {
36
+ redirects: [],
37
+ rewrites: [],
38
+ cache: {
39
+ cachePriming: {
40
+ urls: []
41
+ }
42
+ }
43
+ };
44
+
45
+ if (fs.existsSync(launchJsonPath)) {
46
+ console.log(`${colors.yellow}â„šī¸ Existing launch.json found. We will merge your changes.${colors.reset}\n`);
47
+ try {
48
+ const existingConfig = JSON.parse(fs.readFileSync(launchJsonPath, 'utf-8'));
49
+
50
+ // Preserve everything, but ensure our managed fields are initialized as arrays/objects
51
+ config = {
52
+ ...existingConfig,
53
+ redirects: existingConfig.redirects || [],
54
+ rewrites: existingConfig.rewrites || [],
55
+ cache: {
56
+ ...(existingConfig.cache || {}),
57
+ cachePriming: {
58
+ ...(existingConfig.cache?.cachePriming || {}),
59
+ urls: existingConfig.cache?.cachePriming?.urls || []
60
+ }
61
+ }
62
+ };
63
+ } catch (e) {
64
+ console.log(`${colors.red}âš ī¸ Error parsing existing launch.json. Starting with a fresh config.${colors.reset}\n`);
65
+ }
66
+ }
67
+
68
+ // 1. Redirects
69
+ while (true) {
70
+ const addRedirect = await question(`Do you want to add a Redirect?${config.redirects.length > 0 ? ' another?' : ''} (y/n): `);
71
+ if (addRedirect.toLowerCase() !== 'y') break;
72
+
73
+ const source = await question(' Source path (e.g., /source): ');
74
+ const destination = await question(' Destination path (e.g., /destination): ');
75
+ const code = await question(' Status code (default 308): ');
76
+ config.redirects.push({
77
+ source,
78
+ destination,
79
+ statusCode: parseInt(code) || 308
80
+ });
81
+ console.log(`${colors.green} ✔ Redirect added.${colors.reset}`);
82
+ }
83
+
84
+ // 2. Rewrites
85
+ while (true) {
86
+ const addRewrite = await question(`Do you want to add a Rewrite?${config.rewrites.length > 0 ? ' another?' : ''} (y/n): `);
87
+ if (addRewrite.toLowerCase() !== 'y') break;
88
+
89
+ const source = await question(' Source path (e.g., /api/*): ');
90
+ const destination = await question(' Destination URL: ');
91
+ config.rewrites.push({ source, destination });
92
+ console.log(`${colors.green} ✔ Rewrite added.${colors.reset}`);
93
+ }
94
+
95
+ // 3. Cache Priming
96
+ const addPrime = await question('Do you want to add Cache Priming URLs? (y/n): ');
97
+ if (addPrime.toLowerCase() === 'y') {
98
+ console.log(`${colors.cyan}Note: Only relative paths are supported. No Regex/Wildcards.${colors.reset}`);
99
+ const urls = await question('Enter URLs separated by commas (e.g., /home,/about,/shop): ');
100
+ const urlList = urls.split(',').map(u => u.trim()).filter(u => u);
101
+ config.cache.cachePriming.urls = [...new Set([...(config.cache.cachePriming.urls || []), ...urlList])];
102
+ }
103
+
104
+ // Clean up empty fields before writing
105
+ if (config.redirects && config.redirects.length === 0) delete config.redirects;
106
+ if (config.rewrites && config.rewrites.length === 0) delete config.rewrites;
107
+
108
+ if (config.cache) {
109
+ if (config.cache.cachePriming && config.cache.cachePriming.urls && config.cache.cachePriming.urls.length === 0) {
110
+ delete config.cache.cachePriming;
111
+ }
112
+ if (Object.keys(config.cache).length === 0) {
113
+ delete config.cache;
114
+ }
115
+ }
116
+
117
+ fs.writeFileSync(launchJsonPath, JSON.stringify(config, null, 2));
118
+ console.log(`\n${colors.green}✅ Successfully updated launch.json!${colors.reset}\n`);
119
+
120
+ rl.close();
121
+ }
122
+
123
+ run();
package/dist/index.js CHANGED
@@ -6,4 +6,5 @@ export * from "./security/ip-access.js";
6
6
  export * from "./security/block-bots.js";
7
7
  export * from "./security/block-default-domains.js";
8
8
  export * from "./geo/geo-headers.js";
9
+ export * from "./launch/config.js";
9
10
  export * from "./nextjs/rsc.js";
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Generates a standard launch.json configuration object.
3
+ */
4
+ export function generateLaunchConfig(options) {
5
+ return {
6
+ redirects: options.redirects || [],
7
+ rewrites: options.rewrites || [],
8
+ cache: {
9
+ cachePriming: {
10
+ urls: options.cache?.cachePriming?.urls || []
11
+ }
12
+ }
13
+ };
14
+ }
@@ -1,7 +1,3 @@
1
- /**
2
- * Blocks access to the site when accessed via default Contentstack Launch domains.
3
- * This is useful for ensuring users only access the site via your custom domain.
4
- */
5
1
  export function blockDefaultDomains(request, options = {}) {
6
2
  const domain = options.domainToBlock || 'contentstackapps.com';
7
3
  const url = new URL(request.url);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aryanbansal-launch/edge-utils",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "repository": {
@@ -13,7 +13,8 @@
13
13
  },
14
14
  "main": "dist/index.js",
15
15
  "bin": {
16
- "launch-init": "./bin/launch-init.js"
16
+ "launch-init": "./bin/launch-init.js",
17
+ "launch-config": "./bin/launch-config.js"
17
18
  },
18
19
  "exports": {
19
20
  ".": "./dist/index.js"
package/readme.md CHANGED
@@ -13,6 +13,7 @@ A lightweight, high-performance toolkit specifically designed for **Contentstack
13
13
  - 🔐 **Edge Auth**: Implement Basic Auth directly at the edge for specific hostnames.
14
14
  - 📍 **Geo-Aware**: Easily extract location data from request headers.
15
15
  - âš›ī¸ **Next.js Ready**: Built-in fixes for RSC header issues on Launch proxies.
16
+ - 🚀 **Cache Priming**: Easily manage cache warming URLs via CLI.
16
17
  - 🔀 **Smart Routing**: Declarative redirects based on path and method.
17
18
  - ⚡ **Zero Dependencies**: Lightweight and optimized for edge runtime limits.
18
19
 
@@ -34,6 +35,12 @@ npx launch-init
34
35
  ```
35
36
  This will automatically create the `functions/` directory and a boilerplate `[proxy].edge.js` handler for you.
36
37
 
38
+ ### 3. Configure (Optional)
39
+ Manage your `launch.json` (Redirects, Rewrites, and Cache Priming) interactively:
40
+ ```bash
41
+ npx launch-config
42
+ ```
43
+
37
44
  ---
38
45
 
39
46
  ## đŸ› ī¸ Usage Example