@enclave-run/sdk 0.0.0-dev.5 → 0.0.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/dist/index.mjs CHANGED
@@ -60,7 +60,7 @@ import createClient from "openapi-fetch";
60
60
  import platform2 from "platform";
61
61
 
62
62
  // package.json
63
- var version = "0.0.0-dev.5";
63
+ var version = "0.0.0";
64
64
 
65
65
  // src/utils.ts
66
66
  import platform from "platform";
@@ -286,11 +286,11 @@ function formatLog(log) {
286
286
  return JSON.parse(JSON.stringify(log));
287
287
  }
288
288
  function createRpcLogger(logger) {
289
- function logEach(stream) {
289
+ function logEach(stream2) {
290
290
  return __asyncGenerator(this, null, function* () {
291
291
  var _a3;
292
292
  try {
293
- for (var iter = __forAwait(stream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
293
+ for (var iter = __forAwait(stream2), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
294
294
  const m = temp.value;
295
295
  (_a3 = logger.debug) == null ? void 0 : _a3.call(logger, "Response stream:", formatLog(m));
296
296
  yield m;
@@ -4424,6 +4424,10 @@ var SandboxRegionScope = class {
4424
4424
  }
4425
4425
  };
4426
4426
 
4427
+ // src/template/buildApi.ts
4428
+ import fs2 from "fs";
4429
+ import stream from "stream";
4430
+
4427
4431
  // src/template/logger.ts
4428
4432
  import chalk from "chalk";
4429
4433
  var LogEntry = class {
@@ -4545,11 +4549,13 @@ function defaultBuildLogger(options) {
4545
4549
  // src/template/utils.ts
4546
4550
  import crypto2 from "crypto";
4547
4551
  import fs from "fs";
4552
+ import os from "os";
4548
4553
  import path from "path";
4549
4554
  import url from "url";
4550
4555
 
4551
4556
  // src/template/consts.ts
4552
4557
  var FINALIZE_STEP_NAME = "finalize";
4558
+ var FILE_UPLOAD_TIMEOUT_MS = 36e5;
4553
4559
  var BASE_STEP_NAME = "base";
4554
4560
  var STACK_TRACE_DEPTH = 3;
4555
4561
  var RESOLVE_SYMLINKS = false;
@@ -4701,7 +4707,7 @@ function getCallerDirectory(depth) {
4701
4707
  function padOctal(mode) {
4702
4708
  return mode.toString(8).padStart(4, "0");
4703
4709
  }
4704
- async function tarFileStream(fileName, fileContextPath, ignorePatterns, resolveSymlinks) {
4710
+ async function getTarArchiveInputs(fileName, fileContextPath, ignorePatterns) {
4705
4711
  const { create } = await dynamicImport("tar");
4706
4712
  const allFiles = await getAllFilesInPath(
4707
4713
  fileName,
@@ -4709,24 +4715,40 @@ async function tarFileStream(fileName, fileContextPath, ignorePatterns, resolveS
4709
4715
  ignorePatterns,
4710
4716
  true
4711
4717
  );
4712
- const filePaths = allFiles.map((file) => file.relativePosix());
4713
- return create(
4714
- {
4715
- gzip: true,
4716
- cwd: fileContextPath,
4717
- follow: resolveSymlinks,
4718
- noDirRecurse: true
4719
- },
4720
- filePaths
4721
- );
4718
+ return {
4719
+ create,
4720
+ filePaths: allFiles.map((file) => file.relativePosix())
4721
+ };
4722
4722
  }
4723
- async function tarFileStreamUpload(fileName, fileContextPath, ignorePatterns, resolveSymlinks) {
4724
- return tarFileStream(
4723
+ async function spoolTarArchive(fileName, fileContextPath, ignorePatterns, resolveSymlinks) {
4724
+ const { create, filePaths } = await getTarArchiveInputs(
4725
4725
  fileName,
4726
4726
  fileContextPath,
4727
- ignorePatterns,
4728
- resolveSymlinks
4727
+ ignorePatterns
4729
4728
  );
4729
+ const tmpDir = await fs.promises.mkdtemp(
4730
+ path.join(os.tmpdir(), "enclave-template-")
4731
+ );
4732
+ const tarPath = path.join(tmpDir, "context.tar.gz");
4733
+ const cleanup = () => fs.promises.rm(tmpDir, { recursive: true, force: true }).catch(() => {
4734
+ });
4735
+ try {
4736
+ await create(
4737
+ {
4738
+ gzip: true,
4739
+ cwd: fileContextPath,
4740
+ follow: resolveSymlinks,
4741
+ noDirRecurse: true,
4742
+ file: tarPath
4743
+ },
4744
+ filePaths
4745
+ );
4746
+ const { size } = await fs.promises.stat(tarPath);
4747
+ return { path: tarPath, size, cleanup };
4748
+ } catch (error) {
4749
+ await cleanup();
4750
+ throw error;
4751
+ }
4730
4752
  }
4731
4753
  function getBuildStepIndex(step, stackTracesLength) {
4732
4754
  if (step === BASE_STEP_NAME) {
@@ -4786,22 +4808,34 @@ async function getFileUploadLink(client, { templateID, filesHash }, stackTrace)
4786
4808
  }
4787
4809
  async function uploadFile(options, stackTrace) {
4788
4810
  const { fileName, url: url2, fileContextPath, ignorePatterns, resolveSymlinks } = options;
4811
+ let cleanup;
4812
+ let bodyStream;
4789
4813
  try {
4790
- const uploadStream = await tarFileStreamUpload(
4814
+ const archive = await spoolTarArchive(
4791
4815
  fileName,
4792
4816
  fileContextPath,
4793
4817
  ignorePatterns,
4794
4818
  resolveSymlinks
4795
4819
  );
4820
+ cleanup = archive.cleanup;
4821
+ bodyStream = fs2.createReadStream(archive.path);
4796
4822
  const res = await fetch(url2, {
4797
4823
  method: "PUT",
4798
- // @ts-expect-error
4799
- body: uploadStream,
4824
+ body: stream.Readable.toWeb(bodyStream),
4825
+ headers: {
4826
+ "Content-Length": archive.size.toString()
4827
+ },
4828
+ signal: AbortSignal.timeout(FILE_UPLOAD_TIMEOUT_MS),
4829
+ // Node fetch requires duplex for a streaming request body, but the DOM
4830
+ // RequestInit type does not expose it.
4831
+ // @ts-expect-error Node fetch extension
4800
4832
  duplex: "half"
4801
4833
  });
4802
4834
  if (!res.ok) {
4835
+ const responseDetails = (await res.text().catch(() => "")).slice(0, 4096);
4836
+ const status = `${res.status} ${res.statusText}`.trim();
4803
4837
  throw new FileUploadError(
4804
- `Failed to upload file: ${res.statusText}`,
4838
+ `Failed to upload file: ${status}${responseDetails ? ` - ${responseDetails}` : ""}`,
4805
4839
  stackTrace
4806
4840
  );
4807
4841
  }
@@ -4810,6 +4844,14 @@ async function uploadFile(options, stackTrace) {
4810
4844
  throw error;
4811
4845
  }
4812
4846
  throw new FileUploadError(`Failed to upload file: ${error}`, stackTrace);
4847
+ } finally {
4848
+ if (bodyStream && !bodyStream.closed) {
4849
+ await new Promise((resolve) => {
4850
+ bodyStream == null ? void 0 : bodyStream.once("close", resolve);
4851
+ bodyStream == null ? void 0 : bodyStream.destroy();
4852
+ });
4853
+ }
4854
+ await (cleanup == null ? void 0 : cleanup());
4813
4855
  }
4814
4856
  }
4815
4857
  async function triggerBuild(client, { templateID, buildID, template }) {
@@ -4998,7 +5040,7 @@ async function getTemplateTags(client, { templateID }) {
4998
5040
  import {
4999
5041
  DockerfileParser
5000
5042
  } from "dockerfile-ast";
5001
- import fs2 from "fs";
5043
+ import fs3 from "fs";
5002
5044
 
5003
5045
  // src/template/readycmd.ts
5004
5046
  var ReadyCmd = class {
@@ -5035,8 +5077,8 @@ function waitForTimeout(timeout) {
5035
5077
  function parseDockerfile(dockerfileContentOrPath, templateBuilder) {
5036
5078
  let dockerfileContent;
5037
5079
  try {
5038
- if (fs2.existsSync(dockerfileContentOrPath) && fs2.statSync(dockerfileContentOrPath).isFile()) {
5039
- dockerfileContent = fs2.readFileSync(dockerfileContentOrPath, "utf-8");
5080
+ if (fs3.existsSync(dockerfileContentOrPath) && fs3.statSync(dockerfileContentOrPath).isFile()) {
5081
+ dockerfileContent = fs3.readFileSync(dockerfileContentOrPath, "utf-8");
5040
5082
  } else {
5041
5083
  dockerfileContent = dockerfileContentOrPath;
5042
5084
  }