OverviewFilesCommitsIssuesDocsReleasesPeers

favela-web / lib / bare-globals.js

33 lines · javascript ref:
// Minimal web-global shims for Bare (adapted from hyper-proto-peer/lib/bare-globals.js).
// Bare omits TextEncoder/TextDecoder (used at module-load by deps like @noble/curves and by
// atcute's encoding paths). Buffer IS a Bare global, so we back them with it. Import FIRST.
// atcute's P2P handler REPLACES fetch, so we deliberately do NOT shim fetch — only what the
// lexicon/encoding + Response-handling paths need. No-op under Node.

import bareProcess from 'bare-process'
if (typeof globalThis.process === 'undefined') globalThis.process = bareProcess

if (typeof globalThis.TextEncoder === 'undefined') {
  globalThis.TextEncoder = class TextEncoder {
    get encoding () { return 'utf-8' }
    encode (input = '') { return new Uint8Array(Buffer.from(String(input), 'utf8')) }
    encodeInto (src, dest) {
      const bytes = this.encode(src)
      const n = Math.min(bytes.length, dest.length)
      dest.set(bytes.subarray(0, n))
      return { read: src.length, written: n }
    }
  }
}

if (typeof globalThis.TextDecoder === 'undefined') {
  globalThis.TextDecoder = class TextDecoder {
    get encoding () { return 'utf-8' }
    decode (input) {
      if (input == null) return ''
      const u8 = input instanceof Uint8Array ? input : new Uint8Array(input.buffer || input, input.byteOffset || 0, input.byteLength)
      return Buffer.from(u8).toString('utf8')
    }
  }
}