@drawcall/market 0.6.14 → 0.7.1

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 (45) hide show
  1. package/dist/client.d.ts.map +1 -1
  2. package/dist/client.js +7 -4
  3. package/dist/client.js.map +1 -1
  4. package/dist/commands/install.d.ts.map +1 -1
  5. package/dist/commands/install.js +6 -4
  6. package/dist/commands/install.js.map +1 -1
  7. package/dist/commands/preview.d.ts +2 -14
  8. package/dist/commands/preview.d.ts.map +1 -1
  9. package/dist/commands/preview.js +4 -8
  10. package/dist/commands/preview.js.map +1 -1
  11. package/dist/commands/upload.d.ts.map +1 -1
  12. package/dist/commands/upload.js +5 -6
  13. package/dist/commands/upload.js.map +1 -1
  14. package/dist/index.d.ts +1 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +2 -0
  17. package/dist/index.js.map +1 -1
  18. package/dist/install.d.ts.map +1 -1
  19. package/dist/install.js +11 -12
  20. package/dist/install.js.map +1 -1
  21. package/dist/pack.d.ts.map +1 -1
  22. package/dist/pack.js +10 -17
  23. package/dist/pack.js.map +1 -1
  24. package/dist/refs.d.ts +20 -0
  25. package/dist/refs.d.ts.map +1 -0
  26. package/dist/refs.js +25 -0
  27. package/dist/refs.js.map +1 -0
  28. package/dist/resolve.d.ts +3 -0
  29. package/dist/resolve.d.ts.map +1 -1
  30. package/dist/resolve.js +27 -42
  31. package/dist/resolve.js.map +1 -1
  32. package/dist/v1/contract.d.ts +1 -0
  33. package/dist/v1/contract.d.ts.map +1 -1
  34. package/dist/v1/contract.js.map +1 -1
  35. package/package.json +1 -1
  36. package/src/client.ts +7 -4
  37. package/src/commands/install.ts +6 -4
  38. package/src/commands/preview.ts +10 -24
  39. package/src/commands/upload.ts +5 -6
  40. package/src/index.ts +3 -0
  41. package/src/install.ts +12 -15
  42. package/src/pack.ts +16 -22
  43. package/src/refs.ts +44 -0
  44. package/src/resolve.ts +44 -61
  45. package/src/v1/contract.ts +1 -0
package/src/pack.ts CHANGED
@@ -3,7 +3,6 @@ import * as fs from 'fs/promises'
3
3
  import * as path from 'path'
4
4
  import { unzipSync, zipSync } from 'fflate'
5
5
  import ignore from 'ignore'
6
- import pMap from 'p-map'
7
6
  import semver from 'semver'
