From 450cfe102bfe3a20c553cafef0eff03549d88214 Mon Sep 17 00:00:00 2001 From: Jason Paulos Date: Fri, 13 Jan 2023 15:19:43 -0800 Subject: [PATCH 1/3] Don't use global `Buffer` object --- .eslintrc.js | 22 +++++++++++++++---- examples/app_transaction_examples.js | 1 + examples/asset_create_example.js | 1 + examples/logic_sig_example.js | 1 + examples/notefield_example.js | 1 + examples/typescript_example.ts | 1 + package-lock.json | 15 +------------ package.json | 3 +-- src/abi/abi_type.ts | 1 + src/bid.ts | 1 + src/client/client.ts | 1 + src/client/kmd.ts | 1 + src/client/urlTokenBaseHTTPClient.ts | 1 + src/client/v2/algod/compile.ts | 1 + src/client/v2/algod/dryrun.ts | 1 + .../v2/algod/getApplicationBoxByName.ts | 1 + src/client/v2/algod/models/types.ts | 1 + src/client/v2/algod/sendRawTransaction.ts | 1 + src/client/v2/basemodel.ts | 2 ++ .../v2/indexer/lookupAccountTransactions.ts | 1 + .../lookupApplicationBoxByIDandName.ts | 1 + src/client/v2/indexer/models/types.ts | 1 + src/composer.ts | 1 + src/dryrun.ts | 1 + src/encoding/address.ts | 1 + src/encoding/bigint.ts | 2 ++ src/encoding/uint64.ts | 4 +++- src/group.ts | 1 + src/logicsig.ts | 1 + src/main.ts | 1 + src/multisig.ts | 1 + src/transaction.ts | 1 + src/types/transactions/encoded.ts | 2 ++ src/utils/utils.ts | 3 +++ tests/1.Mnemonics_test.js | 1 + tests/10.ABI.ts | 1 + tests/2.Encoding.js | 2 ++ tests/3.Address.js | 1 + tests/4.Utils.ts | 1 + tests/5.Transaction.js | 2 ++ tests/6.Multisig.ts | 2 ++ tests/7.AlgoSDK.js | 2 ++ tests/8.LogicSig.ts | 2 ++ tests/9.Client.ts | 1 + tests/cucumber/browser/test.js | 2 ++ tests/cucumber/browser/webpack.config.js | 6 ----- tests/cucumber/steps/index.js | 5 +++-- tests/cucumber/steps/steps.js | 1 + tests/mocha.js | 1 + webpack.config.js | 6 ----- 50 files changed, 80 insertions(+), 35 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 78f7990e8..fcd70fb75 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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', + ], 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', diff --git a/examples/app_transaction_examples.js b/examples/app_transaction_examples.js index b4d3f3fd6..14529b72b 100644 --- a/examples/app_transaction_examples.js +++ b/examples/app_transaction_examples.js @@ -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'); diff --git a/examples/asset_create_example.js b/examples/asset_create_example.js index ee5387234..1bb8eb513 100644 --- a/examples/asset_create_example.js +++ b/examples/asset_create_example.js @@ -1,5 +1,6 @@ // Example: creating an asset +const { Buffer } = require('buffer'); const algosdk = require('../src'); const utils = require('./utils'); diff --git a/examples/logic_sig_example.js b/examples/logic_sig_example.js index 36b1370f6..11540f9b9 100644 --- a/examples/logic_sig_example.js +++ b/examples/logic_sig_example.js @@ -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'); diff --git a/examples/notefield_example.js b/examples/notefield_example.js index 2754db55a..067ce2a48 100644 --- a/examples/notefield_example.js +++ b/examples/notefield_example.js @@ -5,6 +5,7 @@ * want in the "note" field. */ +const { Buffer } = require('buffer'); const algosdk = require('../src'); const utils = require('./utils'); diff --git a/examples/typescript_example.ts b/examples/typescript_example.ts index f0e298c48..9dff30bc6 100644 --- a/examples/typescript_example.ts +++ b/examples/typescript_example.ts @@ -1,5 +1,6 @@ // Example: with TypeScript +import { Buffer } from 'buffer'; import algosdk from '../src'; import utils from './utils'; diff --git a/package-lock.json b/package-lock.json index 2d937dc54..29fa26a16 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "MIT", "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", @@ -23,7 +23,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", @@ -455,12 +454,6 @@ "@types/node": "*" } }, - "node_modules/@types/url-parse": { - "version": "1.4.3", - "resolved": "https://registry.npmjs.org/@types/url-parse/-/url-parse-1.4.3.tgz", - "integrity": "sha512-4kHAkbV/OfW2kb5BLVUuUMoumB3CP8rHqlw48aHvFy5tf9ER0AfOonBlX29l/DD68G70DmyhRlSYfQPSYpC5Vw==", - "dev": true - }, "node_modules/@types/yauzl": { "version": "2.9.1", "resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.9.1.tgz", @@ -8526,12 +8519,6 @@ "@types/node": "*" } }, - "@types/url-parse": { - "version": "1.4.3", - "resolved": "https://registry.npmjs.org/@types/url-parse/-/url-parse-1.4.3.tgz", - "integrity": "sha512-4kHAkbV/OfW2kb5BLVUuUMoumB3CP8rHqlw48aHvFy5tf9ER0AfOonBlX29l/DD68G70DmyhRlSYfQPSYpC5Vw==", - "dev": true - }, "@types/yauzl": { "version": "2.9.1", "resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.9.1.tgz", diff --git a/package.json b/package.json index 2db105465..989b5d5d1 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/abi/abi_type.ts b/src/abi/abi_type.ts index 3ea89e67c..cbcbd9b9d 100644 --- a/src/abi/abi_type.ts +++ b/src/abi/abi_type.ts @@ -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'; diff --git a/src/bid.ts b/src/bid.ts index 8d4485e76..7874d647c 100644 --- a/src/bid.ts +++ b/src/bid.ts @@ -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'; diff --git a/src/client/client.ts b/src/client/client.ts index b40a0a666..6a2036632 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -1,3 +1,4 @@ +import { Buffer } from 'buffer'; import * as utils from '../utils/utils'; import { BaseHTTPClient, diff --git a/src/client/kmd.ts b/src/client/kmd.ts index 3a1bd27c8..4c832b259 100644 --- a/src/client/kmd.ts +++ b/src/client/kmd.ts @@ -1,3 +1,4 @@ +import { Buffer } from 'buffer'; import ServiceClient from './v2/serviceClient'; import * as txn from '../transaction'; import { CustomTokenHeader, KMDTokenHeader } from './urlTokenBaseHTTPClient'; diff --git a/src/client/urlTokenBaseHTTPClient.ts b/src/client/urlTokenBaseHTTPClient.ts index 85c20d5f4..aba61fee5 100644 --- a/src/client/urlTokenBaseHTTPClient.ts +++ b/src/client/urlTokenBaseHTTPClient.ts @@ -1,3 +1,4 @@ +import { Buffer } from 'buffer'; import { fetch, Response, Headers } from 'cross-fetch'; import { BaseHTTPClient, diff --git a/src/client/v2/algod/compile.ts b/src/client/v2/algod/compile.ts index 85f02f182..db8769c0e 100644 --- a/src/client/v2/algod/compile.ts +++ b/src/client/v2/algod/compile.ts @@ -1,3 +1,4 @@ +import { Buffer } from 'buffer'; import JSONRequest from '../jsonrequest'; import HTTPClient from '../../client'; diff --git a/src/client/v2/algod/dryrun.ts b/src/client/v2/algod/dryrun.ts index 7e5b650c3..98b2fb463 100644 --- a/src/client/v2/algod/dryrun.ts +++ b/src/client/v2/algod/dryrun.ts @@ -1,3 +1,4 @@ +import { Buffer } from 'buffer'; import JSONRequest from '../jsonrequest'; import HTTPClient from '../../client'; import * as modelsv2 from './models/types'; diff --git a/src/client/v2/algod/getApplicationBoxByName.ts b/src/client/v2/algod/getApplicationBoxByName.ts index 29e832369..789fb0b16 100644 --- a/src/client/v2/algod/getApplicationBoxByName.ts +++ b/src/client/v2/algod/getApplicationBoxByName.ts @@ -1,3 +1,4 @@ +import { Buffer } from 'buffer'; import JSONRequest from '../jsonrequest'; import HTTPClient from '../../client'; import IntDecoding from '../../../types/intDecoding'; diff --git a/src/client/v2/algod/models/types.ts b/src/client/v2/algod/models/types.ts index a8f630390..4d13d8d91 100644 --- a/src/client/v2/algod/models/types.ts +++ b/src/client/v2/algod/models/types.ts @@ -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'; diff --git a/src/client/v2/algod/sendRawTransaction.ts b/src/client/v2/algod/sendRawTransaction.ts index e34e68c4a..8977c1c5f 100644 --- a/src/client/v2/algod/sendRawTransaction.ts +++ b/src/client/v2/algod/sendRawTransaction.ts @@ -1,3 +1,4 @@ +import { Buffer } from 'buffer'; import JSONRequest from '../jsonrequest'; import HTTPClient from '../../client'; import { concatArrays } from '../../../utils/utils'; diff --git a/src/client/v2/basemodel.ts b/src/client/v2/basemodel.ts index ffb0ea169..39fa52e57 100644 --- a/src/client/v2/basemodel.ts +++ b/src/client/v2/basemodel.ts @@ -1,3 +1,5 @@ +import { Buffer } from 'buffer'; + /** * Base class for models */ diff --git a/src/client/v2/indexer/lookupAccountTransactions.ts b/src/client/v2/indexer/lookupAccountTransactions.ts index b2343f1ca..12f48fa7e 100644 --- a/src/client/v2/indexer/lookupAccountTransactions.ts +++ b/src/client/v2/indexer/lookupAccountTransactions.ts @@ -1,3 +1,4 @@ +import { Buffer } from 'buffer'; import JSONRequest from '../jsonrequest'; import HTTPClient from '../../client'; import IntDecoding from '../../../types/intDecoding'; diff --git a/src/client/v2/indexer/lookupApplicationBoxByIDandName.ts b/src/client/v2/indexer/lookupApplicationBoxByIDandName.ts index 68b456aa8..94bc80175 100644 --- a/src/client/v2/indexer/lookupApplicationBoxByIDandName.ts +++ b/src/client/v2/indexer/lookupApplicationBoxByIDandName.ts @@ -1,3 +1,4 @@ +import { Buffer } from 'buffer'; import JSONRequest from '../jsonrequest'; import HTTPClient from '../../client'; import IntDecoding from '../../../types/intDecoding'; diff --git a/src/client/v2/indexer/models/types.ts b/src/client/v2/indexer/models/types.ts index 7f5e417fb..12b0555f4 100644 --- a/src/client/v2/indexer/models/types.ts +++ b/src/client/v2/indexer/models/types.ts @@ -3,6 +3,7 @@ */ /* eslint-disable no-use-before-define */ +import { Buffer } from 'buffer'; import BaseModel from '../../basemodel'; /** diff --git a/src/composer.ts b/src/composer.ts index 1376977f7..7fba1d3fa 100644 --- a/src/composer.ts +++ b/src/composer.ts @@ -1,3 +1,4 @@ +import { Buffer } from 'buffer'; import { ABIType, ABITupleType, diff --git a/src/dryrun.ts b/src/dryrun.ts index 5755e74c9..ce9739bc5 100644 --- a/src/dryrun.ts +++ b/src/dryrun.ts @@ -1,3 +1,4 @@ +import { Buffer } from 'buffer'; import AlgodClient from './client/v2/algod/algod'; import { AccountStateDelta, diff --git a/src/encoding/address.ts b/src/encoding/address.ts index dcd445fad..b1e287ebc 100644 --- a/src/encoding/address.ts +++ b/src/encoding/address.ts @@ -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'; diff --git a/src/encoding/bigint.ts b/src/encoding/bigint.ts index 1f1b15d78..cc039904d 100644 --- a/src/encoding/bigint.ts +++ b/src/encoding/bigint.ts @@ -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. diff --git a/src/encoding/uint64.ts b/src/encoding/uint64.ts index 3dc6e1a2e..ecd808b95 100644 --- a/src/encoding/uint64.ts +++ b/src/encoding/uint64.ts @@ -1,3 +1,5 @@ +import { Buffer } from 'buffer'; + /** * encodeUint64 converts an integer to its binary representation. * @param num - The number to convert. This must be an unsigned integer less than @@ -65,7 +67,7 @@ export function decodeUint64(data: any, decodingMode: any = 'safe') { const buf = Buffer.concat([padding, Buffer.from(data)]); const num = buf.readBigUInt64BE(); - const isBig = num > Number.MAX_SAFE_INTEGER; + const isBig = num > BigInt(Number.MAX_SAFE_INTEGER); if (decodingMode === 'safe') { if (isBig) { diff --git a/src/group.ts b/src/group.ts index c20d5bea7..0e26384d3 100644 --- a/src/group.ts +++ b/src/group.ts @@ -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'; diff --git a/src/logicsig.ts b/src/logicsig.ts index c63e6afbd..2644d73c1 100644 --- a/src/logicsig.ts +++ b/src/logicsig.ts @@ -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'; diff --git a/src/main.ts b/src/main.ts index 2fa4d21d9..f5bb807cd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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'; diff --git a/src/multisig.ts b/src/multisig.ts index 8efd5797a..7bf821d2d 100644 --- a/src/multisig.ts +++ b/src/multisig.ts @@ -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'; diff --git a/src/transaction.ts b/src/transaction.ts index a74778c6d..c91fdc457 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -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'; diff --git a/src/types/transactions/encoded.ts b/src/types/transactions/encoded.ts index 2f0f48ed0..7864e4e7d 100644 --- a/src/types/transactions/encoded.ts +++ b/src/types/transactions/encoded.ts @@ -1,3 +1,5 @@ +import { Buffer } from 'buffer'; + /** * Interfaces for the encoded transaction object. Every property is labelled with its associated Transaction type property */ diff --git a/src/utils/utils.ts b/src/utils/utils.ts index c504a091f..6abfbc00c 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -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' ); } diff --git a/tests/1.Mnemonics_test.js b/tests/1.Mnemonics_test.js index 9e388e7b9..de8b1835a 100644 --- a/tests/1.Mnemonics_test.js +++ b/tests/1.Mnemonics_test.js @@ -1,3 +1,4 @@ +/* eslint-env mocha */ const assert = require('assert'); const algosdk = require('../src/index'); const nacl = require('../src/nacl/naclWrappers'); diff --git a/tests/10.ABI.ts b/tests/10.ABI.ts index 2e778e21e..d8a8600af 100644 --- a/tests/10.ABI.ts +++ b/tests/10.ABI.ts @@ -1,3 +1,4 @@ +/* eslint-env mocha */ import assert from 'assert'; import { ABIAddressType, diff --git a/tests/2.Encoding.js b/tests/2.Encoding.js index 4961c969c..b3f6ca0c1 100644 --- a/tests/2.Encoding.js +++ b/tests/2.Encoding.js @@ -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'); diff --git a/tests/3.Address.js b/tests/3.Address.js index 78090f88f..73ac2b2c1 100644 --- a/tests/3.Address.js +++ b/tests/3.Address.js @@ -1,3 +1,4 @@ +/* eslint-env mocha */ const assert = require('assert'); const nacl = require('../src/nacl/naclWrappers'); const algosdk = require('../src/index'); diff --git a/tests/4.Utils.ts b/tests/4.Utils.ts index c372fed91..a2b589d4b 100644 --- a/tests/4.Utils.ts +++ b/tests/4.Utils.ts @@ -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'; diff --git a/tests/5.Transaction.js b/tests/5.Transaction.js index 0b2d3b378..7069d4b1a 100644 --- a/tests/5.Transaction.js +++ b/tests/5.Transaction.js @@ -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'); diff --git a/tests/6.Multisig.ts b/tests/6.Multisig.ts index b2030cee7..a2577e98a 100644 --- a/tests/6.Multisig.ts +++ b/tests/6.Multisig.ts @@ -1,3 +1,5 @@ +/* eslint-env mocha */ +import { Buffer } from 'buffer'; import assert from 'assert'; import algosdk from '../src/index'; import { diff --git a/tests/7.AlgoSDK.js b/tests/7.AlgoSDK.js index 94686e98b..4eaac1361 100644 --- a/tests/7.AlgoSDK.js +++ b/tests/7.AlgoSDK.js @@ -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'); diff --git a/tests/8.LogicSig.ts b/tests/8.LogicSig.ts index 13609ac12..f9c38a33a 100644 --- a/tests/8.LogicSig.ts +++ b/tests/8.LogicSig.ts @@ -1,3 +1,5 @@ +/* eslint-env mocha */ +import { Buffer } from 'buffer'; import assert from 'assert'; import algosdk from '../src/index'; diff --git a/tests/9.Client.ts b/tests/9.Client.ts index f662cefde..b10c842c3 100644 --- a/tests/9.Client.ts +++ b/tests/9.Client.ts @@ -1,3 +1,4 @@ +/* eslint-env mocha */ import assert from 'assert'; import HTTPClient from '../src/client/client'; import { URLTokenBaseHTTPClient } from '../src/client/urlTokenBaseHTTPClient'; diff --git a/tests/cucumber/browser/test.js b/tests/cucumber/browser/test.js index 7fd997d09..e276efcb3 100644 --- a/tests/cucumber/browser/test.js +++ b/tests/cucumber/browser/test.js @@ -1,3 +1,5 @@ +/* eslint-env browser */ +const { Buffer } = require('buffer'); const assert = require('assert'); const sha512 = require('js-sha512'); const nacl = require('tweetnacl'); diff --git a/tests/cucumber/browser/webpack.config.js b/tests/cucumber/browser/webpack.config.js index f0a59ef1c..d39c5439b 100644 --- a/tests/cucumber/browser/webpack.config.js +++ b/tests/cucumber/browser/webpack.config.js @@ -1,5 +1,4 @@ const path = require('path'); -const webpack = require('webpack'); module.exports = { entry: path.resolve(__dirname, 'test.js'), @@ -8,9 +7,4 @@ module.exports = { path: path.resolve(__dirname, 'build'), }, devtool: 'source-map', - plugins: [ - new webpack.ProvidePlugin({ - Buffer: ['buffer', 'Buffer'], - }), - ], }; diff --git a/tests/cucumber/steps/index.js b/tests/cucumber/steps/index.js index 0117dc84e..2b411ffa2 100644 --- a/tests/cucumber/steps/index.js +++ b/tests/cucumber/steps/index.js @@ -1,4 +1,5 @@ /* eslint-disable no-console,global-require,no-loop-func,func-names */ +const { Buffer } = require('buffer'); const path = require('path'); const fs = require('fs'); const { @@ -363,8 +364,8 @@ if (browser) { const done = rest[rest.length - 1]; try { const testArgs = rest.slice(0, rest.length - 1); - const test = window.getStep(scopedType, scopedName); - await test.apply(window.testWorld, testArgs); + const test = window.getStep(scopedType, scopedName); // eslint-disable-line no-undef + await test.apply(window.testWorld, testArgs); // eslint-disable-line no-undef done({ error: null }); } catch (err) { console.error(err); diff --git a/tests/cucumber/steps/steps.js b/tests/cucumber/steps/steps.js index 56af3f773..ec320ed69 100644 --- a/tests/cucumber/steps/steps.js +++ b/tests/cucumber/steps/steps.js @@ -1,4 +1,5 @@ /* eslint-disable func-names,radix */ +const { Buffer } = require('buffer'); const assert = require('assert'); const fs = require('fs'); const path = require('path'); diff --git a/tests/mocha.js b/tests/mocha.js index 666ab5034..d2202ea72 100644 --- a/tests/mocha.js +++ b/tests/mocha.js @@ -1,3 +1,4 @@ +/* eslint-env node, mocha */ /* eslint-disable no-console */ const Mocha = require('mocha'); const webpack = require('webpack'); diff --git a/webpack.config.js b/webpack.config.js index e1c672802..74f8bc7eb 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,5 +1,4 @@ const path = require('path'); -const webpack = require('webpack'); module.exports = { mode: 'production', @@ -17,11 +16,6 @@ module.exports = { // Add '.ts' as resolvable extensions extensions: ['.ts', '.js'], }, - plugins: [ - new webpack.ProvidePlugin({ - Buffer: ['buffer', 'Buffer'], - }), - ], module: { rules: [ // All files with a '.ts' extension will be handled by 'ts-loader'. From ca62f15083194c49a8103347fe81913a5d6457db Mon Sep 17 00:00:00 2001 From: Jason Paulos Date: Fri, 13 Jan 2023 16:25:56 -0800 Subject: [PATCH 2/3] Don't use Buffer to encode & decode uint64s --- src/encoding/uint64.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/encoding/uint64.ts b/src/encoding/uint64.ts index ecd808b95..2d3eea03d 100644 --- a/src/encoding/uint64.ts +++ b/src/encoding/uint64.ts @@ -1,4 +1,9 @@ -import { Buffer } from 'buffer'; +import { concatArrays } from '../utils/utils'; + +// NOTE: at the moment we specifically do not use Buffer.writeBigUInt64BE and +// Buffer.getBigUint64. 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. @@ -14,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); - - buf.writeBigUInt64BE(BigInt(num)); + const encoding = new Uint8Array(8); + const view = new DataView(encoding.buffer); + view.setBigUint64(0, BigInt(num)); - return new Uint8Array(buf); + return encoding; } /** @@ -61,12 +66,11 @@ 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 num = view.getBigUint64(0); const isBig = num > BigInt(Number.MAX_SAFE_INTEGER); if (decodingMode === 'safe') { From 8badc23c77fd0da69558a5c6aa4a4e502f37f6df Mon Sep 17 00:00:00 2001 From: Jason Paulos Date: Fri, 13 Jan 2023 16:33:26 -0800 Subject: [PATCH 3/3] Fix wrong function name in comment --- src/encoding/uint64.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/encoding/uint64.ts b/src/encoding/uint64.ts index 2d3eea03d..d19099654 100644 --- a/src/encoding/uint64.ts +++ b/src/encoding/uint64.ts @@ -1,9 +1,9 @@ import { concatArrays } from '../utils/utils'; // NOTE: at the moment we specifically do not use Buffer.writeBigUInt64BE and -// Buffer.getBigUint64. 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. +// 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.