@orpc/standard-server 0.0.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/LICENSE +21 -0
- package/dist/index.js +225 -0
- package/dist/src/event-source/decoder.d.ts +16 -0
- package/dist/src/event-source/encoder.d.ts +7 -0
- package/dist/src/event-source/errors.d.ts +16 -0
- package/dist/src/event-source/index.d.ts +6 -0
- package/dist/src/event-source/meta.d.ts +6 -0
- package/dist/src/event-source/types.d.ts +10 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/types.d.ts +27 -0
- package/dist/src/utils.d.ts +6 -0
- package/package.json +39 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 oRPC
|
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.
|
package/dist/index.js
ADDED
@@ -0,0 +1,225 @@
|
|
1
|
+
// src/event-source/errors.ts
|
2
|
+
var EventEncoderError = class extends TypeError {
|
3
|
+
};
|
4
|
+
var EventDecoderError = class extends TypeError {
|
5
|
+
};
|
6
|
+
var ErrorEvent = class extends Error {
|
7
|
+
data;
|
8
|
+
constructor(options) {
|
9
|
+
super(options?.message ?? "An error event was received", options);
|
10
|
+
this.data = options?.data;
|
11
|
+
}
|
12
|
+
};
|
13
|
+
var UnknownEvent = class extends ErrorEvent {
|
14
|
+
};
|
15
|
+
|
16
|
+
// src/event-source/decoder.ts
|
17
|
+
function decodeEventMessage(encoded) {
|
18
|
+
const lines = encoded.replace(/\n+$/, "").split(/\n/);
|
19
|
+
const message = {
|
20
|
+
data: "",
|
21
|
+
event: void 0,
|
22
|
+
id: void 0,
|
23
|
+
retry: void 0
|
24
|
+
};
|
25
|
+
for (const line of lines) {
|
26
|
+
const index = line.indexOf(": ");
|
27
|
+
if (index === -1) {
|
28
|
+
throw new EventDecoderError(`Invalid EventSource message line: ${line}`);
|
29
|
+
}
|
30
|
+
const key = line.slice(0, index);
|
31
|
+
const value = line.slice(index + 2);
|
32
|
+
if (key !== "data" && key in message && message[key] !== void 0) {
|
33
|
+
throw new EventDecoderError(`Duplicate EventSource message key: ${key}`);
|
34
|
+
}
|
35
|
+
if (key === "data") {
|
36
|
+
message.data += `${value}
|
37
|
+
`;
|
38
|
+
} else if (key === "event") {
|
39
|
+
message.event = value;
|
40
|
+
} else if (key === "id") {
|
41
|
+
message.id = value;
|
42
|
+
} else if (key === "retry") {
|
43
|
+
const maybeInteger = Number.parseInt(value);
|
44
|
+
if (!Number.isInteger(maybeInteger) || maybeInteger < 0 || maybeInteger.toString() !== value) {
|
45
|
+
throw new EventDecoderError(`Invalid EventSource message retry value: ${value}`);
|
46
|
+
}
|
47
|
+
message.retry = maybeInteger;
|
48
|
+
} else {
|
49
|
+
throw new EventDecoderError(`Unknown EventSource message key: ${key}`);
|
50
|
+
}
|
51
|
+
}
|
52
|
+
message.data = message.data.replace(/\n$/, "");
|
53
|
+
return message;
|
54
|
+
}
|
55
|
+
var EventDecoder = class {
|
56
|
+
constructor(options = {}) {
|
57
|
+
this.options = options;
|
58
|
+
}
|
59
|
+
incomplete = "";
|
60
|
+
feed(chunk) {
|
61
|
+
this.incomplete += chunk;
|
62
|
+
const lastCompleteIndex = this.incomplete.lastIndexOf("\n\n");
|
63
|
+
if (lastCompleteIndex === -1) {
|
64
|
+
return;
|
65
|
+
}
|
66
|
+
const completes = this.incomplete.slice(0, lastCompleteIndex + 2).split(/\n{2,}/);
|
67
|
+
this.incomplete = this.incomplete.slice(lastCompleteIndex + 2);
|
68
|
+
for (const encoded of completes) {
|
69
|
+
if (!encoded) {
|
70
|
+
continue;
|
71
|
+
}
|
72
|
+
const message = decodeEventMessage(`${encoded}
|
73
|
+
|
74
|
+
`);
|
75
|
+
if (this.options.onEvent) {
|
76
|
+
this.options.onEvent(message);
|
77
|
+
}
|
78
|
+
}
|
79
|
+
this.incomplete = "";
|
80
|
+
}
|
81
|
+
end() {
|
82
|
+
this.feed("\n\n");
|
83
|
+
}
|
84
|
+
};
|
85
|
+
var EventDecoderStream = class extends TransformStream {
|
86
|
+
constructor() {
|
87
|
+
let decoder;
|
88
|
+
super({
|
89
|
+
start(controller) {
|
90
|
+
decoder = new EventDecoder({
|
91
|
+
onEvent: (event) => {
|
92
|
+
controller.enqueue(event);
|
93
|
+
}
|
94
|
+
});
|
95
|
+
},
|
96
|
+
transform(chunk) {
|
97
|
+
decoder.feed(chunk);
|
98
|
+
},
|
99
|
+
flush() {
|
100
|
+
decoder.end();
|
101
|
+
}
|
102
|
+
});
|
103
|
+
}
|
104
|
+
};
|
105
|
+
|
106
|
+
// src/event-source/encoder.ts
|
107
|
+
function assertEventId(id) {
|
108
|
+
if (id.includes("\n")) {
|
109
|
+
throw new EventEncoderError("Event-source id must not contain a newline character");
|
110
|
+
}
|
111
|
+
}
|
112
|
+
function assertEventName(event) {
|
113
|
+
if (event.includes("\n")) {
|
114
|
+
throw new EventEncoderError("Event-source event must not contain a newline character");
|
115
|
+
}
|
116
|
+
}
|
117
|
+
function assertEventRetry(retry) {
|
118
|
+
if (!Number.isInteger(retry) || retry < 0) {
|
119
|
+
throw new EventEncoderError("Event-source retry must be a integer and >= 0");
|
120
|
+
}
|
121
|
+
}
|
122
|
+
function encodeEventData(data) {
|
123
|
+
const lines = data ? data.split(/\n/) : [""];
|
124
|
+
let output = "";
|
125
|
+
for (const line of lines) {
|
126
|
+
output += `data: ${line}
|
127
|
+
`;
|
128
|
+
}
|
129
|
+
return output;
|
130
|
+
}
|
131
|
+
function encodeEventMessage(message) {
|
132
|
+
let output = "";
|
133
|
+
if (message.event !== void 0) {
|
134
|
+
assertEventName(message.event);
|
135
|
+
output += `event: ${message.event}
|
136
|
+
`;
|
137
|
+
}
|
138
|
+
if (message.retry !== void 0) {
|
139
|
+
assertEventRetry(message.retry);
|
140
|
+
output += `retry: ${message.retry}
|
141
|
+
`;
|
142
|
+
}
|
143
|
+
if (message.id !== void 0) {
|
144
|
+
assertEventId(message.id);
|
145
|
+
output += `id: ${message.id}
|
146
|
+
`;
|
147
|
+
}
|
148
|
+
output += encodeEventData(message.data);
|
149
|
+
output += "\n";
|
150
|
+
return output;
|
151
|
+
}
|
152
|
+
|
153
|
+
// src/event-source/meta.ts
|
154
|
+
var EVENT_SOURCE_META_SYMBOL = Symbol("ORPC_EVENT_SOURCE_META");
|
155
|
+
function isEventMetaContainer(value) {
|
156
|
+
return !!value && (typeof value === "object" || typeof value === "function");
|
157
|
+
}
|
158
|
+
function withEventMeta(container, meta) {
|
159
|
+
if (meta.id !== void 0) {
|
160
|
+
assertEventId(meta.id);
|
161
|
+
}
|
162
|
+
if (meta.retry !== void 0) {
|
163
|
+
assertEventRetry(meta.retry);
|
164
|
+
}
|
165
|
+
return new Proxy(container, {
|
166
|
+
get(target, prop, receiver) {
|
167
|
+
if (prop === EVENT_SOURCE_META_SYMBOL) {
|
168
|
+
return meta;
|
169
|
+
}
|
170
|
+
return Reflect.get(target, prop, receiver);
|
171
|
+
}
|
172
|
+
});
|
173
|
+
}
|
174
|
+
function getEventMeta(container) {
|
175
|
+
return isEventMetaContainer(container) ? Reflect.get(container, EVENT_SOURCE_META_SYMBOL) : void 0;
|
176
|
+
}
|
177
|
+
|
178
|
+
// src/utils.ts
|
179
|
+
import { contentDisposition, parse } from "@tinyhttp/content-disposition";
|
180
|
+
function once(fn) {
|
181
|
+
let cached;
|
182
|
+
return () => {
|
183
|
+
if (cached) {
|
184
|
+
return cached.result;
|
185
|
+
}
|
186
|
+
const result = fn();
|
187
|
+
cached = { result };
|
188
|
+
return result;
|
189
|
+
};
|
190
|
+
}
|
191
|
+
function parseEmptyableJSON(text) {
|
192
|
+
if (!text) {
|
193
|
+
return void 0;
|
194
|
+
}
|
195
|
+
return JSON.parse(text);
|
196
|
+
}
|
197
|
+
function isAsyncIteratorObject(maybe) {
|
198
|
+
if (!maybe || typeof maybe !== "object") {
|
199
|
+
return false;
|
200
|
+
}
|
201
|
+
return Symbol.asyncIterator in maybe && typeof maybe[Symbol.asyncIterator] === "function";
|
202
|
+
}
|
203
|
+
export {
|
204
|
+
ErrorEvent,
|
205
|
+
EventDecoder,
|
206
|
+
EventDecoderError,
|
207
|
+
EventDecoderStream,
|
208
|
+
EventEncoderError,
|
209
|
+
UnknownEvent,
|
210
|
+
assertEventId,
|
211
|
+
assertEventName,
|
212
|
+
assertEventRetry,
|
213
|
+
contentDisposition,
|
214
|
+
decodeEventMessage,
|
215
|
+
encodeEventData,
|
216
|
+
encodeEventMessage,
|
217
|
+
getEventMeta,
|
218
|
+
isAsyncIteratorObject,
|
219
|
+
isEventMetaContainer,
|
220
|
+
once,
|
221
|
+
parse as parseContentDisposition,
|
222
|
+
parseEmptyableJSON,
|
223
|
+
withEventMeta
|
224
|
+
};
|
225
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import type { EventMessage } from './types';
|
2
|
+
export declare function decodeEventMessage(encoded: string): EventMessage;
|
3
|
+
export interface EventDecoderOptions {
|
4
|
+
onEvent?: (event: EventMessage) => void;
|
5
|
+
}
|
6
|
+
export declare class EventDecoder {
|
7
|
+
private options;
|
8
|
+
private incomplete;
|
9
|
+
constructor(options?: EventDecoderOptions);
|
10
|
+
feed(chunk: string): void;
|
11
|
+
end(): void;
|
12
|
+
}
|
13
|
+
export declare class EventDecoderStream extends TransformStream<string, EventMessage> {
|
14
|
+
constructor();
|
15
|
+
}
|
16
|
+
//# sourceMappingURL=decoder.d.ts.map
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import type { EventMessage } from './types';
|
2
|
+
export declare function assertEventId(id: string): void;
|
3
|
+
export declare function assertEventName(event: string): void;
|
4
|
+
export declare function assertEventRetry(retry: number): void;
|
5
|
+
export declare function encodeEventData(data: string | undefined): string;
|
6
|
+
export declare function encodeEventMessage(message: Partial<EventMessage>): string;
|
7
|
+
//# sourceMappingURL=encoder.d.ts.map
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import type { JsonValue } from 'type-fest';
|
2
|
+
export declare class EventEncoderError extends TypeError {
|
3
|
+
}
|
4
|
+
export declare class EventDecoderError extends TypeError {
|
5
|
+
}
|
6
|
+
export interface ErrorEventOptions extends ErrorOptions {
|
7
|
+
message?: string;
|
8
|
+
data?: undefined | JsonValue;
|
9
|
+
}
|
10
|
+
export declare class ErrorEvent extends Error {
|
11
|
+
data: undefined | JsonValue;
|
12
|
+
constructor(options?: ErrorEventOptions);
|
13
|
+
}
|
14
|
+
export declare class UnknownEvent extends ErrorEvent {
|
15
|
+
}
|
16
|
+
//# sourceMappingURL=errors.d.ts.map
|
@@ -0,0 +1,6 @@
|
|
1
|
+
import type { EventMessage } from './types';
|
2
|
+
export type EventMeta = Partial<Pick<EventMessage, 'retry' | 'id'>>;
|
3
|
+
export declare function isEventMetaContainer(value: unknown): value is Record<PropertyKey, unknown>;
|
4
|
+
export declare function withEventMeta<T extends object>(container: T, meta: EventMeta): T;
|
5
|
+
export declare function getEventMeta(container: unknown): EventMeta | undefined;
|
6
|
+
//# sourceMappingURL=meta.d.ts.map
|
@@ -0,0 +1,10 @@
|
|
1
|
+
export interface EventMessage {
|
2
|
+
event: string | undefined;
|
3
|
+
id: string | undefined;
|
4
|
+
data: string;
|
5
|
+
/**
|
6
|
+
* The number of milliseconds to wait before retrying the event source if error occurs.
|
7
|
+
*/
|
8
|
+
retry: number | undefined;
|
9
|
+
}
|
10
|
+
//# sourceMappingURL=types.d.ts.map
|
@@ -0,0 +1,27 @@
|
|
1
|
+
import type { JsonValue } from 'type-fest';
|
2
|
+
export type { JsonValue };
|
3
|
+
export interface StandardHeaders {
|
4
|
+
[key: string]: string | string[] | undefined;
|
5
|
+
}
|
6
|
+
export type StandardBody = undefined | JsonValue | Blob | URLSearchParams | FormData | AsyncIterator<JsonValue | void, JsonValue | void, undefined>;
|
7
|
+
export interface StandardRequest {
|
8
|
+
/**
|
9
|
+
* Can be { request: Request } or { request: IncomingMessage, response: ServerResponse } based on the adapter.
|
10
|
+
*/
|
11
|
+
raw: Record<string, unknown>;
|
12
|
+
method: string;
|
13
|
+
url: URL;
|
14
|
+
headers: StandardHeaders;
|
15
|
+
/**
|
16
|
+
* The body has been parsed base on the content-type header.
|
17
|
+
* This method can safely call multiple times (cached).
|
18
|
+
*/
|
19
|
+
body: () => Promise<StandardBody>;
|
20
|
+
signal: AbortSignal | undefined;
|
21
|
+
}
|
22
|
+
export interface StandardResponse {
|
23
|
+
status: number;
|
24
|
+
headers: StandardHeaders;
|
25
|
+
body: StandardBody;
|
26
|
+
}
|
27
|
+
//# sourceMappingURL=types.d.ts.map
|
@@ -0,0 +1,6 @@
|
|
1
|
+
import type { JsonValue } from 'type-fest';
|
2
|
+
export declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
3
|
+
export declare function parseEmptyableJSON(text: string): JsonValue | undefined;
|
4
|
+
export declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
5
|
+
export { contentDisposition, parse as parseContentDisposition } from '@tinyhttp/content-disposition';
|
6
|
+
//# sourceMappingURL=utils.d.ts.map
|
package/package.json
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
{
|
2
|
+
"name": "@orpc/standard-server",
|
3
|
+
"type": "module",
|
4
|
+
"version": "0.0.0",
|
5
|
+
"license": "MIT",
|
6
|
+
"homepage": "https://unnoq.com",
|
7
|
+
"repository": {
|
8
|
+
"type": "git",
|
9
|
+
"url": "git+https://github.com/unnoq/orpc.git",
|
10
|
+
"directory": "packages/standard-server"
|
11
|
+
},
|
12
|
+
"keywords": [
|
13
|
+
"orpc"
|
14
|
+
],
|
15
|
+
"exports": {
|
16
|
+
".": {
|
17
|
+
"types": "./dist/src/index.d.ts",
|
18
|
+
"import": "./dist/index.js",
|
19
|
+
"default": "./dist/index.js"
|
20
|
+
},
|
21
|
+
"./🔒/*": {
|
22
|
+
"types": "./dist/src/*.d.ts"
|
23
|
+
}
|
24
|
+
},
|
25
|
+
"files": [
|
26
|
+
"!**/*.map",
|
27
|
+
"!**/*.tsbuildinfo",
|
28
|
+
"dist"
|
29
|
+
],
|
30
|
+
"dependencies": {
|
31
|
+
"@tinyhttp/content-disposition": "^2.2.2",
|
32
|
+
"type-fest": "^4.34.1"
|
33
|
+
},
|
34
|
+
"scripts": {
|
35
|
+
"build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
|
36
|
+
"build:watch": "pnpm run build --watch",
|
37
|
+
"type:check": "tsc -b"
|
38
|
+
}
|
39
|
+
}
|