@zjazj/server 0.0.8 → 0.0.9

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.
@@ -1,146 +0,0 @@
1
- import {
2
- getRequestId
3
- } from "./chunk-ERGI2X5S.js";
4
-
5
- // src/errors.ts
6
- import { HTTPException } from "hono/http-exception";
7
- var AppError = class extends Error {
8
- code;
9
- status;
10
- field;
11
- constructor(message, options = {}) {
12
- super(message, options.cause === void 0 ? void 0 : { cause: options.cause });
13
- this.name = new.target.name;
14
- this.code = options.code ?? "app_error";
15
- this.status = options.status ?? 400;
16
- this.field = options.field;
17
- }
18
- };
19
- var UnauthorizedError = class extends AppError {
20
- constructor(message = "Authentication required", options = {}) {
21
- super(message, { code: "unauthorized", status: 401, ...options });
22
- }
23
- };
24
- var ForbiddenError = class extends AppError {
25
- constructor(message = "Forbidden", options = {}) {
26
- super(message, { code: "forbidden", status: 403, ...options });
27
- }
28
- };
29
- var NotFoundError = class extends AppError {
30
- constructor(message = "Not found", options = {}) {
31
- super(message, { code: "not_found", status: 404, ...options });
32
- }
33
- };
34
- var ConflictError = class extends AppError {
35
- constructor(message = "Conflict", options = {}) {
36
- super(message, { code: "conflict", status: 409, ...options });
37
- }
38
- };
39
- var ValidationError = class extends AppError {
40
- constructor(message, field, options = {}) {
41
- super(message, { code: "validation_error", status: 422, field, ...options });
42
- }
43
- };
44
- function withTrace(body) {
45
- const traceId = getRequestId();
46
- return traceId ? { ...body, trace_id: traceId } : body;
47
- }
48
- function toErrorResponse(error) {
49
- if (error instanceof AppError) {
50
- const body = { code: error.code, message: error.message };
51
- if (error.field) body.field = error.field;
52
- return { status: error.status, body: withTrace(body) };
53
- }
54
- if (error instanceof HTTPException) {
55
- const code = error.status === 404 ? "not_found" : error.status >= 500 ? "internal_error" : "http_error";
56
- return {
57
- status: error.status,
58
- body: withTrace({ code, message: error.message || "Request failed" })
59
- };
60
- }
61
- return {
62
- status: 500,
63
- body: withTrace({ code: "internal_error", message: "Internal server error" })
64
- };
65
- }
66
- function respond(c, status, body) {
67
- if (status === 401) c.header("WWW-Authenticate", "Bearer");
68
- return c.json(body, status);
69
- }
70
- function errorHandler(onUnexpected) {
71
- return (error, c) => {
72
- const { status, body } = toErrorResponse(error);
73
- if (status >= 500) onUnexpected?.(error, c);
74
- return respond(c, status, body);
75
- };
76
- }
77
- function notFoundHandler() {
78
- return (c) => respond(c, 404, withTrace({ code: "not_found", message: "Not found" }));
79
- }
80
-
81
- // src/ids.ts
82
- import { randomBytes, randomInt } from "crypto";
83
- var APP_NAME_PATTERN = /^[a-z][a-z0-9-]{1,30}$/;
84
- var APP_ID_PATTERN = /^app[a-z0-9]{6}$/;
85
- var UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
86
- var ID_ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789";
87
- function isUuid(value) {
88
- return typeof value === "string" && UUID_PATTERN.test(value);
89
- }
90
- function isValidAppName(value) {
91
- return typeof value === "string" && APP_NAME_PATTERN.test(value);
92
- }
93
- function isValidAppId(value) {
94
- return typeof value === "string" && APP_ID_PATTERN.test(value);
95
- }
96
- function appBasePath(name) {
97
- return `/apps/${name}/`;
98
- }
99
- function assertValidAppId(value) {
100
- if (!isValidAppId(value)) {
101
- throw new Error(
102
- `Invalid app id ${JSON.stringify(value)}. Expected "app" followed by six characters from [a-z0-9].`
103
- );
104
- }
105
- }
106
- function generateAppId() {
107
- let id = "app";
108
- for (let i = 0; i < 6; i += 1) id += ID_ALPHABET[randomInt(ID_ALPHABET.length)];
109
- return id;
110
- }
111
- function uuidv7(now = Date.now()) {
112
- const bytes = randomBytes(16);
113
- const time = BigInt(now);
114
- bytes[0] = Number(time >> 40n & 0xffn);
115
- bytes[1] = Number(time >> 32n & 0xffn);
116
- bytes[2] = Number(time >> 24n & 0xffn);
117
- bytes[3] = Number(time >> 16n & 0xffn);
118
- bytes[4] = Number(time >> 8n & 0xffn);
119
- bytes[5] = Number(time & 0xffn);
120
- bytes[6] = (bytes[6] ?? 0) & 15 | 112;
121
- bytes[8] = (bytes[8] ?? 0) & 63 | 128;
122
- const hex = bytes.toString("hex");
123
- return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
124
- }
125
-
126
- export {
127
- AppError,
128
- UnauthorizedError,
129
- ForbiddenError,
130
- NotFoundError,
131
- ConflictError,
132
- ValidationError,
133
- toErrorResponse,
134
- errorHandler,
135
- notFoundHandler,
136
- APP_NAME_PATTERN,
137
- APP_ID_PATTERN,
138
- UUID_PATTERN,
139
- isUuid,
140
- isValidAppName,
141
- isValidAppId,
142
- appBasePath,
143
- assertValidAppId,
144
- generateAppId,
145
- uuidv7
146
- };