Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/fuzzy-hornets-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@endo/compartment-mapper': patch
'@endo/bundle-source': patch
---

Fixes poorly-defined support for asynchronous parsers.
11 changes: 6 additions & 5 deletions packages/bundle-source/src/endo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { defaultParserForLanguage as transformingParserForLanguage } from '@endo
import { defaultParserForLanguage as transparentParserForLanguage } from '@endo/compartment-mapper/import-parsers.js';
import tsBlankSpace from 'ts-blank-space';

/** @import {Language, ParserImplementation} from '@endo/compartment-mapper/node-powers.js' */
/** @import {Language} from '@endo/compartment-mapper/node-powers.js' */
/** @import {AsyncParserImplementation} from '@endo/compartment-mapper' */
/** @import {BundlingKit, BundlingKitIO, BundlingKitOptions, ModuleTransformsLike, ParserForLanguageLike, SourceMapDescriptor} from './types.js' */

const textEncoder = new TextEncoder();
Expand Down Expand Up @@ -189,9 +190,9 @@ export const makeBundlingKit = (
};
}

/** @type {ParserImplementation} */
/** @type {AsyncParserImplementation} */
const mtsParser = {
parse(
async parse(
sourceBytes,
specifier,
moduleLocation,
Expand All @@ -213,9 +214,9 @@ export const makeBundlingKit = (
synchronous: false,
};

/** @type {ParserImplementation} */
/** @type {AsyncParserImplementation} */
const ctsParser = {
parse(
async parse(
sourceBytes,
specifier,
moduleLocation,
Expand Down
37 changes: 23 additions & 14 deletions packages/compartment-mapper/src/import-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
*
* @module
*/

import { asyncTrampoline, syncTrampoline } from '@endo/trampoline';
import { resolve } from './node-module-specifier.js';
import {
attenuateModuleHook,
enforcePolicyByModule,
enforcePackagePolicyByCanonicalName,
} from './policy.js';
import { ATTENUATORS_COMPARTMENT } from './policy-format.js';
import { unpackReadPowers } from './powers.js';
/**
* @import {
* ImportHook,
Expand Down Expand Up @@ -43,18 +51,19 @@
* CanonicalName,
* LocalModuleSource,
* ModuleSourceHook,
* ParseFn,
* AsyncParseFn,
* } from './types.js'
*/

import { asyncTrampoline, syncTrampoline } from '@endo/trampoline';
import { resolve } from './node-module-specifier.js';
import {
attenuateModuleHook,
enforcePolicyByModule,
enforcePackagePolicyByCanonicalName,
} from './policy.js';
import { ATTENUATORS_COMPARTMENT } from './policy-format.js';
import { unpackReadPowers } from './powers.js';
/**
* Type guard that narrows a `ParseFn | AsyncParseFn` to `ParseFn` by checking
* for the `isSyncParser` flag.
*
* @param {ParseFn | AsyncParseFn} parse
* @returns {parse is ParseFn}
*/
const isSyncParseFn = parse => 'isSyncParser' in parse && !!parse.isSyncParser;

// q, as in quote, for quoting strings in error messages.
const { quote: q } = assert;
Expand Down Expand Up @@ -324,8 +333,7 @@ const executeLocalModuleSourceHook = (
* {@link StaticModuleType} for a particular {@link CompartmentDescriptor} (or
* `undefined`).
*
* Supports both {@link SyncChooseModuleDescriptorOperators sync} and
* {@link AsyncChooseModuleDescriptorOperators async} operators.
* Supports both sync and async operators.
*
* Used by both {@link makeImportNowHookMaker} and {@link makeImportHookMaker}.
*
Expand Down Expand Up @@ -839,13 +847,14 @@ export function makeImportNowHookMaker(
}
};

if (!('isSyncParser' in parse)) {
if (!isSyncParseFn(parse)) {
return function impossibleTransformImportNowHook() {
throw new Error(
'Dynamic requires are only possible with synchronous parsers and no asynchronous module transforms in options',
);
};
}
const /** @type {ParseFn} */ syncParse = parse;

const compartmentDescriptor =
compartmentDescriptors[packageLocation] || create(null);
Expand Down Expand Up @@ -924,7 +933,7 @@ export function makeImportNowHookMaker(
},
{
maybeRead: maybeReadNow,
parse,
parse: syncParse,
shouldDeferError,
},
);
Expand Down
3 changes: 1 addition & 2 deletions packages/compartment-mapper/src/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,7 @@ export const link = (
/** @type {ShouldDeferError} */
const shouldDeferError = language => {
if (language && has(parserForLanguage, language)) {
return /** @type {ParserImplementation} */ (parserForLanguage[language])
.heuristicImports;
return parserForLanguage[language].heuristicImports;
} else {
// If language is undefined or there's no parser, the error we could consider deferring is surely related to
// that. Nothing to throw here.
Expand Down
Loading
Loading