Skip to content

Commit

Permalink
Merge pull request #341 from xylabs/feature/config-glob-path-handling
Browse files Browse the repository at this point in the history
Config Parsing
  • Loading branch information
JoelBCarter authored Oct 16, 2023
2 parents a6ab81e + 60a540b commit 037fbd3
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 106 deletions.
8 changes: 7 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/src/bin/start-meta.ts",
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"args": [
"ts-node-script",
"${workspaceFolder}/src/bin/start-meta.ts"
],
"outFiles": [
"${workspaceFolder}/dist/**/*.*",
],
"env": {
"NODE_OPTIONS": "-r ts-node/register --loader ts-node/esm --max-old-space-size=8192"
},
Expand Down
1 change: 1 addition & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ words:
- pako
- printenv
- proxied
- regexes
- repost
- sandboxing
- snyk
Expand Down
9 changes: 9 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"env": {
"NODE_ENV": "development"
},
"exec": "ts-node-script ./src/bin/start-meta.ts",
"ext": ".ts",
"ignore": ["**/*.spec.ts"],
"watch": ["./src"]
}
28 changes: 16 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
"url": "https://github.com/xylabs/sdk-meta-server-nodejs/issues"
},
"bin": {
"docker-build": "dist/cjs/bin/docker-build.js",
"generate-dotenv": "dist/cjs/bin/generate-dotenv.js",
"start-meta": "dist/cjs/bin/start-meta.js"
"docker-build": "dist/node/bin/docker-build.js",
"generate-dotenv": "dist/node/bin/generate-dotenv.js",
"start-meta": "dist/node/bin/start-meta.js"
},
"dependencies": {
"@aws-sdk/client-secrets-manager": "^3.428.0",
"@xylabs/assert": "^2.12.24",
"@xylabs/delay": "^2.12.24",
"@xylabs/exists": "^2.12.24",
"@xylabs/forget": "^2.12.24",
"@xylabs/lodash": "^2.12.24",
"@xylabs/sdk-api-express-ecs": "^1.4.0",
"@xyo-network/account": "^2.76.0",
"@xyo-network/archivist-model": "^2.76.0",
Expand All @@ -36,6 +38,7 @@
"lodash": "^4.17.21",
"lru-cache": "^10.0.1",
"mime": "^3.0.0",
"minimatch": "^9.0.3",
"node-cache": "^5.1.2",
"puppeteer": "^21.3.8",
"rollbar": "^2.26.2",
Expand All @@ -51,18 +54,17 @@
"@types/express": "^4.17.19",
"@types/jest": "^29.5.5",
"@types/jest-image-snapshot": "^6.2.1",
"@types/lodash": "^4.14.199",
"@types/node": "^20.8.5",
"@types/serve-static": "^1.15.3",
"@types/supertest": "^2.0.14",
"@xylabs/eslint-config": "^3.1.9",
"@xylabs/eslint-config-react": "^3.1.9",
"@xylabs/jest-helpers": "^2.12.24",
"@xylabs/ts-scripts-yarn3": "^3.1.9",
"@xylabs/tsconfig": "^3.1.9",
"@xylabs/tsconfig-dom": "^3.1.9",
"@xylabs/tsconfig-dom-jest": "^3.1.9",
"@xylabs/tsconfig-jest": "^3.1.9",
"@xylabs/ts-scripts-yarn3": "^3.1.12",
"@xylabs/tsconfig": "^3.1.12",
"@xylabs/tsconfig-dom": "^3.1.12",
"@xylabs/tsconfig-dom-jest": "^3.1.12",
"@xylabs/tsconfig-jest": "^3.1.12",
"axios": "^1.5.1",
"eslint": "^8.51.0",
"jest": "^29.7.0",
Expand All @@ -81,7 +83,7 @@
".": {
"node": {
"require": {
"types": "./dist/node/index.d.ts",
"types": "./dist/node/index.d.cts",
"default": "./dist/node/index.js"
},
"import": {
Expand Down Expand Up @@ -124,7 +126,9 @@
},
"scripts": {
"coverage": "yarn jest --coverage",
"start": "node dist/cjs/bin/start-meta.js",
"start": "nodemon",
"start-cjs": "node ./dist/node/bin/start-meta.js",
"start-esm": "node ./dist/node/bin/start-meta.mjs",
"test": "yarn jest"
},
"sideEffects": false,
Expand All @@ -134,7 +138,7 @@
"node": ">=18.13.0"
},
"volta": {
"node": "18.13.0",
"node": "18.17.1",
"yarn": "1.22.19"
},
"packageManager": "[email protected]"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cloneDeep } from '@xylabs/lodash'
import { BoundWitnessSchema } from '@xyo-network/boundwitness-model'
import { PayloadWrapper } from '@xyo-network/payload-wrapper'
import { Meta, metaBuilder } from '@xyo-network/sdk-meta'
import cloneDeep from 'lodash/cloneDeep'

import { getArchivistForDomain } from '../../../lib'
import { PayloadInfo } from '../../../types'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { assertEx } from '@xylabs/assert'
import { delay } from '@xylabs/delay'
import { exists } from '@xylabs/exists'
import { asyncHandler } from '@xylabs/sdk-api-express-ecs'
import { RequestHandler } from 'express'
import { existsSync, readFileSync } from 'fs'
import { ReasonPhrases, StatusCodes } from 'http-status-codes'
import { makeRe, MMRegExp } from 'minimatch'
import { extname, join } from 'path'

import { getAdjustedPath, getUriBehindProxy, preCacheFacebookShare } from '../../lib'
Expand Down Expand Up @@ -34,6 +36,28 @@ const maxImageGenerationWait = 8000

const imageCache = getImageCache()

/**
* Function which checks if a route matches any of the included glob patterns
*/
type RouteMatcher = (route: string) => boolean

/**
* Higher order function which creates precompiled RegEx matchers
* from a list of Glob Patterns
* @param patterns Glob patterns for route paths to match against
* @returns
*/
const createMatcher = (patterns: string[]): RouteMatcher => {
const regexesOrFalse = patterns.map((pattern) => makeRe(pattern))
const invalidGlobPatternIndexes = regexesOrFalse.reduce<number[]>((acc, curr, idx) => {
if (curr === false) acc.push(idx)
return acc
}, [])
assertEx(invalidGlobPatternIndexes.length === 0, `Invalid glob pattern(s): ${invalidGlobPatternIndexes.map((i) => patterns[i]).join(', ')}`)
const regexes = regexesOrFalse.filter((regex): regex is MMRegExp => assertEx(regex !== false))
return (route: string) => regexes.some((regex) => regex.test(route))
}

const getPageHandler = (baseDir: string) => {
// Ensure file containing base HTML exists
const filePath = join(baseDir, 'index.html')
Expand Down Expand Up @@ -105,13 +129,45 @@ const imageHandler: RequestHandler = asyncHandler(async (req, res, next) => {
next()
})

const liveSharePageHandler = (opts: ApplicationMiddlewareOptions): MountPathAndMiddleware | undefined => {
const { baseDir } = opts
const filePath = join(baseDir, 'xy.config.json')
if (existsSync(filePath)) {
// Read in config file
const xyConfig = JSON.parse(readFileSync(filePath, { encoding: 'utf-8' }))
// TODO: Validate xyConfig
if (xyConfig.liveShare) {
const { include, exclude } = xyConfig.liveShare
const matchesIncluded: RouteMatcher = include ? createMatcher(include) : () => true
const matchesExcluded: RouteMatcher = exclude ? createMatcher(exclude) : () => false

const handler: RequestHandler = asyncHandler(async (req, res, next) => {
// Exclude query string from glob via req.path
const uri = req.path
// // NOTE: Uncomment if we want to also include query string
// const uri = req.originalUrl
await Promise.resolve()
if (matchesIncluded(uri) && !matchesExcluded(uri)) {
// TODO: Grab helmet head data, next for now
next()
} else {
next()
}
})
return ['get', ['/*', handler]]
}
return undefined
}
}

/**
* Middleware for augmenting HTML metadata for Foreventory shares
*/
const foreventorySharePageHandler = (opts: ApplicationMiddlewareOptions): MountPathAndMiddleware => [
const netflixInsightsSharePageHandler = (opts: ApplicationMiddlewareOptions): MountPathAndMiddleware => [
'get',
['/netflix/insights/:hash', getPageHandler(opts.baseDir)],
]
const foreventoryImageHandler = (): MountPathAndMiddleware => ['get', ['/netflix/insights/:hash/preview/:width/:height/img.png', imageHandler]]
const netflixInsightsImageHandler = (): MountPathAndMiddleware => ['get', ['/netflix/insights/:hash/preview/:width/:height/img.png', imageHandler]]

export const foreventoryHandlers = (opts: ApplicationMiddlewareOptions) => [foreventorySharePageHandler(opts), foreventoryImageHandler()]
export const foreventoryHandlers = (opts: ApplicationMiddlewareOptions) =>
[netflixInsightsSharePageHandler(opts), netflixInsightsImageHandler(), liveSharePageHandler(opts)].filter(exists)
4 changes: 2 additions & 2 deletions src/modules/metaServer/lib/file/isKnownFileExtension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getType } from 'mime'
import mime from 'mime'

export const isKnownFileExtension = (filePath: string): boolean => {
// const extension = extname(filePath).toLowerCase()
// return knownFileExtensions[extension] ?? false
return getType(filePath) ? true : false
return mime.getType(filePath) ? true : false
}
3 changes: 2 additions & 1 deletion xy.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const config: XyTsupConfig = {
browser: {
src: false,
},
entryMode: 'all',
node: {
src: true,
src: {},
},
},
}
Expand Down
Loading

0 comments on commit 037fbd3

Please sign in to comment.