UNPKG

@taqueria/protocol

Version:

A TypeScript package which contains types that are to be shared between @taqueria/node-sdk and @taqueria/taqueria.

168 lines (164 loc) • 5.86 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // SHA256.ts var SHA256_exports = {}; __export(SHA256_exports, { Crypto: () => Crypto, create: () => create2, internalSchema: () => internalSchema, make: () => make, of: () => of, rawSchema: () => rawSchema, schemas: () => schemas, toSHA256: () => toSHA256 }); module.exports = __toCommonJS(SHA256_exports); // TaqError.ts var import_fluture = require("fluture"); var E_TaqError = class extends Error { constructor(taqErr) { super(taqErr.msg); this.context = taqErr.context; this.kind = taqErr.kind; this.name = this.kind; this.previous = taqErr.previous; } }; var toFutureParseErr = (previous, msg, context) => (0, import_fluture.reject)(toParseErr(previous, msg, context)).pipe((0, import_fluture.map)((val) => val)); var toParseErr = (previous, msg, context) => create({ kind: "E_PARSE", msg, context, previous }); var toParseUnknownErr = (previous, msg, context) => create({ kind: "E_PARSE_UNKNOWN", msg, context, previous }); var toFutureParseUnknownErr = (previous, msg, context) => (0, import_fluture.reject)(toParseUnknownErr(previous, msg, context)).pipe((0, import_fluture.map)((val) => val)); var create = (err) => err; // Base.ts var import_fluture2 = require("fluture"); var import_zod = require("zod"); var createSchema = (params) => { var _a, _b; const { rawSchema: rawSchema2, isStringLike } = params; const internalSchema2 = (_a = params.internalSchema) != null ? _a : params.rawSchema; const noop = (val) => val; const transformer = (_b = params.transformer) != null ? _b : noop; const schema = isStringLike ? internalSchema2.transform( (val) => transformer(val) ) : internalSchema2.transform( (val) => transformer(val) ); return { rawSchema: rawSchema2, internalSchema: internalSchema2, schema }; }; var createType = (params) => { const schemas2 = createSchema(params); const { parseErrMsg, unknownErrMsg } = params; const internalOf = (input) => { try { return (0, import_fluture2.resolve)(schemas2.schema.parse(input)); } catch (previous) { const parseMsg = typeof parseErrMsg === "string" ? parseErrMsg : parseErrMsg(input, previous); const unknownMsg = typeof unknownErrMsg === "string" ? unknownErrMsg : unknownErrMsg(input); if (previous instanceof import_zod.ZodError) { return toFutureParseErr(previous, parseMsg, input); } return toFutureParseUnknownErr(previous, unknownMsg, input); } }; const of2 = internalOf; const make2 = (input) => internalOf(input); const create3 = (input) => schemas2.schema.parse(input); const from = (input) => schemas2.schema.parse(input); const factory2 = { make: make2, of: of2, create: create3, from }; return { schemas: schemas2, factory: factory2 }; }; var Base_default = createType; // SHA256.ts var import_fluture3 = require("fluture"); var import_rambda = require("rambda"); var import_zod2 = require("zod"); var eager = (f) => (0, import_fluture3.promise)( (0, import_fluture3.mapRej)((err) => new E_TaqError(err))(f) ); var Crypto = class { constructor() { this.subtle = { digest(_method, _data) { return new ArrayBuffer(50); } }; } }; var getSubtleCrypto = async () => { if ((0, import_rambda.has)("SubtleCrypto", globalThis)) { return Promise.resolve(crypto.subtle); } const { Crypto: Crypto2 } = await import("@peculiar/webcrypto"); const webcrypto = new Crypto2(); return Promise.resolve(webcrypto.subtle); }; var rawSchema = import_zod2.z.string({ description: "SHA256" }).length(64); var { schemas: generatedSchemas, factory } = Base_default({ isStringLike: true, rawSchema, parseErrMsg: (value) => `${value} is an invalid SHA256 hash`, unknownErrMsg: (value) => `Something went wrong trying to parse the following as a SHA256 value, ${value}` }); var toSHA256 = async (value) => { const encoder = new TextEncoder(); const data = encoder.encode(value); const hash = await (await getSubtleCrypto()).digest("SHA-256", data); const hashArray = Array.from(new Uint8Array(hash)); const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join(""); return eager(factory.make(hashHex)); }; var internalSchema = generatedSchemas.schema; var { create: create2, of, make } = factory; var schemas = { ...generatedSchemas, schema: generatedSchemas.schema.transform((val) => val) }; //# sourceMappingURL=SHA256.js.map