Skip to content

Commit 6c10c8d

Browse files
committed
setup repo
0 parents  commit 6c10c8d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1929
-0
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
indent_style = tab
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
10+
[*.{yml,yaml}]
11+
indent_style = space

.githooks/pre-commit

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
pnpm run fmt
6+
pnpm run lint

.github/workflows/ci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: ci
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
15+
- uses: pnpm/action-setup@v2
16+
with:
17+
version: latest
18+
19+
- uses: actions/setup-node@v2
20+
with:
21+
node-version: 18
22+
cache: 'pnpm'
23+
24+
- run: pnpm i
25+
- run: pnpm run test-unit
26+
- run: pnpm run fmt-check

.github/workflows/release.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: release
2+
3+
on:
4+
push:
5+
tags:
6+
- '*'
7+
8+
jobs:
9+
release:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- uses: pnpm/action-setup@v2
14+
with:
15+
version: latest
16+
17+
- uses: actions/setup-node@v2
18+
with:
19+
node-version: 18
20+
registry-url: https://registry.npmjs.org/
21+
cache: 'pnpm'
22+
23+
- run: pnpm i
24+
- run: pnpm publish --access public --no-git-checks -r modules
25+
env:
26+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
27+
28+
- uses: yyx990803/release-tag@master
29+
env:
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
with:
32+
tag_name: ${{ github.ref }}

.gitignore

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# TypeScript v1 declaration files
45+
typings/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Microbundle cache
57+
.rpt2_cache/
58+
.rts2_cache_cjs/
59+
.rts2_cache_es/
60+
.rts2_cache_umd/
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
.env.test
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
78+
# Next.js build output
79+
.next
80+
81+
# Nuxt.js build / generate output
82+
.nuxt
83+
dist
84+
85+
# Gatsby files
86+
.cache/
87+
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88+
# https://nextjs.org/blog/next-9-1#public-directory-support
89+
# public
90+
91+
# vuepress build output
92+
.vuepress/dist
93+
94+
# Serverless directories
95+
.serverless/
96+
97+
# FuseBox cache
98+
.fusebox/
99+
100+
# DynamoDB Local files
101+
.dynamodb/
102+
103+
# TernJS port file
104+
.tern-port
105+
106+
# Lockfiles
107+
package-lock.json
108+
yarn.lock
109+
pnpm-lock.yaml

.npmrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
lockfile=false
2+
engine-strict=true
3+
strict-peer-dependencies=false

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Jeff Yang
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# EdgeKit
2+
3+
- ![npm package version](https://badgen.net/npm/v/edgekit)
4+
- ![node compatibility](https://badgen.net/npm/node/edgekit)
5+
- ![ci status](https://github.com/alpineland/edgekit/actions/workflows/ci.yml/badge.svg?branch=main)
6+
- [Source](https://github.com/alpineland/edgekit)
7+
- [Docs](https://edgekit.deno.dev)
8+
9+
## License
10+
11+
[MIT](./LICENSE)

modules/edgekit/node/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './mod.js';

modules/edgekit/node/mod.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// from https://github.com/sveltejs/kit/blob/master/packages/kit/src/node/index.js
2+
import { webcrypto as crypto } from 'node:crypto';
3+
import { URLPattern } from 'urlpattern-polyfill/urlpattern';
4+
5+
/** @param {import('http').IncomingMessage} req */
6+
function get_raw_body(req) {
7+
const headers = req.headers;
8+
9+
if (!headers['content-type']) {
10+
return null;
11+
}
12+
13+
const length = Number(headers['content-length']);
14+
15+
// check if no request body
16+
// https://github.com/jshttp/type-is/blob/c1f4388c71c8a01f79934e68f630ca4a15fffcd6/index.js#L81-L95
17+
if (isNaN(length) && !headers['transfer-encoding']) {
18+
return null;
19+
}
20+
21+
return new ReadableStream({
22+
start(controller) {
23+
req.on('error', (err) => controller.error(err));
24+
25+
let size = 0;
26+
27+
req.on('data', (chunk) => {
28+
size += chunk.length;
29+
30+
if (size > length) {
31+
controller.error(new Error('content-length exceeded'));
32+
}
33+
34+
controller.enqueue(chunk);
35+
});
36+
37+
req.on('end', () => controller.close());
38+
},
39+
});
40+
}
41+
42+
/**
43+
* @param {string} origin
44+
* @param {import('http').IncomingMessage} req
45+
*/
46+
export function get_request(origin, req) {
47+
return new Request(origin + req.url, {
48+
method: req.method,
49+
headers: /** @type {Record<string, string>} */ (req.headers),
50+
body: get_raw_body(req),
51+
});
52+
}
53+
54+
/**
55+
* @param {import('http').ServerResponse} res
56+
* @param {Response} response
57+
*/
58+
export async function set_response(res, response) {
59+
const headers = Object.fromEntries(response.headers);
60+
61+
if (response.headers.has('set-cookie')) {
62+
// @ts-expect-error (headers.raw() is non-standard)
63+
headers['set-cookie'] = response.headers.raw()['set-cookie'];
64+
}
65+
66+
res.writeHead(response.status, headers);
67+
68+
if (response.body) {
69+
let cancelled = false;
70+
71+
const reader = response.body.getReader();
72+
73+
res.on('close', () => {
74+
reader.cancel();
75+
cancelled = true;
76+
});
77+
78+
const next = async () => {
79+
const { done, value } = await reader.read();
80+
81+
if (cancelled) return;
82+
83+
if (done) {
84+
res.end();
85+
return;
86+
}
87+
88+
res.write(Buffer.from(value), (error) => {
89+
if (error) {
90+
console.error('error writing stream', error);
91+
res.end();
92+
} else {
93+
next();
94+
}
95+
});
96+
};
97+
98+
next();
99+
} else {
100+
res.end();
101+
}
102+
}
103+
104+
const polyfills = {
105+
crypto,
106+
URLPattern,
107+
};
108+
109+
export function install_polyfills() {
110+
for (const [k, v] of Object.entries(polyfills)) {
111+
// eslint-disable-next-line no-undef
112+
Object.defineProperty(globalThis, k, {
113+
enumerable: true,
114+
configurable: true,
115+
value: v,
116+
});
117+
}
118+
}

0 commit comments

Comments
 (0)