Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@
import type { Declaration, Expression, Program } from 'estree';
import type { AstNode } from 'rollup';

export interface BackendFunction {
/** Relative path from project root to the .backend.ts file (without extension) */
relativePath: string;
/** Exported function name */
name: string;
/** Absolute path to the .backend.ts source file */
absolutePath: string;
/** Connection IDs this backend function is allowed to use. */
allowedConnectionIds: string[];
}
import { isProgramNode } from './type-guards';
import type { BackendExport } from './types';

/**
* Extract exported value (non-type) symbols from an ESTree AST.
Expand All @@ -27,11 +19,11 @@ export interface BackendFunction {
* @param ast - AstNode from `this.parse()` in unplugin's transform hook
* @param filePath - Path to the source file (used in error messages)
*/
function isProgramNode(node: AstNode): node is AstNode & Program {
return node.type === 'Program';
export function extractExportedFunctions(ast: AstNode, filePath: string): string[] {
return enumerateBackendExports(ast, filePath).map((backendExport) => backendExport.name);
}

export function extractExportedFunctions(ast: AstNode, filePath: string): string[] {
export function enumerateBackendExports(ast: AstNode, filePath: string): BackendExport[] {
if (!isProgramNode(ast)) {
throw new Error(
`Expected a Program node from this.parse() for ${filePath}, got ${ast.type}`,
Expand All @@ -41,7 +33,7 @@ export function extractExportedFunctions(ast: AstNode, filePath: string): string
// Build a map of top-level declarations so we can validate export specifiers.
const declarations = buildDeclarationMap(ast);

const names: string[] = [];
const backendExports: BackendExport[] = [];
for (const node of ast.body) {
// handles: export default ...
if (node.type === 'ExportDefaultDeclaration') {
Expand All @@ -61,9 +53,16 @@ export function extractExportedFunctions(ast: AstNode, filePath: string): string

// handles: export function add() {} / export const add = ...
if (node.declaration) {
names.push(...namesFromDeclaration(node.declaration, filePath));
backendExports.push(
...namesFromDeclaration(node.declaration, filePath).map((name) => ({
kind: 'local' as const,
name,
localName: name,
})),
);
}

const source = typeof node.source?.value === 'string' ? node.source.value : null;
for (const spec of node.specifiers) {
if (spec.exported.type !== 'Identifier') {
continue;
Expand All @@ -77,14 +76,37 @@ export function extractExportedFunctions(ast: AstNode, filePath: string): string
// Validate specifier binding is callable when we can resolve it.
// e.g. `const VERSION = '1.0'; export { VERSION };` — rejected
// e.g. `function add() {}; export { add };` — allowed
const localName =
spec.local.type === 'Identifier'
? spec.local.name
: typeof spec.local.value === 'string'
? spec.local.value
: null;
if (!localName) {
continue;
}

if (source) {
backendExports.push({
kind: 're-export',
name: spec.exported.name,
localName,
source,
});
continue;
}

if (spec.local.type === 'Identifier') {
validateSpecifierBinding(spec.local.name, declarations, filePath);
validateSpecifierBinding(localName, declarations, filePath);
}
// handles: export { add, multiply }
names.push(spec.exported.name);
backendExports.push({
kind: 'local',
name: spec.exported.name,
localName,
});
}
}
return names;
return backendExports;
}

/** Init types that are definitively non-callable at runtime. */
Expand Down
10 changes: 10 additions & 0 deletions packages/plugins/apps/src/backend/ast-parsing/type-guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import type { Program } from 'estree';
import type { AstNode } from 'rollup';

export function isProgramNode(node: AstNode): node is AstNode & Program {
return node.type === 'Program';
}
18 changes: 18 additions & 0 deletions packages/plugins/apps/src/backend/ast-parsing/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

export type BackendExport = BackendLocalExport | BackendReExport;

export interface BackendLocalExport {
kind: 'local';
name: string;
localName: string;
}

export interface BackendReExport {
kind: 're-export';
name: string;
localName: string;
source: string;
}
2 changes: 1 addition & 1 deletion packages/plugins/apps/src/backend/discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import { extractExportedFunctions } from '@dd/apps-plugin/backend/discovery';
import { extractExportedFunctions } from '@dd/apps-plugin/backend/ast-parsing/extract-backend-functions';
import type { Program } from 'estree';
import type { AstNode } from 'rollup';

Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/apps/src/backend/encodeQueryName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { createHash } from 'crypto';
import path from 'path';

import type { BackendFunction } from './discovery';
import type { BackendFunction } from './types';

/**
* Encode a BackendFunction into an opaque query name string.
Expand Down
14 changes: 14 additions & 0 deletions packages/plugins/apps/src/backend/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

export interface BackendFunction {
/** Relative path from project root to the .backend.ts file (without extension) */
relativePath: string;
/** Exported function name */
name: string;
/** Absolute path to the .backend.ts source file */
absolutePath: string;
/** Connection IDs this backend function is allowed to use. */
allowedConnectionIds: string[];
}
4 changes: 2 additions & 2 deletions packages/plugins/apps/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import path from 'path';
import { createArchive } from './archive';
import type { Asset } from './assets';
import { collectAssets } from './assets';
import type { BackendFunction } from './backend/discovery';
import { extractExportedFunctions } from './backend/discovery';
import { extractExportedFunctions } from './backend/ast-parsing/extract-backend-functions';
import { encodeQueryName } from './backend/encodeQueryName';
import { generateProxyModule } from './backend/proxy-codegen';
import type { BackendFunction } from './backend/types';
import { BACKEND_FILE_RE, CONFIG_KEY, PLUGIN_NAME } from './constants';
import { resolveIdentifier } from './identifier';
import type { AppsManifest, AppsOptions } from './types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { tmpdir } from 'os';
import path from 'path';
import type { build } from 'vite';

import type { BackendFunction } from '../backend/discovery';
import { encodeQueryName } from '../backend/encodeQueryName';
import type { BackendFunction } from '../backend/types';
import { generateVirtualEntryContent } from '../backend/virtual-entry';

import { getBaseBackendBuildConfig } from './build-config';
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/apps/src/vite/dev-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { EventEmitter } from 'events';
import type { IncomingMessage, ServerResponse } from 'http';
import nock from 'nock';

import type { BackendFunction } from '../backend/discovery';
import { encodeQueryName } from '../backend/encodeQueryName';
import type { BackendFunction } from '../backend/types';

const mockViteBuild = jest.fn();

Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/apps/src/vite/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { randomUUID } from 'crypto';
import type { IncomingMessage, ServerResponse } from 'http';
import type { build } from 'vite';

import type { BackendFunction } from '../backend/discovery';
import { encodeQueryName } from '../backend/encodeQueryName';
import type { ExecuteActionRequest, ExecuteActionResponse } from '../backend/protocol';
import type { BackendFunction } from '../backend/types';
import { generateDevVirtualEntryContent } from '../backend/virtual-entry';

import { getBaseBackendBuildConfig } from './build-config';
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/apps/src/vite/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import { getVitePlugin } from '@dd/apps-plugin/vite/index';
import { getMockLogger } from '@dd/tests/_jest/helpers/mocks';

import type { BackendFunction } from '../backend/discovery';
import { encodeQueryName } from '../backend/encodeQueryName';
import type { BackendFunction } from '../backend/types';

const log = getMockLogger();

Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/apps/src/vite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { rm } from '@dd/core/helpers/fs';
import type { AuthOptionsWithDefaults, Logger, PluginOptions } from '@dd/core/types';
import type { build } from 'vite';

import type { BackendFunction } from '../backend/discovery';
import type { BackendFunction } from '../backend/types';

import { buildBackendFunctions } from './build-backend-functions';
import { createDevServerMiddleware } from './dev-server';
Expand Down
Loading