@flash-tunnel/adapter-cloudflare 0.1.0 → 0.2.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 +23 -9
  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];
@@ -282,11 +282,22 @@ function createCloudflareDns(options) {
282
282
 
283
283
  const ADAPTER_ID = "cloudflare";
284
284
  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}"`;
285
+ function assertTunnelTarget(localUrl) {
286
+ let parsed;
287
+ try {
288
+ parsed = new URL(localUrl);
289
+ } catch {
290
+ throw new AdapterError(`Invalid tunnel target "${localUrl}"`, {
291
+ code: "INVALID_LOCAL_URL",
292
+ hint: "Pass a full http(s) URL such as http://127.0.0.1:5173"
293
+ });
294
+ }
295
+ if (parsed.pathname !== "/" || parsed.search) {
296
+ throw new AdapterError(`Tunnel target must be an origin without a path, got "${localUrl}"`, {
297
+ code: "INVALID_LOCAL_URL",
298
+ 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"
299
+ });
288
300
  }
289
- return path;
290
301
  }
291
302
  function createCloudflareTunnel(options = {}) {
292
303
  const cloudflaredPath = options.cloudflaredPath ?? "cloudflared";
@@ -319,9 +330,10 @@ function createCloudflareTunnel(options = {}) {
319
330
  return api;
320
331
  }
321
332
  function ensureCloudflared() {
322
- const check = spawnSync(shellCommand(cloudflaredPath), ["--version"], {
333
+ const invocation = shellInvocation(cloudflaredPath, ["--version"]);
334
+ const check = spawnSync(invocation.command, invocation.args, {
323
335
  encoding: "utf8",
324
- shell: process.platform === "win32",
336
+ shell: invocation.shell,
325
337
  timeout: 1e4,
326
338
  windowsHide: true
327
339
  });
@@ -398,6 +410,7 @@ function createCloudflareTunnel(options = {}) {
398
410
  },
399
411
  async provision(_ctx, spec) {
400
412
  reset();
413
+ assertTunnelTarget(spec.localUrl);
401
414
  if (plannedMode === "quick") {
402
415
  return {
403
416
  provider: ADAPTER_ID,
@@ -467,8 +480,9 @@ function createCloudflareTunnel(options = {}) {
467
480
  });
468
481
  }
469
482
  const args = named ? ["--no-autoupdate", "tunnel", "run"] : ["--no-autoupdate", "tunnel", "--url", localUrl];
470
- const child = spawn(shellCommand(cloudflaredPath), args, {
471
- shell: process.platform === "win32",
483
+ const invocation = shellInvocation(cloudflaredPath, args);
484
+ const child = spawn(invocation.command, invocation.args, {
485
+ shell: invocation.shell,
472
486
  stdio: ["ignore", "pipe", "pipe"],
473
487
  windowsHide: true,
474
488
  detached: process.platform !== "win32",
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.2.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.2.0",
34
+ "@flash-tunnel/utils": "0.2.0"
35
35
  },
36
36
  "scripts": {
37
37
  "stub": "unbuild --stub",