@project-ajax/sdk 0.0.60 → 0.0.61

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 (142) hide show
  1. package/package.json +3 -11
  2. package/dist/cli/api/client.d.ts +0 -212
  3. package/dist/cli/api/client.d.ts.map +0 -1
  4. package/dist/cli/api/client.js +0 -330
  5. package/dist/cli/api/result.d.ts +0 -43
  6. package/dist/cli/api/result.d.ts.map +0 -1
  7. package/dist/cli/api/result.js +0 -43
  8. package/dist/cli/bin/cli.d.ts +0 -3
  9. package/dist/cli/bin/cli.d.ts.map +0 -1
  10. package/dist/cli/bin/cli.js +0 -5
  11. package/dist/cli/commands/auth.d.ts +0 -2
  12. package/dist/cli/commands/auth.d.ts.map +0 -1
  13. package/dist/cli/commands/auth.impl.d.ts +0 -5
  14. package/dist/cli/commands/auth.impl.d.ts.map +0 -1
  15. package/dist/cli/commands/auth.impl.js +0 -45
  16. package/dist/cli/commands/auth.impl.test.d.ts +0 -2
  17. package/dist/cli/commands/auth.impl.test.d.ts.map +0 -1
  18. package/dist/cli/commands/auth.js +0 -56
  19. package/dist/cli/commands/bundle.d.ts +0 -2
  20. package/dist/cli/commands/bundle.d.ts.map +0 -1
  21. package/dist/cli/commands/bundle.impl.d.ts +0 -2
  22. package/dist/cli/commands/bundle.impl.d.ts.map +0 -1
  23. package/dist/cli/commands/bundle.impl.js +0 -21
  24. package/dist/cli/commands/bundle.impl.test.d.ts +0 -2
  25. package/dist/cli/commands/bundle.impl.test.d.ts.map +0 -1
  26. package/dist/cli/commands/bundle.js +0 -23
  27. package/dist/cli/commands/capabilities.d.ts +0 -2
  28. package/dist/cli/commands/capabilities.d.ts.map +0 -1
  29. package/dist/cli/commands/capabilities.impl.d.ts +0 -3
  30. package/dist/cli/commands/capabilities.impl.d.ts.map +0 -1
  31. package/dist/cli/commands/capabilities.impl.js +0 -40
  32. package/dist/cli/commands/capabilities.js +0 -24
  33. package/dist/cli/commands/connect.d.ts +0 -2
  34. package/dist/cli/commands/connect.d.ts.map +0 -1
  35. package/dist/cli/commands/connect.impl.d.ts +0 -6
  36. package/dist/cli/commands/connect.impl.d.ts.map +0 -1
  37. package/dist/cli/commands/connect.impl.js +0 -116
  38. package/dist/cli/commands/connect.js +0 -78
  39. package/dist/cli/commands/deploy.d.ts +0 -3
  40. package/dist/cli/commands/deploy.d.ts.map +0 -1
  41. package/dist/cli/commands/deploy.impl.d.ts +0 -6
  42. package/dist/cli/commands/deploy.impl.d.ts.map +0 -1
  43. package/dist/cli/commands/deploy.impl.js +0 -60
  44. package/dist/cli/commands/deploy.impl.test.d.ts +0 -2
  45. package/dist/cli/commands/deploy.impl.test.d.ts.map +0 -1
  46. package/dist/cli/commands/deploy.js +0 -22
  47. package/dist/cli/commands/env.d.ts +0 -2
  48. package/dist/cli/commands/env.d.ts.map +0 -1
  49. package/dist/cli/commands/env.impl.d.ts +0 -11
  50. package/dist/cli/commands/env.impl.d.ts.map +0 -1
  51. package/dist/cli/commands/env.impl.js +0 -62
  52. package/dist/cli/commands/env.js +0 -39
  53. package/dist/cli/commands/exec.d.ts +0 -3
  54. package/dist/cli/commands/exec.d.ts.map +0 -1
  55. package/dist/cli/commands/exec.impl.d.ts +0 -7
  56. package/dist/cli/commands/exec.impl.d.ts.map +0 -1
  57. package/dist/cli/commands/exec.impl.js +0 -123
  58. package/dist/cli/commands/exec.js +0 -30
  59. package/dist/cli/commands/runs.d.ts +0 -2
  60. package/dist/cli/commands/runs.d.ts.map +0 -1
  61. package/dist/cli/commands/runs.impl.d.ts +0 -4
  62. package/dist/cli/commands/runs.impl.d.ts.map +0 -1
  63. package/dist/cli/commands/runs.impl.js +0 -71
  64. package/dist/cli/commands/runs.js +0 -45
  65. package/dist/cli/commands/secrets.d.ts +0 -2
  66. package/dist/cli/commands/secrets.d.ts.map +0 -1
  67. package/dist/cli/commands/secrets.impl.d.ts +0 -5
  68. package/dist/cli/commands/secrets.impl.d.ts.map +0 -1
  69. package/dist/cli/commands/secrets.impl.js +0 -99
  70. package/dist/cli/commands/secrets.js +0 -64
  71. package/dist/cli/commands/utils/testing.d.ts +0 -13
  72. package/dist/cli/commands/utils/testing.d.ts.map +0 -1
  73. package/dist/cli/commands/utils/testing.js +0 -58
  74. package/dist/cli/config.d.ts +0 -63
  75. package/dist/cli/config.d.ts.map +0 -1
  76. package/dist/cli/config.js +0 -194
  77. package/dist/cli/config.test.d.ts +0 -2
  78. package/dist/cli/config.test.d.ts.map +0 -1
  79. package/dist/cli/context.d.ts +0 -15
  80. package/dist/cli/context.d.ts.map +0 -1
  81. package/dist/cli/context.js +0 -16
  82. package/dist/cli/deploy.d.ts +0 -37
  83. package/dist/cli/deploy.d.ts.map +0 -1
  84. package/dist/cli/deploy.js +0 -100
  85. package/dist/cli/flags.d.ts +0 -21
  86. package/dist/cli/flags.d.ts.map +0 -1
  87. package/dist/cli/flags.js +0 -49
  88. package/dist/cli/handler.d.ts +0 -14
  89. package/dist/cli/handler.d.ts.map +0 -1
  90. package/dist/cli/handler.js +0 -32
  91. package/dist/cli/io.d.ts +0 -55
  92. package/dist/cli/io.d.ts.map +0 -1
  93. package/dist/cli/io.js +0 -96
  94. package/dist/cli/routes.d.ts +0 -2
  95. package/dist/cli/routes.d.ts.map +0 -1
  96. package/dist/cli/routes.js +0 -62
  97. package/dist/cli/utils/array.d.ts +0 -2
  98. package/dist/cli/utils/array.d.ts.map +0 -1
  99. package/dist/cli/utils/array.js +0 -10
  100. package/dist/cli/utils/openUrl.d.ts +0 -4
  101. package/dist/cli/utils/openUrl.d.ts.map +0 -1
  102. package/dist/cli/utils/openUrl.js +0 -43
  103. package/dist/cli/utils/string.d.ts +0 -2
  104. package/dist/cli/utils/string.d.ts.map +0 -1
  105. package/dist/cli/utils/string.js +0 -12
  106. package/src/cli/api/client.ts +0 -628
  107. package/src/cli/api/result.ts +0 -71
  108. package/src/cli/bin/cli.ts +0 -7
  109. package/src/cli/commands/.cursor/rules/testing-commands.mdc +0 -212
  110. package/src/cli/commands/auth.impl.test.ts +0 -228
  111. package/src/cli/commands/auth.impl.ts +0 -56
  112. package/src/cli/commands/auth.ts +0 -63
  113. package/src/cli/commands/bundle.impl.test.ts +0 -143
  114. package/src/cli/commands/bundle.impl.ts +0 -21
  115. package/src/cli/commands/bundle.ts +0 -23
  116. package/src/cli/commands/capabilities.impl.ts +0 -47
  117. package/src/cli/commands/capabilities.ts +0 -25
  118. package/src/cli/commands/connect.impl.ts +0 -149
  119. package/src/cli/commands/connect.ts +0 -80
  120. package/src/cli/commands/deploy.impl.test.ts +0 -254
  121. package/src/cli/commands/deploy.impl.ts +0 -73
  122. package/src/cli/commands/deploy.ts +0 -22
  123. package/src/cli/commands/env.impl.ts +0 -88
  124. package/src/cli/commands/env.ts +0 -38
  125. package/src/cli/commands/exec.impl.ts +0 -171
  126. package/src/cli/commands/exec.ts +0 -32
  127. package/src/cli/commands/runs.impl.ts +0 -87
  128. package/src/cli/commands/runs.ts +0 -49
  129. package/src/cli/commands/secrets.impl.ts +0 -130
  130. package/src/cli/commands/secrets.ts +0 -73
  131. package/src/cli/commands/utils/testing.ts +0 -66
  132. package/src/cli/config.test.ts +0 -108
  133. package/src/cli/config.ts +0 -265
  134. package/src/cli/context.ts +0 -26
  135. package/src/cli/deploy.ts +0 -190
  136. package/src/cli/flags.ts +0 -72
  137. package/src/cli/handler.ts +0 -68
  138. package/src/cli/io.ts +0 -132
  139. package/src/cli/routes.ts +0 -61
  140. package/src/cli/utils/array.ts +0 -7
  141. package/src/cli/utils/openUrl.ts +0 -53
  142. package/src/cli/utils/string.ts +0 -9
