@mark1russell7/service 0.6.1 → 0.6.2
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.
- package/README.md +26 -3
- package/build/{builder-BNAkpeTT.d.ts → builder-0OlfomVU.d.ts} +2 -2
- package/build/{core-CWsGgr9J.d.ts → core-Iy9rIXOT.d.ts} +3 -3
- package/build/{dial-JEJavY0U.d.ts → dial-DLT7bTlA.d.ts} +2 -2
- package/build/grpc.d.ts +3 -3
- package/build/grpc.js +1 -1
- package/build/http-client.d.ts +3 -3
- package/build/http-client.js +1 -1
- package/build/http.d.ts +17 -3
- package/build/http.js +53 -10
- package/build/http.js.map +1 -1
- package/build/index.d.ts +43 -6
- package/build/index.js +92 -6
- package/build/index.js.map +1 -1
- package/build/{lifecycle-CCDLvXLF.d.ts → lifecycle-DXK880vG.d.ts} +44 -3
- package/build/{limiter-B0849Vhp.d.ts → limiter-BoZkXIxk.d.ts} +2 -2
- package/build/{limiter-pkBZUp-t.js → limiter-CmC4vCjk.js} +21 -13
- package/build/limiter-CmC4vCjk.js.map +1 -0
- package/build/limiter.d.ts +3 -3
- package/build/limiter.js +1 -1
- package/build/{machine-DZMb_QEU.js → machine-Dq2OlzCG.js} +7 -20
- package/build/machine-Dq2OlzCG.js.map +1 -0
- package/build/mongodb.d.ts +3 -3
- package/build/mongodb.js +1 -1
- package/build/pg.d.ts +2 -2
- package/build/pg.js +1 -1
- package/build/{queue-eLs5IruM.d.ts → queue-CvBNNZZJ.d.ts} +2 -2
- package/build/rabbitmq.d.ts +4 -4
- package/build/rabbitmq.js +1 -1
- package/build/redis.d.ts +3 -3
- package/build/redis.js +1 -1
- package/build/retrier-CRda6z9G.js +73 -0
- package/build/retrier-CRda6z9G.js.map +1 -0
- package/build/{store-D5HLgBnW.d.ts → store-Cwuepb-z.d.ts} +14 -3
- package/build/testing.d.ts +4 -2
- package/build/testing.js +12 -1
- package/build/testing.js.map +1 -1
- package/build/websocket.d.ts +3 -3
- package/build/websocket.js +1 -1
- package/package.json +2 -2
- package/build/limiter-pkBZUp-t.js.map +0 -1
- package/build/machine-DZMb_QEU.js.map +0 -1
- package/build/operation-DL5RxFds.js +0 -25
- package/build/operation-DL5RxFds.js.map +0 -1
package/build/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { platformClock, realClock } from "./clock.js";
|
|
2
2
|
import { detectionTime, heartbeat } from "./heartbeat.js";
|
|
3
3
|
import { a as recoverSpan, i as heartbeatDetection, n as asWindow, r as failSpan, t as asDebounce } from "./timing-DGRi3XCI.js";
|
|
4
|
-
import {
|
|
4
|
+
import { i as operationProblems, r as mayReissue, t as retrier } from "./retrier-CRda6z9G.js";
|
|
5
5
|
import { a as limiterProblems, i as limitRule, n as freeAt, o as longestRetryAfter, r as idleSpan, s as retryAfterSeconds, t as decide } from "./policy-CgS4y94U.js";
|
|
6
6
|
import { a as lateHolds, l as takeOutside, n as countOf, r as millisecondsOf, s as ownStream, t as boundedMillisecondsOf } from "./knobs-D2ClI68I.js";
|
|
7
7
|
import { backoffBand, backoffDelay, backoffProblems } from "./backoff.js";
|
|
8
8
|
import { DRAIN_RULES, O04, O09, O10, O16 } from "./rules.js";
|
|
9
|
-
import { a as keyedLimiter, i as limiterStateOf, n as TOKEN_BUCKET_MACHINE, o as memoryStore, r as limiterMachine, t as SLIDING_WINDOW_MACHINE } from "./limiter-
|
|
9
|
+
import { a as keyedLimiter, i as limiterStateOf, n as TOKEN_BUCKET_MACHINE, o as memoryStore, r as limiterMachine, t as SLIDING_WINDOW_MACHINE } from "./limiter-CmC4vCjk.js";
|
|
10
10
|
import { convertExact, convertible, quantity, rationals, valueIn } from "@mark1russell7/quantity";
|
|
11
11
|
import { replay } from "@mark1russell7/algebra/replay";
|
|
12
12
|
//#region src/description.ts
|
|
@@ -620,16 +620,50 @@ const LOG_LEVELS = [
|
|
|
620
620
|
];
|
|
621
621
|
/** This function tells if `level` is at `least` or more severe. */
|
|
622
622
|
const atLeast = (level, least) => LOG_LEVELS.indexOf(level) >= LOG_LEVELS.indexOf(least);
|
|
623
|
+
/** This function gives a value of a field that JSON can write: an error as its name, message and stack, and a bigint as text. */
|
|
624
|
+
const jsonValue = (_key, value) => {
|
|
625
|
+
if (typeof value === "bigint") return value.toString();
|
|
626
|
+
if (value instanceof Error) return {
|
|
627
|
+
name: value.name,
|
|
628
|
+
message: value.message,
|
|
629
|
+
...value.stack === void 0 ? {} : { stack: value.stack }
|
|
630
|
+
};
|
|
631
|
+
return value;
|
|
632
|
+
};
|
|
633
|
+
/**
|
|
634
|
+
* This function gives one line of JSON for an entry. A field named `level` or `message` does not
|
|
635
|
+
* replace the level or the message. A field that JSON cannot write, for example a cycle, gives its failure as text.
|
|
636
|
+
*/
|
|
637
|
+
const jsonLine = (level, message, fields) => {
|
|
638
|
+
const { level: _level, message: _message, ...rest } = fields;
|
|
639
|
+
try {
|
|
640
|
+
return JSON.stringify({
|
|
641
|
+
level,
|
|
642
|
+
message,
|
|
643
|
+
...rest
|
|
644
|
+
}, jsonValue);
|
|
645
|
+
} catch (error) {
|
|
646
|
+
return JSON.stringify({
|
|
647
|
+
level,
|
|
648
|
+
message,
|
|
649
|
+
unwritten: error instanceof Error ? error.message : String(error)
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
};
|
|
623
653
|
/**
|
|
624
654
|
* This function makes the console logger. It writes each entry at its level or more severe to the
|
|
625
|
-
* method of that level
|
|
655
|
+
* method of that level. In the format `text`, it writes the message first and then the fields. In
|
|
656
|
+
* the format `json`, it writes one line of JSON.
|
|
626
657
|
*/
|
|
627
658
|
const consoleLogger = (options = {}) => {
|
|
628
659
|
const least = options.level ?? "info";
|
|
660
|
+
const format = options.format ?? "text";
|
|
661
|
+
if (format !== "text" && format !== "json") throw new Error(`the log format ${JSON.stringify(format)} is not "text" or "json"`);
|
|
629
662
|
return { log(level, message, fields) {
|
|
630
663
|
if (!atLeast(level, least)) return;
|
|
631
664
|
const target = options.console ?? globalThis.console;
|
|
632
|
-
if (
|
|
665
|
+
if (format === "json") target[level](jsonLine(level, message, fields));
|
|
666
|
+
else if (Object.keys(fields).length === 0) target[level](message);
|
|
633
667
|
else target[level](message, fields);
|
|
634
668
|
} };
|
|
635
669
|
};
|
|
@@ -757,6 +791,8 @@ const PROCESS_TRIGGERS = [
|
|
|
757
791
|
"ipc-close",
|
|
758
792
|
"ipc-message"
|
|
759
793
|
];
|
|
794
|
+
/** These are the origins of the errors that the option `uncaught` of `run()` receives. */
|
|
795
|
+
const UNCAUGHT_ORIGINS = ["uncaughtException", "unhandledRejection"];
|
|
760
796
|
/** This function tells if a number is an exit code of a process: a whole number from 0 to 255. */
|
|
761
797
|
const isExitCode = (code) => Number.isInteger(code) && code >= 0 && code <= 255;
|
|
762
798
|
/** This function tells if a trigger is an exit signal. A repeat signal obeys `onRepeatSignal`. */
|
|
@@ -848,6 +884,19 @@ const processPortOf = (find) => {
|
|
|
848
884
|
host.off(trigger, handler);
|
|
849
885
|
};
|
|
850
886
|
},
|
|
887
|
+
onUncaught(handler) {
|
|
888
|
+
const host = hostProcess();
|
|
889
|
+
const offs = UNCAUGHT_ORIGINS.map((origin) => {
|
|
890
|
+
const receive = (error) => handler(error, origin);
|
|
891
|
+
host.on(origin, receive);
|
|
892
|
+
return () => {
|
|
893
|
+
host.off(origin, receive);
|
|
894
|
+
};
|
|
895
|
+
});
|
|
896
|
+
return () => {
|
|
897
|
+
for (const off of offs) off();
|
|
898
|
+
};
|
|
899
|
+
},
|
|
851
900
|
async flush() {
|
|
852
901
|
const host = hostProcess();
|
|
853
902
|
await Promise.all([written(host.stdout), written(host.stderr)]);
|
|
@@ -1516,7 +1565,18 @@ const cooperativeAbortAt = (cap, window, floor = "half") => {
|
|
|
1516
1565
|
const before = window.value === "infinite" ? Number.POSITIVE_INFINITY : valueIn(window, "ms");
|
|
1517
1566
|
return Math.max(cap - before, floor === "none" ? 0 : cap / 2);
|
|
1518
1567
|
};
|
|
1568
|
+
/** This function gives the problems of the option `uncaught`: an exit code that is not from 0 to 255. */
|
|
1569
|
+
const uncaughtProblems = (uncaught) => {
|
|
1570
|
+
const code = uncaught?.exitCode;
|
|
1571
|
+
return code === void 0 || isExitCode(code) ? [] : [`the exit code ${code} of an uncaught error is not a whole number from 0 to 255`];
|
|
1572
|
+
};
|
|
1519
1573
|
const message = (error) => error instanceof Error ? error.message : String(error);
|
|
1574
|
+
/** This function gives the text of an error with its stack, when it has one. */
|
|
1575
|
+
const withStack = (error) => error instanceof Error && typeof error.stack === "string" && error.stack.length > 0 ? error.stack : message(error);
|
|
1576
|
+
const UNCAUGHT_NAMES = {
|
|
1577
|
+
uncaughtException: "an uncaught exception",
|
|
1578
|
+
unhandledRejection: "an unhandled rejection"
|
|
1579
|
+
};
|
|
1520
1580
|
/**
|
|
1521
1581
|
* This function gives the problems of a declaration that the lifecycle cannot start: a stage with
|
|
1522
1582
|
* work and no declared phase. The runtime has no defaults, thus a transport without a drain phase
|
|
@@ -1631,6 +1691,31 @@ const lifecycle = (description, runtime, components, options = {}, bind = () =>
|
|
|
1631
1691
|
if (found.as === void 0) triggered("ipc-message");
|
|
1632
1692
|
else triggered(found.as, `${found.as} (a message of the IPC channel)`);
|
|
1633
1693
|
};
|
|
1694
|
+
const uncaught = options.uncaught;
|
|
1695
|
+
const uncaughtCode = uncaught?.exitCode !== void 0 && isExitCode(uncaught.exitCode) ? uncaught.exitCode : 1;
|
|
1696
|
+
/**
|
|
1697
|
+
* This function receives an error that no code caught. A skipped error goes to the logger at
|
|
1698
|
+
* `warn`. Each other error is an error of the lifecycle, and it starts the shutdown with its code.
|
|
1699
|
+
*/
|
|
1700
|
+
const caught = (error, origin) => {
|
|
1701
|
+
if (exitState === "exited") return;
|
|
1702
|
+
const name = UNCAUGHT_NAMES[origin];
|
|
1703
|
+
let skip = false;
|
|
1704
|
+
try {
|
|
1705
|
+
skip = uncaught?.ignore?.(error, origin) === true;
|
|
1706
|
+
} catch {}
|
|
1707
|
+
if (skip) {
|
|
1708
|
+
try {
|
|
1709
|
+
options.logger?.log("warn", `${name} was skipped: ${message(error)}`, {
|
|
1710
|
+
origin,
|
|
1711
|
+
error: withStack(error)
|
|
1712
|
+
});
|
|
1713
|
+
} catch {}
|
|
1714
|
+
return;
|
|
1715
|
+
}
|
|
1716
|
+
fail(`${name}: ${withStack(error)}`);
|
|
1717
|
+
shutdown(`${name}: ${message(error)}`, { code: uncaughtCode });
|
|
1718
|
+
};
|
|
1634
1719
|
/**
|
|
1635
1720
|
* This function does the work of one phase, until its cap. It tells if the work ended in time. When
|
|
1636
1721
|
* work is left at the cap, `excuse` gives the cause of a cap that is not a failure, or null.
|
|
@@ -1802,6 +1887,7 @@ const lifecycle = (description, runtime, components, options = {}, bind = () =>
|
|
|
1802
1887
|
bind(life);
|
|
1803
1888
|
release = port.hold?.() ?? (() => {});
|
|
1804
1889
|
offs = [...EXIT_SIGNALS, ...description.shutdown.triggers ?? []].map((trigger) => port.on(trigger, trigger === "ipc-message" ? received : () => triggered(trigger)));
|
|
1890
|
+
if (uncaught !== void 0 && port.onUncaught !== void 0) offs.push(port.onUncaught(caught));
|
|
1805
1891
|
(async () => {
|
|
1806
1892
|
const started = (what) => record("gate", "starting", "starting", "component.started", what);
|
|
1807
1893
|
try {
|
|
@@ -2358,7 +2444,7 @@ const service = (name) => {
|
|
|
2358
2444
|
choices,
|
|
2359
2445
|
stopPollers: () => {}
|
|
2360
2446
|
};
|
|
2361
|
-
const problems = lifecycleProblems(description, components);
|
|
2447
|
+
const problems = [...lifecycleProblems(description, components), ...uncaughtProblems(options.uncaught)];
|
|
2362
2448
|
if (problems.length > 0) throw new Error(`${name}: ${problems.join("; ")}`);
|
|
2363
2449
|
const made = makeRuntime(options);
|
|
2364
2450
|
started = true;
|
|
@@ -4163,6 +4249,6 @@ const MACHINES = {
|
|
|
4163
4249
|
watchdog: WATCHDOG_MACHINE
|
|
4164
4250
|
};
|
|
4165
4251
|
//#endregion
|
|
4166
|
-
export { BOUND_RULES, CLOSE_BOUND_RULE, DECLARATION_RULE, DRAIN_RULES, DRAIN_RULE_IDS, DURATION_FRACTIONS, EPISODES_KEPT, EXIT_MACHINE, EXIT_SIGNALS, FORMAT, GATE_MACHINE, LATCH_MACHINE, LAW_STATISTIC, LIMITER_RULES, LOG_LEVELS, MACHINES, NAME_RULE, O04, O09, O10, O16, PLATFORM_MARKS, POLLER_MACHINE, PROCESS_TRIGGERS, PROGRESS_RULE, SERVICE_RULES, SLIDING_WINDOW_MACHINE, STAGES, STOPS, STOP_RULE, TOKEN_BUCKET_MACHINE, UNIT_BOUND_RULE, WATCHDOG_MACHINE, WINDOW_MACHINE, asDebounce, asWindow, atLeast, backoffBand, backoffDelay, backoffProblems, capKnob, compress, compression, compressionOf, consoleLogger, cooperativeAbortAt, daemon, decide, detectionTime, drainMachine, embeddedProcess, ephemeral, evaluate, failSpan, freeAt, heartbeat, heartbeatDetection, idleSpan, inequalities, isExitCode, isExitSignal, isMessageShape, keyedLimiter, kubernetes, limitRule, limiterMachine, limiterProblems, limiterStateOf, lint, lintStop, localDev, logEvents, longestRetryAfter, matchesMessage, mayReissue, memoryStore, nodeProcess, platformClock, processPortOf, queueDrain, realClock, recoverSpan, redact, redeliverOnce, replay, retryAfterSeconds, service, serviceBindings, stageRank, symbolsOf, verdictOf, watchdog, workerLoop };
|
|
4252
|
+
export { BOUND_RULES, CLOSE_BOUND_RULE, DECLARATION_RULE, DRAIN_RULES, DRAIN_RULE_IDS, DURATION_FRACTIONS, EPISODES_KEPT, EXIT_MACHINE, EXIT_SIGNALS, FORMAT, GATE_MACHINE, LATCH_MACHINE, LAW_STATISTIC, LIMITER_RULES, LOG_LEVELS, MACHINES, NAME_RULE, O04, O09, O10, O16, PLATFORM_MARKS, POLLER_MACHINE, PROCESS_TRIGGERS, PROGRESS_RULE, SERVICE_RULES, SLIDING_WINDOW_MACHINE, STAGES, STOPS, STOP_RULE, TOKEN_BUCKET_MACHINE, UNCAUGHT_ORIGINS, UNIT_BOUND_RULE, WATCHDOG_MACHINE, WINDOW_MACHINE, asDebounce, asWindow, atLeast, backoffBand, backoffDelay, backoffProblems, capKnob, compress, compression, compressionOf, consoleLogger, cooperativeAbortAt, daemon, decide, detectionTime, drainMachine, embeddedProcess, ephemeral, evaluate, failSpan, freeAt, heartbeat, heartbeatDetection, idleSpan, inequalities, isExitCode, isExitSignal, isMessageShape, keyedLimiter, kubernetes, limitRule, limiterMachine, limiterProblems, limiterStateOf, lint, lintStop, localDev, logEvents, longestRetryAfter, matchesMessage, mayReissue, memoryStore, nodeProcess, platformClock, processPortOf, queueDrain, realClock, recoverSpan, redact, redeliverOnce, replay, retrier, retryAfterSeconds, service, serviceBindings, stageRank, symbolsOf, verdictOf, watchdog, workerLoop };
|
|
4167
4253
|
|
|
4168
4254
|
//# sourceMappingURL=index.js.map
|