@backstage/plugin-user-settings-backend 0.2.25-next.1 → 0.2.25

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,34 @@
1
1
  # @backstage/plugin-user-settings-backend
2
2
 
3
+ ## 0.2.25
4
+
5
+ ### Patch Changes
6
+
7
+ - 3109c24: The export for the new backend system at the `/alpha` export is now also available via the main entry point, which means that you can remove the `/alpha` suffix from the import.
8
+ - Updated dependencies
9
+ - @backstage/backend-defaults@0.5.1
10
+ - @backstage/plugin-auth-node@0.5.3
11
+ - @backstage/plugin-signals-node@0.1.12
12
+ - @backstage/backend-plugin-api@1.0.1
13
+ - @backstage/config@1.2.0
14
+ - @backstage/errors@1.2.4
15
+ - @backstage/types@1.1.1
16
+ - @backstage/plugin-user-settings-common@0.0.1
17
+
18
+ ## 0.2.25-next.2
19
+
20
+ ### Patch Changes
21
+
22
+ - Updated dependencies
23
+ - @backstage/backend-defaults@0.5.1-next.2
24
+ - @backstage/plugin-auth-node@0.5.3-next.1
25
+ - @backstage/backend-plugin-api@1.0.1-next.1
26
+ - @backstage/config@1.2.0
27
+ - @backstage/errors@1.2.4
28
+ - @backstage/types@1.1.1
29
+ - @backstage/plugin-signals-node@0.1.12-next.1
30
+ - @backstage/plugin-user-settings-common@0.0.1
31
+
3
32
  ## 0.2.25-next.1
4
33
 
5
34
  ### Patch Changes
package/README.md CHANGED
@@ -16,7 +16,7 @@ yarn --cwd packages/backend add @backstage/plugin-user-settings-backend @backsta
16
16
  Add the plugin to your backend in `packages/backend/src/index.ts`:
17
17
 
