@platforma-sdk/block-tools 2.1.11 → 2.2.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.
Files changed (59) hide show
  1. package/dist/cli.js +1 -1
  2. package/dist/cli.js.map +1 -1
  3. package/dist/cli.mjs +266 -80
  4. package/dist/cli.mjs.map +1 -1
  5. package/dist/cmd/index.d.ts +2 -0
  6. package/dist/cmd/index.d.ts.map +1 -1
  7. package/dist/cmd/publish.d.ts +11 -0
  8. package/dist/cmd/publish.d.ts.map +1 -0
  9. package/dist/config-DJqN5LSx.js +3 -0
  10. package/dist/config-DJqN5LSx.js.map +1 -0
  11. package/dist/{config-Br5DwFuM.mjs → config-rGaQLD-7.mjs} +636 -603
  12. package/dist/config-rGaQLD-7.mjs.map +1 -0
  13. package/dist/index.js +1 -1
  14. package/dist/index.mjs +29 -27
  15. package/dist/registry_v1/config_schema.d.ts +13 -13
  16. package/dist/v2/build_dist.d.ts +2 -1
  17. package/dist/v2/build_dist.d.ts.map +1 -1
  18. package/dist/v2/model/block_components.d.ts +169 -177
  19. package/dist/v2/model/block_components.d.ts.map +1 -1
  20. package/dist/v2/model/block_description.d.ts +2873 -0
  21. package/dist/v2/model/block_description.d.ts.map +1 -0
  22. package/dist/v2/model/{meta.d.ts → block_meta.d.ts} +1 -1
  23. package/dist/v2/model/block_meta.d.ts.map +1 -0
  24. package/dist/v2/model/content_conversion.d.ts +5 -2
  25. package/dist/v2/model/content_conversion.d.ts.map +1 -1
  26. package/dist/v2/model/index.d.ts +3 -2079
  27. package/dist/v2/model/index.d.ts.map +1 -1
  28. package/dist/v2/registry/registry.d.ts +16 -0
  29. package/dist/v2/registry/registry.d.ts.map +1 -0
  30. package/dist/v2/registry/schema_internal.d.ts +7 -0
  31. package/dist/v2/registry/schema_internal.d.ts.map +1 -0
  32. package/dist/v2/registry/schema_public.d.ts +2828 -0
  33. package/dist/v2/registry/schema_public.d.ts.map +1 -0
  34. package/package.json +3 -3
  35. package/src/cmd/build-meta.ts +1 -1
  36. package/src/cmd/index.ts +2 -0
  37. package/src/cmd/publish.ts +69 -0
  38. package/src/v2/build_dist.ts +16 -12
  39. package/src/v2/model/block_components.ts +14 -10
  40. package/src/v2/model/block_description.ts +49 -0
  41. package/src/v2/model/{meta.ts → block_meta.ts} +1 -1
  42. package/src/v2/model/content_conversion.ts +49 -10
  43. package/src/v2/model/index.ts +3 -44
  44. package/src/v2/registry/registry.ts +233 -0
  45. package/src/v2/registry/schema_internal.ts +13 -0
  46. package/src/v2/registry/schema_public.ts +90 -0
  47. package/dist/config-Br5DwFuM.mjs.map +0 -1
  48. package/dist/config-DMAm68Pm.js +0 -3
  49. package/dist/config-DMAm68Pm.js.map +0 -1
  50. package/dist/v2/model/common.d.ts +0 -3
  51. package/dist/v2/model/common.d.ts.map +0 -1
  52. package/dist/v2/model/content_types.d.ts +0 -478
  53. package/dist/v2/model/content_types.d.ts.map +0 -1
  54. package/dist/v2/model/meta.d.ts.map +0 -1
  55. package/dist/v2/registry/schema.d.ts +0 -15
  56. package/dist/v2/registry/schema.d.ts.map +0 -1
  57. package/src/v2/model/common.ts +0 -2
  58. package/src/v2/model/content_types.ts +0 -233
  59. package/src/v2/registry/schema.ts +0 -29
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema_public.d.ts","sourceRoot":"","sources":["../../../src/v2/registry/schema_public.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,WAAW,EACX,oBAAoB,EAKrB,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,qBAAqB,EAAmD,MAAM,UAAU,CAAC;AAIlG,eAAO,MAAM,gBAAgB,kBAAkB,CAAC;AAEhD,wBAAgB,oBAAoB,CAAC,EAAE,EAAE,WAAW,GAAG,MAAM,CAE5D;AAED,eAAO,MAAM,cAAc,QAAyB,CAAC;AAMrD,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAUjF;AACD,MAAM,MAAM,kCAAkC,GAAG,CAAC,CAAC,KAAK,CACtD,UAAU,CAAC,OAAO,oCAAoC,CAAC,CACxD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platforma-sdk/block-tools",
3
- "version": "2.1.11",
3
+ "version": "2.2.0",
4
4
  "description": "Utility to manipulate Platforma Blocks and Block Registry",
