directus

Форк
0
48 строк · 1.6 Кб
1
import type { ExtensionSandboxRequestedScopes } from '@directus/extensions';
2
import { numberGenerator } from '@directus/utils';
3
import type { Isolate, Module } from 'isolated-vm';
4
import { generateHostFunctionReference } from '../generate-host-function-reference.js';
5
import { getSdk } from './sdk.js';
6
import { wrap } from './utils/wrap.js';
7

8
/**
9
 * Creates a new isolate context, generates the sandbox SDK, and returns an isolate Module with the
10
 * SDK included in it's global scope
11
 *
12
 * @param isolate - Existing isolate in which to inject the SDK globally
13
 * @param requestedScopes - Permissions as requested by the extension to generate the SDK for
14
 * @returns Isolate module with SDK available in it's global scope
15
 */
16
export async function instantiateSandboxSdk(
17
	isolate: Isolate,
18
	requestedScopes: ExtensionSandboxRequestedScopes,
19
): Promise<Module> {
20
	const apiContext = await isolate.createContext();
21

22
	await apiContext.global.set('sdk', apiContext.global.derefInto());
23

24
	const index = numberGenerator();
25
	const sdk = getSdk();
26

27
	const handlerCode = sdk
28
		.map(({ name, args, async }) => `sdk.${name} = ${generateHostFunctionReference(index, args, { async })}`)
29
		.join('\n');
30

31
	await apiContext.evalClosure(
32
		handlerCode,
33
		sdk.map(({ name, generator, async }) =>
34
			async ? wrap(name, generator(requestedScopes)) : generator(requestedScopes),
35
		),
36
		{ filename: '<extensions-sdk>', arguments: { reference: true } },
37
	);
38

39
	const exportCode = sdk.map(({ name }) => `export const ${name} = sdk.${name};`).join('\n');
40

41
	const apiModule = await isolate.compileModule(exportCode);
42

43
	await apiModule.instantiate(apiContext, () => {
44
		throw new Error();
45
	});
46

47
	return apiModule;
48
}
49

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.