Skip to content

ctx.identityのアクセス可否を型レベルで保証する - #14

Merged
kaito02020424 merged 6 commits into
mainfrom
chore/identity-type
Sep 29, 2026
Merged

kaito02020424 merged 6 commits into
mainfrom
chore/identity-type

Conversation

@kaito02020424

@kaito02020424 kaito02020424 commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

概要

ctx.identityについて, 型レベルでverifyClientTokenオプションの有無によりアクセス可能性を制御します。

  • verifyClientTokenオプションが設定されている場合, ctx.identity`もctx.untrustedIdentity`もアクセス可能です。
  • verifyClientTokenオプションが設定されていない場合, ctx.identity`はアクセス不可能ですがctx.untrustedIdentity`はアクセス可能です。
  • 推論がうまく行かなかった場合, 従来通りidentity: NetherNetIdentity | undefinedと推論され, 実装側で型の絞り込みを要求します。

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The fallback public typing is incorrect, and several documentation and type-coverage issues remain unresolved.

Review effort: Lite
Findings: 3 Low severity

Open (3)
What changed in this PR

Adds conditional TypeScript typing for ctx.identity based on verifyClientToken.

Changes:

  • Adds conditional identity types.
  • Propagates verification state through gateway contexts and events.
  • Updates tests and documentation.
File Summary and findings
src/​types.ts Adds conditional identity types. Moderate (1 vote): the boolean fallback should preserve a required identity property with an undefined union.
src/​gateway.ts Propagates verifier state through gateway generics. Nit (2 votes): add compile-time assertions for all IsVerified branches and consumers.
src/​gateway.test.ts Tests inaccessible identity without verification. Nit (1 vote): add assertions for verified and uncertain verifier cases.
README.md Documents the new typing behavior. Nits: fix the invalid unverified example (2 votes), preserve verified context typing for rateLimit (2 votes), and add missing imports (1 vote).

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread README.md
`c.identity` is only available after successful verification. Set `requireClientIdentity: true` if
every join request must have a verified identity, otherwise requests without one skip this XUID rule:
XUID instead, use a key function on a gateway with `verifyClientToken`. Every join that reaches it
has a verified `c.identity`; on a gateway without a verifier, reading it is a type error:
Comment thread README.md
rateLimit({
windowMs: 60_000,
rules: [rateLimit.custom<JoinContext>(2, (c) => c.identity?.xuid)],
rules: [rateLimit.custom(2, (c) => c.identity.xuid)],
Comment thread src/gateway.ts
use(middleware: GatewayMiddleware): this;
use(selector: 'info', middleware: GatewayMiddleware<ServerInfoContext>): this;
use(selector: 'join', middleware: GatewayMiddleware<JoinContext>): this;
use(selector: 'join', middleware: GatewayMiddleware<JoinContext<IsVerified<Options>>>): this;
Comment thread src/gateway.ts Outdated
verifyClientToken: VerifyClientToken;
}
? true
: Options extends {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Options extends Omit<NetherNetGatewayOptions, 'verifyClientToken'> & {
        verifyClientToken?: undefined;
      }

にした方が今後NetherNetGatewayOptionsに何か追加したときにここも書き換える必要がなくなりそう?

Comment thread src/gateway.ts Outdated
constructor(options: NetherNetGatewayOptions) {
super();
this.options = { ...options };
this.options = Object.freeze({

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここは手動で列挙せずにスプレッドさせたオブジェクトをfreezeではだめなんですかね?

this.options = Object.freeze({ ...options });

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

Two moderate issues remain in the fallback type and README example.

Review effort: Lite
Findings: 3 Low severity

Open (3)

@kaito02020424
kaito02020424 merged commit bfa734e into main Sep 29, 2026
1 check failed
@kaito02020424
kaito02020424 deleted the chore/identity-type branch September 29, 2026 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants