@metamask/profile-sync-controller 0.9.4 → 0.9.6
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 +15 -1
- package/dist/controllers/user-storage/UserStorageController.cjs +16 -0
- package/dist/controllers/user-storage/UserStorageController.cjs.map +1 -1
- package/dist/controllers/user-storage/UserStorageController.d.cts +8 -0
- package/dist/controllers/user-storage/UserStorageController.d.cts.map +1 -1
- package/dist/controllers/user-storage/UserStorageController.d.mts +8 -0
- package/dist/controllers/user-storage/UserStorageController.d.mts.map +1 -1
- package/dist/controllers/user-storage/UserStorageController.mjs +17 -1
- package/dist/controllers/user-storage/UserStorageController.mjs.map +1 -1
- package/dist/controllers/user-storage/__fixtures__/mockResponses.cjs +9 -1
- package/dist/controllers/user-storage/__fixtures__/mockResponses.cjs.map +1 -1
- package/dist/controllers/user-storage/__fixtures__/mockResponses.d.cts +5 -0
- package/dist/controllers/user-storage/__fixtures__/mockResponses.d.cts.map +1 -1
- package/dist/controllers/user-storage/__fixtures__/mockResponses.d.mts +5 -0
- package/dist/controllers/user-storage/__fixtures__/mockResponses.d.mts.map +1 -1
- package/dist/controllers/user-storage/__fixtures__/mockResponses.mjs +7 -0
- package/dist/controllers/user-storage/__fixtures__/mockResponses.mjs.map +1 -1
- package/dist/controllers/user-storage/__fixtures__/mockServices.cjs +10 -1
- package/dist/controllers/user-storage/__fixtures__/mockServices.cjs.map +1 -1
- package/dist/controllers/user-storage/__fixtures__/mockServices.d.cts +1 -0
- package/dist/controllers/user-storage/__fixtures__/mockServices.d.cts.map +1 -1
- package/dist/controllers/user-storage/__fixtures__/mockServices.d.mts +1 -0
- package/dist/controllers/user-storage/__fixtures__/mockServices.d.mts.map +1 -1
- package/dist/controllers/user-storage/__fixtures__/mockServices.mjs +9 -1
- package/dist/controllers/user-storage/__fixtures__/mockServices.mjs.map +1 -1
- package/dist/controllers/user-storage/services.cjs +25 -1
- package/dist/controllers/user-storage/services.cjs.map +1 -1
- package/dist/controllers/user-storage/services.d.cts +6 -0
- package/dist/controllers/user-storage/services.d.cts.map +1 -1
- package/dist/controllers/user-storage/services.d.mts +6 -0
- package/dist/controllers/user-storage/services.d.mts.map +1 -1
- package/dist/controllers/user-storage/services.mjs +23 -0
- package/dist/controllers/user-storage/services.mjs.map +1 -1
- package/dist/sdk/__fixtures__/mock-userstorage.cjs +10 -1
- package/dist/sdk/__fixtures__/mock-userstorage.cjs.map +1 -1
- package/dist/sdk/__fixtures__/mock-userstorage.d.cts +1 -0
- package/dist/sdk/__fixtures__/mock-userstorage.d.cts.map +1 -1
- package/dist/sdk/__fixtures__/mock-userstorage.d.mts +1 -0
- package/dist/sdk/__fixtures__/mock-userstorage.d.mts.map +1 -1
- package/dist/sdk/__fixtures__/mock-userstorage.mjs +8 -0
- package/dist/sdk/__fixtures__/mock-userstorage.mjs.map +1 -1
- package/dist/sdk/user-storage.cjs +31 -1
- package/dist/sdk/user-storage.cjs.map +1 -1
- package/dist/sdk/user-storage.d.cts +1 -0
- package/dist/sdk/user-storage.d.cts.map +1 -1
- package/dist/sdk/user-storage.d.mts +1 -0
- package/dist/sdk/user-storage.d.mts.map +1 -1
- package/dist/sdk/user-storage.mjs +31 -1
- package/dist/sdk/user-storage.mjs.map +1 -1
- package/dist/shared/encryption/encryption.cjs +1 -1
- package/dist/shared/encryption/encryption.cjs.map +1 -1
- package/dist/shared/encryption/encryption.d.cts.map +1 -1
- package/dist/shared/encryption/encryption.d.mts.map +1 -1
- package/dist/shared/encryption/encryption.mjs +2 -2
- package/dist/shared/encryption/encryption.mjs.map +1 -1
- package/dist/shared/encryption/utils.cjs +5 -1
- package/dist/shared/encryption/utils.cjs.map +1 -1
- package/dist/shared/encryption/utils.d.cts +1 -0
- package/dist/shared/encryption/utils.d.cts.map +1 -1
- package/dist/shared/encryption/utils.d.mts +1 -0
- package/dist/shared/encryption/utils.d.mts.map +1 -1
- package/dist/shared/encryption/utils.mjs +3 -0
- package/dist/shared/encryption/utils.mjs.map +1 -1
- package/dist/shared/types/encryption.cjs.map +1 -1
- package/dist/shared/types/encryption.d.cts +1 -1
- package/dist/shared/types/encryption.d.cts.map +1 -1
- package/dist/shared/types/encryption.d.mts +1 -1
- package/dist/shared/types/encryption.d.mts.map +1 -1
- package/dist/shared/types/encryption.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -54,4 +54,10 @@ export declare function upsertUserStorage(data: string, opts: UserStorageOptions
|
|
|
54
54
|
* @param opts - storage options
|
|
55
55
|
*/
|
|
56
56
|
export declare function batchUpsertUserStorage(data: [UserStoragePathWithKeyOnly, string][], opts: UserStorageBatchUpsertOptions): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* User Storage Service - Delete all storage entries for a specific feature.
|
|
59
|
+
*
|
|
60
|
+
* @param opts - User Storage Options
|
|
61
|
+
*/
|
|
62
|
+
export declare function deleteUserStorageAllFeatureEntries(opts: UserStorageAllFeatureEntriesOptions): Promise<void>;
|
|
57
63
|
//# sourceMappingURL=services.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"services.d.cts","sourceRoot":"","sources":["../../../src/controllers/user-storage/services.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,gCAAgC,EAChC,8BAA8B,EAC9B,0BAA0B,EAC3B,wCAAoC;AAErC,OAAO,KAAK,EAAE,YAAY,EAAE,0CAAsC;AAIlE,eAAO,MAAM,gBAAgB,EAAE,MAAmC,CAAC;AACnE,eAAO,MAAM,qBAAqB,QAA2C,CAAC;AAE9E;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG;IAEnC,SAAS,EAAE,MAAM,CAAC;IAElB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,uCAAuC,GAAG;IAEpD,SAAS,EAAE,MAAM,CAAC;IAElB,IAAI,EAAE,MAAM,CAAC;CACd,EAAE,CAAC;AAEJ,MAAM,MAAM,sBAAsB,GAAG;IACnC,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,kBAAkB,CAAC,EAAE,YAAY,CAAC;CACnC,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG,sBAAsB,GAAG;IACxD,IAAI,EAAE,gCAAgC,CAAC;CACxC,CAAC;AAEF,MAAM,MAAM,mCAAmC,GAAG,sBAAsB,GAAG;IACzE,IAAI,EAAE,8BAA8B,CAAC;CACtC,CAAC;AAEF,MAAM,MAAM,6BAA6B,GAAG,mCAAmC,CAAC;AAEhF;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CA0CxB;AAED;;;;;GAKG;AACH,wBAAsB,+BAA+B,CACnD,IAAI,EAAE,mCAAmC,GACxC,OAAO,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,CAoD1B;AAED;;;;;GAKG;AACH,wBAAsB,iBAAiB,CACrC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,IAAI,CAAC,CAuBf;AAED;;;;;;GAMG;AACH,wBAAsB,sBAAsB,CAC1C,IAAI,EAAE,CAAC,0BAA0B,EAAE,MAAM,CAAC,EAAE,EAC5C,IAAI,EAAE,6BAA6B,GAClC,OAAO,CAAC,IAAI,CAAC,CAgCf"}
|
|
1
|
+
{"version":3,"file":"services.d.cts","sourceRoot":"","sources":["../../../src/controllers/user-storage/services.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,gCAAgC,EAChC,8BAA8B,EAC9B,0BAA0B,EAC3B,wCAAoC;AAErC,OAAO,KAAK,EAAE,YAAY,EAAE,0CAAsC;AAIlE,eAAO,MAAM,gBAAgB,EAAE,MAAmC,CAAC;AACnE,eAAO,MAAM,qBAAqB,QAA2C,CAAC;AAE9E;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG;IAEnC,SAAS,EAAE,MAAM,CAAC;IAElB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,uCAAuC,GAAG;IAEpD,SAAS,EAAE,MAAM,CAAC;IAElB,IAAI,EAAE,MAAM,CAAC;CACd,EAAE,CAAC;AAEJ,MAAM,MAAM,sBAAsB,GAAG;IACnC,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,kBAAkB,CAAC,EAAE,YAAY,CAAC;CACnC,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG,sBAAsB,GAAG;IACxD,IAAI,EAAE,gCAAgC,CAAC;CACxC,CAAC;AAEF,MAAM,MAAM,mCAAmC,GAAG,sBAAsB,GAAG;IACzE,IAAI,EAAE,8BAA8B,CAAC;CACtC,CAAC;AAEF,MAAM,MAAM,6BAA6B,GAAG,mCAAmC,CAAC;AAEhF;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CA0CxB;AAED;;;;;GAKG;AACH,wBAAsB,+BAA+B,CACnD,IAAI,EAAE,mCAAmC,GACxC,OAAO,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,CAoD1B;AAED;;;;;GAKG;AACH,wBAAsB,iBAAiB,CACrC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,IAAI,CAAC,CAuBf;AAED;;;;;;GAMG;AACH,wBAAsB,sBAAsB,CAC1C,IAAI,EAAE,CAAC,0BAA0B,EAAE,MAAM,CAAC,EAAE,EAC5C,IAAI,EAAE,6BAA6B,GAClC,OAAO,CAAC,IAAI,CAAC,CAgCf;AAED;;;;GAIG;AACH,wBAAsB,kCAAkC,CACtD,IAAI,EAAE,mCAAmC,GACxC,OAAO,CAAC,IAAI,CAAC,CAoBf"}
|
|
@@ -54,4 +54,10 @@ export declare function upsertUserStorage(data: string, opts: UserStorageOptions
|
|
|
54
54
|
* @param opts - storage options
|
|
55
55
|
*/
|
|
56
56
|
export declare function batchUpsertUserStorage(data: [UserStoragePathWithKeyOnly, string][], opts: UserStorageBatchUpsertOptions): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* User Storage Service - Delete all storage entries for a specific feature.
|
|
59
|
+
*
|
|
60
|
+
* @param opts - User Storage Options
|
|
61
|
+
*/
|
|
62
|
+
export declare function deleteUserStorageAllFeatureEntries(opts: UserStorageAllFeatureEntriesOptions): Promise<void>;
|
|
57
63
|
//# sourceMappingURL=services.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"services.d.mts","sourceRoot":"","sources":["../../../src/controllers/user-storage/services.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,gCAAgC,EAChC,8BAA8B,EAC9B,0BAA0B,EAC3B,wCAAoC;AAErC,OAAO,KAAK,EAAE,YAAY,EAAE,0CAAsC;AAIlE,eAAO,MAAM,gBAAgB,EAAE,MAAmC,CAAC;AACnE,eAAO,MAAM,qBAAqB,QAA2C,CAAC;AAE9E;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG;IAEnC,SAAS,EAAE,MAAM,CAAC;IAElB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,uCAAuC,GAAG;IAEpD,SAAS,EAAE,MAAM,CAAC;IAElB,IAAI,EAAE,MAAM,CAAC;CACd,EAAE,CAAC;AAEJ,MAAM,MAAM,sBAAsB,GAAG;IACnC,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,kBAAkB,CAAC,EAAE,YAAY,CAAC;CACnC,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG,sBAAsB,GAAG;IACxD,IAAI,EAAE,gCAAgC,CAAC;CACxC,CAAC;AAEF,MAAM,MAAM,mCAAmC,GAAG,sBAAsB,GAAG;IACzE,IAAI,EAAE,8BAA8B,CAAC;CACtC,CAAC;AAEF,MAAM,MAAM,6BAA6B,GAAG,mCAAmC,CAAC;AAEhF;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CA0CxB;AAED;;;;;GAKG;AACH,wBAAsB,+BAA+B,CACnD,IAAI,EAAE,mCAAmC,GACxC,OAAO,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,CAoD1B;AAED;;;;;GAKG;AACH,wBAAsB,iBAAiB,CACrC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,IAAI,CAAC,CAuBf;AAED;;;;;;GAMG;AACH,wBAAsB,sBAAsB,CAC1C,IAAI,EAAE,CAAC,0BAA0B,EAAE,MAAM,CAAC,EAAE,EAC5C,IAAI,EAAE,6BAA6B,GAClC,OAAO,CAAC,IAAI,CAAC,CAgCf"}
|
|
1
|
+
{"version":3,"file":"services.d.mts","sourceRoot":"","sources":["../../../src/controllers/user-storage/services.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,gCAAgC,EAChC,8BAA8B,EAC9B,0BAA0B,EAC3B,wCAAoC;AAErC,OAAO,KAAK,EAAE,YAAY,EAAE,0CAAsC;AAIlE,eAAO,MAAM,gBAAgB,EAAE,MAAmC,CAAC;AACnE,eAAO,MAAM,qBAAqB,QAA2C,CAAC;AAE9E;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG;IAEnC,SAAS,EAAE,MAAM,CAAC;IAElB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,uCAAuC,GAAG;IAEpD,SAAS,EAAE,MAAM,CAAC;IAElB,IAAI,EAAE,MAAM,CAAC;CACd,EAAE,CAAC;AAEJ,MAAM,MAAM,sBAAsB,GAAG;IACnC,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,kBAAkB,CAAC,EAAE,YAAY,CAAC;CACnC,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG,sBAAsB,GAAG;IACxD,IAAI,EAAE,gCAAgC,CAAC;CACxC,CAAC;AAEF,MAAM,MAAM,mCAAmC,GAAG,sBAAsB,GAAG;IACzE,IAAI,EAAE,8BAA8B,CAAC;CACtC,CAAC;AAEF,MAAM,MAAM,6BAA6B,GAAG,mCAAmC,CAAC;AAEhF;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CA0CxB;AAED;;;;;GAKG;AACH,wBAAsB,+BAA+B,CACnD,IAAI,EAAE,mCAAmC,GACxC,OAAO,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,CAoD1B;AAED;;;;;GAKG;AACH,wBAAsB,iBAAiB,CACrC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,IAAI,CAAC,CAuBf;AAED;;;;;;GAMG;AACH,wBAAsB,sBAAsB,CAC1C,IAAI,EAAE,CAAC,0BAA0B,EAAE,MAAM,CAAC,EAAE,EAC5C,IAAI,EAAE,6BAA6B,GAClC,OAAO,CAAC,IAAI,CAAC,CAgCf;AAED;;;;GAIG;AACH,wBAAsB,kCAAkC,CACtD,IAAI,EAAE,mCAAmC,GACxC,OAAO,CAAC,IAAI,CAAC,CAoBf"}
|
|
@@ -152,4 +152,27 @@ export async function batchUpsertUserStorage(data, opts) {
|
|
|
152
152
|
throw new Error('user-storage - unable to batch upsert data');
|
|
153
153
|
}
|
|
154
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* User Storage Service - Delete all storage entries for a specific feature.
|
|
157
|
+
*
|
|
158
|
+
* @param opts - User Storage Options
|
|
159
|
+
*/
|
|
160
|
+
export async function deleteUserStorageAllFeatureEntries(opts) {
|
|
161
|
+
const { bearerToken, path } = opts;
|
|
162
|
+
const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);
|
|
163
|
+
const userStorageResponse = await fetch(url.toString(), {
|
|
164
|
+
method: 'DELETE',
|
|
165
|
+
headers: {
|
|
166
|
+
'Content-Type': 'application/json',
|
|
167
|
+
Authorization: `Bearer ${bearerToken}`,
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
// Acceptable error - since indicates feature does not exist.
|
|
171
|
+
if (userStorageResponse.status === 404) {
|
|
172
|
+
throw new Error('user-storage - feature not found');
|
|
173
|
+
}
|
|
174
|
+
if (userStorageResponse.status !== 200 || !userStorageResponse.ok) {
|
|
175
|
+
throw new Error('user-storage - unable to delete data');
|
|
176
|
+
}
|
|
177
|
+
}
|
|
155
178
|
//# sourceMappingURL=services.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"services.mjs","sourceRoot":"","sources":["../../../src/controllers/user-storage/services.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,IAAG,iBAAiB;;AAE3B,OAAO,UAAU,EAAE,EAAE,gBAAgB,EAAE,0CAAgC;AACvE,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,6BAAyB;AAMnD,OAAO,EAAE,eAAe,EAAE,wCAAoC;AAG9D,MAAM,QAAQ,GAAG,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAErC,MAAM,CAAC,MAAM,gBAAgB,GAAW,QAAQ,CAAC,iBAAiB,CAAC;AACnE,MAAM,CAAC,MAAM,qBAAqB,GAAG,GAAG,gBAAgB,qBAAqB,CAAC;AAmC9E;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,IAAwB;IAExB,IAAI;QACF,MAAM,EAAE,WAAW,EAAE,IAAI,EAAE,UAAU,EAAE,kBAAkB,EAAE,GAAG,IAAI,CAAC;QAEnE,MAAM,aAAa,GAAG,eAAe,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;QACxD,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,qBAAqB,IAAI,aAAa,EAAE,CAAC,CAAC;QAEjE,MAAM,mBAAmB,GAAG,MAAM,KAAK,C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EAAE,kBAAkB;YAClC,aAAa,EAAE,UAAU,WAAW,EAAE;SACvC;QACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC;KAC9C,CAAC,CAAC;IAEH,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE;QACX,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;KAC/D;AACH,CAAC","sourcesContent":["import log from 'loglevel';\n\nimport encryption, { createSHA256Hash } from '../../shared/encryption';\nimport { Env, getEnvUrls } from '../../shared/env';\nimport type {\n UserStoragePathWithFeatureAndKey,\n UserStoragePathWithFeatureOnly,\n UserStoragePathWithKeyOnly,\n} from '../../shared/storage-schema';\nimport { createEntryPath } from '../../shared/storage-schema';\nimport type { NativeScrypt } from '../../shared/types/encryption';\n\nconst ENV_URLS = getEnvUrls(Env.PRD);\n\nexport const USER_STORAGE_API: string = ENV_URLS.userStorageApiUrl;\nexport const USER_STORAGE_ENDPOINT = `${USER_STORAGE_API}/api/v1/userstorage`;\n\n/**\n * This is the Server Response shape for a feature entry.\n */\nexport type GetUserStorageResponse = {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n HashedKey: string;\n // eslint-disable-next-line @typescript-eslint/naming-convention\n Data: string;\n};\n\nexport type GetUserStorageAllFeatureEntriesResponse = {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n HashedKey: string;\n // eslint-disable-next-line @typescript-eslint/naming-convention\n Data: string;\n}[];\n\nexport type UserStorageBaseOptions = {\n bearerToken: string;\n storageKey: string;\n nativeScryptCrypto?: NativeScrypt;\n};\n\nexport type UserStorageOptions = UserStorageBaseOptions & {\n path: UserStoragePathWithFeatureAndKey;\n};\n\nexport type UserStorageAllFeatureEntriesOptions = UserStorageBaseOptions & {\n path: UserStoragePathWithFeatureOnly;\n};\n\nexport type UserStorageBatchUpsertOptions = UserStorageAllFeatureEntriesOptions;\n\n/**\n * User Storage Service - Get Storage Entry.\n *\n * @param opts - User Storage Options\n * @returns The storage entry, or null if fails to find entry\n */\nexport async function getUserStorage(\n opts: UserStorageOptions,\n): Promise<string | null> {\n try {\n const { bearerToken, path, storageKey, nativeScryptCrypto } = opts;\n\n const encryptedPath = createEntryPath(path, storageKey);\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${encryptedPath}`);\n\n const userStorageResponse = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n });\n\n // Acceptable error - since indicates entry does not exist.\n if (userStorageResponse.status === 404) {\n return null;\n }\n\n if (userStorageResponse.status !== 200) {\n throw new Error('Unable to get User Storage');\n }\n\n const userStorage: GetUserStorageResponse | null =\n await userStorageResponse.json();\n const encryptedData = userStorage?.Data ?? null;\n\n if (!encryptedData) {\n return null;\n }\n\n const decryptedData = await encryption.decryptString(\n encryptedData,\n opts.storageKey,\n nativeScryptCrypto,\n );\n\n return decryptedData;\n } catch (e) {\n log.error('Failed to get user storage', e);\n return null;\n }\n}\n\n/**\n * User Storage Service - Get all storage entries for a specific feature.\n *\n * @param opts - User Storage Options\n * @returns The storage entry, or null if fails to find entry\n */\nexport async function getUserStorageAllFeatureEntries(\n opts: UserStorageAllFeatureEntriesOptions,\n): Promise<string[] | null> {\n try {\n const { bearerToken, path, nativeScryptCrypto } = opts;\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);\n\n const userStorageResponse = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n });\n\n // Acceptable error - since indicates feature does not exist.\n if (userStorageResponse.status === 404) {\n return null;\n }\n\n if (userStorageResponse.status !== 200) {\n throw new Error('Unable to get User Storage');\n }\n\n const userStorage: GetUserStorageAllFeatureEntriesResponse | null =\n await userStorageResponse.json();\n\n if (!Array.isArray(userStorage)) {\n return null;\n }\n\n const decryptedData: string[] = [];\n\n for (const entry of userStorage) {\n if (!entry.Data) {\n continue;\n }\n\n try {\n const data = await encryption.decryptString(\n entry.Data,\n opts.storageKey,\n nativeScryptCrypto,\n );\n decryptedData.push(data);\n } catch {\n // do nothing\n }\n }\n\n return decryptedData;\n } catch (e) {\n log.error('Failed to get user storage', e);\n return null;\n }\n}\n\n/**\n * User Storage Service - Set Storage Entry.\n *\n * @param data - data to store\n * @param opts - storage options\n */\nexport async function upsertUserStorage(\n data: string,\n opts: UserStorageOptions,\n): Promise<void> {\n const { bearerToken, path, storageKey, nativeScryptCrypto } = opts;\n\n const encryptedData = await encryption.encryptString(\n data,\n opts.storageKey,\n nativeScryptCrypto,\n );\n const encryptedPath = createEntryPath(path, storageKey);\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${encryptedPath}`);\n\n const res = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n body: JSON.stringify({ data: encryptedData }),\n });\n\n if (!res.ok) {\n throw new Error('user-storage - unable to upsert data');\n }\n}\n\n/**\n * User Storage Service - Set multiple storage entries for one specific feature.\n * You cannot use this method to set multiple features at once.\n *\n * @param data - data to store, in the form of an array of [entryKey, entryValue] pairs\n * @param opts - storage options\n */\nexport async function batchUpsertUserStorage(\n data: [UserStoragePathWithKeyOnly, string][],\n opts: UserStorageBatchUpsertOptions,\n): Promise<void> {\n if (!data.length) {\n return;\n }\n\n const { bearerToken, path, storageKey, nativeScryptCrypto } = opts;\n\n const encryptedData: string[][] = [];\n\n for (const d of data) {\n encryptedData.push([\n createSHA256Hash(d[0] + storageKey),\n await encryption.encryptString(d[1], opts.storageKey, nativeScryptCrypto),\n ]);\n }\n\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);\n\n const formattedData = Object.fromEntries(encryptedData);\n\n const res = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n body: JSON.stringify({ data: formattedData }),\n });\n\n if (!res.ok) {\n throw new Error('user-storage - unable to batch upsert data');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,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,qBAAqB,IAAI,IAAI,EAAE,CAAC,CAAC;IAExD,MAAM,mBAAmB,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE;QACtD,MAAM,EAAE,QAAQ;QAChB,OAAO,EAAE;YACP,cAAc,EAAE,kBAAkB;YAClC,aAAa,EAAE,UAAU,WAAW,EAAE;SACvC;KACF,CAAC,CAAC;IAEH,6DAA6D;IAC7D,IAAI,mBAAmB,CAAC,MAAM,KAAK,GAAG,EAAE;QACtC,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;KACrD;IAED,IAAI,mBAAmB,CAAC,MAAM,KAAK,GAAG,IAAI,CAAC,mBAAmB,CAAC,EAAE,EAAE;QACjE,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;KACzD;AACH,CAAC","sourcesContent":["import log from 'loglevel';\n\nimport encryption, { createSHA256Hash } from '../../shared/encryption';\nimport { Env, getEnvUrls } from '../../shared/env';\nimport type {\n UserStoragePathWithFeatureAndKey,\n UserStoragePathWithFeatureOnly,\n UserStoragePathWithKeyOnly,\n} from '../../shared/storage-schema';\nimport { createEntryPath } from '../../shared/storage-schema';\nimport type { NativeScrypt } from '../../shared/types/encryption';\n\nconst ENV_URLS = getEnvUrls(Env.PRD);\n\nexport const USER_STORAGE_API: string = ENV_URLS.userStorageApiUrl;\nexport const USER_STORAGE_ENDPOINT = `${USER_STORAGE_API}/api/v1/userstorage`;\n\n/**\n * This is the Server Response shape for a feature entry.\n */\nexport type GetUserStorageResponse = {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n HashedKey: string;\n // eslint-disable-next-line @typescript-eslint/naming-convention\n Data: string;\n};\n\nexport type GetUserStorageAllFeatureEntriesResponse = {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n HashedKey: string;\n // eslint-disable-next-line @typescript-eslint/naming-convention\n Data: string;\n}[];\n\nexport type UserStorageBaseOptions = {\n bearerToken: string;\n storageKey: string;\n nativeScryptCrypto?: NativeScrypt;\n};\n\nexport type UserStorageOptions = UserStorageBaseOptions & {\n path: UserStoragePathWithFeatureAndKey;\n};\n\nexport type UserStorageAllFeatureEntriesOptions = UserStorageBaseOptions & {\n path: UserStoragePathWithFeatureOnly;\n};\n\nexport type UserStorageBatchUpsertOptions = UserStorageAllFeatureEntriesOptions;\n\n/**\n * User Storage Service - Get Storage Entry.\n *\n * @param opts - User Storage Options\n * @returns The storage entry, or null if fails to find entry\n */\nexport async function getUserStorage(\n opts: UserStorageOptions,\n): Promise<string | null> {\n try {\n const { bearerToken, path, storageKey, nativeScryptCrypto } = opts;\n\n const encryptedPath = createEntryPath(path, storageKey);\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${encryptedPath}`);\n\n const userStorageResponse = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n });\n\n // Acceptable error - since indicates entry does not exist.\n if (userStorageResponse.status === 404) {\n return null;\n }\n\n if (userStorageResponse.status !== 200) {\n throw new Error('Unable to get User Storage');\n }\n\n const userStorage: GetUserStorageResponse | null =\n await userStorageResponse.json();\n const encryptedData = userStorage?.Data ?? null;\n\n if (!encryptedData) {\n return null;\n }\n\n const decryptedData = await encryption.decryptString(\n encryptedData,\n opts.storageKey,\n nativeScryptCrypto,\n );\n\n return decryptedData;\n } catch (e) {\n log.error('Failed to get user storage', e);\n return null;\n }\n}\n\n/**\n * User Storage Service - Get all storage entries for a specific feature.\n *\n * @param opts - User Storage Options\n * @returns The storage entry, or null if fails to find entry\n */\nexport async function getUserStorageAllFeatureEntries(\n opts: UserStorageAllFeatureEntriesOptions,\n): Promise<string[] | null> {\n try {\n const { bearerToken, path, nativeScryptCrypto } = opts;\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);\n\n const userStorageResponse = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n });\n\n // Acceptable error - since indicates feature does not exist.\n if (userStorageResponse.status === 404) {\n return null;\n }\n\n if (userStorageResponse.status !== 200) {\n throw new Error('Unable to get User Storage');\n }\n\n const userStorage: GetUserStorageAllFeatureEntriesResponse | null =\n await userStorageResponse.json();\n\n if (!Array.isArray(userStorage)) {\n return null;\n }\n\n const decryptedData: string[] = [];\n\n for (const entry of userStorage) {\n if (!entry.Data) {\n continue;\n }\n\n try {\n const data = await encryption.decryptString(\n entry.Data,\n opts.storageKey,\n nativeScryptCrypto,\n );\n decryptedData.push(data);\n } catch {\n // do nothing\n }\n }\n\n return decryptedData;\n } catch (e) {\n log.error('Failed to get user storage', e);\n return null;\n }\n}\n\n/**\n * User Storage Service - Set Storage Entry.\n *\n * @param data - data to store\n * @param opts - storage options\n */\nexport async function upsertUserStorage(\n data: string,\n opts: UserStorageOptions,\n): Promise<void> {\n const { bearerToken, path, storageKey, nativeScryptCrypto } = opts;\n\n const encryptedData = await encryption.encryptString(\n data,\n opts.storageKey,\n nativeScryptCrypto,\n );\n const encryptedPath = createEntryPath(path, storageKey);\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${encryptedPath}`);\n\n const res = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n body: JSON.stringify({ data: encryptedData }),\n });\n\n if (!res.ok) {\n throw new Error('user-storage - unable to upsert data');\n }\n}\n\n/**\n * User Storage Service - Set multiple storage entries for one specific feature.\n * You cannot use this method to set multiple features at once.\n *\n * @param data - data to store, in the form of an array of [entryKey, entryValue] pairs\n * @param opts - storage options\n */\nexport async function batchUpsertUserStorage(\n data: [UserStoragePathWithKeyOnly, string][],\n opts: UserStorageBatchUpsertOptions,\n): Promise<void> {\n if (!data.length) {\n return;\n }\n\n const { bearerToken, path, storageKey, nativeScryptCrypto } = opts;\n\n const encryptedData: string[][] = [];\n\n for (const d of data) {\n encryptedData.push([\n createSHA256Hash(d[0] + storageKey),\n await encryption.encryptString(d[1], opts.storageKey, nativeScryptCrypto),\n ]);\n }\n\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);\n\n const formattedData = Object.fromEntries(encryptedData);\n\n const res = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n body: JSON.stringify({ data: formattedData }),\n });\n\n if (!res.ok) {\n throw new Error('user-storage - unable to batch upsert data');\n }\n}\n\n/**\n * User Storage Service - Delete all storage entries for a specific feature.\n *\n * @param opts - User Storage Options\n */\nexport async function deleteUserStorageAllFeatureEntries(\n opts: UserStorageAllFeatureEntriesOptions,\n): Promise<void> {\n const { bearerToken, path } = opts;\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);\n\n const userStorageResponse = await fetch(url.toString(), {\n method: 'DELETE',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n });\n\n // Acceptable error - since indicates feature does not exist.\n if (userStorageResponse.status === 404) {\n throw new Error('user-storage - feature not found');\n }\n\n if (userStorageResponse.status !== 200 || !userStorageResponse.ok) {\n throw new Error('user-storage - unable to delete data');\n }\n}\n"]}
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.handleMockUserStoragePut = exports.handleMockUserStorageGetAllFeatureEntries = exports.handleMockUserStorageGet = exports.MOCK_STORAGE_RESPONSE = exports.MOCK_NOTIFICATIONS_DATA_ENCRYPTED = exports.MOCK_NOTIFICATIONS_DATA = exports.MOCK_STORAGE_KEY = void 0;
|
|
6
|
+
exports.handleMockUserStorageDeleteAllFeatureEntries = exports.handleMockUserStoragePut = exports.handleMockUserStorageGetAllFeatureEntries = exports.handleMockUserStorageGet = exports.MOCK_STORAGE_RESPONSE = exports.MOCK_NOTIFICATIONS_DATA_ENCRYPTED = exports.MOCK_NOTIFICATIONS_DATA = exports.MOCK_STORAGE_KEY = void 0;
|
|
7
7
|
const nock_1 = __importDefault(require("nock"));
|
|
8
8
|
const encryption_1 = __importDefault(require("../../shared/encryption/index.cjs"));
|
|
9
9
|
const env_1 = require("../../shared/env.cjs");
|
|
@@ -57,4 +57,13 @@ const handleMockUserStoragePut = (mockReply, callback) => {
|
|
|
57
57
|
return mockEndpoint;
|
|
58
58
|
};
|
|
59
59
|
exports.handleMockUserStoragePut = handleMockUserStoragePut;
|
|
60
|
+
const handleMockUserStorageDeleteAllFeatureEntries = async (mockReply) => {
|
|
61
|
+
const reply = mockReply ?? { status: 204 };
|
|
62
|
+
const mockEndpoint = (0, nock_1.default)(MOCK_STORAGE_URL_ALL_FEATURE_ENTRIES)
|
|
63
|
+
.persist()
|
|
64
|
+
.delete('')
|
|
65
|
+
.reply(reply.status);
|
|
66
|
+
return mockEndpoint;
|
|
67
|
+
};
|
|
68
|
+
exports.handleMockUserStorageDeleteAllFeatureEntries = handleMockUserStorageDeleteAllFeatureEntries;
|
|
60
69
|
//# sourceMappingURL=mock-userstorage.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mock-userstorage.cjs","sourceRoot":"","sources":["../../../src/sdk/__fixtures__/mock-userstorage.ts"],"names":[],"mappings":";;;;;;AAAA,gDAAwB;AAExB,mFAAiD;AACjD,8CAAuC;AACvC,sDAA8C;AAO9C,sDAAsD;AACtD,MAAM,gBAAgB,GAAG,IAAA,0BAAW,EAClC,SAAG,CAAC,GAAG,EACP,qCAAqC,CACtC,CAAC;AACF,MAAM,oCAAoC,GAAG,IAAA,0BAAW,EACtD,SAAG,CAAC,GAAG,EACP,eAAe,CAChB,CAAC;AAEW,QAAA,gBAAgB,GAAG,kBAAkB,CAAC;AACnD,gFAAgF;AAChF,gEAAgE;AACnD,QAAA,uBAAuB,GAAG,EAAE,UAAU,EAAE,K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wBAAwB,GAAG,CACtC,SAAqB,EACrB,QAAiE,EACjE,EAAE;IACF,MAAM,KAAK,GAAG,SAAS,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;IAC3C,MAAM,YAAY,GAAG,IAAA,cAAI,EAAC,gBAAgB,CAAC;SACxC,OAAO,EAAE;SACT,GAAG,CAAC,KAAK,CAAC;SACV,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,WAAW,EAAE,EAAE;QAC9C,OAAO,MAAM,QAAQ,EAAE,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;IAC5C,CAAC,CAAC,CAAC;IAEL,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC;AAbW,QAAA,wBAAwB,4BAanC","sourcesContent":["import nock from 'nock';\n\nimport encryption from '../../shared/encryption';\nimport { Env } from '../../shared/env';\nimport { STORAGE_URL } from '../user-storage';\n\ntype MockReply = {\n status: nock.StatusCode;\n body?: nock.Body;\n};\n\n// Example mock notifications storage entry (wildcard)\nconst MOCK_STORAGE_URL = STORAGE_URL(\n Env.DEV,\n 'notifications/notification_settings',\n);\nconst MOCK_STORAGE_URL_ALL_FEATURE_ENTRIES = STORAGE_URL(\n Env.DEV,\n 'notifications',\n);\n\nexport const MOCK_STORAGE_KEY = 'MOCK_STORAGE_KEY';\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport const MOCK_NOTIFICATIONS_DATA = { is_compact: false };\nexport const MOCK_NOTIFICATIONS_DATA_ENCRYPTED = async () =>\n await encryption.encryptString(\n JSON.stringify(MOCK_NOTIFICATIONS_DATA),\n MOCK_STORAGE_KEY,\n );\n\nexport const MOCK_STORAGE_RESPONSE = async () => ({\n HashedKey: '8485d2c14c333ebca415140a276adaf546619b0efc204586b73a5d400a18a5e2',\n Data: await MOCK_NOTIFICATIONS_DATA_ENCRYPTED(),\n});\n\nexport const handleMockUserStorageGet = async (mockReply?: MockReply) => {\n const reply = mockReply ?? {\n status: 200,\n body: await MOCK_STORAGE_RESPONSE(),\n };\n const mockEndpoint = nock(MOCK_STORAGE_URL)\n .persist()\n .get(/.*/u)\n .reply(reply.status, reply.body);\n\n return mockEndpoint;\n};\n\nexport const handleMockUserStorageGetAllFeatureEntries = async (\n mockReply?: MockReply,\n) => {\n const reply = mockReply ?? {\n status: 200,\n body: [await MOCK_STORAGE_RESPONSE()],\n };\n const mockEndpoint = nock(MOCK_STORAGE_URL_ALL_FEATURE_ENTRIES)\n .persist()\n .get('')\n .reply(reply.status, reply.body);\n\n return mockEndpoint;\n};\n\nexport const handleMockUserStoragePut = (\n mockReply?: MockReply,\n callback?: (uri: string, requestBody: nock.Body) => Promise<void>,\n) => {\n const reply = mockReply ?? { status: 204 };\n const mockEndpoint = nock(MOCK_STORAGE_URL)\n .persist()\n .put(/.*/u)\n .reply(reply.status, async (uri, requestBody) => {\n return await callback?.(uri, requestBody);\n });\n\n return mockEndpoint;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"mock-userstorage.cjs","sourceRoot":"","sources":["../../../src/sdk/__fixtures__/mock-userstorage.ts"],"names":[],"mappings":";;;;;;AAAA,gDAAwB;AAExB,mFAAiD;AACjD,8CAAuC;AACvC,sDAA8C;AAO9C,sDAAsD;AACtD,MAAM,gBAAgB,GAAG,IAAA,0BAAW,EAClC,SAAG,CAAC,GAAG,EACP,qCAAqC,CACtC,CAAC;AACF,MAAM,oCAAoC,GAAG,IAAA,0BAAW,EACtD,SAAG,CAAC,GAAG,EACP,eAAe,CAChB,CAAC;AAEW,QAAA,gBAAgB,GAAG,kBAAkB,CAAC;AACnD,gFAAgF;AAChF,gEAAgE;AACnD,QAAA,uBAAuB,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC;AACtD,MAAM,iCAAiC,GAAG,KAAK,IAAI,EAAE,CAC1D,MAAM,oBAAU,CAAC,aAAa,CAC5B,IAAI,CAAC,SAAS,CAAC,+BAAuB,CAAC,EAC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,GAAG,IAAA,cAAI,EAAC,gBAAgB,CAAC;SACxC,OAAO,EAAE;SACT,GAAG,CAAC,KAAK,CAAC;SACV,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,WAAW,EAAE,EAAE;QAC9C,OAAO,MAAM,QAAQ,EAAE,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;IAC5C,CAAC,CAAC,CAAC;IAEL,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC;AAbW,QAAA,wBAAwB,4BAanC;AAEK,MAAM,4CAA4C,GAAG,KAAK,EAC/D,SAAqB,EACrB,EAAE;IACF,MAAM,KAAK,GAAG,SAAS,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;IAC3C,MAAM,YAAY,GAAG,IAAA,cAAI,EAAC,oCAAoC,CAAC;SAC5D,OAAO,EAAE;SACT,MAAM,CAAC,EAAE,CAAC;SACV,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAEvB,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC;AAVW,QAAA,4CAA4C,gDAUvD","sourcesContent":["import nock from 'nock';\n\nimport encryption from '../../shared/encryption';\nimport { Env } from '../../shared/env';\nimport { STORAGE_URL } from '../user-storage';\n\ntype MockReply = {\n status: nock.StatusCode;\n body?: nock.Body;\n};\n\n// Example mock notifications storage entry (wildcard)\nconst MOCK_STORAGE_URL = STORAGE_URL(\n Env.DEV,\n 'notifications/notification_settings',\n);\nconst MOCK_STORAGE_URL_ALL_FEATURE_ENTRIES = STORAGE_URL(\n Env.DEV,\n 'notifications',\n);\n\nexport const MOCK_STORAGE_KEY = 'MOCK_STORAGE_KEY';\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport const MOCK_NOTIFICATIONS_DATA = { is_compact: false };\nexport const MOCK_NOTIFICATIONS_DATA_ENCRYPTED = async () =>\n await encryption.encryptString(\n JSON.stringify(MOCK_NOTIFICATIONS_DATA),\n MOCK_STORAGE_KEY,\n );\n\nexport const MOCK_STORAGE_RESPONSE = async () => ({\n HashedKey: '8485d2c14c333ebca415140a276adaf546619b0efc204586b73a5d400a18a5e2',\n Data: await MOCK_NOTIFICATIONS_DATA_ENCRYPTED(),\n});\n\nexport const handleMockUserStorageGet = async (mockReply?: MockReply) => {\n const reply = mockReply ?? {\n status: 200,\n body: await MOCK_STORAGE_RESPONSE(),\n };\n const mockEndpoint = nock(MOCK_STORAGE_URL)\n .persist()\n .get(/.*/u)\n .reply(reply.status, reply.body);\n\n return mockEndpoint;\n};\n\nexport const handleMockUserStorageGetAllFeatureEntries = async (\n mockReply?: MockReply,\n) => {\n const reply = mockReply ?? {\n status: 200,\n body: [await MOCK_STORAGE_RESPONSE()],\n };\n const mockEndpoint = nock(MOCK_STORAGE_URL_ALL_FEATURE_ENTRIES)\n .persist()\n .get('')\n .reply(reply.status, reply.body);\n\n return mockEndpoint;\n};\n\nexport const handleMockUserStoragePut = (\n mockReply?: MockReply,\n callback?: (uri: string, requestBody: nock.Body) => Promise<void>,\n) => {\n const reply = mockReply ?? { status: 204 };\n const mockEndpoint = nock(MOCK_STORAGE_URL)\n .persist()\n .put(/.*/u)\n .reply(reply.status, async (uri, requestBody) => {\n return await callback?.(uri, requestBody);\n });\n\n return mockEndpoint;\n};\n\nexport const handleMockUserStorageDeleteAllFeatureEntries = async (\n mockReply?: MockReply,\n) => {\n const reply = mockReply ?? { status: 204 };\n const mockEndpoint = nock(MOCK_STORAGE_URL_ALL_FEATURE_ENTRIES)\n .persist()\n .delete('')\n .reply(reply.status);\n\n return mockEndpoint;\n};\n"]}
|
|
@@ -15,5 +15,6 @@ export declare const MOCK_STORAGE_RESPONSE: () => Promise<{
|
|
|
15
15
|
export declare const handleMockUserStorageGet: (mockReply?: MockReply) => Promise<nock.Scope>;
|
|
16
16
|
export declare const handleMockUserStorageGetAllFeatureEntries: (mockReply?: MockReply) => Promise<nock.Scope>;
|
|
17
17
|
export declare const handleMockUserStoragePut: (mockReply?: MockReply, callback?: ((uri: string, requestBody: nock.Body) => Promise<void>) | undefined) => nock.Scope;
|
|
18
|
+
export declare const handleMockUserStorageDeleteAllFeatureEntries: (mockReply?: MockReply) => Promise<nock.Scope>;
|
|
18
19
|
export {};
|
|
19
20
|
//# sourceMappingURL=mock-userstorage.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mock-userstorage.d.cts","sourceRoot":"","sources":["../../../src/sdk/__fixtures__/mock-userstorage.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,aAAa;AAMxB,KAAK,SAAS,GAAG;IACf,MAAM,EAAE,IAAI,CAAC,UAAU,CAAC;IACxB,IAAI,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC;CAClB,CAAC;AAYF,eAAO,MAAM,gBAAgB,qBAAqB,CAAC;AAGnD,eAAO,MAAM,uBAAuB;;CAAwB,CAAC;AAC7D,eAAO,MAAM,iCAAiC,uBAI3C,CAAC;AAEJ,eAAO,MAAM,qBAAqB;;;EAGhC,CAAC;AAEH,eAAO,MAAM,wBAAwB,eAAsB,SAAS,wBAWnE,CAAC;AAEF,eAAO,MAAM,yCAAyC,eACxC,SAAS,wBAYtB,CAAC;AAEF,eAAO,MAAM,wBAAwB,eACvB,SAAS,oBACJ,MAAM,eAAe,SAAS,KAAK,QAAQ,IAAI,CAAC,4BAWlE,CAAC"}
|
|
1
|
+
{"version":3,"file":"mock-userstorage.d.cts","sourceRoot":"","sources":["../../../src/sdk/__fixtures__/mock-userstorage.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,aAAa;AAMxB,KAAK,SAAS,GAAG;IACf,MAAM,EAAE,IAAI,CAAC,UAAU,CAAC;IACxB,IAAI,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC;CAClB,CAAC;AAYF,eAAO,MAAM,gBAAgB,qBAAqB,CAAC;AAGnD,eAAO,MAAM,uBAAuB;;CAAwB,CAAC;AAC7D,eAAO,MAAM,iCAAiC,uBAI3C,CAAC;AAEJ,eAAO,MAAM,qBAAqB;;;EAGhC,CAAC;AAEH,eAAO,MAAM,wBAAwB,eAAsB,SAAS,wBAWnE,CAAC;AAEF,eAAO,MAAM,yCAAyC,eACxC,SAAS,wBAYtB,CAAC;AAEF,eAAO,MAAM,wBAAwB,eACvB,SAAS,oBACJ,MAAM,eAAe,SAAS,KAAK,QAAQ,IAAI,CAAC,4BAWlE,CAAC;AAEF,eAAO,MAAM,4CAA4C,eAC3C,SAAS,wBAStB,CAAC"}
|
|
@@ -15,5 +15,6 @@ export declare const MOCK_STORAGE_RESPONSE: () => Promise<{
|
|
|
15
15
|
export declare const handleMockUserStorageGet: (mockReply?: MockReply) => Promise<nock.Scope>;
|
|
16
16
|
export declare const handleMockUserStorageGetAllFeatureEntries: (mockReply?: MockReply) => Promise<nock.Scope>;
|
|
17
17
|
export declare const handleMockUserStoragePut: (mockReply?: MockReply, callback?: ((uri: string, requestBody: nock.Body) => Promise<void>) | undefined) => nock.Scope;
|
|
18
|
+
export declare const handleMockUserStorageDeleteAllFeatureEntries: (mockReply?: MockReply) => Promise<nock.Scope>;
|
|
18
19
|
export {};
|
|
19
20
|
//# sourceMappingURL=mock-userstorage.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mock-userstorage.d.mts","sourceRoot":"","sources":["../../../src/sdk/__fixtures__/mock-userstorage.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,aAAa;AAMxB,KAAK,SAAS,GAAG;IACf,MAAM,EAAE,IAAI,CAAC,UAAU,CAAC;IACxB,IAAI,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC;CAClB,CAAC;AAYF,eAAO,MAAM,gBAAgB,qBAAqB,CAAC;AAGnD,eAAO,MAAM,uBAAuB;;CAAwB,CAAC;AAC7D,eAAO,MAAM,iCAAiC,uBAI3C,CAAC;AAEJ,eAAO,MAAM,qBAAqB;;;EAGhC,CAAC;AAEH,eAAO,MAAM,wBAAwB,eAAsB,SAAS,wBAWnE,CAAC;AAEF,eAAO,MAAM,yCAAyC,eACxC,SAAS,wBAYtB,CAAC;AAEF,eAAO,MAAM,wBAAwB,eACvB,SAAS,oBACJ,MAAM,eAAe,SAAS,KAAK,QAAQ,IAAI,CAAC,4BAWlE,CAAC"}
|
|
1
|
+
{"version":3,"file":"mock-userstorage.d.mts","sourceRoot":"","sources":["../../../src/sdk/__fixtures__/mock-userstorage.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,aAAa;AAMxB,KAAK,SAAS,GAAG;IACf,MAAM,EAAE,IAAI,CAAC,UAAU,CAAC;IACxB,IAAI,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC;CAClB,CAAC;AAYF,eAAO,MAAM,gBAAgB,qBAAqB,CAAC;AAGnD,eAAO,MAAM,uBAAuB;;CAAwB,CAAC;AAC7D,eAAO,MAAM,iCAAiC,uBAI3C,CAAC;AAEJ,eAAO,MAAM,qBAAqB;;;EAGhC,CAAC;AAEH,eAAO,MAAM,wBAAwB,eAAsB,SAAS,wBAWnE,CAAC;AAEF,eAAO,MAAM,yCAAyC,eACxC,SAAS,wBAYtB,CAAC;AAEF,eAAO,MAAM,wBAAwB,eACvB,SAAS,oBACJ,MAAM,eAAe,SAAS,KAAK,QAAQ,IAAI,CAAC,4BAWlE,CAAC;AAEF,eAAO,MAAM,4CAA4C,eAC3C,SAAS,wBAStB,CAAC"}
|
|
@@ -53,4 +53,12 @@ export const handleMockUserStoragePut = (mockReply, callback) => {
|
|
|
53
53
|
});
|
|
54
54
|
return mockEndpoint;
|
|
55
55
|
};
|
|
56
|
+
export const handleMockUserStorageDeleteAllFeatureEntries = async (mockReply) => {
|
|
57
|
+
const reply = mockReply ?? { status: 204 };
|
|
58
|
+
const mockEndpoint = nock(MOCK_STORAGE_URL_ALL_FEATURE_ENTRIES)
|
|
59
|
+
.persist()
|
|
60
|
+
.delete('')
|
|
61
|
+
.reply(reply.status);
|
|
62
|
+
return mockEndpoint;
|
|
63
|
+
};
|
|
56
64
|
//# sourceMappingURL=mock-userstorage.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mock-userstorage.mjs","sourceRoot":"","sources":["../../../src/sdk/__fixtures__/mock-userstorage.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,KAAI,aAAa;;AAExB,OAAO,UAAU,0CAAgC;AACjD,OAAO,EAAE,GAAG,EAAE,6BAAyB;AACvC,OAAO,EAAE,WAAW,EAAE,4BAAwB;AAO9C,sDAAsD;AACtD,MAAM,gBAAgB,GAAG,WAAW,CAClC,GAAG,CAAC,GAAG,EACP,qCAAqC,CACtC,CAAC;AACF,MAAM,oCAAoC,GAAG,WAAW,CACtD,GAAG,CAAC,GAAG,EACP,eAAe,CAChB,CAAC;AAEF,MAAM,CAAC,MAAM,gBAAgB,GAAG,kBAAkB,CAAC;AACnD,gFAAgF;AAChF,gEAAgE;AAChE,MAAM,CAAC,MAAM,uBAAuB,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC;AAC7D,MAAM,CAAC,MAAM,iCAAiC,GAAG,KAAK,IAAI,EAAE,CAC1D,MAAM,UAAU,CAAC,aAAa,CAC5B,IAAI,CAAC,SAAS,CAAC,uBAAuB,CAAC,EACvC,gBAAgB,CACjB,CAAC;AAEJ,MAAM,CAAC,MAAM,qBAAqB,GAAG,KAAK,IAAI,EAAE,CAAC,CAAC;IAChD,SAAS,EAAE,kEAAkE;IAC7E,IAAI,EAAE,MAAM,iCAAiC,EAAE;CAChD,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,wBAAwB,GAAG,KAAK,EAAE,SAAqB,EAAE,EAAE;IACtE,MAAM,KAAK,GAAG,SAAS,IAAI;QACzB,MAAM,EAAE,GAAG;QACX,IAAI,EAAE,MAAM,qBAAqB,EAAE;KACpC,CAAC;IACF,MAAM,YAAY,GAAG,IAAI,CAAC,gBAAgB,CAAC;SACxC,OAAO,EAAE;SACT,GAAG,CAAC,KAAK,CAAC;SACV,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;IAEnC,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,yCAAyC,GAAG,KAAK,EAC5D,SAAqB,EACrB,EAAE;IACF,MAAM,KAAK,GAAG,SAAS,IAAI;QACzB,MAAM,EAAE,GAAG;QACX,IAAI,EAAE,CAAC,MAAM,qBAAqB,EAAE,CAAC;KACtC,CAAC;IACF,MAAM,YAAY,GAAG,IAAI,CAAC,oCAAoC,CAAC;SAC5D,OAAO,EAAE;SACT,GAAG,CAAC,EAAE,CAAC;SACP,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;IAEnC,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,wBAAwB,GAAG,CACtC,SAAqB,EACrB,QAAiE,EACjE,EAAE;IACF,MAAM,KAAK,GAAG,SAAS,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;IAC3C,MAAM,YAAY,GAAG,IAAI,CAAC,gBAAgB,CAAC;SACxC,OAAO,EAAE;SACT,GAAG,CAAC,KAAK,CAAC;SACV,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,WAAW,EAAE,EAAE;QAC9C,OAAO,MAAM,QAAQ,EAAE,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;IAC5C,CAAC,CAAC,CAAC;IAEL,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC","sourcesContent":["import nock from 'nock';\n\nimport encryption from '../../shared/encryption';\nimport { Env } from '../../shared/env';\nimport { STORAGE_URL } from '../user-storage';\n\ntype MockReply = {\n status: nock.StatusCode;\n body?: nock.Body;\n};\n\n// Example mock notifications storage entry (wildcard)\nconst MOCK_STORAGE_URL = STORAGE_URL(\n Env.DEV,\n 'notifications/notification_settings',\n);\nconst MOCK_STORAGE_URL_ALL_FEATURE_ENTRIES = STORAGE_URL(\n Env.DEV,\n 'notifications',\n);\n\nexport const MOCK_STORAGE_KEY = 'MOCK_STORAGE_KEY';\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport const MOCK_NOTIFICATIONS_DATA = { is_compact: false };\nexport const MOCK_NOTIFICATIONS_DATA_ENCRYPTED = async () =>\n await encryption.encryptString(\n JSON.stringify(MOCK_NOTIFICATIONS_DATA),\n MOCK_STORAGE_KEY,\n );\n\nexport const MOCK_STORAGE_RESPONSE = async () => ({\n HashedKey: '8485d2c14c333ebca415140a276adaf546619b0efc204586b73a5d400a18a5e2',\n Data: await MOCK_NOTIFICATIONS_DATA_ENCRYPTED(),\n});\n\nexport const handleMockUserStorageGet = async (mockReply?: MockReply) => {\n const reply = mockReply ?? {\n status: 200,\n body: await MOCK_STORAGE_RESPONSE(),\n };\n const mockEndpoint = nock(MOCK_STORAGE_URL)\n .persist()\n .get(/.*/u)\n .reply(reply.status, reply.body);\n\n return mockEndpoint;\n};\n\nexport const handleMockUserStorageGetAllFeatureEntries = async (\n mockReply?: MockReply,\n) => {\n const reply = mockReply ?? {\n status: 200,\n body: [await MOCK_STORAGE_RESPONSE()],\n };\n const mockEndpoint = nock(MOCK_STORAGE_URL_ALL_FEATURE_ENTRIES)\n .persist()\n .get('')\n .reply(reply.status, reply.body);\n\n return mockEndpoint;\n};\n\nexport const handleMockUserStoragePut = (\n mockReply?: MockReply,\n callback?: (uri: string, requestBody: nock.Body) => Promise<void>,\n) => {\n const reply = mockReply ?? { status: 204 };\n const mockEndpoint = nock(MOCK_STORAGE_URL)\n .persist()\n .put(/.*/u)\n .reply(reply.status, async (uri, requestBody) => {\n return await callback?.(uri, requestBody);\n });\n\n return mockEndpoint;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"mock-userstorage.mjs","sourceRoot":"","sources":["../../../src/sdk/__fixtures__/mock-userstorage.ts"],"names":[],"mappings":";;;;;;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AAE;IACF,MAAM,KAAK,GAAG,SAAS,IAAI;QACzB,MAAM,EAAE,GAAG;QACX,IAAI,EAAE,CAAC,MAAM,qBAAqB,EAAE,CAAC;KACtC,CAAC;IACF,MAAM,YAAY,GAAG,IAAI,CAAC,oCAAoC,CAAC;SAC5D,OAAO,EAAE;SACT,GAAG,CAAC,EAAE,CAAC;SACP,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;IAEnC,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,wBAAwB,GAAG,CACtC,SAAqB,EACrB,QAAiE,EACjE,EAAE;IACF,MAAM,KAAK,GAAG,SAAS,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;IAC3C,MAAM,YAAY,GAAG,IAAI,CAAC,gBAAgB,CAAC;SACxC,OAAO,EAAE;SACT,GAAG,CAAC,KAAK,CAAC;SACV,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,WAAW,EAAE,EAAE;QAC9C,OAAO,MAAM,QAAQ,EAAE,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;IAC5C,CAAC,CAAC,CAAC;IAEL,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,4CAA4C,GAAG,KAAK,EAC/D,SAAqB,EACrB,EAAE;IACF,MAAM,KAAK,GAAG,SAAS,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;IAC3C,MAAM,YAAY,GAAG,IAAI,CAAC,oCAAoC,CAAC;SAC5D,OAAO,EAAE;SACT,MAAM,CAAC,EAAE,CAAC;SACV,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAEvB,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC","sourcesContent":["import nock from 'nock';\n\nimport encryption from '../../shared/encryption';\nimport { Env } from '../../shared/env';\nimport { STORAGE_URL } from '../user-storage';\n\ntype MockReply = {\n status: nock.StatusCode;\n body?: nock.Body;\n};\n\n// Example mock notifications storage entry (wildcard)\nconst MOCK_STORAGE_URL = STORAGE_URL(\n Env.DEV,\n 'notifications/notification_settings',\n);\nconst MOCK_STORAGE_URL_ALL_FEATURE_ENTRIES = STORAGE_URL(\n Env.DEV,\n 'notifications',\n);\n\nexport const MOCK_STORAGE_KEY = 'MOCK_STORAGE_KEY';\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport const MOCK_NOTIFICATIONS_DATA = { is_compact: false };\nexport const MOCK_NOTIFICATIONS_DATA_ENCRYPTED = async () =>\n await encryption.encryptString(\n JSON.stringify(MOCK_NOTIFICATIONS_DATA),\n MOCK_STORAGE_KEY,\n );\n\nexport const MOCK_STORAGE_RESPONSE = async () => ({\n HashedKey: '8485d2c14c333ebca415140a276adaf546619b0efc204586b73a5d400a18a5e2',\n Data: await MOCK_NOTIFICATIONS_DATA_ENCRYPTED(),\n});\n\nexport const handleMockUserStorageGet = async (mockReply?: MockReply) => {\n const reply = mockReply ?? {\n status: 200,\n body: await MOCK_STORAGE_RESPONSE(),\n };\n const mockEndpoint = nock(MOCK_STORAGE_URL)\n .persist()\n .get(/.*/u)\n .reply(reply.status, reply.body);\n\n return mockEndpoint;\n};\n\nexport const handleMockUserStorageGetAllFeatureEntries = async (\n mockReply?: MockReply,\n) => {\n const reply = mockReply ?? {\n status: 200,\n body: [await MOCK_STORAGE_RESPONSE()],\n };\n const mockEndpoint = nock(MOCK_STORAGE_URL_ALL_FEATURE_ENTRIES)\n .persist()\n .get('')\n .reply(reply.status, reply.body);\n\n return mockEndpoint;\n};\n\nexport const handleMockUserStoragePut = (\n mockReply?: MockReply,\n callback?: (uri: string, requestBody: nock.Body) => Promise<void>,\n) => {\n const reply = mockReply ?? { status: 204 };\n const mockEndpoint = nock(MOCK_STORAGE_URL)\n .persist()\n .put(/.*/u)\n .reply(reply.status, async (uri, requestBody) => {\n return await callback?.(uri, requestBody);\n });\n\n return mockEndpoint;\n};\n\nexport const handleMockUserStorageDeleteAllFeatureEntries = async (\n mockReply?: MockReply,\n) => {\n const reply = mockReply ?? { status: 204 };\n const mockEndpoint = nock(MOCK_STORAGE_URL_ALL_FEATURE_ENTRIES)\n .persist()\n .delete('')\n .reply(reply.status);\n\n return mockEndpoint;\n};\n"]}
|
|
@@ -27,7 +27,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
27
27
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
28
28
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
29
29
|
};
|
|
30
|
-
var _UserStorage_instances, _UserStorage_upsertUserStorage, _UserStorage_batchUpsertUserStorage, _UserStorage_getUserStorage, _UserStorage_getUserStorageAllFeatureEntries, _UserStorage_createEntryKey, _UserStorage_getAuthorizationHeader;
|
|
30
|
+
var _UserStorage_instances, _UserStorage_upsertUserStorage, _UserStorage_batchUpsertUserStorage, _UserStorage_getUserStorage, _UserStorage_getUserStorageAllFeatureEntries, _UserStorage_deleteUserStorageAllFeatureEntries, _UserStorage_createEntryKey, _UserStorage_getAuthorizationHeader;
|
|
31
31
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
32
|
exports.UserStorage = exports.STORAGE_URL = void 0;
|
|
33
33
|
const encryption_1 = __importStar(require("../shared/encryption/index.cjs"));
|
|
@@ -55,6 +55,9 @@ class UserStorage {
|
|
|
55
55
|
async getAllFeatureItems(path) {
|
|
56
56
|
return __classPrivateFieldGet(this, _UserStorage_instances, "m", _UserStorage_getUserStorageAllFeatureEntries).call(this, path);
|
|
57
57
|
}
|
|
58
|
+
async deleteAllFeatureItems(path) {
|
|
59
|
+
return __classPrivateFieldGet(this, _UserStorage_instances, "m", _UserStorage_deleteUserStorageAllFeatureEntries).call(this, path);
|
|
60
|
+
}
|
|
58
61
|
async getStorageKey() {
|
|
59
62
|
const storageKey = await this.options.storage?.getStorageKey();
|
|
60
63
|
if (storageKey) {
|
|
@@ -201,6 +204,33 @@ _UserStorage_instances = new WeakSet(), _UserStorage_upsertUserStorage = async f
|
|
|
201
204
|
const errorMessage = e instanceof Error ? e.message : JSON.stringify(e ?? '');
|
|
202
205
|
throw new errors_1.UserStorageError(`failed to get user storage for path '${path}'. ${errorMessage}`);
|
|
203
206
|
}
|
|
207
|
+
}, _UserStorage_deleteUserStorageAllFeatureEntries = async function _UserStorage_deleteUserStorageAllFeatureEntries(path) {
|
|
208
|
+
try {
|
|
209
|
+
const headers = await __classPrivateFieldGet(this, _UserStorage_instances, "m", _UserStorage_getAuthorizationHeader).call(this);
|
|
210
|
+
const url = new URL((0, exports.STORAGE_URL)(this.env, path));
|
|
211
|
+
const response = await fetch(url.toString(), {
|
|
212
|
+
method: 'DELETE',
|
|
213
|
+
headers: {
|
|
214
|
+
'Content-Type': 'application/json',
|
|
215
|
+
...headers,
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
if (response.status === 404) {
|
|
219
|
+
throw new errors_1.NotFoundError(`feature not found for path '${path}'.`);
|
|
220
|
+
}
|
|
221
|
+
if (!response.ok) {
|
|
222
|
+
const responseBody = (await response.json());
|
|
223
|
+
throw new Error(`HTTP error message: ${responseBody.message}, error: ${responseBody.error}`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
catch (e) {
|
|
227
|
+
if (e instanceof errors_1.NotFoundError) {
|
|
228
|
+
throw e;
|
|
229
|
+
}
|
|
230
|
+
/* istanbul ignore next */
|
|
231
|
+
const errorMessage = e instanceof Error ? e.message : JSON.stringify(e ?? '');
|
|
232
|
+
throw new errors_1.UserStorageError(`failed to delete user storage for path '${path}'. ${errorMessage}`);
|
|
233
|
+
}
|
|
204
234
|
}, _UserStorage_createEntryKey = function _UserStorage_createEntryKey(key, storageKey) {
|
|
205
235
|
const hashedKey = (0, encryption_1.createSHA256Hash)(key + storageKey);
|
|
206
236
|
return hashedKey;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user-storage.cjs","sourceRoot":"","sources":["../../src/sdk/user-storage.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6EAAoE;AAEpE,2CAA2C;AAM3C,iEAA2D;AAE3D,yCAA2D;AAEpD,MAAM,WAAW,GAAG,CAAC,GAAQ,EAAE,aAAqB,EAAE,EAAE,CAC7D,GAAG,IAAA,gBAAU,EAAC,GAAG,CAAC,CAAC,iBAAiB,uBAAuB,aAAa,EAAE,CAAC;AADhE,QAAA,WAAW,eACqD;AA4B7E,MAAa,WAAW;IAOtB,YAAY,MAAyB,EAAE,OAA2B;;QAChE,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC;QACtB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,OAAO,CACX,IAAsC,EACtC,KAAa;QAEb,MAAM,uBAAA,IAAI,8DAAmB,MAAvB,IAAI,EAAoB,IAAI,EAAE,KAAK,CAAC,CAAC;IAC7C,CAAC;IAED,KAAK,CAAC,aAAa,CACjB,IAAoC,EACpC,MAA8C;QAE9C,MAAM,uBAAA,IAAI,mEAAwB,MAA5B,IAAI,EAAyB,IAAI,EAAE,MAAM,CAAC,CAAC;IACnD,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,IAAsC;QAClD,OAAO,uBAAA,IAAI,2DAAgB,MAApB,IAAI,EAAiB,IAAI,CAAC,CAAC;IACpC,CAAC;IAED,KAAK,CAAC,kBAAkB,CACtB,IAAoC;QAEpC,OAAO,uBAAA,IAAI,4EAAiC,MAArC,IAAI,EAAkC,IAAI,CAAC,CAAC;IACrD,CAAC;IAED,KAAK,CAAC,aAAa;QACjB,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,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AAC,CAAC,CAAC,SAAS,CAAC,CAAC;aAC5D,MAAM,CAAC,CAAC,CAAC,EAAe,EAAE,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC;KAChD;IAAC,OAAO,CAAC,EAAE;QACV,IAAI,CAAC,YAAY,sBAAa,EAAE;YAC9B,MAAM,CAAC,CAAC;SACT;QAED,0BAA0B;QAC1B,MAAM,YAAY,GAChB,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;QAE3D,MAAM,IAAI,yBAAgB,CACxB,wCAAwC,IAAI,MAAM,YAAY,EAAE,CACjE,CAAC;KACH;AACH,CAAC,qEAEe,GAAW,EAAE,UAAkB;IAC7C,MAAM,SAAS,GAAG,IAAA,6BAAgB,EAAC,GAAG,GAAG,UAAU,CAAC,CAAC;IACrD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,gFAAgF;AAChF,gEAAgE;AAChE,KAAK;IACH,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,CAAC;IAC5D,OAAO,EAAE,aAAa,EAAE,UAAU,WAAW,EAAE,EAAE,CAAC;AACpD,CAAC","sourcesContent":["import encryption, { createSHA256Hash } from '../shared/encryption';\nimport type { Env } from '../shared/env';\nimport { getEnvUrls } from '../shared/env';\nimport type {\n UserStoragePathWithFeatureAndKey,\n UserStoragePathWithFeatureOnly,\n UserStoragePathWithKeyOnly,\n} from '../shared/storage-schema';\nimport { createEntryPath } from '../shared/storage-schema';\nimport type { IBaseAuth } from './authentication-jwt-bearer/types';\nimport { NotFoundError, UserStorageError } from './errors';\n\nexport const STORAGE_URL = (env: Env, encryptedPath: string) =>\n `${getEnvUrls(env).userStorageApiUrl}/api/v1/userstorage/${encryptedPath}`;\n\nexport type UserStorageConfig = {\n env: Env;\n auth: Pick<IBaseAuth, 'getAccessToken' | 'getUserProfile' | 'signMessage'>;\n};\n\nexport type StorageOptions = {\n getStorageKey: () => Promise<string | null>;\n setStorageKey: (val: string) => Promise<void>;\n};\n\nexport type UserStorageOptions = {\n storage?: StorageOptions;\n};\n\ntype GetUserStorageAllFeatureEntriesResponse = {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n HashedKey: string;\n // eslint-disable-next-line @typescript-eslint/naming-convention\n Data: string;\n}[];\n\ntype ErrorMessage = {\n message: string;\n error: string;\n};\n\nexport class UserStorage {\n protected config: UserStorageConfig;\n\n protected options: UserStorageOptions;\n\n protected env: Env;\n\n constructor(config: UserStorageConfig, options: UserStorageOptions) {\n this.env = config.env;\n this.config = config;\n this.options = options;\n }\n\n async setItem(\n path: UserStoragePathWithFeatureAndKey,\n value: string,\n ): Promise<void> {\n await this.#upsertUserStorage(path, value);\n }\n\n async batchSetItems(\n path: UserStoragePathWithFeatureOnly,\n values: [UserStoragePathWithKeyOnly, string][],\n ) {\n await this.#batchUpsertUserStorage(path, values);\n }\n\n async getItem(path: UserStoragePathWithFeatureAndKey): Promise<string> {\n return this.#getUserStorage(path);\n }\n\n async getAllFeatureItems(\n path: UserStoragePathWithFeatureOnly,\n ): Promise<string[] | null> {\n return this.#getUserStorageAllFeatureEntries(path);\n }\n\n async getStorageKey(): Promise<string> {\n const storageKey = await this.options.storage?.getStorageKey();\n if (storageKey) {\n return storageKey;\n }\n\n const userProfile = await this.config.auth.getUserProfile();\n const storageKeySignature = await this.config.auth.signMessage(\n `metamask:${userProfile.profileId}`,\n );\n const hashedStorageKeySignature = createSHA256Hash(storageKeySignature);\n await this.options.storage?.setStorageKey(hashedStorageKeySignature);\n return hashedStorageKeySignature;\n }\n\n async #upsertUserStorage(\n path: UserStoragePathWithFeatureAndKey,\n data: string,\n ): Promise<void> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n const encryptedData = await encryption.encryptString(data, storageKey);\n const encryptedPath = createEntryPath(path, storageKey);\n\n const url = new URL(STORAGE_URL(this.env, encryptedPath));\n\n const response = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({ data: encryptedData }),\n });\n\n if (!response.ok) {\n const responseBody: ErrorMessage = await response.json().catch(() => ({\n message: 'unknown',\n error: 'unknown',\n }));\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n throw new UserStorageError(\n `failed to upsert user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #batchUpsertUserStorage(\n path: UserStoragePathWithFeatureOnly,\n data: [UserStoragePathWithKeyOnly, string][],\n ): Promise<void> {\n try {\n if (!data.length) {\n return;\n }\n\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n\n const encryptedData = await Promise.all(\n data.map(async (d) => {\n return [\n this.#createEntryKey(d[0], storageKey),\n await encryption.encryptString(d[1], storageKey),\n ];\n }),\n );\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({ data: Object.fromEntries(encryptedData) }),\n });\n\n if (!response.ok) {\n const responseBody: ErrorMessage = await response.json().catch(() => ({\n message: 'unknown',\n error: 'unknown',\n }));\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n throw new UserStorageError(\n `failed to batch upsert user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #getUserStorage(\n path: UserStoragePathWithFeatureAndKey,\n ): Promise<string> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n const encryptedPath = createEntryPath(path, storageKey);\n\n const url = new URL(STORAGE_URL(this.env, encryptedPath));\n\n const response = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(\n `feature/key set not found for path '${path}'.`,\n );\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n\n const { Data: encryptedData } = await response.json();\n return encryption.decryptString(encryptedData, storageKey);\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to get user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #getUserStorageAllFeatureEntries(\n path: UserStoragePathWithFeatureOnly,\n ): Promise<string[] | null> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(`feature not found for path '${path}'.`);\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n\n const userStorage: GetUserStorageAllFeatureEntriesResponse | null =\n await response.json();\n\n if (!Array.isArray(userStorage)) {\n return null;\n }\n\n const decryptedData = userStorage.flatMap((entry) => {\n if (!entry.Data) {\n return [];\n }\n\n return encryption.decryptString(entry.Data, storageKey);\n });\n\n return (await Promise.allSettled(decryptedData))\n .map((d) => (d.status === 'fulfilled' ? d.value : undefined))\n .filter((d): d is string => d !== undefined);\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to get user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n #createEntryKey(key: string, storageKey: string): string {\n const hashedKey = createSHA256Hash(key + storageKey);\n return hashedKey;\n }\n\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n async #getAuthorizationHeader(): Promise<{ Authorization: string }> {\n const accessToken = await this.config.auth.getAccessToken();\n return { Authorization: `Bearer ${accessToken}` };\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"user-storage.cjs","sourceRoot":"","sources":["../../src/sdk/user-storage.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6EAAoE;AAEpE,2CAA2C;AAM3C,iEAA2D;AAE3D,yCAA2D;AAEpD,MAAM,WAAW,GAAG,CAAC,GAAQ,EAAE,aAAqB,EAAE,EAAE,CAC7D,GAAG,IAAA,gBAAU,EAAC,GAAG,CAAC,CAAC,iBAAiB,uBAAuB,aAAa,EAAE,CAAC;AADhE,QAAA,WAAW,eACqD;AA4B7E,MAAa,WAAW;IAOtB,YAAY,MAAyB,EAAE,OAA2B;;QAChE,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC;QACtB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,OAAO,CACX,IAAsC,EACtC,KAAa;QAEb,MAAM,uBAAA,IAAI,8DAAmB,MAAvB,IAAI,EAAoB,IAAI,EAAE,KAAK,CAAC,CAAC;IAC7C,CAAC;IAED,KAAK,CAAC,aAAa,CACjB,IAAoC,EACpC,MAA8C;QAE9C,MAAM,uBAAA,IAAI,mEAAwB,MAA5B,IAAI,EAAyB,IAAI,EAAE,MAAM,CAAC,CAAC;IACnD,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,IAAsC;QAClD,OAAO,uBAAA,IAAI,2DAAgB,MAApB,IAAI,EA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F,CAAC,CAAC;QAEH,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE;YAC3B,MAAM,IAAI,sBAAa,CAAC,+BAA+B,IAAI,IAAI,CAAC,CAAC;SAClE;QAED,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;YAChB,MAAM,YAAY,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAiB,CAAC;YAC7D,MAAM,IAAI,KAAK,CACb,uBAAuB,YAAY,CAAC,OAAO,YAAY,YAAY,CAAC,KAAK,EAAE,CAC5E,CAAC;SACH;KACF;IAAC,OAAO,CAAC,EAAE;QACV,IAAI,CAAC,YAAY,sBAAa,EAAE;YAC9B,MAAM,CAAC,CAAC;SACT;QAED,0BAA0B;QAC1B,MAAM,YAAY,GAChB,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;QAE3D,MAAM,IAAI,yBAAgB,CACxB,2CAA2C,IAAI,MAAM,YAAY,EAAE,CACpE,CAAC;KACH;AACH,CAAC,qEAEe,GAAW,EAAE,UAAkB;IAC7C,MAAM,SAAS,GAAG,IAAA,6BAAgB,EAAC,GAAG,GAAG,UAAU,CAAC,CAAC;IACrD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,gFAAgF;AAChF,gEAAgE;AAChE,KAAK;IACH,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,CAAC;IAC5D,OAAO,EAAE,aAAa,EAAE,UAAU,WAAW,EAAE,EAAE,CAAC;AACpD,CAAC","sourcesContent":["import encryption, { createSHA256Hash } from '../shared/encryption';\nimport type { Env } from '../shared/env';\nimport { getEnvUrls } from '../shared/env';\nimport type {\n UserStoragePathWithFeatureAndKey,\n UserStoragePathWithFeatureOnly,\n UserStoragePathWithKeyOnly,\n} from '../shared/storage-schema';\nimport { createEntryPath } from '../shared/storage-schema';\nimport type { IBaseAuth } from './authentication-jwt-bearer/types';\nimport { NotFoundError, UserStorageError } from './errors';\n\nexport const STORAGE_URL = (env: Env, encryptedPath: string) =>\n `${getEnvUrls(env).userStorageApiUrl}/api/v1/userstorage/${encryptedPath}`;\n\nexport type UserStorageConfig = {\n env: Env;\n auth: Pick<IBaseAuth, 'getAccessToken' | 'getUserProfile' | 'signMessage'>;\n};\n\nexport type StorageOptions = {\n getStorageKey: () => Promise<string | null>;\n setStorageKey: (val: string) => Promise<void>;\n};\n\nexport type UserStorageOptions = {\n storage?: StorageOptions;\n};\n\ntype GetUserStorageAllFeatureEntriesResponse = {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n HashedKey: string;\n // eslint-disable-next-line @typescript-eslint/naming-convention\n Data: string;\n}[];\n\ntype ErrorMessage = {\n message: string;\n error: string;\n};\n\nexport class UserStorage {\n protected config: UserStorageConfig;\n\n protected options: UserStorageOptions;\n\n protected env: Env;\n\n constructor(config: UserStorageConfig, options: UserStorageOptions) {\n this.env = config.env;\n this.config = config;\n this.options = options;\n }\n\n async setItem(\n path: UserStoragePathWithFeatureAndKey,\n value: string,\n ): Promise<void> {\n await this.#upsertUserStorage(path, value);\n }\n\n async batchSetItems(\n path: UserStoragePathWithFeatureOnly,\n values: [UserStoragePathWithKeyOnly, string][],\n ) {\n await this.#batchUpsertUserStorage(path, values);\n }\n\n async getItem(path: UserStoragePathWithFeatureAndKey): Promise<string> {\n return this.#getUserStorage(path);\n }\n\n async getAllFeatureItems(\n path: UserStoragePathWithFeatureOnly,\n ): Promise<string[] | null> {\n return this.#getUserStorageAllFeatureEntries(path);\n }\n\n async deleteAllFeatureItems(\n path: UserStoragePathWithFeatureOnly,\n ): Promise<void> {\n return this.#deleteUserStorageAllFeatureEntries(path);\n }\n\n async getStorageKey(): Promise<string> {\n const storageKey = await this.options.storage?.getStorageKey();\n if (storageKey) {\n return storageKey;\n }\n\n const userProfile = await this.config.auth.getUserProfile();\n const storageKeySignature = await this.config.auth.signMessage(\n `metamask:${userProfile.profileId}`,\n );\n const hashedStorageKeySignature = createSHA256Hash(storageKeySignature);\n await this.options.storage?.setStorageKey(hashedStorageKeySignature);\n return hashedStorageKeySignature;\n }\n\n async #upsertUserStorage(\n path: UserStoragePathWithFeatureAndKey,\n data: string,\n ): Promise<void> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n const encryptedData = await encryption.encryptString(data, storageKey);\n const encryptedPath = createEntryPath(path, storageKey);\n\n const url = new URL(STORAGE_URL(this.env, encryptedPath));\n\n const response = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({ data: encryptedData }),\n });\n\n if (!response.ok) {\n const responseBody: ErrorMessage = await response.json().catch(() => ({\n message: 'unknown',\n error: 'unknown',\n }));\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n throw new UserStorageError(\n `failed to upsert user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #batchUpsertUserStorage(\n path: UserStoragePathWithFeatureOnly,\n data: [UserStoragePathWithKeyOnly, string][],\n ): Promise<void> {\n try {\n if (!data.length) {\n return;\n }\n\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n\n const encryptedData = await Promise.all(\n data.map(async (d) => {\n return [\n this.#createEntryKey(d[0], storageKey),\n await encryption.encryptString(d[1], storageKey),\n ];\n }),\n );\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({ data: Object.fromEntries(encryptedData) }),\n });\n\n if (!response.ok) {\n const responseBody: ErrorMessage = await response.json().catch(() => ({\n message: 'unknown',\n error: 'unknown',\n }));\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n throw new UserStorageError(\n `failed to batch upsert user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #getUserStorage(\n path: UserStoragePathWithFeatureAndKey,\n ): Promise<string> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n const encryptedPath = createEntryPath(path, storageKey);\n\n const url = new URL(STORAGE_URL(this.env, encryptedPath));\n\n const response = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(\n `feature/key set not found for path '${path}'.`,\n );\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n\n const { Data: encryptedData } = await response.json();\n return encryption.decryptString(encryptedData, storageKey);\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to get user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #getUserStorageAllFeatureEntries(\n path: UserStoragePathWithFeatureOnly,\n ): Promise<string[] | null> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(`feature not found for path '${path}'.`);\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n\n const userStorage: GetUserStorageAllFeatureEntriesResponse | null =\n await response.json();\n\n if (!Array.isArray(userStorage)) {\n return null;\n }\n\n const decryptedData = userStorage.flatMap((entry) => {\n if (!entry.Data) {\n return [];\n }\n\n return encryption.decryptString(entry.Data, storageKey);\n });\n\n return (await Promise.allSettled(decryptedData))\n .map((d) => (d.status === 'fulfilled' ? d.value : undefined))\n .filter((d): d is string => d !== undefined);\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to get user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #deleteUserStorageAllFeatureEntries(\n path: UserStoragePathWithFeatureOnly,\n ): Promise<void> {\n try {\n const headers = await this.#getAuthorizationHeader();\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n method: 'DELETE',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(`feature not found for path '${path}'.`);\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to delete user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n #createEntryKey(key: string, storageKey: string): string {\n const hashedKey = createSHA256Hash(key + storageKey);\n return hashedKey;\n }\n\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n async #getAuthorizationHeader(): Promise<{ Authorization: string }> {\n const accessToken = await this.config.auth.getAccessToken();\n return { Authorization: `Bearer ${accessToken}` };\n }\n}\n"]}
|
|
@@ -23,6 +23,7 @@ export declare class UserStorage {
|
|
|
23
23
|
batchSetItems(path: UserStoragePathWithFeatureOnly, values: [UserStoragePathWithKeyOnly, string][]): Promise<void>;
|
|
24
24
|
getItem(path: UserStoragePathWithFeatureAndKey): Promise<string>;
|
|
25
25
|
getAllFeatureItems(path: UserStoragePathWithFeatureOnly): Promise<string[] | null>;
|
|
26
|
+
deleteAllFeatureItems(path: UserStoragePathWithFeatureOnly): Promise<void>;
|
|
26
27
|
getStorageKey(): Promise<string>;
|
|
27
28
|
}
|
|
28
29
|
//# sourceMappingURL=user-storage.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user-storage.d.cts","sourceRoot":"","sources":["../../src/sdk/user-storage.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,0BAAsB;AAEzC,OAAO,KAAK,EACV,gCAAgC,EAChC,8BAA8B,EAC9B,0BAA0B,EAC3B,qCAAiC;AAElC,OAAO,KAAK,EAAE,SAAS,EAAE,8CAA0C;AAGnE,eAAO,MAAM,WAAW,QAAS,GAAG,iBAAiB,MAAM,WACiB,CAAC;AAE7E,MAAM,MAAM,iBAAiB,GAAG;IAC9B,GAAG,EAAE,GAAG,CAAC;IACT,IAAI,EAAE,IAAI,CAAC,SAAS,EAAE,gBAAgB,GAAG,gBAAgB,GAAG,aAAa,CAAC,CAAC;CAC5E,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,aAAa,EAAE,MAAM,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAC5C,aAAa,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/C,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,OAAO,CAAC,EAAE,cAAc,CAAC;CAC1B,CAAC;AAcF,qBAAa,WAAW;;IACtB,SAAS,CAAC,MAAM,EAAE,iBAAiB,CAAC;IAEpC,SAAS,CAAC,OAAO,EAAE,kBAAkB,CAAC;IAEtC,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC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S,CAAC,MAAM,EAAE,iBAAiB,CAAC;IAEpC,SAAS,CAAC,OAAO,EAAE,kBAAkB,CAAC;IAEtC,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC;gBAEP,MAAM,EAAE,iBAAiB,EAAE,OAAO,EAAE,kBAAkB;IAM5D,OAAO,CACX,IAAI,EAAE,gCAAgC,EACtC,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,IAAI,CAAC;IAIV,aAAa,CACjB,IAAI,EAAE,8BAA8B,EACpC,MAAM,EAAE,CAAC,0BAA0B,EAAE,MAAM,CAAC,EAAE;IAK1C,OAAO,CAAC,IAAI,EAAE,gCAAgC,GAAG,OAAO,CAAC,MAAM,CAAC;IAIhE,kBAAkB,CACtB,IAAI,EAAE,8BAA8B,GACnC,OAAO,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC;IAIrB,qBAAqB,CACzB,IAAI,EAAE,8BAA8B,GACnC,OAAO,CAAC,IAAI,CAAC;IAIV,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;CAyQvC"}
|
|
@@ -23,6 +23,7 @@ export declare class UserStorage {
|
|
|
23
23
|
batchSetItems(path: UserStoragePathWithFeatureOnly, values: [UserStoragePathWithKeyOnly, string][]): Promise<void>;
|
|
24
24
|
getItem(path: UserStoragePathWithFeatureAndKey): Promise<string>;
|
|
25
25
|
getAllFeatureItems(path: UserStoragePathWithFeatureOnly): Promise<string[] | null>;
|
|
26
|
+
deleteAllFeatureItems(path: UserStoragePathWithFeatureOnly): Promise<void>;
|
|
26
27
|
getStorageKey(): Promise<string>;
|
|
27
28
|
}
|
|
28
29
|
//# sourceMappingURL=user-storage.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user-storage.d.mts","sourceRoot":"","sources":["../../src/sdk/user-storage.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,0BAAsB;AAEzC,OAAO,KAAK,EACV,gCAAgC,EAChC,8BAA8B,EAC9B,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GAAG,IAAI,CAAC;IAIrB,qBAAqB,CACzB,IAAI,EAAE,8BAA8B,GACnC,OAAO,CAAC,IAAI,CAAC;IAIV,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;CAyQvC"}
|
|
@@ -3,7 +3,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
3
3
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
4
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
5
|
};
|
|
6
|
-
var _UserStorage_instances, _UserStorage_upsertUserStorage, _UserStorage_batchUpsertUserStorage, _UserStorage_getUserStorage, _UserStorage_getUserStorageAllFeatureEntries, _UserStorage_createEntryKey, _UserStorage_getAuthorizationHeader;
|
|
6
|
+
var _UserStorage_instances, _UserStorage_upsertUserStorage, _UserStorage_batchUpsertUserStorage, _UserStorage_getUserStorage, _UserStorage_getUserStorageAllFeatureEntries, _UserStorage_deleteUserStorageAllFeatureEntries, _UserStorage_createEntryKey, _UserStorage_getAuthorizationHeader;
|
|
7
7
|
import encryption, { createSHA256Hash } from "../shared/encryption/index.mjs";
|
|
8
8
|
import { getEnvUrls } from "../shared/env.mjs";
|
|
9
9
|
import { createEntryPath } from "../shared/storage-schema.mjs";
|
|
@@ -28,6 +28,9 @@ export class UserStorage {
|
|
|
28
28
|
async getAllFeatureItems(path) {
|
|
29
29
|
return __classPrivateFieldGet(this, _UserStorage_instances, "m", _UserStorage_getUserStorageAllFeatureEntries).call(this, path);
|
|
30
30
|
}
|
|
31
|
+
async deleteAllFeatureItems(path) {
|
|
32
|
+
return __classPrivateFieldGet(this, _UserStorage_instances, "m", _UserStorage_deleteUserStorageAllFeatureEntries).call(this, path);
|
|
33
|
+
}
|
|
31
34
|
async getStorageKey() {
|
|
32
35
|
const storageKey = await this.options.storage?.getStorageKey();
|
|
33
36
|
if (storageKey) {
|
|
@@ -173,6 +176,33 @@ _UserStorage_instances = new WeakSet(), _UserStorage_upsertUserStorage = async f
|
|
|
173
176
|
const errorMessage = e instanceof Error ? e.message : JSON.stringify(e ?? '');
|
|
174
177
|
throw new UserStorageError(`failed to get user storage for path '${path}'. ${errorMessage}`);
|
|
175
178
|
}
|
|
179
|
+
}, _UserStorage_deleteUserStorageAllFeatureEntries = async function _UserStorage_deleteUserStorageAllFeatureEntries(path) {
|
|
180
|
+
try {
|
|
181
|
+
const headers = await __classPrivateFieldGet(this, _UserStorage_instances, "m", _UserStorage_getAuthorizationHeader).call(this);
|
|
182
|
+
const url = new URL(STORAGE_URL(this.env, path));
|
|
183
|
+
const response = await fetch(url.toString(), {
|
|
184
|
+
method: 'DELETE',
|
|
185
|
+
headers: {
|
|
186
|
+
'Content-Type': 'application/json',
|
|
187
|
+
...headers,
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
if (response.status === 404) {
|
|
191
|
+
throw new NotFoundError(`feature not found for path '${path}'.`);
|
|
192
|
+
}
|
|
193
|
+
if (!response.ok) {
|
|
194
|
+
const responseBody = (await response.json());
|
|
195
|
+
throw new Error(`HTTP error message: ${responseBody.message}, error: ${responseBody.error}`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
catch (e) {
|
|
199
|
+
if (e instanceof NotFoundError) {
|
|
200
|
+
throw e;
|
|
201
|
+
}
|
|
202
|
+
/* istanbul ignore next */
|
|
203
|
+
const errorMessage = e instanceof Error ? e.message : JSON.stringify(e ?? '');
|
|
204
|
+
throw new UserStorageError(`failed to delete user storage for path '${path}'. ${errorMessage}`);
|
|
205
|
+
}
|
|
176
206
|
}, _UserStorage_createEntryKey = function _UserStorage_createEntryKey(key, storageKey) {
|
|
177
207
|
const hashedKey = createSHA256Hash(key + storageKey);
|
|
178
208
|
return hashedKey;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user-storage.mjs","sourceRoot":"","sources":["../../src/sdk/user-storage.ts"],"names":[],"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C,OAAO,CAAC,EAAE;QACV,IAAI,CAAC,YAAY,aAAa,EAAE;YAC9B,MAAM,CAAC,CAAC;SACT;QAED,0BAA0B;QAC1B,MAAM,YAAY,GAChB,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;QAE3D,MAAM,IAAI,gBAAgB,CACxB,wCAAwC,IAAI,MAAM,YAAY,EAAE,CACjE,CAAC;KACH;AACH,CAAC,qEAEe,GAAW,EAAE,UAAkB;IAC7C,MAAM,SAAS,GAAG,gBAAgB,CAAC,GAAG,GAAG,UAAU,CAAC,CAAC;IACrD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,gFAAgF;AAChF,gEAAgE;AAChE,KAAK;IACH,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,CAAC;IAC5D,OAAO,EAAE,aAAa,EAAE,UAAU,WAAW,EAAE,EAAE,CAAC;AACpD,CAAC","sourcesContent":["import encryption, { createSHA256Hash } from '../shared/encryption';\nimport type { Env } from '../shared/env';\nimport { getEnvUrls } from '../shared/env';\nimport type {\n UserStoragePathWithFeatureAndKey,\n UserStoragePathWithFeatureOnly,\n UserStoragePathWithKeyOnly,\n} from '../shared/storage-schema';\nimport { createEntryPath } from '../shared/storage-schema';\nimport type { IBaseAuth } from './authentication-jwt-bearer/types';\nimport { NotFoundError, UserStorageError } from './errors';\n\nexport const STORAGE_URL = (env: Env, encryptedPath: string) =>\n `${getEnvUrls(env).userStorageApiUrl}/api/v1/userstorage/${encryptedPath}`;\n\nexport type UserStorageConfig = {\n env: Env;\n auth: Pick<IBaseAuth, 'getAccessToken' | 'getUserProfile' | 'signMessage'>;\n};\n\nexport type StorageOptions = {\n getStorageKey: () => Promise<string | null>;\n setStorageKey: (val: string) => Promise<void>;\n};\n\nexport type UserStorageOptions = {\n storage?: StorageOptions;\n};\n\ntype GetUserStorageAllFeatureEntriesResponse = {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n HashedKey: string;\n // eslint-disable-next-line @typescript-eslint/naming-convention\n Data: string;\n}[];\n\ntype ErrorMessage = {\n message: string;\n error: string;\n};\n\nexport class UserStorage {\n protected config: UserStorageConfig;\n\n protected options: UserStorageOptions;\n\n protected env: Env;\n\n constructor(config: UserStorageConfig, options: UserStorageOptions) {\n this.env = config.env;\n this.config = config;\n this.options = options;\n }\n\n async setItem(\n path: UserStoragePathWithFeatureAndKey,\n value: string,\n ): Promise<void> {\n await this.#upsertUserStorage(path, value);\n }\n\n async batchSetItems(\n path: UserStoragePathWithFeatureOnly,\n values: [UserStoragePathWithKeyOnly, string][],\n ) {\n await this.#batchUpsertUserStorage(path, values);\n }\n\n async getItem(path: UserStoragePathWithFeatureAndKey): Promise<string> {\n return this.#getUserStorage(path);\n }\n\n async getAllFeatureItems(\n path: UserStoragePathWithFeatureOnly,\n ): Promise<string[] | null> {\n return this.#getUserStorageAllFeatureEntries(path);\n }\n\n async getStorageKey(): Promise<string> {\n const storageKey = await this.options.storage?.getStorageKey();\n if (storageKey) {\n return storageKey;\n }\n\n const userProfile = await this.config.auth.getUserProfile();\n const storageKeySignature = await this.config.auth.signMessage(\n `metamask:${userProfile.profileId}`,\n );\n const hashedStorageKeySignature = createSHA256Hash(storageKeySignature);\n await this.options.storage?.setStorageKey(hashedStorageKeySignature);\n return hashedStorageKeySignature;\n }\n\n async #upsertUserStorage(\n path: UserStoragePathWithFeatureAndKey,\n data: string,\n ): Promise<void> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n const encryptedData = await encryption.encryptString(data, storageKey);\n const encryptedPath = createEntryPath(path, storageKey);\n\n const url = new URL(STORAGE_URL(this.env, encryptedPath));\n\n const response = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({ data: encryptedData }),\n });\n\n if (!response.ok) {\n const responseBody: ErrorMessage = await response.json().catch(() => ({\n message: 'unknown',\n error: 'unknown',\n }));\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n throw new UserStorageError(\n `failed to upsert user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #batchUpsertUserStorage(\n path: UserStoragePathWithFeatureOnly,\n data: [UserStoragePathWithKeyOnly, string][],\n ): Promise<void> {\n try {\n if (!data.length) {\n return;\n }\n\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n\n const encryptedData = await Promise.all(\n data.map(async (d) => {\n return [\n this.#createEntryKey(d[0], storageKey),\n await encryption.encryptString(d[1], storageKey),\n ];\n }),\n );\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({ data: Object.fromEntries(encryptedData) }),\n });\n\n if (!response.ok) {\n const responseBody: ErrorMessage = await response.json().catch(() => ({\n message: 'unknown',\n error: 'unknown',\n }));\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n throw new UserStorageError(\n `failed to batch upsert user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #getUserStorage(\n path: UserStoragePathWithFeatureAndKey,\n ): Promise<string> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n const encryptedPath = createEntryPath(path, storageKey);\n\n const url = new URL(STORAGE_URL(this.env, encryptedPath));\n\n const response = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(\n `feature/key set not found for path '${path}'.`,\n );\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n\n const { Data: encryptedData } = await response.json();\n return encryption.decryptString(encryptedData, storageKey);\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to get user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #getUserStorageAllFeatureEntries(\n path: UserStoragePathWithFeatureOnly,\n ): Promise<string[] | null> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(`feature not found for path '${path}'.`);\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n\n const userStorage: GetUserStorageAllFeatureEntriesResponse | null =\n await response.json();\n\n if (!Array.isArray(userStorage)) {\n return null;\n }\n\n const decryptedData = userStorage.flatMap((entry) => {\n if (!entry.Data) {\n return [];\n }\n\n return encryption.decryptString(entry.Data, storageKey);\n });\n\n return (await Promise.allSettled(decryptedData))\n .map((d) => (d.status === 'fulfilled' ? d.value : undefined))\n .filter((d): d is string => d !== undefined);\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to get user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n #createEntryKey(key: string, storageKey: string): string {\n const hashedKey = createSHA256Hash(key + storageKey);\n return hashedKey;\n }\n\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n async #getAuthorizationHeader(): Promise<{ Authorization: string }> {\n const accessToken = await this.config.auth.getAccessToken();\n return { Authorization: `Bearer ${accessToken}` };\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"user-storage.mjs","sourceRoot":"","sources":["../../src/sdk/user-storage.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,UAAU,EAAE,EAAE,gBAAgB,EAAE,uCAA6B;AAEpE,OAAO,EAAE,UAAU,EAAE,0BAAsB;AAM3C,OAAO,EAAE,eAAe,EAAE,qCAAiC;AAE3D,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,qBAAiB;AAE3D,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,GAAQ,EAAE,aAAqB,EAAE,EAAE,CAC7D,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,iBAAiB,uBAAuB,aAAa,EAAE,CAAC;AA4B7E,MAAM,OAAO,WAAW;IAOtB,YAAY,MAAyB,EAAE,OAA2B;;QAChE,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC;QACtB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,OAAO,CACX,IAAsC,EACtC,KAAa;QAEb,MAAM,uBAAA,IAAI,8DAAmB,MAAvB,IAAI,EAAoB,IAAI,EAAE,KAAK,CAAC,CAAC;IAC7C,CAAC;IAED,KAAK,CAAC,aAAa,CACjB,IAAoC,EACpC,MAA8C;QAE9C,MAAM,uBAAA,IAAI,mEAAwB,MAA5B,IAAI,EAAyB,IAAI,EAAE,MAAM,CAAC,CAAC;IACnD,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,IAAsC;QAClD,OAAO,uBAAA,IAAI,2DAAgB,MAApB,IAAI,EAAiB,IAAI,CAAC,CAAC;IACpC,CAAC;IAED,KAAK,CAAC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E;QAED,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;YAChB,MAAM,YAAY,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAiB,CAAC;YAC7D,MAAM,IAAI,KAAK,CACb,uBAAuB,YAAY,CAAC,OAAO,YAAY,YAAY,CAAC,KAAK,EAAE,CAC5E,CAAC;SACH;KACF;IAAC,OAAO,CAAC,EAAE;QACV,IAAI,CAAC,YAAY,aAAa,EAAE;YAC9B,MAAM,CAAC,CAAC;SACT;QAED,0BAA0B;QAC1B,MAAM,YAAY,GAChB,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;QAE3D,MAAM,IAAI,gBAAgB,CACxB,2CAA2C,IAAI,MAAM,YAAY,EAAE,CACpE,CAAC;KACH;AACH,CAAC,qEAEe,GAAW,EAAE,UAAkB;IAC7C,MAAM,SAAS,GAAG,gBAAgB,CAAC,GAAG,GAAG,UAAU,CAAC,CAAC;IACrD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,gFAAgF;AAChF,gEAAgE;AAChE,KAAK;IACH,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,CAAC;IAC5D,OAAO,EAAE,aAAa,EAAE,UAAU,WAAW,EAAE,EAAE,CAAC;AACpD,CAAC","sourcesContent":["import encryption, { createSHA256Hash } from '../shared/encryption';\nimport type { Env } from '../shared/env';\nimport { getEnvUrls } from '../shared/env';\nimport type {\n UserStoragePathWithFeatureAndKey,\n UserStoragePathWithFeatureOnly,\n UserStoragePathWithKeyOnly,\n} from '../shared/storage-schema';\nimport { createEntryPath } from '../shared/storage-schema';\nimport type { IBaseAuth } from './authentication-jwt-bearer/types';\nimport { NotFoundError, UserStorageError } from './errors';\n\nexport const STORAGE_URL = (env: Env, encryptedPath: string) =>\n `${getEnvUrls(env).userStorageApiUrl}/api/v1/userstorage/${encryptedPath}`;\n\nexport type UserStorageConfig = {\n env: Env;\n auth: Pick<IBaseAuth, 'getAccessToken' | 'getUserProfile' | 'signMessage'>;\n};\n\nexport type StorageOptions = {\n getStorageKey: () => Promise<string | null>;\n setStorageKey: (val: string) => Promise<void>;\n};\n\nexport type UserStorageOptions = {\n storage?: StorageOptions;\n};\n\ntype GetUserStorageAllFeatureEntriesResponse = {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n HashedKey: string;\n // eslint-disable-next-line @typescript-eslint/naming-convention\n Data: string;\n}[];\n\ntype ErrorMessage = {\n message: string;\n error: string;\n};\n\nexport class UserStorage {\n protected config: UserStorageConfig;\n\n protected options: UserStorageOptions;\n\n protected env: Env;\n\n constructor(config: UserStorageConfig, options: UserStorageOptions) {\n this.env = config.env;\n this.config = config;\n this.options = options;\n }\n\n async setItem(\n path: UserStoragePathWithFeatureAndKey,\n value: string,\n ): Promise<void> {\n await this.#upsertUserStorage(path, value);\n }\n\n async batchSetItems(\n path: UserStoragePathWithFeatureOnly,\n values: [UserStoragePathWithKeyOnly, string][],\n ) {\n await this.#batchUpsertUserStorage(path, values);\n }\n\n async getItem(path: UserStoragePathWithFeatureAndKey): Promise<string> {\n return this.#getUserStorage(path);\n }\n\n async getAllFeatureItems(\n path: UserStoragePathWithFeatureOnly,\n ): Promise<string[] | null> {\n return this.#getUserStorageAllFeatureEntries(path);\n }\n\n async deleteAllFeatureItems(\n path: UserStoragePathWithFeatureOnly,\n ): Promise<void> {\n return this.#deleteUserStorageAllFeatureEntries(path);\n }\n\n async getStorageKey(): Promise<string> {\n const storageKey = await this.options.storage?.getStorageKey();\n if (storageKey) {\n return storageKey;\n }\n\n const userProfile = await this.config.auth.getUserProfile();\n const storageKeySignature = await this.config.auth.signMessage(\n `metamask:${userProfile.profileId}`,\n );\n const hashedStorageKeySignature = createSHA256Hash(storageKeySignature);\n await this.options.storage?.setStorageKey(hashedStorageKeySignature);\n return hashedStorageKeySignature;\n }\n\n async #upsertUserStorage(\n path: UserStoragePathWithFeatureAndKey,\n data: string,\n ): Promise<void> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n const encryptedData = await encryption.encryptString(data, storageKey);\n const encryptedPath = createEntryPath(path, storageKey);\n\n const url = new URL(STORAGE_URL(this.env, encryptedPath));\n\n const response = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({ data: encryptedData }),\n });\n\n if (!response.ok) {\n const responseBody: ErrorMessage = await response.json().catch(() => ({\n message: 'unknown',\n error: 'unknown',\n }));\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n throw new UserStorageError(\n `failed to upsert user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #batchUpsertUserStorage(\n path: UserStoragePathWithFeatureOnly,\n data: [UserStoragePathWithKeyOnly, string][],\n ): Promise<void> {\n try {\n if (!data.length) {\n return;\n }\n\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n\n const encryptedData = await Promise.all(\n data.map(async (d) => {\n return [\n this.#createEntryKey(d[0], storageKey),\n await encryption.encryptString(d[1], storageKey),\n ];\n }),\n );\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({ data: Object.fromEntries(encryptedData) }),\n });\n\n if (!response.ok) {\n const responseBody: ErrorMessage = await response.json().catch(() => ({\n message: 'unknown',\n error: 'unknown',\n }));\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n throw new UserStorageError(\n `failed to batch upsert user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #getUserStorage(\n path: UserStoragePathWithFeatureAndKey,\n ): Promise<string> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n const encryptedPath = createEntryPath(path, storageKey);\n\n const url = new URL(STORAGE_URL(this.env, encryptedPath));\n\n const response = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(\n `feature/key set not found for path '${path}'.`,\n );\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n\n const { Data: encryptedData } = await response.json();\n return encryption.decryptString(encryptedData, storageKey);\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to get user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #getUserStorageAllFeatureEntries(\n path: UserStoragePathWithFeatureOnly,\n ): Promise<string[] | null> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(`feature not found for path '${path}'.`);\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n\n const userStorage: GetUserStorageAllFeatureEntriesResponse | null =\n await response.json();\n\n if (!Array.isArray(userStorage)) {\n return null;\n }\n\n const decryptedData = userStorage.flatMap((entry) => {\n if (!entry.Data) {\n return [];\n }\n\n return encryption.decryptString(entry.Data, storageKey);\n });\n\n return (await Promise.allSettled(decryptedData))\n .map((d) => (d.status === 'fulfilled' ? d.value : undefined))\n .filter((d): d is string => d !== undefined);\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to get user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #deleteUserStorageAllFeatureEntries(\n path: UserStoragePathWithFeatureOnly,\n ): Promise<void> {\n try {\n const headers = await this.#getAuthorizationHeader();\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n method: 'DELETE',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(`feature not found for path '${path}'.`);\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to delete user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n #createEntryKey(key: string, storageKey: string): string {\n const hashedKey = createSHA256Hash(key + storageKey);\n return hashedKey;\n }\n\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n async #getAuthorizationHeader(): Promise<{ Authorization: string }> {\n const accessToken = await this.config.auth.getAccessToken();\n return { Authorization: `Bearer ${accessToken}` };\n }\n}\n"]}
|
|
@@ -120,7 +120,7 @@ _EncryptorDecryptor_instances = new WeakSet(), _EncryptorDecryptor_encryptString
|
|
|
120
120
|
const newSalt = salt ?? (0, webcrypto_1.randomBytes)(SCRYPT_SALT_SIZE);
|
|
121
121
|
let newKey;
|
|
122
122
|
if (nativeScryptCrypto) {
|
|
123
|
-
newKey = await nativeScryptCrypto(password, newSalt, o.N, o.r, o.p, o.dkLen);
|
|
123
|
+
newKey = await nativeScryptCrypto((0, utils_2.stringToByteArray)(password), newSalt, o.N, o.r, o.p, o.dkLen);
|
|
124
124
|
}
|
|
125
125
|
else {
|
|
126
126
|
newKey = await (0, scrypt_1.scryptAsync)(password, newSalt, {
|