UNPKG

@aws-amplify/core

Version:

Core category of aws-amplify

1 lines • 10 kB
{"version":3,"file":"legacyServerContext.mjs","sources":["../../../../src/adapterCore/serverContext/legacyServerContext.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { AMPLIFY_CONTEXT_BRAND } from '../../context/contextBrand';\nimport { createAmplifyContext } from '../../context/createAmplifyContext';\nimport { AmplifyServerContextError } from '../../errors/AmplifyServerContextError';\n/**\n * Registry of live legacy server contexts, keyed by the per-context token\n * symbol carried on every branded {@link AmplifyContext}\n * (`contextSpec.token.value`). Entries are added by\n * {@link createAmplifyServerContext} and removed by\n * {@link destroyAmplifyServerContext}, exactly mirroring the lifecycle of the\n * removed server-context registry.\n */\nconst legacyServerContextRegistry = new Map();\n/**\n * Builds the {@link LegacyBridgedAmplify} reverse bridge for a context.\n * The bridge is branded (it IS a valid {@link AmplifyContext} by delegation)\n * and frozen, mirroring the invariants of `createAmplifyContext`.\n */\nconst createLegacyBridgedAmplify = (ctx) => {\n const auth = Object.freeze({\n fetchAuthSession: (options) => ctx.fetchAuthSession(options),\n clearCredentials: () => ctx.clearCredentials(),\n getTokens: (options) => ctx.getTokens(options),\n });\n const amplify = {\n // Full AmplifyContext surface, delegating to the real context. The token\n // is shared so the bridge itself also resolves in the registry.\n resourcesConfig: ctx.resourcesConfig,\n libraryOptions: ctx.libraryOptions,\n token: ctx.token,\n fetchAuthSession: options => ctx.fetchAuthSession(options),\n clearCredentials: () => ctx.clearCredentials(),\n getTokens: options => ctx.getTokens(options),\n // Legacy AmplifyClass surface.\n getConfig: () => ctx.resourcesConfig,\n Auth: auth,\n configure: () => {\n throw new AmplifyServerContextError({\n message: '`configure()` is not supported on a server context. The legacy ' +\n 'per-request `AmplifyClass` has been replaced by an immutable, ' +\n 'branded `AmplifyContext`.',\n recoverySuggestion: 'Pass the resource config and library options when creating the ' +\n 'context (e.g. via `createAmplifyContext()` or ' +\n '`createAmplifyServerContext()`) instead of reconfiguring it.',\n });\n },\n };\n // Brand the bridge for runtime identification by `isAmplifyContext()`, so\n // context-first internals accept it without re-bridging (mirrors\n // `createAmplifyContext`).\n Object.defineProperty(amplify, AMPLIFY_CONTEXT_BRAND, {\n value: true,\n enumerable: false,\n configurable: false,\n writable: false,\n });\n return Object.freeze(amplify);\n};\n/**\n * Creates an Amplify server context.\n *\n * @deprecated This registry-backed API exists solely so OLD published\n * `@aws-amplify/adapter-nextjs` versions (≤ 1.7.3, peer `aws-amplify: ^6.16.4`)\n * — which call it (via `runWithAmplifyServerContext`) at runtime — keep working\n * against this version of `aws-amplify`. It will be removed in the next major\n * version. New code should use `createAmplifyContext()` and pass the context\n * to category APIs directly.\n *\n * Unlike the removed original (which registered a per-request `AmplifyClass`),\n * this shim builds a REAL branded `AmplifyContext` via `createAmplifyContext`\n * and returns it as the context spec: the branded context structurally\n * satisfies the legacy `ContextSpec` shape (`{ token: { value: symbol } }`),\n * so old adapter code can hand it BOTH to new context-first APIs (e.g.\n * `fetchAuthSession(contextSpec)` from `aws-amplify/auth/server`) and to the\n * legacy {@link getAmplifyServerContext} lookup.\n *\n * @param amplifyConfig The Amplify resource config.\n * @param libraryOptions The Amplify library options (the old adapter supplies\n * cookie-backed `Auth.tokenProvider` / `Auth.credentialsProvider` here).\n * @returns The branded {@link AmplifyContext}, usable as the legacy context\n * spec.\n */\nexport const createAmplifyServerContext = (amplifyConfig, libraryOptions) => {\n const ctx = createAmplifyContext(amplifyConfig, libraryOptions);\n legacyServerContextRegistry.set(ctx.token.value, {\n amplify: createLegacyBridgedAmplify(ctx),\n });\n return ctx;\n};\n/**\n * Returns an Amplify server context.\n *\n * @deprecated See {@link createAmplifyServerContext}; retained solely for OLD\n * published `@aws-amplify/adapter-nextjs` versions (≤ 1.7.3) and removed in\n * the next major version. The returned envelope's `amplify` member is a\n * {@link LegacyBridgedAmplify} reverse bridge, not an `AmplifyClass`.\n *\n * @param contextSpec The context spec used to get the Amplify server context.\n * @returns The Amplify server context.\n */\nexport const getAmplifyServerContext = (contextSpec) => {\n assertContextSpec(contextSpec);\n const context = legacyServerContextRegistry.get(contextSpec.token.value);\n if (context) {\n return context;\n }\n // Preserves the message of the removed original: reaching here means the\n // spec was well-formed but already destroyed (or never registered).\n throw new AmplifyServerContextError({\n message: 'Attempted to get the Amplify Server Context that may have been destroyed.',\n recoverySuggestion: 'Ensure always call Amplify APIs within `runWithAmplifyServerContext` function, and do not attempt to reuse `contextSpec` object.',\n });\n};\n/**\n * Destroys an Amplify server context.\n *\n * @deprecated See {@link createAmplifyServerContext}; retained solely for OLD\n * published `@aws-amplify/adapter-nextjs` versions (≤ 1.7.3) and removed in\n * the next major version.\n *\n * @param contextSpec The context spec used to destroy the Amplify server\n * context.\n */\nexport const destroyAmplifyServerContext = (contextSpec) => {\n legacyServerContextRegistry.delete(contextSpec.token.value);\n};\n/**\n * Validates the structural `ContextSpec` shape exactly like the removed\n * original, so malformed input from untyped JS callers fails with the same\n * descriptive error as before.\n */\nconst assertContextSpec = (contextSpec) => {\n let invalid = false;\n if (!Object.prototype.hasOwnProperty.call(contextSpec, 'token')) {\n invalid = true;\n }\n else if (!Object.prototype.hasOwnProperty.call(contextSpec.token, 'value')) {\n invalid = true;\n }\n else if (Object.prototype.toString.call(contextSpec.token?.value) !==\n '[object Symbol]') {\n invalid = true;\n }\n if (invalid) {\n throw new AmplifyServerContextError({\n message: 'Invalid `contextSpec`.',\n recoverySuggestion: 'Ensure to use the `contextSpec` object injected by `runWithAmplifyServerContext` function.',\n });\n }\n};\n"],"names":[],"mappings":";;;;AAAA;AACA;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,2BAA2B,GAAG,IAAI,GAAG,EAAE;AAC7C;AACA;AACA;AACA;AACA;AACA,MAAM,0BAA0B,GAAG,CAAC,GAAG,KAAK;AAC5C,IAAI,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC;AAC/B,QAAQ,gBAAgB,EAA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