5
5
  "types": "./dist/lib.d.ts",
6
6
  "main": "./dist/index.js",
@@ -29,8 +29,8 @@
29
29
  "tar": "^7.4.3",
30
30
  "yaml": "^2.5.1",
31
31
  "zod": "^3.23.8",
32
- "@milaboratories/resolve-helper": "1.0.1",
33
- "@milaboratories/pl-model-middle-layer": "^1.2.20",
32
+ "@milaboratories/resolve-helper": "^1.0.1",
33
+ "@milaboratories/pl-model-middle-layer": "^1.3.0",
34
34
  "@milaboratories/ts-helpers": "^1.0.26",
35
35
  "@milaboratories/ts-helpers-oclif": "^1.1.4"
36
36
  },
@@ -2,7 +2,7 @@ import { Command, Flags } from '@oclif/core';
2
2
  import path from 'path';
3
3
  import fs from 'fs';
4
4
  import { loadPackDescriptionRaw } from '../v2';
5
- import { BlockPackMetaDescription, BlockPackMetaEmbed } from '../v2/model/meta';
5
+ import { BlockPackMetaDescription, BlockPackMetaEmbed } from '../v2/model/block_meta';
6
6
 
7
7
  export default class BuildMeta extends Command {
8
8
  static override description =
package/src/cmd/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import BuildMeta from './build-meta';
2
2
  import BuildModel from './build-model';
3
3
  import PackBlock from './pack-block';
4
+ import Publish from './publish';
4
5
  import UploadPackageV1 from './upload-package-v1';
5
6
 
6
7
  // prettier-ignore
@@ -9,4 +10,5 @@ export const COMMANDS = {
9
10
  'pack': PackBlock,
10
11
  'build-model': BuildModel,
11
12
  'build-meta': BuildMeta,
13
+ 'publish': Publish,
12
14
  };
@@ -0,0 +1,69 @@
1
+ import { Command, Flags } from '@oclif/core';
2
+ import fs from 'node:fs';
3
+ import { OclifLoggerAdapter } from '@milaboratories/ts-helpers-oclif';
4
+ import { ManifestFileName } from '../v2/registry/schema_public';
5
+ import { BlockPackManifest } from '@milaboratories/pl-model-middle-layer';
6
+ import { storageByUrl } from '../lib/storage';
7
+ import { BlockRegistryV2 } from '../v2/registry/registry';
8
+ import path from 'node:path';
9
+
10
+ export default class Publish extends Command {
11
+ static description =
12
+ 'Publishes the block package and refreshes the registry (for v2 block-pack schema)';
13
+
14
+ static flags = {
15
+ registry: Flags.string({
16
+ char: 'r',
17
+ summary: 'full address of the registry',
18
+ helpValue: '<address>',
19
+ env: 'PL_REGISTRY',
20
+ required: true
21
+ }),
22
+
23
+ manifest: Flags.file({
24
+ char: 'm',
25
+ summary: 'manifest file path',
26
+ exists: true,
27
+ default: `./block-pack/${ManifestFileName}`
28
+ }),
29
+
30
+ refresh: Flags.boolean({
31
+ summary: 'refresh repository after adding the package',
32
+ default: true,
33
+ allowNo: true,
34
+ env: 'PL_REGISTRY_REFRESH'
35
+ })
36
+ };
37
+
38
+ public async run(): Promise<void> {
39
+ const { flags } = await this.parse(Publish);
40
+
41
+ // if (flags.meta) {
42
+ // if (flags.meta.endsWith('.json'))
43
+ // configFromFlags.meta = JSON.parse(
44
+ // await fs.promises.readFile(flags.meta, { encoding: 'utf-8' })
45
+ // );
46
+ // else if (flags.meta.endsWith('.yaml'))
47
+ // configFromFlags.meta = YAML.parse(
48
+ // await fs.promises.readFile(flags.meta, { encoding: 'utf-8' })
49
+ // );
50
+ // }
51
+
52
+ const manifestPath = path.resolve(flags.manifest);
53
+ const manifest = BlockPackManifest.parse(
54
+ JSON.parse(await fs.promises.readFile(manifestPath, { encoding: 'utf-8' }))
55
+ );
56
+ const manifestRoot = path.dirname(manifestPath);
57
+
58
+ this.log(`Manifest root = ${manifestRoot}`);
59
+
60
+ const storage = storageByUrl(flags.registry);
61
+ const registry = new BlockRegistryV2(storage, new OclifLoggerAdapter(this));
62
+
63
+ await registry.publishPackage(manifest, async (file) =>
64
+ Buffer.from(await fs.promises.readFile(path.resolve(manifestRoot, file)))
65
+ );
66
+
67
+ if (flags.refresh) await registry.updateIfNeeded();
68
+ }
69
+ }
@@ -1,13 +1,11 @@
1
1
  import {
2
- BlockPackDescriptionAbsolute,
3
- BlockPackDescriptionConsolidateToFolder,
4
2
  BlockPackDescriptionManifest,
5
3
  BlockPackManifest,
6
- BlockPackManifestFile
7
- } from './model';
4
+ BlockPackManifestFile,
5
+ ManifestFileInfo
6
+ } from '@milaboratories/pl-model-middle-layer';
7
+ import { BlockPackDescriptionAbsolute, BlockPackDescriptionConsolidateToFolder } from './model';
8
8
  import fsp from 'node:fs/promises';
