From 29d70dfa01c0e6f4b92cb85f0196a5cdeca876db Mon Sep 17 00:00:00 2001 From: Nicolas Brichet Date: Fri, 7 Mar 2025 16:54:28 +0100 Subject: [PATCH 1/3] Update the manager list() method to return also the values --- src/connectors/local-storage.ts | 18 ++++++++++-------- src/manager.ts | 13 +++++++++---- src/token.ts | 6 +++++- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/connectors/local-storage.ts b/src/connectors/local-storage.ts index 9d60a2d..09c8bb1 100644 --- a/src/connectors/local-storage.ts +++ b/src/connectors/local-storage.ts @@ -1,4 +1,4 @@ -import { ISecret, ISecretsConnector } from '../token'; +import { ISecret, ISecretsConnector, ISecretsConnectorList } from '../token'; /** * Example connector that save the secrets to the local storage. @@ -36,13 +36,15 @@ passwords are stored as plain text in the local storage of the browser' localStorage.setItem(this.storage, JSON.stringify(secrets)); } - async list( - query?: string | undefined - ): Promise<{ ids: string[]; values: ISecret[] }> { + async list(query?: string | undefined): Promise { const secrets = JSON.parse(localStorage.getItem(this.storage) ?? '{}'); - return { - ids: Object.keys(secrets).filter(key => secrets[key].namespace === query), - values: [] - }; + const initialValue: ISecretsConnectorList = { ids: [], values: [] }; + return Object.keys(secrets) + .filter(key => secrets[key].namespace === query) + .reduce((acc, cur) => { + acc.ids.push(cur); + acc.values.push(secrets[cur]); + return acc; + }, initialValue); } } diff --git a/src/manager.ts b/src/manager.ts index e01c542..416f335 100644 --- a/src/manager.ts +++ b/src/manager.ts @@ -1,4 +1,9 @@ -import { ISecret, ISecretsConnector, ISecretsManager } from './token'; +import { + ISecret, + ISecretsConnector, + ISecretsConnectorList, + ISecretsManager +} from './token'; export namespace SecretsManager { export interface IOptions { @@ -22,8 +27,8 @@ export class SecretsManager implements ISecretsManager { this._connector.remove(id); } - async list(namespace: string): Promise { - return (await this._connector.list(namespace)).ids; + async list(namespace: string): Promise { + return await this._connector.list(namespace); } private _onchange = (e: Event): void => { @@ -82,7 +87,7 @@ export class SecretsManager implements ISecretsManager { async detachAll(namespace: string): Promise { const attachedIds = await this.list(namespace); - attachedIds.forEach(id => { + attachedIds.ids.forEach(id => { this.detach(namespace, id); }); } diff --git a/src/token.ts b/src/token.ts index 5531bae..67491cf 100644 --- a/src/token.ts +++ b/src/token.ts @@ -8,6 +8,10 @@ export interface ISecret { } export interface ISecretsConnector extends IDataConnector {} +export interface ISecretsConnectorList { + ids: string[]; + values: T[]; +} export const ISecretsConnector = new Token( 'jupyter-secret-manager:connector', @@ -18,7 +22,7 @@ export interface ISecretsManager { get(id: string): Promise; set(id: string, secret: ISecret): Promise; remove(id: string): Promise; - list(namespace: string): Promise; + list(namespace: string): Promise; attach( namespace: string, id: string, From fa28ca193e7f51a872323f5c4df853d239aa60fc Mon Sep 17 00:00:00 2001 From: Nicolas Brichet Date: Fri, 7 Mar 2025 16:59:54 +0100 Subject: [PATCH 2/3] Use the list interface in the inMemory connector --- src/connectors/in-memory.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/connectors/in-memory.ts b/src/connectors/in-memory.ts index 8041887..724ca0a 100644 --- a/src/connectors/in-memory.ts +++ b/src/connectors/in-memory.ts @@ -1,4 +1,4 @@ -import { ISecret, ISecretsConnector } from '../token'; +import { ISecret, ISecretsConnector, ISecretsConnectorList } from '../token'; /** * An in memory password connector to store passwords during the session. @@ -19,9 +19,7 @@ export class InMemoryConnector implements ISecretsConnector { this._secrets.delete(id); } - async list( - query?: string | undefined - ): Promise<{ ids: string[]; values: ISecret[] }> { + async list(query?: string | undefined): Promise { const ids: string[] = []; const values: ISecret[] = []; this._secrets.forEach((value, key) => { From cbcbcc163996a2ca5ff61cdac7b9f7825320df98 Mon Sep 17 00:00:00 2001 From: Nicolas Brichet Date: Fri, 7 Mar 2025 19:44:44 +0100 Subject: [PATCH 3/3] Rename ISecretConnectorList to ISecretList to avoid confusion --- src/connectors/in-memory.ts | 4 ++-- src/connectors/local-storage.ts | 6 +++--- src/manager.ts | 4 ++-- src/token.ts | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/connectors/in-memory.ts b/src/connectors/in-memory.ts index 724ca0a..2ca62de 100644 --- a/src/connectors/in-memory.ts +++ b/src/connectors/in-memory.ts @@ -1,4 +1,4 @@ -import { ISecret, ISecretsConnector, ISecretsConnectorList } from '../token'; +import { ISecret, ISecretsConnector, ISecretsList } from '../token'; /** * An in memory password connector to store passwords during the session. @@ -19,7 +19,7 @@ export class InMemoryConnector implements ISecretsConnector { this._secrets.delete(id); } - async list(query?: string | undefined): Promise { + async list(query?: string | undefined): Promise { const ids: string[] = []; const values: ISecret[] = []; this._secrets.forEach((value, key) => { diff --git a/src/connectors/local-storage.ts b/src/connectors/local-storage.ts index 09c8bb1..081d78a 100644 --- a/src/connectors/local-storage.ts +++ b/src/connectors/local-storage.ts @@ -1,4 +1,4 @@ -import { ISecret, ISecretsConnector, ISecretsConnectorList } from '../token'; +import { ISecret, ISecretsConnector, ISecretsList } from '../token'; /** * Example connector that save the secrets to the local storage. @@ -36,9 +36,9 @@ passwords are stored as plain text in the local storage of the browser' localStorage.setItem(this.storage, JSON.stringify(secrets)); } - async list(query?: string | undefined): Promise { + async list(query?: string | undefined): Promise { const secrets = JSON.parse(localStorage.getItem(this.storage) ?? '{}'); - const initialValue: ISecretsConnectorList = { ids: [], values: [] }; + const initialValue: ISecretsList = { ids: [], values: [] }; return Object.keys(secrets) .filter(key => secrets[key].namespace === query) .reduce((acc, cur) => { diff --git a/src/manager.ts b/src/manager.ts index 416f335..25b3933 100644 --- a/src/manager.ts +++ b/src/manager.ts @@ -1,7 +1,7 @@ import { ISecret, ISecretsConnector, - ISecretsConnectorList, + ISecretsList, ISecretsManager } from './token'; @@ -27,7 +27,7 @@ export class SecretsManager implements ISecretsManager { this._connector.remove(id); } - async list(namespace: string): Promise { + async list(namespace: string): Promise { return await this._connector.list(namespace); } diff --git a/src/token.ts b/src/token.ts index 67491cf..63324ef 100644 --- a/src/token.ts +++ b/src/token.ts @@ -8,7 +8,7 @@ export interface ISecret { } export interface ISecretsConnector extends IDataConnector {} -export interface ISecretsConnectorList { +export interface ISecretsList { ids: string[]; values: T[]; } @@ -22,7 +22,7 @@ export interface ISecretsManager { get(id: string): Promise; set(id: string, secret: ISecret): Promise; remove(id: string): Promise; - list(namespace: string): Promise; + list(namespace: string): Promise; attach( namespace: string, id: string,