@toolproof-npm/shared 0.1.100 → 0.1.102

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 CHANGED
@@ -1,56 +1,56 @@
1
- # @toolproof-npm/shared
2
-
3
- Core library utilities for ToolProof.
4
-
5
- ## Installation
6
-
7
- ```bash
8
- pnpm add @toolproof-npm/shared
9
- # or
10
- npm install @toolproof-npm/shared
11
- # or
12
- yarn add @toolproof-npm/shared
13
- ```
14
-
15
- ## Usage
16
-
17
- ### Basic Import
18
-
19
- ```typescript
20
- import { /* your exports */ } from '@toolproof-npm/shared';
21
- ```
22
-
23
- ### Constants
24
-
25
- ```typescript
26
- import { /* constants */ } from '@toolproof-npm/shared/constants';
27
- ```
28
-
29
- ### Types
30
-
31
- ```typescript
32
- import { /* types */ } from '@toolproof-npm/shared/types';
33
- ```
34
-
35
- ### Server Utilities (Node.js only)
36
-
37
- ```typescript
38
- import { /* server utilities */ } from '@toolproof-npm/shared/server';
39
- ```
40
-
41
- ## Features
42
-
43
- - TypeScript support with full type definitions
44
- - ESM module format
45
- - Tree-shakeable exports
46
- - Server-side utilities for Firestore admin operations
47
-
48
- ## Requirements
49
-
50
- - Node.js 16+
51
- - TypeScript 4.5+ (for TypeScript projects)
52
-
53
- ## License
54
-
55
- MIT
56
-
1
+ # @toolproof-npm/shared
2
+
3
+ Core library utilities for ToolProof.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @toolproof-npm/shared
9
+ # or
10
+ npm install @toolproof-npm/shared
11
+ # or
12
+ yarn add @toolproof-npm/shared
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Basic Import
18
+
19
+ ```typescript
20
+ import { /* your exports */ } from '@toolproof-npm/shared';
21
+ ```
22
+
23
+ ### Constants
24
+
25
+ ```typescript
26
+ import { /* constants */ } from '@toolproof-npm/shared/constants';
27
+ ```
28
+
29
+ ### Types
30
+
31
+ ```typescript
32
+ import { /* types */ } from '@toolproof-npm/shared/types';
33
+ ```
34
+
35
+ ### Server Utilities (Node.js only)
36
+
37
+ ```typescript
38
+ import { /* server utilities */ } from '@toolproof-npm/shared/server';
39
+ ```
40
+
41
+ ## Features
42
+
43
+ - TypeScript support with full type definitions
44
+ - ESM module format
45
+ - Tree-shakeable exports
46
+ - Server-side utilities for Firestore admin operations
47
+
48
+ ## Requirements
49
+
50
+ - Node.js 16+
51
+ - TypeScript 4.5+ (for TypeScript projects)
52
+
53
+ ## License
54
+
55
+ MIT
56
+
@@ -0,0 +1,3 @@
1
+ import type { WorkStepJson, WhileStepJson, ForStepJson, JobJson } from '@toolproof-npm/schema';
2
+ export declare const makeWorkStepFromJob: (job: JobJson) => Promise<WorkStepJson>;
3
+ export declare const makeLoopStepFromJob: (job: JobJson, whenJob: JobJson, kind: "for" | "while") => Promise<ForStepJson | WhileStepJson>;
@@ -0,0 +1,58 @@
1
+ import { CONSTANTS } from '@toolproof-npm/shared/constants';
2
+ import { getNewIdentity } from '@toolproof-npm/shared/server';
3
+ // DOC: Helper function to construct an Execution object from a job
4
+ const makeExecutionFromJob = async (job, executionRef) => {
5
+ const execIdentity = executionRef ?? await getNewIdentity(CONSTANTS.TERMINALS.execution);
6
+ const inputBindingMap = {};
7
+ const inputs = job.roles.inputMap;
8
+ await Promise.all(Object.keys(inputs).map(async (resourceRoleRef) => {
9
+ const resourceId = await getNewIdentity(CONSTANTS.TERMINALS.resource);
10
+ inputBindingMap[resourceRoleRef] = resourceId;
11
+ }));
12
+ const outputBindingMap = {};
13
+ const outputs = job.roles.outputMap;
14
+ await Promise.all(Object.keys(outputs).map(async (resourceRoleRef) => {
15
+ const resourceId = await getNewIdentity(CONSTANTS.TERMINALS.resource);
16
+ outputBindingMap[resourceRoleRef] = resourceId;
17
+ }));
18
+ return {
19
+ identity: execIdentity,
20
+ jobRef: job.identity,
21
+ roleBindings: {
22
+ inputBindingMap,
23
+ outputBindingMap
24
+ }
25
+ };
26
+ };
27
+ // DOC: Helper function to construct a WorkStep object from an jobMetaJson (does not append to statelessStrategy.steps)
28
+ export const makeWorkStepFromJob = async (job) => {
29
+ const workStepIdentity = await getNewIdentity(CONSTANTS.STEPS.work);
30
+ const executionRef = workStepIdentity.replace(CONSTANTS.STEPS.work.toUpperCase(), 'execution'.toUpperCase()); // ATTENTION: use function
31
+ const execution = await makeExecutionFromJob(job, executionRef);
32
+ const newWorkStep = {
33
+ identity: workStepIdentity,
34
+ kind: CONSTANTS.STEPS.work,
35
+ execution
36
+ };
37
+ return newWorkStep;
38
+ };
39
+ // DOC: Helper function to construct a ForStep or WhileStep from a job
40
+ export const makeLoopStepFromJob = async (job, whenJob, kind) => {
41
+ // Create the "what" WorkStep from the provided job
42
+ const whatWorkStep = await makeWorkStepFromJob(job);
43
+ // Create the "when" WorkStep from the LessThan job
44
+ const whenWorkStep = await makeWorkStepFromJob(whenJob);
45
+ // Generate loop step identity based on kind
46
+ const stepIdentity = (kind === 'for'
47
+ ? await getNewIdentity(CONSTANTS.STEPS.for)
48
+ : await getNewIdentity(CONSTANTS.STEPS.while));
49
+ // Assemble the loop step
50
+ return {
51
+ identity: stepIdentity,
52
+ kind,
53
+ case: {
54
+ what: whatWorkStep,
55
+ when: whenWorkStep
56
+ }
57
+ };
58
+ };
@@ -0,0 +1,21 @@
1
+ import type { CreationContextJson, ResourceJson, ResourceMissingJson, ResourcePotentialOutputJson, StrategyStateJson } from '@toolproof-npm/schema';
2
+ export type ResolveResult = {
3
+ status: 'materialized';
4
+ entry: ResourceJson;
5
+ path: CreationContextJson[];
6
+ } | {
7
+ status: 'missing';
8
+ entry: ResourceMissingJson;
9
+ path: CreationContextJson[];
10
+ } | {
11
+ status: 'potential-output';
12
+ entry: ResourcePotentialOutputJson;
13
+ path: CreationContextJson[];
14
+ } | {
15
+ status: 'unresolved';
16
+ reason: 'not-found' | 'cycle' | 'depth-exceeded';
17
+ path: CreationContextJson[];
18
+ };
19
+ export declare function resolveResourceChain(strategyState: StrategyStateJson, start: CreationContextJson, opts?: {
20
+ maxDepth?: number;
21
+ }): ResolveResult;
@@ -0,0 +1,40 @@
1
+ export function resolveResourceChain(strategyState, start, opts) {
2
+ const maxDepth = opts?.maxDepth ?? 50;
3
+ const visited = new Set();
4
+ const path = [];
5
+ let current = start;
6
+ for (let depth = 0; depth <= maxDepth; depth++) {
7
+ path.push(current);
8
+ const visitKey = `${current.executionRef}::${current.resourceRoleRef}`;
9
+ if (visited.has(visitKey)) {
10
+ return { status: 'unresolved', reason: 'cycle', path };
11
+ }
12
+ visited.add(visitKey);
13
+ const bucket = strategyState[current.executionRef];
14
+ if (!bucket)
15
+ return { status: 'unresolved', reason: 'not-found', path };
16
+ const entry = bucket[current.resourceRoleRef];
17
+ if (!entry)
18
+ return { status: 'unresolved', reason: 'not-found', path };
19
+ if (entry.kind === 'materialized') {
20
+ return { status: 'materialized', entry: entry, path };
21
+ }
22
+ if (entry.kind === 'missing') {
23
+ return { status: 'missing', entry: entry, path };
24
+ }
25
+ if (entry.kind === 'potential-output') {
26
+ return { status: 'potential-output', entry: entry, path };
27
+ }
28
+ // potential-input: follow ref backwards
29
+ if (entry.kind === 'potential-input') {
30
+ const rpi = entry.creationContext;
31
+ if (!rpi)
32
+ return { status: 'unresolved', reason: 'not-found', path };
33
+ current = rpi;
34
+ continue;
35
+ }
36
+ // Unknown case
37
+ return { status: 'unresolved', reason: 'not-found', path };
38
+ }
39
+ return { status: 'unresolved', reason: 'depth-exceeded', path };
40
+ }
@@ -0,0 +1,7 @@
1
+ import type { ExecutionIdentityJson, ResourceJson, ResourceRoleIdentityJson, StatefulStrategyJson } from '@toolproof-npm/schema';
2
+ export declare function bindInputRes(statefulStrategy: StatefulStrategyJson, executionRef: ExecutionIdentityJson, resourceRoleIdentity: ResourceRoleIdentityJson, resource: ResourceJson): StatefulStrategyJson;
3
+ export declare function bindInputRef(statefulStrategy: StatefulStrategyJson, targetExecRef: ExecutionIdentityJson, resourceRoleIdentity: ResourceRoleIdentityJson, source: {
4
+ executionRef: ExecutionIdentityJson;
5
+ resourceRoleRef: ResourceRoleIdentityJson;
6
+ }): StatefulStrategyJson;
7
+ export declare function clearInputBinding(statefulStrategy: StatefulStrategyJson, execRef: ExecutionIdentityJson, resourceRoleRef: ResourceRoleIdentityJson): StatefulStrategyJson;
@@ -0,0 +1,87 @@
1
+ import { resolveResourceChain } from './resourceChain.js';
2
+ export function bindInputRes(statefulStrategy, executionRef, resourceRoleIdentity, resource) {
3
+ const strategyState = statefulStrategy.strategyState;
4
+ const bucket = strategyState[executionRef] ?? {};
5
+ return {
6
+ ...statefulStrategy,
7
+ strategyState: {
8
+ ...strategyState,
9
+ [executionRef]: {
10
+ ...bucket,
11
+ [resourceRoleIdentity]: resource,
12
+ },
13
+ },
14
+ };
15
+ }
16
+ export function bindInputRef(statefulStrategy, targetExecRef, resourceRoleIdentity, source) {
17
+ const strategyState = statefulStrategy.strategyState;
18
+ const sourceEntry = strategyState?.[source.executionRef]?.[source.resourceRoleRef];
19
+ if (!sourceEntry)
20
+ throw new Error(`resourceEntry not found for source (${source.executionRef}, ${source.resourceRoleRef})`);
21
+ const bucket = strategyState[targetExecRef] ?? {};
22
+ const result = resolveResourceChain(strategyState, { executionRef: source.executionRef, resourceRoleRef: source.resourceRoleRef });
23
+ if (result.status === 'materialized') {
24
+ return {
25
+ ...statefulStrategy,
26
+ strategyState: {
27
+ ...strategyState,
28
+ [targetExecRef]: {
29
+ ...bucket,
30
+ [resourceRoleIdentity]: result.entry,
31
+ },
32
+ },
33
+ };
34
+ }
35
+ if (result.status === 'missing') {
36
+ const missing = result.entry;
37
+ const reusedMissing = {
38
+ identity: missing.identity,
39
+ resourceTypeRef: missing.resourceTypeRef,
40
+ kind: 'missing',
41
+ };
42
+ return {
43
+ ...statefulStrategy,
44
+ strategyState: {
45
+ ...strategyState,
46
+ [targetExecRef]: {
47
+ ...bucket,
48
+ [resourceRoleIdentity]: reusedMissing,
49
+ },
50
+ },
51
+ };
52
+ }
53
+ if (result.status === 'potential-output') {
54
+ const potentialInput = {
55
+ identity: sourceEntry.identity,
56
+ resourceTypeRef: sourceEntry.resourceTypeRef,
57
+ creationContext: { executionRef: source.executionRef, resourceRoleRef: source.resourceRoleRef },
58
+ kind: 'potential-input',
59
+ };
60
+ return {
61
+ ...statefulStrategy,
62
+ strategyState: {
63
+ ...strategyState,
64
+ [targetExecRef]: {
65
+ ...bucket,
66
+ [resourceRoleIdentity]: potentialInput,
67
+ },
68
+ },
69
+ };
70
+ }
71
+ throw new Error(`Failed to resolve resource chain: ${result.reason}`);
72
+ }
73
+ export function clearInputBinding(statefulStrategy, execRef, resourceRoleRef) {
74
+ const strategyState = statefulStrategy.strategyState;
75
+ const bucket = strategyState?.[execRef];
76
+ if (!bucket?.[resourceRoleRef])
77
+ return statefulStrategy;
78
+ const nextBucket = { ...bucket };
79
+ delete nextBucket[resourceRoleRef];
80
+ return {
81
+ ...statefulStrategy,
82
+ strategyState: {
83
+ ...strategyState,
84
+ [execRef]: nextBucket,
85
+ },
86
+ };
87
+ }
@@ -1,24 +1,4 @@
1
- import type { ResourceIdentityJson, ResourceTypeIdentityJson, ResourceJson, ResourceMissingJson, JobJson, ResourcePotentialOutputJson, StrategyStateJson, CreationContextJson } from '@toolproof-npm/schema';
1
+ import type { ResourceIdentityJson, ResourceTypeIdentityJson, ResourceJson, JobJson } from '@toolproof-npm/schema';
2
2
  import type { ResourceMap } from '../types.js';
