@vercel/sandbox 1.9.0 → 1.9.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 (190) hide show
  1. package/README.md +8 -0
  2. package/dist/_virtual/rolldown_runtime.cjs +29 -0
  3. package/dist/api-client/api-client.cjs +375 -0
  4. package/dist/api-client/api-client.cjs.map +1 -0
  5. package/dist/api-client/api-client.d.cts +403 -0
  6. package/dist/api-client/api-client.d.ts +397 -392
  7. package/dist/api-client/api-client.js +365 -405
  8. package/dist/api-client/api-client.js.map +1 -1
  9. package/dist/api-client/api-error.cjs +31 -0
  10. package/dist/api-client/api-error.cjs.map +1 -0
  11. package/dist/api-client/api-error.d.cts +27 -0
  12. package/dist/api-client/api-error.d.ts +19 -16
  13. package/dist/api-client/api-error.js +27 -32
  14. package/dist/api-client/api-error.js.map +1 -1
  15. package/dist/api-client/base-client.cjs +113 -0
  16. package/dist/api-client/base-client.cjs.map +1 -0
  17. package/dist/api-client/base-client.d.cts +38 -0
  18. package/dist/api-client/base-client.d.ts +31 -36
  19. package/dist/api-client/base-client.js +101 -118
  20. package/dist/api-client/base-client.js.map +1 -1
  21. package/dist/api-client/file-writer.cjs +62 -0
  22. package/dist/api-client/file-writer.cjs.map +1 -0
  23. package/dist/api-client/file-writer.d.cts +66 -0
  24. package/dist/api-client/file-writer.d.ts +56 -52
  25. package/dist/api-client/file-writer.js +57 -61
  26. package/dist/api-client/file-writer.js.map +1 -1
  27. package/dist/api-client/index.cjs +2 -0
  28. package/dist/api-client/index.d.ts +2 -2
  29. package/dist/api-client/index.js +4 -21
  30. package/dist/api-client/validators.cjs +149 -0
  31. package/dist/api-client/validators.cjs.map +1 -0
  32. package/dist/api-client/validators.d.cts +1677 -0
  33. package/dist/api-client/validators.d.ts +1501 -2412
  34. package/dist/api-client/validators.js +124 -154
  35. package/dist/api-client/validators.js.map +1 -1
  36. package/dist/api-client/with-retry.cjs +89 -0
  37. package/dist/api-client/with-retry.cjs.map +1 -0
  38. package/dist/api-client/with-retry.d.cts +10 -0
  39. package/dist/api-client/with-retry.d.ts +9 -13
  40. package/dist/api-client/with-retry.js +81 -102
  41. package/dist/api-client/with-retry.js.map +1 -1
  42. package/dist/auth/api.cjs +29 -0
  43. package/dist/auth/api.cjs.map +1 -0
  44. package/dist/auth/api.js +26 -25
  45. package/dist/auth/api.js.map +1 -1
  46. package/dist/auth/error.cjs +13 -0
  47. package/dist/auth/error.cjs.map +1 -0
  48. package/dist/auth/error.js +11 -11
  49. package/dist/auth/error.js.map +1 -1
  50. package/dist/auth/file.cjs +64 -0
  51. package/dist/auth/file.cjs.map +1 -0
  52. package/dist/auth/file.d.cts +26 -0
  53. package/dist/auth/file.d.ts +19 -15
  54. package/dist/auth/file.js +49 -64
  55. package/dist/auth/file.js.map +1 -1
  56. package/dist/auth/index.cjs +12 -0
  57. package/dist/auth/index.d.cts +5 -0
  58. package/dist/auth/index.d.ts +5 -6
  59. package/dist/auth/index.js +6 -27
  60. package/dist/auth/linked-project.cjs +38 -0
  61. package/dist/auth/linked-project.cjs.map +1 -0
  62. package/dist/auth/linked-project.js +30 -64
  63. package/dist/auth/linked-project.js.map +1 -1
  64. package/dist/auth/oauth.cjs +205 -0
  65. package/dist/auth/oauth.cjs.map +1 -0
  66. package/dist/auth/oauth.d.cts +135 -0
  67. package/dist/auth/oauth.d.ts +113 -109
  68. package/dist/auth/oauth.js +185 -252
  69. package/dist/auth/oauth.js.map +1 -1
  70. package/dist/auth/poll-for-token.cjs +82 -0
  71. package/dist/auth/poll-for-token.cjs.map +1 -0
  72. package/dist/auth/poll-for-token.d.cts +28 -0
  73. package/dist/auth/poll-for-token.d.ts +23 -15
  74. package/dist/auth/poll-for-token.js +79 -64
  75. package/dist/auth/poll-for-token.js.map +1 -1
  76. package/dist/auth/project.cjs +80 -0
  77. package/dist/auth/project.cjs.map +1 -0
  78. package/dist/auth/project.d.cts +44 -0
  79. package/dist/auth/project.d.ts +12 -8
  80. package/dist/auth/project.js +70 -72
  81. package/dist/auth/project.js.map +1 -1
  82. package/dist/auth/zod.cjs +22 -0
  83. package/dist/auth/zod.cjs.map +1 -0
  84. package/dist/auth/zod.js +18 -17
  85. package/dist/auth/zod.js.map +1 -1
  86. package/dist/command.cjs +328 -0
  87. package/dist/command.cjs.map +1 -0
  88. package/dist/command.d.cts +289 -0
  89. package/dist/command.d.ts +265 -171
  90. package/dist/command.js +323 -226
  91. package/dist/command.js.map +1 -1
  92. package/dist/constants.d.cts +5 -0
  93. package/dist/constants.d.ts +5 -1
  94. package/dist/index.cjs +11 -0
  95. package/dist/index.d.cts +6 -0
  96. package/dist/index.d.ts +6 -5
  97. package/dist/index.js +6 -15
  98. package/dist/network-policy.d.cts +100 -0
  99. package/dist/network-policy.d.ts +32 -28
  100. package/dist/sandbox.cjs +543 -0
  101. package/dist/sandbox.cjs.map +1 -0
  102. package/dist/sandbox.d.cts +538 -0
  103. package/dist/sandbox.d.ts +521 -479
  104. package/dist/sandbox.js +539 -512
  105. package/dist/sandbox.js.map +1 -1
  106. package/dist/snapshot.cjs +116 -0
  107. package/dist/snapshot.cjs.map +1 -0
  108. package/dist/snapshot.d.cts +109 -0
  109. package/dist/snapshot.d.ts +100 -92
  110. package/dist/snapshot.js +114 -114
  111. package/dist/snapshot.js.map +1 -1
  112. package/dist/utils/array.cjs +17 -0
  113. package/dist/utils/array.cjs.map +1 -0
  114. package/dist/utils/array.js +12 -15
  115. package/dist/utils/array.js.map +1 -1
  116. package/dist/utils/consume-readable.cjs +18 -0
  117. package/dist/utils/consume-readable.cjs.map +1 -0
  118. package/dist/utils/consume-readable.js +13 -12
  119. package/dist/utils/consume-readable.js.map +1 -1
  120. package/dist/utils/decode-base64-url.cjs +15 -0
  121. package/dist/utils/decode-base64-url.cjs.map +1 -0
  122. package/dist/utils/decode-base64-url.js +10 -9
  123. package/dist/utils/decode-base64-url.js.map +1 -1
  124. package/dist/utils/dev-credentials.cjs +142 -0
  125. package/dist/utils/dev-credentials.cjs.map +1 -0
  126. package/dist/utils/dev-credentials.js +126 -184
  127. package/dist/utils/dev-credentials.js.map +1 -1
  128. package/dist/utils/get-credentials.cjs +123 -0
  129. package/dist/utils/get-credentials.cjs.map +1 -0
  130. package/dist/utils/get-credentials.d.cts +21 -0
  131. package/dist/utils/get-credentials.d.ts +19 -61
  132. package/dist/utils/get-credentials.js +106 -140
  133. package/dist/utils/get-credentials.js.map +1 -1
  134. package/dist/utils/log.cjs +25 -0
  135. package/dist/utils/log.cjs.map +1 -0
  136. package/dist/utils/log.js +15 -17
  137. package/dist/utils/log.js.map +1 -1
  138. package/dist/utils/network-policy.cjs +65 -0
  139. package/dist/utils/network-policy.cjs.map +1 -0
  140. package/dist/utils/network-policy.js +58 -77
  141. package/dist/utils/network-policy.js.map +1 -1
  142. package/dist/utils/normalizePath.cjs +27 -0
  143. package/dist/utils/normalizePath.cjs.map +1 -0
  144. package/dist/utils/normalizePath.js +21 -28
  145. package/dist/utils/normalizePath.js.map +1 -1
  146. package/dist/utils/resolveSignal.cjs +20 -0
  147. package/dist/utils/resolveSignal.cjs.map +1 -0
  148. package/dist/utils/resolveSignal.d.cts +15 -0
  149. package/dist/utils/resolveSignal.d.ts +12 -10
  150. package/dist/utils/resolveSignal.js +14 -17
  151. package/dist/utils/resolveSignal.js.map +1 -1
  152. package/dist/utils/sandbox-snapshot.cjs +14 -0
  153. package/dist/utils/sandbox-snapshot.cjs.map +1 -0
  154. package/dist/utils/sandbox-snapshot.d.cts +10 -0
  155. package/dist/utils/sandbox-snapshot.d.ts +11 -0
  156. package/dist/utils/sandbox-snapshot.js +14 -0
  157. package/dist/utils/sandbox-snapshot.js.map +1 -0
  158. package/dist/utils/types.cjs +13 -0
  159. package/dist/utils/types.cjs.map +1 -0
  160. package/dist/utils/types.d.cts +11 -0
  161. package/dist/utils/types.d.ts +5 -7
  162. package/dist/utils/types.js +8 -8
  163. package/dist/utils/types.js.map +1 -1
  164. package/dist/version.cjs +7 -0
  165. package/dist/version.cjs.map +1 -0
  166. package/dist/version.js +5 -5
  167. package/dist/version.js.map +1 -1
  168. package/package.json +23 -3
  169. package/dist/api-client/index.js.map +0 -1
  170. package/dist/auth/api.d.ts +0 -6
  171. package/dist/auth/error.d.ts +0 -11
  172. package/dist/auth/index.js.map +0 -1
  173. package/dist/auth/linked-project.d.ts +0 -10
  174. package/dist/auth/zod.d.ts +0 -5
  175. package/dist/constants.js +0 -3
  176. package/dist/constants.js.map +0 -1
  177. package/dist/index.js.map +0 -1
  178. package/dist/network-policy.js +0 -3
  179. package/dist/network-policy.js.map +0 -1
  180. package/dist/utils/array.d.ts +0 -9
  181. package/dist/utils/consume-readable.d.ts +0 -5
  182. package/dist/utils/convert-sandbox.d.ts +0 -6
  183. package/dist/utils/convert-sandbox.js +0 -14
  184. package/dist/utils/convert-sandbox.js.map +0 -1
  185. package/dist/utils/decode-base64-url.d.ts +0 -7
  186. package/dist/utils/dev-credentials.d.ts +0 -37
  187. package/dist/utils/log.d.ts +0 -2
  188. package/dist/utils/network-policy.d.ts +0 -7
  189. package/dist/utils/normalizePath.d.ts +0 -17
  190. package/dist/version.d.ts +0 -1
