web_plsql 0.12.1 → 0.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/README.md +3 -5
  2. package/examples/config-apex.js +2 -3
  3. package/examples/config-docker.js +0 -1
  4. package/examples/config-docker.json +1 -2
  5. package/examples/config-native.js +2 -3
  6. package/examples/sql/install.sql +2 -0
  7. package/examples/sql/sample_package.sql +2 -1
  8. package/examples/sql/sample_package_body.sql +71 -16
  9. package/examples/sql/uninstall.sql +2 -0
  10. package/package.json +9 -11
  11. package/src/handler/handlerMetrics.js +68 -0
  12. package/src/{handlerUpload.js → handler/handlerUpload.js} +2 -2
  13. package/src/{cgi.js → handler/plsql/cgi.js} +6 -2
  14. package/src/{errorPage.js → handler/plsql/errorPage.js} +5 -5
  15. package/src/{handlerPlSql.js → handler/plsql/handlerPlSql.js} +2 -2
  16. package/src/{parsePage.js → handler/plsql/parsePage.js} +24 -8
  17. package/src/{procedure.js → handler/plsql/procedure.js} +7 -10
  18. package/src/{procedureError.js → handler/plsql/procedureError.js} +2 -2
  19. package/src/{procedureNamed.js → handler/plsql/procedureNamed.js} +4 -4
  20. package/src/{procedureSanitize.js → handler/plsql/procedureSanitize.js} +6 -6
  21. package/src/{procedureVariable.js → handler/plsql/procedureVariable.js} +3 -3
  22. package/src/{request.js → handler/plsql/request.js} +3 -9
  23. package/src/{sendResponse.js → handler/plsql/sendResponse.js} +6 -6
  24. package/src/{upload.js → handler/plsql/upload.js} +2 -2
  25. package/src/index.js +8 -9
  26. package/src/server/config.js +45 -0
  27. package/src/server/connections.js +40 -0
  28. package/src/server/server.js +184 -0
  29. package/src/types.js +2 -13
  30. package/src/util/date.js +23 -0
  31. package/src/{shutdown.js → util/shutdown.js} +3 -16
  32. package/src/util/type.js +6 -0
  33. package/types/handler/handlerMetrics.d.ts +25 -0
  34. package/types/{handlerUpload.d.ts → handler/handlerUpload.d.ts} +2 -2
  35. package/types/{cgi.d.ts → handler/plsql/cgi.d.ts} +2 -2
  36. package/types/{errorPage.d.ts → handler/plsql/errorPage.d.ts} +3 -3
  37. package/types/{handlerPlSql.d.ts → handler/plsql/handlerPlSql.d.ts} +2 -2
  38. package/types/handler/plsql/parsePage.d.ts +3 -0
  39. package/types/{procedure.d.ts → handler/plsql/procedure.d.ts} +5 -5
  40. package/types/{procedureError.d.ts → handler/plsql/procedureError.d.ts} +4 -4
  41. package/types/{procedureNamed.d.ts → handler/plsql/procedureNamed.d.ts} +3 -3
  42. package/types/{procedureSanitize.d.ts → handler/plsql/procedureSanitize.d.ts} +5 -5
  43. package/types/{procedureVariable.d.ts → handler/plsql/procedureVariable.d.ts} +3 -3
  44. package/types/{request.d.ts → handler/plsql/request.d.ts} +2 -2
  45. package/types/{sendResponse.d.ts → handler/plsql/sendResponse.d.ts} +3 -1
  46. package/types/{upload.d.ts → handler/plsql/upload.d.ts} +1 -1
  47. package/types/index.d.ts +8 -9
  48. package/types/server/config.d.ts +2 -0
  49. package/types/server/server.d.ts +52 -0
  50. package/types/types.d.ts +4 -37
  51. package/types/util/type.d.ts +1 -0
  52. package/src/handlerMetrics.js +0 -46
  53. package/src/server.js +0 -223
  54. package/src/tty.js +0 -48
  55. package/types/handlerMetrics.d.ts +0 -4
  56. package/types/parsePage.d.ts +0 -3
  57. package/types/server.d.ts +0 -14
  58. package/types/tty.d.ts +0 -4
  59. /package/src/{handlerLogger.js → handler/handlerLogger.js} +0 -0
  60. /package/src/{requestError.js → handler/plsql/requestError.js} +0 -0
  61. /package/src/{stream.js → handler/plsql/stream.js} +0 -0
  62. /package/src/{error.js → util/errorToString.js} +0 -0
  63. /package/src/{file.js → util/file.js} +0 -0
  64. /package/src/{oracle.js → util/oracle.js} +0 -0
  65. /package/src/{trace.js → util/trace.js} +0 -0
  66. /package/types/{handlerLogger.d.ts → handler/handlerLogger.d.ts} +0 -0
  67. /package/types/{requestError.d.ts → handler/plsql/requestError.d.ts} +0 -0
  68. /package/types/{stream.d.ts → handler/plsql/stream.d.ts} +0 -0
  69. /package/types/{error.d.ts → util/errorToString.d.ts} +0 -0
  70. /package/types/{file.d.ts → util/file.d.ts} +0 -0
  71. /package/types/{oracle.d.ts → util/oracle.d.ts} +0 -0
  72. /package/types/{shutdown.d.ts → util/shutdown.d.ts} +0 -0
  73. /package/types/{trace.d.ts → util/trace.d.ts} +0 -0
