{"version":3,"file":"createRunWithAmplifyServerContext.mjs","sources":["../../../src/utils/createRunWithAmplifyServerContext.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { sharedInMemoryStorage } from 'aws-amplify/utils';\nimport { createAWSCredentialsAndIdentityIdProvider, createKeyValueStorageFromCookieStorageAdapter, createUserPoolsTokenProvider, runWithAmplifyServerContext as runWithAmplifyServerContextCore, } from 'aws-amplify/adapter-core';\nimport { DEFAULT_SERVER_SIDE_AUTH_SET_COOKIE_OPTIONS, ENFORCED_SERVER_SIDE_AUTH_SET_COOKIE_OPTIONS, } from '../auth/constant';\nimport { createCookieStorageAdapterFromNextServerContext } from './createCookieStorageAdapterFromNextServerContext';\nexport const createRunWithAmplifyServerContext = ({ config: resourcesConfig, tokenValidator, globalSettings, }) => {\n    const isServerSideAuthEnabled = globalSettings.isServerSideAuthEnabled();\n    const isSSLOrigin = globalSettings.isSSLOrigin();\n    const setCookieOptions = globalSettings.getRuntimeOptions().cookies ?? {};\n    const mergedSetCookieOptions = {\n        // default options when not specified\n        ...(isServerSideAuthEnabled && DEFAULT_SERVER_SIDE_AUTH_SET_COOKIE_OPTIONS),\n        // user-specified options\n        ...setCookieOptions,\n        // enforced options when server-side auth is enabled\n        ...(isServerSideAuthEnabled && {\n            ...ENFORCED_SERVER_SIDE_AUTH_SET_COOKIE_OPTIONS,\n            secure: isSSLOrigin,\n        }),\n        // only support root path\n        path: '/',\n    };\n    const runWithAmplifyServerContext = async ({ nextServerContext, operation }) => {\n        // When the Auth config is presented, attempt to create a Amplify server\n        // context with token and credentials provider.\n        if (resourcesConfig.Auth) {\n            const keyValueStorage = \n            // When `null` is passed as the value of `nextServerContext`, opt-in\n            // unauthenticated role (primarily for static rendering). It's\n            // safe to use the singleton `MemoryKeyValueStorage` here, as the\n            // static rendering uses the same unauthenticated role cross-sever.\n            nextServerContext === null\n                ? sharedInMemoryStorage\n                : createKeyValueStorageFromCookieStorageAdapter(await createCookieStorageAdapterFromNextServerContext(nextServerContext, isServerSideAuthEnabled), tokenValidator, mergedSetCookieOptions);\n            const credentialsProvider = createAWSCredentialsAndIdentityIdProvider(resourcesConfig.Auth, keyValueStorage);\n            const tokenProvider = createUserPoolsTokenProvider(resourcesConfig.Auth, keyValueStorage);\n            return runWithAmplifyServerContextCore(resourcesConfig, {\n                Auth: { credentialsProvider, tokenProvider },\n            }, operation);\n        }\n        // Otherwise it may be the case that auth is not used, e.g. API key.\n        // Omitting the `Auth` in the second parameter.\n        return runWithAmplifyServerContextCore(resourcesConfig, {}, operation);\n    };\n    return runWithAmplifyServerContext;\n};\n"],"names":["runWithAmplifyServerContext","runWithAmplifyServerContextCore"],"mappings":";;;;;AAAA;AACA;AAKY,MAAC,iCAAiC,GAAG,CAAC,EAAE,MAAM,EAAE,eAAe,EAAE,cAAc,EAAE,cAAc,GAAG,KAAK;AACnH,IAAI,MAAM,uBAAuB,GAAG,cAAc,CAAC,uBAAuB,EAAE;AAC5E,IAAI,MAAM,WAAW,GAAG,cAAc,CAAC,WAAW,EAAE;AACpD,IAAI,MAAM,gBAAgB,GAAG,cAAc,CAAC,iBAAiB,EAAE,CAAC,OAAO,IAAI,EAAE;AAC7E,IAAI,MAAM,sBAAsB,GAAG;AACnC;AACA,QAAQ,IAAI,uBAAuB,IAAI,2CAA2C,CAAC;AACnF;AACA,QAAQ,GAAG,gBAAgB;AAC3B;AACA,QAAQ,IAAI,uBAAuB,IAAI;AACvC,YAAY,GAAG,4CAA4C;AAC3D,YAAY,MAAM,EAAE,WAAW;AAC/B,SAAS,CAAC;AACV;AACA,QAAQ,IAAI,EAAE,GAAG;AACjB,KAAK;AACL,IAAI,MAAMA,6BAA2B,GAAG,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,KAAK;AACpF;AACA;AACA,QAAQ,IAAI,eAAe,CAAC,IAAI,EAAE;AAClC,YAAY,MAAM,eAAe;AACjC;AACA;AACA;AACA;AACA,YAA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