UNPKG

@vulppi/toolbelt

Version:

A collection of tools and utilities.

2 lines (1 loc) 1.84 kB
import{Buffer as c}from"buffer";import{brotliCompress as p,brotliDecompress as u,createBrotliCompress as l,createBrotliDecompress as m,createDeflate as d,createGunzip as b,createGzip as y,createInflate as g,deflate as B,gunzip as w,gzip as z,inflate as R}from"zlib";function P(o){if(!o)return null;let a=o.trim(),e=a.match(/^Bearer\s+(.+)$/i);if(e)return{type:"bearer",token:e[1]};let t=a.match(/^Basic\s+(.+)$/i);if(t)try{let n=c.from(t[1],"base64").toString("utf-8"),[s,i]=n.split(":");return{type:"basic",username:s,password:i}}catch{return null}return null}var r={gzip:/^(?:x-)?gzip$/i,deflate:/^deflate$/i,identity:/^identity$/i,br:/^br$/i};async function $(o,a=["identity"]){let e=o;for(let t of a)r.identity.test(t)||(r.gzip.test(t)?e=await new Promise((n,s)=>{w(e,(i,f)=>{if(i)return s(i);n(f)})}):r.deflate.test(t)?e=await new Promise((n,s)=>{R(e,(i,f)=>{if(i)return s(i);n(f)})}):r.br.test(t)&&(e=await new Promise((n,s)=>{u(e,(i,f)=>{if(i)return s(i);n(f)})})));return e}async function h(o,a=["identity"]){let e=o;for(let t of a.toReversed())r.identity.test(t)||(r.gzip.test(t)?e=await new Promise((n,s)=>{z(e,(i,f)=>{if(i)return s(i);n(f)})}):r.deflate.test(t)?e=await new Promise((n,s)=>{B(e,(i,f)=>{if(i)return s(i);n(f)})}):r.br.test(t)&&(e=await new Promise((n,s)=>{p(e,(i,f)=>{if(i)return s(i);n(f)})})));return e}function q(o,a=["identity"]){let e=o;for(let t of a)r.identity.test(t)||(r.gzip.test(t)?e=e.pipe(b()):r.deflate.test(t)?e=e.pipe(g()):r.br.test(t)&&(e=e.pipe(m())));return e}function D(o,a=["identity"]){let e=o;for(let t of a.toReversed())r.identity.test(t)||(r.gzip.test(t)?e=e.pipe(y()):r.deflate.test(t)?e=e.pipe(d()):r.br.test(t)&&(e=e.pipe(l())));return e}export{P as extractTokenFromAuthorization,h as parseCompressBuffer,D as parseCompressStream,$ as parseDecompressBuffer,q as parseDecompressStream};