9
- import { BlockPackMetaConsolidate, BlockPackMetaDescription } from './model/meta';
10
- import { patch } from 'semver';
11
9
  import path from 'node:path';
12
10
 
13
11
  export async function buildBlockPackDist(
@@ -16,14 +14,20 @@ export async function buildBlockPackDist(
16
14
  ): Promise<BlockPackManifest> {
17
15
  await fsp.mkdir(dst, { recursive: true });
18
16
  const files: string[] = [];
19
- const descriptionRelative = await BlockPackDescriptionConsolidateToFolder(dst, files).parseAsync(
20
- description
17
+ const descriptionRelative: BlockPackDescriptionManifest =
18
+ await BlockPackDescriptionConsolidateToFolder(dst, files).parseAsync(description);
19
+ const filesForManifest = await Promise.all(
20
+ files.map(async (f): Promise<ManifestFileInfo> => {
21
+ const bytes = await fsp.readFile(path.resolve(dst, f));
22
+ const sha256 = Buffer.from(await crypto.subtle.digest('sha-256', bytes)).toString('hex');
23
+ return { name: f, size: bytes.length, sha256 };
24
+ })
21
25
  );
22
26
  const manifest: BlockPackManifest = BlockPackManifest.parse({
23
- schema: 'v1',
24
- ...descriptionRelative,
25
- files
26
- });
27
+ schema: 'v2',
28
+ description: descriptionRelative,
29
+ files: filesForManifest
30
+ } satisfies BlockPackManifest);
27
31
  await fsp.writeFile(path.resolve(dst, BlockPackManifestFile), JSON.stringify(manifest));
28
32
  return manifest;
29
33
  }
@@ -3,10 +3,14 @@ import {
3
3
  ResolvedModuleFile,
4
4
  ResolvedModuleFolder,
5
5
  packFolderToRelativeTgz,
6
- cpAbsoluteToRelative,
7
- mapRemoteToAbsolute,
6
+ cpAbsoluteToRelative
8
7
  } from './content_conversion';
9
- import { BlockComponents, BlockComponentsManifest, ContentAbsoluteBinaryLocal, ContentAbsoluteFolder, ContentRelativeBinary } from '@milaboratories/pl-model-middle-layer';
8
+ import {
9
+ BlockComponents,
10
+ BlockComponentsManifest,
11
+ ContentAbsoluteBinaryLocal,
12
+ ContentAbsoluteFolder
13
+ } from '@milaboratories/pl-model-middle-layer';
10
14
 
