refactor(util): remove using `Buffer` from `encode.ts` (#925)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 71945cb · Browse files · 2 files·+16 added−69 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+16 added−69 removed
- other:+16 added−69 removed
Provenance
- Commit
b9227b8- Tree
4b7ee36- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
71945cb
2 files changed+16 added−69 removed
modified:deno_dist/utils/encode.ts+8 added-35 removed
1123467895671011121314151617891011121324252627281415161729303132333435361819202119 unmodified lines41424344646545666768697071724647484950import { Buffer } from "https://deno.land/std@0.177.0/node/buffer.ts";export const encodeBase64 = (str: string): string => { if (str === null) { throw new TypeError('1st argument of "encodeBase64" should not be null.') } try { const encoder = new TextEncoder() const bytes = encoder.encode(str) return btoa(String.fromCharCode(...bytes)) const encoder = new TextEncoder() const bytes = encoder.encode(str) return btoa(String.fromCharCode(...bytes)) } catch {}
try { return Buffer.from(str).toString('base64') } catch (e) { console.error('Error: If you want to do "encodeBase64", polyfill "buffer" module.') throw e }}
export const decodeBase64 = (str: string): string => { if (str === null) { throw new TypeError('1st argument of "decodeBase64" should not be null.') } try { const text = atob(str) const bytes = new Uint8Array(text.split('').map((c) => c.charCodeAt(0))) const decoder = new TextDecoder() return decoder.decode(bytes) const text = atob(str) const bytes = new Uint8Array(text.split('').map((c) => c.charCodeAt(0))) const decoder = new TextDecoder() return decoder.decode(bytes) } catch {}
try { return Buffer.from(str, 'base64').toString() } catch (e) { console.error('Error: If you want to do "decodeBase64", polyfill "buffer" module.') throw e }}
export const encodeBase64URL = (str: string): string => { return encodeBase64(str).replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')19 unmodified lines return encoder.encode(str)}
export const arrayBufferToBase64 = async (buf: ArrayBuffer): Promise<string> => { if (typeof btoa === 'function') { return btoa(String.fromCharCode(...new Uint8Array(buf))) return btoa(String.fromCharCode(...new Uint8Array(buf))) }
try { return Buffer.from(String.fromCharCode(...new Uint8Array(buf))).toString('base64') } catch (e) {}
return ''}
export const arrayBufferToBase64URL = async (buf: ArrayBuffer) => { return (await arrayBufferToBase64(buf)).replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')}modified:src/utils/encode.ts+8 added-34 removed
12345678567910111213141516891011121323242526271415161728293031323334351819202119 unmodified lines41424344636445656667686970714647484950export const encodeBase64 = (str: string): string => { if (str === null) { throw new TypeError('1st argument of "encodeBase64" should not be null.') } try { const encoder = new TextEncoder() const bytes = encoder.encode(str) return btoa(String.fromCharCode(...bytes)) const encoder = new TextEncoder() const bytes = encoder.encode(str) return btoa(String.fromCharCode(...bytes)) } catch {}
try { return Buffer.from(str).toString('base64') } catch (e) { console.error('Error: If you want to do "encodeBase64", polyfill "buffer" module.') throw e }}
export const decodeBase64 = (str: string): string => { if (str === null) { throw new TypeError('1st argument of "decodeBase64" should not be null.') } try { const text = atob(str) const bytes = new Uint8Array(text.split('').map((c) => c.charCodeAt(0))) const decoder = new TextDecoder() return decoder.decode(bytes) const text = atob(str) const bytes = new Uint8Array(text.split('').map((c) => c.charCodeAt(0))) const decoder = new TextDecoder() return decoder.decode(bytes) } catch {}
try { return Buffer.from(str, 'base64').toString() } catch (e) { console.error('Error: If you want to do "decodeBase64", polyfill "buffer" module.') throw e }}
export const encodeBase64URL = (str: string): string => { return encodeBase64(str).replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')19 unmodified lines return encoder.encode(str)}
export const arrayBufferToBase64 = async (buf: ArrayBuffer): Promise<string> => { if (typeof btoa === 'function') { return btoa(String.fromCharCode(...new Uint8Array(buf))) return btoa(String.fromCharCode(...new Uint8Array(buf))) }
try { return Buffer.from(String.fromCharCode(...new Uint8Array(buf))).toString('base64') } catch (e) {}
return ''}
export const arrayBufferToBase64URL = async (buf: ArrayBuffer) => { return (await arrayBufferToBase64(buf)).replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')}