UNPKG

@aws-amplify/pubsub

Version:

Pubsub category of aws-amplify

54 lines (51 loc) • 2.39 kB
import { createCtxResolver, assertOptionalCtxArg, Signer } from '@aws-amplify/core/internals/utils'; import { isAmplifyContext } from '@aws-amplify/core'; import { MqttOverWS } from './MqttOverWS.mjs'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 const SERVICE_NAME = 'iotdevicegateway'; class AWSIoT extends MqttOverWS { constructor(ctxOrOptions, maybeOptions) { if (isAmplifyContext(ctxOrOptions)) { super(maybeOptions ?? {}); this._resolveCtx = createCtxResolver(ctxOrOptions); } else { // When a second argument is supplied the caller used the // `(ctx, options)` overload, so the first argument is required to be a // branded AmplifyContext. Guard against a defined-but-unbranded value // being silently swallowed as `options`. In the single-argument form // `ctxOrOptions` is legitimately the options object, so no assertion. if (maybeOptions !== undefined) { assertOptionalCtxArg(ctxOrOptions); } // `ctxOrOptions` reaching this branch is either the single-argument // options object or `undefined`. In the `(undefined, options)` form the // caller omitted the context but still supplied options in the second // slot, so fall back to `maybeOptions` rather than discarding it. super(ctxOrOptions ?? maybeOptions ?? {}); this._resolveCtx = createCtxResolver(); } } get region() { return this.options?.region; } get endpoint() { return (async () => { const { endpoint } = this.options; const serviceInfo = { service: SERVICE_NAME, region: this.region, }; const session = await this._resolveCtx().fetchAuthSession(); if (!session.credentials) { throw new Error('No auth session credentials'); } const { accessKeyId: access_key, secretAccessKey: secret_key, sessionToken: session_token, } = session.credentials; const result = Signer.signUrl(endpoint, { access_key, secret_key, session_token }, serviceInfo); return result; })(); } } export { AWSIoT }; //# sourceMappingURL=AWSIot.mjs.map