@prompd/core 0.5.0-beta.10 → 0.5.0-beta.12

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.
package/dist/index.cjs CHANGED
@@ -3,6 +3,7 @@
3
3
  var yaml = require('yaml');
4
4
  var semver = require('semver');
5
5
  var nunjucks = require('nunjucks');
6
+ var JSZip = require('jszip');
6
7
 
7
8
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
8
9
 
@@ -27,6 +28,7 @@ function _interopNamespace(e) {
27
28
  var yaml__namespace = /*#__PURE__*/_interopNamespace(yaml);
28
29
  var semver__default = /*#__PURE__*/_interopDefault(semver);
29
30
  var nunjucks__namespace = /*#__PURE__*/_interopNamespace(nunjucks);
31
+ var JSZip__default = /*#__PURE__*/_interopDefault(JSZip);
30
32
 
31
33
  // src/lib/parser.ts
32
34
  var PrompdParser = class {
@@ -572,6 +574,30 @@ function resolvePackageFile(packagePath, filePath) {
572
574
  }
573
575
  return resolvedPath;
574
576
  }
577
+ function isPackageCapable(fs) {
578
+ const f = fs;
579
+ return !!f && typeof f.getPackagePath === "function" && typeof f.addPackage === "function" && typeof f.isDirectory === "function";
580
+ }
581
+ var MemoryPackageResolver = class {
582
+ constructor(download) {
583
+ this.download = download;
584
+ }
585
+ async resolvePackage(packageRef, options) {
586
+ const fs = options.fileSystem;
587
+ if (!isPackageCapable(fs)) {
588
+ throw new Error("MemoryPackageResolver requires a package-capable in-memory file system (MemoryFileSystem or HybridFileSystem)");
589
+ }
590
+ if (!isValidPackageReference(packageRef)) {
591
+ throw new SecurityError(`Invalid package reference format: ${packageRef}`);
592
+ }
593
+ const { name, version } = parsePackageReference(packageRef);
594
+ const packagePath = fs.getPackagePath(name, version);
595
+ if (await fs.isDirectory(packagePath)) return packagePath;
596
+ const buffer = await this.download(name, version);
597
+ await fs.addPackage(name, version, buffer);
598
+ return packagePath;
599
+ }
600
+ };
575
601
 
576
602
  // src/lib/compiler/path-utils.ts
577
603
  function normalizePosix(p) {
@@ -3071,8 +3097,54 @@ var CodeGenerationStage = class {
3071
3097
  return "Code Generation";
3072
3098
  }
3073
3099
  };
3074
-
3075
- // src/lib/compiler/file-system.ts
3100
+ var BINARY_ASSET_EXT = /* @__PURE__ */ new Set([
3101
+ "png",
3102
+ "jpg",
3103
+ "jpeg",
3104
+ "gif",
3105
+ "webp",
3106
+ "svg",
3107
+ "ico",
3108
+ "bmp",
3109
+ "tiff",
3110
+ "pdf",
3111
+ "xlsx",
3112
+ "xls",
3113
+ "docx",
3114
+ "doc",
3115
+ "pptx",
3116
+ "ppt",
3117
+ "zip",
3118
+ "pdpkg",
3119
+ "gz",
3120
+ "tar",
3121
+ "wasm",
3122
+ "woff",
3123
+ "woff2",
3124
+ "ttf",
3125
+ "otf",
3126
+ "eot",
3127
+ "mp3",
3128
+ "mp4",
3129
+ "wav",
3130
+ "ogg",
3131
+ "webm",
3132
+ "mov",
3133
+ "avi"
3134
+ ]);
3135
+ function isBinaryAsset(name) {
3136
+ const dot = name.lastIndexOf(".");
3137
+ return dot >= 0 && BINARY_ASSET_EXT.has(name.slice(dot + 1).toLowerCase());
3138
+ }
3139
+ async function extractPdpkg(buffer) {
3140
+ const zip = await JSZip__default.default.loadAsync(buffer);
3141
+ const out = /* @__PURE__ */ new Map();
3142
+ for (const entry of Object.values(zip.files)) {
3143
+ if (entry.dir || isBinaryAsset(entry.name)) continue;
3144
+ out.set(entry.name, await entry.async("string"));
3145
+ }
3146
+ return out;
3147
+ }
3076
3148
  function normalizePosix2(p) {
3077
3149
  const isAbs = p.startsWith("/");
3078
3150
  const out = [];
@@ -3110,7 +3182,7 @@ function toRelKey(filePath) {
3110
3182
  if (n.endsWith("/") && n.length > 1) n = n.substring(0, n.length - 1);
3111
3183
  return n;
3112
3184
  }
3113
- var MemoryFileSystem = class {
3185
+ var _MemoryFileSystem = class _MemoryFileSystem {
3114
3186
  constructor(files = {}) {
3115
3187
  this.files = /* @__PURE__ */ new Map();
3116
3188
  for (const [filePath, content] of Object.entries(files)) {
@@ -3177,6 +3249,33 @@ var MemoryFileSystem = class {
3177
3249
  getPackagePath(packageName, version) {
3178
3250
  return `/packages/${packageName}@${version}`;
3179
3251
  }
3252
+ /**
3253
+ * Extract a `.pdpkg` (ZIP) buffer into memory under getPackagePath(name, ver).
3254
+ *
3255
+ * Uses JSZip — isomorphic (browser + Node) — so this is the SINGLE package-ingest
3256
+ * path for every host (replacing the CLI's Node-only adm-zip subclass and the
3257
+ * skill installer's ad-hoc unzip). Text files only: binary assets aren't
3258
+ * representable in the string-backed FS, so they're skipped (a `using:` package's
3259
+ * .prmd/.md/.json/.yaml is what matters for compilation). Validates entry paths
3260
+ * can't escape the package directory.
3261
+ */
3262
+ async addPackage(packageName, version, packageBuffer) {
3263
+ if (packageBuffer.length > _MemoryFileSystem.MAX_PACKAGE_SIZE) {
3264
+ throw new Error(`Package too large: ${packageBuffer.length} bytes (max ${_MemoryFileSystem.MAX_PACKAGE_SIZE})`);
3265
+ }
3266
+ const files = await extractPdpkg(packageBuffer);
3267
+ const packagePath = this.getPackagePath(packageName, version);
3268
+ const packagePrefix = this.normalizePath(packagePath.endsWith("/") ? packagePath : packagePath + "/");
3269
+ const packageRoot = this.normalizePath(packagePath);
3270
+ for (const [rel, content] of files) {
3271
+ const filePath = this.join(packagePath, rel);
3272
+ const normalized = this.normalizePath(filePath);
3273
+ if (!normalized.startsWith(packagePrefix) && normalized !== packageRoot) {
3274
+ throw new Error(`Security violation: extracted path escapes package directory: ${rel}`);
3275
+ }
3276
+ this.addFile(filePath, content);
3277
+ }
3278
+ }
3180
3279
  /** Get all files under an optional base path. */
3181
3280
  getAllFiles(basePath) {
3182
3281
  if (!basePath) {
@@ -3206,6 +3305,9 @@ var MemoryFileSystem = class {
3206
3305
  return toRelKey(filePath);
3207
3306
  }
3208
3307
  };
3308
+ /** Cap on an ingested package buffer (defends against a hostile/huge .pdpkg). */
3309
+ _MemoryFileSystem.MAX_PACKAGE_SIZE = 50 * 1024 * 1024;
3310
+ var MemoryFileSystem = _MemoryFileSystem;
3209
3311
  var HybridFileSystem = class {
3210
3312
  constructor(files = {}, backend) {
3211
3313
  this.mem = new MemoryFileSystem(files);
@@ -3219,6 +3321,15 @@ var HybridFileSystem = class {
3219
3321
  addFiles(files) {
3220
3322
  this.mem.addFiles(files);
3221
3323
  }
3324
+ /** Virtual path where a package's files live (delegates to the memory layer). */
3325
+ getPackagePath(packageName, version) {
3326
+ return this.mem.getPackagePath(packageName, version);
3327
+ }
3328
+ /** Ingest a `.pdpkg` (ZIP) buffer into the in-memory layer, so package files are
3329
+ * served synchronously alongside the workspace sources. */
3330
+ addPackage(packageName, version, packageBuffer) {
3331
+ return this.mem.addPackage(packageName, version, packageBuffer);
3332
+ }
3222
3333
  exists(filePath) {
3223
3334
  if (this.mem.exists(filePath)) return true;
3224
3335
  return this.backend.readFile(this.normalize(filePath)).then((content) => content !== null).catch(() => false);
@@ -4668,6 +4779,7 @@ exports.LexicalAnalysisStage = LexicalAnalysisStage;
4668
4779
  exports.MEMORY_OPERATIONS_BY_MODE = MEMORY_OPERATIONS_BY_MODE;
4669
4780
  exports.MarkdownFormatter = MarkdownFormatter;
4670
4781
  exports.MemoryFileSystem = MemoryFileSystem;
4782
+ exports.MemoryPackageResolver = MemoryPackageResolver;
4671
4783
  exports.OpenAIFormatter = OpenAIFormatter;
4672
4784
  exports.PACKAGE_TYPE_DIRS = PACKAGE_TYPE_DIRS;
4673
4785
  exports.PROMPD_EXTENSIONS = PROMPD_EXTENSIONS;
@@ -4691,6 +4803,7 @@ exports.createPrompdEnvironment = createPrompdEnvironment;
4691
4803
  exports.createWorkflowNode = createWorkflowNode;
4692
4804
  exports.dirnamePosix = dirnamePosix;
4693
4805
  exports.extname = extname;
4806
+ exports.extractPdpkg = extractPdpkg;
4694
4807
  exports.getContentType = getContentType;
4695
4808
  exports.getExecutionOrder = getExecutionOrder;
4696
4809
  exports.getInstallDirForType = getInstallDirForType;