Initial commit (by create-cloudflare CLI)

This commit is contained in:
Jonas_Jones 2023-09-12 00:20:38 +02:00
parent 8cb86120f1
commit fff961078a
1777 changed files with 1011798 additions and 0 deletions

View file

@ -0,0 +1,5 @@
export declare type BrowserEncoding = 'hex' | 'base64' | 'utf8';
/**
* @hidden
*/
export declare const mustGetEncoder: (encoding: BrowserEncoding) => (data: Uint8Array) => string;

View file

@ -0,0 +1,28 @@
// A small collection of encodings for convenience of use in the browser.
const decoder = new TextDecoder();
const encoders = {
// certainly not the fastest, but hashes are pretty small
base64: data => btoa(String.fromCharCode(...data)),
hex: data => {
let out = '';
for (const byte of data) {
if (byte < 0x10) {
out += '0';
}
out += byte.toString(16);
}
return out;
},
utf8: data => decoder.decode(data),
};
/**
* @hidden
*/
export const mustGetEncoder = (encoding) => {
const encoder = encoders[encoding];
if (!encoder) {
throw new Error(`Unknown encoding ${encoding}`);
}
return encoder;
};
//# sourceMappingURL=encoding.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"encoding.js","sourceRoot":"","sources":["../../ts/browser/encoding.ts"],"names":[],"mappings":"AAAA,yEAAyE;AAIzE,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAElC,MAAM,QAAQ,GAA6D;IACzE,yDAAyD;IACzD,MAAM,EAAE,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,CAAC;IAElD,GAAG,EAAE,IAAI,CAAC,EAAE;QACV,IAAI,GAAG,GAAG,EAAE,CAAC;QACb,KAAK,MAAM,IAAI,IAAI,IAAI,EAAE;YACvB,IAAI,IAAI,GAAG,IAAI,EAAE;gBACf,GAAG,IAAI,GAAG,CAAC;aACZ;YAED,GAAG,IAAI,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;SAC1B;QAED,OAAO,GAAG,CAAC;IACb,CAAC;IAED,IAAI,EAAE,IAAI,CAAC,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC;CACnC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,QAAyB,EAAE,EAAE;IAC1D,MAAM,OAAO,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACnC,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CAAC,oBAAoB,QAAQ,EAAE,CAAC,CAAC;KACjD;IAED,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC"}

View file

@ -0,0 +1,24 @@
import { BaseHashInput, IBaseHashOptions } from '../base/hash-fn';
import { Hash } from './hash';
/**
* Input used for browser-based hashes.
*/
export declare type HashInput = BaseHashInput | string;
/**
* @hidden
*/
export declare const normalizeInput: (input: import("..").HashInput) => Uint8Array;
/**
* Returns a blake3 hash of the input.
*/
export declare function hash(input: HashInput, { length }?: IBaseHashOptions): Hash;
/**
* Given cryptographic key material and a context string, services a subkey of
* any length. See {@link https://docs.rs/blake3/0.1.3/blake3/fn.derive_key.html}
* for more information.
*/
export declare function deriveKey(context: string, material: HashInput, { length }?: IBaseHashOptions): Hash;
/**
* The keyed hash function. See {@link https://docs.rs/blake3/0.1.3/blake3/fn.keyed_hash.html}.
*/
export declare function keyedHash(key: Uint8Array, input: HashInput, { length }?: IBaseHashOptions): Hash;

View file

