Skip to content

Instantly share code, notes, and snippets.

@cmidgley
Created December 14, 2021 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmidgley/807b06d5f6e0c2483619d269a53f0803 to your computer and use it in GitHub Desktop.
Save cmidgley/807b06d5f6e0c2483619d269a53f0803 to your computer and use it in GitHub Desktop.
io/system and SES typings
/*### xs.d.ts start ###*/
/*
Types for io/system
*/
declare class Timer {}
declare type TimerCallback = (timer?: Timer) => void;
declare class System {
static restart(): void;
static deepSleep(): void;
static resolve(name: string, callback: (name: string, ip: string) => void);
static setTimeout(callback: TimerCallback, delay?: number): Timer;
static clearTimeout(id: Timer): void;
static setInterval(callback: TimerCallback, delay?: number): Timer;
static clearInterval(id: Timer): void;
}
/*
The following defines SES (Secure EcmaScript), based on the definition from
https://raw.githubusercontent.com/endojs/endo/master/packages/ses/index.d.ts
but modified to include Moddable extensions and adjusted to work as a global
definition.
Modifications are as follows:
- Remove all use of "export"
- See comments inline tagged "MODDABLE"
*/
// It's academically tempting to define a hardened type, but TypeScript doesn't
// strike a good balance in distinguishing "readonly" in the sense that you
// promise not to change vs "readonly" in the sense that you depend on a thing
// not changing.
// type Hardened<T> =
// T extends number | bigint | string | null | undefined | Function ? T :
// { readonly [P in keyof T]: Hardened<T[P]> };
// So Harden just passes the type through without modification.
// This will occasionally conflict with the type of Object.freeze.
// In those cases, we recommend casting the result of Object.freeze to the
// original thawn type, as if the signature of freeze were identical to this
// version of harden.
type Harden = <T>(value: T) => T; // not Hardened<T>;
interface LockdownOptions {
regExpTaming?: "safe" | "unsafe";
localeTaming?: "safe" | "unsafe";
consoleTaming?: "safe" | "unsafe";
errorTrapping?: "platform" | "exit" | "abort" | "report" | "none";
errorTaming?: "safe" | "unsafe";
dateTaming?: "safe" | "unsafe"; // deprecated
mathTaming?: "safe" | "unsafe"; // deprecated
stackFiltering?: "concise" | "verbose";
overrideTaming?: "moderate" | "min" | "severe";
overrideDebug?: Array<string>;
domainTaming?: "safe" | "unsafe";
__allowUnsafeMonkeyPatching__?: "safe" | "unsafe";
}
type Lockdown = (options?: LockdownOptions) => void;
type __LiveExportsMap__ = Record<string, [string, boolean]>;
type __FixedExportsMap__ = Record<string, [string]>;
interface PrecompiledStaticModuleInterface {
imports: Array<string>;
exports: Array<string>;
reexports: Array<string>;
__syncModuleProgram__: string;
__liveExportsMap__: __LiveExportsMap__;
__fixedExportsMap__: __FixedExportsMap__;
}
interface ThirdPartyStaticModuleInterface {
imports: Array<string>;
exports: Array<string>;
execute(proxiedExports: Object, compartment: Compartment, resolvedImports: Record<string, string>): void;
}
type FinalStaticModuleType = PrecompiledStaticModuleInterface | ThirdPartyStaticModuleInterface;
interface RedirectStaticModuleInterface {
record: FinalStaticModuleType;
specifier: string;
}
type StaticModuleType = RedirectStaticModuleInterface | FinalStaticModuleType;
type ModuleExportsNamespace = Record<string, any>;
type Transform = (source: string) => string;
type ResolveHook = (importSpecifier: string, referrerSpecifier: string) => string;
type LoadNowHook = (specifier: string) => StaticModuleRecord; // MODDABLE added
type ModuleMap = Record<string, string | ModuleExportsNamespace>;
type ModuleMapHook = (moduleSpecifier: string) => string | ModuleExportsNamespace | void;
type ImportHook = (moduleSpecifier: string) => Promise<StaticModuleType>;
interface CompartmentOptions {
name?: string;
transforms?: Array<Transform>;
globalLexicals?: Record<string, any>;
moduleMapHook?: ModuleMapHook;
importHook?: ImportHook;
resolveHook?: ResolveHook;
loadNowHook?: LoadNowHook; // MODDABLE added
__shimTransforms__?: Array<Transform>;
}
interface EvaluateOptions {
transforms?: Array<Transform>;
sloppyGlobalsMode?: boolean;
__moduleShimLexicals__?: Record<string, any>;
__evadeHtmlCommentTest__?: boolean;
__rejectSomeDirectEvalExpressions__?: boolean;
}
// The DetailsToken is an empty object literal.
type DetailsToken = Record<any, never>;
type Details = string | DetailsToken;
interface AssertMakeErrorOptions {}
type AssertTypeofBigint = (specimen: any, typeName: "bigint", details?: Details) => asserts specimen is bigint;
type AssertTypeofBoolean = (specimen: any, typeName: "boolean", details?: Details) => asserts specimen is boolean;
type AssertTypeofFunction = (specimen: any, typeName: "function", details?: Details) => asserts specimen is Function;
type AssertTypeofNumber = (specimen: any, typeName: "number", details?: Details) => asserts specimen is number;
type AssertTypeofObject = (
specimen: any,
typeName: "object",
details?: Details
) => asserts specimen is Record<any, any> | null;
type AssertTypeofString = (specimen: any, typeName: "string", details?: Details) => asserts specimen is string;
type AssertTypeofSymbol = (specimen: any, typeName: "symbol", details?: Details) => asserts specimen is symbol;
type AssertTypeofUndefined = (specimen: any, typeName: "undefined", details?: Details) => asserts specimen is undefined;
type AssertTypeof = AssertTypeofBigint &
AssertTypeofBoolean &
AssertTypeofFunction &
AssertTypeofNumber &
AssertTypeofObject &
AssertTypeofString &
AssertTypeofSymbol &
AssertTypeofUndefined;
type Raise = (reason: Error) => void;
type MakeAssert = (raise?: Raise, unredacted?: boolean) => Assert;
interface ToStringable {
toString(): string;
}
interface Assert {
(value: any, details?: Details, errorConstructor?: ErrorConstructor): asserts value;
typeof: AssertTypeof;
error(details?: Details, errorConstructor?: ErrorConstructor): Error;
fail(details?: Details, errorConstructor?: ErrorConstructor): never;
equal(left: any, right: any, details?: Details, errorConstructor?: ErrorConstructor): void;
string(specimen: any, details?: Details): asserts specimen is string;
note(error: Error, details: Details): void;
details(template: TemplateStringsArray | string[], ...args: any): DetailsToken;
quote(payload: any, spaces?: string | number): ToStringable;
makeAssert: MakeAssert;
}
// declare global { // MODDABLE removed
declare var harden: Harden; // MODDABLE added 'declare'
declare var lockdown: Lockdown; // MODDABLE added 'declare'
declare var assert: Assert; // MODDABLE added 'declare'
/**
* Each Compartment constructor is a global. A host that wants to execute
* code in a context bound to a new global creates a new compartment.
*/
// MODDABLE added 'declare'
declare class Compartment {
constructor(globals?: Object, moduleMap?: ModuleMap, options?: CompartmentOptions);
get globalThis(): Record<string, any>;
get name(): string;
evaluate(code: string): any;
import(specifier: string): Promise<{ namespace: ModuleExportsNamespace }>;
load(specifier: string): Promise<void>;
importNow(specifier: string): ModuleExportsNamespace;
module(specifier: string): ModuleExportsNamespace;
}
// MODDABLE class added
declare class StaticModuleRecord {
constructor(options: { archive?: string; source?: string });
}
// } MODDABLE removed
/*### xs.d.ts end ###*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment