@vercel/sandbox 1.1.4 → 1.1.5

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 (70) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/.turbo/turbo-test.log +14 -8
  3. package/.turbo/turbo-typecheck.log +1 -1
  4. package/CHANGELOG.md +6 -0
  5. package/__mocks__/picocolors.ts +13 -0
  6. package/dist/api-client/with-retry.js +1 -1
  7. package/dist/api-client/with-retry.js.map +1 -1
  8. package/dist/auth/api.d.ts +6 -0
  9. package/dist/auth/api.js +28 -0
  10. package/dist/auth/api.js.map +1 -0
  11. package/dist/auth/error.d.ts +11 -0
  12. package/dist/auth/error.js +12 -0
  13. package/dist/auth/error.js.map +1 -0
  14. package/dist/auth/file.d.ts +22 -0
  15. package/dist/auth/file.js +66 -0
  16. package/dist/auth/file.js.map +1 -0
  17. package/dist/auth/index.d.ts +6 -0
  18. package/dist/auth/index.js +27 -0
  19. package/dist/auth/index.js.map +1 -0
  20. package/dist/auth/linked-project.d.ts +10 -0
  21. package/dist/auth/linked-project.js +69 -0
  22. package/dist/auth/linked-project.js.map +1 -0
  23. package/dist/auth/oauth.d.ts +131 -0
  24. package/dist/auth/oauth.js +269 -0
  25. package/dist/auth/oauth.js.map +1 -0
  26. package/dist/auth/poll-for-token.d.ts +20 -0
  27. package/dist/auth/poll-for-token.js +66 -0
  28. package/dist/auth/poll-for-token.js.map +1 -0
  29. package/dist/auth/project.d.ts +40 -0
  30. package/dist/auth/project.js +80 -0
  31. package/dist/auth/project.js.map +1 -0
  32. package/dist/auth/zod.d.ts +5 -0
  33. package/dist/auth/zod.js +20 -0
  34. package/dist/auth/zod.js.map +1 -0
  35. package/dist/sandbox.js +1 -1
  36. package/dist/sandbox.js.map +1 -1
  37. package/dist/utils/dev-credentials.d.ts +37 -0
  38. package/dist/utils/dev-credentials.js +191 -0
  39. package/dist/utils/dev-credentials.js.map +1 -0
  40. package/dist/utils/get-credentials.d.ts +16 -0
  41. package/dist/utils/get-credentials.js +66 -7
  42. package/dist/utils/get-credentials.js.map +1 -1
  43. package/dist/utils/log.d.ts +2 -0
  44. package/dist/utils/log.js +24 -0
  45. package/dist/utils/log.js.map +1 -0
  46. package/dist/version.d.ts +1 -1
  47. package/dist/version.js +1 -1
  48. package/package.json +4 -1
  49. package/src/api-client/api-client.test.ts +128 -0
  50. package/src/api-client/with-retry.ts +1 -1
  51. package/src/auth/api.ts +31 -0
  52. package/src/auth/error.ts +8 -0
  53. package/src/auth/file.ts +69 -0
  54. package/src/auth/index.ts +9 -0
  55. package/src/auth/infer-scope.test.ts +178 -0
  56. package/src/auth/linked-project.test.ts +86 -0
  57. package/src/auth/linked-project.ts +40 -0
  58. package/src/auth/oauth.ts +333 -0
  59. package/src/auth/poll-for-token.ts +89 -0
  60. package/src/auth/project.ts +92 -0
  61. package/src/auth/zod.ts +16 -0
  62. package/src/sandbox.ts +1 -1
  63. package/src/utils/dev-credentials.test.ts +217 -0
  64. package/src/utils/dev-credentials.ts +189 -0
  65. package/src/utils/get-credentials.test.ts +20 -0
  66. package/src/utils/get-credentials.ts +72 -8
  67. package/src/utils/log.ts +20 -0
  68. package/src/version.ts +1 -1
  69. package/test-utils/mock-response.ts +12 -0
  70. package/vitest.config.ts +1 -0
@@ -1,6 +1,10 @@
1
1
  import { getVercelOidcToken } from "@vercel/oidc";
2
2
  import { decodeBase64Url } from "./decode-base64-url";
3
3
  import { z } from "zod";
4
+ import {
5
+ cachedGenerateCredentials,
6
+ shouldPromptForCredentials,
7
+ } from "./dev-credentials";
4
8
 
