Skip to content

Commit 84c14f7

Browse files
committed
Add cli
1 parent 20628d8 commit 84c14f7

File tree

13 files changed

+5951
-0
lines changed

13 files changed

+5951
-0
lines changed

cli/.changeset/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changesets
2+
3+
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
4+
with multi-package repos, or single-package repos to help you version and publish your code. You can
5+
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
6+
7+
We have a quick list of common questions to get you started engaging with this project in
8+
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)

cli/.changeset/aggregate.mjs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import fs from "node:fs/promises";
2+
import path from "node:path";
3+
import { execSync } from "node:child_process";
4+
5+
const THANKLESS_COMMITTERS = ["thdxr", "fwang", "jayair"];
6+
7+
const { version } = JSON.parse(
8+
await fs.readFile("./packages/core/package.json")
9+
);
10+
11+
const changesets = JSON.parse(await fs.readFile(".changeset/config.json"));
12+
const packages = changesets.fixed[0];
13+
14+
const changes = new Set();
15+
for (const pkg of packages) {
16+
const changelog = path.join(
17+
"packages",
18+
pkg.split("/").at(-1),
19+
"CHANGELOG.md"
20+
);
21+
const lines = (await fs.readFile(changelog)).toString().split("\n");
22+
let start = false;
23+
for (let line of lines) {
24+
if (!start) {
25+
if (line === `## ${version}`) {
26+
start = true;
27+
continue;
28+
}
29+
}
30+
31+
if (start) {
32+
if (line.startsWith("-") || line.startsWith("*")) {
33+
if (line.includes("Updated dependencies")) continue;
34+
if (line.includes("@serverless-stack/")) continue;
35+
36+
for (const user of THANKLESS_COMMITTERS) {
37+
line = line.replace(
38+
`Thanks [@${user}](https://github.com/${user})! `,
39+
""
40+
);
41+
}
42+
changes.add(line);
43+
continue;
44+
}
45+
46+
if (line.startsWith("## ")) break;
47+
}
48+
}
49+
}
50+
51+
console.log(`::set-output name=version::v${version}`);
52+
53+
execSync(`git tag v${version}`);
54+
execSync(`git push origin --tags`);

cli/.changeset/config.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
3+
"changelog": [
4+
"@changesets/changelog-github",
5+
{ "repo": "serverless-stack/open-next" }
6+
],
7+
"commit": false,
8+
"fixed": [
9+
[
10+
"open-next"
11+
]
12+
],
13+
"linked": [],
14+
"access": "restricted",
15+
"baseBranch": "main",
16+
"updateInternalDependencies": "patch",
17+
"ignore": []
18+
}

cli/.changeset/release

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
set -e
3+
4+
pnpm build
5+
cp package.json dist/package.json
6+
sed -i.bak -e '2,5d' dist/package.json
7+
pnpm changeset publish
8+

cli/.changeset/snapshot

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
set -e
3+
4+
yarn build
5+
sed -i.bak -e '3,6d' .changeset/config.json
6+
pnpm changeset version --snapshot
7+
cp package.json dist/package.json
8+
sed -i.bak -e '2,5d' dist/package.json
9+
pnpm changeset publish --no-git-tag --tag=snapshot
10+
cp .changeset/config.json.bak .changeset/config.json
11+
git checkout '**/package.json' '**/CHANGELOG.md' '.changeset'
12+

cli/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# open-next
2+
3+
## 0.0.0-20221205225535
4+
5+
### Patch Changes
6+
7+
- Test