@ -0,0 +1,42 @@
import { inputToArray, defaultHashLength } from '../base/hash-fn.js';
import { Hash } from './hash.js';
import { getWasm } from './wasm.js';
const textEncoder = new TextEncoder();
/**
* @hidden
*/
export const normalizeInput = (input) => inputToArray(typeof input === 'string' ? textEncoder.encode(input) : input);
/**
* Returns a blake3 hash of the input.
*/
export function hash(input, { length = defaultHashLength } = {}) {
const result = new Hash(length);
getWasm().hash(normalizeInput(input), result);
return result;
}
/**
* Given cryptographic key material and a context string, services a subkey of
* any length. See {@link https://docs.rs/blake3/0.1.3/blake3/fn.derive_key.html}
* for more information.
*/
export function deriveKey(context, material, { length = defaultHashLength } = {}) {
const derive = getWasm().create_derive(context);
derive.update(normalizeInput(material));
const result = new Hash(length);
derive.digest(result);
return result;
}
/**
* The keyed hash function. See {@link https://docs.rs/blake3/0.1.3/blake3/fn.keyed_hash.html}.
*/
export function keyedHash(key, input, { length = defaultHashLength } = {}) {
if (key.length !== 32) {
throw new Error(`key provided to keyedHash must be 32 bytes, got ${key.length}`);
}
const derive = getWasm().create_keyed(key);
derive.update(normalizeInput(input));
const result = new Hash(length);
derive.digest(result);
return result;
}
//# sourceMappingURL=hash-fn.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"hash-fn.js","sourceRoot":"","sources":["../../ts/browser/hash-fn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmC,YAAY,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACnG,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAOjC,MAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC;AAEtC;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,KAAgB,EAAc,EAAE,CAC7D,YAAY,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AAE9E;;GAEG;AACH,MAAM,UAAU,IAAI,CAClB,KAAgB,EAChB,EAAE,MAAM,GAAG,iBAAiB,KAAuB,EAAE;IAErD,MAAM,MAAM,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC;IAChC,OAAO,EAAE,CAAC,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,CAAC;IAC9C,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,SAAS,CACvB,OAAe,EACf,QAAmB,EACnB,EAAE,MAAM,GAAG,iBAAiB,KAAuB,EAAE;IAErD,MAAM,MAAM,GAAG,OAAO,EAAE,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IAChD,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC,CAAC;IACxC,MAAM,MAAM,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC;IAChC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACtB,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,SAAS,CACvB,GAAe,EACf,KAAgB,EAChB,EAAE,MAAM,GAAG,iBAAiB,KAAuB,EAAE;IAErD,IAAI,GAAG,CAAC,MAAM,KAAK,EAAE,EAAE;QACrB,MAAM,IAAI,KAAK,CAAC,mDAAmD,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;KAClF;IAED,MAAM,MAAM,GAAG,OAAO,EAAE,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;IAC3C,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC;IACrC,MAAM,MAAM,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC;IAChC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACtB,OAAO,MAAM,CAAC;AAChB,CAAC"}

View file

@ -0,0 +1,31 @@
import { BaseHash as BaseHasher } from '../base/index';
import { HashInput } from './hash-fn';
import { BrowserEncoding } from './encoding';
import { IBaseHashOptions } from '../base/hash-fn';
import { BrowserHashReader } from './hash-reader';
import { IInternalReader } from '../base/hash-reader';
import { Hash } from './hash';
/**
* @inheritdoc
*/
export declare class BrowserHasher extends BaseHasher<Hash, IInternalReader, BrowserHashReader> {
/**
* @inheritdoc
* @override
*/
update(data: HashInput): this;
/**
* Returns a digest of the hash with the given encoding.
*/
digest(options?: IBaseHashOptions): Hash;
digest(encoding: undefined, options: IBaseHashOptions): Hash;
digest(encoding: BrowserEncoding, options?: IBaseHashOptions): string;
}
/**
* A Node.js crypto-like createHash method.
*/
export declare const createHash: () => BrowserHasher;
/**
* A Node.js crypto-like createHash method.
*/
export declare const createKeyed: (key: Uint8Array) => BrowserHasher;

View file

