Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packaging: Don't use global Buffer object #733

Merged
merged 3 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
module.exports = {
env: {
browser: true,
es2021: true,
node: true,
mocha: true,
'shared-node-browser': true,
},
extends: ['airbnb-base', 'prettier', 'plugin:import/typescript'],
extends: [
'airbnb-base',
'prettier',
'plugin:import/typescript',
'eslint:recommended',
],
Comment on lines +4 to +11
Copy link
Contributor Author

@jasonpaulos jasonpaulos Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes aren't explicitly necessary, but I thought I would include them since I'm making changes to our eslint settings.

  • shared-node-browser causes eslint to only recognize global variables present in both node and browser contexts, instead of either.
  • I removed mocha from the global list of environments in favor of adding /* eslint-env mocha */ at the top of each mocha test file. This way we won't accidentally reference mocha global variables from our production files.
  • eslint:recommended contains a set of recommended rules. It only caused one new issue in our codebase (which I fixed by configuring no-constant-condition below), so I figured it was a good idea to include.

parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 12,
},
plugins: ['@typescript-eslint', 'eslint-plugin-tsdoc'],
rules: {
'no-restricted-globals': [
'error',
{
// This is to ensure that we use the 'buffer' package in the browser. In Node it doesn't
// make a difference.
name: 'Buffer',
message:
"Explictly import Buffer with `import { Buffer } from 'buffer'`",
},
],
'no-constant-condition': ['error', { checkLoops: false }],
'no-restricted-syntax': ['error', 'LabeledStatement', 'WithStatement'],
'no-plusplus': ['error', { allowForLoopAfterthoughts: true }],
'max-classes-per-file': 'off',
Expand Down
1 change: 1 addition & 0 deletions examples/app_transaction_examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// makeApplicationCreateTxnFromObject, makeApplicationOptInTxnFromObject, etc.
// counterparts in your code for readability.

const { Buffer } = require('buffer');
const algosdk = require('../src');
const utils = require('./utils');

Expand Down
1 change: 1 addition & 0 deletions examples/asset_create_example.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Example: creating an asset

const { Buffer } = require('buffer');
const algosdk = require('../src');
const utils = require('./utils');

Expand Down
1 change: 1 addition & 0 deletions examples/logic_sig_example.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Example: creating a LogicSig transaction signed by a program that never approves the transfer.

const { Buffer } = require('buffer');
const algosdk = require('../src');
const utils = require('./utils');

Expand Down
1 change: 1 addition & 0 deletions examples/notefield_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* want in the "note" field.
*/

const { Buffer } = require('buffer');
const algosdk = require('../src');
const utils = require('./utils');

Expand Down
1 change: 1 addition & 0 deletions examples/typescript_example.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Example: with TypeScript

import { Buffer } from 'buffer';
import algosdk from '../src';
import utils from './utils';

Expand Down
15 changes: 1 addition & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"dependencies": {
"algo-msgpack-with-bigint": "^2.1.1",
"buffer": "^6.0.2",
"buffer": "^6.0.3",
"cross-fetch": "^3.1.5",
"hi-base32": "^0.5.1",
"js-sha256": "^0.9.0",
Expand All @@ -35,7 +35,6 @@
"devDependencies": {
"@types/json-bigint": "^1.0.0",
"@types/mocha": "^8.2.2",
"@types/url-parse": "^1.4.3",
"@typescript-eslint/eslint-plugin": "^4.26.1",
"@typescript-eslint/parser": "^4.26.1",
"assert": "^2.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/abi/abi_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// | string
// | (T1, ..., Tn)
*/
import { Buffer } from 'buffer';
import { encodeAddress, decodeAddress } from '../encoding/address';
import { bigIntToBytes, bytesToBigInt } from '../encoding/bigint';
import { concatArrays } from '../utils/utils';
Expand Down
1 change: 1 addition & 0 deletions src/bid.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import * as address from './encoding/address';
import * as encoding from './encoding/encoding';
import * as nacl from './nacl/naclWrappers';
Expand Down
1 change: 1 addition & 0 deletions src/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import * as utils from '../utils/utils';
import {
BaseHTTPClient,
Expand Down
1 change: 1 addition & 0 deletions src/client/kmd.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import ServiceClient from './v2/serviceClient';
import * as txn from '../transaction';
import { CustomTokenHeader, KMDTokenHeader } from './urlTokenBaseHTTPClient';
Expand Down
1 change: 1 addition & 0 deletions src/client/urlTokenBaseHTTPClient.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import { fetch, Response, Headers } from 'cross-fetch';
import {
BaseHTTPClient,
Expand Down
1 change: 1 addition & 0 deletions src/client/v2/algod/compile.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';

Expand Down
1 change: 1 addition & 0 deletions src/client/v2/algod/dryrun.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import * as modelsv2 from './models/types';
Expand Down
1 change: 1 addition & 0 deletions src/client/v2/algod/getApplicationBoxByName.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import IntDecoding from '../../../types/intDecoding';
Expand Down
1 change: 1 addition & 0 deletions src/client/v2/algod/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

/* eslint-disable no-use-before-define */
import { Buffer } from 'buffer';
import BaseModel from '../../basemodel';
import { EncodedSignedTransaction } from '../../../../types/transactions/encoded';
import BlockHeader from '../../../../types/blockHeader';
Expand Down
1 change: 1 addition & 0 deletions src/client/v2/algod/sendRawTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import { concatArrays } from '../../../utils/utils';
Expand Down
2 changes: 2 additions & 0 deletions src/client/v2/basemodel.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Buffer } from 'buffer';

/**
* Base class for models
*/
Expand Down
1 change: 1 addition & 0 deletions src/client/v2/indexer/lookupAccountTransactions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import IntDecoding from '../../../types/intDecoding';
Expand Down
1 change: 1 addition & 0 deletions src/client/v2/indexer/lookupApplicationBoxByIDandName.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import IntDecoding from '../../../types/intDecoding';
Expand Down
1 change: 1 addition & 0 deletions src/client/v2/indexer/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

/* eslint-disable no-use-before-define */
import { Buffer } from 'buffer';
import BaseModel from '../../basemodel';

/**
Expand Down
1 change: 1 addition & 0 deletions src/composer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import {
ABIType,
ABITupleType,
Expand Down
1 change: 1 addition & 0 deletions src/dryrun.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import AlgodClient from './client/v2/algod/algod';
import {
AccountStateDelta,
Expand Down
1 change: 1 addition & 0 deletions src/encoding/address.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import base32 from 'hi-base32';
import * as nacl from '../nacl/naclWrappers';
import * as utils from '../utils/utils';
Expand Down
2 changes: 2 additions & 0 deletions src/encoding/bigint.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Buffer } from 'buffer';

/**
* bigIntToBytes converts a BigInt to a big-endian Uint8Array for encoding.
* @param bi - The bigint to convert.
Expand Down
26 changes: 16 additions & 10 deletions src/encoding/uint64.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import { concatArrays } from '../utils/utils';

// NOTE: at the moment we specifically do not use Buffer.writeBigUInt64BE and
// Buffer.readBigUInt64BE. This is because projects using webpack v4
// automatically include an old version of the npm `buffer` package (v4.9.2 at
// the time of writing), and this old version does not have these methods.

/**
* encodeUint64 converts an integer to its binary representation.
* @param num - The number to convert. This must be an unsigned integer less than
Expand All @@ -12,11 +19,11 @@ export function encodeUint64(num: number | bigint) {
throw new Error('Input is not a 64-bit unsigned integer');
}

const buf = Buffer.allocUnsafe(8);
const encoding = new Uint8Array(8);
const view = new DataView(encoding.buffer);
view.setBigUint64(0, BigInt(num));

buf.writeBigUInt64BE(BigInt(num));

return new Uint8Array(buf);
return encoding;
}

/**
Expand Down Expand Up @@ -59,13 +66,12 @@ export function decodeUint64(data: any, decodingMode: any = 'safe') {
}

// insert 0s at the beginning if data is smaller than 8 bytes
const padding = Buffer.allocUnsafe(8 - data.byteLength);
padding.fill(0);

const buf = Buffer.concat([padding, Buffer.from(data)]);
const padding = new Uint8Array(8 - data.byteLength);
const encoding = concatArrays(padding, data);
const view = new DataView(encoding.buffer);

const num = buf.readBigUInt64BE();
const isBig = num > Number.MAX_SAFE_INTEGER;
const num = view.getBigUint64(0);
const isBig = num > BigInt(Number.MAX_SAFE_INTEGER);

if (decodingMode === 'safe') {
if (isBig) {
Expand Down
1 change: 1 addition & 0 deletions src/group.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import * as txnBuilder from './transaction';
import * as nacl from './nacl/naclWrappers';
import * as encoding from './encoding/encoding';
Expand Down
1 change: 1 addition & 0 deletions src/logicsig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import * as nacl from './nacl/naclWrappers';
import * as address from './encoding/address';
import * as encoding from './encoding/encoding';
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import * as nacl from './nacl/naclWrappers';
import * as address from './encoding/address';
import * as encoding from './encoding/encoding';
Expand Down
1 change: 1 addition & 0 deletions src/multisig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import * as nacl from './nacl/naclWrappers';
import * as address from './encoding/address';
import * as encoding from './encoding/encoding';
Expand Down
1 change: 1 addition & 0 deletions src/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'buffer';
import base32 from 'hi-base32';
import * as address from './encoding/address';
import * as encoding from './encoding/encoding';
Expand Down
2 changes: 2 additions & 0 deletions src/types/transactions/encoded.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Buffer } from 'buffer';

/**
* Interfaces for the encoded transaction object. Every property is labelled with its associated Transaction type property
*/
Expand Down
3 changes: 3 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ export function removeUndefinedProperties(
*/
export function isNode() {
return (
// @ts-ignore
typeof process === 'object' &&
// @ts-ignore
typeof process.versions === 'object' &&
// @ts-ignore
typeof process.versions.node !== 'undefined'
);
}
1 change: 1 addition & 0 deletions tests/1.Mnemonics_test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-env mocha */
const assert = require('assert');
const algosdk = require('../src/index');
const nacl = require('../src/nacl/naclWrappers');
Expand Down
1 change: 1 addition & 0 deletions tests/10.ABI.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-env mocha */
import assert from 'assert';
import {
ABIAddressType,
Expand Down
2 changes: 2 additions & 0 deletions tests/2.Encoding.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-env mocha */
const { Buffer } = require('buffer');
const assert = require('assert');
const algosdk = require('../src/index');
const utils = require('../src/utils/utils');
Expand Down
1 change: 1 addition & 0 deletions tests/3.Address.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-env mocha */
const assert = require('assert');
const nacl = require('../src/nacl/naclWrappers');
const algosdk = require('../src/index');
Expand Down
1 change: 1 addition & 0 deletions tests/4.Utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-env mocha */
import assert from 'assert';
import * as utils from '../src/utils/utils';
import * as nacl from '../src/nacl/naclWrappers';
Expand Down
2 changes: 2 additions & 0 deletions tests/5.Transaction.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-env mocha */
const { Buffer } = require('buffer');
const assert = require('assert');
const algosdk = require('../src/index');
const { translateBoxReferences } = require('../src/boxStorage');
Expand Down
2 changes: 2 additions & 0 deletions tests/6.Multisig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-env mocha */
import { Buffer } from 'buffer';
import assert from 'assert';
import algosdk from '../src/index';
import {
Expand Down
2 changes: 2 additions & 0 deletions tests/7.AlgoSDK.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-env mocha */
const { Buffer } = require('buffer');
const assert = require('assert');
const algosdk = require('../src/index');
const nacl = require('../src/nacl/naclWrappers');
Expand Down
2 changes: 2 additions & 0 deletions tests/8.LogicSig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-env mocha */
import { Buffer } from 'buffer';
import assert from 'assert';
import algosdk from '../src/index';

Expand Down
1 change: 1 addition & 0 deletions tests/9.Client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-env mocha */
import assert from 'assert';
import HTTPClient from '../src/client/client';
import { URLTokenBaseHTTPClient } from '../src/client/urlTokenBaseHTTPClient';
Expand Down
2 changes: 2 additions & 0 deletions tests/cucumber/browser/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-env browser */
const { Buffer } = require('buffer');
const assert = require('assert');
const sha512 = require('js-sha512');
const nacl = require('tweetnacl');
Expand Down
6 changes: 0 additions & 6 deletions tests/cucumber/browser/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const path = require('path');
const webpack = require('webpack');

module.exports = {
entry: path.resolve(__dirname, 'test.js'),
Expand All @@ -8,9 +7,4 @@ module.exports = {
path: path.resolve(__dirname, 'build'),
},
devtool: 'source-map',
plugins: [
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
}),
],
};
Loading