refactor: moved urlsafe base64 functions to core and exported them
This commit is contained in:
parent
98fe7e3d31
commit
a2013acaf6
8 changed files with 44 additions and 43 deletions
|
@ -9,6 +9,7 @@ export * from './utils/tl-json'
|
||||||
export * from './utils/async-lock'
|
export * from './utils/async-lock'
|
||||||
export * from './utils/lru-map'
|
export * from './utils/lru-map'
|
||||||
export * from './utils/function-utils'
|
export * from './utils/function-utils'
|
||||||
|
export { encodeUrlSafeBase64, parseUrlSafeBase64 } from './utils/buffer-utils'
|
||||||
|
|
||||||
export { BinaryReader } from './utils/binary/binary-reader'
|
export { BinaryReader } from './utils/binary/binary-reader'
|
||||||
export { BinaryWriter } from './utils/binary/binary-writer'
|
export { BinaryWriter } from './utils/binary/binary-writer'
|
||||||
|
|
|
@ -53,3 +53,17 @@ export function cloneBuffer(buf: Buffer, start = 0, end = buf.length): Buffer {
|
||||||
buf.copy(ret, 0, start, end)
|
buf.copy(ret, 0, start, end)
|
||||||
return ret
|
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, '')
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ import { expect } from 'chai'
|
||||||
import {
|
import {
|
||||||
buffersEqual,
|
buffersEqual,
|
||||||
cloneBuffer,
|
cloneBuffer,
|
||||||
|
encodeUrlSafeBase64,
|
||||||
|
parseUrlSafeBase64,
|
||||||
randomBytes,
|
randomBytes,
|
||||||
xorBuffer,
|
xorBuffer,
|
||||||
xorBufferInPlace,
|
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', () => {
|
// describe('isProbablyPlainText', () => {
|
||||||
// it('should return true for buffers only containing printable ascii', () => {
|
// it('should return true for buffers only containing printable ascii', () => {
|
||||||
// expect(
|
// expect(
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { parseUrlSafeBase64, telegramRleDecode } from './utils'
|
import { telegramRleDecode } from './utils'
|
||||||
import { tdFileId as td } from './types'
|
import { tdFileId as td } from './types'
|
||||||
import { BinaryReader } from '@mtcute/core'
|
import { BinaryReader, parseUrlSafeBase64 } from '@mtcute/core'
|
||||||
|
|
||||||
function parseWebFileLocation(
|
function parseWebFileLocation(
|
||||||
reader: BinaryReader
|
reader: BinaryReader
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { tdFileId, tdFileId as td } from './types'
|
import { tdFileId, tdFileId as td } from './types'
|
||||||
import { BinaryWriter } from '@mtcute/core/src/utils/binary/binary-writer'
|
import { encodeUrlSafeBase64, BinaryWriter } from '@mtcute/core'
|
||||||
import { encodeUrlSafeBase64, telegramRleEncode } from './utils'
|
import { telegramRleEncode } from './utils'
|
||||||
import FileType = tdFileId.FileType
|
import FileType = tdFileId.FileType
|
||||||
|
|
||||||
type InputUniqueLocation =
|
type InputUniqueLocation =
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { tdFileId as td } from './types'
|
import { tdFileId as td } from './types'
|
||||||
import { BinaryWriter } from '@mtcute/core/src/utils/binary/binary-writer'
|
import { encodeUrlSafeBase64, BinaryWriter } from '@mtcute/core'
|
||||||
import { encodeUrlSafeBase64, telegramRleEncode } from './utils'
|
import { telegramRleEncode } from './utils'
|
||||||
|
|
||||||
const SUFFIX = Buffer.from([td.CURRENT_VERSION, td.PERSISTENT_ID_VERSION])
|
const SUFFIX = Buffer.from([td.CURRENT_VERSION, td.PERSISTENT_ID_VERSION])
|
||||||
|
|
||||||
|
|
|
@ -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
|
// telegram has some cursed RLE which only encodes consecutive \x00
|
||||||
|
|
||||||
export function telegramRleEncode(buf: Buffer): Buffer {
|
export function telegramRleEncode(buf: Buffer): Buffer {
|
||||||
|
|
|
@ -1,33 +1,10 @@
|
||||||
import { describe, it } from 'mocha'
|
import { describe, it } from 'mocha'
|
||||||
import { expect } from 'chai'
|
import { expect } from 'chai'
|
||||||
import {
|
import {
|
||||||
encodeUrlSafeBase64,
|
|
||||||
parseUrlSafeBase64,
|
|
||||||
telegramRleDecode,
|
telegramRleDecode,
|
||||||
telegramRleEncode,
|
telegramRleEncode,
|
||||||
} from '../src/utils'
|
} 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', () => {
|
describe('telegramRleEncode', () => {
|
||||||
it('should not modify input if there are no \\x00', () => {
|
it('should not modify input if there are no \\x00', () => {
|
||||||
expect(
|
expect(
|
||||||
|
|
Loading…
Reference in a new issue