Skip to content

Commit 04458ff

Browse files
committed
chore: wip
chore: wip chore: wip chore: wip chore: wip
1 parent 6c36951 commit 04458ff

File tree

15 files changed

+324
-41
lines changed

15 files changed

+324
-41
lines changed

.stacks/core/cloud/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
"build": "bun build.ts",
4949
"build-edge": "bun build ./src/edge/origin-request.ts --outfile ./dist/origin-request.js",
5050
"build-layer": "bun ./src/drivers/aws/runtime/scripts/build-layer.ts",
51+
"publish-layer": "bun ./src/drivers/aws/runtime/scripts/publish-layer.ts",
52+
"build-server": "bun --bun build-server.ts",
5153
"typecheck": "bun --bun tsc --noEmit",
5254
"prepublishOnly": "bun run build"
5355
},

.stacks/core/cloud/src/cloud.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
aws_cloudfront as cloudfront,
1212
aws_ec2 as ec2,
1313
aws_efs as efs,
14+
aws_iam as iam,
1415
aws_lambda as lambda,
1516
aws_cloudfront_origins as origins,
1617
aws_route53 as route53,
@@ -325,11 +326,38 @@ export class StacksCloud extends Stack {
325326
fileSystemName: `${config.app.name}-${config.app.env}-efs`,
326327
removalPolicy: RemovalPolicy.DESTROY,
327328
lifecyclePolicy: efs.LifecyclePolicy.AFTER_7_DAYS,
329+
performanceMode: efs.PerformanceMode.GENERAL_PURPOSE,
330+
throughputMode: efs.ThroughputMode.BURSTING,
331+
enableAutomaticBackups: true,
332+
encrypted: true,
333+
})
334+
335+
const role = new iam.Role(this, 'InstanceRole', {
336+
assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'),
337+
})
338+
339+
role.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonSSMManagedInstanceCore'))
340+
341+
new ec2.Instance(this, 'Instance', {
342+
vpc: this.vpc,
343+
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO),
344+
machineImage: new ec2.AmazonLinuxImage(),
345+
role,
346+
userData: ec2.UserData.custom(`
347+
#!/bin/bash
348+
yum update -y
349+
yum install -y amazon-efs-utils
350+
yum install -y git
351+
yum install -y https://s3.us-east-1.amazonaws.com/amazon-ssm-us-east-1/latest/linux_amd64/amazon-ssm-agent.rpm
352+
mkdir /mnt/efs
353+
mount -t efs ${this.storage.fileSystem.fileSystemId}:/ /mnt/efs
354+
git clone https://github.com/stacksjs/stacks.git /mnt/efs
355+
`),
328356
})
329357

330358
this.storage.accessPoint = new efs.AccessPoint(this, 'StacksAccessPoint', {
331359
fileSystem: this.storage.fileSystem,
332-
path: '/public',
360+
path: '/',
333361
posixUser: {
334362
uid: '1000',
335363
gid: '1000',
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { spawnSync } from 'node:child_process'
2+
import { BuildCommand } from './build-layer'
3+
4+
export class PublishCommand extends BuildCommand {
5+
static summary = 'Publish a custom Lambda layer for Bun.'
6+
7+
#aws(args: string[]): string {
8+
this.debug('$', 'aws', ...args)
9+
const { status, stdout, stderr } = spawnSync('aws', args, {
10+
stdio: 'pipe',
11+
})
12+
const result = stdout.toString('utf-8').trim()
13+
if (status === 0)
14+
return result
15+
16+
const reason = stderr.toString('utf-8').trim() || result
17+
throw new Error(`aws ${args.join(' ')} exited with ${status}: ${reason}`)
18+
}
19+
20+
async run() {
21+
const { flags } = await this.parse(PublishCommand)
22+
this.debug('Options:', flags)
23+
try {
24+
const version = this.#aws(['--version'])
25+
this.debug('AWS CLI:', version)
26+
}
27+
catch (error) {
28+
this.debug(error)
29+
this.error(
30+
'Install the `aws` CLI to continue: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html',
31+
{ exit: 1 },
32+
)
33+
}
34+
const { layer, region, arch, output, public: isPublic } = flags
35+
if (region.includes('*')) {
36+
// prettier-ignore
37+
const result = this.#aws([
38+
'ec2',
39+
'describe-regions',
40+
'--query', 'Regions[].RegionName',
41+
'--output', 'json',
42+
])
43+
region.length = 0
44+
for (const name of JSON.parse(result))
45+
region.push(name)
46+
}
47+
else if (!region.length) {
48+
// prettier-ignore
49+
region.push(this.#aws([
50+
'configure',
51+
'get',
52+
'region',
53+
]))
54+
}
55+
this.log('Publishing...')
56+
for (const regionName of region) {
57+
for (const layerName of layer) {
58+
// prettier-ignore
59+
const result = this.#aws([
60+
'lambda',
61+
'publish-layer-version',
62+
'--layer-name', layerName,
63+
'--region', regionName,
64+
'--description', 'Bun is an incredibly fast JavaScript runtime, bundler, transpiler, and package manager.',
65+
'--license-info', 'MIT',
66+
'--compatible-architectures', arch === 'x64' ? 'x86_64' : 'arm64',
67+
'--compatible-runtimes', 'provided.al2', 'provided',
68+
'--zip-file', `fileb://${output}`,
69+
'--output', 'json',
70+
])
71+
const { LayerVersionArn } = JSON.parse(result)
72+
this.log('Published', LayerVersionArn)
73+
// if (isPublic) {
74+
// prettier-ignore
75+
this.#aws([
76+
'lambda',
77+
'add-layer-version-permission',
78+
'--layer-name', layerName,
79+
'--region', regionName,
80+
'--version-number', LayerVersionArn.split(':').pop(),
81+
'--statement-id', `${layerName}-public`,
82+
'--action', 'lambda:GetLayerVersion',
83+
'--principal', '*',
84+
])
85+
// }
86+
}
87+
}
88+
this.log('Done')
89+
}
90+
}
91+
92+
await PublishCommand.run(process.argv.slice(2))

.stacks/core/cloud/src/drivers/aws/runtime/server.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import type { Server } from 'bun'
22
import { serverResponse } from '@stacksjs/router'
33

4-
// import type { Server, ServerWebSocket } from 'bun'
5-
64
export default {
75
async fetch(request: Request, server: Server): Promise<Response | undefined> {
86
// eslint-disable-next-line no-console
@@ -12,6 +10,7 @@ export default {
1210
headers: request.headers.toJSON(),
1311
body: request.body ? await request.text() : null,
1412
})
13+
1514
if (server.upgrade(request)) {
1615
// eslint-disable-next-line no-console
1716
console.log('WebSocket upgraded')

.stacks/core/config/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const config: StacksOptions = {
88
}
99

1010
export const {
11+
analytics,
1112
api,
1213
app,
1314
binary,

.stacks/core/router/src/middleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'node:fs'
2-
import path from 'node:path'
32
import { promisify } from 'node:util'
3+
import { appPath } from '@stacksjs/path'
44
import type { MiddlewareType } from '@stacksjs/types'
55

66
export class Middleware implements MiddlewareType {
@@ -32,6 +32,6 @@ async function importMiddlewares(directory: any) {
3232
}
3333

3434
// Example usage:
35-
const middlewareDirectory = path.join(__dirname, '../../../../app/middleware')
35+
const middlewareDirectory = appPath('/app/middleware')
3636

3737
export const middlewares = await importMiddlewares(middlewareDirectory)

.stacks/core/router/src/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const routesList: Route[] = await route.getRoutes()
99

1010
export function serve() {
1111
Bun.serve({
12-
async fetch(req: Request) {
12+
fetch(req: Request) {
1313
return serverResponse(req)
1414
},
1515
})
@@ -78,7 +78,6 @@ function execute(route: Route, request: Request, { statusCode }: Options) {
7878

7979
if (route?.method === 'GET' && (statusCode === 301 || statusCode === 302)) {
8080
const callback = String(route.callback)
81-
8281
const response = Response.redirect(callback, statusCode)
8382

8483
return noCache(response)
@@ -118,6 +117,7 @@ function noCache(response: Response) {
118117
response.headers.set('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate')
119118
response.headers.set('Pragma', 'no-cache')
120119
response.headers.set('Expires', '0')
120+
121121
return response
122122
}
123123

.stacks/core/types/src/analytics.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* **Analytics Options**
3+
*
4+
* This configuration defines all of your Analytics options. Because Stacks is fully-typed, you
5+
* may hover any of the options below and the definitions will be provided. In case you
6+
* have any questions, feel free to reach out via Discord or GitHub Discussions.
7+
*/
8+
export interface AnalyticsOptions {
9+
driver: 'google-analytics' | 'fathom'
10+
11+
drivers: {
12+
googleAnalytics?: {
13+
trackingId: string
14+
}
15+
fathom?: {
16+
siteId: string
17+
}
18+
}
19+
}
20+
21+
export type AnalyticsConfig = Partial<AnalyticsOptions>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Describes a plain-text file.
3+
*/
4+
export interface TextFile {
5+
path: string
6+
data: string
7+
}
8+
9+
/**
10+
* Describes a JSON file.
11+
*/
12+
export interface JsonFile {
13+
path: string
14+
data: unknown
15+
indent: string
16+
newline: string | undefined
17+
}
18+
19+
/**
20+
* Describes a package.json file.
21+
*/
22+
export interface PackageJson {
23+
engines: {
24+
node: string
25+
pnpm: string
26+
}
27+
version: string
28+
packageManager: string
29+
// wip
30+
}
31+
32+
export interface FileSystemsConfig {
33+
default: string
34+
disks: {
35+
local: {
36+
driver: 'local'
37+
root: string
38+
}
39+
40+
public: {
41+
driver: 'public'
42+
root: string
43+
visibility?: 'public'
44+
}
45+
46+
private: {
47+
driver: 'private'
48+
root: string
49+
visibility?: 'private'
50+
}
51+
52+
efs: {
53+
driver: 'local'
54+
root: string
55+
}
56+
57+
s3: {
58+
driver: 's3'
59+
root: string
60+
}
61+
62+
[key: string]: {
63+
driver: string
64+
root: string
65+
visibility?: 'public' | 'private'
66+
}
67+
}
68+
}

.stacks/core/types/src/fs.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)