@@ -1,66 +0,0 @@
1
- import * as fs from "node:fs";
2
- import { mkdtemp, rm, writeFile } from "node:fs/promises";
3
- import * as os from "node:os";
4
- import { tmpdir } from "node:os";
5
- import * as path from "node:path";
6
- import { Config, type ConfigMap } from "../../config.js";
7
- import type { LocalContext } from "../../context.js";
8
- import type { GlobalFlags } from "../../flags.js";
9
- import { IO } from "../../io.js";
10
-
11
- export const tmpDirectories: string[] = [];
12
-
13
- export const baseFlags: GlobalFlags = {
14
- debug: false,
15
- };
16
-
17
- export async function createAndLoadConfig({
18
- configFile,
19
- env,
20
- flags,
21
- }: {
22
- configFile: Partial<ConfigMap>;
23
- env?: Partial<NodeJS.ProcessEnv>;
24
- flags?: Partial<GlobalFlags>;
25
- }): Promise<[config: Config, path: string]> {
26
- const configFilePath = await createConfigFile(configFile);
27
-
28
- const map = await Config.load({
29
- configFilePath,
30
- processEnv: env ?? {},
31
- flags: {
32
- ...baseFlags,
33
- ...(flags ?? {}),
34
- },
35
- });
36
-
37
- return [map, configFilePath];
38
- }
39
-
40
- export function createBaseContext(): LocalContext {
41
- return {
42
- fs,
43
- io: new IO({ debugEnabled: false }),
44
- os,
45
- path,
46
- process,
47
- };
48
- }
49
-
50
- export async function cleanupTmpDirectories() {
51
- while (tmpDirectories.length > 0) {
52
- const dir = tmpDirectories.pop();
53
- if (dir) {
54
- await rm(dir, { recursive: true, force: true });
55
- }
56
- }
57
- }
58
-
59
- async function createConfigFile(contents: Partial<ConfigMap>) {
60
- const dir = await mkdtemp(path.join(tmpdir(), "cmd-test-"));
61
- tmpDirectories.push(dir);
62
- const configFilePath = path.join(dir, "config.json");
63
-
64
- await writeFile(configFilePath, JSON.stringify(contents, null, 2), "utf-8");
65
- return configFilePath;
66
- }
@@ -1,108 +0,0 @@
1
- import { readFile } from "node:fs/promises";
2
- import { afterEach, describe, expect, it } from "vitest";
3
- import {
4
- cleanupTmpDirectories,
5
- createAndLoadConfig,
6
- } from "./commands/utils/testing.js";
7
- import { Config, type ConfigMap } from "./config.js";
8
-
9
- const baseConfigMap: ConfigMap = {
10
- token: "file-token",
11
- workerId: "file-worker",
12
- environment: "local",
13
- baseUrl: "https://config.example.com",
14
- };
15
-
16
- afterEach(cleanupTmpDirectories);
17
-
18
- describe("Config.load precedence", () => {
19
- it("uses values from the config file when no overrides are present", async () => {
20
- const [config] = await createAndLoadConfig({
21
- configFile: baseConfigMap,
22
- });
23
-
24
- expect(config.token).toBe(baseConfigMap.token);
25
- expect(config.workerId).toBe(baseConfigMap.workerId);
26
- expect(config.environment).toBe(baseConfigMap.environment);
27
- expect(config.baseUrl).toBe(baseConfigMap.baseUrl);
28
- });
29
-
30
- it("prefers environment variables over the config file", async () => {
31
- const envVars = {
32
- WORKERS_TOKEN: "env-token",
33
- WORKERS_WORKER_ID: "env-worker",
34
- WORKERS_ENVIRONMENT: "staging",
35
- };
36
- const [config] = await createAndLoadConfig({
37
- configFile: baseConfigMap,
38
- env: envVars,
39
- });
40
-
41
- expect(config.token).toBe(envVars.WORKERS_TOKEN);
42
- expect(config.workerId).toBe(envVars.WORKERS_WORKER_ID);
43
- expect(config.environment).toBe(envVars.WORKERS_ENVIRONMENT);
44
- expect(config.baseUrl).toBe(baseConfigMap.baseUrl);
45
- });
46
-
47
- it("prefers flags over environment variables and config file values", async () => {
48
- const envVars = {
49
- WORKERS_WORKER_ID: "env-worker",
50
- WORKERS_ENVIRONMENT: "staging",
51
- WORKERS_BASE_URL: "https://env.example.com",
52
- };
53
-
54
- const flags = {
55
- env: "dev" as const,
56
- "base-url": "https://flag.example.com",
57
- };
58
-
59
- const [config] = await createAndLoadConfig({
60
- configFile: baseConfigMap,
61
- env: envVars,
62
- flags,
63
- });
64
-
65
- expect(config.token).toBe(baseConfigMap.token);
66
- expect(config.workerId).toBe(envVars.WORKERS_WORKER_ID);
67
- expect(config.environment).toBe(flags.env);
68
- expect(config.baseUrl).toBe(flags["base-url"]);
69
- });
70
- });
71
-
72
- describe("Config.update", () => {
73
- it("persists only provided keys to disk", async () => {
74
- const [, configFilePath] = await createAndLoadConfig({
75
- configFile: {
76
- token: "file-token",
77
- workerId: "file-worker",
78
- environment: "prod",
79
- baseUrl: "https://config.example.com",
80
- },
81
- });
82
-
83
- const config = await Config.load({
84
- configFilePath,
85
- processEnv: {
86
- WORKERS_BASE_URL: "https://env.example.com",
87
- } as NodeJS.ProcessEnv,
88
- flags: { debug: false },
89
- });
90
-
91
- expect(config.baseUrl).toBe("https://env.example.com");
92
-
93
- await config.update({
94
- token: "updated-token",
95
- });
96
-
97
- expect(config.token).toBe("updated-token");
98
-
99
- const persisted = JSON.parse(await readFile(configFilePath, "utf-8"));
100
-
101
- expect(persisted).toEqual({
102
- token: "updated-token",
103
- workerId: "file-worker",
104
- environment: "prod",
105
- baseUrl: "https://config.example.com",
106
- });
107
- });
108
- });
package/src/cli/config.ts DELETED
@@ -1,265 +0,0 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import type { GlobalFlags } from "./flags.js";
4
-
5
- export const Environments = ["local", "staging", "dev", "prod"] as const;
6
- export type Environment = (typeof Environments)[number];
7
-
8
- export interface ConfigMap {
9
- environment: Environment;
10
- baseUrl: string;
11
- token: string | null;
12
- workerId: string | null;
13
- }
14
-
15
- export function parseEnvironment(name: string): Environment {
16
- if (["local", "staging", "dev", "prod"].includes(name)) {
17
- return name as Environment;
18
- }
19
-
20
- throw new Error(`Invalid environment name: ${name}`);
21
- }
22
-
23
- export class TokenNotSetError extends Error {
24
- constructor(
25
- message: string = "Not authenticated. Run 'workers auth login' first.",
26
- ) {
27
- super(message);
28
- this.name = "TokenNotSetError";
29
- }
30
- }
31
-
32
- /**
33
- * Manages configuration for the CLI.
34
- *
35
- * Environment variables take precedence over the config file.
36
- *
37
- * | Environment Variable | Config File | Description |
38
- * |----------------------|-------------|-------------|
39
- * | WORKERS_TOKEN | token | The token to use for authentication |
40
- * | WORKERS_ENVIRONMENT | environment | The environment to use |
41
- * | WORKERS_WORKER_ID | workerId | The worker ID to use |
42
- * | WORKERS_BASE_URL | baseUrl | The base URL to use |
43
- */
44
- export class Config {
45
- readonly #configMap: ConfigMap;
46
- readonly #configFilePath: string;
47
-
48
- constructor(opts: {
49
- configMap: ConfigMap;
50
- configFilePath: string;
51
- }) {
52
- this.#configMap = opts.configMap;
53
- this.#configFilePath = opts.configFilePath;
54
- }
55
-
56
- // Getters read from the environment variables, and then the config file.
57
-
58
- get baseUrl() {
59
- return this.#configMap.baseUrl;
60
- }
61
-
62
- get token() {
63
- return this.#configMap.token;
64
- }
65
-
66
- get environment() {
67
- return this.#configMap.environment;
68
- }
69
-
70
- get workerId() {
71
- return this.#configMap.workerId;
72
- }
73
-
74
- get tokenInfo(): { token: string; spaceId: string; cellId: string } {
75
- const token = this.token;
76
- if (!token) {
77
- throw new TokenNotSetError();
78
- }
79
-
80
- const { spaceId, cellId } = extractPayloadFromToken(token);
81
- return { token, spaceId, cellId };
82
- }
83
-
84
- /**
85
- * Update the config with a partial config map.
86
- *
87
- * This will write only the updated keys in the config file on disk. Not all
88
- * keys are written, since some current keys in the Config object may have
89
- * come from e.g. environment variables, rather than the original config
90
- * file.
91
- *
92
- * @param config The config update.
93
- */
94
- async update(config: Partial<ConfigMap>) {
95
- Object.assign(this.#configMap, config);
96
-
97
- const currentConfigFile = await Config.#readConfigFile(
98
- this.#configFilePath,
99
- );
100
- Object.assign(currentConfigFile, config);
101
- await fs.promises.writeFile(
102
- this.#configFilePath,
103
- JSON.stringify(currentConfigFile, null, 2),
104
- "utf-8",
105
- );
106
- }
107
-
108
- static async load(opts: {
109
- configFilePath: string;
110
- processEnv: NodeJS.ProcessEnv;
111
- flags: GlobalFlags;
112
- }) {
113
- const absConfigFilePath = path.resolve(process.cwd(), opts.configFilePath);
114
- const partialConfig = await Config.#readConfigFile(absConfigFilePath);
115
-
116
- if (opts.processEnv.WORKERS_TOKEN) {
117
- partialConfig.token = opts.processEnv.WORKERS_TOKEN;
118
- }
119
- if (opts.processEnv.WORKERS_ENVIRONMENT) {
120
- partialConfig.environment = parseEnvironment(
121
- opts.processEnv.WORKERS_ENVIRONMENT,
122
- );
123
- }
124
- if (opts.processEnv.WORKERS_WORKER_ID) {
125
- partialConfig.workerId = opts.processEnv.WORKERS_WORKER_ID;
126
- }
127
- if (opts.processEnv.WORKERS_BASE_URL) {
128
- partialConfig.baseUrl = opts.processEnv.WORKERS_BASE_URL;
129
- }
130
-
131
- if (opts.flags.token) {
132
- partialConfig.token = opts.flags.token;
133
- }
134
- if (opts.flags.env) {
135
- partialConfig.environment = parseEnvironment(opts.flags.env);
136
- }
137
- if (opts.flags["base-url"]) {
138
- partialConfig.baseUrl = opts.flags["base-url"];
139
- }
140
- if (opts.flags["worker-id"]) {
141
- partialConfig.workerId = opts.flags["worker-id"];
142
- }
143
-
144
- partialConfig.environment ??= "prod";
145
- partialConfig.baseUrl ??= baseUrlForEnvironment(partialConfig.environment);
146
-
147
- const environment = partialConfig.environment;
148
- if (!environment) {
149
- throw new Error("Environment is required");
150
- }
151
-
152
- const baseUrl = partialConfig.baseUrl;
153
- if (!baseUrl) {
154
- throw new Error("Base URL is required");
155
- }
156
-
157
- const token = partialConfig.token;
158
- if (token === undefined) {
159
- throw new Error("Token is required");
160
- }
161
-
162
- const workerId = partialConfig.workerId;
163
- if (workerId === undefined) {
164
- throw new Error("Worker ID is required");
165
- }
166
-
167
- const configMap: ConfigMap = {
168
- environment,
169
- baseUrl,
170
- token,
171
- workerId,
172
- };
173
-
174
- return new Config({
175
- configFilePath: absConfigFilePath,
176
- configMap,
177
- });
178
- }
179
-
180
- static async #readConfigFile(configFilePath: string) {
181
- let configFile: Partial<ConfigMap>;
182
- try {
183
- const configContents = await fs.promises.readFile(
184
- configFilePath,
185
- "utf-8",
186
- );
187
-
188
- configFile = JSON.parse(configContents) as Partial<ConfigMap>;
189
- } catch (error) {
190
- if (isENOENT(error)) {
191
- // The default, non-logged in configuration before overrides are
192
- // applied from environment variables, and then flags.
193
- configFile = {
194
- token: null,
195
- workerId: null,
196
- };
197
- } else {
198
- throw error;
199
- }
200
- }
201
-
202
- return configFile;
203
- }
204
- }
205
-
206
- function isENOENT(
207
- error: unknown,
208
- ): error is NodeJS.ErrnoException & { code: "ENOENT" } {
209
- return error instanceof Error && "code" in error && error.code === "ENOENT";
210
- }
211
-
212
- export function extractPayloadFromToken(token: string): {
213
- spaceId: string;
214
- userId: string;
215
- cellId: string;
216
- } {
217
- try {
218
- // Token format: <version>.<type>.<encoded-payload-json>.<encoded-signature>
219
- const parts = token.split(".");
220
- if (parts.length !== 4 || !parts[2]) {
221
- throw new Error("Invalid token format.");
222
- }
223
-
224
- // Decode the payload (third part)
225
- const payloadBase64 = parts[2];
226
- const payloadJson = Buffer.from(payloadBase64, "base64url").toString(
227
- "utf-8",
228
- );
229
- const payload = JSON.parse(payloadJson) as {
230
- spaceId?: string;
231
- userId?: string;
232
- cellId?: string;
233
- };
234
-
235
- if (!payload.spaceId || !payload.userId || !payload.cellId) {
236
- throw new Error(
237
- "Token payload missing required fields (spaceId, userId, cellId).",
238
- );
239
- }
240
-
241
- return {
242
- spaceId: payload.spaceId,
243
- userId: payload.userId,
244
- cellId: payload.cellId,
245
- };
246
- } catch (error) {
247
- if (error instanceof Error && error.message.startsWith("Token")) {
248
- throw error;
249
- }
250
- throw new Error("Failed to parse token payload.");
251
- }
252
- }
253
-
254
- function baseUrlForEnvironment(environment: Environment): string {
255
- switch (environment) {
256
- case "local":
257
- return "http://localhost:3000";
258
- case "staging":
259
- return "https://staging.notion.so";
260
- case "dev":
261
- return "https://dev.notion.so";
262
- case "prod":
263
- return "https://www.notion.so";
264
- }
265
- }
@@ -1,26 +0,0 @@
1
- import * as fs from "node:fs";
2
- import * as os from "node:os";
3
- import * as path from "node:path";
4
- import type { StricliAutoCompleteContext } from "@stricli/auto-complete";
5
- import type { CommandContext } from "@stricli/core";
6
- import { IO } from "./io.js";
7
-
8
- export interface LocalContext
9
- extends CommandContext,
10
- StricliAutoCompleteContext {
11
- readonly fs: typeof fs;
12
- readonly io: IO;
13
- readonly os: typeof os;
14
- readonly path: typeof path;
15
- readonly process: NodeJS.Process;
16
- }
17
-
18
- export function buildContext(process: NodeJS.Process): LocalContext {
19
- return {
20
- fs,
21
- io: new IO({ debugEnabled: false }),
22
- os,
23
- path,
24
- process,
25
- };
26
- }
package/src/cli/deploy.ts DELETED
@@ -1,190 +0,0 @@
1
- // AI-Generated: Significantly generated by model: Claude Sonnet 4.5; agent: AI Agents.
2
-
3
- import * as childProcess from "node:child_process";
4
- import * as fs from "node:fs";
5
- import * as path from "node:path";
6
- import * as esbuild from "esbuild";
7
- import type { ApiError, Environment } from "./api/client.js";
8
- import { Result } from "./api/result.js";
9
- import type { LocalContext } from "./context.js";
10
- import type { AuthedContext } from "./handler.js";
11
-
12
- export interface BaseDeployOptions {
13
- /**
14
- * Path to the worker directory to deploy
15
- */
16
- workerPath: string;
17
-
18
- /**
19
- * API token for authentication
20
- */
21
- token: string;
22
-
23
- /**
24
- * Environment to deploy to
25
- */
26
- environment: Environment;
27
- }
28
-
29
- export interface CreateDeployOptions extends BaseDeployOptions {
30
- /**
31
- * Name of the worker (if it has not yet been created)
32
- */
33
- name: string;
34
- }
35
-
36
- export interface UpdateDeployOptions extends BaseDeployOptions {
37
- /**
38
- * ID of the worker to update
39
- */
40
- workerId: string;
41
- }
42
-
43
- export type DeployOptions = CreateDeployOptions | UpdateDeployOptions;
44
-
45
- /**
46
- * Deploy a worker to the specified environment
47
- */
48
- export async function deployWorker(
49
- context: AuthedContext,
50
- options: DeployOptions,
51
- ): Promise<Result<{ workerId: string }, ApiError>> {
52
- const { workerPath } = options;
53
-
54
- // Resolve absolute path
55
- const absPath = path.resolve(process.cwd(), workerPath);
56
- context.io.debug(`Deploying worker from: ${absPath}`);
57
-
58
- // Create API client
59
- const client = context.apiClient;
60
-
61
- let uploadUrl: string;
62
- let uploadFields: Record<string, string>;
63
- let workerId: string;
64
-
65
- if ("workerId" in options) {
66
- context.io.writeErr(`Updating worker...`);
67
-
68
- workerId = options.workerId;
69
-
70
- const updateResult = await client.updateWorkerBundle(workerId);
71
- if (Result.isSuccess(updateResult)) {
72
- const res = Result.unwrap(updateResult);
73
- uploadUrl = res.url;
74
- uploadFields = res.fields;
75
- } else {
76
- context.io.writeErr(
77
- "Failed to generate pre-signed upload URL for worker bundle:",
78
- );
79
- throw new Error(updateResult.error.message);
80
- }
81
- } else {
82
- context.io.writeErr(`Creating worker...`);
83
-
84
- const createResult = await client.createWorker(options.name);
85
- if (Result.isSuccess(createResult)) {
86
- const res = Result.unwrap(createResult);
87
- uploadUrl = res.url;
88
- uploadFields = res.fields;
89
- workerId = res.worker.workerId;
90
- } else {
91
- console.error("Failed to create worker:");
92
- throw new Error(createResult.error.message);
93
- }
94
- }
95
-
96
- context.io.debug(`Generated upload URL: ${uploadUrl}`);
97
-
98
- // Build the worker bundle
99
- context.io.writeErr("Building worker bundle...");
100
- const archivePath = await buildWorkerBundle(context, absPath);
101
- context.io.debug(`Created bundle at: ${archivePath}`);
102
-
103
- // Upload the bundle
104
- context.io.writeErr("Uploading bundle...");
105
- await uploadBundle(uploadUrl, uploadFields, archivePath);
106
- context.io.debug("Upload complete");
107
-
108
- // Fetch and save capabilities
109
- context.io.writeErr("Fetching and saving worker capabilities...");
110
- const capabilitiesResult = await client.fetchAndSaveCapabilities(workerId);
111
-
112
- if (Result.isSuccess(capabilitiesResult)) {
113
- return Result.success({ workerId });
114
- } else {
115
- return capabilitiesResult;
116
- }
117
- }
118
-
119
- /**
120
- * Build the worker bundle using esbuild and create a tar.gz archive
121
- */
122
- async function buildWorkerBundle(
123
- context: LocalContext,
124
- workerPath: string,
125
- ): Promise<string> {
126
- const entrypoint = "src/index.ts";
127
-
128
- if (!fs.existsSync(path.join(workerPath, entrypoint))) {
129
- throw new Error(`Entrypoint not found: ${entrypoint}`);
130
- }
131
-
132
- // Create temporary output directory
133
- const outdir = fs.mkdtempSync("/tmp/workers-build-");
134
-
135
- // Build with esbuild
136
- esbuild.buildSync({
137
- entryPoints: [entrypoint],
138
- absWorkingDir: workerPath,
139
- bundle: true,
140
- outdir,
141
- platform: "node",
142
- });
143
-
144
- context.io.debug(`Built bundle to: ${outdir}`);
145
-
146
- // Create tar.gz archive
147
- const archiveDir = fs.mkdtempSync("/tmp/workers-archive-");
148
- const archivePath = path.join(archiveDir, "archive.tar.gz");
149
-
150
- childProcess.execSync(`tar -czf ${archivePath} -C ${outdir} .`);
151
-
152
- return archivePath;
153
- }
154
-
155
- /**
156
- * Upload the worker bundle to the pre-signed URL
157
- */
158
- async function uploadBundle(
159
- url: string,
160
- fields: Record<string, string>,
161
- archivePath: string,
162
- ): Promise<void> {
163
- const formData = new FormData();
164
-
165
- // Add all the pre-signed fields
166
- for (const [key, value] of Object.entries(fields)) {
167
- formData.append(key, value);
168
- }
169
-
170
- // Add the file
171
- const fileBuffer = fs.readFileSync(archivePath);
172
- formData.append(
173
- "file",
174
- new Blob([new Uint8Array(fileBuffer)]),
175
- "archive.tar.gz",
176
- );
177
-
178
- // Upload
179
- const response = await fetch(url, {
180
- method: "POST",
181
- body: formData,
182
- });
183
-
184
- if (!response.ok) {
185
- const errorText = await response.text();
186
- throw new Error(
187
- `Failed to upload bundle: ${response.status} ${response.statusText}\n${errorText}`,
188
- );
189
- }
190
- }