3
3
  export declare function extractResourcesByType(resourceMap: ResourceMap, resourceTypeRef: ResourceTypeIdentityJson): Record<ResourceIdentityJson, ResourceJson>;
4
4
  export declare function extractJobMap(resourceMap: ResourceMap): Map<`JOB-${string}`, JobJson>;
5
- export type ResolveResult = {
6
- status: 'materialized';
7
- entry: ResourceJson;
8
- path: CreationContextJson[];
9
- } | {
10
- status: 'missing';
11
- entry: ResourceMissingJson;
12
- path: CreationContextJson[];
13
- } | {
14
- status: 'potential-output';
15
- entry: ResourcePotentialOutputJson;
16
- path: CreationContextJson[];
17
- } | {
18
- status: 'unresolved';
19
- reason: 'not-found' | 'cycle' | 'depth-exceeded';
20
- path: CreationContextJson[];
21
- };
22
- export declare function resolveResourceChain(strategyState: StrategyStateJson, start: CreationContextJson, opts?: {
23
- maxDepth?: number;
24
- }): ResolveResult;
@@ -16,43 +16,3 @@ export function extractJobMap(resourceMap) {
16
16
  });
17
17
  return jobMap;
18
18
  }
19
- export function resolveResourceChain(strategyState, start, opts) {
20
- const maxDepth = opts?.maxDepth ?? 50;
21
- const visited = new Set();
22
- const path = [];
23
- let current = start;
24
- for (let depth = 0; depth <= maxDepth; depth++) {
25
- path.push(current);
26
- const visitKey = `${current.executionRef}::${current.resourceRoleRef}`;
27
- if (visited.has(visitKey)) {
28
- return { status: 'unresolved', reason: 'cycle', path };
29
- }
30
- visited.add(visitKey);
31
- const bucket = strategyState[current.executionRef];
32
- if (!bucket)
33
- return { status: 'unresolved', reason: 'not-found', path };
34
- const entry = bucket[current.resourceRoleRef];
35
- if (!entry)
36
- return { status: 'unresolved', reason: 'not-found', path };
37
- if (entry.kind === 'materialized') {
38
- return { status: 'materialized', entry: entry, path };
39
- }
40
- if (entry.kind === 'missing') {
41
- return { status: 'missing', entry: entry, path };
42
- }
43
- if (entry.kind === 'potential-output') {
44
- return { status: 'potential-output', entry: entry, path };
45
- }
46
- // potential-input: follow ref backwards
47
- if (entry.kind === 'potential-input') {
48
- const rpi = entry.creationContext;
49
- if (!rpi)
50
- return { status: 'unresolved', reason: 'not-found', path };
51
- current = rpi;
52
- continue;
53
- }
54
- // Unknown case
55
- return { status: 'unresolved', reason: 'not-found', path };
56
- }
57
- return { status: 'unresolved', reason: 'depth-exceeded', path };
58
- }
@@ -48,6 +48,7 @@ export declare const CONSTANTS: {
48
48
  readonly TYPE_StatelessStrategy: "TYPE-StatelessStrategy";
49
49
  readonly TYPE_StatefulStrategy: "TYPE-StatefulStrategy";
50
50
  readonly ROLE_Manual: "ROLE-Manual";
51
+ readonly ROLE_Iteration: "ROLE-Iteration";
51
52
  readonly ROLE_ErrorOutput: "ROLE-ErrorOutput";
52
53
  readonly ROLE_DynamicSource: "ROLE-u4vW7PDlX6V9IGJoNxhl";
53
54
  readonly ROLE_StaticTarget: "ROLE-xLaQo8L1fMxKTQUJcaJn";
package/dist/constants.js CHANGED
@@ -48,6 +48,7 @@ export const CONSTANTS = {
48
48
  TYPE_StatelessStrategy: 'TYPE-StatelessStrategy',
49
49
  TYPE_StatefulStrategy: 'TYPE-StatefulStrategy',
50
50
  ROLE_Manual: 'ROLE-Manual',
51
+ ROLE_Iteration: 'ROLE-Iteration',
51
52
  ROLE_ErrorOutput: 'ROLE-ErrorOutput',
52
53
  ROLE_DynamicSource: 'ROLE-u4vW7PDlX6V9IGJoNxhl',
53
54
  ROLE_StaticTarget: 'ROLE-xLaQo8L1fMxKTQUJcaJn',
package/dist/index.d.ts CHANGED
@@ -3,5 +3,7 @@ export * as TYPES from './_lib/types.js';
3
3
  export * as UTILS from './_lib/utils/utils.js';
4
4
  export * as RESOURCE_CREATION from './_lib/utils/resourceCreation.js';
5
5
  export * as STEP_CREATION from './_lib/utils/stepCreation.js';
6
+ export * as ROLE_RESOURCE_BINDINGS from './_lib/utils/roleResourceBinding.js';
7
+ export * as RESOURCE_CHAIN from './_lib/utils/resourceChain.js';
6
8
  export * from './firebaseAdminHelpers.js';
7
9
  export * from './firebaseAdminInit.js';
package/dist/index.js CHANGED
@@ -3,5 +3,7 @@ export * as TYPES from './_lib/types.js';
3
3
  export * as UTILS from './_lib/utils/utils.js';
4
4
  export * as RESOURCE_CREATION from './_lib/utils/resourceCreation.js';
5
5
  export * as STEP_CREATION from './_lib/utils/stepCreation.js';
6
+ export * as ROLE_RESOURCE_BINDINGS from './_lib/utils/roleResourceBinding.js';
7
+ export * as RESOURCE_CHAIN from './_lib/utils/resourceChain.js';
6
8
  export * from './firebaseAdminHelpers.js';
7
9
  export * from './firebaseAdminInit.js';
package/package.json CHANGED
@@ -1,65 +1,65 @@
1
1
  {
2
- "name": "@toolproof-npm/shared",
3
- "version": "0.1.100",
4
- "description": "Core library utilities for ToolProof",
5
- "keywords": [
6
- "toolproof",
7
- "utilities",
8
- "library"
9
- ],
10
- "author": "ToolProof Team",
11
- "license": "MIT",
12
- "repository": {
13
- "type": "git",
14
- "url": "https://github.com/ToolProof/core.git",
15
- "directory": "packages/shared"
2
+ "name": "@toolproof-npm/shared",
3
+ "version": "0.1.102",
4
+ "description": "Core library utilities for ToolProof",
5
+ "keywords": [
6
+ "toolproof",
7
+ "utilities",
8
+ "library"
9
+ ],
10
+ "author": "ToolProof Team",
11
+ "license": "MIT",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/ToolProof/core.git",
15
+ "directory": "packages/shared"
16
+ },
17
+ "homepage": "https://github.com/ToolProof/core#readme",
18
+ "bugs": {
19
+ "url": "https://github.com/ToolProof/core/issues"
20
+ },
21
+ "type": "module",
22
+ "main": "dist/index.js",
23
+ "types": "dist/index.d.ts",
24
+ "sideEffects": false,
25
+ "exports": {
26
+ ".": {
27
+ "import": "./dist/index.js",
28
+ "types": "./dist/index.d.ts"
16
29
  },
17
- "homepage": "https://github.com/ToolProof/core#readme",
18
- "bugs": {
19
- "url": "https://github.com/ToolProof/core/issues"
30
+ "./constants": {
31
+ "import": "./dist/constants.js",
32
+ "types": "./dist/constants.d.ts"
20
33
  },
21
- "type": "module",
22
- "main": "dist/index.js",
23
- "types": "dist/index.d.ts",
24
- "sideEffects": false,
25
- "exports": {
26
- ".": {
27
- "import": "./dist/index.js",
28
- "types": "./dist/index.d.ts"
29
- },
30
- "./constants": {
31
- "import": "./dist/constants.js",
32
- "types": "./dist/constants.d.ts"
33
- },
34
- "./types": {
35
- "import": "./dist/_lib/types.js",
36
- "types": "./dist/_lib/types.d.ts"
37
- },
38
- "./utils": {
39
- "import": "./dist/_lib/utils/utils.js",
40
- "types": "./dist/_lib/utils/utils.d.ts"
41
- },
42
- "./server": {
43
- "node": "./dist/firebaseAdminHelpers.js",
44
- "types": "./dist/firebaseAdminHelpers.d.ts"
45
- }
34
+ "./types": {
35
+ "import": "./dist/_lib/types.js",
36
+ "types": "./dist/_lib/types.d.ts"
46
37
  },
47
- "scripts": {
48
- "build": "tsc -b"
38
+ "./utils": {
39
+ "import": "./dist/_lib/utils/utils.js",
40
+ "types": "./dist/_lib/utils/utils.d.ts"
49
41
  },
50
- "files": [
51
- "dist",
52
- "README.md"
53
- ],
54
- "devDependencies": {
55
- "@google-cloud/storage": "^7.17.3",
56
- "@types/node": "^20.19.25",
57
- "json-schema-to-typescript": "^15.0.4",
58
- "ts-node": "^10.9.2",
59
- "typescript": "^5.9.3"
60
- },
61
- "dependencies": {
62
- "@toolproof-npm/schema": "^0.1.68",
63
- "firebase-admin": "^13.6.0"
42
+ "./server": {
43
+ "node": "./dist/firebaseAdminHelpers.js",
44
+ "types": "./dist/firebaseAdminHelpers.d.ts"
64
45
  }
65
- }
46
+ },
47
+ "files": [
48
+ "dist",
49
+ "README.md"
50
+ ],
51
+ "devDependencies": {
52
+ "@google-cloud/storage": "^7.17.3",
53
+ "@types/node": "^20.19.25",
54
+ "json-schema-to-typescript": "^15.0.4",
55
+ "ts-node": "^10.9.2",
56
+ "typescript": "^5.9.3"
57
+ },
58
+ "dependencies": {
59
+ "@toolproof-npm/schema": "^0.1.68",
60
+ "firebase-admin": "^13.6.0"
61
+ },
62
+ "scripts": {
63
+ "build": "tsc -b"
64
+ }
65
+ }