@platforma-sdk/block-tools 2.4.7 → 2.4.8

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.
@@ -16,12 +16,12 @@ import {
16
16
  MainPrefix,
17
17
  ManifestFileName,
18
18
  ManifestSuffix,
19
- packageContentPrefixInsideV2,
20
- packageOverviewPathInsideV2
19
+ packageContentPrefixInsideV2
21
20
  } from './schema_public';
22
21
  import { BlockComponentsAbsoluteUrl, BlockPackMetaEmbedBytes } from '../model';
23
22
  import { LRUCache } from 'lru-cache';
24
23
  import { calculateSha256 } from '../../util';
24
+ import { retry, Retry2TimesWithDelay } from '@milaboratories/ts-helpers';
25
25
 
26
26
  export type BlockPackOverviewNoRegLabel = Omit<BlockPackOverview, 'registryId'>;
27
27
 
@@ -60,15 +60,16 @@ export class RegistryV2Reader {
60
60
  { meta: BlockPackMetaManifest; relativeTo?: BlockPackId }
61
61
  >({
62
62
  max: 500,
63
- fetchMethod: async (_key, _staleValue, options) => {
64
- const contentReader =
65
- options.context.relativeTo !== undefined
66
- ? this.v2RootFolderReader
67
- .relativeReader(packageContentPrefixInsideV2(options.context.relativeTo))
68
- .getContentReader()
69
- : this.v2RootFolderReader.getContentReader();
70
- return await BlockPackMetaEmbedBytes(contentReader).parseAsync(options.context.meta);
71
- }
63
+ fetchMethod: async (_key, _staleValue, options) =>
64
+ await retry(async () => {
65
+ const contentReader =
66
+ options.context.relativeTo !== undefined
67
+ ? this.v2RootFolderReader
68
+ .relativeReader(packageContentPrefixInsideV2(options.context.relativeTo))
69
+ .getContentReader()
70
+ : this.v2RootFolderReader.getContentReader();
71
+ return await BlockPackMetaEmbedBytes(contentReader).parseAsync(options.context.meta);
72
+ }, Retry2TimesWithDelay)
72
73
  });
73
74
 
74
75
  private async embedMetaContent(
@@ -93,47 +94,49 @@ export class RegistryV2Reader {
93
94
  )
94
95
  return this.listCache;
95
96
  try {
96
- // const rootContentReader = this.v2RootFolderReader.getContentReader();
97
- const globalOverview = GlobalOverviewReg.parse(
98
- JSON.parse(
99
- Buffer.from(await this.v2RootFolderReader.readFile(GlobalOverviewFileName)).toString()
100
- )
101
- );
102
-
103
- const result = await Promise.all(
104
- globalOverview.packages.map(async (p) => {
105
- const byChannelEntries = await Promise.all(
106
- Object.entries(p.latestByChannel).map(async ([channel, data]) => [
107
- channel,
108
- {
109
- id: data.description.id,
110
- meta: await this.embedMetaContent(
111
- p.latest.id,
112
- p.latestManifestSha256,
113
- true,
114
- p.latest.meta
115
- ),
116
- spec: {
117
- type: 'from-registry-v2',
118
- id: p.latest.id,
119
- registryUrl: this.registryReader.rootUrl.toString(),
120
- channel
97
+ return await retry(async () => {
98
+ // const rootContentReader = this.v2RootFolderReader.getContentReader();
99
+ const globalOverview = GlobalOverviewReg.parse(
100
+ JSON.parse(
101
+ Buffer.from(await this.v2RootFolderReader.readFile(GlobalOverviewFileName)).toString()
102
+ )
103
+ );
104
+
105
+ const result = await Promise.all(
106
+ globalOverview.packages.map(async (p) => {
107
+ const byChannelEntries = await Promise.all(
108
+ Object.entries(p.latestByChannel).map(async ([channel, data]) => [
109
+ channel,
110
+ {
111
+ id: data.description.id,
112
+ meta: await this.embedMetaContent(
113
+ p.latest.id,
114
+ p.latestManifestSha256,
115
+ true,
116
+ p.latest.meta
117
+ ),
118
+ spec: {
119
+ type: 'from-registry-v2',
120
+ id: p.latest.id,
121
+ registryUrl: this.registryReader.rootUrl.toString(),
122
+ channel
123
+ }
121
124
  }
122
- }
123
- ])
124
- );
125
- return {
126
- id: p.id,
127
- latestByChannel: Object.fromEntries(byChannelEntries),
128
- allVersions: p.allVersionsWithChannels
129
- } satisfies BlockPackOverviewNoRegLabel;
130
- })
131
- );
132
-
133
- this.listCache = result;
134
- this.listCacheTimestamp = Date.now();
135
-
136
- return result;
125
+ ])
126
+ );
127
+ return {
128
+ id: p.id,
129
+ latestByChannel: Object.fromEntries(byChannelEntries),
130
+ allVersions: p.allVersionsWithChannels
131
+ } satisfies BlockPackOverviewNoRegLabel;
132
+ })
133
+ );
134
+
135
+ this.listCache = result;
136
+ this.listCacheTimestamp = Date.now();
137
+
138
+ return result;
139
+ }, Retry2TimesWithDelay);
137
140
  } catch (e: unknown) {
138
141
  if (
139
142
  this.listCache !== undefined &&
@@ -148,51 +151,56 @@ export class RegistryV2Reader {
148
151
  id: BlockPackIdNoVersion,
149
152
  channel: string
150
153
  ): Promise<SingleBlockPackOverview | undefined> {
151
- const overview = (await this.listBlockPacks()).find((e) =>
152
- blockPackIdNoVersionEquals(id, e.id)
153
- );
154
- if (overview === undefined) return undefined;
155
- return overview.latestByChannel[channel];
154
+ return await retry(async () => {
155
+ const overview = (await this.listBlockPacks()).find((e) =>
156
+ blockPackIdNoVersionEquals(id, e.id)
157
+ );
158
+ if (overview === undefined) return undefined;
159
+ return overview.latestByChannel[channel];
160
+ }, Retry2TimesWithDelay);
156
161
  }
157
162
 
158
163
  public async getSpecificOverview(
159
164
  id: BlockPackId,
160
165
  channel: string
161
166
  ): Promise<SingleBlockPackOverview> {
162
- const manifestContent = await this.v2RootFolderReader.readFile(
163
- packageContentPrefixInsideV2(id) + ManifestSuffix
164
- );
165
- const overview = BlockPackManifest.parse(JSON.parse(Buffer.from(manifestContent).toString()));
166
- return {
167
- id: id,
168
- meta: await this.embedMetaContent(
169
- id,
170
- await calculateSha256(manifestContent),
171
- false,
172
- overview.description.meta
173
- ),
174
- spec: {
175
- type: 'from-registry-v2',
176
- id,
177
- registryUrl: this.registryReader.rootUrl.toString(),
178
- channel
179
- }
180
- };
167
+ return await retry(async () => {
168
+ const manifestContent = await this.v2RootFolderReader.readFile(
169
+ packageContentPrefixInsideV2(id) + ManifestSuffix
170
+ );
171
+ const overview = BlockPackManifest.parse(JSON.parse(Buffer.from(manifestContent).toString()));
172
+ return {
173
+ id: id,
174
+ meta: await this.embedMetaContent(
175
+ id,
176
+ await calculateSha256(manifestContent),
177
+ false,
178
+ overview.description.meta
179
+ ),
180
+ spec: {
181
+ type: 'from-registry-v2',
182
+ id,
183
+ registryUrl: this.registryReader.rootUrl.toString(),
184
+ channel
185
+ }
186
+ };
187
+ }, Retry2TimesWithDelay);
181
188
  }
182
189
 
183
190
  private readonly componentsCache = new LRUCache<string, BlockComponentsAbsoluteUrl, BlockPackId>({
184
191
  max: 500,
185
- fetchMethod: async (key, staleValue, { context: id }) => {
186
- const packageFolderReader = this.v2RootFolderReader.relativeReader(
187
- packageContentPrefixInsideV2(id)
188
- );
189
- const manifest = BlockPackManifest.parse(
190
- JSON.parse(Buffer.from(await packageFolderReader.readFile(ManifestFileName)).toString())
191
- );
192
- return BlockComponentsAbsoluteUrl(packageFolderReader.rootUrl.toString()).parse(
193
- manifest.description.components
194
- );
195
- }
192
+ fetchMethod: async (key, staleValue, { context: id }) =>
193
+ await retry(async () => {
194
+ const packageFolderReader = this.v2RootFolderReader.relativeReader(
195
+ packageContentPrefixInsideV2(id)
196
+ );
197
+ const manifest = BlockPackManifest.parse(
198
+ JSON.parse(Buffer.from(await packageFolderReader.readFile(ManifestFileName)).toString())
199
+ );
200
+ return BlockComponentsAbsoluteUrl(packageFolderReader.rootUrl.toString()).parse(
201
+ manifest.description.components
202
+ );
203
+ }, Retry2TimesWithDelay)
196
204
  });
197
205
 
198
206
  public async getComponents(id: BlockPackId): Promise<BlockComponentsAbsoluteUrl> {