{"record":{"id":"c0cd18473b2098c3","repo":"santifer/career-ops","slug":"h1b-api-base-is-not-a-valid-url-trimmed","errorCode":null,"errorMessage":"H1B_API_BASE is not a valid URL: ${trimmed}","messagePattern":"H1B_API_BASE is not a valid URL: (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"plugins/h1b-sponsor/lib/api.mjs","lineNumber":50,"sourceCode":"}\n\nfunction resolveBase() {\n  const raw = process.env.H1B_API_BASE;\n  // Absent means \"use the default\". Present but blank is a misconfiguration\n  // (an unset shell variable, an empty .env line, a CI secret that did not\n  // populate), and silently falling back would send someone's shortlist and\n  // their token to a host they believed they had replaced.\n  if (raw === undefined) return DEFAULT_BASE;\n  const trimmed = String(raw).trim();\n  if (!trimmed) {\n    throw new Error('H1B_API_BASE is set but empty. Unset it to use the default endpoint.');\n  }\n\n  let parsed;\n  try {\n    parsed = new URL(trimmed);\n  } catch {\n    throw new Error(`H1B_API_BASE is not a valid URL: ${trimmed}`);\n  }\n  if (parsed.username || parsed.password) {\n    // Undici refuses a credentialed Request anyway, and the value reaches\n    // stdout through the source field, so this would print a password.\n    throw new Error('H1B_API_BASE must not embed credentials.');\n  }\n  if (parsed.search || parsed.hash) {\n    // Paths are appended, so a query or fragment swallows them: the request\n    // would go to the base itself and answer about a company never asked for.\n    throw new Error('H1B_API_BASE must not contain a query string or a fragment.');\n  }\n  // Plain http would put an Authorization header on the wire in the clear.\n  // Loopback is exempt so a self-hoster can develop against a local worker,\n  // but only for http. Exempting every scheme on a loopback host let\n  // ftp://localhost and ws://localhost past validation, and those die later\n  // inside fetch as a bare \"fetch failed\", which is the opaque failure this\n  // check exists to replace with a named configuration error.\n  const loopback = /^(localhost|127\\.\\d+\\.\\d+\\.\\d+|\\[::1\\]|::1)$/i.test(parsed.hostname);","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/santifer/career-ops/blob/aac998c7ed7248ea853b720ceeb1fdbeb322fc5d/plugins/h1b-sponsor/lib/api.mjs#L32-L68","documentation":"resolveBase throws this when H1B_API_BASE is set to a non-empty string that the URL constructor cannot parse. The plugin validates the variable up front so a malformed base fails with a named configuration error instead of a bare 'fetch failed' later.","triggerScenarios":"H1B_API_BASE contains a value like 'my-worker.example.com' (missing scheme), 'https://oops', or any string that fails `new URL(trimmed)`.","commonSituations":"Typing the host without the https:// prefix; stray characters or quotes pasted from documentation; a truncated value from a secrets manager.","solutions":["Prefix the value with its scheme, e.g. H1B_API_BASE=https://my-worker.example.com.","Print the variable (`echo \"$H1B_API_BASE\"`) and check for hidden whitespace, quotes, or truncation.","Re-copy the URL from your worker's deployment output."],"exampleFix":"// before\nH1B_API_BASE=my-worker.example.com\n// after\nH1B_API_BASE=https://my-worker.example.com","handlingStrategy":"validation","validationCode":"const raw = process.env.H1B_API_BASE;\nif (raw !== undefined) { try { new URL(raw.trim()); } catch { throw new Error(`H1B_API_BASE is not a URL: ${raw}`); } }","typeGuard":"function isUsableUrl(s) {\n  if (typeof s !== 'string') return false;\n  try { new URL(s.trim()); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  await lookup(name);\n} catch (e) {\n  if (e.message.includes('not a valid URL')) {\n    console.error('Add the scheme, e.g. H1B_API_BASE=https://your-worker.example.com');\n  }\n}","preventionTips":["Always include the https:// scheme in base-URL env vars.","Quote values in .env/shell to avoid shell mangling.","Run a startup config validation step that parses every URL-shaped variable."],"tags":["env","url","configuration"],"backgroundTag":"invalid-url-format","analyzedSha":"aac998c7ed7248ea853b720ceeb1fdbeb322fc5d","analyzedAt":"2026-09-16T06:35:29.214Z","contentChangedAt":"2026-09-16T06:35:29.214Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}