Skip to content

Commit

Permalink
feat: get config in server mode (#175)
Browse files Browse the repository at this point in the history
* chore: move test

* feat: read function config
  • Loading branch information
eduardoboucas committed Oct 28, 2022
1 parent 2ab8760 commit ea47d4e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 46 deletions.
54 changes: 54 additions & 0 deletions node/server/server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { join } from 'path'

import getPort from 'get-port'
import fetch from 'node-fetch'
import { test, expect } from 'vitest'

import { fixturesDir } from '../../test/util.js'
import { serve } from '../index.js'

test('Starts a server and serves requests for edge functions', async () => {
const port = await getPort()
const server = await serve({
port,
})
const functionPath = join(fixturesDir, 'serve_test', 'echo_env.ts')

const functions = [
{
name: 'echo_env',
path: functionPath,
},
]
const options = {
getFunctionsConfig: true,
}
const { functionsConfig, graph, success } = await server(
functions,
{
very_secret_secret: 'i love netlify',
},
options,
)
expect(success).toBe(true)

expect(functionsConfig).toEqual([{ path: '/my-function' }])

const graphEntry = graph?.modules.some(
// @ts-expect-error TODO: Module graph is currently not typed
({ kind, mediaType, local }) => kind === 'esm' && mediaType === 'TypeScript' && local === functionPath,
)
expect(graphEntry).toBe(true)

const response = await fetch(`http://0.0.0.0:${port}/foo`, {
headers: {
'x-deno-functions': 'echo_env',
'x-deno-pass': 'passthrough',
'X-NF-Request-ID': 'foo',
},
})
expect(response.status).toBe(200)

const body = (await response.json()) as Record<string, string>
expect(body.very_secret_secret).toBe('i love netlify')
})
30 changes: 26 additions & 4 deletions node/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { tmpName } from 'tmp-promise'

import { DenoBridge, OnAfterDownloadHook, OnBeforeDownloadHook, ProcessRef } from '../bridge.js'
import { getFunctionConfig, FunctionConfig } from '../config.js'
import type { EdgeFunction } from '../edge_function.js'
import { generateStage2 } from '../formats/javascript.js'
import { ImportMap, ImportMapFile } from '../import_map.js'
import { getLogger, LogFunction } from '../logger.js'
import { getLogger, LogFunction, Logger } from '../logger.js'
import { ensureLatestTypes } from '../types.js'

import { killProcess, waitForServer } from './util.js'
Expand All @@ -18,19 +19,31 @@ interface PrepareServerOptions {
flags: string[]
formatExportTypeError?: FormatFunction
formatImportError?: FormatFunction
importMap: ImportMap
logger: Logger
port: number
}

interface StartServerOptions {
getFunctionsConfig?: boolean
}

const prepareServer = ({
deno,
distDirectory,
flags: denoFlags,
formatExportTypeError,
formatImportError,
importMap,
logger,
port,
}: PrepareServerOptions) => {
const processRef: ProcessRef = {}
const startIsolate = async (newFunctions: EdgeFunction[], env: NodeJS.ProcessEnv = {}) => {
const startServer = async (
functions: EdgeFunction[],
env: NodeJS.ProcessEnv = {},
options: StartServerOptions = {},
) => {
if (processRef?.ps !== undefined) {
await killProcess(processRef.ps)
}
Expand All @@ -40,7 +53,7 @@ const prepareServer = ({
const stage2Path = await generateStage2({
distDirectory,
fileName: 'dev.js',
functions: newFunctions,
functions,
formatExportTypeError,
formatImportError,
type: 'local',
Expand Down Expand Up @@ -69,15 +82,22 @@ const prepareServer = ({
extendEnv: false,
})

let functionsConfig: FunctionConfig[] = []

if (options.getFunctionsConfig) {
functionsConfig = await Promise.all(functions.map((func) => getFunctionConfig(func, importMap, deno, logger)))
}

const success = await waitForServer(port, processRef.ps)

return {
functionsConfig,
graph,
success,
}
}

return startIsolate
return startServer
}

interface InspectSettings {
Expand Down Expand Up @@ -170,6 +190,8 @@ const serve = async ({
flags,
formatExportTypeError,
formatImportError,
importMap,
logger,
port,
})

Expand Down
42 changes: 0 additions & 42 deletions node/serving.test.ts

This file was deleted.

6 changes: 6 additions & 0 deletions test/fixtures/serve_test/echo_env.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
import { Config } from 'https://edge.netlify.com'

export default async () => new Response(JSON.stringify(Deno.env.toObject()))

export const config: Config = () => ({
path: '/my-function',
})

0 comments on commit ea47d4e

Please sign in to comment.