@backstage/integration 2.0.2-next.0 → 2.0.2-next.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @backstage/integration
2
2
 
3
+ ## 2.0.2-next.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 6b112d3: Fixed two issues in the GitLab integration's fetch behavior:
8
+
9
+ - The internal fetch wrapper was passing `mode: 'same-origin'` on every request. This had no practical effect server-side, but would have caused cross-origin requests to be rejected when the integration is used from a browser. Requests now use the default fetch mode and work correctly in both browser and Node environments.
10
+ - When retries are configured, transient network errors (such as dropped connections or DNS hiccups) are now retried using the same `maxRetries` and exponential delay as retryable HTTP status codes. Previously, a thrown fetch error would propagate immediately on the first failure regardless of the retry configuration. Caller-initiated aborts continue to surface immediately without being retried.
11
+
3
12
  ## 2.0.2-next.0
4
13
 
5
14
  ### Patch Changes
@@ -42,9 +42,7 @@ class GitLabIntegration {
42
42
  return this.fetchImpl(input, init);
43
43
  }
44
44
  createFetchStrategy() {
45
- let fetchFn = async (url, options) => {
46
- return fetch(url, { ...options, mode: "same-origin" });
47
- };
45
+ let fetchFn = (url, options) => fetch(url, options);
48
46
  const retryConfig = this.integrationConfig.retry;
49
47
  if (retryConfig) {
50
48
  fetchFn = this.withRetry(fetchFn, retryConfig);
@@ -63,26 +61,55 @@ class GitLabIntegration {
63
61
  if (maxRetries <= 0 || retryStatusCodes.length === 0) {
64
62
  return fetchFn;
65
63
  }
64
+ const backoffDelay = (a) => Math.min(100 * Math.pow(2, a - 1), 1e4);
66
65
  return async (url, options) => {
67
66
  const abortSignal = options?.signal;
68
- let response;
69
67
  let attempt = 0;
70
68
  for (; ; ) {
71
- response = await fetchFn(url, options);
69
+ let response;
70
+ try {
71
+ response = await fetchFn(url, options);
72
+ } catch (e) {
73
+ if (abortSignal?.aborted) throw e;
74
+ if (attempt++ >= maxRetries) throw e;
75
+ await sleep(backoffDelay(attempt), abortSignal);
76
+ if (abortSignal?.aborted) throw e;
77
+ continue;
78
+ }
72
79
  if (!retryStatusCodes.includes(response.status)) {
73
- break;
80
+ return response;
74
81
  }
75
82
  if (attempt++ >= maxRetries) {
76
- break;
83
+ return response;
77
84
  }
78
- const retryAfter = response.headers.get("Retry-After");
79
- const delay = retryAfter ? parseInt(retryAfter, 10) * 1e3 : Math.min(100 * Math.pow(2, attempt - 1), 1e4);
85
+ const delay = parseRetryAfterMs(
86
+ response.headers.get("Retry-After"),
87
+ backoffDelay(attempt)
88
+ );
89
+ await response.body?.cancel().catch(() => {
90
+ });
80
91
  await sleep(delay, abortSignal);
92
+ if (abortSignal?.aborted) return response;
81
93
  }
82
- return response;
83
94
  };
84
95
  }
85
96
  }
97
+ function parseRetryAfterMs(headerValue, fallbackMs) {
98
+ if (!headerValue) {
99
+ return fallbackMs;
100
+ }
101
+ if (/^\d+$/.test(headerValue)) {
102
+ return Number(headerValue) * 1e3;
103
+ }
104
+ if (headerValue.includes(",")) {
105
+ const dateMs = Date.parse(headerValue);
106
+ if (Number.isFinite(dateMs)) {
107
+ const deltaMs = dateMs - Date.now();
108
+ return deltaMs > 0 ? deltaMs : 0;
109
+ }
110
+ }
111
+ return fallbackMs;
112
+ }
86
113
  async function sleep(durationMs, abortSignal) {
87
114
  if (abortSignal?.aborted) {
88
115
  return;
@@ -105,6 +132,7 @@ function replaceGitLabUrlType(url, type) {
105
132
  }
106
133
 
107
134
  exports.GitLabIntegration = GitLabIntegration;
135
+ exports.parseRetryAfterMs = parseRetryAfterMs;
108
136
  exports.replaceGitLabUrlType = replaceGitLabUrlType;
109
137
  exports.sleep = sleep;
110
138
  //# sourceMappingURL=GitLabIntegration.cjs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"GitLabIntegration.cjs.js","sources":["../../src/gitlab/GitLabIntegration.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { basicIntegrations, defaultScmResolveUrl } from '../helpers';\nimport { ScmIntegration, ScmIntegrationsFactory } from '../types';\nimport {\n GitLabIntegrationConfig,\n readGitLabIntegrationConfigs,\n} from './config';\nimport pThrottle from 'p-throttle';\n\ntype FetchFunction = typeof fetch;\n\n/**\n * A GitLab based integration.\n *\n * @public\n */\nexport class GitLabIntegration implements ScmIntegration {\n static factory: ScmIntegrationsFactory<GitLabIntegration> = ({ config }) => {\n const configs = readGitLabIntegrationConfigs(\n config.getOptionalConfigArray('integrations.gitlab') ?? [],\n );\n return basicIntegrations(\n configs.map(c => new GitLabIntegration(c)),\n i => i.config.host,\n );\n };\n\n private readonly fetchImpl: FetchFunction;\n\n constructor(private readonly integrationConfig: GitLabIntegrationConfig) {\n // Configure fetch strategy based on configuration\n this.fetchImpl = this.createFetchStrategy();\n }\n\n get type(): string {\n return 'gitlab';\n }\n\n get title(): string {\n return this.integrationConfig.host;\n }\n\n get config(): GitLabIntegrationConfig {\n return this.integrationConfig;\n }\n\n resolveUrl(options: {\n url: string;\n base: string;\n lineNumber?: number;\n }): string {\n return defaultScmResolveUrl(options);\n }\n\n resolveEditUrl(url: string): string {\n return replaceGitLabUrlType(url, 'edit');\n }\n\n fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {\n return this.fetchImpl(input, init);\n }\n\n private createFetchStrategy(): FetchFunction {\n let fetchFn: FetchFunction = async (url, options) => {\n return fetch(url, { ...options, mode: 'same-origin' });\n };\n\n const retryConfig = this.integrationConfig.retry;\n if (retryConfig) {\n // Apply retry wrapper if configured\n fetchFn = this.withRetry(fetchFn, retryConfig);\n\n // Apply throttling wrapper if configured\n if (\n retryConfig.maxApiRequestsPerMinute &&\n retryConfig.maxApiRequestsPerMinute > 0\n ) {\n fetchFn = pThrottle({\n limit: retryConfig.maxApiRequestsPerMinute,\n interval: 60_000,\n })(fetchFn);\n }\n }\n\n return fetchFn;\n }\n\n private withRetry(\n fetchFn: FetchFunction,\n retryConfig: { maxRetries?: number; retryStatusCodes?: number[] },\n ): FetchFunction {\n const maxRetries = retryConfig?.maxRetries ?? 0;\n const retryStatusCodes = retryConfig?.retryStatusCodes ?? [];\n if (maxRetries <= 0 || retryStatusCodes.length === 0) {\n return fetchFn;\n }\n\n return async (url, options) => {\n const abortSignal = options?.signal;\n let response: Response;\n let attempt = 0;\n for (;;) {\n response = await fetchFn(url, options);\n // If response is not retryable, return immediately\n if (!retryStatusCodes.includes(response.status)) {\n break;\n }\n\n // If this was the last allowed attempt, return response\n if (attempt++ >= maxRetries) {\n break;\n }\n // Determine delay from Retry-After header if present, otherwise exponential backoff\n const retryAfter = response.headers.get('Retry-After');\n const delay = retryAfter\n ? parseInt(retryAfter, 10) * 1000\n : Math.min(100 * Math.pow(2, attempt - 1), 10000); // Exponential backoff, cap at 10 seconds\n\n await sleep(delay, abortSignal);\n }\n\n return response;\n };\n }\n}\n\n/** @internal */\nexport async function sleep(\n durationMs: number,\n abortSignal: AbortSignal | null | undefined,\n): Promise<void> {\n if (abortSignal?.aborted) {\n return;\n }\n\n await new Promise<void>(resolve => {\n let timeoutHandle: NodeJS.Timeout | undefined = undefined;\n\n const done = () => {\n if (timeoutHandle) {\n clearTimeout(timeoutHandle);\n }\n abortSignal?.removeEventListener('abort', done);\n resolve();\n };\n\n timeoutHandle = setTimeout(done, durationMs);\n abortSignal?.addEventListener('abort', done);\n });\n}\n\n/**\n * Takes a GitLab URL and replaces the type part (blob, tree etc).\n *\n * @param url - The original URL\n * @param type - The desired type, e.g. 'blob', 'tree', 'edit'\n * @public\n */\nexport function replaceGitLabUrlType(\n url: string,\n type: 'blob' | 'tree' | 'edit',\n): string {\n return url.replace(/\\/\\-\\/(blob|tree|edit)\\//, `/-/${type}/`);\n}\n"],"names":["config","readGitLabIntegrationConfigs","basicIntegrations","defaultScmResolveUrl","pThrottle"],"mappings":";;;;;;;;;;AA8BO,MAAM,iBAAA,CAA4C;AAAA,EAavD,YAA6B,iBAAA,EAA4C;AAA5C,IAAA,IAAA,CAAA,iBAAA,GAAA,iBAAA;AAE3B,IAAA,IAAA,CAAK,SAAA,GAAY,KAAK,mBAAA,EAAoB;AAAA,EAC5C;AAAA,EAfA,OAAO,OAAA,GAAqD,CAAC,UAAEA,UAAO,KAAM;AAC1E,IAAA,MAAM,OAAA,GAAUC,mCAAA;AAAA,MACdD,QAAA,CAAO,sBAAA,CAAuB,qBAAqB,CAAA,IAAK;AAAC,KAC3D;AACA,IAAA,OAAOE,yBAAA;AAAA,MACL,QAAQ,GAAA,CAAI,CAAA,CAAA,KAAK,IAAI,iBAAA,CAAkB,CAAC,CAAC,CAAA;AAAA,MACzC,CAAA,CAAA,KAAK,EAAE,MAAA,CAAO;AAAA,KAChB;AAAA,EACF,CAAA;AAAA,EAEiB,SAAA;AAAA,EAOjB,IAAI,IAAA,GAAe;AACjB,IAAA,OAAO,QAAA;AAAA,EACT;AAAA,EAEA,IAAI,KAAA,GAAgB;AAClB,IAAA,OAAO,KAAK,iBAAA,CAAkB,IAAA;AAAA,EAChC;AAAA,EAEA,IAAI,MAAA,GAAkC;AACpC,IAAA,OAAO,IAAA,CAAK,iBAAA;AAAA,EACd;AAAA,EAEA,WAAW,OAAA,EAIA;AACT,IAAA,OAAOC,6BAAqB,OAAO,CAAA;AAAA,EACrC;AAAA,EAEA,eAAe,GAAA,EAAqB;AAClC,IAAA,OAAO,oBAAA,CAAqB,KAAK,MAAM,CAAA;AAAA,EACzC;AAAA,EAEA,KAAA,CAAM,OAA0B,IAAA,EAAuC;AACrE,IAAA,OAAO,IAAA,CAAK,SAAA,CAAU,KAAA,EAAO,IAAI,CAAA;AAAA,EACnC;AAAA,E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authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { basicIntegrations, defaultScmResolveUrl } from '../helpers';\nimport { ScmIntegration, ScmIntegrationsFactory } from '../types';\nimport {\n GitLabIntegrationConfig,\n readGitLabIntegrationConfigs,\n} from './config';\nimport pThrottle from 'p-throttle';\n\ntype FetchFunction = typeof fetch;\n\n/**\n * A GitLab based integration.\n *\n * @public\n */\nexport class GitLabIntegration implements ScmIntegration {\n static factory: ScmIntegrationsFactory<GitLabIntegration> = ({ config }) => {\n const configs = readGitLabIntegrationConfigs(\n config.getOptionalConfigArray('integrations.gitlab') ?? [],\n );\n return basicIntegrations(\n configs.map(c => new GitLabIntegration(c)),\n i => i.config.host,\n );\n };\n\n private readonly fetchImpl: FetchFunction;\n\n constructor(private readonly integrationConfig: GitLabIntegrationConfig) {\n // Configure fetch strategy based on configuration\n this.fetchImpl = this.createFetchStrategy();\n }\n\n get type(): string {\n return 'gitlab';\n }\n\n get title(): string {\n return this.integrationConfig.host;\n }\n\n get config(): GitLabIntegrationConfig {\n return this.integrationConfig;\n }\n\n resolveUrl(options: {\n url: string;\n base: string;\n lineNumber?: number;\n }): string {\n return defaultScmResolveUrl(options);\n }\n\n resolveEditUrl(url: string): string {\n return replaceGitLabUrlType(url, 'edit');\n }\n\n fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {\n return this.fetchImpl(input, init);\n }\n\n private createFetchStrategy(): FetchFunction {\n let fetchFn: FetchFunction = (url, options) => fetch(url, options);\n\n const retryConfig = this.integrationConfig.retry;\n if (retryConfig) {\n // Apply retry wrapper if configured\n fetchFn = this.withRetry(fetchFn, retryConfig);\n\n // Apply throttling wrapper if configured\n if (\n retryConfig.maxApiRequestsPerMinute &&\n retryConfig.maxApiRequestsPerMinute > 0\n ) {\n fetchFn = pThrottle({\n limit: retryConfig.maxApiRequestsPerMinute,\n interval: 60_000,\n })(fetchFn);\n }\n }\n\n return fetchFn;\n }\n\n private withRetry(\n fetchFn: FetchFunction,\n retryConfig: { maxRetries?: number; retryStatusCodes?: number[] },\n ): FetchFunction {\n const maxRetries = retryConfig?.maxRetries ?? 0;\n const retryStatusCodes = retryConfig?.retryStatusCodes ?? [];\n if (maxRetries <= 0 || retryStatusCodes.length === 0) {\n return fetchFn;\n }\n\n // Exponential backoff, cap at 10 seconds\n const backoffDelay = (a: number) =>\n Math.min(100 * Math.pow(2, a - 1), 10000);\n\n return async (url, options) => {\n const abortSignal = options?.signal;\n let attempt = 0;\n for (;;) {\n let response: Response;\n try {\n response = await fetchFn(url, options);\n } catch (e) {\n // The caller aborted — surface that immediately rather than retrying.\n if (abortSignal?.aborted) throw e;\n // No more attempts left — propagate the network error.\n if (attempt++ >= maxRetries) throw e;\n await sleep(backoffDelay(attempt), abortSignal);\n if (abortSignal?.aborted) throw e;\n continue;\n }\n\n // Successful, non-retryable response: return immediately\n if (!retryStatusCodes.includes(response.status)) {\n return response;\n }\n\n // No more attempts left — return the last (retryable) response.\n if (attempt++ >= maxRetries) {\n return response;\n }\n\n // Retry-After is either delay-seconds or an HTTP-date (RFC 9110 §10.2.3).\n const delay = parseRetryAfterMs(\n response.headers.get('Retry-After'),\n backoffDelay(attempt),\n );\n\n // Release the underlying connection so it can be reused, since we're\n // about to discard this response in favor of a retry.\n await response.body?.cancel().catch(() => {});\n\n await sleep(delay, abortSignal);\n if (abortSignal?.aborted) return response;\n }\n };\n }\n}\n\n/** @internal */\nexport function parseRetryAfterMs(\n headerValue: string | null,\n fallbackMs: number,\n): number {\n if (!headerValue) {\n return fallbackMs;\n }\n\n // delay-seconds per RFC 9110 is 1*DIGIT\n if (/^\\d+$/.test(headerValue)) {\n return Number(headerValue) * 1000;\n }\n\n // HTTP-dates (IMF-fixdate) always contain a comma, e.g.\n // \"Sun, 06 Nov 1994 08:49:37 GMT\" — use that as a prerequisite\n // to avoid Date.parse interpreting random strings as dates.\n if (headerValue.includes(',')) {\n const dateMs = Date.parse(headerValue);\n if (Number.isFinite(dateMs)) {\n const deltaMs = dateMs - Date.now();\n return deltaMs > 0 ? deltaMs : 0;\n }\n }\n\n return fallbackMs;\n}\n\n/** @internal */\nexport async function sleep(\n durationMs: number,\n abortSignal: AbortSignal | null | undefined,\n): Promise<void> {\n if (abortSignal?.aborted) {\n return;\n }\n\n await new Promise<void>(resolve => {\n let timeoutHandle: NodeJS.Timeout | undefined = undefined;\n\n const done = () => {\n if (timeoutHandle) {\n clearTimeout(timeoutHandle);\n }\n abortSignal?.removeEventListener('abort', done);\n resolve();\n };\n\n timeoutHandle = setTimeout(done, durationMs);\n abortSignal?.addEventListener('abort', done);\n });\n}\n\n/**\n * Takes a GitLab URL and replaces the type part (blob, tree etc).\n *\n * @param url - The original URL\n * @param type - The desired type, e.g. 'blob', 'tree', 'edit'\n * @public\n */\nexport function replaceGitLabUrlType(\n url: string,\n type: 'blob' | 'tree' | 'edit',\n): string {\n return url.replace(/\\/\\-\\/(blob|tree|edit)\\//, `/-/${type}/`);\n}\n"],"names":["config","readGitLabIntegrationConfigs","basicIntegrations","defaultScmResolveUrl","pThrottle"],"mappings":";;;;;;;;;;AA8BO,MAAM,iBAAA,CAA4C;AAAA,EAavD,YAA6B,iBAAA,EAA4C;AAA5C,IAAA,IAAA,CAAA,iBAAA,GAAA,iBAAA;AAE3B,IAAA,IAAA,CAAK,SAAA,GAAY,KAAK,mBAAA,EAAoB;AAAA,EAC5C;AAAA,EAfA,OAAO,OAAA,GAAqD,CAAC,UAAEA,UAAO,KAAM;AAC1E,IAAA,MAAM,OAAA,GAAUC,mCAAA;AAAA,MACdD,QAAA,CAAO,sBAAA,CAAuB,qBAAqB,CAAA,IAAK;AAAC,KAC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AAAA,MAC5B;AACA,MAAA,WAAA,EAAa,mBAAA,CAAoB,SAAS,IAAI,CAAA;AAC9C,MAAA,OAAA,EAAQ;AAAA,IACV,CAAA;AAEA,IAAA,aAAA,GAAgB,UAAA,CAAW,MAAM,UAAU,CAAA;AAC3C,IAAA,WAAA,EAAa,gBAAA,CAAiB,SAAS,IAAI,CAAA;AAAA,EAC7C,CAAC,CAAA;AACH;AASO,SAAS,oBAAA,CACd,KACA,IAAA,EACQ;AACR,EAAA,OAAO,GAAA,CAAI,OAAA,CAAQ,0BAAA,EAA4B,CAAA,GAAA,EAAM,IAAI,CAAA,CAAA,CAAG,CAAA;AAC9D;;;;;;;"}
@@ -36,9 +36,7 @@ class GitLabIntegration {
36
36
  return this.fetchImpl(input, init);
37
37
  }
38
38
  createFetchStrategy() {
39
- let fetchFn = async (url, options) => {
40
- return fetch(url, { ...options, mode: "same-origin" });
41
- };
39
+ let fetchFn = (url, options) => fetch(url, options);
42
40
  const retryConfig = this.integrationConfig.retry;
43
41
  if (retryConfig) {
44
42
  fetchFn = this.withRetry(fetchFn, retryConfig);
@@ -57,26 +55,55 @@ class GitLabIntegration {
57
55
  if (maxRetries <= 0 || retryStatusCodes.length === 0) {
58
56
  return fetchFn;
59
57
  }
58
+ const backoffDelay = (a) => Math.min(100 * Math.pow(2, a - 1), 1e4);
60
59
  return async (url, options) => {
61
60
  const abortSignal = options?.signal;
62
- let response;
63
61
  let attempt = 0;
64
62
  for (; ; ) {
65
- response = await fetchFn(url, options);
63
+ let response;
64
+ try {
65
+ response = await fetchFn(url, options);
66
+ } catch (e) {
67
+ if (abortSignal?.aborted) throw e;
68
+ if (attempt++ >= maxRetries) throw e;
69
+ await sleep(backoffDelay(attempt), abortSignal);
70
+ if (abortSignal?.aborted) throw e;
71
+ continue;
72
+ }
66
73
  if (!retryStatusCodes.includes(response.status)) {
67
- break;
74
+ return response;
68
75
  }
69
76
  if (attempt++ >= maxRetries) {
70
- break;
77
+ return response;
71
78
  }
72
- const retryAfter = response.headers.get("Retry-After");
73
- const delay = retryAfter ? parseInt(retryAfter, 10) * 1e3 : Math.min(100 * Math.pow(2, attempt - 1), 1e4);
79
+ const delay = parseRetryAfterMs(
80
+ response.headers.get("Retry-After"),
81
+ backoffDelay(attempt)
82
+ );
83
+ await response.body?.cancel().catch(() => {
84
+ });
74
85
  await sleep(delay, abortSignal);
86
+ if (abortSignal?.aborted) return response;
75
87
  }
76
- return response;
77
88
  };
78
89
  }
79
90
  }
91
+ function parseRetryAfterMs(headerValue, fallbackMs) {
92
+ if (!headerValue) {
93
+ return fallbackMs;
94
+ }
95
+ if (/^\d+$/.test(headerValue)) {
96
+ return Number(headerValue) * 1e3;
97
+ }
98
+ if (headerValue.includes(",")) {
99
+ const dateMs = Date.parse(headerValue);
100
+ if (Number.isFinite(dateMs)) {
101
+ const deltaMs = dateMs - Date.now();
102
+ return deltaMs > 0 ? deltaMs : 0;
103
+ }
104
+ }
105
+ return fallbackMs;
106
+ }
80
107
  async function sleep(durationMs, abortSignal) {
81
108
  if (abortSignal?.aborted) {
82
109
  return;
@@ -98,5 +125,5 @@ function replaceGitLabUrlType(url, type) {
98
125
  return url.replace(/\/\-\/(blob|tree|edit)\//, `/-/${type}/`);
99
126
  }
100
127
 
101
- export { GitLabIntegration, replaceGitLabUrlType, sleep };
128
+ export { GitLabIntegration, parseRetryAfterMs, replaceGitLabUrlType, sleep };
102
129
  //# sourceMappingURL=GitLabIntegration.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"GitLabIntegration.esm.js","sources":["../../src/gitlab/GitLabIntegration.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { basicIntegrations, defaultScmResolveUrl } from '../helpers';\nimport { ScmIntegration, ScmIntegrationsFactory } from '../types';\nimport {\n GitLabIntegrationConfig,\n readGitLabIntegrationConfigs,\n} from './config';\nimport pThrottle from 'p-throttle';\n\ntype FetchFunction = typeof fetch;\n\n/**\n * A GitLab based integration.\n *\n * @public\n */\nexport class GitLabIntegration implements ScmIntegration {\n static factory: ScmIntegrationsFactory<GitLabIntegration> = ({ config }) => {\n const configs = readGitLabIntegrationConfigs(\n config.getOptionalConfigArray('integrations.gitlab') ?? [],\n );\n return basicIntegrations(\n configs.map(c => new GitLabIntegration(c)),\n i => i.config.host,\n );\n };\n\n private readonly fetchImpl: FetchFunction;\n\n constructor(private readonly integrationConfig: GitLabIntegrationConfig) {\n // Configure fetch strategy based on configuration\n this.fetchImpl = this.createFetchStrategy();\n }\n\n get type(): string {\n return 'gitlab';\n }\n\n get title(): string {\n return this.integrationConfig.host;\n }\n\n get config(): GitLabIntegrationConfig {\n return this.integrationConfig;\n }\n\n resolveUrl(options: {\n url: string;\n base: string;\n lineNumber?: number;\n }): string {\n return defaultScmResolveUrl(options);\n }\n\n resolveEditUrl(url: string): string {\n return replaceGitLabUrlType(url, 'edit');\n }\n\n fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {\n return this.fetchImpl(input, init);\n }\n\n private createFetchStrategy(): FetchFunction {\n let fetchFn: FetchFunction = async (url, options) => {\n return fetch(url, { ...options, mode: 'same-origin' });\n };\n\n const retryConfig = this.integrationConfig.retry;\n if (retryConfig) {\n // Apply retry wrapper if configured\n fetchFn = this.withRetry(fetchFn, retryConfig);\n\n // Apply throttling wrapper if configured\n if (\n retryConfig.maxApiRequestsPerMinute &&\n retryConfig.maxApiRequestsPerMinute > 0\n ) {\n fetchFn = pThrottle({\n limit: retryConfig.maxApiRequestsPerMinute,\n interval: 60_000,\n })(fetchFn);\n }\n }\n\n return fetchFn;\n }\n\n private withRetry(\n fetchFn: FetchFunction,\n retryConfig: { maxRetries?: number; retryStatusCodes?: number[] },\n ): FetchFunction {\n const maxRetries = retryConfig?.maxRetries ?? 0;\n const retryStatusCodes = retryConfig?.retryStatusCodes ?? [];\n if (maxRetries <= 0 || retryStatusCodes.length === 0) {\n return fetchFn;\n }\n\n return async (url, options) => {\n const abortSignal = options?.signal;\n let response: Response;\n let attempt = 0;\n for (;;) {\n response = await fetchFn(url, options);\n // If response is not retryable, return immediately\n if (!retryStatusCodes.includes(response.status)) {\n break;\n }\n\n // If this was the last allowed attempt, return response\n if (attempt++ >= maxRetries) {\n break;\n }\n // Determine delay from Retry-After header if present, otherwise exponential backoff\n const retryAfter = response.headers.get('Retry-After');\n const delay = retryAfter\n ? parseInt(retryAfter, 10) * 1000\n : Math.min(100 * Math.pow(2, attempt - 1), 10000); // Exponential backoff, cap at 10 seconds\n\n await sleep(delay, abortSignal);\n }\n\n return response;\n };\n }\n}\n\n/** @internal */\nexport async function sleep(\n durationMs: number,\n abortSignal: AbortSignal | null | undefined,\n): Promise<void> {\n if (abortSignal?.aborted) {\n return;\n }\n\n await new Promise<void>(resolve => {\n let timeoutHandle: NodeJS.Timeout | undefined = undefined;\n\n const done = () => {\n if (timeoutHandle) {\n clearTimeout(timeoutHandle);\n }\n abortSignal?.removeEventListener('abort', done);\n resolve();\n };\n\n timeoutHandle = setTimeout(done, durationMs);\n abortSignal?.addEventListener('abort', done);\n });\n}\n\n/**\n * Takes a GitLab URL and replaces the type part (blob, tree etc).\n *\n * @param url - The original URL\n * @param type - The desired type, e.g. 'blob', 'tree', 'edit'\n * @public\n */\nexport function replaceGitLabUrlType(\n url: string,\n type: 'blob' | 'tree' | 'edit',\n): string {\n return url.replace(/\\/\\-\\/(blob|tree|edit)\\//, `/-/${type}/`);\n}\n"],"names":[],"mappings":";;;;AA8BO,MAAM,iBAAA,CAA4C;AAAA,EAavD,YAA6B,iBAAA,EAA4C;AAA5C,IAAA,IAAA,CAAA,iBAAA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AAA,GAAM,KAAK,GAAA,CAAI,CAAA,EAAG,OAAA,GAAU,CAAC,GAAG,GAAK,CAAA;AAElD,QAAA,MAAM,KAAA,CAAM,OAAO,WAAW,CAAA;AAAA,MAChC;AAEA,MAAA,OAAO,QAAA;AAAA,IACT,CAAA;AAAA,EACF;AACF;AAGA,eAAsB,KAAA,CACpB,YACA,WAAA,EACe;AACf,EAAA,IAAI,aAAa,OAAA,EAAS;AACxB,IAAA;AAAA,EACF;AAEA,EAAA,MAAM,IAAI,QAAc,CAAA,OAAA,KAAW;AACjC,IAAA,IAAI,aAAA,GAA4C,MAAA;AAEhD,IAAA,MAAM,OAAO,MAAM;AACjB,MAAA,IAAI,aAAA,EAAe;AACjB,QAAA,YAAA,CAAa,aAAa,CAAA;AAAA,MAC5B;AACA,MAAA,WAAA,EAAa,mBAAA,CAAoB,SAAS,IAAI,CAAA;AAC9C,MAAA,OAAA,EAAQ;AAAA,IACV,CAAA;AAEA,IAAA,aAAA,GAAgB,UAAA,CAAW,MAAM,UAAU,CAAA;AAC3C,IAAA,WAAA,EAAa,gBAAA,CAAiB,SAAS,IAAI,CAAA;AAAA,EAC7C,CAAC,CAAA;AACH;AASO,SAAS,oBAAA,CACd,KACA,IAAA,EACQ;AACR,EAAA,OAAO,GAAA,CAAI,OAAA,CAAQ,0BAAA,EAA4B,CAAA,GAAA,EAAM,IAAI,CAAA,CAAA,CAAG,CAAA;AAC9D;;;;"}
1
+ {"version":3,"file":"GitLabIntegration.esm.js","sources":["../../src/gitlab/GitLabIntegration.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { basicIntegrations, defaultScmResolveUrl } from '../helpers';\nimport { ScmIntegration, ScmIntegrationsFactory } from '../types';\nimport {\n GitLabIntegrationConfig,\n readGitLabIntegrationConfigs,\n} from './config';\nimport pThrottle from 'p-throttle';\n\ntype FetchFunction = typeof fetch;\n\n/**\n * A GitLab based integration.\n *\n * @public\n */\nexport class GitLabIntegration implements ScmIntegration {\n static factory: ScmIntegrationsFactory<GitLabIntegration> = ({ config }) => {\n const configs = readGitLabIntegrationConfigs(\n config.getOptionalConfigArray('integrations.gitlab') ?? [],\n );\n return basicIntegrations(\n configs.map(c => new GitLabIntegration(c)),\n i => i.config.host,\n );\n };\n\n private readonly fetchImpl: FetchFunction;\n\n constructor(private readonly integrationConfig: GitLabIntegrationConfig) {\n // Configure fetch strategy based on configuration\n this.fetchImpl = this.createFetchStrategy();\n }\n\n get type(): string {\n return 'gitlab';\n }\n\n get title(): string {\n return this.integrationConfig.host;\n }\n\n get config(): GitLabIntegrationConfig {\n return this.integrationConfig;\n }\n\n resolveUrl(options: {\n url: string;\n base: string;\n lineNumber?: number;\n }): string {\n return defaultScmResolveUrl(options);\n }\n\n resolveEditUrl(url: string): string {\n return replaceGitLabUrlType(url, 'edit');\n }\n\n fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {\n return this.fetchImpl(input, init);\n }\n\n private createFetchStrategy(): FetchFunction {\n let fetchFn: FetchFunction = (url, options) => fetch(url, options);\n\n const retryConfig = this.integrationConfig.retry;\n if (retryConfig) {\n // Apply retry wrapper if configured\n fetchFn = this.withRetry(fetchFn, retryConfig);\n\n // Apply throttling wrapper if configured\n if (\n retryConfig.maxApiRequestsPerMinute &&\n retryConfig.maxApiRequestsPerMinute > 0\n ) {\n fetchFn = pThrottle({\n limit: retryConfig.maxApiRequestsPerMinute,\n interval: 60_000,\n })(fetchFn);\n }\n }\n\n return fetchFn;\n }\n\n private withRetry(\n fetchFn: FetchFunction,\n retryConfig: { maxRetries?: number; retryStatusCodes?: number[] },\n ): FetchFunction {\n const maxRetries = retryConfig?.maxRetries ?? 0;\n const retryStatusCodes = retryConfig?.retryStatusCodes ?? [];\n if (maxRetries <= 0 || retryStatusCodes.length === 0) {\n return fetchFn;\n }\n\n // Exponential backoff, cap at 10 seconds\n const backoffDelay = (a: number) =>\n Math.min(100 * Math.pow(2, a - 1), 10000);\n\n return async (url, options) => {\n const abortSignal = options?.signal;\n let attempt = 0;\n for (;;) {\n let response: Response;\n try {\n response = await fetchFn(url, options);\n } catch (e) {\n // The caller aborted — surface that immediately rather than retrying.\n if (abortSignal?.aborted) throw e;\n // No more attempts left — propagate the network error.\n if (attempt++ >= maxRetries) throw e;\n await sleep(backoffDelay(attempt), abortSignal);\n if (abortSignal?.aborted) throw e;\n continue;\n }\n\n // Successful, non-retryable response: return immediately\n if (!retryStatusCodes.includes(response.status)) {\n return response;\n }\n\n // No more attempts left — return the last (retryable) response.\n if (attempt++ >= maxRetries) {\n return response;\n }\n\n // Retry-After is either delay-seconds or an HTTP-date (RFC 9110 §10.2.3).\n const delay = parseRetryAfterMs(\n response.headers.get('Retry-After'),\n backoffDelay(attempt),\n );\n\n // Release the underlying connection so it can be reused, since we're\n // about to discard this response in favor of a retry.\n await response.body?.cancel().catch(() => {});\n\n await sleep(delay, abortSignal);\n if (abortSignal?.aborted) return response;\n }\n };\n }\n}\n\n/** @internal */\nexport function parseRetryAfterMs(\n headerValue: string | null,\n fallbackMs: number,\n): number {\n if (!headerValue) {\n return fallbackMs;\n }\n\n // delay-seconds per RFC 9110 is 1*DIGIT\n if (/^\\d+$/.test(headerValue)) {\n return Number(headerValue) * 1000;\n }\n\n // HTTP-dates (IMF-fixdate) always contain a comma, e.g.\n // \"Sun, 06 Nov 1994 08:49:37 GMT\" — use that as a prerequisite\n // to avoid Date.parse interpreting random strings as dates.\n if (headerValue.includes(',')) {\n const dateMs = Date.parse(headerValue);\n if (Number.isFinite(dateMs)) {\n const deltaMs = dateMs - Date.now();\n return deltaMs > 0 ? deltaMs : 0;\n }\n }\n\n return fallbackMs;\n}\n\n/** @internal */\nexport async function sleep(\n durationMs: number,\n abortSignal: AbortSignal | null | undefined,\n): Promise<void> {\n if (abortSignal?.aborted) {\n return;\n }\n\n await new Promise<void>(resolve => {\n let timeoutHandle: NodeJS.Timeout | undefined = undefined;\n\n const done = () => {\n if (timeoutHandle) {\n clearTimeout(timeoutHandle);\n }\n abortSignal?.removeEventListener('abort', done);\n resolve();\n };\n\n timeoutHandle = setTimeout(done, durationMs);\n abortSignal?.addEventListener('abort', done);\n });\n}\n\n/**\n * Takes a GitLab URL and replaces the type part (blob, tree etc).\n *\n * @param url - The original URL\n * @param type - The desired type, e.g. 'blob', 'tree', 'edit'\n * @public\n */\nexport function replaceGitLabUrlType(\n url: string,\n type: 'blob' | 'tree' | 'edit',\n): string {\n return url.replace(/\\/\\-\\/(blob|tree|edit)\\//, `/-/${type}/`);\n}\n"],"names":[],"mappings":";;;;AA8BO,MAAM,iBAAA,CAA4C;AAAA,EAavD,YAA6B,iBAAA,EAA4C;AAA5C,IAAA,IAAA,CAAA,iBAAA,GAAA,iBAAA;AAE3B,IAAA,IAAA,CAAK,SAAA,GAAY,KAAK,mBAAA,EAAoB;AAAA,EAC5C;AAAA,EAfA,OAAO,OAAA,GAAqD,CAAC,EAAE,QAAO,KAAM;AAC1E,IAAA,MAAM,OAAA,GAAU,4BAAA;AAAA,MACd,MAAA,CAAO,sBAAA,CAAuB,qBAAqB,CAAA,IAAK;AAAC,KAC3D;AACA,IAAA,OAAO,iBAAA;AAAA,MACL,QAAQ,GAAA,CAAI,CAAA,CAAA,KAAK,IAAI,iBAAA,CAAkB,CAAC,CAAC,CAAA;AAAA,MACzC,CAAA,CAAA,KAAK,EAAE,MAAA,CAAO;AAAA,KAChB;AAAA,EACF,CAAA;AAAA,EAEiB,SAAA;AAAA,EAOjB,IAAI,IAAA,GAAe;AACjB,IAAA,OAAO,QAAA;AAAA,EACT;AAAA,EAEA,IAAI,KAAA,GAAgB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GAAA,CAAI,OAAA,CAAQ,0BAAA,EAA4B,CAAA,GAAA,EAAM,IAAI,CAAA,CAAA,CAAG,CAAA;AAC9D;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/integration",
3
- "version": "2.0.2-next.0",
3
+ "version": "2.0.2-next.1",
4
4
  "description": "Helpers for managing integrations towards external systems",
5
5
  "backstage": {
6
6
  "role": "common-library"
@@ -50,8 +50,8 @@
50
50
  "p-throttle": "^4.1.1"
51
51
  },
52
52
  "devDependencies": {
53
- "@backstage/backend-test-utils": "1.11.3-next.0",
54
- "@backstage/cli": "0.36.2-next.0",
53
+ "@backstage/backend-test-utils": "1.11.3-next.1",
54
+ "@backstage/cli": "0.36.2-next.1",
55
55
  "@backstage/config-loader": "1.10.11-next.0",
56
56
  "msw": "^1.0.0"
57
57
  },