Skip to content

Commit

Permalink
Update GitHub Action; Make NTSchema prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
zjkmxy committed Feb 9, 2024
1 parent 84bd387 commit 5593f4a
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/lint-code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
- name: Set up pnpm
uses: pnpm/action-setup@v2
uses: pnpm/action-setup@v3
with:
version: 8
run_install: false
- name: Set up Node
uses: actions/setup-node@v4
with:
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
- name: Set up pnpm
uses: pnpm/action-setup@v2
uses: pnpm/action-setup@v3
with:
version: 8
run_install: false
- name: Set up Node
uses: actions/setup-node@v4
with:
Expand Down
4 changes: 2 additions & 2 deletions src/namespace/base-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ export class BaseNode {
public async processAttach(path: namePattern.Pattern, handler: NamespaceHandler) {
// All children's attach events are called
this.handler = handler;
await this.onAttach.emit(path, handler.endpoint);
await this.onAttach.emit(path, handler.endpoint!);
}

public async processDetach() {
await this.onDetach.emit(this.handler!.endpoint);
await this.onDetach.emit(this.handler!.endpoint!);
this.handler = undefined;
// Then call children's detach
}
Expand Down
5 changes: 3 additions & 2 deletions src/namespace/expressing-point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ export class ExpressingPoint extends BaseNode {
} = {},
): Promise<Data | undefined> {
// Construct Interest, but without signing, so the parameter digest is not there
const interestArgs = [matched.name] as Array<Interest.CtorArg>;
const interestName = this.handler!.attachedPrefix!.append(...matched.name.comps);
const interestArgs = [interestName] as Array<Interest.CtorArg>;
if (this.config.canBePrefix) {
// Be aware that if CanBePrefix is set, you may need to also validate the data against the LeafNode's validator.
interestArgs.push(Interest.CanBePrefix);
Expand Down Expand Up @@ -180,7 +181,7 @@ export class ExpressingPoint extends BaseNode {
return undefined;
}

const data = await this.handler!.endpoint.consume(interest, {
const data = await this.handler!.endpoint!.consume(interest, {
// deno-lint-ignore no-explicit-any
signal: opts.abortSignal as any,
retx: this.config.retx,
Expand Down
3 changes: 2 additions & 1 deletion src/namespace/leaf-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ export class LeafNode extends ExpressingPoint {
const payload = content instanceof Uint8Array ? content : new TextEncoder().encode(content);

// Create Data
const dataName = this.handler!.attachedPrefix!.append(...matched.name.comps);
const data = new Data(
matched.name,
dataName,
Data.ContentType(this.config.contentType ?? 0), // Default is BLOB
Data.FreshnessPeriod(opts.freshnessMs ?? this.config.freshnessMs),
payload,
Expand Down
1 change: 1 addition & 0 deletions src/namespace/nt-schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO: To be added
44 changes: 34 additions & 10 deletions src/namespace/nt-schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Endpoint } from '@ndn/endpoint';
import { Endpoint, Producer } from '@ndn/endpoint';
import { Data, Interest, Name, type Verifier } from '@ndn/packet';
// import * as namePattern from './name-pattern.ts';
import * as schemaTree from './schema-tree.ts';
Expand All @@ -14,18 +14,24 @@ export enum VerifyResult {
}

export interface NamespaceHandler {
get endpoint(): Endpoint;
get endpoint(): Endpoint | undefined;
get attachedPrefix(): Name | undefined;
getVerifier(deadline: number): Verifier;
storeData(data: Data): Promise<void>;
}

export class NtSchema implements NamespaceHandler {
export class NtSchema implements NamespaceHandler, AsyncDisposable {
public readonly tree = schemaTree.create<BaseNode>();
protected _endpoint: Endpoint | undefined;
protected _attachedPrefix: Name | undefined;
protected _producer: Producer | undefined;

get endpoint(): Endpoint {
return this._endpoint!;
get endpoint() {
return this._endpoint;
}

get attachedPrefix() {
return this._attachedPrefix;
}

public match(name: Name) {
Expand Down Expand Up @@ -65,10 +71,28 @@ export class NtSchema implements NamespaceHandler {
return undefined;
}

// TODO: schemaTree.traverse
// public async attach(prefix: Name, endpoint: Endpoint) {
// }
public async attach(prefix: Name, endpoint: Endpoint) {
this._attachedPrefix = prefix;
this._endpoint = endpoint;
await schemaTree.traverse(this.tree, {
post: async (node, path) => await node.resource?.onAttach?.emit(path, endpoint),
});

this._producer = endpoint.produce(prefix, this.onInterest.bind(this));
}

public async detach() {
this._producer!.close();
await schemaTree.traverse(this.tree, {
pre: async (node) => await node.resource?.onDetach?.emit(this.endpoint!),
});
this._endpoint = undefined;
this._attachedPrefix = undefined;
}

// public async detach() {
// }
async [Symbol.asyncDispose]() {
if (this._producer) {
await this.detach();
}
}
}
18 changes: 18 additions & 0 deletions src/namespace/schema-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,21 @@ export const create = <R>(): Node<R> => ({
upEdge: undefined,
resource: undefined,
});

export const traverse = async <R>(
root: Node<R>,
action: {
pre?: (node: Node<R>, path: namePattern.Pattern) => Promise<void>;
post?: (node: Node<R>, path: namePattern.Pattern) => Promise<void>;
},
path: namePattern.Pattern = [],
) => {
await action.pre?.(root, path);
for (const child of root.fixedChildren) {
await traverse(child.dest, action, [...path, child.edge]);
}
for (const child of root.fixedChildren) {
await traverse(child.dest, action, [...path, child.edge]);
}
await action.post?.(root, path);
};

0 comments on commit 5593f4a

Please sign in to comment.