Dorian 0d073fa89e Add comprehensive installation and setup documentation
- Add GETTING_STARTED.md with quick start guide and development modes
- Add INSTALL.sh automated installation script
- Add INSTALLATION_CHECKLIST.md, INSTALLATION_SUCCESS.md, and INSTALLATION_SUMMARY.md
- Add QUICK_REFERENCE.md for common commands
- Add SETUP_GUIDE.md with detailed setup instructions
- Update README.md with improved project overview
- Add did-wallet app dependencies and node_modules
2026-01-27 17:18:21 +00:00

62 lines
2.0 KiB
JavaScript

import { makeCborEncoders, objectToTokens } from './encode.js'
import { quickEncodeToken } from './jump.js'
/**
* @typedef {import('../interface').EncodeOptions} EncodeOptions
* @typedef {import('../interface').TokenTypeEncoder} TokenTypeEncoder
* @typedef {import('../interface').TokenOrNestedTokens} TokenOrNestedTokens
*/
const cborEncoders = makeCborEncoders()
/** @type {EncodeOptions} */
const defaultEncodeOptions = {
float64: false,
quickEncodeToken
}
/**
* Calculate the byte length of the given data when encoded as CBOR with the
* options provided.
* This calculation will be accurate if the same options are used as when
* performing a normal encode. Some encode options can change the encoding
* output length.
*
* @param {any} data
* @param {EncodeOptions} [options]
* @returns {number}
*/
export function encodedLength (data, options) {
options = Object.assign({}, defaultEncodeOptions, options)
options.mapSorter = undefined // won't change the length
const tokens = objectToTokens(data, options)
return tokensToLength(tokens, cborEncoders, options)
}
/**
* Calculate the byte length of the data as represented by the given tokens when
* encoded as CBOR with the options provided.
* This function is for advanced users and would not normally be called
* directly. See `encodedLength()` for appropriate use.
*
* @param {TokenOrNestedTokens} tokens
* @param {TokenTypeEncoder[]} [encoders]
* @param {EncodeOptions} [options]
*/
export function tokensToLength (tokens, encoders = cborEncoders, options = defaultEncodeOptions) {
if (Array.isArray(tokens)) {
let len = 0
for (const token of tokens) {
len += tokensToLength(token, encoders, options)
}
return len
} else {
const encoder = encoders[tokens.type.major]
/* c8 ignore next 3 */
if (encoder.encodedSize === undefined || typeof encoder.encodedSize !== 'function') {
throw new Error(`Encoder for ${tokens.type.name} does not have an encodedSize()`)
}
return encoder.encodedSize(tokens, options)
}
}