From 87ecd8c622c6b602a23af9fa2ecc50820bce32f8 Mon Sep 17 00:00:00 2001 From: Timo Glastra Date: Thu, 20 Jan 2022 22:11:37 +0100 Subject: [PATCH] fix: disallow usage of global buffer (#601) --- .eslintrc.js | 17 +++++++++++++++++ .../src/utils/__tests__/HashlinkEncoder.test.ts | 1 + .../utils/__tests__/MultibaseEncoder.test.ts | 1 + .../utils/__tests__/MultihashEncoder.test.ts | 1 + packages/core/src/utils/did.ts | 1 + 5 files changed, 21 insertions(+) diff --git a/.eslintrc.js b/.eslintrc.js index ba1a4ff8e2..adbe497b4b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -52,6 +52,23 @@ module.exports = { ], }, overrides: [ + { + files: ['packages/core/**'], + rules: { + 'no-restricted-globals': [ + 'error', + { + name: 'Buffer', + message: 'Global buffer is not supported on all platforms. Import buffer from `src/utils/buffer`', + }, + { + name: 'AbortController', + message: + "Global AbortController is not supported on all platforms. Use `import { AbortController } from 'abort-controller'`", + }, + ], + }, + }, { files: ['jest.config.ts', '.eslintrc.js'], env: { diff --git a/packages/core/src/utils/__tests__/HashlinkEncoder.test.ts b/packages/core/src/utils/__tests__/HashlinkEncoder.test.ts index 30700569b0..adf916866c 100644 --- a/packages/core/src/utils/__tests__/HashlinkEncoder.test.ts +++ b/packages/core/src/utils/__tests__/HashlinkEncoder.test.ts @@ -1,4 +1,5 @@ import { HashlinkEncoder } from '../HashlinkEncoder' +import { Buffer } from '../buffer' const validData = { data: Buffer.from('Hello World!'), diff --git a/packages/core/src/utils/__tests__/MultibaseEncoder.test.ts b/packages/core/src/utils/__tests__/MultibaseEncoder.test.ts index 28b0d080ed..49858a48d6 100644 --- a/packages/core/src/utils/__tests__/MultibaseEncoder.test.ts +++ b/packages/core/src/utils/__tests__/MultibaseEncoder.test.ts @@ -1,5 +1,6 @@ import { BufferEncoder } from '../BufferEncoder' import { MultiBaseEncoder } from '../MultiBaseEncoder' +import { Buffer } from '../buffer' const validData = Buffer.from('Hello World!') const validMultiBase = 'zKWfinQuRQ3ekD1danFHqvKRg9koFp8vpokUeREEgjSyHwweeKDFaxVHi' diff --git a/packages/core/src/utils/__tests__/MultihashEncoder.test.ts b/packages/core/src/utils/__tests__/MultihashEncoder.test.ts index e5bcac867d..38b3eeddb4 100644 --- a/packages/core/src/utils/__tests__/MultihashEncoder.test.ts +++ b/packages/core/src/utils/__tests__/MultihashEncoder.test.ts @@ -1,5 +1,6 @@ import { BufferEncoder } from '../BufferEncoder' import { MultiHashEncoder } from '../MultiHashEncoder' +import { Buffer } from '../buffer' const validData = Buffer.from('Hello World!') const validMultiHash = new Uint8Array([18, 12, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]) diff --git a/packages/core/src/utils/did.ts b/packages/core/src/utils/did.ts index 8622ccbb37..566ca4791b 100644 --- a/packages/core/src/utils/did.ts +++ b/packages/core/src/utils/did.ts @@ -16,6 +16,7 @@ */ import { BufferEncoder } from './BufferEncoder' +import { Buffer } from './buffer' export const FULL_VERKEY_REGEX = /^[1-9A-HJ-NP-Za-km-z]{43,44}$/ export const ABBREVIATED_VERKEY_REGEX = /^~[1-9A-HJ-NP-Za-km-z]{21,22}$/