Repository navigation
Expand file tree
/
Copy pathextension.ts
More file actions
94 lines (80 loc) · 4.64 KB
/
Copy pathextension.ts
File metadata and controls
94 lines (80 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
export * as Extension from "./extension.js"
import type { Effect } from "effect"
/**
* Host values a program reaches directly as globals: functions, or objects of functions such as `fs`. Values crossing
* in either direction are converted, never shared: arguments come in as copies, results go out as copies, and a
* function inside a result is callable the same way. When the program passes such a function back, it arrives as
* itself, so `Bun.write(dest, Bun.file(src))` can tell what it was given; a global never crosses back, and is left out
* like the program's own functions. Extension calls are not tool calls. A global may return a value, a Promise, or an
* Effect, and an Effect's requirements join the runtime's environment.
*
* A global is synchronous unless it is an `async` function or is marked with `async`. A synchronous global that
* returns an Effect blocks the program until the Effect completes and returns its value, as `fs.readFileSync` does;
* an asynchronous one hands the program a promise. The `extension.before` hook
* runs before the call, so a denied call never reaches the host; it fails the way the global would, by rejecting the
* promise of an asynchronous global and by throwing from a synchronous one. Functions inside a global object are
* called the same way, named by their path (`fs.readFile`).
*
* Programs can also `import` or `require` a global by its Node module name; see `runtime/modules.ts`.
*/
export type Extension<Globals extends Readonly<Record<string, unknown>> = Readonly<Record<string, unknown>>> = {
readonly name: string
readonly globals: Globals
/** Module names that load one of these globals, as `{ bun: "Bun" }` makes `import { file } from "bun"` work. */
readonly modules?: Readonly<Record<string, keyof Globals & string>> | undefined
/** How a suspended run saves the values this extension's functions returned; see `Snapshot`. */
readonly snapshot?: Snapshot | undefined
}
/**
* Lets a suspended run keep the functions inside values this extension's functions returned (a response's `text`,
* say). `encode` gets such a value, as the host function returned it, and gives plain data: JSON values and
* `Uint8Array`s. On resume, `decode` gets that data back, once per value, and gives a value of the same shape, whose
* functions the program's copies call from then on. Without it, those functions are lost on resume: calling one
* throws. A suspend fails, and the run goes on, when `encode` throws or gives anything else.
*/
export type Snapshot = {
readonly encode: (value: unknown) => unknown
readonly decode: (data: unknown) => unknown
}
/** The Effect environment required by an extension's Effect-returning functions, at any depth (`fs.promises.readFile`). */
export type Services<E> = E extends { readonly globals: infer Globals } ? GlobalServices<Globals[keyof Globals]> : never
type GlobalServices<Global> = Global extends (...args: never) => unknown
? Requirements<Global>
: Global extends object
? GlobalServices<Global[keyof Global]>
: never
type Requirements<F> = F extends (...args: never) => infer Result ? Effect.Services<Result> : never
export const make = <const Globals extends Readonly<Record<string, unknown>>>(
options: Extension<Globals>,
): Extension<Globals> => {
for (const [name, value] of Object.entries(options.globals)) {
if (typeof value !== "function" && (typeof value !== "object" || value === null)) {
throw new TypeError(`Extension "${options.name}" global "${name}" must be a function or an object.`)
}
}
return {
name: options.name,
globals: { ...options.globals },
...(options.modules === undefined ? {} : { modules: { ...options.modules } }),
...(options.snapshot === undefined ? {} : { snapshot: options.snapshot }),
}
}
const asyncGlobals = new WeakSet<Function>()
export { StreamingResponse } from "psychoscript/effect"
const AsyncFunction = (async () => {}).constructor
/** Marks a global that returns a Promise or an Effect, so a denied call rejects its promise instead of throwing. */
export const async = <F extends Function>(global: F): F => {
asyncGlobals.add(global)
return global
}
export const isAsync = (global: Function) => global instanceof AsyncFunction || asyncGlobals.has(global)
const constructibleGlobals = new WeakSet<Function>()
/**
* Lets a program call a function with `new`, as it would a class such as `Bun.Glob`. `new` calls it the same way a
* plain call does, and the program gets what it returns.
*/
export const constructible = <F extends Function>(global: F): F => {
constructibleGlobals.add(global)
return global
}
export const isConstructible = (global: Function) => constructibleGlobals.has(global)