Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6756bc5

Browse files
committed
fix(token-providers): break dependency cycle with sso-oidc
1 parent 727ebec commit 6756bc5

File tree

12 files changed

+247
-13
lines changed

12 files changed

+247
-13
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"concurrently": "7.0.0",
8181
"decomment": "0.9.5",
8282
"downlevel-dts": "0.10.1",
83+
"esbuild": "0.18.17",
8384
"eslint": "8.36.0",
8485
"eslint-config-prettier": "8.5.0",
8586
"eslint-plugin-prettier": "4.0.0",

packages/token-providers/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { fromStatic } from "@aws-sdk/token-providers"
1515
const token = { token: "TOKEN" };
1616
const staticTokenProvider = fromStatic(token);
1717

18-
cont staticToken = await staticTokenProvider(); // returns { token: "TOKEN" }
18+
const staticToken = await staticTokenProvider(); // returns { token: "TOKEN" }
1919
```
2020

2121
## SSO Token Provider
@@ -24,7 +24,7 @@ cont staticToken = await staticTokenProvider(); // returns { token: "TOKEN" }
2424
import { fromSso } from "@aws-sdk/token-providers"
2525

2626
// returns token from SSO token cache or ssoOidc.createToken() call.
27-
cont ssoToken = await fromSso();
27+
const ssoToken = await fromSso();
2828
```
2929

3030
## Token Provider Chain
@@ -33,7 +33,7 @@ cont ssoToken = await fromSso();
3333
import { nodeProvider } from "@aws-sdk/token-providers"
3434

3535
// returns token from default providers.
36-
cont token = await nodeProvider();
36+
const token = await nodeProvider();
3737
```
3838

3939
[http-bearer-auth-trait]: https://smithy.io/2.0/spec/authentication-traits.html#smithy-api-httpbearerauth-trait

packages/token-providers/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"module": "./dist-es/index.js",
77
"sideEffects": false,
88
"scripts": {
9-
"build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",
9+
"build:client-dist": "node ./scripts/esbuild",
10+
"build": "yarn build:client-dist && concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",
1011
"build:cjs": "tsc -p tsconfig.cjs.json",
1112
"build:es": "tsc -p tsconfig.es.json",
1213
"build:include:deps": "lerna run --scope $npm_package_name --include-dependencies build",
@@ -26,7 +27,6 @@
2627
},
2728
"license": "Apache-2.0",
2829
"dependencies": {
29-
"@aws-sdk/client-sso-oidc": "*",
3030
"@aws-sdk/types": "*",
3131
"@smithy/property-provider": "^2.0.0",
3232
"@smithy/shared-ini-file-loader": "^2.0.0",
@@ -56,6 +56,12 @@
5656
"files": [
5757
"dist-*/**"
5858
],
59+
"browser": {
60+
"./dist-es/client-sso-oidc-bundle/dist-node": "./dist-es/client-sso-oidc-bundle/dist-browser"
61+
},
62+
"react-native": {
63+
"./dist-es/client-sso-oidc-bundle/dist-node": "./dist-es/client-sso-oidc-bundle/dist-browser"
64+
},
5965
"homepage": "https://github.com/aws/aws-sdk-js-v3/tree/main/packages/token-providers",
6066
"repository": {
6167
"type": "git",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
AccessDeniedException,
3+
AuthorizationPendingException,
4+
CreateTokenCommand,
5+
ExpiredTokenException,
6+
InternalServerException,
7+
InvalidClientException,
8+
InvalidRequestException,
9+
InvalidScopeException,
10+
SlowDownException,
11+
SSOOIDCClient,
12+
UnauthorizedClientException,
13+
UnsupportedGrantTypeException,
14+
} from "../../../../clients/client-sso-oidc/src/index";
15+
16+
// create a bundled version of a subset of the SSOOIDC client
17+
// to break the cyclical dependency.
18+
19+
export {
20+
AccessDeniedException,
21+
AuthorizationPendingException,
22+
CreateTokenCommand,
23+
ExpiredTokenException,
24+
InternalServerException,
25+
InvalidClientException,
26+
InvalidRequestException,
27+
InvalidScopeException,
28+
SlowDownException,
29+
SSOOIDCClient,
30+
UnauthorizedClientException,
31+
UnsupportedGrantTypeException,
32+
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
*
3+
* This script builds a minimal bundle of the code
4+
* in SSOOIDCClient and its CreateTokenCommand, with everything else
5+
* left external.
6+
*
7+
* This is to break a cyclical dependency with the credential providers
8+
* and certain services used by those providers.
9+
*
10+
*/
11+
12+
const fs = require("fs");
13+
const path = require("path");
14+
const esbuild = require("esbuild");
15+
16+
const root = path.join(__dirname, "..", "..", "..");
17+
18+
(async () => {
19+
const defaultRuntimeConfigFile = path.join(root, "clients", "client-sso-oidc", "src", "runtimeConfig.ts");
20+
const nodeRuntimeConfig = fs.readFileSync(defaultRuntimeConfigFile);
21+
const browserRuntimeConfig = fs.readFileSync(
22+
path.join(path.dirname(defaultRuntimeConfigFile), "runtimeConfig.browser.ts")
23+
);
24+
25+
for (const platform of ["browser", "node"]) {
26+
if (platform === "browser") {
27+
fs.writeFileSync(defaultRuntimeConfigFile, browserRuntimeConfig);
28+
} else {
29+
fs.writeFileSync(defaultRuntimeConfigFile, nodeRuntimeConfig);
30+
}
31+
32+
const outfile = path.join(
33+
root,
34+
"packages",
35+
"token-providers",
36+
"src",
37+
"client-sso-oidc-bundle",
38+
`dist-${platform}.ts`
39+
);
40+
41+
await esbuild.build({
42+
platform,
43+
bundle: true,
44+
format: "esm",
45+
mainFields: ["module", "main"],
46+
entryPoints: [path.join(root, "packages", "token-providers", "scripts", "api", "source.js")],
47+
outfile: outfile,
48+
external: ["tslib", "@aws-crypto/*", "@smithy/*", "@aws-sdk/middleware-*", "@aws-sdk/types", "@aws-sdk/util-*"],
49+
});
50+
51+
await new Promise((r) => setTimeout(r, 1000));
52+
53+
fs.writeFileSync(outfile, `// @ts-nocheck \n/* eslint-disable */\n` + fs.readFileSync(outfile));
54+
}
55+
56+
fs.writeFileSync(defaultRuntimeConfigFile, nodeRuntimeConfig);
57+
})();