@@ -1,4 +1,6 @@
1
1
  export function sendResponse(req: Request, res: Response, page: pageType): void;
2
2
  export type Request = import("express").Request;
3
3
  export type Response = import("express").Response;
4
- export type pageType = import("./types.js").pageType;
4
+ export type CookieOptions = import("express").CookieOptions;
5
+ export type pageType = import("../../types.js").pageType;
6
+ export type cookieType = import("../../types.js").cookieType;
@@ -2,4 +2,4 @@ export function getFiles(req: Request): fileUploadType[];
2
2
  export function uploadFile(file: fileUploadType, doctable: string, databaseConnection: Connection): Promise<void>;
3
3
  export type Request = import("express").Request;
4
4
  export type Connection = import("oracledb").Connection;
5
- export type fileUploadType = import("./types.js").fileUploadType;
5
+ export type fileUploadType = import("../../types.js").fileUploadType;
package/types/index.d.ts CHANGED
@@ -1,9 +1,8 @@
1
- export * from "./shutdown.js";
2
- export * from "./oracle.js";
3
- export * from "./file.js";
4
- export * from "./tty.js";
5
- export { handlerWebPlSql } from "./handlerPlSql.js";
6
- export { handlerLogger } from "./handlerLogger.js";
7
- export { handlerUpload } from "./handlerUpload.js";
8
- export { createHttpServer, createHttpsServer, startHttpServer, loadConfig } from "./server.js";
9
- export { initMetrics, handlerMetrics } from "./handlerMetrics.js";
1
+ export * from "./util/shutdown.js";
2
+ export * from "./util/oracle.js";
3
+ export * from "./util/file.js";
4
+ export { handlerWebPlSql } from "./handler/plsql/handlerPlSql.js";
5
+ export { handlerLogger } from "./handler/handlerLogger.js";
6
+ export { handlerUpload } from "./handler/handlerUpload.js";
7
+ export { handlerMetrics } from "./handler/handlerMetrics.js";
8
+ export { createServer, startServer, loadConfig, startServerConfig } from "./server/server.js";
@@ -0,0 +1,2 @@
1
+ export function showConfig(config: configType): void;
2
+ export type configType = import("../types.js").configType;
@@ -0,0 +1,52 @@
1
+ export function createServer(app: Express, ssl?: sslConfig): http.Server | https.Server;
2
+ export function startServer(config: configType, ssl?: sslConfig): Promise<webServer>;
3
+ export function loadConfig(filename?: string): configType;
4
+ export function startServerConfig(filename?: string, ssl?: sslConfig): Promise<webServer>;
5
+ export type Socket = import("node:net").Socket;
6
+ export type Express = import("express").Express;
7
+ export type Request = import("express").Request;
8
+ export type Response = import("express").Response;
9
+ export type NextFunction = import("express").NextFunction;
10
+ export type Pool = import("oracledb").Pool;
11
+ export type environmentType = import("../types.js").environmentType;
12
+ export type configType = import("../types.js").configType;
13
+ /**
14
+ * - Web server interface.
15
+ */
16
+ export type webServer = {
17
+ /**
18
+ * - Configuration object.
19
+ */
20
+ config: configType;
21
+ /**
22
+ * - Oracle connection pools.
23
+ */
24
+ connectionPools: Pool[];
25
+ /**
26
+ * - Express app.
27
+ */
28
+ app: Express;
29
+ /**
30
+ * - Native Node http(s) server instance.
31
+ */
32
+ server: http.Server | https.Server;
33
+ /**
34
+ * - Shutdown function.
35
+ */
36
+ shutdown: () => Promise<void>;
37
+ };
38
+ /**
39
+ * - SSL configuration.
40
+ */
41
+ export type sslConfig = {
42
+ /**
43
+ * - key filename.
44
+ */
45
+ keyFilename: string;
46
+ /**
47
+ * - cert filename.
48
+ */
49
+ certFilename: string;
50
+ };
51
+ import http from 'node:http';
52
+ import https from 'node:https';
package/types/types.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  /**
2
2
  * @typedef {import('oracledb').BindParameter} BindParameter
3
+ * @typedef {import('express').CookieOptions} CookieOptions
3
4
  */
