@entity-access/server-pages 1.0.29 → 1.0.31

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.
Files changed (78) hide show
  1. package/.vscode/settings.json +1 -0
  2. package/dist/Content.d.ts +6 -6
  3. package/dist/Content.d.ts.map +1 -1
  4. package/dist/Content.js +21 -37
  5. package/dist/Content.js.map +1 -1
  6. package/dist/Page.d.ts +11 -40
  7. package/dist/Page.d.ts.map +1 -1
  8. package/dist/Page.js +5 -3
  9. package/dist/Page.js.map +1 -1
  10. package/dist/ServerPages.d.ts +13 -5
  11. package/dist/ServerPages.d.ts.map +1 -1
  12. package/dist/ServerPages.js +99 -81
  13. package/dist/ServerPages.js.map +1 -1
  14. package/dist/core/AsyncStream.d.ts +16 -0
  15. package/dist/core/AsyncStream.d.ts.map +1 -0
  16. package/dist/core/AsyncStream.js +55 -0
  17. package/dist/core/AsyncStream.js.map +1 -0
  18. package/dist/core/FileApi.d.ts +5 -0
  19. package/dist/core/FileApi.d.ts.map +1 -0
  20. package/dist/core/FileApi.js +95 -0
  21. package/dist/core/FileApi.js.map +1 -0
  22. package/dist/core/LocalFile.d.ts +2 -1
  23. package/dist/core/LocalFile.d.ts.map +1 -1
  24. package/dist/core/LocalFile.js +8 -0
  25. package/dist/core/LocalFile.js.map +1 -1
  26. package/dist/core/RouteTree.js +1 -1
  27. package/dist/core/RouteTree.js.map +1 -1
  28. package/dist/core/SessionUser.d.ts +2 -2
  29. package/dist/core/SessionUser.d.ts.map +1 -1
  30. package/dist/core/SessionUser.js.map +1 -1
  31. package/dist/core/Wrapped.d.ts +70 -0
  32. package/dist/core/Wrapped.d.ts.map +1 -0
  33. package/dist/core/Wrapped.js +253 -0
  34. package/dist/core/Wrapped.js.map +1 -0
  35. package/dist/core/cached.d.ts +2 -0
  36. package/dist/core/cached.d.ts.map +1 -0
  37. package/dist/core/cached.js +3 -0
  38. package/dist/core/cached.js.map +1 -0
  39. package/dist/decorators/Authorize.d.ts +2 -0
  40. package/dist/decorators/Authorize.d.ts.map +1 -0
  41. package/dist/decorators/Authorize.js +3 -0
  42. package/dist/decorators/Authorize.js.map +1 -0
  43. package/dist/parsers/json/jsonParser.d.ts +2 -0
  44. package/dist/parsers/json/jsonParser.d.ts.map +1 -0
  45. package/dist/parsers/json/jsonParser.js +3 -0
  46. package/dist/parsers/json/jsonParser.js.map +1 -0
  47. package/dist/services/CookieService.d.ts +3 -3
  48. package/dist/services/CookieService.d.ts.map +1 -1
  49. package/dist/services/CookieService.js +2 -4
  50. package/dist/services/CookieService.js.map +1 -1
  51. package/dist/ssl/ACME.d.ts +37 -0
  52. package/dist/ssl/ACME.d.ts.map +1 -0
  53. package/dist/ssl/ACME.js +200 -0
  54. package/dist/ssl/ACME.js.map +1 -0
  55. package/dist/ssl/ChallengeStore.d.ts +8 -0
  56. package/dist/ssl/ChallengeStore.d.ts.map +1 -0
  57. package/dist/ssl/ChallengeStore.js +34 -0
  58. package/dist/ssl/ChallengeStore.js.map +1 -0
  59. package/dist/tsconfig.tsbuildinfo +1 -1
  60. package/package.json +4 -6
  61. package/self-signed/cert.crt +22 -0
  62. package/self-signed/key.pem +27 -0
  63. package/src/Content.tsx +28 -43
  64. package/src/Page.tsx +18 -62
  65. package/src/ServerPages.ts +114 -76
  66. package/src/core/AsyncStream.ts +83 -0
  67. package/src/core/FileApi.ts +52 -0
  68. package/src/core/LocalFile.ts +11 -2
  69. package/src/core/RouteTree.ts +1 -1
  70. package/src/core/SessionUser.ts +2 -2
  71. package/src/core/Wrapped.ts +359 -0
  72. package/src/core/cached.ts +3 -0
  73. package/src/decorators/Authorize.ts +3 -0
  74. package/src/parsers/json/jsonParser.ts +3 -0
  75. package/src/services/CookieService.ts +4 -6
  76. package/src/ssl/ACME.ts +247 -0
  77. package/src/ssl/ChallengeStore.ts +26 -0
  78. package/test.js +1 -3
@@ -0,0 +1,359 @@
1
+ import busboy from "busboy";
2
+ import { IncomingMessage, OutgoingMessage, ServerResponse } from "http";
3
+ import { Http2ServerRequest, Http2ServerResponse } from "http2";
4
+ import SessionUser from "./SessionUser.js";
5
+ import { parse, serialize } from "cookie";
6
+ import TempFolder from "./TempFolder.js";
7
+ import { LocalFile } from "./LocalFile.js";
8
+ import { Writable } from "stream";
9
+ import { ServiceProvider } from "@entity-access/entity-access/dist/di/di.js";
10
+ import CookieService from "../services/CookieService.js";
11
+ import { stat } from "fs/promises";
12
+
13
+
14
+ type UnwrappedRequest = IncomingMessage | Http2ServerRequest;
15
+
16
+ export interface IFormData {
17
+ fields: { [key: string]: string};
18
+ files: LocalFile[];
19
+ }
20
+
21
+ export interface IWrappedRequest {
22
+
23
+ get host(): string;
24
+
25
+ get path(): string;
26
+
27
+ get asyncSessionUser(): Promise<SessionUser>;
28
+
29
+ get asyncJsonBody(): Promise<any>;
30
+
31
+ get asyncForm(): Promise<IFormData>;
32
+
33
+ get query(): { [key: string]: string};
34
+
35
+ get cookies(): { [key: string]: string};
36
+
37
+ get URL(): URL;
38
+
39
+ get remoteIPAddress(): string;
40
+
41
+ accepts(): string[];
42
+ accepts(... types: string[]): boolean;
43
+ }
44
+
45
+ export interface IWrappedResponse {
46
+
47
+ asyncEnd();
48
+
49
+ asyncWrite(buffer: Buffer): Promise<void>;
50
+
51
+ send(data: Buffer | string | Blob, status?: number): Promise<void>;
52
+
53
+ sendRedirect(url: string, permanent?: boolean): void;
54
+
55
+ cookie(name: string, value: string, options?: { secure?: boolean, httpOnly?: boolean, maxAge?: number });
56
+
57
+ // https://github.com/phoenixinfotech1984/node-content-range
58
+ sendFile(filePath: string, options?: {
59
+ acceptRanges?: boolean,
60
+ cacheControl?: boolean,
61
+ maxAge?: number,
62
+ etag?: boolean,
63
+ immutable?: boolean,
64
+ headers?: { [key: string]: string},
65
+ lastModified?: boolean
66
+ }): Promise<void>;
67
+
68
+ }
69
+
70
+ export type WrappedRequest = UnwrappedRequest & IWrappedRequest & {
71
+ scope: ServiceProvider;
72
+ response: WrappedResponse;
73
+ disposables: Disposable[];
74
+ };
75
+
76
+ type UnwrappedResponse = ServerResponse | Http2ServerResponse;
77
+
78
+ export type WrappedResponse = UnwrappedResponse & IWrappedResponse & {
79
+ request: WrappedRequest
80
+ };
81
+
82
+ const requestMethods: { [P in keyof IWrappedRequest]: (this: WrappedRequest) => any} = {
83
+ remoteIPAddress(this: UnwrappedRequest) {
84
+ return this.socket?.remoteAddress;
85
+ },
86
+
87
+ accepts(this: UnwrappedRequest) {
88
+ const accepts = (this.headers.accept ?? "").split(";");
89
+ return (...types: string[]) => {
90
+ if (types.length > 0) {
91
+ for (const type of types) {
92
+ for (const iterator of accepts) {
93
+ if (iterator.includes(type)) {
94
+ return true;
95
+ }
96
+ }
97
+ }
98
+ return false;
99
+ }
100
+ return accepts;
101
+ };
102
+ },
103
+
104
+ URL(this: UnwrappedRequest) {
105
+ const w = this as WrappedRequest;
106
+ return new URL(this.url, `http://${w.host}`);
107
+ },
108
+
109
+ path(this: WrappedRequest) {
110
+ return this.URL.pathname;
111
+ },
112
+
113
+ host(this: UnwrappedRequest) {
114
+ return this.headers[":authority"] ?? this.headers["host"];
115
+ },
116
+ query(this: WrappedRequest) {
117
+ const u = this.URL;
118
+ const items = {};
119
+ for (const [key, value] of u.searchParams.entries()) {
120
+ items[key] = value;
121
+ }
122
+ return items;
123
+ },
124
+ cookies(this: UnwrappedRequest) {
125
+ const cookie = this.headers.cookie;
126
+ const cookies = parse(cookie);
127
+ return cookies;
128
+ },
129
+
130
+ async asyncJsonBody(this: WrappedRequest) {
131
+ const req = this;
132
+ let buffer = null as Buffer;
133
+ let encoding = this.headers["content-encoding"] ?? "utf-8";
134
+ const contentType = this.headers["content-type"];
135
+ if (!/\/json/i.test(contentType)) {
136
+ throw new Error(`Content Type ${contentType} isn't json `);
137
+ }
138
+ await new Promise<void>((resolve, reject) => {
139
+ req.pipe(new Writable({
140
+ write(chunk, enc, callback) {
141
+ encoding ||= enc;
142
+ let b = typeof chunk === "string"
143
+ ? Buffer.from(chunk)
144
+ : chunk as Buffer;
145
+ buffer = buffer
146
+ ? Buffer.concat([buffer, b])
147
+ : b;
148
+ callback();
149
+ },
150
+ final(callback) {
151
+ resolve();
152
+ callback();
153
+ },
154
+ }), { end: true });
155
+ });
156
+ const text = buffer.toString(encoding as any);
157
+ return JSON.parse(text);
158
+ },
159
+
160
+ async asyncSessionUser(this: WrappedRequest) {
161
+ try {
162
+ const cookieService = this.scope.resolve(CookieService);
163
+ const cookie = this.cookies[cookieService.cookieName];
164
+ const sessionUser = await cookieService.createSessionUserFromCookie(cookie, this.remoteIPAddress, this.response);
165
+ return sessionUser;
166
+ } catch (error) {
167
+ console.error(error);
168
+ return new SessionUser(null, null, null);
169
+ }
170
+ },
171
+
172
+ async asyncForm(this: WrappedRequest) {
173
+ let tempFolder: TempFolder;
174
+ const result: IFormData = {
175
+ fields: {},
176
+ files: []
177
+ };
178
+ const req = this;
179
+ const bb = busboy({ headers: req.headers, defParamCharset: "utf8" });
180
+ const tasks = [];
181
+ await new Promise((resolve, reject) => {
182
+
183
+ bb.on("field", (name, value) => {
184
+ result.fields[name] = value;
185
+ });
186
+
187
+ bb.on("file", (name, file, info) => {
188
+ if (!tempFolder) {
189
+ tempFolder = new TempFolder();
190
+ this.disposables.push(tempFolder);
191
+ }
192
+ const tf = tempFolder.get(info.filename, info.mimeType);
193
+ tasks.push(tf.writeAll(file).then(() => {
194
+ result.files.push(tf);
195
+ }));
196
+ });
197
+ bb.on("error", reject);
198
+ bb.on("close", resolve);
199
+ req.pipe(bb);
200
+ });
201
+ await Promise.all(tasks);
202
+ return result;
203
+ }
204
+ };
205
+
206
+ const responseMethods: { [P in keyof IWrappedResponse]: (this: WrappedResponse) => any} = {
207
+
208
+ asyncEnd() {
209
+ return () => new Promise<void>((resolve) => this.end(resolve));
210
+ },
211
+
212
+ asyncWrite() {
213
+ return (buffer: Buffer, start?: number, length?: number) => {
214
+ return new Promise((resolve) =>
215
+ this.write(buffer, resolve)
216
+ );
217
+ };
218
+ },
219
+
220
+ cookie() {
221
+ return (name: string, value: string, options = {}) => {
222
+ const cv = this.getHeaders()["set-cookie"];
223
+ const cookies = Array.isArray(cv) ? cv : [cv];
224
+ const nk = cookies.filter((x) => x.startsWith(name + "="));
225
+ nk.push(serialize(name, value, options));
226
+ this.setHeader("set-cookie", nk);
227
+ }
228
+ },
229
+
230
+ send(this: WrappedResponse) {
231
+ return async (data: Buffer | string, status: number = 200) => {
232
+ try {
233
+ this.statusCode = status;
234
+ this.writeHead(this.statusCode, this.getHeaders());
235
+ await new Promise<void>((resolve, reject) => {
236
+ this.write(data, (error) => error ? reject(error) : resolve());
237
+ });
238
+ return this.asyncEnd();
239
+ } catch (error) {
240
+ console.error(error);
241
+ }
242
+ };
243
+ },
244
+ sendRedirect() {
245
+ return (location: string, permanent = false) => {
246
+ this.statusCode = 301;
247
+ this.writeHead(this.statusCode, {
248
+ location
249
+ });
250
+ return this.asyncEnd();
251
+ }
252
+ },
253
+ sendFile() {
254
+ return async (filePath: string, options?: {
255
+ acceptRanges?: boolean,
256
+ cacheControl?: boolean,
257
+ maxAge?: number,
258
+ etag?: boolean,
259
+ immutable?: boolean,
260
+ headers?: { [key: string]: string},
261
+ lastModified?: boolean
262
+ }) => {
263
+ /** Calculate Size of file */
264
+ const { size } = await stat(filePath);
265
+ const range = this.request.headers.range;
266
+
267
+ const lf = new LocalFile(filePath);
268
+
269
+ /** Check for Range header */
270
+ if (!range) {
271
+ this.writeHead(200, {
272
+ "Content-Length": size,
273
+ "Content-Type": "video/mp4"
274
+ });
275
+
276
+ await lf.writeTo(this);
277
+
278
+ return this.asyncEnd();
279
+ }
280
+
281
+ /** Extracting Start and End value from Range Header */
282
+ let [start, end] = range.replace(/bytes=/, "").split("-") as any[];
283
+ start = parseInt(start, 10);
284
+ end = end ? parseInt(end, 10) : size - 1;
285
+
286
+ if (!isNaN(start) && isNaN(end)) {
287
+ start = start;
288
+ end = size - 1;
289
+ }
290
+ if (isNaN(start) && !isNaN(end)) {
291
+ start = size - end;
292
+ end = size - 1;
293
+ }
294
+
295
+ // Handle unavailable range request
296
+ if (start >= size || end >= size) {
297
+ // Return the 416 Range Not Satisfiable.
298
+ this.writeHead(416, {
299
+ "Content-Range": `bytes */${size}`
300
+ });
301
+ return this.asyncEnd();
302
+ }
303
+
304
+ /** Sending Partial Content With HTTP Code 206 */
305
+ this.writeHead(206, {
306
+ "Content-Range": `bytes ${start}-${end}/${size}`,
307
+ "Accept-Ranges": "bytes",
308
+ "Content-Length": end - start + 1,
309
+ "Content-Type": "video/mp4"
310
+ });
311
+
312
+ await lf.writeTo(this, start, end);
313
+
314
+ }
315
+ },
316
+ };
317
+
318
+ export const Wrapped = {
319
+ request: (req: UnwrappedRequest) => {
320
+ for (const key in requestMethods) {
321
+ if (Object.prototype.hasOwnProperty.call(requestMethods, key)) {
322
+ const element = requestMethods[key];
323
+ Object.defineProperty(req, key, {
324
+ get() {
325
+ const value = element.call(this);
326
+ Object.defineProperty(this, key, { value, enumerable: true, writable: false });
327
+ return value;
328
+ },
329
+ enumerable: true,
330
+ configurable: true
331
+ });
332
+ }
333
+ }
334
+ const wr = req as WrappedRequest;
335
+ wr.disposables = [];
336
+ return wr;
337
+ },
338
+
339
+ response: (req: WrappedRequest, res: UnwrappedResponse) => {
340
+ for (const key in responseMethods) {
341
+ if (Object.prototype.hasOwnProperty.call(responseMethods, key)) {
342
+ const element = responseMethods[key];
343
+ Object.defineProperty(res, key, {
344
+ get() {
345
+ const value = element.call(this);
346
+ Object.defineProperty(this, key, { value, enumerable: true, writable: false });
347
+ return value;
348
+ },
349
+ enumerable: true,
350
+ configurable: true
351
+ });
352
+ }
353
+ }
354
+ const wr = res as WrappedResponse;
355
+ wr.request = req;
356
+ req.response = wr;
357
+ return wr;
358
+ }
359
+ }
@@ -0,0 +1,3 @@
1
+ export function cached() {
2
+
3
+ }
@@ -0,0 +1,3 @@
1
+ export function Authorize() {
2
+
3
+ }
@@ -0,0 +1,3 @@
1
+ export default function jsonParser() {
2
+
3
+ }
@@ -2,10 +2,10 @@ import Inject, { RegisterScoped, RegisterSingleton, ServiceProvider } from "@ent
2
2
  import TokenService, { IAuthCookie } from "./TokenService.js";
3
3
  import TimedCache from "@entity-access/entity-access/dist/common/cache/TimedCache.js";
4
4
  import { BaseDriver } from "@entity-access/entity-access/dist/drivers/base/BaseDriver.js";
5
- import { Request, Response } from "express";
6
5
  import cluster from "cluster";
7
6
  import SessionUser from "../core/SessionUser.js";
8
7
  import UserSessionProvider from "./UserSessionProvider.js";
8
+ import { WrappedResponse } from "../core/Wrapped.js";
9
9
 
10
10
  /**
11
11
  * This will track userID,cookie pair so we can
@@ -65,13 +65,11 @@ export default class CookieService {
65
65
 
66
66
  public clearCache = clearCache;
67
67
 
68
- async createSessionUser(req: Request, resp: Response) {
69
- cookieName ??= this.tokenService.authCookieName;
70
- const sessionCookie = req.cookies[cookieName];
71
- return req.user = await this.createSessionUserFromCookie(sessionCookie, req.ip, resp);
68
+ public get cookieName() {
69
+ return cookieName ??= this.tokenService.authCookieName;
72
70
  }
73
71
 
74
- async createSessionUserFromCookie(cookie: string, ip: string, resp?: Response) {
72
+ async createSessionUserFromCookie(cookie: string, ip: string, resp?: WrappedResponse) {
75
73
  const user = new SessionUser(resp, cookieName, this.tokenService);
76
74
  try {
77
75
  user.ipAddress = ip;
@@ -0,0 +1,247 @@
1
+ import * as forge from "node-forge";
2
+ import * as crypto from "crypto";
3
+ import * as acme from "acme-client";
4
+ import DateTime from "@entity-access/entity-access/dist/types/DateTime.js";
5
+ import cluster from "cluster";
6
+ import { existsSync, writeFileSync, readFileSync, unlinkSync, mkdirSync } from "fs";
7
+ import { join } from "path"
8
+ import ensureDir, { deleteIfExists } from "../core/FileApi.js";
9
+ import Inject, { RegisterSingleton } from "@entity-access/entity-access/dist/di/di.js";
10
+ import ChallengeStore from "./ChallengeStore.js";
11
+ import * as tls from "node:tls";
12
+
13
+ export interface IAcmeOptions {
14
+ sslMode?: string,
15
+ accountPrivateKeyPath?: string,
16
+ emailAddress?: string,
17
+ mode?: "production" | "self-signed" | "staging",
18
+ endPoint?: string,
19
+ eabKid?: string,
20
+ eabHmac?: string
21
+ }
22
+
23
+ export interface ICertOptions extends IAcmeOptions {
24
+ host: string,
25
+ };
26
+
27
+ @RegisterSingleton
28
+ export default class ACME {
29
+
30
+ @Inject
31
+ private challengeStore: ChallengeStore;
32
+
33
+ private map = new Map<string, tls.SecureContext>();
34
+
35
+ public async getSecureContext(options: ICertOptions) {
36
+ const { host } = options;
37
+ let sc = this.map.get(host);
38
+ if (sc) {
39
+ return sc;
40
+ }
41
+
42
+ const { key , cert } = await this.setup(options)
43
+ sc = tls.createSecureContext({ cert, key });
44
+ this.map.set(host, sc);
45
+ return sc;
46
+ }
47
+
48
+ public async setup({
49
+ host,
50
+ sslMode = "./",
51
+ accountPrivateKeyPath = "./",
52
+ emailAddress = "",
53
+ mode = "production" as "production" | "self-signed" | "staging",
54
+ endPoint = "",
55
+ eabKid = "",
56
+ eabHmac = ""
57
+ }) {
58
+
59
+ if (mode === "self-signed") {
60
+ return this.setupSelfSigned(sslMode);
61
+ }
62
+
63
+ const hostRoot = join(sslMode, host);
64
+
65
+ ensureDir(hostRoot);
66
+
67
+ const keyPath = join(hostRoot, "cert.key");
68
+ const certPath = join(hostRoot, "cert.crt");
69
+
70
+ const logs = [];
71
+
72
+ try {
73
+
74
+ const maintainerEmail = emailAddress;
75
+
76
+ let externalAccountBinding;
77
+
78
+ if (eabKid) {
79
+ externalAccountBinding = {
80
+ kid: eabKid,
81
+ hmacKey: eabHmac
82
+ };
83
+ }
84
+
85
+ let cert:string;
86
+ let key:string;
87
+
88
+ if (!existsSync(keyPath)) {
89
+ deleteIfExists(certPath);
90
+ key = (await acme.crypto.createPrivateRsaKey()).toString();
91
+ writeFileSync(keyPath, key);
92
+ console.log(`Creating key at ${keyPath}`);
93
+ } else {
94
+ key = readFileSync(keyPath, "utf8");
95
+ }
96
+
97
+ // load cert...
98
+ if (existsSync(certPath)) {
99
+ cert = readFileSync(certPath, "utf8");
100
+ const certificate = new crypto.X509Certificate(cert);
101
+ const validTo = DateTime.parse(certificate.validTo).diff(DateTime.now);
102
+ if (validTo.totalDays > 30) {
103
+ console.log(`Reusing certificate, valid for ${validTo.totalDays}`);
104
+ return { cert , key };
105
+ }
106
+ console.log(`Deleting old certificates`);
107
+ unlinkSync(certPath);
108
+ }
109
+
110
+ if (!cluster.isPrimary) {
111
+ console.log(`Generating Self Signed SSL Certificate for ${host} in cluster worker. Contact administrator.`);
112
+ return this.setupSelfSigned();
113
+ }
114
+
115
+ let accountKey;
116
+ if( existsSync(accountPrivateKeyPath) ) {
117
+ console.log("Reusing the account private key.");
118
+ accountKey = readFileSync(accountPrivateKeyPath);
119
+ } else {
120
+ console.log("Creating new private key.");
121
+ accountKey = await acme.crypto.createPrivateKey();
122
+ writeFileSync(accountPrivateKeyPath, accountKey);
123
+ }
124
+
125
+ acme.setLogger((message) => {
126
+ // console.log(message);
127
+ logs.push(message);
128
+ });
129
+
130
+ let altNames;
131
+
132
+ // auto renew...
133
+ const client = new acme.Client({
134
+ directoryUrl: endPoint || acme.directory.letsencrypt[mode],
135
+ accountKey,
136
+ externalAccountBinding,
137
+ });
138
+
139
+ /* Create CSR */
140
+ const [csrKey, csr] = await acme.crypto.createCsr({
141
+ commonName: host,
142
+ altNames
143
+ }, key);
144
+
145
+
146
+
147
+ /* Certificate */
148
+ cert = await client.auto({
149
+ csr,
150
+ email: maintainerEmail,
151
+ termsOfServiceAgreed: true,
152
+ skipChallengeVerification: true,
153
+ challengePriority: ["http-01"],
154
+ challengeCreateFn: (authz, challenge, keyAuthorization) => {
155
+ if (challenge.type !== "http-01") {
156
+ return;
157
+ }
158
+ return this.challengeStore.save(challenge.token, keyAuthorization);
159
+ },
160
+ challengeRemoveFn: (authz, challenge, keyAuthorization) => {
161
+ return this.challengeStore.remove(challenge.token);
162
+ },
163
+ });
164
+
165
+ writeFileSync(certPath, cert);
166
+
167
+ return { cert, key };
168
+ } catch (error) {
169
+ console.log(logs.join("\n"));
170
+ console.error(error);
171
+ throw error;
172
+ }
173
+ }
174
+
175
+ public setupSelfSigned(sslMode = "./") {
176
+
177
+ const selfSigned = `${sslMode}/self-signed/`;
178
+
179
+ ensureDir(selfSigned);
180
+
181
+ const certPath = `${selfSigned}/cert.crt`;
182
+ const keyPath = `${selfSigned}/key.pem`;
183
+
184
+ let key;
185
+ let cert;
186
+
187
+ if (existsSync(certPath) && existsSync(keyPath)) {
188
+ key = readFileSync(keyPath);
189
+ cert = readFileSync(certPath);
190
+ return { key, cert };
191
+ }
192
+
193
+ const pki = forge.default.pki;
194
+
195
+ // generate a key pair or use one you have already
196
+ const keys = pki.rsa.generateKeyPair(2048);
197
+
198
+ // create a new certificate
199
+ const crt = pki.createCertificate();
200
+
201
+ // fill the required fields
202
+ crt.publicKey = keys.publicKey;
203
+ crt.serialNumber = '01';
204
+ crt.validity.notBefore = new Date();
205
+ crt.validity.notAfter = new Date();
206
+ crt.validity.notAfter.setFullYear(crt.validity.notBefore.getFullYear() + 40);
207
+
208
+ // use your own attributes here, or supply a csr (check the docs)
209
+ const attrs = [
210
+ {
211
+ name: 'commonName',
212
+ value: 'dev.socialmail.in'
213
+ }, {
214
+ name: 'countryName',
215
+ value: 'IN'
216
+ }, {
217
+ shortName: 'ST',
218
+ value: 'Maharashtra'
219
+ }, {
220
+ name: 'localityName',
221
+ value: 'Navi Mumbai'
222
+ }, {
223
+ name: 'organizationName',
224
+ value: 'NeuroSpeech Technologies Pvt Ltd'
225
+ }, {
226
+ shortName: 'OU',
227
+ value: 'Test'
228
+ }
229
+ ];
230
+
231
+ // here we set subject and issuer as the same one
232
+ crt.setSubject(attrs);
233
+ crt.setIssuer(attrs);
234
+
235
+ // the actual certificate signing
236
+ crt.sign(keys.privateKey);
237
+
238
+ // now convert the Forge certificate to PEM format
239
+ cert = pki.certificateToPem(crt);
240
+ key = pki.privateKeyToPem(keys.privateKey);
241
+
242
+ writeFileSync(certPath, cert);
243
+ writeFileSync(keyPath, key);
244
+
245
+ return { key, cert };
246
+ }
247
+ }
@@ -0,0 +1,26 @@
1
+ import { RegisterSingleton } from "@entity-access/entity-access/dist/di/di.js";
2
+ import ensureDir from "../core/FileApi.js";
3
+ import { readFileSync, unlinkSync, writeFileSync } from "fs";
4
+ import { join } from "node:path";
5
+
6
+ const path = "./challenges";
7
+
8
+ @RegisterSingleton
9
+ export default class ChallengeStore {
10
+
11
+ constructor() {
12
+ ensureDir(path);
13
+ }
14
+
15
+ async get(name: string) {
16
+ return readFileSync(join(path, name));
17
+ }
18
+
19
+ async save(name: string, value: string) {
20
+ writeFileSync(join(path, name), value, "utf8");
21
+ }
22
+
23
+ async remove(name: string) {
24
+ unlinkSync(join(path, name));
25
+ }
26
+ }
package/test.js CHANGED
@@ -7,6 +7,4 @@ import { fileURLToPath } from "node:url";
7
7
  const sp = ServerPages.create();
8
8
  sp.registerRoutes(join(dirname( fileURLToPath(import.meta.url)), "./dist/tests/logger"));
9
9
 
10
- const app = sp.build();
11
-
12
- app.listen(8080);
10
+ const app = sp.build({ createSocketService: false, protocol: "https2" });