@zthun/romulator-api 1.7.1 → 1.8.0

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.
@@ -71,6 +71,16 @@ export interface IZRomulatorFilesService {
71
71
  * The node with the given path or null if no such file exists.
72
72
  */
73
73
  info(path: string): Promise<IZFileSystemNode | null>;
74
+ /**
75
+ * Gets the contents of the file.
76
+ *
77
+ * @param info -
78
+ * The file to read and store.
79
+ *
80
+ * @returns
81
+ * The buffer of data that the file contained.
82
+ */
83
+ read(info: IZFileSystemNode): Promise<Buffer>;
74
84
  /**
75
85
  * Initializes the file repository.
76
86
  */
@@ -87,6 +97,7 @@ export declare class ZRomulatorFilesService implements IZRomulatorFilesService {
87
97
  private static readonly InfoFolderName;
88
98
  private _repository;
89
99
  private _folderStream;
100
+ private _fileStream;
90
101
  private _globs;
91
102
  private _systems;
92
103
  constructor(_configs: IZRomulatorConfigsService, _fileSystem: IZFileSystemService);
@@ -103,4 +114,7 @@ export declare class ZRomulatorFilesService implements IZRomulatorFilesService {
103
114
  systems(path: string): Promise<IZFileSystemNode | null>;
104
115
  info(): Promise<IZFileSystemNode[]>;
105
116
  info(path: string): Promise<IZFileSystemNode | null>;
117
+ games(): Promise<IZFileSystemNode[]>;
118
+ games(path: string): Promise<IZFileSystemNode | null>;
119
+ read(node: IZFileSystemNode): Promise<Buffer>;
106
120
  }
@@ -1,5 +1,5 @@
1
1
  import { IZFileSystemService } from '@zthun/crumbtrail-fs';
2
- import { IZDataRequest, IZPage } from '@zthun/helpful-query';
2
+ import { IZDataMatch, IZDataRequest, IZPage } from '@zthun/helpful-query';
3
3
  import { IZLogger } from '@zthun/lumberjacky-log';
4
4
  import { IZRomulatorGame } from '@zthun/romulator-client';
5
5
  import { IZRestfulGet } from '@zthun/webigail-rest';
@@ -8,11 +8,12 @@ export declare const ZRomulatorGamesToken: unique symbol;
8
8
  export interface IZRomulatorGamesService extends IZRestfulGet<IZRomulatorGame> {
9
9
  list(req: IZDataRequest): Promise<IZPage<IZRomulatorGame>>;
10
10
  }
11
- export declare class ZRomulatorGamesService implements IZRomulatorGamesService {
11
+ export declare class ZRomulatorGamesService implements IZRomulatorGamesService, IZDataMatch<IZRomulatorGame, string> {
12
12
  private readonly _file;
13
13
  private readonly _config;
14
14
  private readonly _logger;
15
15
  constructor(_file: IZFileSystemService, _config: IZRomulatorConfigsService, logger: IZLogger);
16
16
  list(req: IZDataRequest): Promise<IZPage<IZRomulatorGame>>;
17
17
  get(id: string): Promise<IZRomulatorGame>;
18
+ match(data: IZRomulatorGame, filter: string): boolean;
18
19
  }
package/dist/main.cjs CHANGED
@@ -322,7 +322,7 @@ const KnownSystem = "@zthunworks/romulator/known-system";
322
322
  * A {@link ZRomulatorSystemBuilder} instance that has
323
323
  * built the nes.
324
324
  */ static nes() {
325
- return new romulatorClient.ZRomulatorSystemBuilder().id(romulatorClient.ZRomulatorSystemId.Nintendo).console().name("Nintendo Entertainment System").generation(3).build();
325
+ return new romulatorClient.ZRomulatorSystemBuilder().id(romulatorClient.ZRomulatorSystemId.Nintendo).name("Nintendo Entertainment System").build();
326
326
  }
327
327
  /**
328
328
  * Creates a system that represents the Super
@@ -331,7 +331,7 @@ const KnownSystem = "@zthunworks/romulator/known-system";
331
331
  * @returns
332
332
  * This instance.
333
333
  */ static snes() {
334
- return new romulatorClient.ZRomulatorSystemBuilder().id(romulatorClient.ZRomulatorSystemId.SuperNintendo).console().name("Super Nintendo Entertainment System").generation(4).build();
334
+ return new romulatorClient.ZRomulatorSystemBuilder().id(romulatorClient.ZRomulatorSystemId.SuperNintendo).name("Super Nintendo Entertainment System").build();
335
335
  }
336
336
  /**
337
337
  * Creates a system that represents the Nintendo
@@ -340,7 +340,7 @@ const KnownSystem = "@zthunworks/romulator/known-system";
340
340
  * @returns
341
341
  * This instance.
342
342
  */ static n64() {
343
- return new romulatorClient.ZRomulatorSystemBuilder().id(romulatorClient.ZRomulatorSystemId.Nintendo64).console().name("Nintendo 64").generation(5).build();
343
+ return new romulatorClient.ZRomulatorSystemBuilder().id(romulatorClient.ZRomulatorSystemId.Nintendo64).name("Nintendo 64").build();
344
344
  }
345
345
  /**
346
346
  * Creates a system that represents the Nintendo
@@ -349,7 +349,7 @@ const KnownSystem = "@zthunworks/romulator/known-system";
349
349
  * @returns
350
350
  * This instance.
351
351
  */ static gc() {
352
- return new romulatorClient.ZRomulatorSystemBuilder().id(romulatorClient.ZRomulatorSystemId.GameCube).console().name("Nintendo GameCube").generation(6).build();
352
+ return new romulatorClient.ZRomulatorSystemBuilder().id(romulatorClient.ZRomulatorSystemId.GameCube).name("Nintendo GameCube").build();
353
353
  }
354
354
  /**
355
355
  * Creates a system that represents the Nintendo
@@ -358,7 +358,7 @@ const KnownSystem = "@zthunworks/romulator/known-system";
358
358
  * @returns
359
359
  * This instance.
360
360
  */ static wii() {
361
- return new romulatorClient.ZRomulatorSystemBuilder().id(romulatorClient.ZRomulatorSystemId.Wii).console().name("Nintendo Wii").generation(7).build();
361
+ return new romulatorClient.ZRomulatorSystemBuilder().id(romulatorClient.ZRomulatorSystemId.Wii).name("Nintendo Wii").build();
362
362
  }
363
363
  /**
364
364
  * Creates a system that represents the Nintendo
@@ -367,7 +367,7 @@ const KnownSystem = "@zthunworks/romulator/known-system";
367
367
  * @returns
368
368
  * This instance.
369
369
  */ static wiiu() {
370
- return new romulatorClient.ZRomulatorSystemBuilder().id(romulatorClient.ZRomulatorSystemId.WiiU).console().name("Nintendo Wii U").generation(8).build();
370
+ return new romulatorClient.ZRomulatorSystemBuilder().id(romulatorClient.ZRomulatorSystemId.WiiU).name("Nintendo Wii U").build();
371
371
  }
372
372
  /**
373
373
  * Creates a system that represents the Nintendo
@@ -376,7 +376,7 @@ const KnownSystem = "@zthunworks/romulator/known-system";
376
376
  * @returns
377
377
  * This instance.
378
378
  */ static switch() {
379
- return new romulatorClient.ZRomulatorSystemBuilder().id(romulatorClient.ZRomulatorSystemId.Switch).console().name("Nintendo Switch").generation(9).build();
379
+ return new romulatorClient.ZRomulatorSystemBuilder().id(romulatorClient.ZRomulatorSystemId.Switch).name("Nintendo Switch").build();
380
380
  }
381
381
  /**
382
382
  * Creates a system from an id slug.
@@ -436,26 +436,6 @@ _ts_decorate$c([
436
436
  _ts_metadata$8("design:returntype", void 0)
437
437
  ], ZRomulatorSystemKnown, "switch", null);
438
438
 
439
- /**
440
- * A data match function which matches a string (search) filter
441
- * to a game's name, system slug, or system name.
442
- */ class ZRomulatorDataMatchGame {
443
- match(data, filter) {
444
- const needle = filter?.trim().toLowerCase();
445
- const { name = "", system = "" } = data;
446
- const target = ZRomulatorSystemKnown.from(system);
447
- const systemName = helpfulFn.firstTruthy("", target?.name);
448
- if (!needle?.length) {
449
- return true;
450
- }
451
- return [
452
- name,
453
- system,
454
- systemName
455
- ].filter((s)=>s.length).some((k)=>k.toLowerCase().includes(needle));
456
- }
457
- }
458
-
459
439
  function _ts_decorate$b(decorators, target, key, desc) {
460
440
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
461
441
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -504,7 +484,7 @@ class ZRomulatorGamesService {
504
484
  const id = `${dir}-${lodashEs.kebabCase(file)}`;
505
485
  return new romulatorClient.ZRomulatorGameBuilder().id(id).system(dir).file(path).name(file).build();
506
486
  });
507
- const options = new helpfulQuery.ZDataSourceStaticOptionsBuilder().search(new ZRomulatorDataMatchGame()).build();
487
+ const options = new helpfulQuery.ZDataSourceStaticOptionsBuilder().search(this).build();
508
488
  const source = new helpfulQuery.ZDataSourceStatic(games, options);
509
489
  const $sort = new helpfulQuery.ZSortBuilder().sorts(helpfulFn.firstDefined([], req.sort)).ascending("system").ascending("name").build();
510
490
  const $request = new helpfulQuery.ZDataRequestBuilder().copy(req).sort($sort).build();
@@ -520,6 +500,20 @@ class ZRomulatorGamesService {
520
500
  }
521
501
  return match;
522
502
  }
503
+ match(data, filter) {
504
+ const needle = filter?.trim().toLowerCase();
505
+ const { name = "", system = "" } = data;
506
+ const target = ZRomulatorSystemKnown.from(system);
507
+ const systemName = helpfulFn.firstTruthy("", target?.name);
508
+ if (!needle?.length) {
509
+ return true;
510
+ }
511
+ return [
512
+ name,
513
+ system,
514
+ systemName
515
+ ].filter((s)=>s.length).some((k)=>k.toLowerCase().includes(needle));
516
+ }
523
517
  }
524
518
  ZRomulatorGamesService = _ts_decorate$b([
525
519
  common.Injectable(),
@@ -636,6 +630,7 @@ class ZRomulatorFilesService {
636
630
  static InfoFolderName = ".info";
637
631
  _repository = new crumbtrailFs.ZFileRepository();
638
632
  _folderStream = new crumbtrailFs.ZStreamFolder();
633
+ _fileStream = new crumbtrailFs.ZStreamFile();
639
634
  _globs;
640
635
  _systems;
641
636
  constructor(_configs, _fileSystem){
@@ -664,14 +659,17 @@ class ZRomulatorFilesService {
664
659
  const gamesFolder = await this.gamesFolder();
665
660
  return node_path.resolve(gamesFolder, ZRomulatorFilesService.InfoFolderName);
666
661
  }
667
- async contents(root, path) {
662
+ async contents(roots, path) {
668
663
  const repository = await this.seed();
669
- const prefix = lodashEs.trimEnd(root, "/");
670
- const folder = `${prefix}${node_path.sep}`;
671
- let filter = new helpfulQuery.ZFilterBinaryBuilder().subject("path");
672
- filter = path == null ? filter.startsWith().value(folder) : filter.equal().value(node_path.resolve(folder, path));
664
+ const folders = lodashEs.castArray(roots).map((root)=>`${lodashEs.trimEnd(root, "/")}/`);
665
+ const byPaths = folders.map((folder)=>{
666
+ const byPath = new helpfulQuery.ZFilterBinaryBuilder().subject("path");
667
+ const pathFilter = path == null ? byPath.startsWith().value(folder) : byPath.equal().value(node_path.resolve(folder, path));
668
+ return pathFilter.build();
669
+ });
670
+ const filter = byPaths.length > 1 ? new helpfulQuery.ZFilterLogicBuilder().or().clauses(byPaths).build() : lodashEs.first(byPaths);
673
671
  const sort = new helpfulQuery.ZSortBuilder().ascending("path").build();
674
- const request = new helpfulQuery.ZDataRequestBuilder().filter(filter.build()).sort(sort).build();
672
+ const request = new helpfulQuery.ZDataRequestBuilder().filter(filter).sort(sort).build();
675
673
  const nodes = await repository.retrieve(request);
676
674
  return path == null ? nodes : helpfulFn.firstDefined(null, lodashEs.first(nodes));
677
675
  }
@@ -703,13 +701,21 @@ class ZRomulatorFilesService {
703
701
  const games = await this.gamesFolder();
704
702
  const folders = path == null ? this._systems.map((s)=>`${s}/`) : node_path.resolve(games, path);
705
703
  const items = await this._fileSystem.search(folders, {
706
- cwd: games
704
+ cwd: games,
705
+ stat: false
707
706
  });
708
707
  return path == null ? items : helpfulFn.firstDefined(null, lodashEs.first(items));
709
708
  }
710
709
  async info(path) {
711
710
  return this.contents(await this.infoFolder(), path);
712
711
  }
712
+ async games(path) {
713
+ const root = await this.gamesFolder();
714
+ return this.contents(this._systems.map((s)=>node_path.resolve(root, s)), path);
715
+ }
716
+ read(node) {
717
+ return this._fileStream.read(node.path);
718
+ }
713
719
  }
714
720
  ZRomulatorFilesService = _ts_decorate$8([
715
721
  common.Injectable(),
@@ -1066,11 +1072,6 @@ class ZRomulatorSystemsService {
1066
1072
  _files;
1067
1073
  logger;
1068
1074
  _logger;
1069
- _fileStream = new crumbtrailFs.ZStreamFile({
1070
- cache: {
1071
- maxFiles: Object.keys(romulatorClient.ZRomulatorSystemId).length + 1
1072
- }
1073
- });
1074
1075
  constructor(_files, logger){
1075
1076
  this._files = _files;
1076
1077
  this.logger = logger;
@@ -1117,7 +1118,7 @@ class ZRomulatorSystemsService {
1117
1118
  return system.build();
1118
1119
  }
1119
1120
  try {
1120
- const contents = await this._fileStream.read(info.path);
1121
+ const contents = await this._files.read(info);
1121
1122
  const json = JSON.parse(contents.toString());
1122
1123
  return system.assign(json).redact().build();
1123
1124
  } catch (e) {