UNPKG

gnablib

Version:

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

2 lines 2.3 kB
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */ import{ColType as t}from"./ColType.js";import{ACudColType as e}from"./ACudColType.js";import{utf8 as s}from"../../codec/Utf8.js";import{FromBinResult as n}from"../../primitive/FromBinResult.js";import{TypeProblem as r}from"../../error/probs/TypeProblem.js";import{ContentError as i}from"../../error/ContentError.js";import{sLen as o}from"../../safe/safe.js";class l extends e{constructor(t=!1){super(t),this.sqliteType="TEXT",this.postgresType="text"}cudByteSize(t){return t.length+this._lenBytes}valid(t){if(null==t)return this.nullable?void 0:r.Null("Utf8");o("input",t).atMost(this._maxStrLen).throwNot()}unknownBin(t){if(null==t){if(!this.nullable)throw new i("cannot be null","String",void 0);return new Uint8Array([0])}if("string"!=typeof t)throw new TypeError("String required");const e=s.toBytes(t);o("value-bytes",e).atMost(this._maxStrLen).throwNot();const n=new Uint8Array(this._lenBytes+e.length);let r=this._lenBytes-1;return n[r--]=e.length,r>0&&(n[r--]=e.length>>8),r>0&&(n[r--]=e.length>>16),r>0&&(n[r--]=e.length>>24),n.set(e,this._lenBytes),n}binUnknown(t,e){if(e+this._lenBytes>t.length)return new n(0,void 0,"Utf8.binUnknown unable to find length");let r=0;for(let s=0;s<this._lenBytes;s++)r=r<<8|t[e++];if(r>this._maxStrLen)return new n(0,void 0,`Utf8.binUnknown length invalid (0<${this._maxStrLen} got ${r})`);const i=e+r;if(i>t.length)return new n(0,void 0,"Utf8.binUnknown missing data");if(0===r&&this.nullable)return new n(this._lenBytes,void 0);const o=s.fromBytes(t.slice(e,i));return new n(r+this._lenBytes,o)}}export class Utf81 extends l{constructor(){super(...arguments),this._colType=t.Utf81,this._lenBytes=1,this._maxStrLen=255,this.mysqlType="TINYTEXT",this.cudType="utf8-1"}}export class Utf82 extends l{constructor(){super(...arguments),this._colType=t.Utf82,this._lenBytes=2,this._maxStrLen=65535,this.mysqlType="TEXT",this.cudType="utf8-2"}}export class Utf83 extends l{constructor(){super(...arguments),this._colType=t.Utf83,this._lenBytes=3,this._maxStrLen=16777215,this.mysqlType="MEDIUMTEXT",this.cudType="utf8-3"}}export class Utf84ish extends l{constructor(){super(...arguments),this._colType=t.Utf84ish,this._lenBytes=4,this._maxStrLen=1e9,this.mysqlType="LONGTEXT",this.cudType="utf8-4"}}