@@ -1,156 +1,122 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.schema = exports.VercelOidcContextError = exports.LocalOidcContextError = void 0;
4
- exports.getCredentials = getCredentials;
5
- const oidc_1 = require("@vercel/oidc");
6
- const decode_base64_url_1 = require("./decode-base64-url");
7
- const zod_1 = require("zod");
8
- const dev_credentials_1 = require("./dev-credentials");
1
+ import { decodeBase64Url } from "./decode-base64-url.js";
2
+ import { cachedGenerateCredentials, shouldPromptForCredentials } from "./dev-credentials.js";
3
+ import { z } from "zod";
4
+ import { getVercelOidcToken } from "@vercel/oidc";
5
+
6
+ //#region src/utils/get-credentials.ts
9
7
  /**
10
- * Error thrown when OIDC context is not available in local development,
11
- * therefore we should guide how to ensure it is set up by linking a project
12
- */
13
- class LocalOidcContextError extends Error {
14
- constructor(cause) {
15
- const message = [
16
- "Could not get credentials from OIDC context.",
17
- "Please link your Vercel project using `npx vercel link`.",
18
- "Then, pull an initial OIDC token with `npx vercel env pull`",
19
- "and retry.",
20
- "╰▶ Make sure you are loading `.env.local` correctly, or passing $VERCEL_OIDC_TOKEN directly.",
21
- ].join("\n");
22
- super(message, { cause });
23
- this.name = "LocalOidcContextError";
24
- }
25
- }
26
- exports.LocalOidcContextError = LocalOidcContextError;
8
+ * Error thrown when OIDC context is not available in local development,
9
+ * therefore we should guide how to ensure it is set up by linking a project
10
+ */
11
+ var LocalOidcContextError = class extends Error {
12
+ constructor(cause) {
13
+ const message = [
14
+ "Could not get credentials from OIDC context.",
15
+ "Please link your Vercel project using `npx vercel link`.",
16
+ "Then, pull an initial OIDC token with `npx vercel env pull`",
17
+ "and retry.",
18
+ "╰▶ Make sure you are loading `.env.local` correctly, or passing $VERCEL_OIDC_TOKEN directly."
19
+ ].join("\n");
20
+ super(message, { cause });
21
+ this.name = "LocalOidcContextError";
22
+ }
23
+ };
27
24
  /**
28
- * Error thrown when OIDC context is not available in Vercel environment,
29
- * therefore we should guide how to set it up.
30
- */
31
- class VercelOidcContextError extends Error {
32
- constructor(cause) {
33
- const message = [
34
- "Could not get credentials from OIDC context.",
35
- "Please make sure OIDC is set up for your project",
36
- "╰▶ Docs: https://vercel.com/docs/oidc",
37
- ].join("\n");
38
- super(message, { cause });
39
- this.name = "VercelOidcContextError";
40
- }
41
- }
42
- exports.VercelOidcContextError = VercelOidcContextError;
25
+ * Error thrown when OIDC context is not available in Vercel environment,
26
+ * therefore we should guide how to set it up.
27
+ */
28
+ var VercelOidcContextError = class extends Error {
29
+ constructor(cause) {
30
+ const message = [
31
+ "Could not get credentials from OIDC context.",
32
+ "Please make sure OIDC is set up for your project",
33
+ "╰▶ Docs: https://vercel.com/docs/oidc"
34
+ ].join("\n");
35
+ super(message, { cause });
36
+ this.name = "VercelOidcContextError";
37
+ }
38
+ };
43
39
  async function getVercelToken(opts) {
44
- try {
45
- const token = await (0, oidc_1.getVercelOidcToken)({
46
- team: opts.teamId,
47
- project: opts.projectId,
48
- });
49
- return getCredentialsFromOIDCToken(token);
50
- }
51
- catch (error) {
52
- if (!(0, dev_credentials_1.shouldPromptForCredentials)()) {
53
- if (process.env.VERCEL_URL) {
54
- throw new VercelOidcContextError(error);
55
- }
56
- else {
57
- throw new LocalOidcContextError(error);
58
- }
59
- }
60
- return await (0, dev_credentials_1.cachedGenerateCredentials)(opts);
61
- }
40
+ try {
41
+ return getCredentialsFromOIDCToken(await getVercelOidcToken({
42
+ team: opts.teamId,
43
+ project: opts.projectId
44
+ }));
45
+ } catch (error) {
46
+ if (!shouldPromptForCredentials()) if (process.env.VERCEL_URL) throw new VercelOidcContextError(error);
47
+ else throw new LocalOidcContextError(error);
48
+ return await cachedGenerateCredentials(opts);
49
+ }
62
50
  }
