@zhin.js/command 1.1.3 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,8 +32,8 @@ export declare class CommandIndex {
32
32
  constructor(slots: readonly Readonly<CapabilitySlot<CommandDefinition>>[], snapshot: RuntimeSnapshot, menu?: CommandMenuConfig);
33
33
  list(): readonly CommandDescriptor[];
34
34
  has(name: string): boolean;
35
- execute(name: string, args?: readonly string[]): Promise<unknown>;
36
- dispatch(input: CommandMatchInput, source?: unknown, interactionFactory?: UserInteractionFactory, commandPrefix?: string): Promise<CommandDispatchResult>;
35
+ execute(name: string, args?: readonly string[], operationSnapshot?: RuntimeSnapshot): Promise<unknown>;
36
+ dispatch(input: CommandMatchInput, source?: unknown, interactionFactory?: UserInteractionFactory, commandPrefix?: string, operationSnapshot?: RuntimeSnapshot): Promise<CommandDispatchResult>;
37
37
  }
38
38
  export declare function isCommandIndex(value: unknown): value is CommandIndex;
39
39
  export declare class CommandParameterValueError extends TypeError {
@@ -112,23 +112,23 @@ export class CommandIndex {
112
112
  throw error;
113
113
  }
114
114
  }
115
- async execute(name, args = []) {
115
+ async execute(name, args = [], operationSnapshot = this.snapshot) {
116
116
  const match = this.#match(name, true);
117
117
  if (!match) {
118
118
  this.#diagnoseParameter(name);
119
119
  throw new Error(`Unknown Command: ${name}`);
120
120
  }
121
121
  // Host / 无 session:跳过 permit;无 source 时函数默认值得到空 session。
122
- return match.command.slot.definition.execute(createCommandContext(this.snapshot, match.command.slot.owner, args, resolveDynamicParams(match.params, undefined), undefined, Object.freeze([]), undefined, match.command.slot.definition.adapter));
122
+ return match.command.slot.definition.execute(createCommandContext(operationSnapshot, match.command.slot.owner, args, resolveDynamicParams(match.params, undefined), undefined, Object.freeze([]), undefined, match.command.slot.definition.adapter));
123
123
  }
124
- async dispatch(input, source = undefined, interactionFactory, commandPrefix = '') {
124
+ async dispatch(input, source = undefined, interactionFactory, commandPrefix = '', operationSnapshot = this.snapshot) {
125
125
  if (this.#menu) {
126
126
  const menuValue = this.#dispatchMenu(input, commandPrefix);
127
127
  if (menuValue !== undefined) {
128
128
  return Object.freeze({
129
129
  matched: true,
130
130
  command: this.#menu.keyword,
131
- owner: this.snapshot.root,
131
+ owner: operationSnapshot.root,
132
132
  value: menuValue,
133
133
  });
134
134
  }
@@ -139,10 +139,10 @@ export class CommandIndex {
139
139
  if (!commandAdapterMatches(shortcut.record, source)) {
140
140
  return Object.freeze({ matched: false });
141
141
  }
142
- if (!(await this.#permitAllows(shortcut.record, source))) {
142
+ if (!(await this.#permitAllows(shortcut.record, source, operationSnapshot))) {
143
143
  return Object.freeze({ matched: false });
144
144
  }
145
- const value = await shortcut.record.slot.definition.execute(createCommandContext(this.snapshot, shortcut.record.slot.owner, Object.freeze([]), resolveDynamicParams(shortcut.params, source), source, Object.freeze([]), interaction, shortcut.record.slot.definition.adapter));
145
+ const value = await shortcut.record.slot.definition.execute(createCommandContext(operationSnapshot, shortcut.record.slot.owner, Object.freeze([]), resolveDynamicParams(shortcut.params, source), source, Object.freeze([]), interaction, shortcut.record.slot.definition.adapter));
146
146
  return Object.freeze({
147
147
  matched: true,
148
148
  command: shortcut.record.name,
@@ -156,11 +156,11 @@ export class CommandIndex {
156
156
  if (!commandAdapterMatches(match.command, source)) {
157
157
  return Object.freeze({ matched: false });
158
158
  }
159
- if (!(await this.#permitAllows(match.command, source))) {
159
+ if (!(await this.#permitAllows(match.command, source, operationSnapshot))) {
160
160
  return Object.freeze({ matched: false });
161
161
  }
162
162
  const args = textArgs(match.remaining);
163
- const value = await match.command.slot.definition.execute(createCommandContext(this.snapshot, match.command.slot.owner, args, resolveDynamicParams(match.params, source), source, match.remaining, interaction, match.command.slot.definition.adapter));
163
+ const value = await match.command.slot.definition.execute(createCommandContext(operationSnapshot, match.command.slot.owner, args, resolveDynamicParams(match.params, source), source, match.remaining, interaction, match.command.slot.definition.adapter));
164
164
  return Object.freeze({
165
165
  matched: true,
166
166
  command: match.command.name,
@@ -168,21 +168,21 @@ export class CommandIndex {
168
168
  value,
169
169
  });
170
170
  }
171
- async #permitAllows(record, source) {
171
+ async #permitAllows(record, source, operationSnapshot) {
172
172
  const permits = record.permit;
173
173
  if (!permits || permits.length === 0)
174
174
  return true;
175
175
  if (!hasImSession(source))
176
176
  return true;
177
- const host = this.#resolveHost();
177
+ const host = this.#resolveHost(operationSnapshot);
178
178
  if (!host)
179
179
  return false;
180
180
  const subject = toPermissionSubject(resolveCommandSession(source));
181
181
  return host.checkAll(permits, subject);
182
182
  }
183
- #resolveHost() {
183
+ #resolveHost(operationSnapshot) {
184
184
  try {
185
- const resources = this.snapshot.resources.get(this.snapshot.root);
185
+ const resources = operationSnapshot.resources.get(operationSnapshot.root);
186
186
  if (!resources)
187
187
  return undefined;
188
188
  const host = resources.get(permissionHostToken.id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhin.js/command",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "Convention-based Command Feature for Zhin Plugin Runtime",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -210,7 +210,7 @@ export class CommandIndex {
210
210
  }
211
211
  }
212
212
 
213
- async execute(name: string, args: readonly string[] = []): Promise<unknown> {
213
+ async execute(name: string, args: readonly string[] = [], operationSnapshot: RuntimeSnapshot = this.snapshot): Promise<unknown> {
214
214
  const match = this.#match(name, true);
215
215
  if (!match) {
216
216
  this.#diagnoseParameter(name);
@@ -219,7 +219,7 @@ export class CommandIndex {
219
219
  // Host / 无 session:跳过 permit;无 source 时函数默认值得到空 session。
220
220
  return match.command.slot.definition.execute(
221
221
  createCommandContext(
222
- this.snapshot,
222
+ operationSnapshot,
223
223
  match.command.slot.owner,
224
224
  args,
225
225
  resolveDynamicParams(match.params, undefined),
@@ -236,6 +236,7 @@ export class CommandIndex {
236
236
  source: unknown = undefined,
237
237
  interactionFactory?: UserInteractionFactory,
238
238
  commandPrefix = '',
239
+ operationSnapshot: RuntimeSnapshot = this.snapshot,
239
240
  ): Promise<CommandDispatchResult> {
240
241
  if (this.#menu) {
241
242
  const menuValue = this.#dispatchMenu(input, commandPrefix);
@@ -243,7 +244,7 @@ export class CommandIndex {
243
244
  return Object.freeze({
244
245
  matched: true,
245
246
  command: this.#menu.keyword,
246
- owner: this.snapshot.root,
247
+ owner: operationSnapshot.root,
247
248
  value: menuValue,
248
249
  });
249
250
  }
@@ -254,12 +255,12 @@ export class CommandIndex {
254
255
  if (!commandAdapterMatches(shortcut.record, source)) {
255
256
  return Object.freeze({ matched: false });
256
257
  }
257
- if (!(await this.#permitAllows(shortcut.record, source))) {
258
+ if (!(await this.#permitAllows(shortcut.record, source, operationSnapshot))) {
258
259
  return Object.freeze({ matched: false });
259
260
  }
260
261
  const value = await shortcut.record.slot.definition.execute(
261
262
  createCommandContext(
262
- this.snapshot,
263
+ operationSnapshot,
263
264
  shortcut.record.slot.owner,
264
265
  Object.freeze([]),
265
266
  resolveDynamicParams(shortcut.params, source),
@@ -282,13 +283,13 @@ export class CommandIndex {
282
283
  if (!commandAdapterMatches(match.command, source)) {
283
284
  return Object.freeze({ matched: false });
284
285
  }
285
- if (!(await this.#permitAllows(match.command, source))) {
286
+ if (!(await this.#permitAllows(match.command, source, operationSnapshot))) {
286
287
  return Object.freeze({ matched: false });
287
288
  }
288
289
  const args = textArgs(match.remaining);
289
290
  const value = await match.command.slot.definition.execute(
290
291
  createCommandContext(
291
- this.snapshot,
292
+ operationSnapshot,
292
293
  match.command.slot.owner,
293
294
  args,
294
295
  resolveDynamicParams(match.params, source),
@@ -306,19 +307,19 @@ export class CommandIndex {
306
307
  });
307
308
  }
308
309
 
309
- async #permitAllows(record: CommandRecord, source: unknown): Promise<boolean> {
310
+ async #permitAllows(record: CommandRecord, source: unknown, operationSnapshot: RuntimeSnapshot): Promise<boolean> {
310
311
  const permits = record.permit;
311
312
  if (!permits || permits.length === 0) return true;
312
313
  if (!hasImSession(source)) return true;
313
- const host = this.#resolveHost();
314
+ const host = this.#resolveHost(operationSnapshot);
314
315
  if (!host) return false;
315
316
  const subject = toPermissionSubject(resolveCommandSession(source));
316
317
  return host.checkAll(permits, subject);
317
318
  }
318
319
 
319
- #resolveHost(): PermissionHost | undefined {
320
+ #resolveHost(operationSnapshot: RuntimeSnapshot): PermissionHost | undefined {
320
321
  try {
321
- const resources = this.snapshot.resources.get(this.snapshot.root);
322
+ const resources = operationSnapshot.resources.get(operationSnapshot.root);
322
323
  if (!resources) return undefined;
323
324
  const host = resources.get(permissionHostToken.id);
324
325
  return host && typeof (host as PermissionHost).check === 'function'