better-result-cf-workflows 0.0.1 → 0.0.3
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/dist/codec.d.ts +21 -0
- package/dist/codec.js +142 -0
- package/dist/index.d.ts +5 -0
- package/{vitest.config.ts → dist/index.js} +3 -15
- package/dist/step/compat.d.ts +32 -0
- package/{src/index.ts → dist/step/compat.js} +1 -13
- package/dist/step/do.d.ts +5 -0
- package/dist/step/do.js +80 -0
- package/dist/step/index.d.ts +10 -0
- package/{src/step/wait-for-event.ts → dist/step/index.js} +11 -20
- package/dist/step/wait-for-event.d.ts +8 -0
- package/{tests/utils.ts → dist/step/wait-for-event.js} +13 -16
- package/dist/wf-class.d.ts +16 -0
- package/dist/wf-class.js +46 -0
- package/package.json +8 -3
- package/.forgejo/workflows/lint_test.yaml +0 -27
- package/.forgejo/workflows/main.yaml +0 -13
- package/.forgejo/workflows/pr.yaml +0 -8
- package/.husky/pre-commit +0 -1
- package/.oxfmtrc.json +0 -6
- package/.oxlintrc.json +0 -29
- package/.vscode/settings.json +0 -3
- package/CONTRIBUTING.md +0 -14
- package/pnpm-workspace.yaml +0 -3
- package/src/codec.ts +0 -191
- package/src/step/compat.ts +0 -70
- package/src/step/do.ts +0 -124
- package/src/step/index.ts +0 -36
- package/src/wf-class.ts +0 -73
- package/tests/translation.test.ts +0 -72
- package/tests/tsconfig.json +0 -7
- package/tests/wf-class.test.ts +0 -105
- package/tests/worker-configuration.d.ts +0 -16564
- package/tests/workflows.ts +0 -56
- package/tests/wrangler.test.jsonc +0 -12
- package/tsconfig.json +0 -16
- package/tsconfig.tsbuildinfo +0 -1
package/dist/codec.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { AnyTaggedError, Panic, StandardSchemaV1, TaggedErrorInstance } from "better-result";
|
|
2
|
+
import { NonRetryableError } from "cloudflare:workflows";
|
|
3
|
+
export type ErrCodec<E extends AnyTaggedError> = {
|
|
4
|
+
encode: StandardSchemaV1<E, string>;
|
|
5
|
+
decode: StandardSchemaV1<string, E>;
|
|
6
|
+
};
|
|
7
|
+
export type InferErrorsFromErrCodec<T extends ErrCodec<AnyTaggedError>> = T extends ErrCodec<infer E> ? E : never;
|
|
8
|
+
type GenericTaggedErrorClass<T extends string> = abstract new (...args: never[]) => TaggedErrorInstance<T, unknown>;
|
|
9
|
+
/** Creates a result codec that passes on the OK value directly and encodes the tagged errors
|
|
10
|
+
* To remain error identity, all errors must be passed in by value in the errors variable
|
|
11
|
+
*
|
|
12
|
+
* NOTE: The codec returned does not verify the shape of the parameters - so do not use this result codec outside of a workflow
|
|
13
|
+
* A workflow's RPC boundaries are *basically* trustworthy.
|
|
14
|
+
* Technically, your code may change as you wait on old workflows, but there is virtually nothing you can do about this.
|
|
15
|
+
*/
|
|
16
|
+
export declare function errorMapCodec<E extends string>(errors: {
|
|
17
|
+
[K in E]: GenericTaggedErrorClass<K>;
|
|
18
|
+
}): ErrCodec<TaggedErrorInstance<E, unknown>>;
|
|
19
|
+
export declare function encodeErr<E extends AnyTaggedError>(codec: ErrCodec<E>, value: unknown, errClass?: ErrorConstructor | typeof NonRetryableError): Promise<Error>;
|
|
20
|
+
export declare function decodeErr<E extends AnyTaggedError>(codec: ErrCodec<E>, value: unknown): Promise<Panic | E>;
|
|
21
|
+
export {};
|
package/dist/codec.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file is part of better-result-cf-workflows.
|
|
3
|
+
* better-result-cf-workflows is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
4
|
+
* better-result-cf-workflows is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
5
|
+
* You should have received a copy of the GNU General Public License along with better-result-cf-workflows. If not, see <https://www.gnu.org/licenses/>.
|
|
6
|
+
*/
|
|
7
|
+
import { Panic, TaggedError, } from "better-result";
|
|
8
|
+
import { z } from "zod/mini";
|
|
9
|
+
/** Creates a result codec that passes on the OK value directly and encodes the tagged errors
|
|
10
|
+
* To remain error identity, all errors must be passed in by value in the errors variable
|
|
11
|
+
*
|
|
12
|
+
* NOTE: The codec returned does not verify the shape of the parameters - so do not use this result codec outside of a workflow
|
|
13
|
+
* A workflow's RPC boundaries are *basically* trustworthy.
|
|
14
|
+
* Technically, your code may change as you wait on old workflows, but there is virtually nothing you can do about this.
|
|
15
|
+
*/
|
|
16
|
+
export function errorMapCodec(errors) {
|
|
17
|
+
// @ts-expect-error we are using unknown here for efficiency
|
|
18
|
+
// but this codec is only ever used for result error serialization/deserialization
|
|
19
|
+
// so its fine
|
|
20
|
+
const zodCodec = z.codec(z.string(), z.unknown(), {
|
|
21
|
+
// @ts-expect-error see above comment
|
|
22
|
+
encode: (v) => {
|
|
23
|
+
return JSON.stringify(v.toJSON());
|
|
24
|
+
},
|
|
25
|
+
decode: (v, ctx) => {
|
|
26
|
+
let json;
|
|
27
|
+
try {
|
|
28
|
+
json = JSON.parse(v);
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
ctx.issues.push({
|
|
32
|
+
code: "invalid_format",
|
|
33
|
+
format: "json",
|
|
34
|
+
input: v,
|
|
35
|
+
// @ts-expect-error err.message isn't know to ts, but cmon
|
|
36
|
+
message: err?.message,
|
|
37
|
+
continue: false,
|
|
38
|
+
});
|
|
39
|
+
return z.NEVER;
|
|
40
|
+
}
|
|
41
|
+
if (typeof json !== "object" || Array.isArray(json) || json === null) {
|
|
42
|
+
ctx.issues.push({
|
|
43
|
+
code: "invalid_type",
|
|
44
|
+
expected: "object",
|
|
45
|
+
input: json,
|
|
46
|
+
continue: false,
|
|
47
|
+
});
|
|
48
|
+
return z.NEVER;
|
|
49
|
+
}
|
|
50
|
+
if (!("_tag" in json) || typeof json["_tag"] !== "string") {
|
|
51
|
+
ctx.issues.push({
|
|
52
|
+
code: "invalid_type",
|
|
53
|
+
expected: "string",
|
|
54
|
+
input: "_tag" in json ? json["_tag"] : undefined,
|
|
55
|
+
continue: false,
|
|
56
|
+
path: ["_tag"],
|
|
57
|
+
});
|
|
58
|
+
return z.NEVER;
|
|
59
|
+
}
|
|
60
|
+
if (!(json["_tag"] in errors)) {
|
|
61
|
+
ctx.issues.push({
|
|
62
|
+
code: "custom",
|
|
63
|
+
input: json["_tag"],
|
|
64
|
+
continue: false,
|
|
65
|
+
path: ["_tag"],
|
|
66
|
+
message: "Unknown tag",
|
|
67
|
+
});
|
|
68
|
+
return z.NEVER;
|
|
69
|
+
}
|
|
70
|
+
const cls = errors[json["_tag"]];
|
|
71
|
+
// @ts-expect-error we obviously did not give types to the error
|
|
72
|
+
// in fact, this is strictly un-safe
|
|
73
|
+
// however, since we are purely using this for workflow rpc encoding, this will work
|
|
74
|
+
// we are not navigating trust boundaries, we ARE navigating network but only on a technicality
|
|
75
|
+
// lets not bother ourselves with (in this case) pointless safety
|
|
76
|
+
return new cls(json);
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
return {
|
|
80
|
+
encode: z.invertCodec(zodCodec),
|
|
81
|
+
decode: zodCodec,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
// fun discovery: so if you have a custom error class (even one thats not a tagged error)
|
|
85
|
+
// outside of RPC, that error will look like a normal error class. Even if you set the error.name!
|
|
86
|
+
// So the error's message will look like {err name}: {err message}
|
|
87
|
+
// From that, the decision to move forward is to JSONify the data & encode/decode it. Easy :3
|
|
88
|
+
async function wrapStdSchema(codec, value) {
|
|
89
|
+
try {
|
|
90
|
+
const stdResult = await codec["~standard"].validate(value);
|
|
91
|
+
if (stdResult.issues) {
|
|
92
|
+
return new Panic({
|
|
93
|
+
message: "invalid error has been returned",
|
|
94
|
+
cause: stdResult.issues,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return stdResult.value;
|
|
98
|
+
}
|
|
99
|
+
catch (err) {
|
|
100
|
+
return new Panic({ message: "decoder threw while decoding an error!", cause: err });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
export async function encodeErr(codec, value, errClass = Error) {
|
|
104
|
+
if (TaggedError.is(value)) {
|
|
105
|
+
const res = await wrapStdSchema(codec.encode, value);
|
|
106
|
+
if (Panic.is(res)) {
|
|
107
|
+
value = res;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
return new errClass(res);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (Panic.is(value)) {
|
|
114
|
+
return new errClass(JSON.stringify(value.toJSON()));
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
return new errClass(JSON.stringify(new Panic({ message: "Unknown error happened", cause: value }).toJSON()));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
export async function decodeErr(codec, value) {
|
|
121
|
+
if (Panic.is(value) || TaggedError.is(value)) {
|
|
122
|
+
return value;
|
|
123
|
+
}
|
|
124
|
+
if (!(value instanceof Error)) {
|
|
125
|
+
return new Panic({ message: "Unknown error given to decoder", cause: value });
|
|
126
|
+
}
|
|
127
|
+
// a codec may or may not use JSON for encoding. However, a Panic definitely uses JSON
|
|
128
|
+
// so lets test for that first
|
|
129
|
+
try {
|
|
130
|
+
const json = JSON.parse(value.message);
|
|
131
|
+
if (typeof json !== "object" || json === null || !("_tag" in json)) {
|
|
132
|
+
return new Panic({ message: "Non-tagged error given in json", cause: json });
|
|
133
|
+
}
|
|
134
|
+
if (json["_tag"] === "Panic") {
|
|
135
|
+
return new Panic(json);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
// who knows that this can mean - maybe the codec just isn't a json guy
|
|
140
|
+
}
|
|
141
|
+
return await wrapStdSchema(codec.decode, value.message);
|
|
142
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { ResultWorkflowEntrypoint } from "./wf-class";
|
|
2
|
+
export { errorMapCodec, ErrCodec, InferErrorsFromErrCodec } from "./codec";
|
|
3
|
+
export { StepEventTimeoutErr } from "./step/wait-for-event";
|
|
4
|
+
export { ResultStep } from "./step/index";
|
|
5
|
+
export { ResultWorkflowDelayFunction, ResultWorkflowDynamicDelayContext, ResultWorkflowRetryConfig, ResultWorkflowRollbackContext, ResultWorkflowStepConfig, ResultWorkflowStepRollbackOptions, } from "./step/compat";
|
|
@@ -4,18 +4,6 @@
|
|
|
4
4
|
* better-result-cf-workflows is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
5
5
|
* You should have received a copy of the GNU General Public License along with better-result-cf-workflows. If not, see <https://www.gnu.org/licenses/>.
|
|
6
6
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export default defineConfig({
|
|
12
|
-
plugins: [
|
|
13
|
-
cloudflareTest({
|
|
14
|
-
wrangler: { configPath: "./tests/wrangler.test.jsonc" },
|
|
15
|
-
}),
|
|
16
|
-
],
|
|
17
|
-
test: {
|
|
18
|
-
testTimeout: 10_000,
|
|
19
|
-
mockReset: true,
|
|
20
|
-
},
|
|
21
|
-
})
|
|
7
|
+
export { ResultWorkflowEntrypoint } from "./wf-class";
|
|
8
|
+
export { errorMapCodec } from "./codec";
|
|
9
|
+
export { StepEventTimeoutErr } from "./step/wait-for-event";
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { AnyTaggedError, Panic, Result } from "better-result";
|
|
2
|
+
import { WorkflowBackoff, WorkflowDelayDuration, WorkflowRollbackContext, WorkflowStepConfig, WorkflowStepContext, WorkflowStepRollbackConfig, WorkflowStepRollbackOptions } from "cloudflare:workers";
|
|
3
|
+
export type ResultWorkflowDynamicDelayContext<E extends AnyTaggedError> = {
|
|
4
|
+
ctx: WorkflowStepContext<ResultWorkflowDelayFunction<E>>;
|
|
5
|
+
error: E | Panic;
|
|
6
|
+
};
|
|
7
|
+
export type ResultWorkflowDelayFunction<E extends AnyTaggedError> = (input: ResultWorkflowDynamicDelayContext<E>) => WorkflowDelayDuration | Promise<WorkflowDelayDuration>;
|
|
8
|
+
export type ResultWorkflowRetryConfig<E extends AnyTaggedError> = {
|
|
9
|
+
shouldRetry?: (err: E) => boolean | {
|
|
10
|
+
[K in E as K["_tag"]]: boolean | ((err: K) => boolean);
|
|
11
|
+
};
|
|
12
|
+
limit: number;
|
|
13
|
+
delay: WorkflowDelayDuration | number | ResultWorkflowDelayFunction<E>;
|
|
14
|
+
backoff?: WorkflowBackoff;
|
|
15
|
+
};
|
|
16
|
+
export type ResultWorkflowStepConfig<T extends Rpc.Serializable<T>, E extends AnyTaggedError> = Omit<WorkflowStepConfig, "retries"> & {
|
|
17
|
+
callback: (ctx: WorkflowStepContext) => Promise<Result<T, E>>;
|
|
18
|
+
retries?: ResultWorkflowRetryConfig<E>;
|
|
19
|
+
};
|
|
20
|
+
export type ResultWorkflowRollbackContext<T extends Rpc.Serializable<T>, E extends AnyTaggedError> = Omit<WorkflowRollbackContext<T>, "error"> & {
|
|
21
|
+
error: E | Panic;
|
|
22
|
+
};
|
|
23
|
+
type ResultWorkflowRollbackHandler<T extends Rpc.Serializable<T>, E extends AnyTaggedError> = (ctx: ResultWorkflowRollbackContext<T, E>) => Promise<Result<void, E>>;
|
|
24
|
+
type ResultWorkflowStepRollbackConfig<E extends AnyTaggedError> = Omit<WorkflowStepRollbackConfig, "retries"> & {
|
|
25
|
+
retries?: ResultWorkflowRetryConfig<E>;
|
|
26
|
+
};
|
|
27
|
+
export type ResultWorkflowStepRollbackOptions<T extends Rpc.Serializable<T>, E extends AnyTaggedError> = Omit<WorkflowStepRollbackOptions, "rollback" | "rollbackConfig"> & {
|
|
28
|
+
rollback: ResultWorkflowRollbackHandler<T, E>;
|
|
29
|
+
rollbackConfig?: ResultWorkflowStepRollbackConfig<E>;
|
|
30
|
+
};
|
|
31
|
+
export type ResultDoFunction = <T extends Rpc.Serializable<T>, E extends AnyTaggedError>(name: string, config: ResultWorkflowStepConfig<T, E>, rollback?: ResultWorkflowStepRollbackOptions<T, E>) => Promise<Result<T, E>>;
|
|
32
|
+
export {};
|
|
@@ -4,16 +4,4 @@
|
|
|
4
4
|
* better-result-cf-workflows is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
5
5
|
* You should have received a copy of the GNU General Public License along with better-result-cf-workflows. If not, see <https://www.gnu.org/licenses/>.
|
|
6
6
|
*/
|
|
7
|
-
|
|
8
|
-
export { ResultWorkflowEntrypoint } from "./wf-class"
|
|
9
|
-
export { errorMapCodec, ErrCodec, InferErrorsFromErrCodec } from "./codec"
|
|
10
|
-
export { StepEventTimeoutErr } from "./step/wait-for-event"
|
|
11
|
-
export { ResultStep } from "./step/index"
|
|
12
|
-
export {
|
|
13
|
-
ResultWorkflowDelayFunction,
|
|
14
|
-
ResultWorkflowDynamicDelayContext,
|
|
15
|
-
ResultWorkflowRetryConfig,
|
|
16
|
-
ResultWorkflowRollbackContext,
|
|
17
|
-
ResultWorkflowStepConfig,
|
|
18
|
-
ResultWorkflowStepRollbackOptions,
|
|
19
|
-
} from "./step/compat"
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { AnyTaggedError } from "better-result";
|
|
2
|
+
import { WorkflowStep } from "cloudflare:workers";
|
|
3
|
+
import { ErrCodec } from "../codec";
|
|
4
|
+
import { ResultDoFunction } from "./compat";
|
|
5
|
+
export declare function resultDo(ogDo: WorkflowStep["do"], getErrCodec: () => ErrCodec<AnyTaggedError>): ResultDoFunction;
|
package/dist/step/do.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file is part of better-result-cf-workflows.
|
|
3
|
+
* better-result-cf-workflows is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
4
|
+
* better-result-cf-workflows is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
5
|
+
* You should have received a copy of the GNU General Public License along with better-result-cf-workflows. If not, see <https://www.gnu.org/licenses/>.
|
|
6
|
+
*/
|
|
7
|
+
import { Panic, Result } from "better-result";
|
|
8
|
+
import { decodeErr, encodeErr } from "../codec";
|
|
9
|
+
import { NonRetryableError } from "cloudflare:workflows";
|
|
10
|
+
function transformRetries(codec, retryConfig) {
|
|
11
|
+
if (!retryConfig) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
// @ts-expect-error ts just does not recognize that the delay function im giving it es the real deal
|
|
15
|
+
return {
|
|
16
|
+
...retryConfig,
|
|
17
|
+
...(retryConfig.delay instanceof Function && {
|
|
18
|
+
delay: async (ctx) => {
|
|
19
|
+
return retryConfig.delay({
|
|
20
|
+
...ctx,
|
|
21
|
+
error: ctx.error && (await decodeErr(codec, ctx.error)),
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
}),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
async function wrapExecution(codec, retryConfig, cb) {
|
|
28
|
+
let res;
|
|
29
|
+
try {
|
|
30
|
+
res = await cb();
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
throw await encodeErr(codec, new Panic({ message: "Unknown error thrown during execution", cause: err }));
|
|
34
|
+
}
|
|
35
|
+
if (res.isErr()) {
|
|
36
|
+
throw await encodeErr(codec, res.error, !retryConfig?.shouldRetry || retryConfig.shouldRetry(res.error) ? Error : NonRetryableError);
|
|
37
|
+
}
|
|
38
|
+
return res.value;
|
|
39
|
+
}
|
|
40
|
+
export function resultDo(ogDo, getErrCodec) {
|
|
41
|
+
return async (name, config, rollback) => {
|
|
42
|
+
const errCodec = getErrCodec();
|
|
43
|
+
const cfConfig = {
|
|
44
|
+
...config,
|
|
45
|
+
retries: config.retries && transformRetries(errCodec, config.retries),
|
|
46
|
+
};
|
|
47
|
+
// @ts-expect-error cb causes failures
|
|
48
|
+
delete cfConfig["callback"];
|
|
49
|
+
try {
|
|
50
|
+
const resOk = await ogDo(name, cfConfig, async (ctx) => {
|
|
51
|
+
return await wrapExecution(errCodec, config.retries, async () => {
|
|
52
|
+
return config.callback(ctx);
|
|
53
|
+
});
|
|
54
|
+
}, rollback && {
|
|
55
|
+
...rollback,
|
|
56
|
+
rollback: async (inp) => {
|
|
57
|
+
return await wrapExecution(errCodec, config.retries, async () => {
|
|
58
|
+
return await rollback.rollback({
|
|
59
|
+
...inp,
|
|
60
|
+
error: await decodeErr(errCodec, inp.error),
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
rollbackConfig: {
|
|
65
|
+
...rollback.rollbackConfig,
|
|
66
|
+
retries: rollback.rollbackConfig?.retries &&
|
|
67
|
+
transformRetries(errCodec, rollback.rollbackConfig?.retries),
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
return Result.ok(resOk);
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
const parsed = await decodeErr(errCodec, err);
|
|
74
|
+
if (Panic.is(parsed)) {
|
|
75
|
+
throw parsed;
|
|
76
|
+
}
|
|
77
|
+
return Result.err(parsed);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { WorkflowStep, WorkflowStepEvent } from "cloudflare:workers";
|
|
2
|
+
import { ErrCodec } from "../codec";
|
|
3
|
+
import { AnyTaggedError, Result } from "better-result";
|
|
4
|
+
import { ResultDoFunction } from "./compat";
|
|
5
|
+
import { StepEventTimeoutErr } from "./wait-for-event";
|
|
6
|
+
export type ResultStep = Omit<WorkflowStep, "do" | "waitForEvent"> & {
|
|
7
|
+
do: ResultDoFunction;
|
|
8
|
+
waitForEvent(...args: Parameters<WorkflowStep["waitForEvent"]>): Promise<Result<WorkflowStepEvent<unknown>, StepEventTimeoutErr>>;
|
|
9
|
+
};
|
|
10
|
+
export declare function wrapStep(step: WorkflowStep, getErrCodec: () => ErrCodec<AnyTaggedError>): ResultStep;
|
|
@@ -4,24 +4,15 @@
|
|
|
4
4
|
* better-result-cf-workflows is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
5
5
|
* You should have received a copy of the GNU General Public License along with better-result-cf-workflows. If not, see <https://www.gnu.org/licenses/>.
|
|
6
6
|
*/
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
): ResultWaitForEventFunction {
|
|
19
|
-
return async (...args) => {
|
|
20
|
-
try {
|
|
21
|
-
const res = await ogWaitForEvent(...args)
|
|
22
|
-
return Result.ok(res)
|
|
23
|
-
} catch {
|
|
24
|
-
return Result.err(new StepEventTimeoutErr())
|
|
25
|
-
}
|
|
26
|
-
}
|
|
7
|
+
import { wrapStepWaitForEvent } from "./wait-for-event";
|
|
8
|
+
import { resultDo } from "./do";
|
|
9
|
+
export function wrapStep(step, getErrCodec) {
|
|
10
|
+
const ogDo = step.do;
|
|
11
|
+
const ogWaitForEvent = step.waitForEvent;
|
|
12
|
+
// @ts-expect-error we are wrapping this shit :)
|
|
13
|
+
step.do = resultDo(ogDo, getErrCodec);
|
|
14
|
+
// @ts-expect-error we are wrapping this shit :3
|
|
15
|
+
step.waitForEvent = wrapStepWaitForEvent(ogWaitForEvent);
|
|
16
|
+
// @ts-expect-error obviously ts wouldn't like this :3
|
|
17
|
+
return step;
|
|
27
18
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Result } from "better-result";
|
|
2
|
+
import { WorkflowStep, WorkflowStepEvent } from "cloudflare:workers";
|
|
3
|
+
declare const StepEventTimeoutErr_base: import("better-result").TaggedErrorClass<"StepEventTimeoutErr">;
|
|
4
|
+
export declare class StepEventTimeoutErr extends StepEventTimeoutErr_base {
|
|
5
|
+
}
|
|
6
|
+
export type ResultWaitForEventFunction = (...args: Parameters<WorkflowStep["waitForEvent"]>) => Promise<Result<WorkflowStepEvent<unknown>, StepEventTimeoutErr>>;
|
|
7
|
+
export declare function wrapStepWaitForEvent(ogWaitForEvent: WorkflowStep["waitForEvent"]): ResultWaitForEventFunction;
|
|
8
|
+
export {};
|
|
@@ -4,20 +4,17 @@
|
|
|
4
4
|
* better-result-cf-workflows is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
5
5
|
* You should have received a copy of the GNU General Public License along with better-result-cf-workflows. If not, see <https://www.gnu.org/licenses/>.
|
|
6
6
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
await env.TEST_WF.create({ id: wfId })
|
|
21
|
-
|
|
22
|
-
return instance
|
|
7
|
+
import { Result, TaggedError } from "better-result";
|
|
8
|
+
export class StepEventTimeoutErr extends TaggedError("StepEventTimeoutErr") {
|
|
9
|
+
}
|
|
10
|
+
export function wrapStepWaitForEvent(ogWaitForEvent) {
|
|
11
|
+
return async (...args) => {
|
|
12
|
+
try {
|
|
13
|
+
const res = await ogWaitForEvent(...args);
|
|
14
|
+
return Result.ok(res);
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return Result.err(new StepEventTimeoutErr());
|
|
18
|
+
}
|
|
19
|
+
};
|
|
23
20
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AnyTaggedError, Err, Panic, Result } from "better-result";
|
|
2
|
+
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from "cloudflare:workers";
|
|
3
|
+
import { ErrCodec } from "./codec";
|
|
4
|
+
import { ResultStep } from "./step";
|
|
5
|
+
export type ResultGenerator<T, E extends AnyTaggedError> = AsyncGenerator<Err<never, E>, Result<T, E>, unknown>;
|
|
6
|
+
export declare abstract class ResultWorkflowEntrypoint<SupportedErrors extends AnyTaggedError, FinalSuccess extends Rpc.Serializable<FinalSuccess> = Rpc.Serializable<unknown>, Env = unknown, T extends Rpc.Serializable<T> | unknown = unknown> extends WorkflowEntrypoint<Env, T> {
|
|
7
|
+
/** Since the Result class and tagged error classes are not RPC serializable, you need a result codec for returning data
|
|
8
|
+
* from steps. The provided errorMapCodec function will likely do you just fine!
|
|
9
|
+
*/
|
|
10
|
+
abstract errorCodec: ErrCodec<SupportedErrors>;
|
|
11
|
+
constructor(ctx: ExecutionContext, env: Env);
|
|
12
|
+
run(event: Readonly<WorkflowEvent<T>>, step: WorkflowStep): Promise<unknown>;
|
|
13
|
+
abstract execute(event: Readonly<WorkflowEvent<T>>, step: ResultStep): ResultGenerator<FinalSuccess, SupportedErrors>;
|
|
14
|
+
onFinalError(step: ResultStep, err: SupportedErrors | Panic): Promise<Error | null>;
|
|
15
|
+
onFinalSuccess(step: ResultStep, v: FinalSuccess): Promise<unknown>;
|
|
16
|
+
}
|
package/dist/wf-class.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file is part of better-result-cf-workflows.
|
|
3
|
+
* better-result-cf-workflows is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
4
|
+
* better-result-cf-workflows is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
5
|
+
* You should have received a copy of the GNU General Public License along with better-result-cf-workflows. If not, see <https://www.gnu.org/licenses/>.
|
|
6
|
+
*/
|
|
7
|
+
import { Panic, Result } from "better-result";
|
|
8
|
+
import { WorkflowEntrypoint } from "cloudflare:workers";
|
|
9
|
+
import { encodeErr } from "./codec";
|
|
10
|
+
import { wrapStep } from "./step";
|
|
11
|
+
export class ResultWorkflowEntrypoint extends WorkflowEntrypoint {
|
|
12
|
+
constructor(ctx, env) {
|
|
13
|
+
super(ctx, env);
|
|
14
|
+
}
|
|
15
|
+
async run(event, step) {
|
|
16
|
+
const resultStep = wrapStep(step, () => this.errorCodec);
|
|
17
|
+
let error;
|
|
18
|
+
try {
|
|
19
|
+
const res = await Result.gen(this.execute.bind(this, event, resultStep));
|
|
20
|
+
if (res.isOk()) {
|
|
21
|
+
return await this.onFinalSuccess(resultStep, res.value);
|
|
22
|
+
}
|
|
23
|
+
error = res.error;
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
if (Panic.is(err)) {
|
|
27
|
+
error = err;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
error = new Panic({ message: "Workflow implementation threw", cause: err });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const err = await this.onFinalError(resultStep, error);
|
|
34
|
+
if (err === null) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
// I cannot for the
|
|
38
|
+
throw await encodeErr(this.errorCodec, err);
|
|
39
|
+
}
|
|
40
|
+
async onFinalError(step, err) {
|
|
41
|
+
return err;
|
|
42
|
+
}
|
|
43
|
+
async onFinalSuccess(step, v) {
|
|
44
|
+
return v;
|
|
45
|
+
}
|
|
46
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "better-result-cf-workflows",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "better-result compatible Cloudflare Workflows",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"better-result",
|
|
@@ -9,8 +9,12 @@
|
|
|
9
9
|
],
|
|
10
10
|
"license": "GPL-3.0-only",
|
|
11
11
|
"author": "Lucy Dryaeva",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
12
15
|
"type": "module",
|
|
13
|
-
"main": "index.js",
|
|
16
|
+
"main": "dist/index.js",
|
|
17
|
+
"types": "dist/index.d.ts",
|
|
14
18
|
"dependencies": {},
|
|
15
19
|
"devDependencies": {
|
|
16
20
|
"@cloudflare/vitest-plugin": "^1.9.2",
|
|
@@ -51,6 +55,7 @@
|
|
|
51
55
|
"lint:check": "oxlint",
|
|
52
56
|
"lint:fix": "oxlint --fix",
|
|
53
57
|
"format:fix": "oxfmt",
|
|
54
|
-
"format:check": "oxfmt --check"
|
|
58
|
+
"format:check": "oxfmt --check",
|
|
59
|
+
"build": "tsc --build --noEmit false"
|
|
55
60
|
}
|
|
56
61
|
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
on:
|
|
2
|
-
workflow_call:
|
|
3
|
-
inputs:
|
|
4
|
-
runs-on:
|
|
5
|
-
type: string
|
|
6
|
-
|
|
7
|
-
jobs:
|
|
8
|
-
test:
|
|
9
|
-
runs-on: ${{ inputs.runs-on }}
|
|
10
|
-
steps:
|
|
11
|
-
- uses: https://code.forgejo.org/actions/checkout@v7
|
|
12
|
-
- uses: https://github.com/pnpm/setup@v3
|
|
13
|
-
with:
|
|
14
|
-
runtime: "node@26"
|
|
15
|
-
cache: true
|
|
16
|
-
- name: "Test!!!!!"
|
|
17
|
-
run: pnpm test
|
|
18
|
-
lint:
|
|
19
|
-
runs-on: ${{ inputs.runs-on }}
|
|
20
|
-
steps:
|
|
21
|
-
- uses: https://code.forgejo.org/actions/checkout@v7
|
|
22
|
-
- uses: https://github.com/pnpm/setup@v3
|
|
23
|
-
with:
|
|
24
|
-
runtime: "node@26"
|
|
25
|
-
cache: true
|
|
26
|
-
- name: "Lint!!"
|
|
27
|
-
run: pnpm lint:check
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
on:
|
|
2
|
-
push:
|
|
3
|
-
branches: [main]
|
|
4
|
-
|
|
5
|
-
jobs:
|
|
6
|
-
lint_test:
|
|
7
|
-
uses: ./.forgejo/workflows/lint_test.yaml
|
|
8
|
-
with:
|
|
9
|
-
# it is interesting... I would have though -tiny is enough,
|
|
10
|
-
# however I believe that -tiny was actually too small for a simple lint & test
|
|
11
|
-
# I can't tell if thats a really poor reflection on go's part,
|
|
12
|
-
# or if codeberg-tiny is truly so tiny that it can't run test & lint
|
|
13
|
-
runs-on: codeberg-small
|
package/.husky/pre-commit
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
pnpm lint-staged
|
package/.oxfmtrc.json
DELETED
package/.oxlintrc.json
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "./node_modules/oxlint/configuration_schema.json",
|
|
3
|
-
"jsPlugins": [
|
|
4
|
-
{
|
|
5
|
-
"name": "simple-header",
|
|
6
|
-
"specifier": "eslint-plugin-simple-header"
|
|
7
|
-
}
|
|
8
|
-
],
|
|
9
|
-
"rules": {
|
|
10
|
-
"typescript/explicit-function-return-type": [
|
|
11
|
-
"error",
|
|
12
|
-
{
|
|
13
|
-
"allowIIFEs": true
|
|
14
|
-
}
|
|
15
|
-
],
|
|
16
|
-
"require-yield": "off",
|
|
17
|
-
"simple-header/header": [
|
|
18
|
-
"error",
|
|
19
|
-
{
|
|
20
|
-
"text": [
|
|
21
|
-
"This file is part of better-result-cf-workflows.",
|
|
22
|
-
"better-result-cf-workflows is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.",
|
|
23
|
-
"better-result-cf-workflows is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.",
|
|
24
|
-
"You should have received a copy of the GNU General Public License along with better-result-cf-workflows. If not, see <https://www.gnu.org/licenses/>."
|
|
25
|
-
]
|
|
26
|
-
}
|
|
27
|
-
]
|
|
28
|
-
}
|
|
29
|
-
}
|
package/.vscode/settings.json
DELETED
package/CONTRIBUTING.md
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
# Contributing
|
|
2
|
-
|
|
3
|
-
This project is managed by [pnpm](https://pnpm.io/). When opening a PR
|
|
4
|
-
|
|
5
|
-
## PR Requirements
|
|
6
|
-
|
|
7
|
-
- Please keep PRs small
|
|
8
|
-
- For any new bug fix, you need a test.
|
|
9
|
-
- Try to ensure forward compatibility with CF Workflows
|
|
10
|
-
- For new features, please have an accepted issue first!
|
|
11
|
-
|
|
12
|
-
## AI Use
|
|
13
|
-
|
|
14
|
-
No AI was used in any of this code. However, that does not mean AI contributing will be automatically rejected - I can't tell with 100% accuracy if something is or is not AI. The only exception is if a PR was clearly vibe coded, or it is actual slop. I will reject those, even if the features are "useful"
|
package/pnpm-workspace.yaml
DELETED