@dagger.io/dagger 0.3.0 → 0.3.2

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.
Files changed (81) hide show
  1. package/README.md +72 -1
  2. package/dist/api/client.gen.d.ts +305 -145
  3. package/dist/api/client.gen.d.ts.map +1 -1
  4. package/dist/api/client.gen.js +426 -154
  5. package/dist/api/utils.d.ts +1 -1
  6. package/dist/api/utils.d.ts.map +1 -1
  7. package/dist/api/utils.js +43 -41
  8. package/dist/common/errors/DaggerSDKError.d.ts +4 -3
  9. package/dist/common/errors/DaggerSDKError.d.ts.map +1 -1
  10. package/dist/common/errors/DockerImageRefValidationError.d.ts +2 -2
  11. package/dist/common/errors/DockerImageRefValidationError.d.ts.map +1 -1
  12. package/dist/common/errors/DockerImageRefValidationError.js +3 -2
  13. package/dist/common/errors/EngineSessionConnectParamsParseError.d.ts +21 -0
  14. package/dist/common/errors/EngineSessionConnectParamsParseError.d.ts.map +1 -0
  15. package/dist/common/errors/EngineSessionConnectParamsParseError.js +16 -0
  16. package/dist/common/errors/EngineSessionConnectionTimeoutError.d.ts +21 -0
  17. package/dist/common/errors/EngineSessionConnectionTimeoutError.d.ts.map +1 -0
  18. package/dist/common/errors/EngineSessionConnectionTimeoutError.js +16 -0
  19. package/dist/common/errors/EngineSessionEOFErrorOptions.d.ts +16 -0
  20. package/dist/common/errors/EngineSessionEOFErrorOptions.d.ts.map +1 -0
  21. package/dist/common/errors/EngineSessionEOFErrorOptions.js +16 -0
  22. package/dist/common/errors/GraphQLRequestError.d.ts +2 -2
  23. package/dist/common/errors/GraphQLRequestError.d.ts.map +1 -1
  24. package/dist/common/errors/GraphQLRequestError.js +3 -2
  25. package/dist/common/errors/InitEngineSessionBinaryError.d.ts +2 -2
  26. package/dist/common/errors/InitEngineSessionBinaryError.d.ts.map +1 -1
  27. package/dist/common/errors/InitEngineSessionBinaryError.js +3 -2
  28. package/dist/common/errors/TooManyNestedObjectsError.d.ts +2 -2
  29. package/dist/common/errors/TooManyNestedObjectsError.d.ts.map +1 -1
  30. package/dist/common/errors/TooManyNestedObjectsError.js +3 -2
  31. package/dist/common/errors/UnknownDaggerError.d.ts +2 -2
  32. package/dist/common/errors/UnknownDaggerError.d.ts.map +1 -1
  33. package/dist/common/errors/UnknownDaggerError.js +3 -2
  34. package/dist/common/errors/errors-codes.d.ts +43 -0
  35. package/dist/common/errors/errors-codes.d.ts.map +1 -0
  36. package/dist/common/errors/errors-codes.js +35 -0
  37. package/dist/common/errors/index.d.ts +4 -1
  38. package/dist/common/errors/index.d.ts.map +1 -1
  39. package/dist/common/errors/index.js +4 -1
  40. package/dist/common/utils.d.ts +3 -3
  41. package/dist/connect.d.ts +24 -1
  42. package/dist/connect.d.ts.map +1 -1
  43. package/dist/connect.js +29 -5
  44. package/dist/index.d.ts +0 -1
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +0 -1
  47. package/dist/provisioning/{docker-provision/image.d.ts → bin.d.ts} +35 -29
  48. package/dist/provisioning/bin.d.ts.map +1 -0
  49. package/dist/provisioning/bin.js +336 -0
  50. package/dist/provisioning/default.d.ts +1 -1
  51. package/dist/provisioning/default.d.ts.map +1 -1
  52. package/dist/provisioning/default.js +1 -2
  53. package/dist/provisioning/engineconn.d.ts +1 -1
  54. package/dist/provisioning/engineconn.d.ts.map +1 -1
  55. package/dist/provisioning/index.d.ts +1 -2
  56. package/dist/provisioning/index.d.ts.map +1 -1
  57. package/dist/provisioning/index.js +1 -2
  58. package/package.json +17 -11
  59. package/dist/common/errors/EngineSessionPortParseError.d.ts +0 -25
  60. package/dist/common/errors/EngineSessionPortParseError.d.ts.map +0 -1
  61. package/dist/common/errors/EngineSessionPortParseError.js +0 -20
  62. package/dist/provisioning/bin/bin.d.ts +0 -21
  63. package/dist/provisioning/bin/bin.d.ts.map +0 -1
  64. package/dist/provisioning/bin/bin.js +0 -116
  65. package/dist/provisioning/bin/index.d.ts +0 -2
  66. package/dist/provisioning/bin/index.d.ts.map +0 -1
  67. package/dist/provisioning/bin/index.js +0 -1
  68. package/dist/provisioning/docker-provision/image.d.ts.map +0 -1
  69. package/dist/provisioning/docker-provision/image.js +0 -275
  70. package/dist/provisioning/docker-provision/index.d.ts +0 -2
  71. package/dist/provisioning/docker-provision/index.d.ts.map +0 -1
  72. package/dist/provisioning/docker-provision/index.js +0 -1
  73. package/dist/provisioning/http/http.d.ts +0 -14
  74. package/dist/provisioning/http/http.d.ts.map +0 -1
  75. package/dist/provisioning/http/http.js +0 -33
  76. package/dist/provisioning/http/index.d.ts +0 -2
  77. package/dist/provisioning/http/index.d.ts.map +0 -1
  78. package/dist/provisioning/http/index.js +0 -1
  79. package/dist/provisioning/provisioner.d.ts +0 -11
  80. package/dist/provisioning/provisioner.d.ts.map +0 -1
  81. package/dist/provisioning/provisioner.js +0 -26
