{"version":3,"file":"index.mjs","names":[],"sources":["../../src/crypto.ts","../../src/defaults.ts","../../src/util.ts","../../src/hypercore-factory.ts","../../src/iterator.ts"],"sourcesContent":["//\n// Copyright 2022 DXOS.org\n//\n\nimport { callbackify } from 'node:util';\n\nimport { type Codec, type EncodingOptions } from '@dxos/codec-protobuf';\nimport { type Signer, verifySignature } from '@dxos/crypto';\nimport { invariant } from '@dxos/invariant';\nimport { type PublicKey } from '@dxos/keys';\nimport { arrayToBuffer } from '@dxos/util';\nimport { type AbstractValueEncoding, type Crypto } from '@dxos/vendor-hypercore/hypercore';\n\n/**\n * Create encoding (e.g., from protobuf codec).\n */\nexport const createCodecEncoding = <T>(codec: Codec<T>, opts?: EncodingOptions): AbstractValueEncoding<T> => ({\n  encode: (obj: T) => arrayToBuffer(codec.encode(obj, opts)),\n  decode: (buffer: Buffer) => codec.decode(buffer, opts),\n});\n\n/**\n * Create a custom hypercore crypto signer.\n */\n// TODO(burdon): Create test without adding deps.\nexport const createCrypto = (signer: Signer, publicKey: PublicKey): Crypto => {\n  invariant(signer);\n  invariant(publicKey);\n\n  return {\n    sign: (message, secretKey, cb) => {\n      callbackify(signer.sign.bind(signer!))(publicKey, message, (err, result) => {\n        if (err) {\n          cb(err, null);\n          return;\n        }\n\n        cb(null, arrayToBuffer(result));\n      });\n    },\n\n    verify: async (message, signature, key, cb) => {\n      // NOTE: Uses the public key passed into function.\n      callbackify(verifySignature)(publicKey, message, signature, cb);\n    },\n  };\n};\n","//\n// Copyright 2022 DXOS.org\n//\n\nimport type {\n  HypercoreOptions,\n  ReadStreamOptions,\n  ReplicationOptions,\n  WriteStreamOptions,\n} from '@dxos/vendor-hypercore/hypercore';\n\n/**\n * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-feed--hypercorestorage-key-options\n */\nexport const defaultFeedOptions: HypercoreOptions = {\n  createIfMissing: true,\n  valueEncoding: 'binary',\n};\n\n/**\n * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-stream--feedcreatereadstreamoptions\n */\nexport const defaultReadStreamOptions: ReadStreamOptions = {\n  start: 0,\n  end: Infinity,\n  snapshot: true,\n  tail: false,\n  live: false,\n  timeout: 0,\n  wait: true,\n  batch: 1,\n};\n\n/**\n * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-stream--feedcreatewritestreamopts\n */\nexport const defaultWriteStreamOptions: WriteStreamOptions = {\n  maxBlockSize: Infinity,\n};\n\n/**\n * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-stream--feedreplicateisinitiator-options\n */\nexport const defaultReplicateOptions: ReplicationOptions = {\n  live: false,\n  ack: false,\n  download: true,\n  upload: true,\n  encrypted: true,\n  noise: true,\n};\n","//\n// Copyright 2022 DXOS.org\n//\n\nimport { promisify } from 'node:util';\n\nexport const py = (obj: any, fn: Function) => promisify(fn.bind(obj));\n","//\n// Copyright 2022 DXOS.org\n//\n\nimport { invariant } from '@dxos/invariant';\nimport { type Directory, StorageType, createStorage } from '@dxos/random-access-storage';\nimport hypercore from '@dxos/vendor-hypercore/hypercore';\nimport type { Hypercore, HypercoreOptions } from '@dxos/vendor-hypercore/hypercore';\n\nimport { py } from './util';\n\n/**\n * Creates feeds with default properties.\n */\nexport class HypercoreFactory<T> {\n  constructor(\n    private readonly _root: Directory = createStorage({ type: StorageType.RAM }).createDirectory(),\n    private readonly _options?: HypercoreOptions,\n  ) {\n    invariant(this._root);\n  }\n\n  /**\n   * Creates a feed using a storage factory prefixed with the feed's key.\n   * NOTE: We have to use our `random-access-storage` implementation since the native ones\n   * do not behave uniformly across platforms.\n   */\n  createFeed(publicKey: Buffer, options?: HypercoreOptions): Hypercore<T> {\n    const directory = this._root.createDirectory(publicKey.toString('hex'));\n    const storage = (filename: string) => directory.getOrCreateFile(filename).native;\n    return hypercore(storage, publicKey, Object.assign({}, this._options, options));\n  }\n\n  /**\n   * Creates and opens a feed.\n   */\n  async openFeed(publicKey: Buffer, options?: HypercoreOptions): Promise<Hypercore<T>> {\n    const feed = this.createFeed(publicKey, options);\n    await py(feed, feed.open)(); // TODO(burdon): Sometimes strange bug if done inside function.\n    return feed;\n  }\n}\n","//\n// Copyright 2022 DXOS.org\n//\n\nimport { Readable } from 'readable-stream';\nimport { type Readable as StreamXReadable } from 'streamx';\n\n/**\n * Wraps streamx.Readable (hypercore.createReadStream) to a standard Readable stream.\n *\n * The read-stream package is mirror of the streams implementations in Node.js 18.9.0.\n * This function is here to standardize the cast in case there are incompatibilities\n * across different platforms.\n *\n * Hypercore createReadStream returns a `streamx` Readable, which does not close properly on destroy.\n *\n * https://github.com/nodejs/readable-stream\n * https://nodejs.org/api/stream.html#readable-streams\n * https://nodejs.org/dist/v18.9.0/docs/api/stream.html#readablewrapstream\n */\nexport const createReadable = (stream: StreamXReadable): Readable => {\n  return new Readable({ objectMode: true }).wrap(stream as any);\n};\n\n/**\n * Converts streamx.Readable (hypercore.createReadStream) to an async iterator.\n *\n * https://github.com/tc39/proposal-async-iteration\n * https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-3.html#async-iteration\n * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators\n * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols\n */\nexport const createAsyncIterator = (stream: Readable): AsyncIterator<any> => {\n  return stream[Symbol.asyncIterator]();\n};\n"],"mappings":";;;;;;;;;;;;AAgBA,IAAa,uBAA0B,OAAiB,UAAsD;CAC5G,SAAS,QAAW,cAAc,MAAM,OAAO,KAAK,IAAI,CAAC;CACzD,SAAS,WAAmB,MAAM,OAAO,QAAQ,IAAI;AACvD;;;;AAMA,IAAa,gBAAgB,QAAgB,cAAiC;CAC5E,UAAU,QAAK,KAAA,GAAA;EAAA,YAAA;EAAA,GAAA;EAAA,GAAA;EAAA,GAAA,KAAA;EAAA,GAAA,CAAA,UAAA,EAAA;CAAA,CAAC;CAChB,UAAU,WAAQ,KAAA,GAAA;EAAA,YAAA;EAAA,GAAA;EAAA,GAAA;EAAA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