4
5
  /**
5
6
  * @typedef {'basic' | 'debug'} errorStyleType
@@ -78,7 +79,6 @@ export const z$configPlSqlType: z.ZodObject<{
78
79
  * @property {configStaticType[]} routeStatic - The static routes.
79
80
  * @property {configPlSqlType[]} routePlSql - The PL/SQL routes.
80
81
  * @property {string} loggerFilename - name of the request logger filename or '' if not required.
81
- * @property {boolean} monitorConsole - Enable console status monitor.
82
82
  */
83
83
  export const z$configType: z.ZodObject<{
84
84
  port: z.ZodNumber;
@@ -103,9 +103,9 @@ export const z$configType: z.ZodObject<{
103
103
  }>;
104
104
  }, z.core.$strict>>;
105
105
  loggerFilename: z.ZodString;
106
- monitorConsole: z.ZodBoolean;
107
106
  }, z.core.$strict>;
108
107
  export type BindParameter = import("oracledb").BindParameter;
108
+ export type CookieOptions = import("express").CookieOptions;
109
109
  export type errorStyleType = "basic" | "debug";
110
110
  export type configStaticType = {
111
111
  /**
@@ -187,10 +187,6 @@ export type configType = {
187
187
  * - name of the request logger filename or '' if not required.
188
188
  */
189
189
  loggerFilename: string;
190
- /**
191
- * - Enable console status monitor.
192
- */
193
- monitorConsole: boolean;
194
190
  };
195
191
  /**
196
192
  * Environment variables as string key-value pairs
@@ -247,25 +243,9 @@ export type cookieType = {
247
243
  */
248
244
  value: string;
249
245
  /**
250
- * - The path of the cookie.
246
+ * - The cookie options.
251
247
  */
252
- path?: string;
253
- /**
254
- * - The domain of the cookie.
255
- */
256
- domain?: string;
257
- /**
258
- * - The secure flag.
259
- */
260
- secure?: string;
261
- /**
262
- * - The expiration date.
263
- */
264
- expires?: Date;
265
- /**
266
- * - The httpOnly flag.
267
- */
268
- httpOnly?: boolean;
248
+ options: CookieOptions;
269
249
  };
270
250
  /**
271
251
  * - The page.
@@ -297,17 +277,4 @@ export type pageType = {
297
277
  fileBlob: Buffer | null;
298
278
  };
299
279
  };
300
- /**
301
- * - The metrics.
302
- */
303
- export type metricsType = {
304
- /**
305
- * - The total number of requests.
306
- */
307
- totalRequests: number;
308
- /**
309
- * - The number of requests in the last second.
310
- */
311
- requestsInLastInterval: number;
312
- };
313
280
  import z from 'zod';