package/dist/connect.d.ts CHANGED
@@ -1,15 +1,38 @@
1
1
  /// <reference types="node" />
2
- import Client from "./api/client.gen.js";
3
2
  import { Writable } from "node:stream";
3
+ import Client from "./api/client.gen.js";
4
4
  /**
5
5
  * ConnectOpts defines option used to connect to an engine.
6
6
  */
7
7
  export interface ConnectOpts {
8
+ /**
9
+ * Use to overwrite Dagger workdir
10
+ * @defaultValue process.cwd()
11
+ */
8
12
  Workdir?: string;
13
+ /**
14
+ * Use to overwrite Dagger config
15
+ * @defaultValue dagger.json
16
+ */
9
17
  ConfigPath?: string;
18
+ /**
19
+ * Enable logs output
20
+ * @example
21
+ * LogOutput
22
+ * ```ts
23
+ * connect(async (client: Client) => {
24
+ const source = await client.host().workdir().id()
25
+ ...
26
+ }, {LogOutput: process.stdout})
27
+ ```
28
+ */
10
29
  LogOutput?: Writable;
11
30
  }
12
31
  export type CallbackFct = (client: Client) => Promise<void>;
32
+ export interface ConnectParams {
33
+ port: number;
34
+ session_token: string;
35
+ }
13
36
  /**
14
37
  * connect runs GraphQL server and initializes a
15
38
  * GraphQL client to execute query on it through its callback.
@@ -1 +1 @@
1
- {"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../connect.ts"],"names":[],"mappings":";AAAA,OAAO,MAAM,MAAM,qBAAqB,CAAA;AAExC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAEtC;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,SAAS,CAAC,EAAE,QAAQ,CAAA;CACrB;AAED,MAAM,MAAM,WAAW,GAAG,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;AAE3D;;;;GAIG;AACH,wBAAsB,OAAO,CAC3B,EAAE,EAAE,WAAW,EACf,MAAM,GAAE,WAAgB,GACvB,OAAO,CAAC,IAAI,CAAC,CAgBf"}
1
+ {"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../connect.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAEtC,OAAO,MAAM,MAAM,qBAAqB,CAAA;AAGxC;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB;;;;;;;;;;OAUG;IACH,SAAS,CAAC,EAAE,QAAQ,CAAA;CACrB;AAED,MAAM,MAAM,WAAW,GAAG,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;AAE3D,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAA;IACZ,aAAa,EAAE,MAAM,CAAA;CACtB;AAED;;;;GAIG;AACH,wBAAsB,OAAO,CAC3B,EAAE,EAAE,WAAW,EACf,MAAM,GAAE,WAAgB,GACvB,OAAO,CAAC,IAAI,CAAC,CAuCf"}
package/dist/connect.js CHANGED
@@ -7,7 +7,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { getProvisioner, DEFAULT_HOST } from "./provisioning/index.js";
10
+ import Client from "./api/client.gen.js";
11
+ import { Bin, CLI_VERSION } from "./provisioning/index.js";
11
12
  /**
12
13
  * connect runs GraphQL server and initializes a
13
14
  * GraphQL client to execute query on it through its callback.
@@ -18,9 +19,32 @@ export function connect(cb, config = {}) {
18
19
  // Create config with default values that may be overridden
19
20
  // by config if values are set.
20
21
  const _config = Object.assign({ Workdir: process.env["DAGGER_WORKDIR"] || process.cwd(), ConfigPath: process.env["DAGGER_CONFIG"] || "./dagger.json" }, config);
21
- // set host to be DAGGER_HOST env otherwise to provisioning defaults
22
- const host = process.env["DAGGER_HOST"] || DEFAULT_HOST;
23
- const provisioner = getProvisioner(host);
24
- yield cb(yield provisioner.Connect(_config)).finally(() => __awaiter(this, void 0, void 0, function* () { return provisioner.Close(); }));
22
+ let client;
23
+ let close = null;
24
+ // Prefer DAGGER_SESSION_PORT if set
25
+ const daggerSessionPort = process.env["DAGGER_SESSION_PORT"];
26
+ if (daggerSessionPort) {
27
+ const sessionToken = process.env["DAGGER_SESSION_TOKEN"];
28
+ if (!sessionToken) {
29
+ throw new Error("DAGGER_SESSION_TOKEN must be set when using DAGGER_SESSION_PORT");
30
+ }
31
+ client = new Client({
32
+ host: `127.0.0.1:${daggerSessionPort}`,
33
+ sessionToken: sessionToken,
34
+ });
35
+ }
36
+ else {
37
+ // Otherwise, prefer _EXPERIMENTAL_DAGGER_CLI_BIN, with fallback behavior of
38
+ // downloading the CLI and using that as the bin.
39
+ const cliBin = process.env["_EXPERIMENTAL_DAGGER_CLI_BIN"];
40
+ const engineConn = new Bin(cliBin, CLI_VERSION);
41
+ client = yield engineConn.Connect(_config);
42
+ close = () => engineConn.Close();
43
+ }
44
+ yield cb(client).finally(() => __awaiter(this, void 0, void 0, function* () {
45
+ if (close) {
46
+ close();
47
+ }
48
+ }));
25
49
  });
26
50
  }
package/dist/index.d.ts CHANGED
@@ -2,6 +2,5 @@ import Client from "./api/client.gen.js";
2
2
  export { gql } from "graphql-tag";
3
3
  export { GraphQLClient } from "graphql-request";
4
4
  export { connect, ConnectOpts, CallbackFct } from "./connect.js";
5
- export { getProvisioner } from "./provisioning/index.js";
6
5
  export default Client;
7
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,qBAAqB,CAAA;AAExC,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACjC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAE/C,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AAExD,eAAe,MAAM,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,qBAAqB,CAAA;AAExC,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACjC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAE/C,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAEhE,eAAe,MAAM,CAAA"}
package/dist/index.js CHANGED
@@ -2,5 +2,4 @@ import Client from "./api/client.gen.js";
2
2
  export { gql } from "graphql-tag";
3
3
  export { GraphQLClient } from "graphql-request";
4
4
  export { connect } from "./connect.js";
5
- export { getProvisioner } from "./provisioning/index.js";
6
5
  export default Client;
@@ -1,37 +1,45 @@
1
- import { ConnectOpts, EngineConn } from "../engineconn.js";
2
- import Client from "../../api/client.gen.js";
1
+ import Client from "../api/client.gen.js";
2
+ import { ConnectOpts, EngineConn } from "./engineconn.js";
3
3
  /**
4
- * DockerImage is an implementation of EngineConn to set up a Dagger
5
- * Engine session from a pulled docker image.
4
+ * Bin runs an engine session from a specified binary
6
5
  */
