@art-lib/operations 0.0.0-stage → 0.0.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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.0.1
4
+
5
+ ### Added
package/LICENSE-MIT ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Noodlestan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,110 @@
1
- # Temporary Holding Version
1
+ # @art-lib/operations
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ > Operation outcome types and factories, and an append-only operations log that forwards every operation to the logger.
4
+
5
+ This package is part of the [Art Lib toolkit](../../README.md).
6
+
7
+ ## Design
8
+
9
+ The package publishes two subpath modules. **`/model`** is the pure operation model: one unit of work in a command invocation with a `pending`, `success`, or `failure` outcome, plus the factories that move a pending operation to a finished one. **`/log`** is the append-only operations log: created with the logger, it forwards every operation to it and keeps the finished ones queryable.
10
+
11
+ Operations conform to the `Message` interface of [`@art-lib/logger`](../logger/README.md) structurally — the level is derived from the outcome (`pending` → `debug`, `success` → `info`, `failure` → `error`) — so the logger never learns about operations. The logger is this package's only dependency.
12
+
13
+ See also:
14
+
15
+ - [package knowledge](knowledge/index.md) — overview, design, and decision records
16
+ - [repository design record](../../knowledge/design/operations.md) — what a CLI needs from the package
17
+
18
+ ## Usage
19
+
20
+ ### Install
21
+
22
+ ```bash
23
+ npm install @art-lib/operations
24
+ ```
25
+
26
+ `@art-lib/logger` arrives as a dependency; import it wherever you create the logger.
27
+
28
+ ### Configure
29
+
30
+ Create one operations log per command invocation, right after the logger. Every operation logged through it is forwarded to the logger, so resolve the output mode first:
31
+
32
+ ```ts
33
+ import { createLogger } from '@art-lib/logger';
34
+ import { createOperationsLog } from '@art-lib/operations/log';
35
+
36
+ const logger = createLogger();
37
+
38
+ // Buffered messages flush as soon as the mode is resolved.
39
+ logger.setOutputMode(process.env.VERBOSE ? 'verbose' : 'normal');
40
+
41
+ // One log per command invocation; discard it when the invocation ends.
42
+ export const operations = createOperationsLog(logger);
43
+ ```
44
+
45
+ The package root re-exports both subpaths, so `import { ... } from '@art-lib/operations'` works as well.
46
+
47
+ ### Example
48
+
49
+ ```ts
50
+ import {
51
+ createGenericOperation,
52
+ createOperationFailure,
53
+ createOperationSuccess,
54
+ } from '@art-lib/operations';
55
+ import { operations } from './log.js';
56
+
57
+ const startedAt = Date.now();
58
+
59
+ const pending = createGenericOperation('clone', { repo });
60
+ operations.log(pending); // forwarded as `debug`; pending operations are not stored
61
+
62
+ try {
63
+ const path = await clone(pending.data.repo);
64
+
65
+ operations.log(createOperationSuccess(pending, `cloned ${path}`)); // stored
66
+ } catch (error) {
67
+ // The label prefixes `errorSerialized()` only — `error` stays the raw message.
68
+ operations.log(createOperationFailure(pending, error, { label: 'CloneError' })); // stored
69
+ }
70
+
71
+ // The log keeps finished operations only, in order:
72
+ operations.all(); // every finished operation
73
+ operations.since(startedAt); // created at or after `startedAt`
74
+ operations.latest(5); // the last five finished operations
75
+ ```
76
+
77
+ ### Options
78
+
79
+ | Function | Arguments | Returns | Notes |
80
+ | -------------------------------------------- | ------------------------------------------------- | ------------------ | -------------------------------------------------------------------------- |
81
+ | `createGenericOperation(name, data?)` | operation name, optional payload | `OperationPending` | frozen; `message()` reports `data.uri` when present; `timing()` is `NaN` |
82
+ | `createOperationSuccess(pending, message?)` | a pending operation, optional success message | `OperationSuccess` | `message()` falls back to `pending.message()` |
83
+ | `createOperationFailure(pending, error, o?)` | a pending operation, any error-shaped value, `o?` | `OperationFailure` | `o.label` prefixes `errorSerialized()`; without it the raw message is used |
84
+ | `createOperationsLog(logger, options?)` | a `LoggerAPI`, reserved options | `OperationsLog` | forwards every operation; stores finished operations only |
85
+
86
+ - **`data`** — carried on the operation untouched and read back by the factory closures; a `uri` string property becomes `message()` until a success message overrides it.
87
+ - **`options`** — `createOperationsLog` accepts and deliberately ignores it; it is reserved for a future iteration.
88
+ - **`OperationsLog`** — `log(operation)`, `all()`, `since(timestamp)`, and `latest(count)`; queries return copies, never the internal buffer.
89
+
90
+ ## Development
91
+
92
+ Make sure you read the [Art Lib README](../../README.md) first.
93
+
94
+ ### Build Targets
95
+
96
+ This package is meant for use in Node.js environments. The entry point is built using `esbuild` pre-configured by [Workspace Tooling](https://github.com/noodlestan/workspace-tooling).
97
+
98
+ ### Scripts
99
+
100
+ - **$** `npm run dev` — Watch mode for development
101
+ - **$** `npm run lint` — Check formatting, lint, and type check
102
+ - **$** `npm run lint:fix` — Fix formatting and lint issues
103
+ - **$** `npm run build` — Build the package
104
+ - **$** `npm run build:clean` — Remove build artifacts
105
+ - **$** `npm run test` — Run tests
106
+ - **$** `npm run ci` — Run CI pipeline
107
+
108
+ ## Copyright (c) 2026 [Noodlestan](https://noodlestan.org/).
109
+
110
+ Published under a [MIT license](https://noodlestan.mit-license.org/).
@@ -0,0 +1,2 @@
1
+ export * from './log/index.js';
2
+ export * from './model/index.js';
package/dist/index.mjs ADDED
@@ -0,0 +1,147 @@
1
+ // src/log/private/isFinished.ts
2
+ function isFinished(operation) {
3
+ return operation.outcome !== "pending";
4
+ }
5
+
6
+ // src/log/createOperationsLog.ts
7
+ function createOperationsLog(logger, options) {
8
+ void options;
9
+ const storedOperations = [];
10
+ return {
11
+ log(operation) {
12
+ logger.log(operation);
13
+ if (isFinished(operation)) {
14
+ storedOperations.push(operation);
15
+ }
16
+ },
17
+ all() {
18
+ return [...storedOperations];
19
+ },
20
+ since(timestamp) {
21
+ return storedOperations.filter((operation) => operation.ts >= timestamp);
22
+ },
23
+ latest(count) {
24
+ if (count <= 0) {
25
+ return [];
26
+ }
27
+ return storedOperations.slice(Math.max(storedOperations.length - count, 0));
28
+ }
29
+ };
30
+ }
31
+
32
+ // src/model/private/constants.ts
33
+ var LEVELS = {
34
+ pending: "debug",
35
+ success: "info",
36
+ failure: "error"
37
+ };
38
+
39
+ // src/model/private/readInputUri.ts
40
+ function readInputUri(data) {
41
+ if (typeof data !== "object" || data === null || !("uri" in data)) {
42
+ return "";
43
+ }
44
+ const { uri } = data;
45
+ return typeof uri === "string" ? uri : "";
46
+ }
47
+
48
+ // src/model/createGenericOperation.ts
49
+ function createGenericOperation(name, data) {
50
+ const ts = Date.now();
51
+ function message() {
52
+ return readInputUri(data);
53
+ }
54
+ const pending = {
55
+ operation: name,
56
+ ts,
57
+ outcome: "pending",
58
+ level: LEVELS.pending,
59
+ data,
60
+ message,
61
+ timing() {
62
+ return Number.NaN;
63
+ },
64
+ text: message
65
+ };
66
+ return Object.freeze(pending);
67
+ }
68
+
69
+ // src/model/private/readRawErrorMessage.ts
70
+ function readRawErrorMessage(error) {
71
+ if (typeof error === "string") {
72
+ return error;
73
+ }
74
+ if (error === null || error === void 0) {
75
+ return "";
76
+ }
77
+ if (typeof error === "object" && "message" in error) {
78
+ const { message } = error;
79
+ if (typeof message === "string") {
80
+ return message;
81
+ }
82
+ }
83
+ try {
84
+ return String(error);
85
+ } catch {
86
+ return "";
87
+ }
88
+ }
89
+
90
+ // src/model/createOperationFailure.ts
91
+ function createOperationFailure(pending, error, options) {
92
+ const finishedTs = Date.now();
93
+ const rawError = readRawErrorMessage(error);
94
+ const label = options?.label;
95
+ function report() {
96
+ return rawError;
97
+ }
98
+ function errorSerialized() {
99
+ return label ? `${label}: ${rawError}` : rawError;
100
+ }
101
+ const failure = {
102
+ operation: pending.operation,
103
+ ts: pending.ts,
104
+ finishedTs,
105
+ outcome: "failure",
106
+ level: LEVELS.failure,
107
+ data: pending.data,
108
+ error: rawError,
109
+ message: report,
110
+ timing() {
111
+ return finishedTs - pending.ts;
112
+ },
113
+ text: report,
114
+ errorSerialized
115
+ };
116
+ return Object.freeze(failure);
117
+ }
118
+
119
+ // src/model/createOperationSuccess.ts
120
+ function createOperationSuccess(pending, message) {
121
+ const finishedTs = Date.now();
122
+ const givenText = message ?? pending.message();
123
+ function report() {
124
+ return givenText;
125
+ }
126
+ const success = {
127
+ operation: pending.operation,
128
+ ts: pending.ts,
129
+ finishedTs,
130
+ outcome: "success",
131
+ level: LEVELS.success,
132
+ data: pending.data,
133
+ message: report,
134
+ timing() {
135
+ return finishedTs - pending.ts;
136
+ },
137
+ text: report
138
+ };
139
+ return Object.freeze(success);
140
+ }
141
+ export {
142
+ createGenericOperation,
143
+ createOperationFailure,
144
+ createOperationSuccess,
145
+ createOperationsLog
146
+ };
147
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/log/private/isFinished.ts", "../src/log/createOperationsLog.ts", "../src/model/private/constants.ts", "../src/model/private/readInputUri.ts", "../src/model/createGenericOperation.ts", "../src/model/private/readRawErrorMessage.ts", "../src/model/createOperationFailure.ts", "../src/model/createOperationSuccess.ts"],
4
+ "sourcesContent": ["import type { AnyFinishedOperation, LoggableOperation } from '../types.js';\n\nexport function isFinished(operation: LoggableOperation): operation is AnyFinishedOperation {\n\treturn operation.outcome !== 'pending';\n}\n", "import type { LoggerAPI } from '@art-lib/logger';\n\nimport { isFinished } from './private/isFinished.js';\nimport type { AnyFinishedOperation, OperationsLog, OperationsLogOptions } from './types.js';\n\nexport function createOperationsLog(\n\tlogger: LoggerAPI,\n\toptions?: OperationsLogOptions,\n): OperationsLog {\n\t// Reserved for a future iteration: accepted, deliberately not read here.\n\t// eslint-disable-next-line no-void\n\tvoid options;\n\n\tconst storedOperations: AnyFinishedOperation[] = [];\n\n\treturn {\n\t\tlog(operation): void {\n\t\t\tlogger.log(operation); // every operation is a message: pending and finished alike\n\n\t\t\tif (isFinished(operation)) {\n\t\t\t\tstoredOperations.push(operation); // finished operations only\n\t\t\t}\n\t\t},\n\t\tall(): AnyFinishedOperation[] {\n\t\t\treturn [...storedOperations]; // a copy \u2014 never leak the internal buffer\n\t\t},\n\t\tsince(timestamp: number): AnyFinishedOperation[] {\n\t\t\treturn storedOperations.filter(operation => operation.ts >= timestamp);\n\t\t},\n\t\tlatest(count: number): AnyFinishedOperation[] {\n\t\t\tif (count <= 0) {\n\t\t\t\treturn [];\n\t\t\t}\n\n\t\t\treturn storedOperations.slice(Math.max(storedOperations.length - count, 0));\n\t\t},\n\t};\n}\n", "import type { MessageLevel } from '@art-lib/logger';\n\nimport type { OperationOutcome } from '../types.js';\n\nexport const LEVELS: Record<OperationOutcome, MessageLevel> = {\n\tpending: 'debug',\n\tsuccess: 'info',\n\tfailure: 'error',\n};\n", "export function readInputUri(data: unknown): string {\n\tif (typeof data !== 'object' || data === null || !('uri' in data)) {\n\t\treturn '';\n\t}\n\n\tconst { uri } = data as { uri: unknown };\n\n\treturn typeof uri === 'string' ? uri : '';\n}\n", "import { LEVELS } from './private/constants.js';\nimport { readInputUri } from './private/readInputUri.js';\nimport type { OperationPending } from './types.js';\n\nexport function createGenericOperation(name: string, data?: unknown): OperationPending {\n\tconst ts = Date.now();\n\n\tfunction message(): string {\n\t\treturn readInputUri(data);\n\t}\n\n\tconst pending: OperationPending = {\n\t\toperation: name,\n\t\tts,\n\t\toutcome: 'pending',\n\t\tlevel: LEVELS.pending,\n\t\tdata,\n\t\tmessage,\n\t\ttiming(): number {\n\t\t\treturn Number.NaN;\n\t\t},\n\t\ttext: message,\n\t};\n\n\treturn Object.freeze(pending);\n}\n", "export function readRawErrorMessage(error: unknown): string {\n\tif (typeof error === 'string') {\n\t\treturn error;\n\t}\n\n\tif (error === null || error === undefined) {\n\t\treturn '';\n\t}\n\n\tif (typeof error === 'object' && 'message' in error) {\n\t\tconst { message } = error as { message: unknown };\n\n\t\tif (typeof message === 'string') {\n\t\t\treturn message;\n\t\t}\n\t}\n\n\ttry {\n\t\treturn String(error);\n\t} catch {\n\t\treturn '';\n\t}\n}\n", "import { LEVELS } from './private/constants.js';\nimport { readRawErrorMessage } from './private/readRawErrorMessage.js';\nimport type { OperationFailure, OperationPending } from './types.js';\n\nexport function createOperationFailure(\n\tpending: OperationPending,\n\terror: unknown,\n\toptions?: { label?: string },\n): OperationFailure {\n\tconst finishedTs = Date.now();\n\tconst rawError = readRawErrorMessage(error);\n\tconst label = options?.label;\n\n\tfunction report(): string {\n\t\treturn rawError;\n\t}\n\n\tfunction errorSerialized(): string {\n\t\treturn label ? `${label}: ${rawError}` : rawError;\n\t}\n\n\tconst failure: OperationFailure = {\n\t\toperation: pending.operation,\n\t\tts: pending.ts,\n\t\tfinishedTs,\n\t\toutcome: 'failure',\n\t\tlevel: LEVELS.failure,\n\t\tdata: pending.data,\n\t\terror: rawError,\n\t\tmessage: report,\n\t\ttiming(): number {\n\t\t\treturn finishedTs - pending.ts;\n\t\t},\n\t\ttext: report,\n\t\terrorSerialized,\n\t};\n\n\treturn Object.freeze(failure);\n}\n", "import { LEVELS } from './private/constants.js';\nimport type { OperationPending, OperationSuccess } from './types.js';\n\nexport function createOperationSuccess(\n\tpending: OperationPending,\n\tmessage?: string,\n): OperationSuccess {\n\tconst finishedTs = Date.now();\n\tconst givenText = message ?? pending.message();\n\n\tfunction report(): string {\n\t\treturn givenText;\n\t}\n\n\tconst success: OperationSuccess = {\n\t\toperation: pending.operation,\n\t\tts: pending.ts,\n\t\tfinishedTs,\n\t\toutcome: 'success',\n\t\tlevel: LEVELS.success,\n\t\tdata: pending.data,\n\t\tmessage: report,\n\t\ttiming(): number {\n\t\t\treturn finishedTs - pending.ts;\n\t\t},\n\t\ttext: report,\n\t};\n\n\treturn Object.freeze(success);\n}\n"],
5
+ "mappings": "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",
6
+ "names": []
7
+ }
@@ -0,0 +1,3 @@
1
+ import type { LoggerAPI } from '@art-lib/logger';
2
+ import type { OperationsLog, OperationsLogOptions } from './types.js';
3
+ export declare function createOperationsLog(logger: LoggerAPI, options?: OperationsLogOptions): OperationsLog;
@@ -0,0 +1,2 @@
1
+ export { createOperationsLog } from './createOperationsLog.js';
2
+ export type { AnyFinishedOperation, LoggableOperation, OperationsLog, OperationsLogOptions, } from './types.js';
@@ -0,0 +1,34 @@
1
+ // src/log/private/isFinished.ts
2
+ function isFinished(operation) {
3
+ return operation.outcome !== "pending";
4
+ }
5
+
6
+ // src/log/createOperationsLog.ts
7
+ function createOperationsLog(logger, options) {
8
+ void options;
9
+ const storedOperations = [];
10
+ return {
11
+ log(operation) {
12
+ logger.log(operation);
13
+ if (isFinished(operation)) {
14
+ storedOperations.push(operation);
15
+ }
16
+ },
17
+ all() {
18
+ return [...storedOperations];
19
+ },
20
+ since(timestamp) {
21
+ return storedOperations.filter((operation) => operation.ts >= timestamp);
22
+ },
23
+ latest(count) {
24
+ if (count <= 0) {
25
+ return [];
26
+ }
27
+ return storedOperations.slice(Math.max(storedOperations.length - count, 0));
28
+ }
29
+ };
30
+ }
31
+ export {
32
+ createOperationsLog
33
+ };
34
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/log/private/isFinished.ts", "../../src/log/createOperationsLog.ts"],
4
+ "sourcesContent": ["import type { AnyFinishedOperation, LoggableOperation } from '../types.js';\n\nexport function isFinished(operation: LoggableOperation): operation is AnyFinishedOperation {\n\treturn operation.outcome !== 'pending';\n}\n", "import type { LoggerAPI } from '@art-lib/logger';\n\nimport { isFinished } from './private/isFinished.js';\nimport type { AnyFinishedOperation, OperationsLog, OperationsLogOptions } from './types.js';\n\nexport function createOperationsLog(\n\tlogger: LoggerAPI,\n\toptions?: OperationsLogOptions,\n): OperationsLog {\n\t// Reserved for a future iteration: accepted, deliberately not read here.\n\t// eslint-disable-next-line no-void\n\tvoid options;\n\n\tconst storedOperations: AnyFinishedOperation[] = [];\n\n\treturn {\n\t\tlog(operation): void {\n\t\t\tlogger.log(operation); // every operation is a message: pending and finished alike\n\n\t\t\tif (isFinished(operation)) {\n\t\t\t\tstoredOperations.push(operation); // finished operations only\n\t\t\t}\n\t\t},\n\t\tall(): AnyFinishedOperation[] {\n\t\t\treturn [...storedOperations]; // a copy \u2014 never leak the internal buffer\n\t\t},\n\t\tsince(timestamp: number): AnyFinishedOperation[] {\n\t\t\treturn storedOperations.filter(operation => operation.ts >= timestamp);\n\t\t},\n\t\tlatest(count: number): AnyFinishedOperation[] {\n\t\t\tif (count <= 0) {\n\t\t\t\treturn [];\n\t\t\t}\n\n\t\t\treturn storedOperations.slice(Math.max(storedOperations.length - count, 0));\n\t\t},\n\t};\n}\n"],
5
+ "mappings": ";AAEO,SAAS,WAAW,WAAiE;AAC3F,SAAO,UAAU,YAAY;AAC9B;;;ACCO,SAAS,oBACf,QACA,SACgB;AAGhB,OAAK;AAEL,QAAM,mBAA2C,CAAC;AAElD,SAAO;AAAA,IACN,IAAI,WAAiB;AACpB,aAAO,IAAI,SAAS;AAEpB,UAAI,WAAW,SAAS,GAAG;AAC1B,yBAAiB,KAAK,SAAS;AAAA,MAChC;AAAA,IACD;AAAA,IACA,MAA8B;AAC7B,aAAO,CAAC,GAAG,gBAAgB;AAAA,IAC5B;AAAA,IACA,MAAM,WAA2C;AAChD,aAAO,iBAAiB,OAAO,eAAa,UAAU,MAAM,SAAS;AAAA,IACtE;AAAA,IACA,OAAO,OAAuC;AAC7C,UAAI,SAAS,GAAG;AACf,eAAO,CAAC;AAAA,MACT;AAEA,aAAO,iBAAiB,MAAM,KAAK,IAAI,iBAAiB,SAAS,OAAO,CAAC,CAAC;AAAA,IAC3E;AAAA,EACD;AACD;",
6
+ "names": []
7
+ }
@@ -0,0 +1,2 @@
1
+ import type { AnyFinishedOperation, LoggableOperation } from '../types.js';
2
+ export declare function isFinished(operation: LoggableOperation): operation is AnyFinishedOperation;
@@ -0,0 +1,18 @@
1
+ import type { Message } from '@art-lib/logger';
2
+ import type { OperationFailure, OperationSuccess } from '../model/index.js';
3
+ export type AnyFinishedOperation = OperationSuccess | OperationFailure;
4
+ /**
5
+ * Any operation with an `outcome` that also satisfies `Message` of `@art-lib/logger`.
6
+ * Pending operations keep forwarding to the logger; finished-only storage is
7
+ * enforced at runtime by `createOperationsLog`.
8
+ */
9
+ export type LoggableOperation = Message & {
10
+ outcome: string;
11
+ };
12
+ export type OperationsLog = {
13
+ log(operation: LoggableOperation): void;
14
+ all(): AnyFinishedOperation[];
15
+ since(timestamp: number): AnyFinishedOperation[];
16
+ latest(count: number): AnyFinishedOperation[];
17
+ };
18
+ export type OperationsLogOptions = unknown;
@@ -0,0 +1,2 @@
1
+ import type { OperationPending } from './types.js';
2
+ export declare function createGenericOperation(name: string, data?: unknown): OperationPending;
@@ -0,0 +1,4 @@
1
+ import type { OperationFailure, OperationPending } from './types.js';
2
+ export declare function createOperationFailure(pending: OperationPending, error: unknown, options?: {
3
+ label?: string;
4
+ }): OperationFailure;
@@ -0,0 +1,2 @@
1
+ import type { OperationPending, OperationSuccess } from './types.js';
2
+ export declare function createOperationSuccess(pending: OperationPending, message?: string): OperationSuccess;
@@ -0,0 +1,4 @@
1
+ export type { OperationBase, OperationFailure, OperationOutcome, OperationPending, OperationSuccess, } from './types.js';
2
+ export { createGenericOperation } from './createGenericOperation.js';
3
+ export { createOperationFailure } from './createOperationFailure.js';
4
+ export { createOperationSuccess } from './createOperationSuccess.js';
@@ -0,0 +1,115 @@
1
+ // src/model/private/constants.ts
2
+ var LEVELS = {
3
+ pending: "debug",
4
+ success: "info",
5
+ failure: "error"
6
+ };
7
+
8
+ // src/model/private/readInputUri.ts
9
+ function readInputUri(data) {
10
+ if (typeof data !== "object" || data === null || !("uri" in data)) {
11
+ return "";
12
+ }
13
+ const { uri } = data;
14
+ return typeof uri === "string" ? uri : "";
15
+ }
16
+
17
+ // src/model/createGenericOperation.ts
18
+ function createGenericOperation(name, data) {
19
+ const ts = Date.now();
20
+ function message() {
21
+ return readInputUri(data);
22
+ }
23
+ const pending = {
24
+ operation: name,
25
+ ts,
26
+ outcome: "pending",
27
+ level: LEVELS.pending,
28
+ data,
29
+ message,
30
+ timing() {
31
+ return Number.NaN;
32
+ },
33
+ text: message
34
+ };
35
+ return Object.freeze(pending);
36
+ }
37
+
38
+ // src/model/private/readRawErrorMessage.ts
39
+ function readRawErrorMessage(error) {
40
+ if (typeof error === "string") {
41
+ return error;
42
+ }
43
+ if (error === null || error === void 0) {
44
+ return "";
45
+ }
46
+ if (typeof error === "object" && "message" in error) {
47
+ const { message } = error;
48
+ if (typeof message === "string") {
49
+ return message;
50
+ }
51
+ }
52
+ try {
53
+ return String(error);
54
+ } catch {
55
+ return "";
56
+ }
57
+ }
58
+
59
+ // src/model/createOperationFailure.ts
60
+ function createOperationFailure(pending, error, options) {
61
+ const finishedTs = Date.now();
62
+ const rawError = readRawErrorMessage(error);
63
+ const label = options?.label;
64
+ function report() {
65
+ return rawError;
66
+ }
67
+ function errorSerialized() {
68
+ return label ? `${label}: ${rawError}` : rawError;
69
+ }
70
+ const failure = {
71
+ operation: pending.operation,
72
+ ts: pending.ts,
73
+ finishedTs,
74
+ outcome: "failure",
75
+ level: LEVELS.failure,
76
+ data: pending.data,
77
+ error: rawError,
78
+ message: report,
79
+ timing() {
80
+ return finishedTs - pending.ts;
81
+ },
82
+ text: report,
83
+ errorSerialized
84
+ };
85
+ return Object.freeze(failure);
86
+ }
87
+
88
+ // src/model/createOperationSuccess.ts
89
+ function createOperationSuccess(pending, message) {
90
+ const finishedTs = Date.now();
91
+ const givenText = message ?? pending.message();
92
+ function report() {
93
+ return givenText;
94
+ }
95
+ const success = {
96
+ operation: pending.operation,
97
+ ts: pending.ts,
98
+ finishedTs,
99
+ outcome: "success",
100
+ level: LEVELS.success,
101
+ data: pending.data,
102
+ message: report,
103
+ timing() {
104
+ return finishedTs - pending.ts;
105
+ },
106
+ text: report
107
+ };
108
+ return Object.freeze(success);
109
+ }
110
+ export {
111
+ createGenericOperation,
112
+ createOperationFailure,
113
+ createOperationSuccess
114
+ };
115
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/model/private/constants.ts", "../../src/model/private/readInputUri.ts", "../../src/model/createGenericOperation.ts", "../../src/model/private/readRawErrorMessage.ts", "../../src/model/createOperationFailure.ts", "../../src/model/createOperationSuccess.ts"],
4
+ "sourcesContent": ["import type { MessageLevel } from '@art-lib/logger';\n\nimport type { OperationOutcome } from '../types.js';\n\nexport const LEVELS: Record<OperationOutcome, MessageLevel> = {\n\tpending: 'debug',\n\tsuccess: 'info',\n\tfailure: 'error',\n};\n", "export function readInputUri(data: unknown): string {\n\tif (typeof data !== 'object' || data === null || !('uri' in data)) {\n\t\treturn '';\n\t}\n\n\tconst { uri } = data as { uri: unknown };\n\n\treturn typeof uri === 'string' ? uri : '';\n}\n", "import { LEVELS } from './private/constants.js';\nimport { readInputUri } from './private/readInputUri.js';\nimport type { OperationPending } from './types.js';\n\nexport function createGenericOperation(name: string, data?: unknown): OperationPending {\n\tconst ts = Date.now();\n\n\tfunction message(): string {\n\t\treturn readInputUri(data);\n\t}\n\n\tconst pending: OperationPending = {\n\t\toperation: name,\n\t\tts,\n\t\toutcome: 'pending',\n\t\tlevel: LEVELS.pending,\n\t\tdata,\n\t\tmessage,\n\t\ttiming(): number {\n\t\t\treturn Number.NaN;\n\t\t},\n\t\ttext: message,\n\t};\n\n\treturn Object.freeze(pending);\n}\n", "export function readRawErrorMessage(error: unknown): string {\n\tif (typeof error === 'string') {\n\t\treturn error;\n\t}\n\n\tif (error === null || error === undefined) {\n\t\treturn '';\n\t}\n\n\tif (typeof error === 'object' && 'message' in error) {\n\t\tconst { message } = error as { message: unknown };\n\n\t\tif (typeof message === 'string') {\n\t\t\treturn message;\n\t\t}\n\t}\n\n\ttry {\n\t\treturn String(error);\n\t} catch {\n\t\treturn '';\n\t}\n}\n", "import { LEVELS } from './private/constants.js';\nimport { readRawErrorMessage } from './private/readRawErrorMessage.js';\nimport type { OperationFailure, OperationPending } from './types.js';\n\nexport function createOperationFailure(\n\tpending: OperationPending,\n\terror: unknown,\n\toptions?: { label?: string },\n): OperationFailure {\n\tconst finishedTs = Date.now();\n\tconst rawError = readRawErrorMessage(error);\n\tconst label = options?.label;\n\n\tfunction report(): string {\n\t\treturn rawError;\n\t}\n\n\tfunction errorSerialized(): string {\n\t\treturn label ? `${label}: ${rawError}` : rawError;\n\t}\n\n\tconst failure: OperationFailure = {\n\t\toperation: pending.operation,\n\t\tts: pending.ts,\n\t\tfinishedTs,\n\t\toutcome: 'failure',\n\t\tlevel: LEVELS.failure,\n\t\tdata: pending.data,\n\t\terror: rawError,\n\t\tmessage: report,\n\t\ttiming(): number {\n\t\t\treturn finishedTs - pending.ts;\n\t\t},\n\t\ttext: report,\n\t\terrorSerialized,\n\t};\n\n\treturn Object.freeze(failure);\n}\n", "import { LEVELS } from './private/constants.js';\nimport type { OperationPending, OperationSuccess } from './types.js';\n\nexport function createOperationSuccess(\n\tpending: OperationPending,\n\tmessage?: string,\n): OperationSuccess {\n\tconst finishedTs = Date.now();\n\tconst givenText = message ?? pending.message();\n\n\tfunction report(): string {\n\t\treturn givenText;\n\t}\n\n\tconst success: OperationSuccess = {\n\t\toperation: pending.operation,\n\t\tts: pending.ts,\n\t\tfinishedTs,\n\t\toutcome: 'success',\n\t\tlevel: LEVELS.success,\n\t\tdata: pending.data,\n\t\tmessage: report,\n\t\ttiming(): number {\n\t\t\treturn finishedTs - pending.ts;\n\t\t},\n\t\ttext: report,\n\t};\n\n\treturn Object.freeze(success);\n}\n"],
5
+ "mappings": ";AAIO,IAAM,SAAiD;AAAA,EAC7D,SAAS;AAAA,EACT,SAAS;AAAA,EACT,SAAS;AACV;;;ACRO,SAAS,aAAa,MAAuB;AACnD,MAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,SAAS,OAAO;AAClE,WAAO;AAAA,EACR;AAEA,QAAM,EAAE,IAAI,IAAI;AAEhB,SAAO,OAAO,QAAQ,WAAW,MAAM;AACxC;;;ACJO,SAAS,uBAAuB,MAAc,MAAkC;AACtF,QAAM,KAAK,KAAK,IAAI;AAEpB,WAAS,UAAkB;AAC1B,WAAO,aAAa,IAAI;AAAA,EACzB;AAEA,QAAM,UAA4B;AAAA,IACjC,WAAW;AAAA,IACX;AAAA,IACA,SAAS;AAAA,IACT,OAAO,OAAO;AAAA,IACd;AAAA,IACA;AAAA,IACA,SAAiB;AAChB,aAAO,OAAO;AAAA,IACf;AAAA,IACA,MAAM;AAAA,EACP;AAEA,SAAO,OAAO,OAAO,OAAO;AAC7B;;;ACzBO,SAAS,oBAAoB,OAAwB;AAC3D,MAAI,OAAO,UAAU,UAAU;AAC9B,WAAO;AAAA,EACR;AAEA,MAAI,UAAU,QAAQ,UAAU,QAAW;AAC1C,WAAO;AAAA,EACR;AAEA,MAAI,OAAO,UAAU,YAAY,aAAa,OAAO;AACpD,UAAM,EAAE,QAAQ,IAAI;AAEpB,QAAI,OAAO,YAAY,UAAU;AAChC,aAAO;AAAA,IACR;AAAA,EACD;AAEA,MAAI;AACH,WAAO,OAAO,KAAK;AAAA,EACpB,QAAQ;AACP,WAAO;AAAA,EACR;AACD;;;AClBO,SAAS,uBACf,SACA,OACA,SACmB;AACnB,QAAM,aAAa,KAAK,IAAI;AAC5B,QAAM,WAAW,oBAAoB,KAAK;AAC1C,QAAM,QAAQ,SAAS;AAEvB,WAAS,SAAiB;AACzB,WAAO;AAAA,EACR;AAEA,WAAS,kBAA0B;AAClC,WAAO,QAAQ,GAAG,KAAK,KAAK,QAAQ,KAAK;AAAA,EAC1C;AAEA,QAAM,UAA4B;AAAA,IACjC,WAAW,QAAQ;AAAA,IACnB,IAAI,QAAQ;AAAA,IACZ;AAAA,IACA,SAAS;AAAA,IACT,OAAO,OAAO;AAAA,IACd,MAAM,QAAQ;AAAA,IACd,OAAO;AAAA,IACP,SAAS;AAAA,IACT,SAAiB;AAChB,aAAO,aAAa,QAAQ;AAAA,IAC7B;AAAA,IACA,MAAM;AAAA,IACN;AAAA,EACD;AAEA,SAAO,OAAO,OAAO,OAAO;AAC7B;;;ACnCO,SAAS,uBACf,SACA,SACmB;AACnB,QAAM,aAAa,KAAK,IAAI;AAC5B,QAAM,YAAY,WAAW,QAAQ,QAAQ;AAE7C,WAAS,SAAiB;AACzB,WAAO;AAAA,EACR;AAEA,QAAM,UAA4B;AAAA,IACjC,WAAW,QAAQ;AAAA,IACnB,IAAI,QAAQ;AAAA,IACZ;AAAA,IACA,SAAS;AAAA,IACT,OAAO,OAAO;AAAA,IACd,MAAM,QAAQ;AAAA,IACd,SAAS;AAAA,IACT,SAAiB;AAChB,aAAO,aAAa,QAAQ;AAAA,IAC7B;AAAA,IACA,MAAM;AAAA,EACP;AAEA,SAAO,OAAO,OAAO,OAAO;AAC7B;",
6
+ "names": []
7
+ }
@@ -0,0 +1,3 @@
1
+ import type { MessageLevel } from '@art-lib/logger';
2
+ import type { OperationOutcome } from '../types.js';
3
+ export declare const LEVELS: Record<OperationOutcome, MessageLevel>;
@@ -0,0 +1 @@
1
+ export declare function readInputUri(data: unknown): string;
@@ -0,0 +1 @@
1
+ export declare function readRawErrorMessage(error: unknown): string;
@@ -0,0 +1,26 @@
1
+ import type { MessageLevel } from '@art-lib/logger';
2
+ export type OperationOutcome = 'pending' | 'success' | 'failure';
3
+ export type OperationBase = {
4
+ readonly operation: string;
5
+ readonly ts: number;
6
+ readonly finishedTs?: number;
7
+ readonly outcome: OperationOutcome;
8
+ message(): string;
9
+ timing(): number;
10
+ readonly level: MessageLevel;
11
+ text(): string;
12
+ };
13
+ export type OperationPending<D = unknown> = OperationBase & {
14
+ readonly outcome: 'pending';
15
+ readonly data?: D;
16
+ };
17
+ export type OperationSuccess<D = unknown> = OperationBase & {
18
+ readonly outcome: 'success';
19
+ readonly data?: D;
20
+ };
21
+ export type OperationFailure<D = unknown> = OperationBase & {
22
+ readonly outcome: 'failure';
23
+ readonly data?: D;
24
+ readonly error: string;
25
+ errorSerialized(): string;
26
+ };
@@ -0,0 +1,3 @@
1
+ import type { LoggerMock } from './types.js';
2
+ /** @mocks LoggerAPI */
3
+ export declare function createLoggerMock(): LoggerMock;
@@ -0,0 +1,5 @@
1
+ import type { LoggerAPI, Message } from '@art-lib/logger';
2
+ export type LoggerMock = {
3
+ logger: LoggerAPI;
4
+ messages: Message[];
5
+ };
package/package.json CHANGED
@@ -1,6 +1,64 @@
1
1
  {
2
2
  "name": "@art-lib/operations",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.1",
4
+ "description": "Operation outcome types and factories, and an append-only operations log that forwards every operation to the logger.",
5
+ "keywords": [
6
+ "library",
7
+ "operations",
8
+ "cli",
9
+ "tooling",
10
+ "art",
11
+ "terminal-apps"
12
+ ],
13
+ "author": "Noodlestan Collective",
14
+ "license": "MIT",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/noodlestan/art-lib.git",
18
+ "directory": "libs/operations"
19
+ },
20
+ "private": false,
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "files": [
25
+ "dist",
26
+ "README.md",
27
+ "LICENSE-MIT",
28
+ "CHANGELOG.md"
29
+ ],
30
+ "engines": {
31
+ "node": ">=22"
32
+ },
33
+ "type": "module",
34
+ "sideEffects": false,
35
+ "exports": {
36
+ ".": {
37
+ "types": "./dist/index.d.ts",
38
+ "import": "./dist/index.mjs"
39
+ },
40
+ "./model": {
41
+ "types": "./dist/model/index.d.ts",
42
+ "import": "./dist/model/index.mjs"
43
+ },
44
+ "./log": {
45
+ "types": "./dist/log/index.d.ts",
46
+ "import": "./dist/log/index.mjs"
47
+ }
48
+ },
49
+ "scripts": {
50
+ "dev": "esbuild-cli-watch",
51
+ "lint": "prettier . -c && eslint . && tsc --noEmit",
52
+ "lint:fix": "prettier . -c --write && eslint . --fix",
53
+ "build": "esbuild-cli",
54
+ "build:clean": "rm -rf dist",
55
+ "test": "vitest run",
56
+ "test:ci": "vitest run --coverage",
57
+ "test:watch": "vitest",
58
+ "ci": "npm run lint && npm run build && npm run test:ci"
59
+ },
60
+ "dependencies": {
61
+ "@art-lib/logger": "0.0.1"
62
+ },
63
+ "devDependencies": {}
64
+ }