UNPKG

gnablib

Version:

A lean, zero dependency library to provide a useful base for your project.

2 lines 7.04 kB
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */ import{fpb16 as e,fpb32 as t,fpb64 as n}from"../ieee754-fpb.js";import{utf8 as r}from"../Utf8.js";import{BinResult as i}from"../../primitive/FromBinResult.js";import{DateTimeLocal as s}from"../../datetime/dt.js";import{BitWriter as a}from"../../primitive/BitWriter.js";import{BitReader as o}from"../../primitive/BitReader.js";import{sNum as l}from"../../safe/safe.js";import{I64 as c}from"../../primitive/number/I64.js";import{U64 as u}from"../../primitive/number/U64.js";export var Type;function y(e){if(e.eq(c.zero))return new Uint8Array([Type.Zero]);const t=e.toBytesBE(),n=new Uint8Array(9);switch(t.length){case 1:return new Uint8Array([Type.Int_1,t[0]]);case 2:return n[0]=Type.Int_2,n.set(t,1),n.slice(0,3);case 4:return n[0]=Type.Int_4,n.set(t,1),n.slice(0,5);case 8:return n[0]=Type.Int_8,n.set(t,1),n;default:return n[0]=Type.Int_Var,n.set(t,1),n.slice(0,t.length+1)}}!function(e){e[e.Null=0]="Null",e[e.Zero=1]="Zero",e[e.UInt_Var=2]="UInt_Var",e[e.UInt_1=3]="UInt_1",e[e.UInt_2=4]="UInt_2",e[e.UInt_4=5]="UInt_4",e[e.UInt_8=6]="UInt_8",e[e.UInt_16=7]="UInt_16",e[e.Int_Var=8]="Int_Var",e[e.Int_1=9]="Int_1",e[e.Int_2=10]="Int_2",e[e.Int_4=11]="Int_4",e[e.Int_8=12]="Int_8",e[e.Int_16=13]="Int_16",e[e.True=14]="True",e[e.False=15]="False",e[e.DateTime=16]="DateTime",e[e.Utf8_0=17]="Utf8_0",e[e.Utf8_1=18]="Utf8_1",e[e.Utf8_2=19]="Utf8_2",e[e.Utf8_3=20]="Utf8_3",e[e.Utf8_4=21]="Utf8_4",e[e.Float_2=22]="Float_2",e[e.Float_4=23]="Float_4",e[e.Float_8=24]="Float_8",e[e.Float_16=25]="Float_16",e[e.Bin_0=26]="Bin_0",e[e.Bin_1=27]="Bin_1",e[e.Bin_2=28]="Bin_2",e[e.Bin_3=29]="Bin_3",e[e.Bin_4=30]="Bin_4",e[e.Decimal_4=31]="Decimal_4",e[e.Decimal_8=32]="Decimal_8",e[e.Decimal_16=33]="Decimal_16"}(Type||(Type={}));Number.MAX_SAFE_INTEGER;export function encode(e){switch(typeof e){case"undefined":return new Uint8Array([Type.Null]);case"number":return Number.isInteger(e)?y(c.fromInt(e)):function(e){const t=n.toBytes(e),r=new Uint8Array(9);return r[0]=Type.Float_8,r.set(t,1),r}(e);case"boolean":return new Uint8Array([e?Type.True:Type.False]);case"string":return function(e){const t=r.toBytes(e);if(0===t.length)return new Uint8Array([Type.Utf8_0]);if(t.length<=255){const e=new Uint8Array(t.length+2);return e[0]=Type.Utf8_1,e[1]=t.length,e.set(t,2),e}if(t.length<=65535){const e=new Uint8Array(t.length+3);return e[0]=Type.Utf8_2,e[1]=t.length>>8,e[2]=t.length,e.set(t,3),e}if(t.length<=16777215){const e=new Uint8Array(t.length+4);return e[0]=Type.Utf8_3,e[1]=t.length>>16,e[2]=t.length>>8,e[3]=t.length,e.set(t,4),e}const n=new Uint8Array(t.length+5);return n[0]=Type.Utf8_4,n[1]=t.length>>24,n[2]=t.length>>16,n[3]=t.length>>8,n[4]=t.length,n.set(t,5),n}(e);case"object":break;default:throw new TypeError("Cannot encoded "+typeof e)}if(e instanceof u)return function(e){if(e.eq(u.zero))return new Uint8Array([Type.Zero]);const t=e.toBytesBE(),n=new Uint8Array(9);switch(t.length){case 1:return new Uint8Array([Type.UInt_1,t[0]]);case 2:return n[0]=Type.UInt_2,n.set(t,1),n.slice(0,3);case 4:return n[0]=Type.UInt_4,n.set(t,1),n.slice(0,5);case 8:return n[0]=Type.UInt_8,n.set(t,1),n;default:return n[0]=Type.UInt_Var,n.set(t,1),n.slice(0,t.length+1)}}(e);if(e instanceof c)return y(e);if(e instanceof s){const t=new Uint8Array(1+e.serialSizeBits/8),n=a.mount(t);return n.pushNumberBE(Type.DateTime,8),e.serialize(n),t}if(e instanceof Uint8Array)return function(e){if(0===e.length)return new Uint8Array([Type.Bin_0]);if(e.length<=255){const t=new Uint8Array(e.length+2);return t[0]=Type.Bin_1,t[1]=e.length,t.set(e,2),t}if(e.length<=65535){const t=new Uint8Array(e.length+3);return t[0]=Type.Bin_2,t[1]=e.length>>8,t[2]=e.length,t.set(e,3),t}if(e.length<=16777215){const t=new Uint8Array(e.length+4);return t[0]=Type.Bin_3,t[1]=e.length>>16,t[2]=e.length>>8,t[3]=e.length,t.set(e,4),t}const t=new Uint8Array(e.length+5);return t[0]=Type.Bin_4,t[1]=e.length>>24,t[2]=e.length>>16,t[3]=e.length>>8,t[4]=e.length,t.set(e,5),t}(e);if(null==e)return new Uint8Array([Type.Null]);throw new TypeError(`Cannot encode ${e}`)}function f(e,t){if(t+1>e.length)return"decode unable to find length";const n=e[t++];return t+n>e.length?"decode missing data":n}function g(e,t){if(t+2>e.length)return"decode unable to find length";const n=e[t++]<<8|e[t++];return t+n>e.length?"decode missing data":n}function d(e,t){if(t+3>e.length)return"decode unable to find length";const n=e[t++]<<16|e[t++]<<8|e[t++];return t+n>e.length?"decode missing data":n}function _(e,t){if(t+4>e.length)return"decode unable to find length";const n=e[t++]<<24|e[t++]<<16|e[t++]<<8|e[t++];return t+n>e.length?"decode missing data":n}export function decode(a,l){var u;let y=l;if(l>=a.length)return"decode unable to fine type";let p;switch(a[y++]){case Type.Null:return new i(1,void 0);case Type.Zero:return new i(1,0);case Type.UInt_Var:return"Not implemented";case Type.Int_Var:try{c.fromBytesBE(a,l)}catch(e){console.log("Oh dear")}return l+1>a.length?"decode missing data":new i(2,c.fromBytesBE(a,l));case Type.Int_1:return l+1>a.length?"decode missing data":new i(2,c.fromBytesBE(a,l));case Type.Int_2:return l+2>a.length?"decode missing data":new i(3,c.fromBytesBE(a,l));case Type.Int_4:return l+4>a.length?"decode missing data":new i(5,c.fromBytesBE(a,l));case Type.Int_8:return l+8>a.length?"decode missing data":new i(9,c.fromBytesBE(a,l));case Type.True:return new i(1,!0);case Type.False:return new i(1,!1);case Type.DateTime:if(l+8>a.length)return"decode missing data";try{const e=o.mount(a.subarray(l)),t=s.deserialize(e);return new i(9,t)}catch(e){return"decode failed DateTime "+(null!==(u=e.message)&&void 0!==u?u:"")}case Type.Utf8_0:return new i(1,"");case Type.Utf8_1:return p=f(a,l),"string"==typeof p?p:(l+=1)+p>a.length?"decode missing data":new i(2+p,r.fromBytes(a.slice(l,l+p)));case Type.Utf8_2:return p=g(a,l),"string"==typeof p?p:(l+=2)+p>a.length?"decode missing data":new i(3+p,r.fromBytes(a.slice(l,l+p)));case Type.Utf8_3:return p=d(a,l),"string"==typeof p?p:(l+=3)+p>a.length?"decode missing data":new i(4+p,r.fromBytes(a.slice(l,l+p)));case Type.Utf8_4:return p=_(a,l),"string"==typeof p?p:(l+=4)+p>a.length?"decode missing data":new i(5+p,r.fromBytes(a.slice(l,l+p)));case Type.Float_2:return l+2>a.length?"decode missing data":new i(3,e.fromBytes(a,l));case Type.Float_4:return l+4>a.length?"decode missing data":new i(5,t.fromBytes(a,l));case Type.Float_8:return l+8>a.length?"decode missing data":new i(9,n.fromBytes(a,l));case Type.Bin_0:return new i(1,new Uint8Array(0));case Type.Bin_1:return p=f(a,l),"string"==typeof p?p:(l+=1)+p>a.length?"decode missing data":new i(2+p,a.slice(l,l+p));case Type.Bin_2:return p=g(a,l),"string"==typeof p?p:(l+=2)+p>a.length?"decode missing data":new i(3+p,a.slice(l,l+p));case Type.Bin_3:return p=d(a,l),"string"==typeof p?p:(l+=3)+p>a.length?"decode missing data":new i(4+p,a.slice(l,l+p));case Type.Bin_4:return p=_(a,l),"string"==typeof p?p:(l+=4)+p>a.length?"decode missing data":new i(5+p,a.slice(l,l+p))}return"Not implemented"}