-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ben Loe
committed
Apr 15, 2024
1 parent
77778fd
commit e193a7f
Showing
11 changed files
with
308 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/integrations/util/makeIntegrationsContextFromDependencies.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/* eslint-disable security/detect-object-injection -- multiple places use the same constructed key value, not user input */ | ||
|
||
import { type IntegrationDependency } from "@/integrations/integrationTypes"; | ||
import { | ||
type IntegrationDependencyVarRef, | ||
type IntegrationsContext, | ||
type IntegrationsContextValue, | ||
} from "@/types/runtimeTypes"; | ||
import locateSanitizedIntegrationConfigWithRetry from "@/integrations/util/locateSanitizedIntegrationConfigWithRetry"; | ||
import { pickBy } from "lodash"; | ||
import { type UUID } from "@/types/stringTypes"; | ||
import { type RegistryId } from "@/types/registryTypes"; | ||
import { type Nullishable } from "@/utils/nullishUtils"; | ||
import { PIXIEBRIX_INTEGRATION_ID } from "@/integrations/constants"; | ||
import { NotConfiguredError } from "@/errors/businessErrors"; | ||
import { pixiebrixConfigurationFactory } from "@/integrations/util/pixiebrixConfigurationFactory"; | ||
|
||
async function dependencyContextValue({ | ||
integrationId, | ||
configId, | ||
}: { | ||
integrationId: RegistryId; | ||
configId: UUID; | ||
}): Promise<IntegrationsContextValue> { | ||
// Should be safe to call locateWithRetry in parallel b/c the locator.refresh() method debounces/coalesces | ||
// the promise | ||
const integrationConfig = await locateSanitizedIntegrationConfigWithRetry( | ||
integrationId, | ||
configId, | ||
); | ||
return { | ||
...pickBy( | ||
integrationConfig.config, | ||
// Our JSON validator gets mad at undefined values, and we don't want to include the type brand | ||
(value, key) => value !== undefined && key !== "_sanitizedConfigBrand", | ||
), | ||
__service: integrationConfig, | ||
}; | ||
} | ||
|
||
/** Build the integrations context by locating the dependencies */ | ||
export default async function makeIntegrationsContextFromDependencies( | ||
// `ModComponentBase.integrationDependencies` is an optional field. Since we don't have strict-nullness checking on, calls to this method | ||
// are error-prone. So just be defensive in the signature | ||
// https://github.com/pixiebrix/pixiebrix-extension/issues/3262 | ||
dependencies: Nullishable<IntegrationDependency[]>, | ||
): Promise<IntegrationsContext> { | ||
const context: IntegrationsContext = {}; | ||
|
||
if (dependencies == null || dependencies.length === 0) { | ||
return context; | ||
} | ||
|
||
for (const dependency of dependencies) { | ||
const varRef = `@${dependency.outputKey}` as IntegrationDependencyVarRef; | ||
|
||
if (dependency.integrationId === PIXIEBRIX_INTEGRATION_ID) { | ||
context[varRef] = { | ||
__service: pixiebrixConfigurationFactory(), | ||
}; | ||
continue; | ||
} | ||
|
||
if (!dependency.configId) { | ||
if (dependency.isOptional) { | ||
context[varRef] = null; | ||
} | ||
|
||
throw new NotConfiguredError( | ||
`No configuration selected for ${dependency.integrationId}`, | ||
dependency.integrationId, | ||
); | ||
} | ||
|
||
// eslint-disable-next-line no-await-in-loop -- We need to serialize these calls, because they will attempt to refresh the locator services cache | ||
context[varRef] = await dependencyContextValue({ | ||
integrationId: dependency.integrationId, | ||
configId: dependency.configId, | ||
}); | ||
} | ||
|
||
return context; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import type { | ||
SanitizedConfig, | ||
SanitizedIntegrationConfig, | ||
} from "@/integrations/integrationTypes"; | ||
import { | ||
PIXIEBRIX_INTEGRATION_CONFIG_ID, | ||
PIXIEBRIX_INTEGRATION_ID, | ||
} from "@/integrations/constants"; | ||
|
||
export function pixiebrixConfigurationFactory(): SanitizedIntegrationConfig { | ||
return { | ||
id: PIXIEBRIX_INTEGRATION_CONFIG_ID, | ||
serviceId: PIXIEBRIX_INTEGRATION_ID, | ||
// Don't need to proxy requests to our own service | ||
proxy: false, | ||
config: {} as SanitizedConfig, | ||
} as SanitizedIntegrationConfig; | ||
} |
33 changes: 33 additions & 0 deletions
33
src/integrations/util/pixiebrixIntegrationDependencyFactory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { type IntegrationDependency } from "@/integrations/integrationTypes"; | ||
import { | ||
PIXIEBRIX_INTEGRATION_CONFIG_ID, | ||
PIXIEBRIX_INTEGRATION_ID, | ||
PIXIEBRIX_OUTPUT_KEY, | ||
} from "@/integrations/constants"; | ||
|
||
export default function pixiebrixIntegrationDependencyFactory(): IntegrationDependency { | ||
return { | ||
integrationId: PIXIEBRIX_INTEGRATION_ID, | ||
outputKey: PIXIEBRIX_OUTPUT_KEY, | ||
isOptional: false, | ||
apiVersion: "v2", | ||
configId: PIXIEBRIX_INTEGRATION_CONFIG_ID, | ||
} as IntegrationDependency; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ |
Oops, something went wrong.