@croct/sdk 0.17.7 → 0.17.9
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/activeRecord.cjs.map +1 -0
- package/activeRecord.d.ts +8 -4
- package/activeRecord.js +115 -106
- package/activeRecord.js.map +1 -1
- package/apiKey.cjs.map +1 -0
- package/apiKey.d.ts +4 -2
- package/apiKey.js +152 -141
- package/apiKey.js.map +1 -1
- package/base64Url.cjs.map +1 -0
- package/base64Url.d.ts +4 -2
- package/base64Url.js +8 -15
- package/base64Url.js.map +1 -1
- package/cache/cache.cjs.map +1 -0
- package/cache/cache.d.ts +6 -4
- package/cache/cache.js +9 -10
- package/cache/cache.js.map +1 -1
- package/cache/cookieCache.cjs.map +1 -0
- package/cache/cookieCache.d.ts +6 -3
- package/cache/cookieCache.js +52 -54
- package/cache/cookieCache.js.map +1 -1
- package/cache/fallbackCache.cjs.map +1 -0
- package/cache/fallbackCache.d.ts +5 -2
- package/cache/fallbackCache.js +20 -21
- package/cache/fallbackCache.js.map +1 -1
- package/cache/inMemoryCache.cjs.map +1 -0
- package/cache/inMemoryCache.d.ts +5 -2
- package/cache/inMemoryCache.js +15 -17
- package/cache/inMemoryCache.js.map +1 -1
- package/cache/index.cjs.map +1 -0
- package/cache/index.d.ts +4 -4
- package/cache/index.js +9 -11
- package/cache/index.js.map +1 -1
- package/cache/localStorageCache.cjs.map +1 -0
- package/cache/localStorageCache.d.ts +5 -2
- package/cache/localStorageCache.js +55 -63
- package/cache/localStorageCache.js.map +1 -1
- package/channel/channel.cjs.map +1 -0
- package/channel/channel.d.ts +8 -6
- package/channel/channel.js +21 -22
- package/channel/channel.js.map +1 -1
- package/channel/encodedChannel.cjs.map +1 -0
- package/channel/encodedChannel.d.ts +6 -3
- package/channel/encodedChannel.js +13 -14
- package/channel/encodedChannel.js.map +1 -1
- package/channel/guaranteedChannel.cjs.map +1 -0
- package/channel/guaranteedChannel.d.ts +9 -7
- package/channel/guaranteedChannel.js +74 -73
- package/channel/guaranteedChannel.js.map +1 -1
- package/channel/httpBeaconChannel.cjs.map +1 -0
- package/channel/httpBeaconChannel.d.ts +9 -6
- package/channel/httpBeaconChannel.js +82 -83
- package/channel/httpBeaconChannel.js.map +1 -1
- package/channel/index.cjs.map +1 -0
- package/channel/index.d.ts +12 -7
- package/channel/index.js +15 -17
- package/channel/index.js.map +1 -1
- package/channel/queuedChannel.cjs.map +1 -0
- package/channel/queuedChannel.d.ts +7 -4
- package/channel/queuedChannel.js +87 -97
- package/channel/queuedChannel.js.map +1 -1
- package/channel/retryChannel.cjs.map +1 -0
- package/channel/retryChannel.d.ts +7 -5
- package/channel/retryChannel.js +58 -57
- package/channel/retryChannel.js.map +1 -1
- package/channel/sandboxChannel.cjs.map +1 -0
- package/channel/sandboxChannel.d.ts +5 -2
- package/channel/sandboxChannel.js +34 -35
- package/channel/sandboxChannel.js.map +1 -1
- package/cid/assigner.cjs.map +1 -0
- package/cid/assigner.d.ts +3 -1
- package/cid/assigner.js +0 -2
- package/cid/assigner.js.map +1 -1
- package/cid/cachedAssigner.cjs.map +1 -0
- package/cid/cachedAssigner.d.ts +7 -5
- package/cid/cachedAssigner.js +37 -43
- package/cid/cachedAssigner.js.map +1 -1
- package/cid/fixedAssigner.cjs.map +1 -0
- package/cid/fixedAssigner.d.ts +5 -2
- package/cid/fixedAssigner.js +9 -10
- package/cid/fixedAssigner.js.map +1 -1
- package/cid/index.cjs.map +1 -0
- package/cid/index.d.ts +6 -4
- package/cid/index.js +9 -11
- package/cid/index.js.map +1 -1
- package/cid/remoteAssigner.cjs.map +1 -0
- package/cid/remoteAssigner.d.ts +6 -3
- package/cid/remoteAssigner.js +36 -37
- package/cid/remoteAssigner.js.map +1 -1
- package/constants.cjs.map +1 -0
- package/constants.d.ts +6 -4
- package/constants.js +10 -7
- package/constants.js.map +1 -1
- package/container.cjs.map +1 -0
- package/container.d.ts +28 -15
- package/container.js +269 -252
- package/container.js.map +1 -1
- package/contentFetcher.cjs.map +1 -0
- package/contentFetcher.d.ts +17 -14
- package/contentFetcher.js +156 -163
- package/contentFetcher.js.map +1 -1
- package/context.cjs.map +1 -0
- package/context.d.ts +13 -9
- package/context.js +80 -81
- package/context.js.map +1 -1
- package/error.cjs.map +1 -0
- package/error.d.ts +4 -2
- package/error.js +21 -21
- package/error.js.map +1 -1
- package/evaluator.cjs.map +1 -0
- package/evaluator.d.ts +18 -16
- package/evaluator.js +179 -179
- package/evaluator.js.map +1 -1
- package/eventManager.cjs.map +1 -0
- package/eventManager.d.ts +8 -6
- package/eventManager.js +24 -26
- package/eventManager.js.map +1 -1
- package/eventSubjectProcessor.cjs.map +1 -0
- package/eventSubjectProcessor.d.ts +16 -3
- package/eventSubjectProcessor.js +53 -59
- package/eventSubjectProcessor.js.map +1 -1
- package/facade/contentFetcherFacade.cjs.map +1 -0
- package/facade/contentFetcherFacade.d.ts +16 -7
- package/facade/contentFetcherFacade.js +32 -35
- package/facade/contentFetcherFacade.js.map +1 -1
- package/facade/evaluatorFacade.cjs.map +1 -0
- package/facade/evaluatorFacade.d.ts +17 -10
- package/facade/evaluatorFacade.js +56 -60
- package/facade/evaluatorFacade.js.map +1 -1
- package/facade/index.cjs.map +1 -0
- package/facade/index.d.ts +29 -7
- package/facade/index.js +19 -19
- package/facade/index.js.map +1 -1
- package/facade/sdkFacade.cjs.map +1 -0
- package/facade/sdkFacade.d.ts +33 -15
- package/facade/sdkFacade.js +201 -214
- package/facade/sdkFacade.js.map +1 -1
- package/facade/sessionFacade.cjs.map +1 -0
- package/facade/sessionFacade.d.ts +18 -3
- package/facade/sessionFacade.js +10 -11
- package/facade/sessionFacade.js.map +1 -1
- package/facade/sessionPatch.cjs.map +1 -0
- package/facade/sessionPatch.d.ts +17 -4
- package/facade/sessionPatch.js +22 -24
- package/facade/sessionPatch.js.map +1 -1
- package/facade/trackerFacade.cjs.map +1 -0
- package/facade/trackerFacade.d.ts +17 -4
- package/facade/trackerFacade.js +67 -57
- package/facade/trackerFacade.js.map +1 -1
- package/facade/userFacade.cjs.map +1 -0
- package/facade/userFacade.d.ts +21 -4
- package/facade/userFacade.js +17 -18
- package/facade/userFacade.js.map +1 -1
- package/facade/userPatch.cjs.map +1 -0
- package/facade/userPatch.d.ts +17 -4
- package/facade/userPatch.js +22 -24
- package/facade/userPatch.js.map +1 -1
- package/help.cjs.map +1 -0
- package/help.d.ts +3 -1
- package/help.js +19 -23
- package/help.js.map +1 -1
- package/index.cjs.map +1 -0
- package/index.d.ts +25 -3
- package/index.js +7 -7
- package/index.js.map +1 -1
- package/logging/consoleLogger.cjs.map +1 -0
- package/logging/consoleLogger.d.ts +5 -2
- package/logging/consoleLogger.js +23 -24
- package/logging/consoleLogger.js.map +1 -1
- package/logging/filteredLogger.cjs.map +1 -0
- package/logging/filteredLogger.d.ts +8 -5
- package/logging/filteredLogger.js +36 -37
- package/logging/filteredLogger.js.map +1 -1
- package/logging/index.cjs.map +1 -0
- package/logging/index.d.ts +5 -5
- package/logging/index.js +11 -13
- package/logging/index.js.map +1 -1
- package/logging/logger.cjs.map +1 -0
- package/logging/logger.d.ts +4 -2
- package/logging/logger.js +0 -2
- package/logging/logger.js.map +1 -1
- package/logging/namespacedLogger.cjs.map +1 -0
- package/logging/namespacedLogger.d.ts +5 -2
- package/logging/namespacedLogger.js +22 -23
- package/logging/namespacedLogger.js.map +1 -1
- package/logging/nullLogger.cjs.map +1 -0
- package/logging/nullLogger.d.ts +5 -2
- package/logging/nullLogger.js +11 -16
- package/logging/nullLogger.js.map +1 -1
- package/namespacedStorage.cjs.map +1 -0
- package/namespacedStorage.d.ts +3 -1
- package/namespacedStorage.js +51 -52
- package/namespacedStorage.js.map +1 -1
- package/package.json +60 -6
- package/patch.cjs.map +1 -0
- package/patch.d.ts +6 -4
- package/patch.js +0 -2
- package/patch.js.map +1 -1
- package/queue/capacityRestrictedQueue.cjs.map +1 -0
- package/queue/capacityRestrictedQueue.d.ts +5 -2
- package/queue/capacityRestrictedQueue.js +31 -32
- package/queue/capacityRestrictedQueue.js.map +1 -1
- package/queue/inMemoryQueue.cjs.map +1 -0
- package/queue/inMemoryQueue.d.ts +5 -2
- package/queue/inMemoryQueue.js +32 -34
- package/queue/inMemoryQueue.js.map +1 -1
- package/queue/index.cjs.map +1 -0
- package/queue/index.d.ts +6 -5
- package/queue/index.js +11 -13
- package/queue/index.js.map +1 -1
- package/queue/monitoredQueue.cjs.map +1 -0
- package/queue/monitoredQueue.d.ts +8 -5
- package/queue/monitoredQueue.js +104 -110
- package/queue/monitoredQueue.js.map +1 -1
- package/queue/persistentQueue.cjs.map +1 -0
- package/queue/persistentQueue.d.ts +5 -2
- package/queue/persistentQueue.js +52 -54
- package/queue/persistentQueue.js.map +1 -1
- package/queue/queue.cjs.map +1 -0
- package/queue/queue.d.ts +3 -1
- package/queue/queue.js +0 -2
- package/queue/queue.js.map +1 -1
- package/retry/arbitraryPolicy.cjs.map +1 -0
- package/retry/arbitraryPolicy.d.ts +5 -2
- package/retry/arbitraryPolicy.js +14 -15
- package/retry/arbitraryPolicy.js.map +1 -1
- package/retry/backoffPolicy.cjs.map +1 -0
- package/retry/backoffPolicy.d.ts +5 -3
- package/retry/backoffPolicy.js +52 -50
- package/retry/backoffPolicy.js.map +1 -1
- package/retry/index.cjs.map +1 -0
- package/retry/index.d.ts +5 -5
- package/retry/index.js +11 -13
- package/retry/index.js.map +1 -1
- package/retry/maxAttemptsPolicy.cjs.map +1 -0
- package/retry/maxAttemptsPolicy.d.ts +5 -2
- package/retry/maxAttemptsPolicy.js +17 -18
- package/retry/maxAttemptsPolicy.js.map +1 -1
- package/retry/neverPolicy.cjs.map +1 -0
- package/retry/neverPolicy.d.ts +5 -2
- package/retry/neverPolicy.js +9 -10
- package/retry/neverPolicy.js.map +1 -1
- package/retry/policy.cjs.map +1 -0
- package/retry/policy.d.ts +3 -1
- package/retry/policy.js +0 -2
- package/retry/policy.js.map +1 -1
- package/schema/attributeSchema.cjs.map +1 -0
- package/schema/attributeSchema.d.ts +6 -2
- package/schema/attributeSchema.js +7 -7
- package/schema/attributeSchema.js.map +1 -1
- package/schema/contentFetcherSchemas.cjs.map +1 -0
- package/schema/contentFetcherSchemas.d.ts +6 -2
- package/schema/contentFetcherSchemas.js +24 -21
- package/schema/contentFetcherSchemas.js.map +1 -1
- package/schema/contentSchemas.cjs.map +1 -0
- package/schema/contentSchemas.d.ts +6 -2
- package/schema/contentSchemas.js +45 -45
- package/schema/contentSchemas.js.map +1 -1
- package/schema/contextSchemas.cjs.map +1 -0
- package/schema/contextSchemas.d.ts +6 -2
- package/schema/contextSchemas.js +6 -6
- package/schema/contextSchemas.js.map +1 -1
- package/schema/ecommerceSchemas.cjs.map +1 -0
- package/schema/ecommerceSchemas.d.ts +10 -6
- package/schema/ecommerceSchemas.js +176 -172
- package/schema/ecommerceSchemas.js.map +1 -1
- package/schema/evaluatorSchemas.cjs.map +1 -0
- package/schema/evaluatorSchemas.d.ts +6 -2
- package/schema/evaluatorSchemas.js +46 -30
- package/schema/evaluatorSchemas.js.map +1 -1
- package/schema/eventSchemas.cjs.map +1 -0
- package/schema/eventSchemas.d.ts +16 -12
- package/schema/eventSchemas.js +125 -102
- package/schema/eventSchemas.js.map +1 -1
- package/schema/index.cjs.map +1 -0
- package/schema/index.d.ts +14 -11
- package/schema/index.js +11 -14
- package/schema/index.js.map +1 -1
- package/schema/loggerSchema.cjs.map +1 -0
- package/schema/loggerSchema.d.ts +6 -2
- package/schema/loggerSchema.js +13 -13
- package/schema/loggerSchema.js.map +1 -1
- package/schema/operationSchemas.cjs.map +1 -0
- package/schema/operationSchemas.d.ts +14 -10
- package/schema/operationSchemas.js +85 -69
- package/schema/operationSchemas.js.map +1 -1
- package/schema/sdkFacadeSchemas.cjs.map +1 -0
- package/schema/sdkFacadeSchemas.d.ts +6 -2
- package/schema/sdkFacadeSchemas.js +65 -51
- package/schema/sdkFacadeSchemas.js.map +1 -1
- package/schema/sdkSchemas.cjs.map +1 -0
- package/schema/sdkSchemas.d.ts +8 -4
- package/schema/sdkSchemas.js +80 -78
- package/schema/sdkSchemas.js.map +1 -1
- package/schema/tokenSchema.cjs.map +1 -0
- package/schema/tokenSchema.d.ts +6 -2
- package/schema/tokenSchema.js +41 -38
- package/schema/tokenSchema.js.map +1 -1
- package/schema/userSchema.cjs.map +1 -0
- package/schema/userSchema.d.ts +6 -2
- package/schema/userSchema.js +183 -134
- package/schema/userSchema.js.map +1 -1
- package/sdk.cjs.map +1 -0
- package/sdk.d.ts +28 -14
- package/sdk.js +106 -108
- package/sdk.js.map +1 -1
- package/sdkEvents.cjs.map +1 -0
- package/sdkEvents.d.ts +10 -5
- package/sdkEvents.js +0 -2
- package/sdkEvents.js.map +1 -1
- package/sourceLocation.cjs.map +1 -0
- package/sourceLocation.d.ts +7 -5
- package/sourceLocation.js +56 -57
- package/sourceLocation.js.map +1 -1
- package/tab.cjs.map +1 -0
- package/tab.d.ts +9 -7
- package/tab.js +93 -90
- package/tab.js.map +1 -1
- package/token/cachedTokenStore.cjs.map +1 -0
- package/token/cachedTokenStore.d.ts +8 -3
- package/token/cachedTokenStore.js +22 -24
- package/token/cachedTokenStore.js.map +1 -1
- package/token/inMemoryTokenStore.cjs.map +1 -0
- package/token/inMemoryTokenStore.d.ts +7 -2
- package/token/inMemoryTokenStore.js +12 -13
- package/token/inMemoryTokenStore.js.map +1 -1
- package/token/index.cjs.map +1 -0
- package/token/index.d.ts +7 -4
- package/token/index.js +9 -11
- package/token/index.js.map +1 -1
- package/token/replicatedTokenStore.cjs.map +1 -0
- package/token/replicatedTokenStore.d.ts +7 -2
- package/token/replicatedTokenStore.js +14 -15
- package/token/replicatedTokenStore.js.map +1 -1
- package/token/token.cjs.map +1 -0
- package/token/token.d.ts +11 -8
- package/token/token.js +212 -195
- package/token/token.js.map +1 -1
- package/tracker.cjs.map +1 -0
- package/tracker.d.ts +20 -13
- package/tracker.js +307 -307
- package/tracker.js.map +1 -1
- package/trackingEvents.cjs.map +1 -0
- package/trackingEvents.d.ts +65 -62
- package/trackingEvents.js +57 -56
- package/trackingEvents.js.map +1 -1
- package/transformer.cjs.map +1 -0
- package/transformer.d.ts +4 -2
- package/transformer.js +5 -6
- package/transformer.js.map +1 -1
- package/utilityTypes.cjs.map +1 -0
- package/utilityTypes.d.ts +4 -2
- package/utilityTypes.js +0 -2
- package/utilityTypes.js.map +1 -1
- package/uuid.cjs.map +1 -0
- package/uuid.d.ts +3 -1
- package/uuid.js +27 -32
- package/uuid.js.map +1 -1
- package/validation/arrayType.cjs.map +1 -0
- package/validation/arrayType.d.ts +5 -3
- package/validation/arrayType.js +45 -41
- package/validation/arrayType.js.map +1 -1
- package/validation/booleanType.cjs.map +1 -0
- package/validation/booleanType.d.ts +5 -2
- package/validation/booleanType.js +19 -16
- package/validation/booleanType.js.map +1 -1
- package/validation/functionType.cjs.map +1 -0
- package/validation/functionType.d.ts +5 -2
- package/validation/functionType.js +19 -16
- package/validation/functionType.js.map +1 -1
- package/validation/index.cjs.map +1 -0
- package/validation/index.d.ts +12 -12
- package/validation/index.js +27 -29
- package/validation/index.js.map +1 -1
- package/validation/jsonType.cjs.map +1 -0
- package/validation/jsonType.d.ts +8 -6
- package/validation/jsonType.js +89 -82
- package/validation/jsonType.js.map +1 -1
- package/validation/mixedSchema.cjs.map +1 -0
- package/validation/mixedSchema.d.ts +5 -2
- package/validation/mixedSchema.js +5 -7
- package/validation/mixedSchema.js.map +1 -1
- package/validation/nullType.cjs.map +1 -0
- package/validation/nullType.d.ts +5 -2
- package/validation/nullType.js +19 -16
- package/validation/nullType.js.map +1 -1
- package/validation/numberType.cjs.map +1 -0
- package/validation/numberType.d.ts +5 -3
- package/validation/numberType.js +40 -32
- package/validation/numberType.js.map +1 -1
- package/validation/objectType.cjs.map +1 -0
- package/validation/objectType.d.ts +5 -3
- package/validation/objectType.js +84 -85
- package/validation/objectType.js.map +1 -1
- package/validation/schema.cjs.map +1 -0
- package/validation/schema.d.ts +5 -3
- package/validation/schema.js +8 -9
- package/validation/schema.js.map +1 -1
- package/validation/stringType.cjs.map +1 -0
- package/validation/stringType.d.ts +5 -3
- package/validation/stringType.js +74 -70
- package/validation/stringType.js.map +1 -1
- package/validation/unionType.cjs.map +1 -0
- package/validation/unionType.d.ts +5 -2
- package/validation/unionType.js +37 -35
- package/validation/unionType.js.map +1 -1
- package/validation/violation.cjs.map +1 -0
- package/validation/violation.d.ts +4 -2
- package/validation/violation.js +18 -18
- package/validation/violation.js.map +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/activeRecord.ts"],"sourcesContent":["import {JsonStructure, JsonValue} from '@croct/json';\nimport {Operation, Patch} from './patch';\nimport {TrackingEvent} from './trackingEvents';\nimport {\n addOperation,\n clearOperation,\n combineOperation,\n decrementOperation,\n incrementOperation,\n mergeOperation,\n setOperation,\n unsetOperation,\n removeOperation,\n} from './schema';\n\nconst operationSchema = {\n add: addOperation,\n set: setOperation,\n merge: mergeOperation,\n combine: combineOperation,\n increment: incrementOperation,\n decrement: decrementOperation,\n clear: clearOperation,\n unset: unsetOperation,\n remove: removeOperation,\n};\n\nexport abstract class ActiveRecord<T extends TrackingEvent> {\n private readonly operations: Operation[] = [];\n\n public set(value: JsonValue): this;\n\n public set(property: string, value: JsonValue): this;\n\n public set(propertyOrValue: string | JsonValue, value?: JsonValue): this {\n if (typeof propertyOrValue === 'string') {\n return this.pushOperation({\n type: 'set',\n path: propertyOrValue,\n value: value as JsonValue,\n });\n }\n\n return this.pushOperation({\n type: 'set',\n path: '.',\n value: propertyOrValue,\n });\n }\n\n public add(property: string, value: JsonValue): this {\n return this.pushOperation({\n type: 'add',\n path: property,\n value: value,\n });\n }\n\n public combine(property: string, value: JsonValue): this {\n return this.pushOperation({\n type: 'combine',\n path: property,\n value: value,\n });\n }\n\n public merge(value: JsonStructure): this;\n\n public merge(property: string, value: JsonStructure): this;\n\n public merge(propertyOrValue: string | JsonStructure, value?: JsonStructure): this {\n if (typeof propertyOrValue === 'string') {\n return this.pushOperation({\n type: 'merge',\n path: propertyOrValue,\n value: value as JsonStructure,\n });\n }\n\n return this.pushOperation({\n type: 'merge',\n path: '.',\n value: propertyOrValue,\n });\n }\n\n public increment(property: string, amount = 1): this {\n return this.pushOperation({\n type: 'increment',\n path: property,\n value: amount,\n });\n }\n\n public decrement(property: string, amount = 1): this {\n return this.pushOperation({\n type: 'decrement',\n path: property,\n value: amount,\n });\n }\n\n public clear(property: string): this {\n return this.pushOperation({\n type: 'clear',\n path: property,\n });\n }\n\n public unset(property: string): this {\n return this.pushOperation({\n type: 'unset',\n path: property,\n });\n }\n\n public remove(property: string, value: JsonValue): this {\n return this.pushOperation({\n type: 'remove',\n path: property,\n value: value,\n });\n }\n\n private pushOperation(operation: Operation): this {\n const {type, ...data} = operation;\n\n operationSchema[type].validate(data);\n\n this.operations.push(operation);\n\n return this;\n }\n\n protected reset(): this {\n this.operations.splice(0);\n\n return this;\n }\n\n public abstract save(): Promise<T>;\n\n protected isDirty(): boolean {\n return this.operations.length > 0;\n }\n\n protected buildPatch(): Patch {\n return {operations: this.operations.slice()};\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,oBAUO;AAEP,MAAM,kBAAkB;AAAA,EACpB,KAAK;AAAA,EACL,KAAK;AAAA,EACL,OAAO;AAAA,EACP,SAAS;AAAA,EACT,WAAW;AAAA,EACX,WAAW;AAAA,EACX,OAAO;AAAA,EACP,OAAO;AAAA,EACP,QAAQ;AACZ;AAEO,MAAe,aAAsC;AAAA,EAArD;AACH,SAAiB,aAA0B,CAAC;AAAA;AAAA,EAMrC,IAAI,iBAAqC,OAAyB;AACrE,QAAI,OAAO,oBAAoB,UAAU;AACrC,aAAO,KAAK,cAAc;AAAA,QACtB,MAAM;AAAA,QACN,MAAM;AAAA,QACN;AAAA,MACJ,CAAC;AAAA,IACL;AAEA,WAAO,KAAK,cAAc;AAAA,MACtB,MAAM;AAAA,MACN,MAAM;AAAA,MACN,OAAO;AAAA,IACX,CAA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M,GAAG,KAAI,IAAI;AAExB,oBAAgB,IAAI,EAAE,SAAS,IAAI;AAEnC,SAAK,WAAW,KAAK,SAAS;AAE9B,WAAO;AAAA,EACX;AAAA,EAEU,QAAc;AACpB,SAAK,WAAW,OAAO,CAAC;AAExB,WAAO;AAAA,EACX;AAAA,EAIU,UAAmB;AACzB,WAAO,KAAK,WAAW,SAAS;AAAA,EACpC;AAAA,EAEU,aAAoB;AAC1B,WAAO,EAAC,YAAY,KAAK,WAAW,MAAM,EAAC;AAAA,EAC/C;AACJ;","names":[]}
|
package/activeRecord.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Patch } from './patch';
|
|
3
|
-
import { TrackingEvent } from './trackingEvents';
|
|
4
|
-
|
|
1
|
+
import { JsonValue, JsonStructure } from '@croct/json';
|
|
2
|
+
import { Patch } from './patch.js';
|
|
3
|
+
import { TrackingEvent } from './trackingEvents.js';
|
|
4
|
+
import './utilityTypes.js';
|
|
5
|
+
|
|
6
|
+
declare abstract class ActiveRecord<T extends TrackingEvent> {
|
|
5
7
|
private readonly operations;
|
|
6
8
|
set(value: JsonValue): this;
|
|
7
9
|
set(property: string, value: JsonValue): this;
|
|
@@ -20,3 +22,5 @@ export declare abstract class ActiveRecord<T extends TrackingEvent> {
|
|
|
20
22
|
protected isDirty(): boolean;
|
|
21
23
|
protected buildPatch(): Patch;
|
|
22
24
|
}
|
|
25
|
+
|
|
26
|
+
export { ActiveRecord };
|
package/activeRecord.js
CHANGED
|
@@ -1,113 +1,122 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import {
|
|
2
|
+
addOperation,
|
|
3
|
+
clearOperation,
|
|
4
|
+
combineOperation,
|
|
5
|
+
decrementOperation,
|
|
6
|
+
incrementOperation,
|
|
7
|
+
mergeOperation,
|
|
8
|
+
setOperation,
|
|
9
|
+
unsetOperation,
|
|
10
|
+
removeOperation
|
|
11
|
+
} from "./schema";
|
|
5
12
|
const operationSchema = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
add: addOperation,
|
|
14
|
+
set: setOperation,
|
|
15
|
+
merge: mergeOperation,
|
|
16
|
+
combine: combineOperation,
|
|
17
|
+
increment: incrementOperation,
|
|
18
|
+
decrement: decrementOperation,
|
|
19
|
+
clear: clearOperation,
|
|
20
|
+
unset: unsetOperation,
|
|
21
|
+
remove: removeOperation
|
|
15
22
|
};
|
|
16
23
|
class ActiveRecord {
|
|
17
|
-
|
|
18
|
-
|
|
24
|
+
constructor() {
|
|
25
|
+
this.operations = [];
|
|
26
|
+
}
|
|
27
|
+
set(propertyOrValue, value) {
|
|
28
|
+
if (typeof propertyOrValue === "string") {
|
|
29
|
+
return this.pushOperation({
|
|
30
|
+
type: "set",
|
|
31
|
+
path: propertyOrValue,
|
|
32
|
+
value
|
|
33
|
+
});
|
|
19
34
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
merge(propertyOrValue, value) {
|
|
49
|
-
if (typeof propertyOrValue === 'string') {
|
|
50
|
-
return this.pushOperation({
|
|
51
|
-
type: 'merge',
|
|
52
|
-
path: propertyOrValue,
|
|
53
|
-
value: value,
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
return this.pushOperation({
|
|
57
|
-
type: 'merge',
|
|
58
|
-
path: '.',
|
|
59
|
-
value: propertyOrValue,
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
increment(property, amount = 1) {
|
|
63
|
-
return this.pushOperation({
|
|
64
|
-
type: 'increment',
|
|
65
|
-
path: property,
|
|
66
|
-
value: amount,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
decrement(property, amount = 1) {
|
|
70
|
-
return this.pushOperation({
|
|
71
|
-
type: 'decrement',
|
|
72
|
-
path: property,
|
|
73
|
-
value: amount,
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
clear(property) {
|
|
77
|
-
return this.pushOperation({
|
|
78
|
-
type: 'clear',
|
|
79
|
-
path: property,
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
unset(property) {
|
|
83
|
-
return this.pushOperation({
|
|
84
|
-
type: 'unset',
|
|
85
|
-
path: property,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
remove(property, value) {
|
|
89
|
-
return this.pushOperation({
|
|
90
|
-
type: 'remove',
|
|
91
|
-
path: property,
|
|
92
|
-
value: value,
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
pushOperation(operation) {
|
|
96
|
-
const { type, ...data } = operation;
|
|
97
|
-
operationSchema[type].validate(data);
|
|
98
|
-
this.operations.push(operation);
|
|
99
|
-
return this;
|
|
100
|
-
}
|
|
101
|
-
reset() {
|
|
102
|
-
this.operations.splice(0);
|
|
103
|
-
return this;
|
|
104
|
-
}
|
|
105
|
-
isDirty() {
|
|
106
|
-
return this.operations.length > 0;
|
|
107
|
-
}
|
|
108
|
-
buildPatch() {
|
|
109
|
-
return { operations: this.operations.slice() };
|
|
35
|
+
return this.pushOperation({
|
|
36
|
+
type: "set",
|
|
37
|
+
path: ".",
|
|
38
|
+
value: propertyOrValue
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
add(property, value) {
|
|
42
|
+
return this.pushOperation({
|
|
43
|
+
type: "add",
|
|
44
|
+
path: property,
|
|
45
|
+
value
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
combine(property, value) {
|
|
49
|
+
return this.pushOperation({
|
|
50
|
+
type: "combine",
|
|
51
|
+
path: property,
|
|
52
|
+
value
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
merge(propertyOrValue, value) {
|
|
56
|
+
if (typeof propertyOrValue === "string") {
|
|
57
|
+
return this.pushOperation({
|
|
58
|
+
type: "merge",
|
|
59
|
+
path: propertyOrValue,
|
|
60
|
+
value
|
|
61
|
+
});
|
|
110
62
|
}
|
|
63
|
+
return this.pushOperation({
|
|
64
|
+
type: "merge",
|
|
65
|
+
path: ".",
|
|
66
|
+
value: propertyOrValue
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
increment(property, amount = 1) {
|
|
70
|
+
return this.pushOperation({
|
|
71
|
+
type: "increment",
|
|
72
|
+
path: property,
|
|
73
|
+
value: amount
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
decrement(property, amount = 1) {
|
|
77
|
+
return this.pushOperation({
|
|
78
|
+
type: "decrement",
|
|
79
|
+
path: property,
|
|
80
|
+
value: amount
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
clear(property) {
|
|
84
|
+
return this.pushOperation({
|
|
85
|
+
type: "clear",
|
|
86
|
+
path: property
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
unset(property) {
|
|
90
|
+
return this.pushOperation({
|
|
91
|
+
type: "unset",
|
|
92
|
+
path: property
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
remove(property, value) {
|
|
96
|
+
return this.pushOperation({
|
|
97
|
+
type: "remove",
|
|
98
|
+
path: property,
|
|
99
|
+
value
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
pushOperation(operation) {
|
|
103
|
+
const { type, ...data } = operation;
|
|
104
|
+
operationSchema[type].validate(data);
|
|
105
|
+
this.operations.push(operation);
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
reset() {
|
|
109
|
+
this.operations.splice(0);
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
isDirty() {
|
|
113
|
+
return this.operations.length > 0;
|
|
114
|
+
}
|
|
115
|
+
buildPatch() {
|
|
116
|
+
return { operations: this.operations.slice() };
|
|
117
|
+
}
|
|
111
118
|
}
|
|
112
|
-
|
|
119
|
+
export {
|
|
120
|
+
ActiveRecord
|
|
121
|
+
};
|
|
113
122
|
//# sourceMappingURL=activeRecord.js.map
|
package/activeRecord.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["src/activeRecord.ts"],"sourcesContent":["import {JsonStructure, JsonValue} from '@croct/json';\nimport {Operation, Patch} from './patch';\nimport {TrackingEvent} from './trackingEvents';\nimport {\n addOperation,\n clearOperation,\n combineOperation,\n decrementOperation,\n incrementOperation,\n mergeOperation,\n setOperation,\n unsetOperation,\n removeOperation,\n} from './schema';\n\nconst operationSchema = {\n add: addOperation,\n set: setOperation,\n merge: mergeOperation,\n combine: combineOperation,\n increment: incrementOperation,\n decrement: decrementOperation,\n clear: clearOperation,\n unset: unsetOperation,\n remove: removeOperation,\n};\n\nexport abstract class ActiveRecord<T extends TrackingEvent> {\n private readonly operations: Operation[] = [];\n\n public set(value: JsonValue): this;\n\n public set(property: string, value: JsonValue): this;\n\n public set(propertyOrValue: string | JsonValue, value?: JsonValue): this {\n if (typeof propertyOrValue === 'string') {\n return this.pushOperation({\n type: 'set',\n path: propertyOrValue,\n value: value as JsonValue,\n });\n }\n\n return this.pushOperation({\n type: 'set',\n path: '.',\n value: propertyOrValue,\n });\n }\n\n public add(property: string, value: JsonValue): this {\n return this.pushOperation({\n type: 'add',\n path: property,\n value: value,\n });\n }\n\n public combine(property: string, value: JsonValue): this {\n return this.pushOperation({\n type: 'combine',\n path: property,\n value: value,\n });\n }\n\n public merge(value: JsonStructure): this;\n\n public merge(property: string, value: JsonStructure): this;\n\n public merge(propertyOrValue: string | JsonStructure, value?: JsonStructure): this {\n if (typeof propertyOrValue === 'string') {\n return this.pushOperation({\n type: 'merge',\n path: propertyOrValue,\n value: value as JsonStructure,\n });\n }\n\n return this.pushOperation({\n type: 'merge',\n path: '.',\n value: propertyOrValue,\n });\n }\n\n public increment(property: string, amount = 1): this {\n return this.pushOperation({\n type: 'increment',\n path: property,\n value: amount,\n });\n }\n\n public decrement(property: string, amount = 1): this {\n return this.pushOperation({\n type: 'decrement',\n path: property,\n value: amount,\n });\n }\n\n public clear(property: string): this {\n return this.pushOperation({\n type: 'clear',\n path: property,\n });\n }\n\n public unset(property: string): this {\n return this.pushOperation({\n type: 'unset',\n path: property,\n });\n }\n\n public remove(property: string, value: JsonValue): this {\n return this.pushOperation({\n type: 'remove',\n path: property,\n value: value,\n });\n }\n\n private pushOperation(operation: Operation): this {\n const {type, ...data} = operation;\n\n operationSchema[type].validate(data);\n\n this.operations.push(operation);\n\n return this;\n }\n\n protected reset(): this {\n this.operations.splice(0);\n\n return this;\n }\n\n public abstract save(): Promise<T>;\n\n protected isDirty(): boolean {\n return this.operations.length > 0;\n }\n\n protected buildPatch(): Patch {\n return {operations: this.operations.slice()};\n }\n}\n"],"mappings":"AAGA;AAAA,EACI;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACG;AAEP,MAAM,kBAAkB;AAAA,EACpB,KAAK;AAAA,EACL,KAAK;AAAA,EACL,OAAO;AAAA,EACP,SAAS;AAAA,EACT,WAAW;AAAA,EACX,WAAW;AAAA,EACX,OAAO;AAAA,EACP,OAAO;AAAA,EACP,QAAQ;AACZ;AAEO,MAAe,aAAsC;AAAA,EAArD;AACH,SAAiB,aAA0B,CAAC;AAAA;AAAA,EAMrC,IAAI,iBAAqC,OAAyB;AACrE,QAAI,OAAO,oBAAoB,UAAU;AACrC,aAAO,KAAK,cAAc;AAAA,QACtB,MAAM;AAAA,QACN,MAAM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,cAAc;AAAA,MACtB,MAAM;AAAA,MACN,MAAM;AAAA,MACN;AAAA,IACJ,CAAC;AAAA,EACL;AAAA,EAEQ,cAAc,WAA4B;AAC9C,UAAM,EAAC,MAAM,GAAG,KAAI,IAAI;AAExB,oBAAgB,IAAI,EAAE,SAAS,IAAI;AAEnC,SAAK,WAAW,KAAK,SAAS;AAE9B,WAAO;AAAA,EACX;AAAA,EAEU,QAAc;AACpB,SAAK,WAAW,OAAO,CAAC;AAExB,WAAO;AAAA,EACX;AAAA,EAIU,UAAmB;AACzB,WAAO,KAAK,WAAW,SAAS;AAAA,EACpC;AAAA,EAEU,aAAoB;AAC1B,WAAO,EAAC,YAAY,KAAK,WAAW,MAAM,EAAC;AAAA,EAC/C;AACJ;","names":[]}
|
package/apiKey.cjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/apiKey.ts"],"sourcesContent":["export type ParsedPrivateKey = {\n algorithm: string,\n encodedKey: string,\n};\n\ntype Algorithm = {\n keyAlgorithm: EcKeyImportParams,\n signatureAlgorithm: EcdsaParams,\n};\n\nexport class ApiKey {\n private static readonly IDENTIFIER_PATTERN = /^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$/i;\n\n private static readonly PRIVATE_KEY_PATTERN = /^[a-z0-9]+;[^;]+$/i;\n\n private static readonly ALGORITHMS: Record<string, Algorithm> = {\n ES256: {\n keyAlgorithm: {\n name: 'ECDSA',\n namedCurve: 'P-256',\n },\n signatureAlgorithm: {\n name: 'ECDSA',\n hash: 'SHA-256',\n },\n },\n };\n\n private readonly identifier: string;\n\n private readonly privateKey?: ParsedPrivateKey;\n\n private importedKey: Promise<CryptoKey>;\n\n private constructor(identifier: string, privateKey?: ParsedPrivateKey) {\n this.identifier = identifier;\n this.privateKey = privateKey;\n }\n\n public static from(apiKey: string | ApiKey): ApiKey {\n if (apiKey instanceof ApiKey) {\n return apiKey;\n }\n\n return ApiKey.parse(apiKey);\n }\n\n public static parse(apiKey: string): ApiKey {\n const parts = apiKey.split(':');\n\n if (parts.length > 2) {\n throw new Error('Invalid API key format.');\n }\n\n return ApiKey.of(parts[0], parts[1]);\n }\n\n public static of(identifier: string, privateKey?: string): ApiKey {\n if (!ApiKey.IDENTIFIER_PATTERN.test(identifier)) {\n throw new Error('The identifier must be a UUID.');\n }\n\n if (privateKey === undefined || privateKey === '') {\n return new ApiKey(identifier);\n }\n\n if (!ApiKey.PRIVATE_KEY_PATTERN.test(privateKey)) {\n throw new Error('The private key is malformed.');\n }\n\n const [algorithmName, encodedKey] = privateKey.split(';');\n\n if (!(algorithmName in ApiKey.ALGORITHMS)) {\n throw new Error(`Unsupported signing algorithm \"${algorithmName}\".`);\n }\n\n return new ApiKey(identifier, {\n algorithm: algorithmName,\n encodedKey: encodedKey,\n });\n }\n\n public getIdentifier(): string {\n return this.identifier;\n }\n\n public async getIdentifierHash(): Promise<string> {\n const identifierBytes = ApiKey.createByteArrayFromHexString(this.identifier.replace(/-/g, ''));\n const rawHash = await crypto.subtle.digest('SHA-256', identifierBytes);\n\n return ApiKey.convertBufferToHexString(rawHash);\n }\n\n public hasPrivateKey(): boolean {\n return this.privateKey !== undefined;\n }\n\n public getPrivateKey(): string {\n if (this.privateKey === undefined) {\n throw new Error('The API key does not have a private key.');\n }\n\n return `${this.privateKey.algorithm};${this.privateKey.encodedKey}`;\n }\n\n public async sign(data: string): Promise<string> {\n const key = await this.importKey();\n const algorithm = this.getSigningAlgorithm();\n\n return ApiKey.convertBufferToString(\n await crypto.subtle.sign(\n ApiKey.ALGORITHMS[algorithm].signatureAlgorithm,\n key,\n ApiKey.createByteArrayFromString(data),\n ),\n );\n }\n\n public getSigningAlgorithm(): string {\n const {algorithm} = this.getParsedPrivateKey();\n\n return algorithm;\n }\n\n private importKey(): Promise<CryptoKey> {\n const {algorithm, encodedKey} = this.getParsedPrivateKey();\n\n if (this.importedKey === undefined) {\n this.importedKey = crypto.subtle\n .importKey(\n 'pkcs8',\n ApiKey.createByteArrayFromString(atob(encodedKey)),\n ApiKey.ALGORITHMS[algorithm].keyAlgorithm,\n false,\n ['sign'],\n );\n }\n\n return this.importedKey;\n }\n\n private getParsedPrivateKey(): ParsedPrivateKey {\n if (this.privateKey === undefined) {\n throw new Error('The API key does not have a private key.');\n }\n\n return this.privateKey;\n }\n\n public export(): string {\n return this.identifier + (this.hasPrivateKey() ? `:${this.getPrivateKey()}` : '');\n }\n\n public toString(): string {\n return '[redacted]';\n }\n\n /**\n * Create an array buffer from a string.\n *\n * @see https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String\n *\n * @param data The string to convert.\n * @returns The array buffer.\n */\n private static createByteArrayFromString(data: string): Uint8Array {\n const byteArray = new Uint8Array(data.length);\n\n for (let i = 0; i < byteArray.length; i++) {\n byteArray[i] = data.charCodeAt(i);\n }\n\n return byteArray;\n }\n\n private static createByteArrayFromHexString(data: string): Uint8Array {\n const byteArray = new Uint8Array(data.length / 2);\n\n for (let i = 0; i < byteArray.length; i++) {\n byteArray[i] = parseInt(data.substring(i * 2, i * 2 + 2), 16);\n }\n\n return byteArray;\n }\n\n /**\n * Convert an array buffer to a string.\n *\n * @see https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String\n *\n * @param buffer The buffer to convert.\n * @returns The string.\n */\n private static convertBufferToString(buffer: ArrayLike<number> | ArrayBufferLike): string {\n return String.fromCharCode(...new Uint8Array(buffer));\n }\n\n private static convertBufferToHexString(buffer: ArrayLike<number> | ArrayBufferLike): string {\n const bytes = new Uint8Array(buffer);\n let hexString = '';\n\n for (let i = 0; i < bytes.length; i++) {\n hexString += bytes[i].toString(16).padStart(2, '0');\n }\n\n return hexString;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUO,MAAM,UAAN,MAAM,QAAO;AAAA,EAwBR,YAAY,YAAoB,YAA+B;AACnE,SAAK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
|
6
6
|
private static readonly IDENTIFIER_PATTERN;
|
|
7
7
|
private static readonly PRIVATE_KEY_PATTERN;
|
|
8
8
|
private static readonly ALGORITHMS;
|
|
@@ -44,3 +44,5 @@ export declare class ApiKey {
|
|
|
44
44
|
private static convertBufferToString;
|
|
45
45
|
private static convertBufferToHexString;
|
|
46
46
|
}
|
|
47
|
+
|
|
48
|
+
export { ApiKey, type ParsedPrivateKey };
|
package/apiKey.js
CHANGED
|
@@ -1,144 +1,155 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
1
|
+
const _ApiKey = class _ApiKey {
|
|
2
|
+
constructor(identifier, privateKey) {
|
|
3
|
+
this.identifier = identifier;
|
|
4
|
+
this.privateKey = privateKey;
|
|
5
|
+
}
|
|
6
|
+
static from(apiKey) {
|
|
7
|
+
if (apiKey instanceof _ApiKey) {
|
|
8
|
+
return apiKey;
|
|
9
|
+
}
|
|
10
|
+
return _ApiKey.parse(apiKey);
|
|
11
|
+
}
|
|
12
|
+
static parse(apiKey) {
|
|
13
|
+
const parts = apiKey.split(":");
|
|
14
|
+
if (parts.length > 2) {
|
|
15
|
+
throw new Error("Invalid API key format.");
|
|
16
|
+
}
|
|
17
|
+
return _ApiKey.of(parts[0], parts[1]);
|
|
18
|
+
}
|
|
19
|
+
static of(identifier, privateKey) {
|
|
20
|
+
if (!_ApiKey.IDENTIFIER_PATTERN.test(identifier)) {
|
|
21
|
+
throw new Error("The identifier must be a UUID.");
|
|
22
|
+
}
|
|
23
|
+
if (privateKey === void 0 || privateKey === "") {
|
|
24
|
+
return new _ApiKey(identifier);
|
|
25
|
+
}
|
|
26
|
+
if (!_ApiKey.PRIVATE_KEY_PATTERN.test(privateKey)) {
|
|
27
|
+
throw new Error("The private key is malformed.");
|
|
28
|
+
}
|
|
29
|
+
const [algorithmName, encodedKey] = privateKey.split(";");
|
|
30
|
+
if (!(algorithmName in _ApiKey.ALGORITHMS)) {
|
|
31
|
+
throw new Error(`Unsupported signing algorithm "${algorithmName}".`);
|
|
32
|
+
}
|
|
33
|
+
return new _ApiKey(identifier, {
|
|
34
|
+
algorithm: algorithmName,
|
|
35
|
+
encodedKey
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
getIdentifier() {
|
|
39
|
+
return this.identifier;
|
|
40
|
+
}
|
|
41
|
+
async getIdentifierHash() {
|
|
42
|
+
const identifierBytes = _ApiKey.createByteArrayFromHexString(this.identifier.replace(/-/g, ""));
|
|
43
|
+
const rawHash = await crypto.subtle.digest("SHA-256", identifierBytes);
|
|
44
|
+
return _ApiKey.convertBufferToHexString(rawHash);
|
|
45
|
+
}
|
|
46
|
+
hasPrivateKey() {
|
|
47
|
+
return this.privateKey !== void 0;
|
|
48
|
+
}
|
|
49
|
+
getPrivateKey() {
|
|
50
|
+
if (this.privateKey === void 0) {
|
|
51
|
+
throw new Error("The API key does not have a private key.");
|
|
52
|
+
}
|
|
53
|
+
return `${this.privateKey.algorithm};${this.privateKey.encodedKey}`;
|
|
54
|
+
}
|
|
55
|
+
async sign(data) {
|
|
56
|
+
const key = await this.importKey();
|
|
57
|
+
const algorithm = this.getSigningAlgorithm();
|
|
58
|
+
return _ApiKey.convertBufferToString(
|
|
59
|
+
await crypto.subtle.sign(
|
|
60
|
+
_ApiKey.ALGORITHMS[algorithm].signatureAlgorithm,
|
|
61
|
+
key,
|
|
62
|
+
_ApiKey.createByteArrayFromString(data)
|
|
63
|
+
)
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
getSigningAlgorithm() {
|
|
67
|
+
const { algorithm } = this.getParsedPrivateKey();
|
|
68
|
+
return algorithm;
|
|
69
|
+
}
|
|
70
|
+
importKey() {
|
|
71
|
+
const { algorithm, encodedKey } = this.getParsedPrivateKey();
|
|
72
|
+
if (this.importedKey === void 0) {
|
|
73
|
+
this.importedKey = crypto.subtle.importKey(
|
|
74
|
+
"pkcs8",
|
|
75
|
+
_ApiKey.createByteArrayFromString(atob(encodedKey)),
|
|
76
|
+
_ApiKey.ALGORITHMS[algorithm].keyAlgorithm,
|
|
77
|
+
false,
|
|
78
|
+
["sign"]
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return this.importedKey;
|
|
82
|
+
}
|
|
83
|
+
getParsedPrivateKey() {
|
|
84
|
+
if (this.privateKey === void 0) {
|
|
85
|
+
throw new Error("The API key does not have a private key.");
|
|
86
|
+
}
|
|
87
|
+
return this.privateKey;
|
|
88
|
+
}
|
|
89
|
+
export() {
|
|
90
|
+
return this.identifier + (this.hasPrivateKey() ? `:${this.getPrivateKey()}` : "");
|
|
91
|
+
}
|
|
92
|
+
toString() {
|
|
93
|
+
return "[redacted]";
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Create an array buffer from a string.
|
|
97
|
+
*
|
|
98
|
+
* @see https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
|
|
99
|
+
*
|
|
100
|
+
* @param data The string to convert.
|
|
101
|
+
* @returns The array buffer.
|
|
102
|
+
*/
|
|
103
|
+
static createByteArrayFromString(data) {
|
|
104
|
+
const byteArray = new Uint8Array(data.length);
|
|
105
|
+
for (let i = 0; i < byteArray.length; i++) {
|
|
106
|
+
byteArray[i] = data.charCodeAt(i);
|
|
107
|
+
}
|
|
108
|
+
return byteArray;
|
|
109
|
+
}
|
|
110
|
+
static createByteArrayFromHexString(data) {
|
|
111
|
+
const byteArray = new Uint8Array(data.length / 2);
|
|
112
|
+
for (let i = 0; i < byteArray.length; i++) {
|
|
113
|
+
byteArray[i] = parseInt(data.substring(i * 2, i * 2 + 2), 16);
|
|
114
|
+
}
|
|
115
|
+
return byteArray;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Convert an array buffer to a string.
|
|
119
|
+
*
|
|
120
|
+
* @see https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
|
|
121
|
+
*
|
|
122
|
+
* @param buffer The buffer to convert.
|
|
123
|
+
* @returns The string.
|
|
124
|
+
*/
|
|
125
|
+
static convertBufferToString(buffer) {
|
|
126
|
+
return String.fromCharCode(...new Uint8Array(buffer));
|
|
127
|
+
}
|
|
128
|
+
static convertBufferToHexString(buffer) {
|
|
129
|
+
const bytes = new Uint8Array(buffer);
|
|
130
|
+
let hexString = "";
|
|
131
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
132
|
+
hexString += bytes[i].toString(16).padStart(2, "0");
|
|
133
|
+
}
|
|
134
|
+
return hexString;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
_ApiKey.IDENTIFIER_PATTERN = /^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$/i;
|
|
138
|
+
_ApiKey.PRIVATE_KEY_PATTERN = /^[a-z0-9]+;[^;]+$/i;
|
|
139
|
+
_ApiKey.ALGORITHMS = {
|
|
140
|
+
ES256: {
|
|
141
|
+
keyAlgorithm: {
|
|
142
|
+
name: "ECDSA",
|
|
143
|
+
namedCurve: "P-256"
|
|
142
144
|
},
|
|
145
|
+
signatureAlgorithm: {
|
|
146
|
+
name: "ECDSA",
|
|
147
|
+
hash: "SHA-256"
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
let ApiKey = _ApiKey;
|
|
152
|
+
export {
|
|
153
|
+
ApiKey
|
|
143
154
|
};
|
|
144
155
|
//# sourceMappingURL=apiKey.js.map
|