Skip to content

Commit

Permalink
feat: add layer: prefix to ESZIP loader (#201)
Browse files Browse the repository at this point in the history
* feat: add `layer:` prefix to ESZIP loader

* feat: add layer support to ESZIP loader

* refactor: ensure ESZIP path exists

Co-authored-by: Karin Hendrikse <30577427+khendrikse@users.noreply.github.com>
  • Loading branch information
eduardoboucas and khendrikse committed Nov 17, 2022
1 parent a314063 commit 4d0e8dd
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
9 changes: 5 additions & 4 deletions deno/lib/consts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const PUBLIC_SPECIFIER = "netlify:edge";
export const STAGE1_SPECIFIER = "netlify:bootstrap-stage1";
export const STAGE2_SPECIFIER = "netlify:bootstrap-stage2";
export const virtualRoot = "file:///root/";
export const CUSTOM_LAYER_PREFIX = 'layer:'
export const PUBLIC_SPECIFIER = 'netlify:edge'
export const STAGE1_SPECIFIER = 'netlify:bootstrap-stage1'
export const STAGE2_SPECIFIER = 'netlify:bootstrap-stage2'
export const virtualRoot = 'file:///root/'
7 changes: 5 additions & 2 deletions deno/lib/stage2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { build, LoadResponse } from 'https://deno.land/x/eszip@v0.28.0/mod.ts'
import * as path from 'https://deno.land/std@0.127.0/path/mod.ts'

import type { InputFunction, WriteStage2Options } from '../../shared/stage2.ts'
import { PUBLIC_SPECIFIER, STAGE2_SPECIFIER, virtualRoot } from './consts.ts'
import { CUSTOM_LAYER_PREFIX, PUBLIC_SPECIFIER, STAGE2_SPECIFIER, virtualRoot } from './consts.ts'
import { inlineModule, loadFromVirtualRoot, loadWithRetry } from './common.ts'

interface FunctionReference {
Expand Down Expand Up @@ -70,7 +70,7 @@ const stage2Loader = (basePath: string, functions: InputFunction[]) => {
return inlineModule(specifier, stage2Entry)
}

if (specifier === PUBLIC_SPECIFIER) {
if (specifier === PUBLIC_SPECIFIER || specifier.startsWith(CUSTOM_LAYER_PREFIX)) {
return {
kind: 'external',
specifier,
Expand All @@ -88,6 +88,9 @@ const stage2Loader = (basePath: string, functions: InputFunction[]) => {
const writeStage2 = async ({ basePath, destPath, functions, importMapURL }: WriteStage2Options) => {
const loader = stage2Loader(basePath, functions)
const bytes = await build([STAGE2_SPECIFIER], loader, importMapURL)
const directory = path.dirname(destPath)

await Deno.mkdir(directory, { recursive: true })

return await Deno.writeFile(destPath, bytes)
}
Expand Down
35 changes: 35 additions & 0 deletions node/bundler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,38 @@ test('Ignores any user-defined `deno.json` files', async () => {

await deleteAsync([tmpDir.path, denoConfigPath, importMapFile.path], { force: true })
})

test('Processes a function that imports a custom layer', async () => {
const sourceDirectory = resolve(fixturesDir, 'with_layers', 'functions')
const tmpDir = await tmp.dir()
const declarations = [
{
function: 'func1',
path: '/func1',
},
]
const layer = { name: 'test', flag: 'edge-functions-layer-test' }
const result = await bundle([sourceDirectory], tmpDir.path, declarations, {
basePath: fixturesDir,
featureFlags: {
edge_functions_produce_eszip: true,
},
layers: [layer],
})
const generatedFiles = await fs.readdir(tmpDir.path)

expect(result.functions.length).toBe(1)
expect(generatedFiles.length).toBe(2)

const manifestFile = await fs.readFile(resolve(tmpDir.path, 'manifest.json'), 'utf8')
const manifest = JSON.parse(manifestFile)
const { bundles, layers } = manifest

expect(bundles.length).toBe(1)
expect(bundles[0].format).toBe('eszip2')
expect(generatedFiles.includes(bundles[0].asset)).toBe(true)

expect(layers).toEqual([layer])

await fs.rmdir(tmpDir.path, { recursive: true })
})
2 changes: 2 additions & 0 deletions node/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const bundle = async (
distImportMapPath,
featureFlags: inputFeatureFlags,
importMaps,
layers,
onAfterDownload,
onBeforeDownload,
systemLogger,
Expand Down Expand Up @@ -162,6 +163,7 @@ const bundle = async (
declarations,
distDirectory,
functions,
layers,
})

if (distImportMapPath) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/with_layers/functions/func1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { handleRequest } from 'layer:test'

export default (req: Request) => handleRequest(req)

0 comments on commit 4d0e8dd

Please sign in to comment.