@flash-tunnel/adapter-cloudflare 0.1.0 → 0.3.0

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.
Files changed (2) hide show
  1. package/dist/index.mjs +45 -14
  2. package/package.json +3 -3
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { AdapterError } from '@flash-tunnel/types';
2
2
  import { spawnSync, spawn } from 'node:child_process';
3
3
  import process from 'node:process';
4
- import { sleep, stripAnsi } from '@flash-tunnel/utils';
4
+ import { sleep, shellInvocation, stripAnsi } from '@flash-tunnel/utils';
5
5
 
6
6
  function mapError(status, envelope) {
7
7
  const first = envelope.errors?.[0];
@@ -90,7 +90,9 @@ class CloudflareApi {
90
90
  throw new AdapterError("Cannot determine the Cloudflare account id", {
91
91
  code: "ACCOUNT_NOT_FOUND",
92
92
  hint: "Set CLOUDFLARE_ACCOUNT_ID or providers.cloudflare.accountId",
93
- details: { accounts: accounts.map((account) => account.name) }
93
+ details: {
94
+ accounts: accounts.map((account) => account.name)
95
+ }
94
96
  });
95
97
  }
96
98
  async listZones(name) {
@@ -227,7 +229,9 @@ function createCloudflareDns(options) {
227
229
  throw new AdapterError(`DNS record "${target.name}" already exists`, {
228
230
  code: "RECORD_CONFLICT",
229
231
  hint: "Use a different subdomain, or pass --overwrite to replace a non-flash-tunnel record",
230
- details: { records: conflicts.map((record) => record.id) }
232
+ details: {
233
+ records: conflicts.map((record) => record.id)
234
+ }
231
235
  });
232
236
  }
233
237
  },
@@ -282,11 +286,22 @@ function createCloudflareDns(options) {
282
286
 
283
287
  const ADAPTER_ID = "cloudflare";
284
288
  const QUICK_URL_PATTERN = /https:\/\/[a-z0-9-]+\.trycloudflare\.com/;
285
- function shellCommand(path) {
286
- if (process.platform === "win32" && /\s/.test(path) && !path.startsWith('"')) {
287
- return `"${path}"`;
289
+ function assertTunnelTarget(localUrl) {
290
+ let parsed;
291
+ try {
292
+ parsed = new URL(localUrl);
293
+ } catch {
294
+ throw new AdapterError(`Invalid tunnel target "${localUrl}"`, {
295
+ code: "INVALID_LOCAL_URL",
296
+ hint: "Pass a full http(s) URL such as http://127.0.0.1:5173"
297
+ });
298
+ }
299
+ if (parsed.pathname !== "/" || parsed.search) {
300
+ throw new AdapterError(`Tunnel target must be an origin without a path, got "${localUrl}"`, {
301
+ code: "INVALID_LOCAL_URL",
302
+ hint: "cloudflared keeps the incoming path, so only the origin can be proxied. Use http://host:port and put the path on the public URL instead"
303
+ });
288
304
  }
289
- return path;
290
305
  }
291
306
  function createCloudflareTunnel(options = {}) {
292
307
  const cloudflaredPath = options.cloudflaredPath ?? "cloudflared";
@@ -319,9 +334,10 @@ function createCloudflareTunnel(options = {}) {
319
334
  return api;
320
335
  }
321
336
  function ensureCloudflared() {
322
- const check = spawnSync(shellCommand(cloudflaredPath), ["--version"], {
337
+ const invocation = shellInvocation(cloudflaredPath, ["--version"]);
338
+ const check = spawnSync(invocation.command, invocation.args, {
323
339
  encoding: "utf8",
324
- shell: process.platform === "win32",
340
+ shell: invocation.shell,
325
341
  timeout: 1e4,
326
342
  windowsHide: true
327
343
  });
@@ -398,6 +414,7 @@ function createCloudflareTunnel(options = {}) {
398
414
  },
399
415
  async provision(_ctx, spec) {
400
416
  reset();
417
+ assertTunnelTarget(spec.localUrl);
401
418
  if (plannedMode === "quick") {
402
419
  return {
403
420
  provider: ADAPTER_ID,
@@ -466,9 +483,15 @@ function createCloudflareTunnel(options = {}) {
466
483
  code: "PROCESS_SPAWN_FAILED"
467
484
  });
468
485
  }
469
- const args = named ? ["--no-autoupdate", "tunnel", "run"] : ["--no-autoupdate", "tunnel", "--url", localUrl];
470
- const child = spawn(shellCommand(cloudflaredPath), args, {
471
- shell: process.platform === "win32",
486
+ const args = named ? ["--no-autoupdate", "tunnel", "run"] : [
487
+ "--no-autoupdate",
488
+ "tunnel",
489
+ "--url",
490
+ localUrl
491
+ ];
492
+ const invocation = shellInvocation(cloudflaredPath, args);
493
+ const child = spawn(invocation.command, invocation.args, {
494
+ shell: invocation.shell,
472
495
  stdio: ["ignore", "pipe", "pipe"],
473
496
  windowsHide: true,
474
497
  detached: process.platform !== "win32",
@@ -476,7 +499,10 @@ function createCloudflareTunnel(options = {}) {
476
499
  });
477
500
  childProcess = child;
478
501
  if (child.pid) {
479
- handle.meta = { ...handle.meta, processPid: String(child.pid) };
502
+ handle.meta = {
503
+ ...handle.meta,
504
+ processPid: String(child.pid)
505
+ };
480
506
  }
481
507
  child.stderr?.on("data", (chunk) => capture(chunk, hooks));
482
508
  child.stdout?.on("data", (chunk) => capture(chunk, hooks));
@@ -541,7 +567,12 @@ ${output.slice(-10).join("\n")}` : ""}`,
541
567
  return;
542
568
  }
543
569
  if (process.platform === "win32") {
544
- spawnSync("taskkill", ["/PID", String(child.pid), "/T", "/F"], {
570
+ spawnSync("taskkill", [
571
+ "/PID",
572
+ String(child.pid),
573
+ "/T",
574
+ "/F"
575
+ ], {
545
576
  stdio: "ignore",
546
577
  windowsHide: true
547
578
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@flash-tunnel/adapter-cloudflare",
3
3
  "type": "module",
4
- "version": "0.1.0",
4
+ "version": "0.3.0",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/flash-tunnel/flash-tunnel#readme",
7
7
  "repository": {
@@ -30,8 +30,8 @@
30
30
  "access": "public"
31
31
  },
32
32
  "dependencies": {
33
- "@flash-tunnel/types": "0.1.0",
34
- "@flash-tunnel/utils": "0.1.0"
33
+ "@flash-tunnel/types": "0.3.0",
34
+ "@flash-tunnel/utils": "0.3.0"
35
35
  },
36
36
  "scripts": {
37
37
  "stub": "unbuild --stub",