@ -0,0 +1,41 @@
import { BaseHash as BaseHasher } from '../base/index.js';
import { normalizeInput } from './hash-fn.js';
import { mustGetEncoder } from './encoding.js';
import { BrowserHashReader } from './hash-reader.js';
import { Hash } from './hash.js';
import { getWasm } from './wasm.js';
/**
* @inheritdoc
*/
export class BrowserHasher extends BaseHasher {
/**
* @inheritdoc
* @override
*/
update(data) {
return super.update(normalizeInput(data));
}
digest(encoding, options) {
let resolvedOpts;
let resolvedEnc;
if (encoding && typeof encoding === 'object') {
resolvedOpts = encoding;
resolvedEnc = undefined;
}
else {
resolvedOpts = options;
resolvedEnc = encoding;
}
const result = super.digest(resolvedOpts);
return resolvedEnc ? mustGetEncoder(resolvedEnc)(result) : result;
}
}
/**
* A Node.js crypto-like createHash method.
*/
export const createHash = () => new BrowserHasher(getWasm().create_hasher(), l => new Hash(l), r => new BrowserHashReader(r));
/**
* A Node.js crypto-like createHash method.
*/
export const createKeyed = (key) => new BrowserHasher(getWasm().create_keyed(key), l => new Hash(l), r => new BrowserHashReader(r));
//# sourceMappingURL=hash-instance.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"hash-instance.js","sourceRoot":"","sources":["../../ts/browser/hash-instance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,UAAU,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,cAAc,EAAa,MAAM,WAAW,CAAC;AACtD,OAAO,EAAmB,cAAc,EAAE,MAAM,YAAY,CAAC;AAE7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAElD,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAEjC;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,UAAoD;IACrF;;;OAGG;IACI,MAAM,CAAC,IAAe;QAC3B,OAAO,KAAK,CAAC,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,CAAC;IAC5C,CAAC;IAQM,MAAM,CACX,QAA6C,EAC7C,OAA0B;QAE1B,IAAI,YAA0C,CAAC;QAC/C,IAAI,WAAwC,CAAC;QAC7C,IAAI,QAAQ,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE;YAC5C,YAAY,GAAG,QAAQ,CAAC;YACxB,WAAW,GAAG,SAAS,CAAC;SACzB;aAAM;YACL,YAAY,GAAG,OAAO,CAAC;YACvB,WAAW,GAAG,QAAQ,CAAC;SACxB;QAED,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;QAC1C,OAAO,WAAW,CAAC,CAAC,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IACpE,CAAC;CACF;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,GAAG,EAAE,CAC7B,IAAI,aAAa,CACf,OAAO,EAAE,CAAC,aAAa,EAAE,EACzB,CAAC,CAAC,EAAE,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,EAChB,CAAC,CAAC,EAAE,CAAC,IAAI,iBAAiB,CAAC,CAAC,CAAC,CAC9B,CAAC;AAEJ;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,GAAe,EAAE,EAAE,CAC7C,IAAI,aAAa,CACf,OAAO,EAAE,CAAC,YAAY,CAAC,GAAG,CAAC,EAC3B,CAAC,CAAC,EAAE,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,EAChB,CAAC,CAAC,EAAE,CAAC,IAAI,iBAAiB,CAAC,CAAC,CAAC,CAC9B,CAAC"}

View file

@ -0,0 +1,17 @@
import { BaseHashReader } from '../base/hash-reader';
import { BrowserEncoding } from './encoding';
import { Hash } from './hash';
/**
* A hash reader for WebAssembly targets.
*/
export declare class BrowserHashReader extends BaseHashReader<Hash> {
/**
* Converts first 32 bytes of the hash to a string with the given encoding.
*/
toString(encoding?: BrowserEncoding): string;
/**
* Converts first 32 bytes of the hash to an array.
*/
toArray(): Hash;
protected alloc(bytes: number): Hash;
}

View file

@ -0,0 +1,25 @@
import { BaseHashReader } from '../base/hash-reader.js';
import { Hash } from './hash.js';
import { defaultHashLength } from '../base/index.js';
/**
* A hash reader for WebAssembly targets.
*/
export class BrowserHashReader extends BaseHashReader {
/**
* Converts first 32 bytes of the hash to a string with the given encoding.
*/
toString(encoding = 'hex') {
return this.toArray().toString(encoding);
}
/**
* Converts first 32 bytes of the hash to an array.
*/
toArray() {
this.position = BigInt(0);
return this.read(defaultHashLength);
}
alloc(bytes) {
return new Hash(bytes);
}
}
//# sourceMappingURL=hash-reader.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"hash-reader.js","sourceRoot":"","sources":["../../ts/browser/hash-reader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAElD;;GAEG;AACH,MAAM,OAAO,iBAAkB,SAAQ,cAAoB;IACzD;;OAEG;IACI,QAAQ,CAAC,WAA4B,KAAK;QAC/C,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAC3C,CAAC;IAED;;OAEG;IACI,OAAO;QACZ,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;QAC1B,OAAO,IAAI,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;IACtC,CAAC;IAES,KAAK,CAAC,KAAa;QAC3B,OAAO,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC;IACzB,CAAC;CACF"}

