Skip to content

Commit

Permalink
Integrate RunFrame (Use Webworker Beta) (#586)
Browse files Browse the repository at this point in the history
* start implementing necessary endpoints for package management

* progress toward more accurate backend emulation

* fix type errors with placeholders

* adjust spacing on landing page

* init runframe integration

* run frame mostly working

* make using main thread or webworker toggleable

* improve clarity when webworker is on

* exclude runframe and eval-webworker from optimizations to attempt to fix vercel build issue

* remove manual output for eval webworker and runframe

* remove unfinished endpoint

* update to text lockfile, update to runframe version that uses CDN
  • Loading branch information
seveibar authored Jan 19, 2025
1 parent 3f14472 commit 4c7dbcc
Show file tree
Hide file tree
Showing 15 changed files with 3,945 additions and 11 deletions.
3,476 changes: 3,476 additions & 0 deletions bun.lock

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions fake-snippets-api/lib/db/db-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Order,
OrderFile,
AccountSnippet,
packageReleaseSchema,
} from "./schema.ts"
import { combine } from "zustand/middleware"
import { seed as seedFn } from "./seed"
Expand Down Expand Up @@ -85,17 +86,31 @@ const initializer = combine(databaseSchema.parse({}), (set, get) => ({
return newAccount
},
addSnippet: (
snippet: Omit<z.input<typeof snippetSchema>, "snippet_id">,
snippet: Omit<
z.input<typeof snippetSchema>,
"snippet_id" | "package_release_id"
>,
): Snippet => {
const newSnippetId = `snippet_${get().idCounter + 1}`
const newPackageRelease = packageReleaseSchema.parse({
package_release_id: `package_release_${get().idCounter + 1}`,
package_id: newSnippetId,
version: "0.0.1",
is_locked: false,
is_latest: true,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
})
const newSnippet = snippetSchema.parse({
...snippet,
snippet_id: newSnippetId,
package_release_id: newPackageRelease.package_release_id,
})
set((state) => {
return {
snippets: [...state.snippets, newSnippet],
idCounter: state.idCounter + 1,
packageReleases: [...state.packageReleases, newPackageRelease],
idCounter: state.idCounter + 2,
}
})
return { ...newSnippet, snippet_id: newSnippetId }
Expand Down
50 changes: 50 additions & 0 deletions fake-snippets-api/lib/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { z } from "zod"

export const snippetSchema = z.object({
snippet_id: z.string(),
package_release_id: z.string(),
name: z.string(),
unscoped_name: z.string(),
owner_name: z.string(),
Expand Down Expand Up @@ -99,9 +100,58 @@ export const accountSnippetSchema = z.object({
})
export type AccountSnippet = z.infer<typeof accountSnippetSchema>

export const packageReleaseSchema = z.object({
package_release_id: z.string(),
package_id: z.string(),
version: z.string().nullable(),
is_locked: z.boolean(),
is_latest: z.boolean(),
created_at: z.string().datetime(),
})
export type PackageRelease = z.infer<typeof packageReleaseSchema>

export const packageFileSchema = z.object({
package_file_id: z.string(),
package_release_id: z.string(),
file_path: z.string(),
content_text: z.string().nullable().optional(),
created_at: z.string().datetime(),
})
export type PackageFile = z.infer<typeof packageFileSchema>

export const packageSchema = z.object({
package_id: z.string(),
creator_account_id: z.string(),
latest_package_release_id: z.string().nullable(),
latest_version: z.string().nullable(),
license: z.string().nullable(),
owner_org_id: z.string(),
owner_github_username: z.string().nullable(),
is_source_from_github: z.boolean(),
description: z.string().nullable(),
name: z.string(),
unscoped_name: z.string(),
star_count: z.number(),
created_at: z.string().datetime(),
updated_at: z.string().datetime(),
})

export const errorSchema = z
.object({
error_code: z.string(),
message: z.string(),
})
.passthrough()

export const errorResponseSchema = z.object({
error: errorSchema,
})

export const databaseSchema = z.object({
idCounter: z.number().default(0),
snippets: z.array(snippetSchema).default([]),
packageReleases: z.array(packageReleaseSchema).default([]),
packageFiles: z.array(packageFileSchema).default([]),
sessions: z.array(sessionSchema).default([]),
loginPages: z.array(loginPageSchema).default([]),
accounts: z.array(accountSchema).default([]),
Expand Down
1 change: 1 addition & 0 deletions fake-snippets-api/lib/with-winter-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./middleware/with-winter-spec"
170 changes: 170 additions & 0 deletions fake-snippets-api/routes/api/package_files/download.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import { withRouteSpec } from "fake-snippets-api/lib/with-winter-spec"
import { z } from "zod"

export default withRouteSpec({
methods: ["GET"],
auth: "none",
queryParams: z.object({
jsdelivr_resolve: z
.enum(["true", "false"])
.optional()
.transform((v) => v === "true"),
jsdelivr_path: z.string(),
}),
jsonResponse: z.any(),
})(async (req, ctx) => {
const { jsdelivr_path, jsdelivr_resolve } = req.query

// Parse the file path
const [owner, packageWithVersion, ...rest] = jsdelivr_path.split("/")
if (!packageWithVersion) {
return ctx.error(400, {
error_code: "invalid_path",
message: "Invalid path",
})
}
const [packageName, version] = packageWithVersion.split("@")
const fileName = rest.join("/")

// Find the snippet
// const snippet =

// if (!snippet) {
// return ctx.error(404, {
// error_code: "snippet_not_found",
// message: "Snippet not found",
// })
// }

// if (!fileName && !jsdelivr_resolve) {
// return new Response(
// JSON.stringify({
// tags: {
// latest: "0.0.1",
// },
// versions: ["0.0.1"],
// }),
// {
// status: 200,
// headers: { "Content-Type": "application/json" },
// },
// )
// }

// if (!fileName && jsdelivr_resolve) {
// return new Response(
// JSON.stringify({
// version: "0.0.1",
// }),
// {
// status: 200,
// headers: { "Content-Type": "application/json" },
// },
// )
// }

// const latestRelease = await ctx.db
// .selectFrom("main.package_release")
// .where("package_id", "=", snippet.snippet_id)
// .where("is_latest", "=", true)
// .selectAll()
// .executeTakeFirst()

// const dtsFile = await ctx.db
// .selectFrom("main.package_file")
// .where("package_release_id", "=", latestRelease!.package_release_id)
// .where("file_path", "=", "/dist/index.d.ts")
// .select("content_text")
// .executeTakeFirst()

// // If no fileName is provided, return the directory listing
// if (!fileName || fileName === "flat") {
// const files = [
// {
// type: "file",
// name: "index.ts",
// hash: "placeholder_hash",
// time: snippet.updated_at,
// size: snippet.code?.length ?? 0,
// },
// {
// type: "file",
// name: "index.d.ts",
// hash: "placeholder_hash",
// time: snippet.updated_at,
// size: dtsFile?.content_text?.length ?? 0,
// },
// {
// type: "file",
// name: "package.json",
// hash: "placeholder_hash",
// time: snippet.updated_at,
// size: JSON.stringify({
// name: `@tsci/${owner}.${packageName}`,
// version: version || "0.0.1",
// main: "index.ts",
// types: "index.d.ts",
// }).length,
// },
// ]

// const response = {
// default: "/index.ts",
// files:
// fileName === "flat"
// ? files.map((f) => ({
// name: `/${f.name}`,
// hash: f.hash,
// time: f.time,
// size: f.size,
// }))
// : [
// {
// type: "directory",
// name: ".",
// files: files,
// },
// ],
// }

// return new Response(JSON.stringify(response, null, 2), {
// status: 200,
// headers: { "Content-Type": "application/json" },
// })
// }

// // Handle file downloads
// let content: string
// switch (fileName) {
// case "index.ts":
// content = snippet.code ?? ""
// break
// case "index.d.ts":
// content = dtsFile?.content_text ?? ""
// break
// case "package.json":
// content = JSON.stringify(
// {
// name: `@tsci/${owner}.${packageName}`,
// version: version || "0.0.1",
// main: "index.ts",
// types: "index.d.ts",
// },
// null,
// 2,
// )
// break
// default:
// return ctx.error(404, {
// error_code: "file_not_found",
// message: "Requested file not found",
// })
// }

const content = "TODO"

return new Response(content, {
status: 200,
headers: { "Content-Type": "text/plain" },
})
})
52 changes: 52 additions & 0 deletions fake-snippets-api/routes/api/package_files/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { withRouteSpec } from "fake-snippets-api/lib/with-winter-spec"
import { z } from "zod"
import * as ZT from "fake-snippets-api/lib/db/schema"
import { NotFoundError } from "winterspec/middleware"

const routeSpec = {
methods: ["POST"],
auth: "none",
jsonBody: z
.object({
package_file_id: z.string().uuid(),
})
.or(
z.object({
package_release_id: z.string().uuid(),
file_path: z.string(),
}),
)
.or(
z.object({
package_id: z.string().uuid(),
version: z.string().optional(),
file_path: z.string(),
}),
)
.or(
z.object({
package_name: z.string(),
version: z.string().optional(),
file_path: z.string(),
}),
)
.or(
z.object({
package_name_with_version: z.string(),
file_path: z.string(),
}),
),
jsonResponse: z
.object({
ok: z.boolean(),
package_file: ZT.packageFileSchema.optional(),
})
.or(ZT.errorResponseSchema),
} as const

export default withRouteSpec(routeSpec)(async (req, ctx) => {
return ctx.json({
ok: true,
package_file: undefined,
})
})
60 changes: 60 additions & 0 deletions fake-snippets-api/routes/api/package_files/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { withRouteSpec } from "fake-snippets-api/lib/with-winter-spec"
import { z } from "zod"
import * as ZT from "fake-snippets-api/lib/db/schema"

const routeSpec = {
methods: ["POST"],
auth: "none",
jsonBody: z
.object({
package_release_id: z.string().uuid(),
})
.or(
z.object({
package_name: z.string(),
use_latest_version: z.literal(true),
}),
)
.or(
z.object({
package_name_with_version: z.string(),
}),
),
jsonResponse: z.object({
ok: z.boolean(),
package_files: z.array(ZT.packageFileSchema),
}),
} as const

export default withRouteSpec(routeSpec)(async (req, ctx) => {
// const package_release_id = await findPackageReleaseId(req.jsonBody, ctx)
// if (!package_release_id) {
// return ctx.error(404, {
// error_code: "package_release_not_found",
// message: "Package release not found",
// })
// }
// const package_files = await ctx.db
// .selectFrom("main.package_file")
// .select([
// "package_file_id",
// "package_release_id",
// "content_mimetype",
// "file_path",
// "created_at",
// ])
// .where("package_release_id", "=", package_release_id)
// .where("file_path", "not like", ".tscircuit-internal/%")
// .execute()
// return ctx.json({
// ok: true,
// package_files: package_files.map((pf) => ({
// ...pf,
// created_at: pf.created_at.toISOString(),
// })),
// })
return ctx.json({
ok: true,
package_files: [],
})
})
Loading

0 comments on commit 4c7dbcc

Please sign in to comment.