gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 3.5 kB
JavaScript
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */
import{FromBinResult as n}from"../../primitive/FromBinResult.js";import{ColType as t}from"./ColType.js";import{ACudColType as e}from"./ACudColType.js";import{TypeProblem as i}from"../../error/probs/TypeProblem.js";import{ContentError as r}from"../../error/ContentError.js";import{sLen as o}from"../../safe/safe.js";const l=16777216,s=1e9;class u extends e{constructor(n=!1){super(n),this.sqliteType="BLOB",this.postgresType="bytea"}_valid(n,t){if(null==n){if(!this.nullable)return i.Null("input")}else o("input",n).atMost(t).throwNot()}}export class Bin1 extends u{constructor(){super(...arguments),this.mysqlType="TINYBLOB",this.cudType="bin1",this._colType=t.Bin1}cudByteSize(n){return n.length+1}valid(n){return this._valid(n,255)}unknownBin(n){if(!n){if(!this.nullable)throw new r("cannot be null","Bin",void 0);return new Uint8Array([0])}o("value",n).atMost(255).throwNot();const t=new Uint8Array(1+n.length);return t[0]=n.length,t.set(n,1),t}binUnknown(t,e){if(e+1>t.length)return new n(0,void 0,"Bin1.binUnknown unable to find length byte");const i=t[e++];return e+i>t.length?new n(0,void 0,"Bin1.binUnknown missing data"):0===i&&this.nullable?new n(1,void 0):new n(i+1,t.slice(e,e+i))}}export class Bin2 extends u{constructor(){super(...arguments),this.mysqlType="BLOB",this.cudType="bin2",this._colType=t.Bin2}cudByteSize(n){return n.length+2}valid(n){return this._valid(n,65536)}unknownBin(n){if(!n){if(!this.nullable)throw new r("cannot be null","Bin",void 0);return new Uint8Array([0])}o("value",n).atMost(65536).throwNot();const t=new Uint8Array(2+n.length);return t[0]=n.length>>8,t[1]=n.length,t.set(n,2),t}binUnknown(t,e){if(e+2>t.length)return new n(0,void 0,"Bin2.binUnknown unable to find length bytes");const i=t[e++]<<8|t[e++];return e+i>t.length?new n(0,void 0,"Bin2.binUnknown missing data"):0===i&&this.nullable?new n(2,void 0):new n(i+2,t.slice(e,e+i))}}export class Bin3 extends u{constructor(){super(...arguments),this.mysqlType="MEDIUMBLOB",this.cudType="bin3",this._colType=t.Bin3}cudByteSize(n){return n.length+3}valid(n){return this._valid(n,l)}unknownBin(n){if(!n){if(!this.nullable)throw new r("cannot be null","Bin",void 0);return new Uint8Array([0])}o("value",n).atMost(l).throwNot();const t=new Uint8Array(3+n.length);return t[0]=n.length>>16,t[1]=n.length>>8,t[2]=n.length,t.set(n,3),t}binUnknown(t,e){if(e+3>t.length)return new n(0,void 0,"Bin3.binUnknown unable to find length bytes");const i=t[e++]<<16|t[e++]<<8|t[e++];return e+i>t.length?new n(0,void 0,"Bin3.binUnknown missing data"):0===i&&this.nullable?new n(3,void 0):new n(i+3,t.slice(e,e+i))}}export class Bin4ish extends u{constructor(){super(...arguments),this.mysqlType="LONGBLOB",this.cudType="bin4",this._colType=t.Bin4ish}cudByteSize(n){return n.length+4}valid(n){return this._valid(n,s)}unknownBin(n){if(!n){if(!this.nullable)throw new r("cannot be null","Bin",void 0);return new Uint8Array([0])}o("value",n).atMost(s).throwNot();const t=new Uint8Array(4+n.length);return t[0]=n.length>>>24,t[1]=n.length>>16,t[2]=n.length>>8,t[3]=n.length,t.set(n,4),t}binUnknown(t,e){if(e+4>t.length)return new n(0,void 0,"Bin4ish.binUnknown unable to find length bytes");const i=(t[e++]<<24|t[e++]<<16|t[e++]<<8|t[e++])>>>0;if(i>s)return new n(0,void 0,`Bin4ish.binUnknown size invalid (0<1000000000 got ${i})`);return e+i>t.length?new n(0,void 0,"Bin4ish.binUnknown missing data"):0===i&&this.nullable?new n(4,void 0):new n(i+4,t.slice(e,e+i))}}