-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from prismatic-io/tr/improvements-to-components…
…-dev-test Improvements to components:dev:test
- Loading branch information
Showing
7 changed files
with
140 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { decode } from "@msgpack/msgpack"; | ||
import axios from "axios"; | ||
import { fs } from "../../fs"; | ||
import { gql, gqlRequest } from "../../graphql"; | ||
import { extension } from "mime-types"; | ||
|
||
export interface DeserializeResult { | ||
data: unknown; | ||
contentType: string; | ||
} | ||
|
||
export const deserialize = (data: Buffer): DeserializeResult | unknown => | ||
decode(data); | ||
|
||
export const parseData = (data: any, contentType = ""): string | Buffer => { | ||
if (data === null || data === undefined) { | ||
return ""; | ||
} | ||
// Some content-types require additional processing. | ||
const dataType = extension(contentType); | ||
if ( | ||
contentType.startsWith("text/") || | ||
(dataType && ["json", "xml", "csv", "xhtml"].includes(dataType)) | ||
) { | ||
// If the content-types specifies that the data is text/, or if the extension | ||
// is a well-known text-like type, convert the data to a string. | ||
if (dataType === "json") { | ||
try { | ||
// If the payload is supposed to be JSON, ensure that it can be parsed. | ||
return JSON.parse(data.toString()); | ||
} catch { | ||
throw new Error("Received malformed JSON payload."); | ||
} | ||
} | ||
return data; | ||
} else if (contentType.startsWith("binary/")) { | ||
return data; | ||
} | ||
|
||
return typeof data === "string" || | ||
(typeof data === "object" && Buffer.isBuffer(data)) | ||
? data | ||
: JSON.stringify(data); | ||
}; | ||
|
||
const getFinalStepResult = async (executionId: string) => { | ||
const result = await gqlRequest({ | ||
document: gql` | ||
query executionResults($executionId: ID!) { | ||
executionResult(id: $executionId) { | ||
stepResults(last: 1) { | ||
nodes { | ||
id | ||
stepName | ||
resultsUrl | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
variables: { executionId }, | ||
}); | ||
const { resultsUrl } = result.executionResult.stepResults.nodes[0]; | ||
const response = await axios.get(resultsUrl, { | ||
responseType: "arraybuffer", | ||
}); | ||
const resultsBuffer = Buffer.from(await response.data); | ||
const { data: deserializedResult, contentType } = decode( | ||
resultsBuffer | ||
) as DeserializeResult; | ||
|
||
return { | ||
data: parseData(deserializedResult as string, contentType), | ||
contentType, | ||
}; | ||
}; | ||
|
||
export const writeFinalStepResults = async ( | ||
executionId: string, | ||
fileName: string | ||
): Promise<void> => { | ||
const result = await getFinalStepResult(executionId); | ||
await fs.writeFile(fileName, result.data); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { spawn } from "child_process"; | ||
|
||
export const spawnProcess = ( | ||
[command, ...args]: string[], | ||
env: Record<string, string> | ||
): Promise<void> => { | ||
return new Promise((resolve, reject) => { | ||
const child = spawn(command, args, { | ||
env: { ...process.env, ...env }, | ||
}); | ||
|
||
child.stdout.pipe(process.stdout); | ||
child.stderr.pipe(process.stderr); | ||
|
||
child.on("close", (code) => (code === 0 ? resolve() : reject())); | ||
}); | ||
}; |