Skip to content

Commit

Permalink
feat: export metadata object in stage 2 (#122)
Browse files Browse the repository at this point in the history
* feat: export `metadata` object in stage 2

* refactor: simplify metadata generation

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
eduardoboucas and kodiakhq[bot] committed Sep 14, 2022
1 parent 937bd22 commit 99214c7
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { overrides } = require('@netlify/eslint-config-node')

module.exports = {
extends: '@netlify/eslint-config-node',
ignorePatterns: ['deno/**/*.ts'],
ignorePatterns: ['deno/**/*.ts', 'test/deno/**/*.ts'],
parserOptions: {
sourceType: 'module',
},
Expand All @@ -17,7 +17,7 @@ module.exports = {
overrides: [
...overrides,
{
files: ['test/**/*.ts'],
files: ['test/node/**/*.ts'],
rules: {
'max-statements': 'off',
'no-magic-numbers': 'off',
Expand Down
20 changes: 16 additions & 4 deletions deno/lib/stage2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,28 @@ const getFunctionReference = (basePath: string, func: InputFunction, index: numb
return {
exportLine,
importLine: `import ${importName} from "${url}";`,
metadata: {
url,
},
name: func.name,
}
}

const getStage2Entry = (basePath: string, functions: InputFunction[]) => {
export const getStage2Entry = (basePath: string, functions: InputFunction[]) => {
const lines = functions.map((func, index) => getFunctionReference(basePath, func, index))
const importLines = lines.map(({ importLine }) => importLine).join('\n')
const exportLines = lines.map(({ exportLine }) => exportLine).join(', ')
const exportDeclaration = `export const functions = {${exportLines}};`

return [importLines, exportDeclaration].join('\n\n')
const metadata = lines.reduce(
(acc, { metadata, name }) => ({
...acc,
[name]: metadata,
}),
{},
)
const functionsExport = `export const functions = {${exportLines}};`
const metadataExport = `export const metadata = ${JSON.stringify(metadata)};`

return [importLines, functionsExport, metadataExport].join('\n\n')
}

const getVirtualPath = (basePath: string, filePath: string) => {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"test:dev": "run-s build test:dev:*",
"test:ci": "run-s build test:ci:*",
"test:dev:ava": "ava",
"test:dev:deno": "deno test --allow-all test/deno",
"test:ci:ava": "nyc -r lcovonly -r text -r json ava"
},
"config": {
Expand All @@ -36,8 +37,8 @@
},
"ava": {
"files": [
"test/**/*.ts",
"!test/fixtures/**"
"test/node/**/*.ts",
"!test/node/fixtures/**"
],
"extensions": {
"ts": "module"
Expand All @@ -55,7 +56,7 @@
},
"author": "Netlify Inc.",
"directories": {
"test": "test"
"test": "test/node"
},
"devDependencies": {
"@ava/typescript": "^3.0.1",
Expand Down
57 changes: 57 additions & 0 deletions test/deno/stage2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { assertEquals, assertStringIncludes } from 'https://deno.land/std@0.127.0/testing/asserts.ts'

import { join } from 'https://deno.land/std@0.155.0/path/mod.ts'
import { pathToFileURL } from 'https://deno.land/std@0.155.0/node/url.ts'

import { getStage2Entry } from '../../deno/lib/stage2.ts'
import { virtualRoot } from '../../deno/lib/consts.ts'

Deno.test('`getStage2Entry` returns a valid stage 2 file', async () => {
const directory = await Deno.makeTempDir()
const functions = [
{
name: 'func1',
path: join(directory, 'func1.ts'),
response: 'Hello from function 1',
},
{
name: 'func2',
path: join(directory, 'func2.ts'),
response: 'Hello from function 2',
},
]

for (const func of functions) {
const contents = `export default async () => new Response(${JSON.stringify(func.response)})`

await Deno.writeTextFile(func.path, contents)
}

const baseURL = pathToFileURL(directory)
const stage2 = getStage2Entry(
directory,
functions.map(({ name, path }) => ({ name, path })),
)

// Ensuring that the stage 2 paths have the virtual root before we strip it.
assertStringIncludes(stage2, virtualRoot)

// Replacing the virtual root with the URL of the temporary directory so that
// we can actually import the module.
const normalizedStage2 = stage2.replaceAll(virtualRoot, `${baseURL.href}/`)

const stage2Path = join(directory, 'stage2.ts')

await Deno.writeTextFile(stage2Path, normalizedStage2)

const mod = await import(stage2Path)

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

for (const func of functions) {
const result = await mod.functions[func.name]()

assertEquals(await result.text(), func.response)
assertEquals(mod.metadata[func.name].url, pathToFileURL(func.path).toString())
}
})
2 changes: 1 addition & 1 deletion test/bootstrap.ts → test/node/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { env } from 'process'
import test from 'ava'
import fetch, { Headers } from 'node-fetch'

import { getBootstrapURL } from '../node/formats/javascript.js'
import { getBootstrapURL } from '../../node/formats/javascript.js'

test.serial('Imports the bootstrap layer from a valid URL', async (t) => {
const importURL = new URL(getBootstrapURL())
Expand Down
4 changes: 2 additions & 2 deletions test/bridge.ts → test/node/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import nock from 'nock'
import { spy } from 'sinon'
import tmp, { DirectoryResult } from 'tmp-promise'

import { DenoBridge } from '../node/bridge.js'
import { getPlatformTarget } from '../node/platform.js'
import { DenoBridge } from '../../node/bridge.js'
import { getPlatformTarget } from '../../node/platform.js'

const require = createRequire(import.meta.url)
const archiver = require('archiver')
Expand Down
4 changes: 2 additions & 2 deletions test/bundler.ts → test/node/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { fileURLToPath, pathToFileURL } from 'url'
import test from 'ava'
import tmp from 'tmp-promise'

import { BundleError } from '../node/bundle_error.js'
import { bundle, BundleOptions } from '../node/bundler.js'
import { BundleError } from '../../node/bundle_error.js'
import { bundle, BundleOptions } from '../../node/bundler.js'

const url = new URL(import.meta.url)
const dirname = fileURLToPath(url)
Expand Down
9 changes: 5 additions & 4 deletions test/downloader.ts → test/node/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import { execa } from 'execa'
import nock from 'nock'
import tmp from 'tmp-promise'

import { download } from '../node/downloader.js'
import { getLogger } from '../node/logger.js'
import { getPlatformTarget } from '../node/platform.js'
import { download } from '../../node/downloader.js'
import { getLogger } from '../../node/logger.js'
import { getPlatformTarget } from '../../node/platform.js'

// eslint-disable-next-line @typescript-eslint/no-empty-function
const testLogger = getLogger(() => {})

const streamError = () => {
Expand Down Expand Up @@ -64,7 +65,7 @@ test.serial('tries downloading binary up to 4 times', async (t) => {
.get(zipPath)
// 1 second delay
.delayBody(1000)
.replyWithFile(200, platform === 'win32' ? './test/fixtures/deno.win.zip' : './test/fixtures/deno.zip', {
.replyWithFile(200, platform === 'win32' ? './test/node/fixtures/deno.win.zip' : './test/node/fixtures/deno.zip', {
'Content-Type': 'application/zip',
})

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/import_map.ts → test/node/import_map.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava'

import { ImportMap } from '../node/import_map.js'
import { ImportMap } from '../../node/import_map.js'

test('Handles import maps with full URLs without specifying a base URL', (t) => {
const inputFile1 = {
Expand Down
2 changes: 1 addition & 1 deletion test/logger.ts → test/node/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava'
import { stub } from 'sinon'

import { getLogger } from '../node/logger.js'
import { getLogger } from '../../node/logger.js'

const consoleLog = console.log

Expand Down
4 changes: 2 additions & 2 deletions test/main.ts → test/node/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import nock from 'nock'
import { spy } from 'sinon'
import tmp from 'tmp-promise'

import { DenoBridge } from '../node/bridge.js'
import { getPlatformTarget } from '../node/platform.js'
import { DenoBridge } from '../../node/bridge.js'
import { getPlatformTarget } from '../../node/platform.js'

const require = createRequire(import.meta.url)
const archiver = require('archiver')
Expand Down
2 changes: 1 addition & 1 deletion test/manifest.ts → test/node/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { env } from 'process'

import test from 'ava'

import { generateManifest } from '../node/manifest.js'
import { generateManifest } from '../../node/manifest.js'

test('Generates a manifest with different bundles', (t) => {
const bundle1 = {
Expand Down
6 changes: 3 additions & 3 deletions test/types.ts → test/node/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import nock from 'nock'
import { stub } from 'sinon'
import tmp from 'tmp-promise'

import { DenoBridge } from '../node/bridge.js'
import { getLogger } from '../node/logger.js'
import { ensureLatestTypes } from '../node/types.js'
import { DenoBridge } from '../../node/bridge.js'
import { getLogger } from '../../node/logger.js'
import { ensureLatestTypes } from '../../node/types.js'

const testLogger = getLogger(() => {
// no-op
Expand Down

0 comments on commit 99214c7

Please sign in to comment.