From 5593f4ae0c0c410e00f5adf2ced3fdb913e2a0ac Mon Sep 17 00:00:00 2001 From: Xinyu Ma Date: Thu, 8 Feb 2024 22:22:02 -0800 Subject: [PATCH] Update GitHub Action; Make NTSchema prototype --- .github/workflows/lint-code.yaml | 3 ++- .github/workflows/publish.yaml | 3 ++- src/namespace/base-node.ts | 4 +-- src/namespace/expressing-point.ts | 5 ++-- src/namespace/leaf-node.ts | 3 ++- src/namespace/nt-schema.test.ts | 1 + src/namespace/nt-schema.ts | 44 ++++++++++++++++++++++++------- src/namespace/schema-tree.ts | 18 +++++++++++++ 8 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 src/namespace/nt-schema.test.ts diff --git a/.github/workflows/lint-code.yaml b/.github/workflows/lint-code.yaml index 754a2d0..cfd8bd2 100644 --- a/.github/workflows/lint-code.yaml +++ b/.github/workflows/lint-code.yaml @@ -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: diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 86f8786..1513199 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -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: diff --git a/src/namespace/base-node.ts b/src/namespace/base-node.ts index 18542cc..5800af4 100644 --- a/src/namespace/base-node.ts +++ b/src/namespace/base-node.ts @@ -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 } diff --git a/src/namespace/expressing-point.ts b/src/namespace/expressing-point.ts index 46be9d2..d6e8939 100644 --- a/src/namespace/expressing-point.ts +++ b/src/namespace/expressing-point.ts @@ -130,7 +130,8 @@ export class ExpressingPoint extends BaseNode { } = {}, ): Promise { // Construct Interest, but without signing, so the parameter digest is not there - const interestArgs = [matched.name] as Array; + const interestName = this.handler!.attachedPrefix!.append(...matched.name.comps); + const interestArgs = [interestName] as Array; 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); @@ -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, diff --git a/src/namespace/leaf-node.ts b/src/namespace/leaf-node.ts index 7674b83..d5529ed 100644 --- a/src/namespace/leaf-node.ts +++ b/src/namespace/leaf-node.ts @@ -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, diff --git a/src/namespace/nt-schema.test.ts b/src/namespace/nt-schema.test.ts new file mode 100644 index 0000000..2285c07 --- /dev/null +++ b/src/namespace/nt-schema.test.ts @@ -0,0 +1 @@ +// TODO: To be added diff --git a/src/namespace/nt-schema.ts b/src/namespace/nt-schema.ts index c1cd528..fc18fe0 100644 --- a/src/namespace/nt-schema.ts +++ b/src/namespace/nt-schema.ts @@ -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'; @@ -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; } -export class NtSchema implements NamespaceHandler { +export class NtSchema implements NamespaceHandler, AsyncDisposable { public readonly tree = schemaTree.create(); 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) { @@ -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(); + } + } } diff --git a/src/namespace/schema-tree.ts b/src/namespace/schema-tree.ts index 3c234e3..c37b6f7 100644 --- a/src/namespace/schema-tree.ts +++ b/src/namespace/schema-tree.ts @@ -206,3 +206,21 @@ export const create = (): Node => ({ upEdge: undefined, resource: undefined, }); + +export const traverse = async ( + root: Node, + action: { + pre?: (node: Node, path: namePattern.Pattern) => Promise; + post?: (node: Node, path: namePattern.Pattern) => Promise; + }, + 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); +};