ado-npm-auth 0.0.1

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 (107) hide show
  1. package/LICENSE.txt +21 -0
  2. package/README.md +13 -0
  3. package/bin/index.js +3 -0
  4. package/dist/ado-auto-auth.cjs +39047 -0
  5. package/dist/ado-npm-auth.cjs +39047 -0
  6. package/lib/.tsbuildinfo +1 -0
  7. package/lib/azureauth/ado.d.ts +26 -0
  8. package/lib/azureauth/ado.d.ts.map +1 -0
  9. package/lib/azureauth/ado.js +76 -0
  10. package/lib/azureauth/ado.js.map +1 -0
  11. package/lib/azureauth/azureauth-command.d.ts +7 -0
  12. package/lib/azureauth/azureauth-command.d.ts.map +1 -0
  13. package/lib/azureauth/azureauth-command.js +20 -0
  14. package/lib/azureauth/azureauth-command.js.map +1 -0
  15. package/lib/azureauth/index.d.ts +2 -0
  16. package/lib/azureauth/index.d.ts.map +1 -0
  17. package/lib/azureauth/index.js +2 -0
  18. package/lib/azureauth/index.js.map +1 -0
  19. package/lib/azureauth/is-azureauth-installed.d.ts +6 -0
  20. package/lib/azureauth/is-azureauth-installed.d.ts.map +1 -0
  21. package/lib/azureauth/is-azureauth-installed.js +24 -0
  22. package/lib/azureauth/is-azureauth-installed.js.map +1 -0
  23. package/lib/azureauth/is-supported-platform-and-architecture.d.ts +6 -0
  24. package/lib/azureauth/is-supported-platform-and-architecture.d.ts.map +1 -0
  25. package/lib/azureauth/is-supported-platform-and-architecture.js +16 -0
  26. package/lib/azureauth/is-supported-platform-and-architecture.js.map +1 -0
  27. package/lib/cli.d.ts +2 -0
  28. package/lib/cli.d.ts.map +1 -0
  29. package/lib/cli.js +61 -0
  30. package/lib/cli.js.map +1 -0
  31. package/lib/index.d.ts +1 -0
  32. package/lib/index.d.ts.map +1 -0
  33. package/lib/index.js +2 -0
  34. package/lib/index.js.map +1 -0
  35. package/lib/npmrc/base64.d.ts +7 -0
  36. package/lib/npmrc/base64.d.ts.map +1 -0
  37. package/lib/npmrc/base64.js +7 -0
  38. package/lib/npmrc/base64.js.map +1 -0
  39. package/lib/npmrc/checkADO.d.ts +10 -0
  40. package/lib/npmrc/checkADO.d.ts.map +1 -0
  41. package/lib/npmrc/checkADO.js +30 -0
  42. package/lib/npmrc/checkADO.js.map +1 -0
  43. package/lib/npmrc/generate-npmrc-pat.d.ts +6 -0
  44. package/lib/npmrc/generate-npmrc-pat.d.ts.map +1 -0
  45. package/lib/npmrc/generate-npmrc-pat.js +24 -0
  46. package/lib/npmrc/generate-npmrc-pat.js.map +1 -0
  47. package/lib/npmrc/get-repo-npmrc-ado-orgs.d.ts +12 -0
  48. package/lib/npmrc/get-repo-npmrc-ado-orgs.d.ts.map +1 -0
  49. package/lib/npmrc/get-repo-npmrc-ado-orgs.js +39 -0
  50. package/lib/npmrc/get-repo-npmrc-ado-orgs.js.map +1 -0
  51. package/lib/npmrc/getFeedWithoutProtocol.d.ts +7 -0
  52. package/lib/npmrc/getFeedWithoutProtocol.d.ts.map +1 -0
  53. package/lib/npmrc/getFeedWithoutProtocol.js +11 -0
  54. package/lib/npmrc/getFeedWithoutProtocol.js.map +1 -0
  55. package/lib/npmrc/getOrganizationFromFeedUrl.d.ts +8 -0
  56. package/lib/npmrc/getOrganizationFromFeedUrl.d.ts.map +1 -0
  57. package/lib/npmrc/getOrganizationFromFeedUrl.js +27 -0
  58. package/lib/npmrc/getOrganizationFromFeedUrl.js.map +1 -0
  59. package/lib/npmrc/getUserPAT.d.ts +13 -0
  60. package/lib/npmrc/getUserPAT.d.ts.map +1 -0
  61. package/lib/npmrc/getUserPAT.js +32 -0
  62. package/lib/npmrc/getUserPAT.js.map +1 -0
  63. package/lib/npmrc/index.d.ts +1 -0
  64. package/lib/npmrc/index.d.ts.map +1 -0
  65. package/lib/npmrc/index.js +2 -0
  66. package/lib/npmrc/index.js.map +1 -0
  67. package/lib/npmrc/is-valid-pat.d.ts +2 -0
  68. package/lib/npmrc/is-valid-pat.d.ts.map +1 -0
  69. package/lib/npmrc/is-valid-pat.js +59 -0
  70. package/lib/npmrc/is-valid-pat.js.map +1 -0
  71. package/lib/npmrc/makeADORequest.d.ts +15 -0
  72. package/lib/npmrc/makeADORequest.d.ts.map +1 -0
  73. package/lib/npmrc/makeADORequest.js +36 -0
  74. package/lib/npmrc/makeADORequest.js.map +1 -0
  75. package/lib/npmrc/makeRequest.d.ts +9 -0
  76. package/lib/npmrc/makeRequest.d.ts.map +1 -0
  77. package/lib/npmrc/makeRequest.js +38 -0
  78. package/lib/npmrc/makeRequest.js.map +1 -0
  79. package/lib/npmrc/npmrc.d.ts +15 -0
  80. package/lib/npmrc/npmrc.d.ts.map +1 -0
  81. package/lib/npmrc/npmrc.js +22 -0
  82. package/lib/npmrc/npmrc.js.map +1 -0
  83. package/lib/npmrc/pat.d.ts +13 -0
  84. package/lib/npmrc/pat.d.ts.map +1 -0
  85. package/lib/npmrc/pat.js +32 -0
  86. package/lib/npmrc/pat.js.map +1 -0
  87. package/lib/npmrc/set-npmrc-pat.d.ts +16 -0
  88. package/lib/npmrc/set-npmrc-pat.d.ts.map +1 -0
  89. package/lib/npmrc/set-npmrc-pat.js +109 -0
  90. package/lib/npmrc/set-npmrc-pat.js.map +1 -0
  91. package/lib/telemetry/index.d.ts +20 -0
  92. package/lib/telemetry/index.d.ts.map +1 -0
  93. package/lib/telemetry/index.js +21 -0
  94. package/lib/telemetry/index.js.map +1 -0
  95. package/lib/utils/exec.d.ts +4 -0
  96. package/lib/utils/exec.d.ts.map +1 -0
  97. package/lib/utils/exec.js +4 -0
  98. package/lib/utils/exec.js.map +1 -0
  99. package/lib/utils/is-codespaces.d.ts +6 -0
  100. package/lib/utils/is-codespaces.d.ts.map +1 -0
  101. package/lib/utils/is-codespaces.js +10 -0
  102. package/lib/utils/is-codespaces.js.map +1 -0
  103. package/lib/utils/is-wsl.d.ts +6 -0
  104. package/lib/utils/is-wsl.d.ts.map +1 -0
  105. package/lib/utils/is-wsl.js +9 -0
  106. package/lib/utils/is-wsl.js.map +1 -0
  107. package/package.json +39 -0
@@ -0,0 +1,39 @@
1
+ import Config from "@npmcli/config";
2
+ import { getWorkspaceRoot } from "workspace-tools";
3
+ import { join } from "node:path";
4
+ import { getOrganizationFromFeedUrl } from "./getOrganizationFromFeedUrl.js";
5
+ /**
6
+ * Determine what ADO organizations are used in the workspace's .npmrc
7
+ * These should be used to authenticate against
8
+ * @returns A list of the feeds/ado orgs used in the .npmrc
9
+ */
10
+ export const getRepoNpmrcAdoOrganizations = async () => {
11
+ var _a;
12
+ const workspaceRoot = getWorkspaceRoot(process.cwd()) || "";
13
+ let config;
14
+ // TODO: check for workspaces npmrc
15
+ try {
16
+ config = new Config({
17
+ npmPath: join(workspaceRoot, ".npmrc"),
18
+ shorthands: {},
19
+ definitions: {}, // needed so we can access random feed names
20
+ });
21
+ await config.load();
22
+ }
23
+ catch (error) {
24
+ throw new Error("No project .npmrc file found");
25
+ }
26
+ // @npmcli/config does not have a normal way to display all keys
27
+ // so we use this ugly access instead
28
+ const projectNpmrcKeys = Object.keys((((_a = config.data) === null || _a === void 0 ? void 0 : _a.get("project")) || {})["data"] || {});
29
+ // find any and all keys which are a registry
30
+ const registries = projectNpmrcKeys.filter((key) => key.includes("registry"));
31
+ return registries.map((registry) => {
32
+ const feed = config.get(registry, "project");
33
+ return {
34
+ feed,
35
+ organization: getOrganizationFromFeedUrl(feed),
36
+ };
37
+ });
38
+ };
39
+ //# sourceMappingURL=get-repo-npmrc-ado-orgs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-repo-npmrc-ado-orgs.js","sourceRoot":"","sources":["../../src/npmrc/get-repo-npmrc-ado-orgs.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,gBAAgB,CAAC;AACpC,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,0BAA0B,EAAE,MAAM,iCAA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3
+ * @param {string} feed
4
+ * @returns {string}
5
+ */
6
+ export declare const getFeedWithoutProtocol: (feed: string) => string;
7
+ //# sourceMappingURL=getFeedWithoutProtocol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getFeedWithoutProtocol.d.ts","sourceRoot":"","sources":["../../src/npmrc/getFeedWithoutProtocol.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,SAAU,MAAM,WAIlD,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Given the feed url, get the name of the feed without the protocol ("https") at the beginning
3
+ * @param {string} feed
4
+ * @returns {string}
5
+ */
6
+ export const getFeedWithoutProtocol = (feed) => {
7
+ const feedUrl = new URL(feed);
8
+ const feedWithoutProtocol = `${feedUrl.host}${feedUrl.pathname}`;
9
+ return feedWithoutProtocol;
10
+ };
11
+ //# sourceMappingURL=getFeedWithoutProtocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getFeedWithoutProtocol.js","sourceRoot":"","sources":["../../src/npmrc/getFeedWithoutProtocol.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,IAAY,EAAE,EAAE;IACrD,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC;IAC9B,MAAM,mBAAmB,GAAG,GAAG,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;IACjE,OAAO,mBAAmB,CAAC;AAC7B,CAAC,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Get the ADO Org for a npm feed
3
+ * @param {string} feedUrl URL of the feed to get the ADO organization from
4
+ * @param {string} [defaultOrg] Backup org in case it cannot be determined from the feed url
5
+ * @returns ADO Organization for the feed
6
+ */
7
+ export declare const getOrganizationFromFeedUrl: (feedUrl: string, defaultOrg?: string) => string;
8
+ //# sourceMappingURL=getOrganizationFromFeedUrl.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getOrganizationFromFeedUrl.d.ts","sourceRoot":"","sources":["../../src/npmrc/getOrganizationFromFeedUrl.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,0BAA0B,YAAa,MAAM,gCAoBzD,CAAC"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Get the ADO Org for a npm feed
3
+ * @param {string} feedUrl URL of the feed to get the ADO organization from
4
+ * @param {string} [defaultOrg] Backup org in case it cannot be determined from the feed url
5
+ * @returns ADO Organization for the feed
6
+ */
7
+ export const getOrganizationFromFeedUrl = (feedUrl, defaultOrg = "") => {
8
+ try {
9
+ const url = new URL(feedUrl);
10
+ const packagingIndex = url.pathname.indexOf("/_packaging");
11
+ if (packagingIndex > 0) {
12
+ // sometimes org is included in the path as the first item in the path
13
+ // after the initial "/"
14
+ // ex. "https://pkgs.dev.azure.com/foo/bar/_packaging/baz/npm/registry/" -> "foo"
15
+ // these are project-scoped feeds
16
+ return url.pathname.split("/")[1];
17
+ }
18
+ // otherwise feed is the first item in the host
19
+ // ex. "https://foo.pkgs.visualstudio.com/_packaging/npm-mirror/npm/registry" -> "foo"
20
+ // these are org-wide feeds
21
+ return url.host.split(".")[0];
22
+ }
23
+ catch (error) {
24
+ return defaultOrg;
25
+ }
26
+ };
27
+ //# sourceMappingURL=getOrganizationFromFeedUrl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getOrganizationFromFeedUrl.js","sourceRoot":"","sources":["../../src/npmrc/getOrganizationFromFeedUrl.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,OAAe,EAAE,UAAU,GAAG,EAAE,EAAE,EAAE;IAC7E,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC;QAC7B,MAAM,cAAc,GAAG,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;QAE3D,IAAI,cAAc,GAAG,CAAC,EAAE,CAAC;YACvB,sEAAsE;YACtE,wBAAwB;YACxB,iFAAiF;YACjF,iCAAiC;YACjC,OAAO,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACpC,CAAC;QAED,+CAA+C;QAC/C,sFAAsF;QACtF,2BAA2B;QAC3B,OAAO,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IAChC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,UAAU,CAAC;IACpB,CAAC;AACH,CAAC,CAAC"}
@@ -0,0 +1,13 @@
1
+ import { NpmrcOrg } from "./get-repo-npmrc-ado-orgs.js";
2
+ /**
3
+ * Get the User's PAT from the .npmrc file
4
+ * @param {Object} options
5
+ * @param {string} options.npmrc Path to the users' .npmrc file
6
+ * @param {Array<NpmrcOrg> } [options.feeds] Feeds to get the PATs for
7
+ * @returns {Promise<Array<NpmrcOrg>> }
8
+ */
9
+ export declare const getUserPat: ({ npmrc, feeds }: {
10
+ npmrc: string;
11
+ feeds?: NpmrcOrg[];
12
+ }) => Promise<NpmrcOrg[]>;
13
+ //# sourceMappingURL=getUserPAT.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getUserPAT.d.ts","sourceRoot":"","sources":["../../src/npmrc/getUserPAT.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AAIxD;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,qBAAiC;IACtD,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,QAAQ,EAAE,CAAC;CACpB,wBAwBA,CAAC"}
@@ -0,0 +1,32 @@
1
+ import { decode } from "./base64.js";
2
+ import { getFeedWithoutProtocol } from "./getFeedWithoutProtocol.js";
3
+ import { readNpmRC } from "./npmrc.js";
4
+ /**
5
+ * Get the User's PAT from the .npmrc file
6
+ * @param {Object} options
7
+ * @param {string} options.npmrc Path to the users' .npmrc file
8
+ * @param {Array<NpmrcOrg> } [options.feeds] Feeds to get the PATs for
9
+ * @returns {Promise<Array<NpmrcOrg>> }
10
+ */
11
+ export const getUserPat = async ({ npmrc, feeds = [] }) => {
12
+ try {
13
+ const contents = await readNpmRC({ npmrc });
14
+ const feedsWithPat = feeds.slice(); // create a copy of feeds
15
+ for (let i = 0; i < feeds.length; i++) {
16
+ const match = contents.toString().match(
17
+ // SHould match the URL from the user .npmrc
18
+ new RegExp(`${getFeedWithoutProtocol(feeds[i].feed)}:_password=(.*)`));
19
+ if (match) {
20
+ const [, base64password] = match;
21
+ const pat = decode(base64password);
22
+ feedsWithPat[i].pat = pat;
23
+ }
24
+ }
25
+ return feedsWithPat;
26
+ }
27
+ catch (error) {
28
+ // user npmrc does not exist, or is invalid
29
+ return [];
30
+ }
31
+ };
32
+ //# sourceMappingURL=getUserPAT.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getUserPAT.js","sourceRoot":"","sources":["../../src/npmrc/getUserPAT.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,EAAE,EAAE,KAAK,EAAE,KAAK,GAAG,EAAE,EAGnD,EAAE,EAAE;IACH,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,MAAM,SAAS,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;QAC5C,MAAM,YAAY,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,yBAAyB;QAE7D,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACtC,MAAM,KAAK,GAAG,QAAQ,CAAC,QAAQ,EAAE,CAAC,KAAK;YACrC,4CAA4C;YAC5C,IAAI,MAAM,CAAC,GAAG,sBAAsB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,iBAAiB,CAAC,CACtE,CAAC;YAEF,IAAI,KAAK,EAAE,CAAC;gBACV,MAAM,CAAC,EAAE,cAAc,CAAC,GAAG,KAAK,CAAC;gBACjC,MAAM,GAAG,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC;gBAEnC,YAAY,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,CAAC;YAC5B,CAAC;QACH,CAAC;QAED,OAAO,YAAY,CAAC;IACtB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,2CAA2C;QAC3C,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/npmrc/index.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/npmrc/index.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export declare const isValidPat: () => Promise<boolean>;
2
+ //# sourceMappingURL=is-valid-pat.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-valid-pat.d.ts","sourceRoot":"","sources":["../../src/npmrc/is-valid-pat.ts"],"names":[],"mappings":"AAUA,eAAO,MAAM,UAAU,QAAa,QAAQ,OAAO,CAKlD,CAAC"}
@@ -0,0 +1,59 @@
1
+ import { checkADO } from "./checkADO.js";
2
+ import { getOrganizationFromFeedUrl } from "./getOrganizationFromFeedUrl.js";
3
+ import { homedir, EOL } from "node:os";
4
+ import { join } from "node:path";
5
+ import { readFile, writeFile } from "node:fs/promises";
6
+ import { getRepoNpmrcAdoOrganizations, } from "./get-repo-npmrc-ado-orgs.js";
7
+ export const isValidPat = async () => {
8
+ await removeUserNpmrcRegistries();
9
+ const feeds = await getNpmrcFeeds();
10
+ return await checkADO({ feeds });
11
+ };
12
+ const removeUserNpmrcRegistries = async () => {
13
+ const userNpmrc = join(homedir(), ".npmrc");
14
+ try {
15
+ const npmrcContent = await readFile(userNpmrc, "utf-8");
16
+ // remove the entry for registries in the user-level .npmrc
17
+ const updatedNpmrcContent = npmrcContent
18
+ .split(EOL)
19
+ .filter((line) => !line.includes("registry="))
20
+ .join(EOL);
21
+ await writeFile(userNpmrc, updatedNpmrcContent);
22
+ }
23
+ catch (error) {
24
+ // user npmrc does not exist so make an empty one
25
+ await writeFile(userNpmrc, "");
26
+ }
27
+ };
28
+ const getNpmrcFeeds = async () => {
29
+ const projectNpmrcOrgs = await getRepoNpmrcAdoOrganizations();
30
+ const userNpmrc = await getUserNpmrcFeeds();
31
+ // make a lookup map for pats by org
32
+ const tokenMap = userNpmrc.reduce((acc, curr) => {
33
+ acc[curr.organization] = curr.pat;
34
+ return acc;
35
+ }, {});
36
+ // try to fill up pats for each project feed from user npmrc feeds
37
+ projectNpmrcOrgs.forEach((feed) => {
38
+ feed.pat = tokenMap[feed.organization];
39
+ });
40
+ return projectNpmrcOrgs;
41
+ };
42
+ const getUserNpmrcFeeds = async () => {
43
+ const userNpmrcFilepath = join(homedir(), ".npmrc");
44
+ const userNpmrcContent = await readFile(userNpmrcFilepath, "utf-8");
45
+ return userNpmrcContent
46
+ .split(EOL)
47
+ .filter((line) => line.includes(":_password="))
48
+ .map((line) => {
49
+ const [feedRaw, pat] = line.split(":_password=");
50
+ const feed = `https:${feedRaw}`;
51
+ const organization = getOrganizationFromFeedUrl(feed);
52
+ return {
53
+ feed,
54
+ organization,
55
+ pat,
56
+ };
57
+ });
58
+ };
59
+ //# sourceMappingURL=is-valid-pat.js.map
@@ -0,0 +1 @@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,GAAG;SACJ,CAAC;IACJ,CAAC,CAAC,CAAC;AACP,CAAC,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Check if the user is authenticated to the ADO API.
3
+ * This function performs a network request to retrieve an ADO feed for the given organization
4
+ * Since that request is usually slow, and we don't actually know the ID of a valid feed, we just use the "invalid" ID of "1"
5
+ * If the network request returns 401, then the PAT is invalid. If the request returns 404 (or somehow 200) then the PAT is valid
6
+ * @param {Object} options
7
+ * @param {string} options.password The password to use for the login
8
+ * @param {string} options.organization The organization to check against
9
+ * @returns
10
+ */
11
+ export declare const makeADORequest: ({ password, organization }: {
12
+ password: string;
13
+ organization: string;
14
+ }) => Promise<void>;
15
+ //# sourceMappingURL=makeADORequest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makeADORequest.d.ts","sourceRoot":"","sources":["../../src/npmrc/makeADORequest.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,eAAO,MAAM,cAAc,+BAAsC;IAC/D,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;CACtB,kBAyBA,CAAC"}
@@ -0,0 +1,36 @@
1
+ import { makeRequest } from "./makeRequest.js";
2
+ /**
3
+ * Check if the user is authenticated to the ADO API.
4
+ * This function performs a network request to retrieve an ADO feed for the given organization
5
+ * Since that request is usually slow, and we don't actually know the ID of a valid feed, we just use the "invalid" ID of "1"
6
+ * If the network request returns 401, then the PAT is invalid. If the request returns 404 (or somehow 200) then the PAT is valid
7
+ * @param {Object} options
8
+ * @param {string} options.password The password to use for the login
9
+ * @param {string} options.organization The organization to check against
10
+ * @returns
11
+ */
12
+ export const makeADORequest = async ({ password, organization }) => {
13
+ const auth = `Basic ${Buffer.from(`.:${password}`).toString("base64")}`;
14
+ const options = {
15
+ hostname: "feeds.dev.azure.com",
16
+ port: 443,
17
+ path: `/${organization}/_apis/packaging/feeds/1?api-version=6.1-preview.1`,
18
+ method: "GET",
19
+ headers: {
20
+ Accept: "application/json",
21
+ Authorization: auth,
22
+ },
23
+ };
24
+ try {
25
+ await makeRequest(options);
26
+ }
27
+ catch ( /** @type {any} */error) {
28
+ if (error.statusCode === 404) {
29
+ // this is the good case, because we use a non-existant feed ID
30
+ // but if you get a 404 it means you can even determine that with a valid token
31
+ return;
32
+ }
33
+ throw error;
34
+ }
35
+ };
36
+ //# sourceMappingURL=makeADORequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makeADORequest.js","sourceRoot":"","sources":["../../src/npmrc/makeADORequest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,KAAK,EAAE,EAAE,QAAQ,EAAE,YAAY,EAG5D,EAAE,EAAE;IACH,MAAM,IAAI,GAAG,SAAS,MAAM,CAAC,IAAI,CAAC,KAAK,QAAQ,EAAE,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;IAExE,MAAM,OAAO,GAAG;QACd,QAAQ,EAAE,qBAAqB;QAC/B,IAAI,EAAE,GAAG;QACT,IAAI,EAAE,IAAI,YAAY,oDAAoD;QAC1E,MAAM,EAAE,KAAK;QACb,OAAO,EAAE;YACP,MAAM,EAAE,kBAAkB;YAC1B,aAAa,EAAE,IAAI;SACpB;KACF,CAAC;IAEF,IAAI,CAAC;QACH,MAAM,WAAW,CAAC,OAAO,CAAC,CAAC;IAC7B,CAAC;IAAC,QAAO,kBAAmB,KAAK,EAAE,CAAC;QAClC,IAAK,KAAa,CAAC,UAAU,KAAK,GAAG,EAAE,CAAC;YACtC,+DAA+D;YAC/D,+EAA+E;YAC/E,OAAO;QACT,CAAC;QAED,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC,CAAC"}
@@ -0,0 +1,9 @@
1
+ /// <reference types="node" />
2
+ import { RequestOptions } from "https";
3
+ /**
4
+ *
5
+ * @param {import("http").RequestOptions} options
6
+ * @returns
7
+ */
8
+ export declare const makeRequest: (options: RequestOptions) => Promise<unknown>;
9
+ //# sourceMappingURL=makeRequest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makeRequest.d.ts","sourceRoot":"","sources":["../../src/npmrc/makeRequest.ts"],"names":[],"mappings":";AAAA,OAAc,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AAC9C;;;;GAIG;AACH,eAAO,MAAM,WAAW,YAAmB,cAAc,qBAmCxD,CAAC"}
@@ -0,0 +1,38 @@
1
+ import https from "https";
2
+ /**
3
+ *
4
+ * @param {import("http").RequestOptions} options
5
+ * @returns
6
+ */
7
+ export const makeRequest = async (options) => {
8
+ return new Promise((resolve, reject) => {
9
+ const req = https.request(options, (res) => {
10
+ let data = "";
11
+ let dataJson = {};
12
+ let ok = res.statusCode === 200;
13
+ res.on("data", (d) => {
14
+ data += d;
15
+ });
16
+ res.on("end", () => {
17
+ var _a;
18
+ if (data && ((_a = options === null || options === void 0 ? void 0 : options.headers) === null || _a === void 0 ? void 0 : _a.Accept) === "application/json") {
19
+ dataJson = JSON.parse(data.toString().trim());
20
+ }
21
+ if (ok) {
22
+ resolve(dataJson || data);
23
+ }
24
+ else {
25
+ if (dataJson) {
26
+ dataJson = { ...dataJson, statusCode: res.statusCode };
27
+ }
28
+ reject(dataJson || data || new Error(`Error code: ${res.statusCode}.`));
29
+ }
30
+ });
31
+ res.on("error", (/** @type {string} */ error) => {
32
+ reject(new Error(error));
33
+ });
34
+ });
35
+ req.end();
36
+ });
37
+ };
38
+ //# sourceMappingURL=makeRequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makeRequest.js","sourceRoot":"","sources":["../../src/npmrc/makeRequest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAyB,MAAM,OAAO,CAAC;AAC9C;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAAE,OAAuB,EAAE,EAAE;IAC3D,OAAO,IAAI,OAAO,CAAC,CAA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AC;QAEH,GAAG,CAAC,GAAG,EAAE,CAAC;IACZ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Cross platform way to find the users' .npmrc file
3
+ * @returns The user's npmrc from home or user profile
4
+ */
5
+ export declare const getUserNPMRC: () => string;
6
+ /**
7
+ * Read the an .npmrc file
8
+ * @param {Object} options
9
+ * @param {string} options.npmrc Path to the users' .npmrc file
10
+ * @returns
11
+ */
12
+ export declare const readNpmRC: ({ npmrc }: {
13
+ npmrc: string;
14
+ }) => Promise<string>;
15
+ //# sourceMappingURL=npmrc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"npmrc.d.ts","sourceRoot":"","sources":["../../src/npmrc/npmrc.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,eAAO,MAAM,YAAY,cAKxB,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,SAAS,cAAqB;IAAE,KAAK,EAAE,MAAM,CAAC;CAAE,oBAI5D,CAAC"}
@@ -0,0 +1,22 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ /**
4
+ * Cross platform way to find the users' .npmrc file
5
+ * @returns The user's npmrc from home or user profile
6
+ */
7
+ export const getUserNPMRC = () => {
8
+ const userHome = process.env["HOME"] || process.env["USERPROFILE"] || "";
9
+ const userNpmRc = path.join(userHome, ".npmrc");
10
+ return userNpmRc;
11
+ };
12
+ /**
13
+ * Read the an .npmrc file
14
+ * @param {Object} options
15
+ * @param {string} options.npmrc Path to the users' .npmrc file
16
+ * @returns
17
+ */
18
+ export const readNpmRC = async ({ npmrc }) => {
19
+ const contents = await fs.promises.readFile(npmrc, "utf8");
20
+ return contents;
21
+ };
22
+ //# sourceMappingURL=npmrc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"npmrc.js","sourceRoot":"","sources":["../../src/npmrc/npmrc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,GAAG,EAAE;IAC/B,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;IACzE,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAEhD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,KAAK,EAAE,EAAE,KAAK,EAAsB,EAAE,EAAE;IAC/D,MAAM,QAAQ,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAE3D,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC"}
@@ -0,0 +1,13 @@
1
+ import { NpmrcOrg } from "./get-repo-npmrc-ado-orgs.js";
2
+ /**
3
+ * Get the User's PAT from the .npmrc file
4
+ * @param {Object} options
5
+ * @param {string} options.npmrc Path to the users' .npmrc file
6
+ * @param { Array<NpmrcOrg> } [options.feeds] Feeds to get the PATs for
7
+ * @returns { Promise<Array<NpmrcOrg>> }
8
+ */
9
+ export declare const getUserPat: ({ npmrc, feeds }: {
10
+ npmrc: string;
11
+ feeds: NpmrcOrg[];
12
+ }) => Promise<NpmrcOrg[]>;
13
+ //# sourceMappingURL=pat.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pat.d.ts","sourceRoot":"","sources":["../../src/npmrc/pat.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AAIxD;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,qBAAiC;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,QAAQ,EAAE,CAAA;CAAC,wBAwB1F,CAAC"}
@@ -0,0 +1,32 @@
1
+ import { decode } from "./base64.js";
2
+ import { getFeedWithoutProtocol } from "./getFeedWithoutProtocol.js";
3
+ import { readNpmRC } from "./npmrc.js";
4
+ /**
5
+ * Get the User's PAT from the .npmrc file
6
+ * @param {Object} options
7
+ * @param {string} options.npmrc Path to the users' .npmrc file
8
+ * @param { Array<NpmrcOrg> } [options.feeds] Feeds to get the PATs for
9
+ * @returns { Promise<Array<NpmrcOrg>> }
10
+ */
11
+ export const getUserPat = async ({ npmrc, feeds = [] }) => {
12
+ try {
13
+ const contents = await readNpmRC({ npmrc });
14
+ const feedsWithPat = feeds.slice(); // create a copy of feeds
15
+ for (let i = 0; i < feeds.length; i++) {
16
+ const match = contents.toString().match(
17
+ // SHould match the URL from the user .npmrc
18
+ new RegExp(`${getFeedWithoutProtocol(feeds[i].feed)}:_password=(.*)`));
19
+ if (match) {
20
+ const [, base64password] = match;
21
+ const pat = decode(base64password);
22
+ feedsWithPat[i].pat = pat;
23
+ }
24
+ }
25
+ return feedsWithPat;
26
+ }
27
+ catch (error) {
28
+ // user npmrc does not exist, or is invalid
29
+ return [];
30
+ }
31
+ };
32
+ //# sourceMappingURL=pat.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pat.js","sourceRoot":"","sources":["../../src/npmrc/pat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,EAAE,EAAE,KAAK,EAAE,KAAK,GAAG,EAAE,EAAuC,EAAE,EAAE;IAC7F,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,MAAM,SAAS,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;QAC5C,MAAM,YAAY,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,yBAAyB;QAE7D,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACtC,MAAM,KAAK,GAAG,QAAQ,CAAC,QAAQ,EAAE,CAAC,KAAK;YACrC,4CAA4C;YAC5C,IAAI,MAAM,CAAC,GAAG,sBAAsB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,iBAAiB,CAAC,CACtE,CAAC;YAEF,IAAI,KAAK,EAAE,CAAC;gBACV,MAAM,CAAC,EAAE,cAAc,CAAC,GAAG,KAAK,CAAC;gBACjC,MAAM,GAAG,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC;gBAEnC,YAAY,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,CAAC;YAC5B,CAAC;QACH,CAAC;QAED,OAAO,YAAY,CAAC;IACtB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,2CAA2C;QAC3C,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC,CAAC"}
@@ -0,0 +1,16 @@
1
+ import { NpmrcOrg } from "./get-repo-npmrc-ado-orgs.js";
2
+ /**
3
+ *
4
+ * @param {Object} options
5
+ * @param {Record<string, string>} [options.organizationPatMap] Map of ADO Organization to packaging PAT for that org
6
+ * @param {boolean} [options.accept]
7
+ * @param {string} [options.npmrcFilePath]
8
+ * @param {boolean} [options.silent]
9
+ * @returnsshort
10
+ */
11
+ export declare const setupNpmrc: ({ organizationPatMap, feeds, }: {
12
+ organizationPatMap: Record<string, string>;
13
+ feeds: NpmrcOrg[];
14
+ }) => Promise<void>;
15
+ export declare const setNpmrcPat: () => Promise<void>;
16
+ //# sourceMappingURL=set-npmrc-pat.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set-npmrc-pat.d.ts","sourceRoot":"","sources":["../../src/npmrc/set-npmrc-pat.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAgC,MAAM,8BAA8B,CAAC;AAyEtF;;;;;;;;GAQG;AACH,eAAO,MAAM,UAAU,mCAGpB;IACD,kBAAkB,EAAE,OAAO,MAAM,EAAE,MAAM,CAAC,CAAC;IAC3C,KAAK,EAAE,QAAQ,EAAE,CAAC;CACnB,kBAgCA,CAAC;AAEF,eAAO,MAAM,WAAW,QAAa,QAAQ,IAAI,CAgBhD,CAAC"}
@@ -0,0 +1,109 @@
1
+ import fs from "fs";
2
+ import { EOL } from "os";
3
+ import { generateNpmrcPat } from "./generate-npmrc-pat.js";
4
+ import { getRepoNpmrcAdoOrganizations } from "./get-repo-npmrc-ado-orgs.js";
5
+ import { getUserNPMRC, readNpmRC } from "./npmrc.js";
6
+ import { getFeedWithoutProtocol } from "./getFeedWithoutProtocol.js";
7
+ // /**
8
+ // * URL for the user's PAT settings
9
+ // * @param { string } organization
10
+ // */
11
+ // const getTokenUrl = (organization: string) =>
12
+ // `https://dev.azure.com/${organization}/_usersSettings/tokens`;
13
+ /**
14
+ * Default user to be used in the .npmrc
15
+ */
16
+ const defaultUser = "me";
17
+ /**
18
+ * Default email to be used in the .npmrc
19
+ */
20
+ const defaultEmail = "me@example.com";
21
+ /**
22
+ * Create a base64 encoded string from a string
23
+ * @param {string} input
24
+ * @returns {string}
25
+ */
26
+ const base64 = (input) => {
27
+ return Buffer.from(input).toString("base64");
28
+ };
29
+ /**
30
+ * Create the user npmrc given an array of registry urls and a token
31
+ * @param {object} options
32
+ * @param {Array<import("../../utils/pat.js").NpmrcOrg> } options.feeds
33
+ * @param {string} options.existingNpmrc
34
+ * @returns {string}
35
+ */
36
+ const createUserNpmrc = async ({ feeds, existingNpmrc }) => {
37
+ let newUserNpmRc = existingNpmrc;
38
+ if (newUserNpmRc.indexOf("Azure Artifacts Auth") === -1) {
39
+ newUserNpmRc = `# Generated by Azure Artifacts Auth${EOL}${newUserNpmRc}`;
40
+ }
41
+ for (const feed of feeds) {
42
+ const feedWithoutProtocol = "//" + getFeedWithoutProtocol(feed.feed);
43
+ let isAlreadyInNpmrc = newUserNpmRc.indexOf(feedWithoutProtocol) > -1;
44
+ if (isAlreadyInNpmrc) {
45
+ newUserNpmRc = newUserNpmRc.replace(new RegExp(`${feedWithoutProtocol}:_password\=(.*)`), `${feedWithoutProtocol}:_password=${feed.pat}`);
46
+ }
47
+ else {
48
+ const entry = [
49
+ `${feedWithoutProtocol}:username=${defaultUser}`,
50
+ `${feedWithoutProtocol}:email=${defaultEmail}`,
51
+ `${feedWithoutProtocol}:_password=${feed.pat}`,
52
+ "",
53
+ ].join(EOL);
54
+ newUserNpmRc += entry + EOL;
55
+ }
56
+ }
57
+ return newUserNpmRc;
58
+ };
59
+ const userNpmrc = getUserNPMRC();
60
+ /**
61
+ *
62
+ * @param {Object} options
63
+ * @param {Record<string, string>} [options.organizationPatMap] Map of ADO Organization to packaging PAT for that org
64
+ * @param {boolean} [options.accept]
65
+ * @param {string} [options.npmrcFilePath]
66
+ * @param {boolean} [options.silent]
67
+ * @returnsshort
68
+ */
69
+ export const setupNpmrc = async function ({ organizationPatMap = {}, feeds, }) {
70
+ for (const feed of feeds) {
71
+ try {
72
+ let pat = organizationPatMap[feed.organization];
73
+ if (!pat) {
74
+ throw new Error(`No PAT found for ${feed.organization}`);
75
+ }
76
+ const b64Pat = base64(pat);
77
+ feed.pat = b64Pat;
78
+ }
79
+ catch (e) {
80
+ throw new Error(`Error setting up npmrc for ${feed.organization} organization: ${e.message}`);
81
+ }
82
+ }
83
+ let userNpmrcFile = "";
84
+ try {
85
+ userNpmrcFile = await readNpmRC({ npmrc: userNpmrc });
86
+ }
87
+ catch (e) {
88
+ /* No user .npmrc file, that's ok */
89
+ }
90
+ // const newnpmrc =
91
+ let newnpmrc = await createUserNpmrc({
92
+ existingNpmrc: userNpmrcFile,
93
+ feeds,
94
+ });
95
+ await fs.promises.writeFile(userNpmrc, newnpmrc);
96
+ };
97
+ export const setNpmrcPat = async () => {
98
+ const adoOrgs = await getRepoNpmrcAdoOrganizations();
99
+ // get a token for each feed
100
+ const organizationPatMap = {};
101
+ for (const adoOrg of adoOrgs) {
102
+ organizationPatMap[adoOrg.organization] = await generateNpmrcPat(adoOrg.organization, false);
103
+ }
104
+ await setupNpmrc({
105
+ organizationPatMap,
106
+ feeds: adoOrgs
107
+ });
108
+ };
109
+ //# sourceMappingURL=set-npmrc-pat.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set-npmrc-pat.js","sourceRoot":"","sources":["../../src/npmrc/set-npmrc-pat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAE,GAAG,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAY,4BAA4B,EAAE,MAAM,8BAA8B,CAAC;AACtF,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AAErE,MAAM;AACN,qCAAqC;AACrC,oCAAoC;AACpC,MAAM;AACN,gDAAgD;AAChD,mEAAmE;AAEnE;;GAEG;AACH,MAAM,WAAW,GAAG,IAAI,CAAC;AAEzB;;GAEG;AACH,MAAM,YAAY,GAAG,gBAAgB,CAAC;AAEtC;;;;GAIG;AACH,MAAM,MAAM,GAAG,CAAC,KAAa,EAAE,EAAE;IAC/B,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AAC/C,CAAC,CAAC;AAGF;;;;;;GAMG;AACH,MAAM,eAAe,GAAG,KAAK,EAAE,EAAE,KAAK,EAAE,aAAa,EAGpD,EAAE,EAAE;IACH,IAAI,YAAY,GAAG,aAAa,CAAC;IAEjC,IAAI,YAAY,CAAC,OAAO,CAAC,sBAAsB,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;QACxD,YAAY,GAAG,sCAAsC,GAAG,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