@@ -0,0 +1 @@
1
+ export function isStringOrArrayOfString(value: unknown): value is string | string[];
@@ -1,46 +0,0 @@
1
- import debugModule from 'debug';
2
- const debug = debugModule('webplsql:handlerMetrics');
3
-
4
- /**
5
- * @typedef {import('express').RequestHandler} RequestHandler
6
- * @typedef {import('./types.js').metricsType} metricsType
7
- */
8
-
9
- /**
10
- * metrics initializer.
11
- * @returns {metricsType} The metrics.
12
- */
13
- export const initMetrics = () => {
14
- return {
15
- totalRequests: 0,
16
- requestsInLastInterval: 0,
17
- };
18
- };
19
-
20
- /**
21
- * The metrics handler.
22
- * @param {metricsType} metrics - the nmetrics.
23
- * @returns {RequestHandler} - Request handler.
24
- */
25
- export const handlerMetrics = (metrics) => {
26
- debug('register');
27
-
28
- metrics.totalRequests = 0;
29
- metrics.requestsInLastInterval = 0;
30
-
31
- return (req, res, next) => {
32
- metrics.totalRequests++;
33
- metrics.requestsInLastInterval++;
34
-
35
- if (debug.enabled) {
36
- const start = process.hrtime();
37
- res.on('finish', () => {
38
- const [seconds, nanoseconds] = process.hrtime(start);
39
- const duration = seconds * 1000 + nanoseconds / 1_000_000;
40
- debug(`Request to ${req.params?.name} ${req.url} took ${duration.toFixed(3)}ms`);
41
- });
42
- }
43
-
44
- next();
45
- };
46
- };
package/src/server.js DELETED
@@ -1,223 +0,0 @@
1
- import debugModule from 'debug';
2
- const debug = debugModule('webplsql:server');
3
-
4
- import http from 'node:http';
5
- import https from 'node:https';
6
- import express from 'express';
7
- import bodyParser from 'body-parser';
8
- import cookieParser from 'cookie-parser';
9
- import compression from 'compression';
10
- import {z$configType} from './types.js';
11
- import {installShutdown} from './shutdown.js';
12
- import {writeAfterEraseLine} from './tty.js';
13
- import {poolCreate, poolsClose} from '../src/oracle.js';
14
- import {handlerUpload} from './handlerUpload.js';
15
- import {handlerLogger} from './handlerLogger.js';
16
- import {initMetrics, handlerMetrics} from './handlerMetrics.js';
17
- import {handlerWebPlSql} from './handlerPlSql.js';
18
- import {readFileSyncUtf8, getJsonFile} from './file.js';
19
-
20
- /**
21
- * @typedef {import('express').Express} Express
22
- * @typedef {import('express').Request} Request
23
- * @typedef {import('express').Response} Response
24
- * @typedef {import('express').NextFunction} NextFunction
25
- * @typedef {import('oracledb').Pool} Pool
26
- * @typedef {import('./types.js').environmentType} environmentType
27
- * @typedef {import('./types.js').configType} configType
28
- */
29
-
30
- /**
31
- * Show configuration.
32
- * @param {configType} config - The config.
33
- * @returns {void}
34
- */
35
- export const showConfig = (config) => {
36
- console.log('-'.repeat(80));
37
- console.log('NODE PL/SQL SERVER');
38
- console.log('-'.repeat(80));
39
-
40
- console.log(`Server port: ${config.port}`);
41
- console.log(`Access log: ${config.loggerFilename.length > 0 ? config.loggerFilename : ''}`);
42
- console.log(`Monitor console: ${config.monitorConsole ? 'on' : 'off'}`);
43
-
44
- if (config.routeStatic.length > 0) {
45
- console.log('');
46
- config.routeStatic.forEach((e, i) => {
47
- console.log(`Static route #${i + 1}`);
48
- console.log(` Route: "${e.route}"`);
49
- console.log(` Directory path: "${e.directoryPath}"`);
50
- });
51
- }
52
-
53
- if (config.routePlSql.length > 0) {
54
- console.log('');
55
- config.routePlSql.forEach((e, i) => {
56
- console.log(`Application route #${i + 1}`);
57
- console.log(` Route: "http://localhost:${config.port}${e.route}"`);
58
- console.log(` Oracle user: "${e.user}"`);
59
- console.log(` Oracle server: "${e.connectString}"`);
60
- console.log(` Oracle document table: "${e.documentTable}"`);
61
- console.log(` Default page: "${e.defaultPage}"`);
62
- console.log(` Path alias: "${e.pathAlias}"`);
63
- console.log(` Path alias procedure: "${e.pathAliasProcedure}"`);
64
- console.log(` Exclution list: "${e.exclusionList?.join(', ')}"`);
65
- console.log(` Request validation function: "${e.requestValidationFunction}"`);
66
- console.log(` Error style: "${e.errorStyle}"`);
67
- });
68
- }
69
-
70
- console.log('-'.repeat(80));
71
- };
72
-
73
- /**
74
- * Create HTTP server.
75
- * @param {Express} app - express application
76
- * @param {number} port - port number
77
- * @param {Pool[]} connectionPools - database connection
78
- * @returns {Promise<http.Server>} - server
79
- */
80
- export const createHttpServer = (app, port, connectionPools) => {
81
- return new Promise((resolve) => {
82
- // Create server
83
- const server = http.createServer({}, app);
84
-
85
- // Install shutdown handler
86
- installShutdown(async () => {
87
- // Close database pool.
88
- await poolsClose(connectionPools);
89
-
90
- // Close server
91
- return new Promise((resolve) => server.close(() => resolve()));
92
- });
93
-
94
- // Listen on HTTP ports
95
- server.listen(port, () => {
96
- resolve(server);
97
- });
98
- });
99
- };
100
-
101
- /**
102
- * Create HTTPS server.
103
- * @param {Express} app - express application
104
- * @param {string} sslKeyFilename - ssl
105
- * @param {string} sslCertFilename - ssl
106
- * @param {number} port - port number
107
- * @param {Pool[]} connectionPools - database connection
108
- * @returns {Promise<https.Server>} - server
109
- */
110
- export const createHttpsServer = (app, sslKeyFilename, sslCertFilename, port, connectionPools) => {
111
- return new Promise((resolve) => {
112
- // Load certificates
113
- const key = readFileSyncUtf8(sslKeyFilename);
114
- const cert = readFileSyncUtf8(sslCertFilename);
115
-
116
- // Create server
117
- const server = https.createServer({key, cert}, app);
118
-
119
- // Install shutdown handler
120
- installShutdown(async () => {
121
- // Close database pool.
122
- await poolsClose(connectionPools);
123
-
124
- // Close server
125
- return new Promise((resolve) => server.close(() => resolve()));
126
- });
127
-
128
- // Listen on HTTP ports
129
- server.listen(port, () => {
130
- resolve(server);
131
- });
132
- });
133
- };
134
-
135
- /**
136
- * Start HTTP server.
137
- * @param {configType} config - The config.
138
- * @returns {Promise<void>} - Promise.
139
- */
140
- export const startHttpServer = async (config) => {
141
- debug('startHttpServer', config);
142
-
143
- config = z$configType.parse(config);
144
-
145
- showConfig(config);
146
-
147
- // Create express app
148
- const app = express();
149
-
150
- // Debug requests
151
- if (debug.enabled) {
152
- app.use((req, res, next) => {
153
- console.log(`Request: "${req.method}" "${req.url}"`);
154
- next();
155
- });
156
- }
157
-
158
- // Metrics
159
- const metrics = initMetrics();
160
- app.use(handlerMetrics(metrics));
161
-
162
- // Access log
163
- if (config.loggerFilename.length > 0) {
164
- app.use(handlerLogger(config.loggerFilename));
165
- }
166
-
167
- // Serving static files
168
- for (const i of config.routeStatic) {
169
- app.use(i.route, express.static(i.directoryPath));
170
- }
171
-
172
- // Default middleware
173
- app.use(handlerUpload());
174
- app.use(bodyParser.json());
175
- app.use(bodyParser.urlencoded({extended: true}));
176
- app.use(cookieParser());
177
- app.use(compression());
178
-
179
- /** @type {Pool[]} */
180
- const connectionPools = [];
181
-
182
- // Oracle pl/sql express middleware
183
- for (const i of config.routePlSql) {
184
- // Allocate the Oracle database pool
185
- const pool = await poolCreate(i.user, i.password, i.connectString);
186
- connectionPools.push(pool);
187
-
188
- app.use([`${i.route}/:name`, i.route], handlerWebPlSql(pool, i));
189
- }
190
-
191
- // Update metrics every second
192
- if (config.monitorConsole) {
193
- setInterval(() => {
194
- // Update requests per second
195
- const requestsLastInterval = metrics.requestsInLastInterval;
196
- metrics.requestsInLastInterval = 0;
197
-
198
- // Clear console and display metrics
199
- writeAfterEraseLine(`Total requests: ${metrics.totalRequests}, requests per second: ${requestsLastInterval}`);
200
- }, 1000);
201
- }
202
-
203
- await createHttpServer(app, config.port, connectionPools);
204
- };
205
-
206
- /**
207
- * Load configuration.
208
- * @param {string} [filename] - The configuration filename.
209
- * @returns {configType} - Promise.
210
- */
211
- export const loadConfig = (filename) => {
212
- debug('loadConfig', filename);
213
-
214
- if (typeof filename !== 'string' || filename.length === 0) {
215
- filename = 'config.json';
216
- }
217
-
218
- const data = getJsonFile(filename);
219
-
220
- const config = z$configType.parse(data);
221
-
222
- return config;
223
- };
package/src/tty.js DELETED
@@ -1,48 +0,0 @@
1
- /**
2
- * Write to stdout.
3
- * @param {string} text - text to write.
4
- * @returns {void}
5
- */
6
- export const write = (text) => {
7
- if (process.stdout.isTTY) {
8
- process.stdout.write(text);
9
- }
10
- };
11
-
12
- /**
13
- * Write to stdout with new line.
14
- * @param {string} text - text to write.
15
- * @returns {void}
16
- */
17
- export const writeNewLine = (text = '') => {
18
- if (process.stdout.isTTY) {
19
- process.stdout.write(`${text}\n`);
20
- }
21
- };
22
-
23
- /**
24
- * Write to stdout after erasing line.
25
- * @param {string} text - text to write.
26
- * @returns {void}
27
- */
28
- export const writeAfterEraseLine = (text) => {
29
- if (process.stdout.isTTY) {
30
- process.stdout.clearLine(0);
31
- process.stdout.cursorTo(0);
32
- process.stdout.write(text);
33
- }
34
- };
35
-
36
- /**
37
- * Write to stdout starting a column.
38
- * @param {string} text - text to write.
39
- * @param {number} column - column.
40
- * @returns {void}
41
- */
42
- export const writeStartingOnColumn = (text, column) => {
43
- if (process.stdout.isTTY) {
44
- process.stdout.cursorTo(column);
45
- process.stdout.clearLine(1);
46
- process.stdout.write(text);
47
- }
48
- };
@@ -1,4 +0,0 @@
1
- export function initMetrics(): metricsType;
2
- export function handlerMetrics(metrics: metricsType): RequestHandler;
3
- export type RequestHandler = import("express").RequestHandler;
4
- export type metricsType = import("./types.js").metricsType;
@@ -1,3 +0,0 @@
1
- export function parsePage(text: string): pageType;
2
- export type pageType = import("./types.js").pageType;
3
- export type cookieType = import("./types.js").cookieType;
package/types/server.d.ts DELETED
@@ -1,14 +0,0 @@
1
- export function showConfig(config: configType): void;
2
- export function createHttpServer(app: Express, port: number, connectionPools: Pool[]): Promise<http.Server>;
3
- export function createHttpsServer(app: Express, sslKeyFilename: string, sslCertFilename: string, port: number, connectionPools: Pool[]): Promise<https.Server>;
4
- export function startHttpServer(config: configType): Promise<void>;
5
- export function loadConfig(filename?: string): configType;
6
- export type Express = import("express").Express;
7
- export type Request = import("express").Request;
8
- export type Response = import("express").Response;
9
- export type NextFunction = import("express").NextFunction;
10
- export type Pool = import("oracledb").Pool;
11
- export type environmentType = import("./types.js").environmentType;
12
- export type configType = import("./types.js").configType;
13
- import http from 'node:http';
14
- import https from 'node:https';
package/types/tty.d.ts DELETED
@@ -1,4 +0,0 @@
1
- export function write(text: string): void;
2
- export function writeNewLine(text?: string): void;
3
- export function writeAfterEraseLine(text: string): void;
4
- export function writeStartingOnColumn(text: string, column: number): void;
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes