@milaboratories/pframes-rs-serv 1.1.0 → 1.1.2
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/dist/export.cjs +20 -0
- package/dist/export.cjs.map +1 -0
- package/dist/export.d.ts +3 -0
- package/dist/export.d.ts.map +1 -0
- package/dist/export.js +18 -0
- package/dist/export.js.map +1 -0
- package/dist/fs-store.cjs +91 -0
- package/dist/fs-store.cjs.map +1 -0
- package/dist/fs-store.d.ts +14 -0
- package/dist/fs-store.d.ts.map +1 -0
- package/dist/fs-store.js +89 -0
- package/dist/fs-store.js.map +1 -0
- package/dist/handler.cjs +171 -0
- package/dist/handler.cjs.map +1 -0
- package/dist/handler.d.ts +15 -0
- package/dist/handler.d.ts.map +1 -0
- package/dist/handler.js +168 -0
- package/dist/handler.js.map +1 -0
- package/dist/index.cjs +18 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/parquet-server.cjs +109 -0
- package/dist/parquet-server.cjs.map +1 -0
- package/dist/parquet-server.d.ts +24 -0
- package/dist/parquet-server.d.ts.map +1 -0
- package/dist/parquet-server.js +105 -0
- package/dist/parquet-server.js.map +1 -0
- package/dist/serve.cjs +103 -0
- package/dist/serve.cjs.map +1 -0
- package/dist/serve.d.ts +7 -0
- package/dist/serve.d.ts.map +1 -0
- package/dist/serve.js +101 -0
- package/dist/serve.js.map +1 -0
- package/dist/utils/etag.cjs +10 -0
- package/dist/utils/etag.cjs.map +1 -0
- package/dist/utils/etag.d.ts +15 -0
- package/dist/utils/etag.d.ts.map +1 -0
- package/dist/utils/etag.js +8 -0
- package/dist/utils/etag.js.map +1 -0
- package/dist/utils/filename.cjs +15 -0
- package/dist/utils/filename.cjs.map +1 -0
- package/dist/utils/filename.d.ts +4 -0
- package/dist/utils/filename.d.ts.map +1 -0
- package/dist/utils/filename.js +13 -0
- package/dist/utils/filename.js.map +1 -0
- package/dist/utils/headers.cjs +29 -0
- package/dist/utils/headers.cjs.map +1 -0
- package/dist/utils/headers.d.ts +31 -0
- package/dist/utils/headers.d.ts.map +1 -0
- package/dist/utils/headers.js +26 -0
- package/dist/utils/headers.js.map +1 -0
- package/dist/utils/index.d.ts +8 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/method.cjs +16 -0
- package/dist/utils/method.cjs.map +1 -0
- package/dist/utils/method.d.ts +6 -0
- package/dist/utils/method.d.ts.map +1 -0
- package/dist/utils/method.js +12 -0
- package/dist/utils/method.js.map +1 -0
- package/dist/utils/options.cjs +135 -0
- package/dist/utils/options.cjs.map +1 -0
- package/dist/utils/options.d.ts +64 -0
- package/dist/utils/options.d.ts.map +1 -0
- package/dist/utils/options.js +133 -0
- package/dist/utils/options.js.map +1 -0
- package/dist/utils/range.cjs +34 -0
- package/dist/utils/range.cjs.map +1 -0
- package/dist/utils/range.d.ts +4 -0
- package/dist/utils/range.d.ts.map +1 -0
- package/dist/utils/range.js +32 -0
- package/dist/utils/range.js.map +1 -0
- package/dist/utils/status.cjs +21 -0
- package/dist/utils/status.cjs.map +1 -0
- package/dist/utils/status.d.ts +17 -0
- package/dist/utils/status.d.ts.map +1 -0
- package/dist/utils/status.js +19 -0
- package/dist/utils/status.js.map +1 -0
- package/package.json +8 -8
package/dist/handler.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { pipeline } from 'node:stream/promises';
|
|
2
|
+
import { timingSafeEqual } from 'node:crypto';
|
|
3
|
+
import { getFilenameFromUrl } from './utils/filename.js';
|
|
4
|
+
import { createETag } from './utils/etag.js';
|
|
5
|
+
import { HeaderName, HeaderValue } from './utils/headers.js';
|
|
6
|
+
import { Options } from './utils/options.js';
|
|
7
|
+
import { parseRange } from './utils/range.js';
|
|
8
|
+
import { isGetOrHead, isGet, isHead } from './utils/method.js';
|
|
9
|
+
import { StatusCode } from './utils/status.js';
|
|
10
|
+
import { isAbortError } from '@milaboratories/pl-model-common';
|
|
11
|
+
|
|
12
|
+
/** Main request handler for parquet files */
|
|
13
|
+
function handleRequest(request, response, store) {
|
|
14
|
+
// RFC 9110 section 6.6.1: Date header should be present in all responses
|
|
15
|
+
response.sendDate = true;
|
|
16
|
+
// RFC 9110 section 8.6: Content-Length 0 as default for error responses
|
|
17
|
+
response.strictContentLength = true;
|
|
18
|
+
response.setHeader(HeaderName.ContentLength, 0);
|
|
19
|
+
// Note: setting Content-Length disables Node.js default Transfer-Encoding: chunked
|
|
20
|
+
// RFC 9111 section 5.2: Cache-Control header with public allows to cache authenticated responses
|
|
21
|
+
response.setHeader(HeaderName.CacheControl, HeaderValue.CacheControl);
|
|
22
|
+
// RFC 9110 section 15.5.6: Method not allowed
|
|
23
|
+
const method = request.method;
|
|
24
|
+
if (!isGetOrHead(method)) {
|
|
25
|
+
response.setHeader(HeaderName.Allow, HeaderValue.Allow);
|
|
26
|
+
return void response.writeHead(StatusCode.MethodNotAllowed).end();
|
|
27
|
+
}
|
|
28
|
+
const filename = getFilenameFromUrl(request);
|
|
29
|
+
if (filename === null) {
|
|
30
|
+
return void response.writeHead(StatusCode.Gone).end();
|
|
31
|
+
}
|
|
32
|
+
// From now on we are sure that the response would be a Parquet file
|
|
33
|
+
response.setHeader(HeaderName.AcceptRanges, HeaderValue.AcceptRanges);
|
|
34
|
+
response.setHeader(HeaderName.ContentType, HeaderValue.ContentType);
|
|
35
|
+
// RFC 9110 section 8.8.3: ETag header is used for cache versioning
|
|
36
|
+
const etag = createETag(filename);
|
|
37
|
+
// RFC 9110 section 8.8.2: Last-Modified header field for cache validation
|
|
38
|
+
const mtime = new Date(0); // Using fake fixed date since files are immutable
|
|
39
|
+
// RFC 9111 section 5.2: Cache-Control header with public allows to cache authenticated responses
|
|
40
|
+
response.setHeader(HeaderName.CacheControl, HeaderValue.CacheControl);
|
|
41
|
+
response.setHeader(HeaderName.ETag, etag);
|
|
42
|
+
response.setHeader(HeaderName.LastModified, mtime.toUTCString());
|
|
43
|
+
const options = new Options(request);
|
|
44
|
+
// RFC 9110 section 13.1.1: If-Match precondition evaluation
|
|
45
|
+
// RFC 9110 section 13.1.4: If-Unmodified-Since precondition evaluation
|
|
46
|
+
if (options.preconditionFailed(etag, mtime)) {
|
|
47
|
+
return void response.writeHead(StatusCode.PreconditionFailed).end();
|
|
48
|
+
}
|
|
49
|
+
// RFC 9110 section 13.1.2: If-None-Match precondition evaluation
|
|
50
|
+
// RFC 9110 section 13.1.3: If-Modified-Since precondition evaluation
|
|
51
|
+
else if (options.notModified(etag, mtime)) {
|
|
52
|
+
return void response.writeHead(StatusCode.NotModified).end();
|
|
53
|
+
}
|
|
54
|
+
const range = parseRange(request);
|
|
55
|
+
if (range === null) {
|
|
56
|
+
return void response.writeHead(StatusCode.BadRequest).end();
|
|
57
|
+
}
|
|
58
|
+
const abortController = new AbortController();
|
|
59
|
+
request.on('close', () => abortController.abort());
|
|
60
|
+
const signal = abortController.signal;
|
|
61
|
+
store.request(filename, {
|
|
62
|
+
method,
|
|
63
|
+
range,
|
|
64
|
+
signal,
|
|
65
|
+
// pipeline automatically destroys the streams if they were not gracefully closed
|
|
66
|
+
callback: async (result) => {
|
|
67
|
+
if (request.destroyed) {
|
|
68
|
+
// request has timed out, close the connection
|
|
69
|
+
if (response.destroyed) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
else if (response.headersSent) {
|
|
73
|
+
return void response.end();
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
response.setHeader(HeaderName.Connection, HeaderValue.Connection);
|
|
77
|
+
return void response.writeHead(StatusCode.RequestTimeout).end();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
switch (result.type) {
|
|
81
|
+
case 'InternalError':
|
|
82
|
+
// object store encountered network error, retry by client can help
|
|
83
|
+
return void response.writeHead(StatusCode.InternalServerError).end();
|
|
84
|
+
case 'NotFound':
|
|
85
|
+
// RFC 9110 section 15.4.5: Not found
|
|
86
|
+
return void response.writeHead(StatusCode.NotFound).end();
|
|
87
|
+
case 'RangeNotSatisfiable':
|
|
88
|
+
// RFC 9110 section 15.5.17: Range not satisfiable
|
|
89
|
+
response.setHeader(HeaderName.ContentRange, `bytes */${result.size}`);
|
|
90
|
+
return void response.writeHead(StatusCode.RangeNotSatisfiable).end();
|
|
91
|
+
}
|
|
92
|
+
if (isGet(method) && !result.data) {
|
|
93
|
+
// object store implementation is incorrect, retry by client cannot help
|
|
94
|
+
return void response.writeHead(StatusCode.GatewayTimeout).end();
|
|
95
|
+
}
|
|
96
|
+
if (range) {
|
|
97
|
+
// RFC 9110 section 14.4: Partial content response
|
|
98
|
+
response.setHeader(HeaderName.ContentLength, result.range.end - result.range.start + 1);
|
|
99
|
+
response.setHeader(HeaderName.ContentRange, `bytes ${result.range.start}-${result.range.end}/${result.size}`);
|
|
100
|
+
response.writeHead(StatusCode.PartialContent);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
// RFC 9110 section 15.3.1: OK response
|
|
104
|
+
response.setHeader(HeaderName.ContentLength, result.size);
|
|
105
|
+
response.writeHead(StatusCode.Ok);
|
|
106
|
+
}
|
|
107
|
+
// RFC 9110 section 9.3.2: HEAD method must not return message body
|
|
108
|
+
if (isHead(method)) {
|
|
109
|
+
return void response.end();
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
return await pipeline(result.data, response, { signal });
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
if (!isAbortError(error))
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Create a request handler for serving files from an object store
|
|
123
|
+
* compatible with HTTP/1.1 as defined in RFC 9110 and RFC 9111:
|
|
124
|
+
* - <https://datatracker.ietf.org/doc/html/rfc9110>
|
|
125
|
+
* - <https://datatracker.ietf.org/doc/html/rfc9111>
|
|
126
|
+
*
|
|
127
|
+
* Accepts only paths of the form `/<filename>.parquet`, returns 410 Gone otherwise
|
|
128
|
+
* Assumes that files are immutable (and sets cache headers accordingly)
|
|
129
|
+
*/
|
|
130
|
+
function createRequestHandler(options) {
|
|
131
|
+
const { store } = options;
|
|
132
|
+
return (request, response) => handleRequest(request, response, store);
|
|
133
|
+
}
|
|
134
|
+
/** Request authorization middleware */
|
|
135
|
+
function authorizeRequest(request, response, handler, authHeader) {
|
|
136
|
+
// RFC 9110 section 6.6.1: Date header should be present in all responses
|
|
137
|
+
response.sendDate = true;
|
|
138
|
+
// RFC 9110 section 8.6: Content-Length 0 as default for error responses
|
|
139
|
+
response.strictContentLength = true;
|
|
140
|
+
response.setHeader(HeaderName.ContentLength, 0);
|
|
141
|
+
// Note: setting Content-Length disables Node.js default Transfer-Encoding: chunked
|
|
142
|
+
const actualHeader = request.headers[HeaderName.Authorization];
|
|
143
|
+
// Early length check to avoid unnecessary processing
|
|
144
|
+
if (!actualHeader || actualHeader.length !== authHeader.length) {
|
|
145
|
+
// RFC 9110 section 11.6.1: WWW-Authenticate header field
|
|
146
|
+
response.setHeader(HeaderName.WWWAuthenticate, HeaderValue.WWWAuthenticate);
|
|
147
|
+
return void response.writeHead(StatusCode.Unauthorized).end();
|
|
148
|
+
}
|
|
149
|
+
// Use timing-safe comparison to prevent timing attacks
|
|
150
|
+
// <https://developers.cloudflare.com/workers/examples/protect-against-timing-attacks/>
|
|
151
|
+
const encoder = new TextEncoder();
|
|
152
|
+
const receivedBuffer = encoder.encode(actualHeader);
|
|
153
|
+
const expectedBuffer = encoder.encode(authHeader);
|
|
154
|
+
if (receivedBuffer.byteLength !== expectedBuffer.byteLength ||
|
|
155
|
+
!timingSafeEqual(receivedBuffer, expectedBuffer)) {
|
|
156
|
+
response.setHeader(HeaderName.WWWAuthenticate, HeaderValue.WWWAuthenticate);
|
|
157
|
+
return void response.writeHead(StatusCode.Unauthorized).end();
|
|
158
|
+
}
|
|
159
|
+
return handler(request, response);
|
|
160
|
+
}
|
|
161
|
+
/** Apply Bearer token authorization to @param handler */
|
|
162
|
+
function authorizeRequestHandler(handler, authToken) {
|
|
163
|
+
const authHeader = `Bearer ${authToken}`;
|
|
164
|
+
return (request, response) => authorizeRequest(request, response, handler, authHeader);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export { authorizeRequestHandler, createRequestHandler };
|
|
168
|
+
//# sourceMappingURL=handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.js","sources":["../src/handler.ts"],"sourcesContent":["import type {\n IncomingMessage,\n RequestListener,\n ServerResponse\n} from 'node:http';\nimport { pipeline } from 'node:stream/promises';\nimport { timingSafeEqual } from 'node:crypto';\nimport type { PFrameInternal } from '@milaboratories/pl-model-middle-layer';\nimport {\n createETag,\n getFilenameFromUrl,\n parseRange,\n isGetOrHead,\n isGet,\n isHead,\n Options,\n StatusCode,\n HeaderName,\n HeaderValue\n} from './utils';\nimport { isAbortError } from '@milaboratories/pl-model-common';\n\n/** Main request handler for parquet files */\nfunction handleRequest(\n request: IncomingMessage,\n response: ServerResponse,\n store: PFrameInternal.ObjectStore\n): void {\n // RFC 9110 section 6.6.1: Date header should be present in all responses\n response.sendDate = true;\n // RFC 9110 section 8.6: Content-Length 0 as default for error responses\n response.strictContentLength = true;\n response.setHeader(HeaderName.ContentLength, 0);\n // Note: setting Content-Length disables Node.js default Transfer-Encoding: chunked\n\n // RFC 9111 section 5.2: Cache-Control header with public allows to cache authenticated responses\n response.setHeader(HeaderName.CacheControl, HeaderValue.CacheControl);\n\n // RFC 9110 section 15.5.6: Method not allowed\n const method = request.method;\n if (!isGetOrHead(method)) {\n response.setHeader(HeaderName.Allow, HeaderValue.Allow);\n return void response.writeHead(StatusCode.MethodNotAllowed).end();\n }\n\n const filename = getFilenameFromUrl(request);\n if (filename === null) {\n return void response.writeHead(StatusCode.Gone).end();\n }\n\n // From now on we are sure that the response would be a Parquet file\n response.setHeader(HeaderName.AcceptRanges, HeaderValue.AcceptRanges);\n response.setHeader(HeaderName.ContentType, HeaderValue.ContentType);\n\n // RFC 9110 section 8.8.3: ETag header is used for cache versioning\n const etag = createETag(filename);\n // RFC 9110 section 8.8.2: Last-Modified header field for cache validation\n const mtime = new Date(0); // Using fake fixed date since files are immutable\n // RFC 9111 section 5.2: Cache-Control header with public allows to cache authenticated responses\n response.setHeader(HeaderName.CacheControl, HeaderValue.CacheControl);\n response.setHeader(HeaderName.ETag, etag);\n response.setHeader(HeaderName.LastModified, mtime.toUTCString());\n\n const options = new Options(request);\n // RFC 9110 section 13.1.1: If-Match precondition evaluation\n // RFC 9110 section 13.1.4: If-Unmodified-Since precondition evaluation\n if (options.preconditionFailed(etag, mtime)) {\n return void response.writeHead(StatusCode.PreconditionFailed).end();\n }\n // RFC 9110 section 13.1.2: If-None-Match precondition evaluation\n // RFC 9110 section 13.1.3: If-Modified-Since precondition evaluation\n else if (options.notModified(etag, mtime)) {\n return void response.writeHead(StatusCode.NotModified).end();\n }\n\n const range = parseRange(request);\n if (range === null) {\n return void response.writeHead(StatusCode.BadRequest).end();\n }\n\n const abortController = new AbortController();\n request.on('close', () => abortController.abort());\n const signal = abortController.signal;\n\n store.request(filename, {\n method,\n range,\n signal,\n // pipeline automatically destroys the streams if they were not gracefully closed\n callback: async (result) => {\n if (request.destroyed) {\n // request has timed out, close the connection\n if (response.destroyed) {\n return;\n } else if (response.headersSent) {\n return void response.end();\n } else {\n response.setHeader(HeaderName.Connection, HeaderValue.Connection);\n return void response.writeHead(StatusCode.RequestTimeout).end();\n }\n }\n\n switch (result.type) {\n case 'InternalError':\n // object store encountered network error, retry by client can help\n return void response.writeHead(StatusCode.InternalServerError).end();\n case 'NotFound':\n // RFC 9110 section 15.4.5: Not found\n return void response.writeHead(StatusCode.NotFound).end();\n case 'RangeNotSatisfiable':\n // RFC 9110 section 15.5.17: Range not satisfiable\n response.setHeader(HeaderName.ContentRange, `bytes */${result.size}`);\n return void response.writeHead(StatusCode.RangeNotSatisfiable).end();\n case 'Ok':\n break;\n }\n\n if (isGet(method) && !result.data) {\n // object store implementation is incorrect, retry by client cannot help\n return void response.writeHead(StatusCode.GatewayTimeout).end();\n }\n\n if (range) {\n // RFC 9110 section 14.4: Partial content response\n response.setHeader(\n HeaderName.ContentLength,\n result.range.end - result.range.start + 1\n );\n response.setHeader(\n HeaderName.ContentRange,\n `bytes ${result.range.start}-${result.range.end}/${result.size}`\n );\n response.writeHead(StatusCode.PartialContent);\n } else {\n // RFC 9110 section 15.3.1: OK response\n response.setHeader(HeaderName.ContentLength, result.size);\n response.writeHead(StatusCode.Ok);\n }\n\n // RFC 9110 section 9.3.2: HEAD method must not return message body\n if (isHead(method)) {\n return void response.end();\n }\n\n try {\n return await pipeline(result.data!, response, { signal });\n } catch (error: unknown) {\n if (!isAbortError(error)) throw error;\n }\n }\n });\n}\n\n/**\n * Create a request handler for serving files from an object store\n * compatible with HTTP/1.1 as defined in RFC 9110 and RFC 9111:\n * - <https://datatracker.ietf.org/doc/html/rfc9110>\n * - <https://datatracker.ietf.org/doc/html/rfc9111>\n *\n * Accepts only paths of the form `/<filename>.parquet`, returns 410 Gone otherwise\n * Assumes that files are immutable (and sets cache headers accordingly)\n */\nexport function createRequestHandler(\n options: PFrameInternal.RequestHandlerOptions\n): RequestListener {\n const { store } = options;\n return (request, response) => handleRequest(request, response, store);\n}\n\n/** Request authorization middleware */\nfunction authorizeRequest(\n request: IncomingMessage,\n response: ServerResponse,\n handler: RequestListener,\n authHeader: string\n): void {\n // RFC 9110 section 6.6.1: Date header should be present in all responses\n response.sendDate = true;\n // RFC 9110 section 8.6: Content-Length 0 as default for error responses\n response.strictContentLength = true;\n response.setHeader(HeaderName.ContentLength, 0);\n // Note: setting Content-Length disables Node.js default Transfer-Encoding: chunked\n\n const actualHeader = request.headers[HeaderName.Authorization];\n\n // Early length check to avoid unnecessary processing\n if (!actualHeader || actualHeader.length !== authHeader.length) {\n // RFC 9110 section 11.6.1: WWW-Authenticate header field\n response.setHeader(HeaderName.WWWAuthenticate, HeaderValue.WWWAuthenticate);\n return void response.writeHead(StatusCode.Unauthorized).end();\n }\n\n // Use timing-safe comparison to prevent timing attacks\n // <https://developers.cloudflare.com/workers/examples/protect-against-timing-attacks/>\n const encoder = new TextEncoder();\n const receivedBuffer = encoder.encode(actualHeader);\n const expectedBuffer = encoder.encode(authHeader);\n\n if (\n receivedBuffer.byteLength !== expectedBuffer.byteLength ||\n !timingSafeEqual(receivedBuffer, expectedBuffer)\n ) {\n response.setHeader(HeaderName.WWWAuthenticate, HeaderValue.WWWAuthenticate);\n return void response.writeHead(StatusCode.Unauthorized).end();\n }\n\n return handler(request, response);\n}\n\n/** Apply Bearer token authorization to @param handler */\nexport function authorizeRequestHandler(\n handler: RequestListener,\n authToken: PFrameInternal.HttpAuthorizationToken\n): RequestListener {\n const authHeader = `Bearer ${authToken}`;\n return (request, response) =>\n authorizeRequest(request, response, handler, authHeader);\n}\n"],"names":[],"mappings":";;;;;;;;;;;AAsBA;AACA,SAAS,aAAa,CACpB,OAAwB,EACxB,QAAwB,EACxB,KAAiC,EAAA;;AAGjC,IAAA,QAAQ,CAAC,QAAQ,GAAG,IAAI;;AAExB,IAAA,QAAQ,CAAC,mBAAmB,GAAG,IAAI;IACnC,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,aAAa,EAAE,CAAC,CAAC;;;IAI/C,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,YAAY,EAAE,WAAW,CAAC,YAAY,CAAC;;AAGrE,IAAA,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM;AAC7B,IAAA,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE;QACxB,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,KAAK,EAAE,WAAW,CAAC,KAAK,CAAC;AACvD,QAAA,OAAO,KAAK,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC,GAAG,EAAE;IACnE;AAEA,IAAA,MAAM,QAAQ,GAAG,kB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O,OAAO,CAAC,OAAO,EAAE,QAAQ,CAAC;AACnC;AAEA;AACM,SAAU,uBAAuB,CACrC,OAAwB,EACxB,SAAgD,EAAA;AAEhD,IAAA,MAAM,UAAU,GAAG,CAAA,OAAA,EAAU,SAAS,EAAE;AACxC,IAAA,OAAO,CAAC,OAAO,EAAE,QAAQ,KACvB,gBAAgB,CAAC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,CAAC;AAC5D;;;;"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var handler = require('./handler.cjs');
|
|
4
|
+
var fsStore = require('./fs-store.cjs');
|
|
5
|
+
var serve = require('./serve.cjs');
|
|
6
|
+
var _export = require('./export.cjs');
|
|
7
|
+
var parquetServer = require('./parquet-server.cjs');
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
exports.authorizeRequestHandler = handler.authorizeRequestHandler;
|
|
12
|
+
exports.createRequestHandler = handler.createRequestHandler;
|
|
13
|
+
exports.FileSystemStore = fsStore.FileSystemStore;
|
|
14
|
+
exports.serve = serve.serve;
|
|
15
|
+
exports.HttpHelpers = _export.HttpHelpers;
|
|
16
|
+
exports.ParquetServer = parquetServer.ParquetServer;
|
|
17
|
+
exports.runParquetServer = parquetServer.runParquetServer;
|
|
18
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { authorizeRequestHandler, createRequestHandler } from './handler.js';
|
|
2
|
+
export { FileSystemStore } from './fs-store.js';
|
|
3
|
+
export { serve } from './serve.js';
|
|
4
|
+
export { HttpHelpers } from './export.js';
|
|
5
|
+
export { ParquetServer, runParquetServer } from './parquet-server.js';
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var node_child_process = require('node:child_process');
|
|
4
|
+
var promises = require('node:readline/promises');
|
|
5
|
+
var node_path = require('node:path');
|
|
6
|
+
var node_url = require('node:url');
|
|
7
|
+
var commander = require('commander');
|
|
8
|
+
var _export = require('./export.cjs');
|
|
9
|
+
var plModelCommon = require('@milaboratories/pl-model-common');
|
|
10
|
+
|
|
11
|
+
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
12
|
+
const Options = {
|
|
13
|
+
NoHttps: '--no-https',
|
|
14
|
+
NoAuth: '--no-auth',
|
|
15
|
+
Port: '--port'
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Serves parquet files from the given root directory.
|
|
19
|
+
* Manages the server lifecycle with graceful shutdown.
|
|
20
|
+
*/
|
|
21
|
+
async function runParquetServer() {
|
|
22
|
+
const program = new commander.Command();
|
|
23
|
+
program
|
|
24
|
+
.name('parquet-server')
|
|
25
|
+
.description('Serve parquet files from a directory over HTTP(S)')
|
|
26
|
+
.argument('<root-directory>', 'Root directory containing parquet files')
|
|
27
|
+
.option(Options.NoHttps, 'Downgrade HTTPS to HTTP')
|
|
28
|
+
.option(Options.NoAuth, 'Disable authorization')
|
|
29
|
+
.option(`${Options.Port} <number>`, 'Port to listen on', (value) => {
|
|
30
|
+
const port = parseInt(value, 10);
|
|
31
|
+
if (isNaN(port) || port < 0 || port > 65535) {
|
|
32
|
+
throw new commander.InvalidArgumentError('valid port numbers are 0-65535');
|
|
33
|
+
}
|
|
34
|
+
return port;
|
|
35
|
+
}, 0)
|
|
36
|
+
.action(async (rootDir, options) => {
|
|
37
|
+
const abortController = new AbortController();
|
|
38
|
+
process
|
|
39
|
+
.on('SIGINT', () => abortController.abort())
|
|
40
|
+
.on('SIGTERM', () => abortController.abort());
|
|
41
|
+
abortController.signal.throwIfAborted();
|
|
42
|
+
const store = await _export.HttpHelpers.createFsStore({
|
|
43
|
+
rootDir,
|
|
44
|
+
logger: (level, message) => {
|
|
45
|
+
const timestamp = new Date(Date.now()).toISOString();
|
|
46
|
+
console.log(`[${timestamp}] [${level}] ${message}`);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
abortController.signal.throwIfAborted();
|
|
50
|
+
const handler = _export.HttpHelpers.createRequestHandler({ store });
|
|
51
|
+
const server = await _export.HttpHelpers.createHttpServer({
|
|
52
|
+
handler,
|
|
53
|
+
...(!options.https && { noHttps: true }),
|
|
54
|
+
...(!options.auth && { noAuth: true }),
|
|
55
|
+
port: options.port
|
|
56
|
+
});
|
|
57
|
+
abortController.signal.onabort = () => server.stop();
|
|
58
|
+
abortController.signal.throwIfAborted();
|
|
59
|
+
const serverInfo = plModelCommon.stringifyJson(server.info);
|
|
60
|
+
console.log(serverInfo);
|
|
61
|
+
await server.stopped;
|
|
62
|
+
});
|
|
63
|
+
await program.parseAsync();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Reference implementation of a parquet server runner for tests:
|
|
67
|
+
* - Reads the server configuration from the spawned process stdout
|
|
68
|
+
* - Forwards the server logs to the console
|
|
69
|
+
* - Shuts down the server on dispose
|
|
70
|
+
*/
|
|
71
|
+
class ParquetServer {
|
|
72
|
+
#process;
|
|
73
|
+
#info;
|
|
74
|
+
#lineReader;
|
|
75
|
+
constructor(process, info, lineReader) {
|
|
76
|
+
this.#process = process;
|
|
77
|
+
this.#info = info;
|
|
78
|
+
this.#lineReader = lineReader;
|
|
79
|
+
}
|
|
80
|
+
get info() {
|
|
81
|
+
return this.#info;
|
|
82
|
+
}
|
|
83
|
+
static async serve(rootDir, options) {
|
|
84
|
+
const nodeDirname = node_path.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('parquet-server.cjs', document.baseURI).href))));
|
|
85
|
+
const binPath = node_path.join(nodeDirname, '..', 'bin', 'parquet-server.mjs');
|
|
86
|
+
const serverProcess = node_child_process.spawn('node', [
|
|
87
|
+
binPath,
|
|
88
|
+
rootDir,
|
|
89
|
+
...(options?.noHttps ? [Options.NoHttps] : []),
|
|
90
|
+
...(options?.noAuth ? [Options.NoAuth] : []),
|
|
91
|
+
...(options?.port ? [Options.Port, options.port.toString()] : [])
|
|
92
|
+
], {
|
|
93
|
+
stdio: ['ignore', 'pipe', 'ignore']
|
|
94
|
+
});
|
|
95
|
+
const lineReader = promises.createInterface({ input: serverProcess.stdout });
|
|
96
|
+
const firstLine = await lineReader[Symbol.asyncIterator]().next();
|
|
97
|
+
const serverInfo = plModelCommon.parseJson(firstLine.value);
|
|
98
|
+
lineReader.on('line', console.log);
|
|
99
|
+
return new ParquetServer(serverProcess, serverInfo, lineReader);
|
|
100
|
+
}
|
|
101
|
+
[Symbol.dispose]() {
|
|
102
|
+
this.#lineReader.close();
|
|
103
|
+
this.#process.kill();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
exports.ParquetServer = ParquetServer;
|
|
108
|
+
exports.runParquetServer = runParquetServer;
|
|
109
|
+
//# sourceMappingURL=parquet-server.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parquet-server.cjs","sources":["../src/parquet-server.ts"],"sourcesContent":["import { type ChildProcess, spawn } from 'node:child_process';\nimport { createInterface, type Interface } from 'node:readline/promises';\nimport { join, dirname } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { Command, InvalidArgumentError } from 'commander';\nimport { HttpHelpers } from './export';\nimport {\n parseJson,\n stringifyJson,\n type StringifiedJson\n} from '@milaboratories/pl-model-common';\nimport { type PFrameInternal } from '@milaboratories/pl-model-middle-layer';\n\nconst Options = {\n NoHttps: '--no-https',\n NoAuth: '--no-auth',\n Port: '--port'\n} as const;\n\ntype Info = StringifiedJson<PFrameInternal.HttpServerInfo>;\n\n/**\n * Serves parquet files from the given root directory.\n * Manages the server lifecycle with graceful shutdown.\n */\nexport async function runParquetServer(): Promise<void> {\n const program = new Command();\n\n program\n .name('parquet-server')\n .description('Serve parquet files from a directory over HTTP(S)')\n .argument('<root-directory>', 'Root directory containing parquet files')\n .option(Options.NoHttps, 'Downgrade HTTPS to HTTP')\n .option(Options.NoAuth, 'Disable authorization')\n .option(\n `${Options.Port} <number>`,\n 'Port to listen on',\n (value) => {\n const port = parseInt(value, 10);\n if (isNaN(port) || port < 0 || port > 65535) {\n throw new InvalidArgumentError('valid port numbers are 0-65535');\n }\n return port;\n },\n 0\n )\n .action(\n async (\n rootDir: string,\n options: {\n https: boolean;\n auth: boolean;\n port: number;\n }\n ) => {\n const abortController = new AbortController();\n process\n .on('SIGINT', () => abortController.abort())\n .on('SIGTERM', () => abortController.abort());\n abortController.signal.throwIfAborted();\n\n const store = await HttpHelpers.createFsStore({\n rootDir,\n logger: (level, message) => {\n const timestamp = new Date(Date.now()).toISOString();\n console.log(`[${timestamp}] [${level}] ${message}`);\n }\n });\n abortController.signal.throwIfAborted();\n const handler = HttpHelpers.createRequestHandler({ store });\n\n const server = await HttpHelpers.createHttpServer({\n handler,\n ...(!options.https && { noHttps: true }),\n ...(!options.auth && { noAuth: true }),\n port: options.port\n });\n abortController.signal.onabort = () => server.stop();\n abortController.signal.throwIfAborted();\n\n const serverInfo: Info = stringifyJson(server.info);\n console.log(serverInfo);\n\n await server.stopped;\n }\n );\n\n await program.parseAsync();\n}\n\n/**\n * Reference implementation of a parquet server runner for tests:\n * - Reads the server configuration from the spawned process stdout\n * - Forwards the server logs to the console\n * - Shuts down the server on dispose\n */\nexport class ParquetServer implements Disposable {\n readonly #process: ChildProcess;\n readonly #info: PFrameInternal.HttpServerInfo;\n readonly #lineReader: Interface;\n\n private constructor(\n process: ChildProcess,\n info: PFrameInternal.HttpServerInfo,\n lineReader: Interface\n ) {\n this.#process = process;\n this.#info = info;\n this.#lineReader = lineReader;\n }\n\n get info(): PFrameInternal.HttpServerInfo {\n return this.#info;\n }\n\n static async serve(\n rootDir: string,\n options?: {\n noHttps?: true;\n noAuth?: true;\n port?: number;\n }\n ): Promise<ParquetServer> {\n const nodeDirname = dirname(fileURLToPath(import.meta.url));\n const binPath = join(nodeDirname, '..', 'bin', 'parquet-server.mjs');\n\n const serverProcess = spawn(\n 'node',\n [\n binPath,\n rootDir,\n ...(options?.noHttps ? [Options.NoHttps] : []),\n ...(options?.noAuth ? [Options.NoAuth] : []),\n ...(options?.port ? [Options.Port, options.port.toString()] : [])\n ],\n {\n stdio: ['ignore', 'pipe', 'ignore']\n }\n );\n\n const lineReader = createInterface({ input: serverProcess.stdout! });\n\n const firstLine = await lineReader[Symbol.asyncIterator]().next();\n const serverInfo = parseJson(firstLine.value as Info);\n\n lineReader.on('line', console.log);\n\n return new ParquetServer(serverProcess, serverInfo, lineReader);\n }\n\n [Symbol.dispose](): void {\n this.#lineReader.close();\n this.#process.kill();\n }\n}\n"],"names":["Command","InvalidArgumentError","HttpHelpers","stringifyJson","dirname","fileURLToPath","join","spawn","createInterface","parseJson"],"mappings":";;;;;;;;;;;AAaA,MAAM,OAAO,GAAG;AACd,IAAA,OAAO,EAAE,YAAY;AACrB,IAAA,MAAM,EAAE,WAAW;AACnB,IAAA,IAAI,EAAE;CACE;AAIV;;;AAGG;AACI,eAAe,gBAAgB,GAAA;AACpC,IAAA,MAAM,OAAO,GAAG,I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|
3
|
+
* Serves parquet files from the given root directory.
|
|
4
|
+
* Manages the server lifecycle with graceful shutdown.
|
|
5
|
+
*/
|
|
6
|
+
export declare function runParquetServer(): Promise<void>;
|
|
7
|
+
/**
|
|
8
|
+
* Reference implementation of a parquet server runner for tests:
|
|
9
|
+
* - Reads the server configuration from the spawned process stdout
|
|
10
|
+
* - Forwards the server logs to the console
|
|
11
|
+
* - Shuts down the server on dispose
|
|
12
|
+
*/
|
|
13
|
+
export declare class ParquetServer implements Disposable {
|
|
14
|
+
#private;
|
|
15
|
+
private constructor();
|
|
16
|
+
get info(): PFrameInternal.HttpServerInfo;
|
|
17
|
+
static serve(rootDir: string, options?: {
|
|
18
|
+
noHttps?: true;
|
|
19
|
+
noAuth?: true;
|
|
20
|
+
port?: number;
|
|
21
|
+
}): Promise<ParquetServer>;
|
|
22
|
+
[Symbol.dispose](): void;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=parquet-server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parquet-server.d.ts","sourceRoot":"","sources":["../src/parquet-server.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,uCAAuC,CAAC;AAU5E;;;GAGG;AACH,wBAAsB,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC,CA+DtD;AAED;;;;;GAKG;AACH,qBAAa,aAAc,YAAW,UAAU;;IAK9C,OAAO;IAUP,IAAI,IAAI,IAAI,cAAc,CAAC,cAAc,CAExC;WAEY,KAAK,CAChB,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;QACR,OAAO,CAAC,EAAE,IAAI,CAAC;QACf,MAAM,CAAC,EAAE,IAAI,CAAC;QACd,IAAI,CAAC,EAAE,MAAM,CAAC;KACf,GACA,OAAO,CAAC,aAAa,CAAC;IA4BzB,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI;CAIzB"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { createInterface } from 'node:readline/promises';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { Command, InvalidArgumentError } from 'commander';
|
|
6
|
+
import { HttpHelpers } from './export.js';
|
|
7
|
+
import { stringifyJson, parseJson } from '@milaboratories/pl-model-common';
|
|
8
|
+
|
|
9
|
+
const Options = {
|
|
10
|
+
NoHttps: '--no-https',
|
|
11
|
+
NoAuth: '--no-auth',
|
|
12
|
+
Port: '--port'
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Serves parquet files from the given root directory.
|
|
16
|
+
* Manages the server lifecycle with graceful shutdown.
|
|
17
|
+
*/
|
|
18
|
+
async function runParquetServer() {
|
|
19
|
+
const program = new Command();
|
|
20
|
+
program
|
|
21
|
+
.name('parquet-server')
|
|
22
|
+
.description('Serve parquet files from a directory over HTTP(S)')
|
|
23
|
+
.argument('<root-directory>', 'Root directory containing parquet files')
|
|
24
|
+
.option(Options.NoHttps, 'Downgrade HTTPS to HTTP')
|
|
25
|
+
.option(Options.NoAuth, 'Disable authorization')
|
|
26
|
+
.option(`${Options.Port} <number>`, 'Port to listen on', (value) => {
|
|
27
|
+
const port = parseInt(value, 10);
|
|
28
|
+
if (isNaN(port) || port < 0 || port > 65535) {
|
|
29
|
+
throw new InvalidArgumentError('valid port numbers are 0-65535');
|
|
30
|
+
}
|
|
31
|
+
return port;
|
|
32
|
+
}, 0)
|
|
33
|
+
.action(async (rootDir, options) => {
|
|
34
|
+
const abortController = new AbortController();
|
|
35
|
+
process
|
|
36
|
+
.on('SIGINT', () => abortController.abort())
|
|
37
|
+
.on('SIGTERM', () => abortController.abort());
|
|
38
|
+
abortController.signal.throwIfAborted();
|
|
39
|
+
const store = await HttpHelpers.createFsStore({
|
|
40
|
+
rootDir,
|
|
41
|
+
logger: (level, message) => {
|
|
42
|
+
const timestamp = new Date(Date.now()).toISOString();
|
|
43
|
+
console.log(`[${timestamp}] [${level}] ${message}`);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
abortController.signal.throwIfAborted();
|
|
47
|
+
const handler = HttpHelpers.createRequestHandler({ store });
|
|
48
|
+
const server = await HttpHelpers.createHttpServer({
|
|
49
|
+
handler,
|
|
50
|
+
...(!options.https && { noHttps: true }),
|
|
51
|
+
...(!options.auth && { noAuth: true }),
|
|
52
|
+
port: options.port
|
|
53
|
+
});
|
|
54
|
+
abortController.signal.onabort = () => server.stop();
|
|
55
|
+
abortController.signal.throwIfAborted();
|
|
56
|
+
const serverInfo = stringifyJson(server.info);
|
|
57
|
+
console.log(serverInfo);
|
|
58
|
+
await server.stopped;
|
|
59
|
+
});
|
|
60
|
+
await program.parseAsync();
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Reference implementation of a parquet server runner for tests:
|
|
64
|
+
* - Reads the server configuration from the spawned process stdout
|
|
65
|
+
* - Forwards the server logs to the console
|
|
66
|
+
* - Shuts down the server on dispose
|
|
67
|
+
*/
|
|
68
|
+
class ParquetServer {
|
|
69
|
+
#process;
|
|
70
|
+
#info;
|
|
71
|
+
#lineReader;
|
|
72
|
+
constructor(process, info, lineReader) {
|
|
73
|
+
this.#process = process;
|
|
74
|
+
this.#info = info;
|
|
75
|
+
this.#lineReader = lineReader;
|
|
76
|
+
}
|
|
77
|
+
get info() {
|
|
78
|
+
return this.#info;
|
|
79
|
+
}
|
|
80
|
+
static async serve(rootDir, options) {
|
|
81
|
+
const nodeDirname = dirname(fileURLToPath(import.meta.url));
|
|
82
|
+
const binPath = join(nodeDirname, '..', 'bin', 'parquet-server.mjs');
|
|
83
|
+
const serverProcess = spawn('node', [
|
|
84
|
+
binPath,
|
|
85
|
+
rootDir,
|
|
86
|
+
...(options?.noHttps ? [Options.NoHttps] : []),
|
|
87
|
+
...(options?.noAuth ? [Options.NoAuth] : []),
|
|
88
|
+
...(options?.port ? [Options.Port, options.port.toString()] : [])
|
|
89
|
+
], {
|
|
90
|
+
stdio: ['ignore', 'pipe', 'ignore']
|
|
91
|
+
});
|
|
92
|
+
const lineReader = createInterface({ input: serverProcess.stdout });
|
|
93
|
+
const firstLine = await lineReader[Symbol.asyncIterator]().next();
|
|
94
|
+
const serverInfo = parseJson(firstLine.value);
|
|
95
|
+
lineReader.on('line', console.log);
|
|
96
|
+
return new ParquetServer(serverProcess, serverInfo, lineReader);
|
|
97
|
+
}
|
|
98
|
+
[Symbol.dispose]() {
|
|
99
|
+
this.#lineReader.close();
|
|
100
|
+
this.#process.kill();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export { ParquetServer, runParquetServer };
|
|
105
|
+
//# sourceMappingURL=parquet-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parquet-server.js","sources":["../src/parquet-server.ts"],"sourcesContent":["import { type ChildProcess, spawn } from 'node:child_process';\nimport { createInterface, type Interface } from 'node:readline/promises';\nimport { join, dirname } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { Command, InvalidArgumentError } from 'commander';\nimport { HttpHelpers } from './export';\nimport {\n parseJson,\n stringifyJson,\n type StringifiedJson\n} from '@milaboratories/pl-model-common';\nimport { type PFrameInternal } from '@milaboratories/pl-model-middle-layer';\n\nconst Options = {\n NoHttps: '--no-https',\n NoAuth: '--no-auth',\n Port: '--port'\n} as const;\n\ntype Info = StringifiedJson<PFrameInternal.HttpServerInfo>;\n\n/**\n * Serves parquet files from the given root directory.\n * Manages the server lifecycle with graceful shutdown.\n */\nexport async function runParquetServer(): Promise<void> {\n const program = new Command();\n\n program\n .name('parquet-server')\n .description('Serve parquet files from a directory over HTTP(S)')\n .argument('<root-directory>', 'Root directory containing parquet files')\n .option(Options.NoHttps, 'Downgrade HTTPS to HTTP')\n .option(Options.NoAuth, 'Disable authorization')\n .option(\n `${Options.Port} <number>`,\n 'Port to listen on',\n (value) => {\n const port = parseInt(value, 10);\n if (isNaN(port) || port < 0 || port > 65535) {\n throw new InvalidArgumentError('valid port numbers are 0-65535');\n }\n return port;\n },\n 0\n )\n .action(\n async (\n rootDir: string,\n options: {\n https: boolean;\n auth: boolean;\n port: number;\n }\n ) => {\n const abortController = new AbortController();\n process\n .on('SIGINT', () => abortController.abort())\n .on('SIGTERM', () => abortController.abort());\n abortController.signal.throwIfAborted();\n\n const store = await HttpHelpers.createFsStore({\n rootDir,\n logger: (level, message) => {\n const timestamp = new Date(Date.now()).toISOString();\n console.log(`[${timestamp}] [${level}] ${message}`);\n }\n });\n abortController.signal.throwIfAborted();\n const handler = HttpHelpers.createRequestHandler({ store });\n\n const server = await HttpHelpers.createHttpServer({\n handler,\n ...(!options.https && { noHttps: true }),\n ...(!options.auth && { noAuth: true }),\n port: options.port\n });\n abortController.signal.onabort = () => server.stop();\n abortController.signal.throwIfAborted();\n\n const serverInfo: Info = stringifyJson(server.info);\n console.log(serverInfo);\n\n await server.stopped;\n }\n );\n\n await program.parseAsync();\n}\n\n/**\n * Reference implementation of a parquet server runner for tests:\n * - Reads the server configuration from the spawned process stdout\n * - Forwards the server logs to the console\n * - Shuts down the server on dispose\n */\nexport class ParquetServer implements Disposable {\n readonly #process: ChildProcess;\n readonly #info: PFrameInternal.HttpServerInfo;\n readonly #lineReader: Interface;\n\n private constructor(\n process: ChildProcess,\n info: PFrameInternal.HttpServerInfo,\n lineReader: Interface\n ) {\n this.#process = process;\n this.#info = info;\n this.#lineReader = lineReader;\n }\n\n get info(): PFrameInternal.HttpServerInfo {\n return this.#info;\n }\n\n static async serve(\n rootDir: string,\n options?: {\n noHttps?: true;\n noAuth?: true;\n port?: number;\n }\n ): Promise<ParquetServer> {\n const nodeDirname = dirname(fileURLToPath(import.meta.url));\n const binPath = join(nodeDirname, '..', 'bin', 'parquet-server.mjs');\n\n const serverProcess = spawn(\n 'node',\n [\n binPath,\n rootDir,\n ...(options?.noHttps ? [Options.NoHttps] : []),\n ...(options?.noAuth ? [Options.NoAuth] : []),\n ...(options?.port ? [Options.Port, options.port.toString()] : [])\n ],\n {\n stdio: ['ignore', 'pipe', 'ignore']\n }\n );\n\n const lineReader = createInterface({ input: serverProcess.stdout! });\n\n const firstLine = await lineReader[Symbol.asyncIterator]().next();\n const serverInfo = parseJson(firstLine.value as Info);\n\n lineReader.on('line', console.log);\n\n return new ParquetServer(serverProcess, serverInfo, lineReader);\n }\n\n [Symbol.dispose](): void {\n this.#lineReader.close();\n this.#process.kill();\n }\n}\n"],"names":[],"mappings":";;;;;;;;AAaA,MAAM,OAAO,GAAG;AACd,IAAA,OAAO,EAAE,YAAY;AACrB,IAAA,MAAM,EAAE,WAAW;AACnB,IAAA,IAAI,EAAE;CACE;AAIV;;;AAGG;AACI,eAAe,gBAAgB,GAAA;AACpC,IAAA,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE;IAE7B;SACG,IAAI,CAAC,gBAAgB;SACrB,WAAW,CAAC,mDAAmD;AAC/D,SAAA,QAAQ,CAAC,kBAAkB,EAAE,yCAAyC;AACtE,SAAA,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,yBAAyB;AACjD,SAAA,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,uBAAuB;AAC9C,SAAA,MAAM,CACL,CAAA,EAAG,OAAO,CAAC,IAAI,CAAA,SAAA,CAAW,EAC1B,mBAAmB,EACnB,CAAC,KAAK,KAAI;QACR,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC;AAChC,QAAA,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,IAAI,IAAI,GAAG,KAAK,EAAE;AAC3C,YAAA,MAAM,IAAI,oBAAoB,CAAC,gCAAgC,CAAC;QAClE;AACA,QAAA,OAAO,IAAI;IACb,CAAC,EACD,CAAC;AAEF,SAAA,MAAM,CACL,OACE,OAAe,EACf,OAIC,KACC;AACF,QAAA,MAAM,eAAe,GAAG,IAAI,eAAe,EAAE;QAC7C;aACG,EAAE,CAAC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,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE;QACjE,MAAM,UAAU,GAAG,SAAS,CAAC,SAAS,CAAC,KAAa,CAAC;QAErD,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC;QAElC,OAAO,IAAI,aAAa,CAAC,aAAa,EAAE,UAAU,EAAE,UAAU,CAAC;IACjE;IAEA,CAAC,MAAM,CAAC,OAAO,CAAC,GAAA;AACd,QAAA,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE;AACxB,QAAA,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE;IACtB;AACD;;;;"}
|
package/dist/serve.cjs
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var node_http = require('node:http');
|
|
4
|
+
var node_https = require('node:https');
|
|
5
|
+
var helpers = require('@milaboratories/helpers');
|
|
6
|
+
var plModelCommon = require('@milaboratories/pl-model-common');
|
|
7
|
+
var selfsigned = require('selfsigned');
|
|
8
|
+
var node_crypto = require('node:crypto');
|
|
9
|
+
var handler = require('./handler.cjs');
|
|
10
|
+
|
|
11
|
+
/** Generate a self-signed certificate for localhost */
|
|
12
|
+
async function generateCertificate() {
|
|
13
|
+
return await selfsigned.generate([{ name: 'commonName', value: 'localhost' }], {
|
|
14
|
+
keySize: 2048,
|
|
15
|
+
algorithm: 'sha256',
|
|
16
|
+
extensions: [
|
|
17
|
+
{
|
|
18
|
+
name: 'subjectAltName',
|
|
19
|
+
altNames: [
|
|
20
|
+
{ type: 2, value: 'localhost' }, // DNS
|
|
21
|
+
{ type: 7, ip: '127.0.0.1' }, // IPv4
|
|
22
|
+
{ type: 7, ip: '::1' } // IPv6
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
]
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/** Create an object store URL from the server address info. */
|
|
29
|
+
function createObjectStoreUrl(info, noHttps) {
|
|
30
|
+
const protocol = noHttps ? 'http' : 'https';
|
|
31
|
+
switch (info.family) {
|
|
32
|
+
case 'IPv4':
|
|
33
|
+
return `${protocol}://${info.address}:${info.port}/`;
|
|
34
|
+
case 'IPv6':
|
|
35
|
+
return `${protocol}://[${info.address}]:${info.port}/`;
|
|
36
|
+
default:
|
|
37
|
+
return `${protocol}://localhost:${info.port}/`;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Serve HTTP requests using the provided handler.
|
|
42
|
+
* Returns a promise that resolves when the server is stopped.
|
|
43
|
+
*/
|
|
44
|
+
async function serve({ handler: handler$1, port = 0, noHttps, noAuth }) {
|
|
45
|
+
const started = new helpers.Deferred();
|
|
46
|
+
try {
|
|
47
|
+
let stopped = null;
|
|
48
|
+
let authToken;
|
|
49
|
+
let effectiveHandler = handler$1;
|
|
50
|
+
if (!noAuth) {
|
|
51
|
+
authToken = node_crypto.randomUUID();
|
|
52
|
+
effectiveHandler = handler.authorizeRequestHandler(effectiveHandler, authToken);
|
|
53
|
+
}
|
|
54
|
+
// Create HTTP server
|
|
55
|
+
let encodedCaCert;
|
|
56
|
+
const defaultOptions = {
|
|
57
|
+
keepAlive: true
|
|
58
|
+
};
|
|
59
|
+
let server;
|
|
60
|
+
if (noHttps) {
|
|
61
|
+
server = node_http.createServer(defaultOptions, effectiveHandler);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
const { cert, private: key, public: ca } = await generateCertificate();
|
|
65
|
+
encodedCaCert = plModelCommon.base64Encode(cert);
|
|
66
|
+
server = node_https.createServer({ ...defaultOptions, cert, key, ca }, effectiveHandler);
|
|
67
|
+
}
|
|
68
|
+
server
|
|
69
|
+
.on('listening', () => {
|
|
70
|
+
// Cast is safe by specification <https://nodejs.org/api/net.html#serveraddress>
|
|
71
|
+
const url = createObjectStoreUrl(server.address(), noHttps);
|
|
72
|
+
stopped = new helpers.Deferred();
|
|
73
|
+
started.resolve({
|
|
74
|
+
get info() {
|
|
75
|
+
return { url, authToken, encodedCaCert };
|
|
76
|
+
},
|
|
77
|
+
get stopped() {
|
|
78
|
+
return stopped.promise;
|
|
79
|
+
},
|
|
80
|
+
stop() {
|
|
81
|
+
server.close();
|
|
82
|
+
return stopped.promise;
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
})
|
|
86
|
+
.on('error', (err) => {
|
|
87
|
+
started.reject(err);
|
|
88
|
+
stopped?.reject(err);
|
|
89
|
+
})
|
|
90
|
+
.on('close', () => stopped?.resolve())
|
|
91
|
+
.listen({
|
|
92
|
+
host: 'localhost',
|
|
93
|
+
port
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
started.reject(plModelCommon.ensureError(error));
|
|
98
|
+
}
|
|
99
|
+
return started.promise;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
exports.serve = serve;
|
|
103
|
+
//# sourceMappingURL=serve.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serve.cjs","sources":["../src/serve.ts"],"sourcesContent":["import {\n createServer as createHttpServer,\n type RequestListener,\n type Server as HttpServer,\n type ServerOptions\n} from 'node:http';\nimport {\n createServer as createHttpsServer,\n type Server as HttpsServer\n} from 'node:https';\nimport type { AddressInfo } from 'node:net';\nimport { Deferred } from '@milaboratories/helpers';\nimport type { PFrameInternal } from '@milaboratories/pl-model-middle-layer';\nimport {\n base64Encode,\n Base64Encoded,\n ensureError\n} from '@milaboratories/pl-model-common';\nimport { generate, type GenerateResult } from 'selfsigned';\nimport { randomUUID } from 'node:crypto';\nimport { authorizeRequestHandler } from './handler';\n\n/** Generate a self-signed certificate for localhost */\nasync function generateCertificate(): Promise<GenerateResult> {\n return await generate([{ name: 'commonName', value: 'localhost' }], {\n keySize: 2048,\n algorithm: 'sha256',\n extensions: [\n {\n name: 'subjectAltName',\n altNames: [\n { type: 2, value: 'localhost' }, // DNS\n { type: 7, ip: '127.0.0.1' }, // IPv4\n { type: 7, ip: '::1' } // IPv6\n ]\n }\n ]\n });\n}\n\n/** Create an object store URL from the server address info. */\nfunction createObjectStoreUrl(\n info: AddressInfo,\n noHttps?: true\n): PFrameInternal.ObjectStoreUrl {\n const protocol = noHttps ? 'http' : 'https';\n switch (info.family) {\n case 'IPv4':\n return `${protocol}://${info.address}:${info.port}/` as PFrameInternal.ObjectStoreUrl;\n case 'IPv6':\n return `${protocol}://[${info.address}]:${info.port}/` as PFrameInternal.ObjectStoreUrl;\n default:\n return `${protocol}://localhost:${info.port}/` as PFrameInternal.ObjectStoreUrl;\n }\n}\n\n/**\n * Serve HTTP requests using the provided handler.\n * Returns a promise that resolves when the server is stopped.\n */\nexport async function serve({\n handler,\n port = 0,\n noHttps,\n noAuth\n}: PFrameInternal.HttpServerOptions): Promise<PFrameInternal.HttpServer> {\n const started = new Deferred<PFrameInternal.HttpServer>();\n try {\n let stopped: Deferred<void> | null = null;\n\n let authToken: PFrameInternal.HttpAuthorizationToken | undefined;\n let effectiveHandler: RequestListener = handler;\n if (!noAuth) {\n authToken = randomUUID() as PFrameInternal.HttpAuthorizationToken;\n effectiveHandler = authorizeRequestHandler(effectiveHandler, authToken);\n }\n\n // Create HTTP server\n let encodedCaCert: Base64Encoded<PFrameInternal.PemCertificate> | undefined;\n const defaultOptions: ServerOptions = {\n keepAlive: true\n };\n let server: HttpServer | HttpsServer;\n\n if (noHttps) {\n server = createHttpServer(defaultOptions, effectiveHandler);\n } else {\n const { cert, private: key, public: ca } = await generateCertificate();\n encodedCaCert = base64Encode(cert as PFrameInternal.PemCertificate);\n server = createHttpsServer(\n { ...defaultOptions, cert, key, ca },\n effectiveHandler\n );\n }\n\n server\n .on('listening', () => {\n // Cast is safe by specification <https://nodejs.org/api/net.html#serveraddress>\n const url = createObjectStoreUrl(\n server.address() as AddressInfo,\n noHttps\n );\n stopped = new Deferred<void>();\n\n started.resolve({\n get info(): PFrameInternal.HttpServerInfo {\n return { url, authToken, encodedCaCert };\n },\n get stopped(): Promise<void> {\n return stopped!.promise;\n },\n stop(): Promise<void> {\n server.close();\n return stopped!.promise;\n }\n });\n })\n .on('error', (err) => {\n started.reject(err);\n stopped?.reject(err);\n })\n .on('close', () => stopped?.resolve())\n .listen({\n host: 'localhost',\n port\n });\n } catch (error: unknown) {\n started.reject(ensureError(error));\n }\n\n return started.promise;\n}\n"],"names":["generate","handler","Deferred","randomUUID","authorizeRequestHandler","createHttpServer","base64Encode","createHttpsServer","ensureError"],"mappings":";;;;;;;;;;AAsBA;AACA,eAAe,mBAAmB,GAAA;AAChC,IAAA,OAAO,MAAMA,mBAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC,EAAE;AAClE,QAAA,OAAO,EAAE,IAAI;AACb,QAAA,SAAS,EAAE,QAAQ;AACnB,QAAA,UAAU,EAAE;AACV,YAAA;AACE,gBAAA,IAAI,EAAE,gBAAgB;AACtB,gBAAA,QAAQ,EAAE;oBACR,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,WAAW,EAAE;oBAC/B,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,EAAE,WAAW,EAAE;oBAC5B,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE;AACvB;AACF;AACF;AACF,KAAA,CAAC;AACJ;AAEA;AACA,SAAS,oBAAoB,CAC3B,IAAiB,EACjB,OAAc,EAAA;IAEd,MAAM,QAAQ,GAAG,OAAO,GAAG,MAAM,GAAG,OAAO;AAC3C,IAAA,QAAQ,IAAI,CAAC,MAAM;AACjB,QAAA,KAAK,MAAM;YACT,OAAO,CAAA,EAAG,QAAQ,CAAA,GAAA,EAAM,IAAI,CAAC,OAAO,CAAA,CAAA,EAAI,IAAI,CAAC,IAAI,CAAA,CAAA,CAAoC;AACvF,QAAA,KAAK,MAAM;YACT,OAAO,CAAA,EAAG,QAAQ,CAAA,IAAA,EAAO,IAAI,CAAC,OAAO,CAAA,EAAA,EAAK,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