Skip to content

Commit 25e5f10

Browse files
committed
feat: selector builder and async reifier
1 parent 70f520f commit 25e5f10

File tree

2 files changed

+112
-4
lines changed

2 files changed

+112
-4
lines changed

src/traversal.ts

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ export type SelectorNode = {
208208
// Sequence
209209
":>"?: SelectorNode;
210210
};
211+
// ExploreInterpretAs
211212
"~"?: {
212213
// adl
213214
as: string;
@@ -220,7 +221,7 @@ export type SelectorNode = {
220221
"!"?: SelectorNode; // StopAt
221222
};
222223

223-
type LimitNode = {
224+
export type LimitNode = {
224225
none?: {}; // LimitNone
225226
depth?: number; // LimitDepth
226227
};
@@ -255,6 +256,75 @@ export const entriesSelector: SelectorNode = {
255256
},
256257
};
257258

259+
export const selectorBuilder = {
260+
depth(val: number): LimitNode {
261+
return {
262+
depth: val,
263+
};
264+
},
265+
noLimit(): LimitNode {
266+
return {
267+
none: {},
268+
};
269+
},
270+
exploreRecursive(next: SelectorNode, limit: LimitNode): SelectorNode {
271+
return {
272+
R: {
273+
l: limit,
274+
":>": next,
275+
},
276+
};
277+
},
278+
exploreAll(next: SelectorNode): SelectorNode {
279+
return {
280+
a: {
281+
">": next,
282+
},
283+
};
284+
},
285+
edge(): SelectorNode {
286+
return {
287+
"@": {},
288+
};
289+
},
290+
exploreFields(fields: {[key: string]: SelectorNode}): SelectorNode {
291+
return {
292+
f: {
293+
"f>": fields,
294+
},
295+
};
296+
},
297+
exploreInterpretAs(name: string, next: SelectorNode): SelectorNode {
298+
return {
299+
"~": {
300+
as: name,
301+
">": next,
302+
},
303+
};
304+
},
305+
match(): SelectorNode {
306+
return {
307+
".": {},
308+
};
309+
},
310+
matchSubset(from: number, to: number): SelectorNode {
311+
return {
312+
".": {
313+
"[": from,
314+
"]": to,
315+
},
316+
};
317+
},
318+
exploreIndex(index: number, next: SelectorNode): SelectorNode {
319+
return {
320+
i: {
321+
i: index,
322+
">": next,
323+
},
324+
};
325+
},
326+
};
327+
258328
export class PathSegment {
259329
value: string | number;
260330
constructor(value: string | number) {
@@ -570,11 +640,14 @@ async function* mapEntries(node: {
570640
}
571641
}
572642

573-
export type NodeReifier = (node: Node, loader: LinkLoader) => Node;
643+
export type NodeReifier = (node: Node, loader: LinkLoader) => Promise<Node>;
574644

575645
export type KnownReifiers = {[key: string]: NodeReifier};
576646

577-
export function unixfsReifier(node: Node, loader: LinkLoader): Node {
647+
export async function unixfsReifier(
648+
node: Node,
649+
loader: LinkLoader
650+
): Promise<Node> {
578651
if (
579652
node.kind == Kind.Map &&
580653
node.value.Data &&
@@ -619,7 +692,7 @@ export async function* walkBlocks(
619692
if (sel instanceof ExploreInterpretAs) {
620693
const reify = source.reifier(sel.adl);
621694
if (reify) {
622-
const rnd = reify(nd, source);
695+
const rnd = await reify(nd, source);
623696
const next = sel.explore(nd, new PathSegment(""));
624697
if (next) {
625698
sel = next;

test/traversal.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import {importer} from "ipfs-unixfs-importer";
77
import {
88
BasicNode,
99
allSelector,
10+
entriesSelector,
1011
parseContext,
1112
LinkSystem,
1213
walkBlocks,
1314
ExploreRecursive,
1415
unixfsReifier,
16+
selectorBuilder as builder,
1517
} from "../src/traversal.js";
1618
import {unixfsPathSelector} from "../src/resolver.js";
1719

@@ -190,4 +192,37 @@ describe("traversal", () => {
190192
}
191193
expect(i).to.equal(expected.length);
192194
});
195+
196+
it("build a selector", () => {
197+
const selector1 = builder.exploreRecursive(
198+
builder.exploreAll(builder.edge()),
199+
builder.noLimit()
200+
);
201+
202+
expect(selector1).to.deep.equal(allSelector);
203+
204+
const selector2 = builder.exploreRecursive(
205+
builder.exploreAll(builder.edge()),
206+
builder.depth(1)
207+
);
208+
209+
expect(selector2).to.deep.equal(entriesSelector);
210+
211+
const {selector: unixfsSelector} = unixfsPathSelector(
212+
"bafybeiepvdqmdakhtwotvykxujrmt5fcq4xca5jmoo6wzxhjk3q3pqe4te/children/third"
213+
);
214+
215+
const selector3 = builder.exploreInterpretAs(
216+
"unixfs",
217+
builder.exploreFields({
218+
children: builder.exploreInterpretAs(
219+
"unixfs",
220+
builder.exploreFields({
221+
third: selector1,
222+
})
223+
),
224+
})
225+
);
226+
expect(selector3).to.deep.equal(unixfsSelector);
227+
});
193228
});

0 commit comments

Comments
 (0)