Ver Fonte

ts: Improve native ESM compatibility (#1053)

Vladimir Guguiev há 3 anos atrás
pai
commit
5e8d335599

+ 3 - 2
ts/jest.config.js

@@ -1,5 +1,6 @@
 module.exports = {
   preset: 'ts-jest/presets/default',
-	testEnvironment: 'node',
-	testTimeout: 90000
+  testEnvironment: 'node',
+  testTimeout: 90000,
+  resolver: "ts-jest-resolver",
 };

+ 9 - 8
ts/package.json

@@ -2,8 +2,8 @@
   "name": "@project-serum/anchor",
   "version": "0.18.2",
   "description": "Anchor client",
-  "main": "dist/cjs/index.js",
-  "module": "dist/esm/index.js",
+  "module": "./dist/esm/index.js",
+  "main": "./dist/cjs/index.js",
   "license": "(MIT OR Apache-2.0)",
   "types": "dist/cjs/index.d.ts",
   "homepage": "https://github.com/project-serum/anchor#readme",
@@ -52,20 +52,21 @@
     "@types/bn.js": "^4.11.6",
     "@types/bs58": "^4.0.1",
     "@types/crypto-hash": "^1.1.2",
-    "@types/jest": "^26.0.15",
+    "@types/jest": "^27.0.3",
     "@types/pako": "^1.0.1",
     "@typescript-eslint/eslint-plugin": "^4.6.0",
     "@typescript-eslint/parser": "^4.6.0",
     "eslint": "^7.12.1",
     "eslint-config-prettier": "^6.15.0",
     "husky": "^4.3.0",
-    "jest": "26.6.0",
-    "jest-config": "26.6.0",
+    "jest": "27.3.1",
+    "jest-config": "27.3.1",
     "lint-staged": "^10.5.0",
     "prettier": "^2.1.2",
-    "ts-jest": "^26.4.3",
+    "ts-jest": "^27.0.7",
+    "ts-jest-resolver": "^2.0.0",
     "ts-node": "^9.0.0",
-    "typedoc": "^0.20.36",
-    "typescript": "^4.4.3"
+    "typedoc": "^0.22.10",
+    "typescript": "^4.5.2"
   }
 }

+ 3 - 2
ts/src/coder/accounts.ts

@@ -1,6 +1,7 @@
+import { Buffer } from "buffer";
 import { Layout } from "buffer-layout";
-import { Idl } from "../idl";
-import { IdlCoder } from "./idl";
+import { Idl } from "../idl.js";
+import { IdlCoder } from "./idl.js";
 import { sha256 } from "js-sha256";
 import camelcase from "camelcase";
 

+ 3 - 2
ts/src/coder/common.ts

@@ -1,7 +1,8 @@
+import { Buffer } from "buffer";
 import { snakeCase } from "snake-case";
 import { sha256 } from "js-sha256";
