Skip to content

Commit

Permalink
chore: share links (#9)
Browse files Browse the repository at this point in the history
* chore: share links

* chore: clean up re-render

* chore: cleanup

* chore: more minimalg

* chore: lint errors

* chore: use vercel url
  • Loading branch information
ThomasRooney authored Jan 11, 2025
1 parent 7d21717 commit 9e15632
Show file tree
Hide file tree
Showing 21 changed files with 1,937 additions and 65 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea
dist
*.iml
*.iml
.vercel
.env*.local
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

SOURCE=$(shell find . -iname "*.go")



web/src/assets/wasm/lib.wasm: $(SOURCE)
mkdir -p dist
rm -f dist/*
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/speakeasy-api/jsonpath

go 1.21.0

toolchain go1.22.2
go 1.22

require (
github.com/pmezard/go-difflib v1.0.0
Expand Down
1 change: 1 addition & 0 deletions pkg/overlay/parse_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package overlay_test

import (
"github.com/speakeasy-api/jsonpath/pkg/overlay"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
Expand Down
2 changes: 1 addition & 1 deletion pkg/overlay/testdata/overlay.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ actions:
update:
- name: Testing
description: just a description
- target: $.paths["/anything/selectGlobalServer"].x-my-ignore
- target: $.paths["/anything/selectGlobalServer"]["x-my-ignore"]
update:
servers:
- url: http://localhost:35123
Expand Down
1 change: 1 addition & 0 deletions web/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ dist-ssr
*.njsproj
*.sln
*.sw?
.vercel
69 changes: 69 additions & 0 deletions web/api/share.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { put } from "@vercel/blob";
import { createHash } from "crypto";

const MAX_DATA_SIZE = 5 * 1024 * 1024; // 5MB
const AllowedOrigin = process.env.VERCEL_URL ?? "http://localhost";

export function POST(request: Request) {
const origin = request.headers.get("Origin");

if (!origin || !origin.includes(AllowedOrigin)) {
return new Response("Unauthorized", { status: 403 });
}

return new Promise<Response>((resolve, reject) => {
const body: ReadableStream | null = request.body;
if (!body) {
reject(new Error("No body"));
return;
}

const reader = body.getReader();
const chunks: Uint8Array[] = [];

const readData = (): void => {
reader.read().then(({ done, value }) => {
if (done) {
processData(chunks).then(resolve).catch(reject);
} else {
chunks.push(value);
if (
chunks.reduce((acc, chunk) => acc + chunk.length, 0) > MAX_DATA_SIZE
) {
reject(new Error("Data exceeds the maximum allowed size of 5MB"));
} else {
readData();
}
}
});
};

readData();
});
}

async function processData(chunks: Uint8Array[]): Promise<Response> {
const data = new Uint8Array(
chunks.reduce((acc, chunk) => acc + chunk.length, 0),
);
let offset = 0;
for (const chunk of chunks) {
data.set(chunk, offset);
offset += chunk.length;
}

const hash = createHash("sha256").update(data).digest("hex");
const key = `share-urls/${hash}`;

const result = await put(key, data, {
addRandomSuffix: false,
access: "public",
});
const downloadURL = result.downloadUrl;
const encodedDownloadURL = Buffer.from(downloadURL).toString("base64");

return new Response(JSON.stringify(encodedDownloadURL), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
8 changes: 7 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
"packageManager": "[email protected]",
"type": "module",
"scripts": {
"dev:vercel": "cd .. && vercel dev",
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"@monaco-editor/react": "^4.6.0",
"@radix-ui/react-dropdown-menu": "^2.1.4",
"@radix-ui/react-progress": "^1.1.1",
"@radix-ui/react-slot": "^1.1.1",
"@speakeasy-api/moonshine": "^0.52.3",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
Expand All @@ -25,14 +28,17 @@
"react-dom": "^18.2.0",
"tailwind-merge": "^2.6.0",
"tailwindcss-animate": "^1.0.7",
"vite-plugin-svgr": "^4.3.0"
"vite-plugin-svgr": "^4.3.0",
"vite-plugin-vercel": "^9.0.4"
},
"devDependencies": {
"@types/node": "^22.10.5",
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"@vercel/blob": "^0.27.0",
"@vercel/node": "^5.0.2",
"@vitejs/plugin-react": "^4.2.1",
"autoprefixer": "^10.4.20",
"eslint": "^8.57.0",
Expand Down
Loading

0 comments on commit 9e15632

Please sign in to comment.