View file

@ -0,0 +1,11 @@
import { BrowserEncoding } from './encoding';
/**
* Hash returned from functions in the browser.
*/
export declare class Hash extends Uint8Array {
/**
* A constant-time comparison against the other hash/array.
*/
equals(other: unknown): boolean;
toString(encoding?: BrowserEncoding): string;
}

View file

@ -0,0 +1,26 @@
import { mustGetEncoder } from './encoding.js';
/**
* Hash returned from functions in the browser.
*/
export class Hash extends Uint8Array {
/**
* A constant-time comparison against the other hash/array.
*/
equals(other) {
if (!(other instanceof Uint8Array)) {
return false;
}
if (other.length !== this.length) {
return false;
}
let cmp = 0;
for (let i = 0; i < this.length; i++) {
cmp |= this[i] ^ other[i];
}
return cmp === 0;
}
toString(encoding = 'hex') {
return mustGetEncoder(encoding)(this);
}
}
//# sourceMappingURL=hash.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../../ts/browser/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,cAAc,EAAE,MAAM,YAAY,CAAC;AAE7D;;GAEG;AACH,MAAM,OAAO,IAAK,SAAQ,UAAU;IAClC;;OAEG;IACI,MAAM,CAAC,KAAc;QAC1B,IAAI,CAAC,CAAC,KAAK,YAAY,UAAU,CAAC,EAAE;YAClC,OAAO,KAAK,CAAC;SACd;QAED,IAAI,KAAK,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM,EAAE;YAChC,OAAO,KAAK,CAAC;SACd;QAED,IAAI,GAAG,GAAG,CAAC,CAAC;QACZ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YACpC,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;SAC3B;QAED,OAAO,GAAG,KAAK,CAAC,CAAC;IACnB,CAAC;IAEM,QAAQ,CAAC,WAA4B,KAAK;QAC/C,OAAO,cAAc,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC;IACxC,CAAC;CACF"}

View file

@ -0,0 +1,3 @@
export { hash, HashInput, deriveKey, keyedHash } from './hash-fn';
export * from './hash-instance';
export * from '../base/index';

View file

@ -0,0 +1,4 @@
export { hash, deriveKey, keyedHash } from './hash-fn.js';
export * from './hash-instance.js';
export * from '../base/index.js';
//# sourceMappingURL=index.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../ts/browser/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAa,SAAS,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAClE,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC"}

View file

@ -0,0 +1,10 @@
import * as rawWasm from '../../dist/wasm/browser/blake3_js';
/**
* Gets the webassembly module provided in provideWasm.
*/
export declare const getWasm: () => typeof rawWasm;
/**
* Sets the webassembly module used for the browser build. This indirection is
* needed to provide compatibility between the "browser" and "browser-async" modes.
*/
export declare const provideWasm: (w: typeof rawWasm) => void;

View file

@ -0,0 +1,18 @@
let wasm;
/**
* Gets the webassembly module provided in provideWasm.
*/
export const getWasm = () => {
if (!wasm) {
throw new Error('BLAKE3 webassembly not loaded. Please import the module via `blake3/browser` or `blake3/browser-async`');
}
return wasm;
};
/**
* Sets the webassembly module used for the browser build. This indirection is
* needed to provide compatibility between the "browser" and "browser-async" modes.
*/
export const provideWasm = (w) => {
wasm = w;
};
//# sourceMappingURL=wasm.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"wasm.js","sourceRoot":"","sources":["../../ts/browser/wasm.ts"],"names":[],"mappings":"AAEA,IAAI,IAAoB,CAAC;AAEzB;;GAEG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,GAAG,EAAE;IAC1B,IAAI,CAAC,IAAI,EAAE;QACT,MAAM,IAAI,KAAK,CACb,wGAAwG,CACzG,CAAC;KACH;IAED,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAiB,EAAE,EAAE;IAC/C,IAAI,GAAG,CAAC,CAAC;AACX,CAAC,CAAC"}