-import { Idl, IdlField, IdlTypeDef, IdlEnumVariant, IdlType } from "../idl";
-import { IdlError } from "../error";
+import { Idl, IdlField, IdlTypeDef, IdlEnumVariant, IdlType } from "../idl.js";
+import { IdlError } from "../error.js";
 
 export function accountSize(idl: Idl, idlAccount: IdlTypeDef): number {
   if (idlAccount.type.kind === "enum") {

+ 4 - 3
ts/src/coder/event.ts

@@ -1,9 +1,10 @@
+import { Buffer } from "buffer";
 import * as base64 from "base64-js";
 import { Layout } from "buffer-layout";
 import { sha256 } from "js-sha256";
-import { Idl, IdlEvent, IdlTypeDef } from "../idl";
-import { Event, EventData } from "../program/event";
-import { IdlCoder } from "./idl";
+import { Idl, IdlEvent, IdlTypeDef } from "../idl.js";
+import { Event, EventData } from "../program/event.js";
+import { IdlCoder } from "./idl.js";
 
 export class EventCoder {
   /**

+ 2 - 2
ts/src/coder/idl.ts

@@ -1,8 +1,8 @@
 import camelCase from "camelcase";
 import { Layout } from "buffer-layout";
 import * as borsh from "@project-serum/borsh";
-import { IdlField, IdlTypeDef, IdlEnumVariant, IdlType } from "../idl";
-import { IdlError } from "../error";
+import { IdlField, IdlTypeDef, IdlEnumVariant, IdlType } from "../idl.js";
+import { IdlError } from "../error.js";
 
 export class IdlCoder {
   public static fieldLayout(

+ 11 - 11
ts/src/coder/index.ts

@@ -1,15 +1,15 @@
-import { Idl } from "../idl";
-import { InstructionCoder } from "./instruction";
-import { AccountsCoder } from "./accounts";
-import { EventCoder } from "./event";
-import { StateCoder } from "./state";
-import { sighash } from "./common";
+import { Idl } from "../idl.js";
+import { InstructionCoder } from "./instruction.js";
+import { AccountsCoder } from "./accounts.js";
+import { EventCoder } from "./event.js";
+import { StateCoder } from "./state.js";
+import { sighash } from "./common.js";
 
-export { accountSize } from "./common";
-export { InstructionCoder } from "./instruction";
-export { AccountsCoder, ACCOUNT_DISCRIMINATOR_SIZE } from "./accounts";
-export { EventCoder, eventDiscriminator } from "./event";
-export { StateCoder, stateDiscriminator } from "./state";
+export { accountSize } from "./common.js";
+export { InstructionCoder } from "./instruction.js";
+export { AccountsCoder, ACCOUNT_DISCRIMINATOR_SIZE } from "./accounts.js";
+export { EventCoder, eventDiscriminator } from "./event.js";
+export { StateCoder, stateDiscriminator } from "./state.js";
 
 /**
  * Coder provides a facade for encoding and decoding all IDL related objects.

+ 4 - 3
ts/src/coder/instruction.ts

@@ -1,7 +1,8 @@
+import { Buffer } from "buffer";
 import camelCase from "camelcase";
 import { Layout } from "buffer-layout";
 import * as borsh from "@project-serum/borsh";
-import * as bs58 from "bs58";
+import bs58 from "bs58";
 import {
   Idl,
   IdlField,
@@ -12,8 +13,8 @@ import {
   IdlAccountItem,
   IdlTypeDefTyStruct,
 } from "../idl";
-import { IdlCoder } from "./idl";
-import { sighash } from "./common";
+import { IdlCoder } from "./idl.js";
+import { sighash } from "./common.js";
 import { AccountMeta, PublicKey } from "@solana/web3.js";
 
 /**

+ 4 - 3
ts/src/coder/state.ts

@@ -1,8 +1,9 @@
+import { Buffer } from "buffer";
 import { Layout } from "buffer-layout";
 import { sha256 } from "js-sha256";
-import { Idl } from "../idl";
-import { IdlCoder } from "./idl";
-import * as features from "../utils/features";
+import { Idl } from "../idl.js";
+import { IdlCoder } from "./idl.js";
+import * as features from "../utils/features.js";
 
 export class StateCoder {
   private layout: Layout;

+ 1 - 0
ts/src/idl.ts

@@ -1,3 +1,4 @@
+import { Buffer } from "buffer";
 import { PublicKey } from "@solana/web3.js";
 import * as borsh from "@project-serum/borsh";
 

+ 8 - 8
ts/src/index.ts

@@ -5,18 +5,18 @@ export {
   getProvider,
   setProvider,
   NodeWallet as Wallet,
-} from "./provider";
+} from "./provider.js";
 export {
   default as Coder,
   InstructionCoder,
   EventCoder,
   StateCoder,
   AccountsCoder,
-} from "./coder";
+} from "./coder/index.js";
 
-export * from "./error";
-export { Instruction } from "./coder/instruction";
-export { Idl } from "./idl";
-export { default as workspace } from "./workspace";
-export * as utils from "./utils";
-export * from "./program";
+export * from "./error.js";
+export { Instruction } from "./coder/instruction.js";
+export { Idl } from "./idl.js";
+export { default as workspace } from "./workspace.js";
+export * as utils from "./utils/index.js";
+export * from "./program/index.js";

+ 2 - 2
ts/src/program/common.ts

@@ -1,7 +1,7 @@
 import EventEmitter from "eventemitter3";
 import { PublicKey } from "@solana/web3.js";
-import { Idl, IdlInstruction, IdlAccountItem, IdlStateMethod } from "../idl";
-import { Accounts } from "./context";
+import { Idl, IdlInstruction, IdlAccountItem, IdlStateMethod } from "../idl.js";
+import { Accounts } from "./context.js";
 
 export type Subscription = {
   listener: number;

+ 2 - 2
ts/src/program/context.ts

@@ -4,8 +4,8 @@ import {
   ConfirmOptions,
   TransactionInstruction,
 } from "@solana/web3.js";
-import { Address } from "./common";
-import { IdlAccountItem, IdlAccounts, IdlInstruction } from "../idl";
+import { Address } from "./common.js";
+import { IdlAccountItem, IdlAccounts, IdlInstruction } from "../idl.js";
 
 /**
  * Context provides all non-argument inputs for generating Anchor transactions.

+ 4 - 4
ts/src/program/event.ts

@@ -1,9 +1,9 @@
 import { PublicKey } from "@solana/web3.js";
 import * as assert from "assert";
-import { IdlEvent, IdlEventField } from "../idl";
-import Coder from "../coder";
-import { DecodeType } from "./namespace/types";
-import Provider from "../provider";
+import { IdlEvent, IdlEventField } from "../idl.js";
+import Coder from "../coder/index.js";
+import { DecodeType } from "./namespace/types.js";
+import Provider from "../provider.js";
 
 const LOG_START_INDEX = "Program log: ".length;
 

+ 12 - 12
ts/src/program/index.ts

@@ -1,8 +1,8 @@
 import { inflate } from "pako";
 import { PublicKey } from "@solana/web3.js";
-import Provider from "../provider";
-import { Idl, idlAddress, decodeIdlAccount } from "../idl";
-import Coder from "../coder";
+import Provider from "../provider.js";
+import { Idl, idlAddress, decodeIdlAccount } from "../idl.js";
+import Coder from "../coder/index.js";
 import NamespaceFactory, {
   RpcNamespace,
   InstructionNamespace,
@@ -10,16 +10,16 @@ import NamespaceFactory, {
   AccountNamespace,
   StateClient,
   SimulateNamespace,
-} from "./namespace";
-import { getProvider } from "../";
-import { utf8 } from "../utils/bytes";
-import { EventManager } from "./event";
-import { Address, translateAddress } from "./common";
+} from "./namespace/index.js";
+import { getProvider } from "../index.js";
+import { utf8 } from "../utils/bytes/index.js";
+import { EventManager } from "./event.js";
+import { Address, translateAddress } from "./common.js";
 
-export * from "./common";
-export * from "./context";
-export * from "./event";
-export * from "./namespace";
+export * from "./common.js";
+export * from "./context.js";
+export * from "./event.js";
+export * from "./namespace/index.js";
 
 /**
  * ## Program

+ 10 - 9
ts/src/program/namespace/account.ts

@@ -1,6 +1,7 @@
+import { Buffer } from "buffer";
 import camelCase from "camelcase";
 import EventEmitter from "eventemitter3";
-import * as bs58 from "bs58";
+import bs58 from "bs58";
 import {
   Signer,
   PublicKey,
@@ -9,18 +10,18 @@ import {
   Commitment,
   GetProgramAccountsFilter,
 } from "@solana/web3.js";
-import Provider from "../../provider";
-import { Idl, IdlTypeDef } from "../../idl";
+import Provider from "../../provider.js";
+import { Idl, IdlTypeDef } from "../../idl.js";
 import Coder, {
   ACCOUNT_DISCRIMINATOR_SIZE,
   accountSize,
   AccountsCoder,
-} from "../../coder";
-import { Subscription, Address, translateAddress } from "../common";
-import { getProvider } from "../../";
-import { AllAccountsMap, IdlTypes, TypeDef } from "./types";
-import * as pubkeyUtil from "../../utils/pubkey";
-import * as rpcUtil from "../../utils/rpc";
+} from "../../coder/index.js";
+import { Subscription, Address, translateAddress } from "../common.js";
+import { getProvider } from "../../index.js";
+import { AllAccountsMap, IdlTypes, TypeDef } from "./types.js";
+import * as pubkeyUtil from "../../utils/pubkey.js";
+import * as rpcUtil from "../../utils/rpc.js";
 
 export default class AccountFactory {
   public static build<IDL extends Idl>(

+ 18 - 18
ts/src/program/namespace/index.ts

@@ -1,25 +1,25 @@
 import camelCase from "camelcase";
 import { PublicKey } from "@solana/web3.js";
-import Coder from "../../coder";
-import Provider from "../../provider";
-import { Idl, IdlInstruction } from "../../idl";
-import StateFactory, { StateClient } from "./state";
-import InstructionFactory, { InstructionNamespace } from "./instruction";
-import TransactionFactory, { TransactionNamespace } from "./transaction";
-import RpcFactory, { RpcNamespace } from "./rpc";
-import AccountFactory, { AccountNamespace } from "./account";
-import SimulateFactory, { SimulateNamespace } from "./simulate";
-import { parseIdlErrors } from "../common";
-import { AllInstructions } from "./types";
+import Coder from "../../coder/index.js";
+import Provider from "../../provider.js";
+import { Idl, IdlInstruction } from "../../idl.js";
+import StateFactory, { StateClient } from "./state.js";
+import InstructionFactory, { InstructionNamespace } from "./instruction.js";
+import TransactionFactory, { TransactionNamespace } from "./transaction.js";
+import RpcFactory, { RpcNamespace } from "./rpc.js";
+import AccountFactory, { AccountNamespace } from "./account.js";
+import SimulateFactory, { SimulateNamespace } from "./simulate.js";
+import { parseIdlErrors } from "../common.js";
+import { AllInstructions } from "./types.js";
 
 // Re-exports.
-export { StateClient } from "./state";
-export { InstructionNamespace, InstructionFn } from "./instruction";
-export { TransactionNamespace, TransactionFn } from "./transaction";
-export { RpcNamespace, RpcFn } from "./rpc";
-export { AccountNamespace, AccountClient, ProgramAccount } from "./account";
-export { SimulateNamespace, SimulateFn } from "./simulate";
-export { IdlAccounts, IdlTypes } from "./types";
+export { StateClient } from "./state.js";
+export { InstructionNamespace, InstructionFn } from "./instruction.js";
+export { TransactionNamespace, TransactionFn } from "./transaction.js";
+export { RpcNamespace, RpcFn } from "./rpc.js";
+export { AccountNamespace, AccountClient, ProgramAccount } from "./account.js";
+export { SimulateNamespace, SimulateFn } from "./simulate.js";
+export { IdlAccounts, IdlTypes } from "./types.js";
 
 export default class NamespaceFactory {
   /**

+ 6 - 6
ts/src/program/namespace/instruction.ts

@@ -9,23 +9,23 @@ import {
   IdlAccountItem,
   IdlAccounts,
   IdlInstruction,
-} from "../../idl";
-import { IdlError } from "../../error";
+} from "../../idl.js";
+import { IdlError } from "../../error.js";
 import {
   toInstruction,
   validateAccounts,
   translateAddress,
   Address,
-} from "../common";
-import { Accounts, splitArgsAndCtx } from "../context";
-import * as features from "../../utils/features";
+} from "../common.js";
+import { Accounts, splitArgsAndCtx } from "../context.js";
+import * as features from "../../utils/features.js";
 import {
   AllInstructions,
   AllInstructionsMap,
   InstructionContextFn,
   InstructionContextFnArgs,
   MakeInstructionsNamespace,
-} from "./types";
+} from "./types.js";
 
 export default class InstructionNamespaceFactory {
   public static build<IDL extends Idl, I extends AllInstructions<IDL>>(

+ 7 - 7
ts/src/program/namespace/rpc.ts

@@ -1,15 +1,15 @@
 import { TransactionSignature } from "@solana/web3.js";
-import Provider from "../../provider";
-import { Idl } from "../../idl";
-import { splitArgsAndCtx } from "../context";
-import { TransactionFn } from "./transaction";
-import { ProgramError } from "../../error";
-import * as features from "../../utils/features";
+import Provider from "../../provider.js";
+import { Idl } from "../../idl.js";
+import { splitArgsAndCtx } from "../context.js";
+import { TransactionFn } from "./transaction.js";
+import { ProgramError } from "../../error.js";
+import * as features from "../../utils/features.js";
 import {
   AllInstructions,
   InstructionContextFn,
   MakeInstructionsNamespace,
-} from "./types";
+} from "./types.js";
 
 export default class RpcFactory {
   public static build<IDL extends Idl, I extends AllInstructions<IDL>>(

+ 8 - 8
ts/src/program/namespace/simulate.ts

@@ -3,14 +3,14 @@ import {
   RpcResponseAndContext,
   SimulatedTransactionResponse,
 } from "@solana/web3.js";
-import Provider from "../../provider";
-import { splitArgsAndCtx } from "../context";
-import { TransactionFn } from "./transaction";
-import { EventParser, Event } from "../event";
-import Coder from "../../coder";
-import { Idl, IdlEvent } from "../../idl";
-import { ProgramError } from "../../error";
-import * as features from "../../utils/features";
+import Provider from "../../provider.js";
+import { splitArgsAndCtx } from "../context.js";
+import { TransactionFn } from "./transaction.js";
+import { EventParser, Event } from "../event.js";
+import Coder from "../../coder/index.js";
+import { Idl, IdlEvent } from "../../idl.js";
+import { ProgramError } from "../../error.js";
+import * as features from "../../utils/features.js";
 import {
   AllInstructions,
   IdlTypes,

+ 18 - 17
ts/src/program/namespace/state.ts

@@ -6,24 +6,25 @@ import {
   Commitment,
   AccountMeta,
 } from "@solana/web3.js";
-import Provider from "../../provider";
+import Provider from "../../provider.js";
+import { Idl, IdlInstruction, IdlStateMethod, IdlTypeDef } from "../../idl.js";
+import Coder, { stateDiscriminator } from "../../coder/index.js";
 import {
-  Idl,
-  IdlAccountItem,
-  IdlInstruction,
-  IdlStateMethod,
-  IdlTypeDef,
-} from "../../idl";
-import Coder, { stateDiscriminator } from "../../coder";
-import { RpcNamespace, InstructionNamespace, TransactionNamespace } from "./";
-import { getProvider } from "../../";
-import { Subscription, validateAccounts, parseIdlErrors } from "../common";
-import { findProgramAddressSync, createWithSeedSync } from "../../utils/pubkey";
-import { Accounts } from "../context";
-import InstructionNamespaceFactory from "./instruction";
-import RpcNamespaceFactory from "./rpc";
-import TransactionNamespaceFactory from "./transaction";
-import { IdlTypes, TypeDef } from "./types";
+  RpcNamespace,
+  InstructionNamespace,
+  TransactionNamespace,
+} from "./index.js";
+import { getProvider } from "../../index.js";
+import { Subscription, validateAccounts, parseIdlErrors } from "../common.js";
+import {
+  findProgramAddressSync,
+  createWithSeedSync,
+} from "../../utils/pubkey.js";
+import { Accounts } from "../context.js";
+import InstructionNamespaceFactory from "./instruction.js";
+import RpcNamespaceFactory from "./rpc.js";
+import TransactionNamespaceFactory from "./transaction.js";
+import { IdlTypes, TypeDef } from "./types.js";
 
 export default class StateFactory {
   public static build<IDL extends Idl>(

+ 4 - 4
ts/src/program/namespace/transaction.ts

@@ -1,12 +1,12 @@
 import { Transaction } from "@solana/web3.js";
-import { Idl, IdlInstruction } from "../../idl";
-import { splitArgsAndCtx } from "../context";
-import { InstructionFn } from "./instruction";
+import { Idl, IdlInstruction } from "../../idl.js";
+import { splitArgsAndCtx } from "../context.js";
+import { InstructionFn } from "./instruction.js";
 import {
   AllInstructions,
   InstructionContextFn,
   MakeInstructionsNamespace,
-} from "./types";
+} from "./types.js";
 
 export default class TransactionFactory {
   public static build<IDL extends Idl, I extends AllInstructions<IDL>>(

+ 2 - 1
ts/src/provider.ts

@@ -1,3 +1,4 @@
+import { Buffer } from "buffer";
 import {
   Connection,
   Keypair,
@@ -11,7 +12,7 @@ import {
   SimulatedTransactionResponse,
   Commitment,
 } from "@solana/web3.js";
-import { isBrowser } from "./utils/common";
+import { isBrowser } from "./utils/common.js";
 
 /**
  * The network and wallet context used to send transactions paid for and signed

+ 1 - 0
ts/src/utils/bytes/base64.ts

@@ -1,3 +1,4 @@
+import { Buffer } from "buffer";
 import * as base64 from "base64-js";
 
 export function encode(data: Buffer): string {

+ 1 - 1
ts/src/utils/bytes/bs58.ts

@@ -1,4 +1,4 @@
-import * as bs58 from "bs58";
+import bs58 from "bs58";
 
 export function encode(data: Buffer | number[] | Uint8Array) {
   return bs58.encode(data);

+ 2 - 0
ts/src/utils/bytes/hex.ts

@@ -1,3 +1,5 @@
+import { Buffer } from "buffer";
+
 export function encode(data: Buffer): string {
   return data.reduce(
     (str, byte) => str + byte.toString(16).padStart(2, "0"),

+ 4 - 4
ts/src/utils/bytes/index.ts

@@ -1,4 +1,4 @@
-export * as hex from "./hex";
-export * as utf8 from "./utf8";
-export * as bs58 from "./bs58";
-export * as base64 from "./base64";
+export * as hex from "./hex.js";
+export * as utf8 from "./utf8.js";
+export * as bs58 from "./bs58.js";
+export * as base64 from "./base64.js";

+ 6 - 6
ts/src/utils/index.ts

@@ -1,6 +1,6 @@
-export * as sha256 from "./sha256";
-export * as rpc from "./rpc";
-export * as publicKey from "./pubkey";
-export * as bytes from "./bytes";
-export * as token from "./token";
-export * as features from "./features";
+export * as sha256 from "./sha256.js";
+export * as rpc from "./rpc.js";
+export * as publicKey from "./pubkey.js";
+export * as bytes from "./bytes/index.js";
+export * as token from "./token.js";
+export * as features from "./features.js";

+ 2 - 1
ts/src/utils/pubkey.ts

@@ -1,7 +1,8 @@
+import { Buffer } from "buffer";
 import BN from "bn.js";
 import { sha256 as sha256Sync } from "js-sha256";
 import { PublicKey } from "@solana/web3.js";
-import { Address, translateAddress } from "../program/common";
+import { Address, translateAddress } from "../program/common.js";
 
 // Sync version of web3.PublicKey.createWithSeed.
 export function createWithSeedSync(

+ 4 - 3
ts/src/utils/rpc.ts

@@ -1,3 +1,4 @@
+import { Buffer } from "buffer";
 import assert from "assert";
 import {
   AccountInfo,
@@ -8,9 +9,9 @@ import {
   Transaction,
   TransactionInstruction,
 } from "@solana/web3.js";
-import { chunks } from "../utils/common";
-import { Address, translateAddress } from "../program/common";
-import Provider, { getProvider } from "../provider";
+import { chunks } from "../utils/common.js";
+import { Address, translateAddress } from "../program/common.js";
+import Provider, { getProvider } from "../provider.js";
 
 /**
  * Sends a transaction to a program with the given accounts and instruction

+ 3 - 3
ts/src/workspace.ts

@@ -1,9 +1,9 @@
 import camelCase from "camelcase";
 import * as toml from "toml";
 import { PublicKey } from "@solana/web3.js";
-import { Program } from "./program";
-import { Idl } from "./idl";
-import { isBrowser } from "./utils/common";
+import { Program } from "./program/index.js";
+import { Idl } from "./idl.js";
+import { isBrowser } from "./utils/common.js";
 
 let _populatedWorkspace = false;
 

+ 1 - 1
ts/tsconfig.json

@@ -2,7 +2,7 @@
   "include": ["./src/**/*"],
   "compilerOptions": {
     "moduleResolution": "node",
-    "module": "es6",
+    "module": "es2022",
     "target": "es2019",
 
     "outDir": "dist/esm/",

Diff do ficheiro suprimidas por serem muito extensas
+ 393 - 244
ts/yarn.lock


Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff