Skip to content

Commit

Permalink
Make the Config Great Again
Browse files Browse the repository at this point in the history
  • Loading branch information
proudparrot2 committed Jul 16, 2024
1 parent fa9e013 commit 83f72a8
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 21 deletions.
12 changes: 11 additions & 1 deletion build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { rimraf } from 'rimraf'

await rimraf('dist')
await mkdir('dist')
await copyFile('./src/meteor.config.js', './dist/meteor.config.js')
const buildResult = await build({
sourcemap: true,
minify: process.env.NODE_ENV !== 'development',
Expand All @@ -20,5 +19,16 @@ const buildResult = await build({
metafile: true
})

await build({
entryPoints: {
'meteor.config': './src/config.ts'
},
minify: false,
format: 'esm',
bundle: true,
logLevel: 'info',
outdir: 'dist/'
})

if (buildResult.metafile)
writeFile('./dist/metafile.json', JSON.stringify(buildResult.metafile))
6 changes: 3 additions & 3 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Socket } from 'node:net'
import { argv } from 'node:process'
import { fileURLToPath } from 'node:url'

import { copyFile, mkdir } from 'node:fs/promises'
import { mkdir } from 'node:fs/promises'
import { baremuxPath } from '@mercuryworkshop/bare-mux/node'
// @ts-expect-error
import { epoxyPath } from '@mercuryworkshop/epoxy-transport'
Expand Down Expand Up @@ -59,15 +59,15 @@ Fastify({
if (!argv.includes('--no-build')) {
await rimraf('dist')
await mkdir('dist')
await copyFile('./src/meteor.config.js', './dist/meteor.config.js')
const dev = await context({
sourcemap: true,
minify: process.env.NODE_ENV !== 'development',
entryPoints: {
'meteor.bundle': './src/bundle/index.ts',
'meteor.client': './src/client/index.ts',
'meteor.worker': './src/worker.ts',
'meteor.codecs': './src/codecs/index.ts'
'meteor.codecs': './src/codecs/index.ts',
'meteor.config': './src/config.ts'
},
bundle: true,
logLevel: 'info',
Expand Down
10 changes: 3 additions & 7 deletions src/bundle/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { codecs } from '../codecs'
import { config } from '../config'
import { rewriteCss } from './rewrite/css'
import { rewriteHeaders } from './rewrite/headers'
import { rewriteHtml } from './rewrite/html'
Expand All @@ -8,14 +10,8 @@ import { createOrigin } from './util/createOrigin'
import { formatUrl } from './util/formatUrl'
import { log } from './util/logger'

declare global {
interface Window {
$meteor: typeof meteorBundle
}
}

const meteorBundle = {
config: self.__meteor$config,
config: self.$meteor_config,
rewrite: {
html: rewriteHtml,
css: rewriteCss,
Expand Down
2 changes: 1 addition & 1 deletion src/bundle/rewrite/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function rewriteElement(element: Element, origin: URL) {
'client',
'bundle',
'config',
'codecs,
'codecs'
]

for (const script of scriptsToPush) {
Expand Down
8 changes: 5 additions & 3 deletions src/codecs/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Codec } from '@/types'
import { locationvariable } from './locationvariable'
import { locationvariable } from './locvar'

self.__meteor$codecs = {
locationvariable,
export const codecs: Record<string, Codec> = {
locvar: locationvariable,
base64: {
encode(string) {
return encodeURIComponent(btoa(string))
Expand Down Expand Up @@ -50,3 +50,5 @@ self.__meteor$codecs = {
}
}
}

self.$meteor_codecs = codecs
File renamed without changes.
9 changes: 5 additions & 4 deletions src/meteor.config.js → src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/** @type {import('meteorproxy').Config} */
const config = {
import type { Config } from './types'

export const config: Config = {
prefix: '/route/',
codec: self.__meteor$codecs.xor,
codec: self.$meteor_codecs.xor,
debug: true,

plugins: [
Expand Down Expand Up @@ -37,4 +38,4 @@ const config = {
}
}

self.__meteor$config = config
self.$meteor_config = config
5 changes: 3 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { BareResponseFetch } from '@mercuryworkshop/bare-mux'
declare global {
interface Window {
__meteor$config: Config
__meteor$codecs: { [key: string]: Codec }
$meteor: typeof meteorBundle
$meteor_config: Config
$meteor_codecs: Record<string, Codec>
}
}
export interface Plugin {
Expand Down
2 changes: 2 additions & 0 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ class MeteorServiceWorker {
)
}
}

if (url.href.startsWith('data:')) {
const response = await fetch(url)

return new Response(response.body)
}

const fetchHead = new Headers(request.headers)
fetchHead.set('cookie', (await getCookies(url.host)).join('; '))
fetchHead.set('host', url.host)
Expand Down

0 comments on commit 83f72a8

Please sign in to comment.