typenative 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +71 -0
- package/README.md +17 -1
- package/bin/cli.js +25 -11
- package/bin/go/convert.js +11 -1
- package/bin/index.js +221 -44
- package/bin/main.js +35 -14
- package/bin/node/assert.js +2 -2
- package/bin/node/buffer.js +9 -0
- package/bin/node/dns.js +1 -1
- package/bin/node/events.js +1 -1
- package/bin/node/fs.js +1 -1
- package/bin/node/fs_watch.js +407 -0
- package/bin/node/http2.js +33 -6
- package/bin/node/readline.js +1 -1
- package/bin/node/registry.js +2 -1
- package/bin/node/stream.js +6 -3
- package/bin/node/timers.js +1 -1
- package/bin/node/tls.js +3 -1
- package/bin/node/typed_arrays.js +608 -0
- package/bin/node/util.js +3 -0
- package/bin/node/zlib.js +131 -16
- package/bin/transpiler.js +684 -62
- package/bin/version.js +1 -1
- package/conformance.md +16 -4
- package/examples/README.md +54 -0
- package/examples/fibonacci/README.md +89 -0
- package/examples/fibonacci/bench.mjs +87 -0
- package/examples/fibonacci/main.ts +21 -0
- package/examples/hello-world/README.md +62 -0
- package/examples/hello-world/bench.mjs +66 -0
- package/examples/hello-world/main.ts +7 -0
- package/examples/json-server/README.md +125 -0
- package/examples/json-server/bench.mjs +171 -0
- package/examples/json-server/main.ts +67 -0
- package/govendor/github.com/andybalholm/brotli/LICENSE +19 -0
- package/govendor/github.com/andybalholm/brotli/README.md +19 -0
- package/govendor/github.com/andybalholm/brotli/backward_references.go +185 -0
- package/govendor/github.com/andybalholm/brotli/backward_references_hq.go +796 -0
- package/govendor/github.com/andybalholm/brotli/bit_cost.go +436 -0
- package/govendor/github.com/andybalholm/brotli/bit_reader.go +266 -0
- package/govendor/github.com/andybalholm/brotli/bitwriter.go +56 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter.go +144 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_command.go +434 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_distance.go +433 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_literal.go +433 -0
- package/govendor/github.com/andybalholm/brotli/brotli_bit_stream.go +1435 -0
- package/govendor/github.com/andybalholm/brotli/cluster.go +30 -0
- package/govendor/github.com/andybalholm/brotli/cluster_command.go +164 -0
- package/govendor/github.com/andybalholm/brotli/cluster_distance.go +326 -0
- package/govendor/github.com/andybalholm/brotli/cluster_literal.go +326 -0
- package/govendor/github.com/andybalholm/brotli/command.go +254 -0
- package/govendor/github.com/andybalholm/brotli/compress_fragment.go +834 -0
- package/govendor/github.com/andybalholm/brotli/compress_fragment_two_pass.go +775 -0
- package/govendor/github.com/andybalholm/brotli/constants.go +77 -0
- package/govendor/github.com/andybalholm/brotli/context.go +2176 -0
- package/govendor/github.com/andybalholm/brotli/decode.go +2570 -0
- package/govendor/github.com/andybalholm/brotli/dictionary.go +122890 -0
- package/govendor/github.com/andybalholm/brotli/dictionary_hash.go +32779 -0
- package/govendor/github.com/andybalholm/brotli/encode.go +1220 -0
- package/govendor/github.com/andybalholm/brotli/encoder.go +177 -0
- package/govendor/github.com/andybalholm/brotli/encoder_dict.go +22 -0
- package/govendor/github.com/andybalholm/brotli/encoder_fast.go +201 -0
- package/govendor/github.com/andybalholm/brotli/entropy_encode.go +592 -0
- package/govendor/github.com/andybalholm/brotli/entropy_encode_static.go +4399 -0
- package/govendor/github.com/andybalholm/brotli/fast_log.go +290 -0
- package/govendor/github.com/andybalholm/brotli/find_match_length.go +45 -0
- package/govendor/github.com/andybalholm/brotli/go.mod +3 -0
- package/govendor/github.com/andybalholm/brotli/h10.go +287 -0
- package/govendor/github.com/andybalholm/brotli/h5.go +214 -0
- package/govendor/github.com/andybalholm/brotli/h6.go +216 -0
- package/govendor/github.com/andybalholm/brotli/hash.go +342 -0
- package/govendor/github.com/andybalholm/brotli/hash_composite.go +93 -0
- package/govendor/github.com/andybalholm/brotli/hash_forgetful_chain.go +252 -0
- package/govendor/github.com/andybalholm/brotli/hash_longest_match_quickly.go +214 -0
- package/govendor/github.com/andybalholm/brotli/hash_rolling.go +168 -0
- package/govendor/github.com/andybalholm/brotli/histogram.go +226 -0
- package/govendor/github.com/andybalholm/brotli/http.go +201 -0
- package/govendor/github.com/andybalholm/brotli/huffman.go +653 -0
- package/govendor/github.com/andybalholm/brotli/literal_cost.go +182 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain1.go +212 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain2.go +248 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain3.go +278 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/emitter.go +34 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/m0.go +175 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/m4.go +339 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/matchfinder.go +103 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/pathfinder.go +328 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/textencoder.go +53 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/trio.go +327 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zdfast.go +258 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zfast.go +206 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zm.go +302 -0
- package/govendor/github.com/andybalholm/brotli/memory.go +66 -0
- package/govendor/github.com/andybalholm/brotli/metablock.go +574 -0
- package/govendor/github.com/andybalholm/brotli/metablock_command.go +165 -0
- package/govendor/github.com/andybalholm/brotli/metablock_distance.go +165 -0
- package/govendor/github.com/andybalholm/brotli/metablock_literal.go +165 -0
- package/govendor/github.com/andybalholm/brotli/params.go +37 -0
- package/govendor/github.com/andybalholm/brotli/platform.go +103 -0
- package/govendor/github.com/andybalholm/brotli/prefix.go +30 -0
- package/govendor/github.com/andybalholm/brotli/prefix_dec.go +723 -0
- package/govendor/github.com/andybalholm/brotli/quality.go +196 -0
- package/govendor/github.com/andybalholm/brotli/reader.go +127 -0
- package/govendor/github.com/andybalholm/brotli/ringbuffer.go +134 -0
- package/govendor/github.com/andybalholm/brotli/state.go +313 -0
- package/govendor/github.com/andybalholm/brotli/static_dict.go +662 -0
- package/govendor/github.com/andybalholm/brotli/static_dict_lut.go +75094 -0
- package/govendor/github.com/andybalholm/brotli/symbol_list.go +22 -0
- package/govendor/github.com/andybalholm/brotli/transform.go +641 -0
- package/govendor/github.com/andybalholm/brotli/utf8_util.go +70 -0
- package/govendor/github.com/andybalholm/brotli/util.go +7 -0
- package/govendor/github.com/andybalholm/brotli/write_bits.go +52 -0
- package/govendor/github.com/andybalholm/brotli/writer.go +162 -0
- package/govendor/github.com/fsnotify/fsnotify/.mailmap +2 -0
- package/govendor/github.com/fsnotify/fsnotify/CHANGELOG.md +651 -0
- package/govendor/github.com/fsnotify/fsnotify/CONTRIBUTING.md +147 -0
- package/govendor/github.com/fsnotify/fsnotify/LICENSE +25 -0
- package/govendor/github.com/fsnotify/fsnotify/README.md +214 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_fen.go +469 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_inotify.go +597 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_kqueue.go +735 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_other.go +22 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_windows.go +702 -0
- package/govendor/github.com/fsnotify/fsnotify/fsnotify.go +526 -0
- package/govendor/github.com/fsnotify/fsnotify/go.mod +3 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/darwin.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_darwin.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_dragonfly.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_freebsd.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_kqueue.go +32 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_linux.go +56 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_netbsd.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_openbsd.go +16 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_solaris.go +45 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_windows.go +40 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/freebsd.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/internal.go +2 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/unix.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/unix2.go +25 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/windows.go +40 -0
- package/govendor/github.com/fsnotify/fsnotify/shared.go +64 -0
- package/govendor/github.com/fsnotify/fsnotify/staticcheck.conf +3 -0
- package/govendor/github.com/fsnotify/fsnotify/system_bsd.go +7 -0
- package/govendor/github.com/fsnotify/fsnotify/system_darwin.go +8 -0
- package/govendor/manifest.txt +3 -1
- package/package.json +1 -1
- package/types/typenative-npm.d.ts +72 -0
- package/types/typenative.d.ts +117 -1
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// HTTP throughput benchmark: Node.js vs TypeNative for the JSON server.
|
|
2
|
+
//
|
|
3
|
+
// Compiles main.ts to a native binary with the TypeNative CLI from this repo
|
|
4
|
+
// checkout (../../bin/cli.js), then runs the full comparison:
|
|
5
|
+
//
|
|
6
|
+
// 1. starts the server under Node.js, seeds 50 todos, drives load, stops it
|
|
7
|
+
// 2. starts the compiled binary, seeds 50 todos, drives load, stops it
|
|
8
|
+
//
|
|
9
|
+
// Load is two phases per runtime, over keep-alive connections at concurrency
|
|
10
|
+
// 16, measuring req/s plus p50/p99 latency per request:
|
|
11
|
+
// - GET /todos 20,000 requests (a ~50-item JSON list per response)
|
|
12
|
+
// - POST /todos 10,000 requests (create a todo)
|
|
13
|
+
//
|
|
14
|
+
// Usage: node bench.mjs [gets=20000] [posts=10000]
|
|
15
|
+
|
|
16
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
17
|
+
import http from 'node:http';
|
|
18
|
+
import fs from 'node:fs';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import { fileURLToPath } from 'node:url';
|
|
21
|
+
|
|
22
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
23
|
+
const repoRoot = path.resolve(here, '..', '..');
|
|
24
|
+
const cli = path.join(repoRoot, 'bin', 'cli.js');
|
|
25
|
+
const GET_REQUESTS = Number(process.argv[2] ?? '20000') || 20000;
|
|
26
|
+
const POST_REQUESTS = Number(process.argv[3] ?? '10000') || 10000;
|
|
27
|
+
const CONCURRENCY = 16;
|
|
28
|
+
const SEEDED = 50;
|
|
29
|
+
const exeName = process.platform === 'win32' ? 'server.exe' : 'server';
|
|
30
|
+
const exePath = path.join(here, 'dist', exeName);
|
|
31
|
+
|
|
32
|
+
function sleep(ms) {
|
|
33
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Starts a server (node main.ts or the native binary) and resolves with its
|
|
37
|
+
// port once it prints its "listening on ..." banner and answers requests.
|
|
38
|
+
async function startServer(label, command, args) {
|
|
39
|
+
const child = spawn(command, args, { cwd: here, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
40
|
+
let stdout = '';
|
|
41
|
+
const port = await new Promise((resolve, reject) => {
|
|
42
|
+
const timer = setTimeout(() => reject(new Error(`${label}: no port banner, output:\n${stdout}`)), 20000);
|
|
43
|
+
child.stdout.on('data', (chunk) => {
|
|
44
|
+
stdout += chunk;
|
|
45
|
+
const match = stdout.match(/listening on http:\/\/127\.0\.0\.1:(\d+)/);
|
|
46
|
+
if (match) {
|
|
47
|
+
clearTimeout(timer);
|
|
48
|
+
resolve(Number(match[1]));
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
child.on('exit', (code) => {
|
|
52
|
+
clearTimeout(timer);
|
|
53
|
+
reject(new Error(`${label}: server exited early (code ${code}):\n${stdout}`));
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
const base = `http://127.0.0.1:${port}`;
|
|
57
|
+
for (let i = 0; i < 100; i++) {
|
|
58
|
+
try {
|
|
59
|
+
const res = await fetch(`${base}/todos`);
|
|
60
|
+
if (res.ok) return { child, base };
|
|
61
|
+
} catch {
|
|
62
|
+
// not accepting connections yet
|
|
63
|
+
}
|
|
64
|
+
await sleep(100);
|
|
65
|
+
}
|
|
66
|
+
child.kill();
|
|
67
|
+
throw new Error(`${label}: server never became ready`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function seed(base) {
|
|
71
|
+
for (let i = 0; i < SEEDED; i++) {
|
|
72
|
+
const res = await fetch(`${base}/todos`, { method: 'POST', body: JSON.stringify({ title: `task ${i + 1}` }) });
|
|
73
|
+
if (res.status !== 201) throw new Error(`seed request failed with status ${res.status}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// One load phase over keep-alive connections; returns req/s and latencies.
|
|
78
|
+
async function benchPhase(base, { method, reqPath, body, total }) {
|
|
79
|
+
const agent = new http.Agent({ keepAlive: true, maxSockets: CONCURRENCY });
|
|
80
|
+
const latencies = new Float64Array(total);
|
|
81
|
+
let dispatched = 0;
|
|
82
|
+
const target = new URL(base);
|
|
83
|
+
const headers = body
|
|
84
|
+
? { 'content-type': 'application/json', 'content-length': Buffer.byteLength(body) }
|
|
85
|
+
: {};
|
|
86
|
+
|
|
87
|
+
const request = () =>
|
|
88
|
+
new Promise((resolve, reject) => {
|
|
89
|
+
const req = http.request(
|
|
90
|
+
{ host: target.hostname, port: target.port, method, path: reqPath, agent, headers },
|
|
91
|
+
(res) => {
|
|
92
|
+
res.resume();
|
|
93
|
+
res.on('end', () => {
|
|
94
|
+
if (res.statusCode === 200 || res.statusCode === 201) resolve();
|
|
95
|
+
else reject(new Error(`${method} ${reqPath} -> ${res.statusCode}`));
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
);
|
|
99
|
+
req.on('error', reject);
|
|
100
|
+
req.end(body);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const t0 = performance.now();
|
|
104
|
+
const workers = Array.from({ length: CONCURRENCY }, async () => {
|
|
105
|
+
for (;;) {
|
|
106
|
+
const index = dispatched;
|
|
107
|
+
dispatched += 1;
|
|
108
|
+
if (index >= total) return;
|
|
109
|
+
const start = performance.now();
|
|
110
|
+
await request();
|
|
111
|
+
latencies[index] = performance.now() - start;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
await Promise.all(workers);
|
|
115
|
+
const elapsedSec = (performance.now() - t0) / 1000;
|
|
116
|
+
agent.destroy();
|
|
117
|
+
|
|
118
|
+
const sorted = Array.from(latencies).sort((a, b) => a - b);
|
|
119
|
+
return {
|
|
120
|
+
rps: total / elapsedSec,
|
|
121
|
+
p50: sorted[Math.floor(total * 0.5)],
|
|
122
|
+
p99: sorted[Math.min(total - 1, Math.floor(total * 0.99))],
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function measure(label, command, args) {
|
|
127
|
+
console.log(`\nBenchmarking ${label}...`);
|
|
128
|
+
const { child, base } = await startServer(label, command, args);
|
|
129
|
+
try {
|
|
130
|
+
await seed(base);
|
|
131
|
+
const get = await benchPhase(base, { method: 'GET', reqPath: '/todos', total: GET_REQUESTS });
|
|
132
|
+
console.log(` GET /todos ${GET_REQUESTS.toLocaleString()} req -> ${get.rps.toFixed(0)} req/s`);
|
|
133
|
+
const post = await benchPhase(base, {
|
|
134
|
+
method: 'POST',
|
|
135
|
+
reqPath: '/todos',
|
|
136
|
+
body: JSON.stringify({ title: 'bench item' }),
|
|
137
|
+
total: POST_REQUESTS,
|
|
138
|
+
});
|
|
139
|
+
console.log(` POST /todos ${POST_REQUESTS.toLocaleString()} req -> ${post.rps.toFixed(0)} req/s`);
|
|
140
|
+
return { get, post };
|
|
141
|
+
} finally {
|
|
142
|
+
child.kill();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function row(label, phase, result) {
|
|
147
|
+
return `| ${label.padEnd(10)} | ${phase.padEnd(13)} | ${result.rps.toFixed(0).padStart(9)} | ${result.p50.toFixed(2).padStart(8)} | ${result.p99.toFixed(2).padStart(8)} |`;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// 1. Compile the server to a native binary.
|
|
151
|
+
fs.mkdirSync(path.join(here, 'dist'), { recursive: true });
|
|
152
|
+
console.log('Compiling main.ts to a native binary with TypeNative...');
|
|
153
|
+
const build = spawnSync(process.execPath, [cli, '--source', 'main.ts', '--output', path.join('dist', exeName)], {
|
|
154
|
+
cwd: here,
|
|
155
|
+
stdio: 'inherit',
|
|
156
|
+
});
|
|
157
|
+
if (build.status !== 0) process.exit(build.status ?? 1);
|
|
158
|
+
|
|
159
|
+
// 2. Load-test both runtimes.
|
|
160
|
+
const nodeStats = await measure('Node.js', process.execPath, ['main.ts']);
|
|
161
|
+
const nativeStats = await measure('TypeNative', exePath, []);
|
|
162
|
+
|
|
163
|
+
// 3. Report.
|
|
164
|
+
console.log(`\nThroughput (keep-alive, concurrency ${CONCURRENCY}, ${SEEDED} todos seeded):\n`);
|
|
165
|
+
console.log('| Runtime | Endpoint | req/s | p50 (ms) | p99 (ms) |');
|
|
166
|
+
console.log('|------------|---------------|---------|----------|----------|');
|
|
167
|
+
console.log(row('Node.js', 'GET /todos', nodeStats.get));
|
|
168
|
+
console.log(row('TypeNative', 'GET /todos', nativeStats.get));
|
|
169
|
+
console.log(row('Node.js', 'POST /todos', nodeStats.post));
|
|
170
|
+
console.log(row('TypeNative', 'POST /todos', nativeStats.post));
|
|
171
|
+
console.log(`\nTypeNative serves ${(nativeStats.get.rps / nodeStats.get.rps).toFixed(1)}x more GET and ${(nativeStats.post.rps / nodeStats.post.rps).toFixed(1)}x more POST requests per second.`);
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// A tiny JSON API built with nothing but the Node standard library
|
|
2
|
+
// (node:http). The todos live in memory, so the same file is a complete,
|
|
3
|
+
// dependency-free web server under Node.js and as a compiled TypeNative
|
|
4
|
+
// binary.
|
|
5
|
+
//
|
|
6
|
+
// Routes:
|
|
7
|
+
// GET /todos list all todos
|
|
8
|
+
// GET /todos/<id> fetch one todo (404 if missing)
|
|
9
|
+
// POST /todos create a todo from a JSON body: { "title": "..." }
|
|
10
|
+
//
|
|
11
|
+
// node main.ts [port] (default 3000; PORT env var also honored)
|
|
12
|
+
// ./server [port]
|
|
13
|
+
import http from 'node:http';
|
|
14
|
+
|
|
15
|
+
interface Todo {
|
|
16
|
+
id: number;
|
|
17
|
+
title: string;
|
|
18
|
+
done: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const todos: Todo[] = [];
|
|
22
|
+
let nextId = 1;
|
|
23
|
+
|
|
24
|
+
function send(res: http.ServerResponse, status: number, body: string): void {
|
|
25
|
+
res.writeHead(status, { 'Content-Type': 'application/json' });
|
|
26
|
+
res.end(body);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const server = http.createServer((req, res) => {
|
|
30
|
+
let body = '';
|
|
31
|
+
req.on('data', (chunk) => {
|
|
32
|
+
body += String(chunk);
|
|
33
|
+
});
|
|
34
|
+
req.on('end', () => {
|
|
35
|
+
const url = new URL(req.url ?? '/', 'http://localhost');
|
|
36
|
+
|
|
37
|
+
if (req.method === 'GET' && url.pathname === '/todos') {
|
|
38
|
+
send(res, 200, JSON.stringify(todos));
|
|
39
|
+
} else if (req.method === 'GET' && url.pathname.startsWith('/todos/')) {
|
|
40
|
+
const id = Number(url.pathname.slice('/todos/'.length));
|
|
41
|
+
const todo = todos.find((t) => t.id === id);
|
|
42
|
+
if (todo) {
|
|
43
|
+
send(res, 200, JSON.stringify(todo));
|
|
44
|
+
} else {
|
|
45
|
+
send(res, 404, JSON.stringify({ error: 'todo not found' }));
|
|
46
|
+
}
|
|
47
|
+
} else if (req.method === 'POST' && url.pathname === '/todos') {
|
|
48
|
+
const input = JSON.parse(body) as { title: string };
|
|
49
|
+
if (!input.title) {
|
|
50
|
+
send(res, 400, JSON.stringify({ error: 'title is required' }));
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const todo: Todo = { id: nextId, title: input.title, done: false };
|
|
54
|
+
nextId += 1;
|
|
55
|
+
todos.push(todo);
|
|
56
|
+
send(res, 201, JSON.stringify(todo));
|
|
57
|
+
} else {
|
|
58
|
+
send(res, 404, JSON.stringify({ error: 'no route' }));
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const port = Number(process.argv[2] ?? process.env.PORT ?? '3000');
|
|
64
|
+
server.listen(port, '127.0.0.1', () => {
|
|
65
|
+
const address = server.address() as { port: number };
|
|
66
|
+
console.log(`Todo API listening on http://127.0.0.1:${address.port}`);
|
|
67
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2009, 2010, 2013-2016 by the Brotli Authors.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
This package is a brotli compressor and decompressor implemented in Go.
|
|
2
|
+
It was translated from the reference implementation (https://github.com/google/brotli)
|
|
3
|
+
with the `c2go` tool at https://github.com/andybalholm/c2go.
|
|
4
|
+
|
|
5
|
+
I am using it in production with https://github.com/andybalholm/redwood.
|
|
6
|
+
|
|
7
|
+
API documentation is found at https://pkg.go.dev/github.com/andybalholm/brotli?tab=doc.
|
|
8
|
+
|
|
9
|
+
## Roadmap
|
|
10
|
+
|
|
11
|
+
I have been working on new compression algorithms (not translated from C)
|
|
12
|
+
in the matchfinder package.
|
|
13
|
+
You can use them with the NewWriterV2 function.
|
|
14
|
+
Currently they give better results than the old implementation
|
|
15
|
+
(at least for compressing my test file, Newton’s *Opticks*)
|
|
16
|
+
on levels 0 to 9.
|
|
17
|
+
|
|
18
|
+
The new APIs are currently considered experimental,
|
|
19
|
+
and are not covered by any SemVer compatibility guarantees.
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
package brotli
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"sync"
|
|
5
|
+
)
|
|
6
|
+
|
|
7
|
+
/* Copyright 2013 Google Inc. All Rights Reserved.
|
|
8
|
+
|
|
9
|
+
Distributed under MIT license.
|
|
10
|
+
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/* Function to find backward reference copies. */
|
|
14
|
+
|
|
15
|
+
func computeDistanceCode(distance uint, max_distance uint, dist_cache []int) uint {
|
|
16
|
+
if distance <= max_distance {
|
|
17
|
+
var distance_plus_3 uint = distance + 3
|
|
18
|
+
var offset0 uint = distance_plus_3 - uint(dist_cache[0])
|
|
19
|
+
var offset1 uint = distance_plus_3 - uint(dist_cache[1])
|
|
20
|
+
if distance == uint(dist_cache[0]) {
|
|
21
|
+
return 0
|
|
22
|
+
} else if distance == uint(dist_cache[1]) {
|
|
23
|
+
return 1
|
|
24
|
+
} else if offset0 < 7 {
|
|
25
|
+
return (0x9750468 >> (4 * offset0)) & 0xF
|
|
26
|
+
} else if offset1 < 7 {
|
|
27
|
+
return (0xFDB1ACE >> (4 * offset1)) & 0xF
|
|
28
|
+
} else if distance == uint(dist_cache[2]) {
|
|
29
|
+
return 2
|
|
30
|
+
} else if distance == uint(dist_cache[3]) {
|
|
31
|
+
return 3
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return distance + numDistanceShortCodes - 1
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var hasherSearchResultPool sync.Pool
|
|
39
|
+
|
|
40
|
+
func createBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *encoderParams, hasher hasherHandle, dist_cache []int, last_insert_len *uint, commands *[]command, num_literals *uint) {
|
|
41
|
+
var max_backward_limit uint = maxBackwardLimit(params.lgwin)
|
|
42
|
+
var insert_length uint = *last_insert_len
|
|
43
|
+
var pos_end uint = position + num_bytes
|
|
44
|
+
var store_end uint
|
|
45
|
+
if num_bytes >= hasher.StoreLookahead() {
|
|
46
|
+
store_end = position + num_bytes - hasher.StoreLookahead() + 1
|
|
47
|
+
} else {
|
|
48
|
+
store_end = position
|
|
49
|
+
}
|
|
50
|
+
var random_heuristics_window_size uint = literalSpreeLengthForSparseSearch(params)
|
|
51
|
+
var apply_random_heuristics uint = position + random_heuristics_window_size
|
|
52
|
+
var gap uint = 0
|
|
53
|
+
/* Set maximum distance, see section 9.1. of the spec. */
|
|
54
|
+
|
|
55
|
+
const kMinScore uint = scoreBase + 100
|
|
56
|
+
|
|
57
|
+
/* For speed up heuristics for random data. */
|
|
58
|
+
|
|
59
|
+
/* Minimum score to accept a backward reference. */
|
|
60
|
+
hasher.PrepareDistanceCache(dist_cache)
|
|
61
|
+
sr2, _ := hasherSearchResultPool.Get().(*hasherSearchResult)
|
|
62
|
+
if sr2 == nil {
|
|
63
|
+
sr2 = &hasherSearchResult{}
|
|
64
|
+
}
|
|
65
|
+
sr, _ := hasherSearchResultPool.Get().(*hasherSearchResult)
|
|
66
|
+
if sr == nil {
|
|
67
|
+
sr = &hasherSearchResult{}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
for position+hasher.HashTypeLength() < pos_end {
|
|
71
|
+
var max_length uint = pos_end - position
|
|
72
|
+
var max_distance uint = brotli_min_size_t(position, max_backward_limit)
|
|
73
|
+
sr.len = 0
|
|
74
|
+
sr.len_code_delta = 0
|
|
75
|
+
sr.distance = 0
|
|
76
|
+
sr.score = kMinScore
|
|
77
|
+
hasher.FindLongestMatch(¶ms.dictionary, ringbuffer, ringbuffer_mask, dist_cache, position, max_length, max_distance, gap, params.dist.max_distance, sr)
|
|
78
|
+
if sr.score > kMinScore {
|
|
79
|
+
/* Found a match. Let's look for something even better ahead. */
|
|
80
|
+
var delayed_backward_references_in_row int = 0
|
|
81
|
+
max_length--
|
|
82
|
+
for ; ; max_length-- {
|
|
83
|
+
var cost_diff_lazy uint = 175
|
|
84
|
+
if params.quality < minQualityForExtensiveReferenceSearch {
|
|
85
|
+
sr2.len = brotli_min_size_t(sr.len-1, max_length)
|
|
86
|
+
} else {
|
|
87
|
+
sr2.len = 0
|
|
88
|
+
}
|
|
89
|
+
sr2.len_code_delta = 0
|
|
90
|
+
sr2.distance = 0
|
|
91
|
+
sr2.score = kMinScore
|
|
92
|
+
max_distance = brotli_min_size_t(position+1, max_backward_limit)
|
|
93
|
+
hasher.FindLongestMatch(¶ms.dictionary, ringbuffer, ringbuffer_mask, dist_cache, position+1, max_length, max_distance, gap, params.dist.max_distance, sr2)
|
|
94
|
+
if sr2.score >= sr.score+cost_diff_lazy {
|
|
95
|
+
/* Ok, let's just write one byte for now and start a match from the
|
|
96
|
+
next byte. */
|
|
97
|
+
position++
|
|
98
|
+
|
|
99
|
+
insert_length++
|
|
100
|
+
*sr = *sr2
|
|
101
|
+
delayed_backward_references_in_row++
|
|
102
|
+
if delayed_backward_references_in_row < 4 && position+hasher.HashTypeLength() < pos_end {
|
|
103
|
+
continue
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
break
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
apply_random_heuristics = position + 2*sr.len + random_heuristics_window_size
|
|
111
|
+
max_distance = brotli_min_size_t(position, max_backward_limit)
|
|
112
|
+
{
|
|
113
|
+
/* The first 16 codes are special short-codes,
|
|
114
|
+
and the minimum offset is 1. */
|
|
115
|
+
var distance_code uint = computeDistanceCode(sr.distance, max_distance+gap, dist_cache)
|
|
116
|
+
if (sr.distance <= (max_distance + gap)) && distance_code > 0 {
|
|
117
|
+
dist_cache[3] = dist_cache[2]
|
|
118
|
+
dist_cache[2] = dist_cache[1]
|
|
119
|
+
dist_cache[1] = dist_cache[0]
|
|
120
|
+
dist_cache[0] = int(sr.distance)
|
|
121
|
+
hasher.PrepareDistanceCache(dist_cache)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
*commands = append(*commands, makeCommand(¶ms.dist, insert_length, sr.len, sr.len_code_delta, distance_code))
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
*num_literals += insert_length
|
|
128
|
+
insert_length = 0
|
|
129
|
+
/* Put the hash keys into the table, if there are enough bytes left.
|
|
130
|
+
Depending on the hasher implementation, it can push all positions
|
|
131
|
+
in the given range or only a subset of them.
|
|
132
|
+
Avoid hash poisoning with RLE data. */
|
|
133
|
+
{
|
|
134
|
+
var range_start uint = position + 2
|
|
135
|
+
var range_end uint = brotli_min_size_t(position+sr.len, store_end)
|
|
136
|
+
if sr.distance < sr.len>>2 {
|
|
137
|
+
range_start = brotli_min_size_t(range_end, brotli_max_size_t(range_start, position+sr.len-(sr.distance<<2)))
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
hasher.StoreRange(ringbuffer, ringbuffer_mask, range_start, range_end)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
position += sr.len
|
|
144
|
+
} else {
|
|
145
|
+
insert_length++
|
|
146
|
+
position++
|
|
147
|
+
|
|
148
|
+
/* If we have not seen matches for a long time, we can skip some
|
|
149
|
+
match lookups. Unsuccessful match lookups are very very expensive
|
|
150
|
+
and this kind of a heuristic speeds up compression quite
|
|
151
|
+
a lot. */
|
|
152
|
+
if position > apply_random_heuristics {
|
|
153
|
+
/* Going through uncompressible data, jump. */
|
|
154
|
+
if position > apply_random_heuristics+4*random_heuristics_window_size {
|
|
155
|
+
var kMargin uint = brotli_max_size_t(hasher.StoreLookahead()-1, 4)
|
|
156
|
+
/* It is quite a long time since we saw a copy, so we assume
|
|
157
|
+
that this data is not compressible, and store hashes less
|
|
158
|
+
often. Hashes of non compressible data are less likely to
|
|
159
|
+
turn out to be useful in the future, too, so we store less of
|
|
160
|
+
them to not to flood out the hash table of good compressible
|
|
161
|
+
data. */
|
|
162
|
+
|
|
163
|
+
var pos_jump uint = brotli_min_size_t(position+16, pos_end-kMargin)
|
|
164
|
+
for ; position < pos_jump; position += 4 {
|
|
165
|
+
hasher.Store(ringbuffer, ringbuffer_mask, position)
|
|
166
|
+
insert_length += 4
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
var kMargin uint = brotli_max_size_t(hasher.StoreLookahead()-1, 2)
|
|
170
|
+
var pos_jump uint = brotli_min_size_t(position+8, pos_end-kMargin)
|
|
171
|
+
for ; position < pos_jump; position += 2 {
|
|
172
|
+
hasher.Store(ringbuffer, ringbuffer_mask, position)
|
|
173
|
+
insert_length += 2
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
insert_length += pos_end - position
|
|
181
|
+
*last_insert_len = insert_length
|
|
182
|
+
|
|
183
|
+
hasherSearchResultPool.Put(sr)
|
|
184
|
+
hasherSearchResultPool.Put(sr2)
|
|
185
|
+
}
|