63
51
  /**
64
- * Allow to get credentials to access the Vercel API. Credentials can be
65
- * provided in two different ways:
66
- *
67
- * 1. By passing an object with the `teamId`, `token`, and `projectId` properties.
68
- * 2. By using an environment variable VERCEL_OIDC_TOKEN.
69
- *
70
- * If both methods are used, the object properties take precedence over the
71
- * environment variable. If neither method is used, an error is thrown.
72
- */
52
+ * Allow to get credentials to access the Vercel API. Credentials can be
53
+ * provided in two different ways:
54
+ *
55
+ * 1. By passing an object with the `teamId`, `token`, and `projectId` properties.
56
+ * 2. By using an environment variable VERCEL_OIDC_TOKEN.
57
+ *
58
+ * If both methods are used, the object properties take precedence over the
59
+ * environment variable. If neither method is used, an error is thrown.
60
+ */
73
61
  async function getCredentials(params) {
74
- const credentials = getCredentialsFromParams(params ?? {});
75
- if (credentials) {
76
- return credentials;
77
- }
78
- const oidcToken = await getVercelToken({
79
- teamId: params &&
80
- typeof params === "object" &&
81
- "teamId" in params &&
82
- typeof params.teamId === "string"
83
- ? params.teamId
84
- : undefined,
85
- projectId: params &&
86
- typeof params === "object" &&
87
- "projectId" in params &&
88
- typeof params.projectId === "string"
89
- ? params.projectId
90
- : undefined,
91
- });
92
- return oidcToken;
62
+ const credentials = getCredentialsFromParams(params ?? {});
63
+ if (credentials) return credentials;
64
+ return await getVercelToken({
65
+ teamId: params && typeof params === "object" && "teamId" in params && typeof params.teamId === "string" ? params.teamId : void 0,
66
+ projectId: params && typeof params === "object" && "projectId" in params && typeof params.projectId === "string" ? params.projectId : void 0
67
+ });
93
68
  }
94
69
  /**
95
- * Attempt to extract credentials from the provided parameters. Either all
96
- * required fields (`token`, `teamId`, and `projectId`) must be present
97
- * or none of them, otherwise an error is thrown.
98
- */
70
+ * Attempt to extract credentials from the provided parameters. Either all
71
+ * required fields (`token`, `teamId`, and `projectId`) must be present
72
+ * or none of them, otherwise an error is thrown.
73
+ */
99
74
  function getCredentialsFromParams(params) {
100
- // Type guard: params must be an object
101
- if (!params || typeof params !== "object") {
102
- return null;
103
- }
104
- const missing = [
105
- "token" in params && typeof params.token === "string" ? null : "token",
106
- "teamId" in params && typeof params.teamId === "string" ? null : "teamId",
107
- "projectId" in params && typeof params.projectId === "string"
108
- ? null
109
- : "projectId",
110
- ].filter((value) => value !== null);
111
- if (missing.length === 0) {
112
- return {
113
- token: params.token,
114
- projectId: params.projectId,
115
- teamId: params.teamId,
116
- };
117
- }
118
- if (missing.length < 3) {
119
- throw new Error(`Missing credentials parameters to access the Vercel API: ${missing
120
- .filter((value) => value !== null)
121
- .join(", ")}`);
122
- }
123
- return null;
75
+ if (!params || typeof params !== "object") return null;
76
+ const missing = [
77
+ "token" in params && typeof params.token === "string" ? null : "token",
78
+ "teamId" in params && typeof params.teamId === "string" ? null : "teamId",
79
+ "projectId" in params && typeof params.projectId === "string" ? null : "projectId"
80
+ ].filter((value) => value !== null);
81
+ if (missing.length === 0) return {
82
+ token: params.token,
83
+ projectId: params.projectId,
84
+ teamId: params.teamId
85
+ };
86
+ if (missing.length < 3) throw new Error(`Missing credentials parameters to access the Vercel API: ${missing.filter((value) => value !== null).join(", ")}`);
87
+ return null;
124
88
  }
125
89
  /**
126
- * Schema to validate the payload of the Vercel OIDC token where we expect
127
- * to find the `teamId` and `projectId`.
128
- */
129
- exports.schema = zod_1.z.object({
130
- exp: zod_1.z.number().optional().describe("Expiry timestamp (seconds since epoch)"),
131
- iat: zod_1.z.number().optional().describe("Issued at timestamp"),
132
- owner_id: zod_1.z.string(),
133
- project_id: zod_1.z.string(),
90
+ * Schema to validate the payload of the Vercel OIDC token where we expect
91
+ * to find the `teamId` and `projectId`.
92
+ */
93
+ const schema = z.object({
94
+ exp: z.number().optional().describe("Expiry timestamp (seconds since epoch)"),
95
+ iat: z.number().optional().describe("Issued at timestamp"),
96
+ owner_id: z.string(),
97
+ project_id: z.string()
134
98
  });
135
99
  /**
136
- * Extracts credentials from a Vercel OIDC token. The token is expected to be
137
- * a JWT with a payload that contains `project_id` and `owner_id`.
138
- *
139
- * @param token - The Vercel OIDC token.
140
- * @returns An object containing the token, projectId, and teamId.
141
- * @throws If the token is invalid or does not contain the required fields.
142
- */
100
+ * Extracts credentials from a Vercel OIDC token. The token is expected to be
101
+ * a JWT with a payload that contains `project_id` and `owner_id`.
102
+ *
103
+ * @param token - The Vercel OIDC token.
104
+ * @returns An object containing the token, projectId, and teamId.
105
+ * @throws If the token is invalid or does not contain the required fields.
106
+ */
143
107
  function getCredentialsFromOIDCToken(token) {
144
- try {
145
- const payload = exports.schema.parse((0, decode_base64_url_1.decodeBase64Url)(token.split(".")[1]));
146
- return {
147
- token,
148
- projectId: payload.project_id,
149
- teamId: payload.owner_id,
150
- };
151
- }
152
- catch (error) {
153
- throw new Error(`Invalid Vercel OIDC token: ${error instanceof Error ? error.message : String(error)}`);
154
- }
108
+ try {
109
+ const payload = schema.parse(decodeBase64Url(token.split(".")[1]));
110
+ return {
111
+ token,
112
+ projectId: payload.project_id,
113
+ teamId: payload.owner_id
114
+ };
115
+ } catch (error) {
116
+ throw new Error(`Invalid Vercel OIDC token: ${error instanceof Error ? error.message : String(error)}`);
117
+ }
155
118
  }
119
+
120
+ //#endregion
121
+ export { getCredentials };
156
122
  //# sourceMappingURL=get-credentials.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"get-credentials.js","sourceRoot":"","sources":["../../src/utils/get-credentials.ts"],"names":[],"mappings":";;;AA0FA,wCAwBC;AAlHD,uCAAkD;AAClD,2DAAsD;AACtD,6BAAwB;AACxB,uDAG2B;AAkB3B;;;GAGG;AACH,MAAa,qBAAsB,SAAQ,KAAK;IAE9C,YAAY,KAAc;QACxB,MAAM,OAAO,GAAG;YACd,8CAA8C;YAC9C,0DAA0D;YAC1D,6DAA6D;YAC7D,YAAY;YACZ,8FAA8F;SAC/F,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACb,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QAT5B,SAAI,GAAG,uBAAuB,CAAC;IAU/B,CAAC;CACF;AAZD,sDAYC;AAED;;;GAGG;AACH,MAAa,sBAAuB,SAAQ,KAAK;IAE/C,YAAY,KAAc;QACxB,MAAM,OAAO,GAAG;YACd,8CAA8C;YAC9C,kDAAkD;YAClD,uCAAuC;SACxC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACb,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QAP5B,SAAI,GAAG,wBAAwB,CAAC;IAQhC,CAAC;CACF;AAVD,wDAUC;AAED,KAAK,UAAU,cAAc,CAAC,IAG7B;IACC,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,MAAM,IAAA,yBAAkB,EAAC;YACrC,IAAI,EAAE,IAAI,CAAC,MAAM;YACjB,OAAO,EAAE,IAAI,CAAC,SAAS;SACxB,CAAC,CAAC;QACH,OAAO,2BAA2B,CAAC,KAAK,CAAC,CAAC;IAC5C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,CAAC,IAAA,4CAA0B,GAAE,EAAE,CAAC;YAClC,IAAI,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC;gBAC3B,MAAM,IAAI,sBAAsB,CAAC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C,CAAC,CAAC,CAAC,CAAC,CAAC;QACnE,OAAO;YACL,KAAK;YACL,SAAS,EAAE,OAAO,CAAC,UAAU;YAC7B,MAAM,EAAE,OAAO,CAAC,QAAQ;SACzB,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CACb,8BAA8B,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CACvF,CAAC;IACJ,CAAC;AACH,CAAC"}
1
+ {"version":3,"file":"get-credentials.js","names":[],"sources":["../../src/utils/get-credentials.ts"],"sourcesContent":["import { getVercelOidcToken } from \"@vercel/oidc\";\nimport { decodeBase64Url } from \"./decode-base64-url.js\";\nimport { z } from \"zod\";\nimport {\n cachedGenerateCredentials,\n shouldPromptForCredentials,\n} from \"./dev-credentials.js\";\n\nexport interface Credentials {\n /**\n * Authentication token for the Vercel API. It could be an OIDC token\n * or a personal access token.\n */\n token: string;\n /**\n * The ID of the project to associate Sandbox operations.\n */\n projectId: string;\n /**\n * The ID of the team to associate Sandbox operations.\n */\n teamId: string;\n}\n\n/**\n * Error thrown when OIDC context is not available in local development,\n * therefore we should guide how to ensure it is set up by linking a project\n */\nexport class LocalOidcContextError extends Error {\n name = \"LocalOidcContextError\";\n constructor(cause: unknown) {\n const message = [\n \"Could not get credentials from OIDC context.\",\n \"Please link your Vercel project using `npx vercel link`.\",\n \"Then, pull an initial OIDC token with `npx vercel env pull`\",\n \"and retry.\",\n \"╰▶ Make sure you are loading `.env.local` correctly, or passing $VERCEL_OIDC_TOKEN directly.\",\n ].join(\"\\n\");\n super(message, { cause });\n }\n}\n\n/**\n * Error thrown when OIDC context is not available in Vercel environment,\n * therefore we should guide how to set it up.\n */\nexport class VercelOidcContextError extends Error {\n name = \"VercelOidcContextError\";\n constructor(cause: unknown) {\n const message = [\n \"Could not get credentials from OIDC context.\",\n \"Please make sure OIDC is set up for your project\",\n \"╰▶ Docs: https://vercel.com/docs/oidc\",\n ].join(\"\\n\");\n super(message, { cause });\n }\n}\n\nasync function getVercelToken(opts: {\n teamId?: string;\n projectId?: string;\n}): Promise<Credentials> {\n try {\n const token = await getVercelOidcToken({\n team: opts.teamId,\n project: opts.projectId,\n });\n return getCredentialsFromOIDCToken(token);\n } catch (error) {\n if (!shouldPromptForCredentials()) {\n if (process.env.VERCEL_URL) {\n throw new VercelOidcContextError(error);\n } else {\n throw new LocalOidcContextError(error);\n }\n }\n return await cachedGenerateCredentials(opts);\n }\n}\n\n/**\n * Allow to get credentials to access the Vercel API. Credentials can be\n * provided in two different ways:\n *\n * 1. By passing an object with the `teamId`, `token`, and `projectId` properties.\n * 2. By using an environment variable VERCEL_OIDC_TOKEN.\n *\n * If both methods are used, the object properties take precedence over the\n * environment variable. If neither method is used, an error is thrown.\n */\nexport async function getCredentials(params?: unknown): Promise<Credentials> {\n const credentials = getCredentialsFromParams(params ?? {});\n if (credentials) {\n return credentials;\n }\n\n const oidcToken = await getVercelToken({\n teamId:\n params &&\n typeof params === \"object\" &&\n \"teamId\" in params &&\n typeof params.teamId === \"string\"\n ? params.teamId\n : undefined,\n projectId:\n params &&\n typeof params === \"object\" &&\n \"projectId\" in params &&\n typeof params.projectId === \"string\"\n ? params.projectId\n : undefined,\n });\n\n return oidcToken;\n}\n\n/**\n * Attempt to extract credentials from the provided parameters. Either all\n * required fields (`token`, `teamId`, and `projectId`) must be present\n * or none of them, otherwise an error is thrown.\n */\nfunction getCredentialsFromParams(params: unknown): Credentials | null {\n // Type guard: params must be an object\n if (!params || typeof params !== \"object\") {\n return null;\n }\n\n const missing = [\n \"token\" in params && typeof params.token === \"string\" ? null : \"token\",\n \"teamId\" in params && typeof params.teamId === \"string\" ? null : \"teamId\",\n \"projectId\" in params && typeof params.projectId === \"string\"\n ? null\n : \"projectId\",\n ].filter((value) => value !== null);\n\n if (missing.length === 0) {\n return {\n token: (params as any).token,\n projectId: (params as any).projectId,\n teamId: (params as any).teamId,\n };\n }\n\n if (missing.length < 3) {\n throw new Error(\n `Missing credentials parameters to access the Vercel API: ${missing\n .filter((value) => value !== null)\n .join(\", \")}`,\n );\n }\n\n return null;\n}\n\n/**\n * Schema to validate the payload of the Vercel OIDC token where we expect\n * to find the `teamId` and `projectId`.\n */\nexport const schema = z.object({\n exp: z.number().optional().describe(\"Expiry timestamp (seconds since epoch)\"),\n iat: z.number().optional().describe(\"Issued at timestamp\"),\n owner_id: z.string(),\n project_id: z.string(),\n});\n\n/**\n * Extracts credentials from a Vercel OIDC token. The token is expected to be\n * a JWT with a payload that contains `project_id` and `owner_id`.\n *\n * @param token - The Vercel OIDC token.\n * @returns An object containing the token, projectId, and teamId.\n * @throws If the token is invalid or does not contain the required fields.\n */\nfunction getCredentialsFromOIDCToken(token: string): Credentials {\n try {\n const payload = schema.parse(decodeBase64Url(token.split(\".\")[1]));\n return {\n token,\n projectId: payload.project_id,\n teamId: payload.owner_id,\n };\n } catch (error) {\n throw new Error(\n `Invalid Vercel OIDC token: ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n}\n"],"mappings":";;;;;;;;;;AA4BA,IAAa,wBAAb,cAA2C,MAAM;CAE/C,YAAY,OAAgB;EAC1B,MAAM,UAAU;GACd;GACA;GACA;GACA;GACA;GACD,CAAC,KAAK,KAAK;AACZ,QAAM,SAAS,EAAE,OAAO,CAAC;OAT3B,OAAO;;;;;;;AAiBT,IAAa,yBAAb,cAA4C,MAAM;CAEhD,YAAY,OAAgB;EAC1B,MAAM,UAAU;GACd;GACA;GACA;GACD,CAAC,KAAK,KAAK;AACZ,QAAM,SAAS,EAAE,OAAO,CAAC;OAP3B,OAAO;;;AAWT,eAAe,eAAe,MAGL;AACvB,KAAI;AAKF,SAAO,4BAJO,MAAM,mBAAmB;GACrC,MAAM,KAAK;GACX,SAAS,KAAK;GACf,CAAC,CACuC;UAClC,OAAO;AACd,MAAI,CAAC,4BAA4B,CAC/B,KAAI,QAAQ,IAAI,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AI,CAAC,GAAG,CAAC;AAClE,SAAO;GACL;GACA,WAAW,QAAQ;GACnB,QAAQ,QAAQ;GACjB;UACM,OAAO;AACd,QAAM,IAAI,MACR,8BAA8B,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,GACrF"}
@@ -0,0 +1,25 @@
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
+ let picocolors = require("picocolors");
3
+ picocolors = require_rolldown_runtime.__toESM(picocolors);
4
+
5
+ //#region src/utils/log.ts
6
+ const colors = {
7
+ warn: picocolors.default.yellow,
8
+ error: picocolors.default.red,
9
+ success: picocolors.default.green,
10
+ info: picocolors.default.blue
11
+ };
12
+ const logPrefix = picocolors.default.dim("[vercel/sandbox]");
13
+ function write(level, text) {
14
+ text = Array.isArray(text) ? text.join("\n") : text;
15
+ const prefixed = text.replace(/^/gm, `${logPrefix} `);
16
+ console.error(colors[level](prefixed));
17
+ }
18
+ function code(text) {
19
+ return picocolors.default.italic(picocolors.default.dim("`") + text + picocolors.default.dim("`"));
20
+ }
21
+
22
+ //#endregion
23
+ exports.code = code;
24
+ exports.write = write;
25
+ //# sourceMappingURL=log.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"log.cjs","names":["pico"],"sources":["../../src/utils/log.ts"],"sourcesContent":["import pico from \"picocolors\";\nconst colors = {\n warn: pico.yellow,\n error: pico.red,\n success: pico.green,\n info: pico.blue,\n};\nconst logPrefix = pico.dim(\"[vercel/sandbox]\");\nexport function write(\n level: \"warn\" | \"error\" | \"info\" | \"success\",\n text: string | string[],\n) {\n text = Array.isArray(text) ? text.join(\"\\n\") : text;\n const prefixed = text.replace(/^/gm, `${logPrefix} `);\n console.error(colors[level](prefixed));\n}\n\nexport function code(text: string) {\n return pico.italic(pico.dim(\"`\") + text + pico.dim(\"`\"));\n}\n"],"mappings":";;;;;AACA,MAAM,SAAS;CACb,MAAMA,mBAAK;CACX,OAAOA,mBAAK;CACZ,SAASA,mBAAK;CACd,MAAMA,mBAAK;CACZ;AACD,MAAM,YAAYA,mBAAK,IAAI,mBAAmB;AAC9C,SAAgB,MACd,OACA,MACA;AACA,QAAO,MAAM,QAAQ,KAAK,GAAG,KAAK,KAAK,KAAK,GAAG;CAC/C,MAAM,WAAW,KAAK,QAAQ,OAAO,GAAG,UAAU,GAAG;AACrD,SAAQ,MAAM,OAAO,OAAO,SAAS,CAAC;;AAGxC,SAAgB,KAAK,MAAc;AACjC,QAAOA,mBAAK,OAAOA,mBAAK,IAAI,IAAI,GAAG,OAAOA,mBAAK,IAAI,IAAI,CAAC"}
package/dist/utils/log.js CHANGED
@@ -1,24 +1,22 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.write = write;
7
- exports.code = code;
8
- const picocolors_1 = __importDefault(require("picocolors"));
1
+ import pico from "picocolors";
2
+
3
+ //#region src/utils/log.ts
9
4
  const colors = {
10
- warn: picocolors_1.default.yellow,
11
- error: picocolors_1.default.red,
12
- success: picocolors_1.default.green,
13
- info: picocolors_1.default.blue,
5
+ warn: pico.yellow,
6
+ error: pico.red,
7
+ success: pico.green,
8
+ info: pico.blue
14
9
  };
15
- const logPrefix = picocolors_1.default.dim("[vercel/sandbox]");
10
+ const logPrefix = pico.dim("[vercel/sandbox]");
16
11
  function write(level, text) {
17
- text = Array.isArray(text) ? text.join("\n") : text;
18
- const prefixed = text.replace(/^/gm, `${logPrefix} `);
19
- console.error(colors[level](prefixed));
12
+ text = Array.isArray(text) ? text.join("\n") : text;
13
+ const prefixed = text.replace(/^/gm, `${logPrefix} `);
14
+ console.error(colors[level](prefixed));
20
15
  }
21
16
  function code(text) {
22
- return picocolors_1.default.italic(picocolors_1.default.dim("`") + text + picocolors_1.default.dim("`"));
17
+ return pico.italic(pico.dim("`") + text + pico.dim("`"));
23
18
  }
19
+
20
+ //#endregion
21
+ export { code, write };
24
22
  //# sourceMappingURL=log.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"log.js","sourceRoot":"","sources":["../../src/utils/log.ts"],"names":[],"mappings":";;;;;AAQA,sBAOC;AAED,oBAEC;AAnBD,4DAA8B;AAC9B,MAAM,MAAM,GAAG;IACb,IAAI,EAAE,oBAAI,CAAC,MAAM;IACjB,KAAK,EAAE,oBAAI,CAAC,GAAG;IACf,OAAO,EAAE,oBAAI,CAAC,KAAK;IACnB,IAAI,EAAE,oBAAI,CAAC,IAAI;CAChB,CAAC;AACF,MAAM,SAAS,GAAG,oBAAI,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;AAC/C,SAAgB,KAAK,CACnB,KAA4C,EAC5C,IAAuB;IAEvB,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACpD,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,SAAS,GAAG,CAAC,CAAC;IACtD,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC;AACzC,CAAC;AAED,SAAgB,IAAI,CAAC,IAAY;IAC/B,OAAO,oBAAI,CAAC,MAAM,CAAC,oBAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,oBAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;AAC3D,CAAC"}
1
+ {"version":3,"file":"log.js","names":[],"sources":["../../src/utils/log.ts"],"sourcesContent":["import pico from \"picocolors\";\nconst colors = {\n warn: pico.yellow,\n error: pico.red,\n success: pico.green,\n info: pico.blue,\n};\nconst logPrefix = pico.dim(\"[vercel/sandbox]\");\nexport function write(\n level: \"warn\" | \"error\" | \"info\" | \"success\",\n text: string | string[],\n) {\n text = Array.isArray(text) ? text.join(\"\\n\") : text;\n const prefixed = text.replace(/^/gm, `${logPrefix} `);\n console.error(colors[level](prefixed));\n}\n\nexport function code(text: string) {\n return pico.italic(pico.dim(\"`\") + text + pico.dim(\"`\"));\n}\n"],"mappings":";;;AACA,MAAM,SAAS;CACb,MAAM,KAAK;CACX,OAAO,KAAK;CACZ,SAAS,KAAK;CACd,MAAM,KAAK;CACZ;AACD,MAAM,YAAY,KAAK,IAAI,mBAAmB;AAC9C,SAAgB,MACd,OACA,MACA;AACA,QAAO,MAAM,QAAQ,KAAK,GAAG,KAAK,KAAK,KAAK,GAAG;CAC/C,MAAM,WAAW,KAAK,QAAQ,OAAO,GAAG,UAAU,GAAG;AACrD,SAAQ,MAAM,OAAO,OAAO,SAAS,CAAC;;AAGxC,SAAgB,KAAK,MAAc;AACjC,QAAO,KAAK,OAAO,KAAK,IAAI,IAAI,GAAG,OAAO,KAAK,IAAI,IAAI,CAAC"}
@@ -0,0 +1,65 @@
1
+
2
+ //#region src/utils/network-policy.ts
3
+ function toAPINetworkPolicy(policy) {
4
+ if (policy === "allow-all") return { mode: "allow-all" };
5
+ if (policy === "deny-all") return { mode: "deny-all" };
6
+ if (policy.allow && !Array.isArray(policy.allow)) {
7
+ const allowedDomains = Object.keys(policy.allow);
8
+ const injectionRules = [];
9
+ for (const [domain, rules] of Object.entries(policy.allow)) {
10
+ const merged = {};
11
+ for (const rule of rules) for (const t of rule.transform ?? []) Object.assign(merged, t.headers);
12
+ if (Object.keys(merged).length > 0) injectionRules.push({
13
+ domain,
14
+ headers: merged
15
+ });
16
+ }
17
+ return {
18
+ mode: "custom",
19
+ ...allowedDomains.length > 0 && { allowedDomains },
20
+ ...injectionRules.length > 0 && { injectionRules },
21
+ ...policy.subnets?.allow && { allowedCIDRs: policy.subnets.allow },
22
+ ...policy.subnets?.deny && { deniedCIDRs: policy.subnets.deny }
23
+ };
24
+ }
25
+ return {
26
+ mode: "custom",
27
+ ...policy.allow && { allowedDomains: policy.allow },
28
+ ...policy.subnets?.allow && { allowedCIDRs: policy.subnets.allow },
29
+ ...policy.subnets?.deny && { deniedCIDRs: policy.subnets.deny }
30
+ };
31
+ }
32
+ function fromAPINetworkPolicy(api) {
33
+ if (api.mode === "allow-all") return "allow-all";
34
+ if (api.mode === "deny-all") return "deny-all";
35
+ const subnets = api.allowedCIDRs || api.deniedCIDRs ? { subnets: {
36
+ ...api.allowedCIDRs && { allow: api.allowedCIDRs },
37
+ ...api.deniedCIDRs && { deny: api.deniedCIDRs }
38
+ } } : void 0;
39
+ if (api.injectionRules && api.injectionRules.length > 0) {
40
+ const rulesByDomain = new Map(api.injectionRules.map((r) => [r.domain, r.headerNames ?? []]));
41
+ const allow = {};
42
+ for (const domain of api.allowedDomains ?? []) {
43
+ const headerNames = rulesByDomain.get(domain);
44
+ if (headerNames && headerNames.length > 0) allow[domain] = [{ transform: [{ headers: Object.fromEntries(headerNames.map((n) => [n, "<redacted>"])) }] }];
45
+ else allow[domain] = [];
46
+ }
47
+ for (const rule of api.injectionRules) if (!(rule.domain in allow)) {
48
+ const headers = Object.fromEntries((rule.headerNames ?? []).map((n) => [n, "<redacted>"]));
49
+ allow[rule.domain] = [{ transform: [{ headers }] }];
50
+ }
51
+ return {
52
+ allow,
53
+ ...subnets
54
+ };
55
+ }
56
+ return {
57
+ ...api.allowedDomains && { allow: api.allowedDomains },
58
+ ...subnets
59
+ };
60
+ }
61
+
62
+ //#endregion
63
+ exports.fromAPINetworkPolicy = fromAPINetworkPolicy;
64
+ exports.toAPINetworkPolicy = toAPINetworkPolicy;
65
+ //# sourceMappingURL=network-policy.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"network-policy.cjs","names":["injectionRules: z.infer<typeof InjectionRuleValidator>[]","merged: Record<string, string>","allow: Record<string, NetworkPolicyRule[]>"],"sources":["../../src/utils/network-policy.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { NetworkPolicy, NetworkPolicyRule } from \"../network-policy.js\";\nimport {\n NetworkPolicyValidator,\n InjectionRuleValidator,\n} from \"../api-client/validators.js\";\n\ntype APINetworkPolicy = z.infer<typeof NetworkPolicyValidator>;\n\nexport function toAPINetworkPolicy(policy: NetworkPolicy): APINetworkPolicy {\n if (policy === \"allow-all\") return { mode: \"allow-all\" };\n if (policy === \"deny-all\") return { mode: \"deny-all\" };\n\n if (policy.allow && !Array.isArray(policy.allow)) {\n const allowedDomains = Object.keys(policy.allow);\n const injectionRules: z.infer<typeof InjectionRuleValidator>[] = [];\n\n for (const [domain, rules] of Object.entries(policy.allow)) {\n const merged: Record<string, string> = {};\n for (const rule of rules) {\n for (const t of rule.transform ?? []) {\n Object.assign(merged, t.headers);\n }\n }\n if (Object.keys(merged).length > 0) {\n injectionRules.push({ domain, headers: merged });\n }\n }\n\n return {\n mode: \"custom\",\n ...(allowedDomains.length > 0 && { allowedDomains }),\n ...(injectionRules.length > 0 && { injectionRules }),\n ...(policy.subnets?.allow && { allowedCIDRs: policy.subnets.allow }),\n ...(policy.subnets?.deny && { deniedCIDRs: policy.subnets.deny }),\n };\n }\n\n return {\n mode: \"custom\",\n ...(policy.allow && { allowedDomains: policy.allow }),\n ...(policy.subnets?.allow && { allowedCIDRs: policy.subnets.allow }),\n ...(policy.subnets?.deny && { deniedCIDRs: policy.subnets.deny }),\n };\n}\n\nexport function fromAPINetworkPolicy(api: APINetworkPolicy): NetworkPolicy {\n if (api.mode === \"allow-all\") return \"allow-all\";\n if (api.mode === \"deny-all\") return \"deny-all\";\n\n const subnets =\n api.allowedCIDRs || api.deniedCIDRs\n ? {\n subnets: {\n ...(api.allowedCIDRs && { allow: api.allowedCIDRs }),\n ...(api.deniedCIDRs && { deny: api.deniedCIDRs }),\n },\n }\n : undefined;\n\n // If injectionRules are present, reconstruct the record form.\n // The API returns headerNames (secret values are stripped), so we\n // populate each header value with \"<redacted>\".\n if (api.injectionRules && api.injectionRules.length > 0) {\n const rulesByDomain = new Map(\n api.injectionRules.map((r) => [r.domain, r.headerNames ?? []]),\n );\n\n const allow: Record<string, NetworkPolicyRule[]> = {};\n for (const domain of api.allowedDomains ?? []) {\n const headerNames = rulesByDomain.get(domain);\n if (headerNames && headerNames.length > 0) {\n const headers = Object.fromEntries(\n headerNames.map((n) => [n, \"<redacted>\"]),\n );\n allow[domain] = [{ transform: [{ headers }] }];\n } else {\n allow[domain] = [];\n }\n }\n // Include injection rules for domains not in allowedDomains\n for (const rule of api.injectionRules) {\n if (!(rule.domain in allow)) {\n const headers = Object.fromEntries(\n (rule.headerNames ?? []).map((n) => [n, \"<redacted>\"]),\n );\n allow[rule.domain] = [{ transform: [{ headers }] }];\n }\n }\n\n return { allow, ...subnets };\n }\n\n return {\n ...(api.allowedDomains && { allow: api.allowedDomains }),\n ...subnets,\n };\n}\n"],"mappings":";;AASA,SAAgB,mBAAmB,QAAyC;AAC1E,KAAI,WAAW,YAAa,QAAO,EAAE,MAAM,aAAa;AACxD,KAAI,WAAW,WAAY,QAAO,EAAE,MAAM,YAAY;AAEtD,KAAI,OAAO,SAAS,CAAC,MAAM,QAAQ,OAAO,MAAM,EAAE;EAChD,MAAM,iBAAiB,OAAO,KAAK,OAAO,MAAM;EAChD,MAAMA,iBAA2D,EAAE;AAEnE,OAAK,MAAM,CAAC,QAAQ,UAAU,OAAO,QAAQ,OAAO,MAAM,EAAE;GAC1D,MAAMC,SAAiC,EAAE;AACzC,QAAK,MAAM,QAAQ,MACjB,MAAK,MAAM,KAAK,KAAK,aAAa,EAAE,CAClC,QAAO,OAAO,QAAQ,EAAE,QAAQ;AAGpC,OAAI,OAAO,KAAK,OAAO,CAAC,SAAS,EAC/B,gBAAe,KAAK;IAAE;IAAQ,SAAS;IAAQ,CAAC;;AAIpD,SAAO;GACL,MAAM;GACN,GAAI,eAAe,SAAS,KAAK,EAAE,gBAAgB;GACnD,GAAI,eAAe,SAAS,KAAK,EAAE,gBAAgB;GACnD,GAAI,OAAO,SAAS,SAAS,EAAE,cAAc,OAAO,QAAQ,OAAO;GACnE,GAAI,OAAO,SAAS,QAAQ,EAAE,aAAa,OAAO,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;GAAO,GAAG;GAAS;;AAG9B,QAAO;EACL,GAAI,IAAI,kBAAkB,EAAE,OAAO,IAAI,gBAAgB;EACvD,GAAG;EACJ"}
@@ -1,82 +1,63 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toAPINetworkPolicy = toAPINetworkPolicy;
4
- exports.fromAPINetworkPolicy = fromAPINetworkPolicy;
1
+ //#region src/utils/network-policy.ts
5
2
  function toAPINetworkPolicy(policy) {
6
- if (policy === "allow-all")
7
- return { mode: "allow-all" };
8
- if (policy === "deny-all")
9
- return { mode: "deny-all" };
10
- if (policy.allow && !Array.isArray(policy.allow)) {
11
- const allowedDomains = Object.keys(policy.allow);
12
- const injectionRules = [];
13
- for (const [domain, rules] of Object.entries(policy.allow)) {
14
- const merged = {};
15
- for (const rule of rules) {
16
- for (const t of rule.transform ?? []) {
17
- Object.assign(merged, t.headers);
18
- }
19
- }
20
- if (Object.keys(merged).length > 0) {
21
- injectionRules.push({ domain, headers: merged });
22
- }
23
- }
24
- return {
25
- mode: "custom",
26
- ...(allowedDomains.length > 0 && { allowedDomains }),
27
- ...(injectionRules.length > 0 && { injectionRules }),
28
- ...(policy.subnets?.allow && { allowedCIDRs: policy.subnets.allow }),
29
- ...(policy.subnets?.deny && { deniedCIDRs: policy.subnets.deny }),
30
- };
31
- }
32
- return {
33
- mode: "custom",
34
- ...(policy.allow && { allowedDomains: policy.allow }),
35
- ...(policy.subnets?.allow && { allowedCIDRs: policy.subnets.allow }),
36
- ...(policy.subnets?.deny && { deniedCIDRs: policy.subnets.deny }),
37
- };
3
+ if (policy === "allow-all") return { mode: "allow-all" };
4
+ if (policy === "deny-all") return { mode: "deny-all" };
5
+ if (policy.allow && !Array.isArray(policy.allow)) {
6
+ const allowedDomains = Object.keys(policy.allow);
7
+ const injectionRules = [];
8
+ for (const [domain, rules] of Object.entries(policy.allow)) {
9
+ const merged = {};
10
+ for (const rule of rules) for (const t of rule.transform ?? []) Object.assign(merged, t.headers);
11
+ if (Object.keys(merged).length > 0) injectionRules.push({
12
+ domain,
13
+ headers: merged
14
+ });
15
+ }
16
+ return {
17
+ mode: "custom",
18
+ ...allowedDomains.length > 0 && { allowedDomains },
19
+ ...injectionRules.length > 0 && { injectionRules },
20
+ ...policy.subnets?.allow && { allowedCIDRs: policy.subnets.allow },
21
+ ...policy.subnets?.deny && { deniedCIDRs: policy.subnets.deny }
22
+ };
23
+ }
24
+ return {
25
+ mode: "custom",
26
+ ...policy.allow && { allowedDomains: policy.allow },
27
+ ...policy.subnets?.allow && { allowedCIDRs: policy.subnets.allow },
28
+ ...policy.subnets?.deny && { deniedCIDRs: policy.subnets.deny }
29
+ };
38
30
  }
39
31
  function fromAPINetworkPolicy(api) {
40
- if (api.mode === "allow-all")
41
- return "allow-all";
42
- if (api.mode === "deny-all")
43
- return "deny-all";
44
- const subnets = (api.allowedCIDRs || api.deniedCIDRs)
45
- ? {
46
- subnets: {
47
- ...(api.allowedCIDRs && { allow: api.allowedCIDRs }),
48
- ...(api.deniedCIDRs && { deny: api.deniedCIDRs }),
49
- },
50
- }
51
- : undefined;
52
- // If injectionRules are present, reconstruct the record form.
53
- // The API returns headerNames (secret values are stripped), so we
54
- // populate each header value with "<redacted>".
55
- if (api.injectionRules && api.injectionRules.length > 0) {
56
- const rulesByDomain = new Map(api.injectionRules.map((r) => [r.domain, r.headerNames ?? []]));
57
- const allow = {};
58
- for (const domain of api.allowedDomains ?? []) {
59
- const headerNames = rulesByDomain.get(domain);
60
- if (headerNames && headerNames.length > 0) {
61
- const headers = Object.fromEntries(headerNames.map((n) => [n, "<redacted>"]));
62
- allow[domain] = [{ transform: [{ headers }] }];
63
- }
64
- else {
65
- allow[domain] = [];
66
- }
67
- }
68
- // Include injection rules for domains not in allowedDomains
69
- for (const rule of api.injectionRules) {
70
- if (!(rule.domain in allow)) {
71
- const headers = Object.fromEntries((rule.headerNames ?? []).map((n) => [n, "<redacted>"]));
72
- allow[rule.domain] = [{ transform: [{ headers }] }];
73
- }
74
- }
75
- return { allow, ...subnets };
76
- }
77
- return {
78
- ...(api.allowedDomains && { allow: api.allowedDomains }),
79
- ...subnets,
80
- };
32
+ if (api.mode === "allow-all") return "allow-all";
33
+ if (api.mode === "deny-all") return "deny-all";
34
+ const subnets = api.allowedCIDRs || api.deniedCIDRs ? { subnets: {
35
+ ...api.allowedCIDRs && { allow: api.allowedCIDRs },
36
+ ...api.deniedCIDRs && { deny: api.deniedCIDRs }
37
+ } } : void 0;
38
+ if (api.injectionRules && api.injectionRules.length > 0) {
39
+ const rulesByDomain = new Map(api.injectionRules.map((r) => [r.domain, r.headerNames ?? []]));
40
+ const allow = {};
41
+ for (const domain of api.allowedDomains ?? []) {
42
+ const headerNames = rulesByDomain.get(domain);
43
+ if (headerNames && headerNames.length > 0) allow[domain] = [{ transform: [{ headers: Object.fromEntries(headerNames.map((n) => [n, "<redacted>"])) }] }];
44
+ else allow[domain] = [];
45
+ }
46
+ for (const rule of api.injectionRules) if (!(rule.domain in allow)) {
47
+ const headers = Object.fromEntries((rule.headerNames ?? []).map((n) => [n, "<redacted>"]));
48
+ allow[rule.domain] = [{ transform: [{ headers }] }];
49
+ }
50
+ return {
51
+ allow,
52
+ ...subnets
53
+ };
54
+ }
55
+ return {
56
+ ...api.allowedDomains && { allow: api.allowedDomains },
57
+ ...subnets
58
+ };
81
59
  }
60
+
61
+ //#endregion
62
+ export { fromAPINetworkPolicy, toAPINetworkPolicy };
82
63
  //# sourceMappingURL=network-policy.js.map