@dxos/hypercore 0.9.1-staging.ee54ba693a → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,161 @@
1
+ import hypercore, { default as hypercore$1 } from "@dxos/vendor-hypercore/hypercore";
2
+ import { callbackify, promisify } from "@dxos/node-std/util";
3
+ import { verifySignature } from "@dxos/crypto";
4
+ import { invariant } from "@dxos/invariant";
5
+ import { arrayToBuffer } from "@dxos/util";
6
+ import { StorageType, createStorage } from "@dxos/random-access-storage";
7
+ import { Readable } from "readable-stream";
8
+ //#region src/crypto.ts
9
+ var __dxlog_file$1 = "/__w/dxos/dxos/packages/common/hypercore/src/crypto.ts";
10
+ /**
11
+ * Create encoding (e.g., from protobuf codec).
12
+ */
13
+ var createCodecEncoding = (codec, opts) => ({
14
+ encode: (obj) => arrayToBuffer(codec.encode(obj, opts)),
15
+ decode: (buffer) => codec.decode(buffer, opts)
16
+ });
17
+ /**
18
+ * Create a custom hypercore crypto signer.
19
+ */
20
+ var createCrypto = (signer, publicKey) => {
21
+ invariant(signer, void 0, {
22
+ "~LogMeta": "~LogMeta",
23
+ F: __dxlog_file$1,
24
+ L: 27,
25
+ S: void 0,
26
+ A: ["signer", ""]
27
+ });
28
+ invariant(publicKey, void 0, {
29
+ "~LogMeta": "~LogMeta",
30
+ F: __dxlog_file$1,
31
+ L: 28,
32
+ S: void 0,
33
+ A: ["publicKey", ""]
34
+ });
35
+ return {
36
+ sign: (message, secretKey, cb) => {
37
+ callbackify(signer.sign.bind(signer))(publicKey, message, (err, result) => {
38
+ if (err) {
39
+ cb(err, null);
40
+ return;
41
+ }
42
+ cb(null, arrayToBuffer(result));
43
+ });
44
+ },
45
+ verify: async (message, signature, key, cb) => {
46
+ callbackify(verifySignature)(publicKey, message, signature, cb);
47
+ }
48
+ };
49
+ };
50
+ //#endregion
51
+ //#region src/defaults.ts
52
+ /**
53
+ * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-feed--hypercorestorage-key-options
54
+ */
55
+ var defaultFeedOptions = {
56
+ createIfMissing: true,
57
+ valueEncoding: "binary"
58
+ };
59
+ /**
60
+ * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-stream--feedcreatereadstreamoptions
61
+ */
62
+ var defaultReadStreamOptions = {
63
+ start: 0,
64
+ end: Infinity,
65
+ snapshot: true,
66
+ tail: false,
67
+ live: false,
68
+ timeout: 0,
69
+ wait: true,
70
+ batch: 1
71
+ };
72
+ /**
73
+ * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-stream--feedcreatewritestreamopts
74
+ */
75
+ var defaultWriteStreamOptions = { maxBlockSize: Infinity };
76
+ /**
77
+ * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-stream--feedreplicateisinitiator-options
78
+ */
79
+ var defaultReplicateOptions = {
80
+ live: false,
81
+ ack: false,
82
+ download: true,
83
+ upload: true,
84
+ encrypted: true,
85
+ noise: true
86
+ };
87
+ //#endregion
88
+ //#region src/util.ts
89
+ var py = (obj, fn) => promisify(fn.bind(obj));
90
+ //#endregion
91
+ //#region src/hypercore-factory.ts
92
+ var __dxlog_file = "/__w/dxos/dxos/packages/common/hypercore/src/hypercore-factory.ts";
93
+ /**
94
+ * Creates feeds with default properties.
95
+ */
96
+ var HypercoreFactory = class {
97
+ _root;
98
+ _options;
99
+ constructor(_root = createStorage({ type: StorageType.RAM }).createDirectory(), _options) {
100
+ this._root = _root;
101
+ this._options = _options;
102
+ invariant(this._root, void 0, {
103
+ "~LogMeta": "~LogMeta",
104
+ F: __dxlog_file,
105
+ L: 20,
106
+ S: this,
107
+ A: ["this._root", ""]
108
+ });
109
+ }
110
+ /**
111
+ * Creates a feed using a storage factory prefixed with the feed's key.
112
+ * NOTE: We have to use our `random-access-storage` implementation since the native ones
113
+ * do not behave uniformly across platforms.
114
+ */
115
+ createFeed(publicKey, options) {
116
+ const directory = this._root.createDirectory(publicKey.toString("hex"));
117
+ const storage = (filename) => directory.getOrCreateFile(filename).native;
118
+ return hypercore$1(storage, publicKey, Object.assign({}, this._options, options));
119
+ }
120
+ /**
121
+ * Creates and opens a feed.
122
+ */
123
+ async openFeed(publicKey, options) {
124
+ const feed = this.createFeed(publicKey, options);
125
+ await py(feed, feed.open)();
126
+ return feed;
127
+ }
128
+ };
129
+ //#endregion
130
+ //#region src/iterator.ts
131
+ /**
132
+ * Wraps streamx.Readable (hypercore.createReadStream) to a standard Readable stream.
133
+ *
134
+ * The read-stream package is mirror of the streams implementations in Node.js 18.9.0.
135
+ * This function is here to standardize the cast in case there are incompatibilities
136
+ * across different platforms.
137
+ *
138
+ * Hypercore createReadStream returns a `streamx` Readable, which does not close properly on destroy.
139
+ *
140
+ * https://github.com/nodejs/readable-stream
141
+ * https://nodejs.org/api/stream.html#readable-streams
142
+ * https://nodejs.org/dist/v18.9.0/docs/api/stream.html#readablewrapstream
143
+ */
144
+ var createReadable = (stream) => {
145
+ return new Readable({ objectMode: true }).wrap(stream);
146
+ };
147
+ /**
148
+ * Converts streamx.Readable (hypercore.createReadStream) to an async iterator.
149
+ *
150
+ * https://github.com/tc39/proposal-async-iteration
151
+ * https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-3.html#async-iteration
152
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators
153
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
154
+ */
155
+ var createAsyncIterator = (stream) => {
156
+ return stream[Symbol.asyncIterator]();
157
+ };
158
+ //#endregion
159
+ export { HypercoreFactory, createAsyncIterator, createCodecEncoding, createCrypto, createReadable, defaultFeedOptions, defaultReadStreamOptions, defaultReplicateOptions, defaultWriteStreamOptions, hypercore };
160
+
161
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"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,GAAA,KAAA;EAAA,GAAA,CAAA,aAAA,EAAA;CAAA,CAAC;CAEnB,OAAO;EACL,OAAO,SAAS,WAAW,OAAO;GAChC,YAAY,OAAO,KAAK,KAAK,MAAO,CAAC,CAAC,CAAC,WAAW,UAAU,KAAK,WAAW;IAC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