5
9
  export interface Credentials {
6
10
  /**
@@ -18,6 +22,57 @@ export interface Credentials {
18
22
  teamId: string;
19
23
  }
20
24
 
25
+ /**
26
+ * Error thrown when OIDC context is not available in local development,
27
+ * therefore we should guide how to ensure it is set up by linking a project
28
+ */
29
+ export class LocalOidcContextError extends Error {
30
+ name = "LocalOidcContextError";
31
+ constructor(cause: unknown) {
32
+ const message = [
33
+ "Could not get credentials from OIDC context.",
34
+ "Please link your Vercel project using `npx vercel link`.",
35
+ "Then, pull an initial OIDC token with `npx vercel env pull`",
36
+ "and retry.",
37
+ ].join("\n");
38
+ super(message, { cause });
39
+ }
40
+ }
41
+
42
+ /**
43
+ * Error thrown when OIDC context is not available in Vercel environment,
44
+ * therefore we should guide how to set it up.
45
+ */
46
+ export class VercelOidcContextError extends Error {
47
+ name = "VercelOidcContextError";
48
+ constructor(cause: unknown) {
49
+ const message = [
50
+ "Could not get credentials from OIDC context.",
51
+ "Please make sure OIDC is set up for your project",
52
+ "Read more at https://vercel.com/docs/oidc",
53
+ ].join("\n");
54
+ super(message, { cause });
55
+ }
56
+ }
57
+
58
+ async function getVercelToken(opts: {
59
+ teamId?: string;
60
+ projectId?: string;
61
+ }): Promise<Credentials> {
62
+ try {
63
+ return getCredentialsFromOIDCToken(await getVercelOidcToken());
64
+ } catch (error) {
65
+ if (!shouldPromptForCredentials()) {
66
+ if (process.env.VERCEL_URL) {
67
+ throw new VercelOidcContextError(error);
68
+ } else {
69
+ throw new LocalOidcContextError(error);
70
+ }
71
+ }
72
+ return await cachedGenerateCredentials(opts);
73
+ }
74
+ }
75
+
21
76
  /**
22
77
  * Allow to get credentials to access the Vercel API. Credentials can be
23
78
  * provided in two different ways:
@@ -34,15 +89,24 @@ export async function getCredentials(params?: unknown): Promise<Credentials> {
34
89
  return credentials;
35
90
  }
36
91
 
37
- const oidcToken = await getVercelOidcToken();
38
- if (oidcToken) {
39
- return getCredentialsFromOIDCToken(oidcToken);
40
- }
92
+ const oidcToken = await getVercelToken({
93
+ teamId:
94
+ params &&
95
+ typeof params === "object" &&
96
+ "teamId" in params &&
97
+ typeof params.teamId === "string"
98
+ ? params.teamId
99
+ : undefined,
100
+ projectId:
101
+ params &&
102
+ typeof params === "object" &&
103
+ "projectId" in params &&
104
+ typeof params.projectId === "string"
105
+ ? params.projectId
106
+ : undefined,
107
+ });
41
108
 
42
- throw new Error(
43
- "You must provide credentials to access the Vercel API \n" +
44
- "either through parameters or using OpenID Connect [https://vercel.com/docs/oidc]",
45
- );
109
+ return oidcToken;
46
110
  }
47
111
 
48
112
  /**
@@ -0,0 +1,20 @@
1
+ import pico from "picocolors";
2
+ const colors = {
3
+ warn: pico.yellow,
4
+ error: pico.red,
5
+ success: pico.green,
6
+ info: pico.blue,
7
+ };
8
+ const logPrefix = pico.dim("[vercel/sandbox]");
9
+ export function write(
10
+ level: "warn" | "error" | "info" | "success",
11
+ text: string | string[],
12
+ ) {
13
+ text = Array.isArray(text) ? text.join("\n") : text;
14
+ const prefixed = text.replace(/^/gm, `${logPrefix} `);
15
+ console.error(colors[level](prefixed));
16
+ }
17
+
18
+ export function code(text: string) {
19
+ return pico.italic(pico.dim("`") + text + pico.dim("`"));
20
+ }
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // Autogenerated by inject-version.ts
2
- export const VERSION = "1.1.4";
2
+ export const VERSION = "1.1.5";
@@ -0,0 +1,12 @@
1
+ export function createNdjsonStream(
2
+ lines: object[],
3
+ ): ReadableStream<Uint8Array> {
4
+ const encoder = new TextEncoder();
5
+ const ndjson = lines.map((line) => JSON.stringify(line)).join("\n") + "\n";
6
+ return new ReadableStream({
7
+ start(controller) {
8
+ controller.enqueue(encoder.encode(ndjson));
9
+ controller.close();
10
+ },
11
+ });
12
+ }
package/vitest.config.ts CHANGED
@@ -2,6 +2,7 @@ import { defineConfig } from "vitest/config";
2
2
 
3
3
  export default defineConfig({
4
4
  test: {
5
+ env: { FORCE_COLOR: "1" },
5
6
  setupFiles: ["./vitest.setup.ts"],
6
7
  testTimeout: 60_000,
7
8
  },