outbound-sdk 0.7.0 → 0.7.1

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.
@@ -996,6 +996,7 @@ var serveStdio = async (server, options = {}) => {
996
996
  import { execFile } from "node:child_process";
997
997
  import path2 from "node:path";
998
998
  import process2 from "node:process";
999
+ import { setTimeout as setTimeoutPromise } from "node:timers/promises";
999
1000
  import { fileURLToPath } from "node:url";
1000
1001
  import { promisify } from "node:util";
1001
1002
 
@@ -1021,6 +1022,28 @@ var STANDALONE = /* @__PURE__ */ new Map([
1021
1022
  ["version", ["version"]]
1022
1023
  ]);
1023
1024
  var execFileAsync = promisify(execFile);
1025
+ var NpmFailedError = class extends Error {
1026
+ code;
1027
+ constructor(message, code, cause) {
1028
+ super(message, { cause });
1029
+ this.name = "NpmFailedError";
1030
+ this.code = code;
1031
+ }
1032
+ };
1033
+ var NPM_LINE = /^npm (?:error|ERR!) ?(?<said>.*)$/u;
1034
+ var NPM_CODE = /^code (?<code>E[A-Z0-9]+)$/u;
1035
+ var NPM_DETAIL = /^(?:code|syscall|path|errno|signal|cmd|cwd|command) /u;
1036
+ var npmReason = (stderr) => {
1037
+ const lines = stderr.split("\n").map((line) => NPM_LINE.exec(line.trim())?.groups?.said?.trim() ?? "").filter((line) => line !== "" && !line.startsWith("A complete log"));
1038
+ const code = lines.map((line) => NPM_CODE.exec(line)?.groups?.code).find((found) => found !== void 0);
1039
+ const tags = /* @__PURE__ */ new Set(["notarget", code?.toLowerCase()]);
1040
+ const untag = (line) => {
1041
+ const [first = "", ...rest] = line.split(" ");
1042
+ return /^\d{3}$/u.test(first) || tags.has(first) ? rest.join(" ").trim() : line;
1043
+ };
1044
+ const said = lines.filter((line) => !NPM_DETAIL.test(line)).map(untag).find((line) => line !== "") ?? stderr.trim().split("\n").at(-1) ?? "";
1045
+ return { code, said };
1046
+ };
1024
1047
  var runNpm = async (args) => {
1025
1048
  try {
1026
1049
  const { stdout } = await execFileAsync("npm", [...args], {
@@ -1029,13 +1052,44 @@ var runNpm = async (args) => {
1029
1052
  return stdout;
1030
1053
  } catch (error) {
1031
1054
  const stderr = error instanceof Error && "stderr" in error ? String(error.stderr) : "";
1032
- const said = stderr.trim().split("\n").at(-1) ?? "";
1033
- throw new Error(
1034
- `npm ${args.join(" ")} failed${said === "" ? "" : `: ${said}`}`,
1035
- { cause: error }
1055
+ const { code, said } = npmReason(stderr);
1056
+ const label = code === void 0 ? "" : ` (${code})`;
1057
+ throw new NpmFailedError(
1058
+ `npm ${args.join(" ")} failed${label}${said === "" ? "" : `: ${said}`}`,
1059
+ code,
1060
+ error
1036
1061
  );
1037
1062
  }
1038
1063
  };
1064
+ var sleep = async (ms) => {
1065
+ await setTimeoutPromise(ms);
1066
+ };
1067
+ var NOT_YET_SERVED = /* @__PURE__ */ new Set(["E404", "ETARGET"]);
1068
+ var RETRY_EVERY_MS = 3e4;
1069
+ var RETRY_FOR_MS = 15 * 6e4;
1070
+ var installGlobal = async (npm, say, wait, version) => {
1071
+ const target = `${PACKAGE}@${version}`;
1072
+ const tries = RETRY_FOR_MS / RETRY_EVERY_MS;
1073
+ for (let attempt = 1; ; attempt += 1) {
1074
+ try {
1075
+ await npm(
1076
+ attempt === 1 ? ["install", "--global", target] : ["install", "--global", "--prefer-online", target]
1077
+ );
1078
+ return;
1079
+ } catch (error) {
1080
+ const code = error instanceof NpmFailedError ? error.code : void 0;
1081
+ if (code === void 0 || !NOT_YET_SERVED.has(code) || attempt > tries) {
1082
+ throw error;
1083
+ }
1084
+ if (attempt === 1) {
1085
+ say(
1086
+ `npm lists ${version} but is not serving it yet, which happens for a few minutes after a release. Trying again every ${RETRY_EVERY_MS / 1e3} seconds for up to ${RETRY_FOR_MS / 6e4} minutes; Ctrl-C stops.`
1087
+ );
1088
+ }
1089
+ await wait(RETRY_EVERY_MS);
1090
+ }
1091
+ }
1092
+ };
1039
1093
  var packageDir = () => path2.dirname(fileURLToPath(new URL("../../package.json", import.meta.url)));
1040
1094
  var parts = (version) => version.split("-")[0]?.split(".").map((part) => Math.trunc(Number(part)) || 0) ?? [];
1041
1095
  var older = (a, b) => {
@@ -1056,7 +1110,7 @@ var installKind = async (npm, dir) => {
1056
1110
  const globalRoot = printed.trim();
1057
1111
  return globalRoot !== "" && path2.dirname(dir) === path2.resolve(globalRoot) ? "global" : "project";
1058
1112
  };
1059
- var update = async (npm, say) => {
1113
+ var update = async (npm, say, wait = sleep) => {
1060
1114
  const installed = packageVersion();
1061
1115
  const viewed = await npm(["view", PACKAGE, "version"]);
1062
1116
  const latest = viewed.trim();
@@ -1074,17 +1128,17 @@ var update = async (npm, say) => {
1074
1128
  say(`Run "npm install ${PACKAGE}@latest" in this project to update it.`);
1075
1129
  return;
1076
1130
  }
1077
- say(`Running "npm install --global ${PACKAGE}@latest"...`);
1078
- await npm(["install", "--global", `${PACKAGE}@latest`]);
1131
+ say(`Running "npm install --global ${PACKAGE}@${latest}"...`);
1132
+ await installGlobal(npm, say, wait, latest);
1079
1133
  say(`Updated ${PACKAGE} to ${latest}.`);
1080
1134
  };
1081
- var runSelf = async (name, flags, npm, say) => {
1135
+ var runSelf = async (name, flags, npm, say, wait = sleep) => {
1082
1136
  if (name === "version" || name === void 0 && flags.version === true) {
1083
1137
  say(packageVersion());
1084
1138
  return true;
1085
1139
  }
1086
1140
  if (name === "update" && flags.help !== true) {
1087
- await update(npm, say);
1141
+ await update(npm, say, wait);
1088
1142
  return true;
1089
1143
  }
1090
1144
  return false;
@@ -1160,7 +1214,7 @@ var run = async (argv, io) => {
1160
1214
  try {
1161
1215
  const { help, name, rest, values } = parse(argv);
1162
1216
  named = name;
1163
- if (await runSelf(name, values, io.npm ?? runNpm, io.stdout)) {
1217
+ if (await runSelf(name, values, io.npm ?? runNpm, io.stdout, io.sleep)) {
1164
1218
  return 0;
1165
1219
  }
1166
1220
  if (help !== void 0) {
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  run
3
- } from "./chunk-ZJCVLBAE.js";
3
+ } from "./chunk-6NSCPTOY.js";
4
4
 
5
5
  // src/cli/bin.ts
6
6
  import process from "node:process";
package/dist/cli/bin.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
- import "../chunks/chunk-Y7KECJAV.js";
3
- import "../chunks/chunk-ZJCVLBAE.js";
2
+ import "../chunks/chunk-T5MJMW3R.js";
3
+ import "../chunks/chunk-6NSCPTOY.js";
4
4
  import "../chunks/chunk-GL7BABF2.js";
5
5
  import "../chunks/chunk-JRL3QRBS.js";
6
6
  import "../chunks/chunk-4AVYRVN5.js";
@@ -18,6 +18,8 @@ export interface CliIo {
18
18
  readonly fetch?: OutboundOptions["fetch"];
19
19
  /** Runs npm for `outbound update` and resolves to its output. Meant for tests. */
20
20
  readonly npm?: ((args: readonly string[]) => Promise<string>) | undefined;
21
+ /** Waits between `outbound update` install retries. Meant for tests. */
22
+ readonly sleep?: ((ms: number) => Promise<void>) | undefined;
21
23
  }
22
24
  /** Runs the CLI. Returns the process exit code. */
23
25
  export declare const run: (argv: readonly string[], io: CliIo) => Promise<number>;
package/dist/cli/index.js CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  defineConfig,
5
5
  run,
6
6
  serveStdio
7
- } from "../chunks/chunk-ZJCVLBAE.js";
7
+ } from "../chunks/chunk-6NSCPTOY.js";
8
8
  import "../chunks/chunk-GL7BABF2.js";
9
9
  import "../chunks/chunk-JRL3QRBS.js";
10
10
  import "../chunks/chunk-4AVYRVN5.js";
@@ -1,9 +1,31 @@
1
1
  /** Runs npm with these arguments and resolves to what it printed. */
2
2
  export type NpmRunner = (args: readonly string[]) => Promise<string>;
3
3
  export declare const STANDALONE: ReadonlyMap<string, readonly string[]>;
4
+ /** npm failed. `code` is npm's error code, such as `E404`, when it printed one. */
5
+ export declare class NpmFailedError extends Error {
6
+ readonly code: string | undefined;
7
+ constructor(message: string, code: string | undefined, cause: unknown);
8
+ }
9
+ /** What npm said when it failed. */
10
+ interface NpmReason {
11
+ /** npm's error code, such as `E404`. */
12
+ readonly code: string | undefined;
13
+ /** The first line that says what went wrong. */
14
+ readonly said: string;
15
+ }
16
+ /**
17
+ * npm's code and the first line that says what went wrong. npm ends every
18
+ * failure with "A complete log of this run can be found in: ...", so the last
19
+ * line says nothing. npm starts its explanation lines with a tag, such as
20
+ * `404`, `notarget` or the code in lower case, which is dropped.
21
+ */
22
+ export declare const npmReason: (stderr: string) => NpmReason;
4
23
  export declare const runNpm: NpmRunner;
24
+ /** Waits this many milliseconds. */
25
+ export type Sleep = (ms: number) => Promise<void>;
26
+ export declare const sleep: Sleep;
5
27
  /** Checks npm for a newer version and updates a global install. */
6
- export declare const update: (npm: NpmRunner, say: (text: string) => void) => Promise<void>;
28
+ export declare const update: (npm: NpmRunner, say: (text: string) => void, wait?: Sleep) => Promise<void>;
7
29
  /**
8
30
  * Runs `version` (also a bare `--version`) or `update`, and says whether it
9
31
  * did. Version comes before help, which is what no command at all prints;
@@ -12,4 +34,5 @@ export declare const update: (npm: NpmRunner, say: (text: string) => void) => Pr
12
34
  export declare const runSelf: (name: string | undefined, flags: {
13
35
  readonly help?: boolean;
14
36
  readonly version?: boolean;
15
- }, npm: NpmRunner, say: (text: string) => void) => Promise<boolean>;
37
+ }, npm: NpmRunner, say: (text: string) => void, wait?: Sleep) => Promise<boolean>;
38
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "outbound-sdk",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "One small, typed, read-only API over outbound sending tools.",
5
5
  "keywords": [
6
6
  "cold-email",