diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index cc178200..a71ded23 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -9,6 +9,7 @@ export * from './utils/tl-json' export * from './utils/async-lock' export * from './utils/lru-map' export * from './utils/function-utils' +export { encodeUrlSafeBase64, parseUrlSafeBase64 } from './utils/buffer-utils' export { BinaryReader } from './utils/binary/binary-reader' export { BinaryWriter } from './utils/binary/binary-writer' diff --git a/packages/core/src/utils/buffer-utils.ts b/packages/core/src/utils/buffer-utils.ts index f8f89908..84a40884 100644 --- a/packages/core/src/utils/buffer-utils.ts +++ b/packages/core/src/utils/buffer-utils.ts @@ -53,3 +53,17 @@ export function cloneBuffer(buf: Buffer, start = 0, end = buf.length): Buffer { buf.copy(ret, 0, start, end) return ret } + +export function parseUrlSafeBase64(str: string): Buffer { + str = str.replace(/-/g, '+').replace(/_/g, '/') + while (str.length % 4) str += '=' + return Buffer.from(str, 'base64') +} + +export function encodeUrlSafeBase64(buf: Buffer): string { + return buf + .toString('base64') + .replace(/\+/g, '-') + .replace(/\//g, '_') + .replace(/=+$/g, '') +} diff --git a/packages/core/tests/buffer-utils.spec.ts b/packages/core/tests/buffer-utils.spec.ts index ad738a2e..c897d5f0 100644 --- a/packages/core/tests/buffer-utils.spec.ts +++ b/packages/core/tests/buffer-utils.spec.ts @@ -3,6 +3,8 @@ import { expect } from 'chai' import { buffersEqual, cloneBuffer, + encodeUrlSafeBase64, + parseUrlSafeBase64, randomBytes, xorBuffer, xorBufferInPlace, @@ -111,6 +113,27 @@ describe('cloneBuffer', () => { }) }) +describe('parseUrlSafeBase64', () => { + it('should parse url-safe base64', () => { + expect(parseUrlSafeBase64('qu7d8aGTeuF6-g').toString('hex')).eq( + 'aaeeddf1a1937ae17afa' + ) + }) + it('should parse normal base64', () => { + expect(parseUrlSafeBase64('qu7d8aGTeuF6+g==').toString('hex')).eq( + 'aaeeddf1a1937ae17afa' + ) + }) +}) + +describe('encodeUrlSafeBase64', () => { + it('should encode to url-safe base64', () => { + expect( + encodeUrlSafeBase64(Buffer.from('aaeeddf1a1937ae17afa', 'hex')) + ).eq('qu7d8aGTeuF6-g') + }) +}) + // describe('isProbablyPlainText', () => { // it('should return true for buffers only containing printable ascii', () => { // expect( diff --git a/packages/file-id/src/parse.ts b/packages/file-id/src/parse.ts index 9757fb36..652d5cf2 100644 --- a/packages/file-id/src/parse.ts +++ b/packages/file-id/src/parse.ts @@ -1,6 +1,6 @@ -import { parseUrlSafeBase64, telegramRleDecode } from './utils' +import { telegramRleDecode } from './utils' import { tdFileId as td } from './types' -import { BinaryReader } from '@mtcute/core' +import { BinaryReader, parseUrlSafeBase64 } from '@mtcute/core' function parseWebFileLocation( reader: BinaryReader diff --git a/packages/file-id/src/serialize-unique.ts b/packages/file-id/src/serialize-unique.ts index 9c503a04..dfde5de2 100644 --- a/packages/file-id/src/serialize-unique.ts +++ b/packages/file-id/src/serialize-unique.ts @@ -1,6 +1,6 @@ import { tdFileId, tdFileId as td } from './types' -import { BinaryWriter } from '@mtcute/core/src/utils/binary/binary-writer' -import { encodeUrlSafeBase64, telegramRleEncode } from './utils' +import { encodeUrlSafeBase64, BinaryWriter } from '@mtcute/core' +import { telegramRleEncode } from './utils' import FileType = tdFileId.FileType type InputUniqueLocation = diff --git a/packages/file-id/src/serialize.ts b/packages/file-id/src/serialize.ts index 93f62a23..6cf4673e 100644 --- a/packages/file-id/src/serialize.ts +++ b/packages/file-id/src/serialize.ts @@ -1,6 +1,6 @@ import { tdFileId as td } from './types' -import { BinaryWriter } from '@mtcute/core/src/utils/binary/binary-writer' -import { encodeUrlSafeBase64, telegramRleEncode } from './utils' +import { encodeUrlSafeBase64, BinaryWriter } from '@mtcute/core' +import { telegramRleEncode } from './utils' const SUFFIX = Buffer.from([td.CURRENT_VERSION, td.PERSISTENT_ID_VERSION]) diff --git a/packages/file-id/src/utils.ts b/packages/file-id/src/utils.ts index fb236c04..75821901 100644 --- a/packages/file-id/src/utils.ts +++ b/packages/file-id/src/utils.ts @@ -1,17 +1,3 @@ -export function parseUrlSafeBase64(str: string): Buffer { - str = str.replace(/-/g, '+').replace(/_/g, '/') - while (str.length % 4) str += '=' - return Buffer.from(str, 'base64') -} - -export function encodeUrlSafeBase64(buf: Buffer): string { - return buf - .toString('base64') - .replace(/\+/g, '-') - .replace(/\//g, '_') - .replace(/=+$/g, '') -} - // telegram has some cursed RLE which only encodes consecutive \x00 export function telegramRleEncode(buf: Buffer): Buffer { diff --git a/packages/file-id/tests/utils.spec.ts b/packages/file-id/tests/utils.spec.ts index 44ff9134..79c2a2ea 100644 --- a/packages/file-id/tests/utils.spec.ts +++ b/packages/file-id/tests/utils.spec.ts @@ -1,33 +1,10 @@ import { describe, it } from 'mocha' import { expect } from 'chai' import { - encodeUrlSafeBase64, - parseUrlSafeBase64, telegramRleDecode, telegramRleEncode, } from '../src/utils' -describe('parseUrlSafeBase64', () => { - it('should parse url-safe base64', () => { - expect(parseUrlSafeBase64('qu7d8aGTeuF6-g').toString('hex')).eq( - 'aaeeddf1a1937ae17afa' - ) - }) - it('should parse normal base64', () => { - expect(parseUrlSafeBase64('qu7d8aGTeuF6+g==').toString('hex')).eq( - 'aaeeddf1a1937ae17afa' - ) - }) -}) - -describe('encodeUrlSafeBase64', () => { - it('should encode to url-safe base64', () => { - expect( - encodeUrlSafeBase64(Buffer.from('aaeeddf1a1937ae17afa', 'hex')) - ).eq('qu7d8aGTeuF6-g') - }) -}) - describe('telegramRleEncode', () => { it('should not modify input if there are no \\x00', () => { expect(