Skip to content

Commit 5af9139

Browse files
committed
Update internal impl, use name Module
1 parent efe9952 commit 5af9139

File tree

8 files changed

+92
-132
lines changed

8 files changed

+92
-132
lines changed

examples/package.ts renamed to examples/module.ts

+12-15
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,20 @@ async function main() {
3939
// });
4040

4141
// using the module from JSON
42-
const mod = new Module(
43-
{
44-
json: substrate.module.serialize({
45-
nodes: [a, b],
46-
inputs: { x, y, z },
47-
}),
48-
inputs: {
49-
// when commented will use "hello" because it is defined as the default above
50-
// x: "xxx",
51-
y: "yyy",
52-
z: {
53-
arr: ["123"],
54-
},
42+
const mod = new Module({
43+
module_json: substrate.module.serialize({
44+
nodes: [a, b],
45+
inputs: { x, y, z },
46+
}),
47+
inputs: {
48+
// when commented will use "hello" because it is defined as the default above
49+
// x: 123,
50+
y: "yyy",
51+
z: {
52+
arr: ["123"],
5553
},
5654
},
57-
{ id: "Module" },
58-
);
55+
});
5956

6057
// using the module from publication/module id
6158
// const mod = new Module({

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"vitest": "^1.0.4"
4444
},
4545
"dependencies": {
46+
"@types/json-schema": "^7.0.15",
4647
"@types/node-fetch": "^2.6.11",
4748
"node-fetch": "2.7.0",
4849
"pako": "^2.1.0"

src/Future.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { idGenerator } from "substrate/idGenerator";
22
import { Node } from "substrate/Node";
3+
import { type JSONSchema7 } from "json-schema";
34

45
type Accessor = "item" | "attr";
56
type TraceOperation = {
@@ -123,7 +124,7 @@ export class JQ extends Directive {
123124
rawValue: (val: JQCompatible) => ({ future_id: null, val }),
124125
};
125126

126-
override next(...items: TraceProp[]) {
127+
override next(..._items: TraceProp[]) {
127128
return new JQ(this.query, this.target);
128129
}
129130

@@ -318,7 +319,7 @@ export class FutureAnyObject extends Future<Object> {
318319
}
319320

320321
export class Input extends Directive {
321-
items: any[];
322+
items: any[]; // NOTE: unused field (will remove this from direcitve in a later refactor)
322323
name: string | null;
323324

324325
constructor(items: any[]) {
@@ -342,13 +343,11 @@ export class Input extends Directive {
342343
}
343344
}
344345

345-
type JSONSchema = any;
346-
347346
export class FutureInput extends Future<any> {
348347
declare _directive: Input;
349-
schema: JSONSchema;
348+
schema: JSONSchema7;
350349

351-
constructor(schema?: JSONSchema, id: string = newInputId()) {
350+
constructor(schema?: JSONSchema7, id: string = newInputId()) {
352351
super(new Input([]), id);
353352
this.schema = schema ?? {};
354353
}

src/Module.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Node, Options } from "substrate/Node";
2+
import { FutureInput } from "substrate/Future";
3+
4+
export type ModuleInputs = Record<string, FutureInput>;
5+
6+
type ModuleIn =
7+
| {
8+
module_json: any;
9+
inputs: Record<string, any>;
10+
}
11+
| {
12+
module_id: any;
13+
inputs: Record<string, any>;
14+
}
15+
| {
16+
module_uri: any;
17+
inputs: Record<string, any>;
18+
};
19+
20+
export class Module extends Node {
21+
constructor(args: ModuleIn, options?: Options) {
22+
super(args, options);
23+
this.node = "Module";
24+
}
25+
}

src/Package.ts

-100
This file was deleted.

src/Substrate.ts

+42-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Future } from "substrate/Future";
88
import { getPlatformProperties } from "substrate/Platform";
99
import { deflate } from "pako";
1010
import { randomString } from "substrate/idGenerator";
11-
import { packager } from "./Package";
11+
import { ModuleInputs } from "./Module";
1212

1313
type Configuration = {
1414
/**
@@ -179,8 +179,6 @@ export class Substrate {
179179
}
180180
}
181181

182-
createPackageable = packager(this);
183-
184182
/**
185183
* Return a set of all nodes and their dependent nodes.
186184
*/
@@ -296,14 +294,48 @@ export class Substrate {
296294
}
297295

298296
module = {
299-
serialize: ({ nodes, inputs }: { nodes: Node[], inputs: any }) => {
300-
let p = packager(this);
301-
return p(nodes, inputs).toJSON();
297+
/**
298+
* Returns an object that represents a publishable "module" or code that can be used to construct
299+
* a `Module` node.
300+
*/
301+
serialize: ({ nodes, inputs }: { nodes: Node[]; inputs: ModuleInputs }) => {
302+
const inputIdToName = {};
303+
const inputNameToSchema = {};
304+
305+
for (let name in inputs) {
306+
let input = inputs[name];
307+
// @ts-ignore
308+
inputIdToName[input._id] = name;
309+
// @ts-ignore
310+
inputNameToSchema[name] = input.schema;
311+
}
312+
313+
const dag = Substrate.serialize(...nodes);
314+
315+
// update variable name bindings in dag using inputs
316+
dag.futures = dag.futures.map((future: any) => {
317+
if (future.directive.type === "input" && !future.directive.name) {
318+
// @ts-ignore
319+
future.directive.name = inputIdToName[future.id];
320+
}
321+
return future;
322+
});
323+
324+
return {
325+
dag,
326+
inputs: inputNameToSchema,
327+
api_version: this.apiVersion,
328+
};
302329
},
303330

304-
publish: async (publishable: any) => {
331+
/**
332+
* Publishes a module on substrate.run
333+
* A successful response will contain the module id and web uri
334+
*/
335+
publish: async (_publishable: any) => {
305336
console.log("not implemented yet");
306-
return publishable;
307-
}
308-
}
337+
let publication;
338+
return publication;
339+
},
340+
};
309341
}

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export {
5151
DeleteVectors,
5252
} from "substrate/Nodes";
5353

54-
export { Package, Package as Module } from "substrate/Package";
54+
export { Module } from "substrate/Module";
5555

5656
export { sb } from "substrate/sb";
5757
export { Substrate };

0 commit comments

Comments
 (0)