18
18
  ```ts
19
- backend.add(import('@backstage/plugin-user-settings-backend/alpha'));
19
+ backend.add(import('@backstage/plugin-user-settings-backend'));
20
20
  // The signals backend is technically optional but enables real-time update of user
21
21
  // settings across different sessions
22
22
  backend.add(import('@backstage/plugin-signals-backend'));
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/plugin-user-settings-backend__alpha",
3
- "version": "0.2.25-next.1",
3
+ "version": "0.2.25",
4
4
  "main": "../dist/alpha.cjs.js",
5
5
  "types": "../dist/alpha.d.ts"
6
6
  }
package/dist/alpha.cjs.js CHANGED
@@ -2,34 +2,9 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var backendPluginApi = require('@backstage/backend-plugin-api');
6
- var router = require('./cjs/router-BIXN0wtq.cjs.js');
7
- var pluginSignalsNode = require('@backstage/plugin-signals-node');
8
- require('@backstage/errors');
9
- require('express');
10
- require('express-promise-router');
5
+ var plugin = require('./plugin.cjs.js');
11
6
 
12
- var alpha = backendPluginApi.createBackendPlugin({
13
- pluginId: "user-settings",
14
- register(env) {
15
- env.registerInit({
16
- deps: {
17
- database: backendPluginApi.coreServices.database,
18
- httpAuth: backendPluginApi.coreServices.httpAuth,
19
- httpRouter: backendPluginApi.coreServices.httpRouter,
20
- signals: pluginSignalsNode.signalsServiceRef
21
- },
22
- async init({ database, httpAuth, httpRouter, signals }) {
23
- const userSettingsStore = await router.DatabaseUserSettingsStore.create({
24
- database
25
- });
26
- httpRouter.use(
27
- await router.createRouterInternal({ userSettingsStore, httpAuth, signals })
28
- );
29
- }
30
- });
31
- }
32
- });
7
+ const _feature = plugin.default;
33
8
 
34
- exports.default = alpha;
9
+ exports.default = _feature;
35
10
  //# sourceMappingURL=alpha.cjs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"alpha.cjs.js","sources":["../src/alpha.ts"],"sourcesContent":["/*\n * Copyright 2023 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 */\n\nimport {\n coreServices,\n createBackendPlugin,\n} from '@backstage/backend-plugin-api';\nimport { createRouterInternal } from './service/router';\nimport { signalsServiceRef } from '@backstage/plugin-signals-node';\nimport { DatabaseUserSettingsStore } from './database/DatabaseUserSettingsStore';\n\n/**\n * The user settings backend plugin.\n *\n * @alpha\n */\nexport default createBackendPlugin({\n pluginId: 'user-settings',\n register(env) {\n env.registerInit({\n deps: {\n database: coreServices.database,\n httpAuth: coreServices.httpAuth,\n httpRouter: coreServices.httpRouter,\n signals: signalsServiceRef,\n },\n async init({ database, httpAuth, httpRouter, signals }) {\n const userSettingsStore = await DatabaseUserSettingsStore.create({\n database,\n });\n httpRouter.use(\n await createRouterInternal({ userSettingsStore, httpAuth, signals }),\n );\n },\n });\n },\n});\n"],"names":["createBackendPlugin","coreServices","signalsServiceRef","DatabaseUserSettingsStore","createRouterInternal"],"mappings":";;;;;;;;;;;AA6BA,YAAeA,oCAAoB,CAAA;AAAA,EACjC,QAAU,EAAA,eAAA;AAAA,EACV,SAAS,GAAK,EAAA;AACZ,IAAA,GAAA,CAAI,YAAa,CAAA;AAAA,MACf,IAAM,EAAA;AAAA,QACJ,UAAUC,6BAAa,CAAA,QAAA;AAAA,QACvB,UAAUA,6BAAa,CAAA,QAAA;AAAA,QACvB,YAAYA,6BAAa,CAAA,UAAA;AAAA,QACzB,OAAS,EAAAC,mCAAA;AAAA,OACX;AAAA,MACA,MAAM,IAAK,CAAA,EAAE,UAAU,QAAU,EAAA,UAAA,EAAY,SAAW,EAAA;AACtD,QAAM,MAAA,iBAAA,GAAoB,MAAMC,gCAAA,CAA0B,MAAO,CAAA;AAAA,UAC/D,QAAA;AAAA,SACD,CAAA,CAAA;AACD,QAAW,UAAA,CAAA,GAAA;AAAA,UACT,MAAMC,2BAAqB,CAAA,EAAE,iBAAmB,EAAA,QAAA,EAAU,SAAS,CAAA;AAAA,SACrE,CAAA;AAAA,OACF;AAAA,KACD,CAAA,CAAA;AAAA,GACH;AACF,CAAC,CAAA;;;;"}
1
+ {"version":3,"file":"alpha.cjs.js","sources":["../src/alpha.ts"],"sourcesContent":["/*\n * Copyright 2024 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 */\n\nimport { default as feature } from './plugin';\n\n/** @alpha */\nconst _feature = feature;\nexport default _feature;\n"],"names":["feature"],"mappings":";;;;;;AAmBA,MAAM,QAAW,GAAAA;;;;"}
package/dist/alpha.d.ts CHANGED
@@ -1,10 +1,6 @@
1
1
  import * as _backstage_backend_plugin_api from '@backstage/backend-plugin-api';
2
2
 
3
- /**
4
- * The user settings backend plugin.
5
- *
6
- * @alpha
7
- */
8
- declare const _default: _backstage_backend_plugin_api.BackendFeature;
3
+ /** @alpha */
4
+ declare const _feature: _backstage_backend_plugin_api.BackendFeature;
9
5
 
10
- export { _default as default };
6
+ export { _feature as default };
@@ -0,0 +1,59 @@
1
+ 'use strict';
2
+
3
+ var backendPluginApi = require('@backstage/backend-plugin-api');
4
+ var errors = require('@backstage/errors');
5
+
6
+ const migrationsDir = backendPluginApi.resolvePackagePath(
7
+ "@backstage/plugin-user-settings-backend",
8
+ "migrations"
9
+ );
10
+ class DatabaseUserSettingsStore {
11
+ constructor(db) {
12
+ this.db = db;
13
+ }
14
+ static async create(options) {
15
+ const { database } = options;
16
+ const client = await database.getClient();
17
+ if (!database.migrations?.skip) {
18
+ await client.migrate.latest({
19
+ directory: migrationsDir
20
+ });
21
+ }
22
+ return new DatabaseUserSettingsStore(client);
23
+ }
24
+ async get(options) {
25
+ const rows = await this.db("user_settings").where({
26
+ user_entity_ref: options.userEntityRef,
27
+ bucket: options.bucket,
28
+ key: options.key
29
+ }).select(["bucket", "key", "value"]);
30
+ if (!rows.length) {
31
+ throw new errors.NotFoundError(
32
+ `Unable to find '${options.key}' in bucket '${options.bucket}'`
33
+ );
34
+ }
35
+ return {
36
+ bucket: rows[0].bucket,
37
+ key: rows[0].key,
38
+ value: JSON.parse(rows[0].value)
39
+ };
40
+ }
41
+ async set(options) {
42
+ await this.db("user_settings").insert({
43
+ user_entity_ref: options.userEntityRef,
44
+ bucket: options.bucket,
45
+ key: options.key,
46
+ value: JSON.stringify(options.value)
47
+ }).onConflict(["user_entity_ref", "bucket", "key"]).merge(["value"]);
48
+ }
49
+ async delete(options) {
50
+ await this.db("user_settings").where({
51
+ user_entity_ref: options.userEntityRef,
52
+ bucket: options.bucket,
53
+ key: options.key
54
+ }).delete();
55
+ }
56
+ }
57
+
58
+ exports.DatabaseUserSettingsStore = DatabaseUserSettingsStore;
59
+ //# sourceMappingURL=DatabaseUserSettingsStore.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DatabaseUserSettingsStore.cjs.js","sources":["../../src/database/DatabaseUserSettingsStore.ts"],"sourcesContent":["/*\n * Copyright 2022 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 */\n\nimport {\n resolvePackagePath,\n DatabaseService,\n} from '@backstage/backend-plugin-api';\nimport { NotFoundError } from '@backstage/errors';\nimport { JsonValue } from '@backstage/types';\nimport { Knex } from 'knex';\nimport { UserSettingsStore, type UserSetting } from './UserSettingsStore';\n\nconst migrationsDir = resolvePackagePath(\n '@backstage/plugin-user-settings-backend',\n 'migrations',\n);\n\n/**\n * @public\n */\nexport type RawDbUserSettingsRow = {\n user_entity_ref: string;\n bucket: string;\n key: string;\n value: string;\n};\n\n/**\n * Store to manage the user settings.\n *\n * @public\n */\nexport class DatabaseUserSettingsStore implements UserSettingsStore {\n static async create(options: {\n database: DatabaseService;\n }): Promise<DatabaseUserSettingsStore> {\n const { database } = options;\n const client = await database.getClient();\n\n if (!database.migrations?.skip) {\n await client.migrate.latest({\n directory: migrationsDir,\n });\n }\n\n return new DatabaseUserSettingsStore(client);\n }\n\n private constructor(private readonly db: Knex) {}\n\n async get(options: {\n userEntityRef: string;\n bucket: string;\n key: string;\n }): Promise<UserSetting> {\n const rows = await this.db<RawDbUserSettingsRow>('user_settings')\n .where({\n user_entity_ref: options.userEntityRef,\n bucket: options.bucket,\n key: options.key,\n })\n .select(['bucket', 'key', 'value']);\n\n if (!rows.length) {\n throw new NotFoundError(\n `Unable to find '${options.key}' in bucket '${options.bucket}'`,\n );\n }\n\n return {\n bucket: rows[0].bucket,\n key: rows[0].key,\n value: JSON.parse(rows[0].value),\n };\n }\n\n async set(options: {\n userEntityRef: string;\n bucket: string;\n key: string;\n value: JsonValue;\n }): Promise<void> {\n await this.db<RawDbUserSettingsRow>('user_settings')\n .insert({\n user_entity_ref: options.userEntityRef,\n bucket: options.bucket,\n key: options.key,\n value: JSON.stringify(options.value),\n })\n .onConflict(['user_entity_ref', 'bucket', 'key'])\n .merge(['value']);\n }\n\n async delete(options: {\n userEntityRef: string;\n bucket: string;\n key: string;\n }): Promise<void> {\n await this.db<RawDbUserSettingsRow>('user_settings')\n .where({\n user_entity_ref: options.userEntityRef,\n bucket: options.bucket,\n key: options.key,\n })\n .delete();\n }\n}\n"],"names":["resolvePackagePath","NotFoundError"],"mappings":";;;;;AAyBA,MAAM,aAAgB,GAAAA,mCAAA;AAAA,EACpB,yCAAA;AAAA,EACA,YAAA;AACF,CAAA,CAAA;AAiBO,MAAM,yBAAuD,CAAA;AAAA,EAgB1D,YAA6B,EAAU,EAAA;AAAV,IAAA,IAAA,CAAA,EAAA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;AAChB,IAAA,MAAM,IAAK,CAAA,EAAA,CAAyB,eAAe,CAAA,CAChD,MAAO,CAAA;AAAA,MACN,iBAAiB,OAAQ,CAAA,aAAA;AAAA,MACzB,QAAQ,OAAQ,CAAA,MAAA;AAAA,MAChB,KAAK,OAAQ,CAAA,GAAA;AAAA,MACb,KAAO,EAAA,IAAA,CAAK,SAAU,CAAA,OAAA,CAAQ,KAAK,CAAA;AAAA,KACpC,CAAA,CACA,UAAW,CAAA,CAAC,iBAAmB,EAAA,QAAA,EAAU,KAAK,CAAC,CAC/C,CAAA,KAAA,CAAM,CAAC,OAAO,CAAC,CAAA,CAAA;AAAA,GACpB;AAAA,EAEA,MAAM,OAAO,OAIK,EAAA;AAChB,IAAA,MAAM,IAAK,CAAA,EAAA,CAAyB,eAAe,CAAA,CAChD,KAAM,CAAA;AAAA,MACL,iBAAiB,OAAQ,CAAA,aAAA;AAAA,MACzB,QAAQ,OAAQ,CAAA,MAAA;AAAA,MAChB,KAAK,OAAQ,CAAA,GAAA;AAAA,KACd,EACA,MAAO,EAAA,CAAA;AAAA,GACZ;AACF;;;;"}
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ var router = require('./service/router.cjs.js');
4
+
5
+ async function createRouter(options) {
6
+ return await router.createRouter(options);
7
+ }
8
+
9
+ exports.createRouter = createRouter;
10
+ //# sourceMappingURL=deprecated.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deprecated.cjs.js","sources":["../src/deprecated.ts"],"sourcesContent":["/*\n * Copyright 2024 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 */\n\nimport express from 'express';\nimport { DatabaseService } from '@backstage/backend-plugin-api';\nimport { SignalsService } from '@backstage/plugin-signals-node';\n\nimport { createRouter as internalCreateRouter } from './service';\nimport { IdentityApi } from '@backstage/plugin-auth-node';\n\n/**\n * Type for the options passed to the \"createRouter\" function.\n *\n * @public\n * @deprecated This type is only exported for legacy reasons and will be removed in the future.\n */\nexport type RouterOptions = {\n database: DatabaseService;\n identity: IdentityApi;\n signals?: SignalsService;\n};\n\n/**\n * Create the user settings backend routes.\n *\n * @public\n * @deprecated This function is only exported for legacy reasons and will be removed in the future.\n * Please {@link https://backstage.io/docs/backend-system/building-backends/migrating | migrate } to use the new backend system and follow these {@link https://github.com/backstage/backstage/tree/master/plugins/user-settings-backend#new-backend | instructions } to install the user settings backend plugin.\n */\nexport async function createRouter(\n options: RouterOptions,\n): Promise<express.Router> {\n return await internalCreateRouter(options);\n}\n"],"names":["internalCreateRouter"],"mappings":";;;;AA0CA,eAAsB,aACpB,OACyB,EAAA;AACzB,EAAO,OAAA,MAAMA,oBAAqB,OAAO,CAAA,CAAA;AAC3C;;;;"}
package/dist/index.cjs.js CHANGED
@@ -1,14 +1,12 @@
1
1
  'use strict';
2
2
 
3
- var router = require('./cjs/router-BIXN0wtq.cjs.js');
4
- require('@backstage/errors');
5
- require('express');
6
- require('express-promise-router');
7
- require('@backstage/backend-plugin-api');
3
+ Object.defineProperty(exports, '__esModule', { value: true });
8
4
 
9
- async function createRouter(options) {
10
- return await router.createRouter(options);
11
- }
5
+ var plugin = require('./plugin.cjs.js');
6
+ var deprecated = require('./deprecated.cjs.js');
12
7
 
13
- exports.createRouter = createRouter;
8
+
9
+
10
+ exports.default = plugin.default;
11
+ exports.createRouter = deprecated.createRouter;
14
12
  //# sourceMappingURL=index.cjs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs.js","sources":["../src/deprecated.ts"],"sourcesContent":["/*\n * Copyright 2024 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 */\n\nimport express from 'express';\nimport { DatabaseService } from '@backstage/backend-plugin-api';\nimport { SignalsService } from '@backstage/plugin-signals-node';\n\nimport { createRouter as internalCreateRouter } from './service';\nimport { IdentityApi } from '@backstage/plugin-auth-node';\n\n/**\n * Type for the options passed to the \"createRouter\" function.\n *\n * @public\n * @deprecated This type is only exported for legacy reasons and will be removed in the future.\n */\nexport type RouterOptions = {\n database: DatabaseService;\n identity: IdentityApi;\n signals?: SignalsService;\n};\n\n/**\n * Create the user settings backend routes.\n *\n * @public\n * @deprecated This function is only exported for legacy reasons and will be removed in the future.\n * Please {@link https://backstage.io/docs/backend-system/building-backends/migrating | migrate } to use the new backend system and follow these {@link https://github.com/backstage/backstage/tree/master/plugins/user-settings-backend#new-backend | instructions } to install the user settings backend plugin.\n */\nexport async function createRouter(\n options: RouterOptions,\n): Promise<express.Router> {\n return await internalCreateRouter(options);\n}\n"],"names":["internalCreateRouter"],"mappings":";;;;;;;;AA0CA,eAAsB,aACpB,OACyB,EAAA;AACzB,EAAO,OAAA,MAAMA,oBAAqB,OAAO,CAAA,CAAA;AAC3C;;;;"}
1
+ {"version":3,"file":"index.cjs.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;"}
package/dist/index.d.ts CHANGED
@@ -1,8 +1,16 @@
1
- import express from 'express';
1
+ import * as _backstage_backend_plugin_api from '@backstage/backend-plugin-api';
2
2
  import { DatabaseService } from '@backstage/backend-plugin-api';
3
+ import express from 'express';
3
4
  import { SignalsService } from '@backstage/plugin-signals-node';
4
5
  import { IdentityApi } from '@backstage/plugin-auth-node';
5
6
 
7
+ /**
8
+ * The user settings backend plugin.
9
+ *
10
+ * @public
11
+ */
12
+ declare const _default: _backstage_backend_plugin_api.BackendFeature;
13
+
6
14
  /**
7
15
  * Type for the options passed to the "createRouter" function.
8
16
  *
@@ -23,4 +31,4 @@ type RouterOptions = {
23
31
  */
24
32
  declare function createRouter(options: RouterOptions): Promise<express.Router>;
25
33
 
26
- export { type RouterOptions, createRouter };
34
+ export { type RouterOptions, createRouter, _default as default };
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var backendPluginApi = require('@backstage/backend-plugin-api');
6
+ var router = require('./service/router.cjs.js');
7
+ var pluginSignalsNode = require('@backstage/plugin-signals-node');
8
+ var DatabaseUserSettingsStore = require('./database/DatabaseUserSettingsStore.cjs.js');
9
+
10
+ var feature = backendPluginApi.createBackendPlugin({
11
+ pluginId: "user-settings",
12
+ register(env) {
13
+ env.registerInit({
14
+ deps: {
15
+ database: backendPluginApi.coreServices.database,
16
+ httpAuth: backendPluginApi.coreServices.httpAuth,
17
+ httpRouter: backendPluginApi.coreServices.httpRouter,
18
+ signals: pluginSignalsNode.signalsServiceRef
19
+ },
20
+ async init({ database, httpAuth, httpRouter, signals }) {
21
+ const userSettingsStore = await DatabaseUserSettingsStore.DatabaseUserSettingsStore.create({
22
+ database
23
+ });
24
+ httpRouter.use(
25
+ await router.createRouterInternal({ userSettingsStore, httpAuth, signals })
26
+ );
27
+ }
28
+ });
29
+ }
30
+ });
31
+
32
+ exports.default = feature;
33
+ //# sourceMappingURL=plugin.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.cjs.js","sources":["../src/plugin.ts"],"sourcesContent":["/*\n * Copyright 2023 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 */\n\nimport {\n coreServices,\n createBackendPlugin,\n} from '@backstage/backend-plugin-api';\nimport { createRouterInternal } from './service/router';\nimport { signalsServiceRef } from '@backstage/plugin-signals-node';\nimport { DatabaseUserSettingsStore } from './database/DatabaseUserSettingsStore';\n\n/**\n * The user settings backend plugin.\n *\n * @public\n */\nexport default createBackendPlugin({\n pluginId: 'user-settings',\n register(env) {\n env.registerInit({\n deps: {\n database: coreServices.database,\n httpAuth: coreServices.httpAuth,\n httpRouter: coreServices.httpRouter,\n signals: signalsServiceRef,\n },\n async init({ database, httpAuth, httpRouter, signals }) {\n const userSettingsStore = await DatabaseUserSettingsStore.create({\n database,\n });\n httpRouter.use(\n await createRouterInternal({ userSettingsStore, httpAuth, signals }),\n );\n },\n });\n },\n});\n"],"names":["createBackendPlugin","coreServices","signalsServiceRef","DatabaseUserSettingsStore","createRouterInternal"],"mappings":";;;;;;;;;AA6BA,cAAeA,oCAAoB,CAAA;AAAA,EACjC,QAAU,EAAA,eAAA;AAAA,EACV,SAAS,GAAK,EAAA;AACZ,IAAA,GAAA,CAAI,YAAa,CAAA;AAAA,MACf,IAAM,EAAA;AAAA,QACJ,UAAUC,6BAAa,CAAA,QAAA;AAAA,QACvB,UAAUA,6BAAa,CAAA,QAAA;AAAA,QACvB,YAAYA,6BAAa,CAAA,UAAA;AAAA,QACzB,OAAS,EAAAC,mCAAA;AAAA,OACX;AAAA,MACA,MAAM,IAAK,CAAA,EAAE,UAAU,QAAU,EAAA,UAAA,EAAY,SAAW,EAAA;AACtD,QAAM,MAAA,iBAAA,GAAoB,MAAMC,mDAAA,CAA0B,MAAO,CAAA;AAAA,UAC/D,QAAA;AAAA,SACD,CAAA,CAAA;AACD,QAAW,UAAA,CAAA,GAAA;AAAA,UACT,MAAMC,2BAAqB,CAAA,EAAE,iBAAmB,EAAA,QAAA,EAAU,SAAS,CAAA;AAAA,SACrE,CAAA;AAAA,OACF;AAAA,KACD,CAAA,CAAA;AAAA,GACH;AACF,CAAC,CAAA;;;;"}
@@ -3,67 +3,15 @@
3
3
  var errors = require('@backstage/errors');
4
4
  var express = require('express');
5
5
  var Router = require('express-promise-router');
6
- var backendPluginApi = require('@backstage/backend-plugin-api');
6
+ var DatabaseUserSettingsStore = require('../database/DatabaseUserSettingsStore.cjs.js');
7
7
 
8
8
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
9
9
 
10
10
  var express__default = /*#__PURE__*/_interopDefaultCompat(express);
11
11
  var Router__default = /*#__PURE__*/_interopDefaultCompat(Router);
12
12
 
13
- const migrationsDir = backendPluginApi.resolvePackagePath(
14
- "@backstage/plugin-user-settings-backend",
15
- "migrations"
16
- );
17
- class DatabaseUserSettingsStore {
18
- constructor(db) {
19
- this.db = db;
20
- }
21
- static async create(options) {
22
- const { database } = options;
23
- const client = await database.getClient();
24
- if (!database.migrations?.skip) {
25
- await client.migrate.latest({
26
- directory: migrationsDir
27
- });
28
- }
29
- return new DatabaseUserSettingsStore(client);
30
- }
31
- async get(options) {
32
- const rows = await this.db("user_settings").where({
33
- user_entity_ref: options.userEntityRef,
34
- bucket: options.bucket,
35
- key: options.key
36
- }).select(["bucket", "key", "value"]);
37
- if (!rows.length) {
38
- throw new errors.NotFoundError(
39
- `Unable to find '${options.key}' in bucket '${options.bucket}'`
40
- );
41
- }
42
- return {
43
- bucket: rows[0].bucket,
44
- key: rows[0].key,
45
- value: JSON.parse(rows[0].value)
46
- };
47
- }
48
- async set(options) {
49
- await this.db("user_settings").insert({
50
- user_entity_ref: options.userEntityRef,
51
- bucket: options.bucket,
52
- key: options.key,
53
- value: JSON.stringify(options.value)
54
- }).onConflict(["user_entity_ref", "bucket", "key"]).merge(["value"]);
55
- }
56
- async delete(options) {
57
- await this.db("user_settings").where({
58
- user_entity_ref: options.userEntityRef,
59
- bucket: options.bucket,
60
- key: options.key
61
- }).delete();
62
- }
63
- }
64
-
65
13
  async function createRouter(options) {
66
- const userSettingsStore = await DatabaseUserSettingsStore.create({
14
+ const userSettingsStore = await DatabaseUserSettingsStore.DatabaseUserSettingsStore.create({
67
15
  database: options.database
68
16
  });
69
17
  return await createRouterInternal({
@@ -141,7 +89,6 @@ async function createRouterInternal(options) {
141
89
  return router;
142
90
  }
143
91
 
144
- exports.DatabaseUserSettingsStore = DatabaseUserSettingsStore;
145
92
  exports.createRouter = createRouter;
146
93
  exports.createRouterInternal = createRouterInternal;
147
- //# sourceMappingURL=router-BIXN0wtq.cjs.js.map
94
+ //# sourceMappingURL=router.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router.cjs.js","sources":["../../src/service/router.ts"],"sourcesContent":["/*\n * Copyright 2022 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 */\n\nimport { AuthenticationError, InputError } from '@backstage/errors';\nimport { IdentityApi } from '@backstage/plugin-auth-node';\nimport express, { Request } from 'express';\nimport Router from 'express-promise-router';\nimport { DatabaseUserSettingsStore } from '../database/DatabaseUserSettingsStore';\nimport { UserSettingsStore } from '../database/UserSettingsStore';\nimport { SignalsService } from '@backstage/plugin-signals-node';\nimport { UserSettingsSignal } from '@backstage/plugin-user-settings-common';\nimport {\n DatabaseService,\n HttpAuthService,\n} from '@backstage/backend-plugin-api';\n\n/**\n * @public\n */\nexport interface RouterOptions {\n database: DatabaseService;\n identity: IdentityApi;\n signals?: SignalsService;\n}\n\n/**\n * Create the user settings backend routes.\n *\n * @public\n */\nexport async function createRouter(\n options: RouterOptions,\n): Promise<express.Router> {\n const userSettingsStore = await DatabaseUserSettingsStore.create({\n database: options.database,\n });\n\n return await createRouterInternal({\n userSettingsStore,\n identity: options.identity,\n signals: options.signals,\n });\n}\n\nexport async function createRouterInternal(\n options:\n | {\n identity: IdentityApi;\n userSettingsStore: UserSettingsStore;\n signals?: SignalsService;\n }\n | {\n httpAuth: HttpAuthService;\n userSettingsStore: UserSettingsStore;\n signals?: SignalsService;\n },\n): Promise<express.Router> {\n const router = Router();\n router.use(express.json());\n\n /**\n * Helper method to extract the userEntityRef from the request.\n */\n const getUserEntityRef = async (req: Request): Promise<string> => {\n if ('httpAuth' in options) {\n const credentials = await options.httpAuth.credentials(req, {\n allow: ['user'],\n });\n return credentials.principal.userEntityRef;\n }\n\n // throws an AuthenticationError in case the token exists but is invalid\n const identity = await options.identity.getIdentity({ request: req });\n if (!identity) {\n throw new AuthenticationError(`Missing token in 'authorization' header`);\n }\n\n return identity.identity.userEntityRef;\n };\n\n // get a single value\n router.get('/buckets/:bucket/keys/:key', async (req, res) => {\n const userEntityRef = await getUserEntityRef(req);\n const { bucket, key } = req.params;\n\n const setting = await options.userSettingsStore.get({\n userEntityRef,\n bucket,\n key,\n });\n\n res.json(setting);\n });\n\n // set a single value\n router.put('/buckets/:bucket/keys/:key', async (req, res) => {\n const userEntityRef = await getUserEntityRef(req);\n const { bucket, key } = req.params;\n const { value } = req.body;\n\n if (value === undefined) {\n throw new InputError('Missing required field \"value\"');\n }\n\n await options.userSettingsStore.set({\n userEntityRef,\n bucket,\n key,\n value,\n });\n const setting = await options.userSettingsStore.get({\n userEntityRef,\n bucket,\n key,\n });\n\n if (options.signals) {\n await options.signals.publish<UserSettingsSignal>({\n recipients: { type: 'user', entityRef: userEntityRef },\n channel: `user-settings`,\n message: { type: 'key-changed', key },\n });\n }\n\n res.json(setting);\n });\n\n // get a single value\n router.delete('/buckets/:bucket/keys/:key', async (req, res) => {\n const userEntityRef = await getUserEntityRef(req);\n const { bucket, key } = req.params;\n\n await options.userSettingsStore.delete({ userEntityRef, bucket, key });\n if (options.signals) {\n await options.signals.publish<UserSettingsSignal>({\n recipients: { type: 'user', entityRef: userEntityRef },\n channel: 'user-settings',\n message: { type: 'key-deleted', key },\n });\n }\n\n res.status(204).end();\n });\n\n return router;\n}\n"],"names":["DatabaseUserSettingsStore","Router","express","AuthenticationError","InputError"],"mappings":";;;;;;;;;;;;AA2CA,eAAsB,aACpB,OACyB,EAAA;AACzB,EAAM,MAAA,iBAAA,GAAoB,MAAMA,mDAAA,CAA0B,MAAO,CAAA;AAAA,IAC/D,UAAU,OAAQ,CAAA,QAAA;AAAA,GACnB,CAAA,CAAA;AAED,EAAA,OAAO,MAAM,oBAAqB,CAAA;AAAA,IAChC,iBAAA;AAAA,IACA,UAAU,OAAQ,CAAA,QAAA;AAAA,IAClB,SAAS,OAAQ,CAAA,OAAA;AAAA,GAClB,CAAA,CAAA;AACH,CAAA;AAEA,eAAsB,qBACpB,OAWyB,EAAA;AACzB,EAAA,MAAM,SAASC,uBAAO,EAAA,CAAA;AACtB,EAAO,MAAA,CAAA,GAAA,CAAIC,wBAAQ,CAAA,IAAA,EAAM,CAAA,CAAA;AAKzB,EAAM,MAAA,gBAAA,GAAmB,OAAO,GAAkC,KAAA;AAChE,IAAA,IAAI,cAAc,OAAS,EAAA;AACzB,MAAA,MAAM,WAAc,GAAA,MAAM,OAAQ,CAAA,QAAA,CAAS,YAAY,GAAK,EAAA;AAAA,QAC1D,KAAA,EAAO,CAAC,MAAM,CAAA;AAAA,OACf,CAAA,CAAA;AACD,MAAA,OAAO,YAAY,SAAU,CAAA,aAAA,CAAA;AAAA,KAC/B;AAGA,IAAM,MAAA,QAAA,GAAW,MAAM,OAAQ,CAAA,QAAA,CAAS,YAAY,EAAE,OAAA,EAAS,KAAK,CAAA,CAAA;AACpE,IAAA,IAAI,CAAC,QAAU,EAAA;AACb,MAAM,MAAA,IAAIC,2BAAoB,CAAyC,uCAAA,CAAA,CAAA,CAAA;AAAA,KACzE;AAEA,IAAA,OAAO,SAAS,Q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backstage backend plugin to manage user settings",
5
5
  "backstage": {
6
6
  "role": "backend-plugin",
@@ -23,6 +23,7 @@
23
23
  "license": "Apache-2.0",
24
24
  "exports": {
25
25
  ".": {
26
+ "backstage": "@backstage/BackendFeature",
26
27
  "require": "./dist/index.cjs.js",
27
28
  "types": "./dist/index.d.ts",
28
29
  "default": "./dist/index.cjs.js"
@@ -52,14 +53,14 @@
52
53
  "test": "backstage-cli package test"
53
54
  },
54
55
  "dependencies": {
55
- "@backstage/backend-defaults": "0.5.1-next.1",
56
- "@backstage/backend-plugin-api": "1.0.1-next.0",
57
- "@backstage/config": "1.2.0",
58
- "@backstage/errors": "1.2.4",
59
- "@backstage/plugin-auth-node": "0.5.3-next.0",
60
- "@backstage/plugin-signals-node": "0.1.12-next.0",
61
- "@backstage/plugin-user-settings-common": "0.0.1",
62
- "@backstage/types": "1.1.1",
56
+ "@backstage/backend-defaults": "^0.5.1",
57
+ "@backstage/backend-plugin-api": "^1.0.1",
58
+ "@backstage/config": "^1.2.0",
59
+ "@backstage/errors": "^1.2.4",
60
+ "@backstage/plugin-auth-node": "^0.5.3",
61
+ "@backstage/plugin-signals-node": "^0.1.12",
62
+ "@backstage/plugin-user-settings-common": "^0.0.1",
63
+ "@backstage/types": "^1.1.1",
63
64
  "@types/express": "^4.17.6",
64
65
  "express": "^4.17.1",
65
66
  "express-promise-router": "^4.1.0",
@@ -67,9 +68,9 @@
67
68
  "yn": "^4.0.0"
68
69
  },
69
70
  "devDependencies": {
70
- "@backstage/backend-defaults": "0.5.1-next.1",
71
- "@backstage/backend-test-utils": "1.0.1-next.1",
72
- "@backstage/cli": "0.28.0-next.1",
71
+ "@backstage/backend-defaults": "^0.5.1",
72
+ "@backstage/backend-test-utils": "^1.0.1",
73
+ "@backstage/cli": "^0.28.0",
73
74
  "@types/supertest": "^2.0.8",
74
75
  "supertest": "^7.0.0"
75
76
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"router-BIXN0wtq.cjs.js","sources":["../../src/database/DatabaseUserSettingsStore.ts","../../src/service/router.ts"],"sourcesContent":["/*\n * Copyright 2022 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 */\n\nimport {\n resolvePackagePath,\n DatabaseService,\n} from '@backstage/backend-plugin-api';\nimport { NotFoundError } from '@backstage/errors';\nimport { JsonValue } from '@backstage/types';\nimport { Knex } from 'knex';\nimport { UserSettingsStore, type UserSetting } from './UserSettingsStore';\n\nconst migrationsDir = resolvePackagePath(\n '@backstage/plugin-user-settings-backend',\n 'migrations',\n);\n\n/**\n * @public\n */\nexport type RawDbUserSettingsRow = {\n user_entity_ref: string;\n bucket: string;\n key: string;\n value: string;\n};\n\n/**\n * Store to manage the user settings.\n *\n * @public\n */\nexport class DatabaseUserSettingsStore implements UserSettingsStore {\n static async create(options: {\n database: DatabaseService;\n }): Promise<DatabaseUserSettingsStore> {\n const { database } = options;\n const client = await database.getClient();\n\n if (!database.migrations?.skip) {\n await client.migrate.latest({\n directory: migrationsDir,\n });\n }\n\n return new DatabaseUserSettingsStore(client);\n }\n\n private constructor(private readonly db: Knex) {}\n\n async get(options: {\n userEntityRef: string;\n bucket: string;\n key: string;\n }): Promise<UserSetting> {\n const rows = await this.db<RawDbUserSettingsRow>('user_settings')\n .where({\n user_entity_ref: options.userEntityRef,\n bucket: options.bucket,\n key: options.key,\n })\n .select(['bucket', 'key', 'value']);\n\n if (!rows.length) {\n throw new NotFoundError(\n `Unable to find '${options.key}' in bucket '${options.bucket}'`,\n );\n }\n\n return {\n bucket: rows[0].bucket,\n key: rows[0].key,\n value: JSON.parse(rows[0].value),\n };\n }\n\n async set(options: {\n userEntityRef: string;\n bucket: string;\n key: string;\n value: JsonValue;\n }): Promise<void> {\n await this.db<RawDbUserSettingsRow>('user_settings')\n .insert({\n user_entity_ref: options.userEntityRef,\n bucket: options.bucket,\n key: options.key,\n value: JSON.stringify(options.value),\n })\n .onConflict(['user_entity_ref', 'bucket', 'key'])\n .merge(['value']);\n }\n\n async delete(options: {\n userEntityRef: string;\n bucket: string;\n key: string;\n }): Promise<void> {\n await this.db<RawDbUserSettingsRow>('user_settings')\n .where({\n user_entity_ref: options.userEntityRef,\n bucket: options.bucket,\n key: options.key,\n })\n .delete();\n }\n}\n","/*\n * Copyright 2022 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 */\n\nimport { AuthenticationError, InputError } from '@backstage/errors';\nimport { IdentityApi } from '@backstage/plugin-auth-node';\nimport express, { Request } from 'express';\nimport Router from 'express-promise-router';\nimport { DatabaseUserSettingsStore } from '../database/DatabaseUserSettingsStore';\nimport { UserSettingsStore } from '../database/UserSettingsStore';\nimport { SignalsService } from '@backstage/plugin-signals-node';\nimport { UserSettingsSignal } from '@backstage/plugin-user-settings-common';\nimport {\n DatabaseService,\n HttpAuthService,\n} from '@backstage/backend-plugin-api';\n\n/**\n * @public\n */\nexport interface RouterOptions {\n database: DatabaseService;\n identity: IdentityApi;\n signals?: SignalsService;\n}\n\n/**\n * Create the user settings backend routes.\n *\n * @public\n */\nexport async function createRouter(\n options: RouterOptions,\n): Promise<express.Router> {\n const userSettingsStore = await DatabaseUserSettingsStore.create({\n database: options.database,\n });\n\n return await createRouterInternal({\n userSettingsStore,\n identity: options.identity,\n signals: options.signals,\n });\n}\n\nexport async function createRouterInternal(\n options:\n | {\n identity: IdentityApi;\n userSettingsStore: UserSettingsStore;\n signals?: SignalsService;\n }\n | {\n httpAuth: HttpAuthService;\n userSettingsStore: UserSettingsStore;\n signals?: SignalsService;\n },\n): Promise<express.Router> {\n const router = Router();\n router.use(express.json());\n\n /**\n * Helper method to extract the userEntityRef from the request.\n */\n const getUserEntityRef = async (req: Request): Promise<string> => {\n if ('httpAuth' in options) {\n const credentials = await options.httpAuth.credentials(req, {\n allow: ['user'],\n });\n return credentials.principal.userEntityRef;\n }\n\n // throws an AuthenticationError in case the token exists but is invalid\n const identity = await options.identity.getIdentity({ request: req });\n if (!identity) {\n throw new AuthenticationError(`Missing token in 'authorization' header`);\n }\n\n return identity.identity.userEntityRef;\n };\n\n // get a single value\n router.get('/buckets/:bucket/keys/:key', async (req, res) => {\n const userEntityRef = await getUserEntityRef(req);\n const { bucket, key } = req.params;\n\n const setting = await options.userSettingsStore.get({\n userEntityRef,\n bucket,\n key,\n });\n\n res.json(setting);\n });\n\n // set a single value\n router.put('/buckets/:bucket/keys/:key', async (req, res) => {\n const userEntityRef = await getUserEntityRef(req);\n const { bucket, key } = req.params;\n const { value } = req.body;\n\n if (value === undefined) {\n throw new InputError('Missing required field \"value\"');\n }\n\n await options.userSettingsStore.set({\n userEntityRef,\n bucket,\n key,\n value,\n });\n const setting = await options.userSettingsStore.get({\n userEntityRef,\n bucket,\n key,\n });\n\n if (options.signals) {\n await options.signals.publish<UserSettingsSignal>({\n recipients: { type: 'user', entityRef: userEntityRef },\n channel: `user-settings`,\n message: { type: 'key-changed', key },\n });\n }\n\n res.json(setting);\n });\n\n // get a single value\n router.delete('/buckets/:bucket/keys/:key', async (req, res) => {\n const userEntityRef = await getUserEntityRef(req);\n const { bucket, key } = req.params;\n\n await options.userSettingsStore.delete({ userEntityRef, bucket, key });\n if (options.signals) {\n await options.signals.publish<UserSettingsSignal>({\n recipients: { type: 'user', entityRef: userEntityRef },\n channel: 'user-settings',\n message: { type: 'key-deleted', key },\n });\n }\n\n res.status(204).end();\n });\n\n return router;\n}\n"],"names":["resolvePackagePath","NotFoundError","Router","express","AuthenticationError","InputError"],"mappings":";;;;;;;;;;;;AAyBA,MAAM,aAAgB,GAAAA,mCAAA;AAAA,EACpB,yCAAA;AAAA,EACA,YAAA;AACF,CAAA,CAAA;AAiBO,MAAM,yBAAuD,CAAA;AAAA,EAgB1D,YAA6B,EAAU,EAAA;AAAV,IAAA,IAAA,CAAA,EAAA,GAAA,EAAA,CAAA;AAAA,GAAW;AAAA,EAfhD,aAAa,OAAO,OAEmB,EAAA;AACrC,IAAM,MAAA,EAAE,UAAa,GAAA,OAAA,CAAA;AACrB,IAAM,MAAA,MAAA,GAAS,MAAM,QAAA,CAAS,SAAU,EAAA,CAAA;AAExC,IAAI,IAAA,CAAC,QAAS,CAAA,UAAA,EAAY,IAAM,EAAA;AAC9B,MAAM,MAAA,MAAA,CAAO,QAAQ,MAAO,CAAA;AAAA,QAC1B,SAAW,EAAA,aAAA;AAAA,OACZ,CAAA,CAAA;AAAA,KACH;AAEA,IAAO,OAAA,IAAI,0BAA0B,MAAM,CAAA,CAAA;AAAA,GAC7C;AAAA,EAIA,MAAM,IAAI,OAIe,EAAA;AACvB,IAAA,MAAM,OAAO,MAAM,IAAA,CAAK,EAAyB,CAAA,eAAe,EAC7D,KAAM,CAAA;AAAA,MACL,iBAAiB,OAAQ,CAAA,aAAA;AAAA,MACzB,QAAQ,OAAQ,CAAA,MAAA;AAAA,MAChB,KAAK,OAAQ,CAAA,GAAA;AAAA,KACd,CACA,CAAA,MAAA,CAAO,CAAC,QAAU,EAAA,KAAA,EAAO,OAAO,CAAC,CAAA,CAAA;AAEpC,IAAI,IAAA,CAAC,KAAK,MAAQ,EAAA;AAChB,MAAA,MAAM,IAAIC,oBAAA;AAAA,QACR,CAAmB,gBAAA,EAAA,OAAA,CAAQ,GAAG,CAAA,aAAA,EAAgB,QAAQ,MAAM,CAAA,CAAA,CAAA;AAAA,OAC9D,CA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