Skip to content

Commit

Permalink
chore: don't export compilers depending on node in builds for browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jan 12, 2023
1 parent 8239fa0 commit f02fefe
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 75 deletions.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
},
"module": "es/index.mjs",
"browser": {
"es/index.mjs": "./es/index-browser.mjs",
"dist/aepp-sdk.js": "./dist/aepp-sdk.browser.js"
},
"exports": {
"import": "./es/index.mjs",
"node": "./dist/aepp-sdk.js",
"node": {
"import": "./es/index.mjs",
"default": "./dist/aepp-sdk.js"
},
"import": "./es/index-browser.mjs",
"default": "./dist/aepp-sdk.browser.js"
},
"sideEffects": false,
Expand Down
14 changes: 5 additions & 9 deletions src/contract/compiler/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
import { RestError } from '@azure/core-rest-pipeline';
import { readFile } from 'fs/promises';
import {
Compiler as CompilerApi,
ErrorModel,
Expand All @@ -24,8 +23,7 @@ import {
import { genErrorFormatterPolicy, genVersionCheckPolicy } from '../../utils/autorest';
import CompilerBase, { Aci } from './Base';
import { Encoded } from '../../utils/encoder';
import { CompilerError } from '../../utils/errors';
import getFileSystem from './getFileSystem';
import { CompilerError, NotImplementedError } from '../../utils/errors';

type GeneralCompilerError = ErrorModel & {
info?: object;
Expand Down Expand Up @@ -97,10 +95,9 @@ export default class CompilerHttp extends CompilerBase {
}
}

// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars
async compile(path: string): Promise<{ bytecode: Encoded.ContractBytearray; aci: Aci }> {
const fileSystem = await getFileSystem(path);
const sourceCode = await readFile(path, 'utf8');
return this.compileBySourceCode(sourceCode, fileSystem);
throw new NotImplementedError('File system access, use CompilerHttpNode instead');
}

async validateBySourceCode(
Expand All @@ -116,10 +113,9 @@ export default class CompilerHttp extends CompilerBase {
}
}

// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars
async validate(bytecode: Encoded.ContractBytearray, path: string): Promise<boolean> {
const fileSystem = await getFileSystem(path);
const sourceCode = await readFile(path, 'utf8');
return this.validateBySourceCode(bytecode, sourceCode, fileSystem);
throw new NotImplementedError('File system access, use CompilerHttpNode instead');
}

async version(): Promise<string> {
Expand Down
26 changes: 26 additions & 0 deletions src/contract/compiler/HttpNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { readFile } from 'fs/promises';
import HttpBrowser from './Http';
import { Aci } from './Base';
import { Encoded } from '../../utils/encoder';
import getFileSystem from './getFileSystem';

/**
* Contract Compiler over HTTP for Nodejs
*
* Inherits CompilerHttp and implements `compile`, `validate` methods
* @category contract
* @example CompilerHttpNode('COMPILER_URL')
*/
export default class CompilerHttpNode extends HttpBrowser {
override async compile(path: string): Promise<{ bytecode: Encoded.ContractBytearray; aci: Aci }> {
const fileSystem = await getFileSystem(path);
const sourceCode = await readFile(path, 'utf8');
return this.compileBySourceCode(sourceCode, fileSystem);
}

override async validate(bytecode: Encoded.ContractBytearray, path: string): Promise<boolean> {
const fileSystem = await getFileSystem(path);
const sourceCode = await readFile(path, 'utf8');
return this.validateBySourceCode(bytecode, sourceCode, fileSystem);
}
}
58 changes: 58 additions & 0 deletions src/index-browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* ISC License (ISC)
* Copyright (c) 2022 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/

export * from './chain';
export * from './utils/crypto';
export * from './utils/keystore';
export * from './utils/bytes';
export * from './tx';
export * from './tx/builder';
export * from './tx/builder/helpers';
export * from './tx/builder/constants';
export * from './tx/builder/schema';
export * from './utils/amount-formatter';
export * from './utils/hd-wallet';
export { encode, decode, Encoding } from './utils/encoder';
export * from './aens';
export { default as Contract } from './contract/Contract';
export type { ContractMethodsBase } from './contract/Contract';
export * from './oracle';
export * from './spend';
export * from './contract/ga';

export { default as AeSdkMethods } from './AeSdkMethods';
export { default as AeSdkBase } from './AeSdkBase';
export { default as AeSdk } from './AeSdk';
export { default as AeSdkAepp } from './AeSdkAepp';
export { default as AeSdkWallet } from './AeSdkWallet';
export { default as Node } from './Node';
export { default as verifyTransaction } from './tx/validator';
export { default as AccountBase } from './account/Base';
export { default as MemoryAccount } from './account/Memory';
export { default as AccountGeneralized } from './account/Generalized';
export { default as AccountLedger } from './account/Ledger';
export { default as AccountLedgerFactory } from './account/LedgerFactory';
export { default as CompilerBase } from './contract/compiler/Base';
export { default as CompilerHttp } from './contract/compiler/Http';
export { default as Channel } from './channel/Contract';

export { default as connectionProxy } from './aepp-wallet-communication/connection-proxy';
export * from './aepp-wallet-communication/schema';
export { default as walletDetector } from './aepp-wallet-communication/wallet-detector';
export { default as BrowserRuntimeConnection } from './aepp-wallet-communication/connection/BrowserRuntime';
export { default as BrowserWindowMessageConnection } from './aepp-wallet-communication/connection/BrowserWindowMessage';
export * from './utils/errors';
59 changes: 2 additions & 57 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,5 @@
/*
* ISC License (ISC)
* Copyright (c) 2022 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
export * from './index-browser';

export * from './chain';
export * from './utils/crypto';
export * from './utils/keystore';
export * from './utils/bytes';
export * from './tx';
export * from './tx/builder';
export * from './tx/builder/helpers';
export * from './tx/builder/constants';
export * from './tx/builder/schema';
export * from './utils/amount-formatter';
export * from './utils/hd-wallet';
export { encode, decode, Encoding } from './utils/encoder';
export * from './aens';
export { default as Contract } from './contract/Contract';
export type { ContractMethodsBase } from './contract/Contract';
export * from './oracle';
export * from './spend';
export * from './contract/ga';

export { default as AeSdkMethods } from './AeSdkMethods';
export { default as AeSdkBase } from './AeSdkBase';
export { default as AeSdk } from './AeSdk';
export { default as AeSdkAepp } from './AeSdkAepp';
export { default as AeSdkWallet } from './AeSdkWallet';
export { default as Node } from './Node';
export { default as verifyTransaction } from './tx/validator';
export { default as AccountBase } from './account/Base';
export { default as MemoryAccount } from './account/Memory';
export { default as AccountGeneralized } from './account/Generalized';
export { default as AccountLedger } from './account/Ledger';
export { default as AccountLedgerFactory } from './account/LedgerFactory';
export { default as CompilerBase } from './contract/compiler/Base';
export { default as CompilerHttp } from './contract/compiler/Http';
export { default as CompilerCli } from './contract/compiler/Cli';
export { default as getFileSystem } from './contract/compiler/getFileSystem';
export { default as Channel } from './channel/Contract';

export { default as connectionProxy } from './aepp-wallet-communication/connection-proxy';
export * from './aepp-wallet-communication/schema';
export { default as walletDetector } from './aepp-wallet-communication/wallet-detector';
export { default as BrowserRuntimeConnection } from './aepp-wallet-communication/connection/BrowserRuntime';
export { default as BrowserWindowMessageConnection } from './aepp-wallet-communication/connection/BrowserWindowMessage';
export * from './utils/errors';
export { default as CompilerHttpNode } from './contract/compiler/HttpNode';
6 changes: 3 additions & 3 deletions test/integration/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { describe, it } from 'mocha';
import { readFile } from 'fs/promises';
import { compilerUrl, ignoreVersion } from '.';
import {
CompilerBase, CompilerHttp, CompilerCli, CompilerError, getFileSystem,
CompilerBase, CompilerHttpNode, CompilerCli, CompilerError, getFileSystem,
} from '../../src';
import { Encoded } from '../../src/utils/encoder';

Expand Down Expand Up @@ -97,11 +97,11 @@ function testCompiler(compiler: CompilerBase): void {
}

describe('CompilerHttp', () => {
const compiler = new CompilerHttp(compilerUrl, { ignoreVersion });
const compiler = new CompilerHttpNode(compilerUrl, { ignoreVersion });
testCompiler(compiler);

it('throws exception if used invalid compiler url', async () => {
const c = new CompilerHttp('https://compiler.aepps.comas');
const c = new CompilerHttpNode('https://compiler.aepps.comas');
await expect(c.compileBySourceCode('test'))
.to.be.rejectedWith('getaddrinfo ENOTFOUND compiler.aepps.comas');
});
Expand Down
4 changes: 2 additions & 2 deletions test/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import {
AeSdk, CompilerHttp, MemoryAccount, Node,
AeSdk, CompilerHttpNode, MemoryAccount, Node,
} from '../../src';
import '..';

Expand All @@ -30,7 +30,7 @@ const genesisAccount = new MemoryAccount(secretKey);
export async function getSdk(accountCount = 1): Promise<AeSdk> {
const accounts = new Array(accountCount).fill(null).map(() => MemoryAccount.generate());
const sdk = new AeSdk({
onCompiler: new CompilerHttp(compilerUrl, { ignoreVersion }),
onCompiler: new CompilerHttpNode(compilerUrl, { ignoreVersion }),
nodes: [{ name: 'test', instance: new Node(url, { ignoreVersion }) }],
accounts,
_expectedMineRate: 1000,
Expand Down
4 changes: 2 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const babelConfig = require('./babel.config');
function configure(filename, opts = {}) {
const isNode = opts.target.includes('node');
return (env) => ({
entry: './src/index.ts',
entry: `./src/index${isNode ? '' : '-browser'}.ts`,
mode: 'production',
devtool: 'source-map',
module: {
Expand All @@ -25,7 +25,7 @@ function configure(filename, opts = {}) {
},
resolve: {
extensions: ['.ts', '.js'],
fallback: opts.target.includes('browser') && {
fallback: isNode ? {} : {
buffer: require.resolve('buffer/'),
child_process: false,
os: false,
Expand Down

0 comments on commit f02fefe

Please sign in to comment.