@milaboratories/pframes-rs-serv 1.0.68 → 1.0.69
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/fs-store.cjs +27 -13
- package/dist/fs-store.cjs.map +1 -1
- package/dist/fs-store.d.ts.map +1 -1
- package/dist/fs-store.js +28 -14
- package/dist/fs-store.js.map +1 -1
- package/dist/handler.cjs +15 -9
- package/dist/handler.cjs.map +1 -1
- package/dist/handler.d.ts.map +1 -1
- package/dist/handler.js +15 -9
- package/dist/handler.js.map +1 -1
- package/dist/parquet-server.cjs +33 -30
- package/dist/parquet-server.cjs.map +1 -1
- package/dist/parquet-server.d.ts +4 -3
- package/dist/parquet-server.d.ts.map +1 -1
- package/dist/parquet-server.js +33 -30
- package/dist/parquet-server.js.map +1 -1
- package/dist/serve.cjs +12 -20
- package/dist/serve.cjs.map +1 -1
- package/dist/serve.d.ts +1 -1
- package/dist/serve.d.ts.map +1 -1
- package/dist/serve.js +13 -21
- package/dist/serve.js.map +1 -1
- package/dist/utils/headers.cjs +2 -0
- package/dist/utils/headers.cjs.map +1 -1
- package/dist/utils/headers.d.ts +2 -0
- package/dist/utils/headers.d.ts.map +1 -1
- package/dist/utils/headers.js +2 -0
- package/dist/utils/headers.js.map +1 -1
- package/dist/utils/status.cjs +1 -0
- package/dist/utils/status.cjs.map +1 -1
- package/dist/utils/status.d.ts +1 -0
- package/dist/utils/status.d.ts.map +1 -1
- package/dist/utils/status.js +1 -0
- package/dist/utils/status.js.map +1 -1
- package/package.json +4 -3
- package/src/fs-store.ts +35 -17
- package/src/handler.ts +14 -9
- package/src/parquet-server.ts +52 -36
- package/src/serve.ts +14 -29
- package/src/utils/headers.ts +2 -0
- package/src/utils/status.ts +1 -0
package/dist/fs-store.cjs
CHANGED
|
@@ -29,14 +29,15 @@ class FileSystemStore extends plModelMiddleLayer.PFrameInternal.ObjectStore {
|
|
|
29
29
|
async request(filename, params) {
|
|
30
30
|
let file;
|
|
31
31
|
try {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
32
|
+
try {
|
|
33
|
+
const path = node_path.join(this.rootDir, filename);
|
|
34
|
+
file = await promises.open(path, 'r');
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
this.logger('error', `File system store failed to open file ${filename}: ${plModelCommon.ensureError(error)}`);
|
|
38
|
+
return await params.callback({ type: 'NotFound' });
|
|
39
|
+
}
|
|
40
|
+
params.signal.throwIfAborted();
|
|
40
41
|
let size;
|
|
41
42
|
try {
|
|
42
43
|
({ size } = await file.stat());
|
|
@@ -45,6 +46,7 @@ class FileSystemStore extends plModelMiddleLayer.PFrameInternal.ObjectStore {
|
|
|
45
46
|
this.logger('error', `File system store failed to get size of file ${filename}: ${plModelCommon.ensureError(error)}`);
|
|
46
47
|
return await params.callback({ type: 'InternalError' });
|
|
47
48
|
}
|
|
49
|
+
params.signal.throwIfAborted();
|
|
48
50
|
const range = this.translate(size, params.range);
|
|
49
51
|
if (!range) {
|
|
50
52
|
return await params.callback({ type: 'RangeNotSatisfiable', size });
|
|
@@ -56,7 +58,6 @@ class FileSystemStore extends plModelMiddleLayer.PFrameInternal.ObjectStore {
|
|
|
56
58
|
try {
|
|
57
59
|
data = node_fs.createReadStream('ignored', {
|
|
58
60
|
fd: file.fd,
|
|
59
|
-
autoClose: false,
|
|
60
61
|
start: range.start,
|
|
61
62
|
end: range.end,
|
|
62
63
|
signal: params.signal
|
|
@@ -67,12 +68,25 @@ class FileSystemStore extends plModelMiddleLayer.PFrameInternal.ObjectStore {
|
|
|
67
68
|
this.logger('error', `File system store failed to create read stream for ${filename}[${range.start}..=${range.end}]: ${plModelCommon.ensureError(error)}`);
|
|
68
69
|
return await params.callback({ type: 'InternalError' });
|
|
69
70
|
}
|
|
70
|
-
|
|
71
|
-
.callback({ type: 'Ok', size, range, data })
|
|
72
|
-
|
|
71
|
+
try {
|
|
72
|
+
return await params.callback({ type: 'Ok', size, range, data });
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (!plModelCommon.isAbortError(error)) {
|
|
76
|
+
this.logger('error', `File system store received unexpected rejection from callback: ${plModelCommon.ensureError(error)}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
if (!plModelCommon.isAbortError(error)) {
|
|
82
|
+
this.logger('error', `File system store unhandled error: ${plModelCommon.ensureError(error)}`);
|
|
83
|
+
}
|
|
84
|
+
return await params.callback({ type: 'InternalError' });
|
|
73
85
|
}
|
|
74
86
|
finally {
|
|
75
|
-
await file
|
|
87
|
+
await file?.close().catch(() => {
|
|
88
|
+
// already closed
|
|
89
|
+
});
|
|
76
90
|
}
|
|
77
91
|
}
|
|
78
92
|
}
|
package/dist/fs-store.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fs-store.cjs","sources":["../src/fs-store.ts"],"sourcesContent":["import type { Readable } from 'node:stream';\nimport { createReadStream } from 'node:fs';\nimport { stat, open, type FileHandle } from 'node:fs/promises';\nimport { join, resolve } from 'node:path';\nimport { PFrameInternal } from '@milaboratories/pl-model-middle-layer';\nimport { ensureError } from '@milaboratories/pl-model-common';\n\n/** Object store for serving files from a local directory */\nexport class FileSystemStore extends PFrameInternal.ObjectStore {\n private readonly rootDir: string;\n\n private constructor(options: PFrameInternal.FsStoreOptions) {\n super(options);\n\n this.rootDir = options.rootDir;\n }\n\n static async init(\n options: PFrameInternal.FsStoreOptions\n ): Promise<FileSystemStore> {\n const resolvedRootDir = resolve(options.rootDir);\n\n const rootStats = await stat(resolvedRootDir).catch(() => {\n throw new Error(\n `File system store root directory does not exist: ${resolvedRootDir}`\n );\n });\n if (!rootStats.isDirectory()) {\n throw new Error(\n `File system store root path is not a directory: ${resolvedRootDir}`\n );\n }\n\n return new FileSystemStore({\n ...options,\n rootDir: resolvedRootDir\n });\n }\n\n override async request(\n filename: PFrameInternal.ParquetFileName,\n params: {\n method: PFrameInternal.HttpMethod;\n range?: PFrameInternal.HttpRange;\n signal: AbortSignal;\n callback: (response: PFrameInternal.ObjectStoreResponse) => Promise<void>;\n }\n ): Promise<void> {\n let file: FileHandle;\n try {\n const path = join(this.rootDir, filename);\n
|
|
1
|
+
{"version":3,"file":"fs-store.cjs","sources":["../src/fs-store.ts"],"sourcesContent":["import type { Readable } from 'node:stream';\nimport { createReadStream } from 'node:fs';\nimport { stat, open, type FileHandle } from 'node:fs/promises';\nimport { join, resolve } from 'node:path';\nimport { PFrameInternal } from '@milaboratories/pl-model-middle-layer';\nimport { ensureError, isAbortError } from '@milaboratories/pl-model-common';\n\n/** Object store for serving files from a local directory */\nexport class FileSystemStore extends PFrameInternal.ObjectStore {\n private readonly rootDir: string;\n\n private constructor(options: PFrameInternal.FsStoreOptions) {\n super(options);\n\n this.rootDir = options.rootDir;\n }\n\n static async init(\n options: PFrameInternal.FsStoreOptions\n ): Promise<FileSystemStore> {\n const resolvedRootDir = resolve(options.rootDir);\n\n const rootStats = await stat(resolvedRootDir).catch(() => {\n throw new Error(\n `File system store root directory does not exist: ${resolvedRootDir}`\n );\n });\n if (!rootStats.isDirectory()) {\n throw new Error(\n `File system store root path is not a directory: ${resolvedRootDir}`\n );\n }\n\n return new FileSystemStore({\n ...options,\n rootDir: resolvedRootDir\n });\n }\n\n override async request(\n filename: PFrameInternal.ParquetFileName,\n params: {\n method: PFrameInternal.HttpMethod;\n range?: PFrameInternal.HttpRange;\n signal: AbortSignal;\n callback: (response: PFrameInternal.ObjectStoreResponse) => Promise<void>;\n }\n ): Promise<void> {\n let file: FileHandle | undefined;\n try {\n try {\n const path = join(this.rootDir, filename);\n file = await open(path, 'r');\n } catch (error: unknown) {\n this.logger(\n 'error',\n `File system store failed to open file ${filename}: ${ensureError(error)}`\n );\n return await params.callback({ type: 'NotFound' });\n }\n params.signal.throwIfAborted();\n\n let size: number;\n try {\n ({ size } = await file.stat());\n } catch (error: unknown) {\n this.logger(\n 'error',\n `File system store failed to get size of file ${filename}: ${ensureError(error)}`\n );\n return await params.callback({ type: 'InternalError' });\n }\n params.signal.throwIfAborted();\n\n const range = this.translate(size, params.range);\n if (!range) {\n return await params.callback({ type: 'RangeNotSatisfiable', size });\n }\n\n if (params.method === 'HEAD') {\n return await params.callback({ type: 'Ok', size, range });\n }\n\n let data: Readable;\n try {\n data = createReadStream('ignored', {\n fd: file.fd,\n start: range.start,\n end: range.end,\n signal: params.signal\n });\n this.logger(\n 'info',\n `File system store created read stream for ${filename}[${range.start}..=${range.end}]`\n );\n } catch (error: unknown) {\n this.logger(\n 'error',\n `File system store failed to create read stream for ${filename}[${range.start}..=${range.end}]: ${ensureError(error)}`\n );\n return await params.callback({ type: 'InternalError' });\n }\n\n try {\n return await params.callback({ type: 'Ok', size, range, data });\n } catch (error: unknown) {\n if (!isAbortError(error)) {\n this.logger(\n 'error',\n `File system store received unexpected rejection from callback: ${ensureError(error)}`\n );\n }\n }\n } catch (error: unknown) {\n if (!isAbortError(error)) {\n this.logger(\n 'error',\n `File system store unhandled error: ${ensureError(error)}`\n );\n }\n return await params.callback({ type: 'InternalError' });\n } finally {\n await file?.close().catch(() => {\n // already closed\n });\n }\n }\n}\n"],"names":["PFrameInternal","resolve","stat","join","open","ensureError","createReadStream","isAbortError"],"mappings":";;;;;;;;AAOA;AACM,MAAO,eAAgB,SAAQA,iCAAc,CAAC,WAAW,CAAA;AAC5C,IAAA,OAAO;AAExB,IAAA,WAAA,CAAoB,OAAsC,EAAA;QACxD,KAAK,CAAC,OAAO,CAAC;AAEd,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO;IAChC;AAEA,IAAA,aAAa,IAAI,CACf,OAAsC,EAAA;QAEtC,MAAM,eAAe,GAAGC,iBAAO,CAAC,OAAO,CAAC,OAAO,CAAC;QAEhD,MAAM,SAAS,GAAG,MAAMC,aAAI,CAAC,eAAe,CAA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,SAAS,CAAC;QACjC,MAAM,EAAE,WAAW,CAAC;QACpB,QAAQ,EAAE,CAAC,QAAQ,EAAE,cAAc,CAAC,mBAAmB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;KAC3E,GACA,OAAO,CAAC,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"fs-store.d.ts","sourceRoot":"","sources":["../src/fs-store.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,uCAAuC,CAAC;AAGvE,4DAA4D;AAC5D,qBAAa,eAAgB,SAAQ,cAAc,CAAC,WAAW;IAC7D,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IAEjC,OAAO;WAMM,IAAI,CACf,OAAO,EAAE,cAAc,CAAC,cAAc,GACrC,OAAO,CAAC,eAAe,CAAC;IAoBZ,OAAO,CACpB,QAAQ,EAAE,cAAc,CAAC,eAAe,EACxC,MAAM,EAAE;QACN,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC;QAClC,KAAK,CAAC,EAAE,cAAc,CAAC,SAAS,CAAC;QACjC,MAAM,EAAE,WAAW,CAAC;QACpB,QAAQ,EAAE,CAAC,QAAQ,EAAE,cAAc,CAAC,mBAAmB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;KAC3E,GACA,OAAO,CAAC,IAAI,CAAC;CAgFjB"}
|
package/dist/fs-store.js
CHANGED
|
@@ -2,7 +2,7 @@ import { createReadStream } from 'node:fs';
|
|
|
2
2
|
import { stat, open } from 'node:fs/promises';
|
|
3
3
|
import { resolve, join } from 'node:path';
|
|
4
4
|
import { PFrameInternal } from '@milaboratories/pl-model-middle-layer';
|
|
5
|
-
import { ensureError } from '@milaboratories/pl-model-common';
|
|
5
|
+
import { ensureError, isAbortError } from '@milaboratories/pl-model-common';
|
|
6
6
|
|
|
7
7
|
/** Object store for serving files from a local directory */
|
|
8
8
|
class FileSystemStore extends PFrameInternal.ObjectStore {
|
|
@@ -27,14 +27,15 @@ class FileSystemStore extends PFrameInternal.ObjectStore {
|
|
|
27
27
|
async request(filename, params) {
|
|
28
28
|
let file;
|
|
29
29
|
try {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
30
|
+
try {
|
|
31
|
+
const path = join(this.rootDir, filename);
|
|
32
|
+
file = await open(path, 'r');
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
this.logger('error', `File system store failed to open file ${filename}: ${ensureError(error)}`);
|
|
36
|
+
return await params.callback({ type: 'NotFound' });
|
|
37
|
+
}
|
|
38
|
+
params.signal.throwIfAborted();
|
|
38
39
|
let size;
|
|
39
40
|
try {
|
|
40
41
|
({ size } = await file.stat());
|
|
@@ -43,6 +44,7 @@ class FileSystemStore extends PFrameInternal.ObjectStore {
|
|
|
43
44
|
this.logger('error', `File system store failed to get size of file ${filename}: ${ensureError(error)}`);
|
|
44
45
|
return await params.callback({ type: 'InternalError' });
|
|
45
46
|
}
|
|
47
|
+
params.signal.throwIfAborted();
|
|
46
48
|
const range = this.translate(size, params.range);
|
|
47
49
|
if (!range) {
|
|
48
50
|
return await params.callback({ type: 'RangeNotSatisfiable', size });
|
|
@@ -54,7 +56,6 @@ class FileSystemStore extends PFrameInternal.ObjectStore {
|
|
|
54
56
|
try {
|
|
55
57
|
data = createReadStream('ignored', {
|
|
56
58
|
fd: file.fd,
|
|
57
|
-
autoClose: false,
|
|
58
59
|
start: range.start,
|
|
59
60
|
end: range.end,
|
|
60
61
|
signal: params.signal
|
|
@@ -65,12 +66,25 @@ class FileSystemStore extends PFrameInternal.ObjectStore {
|
|
|
65
66
|
this.logger('error', `File system store failed to create read stream for ${filename}[${range.start}..=${range.end}]: ${ensureError(error)}`);
|
|
66
67
|
return await params.callback({ type: 'InternalError' });
|
|
67
68
|
}
|
|
68
|
-
|
|
69
|
-
.callback({ type: 'Ok', size, range, data })
|
|
70
|
-
|
|
69
|
+
try {
|
|
70
|
+
return await params.callback({ type: 'Ok', size, range, data });
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (!isAbortError(error)) {
|
|
74
|
+
this.logger('error', `File system store received unexpected rejection from callback: ${ensureError(error)}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
if (!isAbortError(error)) {
|
|
80
|
+
this.logger('error', `File system store unhandled error: ${ensureError(error)}`);
|
|
81
|
+
}
|
|
82
|
+
return await params.callback({ type: 'InternalError' });
|
|
71
83
|
}
|
|
72
84
|
finally {
|
|
73
|
-
await file
|
|
85
|
+
await file?.close().catch(() => {
|
|
86
|
+
// already closed
|
|
87
|
+
});
|
|
74
88
|
}
|
|
75
89
|
}
|
|
76
90
|
}
|
package/dist/fs-store.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fs-store.js","sources":["../src/fs-store.ts"],"sourcesContent":["import type { Readable } from 'node:stream';\nimport { createReadStream } from 'node:fs';\nimport { stat, open, type FileHandle } from 'node:fs/promises';\nimport { join, resolve } from 'node:path';\nimport { PFrameInternal } from '@milaboratories/pl-model-middle-layer';\nimport { ensureError } from '@milaboratories/pl-model-common';\n\n/** Object store for serving files from a local directory */\nexport class FileSystemStore extends PFrameInternal.ObjectStore {\n private readonly rootDir: string;\n\n private constructor(options: PFrameInternal.FsStoreOptions) {\n super(options);\n\n this.rootDir = options.rootDir;\n }\n\n static async init(\n options: PFrameInternal.FsStoreOptions\n ): Promise<FileSystemStore> {\n const resolvedRootDir = resolve(options.rootDir);\n\n const rootStats = await stat(resolvedRootDir).catch(() => {\n throw new Error(\n `File system store root directory does not exist: ${resolvedRootDir}`\n );\n });\n if (!rootStats.isDirectory()) {\n throw new Error(\n `File system store root path is not a directory: ${resolvedRootDir}`\n );\n }\n\n return new FileSystemStore({\n ...options,\n rootDir: resolvedRootDir\n });\n }\n\n override async request(\n filename: PFrameInternal.ParquetFileName,\n params: {\n method: PFrameInternal.HttpMethod;\n range?: PFrameInternal.HttpRange;\n signal: AbortSignal;\n callback: (response: PFrameInternal.ObjectStoreResponse) => Promise<void>;\n }\n ): Promise<void> {\n let file: FileHandle;\n try {\n const path = join(this.rootDir, filename);\n
|
|
1
|
+
{"version":3,"file":"fs-store.js","sources":["../src/fs-store.ts"],"sourcesContent":["import type { Readable } from 'node:stream';\nimport { createReadStream } from 'node:fs';\nimport { stat, open, type FileHandle } from 'node:fs/promises';\nimport { join, resolve } from 'node:path';\nimport { PFrameInternal } from '@milaboratories/pl-model-middle-layer';\nimport { ensureError, isAbortError } from '@milaboratories/pl-model-common';\n\n/** Object store for serving files from a local directory */\nexport class FileSystemStore extends PFrameInternal.ObjectStore {\n private readonly rootDir: string;\n\n private constructor(options: PFrameInternal.FsStoreOptions) {\n super(options);\n\n this.rootDir = options.rootDir;\n }\n\n static async init(\n options: PFrameInternal.FsStoreOptions\n ): Promise<FileSystemStore> {\n const resolvedRootDir = resolve(options.rootDir);\n\n const rootStats = await stat(resolvedRootDir).catch(() => {\n throw new Error(\n `File system store root directory does not exist: ${resolvedRootDir}`\n );\n });\n if (!rootStats.isDirectory()) {\n throw new Error(\n `File system store root path is not a directory: ${resolvedRootDir}`\n );\n }\n\n return new FileSystemStore({\n ...options,\n rootDir: resolvedRootDir\n });\n }\n\n override async request(\n filename: PFrameInternal.ParquetFileName,\n params: {\n method: PFrameInternal.HttpMethod;\n range?: PFrameInternal.HttpRange;\n signal: AbortSignal;\n callback: (response: PFrameInternal.ObjectStoreResponse) => Promise<void>;\n }\n ): Promise<void> {\n let file: FileHandle | undefined;\n try {\n try {\n const path = join(this.rootDir, filename);\n file = await open(path, 'r');\n } catch (error: unknown) {\n this.logger(\n 'error',\n `File system store failed to open file ${filename}: ${ensureError(error)}`\n );\n return await params.callback({ type: 'NotFound' });\n }\n params.signal.throwIfAborted();\n\n let size: number;\n try {\n ({ size } = await file.stat());\n } catch (error: unknown) {\n this.logger(\n 'error',\n `File system store failed to get size of file ${filename}: ${ensureError(error)}`\n );\n return await params.callback({ type: 'InternalError' });\n }\n params.signal.throwIfAborted();\n\n const range = this.translate(size, params.range);\n if (!range) {\n return await params.callback({ type: 'RangeNotSatisfiable', size });\n }\n\n if (params.method === 'HEAD') {\n return await params.callback({ type: 'Ok', size, range });\n }\n\n let data: Readable;\n try {\n data = createReadStream('ignored', {\n fd: file.fd,\n start: range.start,\n end: range.end,\n signal: params.signal\n });\n this.logger(\n 'info',\n `File system store created read stream for ${filename}[${range.start}..=${range.end}]`\n );\n } catch (error: unknown) {\n this.logger(\n 'error',\n `File system store failed to create read stream for ${filename}[${range.start}..=${range.end}]: ${ensureError(error)}`\n );\n return await params.callback({ type: 'InternalError' });\n }\n\n try {\n return await params.callback({ type: 'Ok', size, range, data });\n } catch (error: unknown) {\n if (!isAbortError(error)) {\n this.logger(\n 'error',\n `File system store received unexpected rejection from callback: ${ensureError(error)}`\n );\n }\n }\n } catch (error: unknown) {\n if (!isAbortError(error)) {\n this.logger(\n 'error',\n `File system store unhandled error: ${ensureError(error)}`\n );\n }\n return await params.callback({ type: 'InternalError' });\n } finally {\n await file?.close().catch(() => {\n // already closed\n });\n }\n }\n}\n"],"names":[],"mappings":";;;;;;AAOA;AACM,MAAO,eAAgB,SAAQ,cAAc,CAAC,WAAW,CAAA;AAC5C,IAAA,OAAO;AAExB,IAAA,WAAA,CAAoB,OAAsC,EAAA;QACxD,KAAK,CAAC,OAAO,CAAC;AAEd,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO;IAChC;AAEA,IAAA,aAAa,IAAI,CACf,OAAsC,EAAA;QAEtC,MAAM,eAAe,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC;QAEhD,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC,CAAC,KAAK,CAAC,MAAK;AACvD,YAAA,MAAM,IAAI,KAAK,CACb,oDAAoD,eAAe,CAAA,CAAE,CACtE;AACH,QAAA,CA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AAc,EAAE;AACvB,gBAAA,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE;AACxB,oBAAA,IAAI,CAAC,MAAM,CACT,OAAO,EACP,CAAA,+DAAA,EAAkE,WAAW,CAAC,KAAK,CAAC,CAAA,CAAE,CACvF;gBACH;YACF;QACF;QAAE,OAAO,KAAc,EAAE;AACvB,YAAA,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE;AACxB,gBAAA,IAAI,CAAC,MAAM,CACT,OAAO,EACP,CAAA,mCAAA,EAAsC,WAAW,CAAC,KAAK,CAAC,CAAA,CAAE,CAC3D;YACH;YACA,OAAO,MAAM,MAAM,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,eAAe,EAAE,CAAC;QACzD;gBAAU;YACR,MAAM,IAAI,EAAE,KAAK,EAAE,CAAC,KAAK,CAAC,MAAK;;AAE/B,YAAA,CAAC,CAAC;QACJ;IACF;AACD;;;;"}
|
package/dist/handler.cjs
CHANGED
|
@@ -9,9 +9,10 @@ var options = require('./utils/options.cjs');
|
|
|
9
9
|
var range = require('./utils/range.cjs');
|
|
10
10
|
var method = require('./utils/method.cjs');
|
|
11
11
|
var status = require('./utils/status.cjs');
|
|
12
|
+
var plModelCommon = require('@milaboratories/pl-model-common');
|
|
12
13
|
|
|
13
14
|
/** Main request handler for parquet files */
|
|
14
|
-
|
|
15
|
+
function handleRequest(request, response, store) {
|
|
15
16
|
// RFC 9110 section 6.6.1: Date header should be present in all responses
|
|
16
17
|
response.sendDate = true;
|
|
17
18
|
// RFC 9110 section 8.6: Content-Length 0 as default for error responses
|
|
@@ -65,8 +66,11 @@ async function handleRequest(request, response, store) {
|
|
|
65
66
|
signal,
|
|
66
67
|
// pipeline automatically destroys the streams if they were not gracefully closed
|
|
67
68
|
callback: async (result) => {
|
|
68
|
-
if (
|
|
69
|
-
|
|
69
|
+
if (request.destroyed) {
|
|
70
|
+
// request has timed out, close the connection
|
|
71
|
+
response.setHeader(headers.HeaderName.Connection, headers.HeaderValue.Connection);
|
|
72
|
+
return void response.writeHead(status.StatusCode.RequestTimeout).end();
|
|
73
|
+
}
|
|
70
74
|
switch (result.type) {
|
|
71
75
|
case 'InternalError':
|
|
72
76
|
// object store encountered network error, retry by client can help
|
|
@@ -98,11 +102,13 @@ async function handleRequest(request, response, store) {
|
|
|
98
102
|
if (method.isHead(method$1)) {
|
|
99
103
|
return void response.end();
|
|
100
104
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
105
|
+
try {
|
|
106
|
+
return await promises.pipeline(result.data, response, { signal });
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
if (!plModelCommon.isAbortError(error))
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
106
112
|
}
|
|
107
113
|
});
|
|
108
114
|
}
|
|
@@ -117,7 +123,7 @@ async function handleRequest(request, response, store) {
|
|
|
117
123
|
*/
|
|
118
124
|
function createRequestHandler(options) {
|
|
119
125
|
const { store } = options;
|
|
120
|
-
return (request, response) =>
|
|
126
|
+
return (request, response) => handleRequest(request, response, store);
|
|
121
127
|
}
|
|
122
128
|
/** Request authorization middleware */
|
|
123
129
|
function authorizeRequest(request, response, handler, authHeader) {
|
package/dist/handler.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.cjs","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';\n\n/** Main request handler for parquet files */\nasync function handleRequest(\n request: IncomingMessage,\n response: ServerResponse,\n store: PFrameInternal.ObjectStore\n): Promise<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: 'GET',\n range,\n signal,\n // pipeline automatically destroys the streams if they were not gracefully closed\n callback: async (result) => {\n if (response.destroyed) return void response.destroy();\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 return await pipeline(result.data!, response, { signal }).catch(() => {\n // Pipeline errors are expected when request is aborted or connection is lost\n // Response head was already written, so we can't change status code\n // Just mute the error - pipeline destroys the response stream\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) => void 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":["HeaderName","HeaderValue","method","isGetOrHead","StatusCode","filename","getFilenameFromUrl","etag","createETag","options","Options","range","parseRange","isGet","isHead","pipeline","timingSafeEqual"],"mappings":";;;;;;;;;;;;AAqBA;AACA,eAAe,aAAa,CAC1B,OAAwB,EACxB,QAAwB,EACxB,KAAiC,EAAA;;AAGjC,IAAA,QAAQ,CAAC,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QAAA,CAACY,2BAAe,CAAC,cAAc,EAAE,cAAc,CAAC,EAChD;QACA,QAAQ,CAAC,SAAS,CAAChB,kBAAU,CAAC,eAAe,EAAEC,mBAAW,CAAC,eAAe,CAAC;AAC3E,QAAA,OAAO,KAAK,QAAQ,CAAC,SAAS,CAACG,iBAAU,CAAC,YAAY,CAAC,CAAC,GAAG,EAAE;IAC/D;AAEA,IAAA,OAAO,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;;;;;"}
|
|
1
|
+
{"version":3,"file":"handler.cjs","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: 'GET',\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 response.setHeader(HeaderName.Connection, HeaderValue.Connection);\n return void response.writeHead(StatusCode.RequestTimeout).end();\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":["HeaderName","HeaderValue","method","isGetOrHead","StatusCode","filename","getFilenameFromUrl","etag","createETag","options","Options","range","parseRange","isGet","isHead","pipeline","isAbortError","timingSafeEqual"],"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,CAACA,kBAAU,CAAC,aAAa,EAAE,CAAC,CAAC;;;IAI/C,QAAQ,CAAC,SAAS,CAACA,kBAAU,CAAC,YAAY,EAAEC,mBAA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C,UAAU;AACvD,QAAA,CAACa,2BAAe,CAAC,cAAc,EAAE,cAAc,CAAC,EAChD;QACA,QAAQ,CAAC,SAAS,CAACjB,kBAAU,CAAC,eAAe,EAAEC,mBAAW,CAAC,eAAe,CAAC;AAC3E,QAAA,OAAO,KAAK,QAAQ,CAAC,SAAS,CAACG,iBAAU,CAAC,YAAY,CAAC,CAAC,GAAG,EAAE;IAC/D;AAEA,IAAA,OAAO,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/handler.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../src/handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,eAAe,EAEhB,MAAM,WAAW,CAAC;AAGnB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uCAAuC,CAAC;
|
|
1
|
+
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../src/handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,eAAe,EAEhB,MAAM,WAAW,CAAC;AAGnB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uCAAuC,CAAC;AA4I5E;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,cAAc,CAAC,qBAAqB,GAC5C,eAAe,CAGjB;AA0CD,yDAAyD;AACzD,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,eAAe,EACxB,SAAS,EAAE,cAAc,CAAC,sBAAsB,GAC/C,eAAe,CAIjB"}
|
package/dist/handler.js
CHANGED
|
@@ -7,9 +7,10 @@ import { Options } from './utils/options.js';
|
|
|
7
7
|
import { parseRange } from './utils/range.js';
|
|
8
8
|
import { isGetOrHead, isGet, isHead } from './utils/method.js';
|
|
9
9
|
import { StatusCode } from './utils/status.js';
|
|
10
|
+
import { isAbortError } from '@milaboratories/pl-model-common';
|
|
10
11
|
|
|
11
12
|
/** Main request handler for parquet files */
|
|
12
|
-
|
|
13
|
+
function handleRequest(request, response, store) {
|
|
13
14
|
// RFC 9110 section 6.6.1: Date header should be present in all responses
|
|
14
15
|
response.sendDate = true;
|
|
15
16
|
// RFC 9110 section 8.6: Content-Length 0 as default for error responses
|
|
@@ -63,8 +64,11 @@ async function handleRequest(request, response, store) {
|
|
|
63
64
|
signal,
|
|
64
65
|
// pipeline automatically destroys the streams if they were not gracefully closed
|
|
65
66
|
callback: async (result) => {
|
|
66
|
-
if (
|
|
67
|
-
|
|
67
|
+
if (request.destroyed) {
|
|
68
|
+
// request has timed out, close the connection
|
|
69
|
+
response.setHeader(HeaderName.Connection, HeaderValue.Connection);
|
|
70
|
+
return void response.writeHead(StatusCode.RequestTimeout).end();
|
|
71
|
+
}
|
|
68
72
|
switch (result.type) {
|
|
69
73
|
case 'InternalError':
|
|
70
74
|
// object store encountered network error, retry by client can help
|
|
@@ -96,11 +100,13 @@ async function handleRequest(request, response, store) {
|
|
|
96
100
|
if (isHead(method)) {
|
|
97
101
|
return void response.end();
|
|
98
102
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
103
|
+
try {
|
|
104
|
+
return await pipeline(result.data, response, { signal });
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
if (!isAbortError(error))
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
104
110
|
}
|
|
105
111
|
});
|
|
106
112
|
}
|
|
@@ -115,7 +121,7 @@ async function handleRequest(request, response, store) {
|
|
|
115
121
|
*/
|
|
116
122
|
function createRequestHandler(options) {
|
|
117
123
|
const { store } = options;
|
|
118
|
-
return (request, response) =>
|
|
124
|
+
return (request, response) => handleRequest(request, response, store);
|
|
119
125
|
}
|
|
120
126
|
/** Request authorization middleware */
|
|
121
127
|
function authorizeRequest(request, response, handler, authHeader) {
|
package/dist/handler.js.map
CHANGED
|
@@ -1 +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';\n\n/** Main request handler for parquet files */\nasync function handleRequest(\n request: IncomingMessage,\n response: ServerResponse,\n store: PFrameInternal.ObjectStore\n): Promise<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: 'GET',\n range,\n signal,\n // pipeline automatically destroys the streams if they were not gracefully closed\n callback: async (result) => {\n if (response.destroyed) return void response.destroy();\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 return await pipeline(result.data!, response, { signal }).catch(() => {\n // Pipeline errors are expected when request is aborted or connection is lost\n // Response head was already written, so we can't change status code\n // Just mute the error - pipeline destroys the response stream\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) => void 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":";;;;;;;;;;AAqBA;AACA,eAAe,aAAa,CAC1B,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,kBAAkB,CAAC,OAAO,CAAC;AAC5C,IAAA,IAAI,QAAQ,KAAK,IAAI,EAAE;AACrB,QAAA,OAAO,KAAK,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE;IACvD;;IAGA,QAAQ,CAAC,S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,MAAM,EAAE;;QAE9D,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,eAAe,EAAE,WAAW,CAAC,eAAe,CAAC;AAC3E,QAAA,OAAO,KAAK,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC,GAAG,EAAE;IAC/D;;;AAIA,IAAA,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE;IACjC,MAAM,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC;IACnD,MAAM,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC;AAEjD,IAAA,IACE,cAAc,CAAC,UAAU,KAAK,cAAc,CAAC,UAAU;AACvD,QAAA,CAAC,eAAe,CAAC,cAAc,EAAE,cAAc,CAAC,EAChD;QACA,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,eAAe,EAAE,WAAW,CAAC,eAAe,CAAC;AAC3E,QAAA,OAAO,KAAK,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC,GAAG,EAAE;IAC/D;AAEA,IAAA,OAAO,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;;;;"}
|
|
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: 'GET',\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 response.setHeader(HeaderName.Connection, HeaderValue.Connection);\n return void response.writeHead(StatusCode.RequestTimeout).end();\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":";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/D;;;AAIA,IAAA,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE;IACjC,MAAM,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC;IACnD,MAAM,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC;AAEjD,IAAA,IACE,cAAc,CAAC,UAAU,KAAK,cAAc,CAAC,UAAU;AACvD,QAAA,CAAC,eAAe,CAAC,cAAc,EAAE,cAAc,CAAC,EAChD;QACA,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,eAAe,EAAE,WAAW,CAAC,eAAe,CAAC;AAC3E,QAAA,OAAO,KAAK,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC,GAAG,EAAE;IAC/D;AAEA,IAAA,OAAO,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/parquet-server.cjs
CHANGED
|
@@ -5,15 +5,14 @@ var promises = require('node:readline/promises');
|
|
|
5
5
|
var node_path = require('node:path');
|
|
6
6
|
var node_url = require('node:url');
|
|
7
7
|
var commander = require('commander');
|
|
8
|
-
var
|
|
9
|
-
var handler = require('./handler.cjs');
|
|
10
|
-
var serve = require('./serve.cjs');
|
|
8
|
+
var _export = require('./export.cjs');
|
|
11
9
|
var plModelCommon = require('@milaboratories/pl-model-common');
|
|
12
10
|
|
|
13
11
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
14
12
|
const Options = {
|
|
15
|
-
|
|
16
|
-
NoAuth: '--no-auth'
|
|
13
|
+
NoHttps: '--no-https',
|
|
14
|
+
NoAuth: '--no-auth',
|
|
15
|
+
Port: '--port'
|
|
17
16
|
};
|
|
18
17
|
/**
|
|
19
18
|
* Serves parquet files from the given root directory.
|
|
@@ -25,15 +24,22 @@ async function runParquetServer() {
|
|
|
25
24
|
.name('parquet-server')
|
|
26
25
|
.description('Serve parquet files from a directory over HTTP(S)')
|
|
27
26
|
.argument('<root-directory>', 'Root directory containing parquet files')
|
|
28
|
-
.option(Options.
|
|
29
|
-
.option(Options.NoAuth, 'Disable
|
|
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)
|
|
30
36
|
.action(async (rootDir, options) => {
|
|
31
37
|
const abortController = new AbortController();
|
|
32
38
|
process
|
|
33
39
|
.on('SIGINT', () => abortController.abort())
|
|
34
40
|
.on('SIGTERM', () => abortController.abort());
|
|
35
41
|
abortController.signal.throwIfAborted();
|
|
36
|
-
const store = await
|
|
42
|
+
const store = await _export.HttpHelpers.createFsStore({
|
|
37
43
|
rootDir,
|
|
38
44
|
logger: (level, message) => {
|
|
39
45
|
const timestamp = new Date(Date.now()).toISOString();
|
|
@@ -41,20 +47,17 @@ async function runParquetServer() {
|
|
|
41
47
|
}
|
|
42
48
|
});
|
|
43
49
|
abortController.signal.throwIfAborted();
|
|
44
|
-
const handler
|
|
45
|
-
const server = await
|
|
46
|
-
handler
|
|
47
|
-
...(options.
|
|
48
|
-
...(!options.auth && { noAuth: true })
|
|
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
|
|
49
56
|
});
|
|
50
57
|
abortController.signal.onabort = () => server.stop();
|
|
51
58
|
abortController.signal.throwIfAborted();
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
...(server.authToken && { authToken: server.authToken }),
|
|
55
|
-
...(server.encodedCaCert && { caCert: server.encodedCaCert })
|
|
56
|
-
});
|
|
57
|
-
console.log(serverConfig);
|
|
59
|
+
const serverInfo = plModelCommon.stringifyJson(server.info);
|
|
60
|
+
console.log(serverInfo);
|
|
58
61
|
await server.stopped;
|
|
59
62
|
});
|
|
60
63
|
await program.parseAsync();
|
|
@@ -67,33 +70,33 @@ async function runParquetServer() {
|
|
|
67
70
|
*/
|
|
68
71
|
class ParquetServer {
|
|
69
72
|
#process;
|
|
70
|
-
#
|
|
73
|
+
#info;
|
|
71
74
|
#lineReader;
|
|
72
|
-
constructor(process,
|
|
75
|
+
constructor(process, info, lineReader) {
|
|
73
76
|
this.#process = process;
|
|
74
|
-
this.#
|
|
77
|
+
this.#info = info;
|
|
75
78
|
this.#lineReader = lineReader;
|
|
76
79
|
}
|
|
77
|
-
get
|
|
78
|
-
return this.#
|
|
80
|
+
get info() {
|
|
81
|
+
return this.#info;
|
|
79
82
|
}
|
|
80
83
|
static async serve(rootDir, options) {
|
|
81
|
-
const
|
|
82
|
-
const nodeDirname = node_path.dirname(node_url.fileURLToPath(nodeFileUrl));
|
|
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))));
|
|
83
85
|
const binPath = node_path.join(nodeDirname, '..', 'bin', 'parquet-server.mjs');
|
|
84
86
|
const serverProcess = node_child_process.spawn('node', [
|
|
85
87
|
binPath,
|
|
86
88
|
rootDir,
|
|
87
|
-
...(options?.
|
|
88
|
-
...(options?.noAuth ? [Options.NoAuth] : [])
|
|
89
|
+
...(options?.noHttps ? [Options.NoHttps] : []),
|
|
90
|
+
...(options?.noAuth ? [Options.NoAuth] : []),
|
|
91
|
+
...(options?.port ? [Options.Port, options.port.toString()] : [])
|
|
89
92
|
], {
|
|
90
93
|
stdio: ['ignore', 'pipe', 'ignore']
|
|
91
94
|
});
|
|
92
95
|
const lineReader = promises.createInterface({ input: serverProcess.stdout });
|
|
93
96
|
const firstLine = await lineReader[Symbol.asyncIterator]().next();
|
|
94
|
-
const
|
|
97
|
+
const serverInfo = plModelCommon.parseJson(firstLine.value);
|
|
95
98
|
lineReader.on('line', console.log);
|
|
96
|
-
return new ParquetServer(serverProcess,
|
|
99
|
+
return new ParquetServer(serverProcess, serverInfo, lineReader);
|
|
97
100
|
}
|
|
98
101
|
[Symbol.dispose]() {
|
|
99
102
|
this.#lineReader.close();
|