7
- export declare class DockerImage implements EngineConn {
8
- private imageRef;
9
- private readonly cacheDir;
10
- private readonly ENGINE_SESSION_BINARY_PREFIX;
6
+ export declare class Bin implements EngineConn {
11
7
  private subProcess?;
12
- constructor(u: URL);
13
- /**
14
- * Generate a unix timestamp in nanosecond
15
- */
16
- private getRandomId;
8
+ private binPath?;
9
+ private cliVersion?;
10
+ private readonly cacheDir;
11
+ private readonly DAGGER_CLI_BIN_PREFIX;
12
+ constructor(binPath?: string, cliVersion?: string);
17
13
  Addr(): string;
18
14
  Connect(opts: ConnectOpts): Promise<Client>;
15
+ private downloadCLI;
16
+ /**
17
+ * runEngineSession execute the engine binary and set up a GraphQL client that
18
+ * target this engine.
19
+ */
20
+ private runEngineSession;
21
+ private readConnectParams;
22
+ Close(): Promise<void>;
19
23
  /**
20
24
  * createCacheDir will create a cache directory on user
21
25
  * host to store dagger binary.
22
26
  *
23
- * If set, it will use XDG directory, if not, it will use `$HOME/.cache`
24
- * as base path.
27
+ * If set, it will use envPaths to determine system's cache directory,
28
+ * if not, it will use `$HOME/.cache` as base path.
25
29
  * Nothing happens if the directory already exists.
26
30
  */
27
31
  private createCacheDir;
28
32
  /**
29
- * buildBinPath create a path to output engine session binary.
33
+ * buildBinPath create a path to output dagger cli binary.
30
34
  *
31
35
  * It will store it in the cache directory with a name composed
32
36
  * of the base engine session as constant and the engine identifier.
33
37
  */
34
38
  private buildBinPath;
39
+ /**
40
+ * buildExePath create a path to output dagger cli binary.
41
+ */
42
+ private buildOsExePath;
35
43
  /**
36
44
  * normalizedArch returns the architecture name used by the rest of our SDKs.
37
45
  */
@@ -40,19 +48,17 @@ export declare class DockerImage implements EngineConn {
40
48
  * normalizedOS returns the os name used by the rest of our SDKs.
41
49
  */
42
50
  private normalizedOS;
51
+ private cliArchiveName;
52
+ private cliArchiveURL;
53
+ private cliChecksumURL;
54
+ private checksumMap;
55
+ private expectedChecksum;
56
+ private extractArchive;
43
57
  /**
44
- * pullEngineSessionBin will retrieve Dagger binary from its docker image
45
- * and copy it to the local host.
46
- * This function automatically resolves host's platform to copy the correct
47
- * binary.
48
- */
49
- private pullEngineSessionBin;
50
- /**
51
- * runEngineSession execute the engine binary and set up a GraphQL client that
52
- * target this engine.
58
+ * Generate a unix timestamp in nanosecond
53
59
  */
54
- private runEngineSession;
55
- private readPort;
56
- Close(): Promise<void>;
60
+ private getRandomId;
57
61
  }
58
- //# sourceMappingURL=image.d.ts.map
62
+ export declare function _overrideCLIURL(url: string): void;
63
+ export declare function _overrideCLIChecksumsURL(url: string): void;
64
+ //# sourceMappingURL=bin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bin.d.ts","sourceRoot":"","sources":["../../provisioning/bin.ts"],"names":[],"mappings":"AAYA,OAAO,MAAM,MAAM,sBAAsB,CAAA;AAQzC,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAMzD;;GAEG;AACH,qBAAa,GAAI,YAAW,UAAU;IACpC,OAAO,CAAC,UAAU,CAAC,CAAmB;IAEtC,OAAO,CAAC,OAAO,CAAC,CAAQ;IACxB,OAAO,CAAC,UAAU,CAAC,CAAQ;IAE3B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAKxB;IAED,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAAW;gBAErC,OAAO,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;IAKjD,IAAI,IAAI,MAAM;IAIR,OAAO,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC;YAOnC,WAAW;IAgEzB;;;OAGG;YACW,gBAAgB;YAgDhB,iBAAiB;IAmBzB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAQ5B;;;;;;;OAOG;IACH,OAAO,CAAC,cAAc;IAItB;;;;;OAKG;IACH,OAAO,CAAC,YAAY;IAOpB;;OAEG;IACH,OAAO,CAAC,cAAc;IAWtB;;OAEG;IACH,OAAO,CAAC,cAAc;IAStB;;OAEG;IACH,OAAO,CAAC,YAAY;IASpB,OAAO,CAAC,cAAc;IAatB,OAAO,CAAC,aAAa;IASrB,OAAO,CAAC,cAAc;YAOR,WAAW;YAkBX,gBAAgB;YAWhB,cAAc;IA4C5B;;OAEG;IACH,OAAO,CAAC,WAAW;CAGpB;AAGD,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAEjD;AAGD,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAE1D"}
@@ -0,0 +1,336 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
11
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
12
+ var m = o[Symbol.asyncIterator], i;
13
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
14
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
15
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
16
+ };
17
+ import AdmZip from "adm-zip";
18
+ import * as crypto from "crypto";
19
+ import envPaths from "env-paths";
20
+ import { execaCommand } from "execa";
21
+ import * as fs from "fs";
22
+ import fetch from "node-fetch";
23
+ import * as os from "os";
24
+ import * as path from "path";
25
+ import * as process from "process";
26
+ import readline from "readline";
27
+ import * as tar from "tar";
28
+ import Client from "../api/client.gen.js";
29
+ import { EngineSessionConnectionTimeoutError, EngineSessionConnectParamsParseError, EngineSessionEOFError, InitEngineSessionBinaryError, } from "../common/errors/index.js";
30
+ const CLI_HOST = "dl.dagger.io";
31
+ let OVERRIDE_CLI_URL;
32
+ let OVERRIDE_CHECKSUMS_URL;
33
+ /**
34
+ * Bin runs an engine session from a specified binary
35
+ */
36
+ export class Bin {
37
+ constructor(binPath, cliVersion) {
38
+ var _a;
39
+ this.cacheDir = path.join(`${((_a = process.env.XDG_CACHE_HOME) === null || _a === void 0 ? void 0 : _a.trim()) || envPaths("", { suffix: "" }).cache}`, "dagger");
40
+ this.DAGGER_CLI_BIN_PREFIX = "dagger";
41
+ this.binPath = binPath;
42
+ this.cliVersion = cliVersion;
43
+ }
44
+ Addr() {
45
+ return "http://dagger";
46
+ }
47
+ Connect(opts) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ if (!this.binPath) {
50
+ this.binPath = yield this.downloadCLI();
51
+ }
52
+ return this.runEngineSession(this.binPath, opts);
53
+ });
54
+ }
55
+ downloadCLI() {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ if (!this.cliVersion) {
58
+ throw new Error("cliVersion is not set");
59
+ }
60
+ const binPath = this.buildBinPath();
61
+ // Create a temporary bin file path
62
+ this.createCacheDir();
63
+ const tmpBinDownloadDir = fs.mkdtempSync(path.join(this.cacheDir, `temp-${this.getRandomId()}`));
64
+ const tmpBinPath = this.buildOsExePath(tmpBinDownloadDir, this.DAGGER_CLI_BIN_PREFIX);
65
+ try {
66
+ // download an archive and use appropriate extraction depending on platforms (zip on windows, tar.gz on other platforms)
67
+ const actualChecksum = yield this.extractArchive(tmpBinDownloadDir, this.normalizedOS());
68
+ const expectedChecksum = yield this.expectedChecksum();
69
+ if (actualChecksum !== expectedChecksum) {
70
+ throw new Error(`checksum mismatch: expected ${expectedChecksum}, got ${actualChecksum}`);
71
+ }
72
+ fs.chmodSync(tmpBinPath, 0o700);
73
+ fs.renameSync(tmpBinPath, binPath);
74
+ fs.rmSync(tmpBinDownloadDir, { recursive: true });
75
+ }
76
+ catch (e) {
77
+ fs.rmSync(tmpBinDownloadDir, { recursive: true });
78
+ throw new InitEngineSessionBinaryError(`failed to download dagger cli binary: ${e}`, { cause: e });
79
+ }
80
+ // Remove all temporary binary files
81
+ // Ignore current dagger cli or other files that have not be
82
+ // created by this SDK.
83
+ try {
84
+ const files = fs.readdirSync(this.cacheDir);
85
+ files.forEach((file) => {
86
+ const filePath = path.join(this.cacheDir, file);
87
+ if (filePath === binPath ||
88
+ !file.startsWith(this.DAGGER_CLI_BIN_PREFIX)) {
89
+ return;
90
+ }
91
+ fs.unlinkSync(filePath);
92
+ });
93
+ }
94
+ catch (e) {
95
+ // Log the error but do not interrupt program.
96
+ console.error("could not clean up temporary binary files");
97
+ }
98
+ return binPath;
99
+ });
100
+ }
101
+ /**
102
+ * runEngineSession execute the engine binary and set up a GraphQL client that
103
+ * target this engine.
104
+ */
105
+ runEngineSession(binPath, opts) {
106
+ return __awaiter(this, void 0, void 0, function* () {
107
+ const args = [binPath, "session"];
108
+ if (opts.Workdir) {
109
+ args.push("--workdir", opts.Workdir);
110
+ }
111
+ if (opts.Project) {
112
+ args.push("--project", opts.Project);
113
+ }
114
+ this.subProcess = execaCommand(args.join(" "), {
115
+ stderr: opts.LogOutput || "ignore",
116
+ reject: true,
117
+ // Kill the process if parent exit.
118
+ cleanup: true,
119
+ });
120
+ const stdoutReader = readline.createInterface({
121
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
122
+ input: this.subProcess.stdout,
123
+ });
124
+ const timeOutDuration = 300000;
125
+ const connectParams = (yield Promise.race([
126
+ this.readConnectParams(stdoutReader),
127
+ new Promise((_, reject) => {
128
+ setTimeout(() => {
129
+ reject(new EngineSessionConnectionTimeoutError("timeout reading connect params from engine session", { timeOutDuration }));
130
+ }, timeOutDuration).unref(); // long timeout to account for extensions, though that should be optimized in future
131
+ }),
132
+ ]));
133
+ return new Client({
134
+ host: `127.0.0.1:${connectParams.port}`,
135
+ sessionToken: connectParams.session_token,
136
+ });
137
+ });
138
+ }
139
+ readConnectParams(stdoutReader) {
140
+ var _a, stdoutReader_1, stdoutReader_1_1;
141
+ var _b, e_1, _c, _d;
142
+ return __awaiter(this, void 0, void 0, function* () {
143
+ try {
144
+ for (_a = true, stdoutReader_1 = __asyncValues(stdoutReader); stdoutReader_1_1 = yield stdoutReader_1.next(), _b = stdoutReader_1_1.done, !_b;) {
145
+ _d = stdoutReader_1_1.value;
146
+ _a = false;
147
+ try {
148
+ const line = _d;
149
+ // parse the the line as json-encoded connect params
150
+ const connectParams = JSON.parse(line);
151
+ if (connectParams.port && connectParams.session_token) {
152
+ return connectParams;
153
+ }
154
+ throw new EngineSessionConnectParamsParseError(`invalid connect params: ${line}`, { parsedLine: line });
155
+ }
156
+ finally {
157
+ _a = true;
158
+ }
159
+ }
160
+ }
161
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
162
+ finally {
163
+ try {
164
+ if (!_a && !_b && (_c = stdoutReader_1.return)) yield _c.call(stdoutReader_1);
165
+ }
166
+ finally { if (e_1) throw e_1.error; }
167
+ }
168
+ throw new EngineSessionEOFError("No line was found to parse the engine connect params");
169
+ });
170
+ }
171
+ Close() {
172
+ var _a;
173
+ return __awaiter(this, void 0, void 0, function* () {
174
+ if ((_a = this.subProcess) === null || _a === void 0 ? void 0 : _a.pid) {
175
+ this.subProcess.kill("SIGTERM", {
176
+ forceKillAfterTimeout: 2000,
177
+ });
178
+ }
179
+ });
180
+ }
181
+ /**
182
+ * createCacheDir will create a cache directory on user
183
+ * host to store dagger binary.
184
+ *
185
+ * If set, it will use envPaths to determine system's cache directory,
186
+ * if not, it will use `$HOME/.cache` as base path.
187
+ * Nothing happens if the directory already exists.
188
+ */
189
+ createCacheDir() {
190
+ fs.mkdirSync(this.cacheDir, { mode: 0o700, recursive: true });
191
+ }
192
+ /**
193
+ * buildBinPath create a path to output dagger cli binary.
194
+ *
195
+ * It will store it in the cache directory with a name composed
196
+ * of the base engine session as constant and the engine identifier.
197
+ */
198
+ buildBinPath() {
199
+ return this.buildOsExePath(this.cacheDir, `${this.DAGGER_CLI_BIN_PREFIX}-${this.cliVersion}`);
200
+ }
201
+ /**
202
+ * buildExePath create a path to output dagger cli binary.
203
+ */
204
+ buildOsExePath(destinationDir, filename) {
205
+ const binPath = path.join(destinationDir, filename);
206
+ switch (this.normalizedOS()) {
207
+ case "windows":
208
+ return `${binPath}.exe`;
209
+ default:
210
+ return binPath;
211
+ }
212
+ }
213
+ /**
214
+ * normalizedArch returns the architecture name used by the rest of our SDKs.
215
+ */
216
+ normalizedArch() {
217
+ switch (os.arch()) {
218
+ case "x64":
219
+ return "amd64";
220
+ default:
221
+ return os.arch();
222
+ }
223
+ }
224
+ /**
225
+ * normalizedOS returns the os name used by the rest of our SDKs.
226
+ */
227
+ normalizedOS() {
228
+ switch (os.platform()) {
229
+ case "win32":
230
+ return "windows";
231
+ default:
232
+ return os.platform();
233
+ }
234
+ }
235
+ cliArchiveName() {
236
+ if (OVERRIDE_CLI_URL) {
237
+ return path.basename(new URL(OVERRIDE_CLI_URL).pathname);
238
+ }
239
+ let ext = "tar.gz";
240
+ if (this.normalizedOS() === "windows") {
241
+ ext = "zip";
242
+ }
243
+ return `dagger_v${this.cliVersion}_${this.normalizedOS()}_${this.normalizedArch()}.${ext}`;
244
+ }
245
+ cliArchiveURL() {
246
+ if (OVERRIDE_CLI_URL) {
247
+ return OVERRIDE_CLI_URL;
248
+ }
249
+ return `https://${CLI_HOST}/dagger/releases/${this.cliVersion}/${this.cliArchiveName()}`;
250
+ }
251
+ cliChecksumURL() {
252
+ if (OVERRIDE_CHECKSUMS_URL) {
253
+ return OVERRIDE_CHECKSUMS_URL;
254
+ }
255
+ return `https://${CLI_HOST}/dagger/releases/${this.cliVersion}/checksums.txt`;
256
+ }
257
+ checksumMap() {
258
+ return __awaiter(this, void 0, void 0, function* () {
259
+ // download checksums.txt
260
+ const checksums = yield fetch(this.cliChecksumURL());
261
+ if (!checksums.ok) {
262
+ throw new Error(`failed to download checksums.txt from ${this.cliChecksumURL()}`);
263
+ }
264
+ const checksumsText = yield checksums.text();
265
+ // iterate over lines filling in map of filename -> checksum
266
+ const checksumMap = new Map();
267
+ for (const line of checksumsText.split("\n")) {
268
+ const [checksum, filename] = line.split(/\s+/);
269
+ checksumMap.set(filename, checksum);
270
+ }
271
+ return checksumMap;
272
+ });
273
+ }
274
+ expectedChecksum() {
275
+ return __awaiter(this, void 0, void 0, function* () {
276
+ const checksumMap = yield this.checksumMap();
277
+ const expectedChecksum = checksumMap.get(this.cliArchiveName());
278
+ if (!expectedChecksum) {
279
+ throw new Error(`failed to find checksum for ${this.cliArchiveName()} in checksums.txt`);
280
+ }
281
+ return expectedChecksum;
282
+ });
283
+ }
284
+ extractArchive(destDir, os) {
285
+ return __awaiter(this, void 0, void 0, function* () {
286
+ // extract the dagger binary in the cli archive and return the archive of the .zip for windows and .tar.gz for other plateforms
287
+ const archiveResp = yield fetch(this.cliArchiveURL());
288
+ if (!archiveResp.ok) {
289
+ throw new Error(`failed to download dagger cli archive from ${this.cliArchiveURL()}`);
290
+ }
291
+ if (!archiveResp.body) {
292
+ throw new Error("archive response body is null");
293
+ }
294
+ // create a temporary file to store the archive
295
+ const archivePath = path.join(destDir, os === "windows" ? "dagger.zip" : "dagger.tar.gz");
296
+ const archiveFile = fs.createWriteStream(archivePath);
297
+ yield new Promise((resolve, reject) => {
298
+ var _a, _b;
299
+ (_a = archiveResp.body) === null || _a === void 0 ? void 0 : _a.pipe(archiveFile);
300
+ (_b = archiveResp.body) === null || _b === void 0 ? void 0 : _b.on("error", reject);
301
+ archiveFile.on("finish", resolve);
302
+ });
303
+ const actualChecksum = crypto
304
+ .createHash("sha256")
305
+ .update(fs.readFileSync(archivePath))
306
+ .digest("hex");
307
+ if (os === "windows") {
308
+ const zip = new AdmZip(archivePath);
309
+ // extract just dagger.exe to the destdir
310
+ zip.extractEntryTo("dagger.exe", destDir, false, true);
311
+ }
312
+ else {
313
+ tar.extract({
314
+ cwd: destDir,
315
+ file: archivePath,
316
+ sync: true,
317
+ });
318
+ }
319
+ return actualChecksum;
320
+ });
321
+ }
322
+ /**
323
+ * Generate a unix timestamp in nanosecond
324
+ */
325
+ getRandomId() {
326
+ return process.hrtime.bigint().toString();
327
+ }
328
+ }
329
+ // Only meant for tests
330
+ export function _overrideCLIURL(url) {
331
+ OVERRIDE_CLI_URL = url;
332
+ }
333
+ // Only meant for tests
334
+ export function _overrideCLIChecksumsURL(url) {
335
+ OVERRIDE_CHECKSUMS_URL = url;
336
+ }
@@ -1,2 +1,2 @@
1
- export declare const DEFAULT_HOST: string;
1
+ export declare const CLI_VERSION = "0.3.10";
2
2
  //# sourceMappingURL=default.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"default.d.ts","sourceRoot":"","sources":["../../provisioning/default.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,YAAY,QAAwC,CAAA"}