packages/token-providers/src/client-sso-oidc-bundle/.gitkeep

Whitespace-only changes.

packages/token-providers/src/getNewSsoOidcToken.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { CreateTokenCommand } from "@aws-sdk/client-sso-oidc";
2-
1+
import { CreateTokenCommand } from "./client-sso-oidc-bundle/dist-node";
32
import { getNewSsoOidcToken } from "./getNewSsoOidcToken";
43
import { getSsoOidcClient } from "./getSsoOidcClient";
54

6-
jest.mock("@aws-sdk/client-sso-oidc");
5+
jest.mock("./client-sso-oidc-bundle/dist-node");
76
jest.mock("./getSsoOidcClient");
87

98
describe(getNewSsoOidcToken.name, () => {

packages/token-providers/src/getNewSsoOidcToken.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { CreateTokenCommand } from "@aws-sdk/client-sso-oidc";
21
import { SSOToken } from "@smithy/shared-ini-file-loader";
32

3+
import { CreateTokenCommand } from "./client-sso-oidc-bundle/dist-node";
44
import { getSsoOidcClient } from "./getSsoOidcClient";
55

66
/**

packages/token-providers/src/getSsoOidcClient.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { SSOOIDCClient } from "@aws-sdk/client-sso-oidc";
1+
import { SSOOIDCClient } from "./client-sso-oidc-bundle/dist-node";
22

3-
jest.mock("@aws-sdk/client-sso-oidc");
3+
jest.mock("./client-sso-oidc-bundle/dist-node");
44

55
describe("getSsoOidcClient", () => {
66
const mockSsoRegion = "mockSsoRegion";

packages/token-providers/src/getSsoOidcClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { SSOOIDCClient } from "@aws-sdk/client-sso-oidc";
1+
import { SSOOIDCClient } from "./client-sso-oidc-bundle/dist-node";
22

3-
const ssoOidcClientsHash: Record<string, SSOOIDCClient> = {};
3+
const ssoOidcClientsHash: Record<string, typeof SSOOIDCClient | any> = {};
44

55
/**
66
* Returns a SSOOIDC client for the given region. If the client has already been created,

0 commit comments

Comments
 (0)