Skip to content

Commit

Permalink
feat: add support for custom layers (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Nov 16, 2022
1 parent d90d756 commit ec3848b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
2 changes: 2 additions & 0 deletions node/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { findFunctions } from './finder.js'
import { bundle as bundleESZIP } from './formats/eszip.js'
import { bundle as bundleJS } from './formats/javascript.js'
import { ImportMap, ImportMapFile } from './import_map.js'
import { Layer } from './layer.js'
import { getLogger, LogFunction } from './logger.js'
import { writeManifest } from './manifest.js'
import { ensureLatestTypes } from './types.js'
Expand All @@ -25,6 +26,7 @@ interface BundleOptions {
distImportMapPath?: string
featureFlags?: FeatureFlags
importMaps?: ImportMapFile[]
layers?: Layer[]
onAfterDownload?: OnAfterDownloadHook
onBeforeDownload?: OnBeforeDownloadHook
systemLogger?: LogFunction
Expand Down
4 changes: 4 additions & 0 deletions node/layer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Layer {
flag: string
name: string
}
29 changes: 29 additions & 0 deletions node/manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,32 @@ test('Generates a manifest with pre and post-cache routes', () => {
expect(manifest.post_cache_routes).toEqual(expectedPostCacheRoutes)
expect(manifest.bundler_version).toBe(env.npm_package_version as string)
})

test('Generates a manifest with layers', () => {
const functions = [
{ name: 'func-1', path: '/path/to/func-1.ts' },
{ name: 'func-2', path: '/path/to/func-2.ts' },
]
const declarations = [
{ function: 'func-1', path: '/f1/*' },
{ function: 'func-2', path: '/f2/*' },
]
const expectedRoutes = [
{ function: 'func-1', pattern: '^/f1/.*/?$' },
{ function: 'func-2', pattern: '^/f2/.*/?$' },
]
const layers = [
{
name: 'onion',
flag: 'edge_functions_onion_layer',
},
]
const manifest1 = generateManifest({ bundles: [], declarations, functions })
const manifest2 = generateManifest({ bundles: [], declarations, functions, layers })

expect(manifest1.routes).toEqual(expectedRoutes)
expect(manifest1.layers).toEqual([])

expect(manifest2.routes).toEqual(expectedRoutes)
expect(manifest2.layers).toEqual(layers)
})
19 changes: 15 additions & 4 deletions node/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import type { Bundle } from './bundle.js'
import { Cache } from './config.js'
import type { Declaration } from './declaration.js'
import { EdgeFunction } from './edge_function.js'
import { Layer } from './layer.js'
import { getPackageVersion } from './package_json.js'
import { nonNullable } from './utils/non_nullable.js'

interface GenerateManifestOptions {
bundles?: Bundle[]
functions: EdgeFunction[]
declarations?: Declaration[]
functions: EdgeFunction[]
layers?: Layer[]
}

/* eslint-disable camelcase */
Expand All @@ -22,6 +24,7 @@ interface Manifest {
bundles: { asset: string; format: string }[]
routes: { function: string; name?: string; pattern: string }[]
post_cache_routes: { function: string; name?: string; pattern: string }[]
layers: { name: string; flag: string }[]
}
/* eslint-enable camelcase */

Expand All @@ -31,7 +34,7 @@ interface Route {
pattern: string
}

const generateManifest = ({ bundles = [], declarations = [], functions }: GenerateManifestOptions) => {
const generateManifest = ({ bundles = [], declarations = [], functions, layers = [] }: GenerateManifestOptions) => {
const preCacheRoutes: Route[] = []
const postCacheRoutes: Route[] = []

Expand Down Expand Up @@ -65,6 +68,7 @@ const generateManifest = ({ bundles = [], declarations = [], functions }: Genera
routes: preCacheRoutes.filter(nonNullable),
post_cache_routes: postCacheRoutes.filter(nonNullable),
bundler_version: getPackageVersion(),
layers,
}

return manifest
Expand Down Expand Up @@ -92,10 +96,17 @@ interface WriteManifestOptions {
declarations: Declaration[]
distDirectory: string
functions: EdgeFunction[]
layers?: Layer[]
}

const writeManifest = async ({ bundles, declarations = [], distDirectory, functions }: WriteManifestOptions) => {
const manifest = generateManifest({ bundles, declarations, functions })
const writeManifest = async ({
bundles,
declarations = [],
distDirectory,
functions,
layers,
}: WriteManifestOptions) => {
const manifest = generateManifest({ bundles, declarations, functions, layers })
const manifestPath = join(distDirectory, 'manifest.json')

await fs.writeFile(manifestPath, JSON.stringify(manifest))
Expand Down

0 comments on commit ec3848b

Please sign in to comment.