OverviewFilesCommitsIssuesDocsReleasesPeers

holepunchto-deps / @holepunchto / legacy-keet-core-schemas / node_modules / compact-encoding / raw.js

1 lines · javascript ref:
const e=require("b4a"),{BE:r}=require("./endian");exports=module.exports={preencode(e,r){e.end+=r.byteLength},encode(e,r){e.buffer.set(r,e.start),e.start+=r.byteLength},decode(e){const r=e.buffer.subarray(e.start,e.end);return e.start=e.end,r}};const t=exports.buffer={preencode(e,r){r?o.preencode(e,r):e.end++},encode(e,r){r?o.encode(e,r):e.buffer[e.start++]=0},decode(e){const r=e.buffer.subarray(e.start);return 0===r.byteLength?null:(e.start=e.end,r)}};function n(e,t){const n=e.BYTES_PER_ELEMENT;return{preencode(e,r){e.end+=r.byteLength},encode(e,n){const o=new Uint8Array(n.buffer,n.byteOffset,n.byteLength);r&&t&&t(o),e.buffer.set(o,e.start),e.start+=n.byteLength},decode(o){let s=o.buffer.subarray(o.start);return s.byteOffset%n!==0&&(s=new Uint8Array(s)),r&&t&&t(s),o.start=o.end,new e(s.buffer,s.byteOffset,s.byteLength/n)}}}exports.binary={...t,preencode(e,r){"string"==typeof r?a.preencode(e,r):t.preencode(e,r)},encode(e,r){"string"==typeof r?a.encode(e,r):t.encode(e,r)}},exports.arraybuffer={preencode(e,r){e.end+=r.byteLength},encode(e,r){const t=new Uint8Array(r);e.buffer.set(t,e.start),e.start+=r.byteLength},decode(e){const r=new ArrayBuffer(e.end-e.start);return new Uint8Array(r).set(e.buffer.subarray(e.start)),e.start=e.end,r}};const o=exports.uint8array=n(Uint8Array);function s(r){return{preencode(t,n){t.end+=e.byteLength(n,r)},encode(t,n){t.start+=e.write(t.buffer,n,t.start,r)},decode(t){const n=e.toString(t.buffer,r,t.start);return t.start=t.end,n}}}exports.uint16array=n(Uint16Array,e.swap16),exports.uint32array=n(Uint32Array,e.swap32),exports.int8array=n(Int8Array),exports.int16array=n(Int16Array,e.swap16),exports.int32array=n(Int32Array,e.swap32),exports.biguint64array=n(BigUint64Array,e.swap64),exports.bigint64array=n(BigInt64Array,e.swap64),exports.float32array=n(Float32Array,e.swap32),exports.float64array=n(Float64Array,e.swap64);const a=exports.string=exports.utf8=s("utf-8");exports.ascii=s("ascii"),exports.hex=s("hex"),exports.base64=s("base64"),exports.ucs2=exports.utf16le=s("utf16le"),exports.array=function(e){return{preencode(r,t){for(const n of t)e.preencode(r,n)},encode(r,t){for(const n of t)e.encode(r,n)},decode(r){const t=[];for(;r.start<r.end;)t.push(e.decode(r));return t}}},exports.json={preencode(e,r){a.preencode(e,JSON.stringify(r))},encode(e,r){a.encode(e,JSON.stringify(r))},decode:e=>JSON.parse(a.decode(e))},exports.ndjson={preencode(e,r){a.preencode(e,JSON.stringify(r)+"\n")},encode(e,r){a.encode(e,JSON.stringify(r)+"\n")},decode:e=>JSON.parse(a.decode(e))};