sandboxedjs 0.1.46 → 0.1.47

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.d.cts CHANGED
@@ -1214,11 +1214,28 @@ declare class CleanPackageInstaller implements RuntimePackageInstaller {
1214
1214
  private readonly fetcher;
1215
1215
  private readonly metadata;
1216
1216
  private readonly tarballs;
1217
+ /** Single-version manifests, for the fields the packument omits. */
1218
+ private readonly details;
1217
1219
  constructor(volume: RuntimeVolume, options?: CleanInstallerOptions);
1218
1220
  forCwd(cwd: string): CleanPackageInstaller;
1219
1221
  install(name: string, version?: string, options?: InstallOptions): Promise<RegistryManifest>;
1220
1222
  installFromManifest(path: string, options?: InstallOptions): Promise<void>;
1221
1223
  private installAt;
1224
+ /**
1225
+ * The `libc` and `main` fields, which the abbreviated packument leaves out.
1226
+ *
1227
+ * Fetched per version rather than as a full packument: the single-version
1228
+ * document is a few kilobytes, while the full one for a popular package runs
1229
+ * to megabytes. Only packages that already declare `os` or `cpu` ask for it,
1230
+ * so an ordinary install of pure-JavaScript dependencies makes no extra
1231
+ * requests at all — it is the prebuilt binaries, a handful per project, that
1232
+ * need the answer.
1233
+ *
1234
+ * A failure here is not fatal. Being unable to read `libc` puts the check
1235
+ * back where it was before this existed, which is worth strictly less than
1236
+ * being right and strictly more than refusing to install.
1237
+ */
1238
+ private platformDetail;
1222
1239
  private getMetadata;
1223
1240
  private getTarball;
1224
1241
  private createBinLinks;
package/dist/index.d.ts CHANGED
@@ -1214,11 +1214,28 @@ declare class CleanPackageInstaller implements RuntimePackageInstaller {
1214
1214
  private readonly fetcher;
1215
1215
  private readonly metadata;
1216
1216
  private readonly tarballs;
1217
+ /** Single-version manifests, for the fields the packument omits. */
1218
+ private readonly details;
1217
1219
  constructor(volume: RuntimeVolume, options?: CleanInstallerOptions);
1218
1220
  forCwd(cwd: string): CleanPackageInstaller;
1219
1221
  install(name: string, version?: string, options?: InstallOptions): Promise<RegistryManifest>;
1220
1222
  installFromManifest(path: string, options?: InstallOptions): Promise<void>;
1221
1223
  private installAt;
1224
+ /**
1225
+ * The `libc` and `main` fields, which the abbreviated packument leaves out.
1226
+ *
1227
+ * Fetched per version rather than as a full packument: the single-version
1228
+ * document is a few kilobytes, while the full one for a popular package runs
1229
+ * to megabytes. Only packages that already declare `os` or `cpu` ask for it,
1230
+ * so an ordinary install of pure-JavaScript dependencies makes no extra
1231
+ * requests at all — it is the prebuilt binaries, a handful per project, that
1232
+ * need the answer.
1233
+ *
1234
+ * A failure here is not fatal. Being unable to read `libc` puts the check
1235
+ * back where it was before this existed, which is worth strictly less than
1236
+ * being right and strictly more than refusing to install.
1237
+ */
1238
+ private platformDetail;
1222
1239
  private getMetadata;
1223
1240
  private getTarball;
1224
1241
  private createBinLinks;
package/dist/index.js CHANGED
@@ -20301,6 +20301,8 @@ var CleanPackageInstaller = class _CleanPackageInstaller {
20301
20301
  fetcher;
20302
20302
  metadata = /* @__PURE__ */ new Map();
20303
20303
  tarballs = /* @__PURE__ */ new Map();
20304
+ /** Single-version manifests, for the fields the packument omits. */
20305
+ details = /* @__PURE__ */ new Map();
20304
20306
  forCwd(cwd) {
20305
20307
  return new _CleanPackageInstaller(this.volume, { ...this.options, cwd });
20306
20308
  }
@@ -20326,10 +20328,17 @@ var CleanPackageInstaller = class _CleanPackageInstaller {
20326
20328
  const version = resolveVersion(metadata, range);
20327
20329
  const manifest = metadata.versions[version];
20328
20330
  if (!manifest) throw new Error(`No matching version found for ${name}@${range}`);
20329
- if (!supportsPlatform(manifest)) {
20330
- throw new Error(`${name}@${version} is not compatible with linux/x64/glibc`);
20331
- }
20332
20331
  const identity = `${name}@${version}`;
20332
+ if (!supportsPlatform(manifest)) throw new IncompatiblePlatform(identity, "");
20333
+ if (manifest.os?.length || manifest.cpu?.length) {
20334
+ const detail = await this.platformDetail(name, version);
20335
+ if (!platformListAllows(detail.libc, PLATFORM.libc)) {
20336
+ throw new IncompatiblePlatform(identity, ` (needs ${detail.libc?.join(", ")})`);
20337
+ }
20338
+ if (detail.main?.endsWith(".node")) {
20339
+ throw new IncompatiblePlatform(identity, " (native addon; this runtime cannot dlopen)");
20340
+ }
20341
+ }
20333
20342
  const target = join(modulesRoot, name);
20334
20343
  const installed2 = this.tryReadJson(join(target, "package.json"));
20335
20344
  if (installed2?.version === version) return installed2;
@@ -20351,11 +20360,44 @@ var CleanPackageInstaller = class _CleanPackageInstaller {
20351
20360
  try {
20352
20361
  await this.installAt(dependency, dependencyRange, childRoot, options, nextAncestry);
20353
20362
  } catch (error) {
20363
+ if (error instanceof IncompatiblePlatform) continue;
20354
20364
  options.onProgress?.(`Skipped optional ${dependency}: ${error instanceof Error ? error.message : String(error)}`);
20355
20365
  }
20356
20366
  }
20357
20367
  return manifest;
20358
20368
  }
20369
+ /**
20370
+ * The `libc` and `main` fields, which the abbreviated packument leaves out.
20371
+ *
20372
+ * Fetched per version rather than as a full packument: the single-version
20373
+ * document is a few kilobytes, while the full one for a popular package runs
20374
+ * to megabytes. Only packages that already declare `os` or `cpu` ask for it,
20375
+ * so an ordinary install of pure-JavaScript dependencies makes no extra
20376
+ * requests at all — it is the prebuilt binaries, a handful per project, that
20377
+ * need the answer.
20378
+ *
20379
+ * A failure here is not fatal. Being unable to read `libc` puts the check
20380
+ * back where it was before this existed, which is worth strictly less than
20381
+ * being right and strictly more than refusing to install.
20382
+ */
20383
+ async platformDetail(name, version) {
20384
+ const key = `${name}@${version}`;
20385
+ let request = this.details.get(key);
20386
+ if (!request) {
20387
+ request = (async () => {
20388
+ try {
20389
+ const encoded = name.startsWith("@") ? name.replace("/", "%2F") : encodeURIComponent(name);
20390
+ const response = await this.fetcher(`${this.registry}/${encoded}/${version}`);
20391
+ if (!response.ok) return {};
20392
+ return await response.json();
20393
+ } catch {
20394
+ return {};
20395
+ }
20396
+ })();
20397
+ this.details.set(key, request);
20398
+ }
20399
+ return request;
20400
+ }
20359
20401
  async getMetadata(name) {
20360
20402
  let request = this.metadata.get(name);
20361
20403
  if (!request) {
@@ -20423,8 +20465,15 @@ var CleanPackageInstaller = class _CleanPackageInstaller {
20423
20465
  }
20424
20466
  }
20425
20467
  };
20468
+ var PLATFORM = { os: "linux", cpu: "x64", libc: "glibc" };
20469
+ var IncompatiblePlatform = class extends Error {
20470
+ constructor(identity, detail) {
20471
+ super(`${identity} is not compatible with ${PLATFORM.os}/${PLATFORM.cpu}/${PLATFORM.libc}${detail}`);
20472
+ this.name = "IncompatiblePlatform";
20473
+ }
20474
+ };
20426
20475
  function supportsPlatform(manifest) {
20427
- return !manifest.main?.endsWith(".node") && platformListAllows(manifest.os, "linux") && platformListAllows(manifest.cpu, "x64") && platformListAllows(manifest.libc, "glibc");
20476
+ return platformListAllows(manifest.os, PLATFORM.os) && platformListAllows(manifest.cpu, PLATFORM.cpu) && platformListAllows(manifest.libc, PLATFORM.libc);
20428
20477
  }
20429
20478
  function platformListAllows(values, current) {
20430
20479
  if (!values?.length) return true;