-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun-function.ts
More file actions
177 lines (155 loc) · 4.18 KB
/
Copy pathrun-function.ts
File metadata and controls
177 lines (155 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* Run a function with the given payload and return the result
*/
import { spawn } from "child_process";
import { FixtureData } from "./load-fixture.js";
/**
* Metadata reported by function-runner for a function execution.
*/
export interface FunctionRunMetadata {
instructionCount: number;
memoryUsageKiB: number;
moduleSizeKiB: number;
}
/**
* Interface for the run function result
*/
export interface RunFunctionResult {
result: { output: any } | null;
metadata: FunctionRunMetadata | null;
error: string | null;
}
function parseFunctionRunnerResult(stdout: string): {
result: RunFunctionResult["result"];
metadata: FunctionRunMetadata | null;
error: string | null;
} {
let result: unknown;
try {
result = JSON.parse(stdout);
} catch (parseError) {
// function-runner is not guaranteed to return JSON when it fails.
return {
result: null,
metadata: null,
error: `Failed to parse function-runner output: ${parseError instanceof Error ? parseError.message : "Unknown error"}`,
};
}
const invalidShapeResult = {
result: null,
metadata: null,
error: `function-runner returned unexpected format. Received: ${JSON.stringify(result)}`,
};
if (typeof result !== "object" || result === null) {
return invalidShapeResult;
}
const resultObject = result as Record<string, unknown>;
const {
output,
instructions,
size: moduleSize,
memory_usage: memoryUsage,
} = resultObject;
if (
output === undefined ||
typeof instructions !== "number" ||
typeof memoryUsage !== "number" ||
typeof moduleSize !== "number"
) {
return invalidShapeResult;
}
return {
result: { output },
metadata: {
instructionCount: instructions,
memoryUsageKiB: memoryUsage,
moduleSizeKiB: moduleSize,
},
error: null,
};
}
/**
* Run a function using Shopify CLI's function runner command
*
* This function:
* - Uses function-runner binary directly to run the function.
* @param {String} functionRunnerPath - Path to the function runner binary
* @param {String} wasmPath - Path to the WASM file
* @param {FixtureData} fixture - The fixture data containing export, input, and target
* @param {String} queryPath - Path to the input query file
* @param {String} schemaPath - Path to the schema file
* @returns {Object} The function run result
*/
export async function runFunction(
fixture: FixtureData,
functionRunnerPath: string,
wasmPath: string,
queryPath: string,
schemaPath: string,
): Promise<RunFunctionResult> {
try {
const inputJson = JSON.stringify(fixture.input);
return new Promise((resolve) => {
const runnerProcess = spawn(
functionRunnerPath,
[
"-f",
wasmPath,
"--export",
fixture.export,
"--query-path",
queryPath,
"--schema-path",
schemaPath,
"--json",
],
{
stdio: ["pipe", "pipe", "pipe"],
},
);
let stdout = "";
let stderr = "";
runnerProcess.stdout.on("data", (data) => {
stdout += data.toString();
});
runnerProcess.stderr.on("data", (data) => {
stderr += data.toString();
});
runnerProcess.on("close", (code) => {
const functionRunnerResult = parseFunctionRunnerResult(stdout);
if (code !== 0) {
resolve({
result: null,
metadata: functionRunnerResult.metadata,
error: `function-runner failed with exit code ${code}: ${stderr}`,
});
return;
}
resolve(functionRunnerResult);
});
runnerProcess.on("error", (error) => {
resolve({
result: null,
metadata: null,
error: `Failed to start function-runner: ${error.message}`,
});
});
runnerProcess.stdin.write(inputJson);
runnerProcess.stdin.end();
});
} catch (error) {
if (error instanceof Error) {
return {
result: null,
metadata: null,
error: error.message,
};
} else {
return {
result: null,
metadata: null,
error: "Unknown error occurred",
};
}
}
}