Beta 7 with some repo names changed.
This commit is contained in:
1 parent
0f7b40cca7
commit
963c19493a
3408 files changed
+462217
-15516
No files matched your search
+2892
File diff suppressed because it is too large.
Load diff
+3
@@ -0,0 +1,3 @@
|
||||
/*! For license information please see index.min.js.LICENSE.txt */
|
||||
!function(t,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports.Multibase=r():t.Multibase=r()}(window,(function(){return function(t){var r={};function e(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,e),o.l=!0,o.exports}return e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{enumerable:!0,get:n})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,r){if(1&r&&(t=e(t)),8&r)return t;if(4&r&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(e.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&r&&"string"!=typeof t)for(var o in t)e.d(n,o,function(r){return t[r]}.bind(null,o));return n},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},e.p="",e(e.s=1)}([function(t,r,e){"use strict";(function(t){var n=e(4),o=e(5),i=e(6);function f(){return s.TYPED_ARRAY_SUPPORT?2147483647:1073741823}function u(t,r){if(f()<r)throw new RangeError("Invalid typed array length");return s.TYPED_ARRAY_SUPPORT?(t=new Uint8Array(r)).__proto__=s.prototype:(null===t&&(t=new s(r)),t.length=r),t}function s(t,r,e){if(!(s.TYPED_ARRAY_SUPPORT||this instanceof s))return new s(t,r,e);if("number"==typeof t){if("string"==typeof r)throw new Error("If encoding is specified then the first argument must be a string");return c(this,t)}return a(this,t,r,e)}function a(t,r,e,n){if("number"==typeof r)throw new TypeError('"value" argument must not be a number');return"undefined"!=typeof ArrayBuffer&&r instanceof ArrayBuffer?function(t,r,e,n){if(r.byteLength,e<0||r.byteLength<e)throw new RangeError("'offset' is out of bounds");if(r.byteLength<e+(n||0))throw new RangeError("'length' is out of bounds");r=void 0===e&&void 0===n?new Uint8Array(r):void 0===n?new Uint8Array(r,e):new Uint8Array(r,e,n);s.TYPED_ARRAY_SUPPORT?(t=r).__proto__=s.prototype:t=l(t,r);return t}(t,r,e,n):"string"==typeof r?function(t,r,e){"string"==typeof e&&""!==e||(e="utf8");if(!s.isEncoding(e))throw new TypeError('"encoding" must be a valid string encoding');var n=0|g(r,e),o=(t=u(t,n)).write(r,e);o!==n&&(t=t.slice(0,o));return t}(t,r,e):function(t,r){if(s.isBuffer(r)){var e=0|p(r.length);return 0===(t=u(t,e)).length||r.copy(t,0,0,e),t}if(r){if("undefined"!=typeof ArrayBuffer&&r.buffer instanceof ArrayBuffer||"length"in r)return"number"!=typeof r.length||(n=r.length)!=n?u(t,0):l(t,r);if("Buffer"===r.type&&i(r.data))return l(t,r.data)}var n;throw new TypeError("First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.")}(t,r)}function h(t){if("number"!=typeof t)throw new TypeError('"size" argument must be a number');if(t<0)throw new RangeError('"size" argument must not be negative')}function c(t,r){if(h(r),t=u(t,r<0?0:0|p(r)),!s.TYPED_ARRAY_SUPPORT)for(var e=0;e<r;++e)t[e]=0;return t}function l(t,r){var e=r.length<0?0:0|p(r.length);t=u(t,e);for(var n=0;n<e;n+=1)t[n]=255&r[n];return t}function p(t){if(t>=f())throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x"+f().toString(16)+" bytes");return 0|t}function g(t,r){if(s.isBuffer(t))return t.length;if("undefined"!=typeof ArrayBuffer&&"function"==typeof ArrayBuffer.isView&&(ArrayBuffer.isView(t)||t instanceof ArrayBuffer))return t.byteLength;"string"!=typeof t&&(t=""+t);var e=t.length;if(0===e)return 0;for(var n=!1;;)switch(r){case"ascii":case"latin1":case"binary":return e;case"utf8":case"utf-8":case void 0:return N(t).length;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return 2*e;case"hex":return e>>>1;case"base64":return z(t).length;default:if(n)return N(t).length;r=(""+r).toLowerCase(),n=!0}}function y(t,r,e){var n=!1;if((void 0===r||r<0)&&(r=0),r>this.length)return"";if((void 0===e||e>this.length)&&(e=this.length),e<=0)return"";if((e>>>=0)<=(r>>>=0))return"";for(t||(t="utf8");;)switch(t){case"hex":return S(this,r,e);case"utf8":case"utf-8":return T(this,r,e);case"ascii":return P(this,r,e);case"latin1":case"binary":return U(this,r,e);case"base64":return B(this,r,e);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return x(this,r,e);default:if(n)throw new TypeError("Unknown encoding: "+t);t=(t+"").toLowerCase(),n=!0}}function d(t,r,e){var n=t[r];t[r]=t[e],t[e]=n}function w(t,r,e,n,o){if(0===t.length)return-1;if("string"==typeof e?(n=e,e=0):e>2147483647?e=2147483647:e<-2147483648&&(e=-2147483648),e=+e,isNaN(e)&&(e=o?0:t.length-1),e<0&&(e=t.length+e),e>=t.length){if(o)return-1;e=t.length-1}else if(e<0){if(!o)return-1;e=0}if("string"==typeof r&&(r=s.from(r,n)),s.isBuffer(r))return 0===r.length?-1:b(t,r,e,n,o);if("number"==typeof r)return r&=255,s.TYPED_ARRAY_SUPPORT&&"function"==typeof Uint8Array.prototype.indexLine truncated
|
||||
//# sourceMappingURL=index.min.js.map
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
{
|
||||
"name": "multibase",
|
||||
"version": "0.7.0",
|
||||
"description": "JavaScript implementation of the multibase specification",
|
||||
"leadMaintainer": "Oli Evans <oli@tableflip.io>",
|
||||
"main": "src/index.js",
|
||||
"scripts": {
|
||||
"lint": "aegir lint",
|
||||
"test": "aegir test",
|
||||
"test:node": "aegir test -t node",
|
||||
"test:browser": "aegir test -t browser",
|
||||
"build": "aegir build",
|
||||
"docs": "aegir docs",
|
||||
"release": "aegir release --docs",
|
||||
"release-minor": "aegir release --type minor --docs",
|
||||
"release-major": "aegir release --type major --docs",
|
||||
"coverage": "aegir coverage",
|
||||
"coverage-publish": "aegir coverage publish"
|
||||
},
|
||||
"files": [
|
||||
"src",
|
||||
"dist"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/multiformats/js-multibase.git"
|
||||
},
|
||||
"keywords": [
|
||||
"IPFS",
|
||||
"multiformats",
|
||||
"multibase",
|
||||
"encode",
|
||||
"decode",
|
||||
"formats"
|
||||
],
|
||||
"devDependencies": {
|
||||
"aegir": "^21.3.0",
|
||||
"chai": "^4.1.2",
|
||||
"dirty-chai": "^2.0.1",
|
||||
"pre-commit": "^1.2.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"base-x": "^3.0.8",
|
||||
"buffer": "^5.5.0"
|
||||
},
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/multiformats/js-multibase/issues"
|
||||
},
|
||||
"homepage": "https://github.com/multiformats/js-multibase#readme",
|
||||
"contributors": [
|
||||
"David Dias <daviddias.p@gmail.com>",
|
||||
"Oli Evans <oli@tableflip.io>",
|
||||
"Hugo Dias <hugomrdias@gmail.com>",
|
||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||
"Alan Shaw <alan.shaw@protocol.ai>",
|
||||
"victorbjelkholm <victorbjelkholm@gmail.com>",
|
||||
"achingbrain <alex@achingbrain.net>",
|
||||
"theobat <theophile.batoz@gmail.com>",
|
||||
"Bora M. Alper <boramalper@gmail.com>",
|
||||
"Henrique Dias <hacdias@gmail.com>",
|
||||
"Richard Littauer <richard.littauer@gmail.com>"
|
||||
]
|
||||
|
||||
,"_resolved": "https://registry.npmjs.org/multibase/-/multibase-0.7.0.tgz"
|
||||
,"_integrity": "sha512-TW8q03O0f6PNFTQDvh3xxH03c8CjGaaYrjkl9UQPG6rz53TQzzxJVCIWVjzcbN/Q5Y53Zd0IBQBMVktVgNx4Fg=="
|
||||
,"_from": "multibase@^0.7.0"
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
'use strict'
|
||||
|
||||
class Base {
|
||||
constructor (name, code, implementation, alphabet) {
|
||||
this.name = name
|
||||
this.code = code
|
||||
this.alphabet = alphabet
|
||||
if (implementation && alphabet) {
|
||||
this.engine = implementation(alphabet)
|
||||
}
|
||||
}
|
||||
|
||||
encode (stringOrBuffer) {
|
||||
return this.engine.encode(stringOrBuffer)
|
||||
}
|
||||
|
||||
decode (stringOrBuffer) {
|
||||
return this.engine.decode(stringOrBuffer)
|
||||
}
|
||||
|
||||
isImplemented () {
|
||||
return this.engine
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Base
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
'use strict'
|
||||
const { Buffer } = require('buffer')
|
||||
|
||||
module.exports = function base16 (alphabet) {
|
||||
return {
|
||||
encode (input) {
|
||||
if (typeof input === 'string') {
|
||||
return Buffer.from(input).toString('hex')
|
||||
}
|
||||
return input.toString('hex')
|
||||
},
|
||||
decode (input) {
|
||||
for (const char of input) {
|
||||
if (alphabet.indexOf(char) < 0) {
|
||||
throw new Error('invalid base16 character')
|
||||
}
|
||||
}
|
||||
return Buffer.from(input, 'hex')
|
||||
}
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
'use strict'
|
||||
|
||||
function decode (input, alphabet) {
|
||||
input = input.replace(new RegExp('=', 'g'), '')
|
||||
const length = input.length
|
||||
|
||||
let bits = 0
|
||||
let value = 0
|
||||
|
||||
let index = 0
|
||||
const output = new Uint8Array((length * 5 / 8) | 0)
|
||||
|
||||
for (let i = 0; i < length; i++) {
|
||||
value = (value << 5) | alphabet.indexOf(input[i])
|
||||
bits += 5
|
||||
|
||||
if (bits >= 8) {
|
||||
output[index++] = (value >>> (bits - 8)) & 255
|
||||
bits -= 8
|
||||
}
|
||||
}
|
||||
|
||||
return output.buffer
|
||||
}
|
||||
|
||||
function encode (buffer, alphabet) {
|
||||
const length = buffer.byteLength
|
||||
const view = new Uint8Array(buffer)
|
||||
const padding = alphabet.indexOf('=') === alphabet.length - 1
|
||||
|
||||
if (padding) {
|
||||
alphabet = alphabet.substring(0, alphabet.length - 1)
|
||||
}
|
||||
|
||||
let bits = 0
|
||||
let value = 0
|
||||
let output = ''
|
||||
|
||||
for (let i = 0; i < length; i++) {
|
||||
value = (value << 8) | view[i]
|
||||
bits += 8
|
||||
|
||||
while (bits >= 5) {
|
||||
output += alphabet[(value >>> (bits - 5)) & 31]
|
||||
bits -= 5
|
||||
}
|
||||
}
|
||||
|
||||
if (bits > 0) {
|
||||
output += alphabet[(value << (5 - bits)) & 31]
|
||||
}
|
||||
|
||||
if (padding) {
|
||||
while ((output.length % 8) !== 0) {
|
||||
output += '='
|
||||
}
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
module.exports = function base32 (alphabet) {
|
||||
return {
|
||||
encode (input) {
|
||||
if (typeof input === 'string') {
|
||||
return encode(Uint8Array.from(input), alphabet)
|
||||
}
|
||||
|
||||
return encode(input, alphabet)
|
||||
},
|
||||
decode (input) {
|
||||
for (const char of input) {
|
||||
if (alphabet.indexOf(char) < 0) {
|
||||
throw new Error('invalid base32 character')
|
||||
}
|
||||
}
|
||||
|
||||
return decode(input, alphabet)
|
||||
}
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
'use strict'
|
||||
const { Buffer } = require('buffer')
|
||||
|
||||
module.exports = function base64 (alphabet) {
|
||||
// The alphabet is only used to know:
|
||||
// 1. If padding is enabled (must contain '=')
|
||||
// 2. If the output must be url-safe (must contain '-' and '_')
|
||||
// 3. If the input of the output function is valid
|
||||
// The alphabets from RFC 4648 are always used.
|
||||
const padding = alphabet.indexOf('=') > -1
|
||||
const url = alphabet.indexOf('-') > -1 && alphabet.indexOf('_') > -1
|
||||
|
||||
return {
|
||||
encode (input) {
|
||||
let output = ''
|
||||
|
||||
if (typeof input === 'string') {
|
||||
output = Buffer.from(input).toString('base64')
|
||||
} else {
|
||||
output = input.toString('base64')
|
||||
}
|
||||
|
||||
if (url) {
|
||||
output = output.replace(/\+/g, '-').replace(/\//g, '_')
|
||||
}
|
||||
|
||||
const pad = output.indexOf('=')
|
||||
if (pad > 0 && !padding) {
|
||||
output = output.substring(0, pad)
|
||||
}
|
||||
|
||||
return output
|
||||
},
|
||||
decode (input) {
|
||||
for (const char of input) {
|
||||
if (alphabet.indexOf(char) < 0) {
|
||||
throw new Error('invalid base64 character')
|
||||
}
|
||||
}
|
||||
|
||||
return Buffer.from(input, 'base64')
|
||||
}
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
'use strict'
|
||||
|
||||
const Base = require('./base.js')
|
||||
const baseX = require('base-x')
|
||||
const base16 = require('./base16')
|
||||
const base32 = require('./base32')
|
||||
const base64 = require('./base64')
|
||||
|
||||
// name, code, implementation, alphabet
|
||||
const constants = [
|
||||
['base1', '1', '', '1'],
|
||||
['base2', '0', baseX, '01'],
|
||||
['base8', '7', baseX, '01234567'],
|
||||
['base10', '9', baseX, '0123456789'],
|
||||
['base16', 'f', base16, '0123456789abcdef'],
|
||||
['base32', 'b', base32, 'abcdefghijklmnopqrstuvwxyz234567'],
|
||||
['base32pad', 'c', base32, 'abcdefghijklmnopqrstuvwxyz234567='],
|
||||
['base32hex', 'v', base32, '0123456789abcdefghijklmnopqrstuv'],
|
||||
['base32hexpad', 't', base32, '0123456789abcdefghijklmnopqrstuv='],
|
||||
['base32z', 'h', base32, 'ybndrfg8ejkmcpqxot1uwisza345h769'],
|
||||
['base58flickr', 'Z', baseX, '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'],
|
||||
['base58btc', 'z', baseX, '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'],
|
||||
['base64', 'm', base64, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'],
|
||||
['base64pad', 'M', base64, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='],
|
||||
['base64url', 'u', base64, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'],
|
||||
['base64urlpad', 'U', base64, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=']
|
||||
]
|
||||
|
||||
const names = constants.reduce((prev, tupple) => {
|
||||
prev[tupple[0]] = new Base(tupple[0], tupple[1], tupple[2], tupple[3])
|
||||
return prev
|
||||
}, {})
|
||||
|
||||
const codes = constants.reduce((prev, tupple) => {
|
||||
prev[tupple[1]] = names[tupple[0]]
|
||||
return prev
|
||||
}, {})
|
||||
|
||||
module.exports = {
|
||||
names: names,
|
||||
codes: codes
|
||||
}
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
/**
|
||||
* Implementation of the [multibase](https://github.com/multiformats/multibase) specification.
|
||||
* @module Multibase
|
||||
*/
|
||||
'use strict'
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const constants = require('./constants')
|
||||
|
||||
exports = module.exports = multibase
|
||||
exports.encode = encode
|
||||
exports.decode = decode
|
||||
exports.isEncoded = isEncoded
|
||||
exports.names = Object.freeze(Object.keys(constants.names))
|
||||
exports.codes = Object.freeze(Object.keys(constants.codes))
|
||||
|
||||
/**
|
||||
* Create a new buffer with the multibase varint+code.
|
||||
*
|
||||
* @param {string|number} nameOrCode - The multibase name or code number.
|
||||
* @param {Buffer} buf - The data to be prefixed with multibase.
|
||||
* @memberof Multibase
|
||||
* @returns {Buffer}
|
||||
*/
|
||||
function multibase (nameOrCode, buf) {
|
||||
if (!buf) {
|
||||
throw new Error('requires an encoded buffer')
|
||||
}
|
||||
const base = getBase(nameOrCode)
|
||||
const codeBuf = Buffer.from(base.code)
|
||||
|
||||
const name = base.name
|
||||
validEncode(name, buf)
|
||||
return Buffer.concat([codeBuf, buf])
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode data with the specified base and add the multibase prefix.
|
||||
*
|
||||
* @param {string|number} nameOrCode - The multibase name or code number.
|
||||
* @param {Buffer} buf - The data to be encoded.
|
||||
* @returns {Buffer}
|
||||
* @memberof Multibase
|
||||
*/
|
||||
function encode (nameOrCode, buf) {
|
||||
const base = getBase(nameOrCode)
|
||||
const name = base.name
|
||||
|
||||
return multibase(name, Buffer.from(base.encode(buf)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a buffer or string encoded with multibase header, decodes it and
|
||||
* returns the decoded buffer
|
||||
*
|
||||
* @param {Buffer|string} bufOrString
|
||||
* @returns {Buffer}
|
||||
* @memberof Multibase
|
||||
*
|
||||
*/
|
||||
function decode (bufOrString) {
|
||||
if (Buffer.isBuffer(bufOrString)) {
|
||||
bufOrString = bufOrString.toString()
|
||||
}
|
||||
|
||||
const code = bufOrString.substring(0, 1)
|
||||
bufOrString = bufOrString.substring(1, bufOrString.length)
|
||||
|
||||
if (typeof bufOrString === 'string') {
|
||||
bufOrString = Buffer.from(bufOrString)
|
||||
}
|
||||
|
||||
const base = getBase(code)
|
||||
return Buffer.from(base.decode(bufOrString.toString()))
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the given data multibase encoded?
|
||||
*
|
||||
* @param {Buffer|string} bufOrString
|
||||
* @returns {boolean}
|
||||
* @memberof Multibase
|
||||
*/
|
||||
function isEncoded (bufOrString) {
|
||||
if (Buffer.isBuffer(bufOrString)) {
|
||||
bufOrString = bufOrString.toString()
|
||||
}
|
||||
|
||||
// Ensure bufOrString is a string
|
||||
if (Object.prototype.toString.call(bufOrString) !== '[object String]') {
|
||||
return false
|
||||
}
|
||||
|
||||
const code = bufOrString.substring(0, 1)
|
||||
try {
|
||||
const base = getBase(code)
|
||||
return base.name
|
||||
} catch (err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} name
|
||||
* @param {Buffer} buf
|
||||
* @private
|
||||
* @returns {undefined}
|
||||
*/
|
||||
function validEncode (name, buf) {
|
||||
const base = getBase(name)
|
||||
base.decode(buf.toString())
|
||||
}
|
||||
|
||||
function getBase (nameOrCode) {
|
||||
let base
|
||||
|
||||
if (constants.names[nameOrCode]) {
|
||||
base = constants.names[nameOrCode]
|
||||
} else if (constants.codes[nameOrCode]) {
|
||||
base = constants.codes[nameOrCode]
|
||||
} else {
|
||||
throw new Error('Unsupported encoding')
|
||||
}
|
||||
|
||||
if (!base.isImplemented()) {
|
||||
throw new Error('Base ' + nameOrCode + ' is not implemented yet')
|
||||
}
|
||||
|
||||
return base
|
||||
}
|
||||
Reference in new issue
Block a user