@chainlink/cre-sdk 1.5.0-alpha.3 → 1.5.0
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 +7 -1
- package/bin/cre-compile.ts +34 -17
- package/package.json +3 -3
- package/scripts/run-standard-tests.sh +3 -3
- package/scripts/run.ts +5 -1
- package/scripts/src/compile-cli-args.test.ts +32 -0
- package/scripts/src/compile-cli-args.ts +35 -0
- package/scripts/src/compile-to-js.test.ts +90 -0
- package/scripts/src/compile-to-js.ts +46 -7
- package/scripts/src/compile-to-wasm.ts +18 -32
- package/scripts/src/compile-workflow.ts +55 -27
- package/scripts/src/generate-chain-selectors.ts +9 -27
- package/scripts/src/typecheck-workflow.test.ts +77 -0
- package/scripts/src/typecheck-workflow.ts +96 -0
package/README.md
CHANGED
|
@@ -58,10 +58,16 @@ CRE workflows are compiled to WASM and executed through Javy (QuickJS). This is
|
|
|
58
58
|
|
|
59
59
|
- Node built-ins like `node:fs`, `node:crypto`, `node:http`, `node:net`, etc. are not supported in workflows.
|
|
60
60
|
- Browser globals like `fetch`, `window`, and `setTimeout` are also not available in workflow runtime.
|
|
61
|
-
- `cre compile:workflow` / `cre-compile` now validates workflow source and fails fast when unsupported APIs are used.
|
|
61
|
+
- `cre compile:workflow` / `cre-compile` now typechecks your workflow project first (using your nearest `tsconfig.json`), then validates workflow source and fails fast when unsupported APIs are used.
|
|
62
62
|
|
|
63
63
|
Use CRE capabilities (for example, `cre.capabilities.HTTPClient`) instead of direct Node/browser APIs.
|
|
64
64
|
|
|
65
|
+
If you need to compile despite TypeScript errors, pass `--skip-type-checks`:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
bun x cre-compile src/workflow.ts dist/workflow.wasm --skip-type-checks
|
|
69
|
+
```
|
|
70
|
+
|
|
65
71
|
## Getting Started
|
|
66
72
|
|
|
67
73
|
We recommend you consult the [getting started docs](https://docs.chain.link/cre/getting-started/cli-installation) and install the CRE CLI.
|
package/bin/cre-compile.ts
CHANGED
|
@@ -1,38 +1,55 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
|
|
3
3
|
import { main as compileWorkflow } from "../scripts/src/compile-workflow";
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
parseCompileCliArgs,
|
|
6
|
+
skipTypeChecksFlag,
|
|
7
|
+
} from "../scripts/src/compile-cli-args";
|
|
8
|
+
import { WorkflowTypecheckError } from "../scripts/src/typecheck-workflow";
|
|
5
9
|
import { WorkflowRuntimeCompatibilityError } from "../scripts/src/validate-workflow-runtime-compat";
|
|
6
10
|
|
|
7
11
|
const main = async () => {
|
|
8
|
-
|
|
9
|
-
|
|
12
|
+
let inputPath: string | undefined;
|
|
13
|
+
let outputPathArg: string | undefined;
|
|
14
|
+
let skipTypeChecks = false;
|
|
10
15
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
try {
|
|
17
|
+
const parsed = parseCompileCliArgs(process.argv.slice(2));
|
|
18
|
+
inputPath = parsed.inputPath;
|
|
19
|
+
outputPathArg = parsed.outputPath;
|
|
20
|
+
skipTypeChecks = parsed.skipTypeChecks;
|
|
21
|
+
} catch (error) {
|
|
22
|
+
console.error(error instanceof Error ? error.message : error);
|
|
15
23
|
console.error(
|
|
16
|
-
|
|
24
|
+
`Usage: cre-compile <path/to/workflow.ts> [path/to/output.wasm] [${skipTypeChecksFlag}]`,
|
|
17
25
|
);
|
|
18
26
|
process.exit(1);
|
|
19
27
|
}
|
|
20
28
|
|
|
21
|
-
if (
|
|
22
|
-
console.error(
|
|
29
|
+
if (!inputPath) {
|
|
30
|
+
console.error(
|
|
31
|
+
`Usage: cre-compile <path/to/workflow.ts> [path/to/output.wasm] [${skipTypeChecksFlag}]`,
|
|
32
|
+
);
|
|
33
|
+
console.error("Examples:");
|
|
34
|
+
console.error(" cre-compile src/standard_tests/secrets/test.ts");
|
|
35
|
+
console.error(
|
|
36
|
+
" cre-compile src/standard_tests/secrets/test.ts .temp/standard_tests/secrets/test.wasm",
|
|
37
|
+
);
|
|
38
|
+
console.error(
|
|
39
|
+
` cre-compile src/standard_tests/secrets/test.ts .temp/standard_tests/secrets/test.wasm ${skipTypeChecksFlag}`,
|
|
40
|
+
);
|
|
23
41
|
process.exit(1);
|
|
24
42
|
}
|
|
25
43
|
|
|
26
|
-
await compileWorkflow(
|
|
27
|
-
inputPath,
|
|
28
|
-
outputPathArg,
|
|
29
|
-
creExports.length > 0 ? creExports : undefined,
|
|
30
|
-
plugin ?? undefined,
|
|
31
|
-
);
|
|
44
|
+
await compileWorkflow(inputPath, outputPathArg, { skipTypeChecks });
|
|
32
45
|
};
|
|
33
46
|
|
|
47
|
+
// CLI entry point
|
|
34
48
|
main().catch((e) => {
|
|
35
|
-
if (
|
|
49
|
+
if (
|
|
50
|
+
e instanceof WorkflowRuntimeCompatibilityError ||
|
|
51
|
+
e instanceof WorkflowTypecheckError
|
|
52
|
+
) {
|
|
36
53
|
console.error(`\n❌ ${e.message}`);
|
|
37
54
|
} else {
|
|
38
55
|
console.error(e);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chainlink/cre-sdk",
|
|
3
|
-
"version": "1.5.0
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"format": "biome format --write ${BIOME_PATHS:-.}",
|
|
50
50
|
"full-checks": "bun generate:sdk && bun run build && bun typecheck && bun check && bun test && bun test:standard",
|
|
51
51
|
"generate:chain-selectors": "bun scripts/run.ts generate-chain-selectors && BIOME_PATHS=\"src/generated\" bun check",
|
|
52
|
-
"generate:proto": "
|
|
52
|
+
"generate:proto": "bun x @bufbuild/buf generate && BIOME_PATHS=\"src/generated\" bun check",
|
|
53
53
|
"generate:sdk": "bun generate:proto && bun generate:chain-selectors && bun scripts/run generate-sdks && BIOME_PATHS=\"src/generated src/generated-sdk src/sdk/test/generated\" bun check",
|
|
54
54
|
"lint": "biome lint --write",
|
|
55
55
|
"prepublishOnly": "bun typecheck && bun check && bun test && bun test:standard",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"dependencies": {
|
|
61
61
|
"@bufbuild/protobuf": "2.6.3",
|
|
62
62
|
"@bufbuild/protoc-gen-es": "2.6.3",
|
|
63
|
-
"@chainlink/cre-sdk-javy-plugin": "1.5.0
|
|
63
|
+
"@chainlink/cre-sdk-javy-plugin": "1.5.0",
|
|
64
64
|
"@standard-schema/spec": "1.0.0",
|
|
65
65
|
"viem": "2.34.0",
|
|
66
66
|
"zod": "3.25.76"
|
|
@@ -8,9 +8,9 @@ set -e
|
|
|
8
8
|
# Create dist test workflow folder
|
|
9
9
|
mkdir -p ./dist/workflows/standard_tests
|
|
10
10
|
|
|
11
|
-
#
|
|
12
|
-
if [ ! -f ../cre-sdk-javy-plugin/dist/
|
|
13
|
-
echo "Error:
|
|
11
|
+
# Build javy wasm
|
|
12
|
+
if [ ! -f ../cre-sdk-javy-plugin/dist/javy_chainlink_sdk.wasm ]; then
|
|
13
|
+
echo "Error: javy_chainlink_sdk.wasm not found"
|
|
14
14
|
exit 1
|
|
15
15
|
fi
|
|
16
16
|
|
package/scripts/run.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
|
|
3
|
+
import { WorkflowTypecheckError } from './src/typecheck-workflow'
|
|
3
4
|
import { WorkflowRuntimeCompatibilityError } from './src/validate-workflow-runtime-compat'
|
|
4
5
|
|
|
5
6
|
const availableScripts = [
|
|
@@ -39,7 +40,10 @@ const main = async () => {
|
|
|
39
40
|
process.exit(1)
|
|
40
41
|
}
|
|
41
42
|
} catch (error) {
|
|
42
|
-
if (
|
|
43
|
+
if (
|
|
44
|
+
error instanceof WorkflowRuntimeCompatibilityError ||
|
|
45
|
+
error instanceof WorkflowTypecheckError
|
|
46
|
+
) {
|
|
43
47
|
console.error(`\n❌ ${error.message}`)
|
|
44
48
|
} else {
|
|
45
49
|
console.error(`Failed to run script ${scriptName}:`, error)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { parseCompileCliArgs } from './compile-cli-args'
|
|
3
|
+
|
|
4
|
+
describe('parseCompileCliArgs', () => {
|
|
5
|
+
test('parses positional input and output', () => {
|
|
6
|
+
const parsed = parseCompileCliArgs(['src/workflow.ts', 'dist/workflow.wasm'])
|
|
7
|
+
expect(parsed).toEqual({
|
|
8
|
+
inputPath: 'src/workflow.ts',
|
|
9
|
+
outputPath: 'dist/workflow.wasm',
|
|
10
|
+
skipTypeChecks: false,
|
|
11
|
+
})
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
test('parses --skip-type-checks flag', () => {
|
|
15
|
+
const parsed = parseCompileCliArgs(['src/workflow.ts', '--skip-type-checks'])
|
|
16
|
+
expect(parsed).toEqual({
|
|
17
|
+
inputPath: 'src/workflow.ts',
|
|
18
|
+
outputPath: undefined,
|
|
19
|
+
skipTypeChecks: true,
|
|
20
|
+
})
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test('throws on unknown flags', () => {
|
|
24
|
+
expect(() => parseCompileCliArgs(['src/workflow.ts', '--foo'])).toThrow('Unknown option: --foo')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('throws on too many positional args', () => {
|
|
28
|
+
expect(() => parseCompileCliArgs(['src/workflow.ts', 'dist/workflow.wasm', 'extra'])).toThrow(
|
|
29
|
+
'Too many positional arguments.',
|
|
30
|
+
)
|
|
31
|
+
})
|
|
32
|
+
})
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export const skipTypeChecksFlag = '--skip-type-checks'
|
|
2
|
+
|
|
3
|
+
export type ParsedCompileArgs = {
|
|
4
|
+
inputPath?: string
|
|
5
|
+
outputPath?: string
|
|
6
|
+
skipTypeChecks: boolean
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const parseCompileCliArgs = (args: string[]): ParsedCompileArgs => {
|
|
10
|
+
const positionalArgs: string[] = []
|
|
11
|
+
let skipTypeChecks = false
|
|
12
|
+
|
|
13
|
+
for (const arg of args) {
|
|
14
|
+
if (arg === skipTypeChecksFlag) {
|
|
15
|
+
skipTypeChecks = true
|
|
16
|
+
continue
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (arg.startsWith('-')) {
|
|
20
|
+
throw new Error(`Unknown option: ${arg}`)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
positionalArgs.push(arg)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (positionalArgs.length > 2) {
|
|
27
|
+
throw new Error('Too many positional arguments.')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
inputPath: positionalArgs[0],
|
|
32
|
+
outputPath: positionalArgs[1],
|
|
33
|
+
skipTypeChecks,
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import { main as compileToJs } from './compile-to-js'
|
|
5
|
+
import { WorkflowTypecheckError } from './typecheck-workflow'
|
|
6
|
+
|
|
7
|
+
let tempDir: string
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
tempDir = mkdtempSync(path.join(process.cwd(), '.tmp-cre-compile-to-js-test-'))
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
rmSync(tempDir, { recursive: true, force: true })
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
const writeTemp = (filename: string, content: string): string => {
|
|
18
|
+
const filePath = path.join(tempDir, filename)
|
|
19
|
+
mkdirSync(path.dirname(filePath), { recursive: true })
|
|
20
|
+
writeFileSync(filePath, content, 'utf-8')
|
|
21
|
+
return filePath
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe('compile-to-js typecheck behavior', () => {
|
|
25
|
+
test('fails on type errors by default', async () => {
|
|
26
|
+
writeTemp(
|
|
27
|
+
'tsconfig.json',
|
|
28
|
+
JSON.stringify(
|
|
29
|
+
{
|
|
30
|
+
compilerOptions: {
|
|
31
|
+
target: 'ES2022',
|
|
32
|
+
module: 'ESNext',
|
|
33
|
+
moduleResolution: 'Bundler',
|
|
34
|
+
skipLibCheck: true,
|
|
35
|
+
strict: true,
|
|
36
|
+
types: [],
|
|
37
|
+
lib: ['ESNext'],
|
|
38
|
+
},
|
|
39
|
+
include: ['src/**/*.ts'],
|
|
40
|
+
},
|
|
41
|
+
null,
|
|
42
|
+
2,
|
|
43
|
+
),
|
|
44
|
+
)
|
|
45
|
+
const entry = writeTemp('src/workflow.ts', "export const shouldBeNumber: number = 'oops'\n")
|
|
46
|
+
const output = path.join(tempDir, 'dist/workflow.js')
|
|
47
|
+
|
|
48
|
+
await expect(compileToJs(entry, output)).rejects.toBeInstanceOf(WorkflowTypecheckError)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('continues when --skip-type-checks is enabled', async () => {
|
|
52
|
+
writeTemp(
|
|
53
|
+
'tsconfig.json',
|
|
54
|
+
JSON.stringify(
|
|
55
|
+
{
|
|
56
|
+
compilerOptions: {
|
|
57
|
+
target: 'ES2022',
|
|
58
|
+
module: 'ESNext',
|
|
59
|
+
moduleResolution: 'Bundler',
|
|
60
|
+
skipLibCheck: true,
|
|
61
|
+
strict: true,
|
|
62
|
+
types: [],
|
|
63
|
+
lib: ['ESNext'],
|
|
64
|
+
},
|
|
65
|
+
include: ['src/**/*.ts'],
|
|
66
|
+
},
|
|
67
|
+
null,
|
|
68
|
+
2,
|
|
69
|
+
),
|
|
70
|
+
)
|
|
71
|
+
const entry = writeTemp('src/workflow.ts', "export const shouldBeNumber: number = 'oops'\n")
|
|
72
|
+
const output = path.join(tempDir, 'dist/workflow.js')
|
|
73
|
+
|
|
74
|
+
let thrownError: unknown
|
|
75
|
+
let result: string | undefined
|
|
76
|
+
try {
|
|
77
|
+
result = await compileToJs(entry, output, {
|
|
78
|
+
skipTypeChecks: true,
|
|
79
|
+
})
|
|
80
|
+
} catch (error) {
|
|
81
|
+
thrownError = error
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
expect(thrownError).not.toBeInstanceOf(WorkflowTypecheckError)
|
|
85
|
+
if (typeof result === 'string') {
|
|
86
|
+
expect(result).toEqual(output)
|
|
87
|
+
expect(existsSync(output)).toBeTrue()
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
})
|
|
@@ -2,24 +2,63 @@ import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'
|
|
|
2
2
|
import { mkdir } from 'node:fs/promises'
|
|
3
3
|
import path from 'node:path'
|
|
4
4
|
import { $ } from 'bun'
|
|
5
|
+
import { parseCompileCliArgs, skipTypeChecksFlag } from './compile-cli-args'
|
|
6
|
+
import { assertWorkflowTypecheck } from './typecheck-workflow'
|
|
5
7
|
import { assertWorkflowRuntimeCompatibility } from './validate-workflow-runtime-compat'
|
|
6
8
|
import { wrapWorkflowCode } from './workflow-wrapper'
|
|
7
9
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
type CompileToJsOptions = {
|
|
11
|
+
skipTypeChecks?: boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const printUsage = () => {
|
|
15
|
+
console.error(`Usage: bun compile:ts-to-js <path-to-file> [output-file] [${skipTypeChecksFlag}]`)
|
|
16
|
+
console.error('Example:')
|
|
17
|
+
console.error(' bun compile:ts-to-js src/tests/foo.ts dist/tests/foo.bundle.js')
|
|
18
|
+
console.error(
|
|
19
|
+
` bun compile:ts-to-js src/tests/foo.ts dist/tests/foo.bundle.js ${skipTypeChecksFlag}`,
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const main = async (
|
|
24
|
+
tsFilePath?: string,
|
|
25
|
+
outputFilePath?: string,
|
|
26
|
+
options?: CompileToJsOptions,
|
|
27
|
+
) => {
|
|
28
|
+
let parsedInputPath: string | undefined
|
|
29
|
+
let parsedOutputPath: string | undefined
|
|
30
|
+
let parsedSkipTypeChecks = false
|
|
31
|
+
|
|
32
|
+
if (tsFilePath != null || outputFilePath != null || options?.skipTypeChecks != null) {
|
|
33
|
+
parsedInputPath = tsFilePath
|
|
34
|
+
parsedOutputPath = outputFilePath
|
|
35
|
+
parsedSkipTypeChecks = options?.skipTypeChecks ?? false
|
|
36
|
+
} else {
|
|
37
|
+
try {
|
|
38
|
+
const parsed = parseCompileCliArgs(process.argv.slice(3))
|
|
39
|
+
parsedInputPath = parsed.inputPath
|
|
40
|
+
parsedOutputPath = parsed.outputPath
|
|
41
|
+
parsedSkipTypeChecks = parsed.skipTypeChecks
|
|
42
|
+
} catch (error) {
|
|
43
|
+
console.error(error instanceof Error ? error.message : error)
|
|
44
|
+
printUsage()
|
|
45
|
+
process.exit(1)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
10
48
|
|
|
11
49
|
// Prefer function params, fallback to CLI args
|
|
12
|
-
const inputPath =
|
|
13
|
-
const outputPathArg =
|
|
50
|
+
const inputPath = parsedInputPath
|
|
51
|
+
const outputPathArg = parsedOutputPath
|
|
14
52
|
|
|
15
53
|
if (!inputPath) {
|
|
16
|
-
|
|
17
|
-
console.error('Example:')
|
|
18
|
-
console.error(' bun test:standard:compile:js src/tests/foo.ts dist/tests/foo.bundle.js')
|
|
54
|
+
printUsage()
|
|
19
55
|
process.exit(1)
|
|
20
56
|
}
|
|
21
57
|
|
|
22
58
|
const resolvedInput = path.resolve(inputPath)
|
|
59
|
+
if (!parsedSkipTypeChecks) {
|
|
60
|
+
assertWorkflowTypecheck(resolvedInput)
|
|
61
|
+
}
|
|
23
62
|
assertWorkflowRuntimeCompatibility(resolvedInput)
|
|
24
63
|
console.info(`📁 Using input file: ${resolvedInput}`)
|
|
25
64
|
|
|
@@ -2,7 +2,6 @@ import { spawn } from 'node:child_process'
|
|
|
2
2
|
import { existsSync } from 'node:fs'
|
|
3
3
|
import { mkdir } from 'node:fs/promises'
|
|
4
4
|
import path from 'node:path'
|
|
5
|
-
import { parseCompileFlags } from '../../../cre-sdk-javy-plugin/scripts/parse-compile-flags'
|
|
6
5
|
|
|
7
6
|
function runBun(args: string[]): Promise<void> {
|
|
8
7
|
return new Promise((resolve, reject) => {
|
|
@@ -20,29 +19,20 @@ function runBun(args: string[]): Promise<void> {
|
|
|
20
19
|
|
|
21
20
|
const isJsFile = (p: string) => ['.js', '.mjs', '.cjs'].includes(path.extname(p).toLowerCase())
|
|
22
21
|
|
|
23
|
-
export const main = async (
|
|
24
|
-
inputFile?: string,
|
|
25
|
-
outputFile?: string,
|
|
26
|
-
creExportsPaths?: string[],
|
|
27
|
-
pluginPath?: string | null,
|
|
28
|
-
) => {
|
|
22
|
+
export const main = async (inputFile?: string, outputFile?: string) => {
|
|
29
23
|
const cliArgs = process.argv.slice(3)
|
|
30
|
-
const { creExports: cliCreExports, plugin: cliPlugin, rest: cliRest } = parseCompileFlags(cliArgs)
|
|
31
24
|
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
const plugin = pluginPath !== undefined ? pluginPath : cliPlugin
|
|
36
|
-
|
|
37
|
-
if (plugin !== null && plugin !== undefined && creExports.length > 0) {
|
|
38
|
-
console.error('❌ Error: --plugin and --cre-exports are mutually exclusive.')
|
|
39
|
-
process.exit(1)
|
|
40
|
-
}
|
|
25
|
+
// Resolve input/output from params or CLI
|
|
26
|
+
const inputPath = inputFile ?? cliArgs[0]
|
|
27
|
+
const outputPathArg = outputFile ?? cliArgs[1]
|
|
41
28
|
|
|
42
29
|
if (!inputPath) {
|
|
43
30
|
console.error(
|
|
44
31
|
'Usage: bun compile:js-to-wasm <path/to/input.(js|mjs|cjs)> [path/to/output.wasm]',
|
|
45
32
|
)
|
|
33
|
+
console.error('Examples:')
|
|
34
|
+
console.error(' bun compile:js-to-wasm ./build/workflows/test.js')
|
|
35
|
+
console.error(' bun compile:js-to-wasm ./build/workflows/test.mjs ./artifacts/test.wasm')
|
|
46
36
|
process.exit(1)
|
|
47
37
|
}
|
|
48
38
|
|
|
@@ -57,35 +47,31 @@ export const main = async (
|
|
|
57
47
|
process.exit(1)
|
|
58
48
|
}
|
|
59
49
|
|
|
50
|
+
// Default output = same dir, same basename, .wasm extension
|
|
60
51
|
const defaultOut = path.join(
|
|
61
52
|
path.dirname(resolvedInput),
|
|
62
53
|
path.basename(resolvedInput).replace(/\.(m|c)?js$/i, '.wasm'),
|
|
63
54
|
)
|
|
64
55
|
const resolvedOutput = outputPathArg ? path.resolve(outputPathArg) : defaultOut
|
|
65
56
|
|
|
57
|
+
// Ensure output directory exists
|
|
66
58
|
await mkdir(path.dirname(resolvedOutput), { recursive: true })
|
|
67
59
|
|
|
68
|
-
console.info(
|
|
60
|
+
console.info(`🔨 Compiling to WASM`)
|
|
69
61
|
console.info(`📁 Input: ${resolvedInput}`)
|
|
70
62
|
console.info(`🎯 Output: ${resolvedOutput}`)
|
|
71
63
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
compileArgs.push('--plugin', path.resolve(plugin))
|
|
75
|
-
} else {
|
|
76
|
-
compileArgs.push(...creExports.flatMap((p) => ['--cre-exports', path.resolve(p)]))
|
|
77
|
-
}
|
|
78
|
-
compileArgs.push(resolvedInput, resolvedOutput)
|
|
79
|
-
|
|
64
|
+
// Prefer the sibling @chainlink/cre-sdk-javy-plugin install (same as monorepo layout).
|
|
65
|
+
// Bun's shell `$` template can throw EINVAL on some Linux/arm64 Docker setups; use spawn.
|
|
80
66
|
const scriptDir = import.meta.dir
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
67
|
+
const compilerPath = path.resolve(
|
|
68
|
+
scriptDir,
|
|
69
|
+
'../../../cre-sdk-javy-plugin/bin/compile-workflow.ts',
|
|
70
|
+
)
|
|
85
71
|
if (existsSync(compilerPath)) {
|
|
86
|
-
await runBun(['--bun', compilerPath,
|
|
72
|
+
await runBun(['--bun', compilerPath, resolvedInput, resolvedOutput])
|
|
87
73
|
} else {
|
|
88
|
-
await runBun(['x', 'cre-compile-workflow',
|
|
74
|
+
await runBun(['x', 'cre-compile-workflow', resolvedInput, resolvedOutput])
|
|
89
75
|
}
|
|
90
76
|
|
|
91
77
|
console.info(`✅ Compiled: ${resolvedOutput}`)
|
|
@@ -1,31 +1,59 @@
|
|
|
1
1
|
import { existsSync } from 'node:fs'
|
|
2
2
|
import { mkdir } from 'node:fs/promises'
|
|
3
3
|
import path from 'node:path'
|
|
4
|
-
import {
|
|
4
|
+
import { parseCompileCliArgs, skipTypeChecksFlag } from './compile-cli-args'
|
|
5
5
|
import { main as compileToJs } from './compile-to-js'
|
|
6
6
|
import { main as compileToWasm } from './compile-to-wasm'
|
|
7
7
|
|
|
8
|
+
type CompileWorkflowOptions = {
|
|
9
|
+
skipTypeChecks?: boolean
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const printUsage = () => {
|
|
13
|
+
console.error(
|
|
14
|
+
`Usage: bun compile:workflow <path/to/workflow.ts> [path/to/output.wasm] [${skipTypeChecksFlag}]`,
|
|
15
|
+
)
|
|
16
|
+
console.error('Examples:')
|
|
17
|
+
console.error(' bun compile:workflow src/standard_tests/secrets/test.ts')
|
|
18
|
+
console.error(
|
|
19
|
+
' bun compile:workflow src/standard_tests/secrets/test.ts .temp/standard_tests/secrets/test.wasm',
|
|
20
|
+
)
|
|
21
|
+
console.error(
|
|
22
|
+
` bun compile:workflow src/standard_tests/secrets/test.ts .temp/standard_tests/secrets/test.wasm ${skipTypeChecksFlag}`,
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
|
|
8
26
|
export const main = async (
|
|
9
27
|
inputFile?: string,
|
|
10
28
|
outputWasmFile?: string,
|
|
11
|
-
|
|
12
|
-
pluginPath?: string | null,
|
|
29
|
+
options?: CompileWorkflowOptions,
|
|
13
30
|
) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const inputPath = inputFile ?? cliRest[0]
|
|
18
|
-
const outputPathArg = outputWasmFile ?? cliRest[1]
|
|
19
|
-
const creExports = creExportsPaths ?? cliCreExports
|
|
20
|
-
const plugin = pluginPath !== undefined ? pluginPath : cliPlugin
|
|
31
|
+
let parsedInputPath: string | undefined
|
|
32
|
+
let parsedOutputPath: string | undefined
|
|
33
|
+
let parsedSkipTypeChecks = false
|
|
21
34
|
|
|
22
|
-
if (
|
|
23
|
-
|
|
24
|
-
|
|
35
|
+
if (inputFile != null || outputWasmFile != null || options?.skipTypeChecks != null) {
|
|
36
|
+
parsedInputPath = inputFile
|
|
37
|
+
parsedOutputPath = outputWasmFile
|
|
38
|
+
parsedSkipTypeChecks = options?.skipTypeChecks ?? false
|
|
39
|
+
} else {
|
|
40
|
+
try {
|
|
41
|
+
const parsed = parseCompileCliArgs(process.argv.slice(3))
|
|
42
|
+
parsedInputPath = parsed.inputPath
|
|
43
|
+
parsedOutputPath = parsed.outputPath
|
|
44
|
+
parsedSkipTypeChecks = parsed.skipTypeChecks
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.error(error instanceof Error ? error.message : error)
|
|
47
|
+
printUsage()
|
|
48
|
+
process.exit(1)
|
|
49
|
+
}
|
|
25
50
|
}
|
|
26
51
|
|
|
52
|
+
const inputPath = parsedInputPath
|
|
53
|
+
const outputPathArg = parsedOutputPath
|
|
54
|
+
|
|
27
55
|
if (!inputPath) {
|
|
28
|
-
|
|
56
|
+
printUsage()
|
|
29
57
|
process.exit(1)
|
|
30
58
|
}
|
|
31
59
|
|
|
@@ -35,33 +63,33 @@ export const main = async (
|
|
|
35
63
|
process.exit(1)
|
|
36
64
|
}
|
|
37
65
|
|
|
66
|
+
// Default final output = same dir, same basename, .wasm
|
|
38
67
|
const defaultWasmOut = path.join(
|
|
39
68
|
path.dirname(resolvedInput),
|
|
40
69
|
path.basename(resolvedInput).replace(/\.[^.]+$/, '.wasm'),
|
|
41
70
|
)
|
|
42
71
|
const resolvedWasmOutput = outputPathArg ? path.resolve(outputPathArg) : defaultWasmOut
|
|
72
|
+
|
|
73
|
+
// Put the intermediate JS next to the final wasm (so custom outputs stay together)
|
|
43
74
|
const resolvedJsOutput = resolvedWasmOutput.replace(/\.wasm$/i, '.js')
|
|
44
75
|
|
|
76
|
+
// Ensure directories exist (handles both intermediate JS dir and wasm dir)
|
|
45
77
|
await mkdir(path.dirname(resolvedJsOutput), { recursive: true })
|
|
46
78
|
|
|
47
|
-
console.info(
|
|
48
|
-
console.info(`📁 Input: ${resolvedInput}`)
|
|
49
|
-
|
|
50
|
-
|
|
79
|
+
console.info(`🚀 Compiling workflow`)
|
|
80
|
+
console.info(`📁 Input: ${resolvedInput}\n`)
|
|
81
|
+
if (parsedSkipTypeChecks) {
|
|
82
|
+
console.info(`⚠️ Skipping TypeScript checks (${skipTypeChecksFlag})`)
|
|
83
|
+
}
|
|
51
84
|
|
|
85
|
+
// Step 1: TS/JS → JS (bundled)
|
|
52
86
|
console.info('📦 Step 1: Compiling JS...')
|
|
53
|
-
await compileToJs(resolvedInput, resolvedJsOutput)
|
|
87
|
+
await compileToJs(resolvedInput, resolvedJsOutput, { skipTypeChecks: parsedSkipTypeChecks })
|
|
54
88
|
|
|
89
|
+
// Step 2: JS → WASM
|
|
55
90
|
console.info('\n🔨 Step 2: Compiling to WASM...')
|
|
56
|
-
await compileToWasm(resolvedJsOutput, resolvedWasmOutput
|
|
91
|
+
await compileToWasm(resolvedJsOutput, resolvedWasmOutput)
|
|
57
92
|
|
|
58
93
|
console.info(`\n✅ Workflow built: ${resolvedWasmOutput}`)
|
|
59
94
|
return resolvedWasmOutput
|
|
60
95
|
}
|
|
61
|
-
|
|
62
|
-
if (import.meta.main) {
|
|
63
|
-
main().catch((e) => {
|
|
64
|
-
console.error(e)
|
|
65
|
-
process.exit(1)
|
|
66
|
-
})
|
|
67
|
-
}
|
|
@@ -78,34 +78,16 @@ const CHAIN_CONFIGS: ChainSelectorConfig[] = [
|
|
|
78
78
|
},
|
|
79
79
|
]
|
|
80
80
|
|
|
81
|
-
const
|
|
82
|
-
//
|
|
83
|
-
//
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
// Try 2 levels up (in case cwd is already in scripts/src/)
|
|
88
|
-
join(process.cwd(), '..', '..', 'node_modules', 'chain-selectors', filename),
|
|
89
|
-
// Try current directory's node_modules
|
|
90
|
-
join(process.cwd(), 'node_modules', 'chain-selectors', filename),
|
|
91
|
-
// Try parent directory's node_modules
|
|
92
|
-
join(process.cwd(), '..', 'node_modules', 'chain-selectors', filename),
|
|
93
|
-
]
|
|
94
|
-
|
|
95
|
-
for (const path of possiblePaths) {
|
|
96
|
-
try {
|
|
97
|
-
return readFileSync(path, 'utf-8')
|
|
98
|
-
} catch {
|
|
99
|
-
// Try next path
|
|
100
|
-
continue
|
|
101
|
-
}
|
|
102
|
-
}
|
|
81
|
+
const resolveChainSelectorsDir = (): string => {
|
|
82
|
+
// Use require.resolve to find the package through bun/Node module resolution,
|
|
83
|
+
// which correctly handles workspace hoisting, .bun cache, etc.
|
|
84
|
+
const packageDir = require.resolve('chain-selectors/selectors.yml')
|
|
85
|
+
return join(packageDir, '..')
|
|
86
|
+
}
|
|
103
87
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
)}`,
|
|
108
|
-
)
|
|
88
|
+
const readYamlFile = (filename: string): string => {
|
|
89
|
+
const filePath = join(resolveChainSelectorsDir(), filename)
|
|
90
|
+
return readFileSync(filePath, 'utf-8')
|
|
109
91
|
}
|
|
110
92
|
|
|
111
93
|
const parseChainSelectors = (): NetworkInfo[] => {
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import { assertWorkflowTypecheck, WorkflowTypecheckError } from './typecheck-workflow'
|
|
6
|
+
|
|
7
|
+
let tempDir: string
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
tempDir = mkdtempSync(path.join(tmpdir(), 'cre-typecheck-test-'))
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
rmSync(tempDir, { recursive: true, force: true })
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
const writeTemp = (filename: string, content: string): string => {
|
|
18
|
+
const filePath = path.join(tempDir, filename)
|
|
19
|
+
mkdirSync(path.dirname(filePath), { recursive: true })
|
|
20
|
+
writeFileSync(filePath, content, 'utf-8')
|
|
21
|
+
return filePath
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe('assertWorkflowTypecheck', () => {
|
|
25
|
+
test('passes for valid project using nearby tsconfig', () => {
|
|
26
|
+
writeTemp(
|
|
27
|
+
'tsconfig.json',
|
|
28
|
+
JSON.stringify(
|
|
29
|
+
{
|
|
30
|
+
compilerOptions: {
|
|
31
|
+
target: 'ES2022',
|
|
32
|
+
module: 'ESNext',
|
|
33
|
+
moduleResolution: 'Bundler',
|
|
34
|
+
skipLibCheck: true,
|
|
35
|
+
},
|
|
36
|
+
include: ['src/**/*.ts'],
|
|
37
|
+
},
|
|
38
|
+
null,
|
|
39
|
+
2,
|
|
40
|
+
),
|
|
41
|
+
)
|
|
42
|
+
const entry = writeTemp('src/workflow.ts', 'export const value: number = 42\n')
|
|
43
|
+
expect(() => assertWorkflowTypecheck(entry)).not.toThrow()
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('fails when tsconfig cannot be found', () => {
|
|
47
|
+
const entry = writeTemp('src/workflow.ts', 'export const value = 1\n')
|
|
48
|
+
expect(() => assertWorkflowTypecheck(entry)).toThrow(WorkflowTypecheckError)
|
|
49
|
+
expect(() => assertWorkflowTypecheck(entry)).toThrow('Could not find tsconfig.json')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('fails on whole-project type errors outside entry file', () => {
|
|
53
|
+
writeTemp(
|
|
54
|
+
'tsconfig.json',
|
|
55
|
+
JSON.stringify(
|
|
56
|
+
{
|
|
57
|
+
compilerOptions: {
|
|
58
|
+
target: 'ES2022',
|
|
59
|
+
module: 'ESNext',
|
|
60
|
+
moduleResolution: 'Bundler',
|
|
61
|
+
skipLibCheck: true,
|
|
62
|
+
strict: true,
|
|
63
|
+
},
|
|
64
|
+
include: ['src/**/*.ts'],
|
|
65
|
+
},
|
|
66
|
+
null,
|
|
67
|
+
2,
|
|
68
|
+
),
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
const entry = writeTemp('src/workflow.ts', 'export const value: number = 1\n')
|
|
72
|
+
writeTemp('src/unrelated.ts', "export const shouldBeNumber: number = 'not-a-number'\n")
|
|
73
|
+
|
|
74
|
+
expect(() => assertWorkflowTypecheck(entry)).toThrow(WorkflowTypecheckError)
|
|
75
|
+
expect(() => assertWorkflowTypecheck(entry)).toThrow('unrelated.ts')
|
|
76
|
+
})
|
|
77
|
+
})
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import * as ts from 'typescript'
|
|
3
|
+
import { skipTypeChecksFlag } from './compile-cli-args'
|
|
4
|
+
|
|
5
|
+
const toAbsolutePath = (filePath: string) => path.resolve(filePath)
|
|
6
|
+
|
|
7
|
+
const formatDiagnostic = (diagnostic: ts.Diagnostic): string => {
|
|
8
|
+
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')
|
|
9
|
+
if (!diagnostic.file || diagnostic.start == null) {
|
|
10
|
+
return message
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const absoluteFilePath = toAbsolutePath(diagnostic.file.fileName)
|
|
14
|
+
const relativeFilePath = path.relative(process.cwd(), absoluteFilePath)
|
|
15
|
+
const displayPath = relativeFilePath || absoluteFilePath
|
|
16
|
+
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start)
|
|
17
|
+
return `${displayPath}:${line + 1}:${character + 1} ${message}`
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class WorkflowTypecheckError extends Error {
|
|
21
|
+
constructor(message: string) {
|
|
22
|
+
super(message)
|
|
23
|
+
this.name = 'WorkflowTypecheckError'
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const findNearestTsconfigPath = (entryFilePath: string): string | null => {
|
|
28
|
+
const configPath = ts.findConfigFile(
|
|
29
|
+
path.dirname(entryFilePath),
|
|
30
|
+
ts.sys.fileExists,
|
|
31
|
+
'tsconfig.json',
|
|
32
|
+
)
|
|
33
|
+
return configPath ?? null
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const assertWorkflowTypecheck = (entryFilePath: string) => {
|
|
37
|
+
const rootFile = toAbsolutePath(entryFilePath)
|
|
38
|
+
const configPath = findNearestTsconfigPath(rootFile)
|
|
39
|
+
if (!configPath) {
|
|
40
|
+
throw new WorkflowTypecheckError(
|
|
41
|
+
`TypeScript typecheck failed before workflow compilation.
|
|
42
|
+
Could not find tsconfig.json near: ${rootFile}
|
|
43
|
+
Create a tsconfig.json in your workflow project, or re-run compile with ${skipTypeChecksFlag}.`,
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let unrecoverableDiagnostic: ts.Diagnostic | null = null
|
|
48
|
+
const parsedConfig = ts.getParsedCommandLineOfConfigFile(
|
|
49
|
+
configPath,
|
|
50
|
+
{},
|
|
51
|
+
{
|
|
52
|
+
...ts.sys,
|
|
53
|
+
onUnRecoverableConfigFileDiagnostic: (diagnostic) => {
|
|
54
|
+
unrecoverableDiagnostic = diagnostic
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
if (!parsedConfig) {
|
|
60
|
+
const details = unrecoverableDiagnostic ? formatDiagnostic(unrecoverableDiagnostic) : ''
|
|
61
|
+
throw new WorkflowTypecheckError(
|
|
62
|
+
`TypeScript typecheck failed before workflow compilation.
|
|
63
|
+
Failed to parse tsconfig.json: ${configPath}
|
|
64
|
+
${details}
|
|
65
|
+
Fix your tsconfig.json, or re-run compile with ${skipTypeChecksFlag}.`,
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const program = ts.createProgram({
|
|
70
|
+
rootNames: parsedConfig.fileNames,
|
|
71
|
+
options: {
|
|
72
|
+
...parsedConfig.options,
|
|
73
|
+
noEmit: true,
|
|
74
|
+
},
|
|
75
|
+
projectReferences: parsedConfig.projectReferences,
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
const diagnostics = [...parsedConfig.errors, ...ts.getPreEmitDiagnostics(program)].filter(
|
|
79
|
+
(diagnostic) => diagnostic.category === ts.DiagnosticCategory.Error,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
if (diagnostics.length > 0) {
|
|
83
|
+
const formatted = diagnostics.map(formatDiagnostic).join('\n')
|
|
84
|
+
const relativeConfigPath = path.relative(process.cwd(), toAbsolutePath(configPath))
|
|
85
|
+
const displayConfigPath = relativeConfigPath || toAbsolutePath(configPath)
|
|
86
|
+
throw new WorkflowTypecheckError(
|
|
87
|
+
`TypeScript typecheck failed before workflow compilation.
|
|
88
|
+
Using tsconfig: ${displayConfigPath}
|
|
89
|
+
Fix TypeScript errors, or re-run compile with ${skipTypeChecksFlag}.
|
|
90
|
+
|
|
91
|
+
${formatted}`,
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { WorkflowTypecheckError }
|