11
15
  export function BlockComponentsDescription(moduleRoot: string) {
12
16
  return BlockComponents(
@@ -23,10 +27,10 @@ export function BlockComponentsConsolidate(dstFolder: string, fileAccumulator?:
23
27
  ).pipe(BlockComponentsManifest);
24
28
  }
25
29
 
26
- export function BlockComponentsAbsoluteUrl(prefix: string) {
27
- return BlockComponents(
28
- ContentRelativeBinary.transform(mapRemoteToAbsolute(prefix)),
29
- ContentRelativeBinary.transform(mapRemoteToAbsolute(prefix))
30
- );
31
- }
32
- export type BlockComponentsAbsolute = z.infer<ReturnType<typeof BlockComponentsAbsoluteUrl>>;
30
+ // export function BlockComponentsAbsoluteUrl(prefix: string) {
31
+ // return BlockComponents(
32
+ // ContentRelativeBinary.transform(mapRemoteToAbsolute(prefix)),
33
+ // ContentRelativeBinary.transform(mapRemoteToAbsolute(prefix))
34
+ // );
35
+ // }
36
+ // export type BlockComponentsAbsolute = z.infer<ReturnType<typeof BlockComponentsAbsoluteUrl>>;
@@ -0,0 +1,49 @@
1
+ import {
2
+ addPrefixToRelative,
3
+ BlockComponents,
4
+ BlockPackDescriptionManifest,
5
+ BlockPackMeta,
6
+ ContentRelative,
7
+ ContentRelativeBinary,
8
+ ContentRelativeText,
9
+ CreateBlockPackDescriptionSchema
10
+ } from '@milaboratories/pl-model-middle-layer';
11
+ import { BlockComponentsConsolidate, BlockComponentsDescription } from './block_components';
12
+ import { BlockPackMetaConsolidate, BlockPackMetaDescription } from './block_meta';
13
+ import { z } from 'zod';
14
+
15
+ export function ResolvedBlockPackDescriptionFromPackageJson(root: string) {
16
+ return CreateBlockPackDescriptionSchema(
17
+ BlockComponentsDescription(root),
18
+ BlockPackMetaDescription(root)
19
+ );
20
+ }
21
+ export type BlockPackDescriptionAbsolute = z.infer<
22
+ ReturnType<typeof ResolvedBlockPackDescriptionFromPackageJson>
23
+ >;
24
+
25
+ export function BlockPackDescriptionConsolidateToFolder(
26
+ dstFolder: string,
27
+ fileAccumulator?: string[]
28
+ ) {
29
+ return CreateBlockPackDescriptionSchema(
30
+ BlockComponentsConsolidate(dstFolder, fileAccumulator),
31
+ BlockPackMetaConsolidate(dstFolder, fileAccumulator)
32
+ ).pipe(BlockPackDescriptionManifest);
33
+ }
34
+
35
+ export function BlockPackDescriptionManifestAddRelativePathPrefix(prefix: string) {
36
+ const transformer = addPrefixToRelative(prefix);
37
+ return BlockPackDescriptionManifest.pipe(
38
+ CreateBlockPackDescriptionSchema(
39
+ BlockComponents(
40
+ ContentRelative.transform(transformer),
41
+ ContentRelative.transform(transformer)
42
+ ),
43
+ BlockPackMeta(
44
+ ContentRelativeText.transform(transformer),
45
+ ContentRelativeBinary.transform(transformer)
46
+ )
47
+ )
48
+ ).pipe(BlockPackDescriptionManifest);
49
+ }
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  BlockPackMeta,
3
+ BlockPackMetaEmbeddedContent,
3
4
  ContentAbsoluteBinaryLocal,
4
5
  ContentAbsoluteTextLocal,
5
6
  DescriptionContentBinary,
@@ -12,7 +13,6 @@ import {
12
13
  mapLocalToAbsolute
13
14
  } from './content_conversion';
14
15
  import { z } from 'zod';
15
- import { BlockPackMetaEmbeddedContent } from '@milaboratories/pl-model-middle-layer';
16
16
 
17
17
  export function BlockPackMetaDescription(root: string) {
18
18
  return BlockPackMeta(
@@ -8,10 +8,13 @@ import {
8
8
  ContentAbsoluteFile,
9
9
  ContentAbsoluteFolder,
10
10
  ContentAbsoluteTextLocal,
11
- ContentAbsoluteUrl,
12
11
  ContentAnyLocal,
13
12
  ContentExplicitBase64,
14
- ContentRelative
13
+ ContentExplicitBytes,
14
+ ContentExplicitString,
15
+ ContentRelative,
16
+ ContentRelativeBinary,
17
+ ContentRelativeText
15
18
  } from '@milaboratories/pl-model-middle-layer';
16
19
  import { tryResolve } from '@milaboratories/resolve-helper';
17
20
 
@@ -157,12 +160,48 @@ export function packFolderToRelativeTgz(
157
160
  };
158
161
  }
159
162
 
160
- export function mapRemoteToAbsolute(
161
- rootUrl: string
162
- ): <T extends ContentAnyLocal>(value: T) => Exclude<T, ContentRelative> | ContentAbsoluteUrl {
163
- const rootWithSlash = rootUrl.endsWith('/') ? rootUrl : `${rootUrl}/`;
164
- return <T extends ContentAnyLocal>(value: T) =>
165
- value.type === 'relative'
166
- ? { type: 'absolute-url', url: rootWithSlash + value.path }
167
- : (value as Exclude<T, ContentRelative>);
163
+ export type RelativeContentReader = (relativePath: string) => Promise<Buffer>;
164
+
165
+ export function relativeToExplicitText(
166
+ reader: RelativeContentReader
167
+ ): (value: ContentRelativeText) => Promise<ContentExplicitString> {
168
+ return async (value) =>
169
+ value.type === 'explicit-string'
170
+ ? value
171
+ : { type: 'explicit-string', content: (await reader(value.path)).toString('utf8') };
172
+ }
173
+
174
+ export function relativeToExplicitBinary64(
175
+ reader: RelativeContentReader
176
+ ): (value: ContentRelativeBinary) => Promise<ContentExplicitBase64> {
177
+ return async (value) => {
178
+ if (value.type === 'explicit-base64') return value;
179
+ const mimeType = mime.lookup(value.path);
180
+ if (!mimeType) throw new Error(`Can't recognize mime type of the file: ${value.path}.`);
181
+ return {
182
+ type: 'explicit-base64',
183
+ mimeType,
184
+ content: (await reader(value.path)).toString('base64')
185
+ };
186
+ };
187
+ }
188
+
189
+ export function relativeToExplicitBytes(
190
+ reader: RelativeContentReader
191
+ ): (value: ContentRelativeBinary) => Promise<ContentExplicitBytes> {
192
+ return async (value) => {
193
+ if (value.type === 'explicit-base64')
194
+ return {
195
+ type: 'explicit-bytes',
196
+ mimeType: value.mimeType,
197
+ content: Buffer.from(value.content, 'base64')
198
+ };
199
+ const mimeType = mime.lookup(value.path);
200
+ if (!mimeType) throw new Error(`Can't recognize mime type of the file: ${value.path}.`);
201
+ return {
202
+ type: 'explicit-bytes',
203
+ mimeType,
204
+ content: await reader(value.path)
205
+ };
206
+ };
168
207
  }
@@ -1,46 +1,5 @@
1
- import { z } from 'zod';
2
- import { BlockComponentsConsolidate, BlockComponentsDescription } from './block_components';
3
- import {
4
- BlockComponentsManifest,
5
- BlockPackMetaManifest,
6
- CreateBlockPackDescriptionSchema
7
- } from '@milaboratories/pl-model-middle-layer';
8
- import { BlockPackMetaConsolidate, BlockPackMetaDescription } from './meta';
9
-
10
- export * from './block_components';
11
1
  export * from './content_conversion';
12
2
 
13
- export function ResolvedBlockPackDescriptionFromPackageJson(root: string) {
14
- return CreateBlockPackDescriptionSchema(
15
- BlockComponentsDescription(root),
16
- BlockPackMetaDescription(root)
17
- );
18
- }
19
- export type BlockPackDescriptionAbsolute = z.infer<
20
- ReturnType<typeof ResolvedBlockPackDescriptionFromPackageJson>
21
- >;
22
-
23
- export function BlockPackDescriptionConsolidateToFolder(
24
- dstFolder: string,
25
- fileAccumulator?: string[]
26
- ) {
27
- return CreateBlockPackDescriptionSchema(
28
- BlockComponentsConsolidate(dstFolder, fileAccumulator),
29
- //BlockPackMetaToExplicit
30
- BlockPackMetaConsolidate(dstFolder, fileAccumulator)
31
- ).pipe(BlockPackDescriptionManifest);
32
- }
33
-
34
- export const BlockPackDescriptionManifest = CreateBlockPackDescriptionSchema(
35
- BlockComponentsManifest,
36
- BlockPackMetaManifest
37
- );
38
- export type BlockPackDescriptionManifest = z.infer<typeof BlockPackDescriptionManifest>;
39
-
40
- export const BlockPackManifest = BlockPackDescriptionManifest.extend({
41
- schema: z.literal('v1'),
42
- files: z.array(z.string())
43
- });
44
- export type BlockPackManifest = z.infer<typeof BlockPackManifest>;
45
-
46
- export const BlockPackManifestFile = 'manifest.json';
3
+ export * from './block_meta';
4
+ export * from './block_components';
5
+ export * from './block_description';
@@ -0,0 +1,233 @@
1
+ import { MiLogger } from '@milaboratories/ts-helpers';
2
+ import { compare as compareSemver, satisfies } from 'semver';
3
+ import { RegistryStorage } from '../../lib/storage';
4
+ import { BlockPackIdNoVersion, BlockPackManifest } from '@milaboratories/pl-model-middle-layer';
5
+ import {
6
+ GlobalUpdateSeedInFile,
7
+ GlobalUpdateSeedOutFile,
8
+ PackageUpdatePattern,
9
+ packageUpdateSeedPath,
10
+ VersionUpdatesPrefix
11
+ } from './schema_internal';
12
+ import {
13
+ GlobalOverviewReg,
14
+ GlobalOverviewPath,
15
+ ManifestSuffix,
16
+ packageContentPrefix,
17
+ PackageOverview,
18
+ packageOverviewPath,
19
+ ManifestFileName
20
+ } from './schema_public';
21
+ import { BlockPackDescriptionManifestAddRelativePathPrefix, RelativeContentReader } from '../model';
22
+ import { randomUUID } from 'node:crypto';
23
+
24
+ type PackageUpdateInfo = {
25
+ package: BlockPackIdNoVersion;
26
+ versions: Set<String>;
27
+ };
28
+
29
+ export class BlockRegistryV2 {
30
+ constructor(
31
+ private readonly storage: RegistryStorage,
32
+ private readonly logger?: MiLogger
33
+ ) {}
34
+
35
+ private async updateRegistry() {
36
+ this.logger?.info('Initiating registry refresh...');
37
+
38
+ // reading update requests
39
+ const packagesToUpdate = new Map<string, PackageUpdateInfo>();
40
+ const seedPaths: string[] = [];
41
+ const rawSeedPaths = await this.storage.listFiles(VersionUpdatesPrefix);
42
+ this.logger?.info('Packages to be updated:');
43
+ for (const seedPath of rawSeedPaths) {
44
+ const match = seedPath.match(PackageUpdatePattern);
45
+ if (!match) continue;
46
+ seedPaths.push(seedPath);
47
+ const { packageKeyWithoutVersion, organization, name, version, seed } = match.groups!;
48
+
49
+ let update = packagesToUpdate.get(packageKeyWithoutVersion);
50
+ let added = false;
51
+ if (!update) {
52
+ packagesToUpdate.set(packageKeyWithoutVersion, {
53
+ package: { organization, name },
54
+ versions: new Set([version])
55
+ });
56
+ added = true;
57
+ } else if (!update.versions.has(version)) {
58
+ update.versions.add(version);
59
+ added = true;
60
+ }
61
+ this.logger?.info(` - ${organization}:${name}:${version} added:${added}`);
62
+ }
63
+
64
+ // loading global overview
65
+ const overviewContent = await this.storage.getFile(GlobalOverviewPath);
66
+ const overview: GlobalOverviewReg =
67
+ overviewContent === undefined
68
+ ? { schema: 'v2', packages: [] }
69
+ : GlobalOverviewReg.parse(JSON.parse(overviewContent.toString()));
70
+ let overviewPackages = overview.packages;
71
+ this.logger?.info(`Global overview loaded, ${overviewPackages.length} records`);
72
+
73
+ // updating packages
74
+ for (const [, packageInfo] of packagesToUpdate.entries()) {
75
+ // reading existing overview
76
+ const overviewFile = packageOverviewPath(packageInfo.package);
77
+ const pOverviewContent = await this.storage.getFile(overviewFile);
78
+ let packageOverview: PackageOverview =
79
+ pOverviewContent === undefined
80
+ ? { schema: 'v2', versions: [] }
81
+ : PackageOverview.parse(JSON.parse(pOverviewContent.toString()));
82
+ this.logger?.info(
83
+ `Updating ${packageInfo.package.organization}:${packageInfo.package.name} overview, ${packageOverview.versions.length} records`
84
+ );
85
+
86
+ // removing versions that we will update
87
+ const newVersions = packageOverview.versions.filter(
88
+ (e) => !packageInfo.versions.has(e.id.version)
89
+ );
90
+
91
+ // reading new entries
92
+ for (const [v] of packageInfo.versions.entries()) {
93
+ const version = v.toString();
94
+ const manifestContent = await this.storage.getFile(
95
+ packageContentPrefix({
96
+ ...packageInfo.package,
97
+ version
98
+ }) + ManifestSuffix
99
+ );
100
+ if (!manifestContent) continue; // absent package
101
+ newVersions.push(
102
+ BlockPackDescriptionManifestAddRelativePathPrefix(version).parse(
103
+ BlockPackManifest.parse(JSON.parse(manifestContent.toString('utf8'))).description
104
+ )
105
+ );
106
+ }
107
+
108
+ // sorting entries according to version
109
+ newVersions.sort((e1, e2) => compareSemver(e2.id.version, e1.id.version));
110
+
111
+ // write package overview back
112
+ await this.storage.putFile(
113
+ overviewFile,
114
+ Buffer.from(
115
+ JSON.stringify({ schema: 'v2', versions: newVersions } satisfies PackageOverview)
116
+ )
117
+ );
118
+ this.logger?.info(`Done (${newVersions.length} records)`);
119
+
120
+ // patching corresponding entry in overview
121
+ overviewPackages = overviewPackages.filter(
122
+ (e) =>
123
+ e.id.organization !== packageInfo.package.organization ||
124
+ e.id.name !== packageInfo.package.name
125
+ );
126
+ overviewPackages.push({
127
+ id: {
128
+ organization: packageInfo.package.organization,
129
+ name: packageInfo.package.name
130
+ },
131
+ allVersions: newVersions.map((e) => e.id.version).reverse(),
132
+ latest: BlockPackDescriptionManifestAddRelativePathPrefix(packageInfo.package.name).parse(
133
+ newVersions[0]
134
+ )
135
+ });
136
+ }
137
+
138
+ // writing global overview
139
+ await this.storage.putFile(
140
+ GlobalOverviewPath,
141
+ Buffer.from(
142
+ JSON.stringify({ schema: 'v2', packages: overviewPackages } satisfies GlobalOverviewReg)
143
+ )
144
+ );
145
+ this.logger?.info(`Global overview updated (${overviewPackages.length} records)`);
146
+
147
+ // deleting seeds
148
+ await this.storage.deleteFiles(...seedPaths.map((sp) => `${VersionUpdatesPrefix}${sp}`));
149
+ this.logger?.info(`Version update requests cleared`);
150
+ }
151
+
152
+ public async updateIfNeeded(force: boolean = false): Promise<void> {
153
+ // implementation of main convergence algorithm
154
+
155
+ this.logger?.info(`Checking if registry requires refresh...`);
156
+ const updateRequestSeed = await this.storage.getFile(GlobalUpdateSeedInFile);
157
+ const currentUpdatedSeed = await this.storage.getFile(GlobalUpdateSeedOutFile);
158
+ if (!force && updateRequestSeed === undefined && currentUpdatedSeed === undefined) return;
159
+ if (
160
+ !force &&
161
+ updateRequestSeed !== undefined &&
162
+ currentUpdatedSeed !== undefined &&
163
+ updateRequestSeed.equals(currentUpdatedSeed)
164
+ )
165
+ return;
166
+
167
+ await this.updateRegistry();
168
+
169
+ if (updateRequestSeed) {
170
+ await this.storage.putFile(GlobalUpdateSeedOutFile, updateRequestSeed);
171
+ this.logger?.info(`Refresh finished`);
172
+ }
173
+ }
174
+
175
+ public async getPackageOverview(
176
+ name: BlockPackIdNoVersion
177
+ ): Promise<undefined | PackageOverview> {
178
+ const content = await this.storage.getFile(packageOverviewPath(name));
179
+ if (content === undefined) return undefined;
180
+ return PackageOverview.parse(JSON.parse(content.toString()));
181
+ }
182
+
183
+ public async getGlobalOverview(): Promise<undefined | GlobalOverviewReg> {
184
+ const content = await this.storage.getFile(GlobalOverviewPath);
185
+ if (content === undefined) return undefined;
186
+ return GlobalOverviewReg.parse(JSON.parse(content.toString()));
187
+ }
188
+
189
+ // public async getGlobalOverviewExplicitBytes(): Promise<undefined | GlobalOverviewReg> {
190
+ // const content = await this.storage.getFile(GlobalOverviewPath);
191
+ // if (content === undefined) return undefined;
192
+ // return GlobalOverviewReg.parse(JSON.parse(content.toString()));
193
+ // }
194
+
195
+ public async publishPackage(
196
+ manifest: BlockPackManifest,
197
+ fileReader: RelativeContentReader
198
+ ): Promise<void> {
199
+ const prefix = packageContentPrefix(manifest.description.id);
200
+ // uploading content files
201
+ for (const f of manifest.files) {
202
+ const bytes = await fileReader(f.name);
203
+ if (bytes.length !== f.size)
204
+ throw new Error(
205
+ `Actual file size don't match file size from the manifest file for ${f.name} (actual = ${bytes.length}; manifest = ${f.size})`
206
+ );
207
+ const sha256 = Buffer.from(await crypto.subtle.digest('sha-256', bytes))
208
+ .toString('hex')
209
+ .toUpperCase();
210
+ if (sha256 !== f.sha256.toUpperCase())
211
+ throw new Error(
212
+ `Actual file SHA-256 don't match the checksum from the manifest file for ${f.name} (actual = ${sha256}; manifest = ${f.sha256.toUpperCase()})`
213
+ );
214
+
215
+ const dst = prefix + '/' + f.name;
216
+ this.logger?.info(`Uploading ${f.name} -> ${dst} ...`);
217
+ await this.storage.putFile(dst, bytes);
218
+ }
219
+
220
+ // uploading manifest as the last upload action
221
+ const manifestDst = prefix + '/' + ManifestFileName;
222
+ this.logger?.info(`Uploading manifest to ${manifestDst} ...`);
223
+ await this.storage.putFile(manifestDst, Buffer.from(JSON.stringify(manifest)));
224
+
225
+ // adding update seed
226
+ const seed = randomUUID();
227
+ const seedPath = packageUpdateSeedPath(manifest.description.id, seed);
228
+ this.logger?.info(`Creating update seed at ${seedPath} ...`);
229
+ await this.storage.putFile(seedPath, Buffer.from(seed));
230
+ this.logger?.info(`Touching global update seed ${GlobalUpdateSeedInFile} ...`);
231
+ await this.storage.putFile(GlobalUpdateSeedInFile, Buffer.from(seed));
232
+ }
233
+ }
@@ -0,0 +1,13 @@
1
+ import { BlockPackId } from '@milaboratories/pl-model-middle-layer';
2
+
3
+ export const VersionUpdatesPrefix = '_updates_v2/per_package_version/';
4
+
5
+ export function packageUpdateSeedPath(bp: BlockPackId, seed: string): string {
6
+ return `${VersionUpdatesPrefix}${bp.organization}/${bp.name}/${bp.version}/${seed}`;
7
+ }
8
+
9
+ export const PackageUpdatePattern =
10
+ /(?<packageKeyWithoutVersion>(?<organization>[^\/]+)\/(?<name>[^\/]+))\/(?<version>[^\/]+)\/(?<seed>[^\/]+)$/;
11
+
12
+ export const GlobalUpdateSeedInFile = '_updates_v2/_global_update_in';
13
+ export const GlobalUpdateSeedOutFile = '_updates_v2/_global_update_out';