From cc4ee6fed0c08c2757112e1d79a8e5e58b5ed199 Mon Sep 17 00:00:00 2001 From: James Prevett Date: Mon, 7 Oct 2024 13:00:20 -0500 Subject: [PATCH] Polyfill `Symbol.metadata`, `DecoratorContext.metadata` --- .github/workflows/release.yaml | 4 +++- src/internal/struct.ts | 23 +++++++++++++++++++++++ test/{struct-test.ts => struct.test.ts} | 0 3 files changed, 26 insertions(+), 1 deletion(-) rename test/{struct-test.ts => struct.test.ts} (100%) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index c13b2b6..a83ea8b 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -14,6 +14,8 @@ jobs: release: runs-on: ubuntu-latest needs: ci + permissions: + id-token: write steps: - name: Checkout uses: actions/checkout@v3 @@ -30,6 +32,6 @@ jobs: run: npm run build - name: Publish - run: npm publish --access=public + run: npm publish --access=public --provenance env: NODE_AUTH_TOKEN: ${{secrets.npm_token}} diff --git a/src/internal/struct.ts b/src/internal/struct.ts index 8a054f7..9073957 100644 --- a/src/internal/struct.ts +++ b/src/internal/struct.ts @@ -62,11 +62,34 @@ export function isValidMetadata( return arg != null && typeof arg == 'object' && metadata in arg; } +/** + * Polyfill Symbol.metadata + * @see https://github.com/microsoft/TypeScript/issues/53461 + */ +(Symbol as { metadata: symbol }).metadata ??= Symbol.for('Symbol.metadata'); + +/** + * Polyfill context.metadata + * @see https://github.com/microsoft/TypeScript/issues/53461 + */ +export function _polyfill_contextMetadata(target: object): void { + if (!Symbol?.metadata) { + return; + } + Object.defineProperty(target, Symbol.metadata, { + enumerable: true, + configurable: true, + writable: true, + value: Object.create(null), + }); +} + /** * Gets a reference to Symbol.metadata, even on platforms that do not expose it globally (like Node) */ export function symbol_metadata(arg: ClassLike): typeof Symbol.metadata { const symbol_metadata = Symbol.metadata || Object.getOwnPropertySymbols(arg).find(s => s.description == 'Symbol.metadata'); + _polyfill_contextMetadata(arg); if (!symbol_metadata) { throw new ReferenceError('Could not get a reference to Symbol.metadata'); } diff --git a/test/struct-test.ts b/test/struct.test.ts similarity index 100% rename from test/struct-test.ts rename to test/struct.test.ts