8
7
  import {
9
8
  ALWAYS_IGNORED_DIRS,
@@ -14,6 +13,7 @@ import {
14
13
  type GitignoreMatcher,
15
14
  } from './project-walk.js'
16
15
  import type { MarketClient } from './client.js'
16
+ import { lookupAssets } from './refs.js'
17
17
  import type { AssetInstallMetadata } from './contract.js'
18
18
  import {
19
19
  packageJsonAssetDependencies,
@@ -102,35 +102,29 @@ const MANIFEST_VERSION_CAP = 20
102
102
  export function fileManifestForRange(asset: MarketClient['asset']): FetchFileManifest {
103
103
  return async (name, range) => {
104
104
  const exact = exactRangeVersion(range)
105
- if (exact) return fileIndexHashes(asset, name, exact)
106
-
107
- const { versions } = await asset.versions({ name })
108
- const inRange = versions
109
- .map((entry) => entry.version)
110
- .filter((version) => semver.satisfies(version, range))
111
- .sort(semver.compare)
112
- .slice(-MANIFEST_VERSION_CAP)
105
+ const inRange = exact
106
+ ? [exact]
107
+ : ((await lookupAssets(asset, [{ name }]))[0]?.versions ?? [])
108
+ .filter((version) => semver.satisfies(version, range))
109
+ .sort(semver.compare)
110
+ .slice(-MANIFEST_VERSION_CAP)
113
111
 
114
112
  // Oldest first, so a newer version's hash wins when a path recurs across versions.
113
+ const entries = await lookupAssets(
114
+ asset,
115
+ inRange.map((version) => ({ name, version })),
116
+ )
115
117
  const merged: Record<string, string> = {}
116
- for (const hashes of await pMap(inRange, (version) => fileIndexHashes(asset, name, version), {
117
- concurrency: 4,
118
- })) {
119
- Object.assign(merged, hashes)
118
+ for (const entry of entries) {
119
+ Object.assign(
120
+ merged,
121
+ Object.fromEntries((entry?.files ?? []).map((file) => [file.path, file.etag])),
122
+ )
120
123
  }
121
124
  return merged
122
125
  }
123
126
  }
124
127
 
125
- async function fileIndexHashes(
126
- asset: MarketClient['asset'],
127
- name: string,
128
- version: string,
129
- ): Promise<Record<string, string>> {
130
- const index = await asset.files({ name, version })
131
- return Object.fromEntries(index.files.map((file) => [file.path, file.etag]))
132
- }
133
-
134
128
  function exactRangeVersion(range: string): string | null {
135
129
  const candidate = range.trim().replace(/^=/u, '')
136
130
  return semverSchema.safeParse(candidate).success ? candidate : null
package/src/refs.ts ADDED
@@ -0,0 +1,44 @@
1
+ import type { MarketClient } from './client.js'
2
+ import type { AssetRefEntry } from './v1/index.js'
3
+
4
+ export interface AssetRef {
5
+ name: string
6
+ version?: string
7
+ }
8
+
9
+ /** The one call the refs read needs — lets tests fake a single function. */
10
+ export interface RefsReader {
11
+ search: MarketClient['asset']['search']
12
+ }
13
+
14
+ /** The server accepts up to 100 refs per read; larger sets chunk transparently. */
15
+ const MAX_REFS_PER_CALL = 100
16
+
17
+ /**
18
+ * The collection read in refs mode: full entries (metadata, version history,
19
+ * files with data-plane URLs) for exactly the given refs — order-preserved,
20
+ * null per miss. Version omitted resolves to the latest visible version.
21
+ */
22
+ export async function lookupAssets(
23
+ asset: RefsReader,
24
+ refs: AssetRef[],
25
+ opts: { includeUnapproved?: boolean; key?: string } = {},
26
+ ): Promise<(AssetRefEntry | null)[]> {
27
+ const chunks: AssetRef[][] = []
28
+ for (let i = 0; i < refs.length; i += MAX_REFS_PER_CALL) {
29
+ chunks.push(refs.slice(i, i + MAX_REFS_PER_CALL))
30
+ }
31
+
32
+ const results = await Promise.all(
33
+ chunks.map(async (chunk) => {
34
+ const result = await asset.search({
35
+ refs: chunk.map((ref) => (ref.version ? `${ref.name}@${ref.version}` : ref.name)).join(','),
36
+ includeUnapproved: opts.includeUnapproved ?? false,
37
+ ...(opts.key ? { key: opts.key } : {}),
38
+ })
39
+ if (!('assets' in result)) throw new Error('refs read unexpectedly returned a search result')
40
+ return result.assets
41
+ }),
42
+ )
43
+ return results.flat()
44
+ }
package/src/resolve.ts CHANGED
@@ -11,7 +11,9 @@
11
11
 
12
12
  import * as semver from 'semver'
13
13
  import type { MarketClient } from './client.js'
14
- import type { AssetExactResult } from './contract.js'
14
+ import type { AssetFileEntry } from './contract.js'
15
+ import { lookupAssets } from './refs.js'
16
+ import type { AssetRefEntry } from './v1/index.js'
15
17
  import {
16
18
  assetDependencyAlias,
17
19
  type AssetDependencyAlias,
@@ -28,6 +30,8 @@ export interface ResolvedAsset {
28
30
  npmDependencies: Record<string, string>
29
31
  assetDependencies: AssetDependencies
30
32
  skillDependencies: Record<string, string>
33
+ /** The resolved version's file index, carried from resolution so install needs no further reads. */
34
+ files: AssetFileEntry[]
31
35
  }
32
36
 
33
37
  export interface ResolveResult {
@@ -74,7 +78,7 @@ export async function resolve(
74
78
  requests: AssetRequest[],
75
79
  opts: { includeUnapproved?: boolean; key?: string } = {},
76
80
  ): Promise<ResolveResult> {
77
- const metaCache = new Map<string, AssetExactResult>()
81
+ const metaCache = new Map<string, AssetRefEntry>()
78
82
  const includeUnapproved = opts.includeUnapproved ?? false
79
83
  let exactPins = exactPinsFor(rootConstraints(requests))
80
84
  const seenPinSets = new Set<string>()
@@ -122,7 +126,7 @@ async function resolvePass(
122
126
  requests: AssetRequest[],
123
127
  initialPins: ExactPins,
124
128
  includeUnapproved: boolean,
125
- metaCache: Map<string, AssetExactResult>,
129
+ metaCache: Map<string, AssetRefEntry>,
126
130
  key?: string,
127
131
  ): Promise<ResolutionPass> {
128
132
  const constraints = rootConstraints(requests)
@@ -138,19 +142,34 @@ async function resolvePass(
138
142
  const names = [...new Set(wave)].filter((name) => !resolved.has(name))
139
143
  wave = []
140
144
 
141
- const metas = await Promise.all(
142
- names.map(async (assetName) => {
143
- const constrainedExact = requiredExactVersion(assetName, constraints.get(assetName) ?? [])
144
- const exactVersion = constrainedExact ?? initialPins.get(assetName) ?? null
145
- return {
146
- assetName,
147
- exactVersion,
148
- meta: await fetchMeta(client, metaCache, assetName, exactVersion, includeUnapproved, key),
149
- }
150
- }),
151
- )
145
+ const wanted = names.map((assetName) => {
146
+ const constrainedExact = requiredExactVersion(assetName, constraints.get(assetName) ?? [])
147
+ const exactVersion = constrainedExact ?? initialPins.get(assetName) ?? null
148
+ return { assetName, exactVersion }
149
+ })
152
150
 
153
- for (const { assetName, exactVersion, meta } of metas) {
151
+ // One collection read per wave; the cache spans convergence passes, so a
152
+ // re-pass that pins already-seen versions makes no further requests.
153
+ const misses = wanted.filter(
154
+ ({ assetName, exactVersion }) => !metaCache.has(metaKey(assetName, exactVersion)),
155
+ )
156
+ const fetched = await lookupAssets(
157
+ client,
158
+ misses.map(({ assetName, exactVersion }) => ({
159
+ name: assetName,
160
+ version: exactVersion ?? undefined,
161
+ })),
162
+ { includeUnapproved, key },
163
+ )
164
+ misses.forEach(({ assetName, exactVersion }, index) => {
165
+ const entry = fetched[index]
166
+ if (!entry) return
167
+ metaCache.set(metaKey(assetName, exactVersion), entry)
168
+ metaCache.set(metaKey(assetName, entry.version), entry)
169
+ })
170
+
171
+ for (const { assetName, exactVersion } of wanted) {
172
+ const meta = metaCache.get(metaKey(assetName, exactVersion)) ?? null
154
173
  if (!meta) {
155
174
  if (exactVersion) {
156
175
  throw new ResolutionError(
@@ -166,12 +185,13 @@ async function resolvePass(
166
185
  throw new ResolutionError(`Asset "${assetName}" has no approved versions.`)
167
186
  }
168
187
 
169
- const selectedVersion = exactVersion
170
- ? exactMetadataVersion(meta, assetName, exactVersion)
171
- : latestMetadataVersion(meta)
172
- // The meta demonstrably answers for this version; caching it under the exact key makes the
173
- // convergence re-pass (which pins every asset) resolve without a single further request.
174
- metaCache.set(`${assetName}@${selectedVersion}`, meta)
188
+ if (exactVersion && meta.version !== exactVersion) {
189
+ throw new ResolutionError(
190
+ `Market API did not return requested version "${assetName}@${exactVersion}". ` +
191
+ 'Refusing to fall back to a different version.',
192
+ )
193
+ }
194
+ const selectedVersion = meta.version
175
195
  const assetDependencies = meta.assetDependencies
176
196
  resolved.set(assetName, {
177
197
  name: assetName,
@@ -181,6 +201,7 @@ async function resolvePass(
181
201
  npmDependencies: meta.npmDependencies,
182
202
  assetDependencies,
183
203
  skillDependencies: meta.skillDependencies,
204
+ files: meta.files,
184
205
  })
185
206
 
186
207
  for (const [dependencyName, dependencyValue] of Object.entries(assetDependencies)) {
@@ -293,28 +314,8 @@ function pinSignature(pins: ExactPins): string {
293
314
  return JSON.stringify([...pins].sort(([left], [right]) => left.localeCompare(right)))
294
315
  }
295
316
 
296
- async function fetchMeta(
297
- client: MarketClient['asset'],
298
- cache: Map<string, AssetExactResult>,
299
- name: string,
300
- version: string | null,
301
- includeUnapproved: boolean,
302
- key?: string,
303
- ): Promise<AssetExactResult | null> {
304
- const cacheKey = version ? `${name}@${version}` : name
305
- const cached = cache.get(cacheKey)
306
- if (cached) return cached
307
- // The key is sent with every lookup in the tree; it only unlocks the asset it belongs to and is
308
- // inert for the rest, so no per-asset routing is needed.
309
- const meta = await client.exact({
310
- name,
311
- ...(version ? { version } : {}),
312
- includeUnapproved,
313
- ...(key ? { key } : {}),
314
- })
315
- if (meta) cache.set(cacheKey, meta)
316
- return meta
317
- }
317
+ const metaKey = (name: string, version: string | null): string =>
318
+ version ? `${name}@${version}` : name
318
319
 
319
320
  function requiredExactVersion(assetName: string, constraints: Constraint[]): string | null {
320
321
  const exactVersions = new Set(
@@ -335,24 +336,6 @@ function exactVersion(range: string): string | null {
335
336
  return semverSchema.safeParse(candidate).success ? candidate : null
336
337
  }
337
338
 
338
- function exactMetadataVersion(
339
- meta: AssetExactResult,
340
- assetName: string,
341
- requestedVersion: string,
342
- ): string {
343
- if (meta.version === requestedVersion) return meta.version
344
- throw new ResolutionError(
345
- `Market API did not return requested version "${assetName}@${requestedVersion}". ` +
346
- 'Refusing to fall back to a different version.',
347
- )
348
- }
349
-
350
- function latestMetadataVersion(meta: AssetExactResult): string {
351
- // Older workers do not return `version`. Range requests can retain their established latest-
352
- // version behavior; exact requests instead fail closed in exactMetadataVersion above.
353
- return meta.version ?? meta.latestVersion
354
- }
355
-
356
339
  /**
357
340
  * Merge npm dependency ranges from all resolved assets.
358
341
  * For each package, check that all declared ranges are compatible
@@ -38,6 +38,7 @@ export interface AssetVersionManifest {
38
38
  name: string
39
39
  type: string
40
40
  version: string
41
+ ownerId: string
41
42
  description: string | null
42
43
  approved: boolean
43
44
  access: 'public' | 'private'