Skip to content

Conversation

p2arthur
Copy link

@p2arthur p2arthur commented Sep 23, 2025

Fix: Dual Package Hazard with Symbol.hasInstance

Status: Ready for Review

Problem

When a package is imported under both CommonJS and ESM, classes can be instantiated separately by each loader.
This causes instanceof checks to fail across boundaries, even when dealing with the same logical class.

Solution
Each class was updated with:
A unique marker property attached to the instance.
A custom Symbol.hasInstance method that checks for the marker, ensuring instanceof works reliably across CJS/ESM boundaries.

`export class SomeClass {
private readonly _isSomeClass = true

static [Symbol.hasInstance](instance: unknown): boolean {
return !!(instance && (instance as any)._isSomeClass === true)
}
}`

Updated Classes
The fix has now been applied to all classes in the project:
./types/account.ts:20 — MultisigAccount
./types/account.ts:79 — SigningAccount
./types/app-arc56.ts:23 — Arc56Method
./types/app-client.ts:478 — AppClient
./types/app-client.ts:1794 — ApplicationClient
./types/app-manager.ts:98 — AppManager
./types/account-manager.ts:46 — AccountManager
./types/algorand-client-transaction-sender.ts:36 — AlgorandClientTransactionSender
./types/app-deployer.ts:112 — AppDeployer
./types/logic-error.ts:22 — LogicError
./types/client-manager.ts:48 — ClientManager
./types/algorand-client.ts:18 — AlgorandClient
./types/async-event-emitter.ts:5 — AsyncEventEmitter
./types/kmd-account-manager.ts:10 — KmdAccountManager
./types/algorand-client-transaction-creator.ts:8 — AlgorandClientTransactionCreator
./types/composer.ts:547 — TransactionComposer
./types/algo-http-client-with-retry.ts:6 — AlgoHttpClientWithRetry
./types/dispenser-client.ts:72 — TestNetDispenserApiClient
./types/config.ts:28 — UpdatableConfig
./types/amount.ts:4 — AlgoAmount
./types/asset-manager.ts:138 — AssetManager
./types/app-factory.ts:170 — AppFactory
./testing/test-logger.ts:8 — TestLogger
./testing/transaction-logger.ts:12 — TransactionLogger
./util.ts:25 — UnsafeConversionError

Tests
Added dual-package-hazard.spec.ts to validate cross-module instanceof between CJS and ESM.

@p2arthur p2arthur requested a review from lempira October 3, 2025 18:47
@p2arthur p2arthur marked this pull request as ready for review October 3, 2025 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant