@ipld/garbage 3.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +4 -0
- package/README.md +80 -0
- package/dist/index.min.js +4 -0
- package/dist/src/index.d.ts +7 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/interface.d.ts +16 -0
- package/dist/src/interface.d.ts.map +1 -0
- package/dist/src/to-string.d.ts +6 -0
- package/dist/src/to-string.d.ts.map +1 -0
- package/package.json +170 -0
- package/src/index.js +173 -0
- package/src/interface.ts +15 -0
- package/src/to-string.js +36 -0
package/LICENSE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# ipld-garbage <!-- omit in toc -->
|
|
2
|
+
|
|
3
|
+
[](https://codecov.io/gh/achingbrain/js-ipld-garbage)
|
|
4
|
+
[](https://github.com/achingbrain/js-ipld-garbage/actions/workflows/js-test-and-release.yml)
|
|
5
|
+
|
|
6
|
+
> Garbage data generator for the IPLD Data Model
|
|
7
|
+
|
|
8
|
+
## Table of contents <!-- omit in toc -->
|
|
9
|
+
|
|
10
|
+
- [Install](#install)
|
|
11
|
+
- [API](#api)
|
|
12
|
+
- [`options`](#options)
|
|
13
|
+
- [License](#license)
|
|
14
|
+
- [Contribute](#contribute)
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```console
|
|
19
|
+
$ npm i ipld-garbage
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Based on [substack's "garbage"](https://github.com/substack/node-garbage).
|
|
23
|
+
|
|
24
|
+
## API
|
|
25
|
+
|
|
26
|
+
`garbage(count = 200, options)`
|
|
27
|
+
|
|
28
|
+
Where `count` determines the approximate target number of bytes a garbage object should consume. And `options` allows for a `weight` object that allows you to provide a number for each object type to weight the random garbage generator. By default, all object types are weighted equally (with a value of `1`), providing a number (>= `0`), you can adjust the liklihood that particular types will appear relative to the weights of the other types. A weighting of `0` will turn off that type entirely.
|
|
29
|
+
|
|
30
|
+
### `options`
|
|
31
|
+
|
|
32
|
+
- `options.weights` an object with properties matching the IPLD data model types (see below) with numbers (>= `0`) that will weight randomness selection. Default: `{ list: 1, map: 1, string: 1, bytes: 1, boolean: 1, integer: 1, float: 1, null: 1, CID: 1 }`.
|
|
33
|
+
- `options.initialWeights` an object, similar to `options.weights`, that only applies to the initial object. Subsequent object creation will use `options.weights`. This allows for weighting of the container object to be more typical of IPLD data, which is typically some kind of map or list. Default `{ list: 10, map: 10, string: 1, bytes: 1, boolean: 1, integer: 1, float: 1, null: 1, CID: 1 }`.
|
|
34
|
+
|
|
35
|
+
Where you provide a custom `weights`, it will override `initialWeights`. e.g. `{ weights: { float: 0 } }` will result in no floats at all, even for the initial object.
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
import { garbage } from 'ipld-garbage'
|
|
39
|
+
|
|
40
|
+
console.log(garbage(100, { weights: { float: 0, object: 0 }}))
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Might yield:
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
{
|
|
47
|
+
'QbN/}`EO\tb6>\tI,`': 7827882605575541,
|
|
48
|
+
"~'wD!☺S}<Q|d1$☺": Uint8Array(12) [
|
|
49
|
+
116, 12, 191, 180, 214,
|
|
50
|
+
0, 88, 26, 116, 213,
|
|
51
|
+
88, 109
|
|
52
|
+
],
|
|
53
|
+
'q<': CID(baguqefrapdjrz7rknhnokqxo75ogs2hfpmdqiy7weez55ezaoyh63sd22n4q)
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
All IPLD Data Model types are within range for random creation, including top-level returns (a single call to `garbage()` might just return a `null`):
|
|
58
|
+
|
|
59
|
+
- null
|
|
60
|
+
- boolean
|
|
61
|
+
- integer
|
|
62
|
+
- float
|
|
63
|
+
- string
|
|
64
|
+
- bytes
|
|
65
|
+
- list
|
|
66
|
+
- map
|
|
67
|
+
- CID
|
|
68
|
+
|
|
69
|
+
Use `import { toString } from 'ipld-garbage/to-string'` to import a function that can turn an object returned by `garbage()` to a JavaScript string. This may be useful for generating a fixed set of test fixtures rather than relying on randomness during each run.
|
|
70
|
+
|
|
71
|
+
## License
|
|
72
|
+
|
|
73
|
+
Licensed under either of
|
|
74
|
+
|
|
75
|
+
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
|
|
76
|
+
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
|
|
77
|
+
|
|
78
|
+
## Contribute
|
|
79
|
+
|
|
80
|
+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.IpldGarbage = factory()}(typeof self !== 'undefined' ? self : this, function () {
|
|
2
|
+
"use strict";var IpldGarbage=(()=>{var k=Object.defineProperty;var fe=Object.getOwnPropertyDescriptor;var le=Object.getOwnPropertyNames;var pe=Object.prototype.hasOwnProperty;var ue=(r,e)=>{for(var t in e)k(r,t,{get:e[t],enumerable:!0})},we=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of le(e))!pe.call(r,o)&&o!==t&&k(r,o,{get:()=>e[o],enumerable:!(n=fe(e,o))||n.enumerable});return r};var be=r=>we(k({},"__esModule",{value:!0}),r);var He={};ue(He,{garbage:()=>Ke});var ge=K,X=128,ye=127,me=~ye,xe=Math.pow(2,31);function K(r,e,t){e=e||[],t=t||0;for(var n=t;r>=xe;)e[t++]=r&255|X,r/=128;for(;r&me;)e[t++]=r&255|X,r>>>=7;return e[t]=r|0,K.bytes=t-n+1,e}var ve=F,Ae=128,Q=127;function F(r,n){var t=0,n=n||0,o=0,s=n,i,a=r.length;do{if(s>=a)throw F.bytes=0,new RangeError("Could not decode varint");i=r[s++],t+=o<28?(i&Q)<<o:(i&Q)*Math.pow(2,o),o+=7}while(i>=Ae);return F.bytes=s-n,t}var Ee=Math.pow(2,7),Me=Math.pow(2,14),Se=Math.pow(2,21),ze=Math.pow(2,28),Ue=Math.pow(2,35),Ce=Math.pow(2,42),Oe=Math.pow(2,49),Ne=Math.pow(2,56),Te=Math.pow(2,63),Ie=function(r){return r<Ee?1:r<Me?2:r<Se?3:r<ze?4:r<Ue?5:r<Ce?6:r<Oe?7:r<Ne?8:r<Te?9:10},Le={encode:ge,decode:ve,encodingLength:Ie},Ve=Le,N=Ve;var T=(r,e=0)=>[N.decode(r,e),N.decode.bytes],z=(r,e,t=0)=>(N.encode(r,e,t),e),U=r=>N.encodingLength(r);var tt=new Uint8Array(0);var H=(r,e)=>{if(r===e)return!0;if(r.byteLength!==e.byteLength)return!1;for(let t=0;t<r.byteLength;t++)if(r[t]!==e[t])return!1;return!0},C=r=>{if(r instanceof Uint8Array&&r.constructor.name==="Uint8Array")return r;if(r instanceof ArrayBuffer)return new Uint8Array(r);if(ArrayBuffer.isView(r))return new Uint8Array(r.buffer,r.byteOffset,r.byteLength);throw new Error("Unknown type, must be binary type")};var D=(r,e)=>{let t=e.byteLength,n=U(r),o=n+U(t),s=new Uint8Array(o+t);return z(r,s,0),z(t,s,n),s.set(e,o),new O(r,t,e,s)},Y=r=>{let e=C(r),[t,n]=T(e),[o,s]=T(e.subarray(n)),i=e.subarray(n+s);if(i.byteLength!==o)throw new Error("Incorrect length");return new O(t,o,i,e)},_=(r,e)=>{if(r===e)return!0;{let t=e;return r.code===t.code&&r.size===t.size&&t.bytes instanceof Uint8Array&&H(r.bytes,t.bytes)}},O=class{constructor(e,t,n,o){this.code=e,this.size=t,this.digest=n,this.bytes=o}};function De(r,e){if(r.length>=255)throw new TypeError("Alphabet too long");for(var t=new Uint8Array(256),n=0;n<t.length;n++)t[n]=255;for(var o=0;o<r.length;o++){var s=r.charAt(o),i=s.charCodeAt(0);if(t[i]!==255)throw new TypeError(s+" is ambiguous");t[i]=o}var a=r.length,h=r.charAt(0),M=Math.log(a)/Math.log(256),l=Math.log(256)/Math.log(a);function S(c){if(c instanceof Uint8Array||(ArrayBuffer.isView(c)?c=new Uint8Array(c.buffer,c.byteOffset,c.byteLength):Array.isArray(c)&&(c=Uint8Array.from(c))),!(c instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(c.length===0)return"";for(var f=0,E=0,u=0,b=c.length;u!==b&&c[u]===0;)u++,f++;for(var g=(b-u)*l+1>>>0,p=new Uint8Array(g);u!==b;){for(var y=c[u],A=0,w=g-1;(y!==0||A<E)&&w!==-1;w--,A++)y+=256*p[w]>>>0,p[w]=y%a>>>0,y=y/a>>>0;if(y!==0)throw new Error("Non-zero carry");E=A,u++}for(var x=g-E;x!==g&&p[x]===0;)x++;for(var B=h.repeat(f);x<g;++x)B+=r.charAt(p[x]);return B}function V(c){if(typeof c!="string")throw new TypeError("Expected String");if(c.length===0)return new Uint8Array;var f=0;if(c[f]!==" "){for(var E=0,u=0;c[f]===h;)E++,f++;for(var b=(c.length-f)*M+1>>>0,g=new Uint8Array(b);c[f];){var p=t[c.charCodeAt(f)];if(p===255)return;for(var y=0,A=b-1;(p!==0||y<u)&&A!==-1;A--,y++)p+=a*g[A]>>>0,g[A]=p%256>>>0,p=p/256>>>0;if(p!==0)throw new Error("Non-zero carry");u=y,f++}if(c[f]!==" "){for(var w=b-u;w!==b&&g[w]===0;)w++;for(var x=new Uint8Array(E+(b-w)),B=E;w!==b;)x[B++]=g[w++];return x}}}function de(c){var f=V(c);if(f)return f;throw new Error(`Non-${e} character`)}return{encode:S,decodeUnsafe:V,decode:de}}var $e=De,ke=$e,ee=ke;var q=class{constructor(e,t,n){this.name=e,this.prefix=t,this.baseEncode=n}encode(e){if(e instanceof Uint8Array)return`${this.prefix}${this.baseEncode(e)}`;throw Error("Unknown type, must be binary type")}},R=class{constructor(e,t,n){if(this.name=e,this.prefix=t,t.codePointAt(0)===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=t.codePointAt(0),this.baseDecode=n}decode(e){if(typeof e=="string"){if(e.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(e)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(e.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(e){return te(this,e)}},j=class{constructor(e){this.decoders=e}or(e){return te(this,e)}decode(e){let t=e[0],n=this.decoders[t];if(n)return n.decode(e);throw RangeError(`Unable to decode multibase string ${JSON.stringify(e)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}},te=(r,e)=>new j({...r.decoders||{[r.prefix]:r},...e.decoders||{[e.prefix]:e}}),P=class{constructor(e,t,n,o){this.name=e,this.prefix=t,this.baseEncode=n,this.baseDecode=o,this.encoder=new q(e,t,n),this.decoder=new R(e,t,o)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}},re=({name:r,prefix:e,encode:t,decode:n})=>new P(r,e,t,n),W=({prefix:r,name:e,alphabet:t})=>{let{encode:n,decode:o}=ee(t,e);return re({prefix:r,name:e,encode:n,decode:s=>C(o(s))})},Fe=(r,e,t,n)=>{let o={};for(let l=0;l<e.length;++l)o[e[l]]=l;let s=r.length;for(;r[s-1]==="=";)--s;let i=new Uint8Array(s*t/8|0),a=0,h=0,M=0;for(let l=0;l<s;++l){let S=o[r[l]];if(S===void 0)throw new SyntaxError(`Non-${n} character`);h=h<<t|S,a+=t,a>=8&&(a-=8,i[M++]=255&h>>a)}if(a>=t||255&h<<8-a)throw new SyntaxError("Unexpected end of data");return i},qe=(r,e,t)=>{let n=e[e.length-1]==="=",o=(1<<t)-1,s="",i=0,a=0;for(let h=0;h<r.length;++h)for(a=a<<8|r[h],i+=8;i>t;)i-=t,s+=e[o&a>>i];if(i&&(s+=e[o&a<<t-i]),n)for(;s.length*t&7;)s+="=";return s},m=({name:r,prefix:e,bitsPerChar:t,alphabet:n})=>re({prefix:e,name:r,encode(o){return qe(o,n,t)},decode(o){return Fe(o,n,t,r)}});var v=W({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),ht=W({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var I=m({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),lt=m({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),pt=m({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),ut=m({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),wt=m({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),bt=m({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),gt=m({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),yt=m({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),mt=m({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var Re=(r,e)=>{let{bytes:t,version:n}=r;switch(n){case 0:return Pe(t,J(r),e||v.encoder);default:return We(t,J(r),e||I.encoder)}},ne=new WeakMap,J=r=>{let e=ne.get(r);if(e==null){let t=new Map;return ne.set(r,t),t}return e},d=class{constructor(e,t,n,o){this.code=t,this.version=e,this.multihash=n,this.bytes=o,this.asCID=this}get byteOffset(){return this.bytes.byteOffset}get byteLength(){return this.bytes.byteLength}toV0(){switch(this.version){case 0:return this;case 1:{let{code:e,multihash:t}=this;if(e!==L)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(t.code!==Je)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return d.createV0(t)}default:throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`)}}toV1(){switch(this.version){case 0:{let{code:e,digest:t}=this.multihash,n=D(e,t);return d.createV1(this.code,n)}case 1:return this;default:throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`)}}equals(e){return d.equals(this,e)}static equals(e,t){let n=t;return n&&e.code===n.code&&e.version===n.version&&_(e.multihash,n.multihash)}toString(e){return Re(this,e)}toJSON(){return{code:this.code,version:this.version,hash:this.multihash.bytes}}link(){return this}get[Symbol.toStringTag](){return"CID"}[Symbol.for("nodejs.util.inspect.custom")](){return`CID(${this.toString()})`}static asCID(e){let t=e;if(t instanceof d)return t;if(t!=null&&t.asCID===t){let{version:n,code:o,multihash:s,bytes:i}=t;return new d(n,o,s,i||oe(n,o,s.bytes))}else if(t!=null&&t[Ge]===!0){let{version:n,multihash:o,code:s}=t,i=Y(o);return d.create(n,s,i)}else return null}static create(e,t,n){if(typeof t!="number")throw new Error("String codecs are no longer supported");if(!(n.bytes instanceof Uint8Array))throw new Error("Invalid digest");switch(e){case 0:{if(t!==L)throw new Error(`Version 0 CID must use dag-pb (code: ${L}) block encoding`);return new d(e,t,n,n.bytes)}case 1:{let o=oe(e,t,n.bytes);return new d(e,t,n,o)}default:throw new Error("Invalid version")}}static createV0(e){return d.create(0,L,e)}static createV1(e,t){return d.create(1,e,t)}static decode(e){let[t,n]=d.decodeFirst(e);if(n.length)throw new Error("Incorrect length");return t}static decodeFirst(e){let t=d.inspectBytes(e),n=t.size-t.multihashSize,o=C(e.subarray(n,n+t.multihashSize));if(o.byteLength!==t.multihashSize)throw new Error("Incorrect length");let s=o.subarray(t.multihashSize-t.digestSize),i=new O(t.multihashCode,t.digestSize,s,o);return[t.version===0?d.createV0(i):d.createV1(t.codec,i),e.subarray(t.size)]}static inspectBytes(e){let t=0,n=()=>{let[S,V]=T(e.subarray(t));return t+=V,S},o=n(),s=L;if(o===18?(o=0,t=0):s=n(),o!==0&&o!==1)throw new RangeError(`Invalid CID version ${o}`);let i=t,a=n(),h=n(),M=t+h,l=M-i;return{version:o,codec:s,multihashCode:a,digestSize:h,multihashSize:l,size:M}}static parse(e,t){let[n,o]=je(e,t),s=d.decode(o);return J(s).set(n,e),s}},je=(r,e)=>{switch(r[0]){case"Q":{let t=e||v;return[v.prefix,t.decode(`${v.prefix}${r}`)]}case v.prefix:{let t=e||v;return[v.prefix,t.decode(r)]}case I.prefix:{let t=e||I;return[I.prefix,t.decode(r)]}default:{if(e==null)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[r[0],e.decode(r)]}}},Pe=(r,e,t)=>{let{prefix:n}=t;if(n!==v.prefix)throw Error(`Cannot string encode V0 in ${t.name} encoding`);let o=e.get(n);if(o==null){let s=t.encode(r).slice(1);return e.set(n,s),s}else return o},We=(r,e,t)=>{let{prefix:n}=t,o=e.get(n);if(o==null){let s=t.encode(r);return e.set(n,s),s}else return o},L=112,Je=18,oe=(r,e,t)=>{let n=U(r),o=n+U(e),s=new Uint8Array(o+t.byteLength);return z(r,s,0),z(e,s,n),s.set(t,o),s},Ge=Symbol.for("@ipld/js-cid/CID");var Xe=["list","map","string","bytes","boolean","integer","float","null","CID"],se=[85,112,113,297],ie=[[18,256],[22,256],[27,256],[45600,256],[19,512],[21,384],[20,512]],ae=`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\`~!@#$%^&*()-_=+[]{}|\\:;'",.<>?/
|
|
3
|
+
\u263A`,Qe={initialWeights:{map:10,list:10}};function Ke(r=200,e){return e=Object.assign({},Qe,e),G(r,e)[1]}function G(r,e){let t=0,n=Object.assign({},e.initialWeights,e.weights||{});e.initialWeights&&(e=Object.assign(e,{initialWeights:null}));let o=Xe.map(a=>{let h=n&&typeof n[a]=="number"?n[a]:1;if(h<0)throw new TypeError("Cannot have a negative weight");return t+=h,[a,h]});if(t===0)throw new Error("Cannot have a total weight of zero");let s=Math.random()*t,i=0;for(let[a,h]of o)if(i+=h,i>=s)return he[a](r,e);throw new Error("Internal error")}function $(r=5){return Math.abs(Math.floor(5/(1-Math.random()**2)-5+Math.random()*r))}function Ze(){return ae.charAt(Math.floor(Math.random()*ae.length))}function ce(){return Math.floor(Math.random()*256)}var he={list(r,e){let t=[],n=$(),o=0;for(let s=0;s<n&&o<r;s++){let i=G(r-o,e);t.push(i[1]),o+=i[0]}return[o,t]},map(r,e){let t={},n=$(),o=0;for(let s=0;s<n&&o<r;s++){let i=he.string(5)[1],a=G(r-o,e);t[i]=a[1],o+=a[0]+i.length}return[o,t]},string(r=50){let e=$(r),t=[];for(let n=0;n<e;n++)t.push(Ze());return[e,t.join("")]},bytes(r=50){let e=$(r),t=new Uint8Array(e);for(let n=0;n<e;n++)t[n]=ce();return[e,t]},boolean(){return[1,Math.random()>.5]},integer(){return[1,Math.floor(Number.MAX_SAFE_INTEGER*Math.random())*(Math.random()<.5?-1:1)]},float(){return[1,Math.tan((Math.random()-.5)*Math.PI)]},null(){return[1,null]},CID(){let r=ie[Math.floor(Math.random()*ie.length)],e=se[Math.floor(Math.random()*se.length)],t=new Uint8Array(r[1]/8);for(let n=0;n<t.length;n++)t[n]=ce();return[t.length,d.create(1,e,D(r[0],t))]}};return be(He);})();
|
|
4
|
+
return IpldGarbage}));
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {number} [count=200]
|
|
3
|
+
* @param {import('./interface').GarbageOptions} [options]
|
|
4
|
+
* @returns {any}
|
|
5
|
+
*/
|
|
6
|
+
export function garbage(count?: number | undefined, options?: import("./interface").GarbageOptions | undefined): any;
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.js"],"names":[],"mappings":"AASA;;;;GAIG;AACH,iHAFa,GAAG,CAKf"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface GarbageWeights {
|
|
2
|
+
list?: number;
|
|
3
|
+
map?: number;
|
|
4
|
+
string?: number;
|
|
5
|
+
bytes?: number;
|
|
6
|
+
boolean?: number;
|
|
7
|
+
integer?: number;
|
|
8
|
+
float?: number;
|
|
9
|
+
null?: number;
|
|
10
|
+
CID?: number;
|
|
11
|
+
}
|
|
12
|
+
export interface GarbageOptions {
|
|
13
|
+
initialWeights?: GarbageWeights;
|
|
14
|
+
weights?: GarbageWeights;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../../src/interface.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,GAAG,CAAC,EAAE,MAAM,CAAA;CACb;AACD,MAAM,WAAW,cAAc;IAC7B,cAAc,CAAC,EAAE,cAAc,CAAA;IAC/B,OAAO,CAAC,EAAE,cAAc,CAAA;CACzB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"to-string.d.ts","sourceRoot":"","sources":["../../src/to-string.js"],"names":[],"mappings":"AAEA;;;GAGG;AACH,8BAHW,GAAG,GACD,MAAM,CA6BlB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ipld/garbage",
|
|
3
|
+
"version": "3.0.5",
|
|
4
|
+
"description": "Garbage data generator for the IPLD Data Model",
|
|
5
|
+
"author": "Rod <rod@vagg.org> (http://r.va.gg/)",
|
|
6
|
+
"license": "Apache-2.0 OR MIT",
|
|
7
|
+
"homepage": "https://github.com/achingbrain/js-ipld-garbage#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/achingbrain/js-ipld-garbage.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/achingbrain/js-ipld-garbage/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"IPLD",
|
|
17
|
+
"garbage"
|
|
18
|
+
],
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=16.0.0",
|
|
21
|
+
"npm": ">=7.0.0"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"types": "./dist/src/index.d.ts",
|
|
25
|
+
"typesVersions": {
|
|
26
|
+
"*": {
|
|
27
|
+
"*": [
|
|
28
|
+
"*",
|
|
29
|
+
"dist/*",
|
|
30
|
+
"dist/src/*",
|
|
31
|
+
"dist/src/*/index"
|
|
32
|
+
],
|
|
33
|
+
"src/*": [
|
|
34
|
+
"*",
|
|
35
|
+
"dist/*",
|
|
36
|
+
"dist/src/*",
|
|
37
|
+
"dist/src/*/index"
|
|
38
|
+
]
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"src",
|
|
43
|
+
"dist",
|
|
44
|
+
"!dist/test",
|
|
45
|
+
"!**/*.tsbuildinfo"
|
|
46
|
+
],
|
|
47
|
+
"exports": {
|
|
48
|
+
".": {
|
|
49
|
+
"types": "./dist/src/index.d.ts",
|
|
50
|
+
"import": "./src/index.js"
|
|
51
|
+
},
|
|
52
|
+
"./to-string": {
|
|
53
|
+
"types": "./src/to-string.d.ts",
|
|
54
|
+
"import": "./src/to-string.js"
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"eslintConfig": {
|
|
58
|
+
"extends": "ipfs",
|
|
59
|
+
"parserOptions": {
|
|
60
|
+
"sourceType": "module"
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"release": {
|
|
64
|
+
"branches": [
|
|
65
|
+
"master"
|
|
66
|
+
],
|
|
67
|
+
"plugins": [
|
|
68
|
+
[
|
|
69
|
+
"@semantic-release/commit-analyzer",
|
|
70
|
+
{
|
|
71
|
+
"preset": "conventionalcommits",
|
|
72
|
+
"releaseRules": [
|
|
73
|
+
{
|
|
74
|
+
"breaking": true,
|
|
75
|
+
"release": "major"
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
"revert": true,
|
|
79
|
+
"release": "patch"
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
"type": "feat",
|
|
83
|
+
"release": "minor"
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
"type": "fix",
|
|
87
|
+
"release": "patch"
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
"type": "docs",
|
|
91
|
+
"release": "patch"
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
"type": "test",
|
|
95
|
+
"release": "patch"
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
"type": "deps",
|
|
99
|
+
"release": "patch"
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
"scope": "no-release",
|
|
103
|
+
"release": false
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
}
|
|
107
|
+
],
|
|
108
|
+
[
|
|
109
|
+
"@semantic-release/release-notes-generator",
|
|
110
|
+
{
|
|
111
|
+
"preset": "conventionalcommits",
|
|
112
|
+
"presetConfig": {
|
|
113
|
+
"types": [
|
|
114
|
+
{
|
|
115
|
+
"type": "feat",
|
|
116
|
+
"section": "Features"
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
"type": "fix",
|
|
120
|
+
"section": "Bug Fixes"
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
"type": "chore",
|
|
124
|
+
"section": "Trivial Changes"
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
"type": "docs",
|
|
128
|
+
"section": "Documentation"
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
"type": "deps",
|
|
132
|
+
"section": "Dependencies"
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
"type": "test",
|
|
136
|
+
"section": "Tests"
|
|
137
|
+
}
|
|
138
|
+
]
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
],
|
|
142
|
+
"@semantic-release/changelog",
|
|
143
|
+
"@semantic-release/npm",
|
|
144
|
+
"@semantic-release/github",
|
|
145
|
+
"@semantic-release/git"
|
|
146
|
+
]
|
|
147
|
+
},
|
|
148
|
+
"scripts": {
|
|
149
|
+
"clean": "aegir clean",
|
|
150
|
+
"lint": "aegir lint",
|
|
151
|
+
"build": "aegir build",
|
|
152
|
+
"release": "aegir release",
|
|
153
|
+
"test": "npm run lint && aegir test",
|
|
154
|
+
"test:ts": "npm run test --prefix test/ts-use",
|
|
155
|
+
"test:node": "aegir test -t node --cov",
|
|
156
|
+
"test:chrome": "aegir test -t browser --cov",
|
|
157
|
+
"test:chrome-webworker": "aegir test -t webworker",
|
|
158
|
+
"test:firefox": "aegir test -t browser -- --browser firefox",
|
|
159
|
+
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
|
|
160
|
+
"test:electron-main": "aegir test -t electron-main",
|
|
161
|
+
"dep-check": "aegir dep-check"
|
|
162
|
+
},
|
|
163
|
+
"dependencies": {
|
|
164
|
+
"multiformats": "^10.0.1"
|
|
165
|
+
},
|
|
166
|
+
"devDependencies": {
|
|
167
|
+
"@sindresorhus/is": "^5.3.0",
|
|
168
|
+
"aegir": "^37.5.6"
|
|
169
|
+
}
|
|
170
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// based on https://github.com/substack/node-garbage/blob/master/index.js
|
|
2
|
+
import { CID } from 'multiformats/cid'
|
|
3
|
+
import { create as createDigest } from 'multiformats/hashes/digest'
|
|
4
|
+
|
|
5
|
+
const kinds = ['list', 'map', 'string', 'bytes', 'boolean', 'integer', 'float', 'null', 'CID']
|
|
6
|
+
const codecs = [0x55, 0x70, 0x71, 0x0129]
|
|
7
|
+
const hashes = [[0x12, 256], [0x16, 256], [0x1b, 256], [0xb220, 256], [0x13, 512], [0x15, 384], [0x14, 512]]
|
|
8
|
+
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`~!@#$%^&*()-_=+[]{}|\\:;\'",.<>?/ \t\n\u263a'
|
|
9
|
+
const baseOptions = { initialWeights: { map: 10, list: 10 } }
|
|
10
|
+
/**
|
|
11
|
+
* @param {number} [count=200]
|
|
12
|
+
* @param {import('./interface').GarbageOptions} [options]
|
|
13
|
+
* @returns {any}
|
|
14
|
+
*/
|
|
15
|
+
export function garbage (count = 200, options) {
|
|
16
|
+
options = Object.assign({}, baseOptions, options)
|
|
17
|
+
return generate(count, options)[1]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {number} count
|
|
22
|
+
* @param {import('./interface').GarbageOptions} options
|
|
23
|
+
* @returns {any}
|
|
24
|
+
*/
|
|
25
|
+
function generate (count, options) {
|
|
26
|
+
let totWeight = 0
|
|
27
|
+
const weights = Object.assign({}, options.initialWeights, options.weights || {})
|
|
28
|
+
if (options.initialWeights) {
|
|
29
|
+
options = Object.assign(options, { initialWeights: null })
|
|
30
|
+
}
|
|
31
|
+
const types = kinds.map((t) => {
|
|
32
|
+
/* c8 ignore next 5 */
|
|
33
|
+
// @ts-ignore not sure why I can't index with [t] from a fixed list of properties
|
|
34
|
+
const weight = weights && typeof weights[t] === 'number' ? weights[t] : 1
|
|
35
|
+
if (weight < 0) {
|
|
36
|
+
throw new TypeError('Cannot have a negative weight')
|
|
37
|
+
}
|
|
38
|
+
totWeight += weight
|
|
39
|
+
return [t, weight]
|
|
40
|
+
})
|
|
41
|
+
/* c8 ignore next 3 */
|
|
42
|
+
if (totWeight === 0) {
|
|
43
|
+
throw new Error('Cannot have a total weight of zero')
|
|
44
|
+
}
|
|
45
|
+
const rnd = Math.random() * totWeight
|
|
46
|
+
let wacc = 0
|
|
47
|
+
for (const [type, weight] of types) {
|
|
48
|
+
wacc += weight
|
|
49
|
+
if (wacc >= rnd) {
|
|
50
|
+
// @ts-ignore not sure why I can't index with [t] from a fixed list of properties
|
|
51
|
+
return generators[type](count, options)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/* c8 ignore next 1 */
|
|
55
|
+
throw new Error('Internal error')
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function rndSize (bias = 5) {
|
|
59
|
+
return Math.abs(Math.floor(5 / (1 - (Math.random() ** 2)) - 5 + Math.random() * bias))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function rndChar () {
|
|
63
|
+
return charset.charAt(Math.floor(Math.random() * charset.length))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function rndByte () {
|
|
67
|
+
return Math.floor(Math.random() * 256)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const generators = {
|
|
71
|
+
/**
|
|
72
|
+
* @param {number} count
|
|
73
|
+
* @param {import('./interface').GarbageOptions} options
|
|
74
|
+
* @returns {[number, Array<any>]}
|
|
75
|
+
*/
|
|
76
|
+
list (count, options) {
|
|
77
|
+
const res = []
|
|
78
|
+
const len = rndSize()
|
|
79
|
+
let size = 0
|
|
80
|
+
for (let i = 0; i < len && size < count; i++) {
|
|
81
|
+
const x = generate(count - size, options)
|
|
82
|
+
res.push(x[1])
|
|
83
|
+
size += x[0]
|
|
84
|
+
}
|
|
85
|
+
return [size, res]
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @param {number} count
|
|
90
|
+
* @param {import('./interface').GarbageOptions} options
|
|
91
|
+
* @returns {[number, {[key: string]: any}]}
|
|
92
|
+
*/
|
|
93
|
+
map (count, options) {
|
|
94
|
+
/** @type {{[key: string]: any}} */
|
|
95
|
+
const res = {}
|
|
96
|
+
const len = rndSize()
|
|
97
|
+
let size = 0
|
|
98
|
+
for (let i = 0; i < len && size < count; i++) {
|
|
99
|
+
const key = /** @type {keyof typeof res} */ generators.string(5)[1]
|
|
100
|
+
const x = generate(count - size, options)
|
|
101
|
+
res[key] = x[1]
|
|
102
|
+
size += x[0] + key.length
|
|
103
|
+
}
|
|
104
|
+
return [size, res]
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @param {number} [bias=50]
|
|
109
|
+
* @returns {[number, string]}
|
|
110
|
+
*/
|
|
111
|
+
string (bias = 50) {
|
|
112
|
+
const len = rndSize(bias)
|
|
113
|
+
const res = []
|
|
114
|
+
for (let i = 0; i < len; i++) {
|
|
115
|
+
res.push(rndChar())
|
|
116
|
+
}
|
|
117
|
+
return [len, res.join('')]
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @param {number} [bias=50]
|
|
122
|
+
* @returns {[number, Uint8Array]}
|
|
123
|
+
*/
|
|
124
|
+
bytes (bias = 50) {
|
|
125
|
+
const len = rndSize(bias)
|
|
126
|
+
const res = new Uint8Array(len)
|
|
127
|
+
for (let i = 0; i < len; i++) {
|
|
128
|
+
res[i] = rndByte()
|
|
129
|
+
}
|
|
130
|
+
return [len, res]
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @returns {[1, boolean]}
|
|
135
|
+
*/
|
|
136
|
+
boolean () {
|
|
137
|
+
return [1, Math.random() > 0.5]
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @returns {[1, number]}
|
|
142
|
+
*/
|
|
143
|
+
integer () {
|
|
144
|
+
return [1, Math.floor(Number.MAX_SAFE_INTEGER * Math.random()) * (Math.random() < 0.5 ? -1 : 1)]
|
|
145
|
+
},
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @returns {[1, number]}
|
|
149
|
+
*/
|
|
150
|
+
float () {
|
|
151
|
+
return [1, Math.tan((Math.random() - 0.5) * Math.PI)]
|
|
152
|
+
},
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* @returns {[1, null]}
|
|
156
|
+
*/
|
|
157
|
+
null () {
|
|
158
|
+
return [1, null]
|
|
159
|
+
},
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* @returns {[number, CID]}
|
|
163
|
+
*/
|
|
164
|
+
CID () {
|
|
165
|
+
const hasher = hashes[Math.floor(Math.random() * hashes.length)]
|
|
166
|
+
const codec = codecs[Math.floor(Math.random() * codecs.length)]
|
|
167
|
+
const bytes = new Uint8Array(hasher[1] / 8)
|
|
168
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
169
|
+
bytes[i] = rndByte()
|
|
170
|
+
}
|
|
171
|
+
return [bytes.length, CID.create(1, codec, createDigest(hasher[0], bytes))]
|
|
172
|
+
}
|
|
173
|
+
}
|
package/src/interface.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface GarbageWeights {
|
|
2
|
+
list?: number
|
|
3
|
+
map?: number
|
|
4
|
+
string?: number
|
|
5
|
+
bytes?: number
|
|
6
|
+
boolean?: number
|
|
7
|
+
integer?: number
|
|
8
|
+
float?: number
|
|
9
|
+
null?: number
|
|
10
|
+
CID?: number
|
|
11
|
+
}
|
|
12
|
+
export interface GarbageOptions {
|
|
13
|
+
initialWeights?: GarbageWeights
|
|
14
|
+
weights?: GarbageWeights
|
|
15
|
+
}
|
package/src/to-string.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { CID } from 'multiformats/cid'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {any} obj
|
|
5
|
+
* @returns {string}
|
|
6
|
+
*/
|
|
7
|
+
export function toString (obj) {
|
|
8
|
+
if (CID.asCID(obj)) {
|
|
9
|
+
return `CID.parse('${obj.toString()}')`
|
|
10
|
+
}
|
|
11
|
+
if (obj === null) {
|
|
12
|
+
return 'null'
|
|
13
|
+
}
|
|
14
|
+
const typ = typeof obj
|
|
15
|
+
if (typ === 'string') {
|
|
16
|
+
return `'${JSON.stringify(obj).replace(/^"|"$/g, '').replace(/'/g, '\\\'')}'`
|
|
17
|
+
}
|
|
18
|
+
if (typ === 'number' || typ === 'boolean') {
|
|
19
|
+
return String(obj)
|
|
20
|
+
}
|
|
21
|
+
if (Array.isArray(obj)) {
|
|
22
|
+
return `[${obj.map(toString).join(',')}]`
|
|
23
|
+
}
|
|
24
|
+
if (obj instanceof Uint8Array) {
|
|
25
|
+
return `Uint8Array.from([${obj.join(',')}])`
|
|
26
|
+
}
|
|
27
|
+
if (typ === 'object') {
|
|
28
|
+
const props = Object.entries(obj).map(([key, value]) => {
|
|
29
|
+
return `[${toString(key)}]: ${toString(value)}`
|
|
30
|
+
})
|
|
31
|
+
return `{${props.join(',')}}`
|
|
32
|
+
}
|
|
33
|
+
throw new Error(`Invalid IPLD kind: ${Object.prototype.toString.call(obj)}`)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// console.log(toString(garbage(100, { weights: { list: 0 } })))
|