cli/assets/middleware-handler.js

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import { default as fetch, Headers, Request, Response } from "node-fetch";
2+
Object.assign(globalThis, {
3+
Request,
4+
Response,
5+
fetch,
6+
Headers,
7+
self: {}
8+
});
9+
const index = await (() => import("./middleware.js"))();
10+
11+
// TODO
12+
//console.log(self);
13+
//handler({
14+
// Records: [
15+
// {
16+
// cf: {
17+
// request: {
18+
// uri: "https://sst.dev/_next/data/5fCVTp6Xr7VQpZ-m8Wxxq/middleware-redirect.json",
19+
// method: "GET",
20+
// headers: {
21+
// host: [{ value: "sst.dev" }]
22+
// },
23+
// querystring: "",
24+
// },
25+
// }
26+
// }
27+
// ]
28+
//}).then((res) => console.log(JSON.stringify(res, null, 2)));
29+
30+
export async function handler(event) {
31+
// Convert CloudFront request to Node request
32+
const request = event.Records[0].cf.request;
33+
const { uri, method, headers, querystring, body } = request;
34+
console.log(uri);
35+
console.log(request);
36+
const requestHeaders = new Headers();
37+
for (const [key, values] of Object.entries(headers)) {
38+
for (const { value } of values) {
39+
if (value) {
40+
requestHeaders.append(key, value)
41+
}
42+
}
43+
}
44+
const host = headers["host"][0].value;
45+
const qs = querystring.length > 0 ? `?${querystring}` : "";
46+
const url = new URL(`${uri}${qs}`, `https://${host}`);
47+
const nodeRequest = new Request(url.toString(), {
48+
method,
49+
headers: requestHeaders,
50+
body: body?.data
51+
? body.encoding === "base64"
52+
? Buffer.from(body.data, "base64").toString()
53+
: body.data
54+
: undefined,
55+
});
56+
57+
// Process request
58+
const response = await index.default(nodeRequest, {
59+
waitUntil: () => {},
60+
});
61+
62+
// Build headers
63+
(response.headers.get("x-middleware-override-headers") || "")
64+
.split(",")
65+
.forEach(key => {
66+
headers[key] = [{
67+
key,
68+
value: response.headers.get(`x-middleware-request-${key}`)
69+
}];
70+
});
71+
72+
if (response.headers.get("x-middleware-next") === "1") {
73+
headers["x-wahaha"] = [{ key: "x-wahaha", value: "wahaha" }];
74+
headers["wahaha"] = [{ key: "wahaha", value: "wahaha" }];
75+
console.log("== conitnue to origin ==", request)
76+
return request;
77+
}
78+
79+
console.log("== do not hit origin ==", {
80+
status: response.status,
81+
headers,
82+
});
83+
return {
84+
status: response.status,
85+
headers,
86+
}
87+
}
88+
89+
/**
90+
* middleware-fetch
91+
*
92+
* nextresponse [response] {
93+
size: 0,
94+
[symbol(body internals)]: {
95+
body: null,
96+
stream: null,
97+
boundary: null,
98+
disturbed: false,
99+
error: null
100+
},
101+
[symbol(response internals)]: {
102+
type: 'default',
103+
url: undefined,
104+
status: 200,
105+
statustext: '',
106+
headers: { 'x-middleware-next': '1' },
107+
counter: undefined,
108+
highwatermark: undefined
109+
},
110+
[Symbol(internal response)]: {
111+
cookies: ResponseCookies { _parsed: Map(0) {}, _headers: [Object] },
112+
url: undefined
113+
}
114+
}
115+
*/
116+
/**
117+
* middleware-set-header
118+
*
119+
* NextResponse [Response] {
120+
size: 0,
121+
[Symbol(Body internals)]: {
122+
body: null,
123+
stream: null,
124+
boundary: null,
125+
disturbed: false,
126+
error: null
127+
},
128+
[Symbol(Response internals)]: {
129+
type: 'default',
130+
url: undefined,
131+
status: 200,
132+
statusText: '',
133+
headers: {
134+
'x-hello-from-middleware2': 'hello',
135+
'x-middleware-next': '1',
136+
'x-middleware-override-headers': 'x-hello-from-middleware1',
137+
'x-middleware-request-x-hello-from-middleware1': 'hello'
138+
},
139+
counter: undefined,
140+
highWaterMark: undefined
141+
},
142+
[Symbol(internal response)]: {
143+
cookies: ResponseCookies { _parsed: Map(0) {}, _headers: [Object] },
144+
url: undefined
145+
}
146+
}
147+
*/
148+
/**
149+
* middleware-redirect
150+
*
151+
* Response {
152+
size: 0,
153+
[Symbol(Body internals)]: {
154+
body: null,
155+
stream: null,
156+
boundary: null,
157+
disturbed: false,
158+
error: null
159+
},
160+
[Symbol(Response internals)]: {
161+
type: 'default',
162+
url: '',
163+
status: 307,
164+
statusText: '',
165+
headers: { location: 'https://sst.dev/ssr' },
166+
counter: undefined,
167+
highWaterMark: undefined
168+
}
169+
}
170+
*/

0 commit comments

Comments
 (0)