@milaboratories/pframes-rs-serv 1.0.68 → 1.0.70

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/src/serve.ts CHANGED
@@ -14,8 +14,7 @@ import type { PFrameInternal } from '@milaboratories/pl-model-middle-layer';
14
14
  import {
15
15
  base64Encode,
16
16
  Base64Encoded,
17
- ensureError,
18
- PFrameError
17
+ ensureError
19
18
  } from '@milaboratories/pl-model-common';
20
19
  import { generate, type GenerateResult } from 'selfsigned';
21
20
  import { randomUUID } from 'node:crypto';
@@ -54,18 +53,16 @@ async function generateCertificate(): Promise<GenerateResult> {
54
53
  /** Create an object store URL from the server address info. */
55
54
  function createObjectStoreUrl(
56
55
  info: AddressInfo,
57
- http?: true
56
+ noHttps?: true
58
57
  ): PFrameInternal.ObjectStoreUrl {
59
- const protocol = http ? 'http' : 'https';
58
+ const protocol = noHttps ? 'http' : 'https';
60
59
  switch (info.family) {
61
60
  case 'IPv4':
62
61
  return `${protocol}://${info.address}:${info.port}/` as PFrameInternal.ObjectStoreUrl;
63
62
  case 'IPv6':
64
63
  return `${protocol}://[${info.address}]:${info.port}/` as PFrameInternal.ObjectStoreUrl;
65
64
  default:
66
- throw new PFrameError(
67
- `PFrame helper HTTP(S) server bound to 'localhost' has unknown address family: ${info.family}`
68
- );
65
+ return `${protocol}://localhost:${info.port}/` as PFrameInternal.ObjectStoreUrl;
69
66
  }
70
67
  }
71
68
 
@@ -76,7 +73,7 @@ function createObjectStoreUrl(
76
73
  export async function serve({
77
74
  handler,
78
75
  port = 0,
79
- http,
76
+ noHttps,
80
77
  noAuth
81
78
  }: PFrameInternal.HttpServerOptions): Promise<PFrameInternal.HttpServer> {
82
79
  const started = new Deferred<PFrameInternal.HttpServer>();
@@ -91,52 +88,41 @@ export async function serve({
91
88
  }
92
89
 
93
90
  // Create HTTP server
94
- let certificateBase64:
95
- | Base64Encoded<PFrameInternal.PemCertificate>
96
- | undefined;
91
+ let encodedCaCert: Base64Encoded<PFrameInternal.PemCertificate> | undefined;
97
92
  const defaultOptions: ServerOptions = {
98
93
  keepAlive: true
99
94
  };
100
95
  let server: HttpServer | HttpsServer;
101
96
 
102
- if (http) {
97
+ if (noHttps) {
103
98
  server = createHttpServer(defaultOptions, effectiveHandler);
104
99
  } else {
105
100
  const { cert, private: key, public: ca } = await generateCertificate();
106
- certificateBase64 = base64Encode(cert as PFrameInternal.PemCertificate);
101
+ encodedCaCert = base64Encode(cert as PFrameInternal.PemCertificate);
107
102
  server = createHttpsServer(
108
103
  { ...defaultOptions, cert, key, ca },
109
104
  effectiveHandler
110
105
  );
111
106
  }
112
107
 
113
- const abortController = new AbortController();
114
108
  server
115
109
  .on('listening', () => {
116
110
  // Cast is safe by specification <https://nodejs.org/api/net.html#serveraddress>
117
- const address = createObjectStoreUrl(
111
+ const url = createObjectStoreUrl(
118
112
  server.address() as AddressInfo,
119
- http
113
+ noHttps
120
114
  );
121
115
  stopped = new Deferred<void>();
122
116
 
123
117
  started.resolve({
124
- get address(): PFrameInternal.ObjectStoreUrl {
125
- return address;
126
- },
127
- get authToken(): PFrameInternal.HttpAuthorizationToken | undefined {
128
- return authToken;
129
- },
130
- get encodedCaCert():
131
- | Base64Encoded<PFrameInternal.PemCertificate>
132
- | undefined {
133
- return certificateBase64;
118
+ get info(): PFrameInternal.HttpServerInfo {
119
+ return { url, authToken, encodedCaCert };
134
120
  },
135
121
  get stopped(): Promise<void> {
136
122
  return stopped!.promise;
137
123
  },
138
124
  stop(): Promise<void> {
139
- abortController.abort();
125
+ server.close();
140
126
  return stopped!.promise;
141
127
  }
142
128
  });
@@ -148,8 +134,7 @@ export async function serve({
148
134
  .on('close', () => stopped?.resolve())
149
135
  .listen({
150
136
  host: 'localhost',
151
- port,
152
- signal: abortController.signal
137
+ port
153
138
  });
154
139
  } catch (error: unknown) {
155
140
  started.reject(ensureError(error));
@@ -5,6 +5,7 @@ export const HeaderName = {
5
5
  Allow: 'allow',
6
6
  Authorization: 'authorization',
7
7
  CacheControl: 'cache-control',
8
+ Connection: 'connection',
8
9
  ContentLength: 'content-length',
9
10
  ContentRange: 'content-range',
10
11
  ContentType: 'content-type',
@@ -24,6 +25,7 @@ export const HeaderValue = {
24
25
  AcceptRanges: 'bytes',
25
26
  Allow: 'GET, HEAD',
26
27
  CacheControl: 'public, immutable, max-age=31536000',
28
+ Connection: 'close',
27
29
  ContentType: 'application/octet-stream',
28
30
  WWWAuthenticate: 'Bearer realm="parquet-server"'
29
31
  } as const;
@@ -7,6 +7,7 @@ export const StatusCode = {
7
7
  Unauthorized: 401, // <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401>
8
8
  NotFound: 404, // <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404>
9
9
  MethodNotAllowed: 405, // <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405>
10
+ RequestTimeout: 408, // <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408>
10
11
  Gone: 410, // <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/410>
11
12
  PreconditionFailed: 412, // <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/412>
12
13
  RangeNotSatisfiable: 416, // <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/416>