From 913a0b6663902e6c92bb82ae968040f11bcf5b22 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Wed, 27 Jun 2018 11:30:34 -0500 Subject: Use more generic pseudo-code --- proposals/0000-hypercore-header.md | 47 +++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 24 deletions(-) (limited to 'proposals') diff --git a/proposals/0000-hypercore-header.md b/proposals/0000-hypercore-header.md index 6718c46..3b44f65 100644 --- a/proposals/0000-hypercore-header.md +++ b/proposals/0000-hypercore-header.md @@ -34,30 +34,29 @@ The "header" is the first entry in a hypercore. It includes a `type` and an opti A program that is reading a hypercore will examine the `type` to determine how to process the hypercore. -```js -function loadCorrectStructure (hypercore, cb) { - readHeader(hypercore, (err, header) => { - if (err) return cb(err) - - if (!header) { - // no header present - treat as a hypercore - return cb(null, hypercore) - } - - switch (header.type) { - case 'hyperdrive': - return cb(null, createHyperdriveV1(hypercore, header)) - case 'hyperdrive-v2': - return cb(null, createHyperdriveV2(hypercore, header)) - case 'hyperdb': - return cb(null, createHyperdbV1(hypercore, header)) - case '': - return cb(null, hypercore) // no structure, treat as a hypercore - // ... - default: - return cb(new Error('Unsupported: ' + header.type)) - } - }) +``` +function loadCorrectStructure (hypercore) { + + var header = parseHypercoreHeader(hypercore.readEntry(0)) + + if (!header) { + // no header present - treat as a hypercore + return hypercore + } + + switch (header.type) { + case 'hyperdrive': + return new HyperdriveV1(hypercore, header) + case 'hyperdrive-v2': + return new HyperdriveV2(hypercore, header) + case 'hyperdb': + return new HyperdbV1(hypercore, header) + // ... + default: + // unknown type - treat as a hypercore + return hypercore + } + } ``` -- cgit v1.2.3