1
+ {"version":3,"file":"default.d.ts","sourceRoot":"","sources":["../../provisioning/default.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,WAAW,WAAW,CAAA"}
@@ -1,3 +1,2 @@
1
1
  // Code generated by dagger. DO NOT EDIT.
2
- const DEFAULT_IMAGE_REF = "ghcr.io/dagger/engine:v0.3.7@sha256:9ad18aca71b1b1bae137d3ac5b8d1c62bd548d4f3b01c42b48120fdbc1490f22";
3
- export const DEFAULT_HOST = `docker-image://${DEFAULT_IMAGE_REF}`;
2
+ export const CLI_VERSION = "0.3.10";
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
- import Client from "../api/client.gen.js";
3
2
  import { Writable } from "node:stream";
3
+ import Client from "../api/client.gen.js";
4
4
  export interface ConnectOpts {
5
5
  Workdir?: string;
6
6
  Project?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"engineconn.d.ts","sourceRoot":"","sources":["../../provisioning/engineconn.ts"],"names":[],"mappings":";AAAA,OAAO,MAAM,MAAM,sBAAsB,CAAA;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAEtC,MAAM,WAAW,WAAW;IAC1B,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,SAAS,CAAC,EAAE,QAAQ,CAAA;IACpB,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,UAAU;IACzB;;OAEG;IACH,IAAI,EAAE,MAAM,MAAM,CAAA;IAElB;;;OAGG;IACH,OAAO,EAAE,CAAC,IAAI,EAAE,WAAW,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;IAE/C;;OAEG;IACH,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAC3B"}
1
+ {"version":3,"file":"engineconn.d.ts","sourceRoot":"","sources":["../../provisioning/engineconn.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAEtC,OAAO,MAAM,MAAM,sBAAsB,CAAA;AAEzC,MAAM,WAAW,WAAW;IAC1B,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,SAAS,CAAC,EAAE,QAAQ,CAAA;IACpB,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,UAAU;IACzB;;OAEG;IACH,IAAI,EAAE,MAAM,MAAM,CAAA;IAElB;;;OAGG;IACH,OAAO,EAAE,CAAC,IAAI,EAAE,WAAW,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;IAE/C;;OAEG;IACH,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAC3B"}
@@ -1,5 +1,4 @@
1
- export * from "./docker-provision/index.js";
2
1
  export * from "./default.js";
3
2
  export * from "./engineconn.js";
4
- export * from "./provisioner.js";
3
+ export * from "./bin.js";
5
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../provisioning/index.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAA;AAC3C,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../provisioning/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,UAAU,CAAA"}
@@ -1,4 +1,3 @@
1
- export * from "./docker-provision/index.js";
2
1
  export * from "./default.js";
3
2
  export * from "./engineconn.js";
4
- export * from "./provisioner.js";
3
+ export * from "./bin.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dagger.io/dagger",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "author": "hello@dagger.io",
5
5
  "license": "Apache-2.0",
6
6
  "types": "./dist/index.d.ts",
@@ -17,31 +17,37 @@
17
17
  "main": "dist/index.js",
18
18
  "dependencies": {
19
19
  "@lifeomic/axios-fetch": "^3.0.1",
20
- "axios": "^1.2.0",
20
+ "adm-zip": "^0.5.10",
21
+ "env-paths": "^3.0.0",
21
22
  "execa": "^6.1.0",
22
23
  "graphql": "^16.5.0",
23
- "graphql-request": "^5.0.0",
24
+ "graphql-request": "^5.1.0",
24
25
  "graphql-tag": "^2.12.6",
25
26
  "node-color-log": "^10.0.2",
26
- "node-fetch": "^3.3.0"
27
+ "node-fetch": "^3.3.0",
28
+ "tar": "^6.1.13"
27
29
  },
28
30
  "scripts": {
29
31
  "build": "tsc",
32
+ "watch": "tsc -w",
30
33
  "test": "mocha",
31
34
  "lint": "yarn eslint --max-warnings=0 .",
32
35
  "fmt": "yarn eslint --fix"
33
36
  },
34
37
  "devDependencies": {
38
+ "@trivago/prettier-plugin-sort-imports": "^4.0.0",
39
+ "@types/adm-zip": "^0.5.0",
35
40
  "@types/mocha": "latest",
36
41
  "@types/node": "~16",
37
- "@typescript-eslint/eslint-plugin": "^5.45.1",
38
- "@typescript-eslint/parser": "^5.45.1",
39
- "eslint": "^8.29.0",
40
- "eslint-config-prettier": "^8.5.0",
42
+ "@types/tar": "^6.1.3",
43
+ "@typescript-eslint/eslint-plugin": "^5.48.2",
44
+ "@typescript-eslint/parser": "^5.49.0",
45
+ "eslint": "^8.32.0",
46
+ "eslint-config-prettier": "^8.6.0",
41
47
  "eslint-plugin-prettier": "^4.2.1",
42
- "mocha": "^10.1.0",
43
- "prettier": "^2.8.0",
48
+ "mocha": "^10.2.0",
49
+ "prettier": "^2.8.3",
44
50
  "ts-node": "^10.9.1",
45
- "typescript": "^4.9.3"
51
+ "typescript": "^4.9.4"
46
52
  }
47
53
  }