-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c86a79
commit 0e367b6
Showing
12 changed files
with
249 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { promises as fs } from 'fs' | ||
import { join, relative } from 'path' | ||
|
||
import { findHandlers } from './finder.js' | ||
import { Handler } from './handler.js' | ||
import { getStringHash } from './utils/sha256.js' | ||
|
||
interface HandlerLine { | ||
exportLine: string | ||
importLine: string | ||
} | ||
|
||
const bundle = async (sourceDirectories: string[], distDirectory: string) => { | ||
await fs.mkdir(distDirectory, { recursive: true }) | ||
|
||
const handlers = await findHandlers(sourceDirectories) | ||
const lines = handlers.map((handler, index) => generateHandlerReference(handler, index, distDirectory)) | ||
const bundleContents = generateBundle(lines) | ||
const hash = await getStringHash(bundleContents) | ||
const preBundlePath = join(distDirectory, `${hash}-pre.ts`) | ||
const bundlePath = join(distDirectory, `${hash}.ts`) | ||
|
||
await fs.writeFile(preBundlePath, bundleContents) | ||
|
||
return { handlers, preBundlePath, bundlePath } | ||
} | ||
|
||
const generateBundle = (lines: HandlerLine[]) => { | ||
const importLines = lines.map(({ importLine }) => importLine).join('\n') | ||
const exportLines = lines.map(({ exportLine }) => exportLine).join(', ') | ||
const exportDeclaration = `const handlers = {${exportLines}};` | ||
|
||
return `${importLines}\n\n${exportDeclaration}\n\nexport default handlers;` | ||
} | ||
|
||
const generateHandlerReference = (handler: Handler, index: number, targetDirectory: string): HandlerLine => { | ||
const importName = `handler${index}` | ||
const exportLine = `"${handler.name}": ${importName}.handler` | ||
const relativePath = relative(targetDirectory, handler.path) | ||
|
||
return { | ||
exportLine, | ||
importLine: `import * as ${importName} from "${relativePath}";`, | ||
} | ||
} | ||
|
||
export { bundle } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
interface DeclarationWithPath { | ||
handler: string | ||
path: string | ||
} | ||
|
||
interface DeclarationWithPattern { | ||
handler: string | ||
pattern: string | ||
} | ||
|
||
type Declaration = DeclarationWithPath | DeclarationWithPattern | ||
|
||
export { Declaration, DeclarationWithPath, DeclarationWithPattern } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { promises as fs } from 'fs' | ||
import { basename, extname, join } from 'path' | ||
|
||
import { Handler } from './handler.js' | ||
import { nonNullable } from './utils/non_nullable.js' | ||
|
||
const ALLOWED_EXTENSIONS = new Set(['.js', '.ts']) | ||
|
||
const findHandlerInDirectory = async (directory: string): Promise<Handler | undefined> => { | ||
const name = basename(directory) | ||
const candidatePaths = [`${name}.js`, `index.js`, `${name}.ts`, `index.ts`].map((filename) => | ||
join(directory, filename), | ||
) | ||
|
||
let handlerPath | ||
|
||
for (const candidatePath of candidatePaths) { | ||
try { | ||
const stats = await fs.stat(candidatePath) | ||
|
||
// eslint-disable-next-line max-depth | ||
if (stats.isFile()) { | ||
handlerPath = candidatePath | ||
|
||
break | ||
} | ||
} catch { | ||
// no-op | ||
} | ||
} | ||
|
||
if (handlerPath === undefined) { | ||
return | ||
} | ||
|
||
return { | ||
name, | ||
path: handlerPath, | ||
} | ||
} | ||
|
||
const findHandlerInPath = async (path: string): Promise<Handler | undefined> => { | ||
const stats = await fs.stat(path) | ||
|
||
if (stats.isDirectory()) { | ||
return findHandlerInDirectory(path) | ||
} | ||
|
||
const extension = extname(path) | ||
|
||
if (ALLOWED_EXTENSIONS.has(extension)) { | ||
return { name: basename(path, extension), path } | ||
} | ||
} | ||
|
||
const findHandlersInDirectory = async (baseDirectory: string) => { | ||
let items: string[] = [] | ||
|
||
try { | ||
items = await fs.readdir(baseDirectory) | ||
} catch { | ||
// no-op | ||
} | ||
|
||
const handlers = await Promise.all(items.map((item) => findHandlerInPath(join(baseDirectory, item)))) | ||
|
||
return handlers.filter(nonNullable) | ||
} | ||
|
||
const findHandlers = async (directories: string[]) => { | ||
const handlers = await Promise.all(directories.map(findHandlersInDirectory)) | ||
|
||
return handlers.flat() | ||
} | ||
|
||
export { findHandlers } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface Handler { | ||
name: string | ||
path: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { bundle } from './bundler.js' | ||
export { DenoBridge } from './bridge.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import globToRegExp from 'glob-to-regexp' | ||
|
||
import type { Declaration } from './declaration.js' | ||
import { Handler } from './handler.js' | ||
import { nonNullable } from './utils/non_nullable.js' | ||
|
||
const generateManifest = (bundlePath: string, handlers: Handler[], declarations: Declaration[]) => { | ||
const handlersWithRoutes = handlers.map((handler) => { | ||
const declaration = declarations.find((candidate) => candidate.handler === handler.name) | ||
|
||
if (declaration === undefined) { | ||
return | ||
} | ||
|
||
const pattern = 'pattern' in declaration ? new RegExp(declaration.pattern) : globToRegExp(declaration.path) | ||
|
||
return { | ||
handler: handler.name, | ||
pattern: pattern.toString(), | ||
} | ||
}) | ||
const manifest = { | ||
bundle: bundlePath, | ||
handlers: handlersWithRoutes.filter(nonNullable), | ||
} | ||
|
||
return manifest | ||
} | ||
|
||
export { generateManifest } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const nonNullable = <T>(value: T): value is NonNullable<T> => value !== null && value !== undefined | ||
|
||
export { nonNullable } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import crypto from 'crypto' | ||
|
||
const getStringHash = (input: string) => { | ||
const shasum = crypto.createHash('sha256') | ||
|
||
shasum.update(input) | ||
|
||
const hash = shasum.digest('hex') | ||
|
||
return hash | ||
} | ||
|
||
export { getStringHash } |
Oops, something went wrong.