web_plsql 0.3.2 → 0.5.0

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 (44) hide show
  1. package/.editorconfig +8 -8
  2. package/.eslintignore +3 -3
  3. package/.eslintrc.js +347 -273
  4. package/CHANGELOG.md +127 -106
  5. package/LICENSE +21 -21
  6. package/README.md +165 -164
  7. package/examples/apex.js +70 -70
  8. package/examples/credentials.js +22 -22
  9. package/examples/oracledb_example.js +30 -30
  10. package/examples/sample.js +101 -84
  11. package/examples/sql/doc_table.sql +13 -13
  12. package/examples/sql/install.sql +31 -31
  13. package/examples/sql/sample.pkb +223 -223
  14. package/examples/sql/sample.pks +24 -24
  15. package/examples/sql/uninstall.sql +5 -5
  16. package/examples/static/sample.css +25 -25
  17. package/jest.config.js +204 -0
  18. package/package.json +85 -109
  19. package/src/cgi.ts +95 -95
  20. package/src/config.ts +97 -97
  21. package/src/errorPage.ts +286 -286
  22. package/src/fileUpload.ts +126 -132
  23. package/src/index.ts +65 -66
  24. package/src/page.ts +275 -277
  25. package/src/procedure.ts +360 -359
  26. package/src/procedureError.ts +27 -27
  27. package/src/request.ts +139 -139
  28. package/src/requestError.ts +19 -19
  29. package/src/stream.ts +26 -26
  30. package/src/trace.ts +194 -193
  31. package/test/.eslintrc.json +5 -5
  32. package/test/{cgi.ts → __tests__/cgi.ts} +96 -95
  33. package/test/{config.ts → __tests__/config.ts} +41 -41
  34. package/test/__tests__/errorPage.ts +101 -0
  35. package/test/{oracledb_mock.ts → __tests__/oracledb_mock.ts} +98 -98
  36. package/test/{server.ts → __tests__/server.ts} +495 -498
  37. package/test/{stream.ts → __tests__/stream.ts} +21 -21
  38. package/test/mock/oracledb.ts +85 -85
  39. package/test/static/static.html +1 -1
  40. package/tsconfig.json +24 -23
  41. package/tsconfig.src.json +23 -22
  42. package/test/errorPage.ts +0 -101
  43. package/test/mocha.opts +0 -7
  44. package/tsconfig.test.json +0 -19
package/src/errorPage.ts CHANGED
@@ -1,286 +1,286 @@
1
- /*
2
- * Error handling
3
- */
4
-
5
- import util from 'util';
6
- import escape from 'escape-html';
7
- import {ProcedureError} from './procedureError';
8
- import {RequestError} from './requestError';
9
- import express from 'express';
10
- import {Trace} from './trace';
11
- import {oracleExpressMiddleware$options} from './config';
12
- import {environmentType} from './cgi';
13
- type outputType = {html: string; text: string};
14
-
15
- /**
16
- * Show an error page
17
- *
18
- * @param {express.Request} req - The req object represents the HTTP request.
19
- * @param {express.Response} res - The res object represents the HTTP response that an Express app sends when it gets an HTTP request.
20
- * @param {Object} options - The configuration options.
21
- * @param {Trace} trace - Tracing object.
22
- * @param {Error} error - The error.
23
- */
24
- export function errorPage(req: express.Request, res: express.Response, options: oracleExpressMiddleware$options, trace: Trace, error: Error): void {
25
- let output = {
26
- html: '',
27
- text: ''
28
- };
29
-
30
- // get the error description
31
- try {
32
- output = getError(req, error);
33
- } catch (e) {
34
- /* istanbul ignore next */
35
- const header = 'ERROR';
36
- /* istanbul ignore next */
37
- const message = `${e.message}\n${e.stack}`;
38
-
39
- /* istanbul ignore next */
40
- output.html += getHeaderHtml(header) + getHtml(message);
41
- /* istanbul ignore next */
42
- output.text += getHeaderHtml(header) + getHtml(message);
43
- }
44
-
45
- // trace to file
46
- trace.write(output.text);
47
-
48
- // console
49
- console.error(output.text);
50
-
51
- // show page
52
- if (options.errorStyle === 'basic') {
53
- res.status(404).send('Page not found');
54
- } else {
55
- res.status(404).send(getHtmlPage(output.html));
56
- }
57
- }
58
-
59
- /*
60
- * Show an error page
61
- */
62
- function getError(req: express.Request, error: Error): outputType {
63
- let timestamp: Date = new Date();
64
- let message: string = '';
65
- let environment: environmentType | null = null;
66
- let sql: string | null = null;
67
- let bind: any | null = null;
68
-
69
- // what type of Error did we receive
70
- if (error instanceof ProcedureError) {
71
- //@ts-ignore
72
- timestamp = error.timestamp;
73
- /* istanbul ignore next */
74
- message = error.stack || '';
75
- //@ts-ignore
76
- environment = error.environment;
77
- //@ts-ignore
78
- sql = error.sql;
79
- //@ts-ignore
80
- bind = error.bind;
81
- } else if (error instanceof RequestError) {
82
- //@ts-ignore
83
- timestamp = error.timestamp;
84
- /* istanbul ignore next */
85
- message = error.stack || '';
86
- } else if (error instanceof Error) {
87
- /* istanbul ignore next */
88
- message = error.stack || '';
89
- } else {
90
- if (typeof error === 'string') {
91
- /* istanbul ignore next */
92
- message = error + '\n';
93
- }
94
- try {
95
- /* istanbul ignore next */
96
- new Error(); // eslint-disable-line no-new
97
- } catch (e) {
98
- /* istanbul ignore next */
99
- message += e.stack;
100
- }
101
- }
102
-
103
- const output: outputType = {
104
- html: '',
105
- text: ''
106
- };
107
-
108
- // timestamp
109
- let header = 'TIMESTAMP';
110
- output.html += getHeaderHtml(header);
111
- output.html += getHtml(timestamp.toUTCString());
112
-
113
- // error
114
- header = 'ERROR';
115
- output.html += getHeaderHtml(header);
116
- output.html += getHtml(message);
117
- output.text += getHeaderText(header);
118
- output.text += getText(message);
119
-
120
- // request
121
- header = 'REQUEST';
122
- output.text += getHeaderText(header);
123
- output.text += getText(Trace.inspectRequest(req));
124
-
125
- // parameters
126
- if (typeof sql === 'string' && bind) {
127
- header = 'PROCEDURE';
128
- output.html += getHeaderHtml(header);
129
- output.text += getHeaderText(header);
130
- getProcedure(output, sql, bind);
131
- }
132
-
133
- // environment
134
- if (environment) {
135
- header = 'ENVIRONMENT';
136
- output.html += getHeaderHtml(header);
137
- output.text += getHeaderText(header);
138
- getEnvironment(output, environment);
139
- }
140
-
141
- return output;
142
- }
143
-
144
- /*
145
- * get procedure
146
- */
147
- function getProcedure(output: outputType, sql: string, bind: any) {
148
- let html = '<table>';
149
- let text = '';
150
-
151
- text += 'PROCEDURE: ' + sql + '\n';
152
- html += `<tr><td>PROCEDURE:</td><td>${sql}</td></tr>`;
153
-
154
- try {
155
- /* istanbul ignore else */
156
- if (sql.indexOf('(:argnames, :argvalues)') < 0) {
157
- for (const key in bind) {
158
- const value = inspect(bind[key].val);
159
-
160
- html += `<tr><td>${key}:</td><td>${value}</td></tr>`;
161
- text += key + ': ' + value + '\n';
162
- }
163
- } else {
164
- bind.argnames.val.forEach((name: string, index: number) => {
165
- const value = inspect(bind.argvalues.val[index]);
166
-
167
- html += `<tr><td>${name}:</td><td>${value}</td></tr>`;
168
- text += name + ': ' + value + '\n';
169
- });
170
- }
171
- } catch (e) {
172
- /* istanbul ignore next */
173
- output.html += e.toString();
174
- /* istanbul ignore next */
175
- output.text += e.toString();
176
- /* istanbul ignore next */
177
- return;
178
- }
179
-
180
- html += '</table>';
181
-
182
- output.html += html;
183
- output.text += text;
184
- }
185
-
186
- /*
187
- * get evnironment
188
- */
189
- function getEnvironment(output: outputType, environment: environmentType) {
190
- let html = '<table>';
191
- let text = '';
192
-
193
- try {
194
- for (const key in environment) {
195
- html += `<tr><td>${key}:</td><td>${environment[key]}</td></tr>`;
196
- text += key + '=' + environment[key] + '\n';
197
- }
198
- } catch (e) {
199
- /* istanbul ignore next */
200
- output.html += e.toString();
201
- /* istanbul ignore next */
202
- output.text += e.toString();
203
- /* istanbul ignore next */
204
- return;
205
- }
206
-
207
- html += '</table>';
208
-
209
- output.html += html;
210
- output.text += text;
211
- }
212
-
213
- /*
214
- * get text header
215
- */
216
- function getHeaderText(text: string): string {
217
- return `\n${text}\n${'='.repeat(text.length)}\n`;
218
- }
219
-
220
- /*
221
- * get html header
222
- */
223
- function getHeaderHtml(text: string): string {
224
- return `<h1>${text}</h1>`;
225
- }
226
-
227
- /*
228
- * get text
229
- */
230
- function getText(text: string): string {
231
- return text + '\n';
232
- }
233
-
234
- /*
235
- * get html
236
- */
237
- function getHtml(text: string): string {
238
- return `<p>${convertToHtml(text)}</p>`;
239
- }
240
-
241
- /*
242
- * convert value to string
243
- */
244
- function inspect(value: any): string {
245
- return util.inspect(value, {showHidden: false, depth: null, colors: false});
246
- }
247
-
248
- /*
249
- * convert LF and/or CR to <br>
250
- */
251
- function convertToHtml(text: string): string {
252
- let html = escape(text);
253
-
254
- html = html.replace(/(?:\r\n|\r|\n)/g, '<br />');
255
- html = html.replace(/\t/g, '&nbsp;&nbsp;&nbsp;');
256
-
257
- return html;
258
- }
259
-
260
- /*
261
- * getHtmlPage
262
- */
263
- function getHtmlPage(body: string): string {
264
- return `<!DOCTYPE html>
265
- <html lang="en">
266
- <head>
267
- <meta charset="utf-8">
268
- <title>web_plsql error page</title>
269
- <style type="text/css">
270
- html {
271
- font-family: monospace, sans-serif;
272
- font-size: 12px;
273
- }
274
- h1 {
275
- font-size: 16px;
276
- padding: 2px;
277
- background-color: #cc0000;
278
- }
279
- </style>
280
- </head>
281
- <body>
282
- ` + body + `
283
- </body>
284
- </html>
285
- `;
286
- }
1
+ /*
2
+ * Error handling
3
+ */
4
+
5
+ import util from 'util';
6
+ import escape from 'escape-html';
7
+ import {ProcedureError} from './procedureError';
8
+ import {RequestError} from './requestError';
9
+ import express from 'express';
10
+ import {Trace} from './trace';
11
+ import {oracleExpressMiddleware$options} from './config';
12
+ import {environmentType} from './cgi';
13
+ type outputType = {html: string; text: string};
14
+
15
+ /**
16
+ * Show an error page
17
+ *
18
+ * @param {express.Request} req - The req object represents the HTTP request.
19
+ * @param {express.Response} res - The res object represents the HTTP response that an Express app sends when it gets an HTTP request.
20
+ * @param {Object} options - The configuration options.
21
+ * @param {Trace} trace - Tracing object.
22
+ * @param {Error} error - The error.
23
+ */
24
+ export function errorPage(req: express.Request, res: express.Response, options: oracleExpressMiddleware$options, trace: Trace, error: unknown): void {
25
+ let output = {
26
+ html: '',
27
+ text: ''
28
+ };
29
+
30
+ // get the error description
31
+ try {
32
+ output = getError(req, error);
33
+ } catch (err) {
34
+ /* istanbul ignore next */
35
+ const header = 'ERROR';
36
+
37
+ /* istanbul ignore next */
38
+ const message = err instanceof Error ? `${err.message}\n${err.stack}` : JSON.stringify(err);
39
+
40
+ /* istanbul ignore next */
41
+ output.html += getHeaderHtml(header) + getHtml(message);
42
+
43
+ /* istanbul ignore next */
44
+ output.text += getHeaderHtml(header) + getHtml(message);
45
+ }
46
+
47
+ // trace to file
48
+ trace.write(output.text);
49
+
50
+ // console
51
+ console.error(output.text);
52
+
53
+ // show page
54
+ if (options.errorStyle === 'basic') {
55
+ res.status(404).send('Page not found');
56
+ } else {
57
+ res.status(404).send(getHtmlPage(output.html));
58
+ }
59
+ }
60
+
61
+ /*
62
+ * Show an error page
63
+ */
64
+ function getError(req: express.Request, error: unknown): outputType {
65
+ let timestamp: Date = new Date();
66
+ let message: string = '';
67
+ let environment: environmentType | null = null;
68
+ let sql: string | null = null;
69
+ let bind: any | null = null;
70
+
71
+ // what type of Error did we receive
72
+ if (error instanceof ProcedureError) {
73
+ timestamp = error.timestamp;
74
+ /* istanbul ignore next */
75
+ message = error.stack || '';
76
+ environment = error.environment;
77
+ sql = error.sql;
78
+ bind = error.bind;
79
+ } else if (error instanceof RequestError) {
80
+ timestamp = error.timestamp;
81
+ /* istanbul ignore next */
82
+ message = error.stack || '';
83
+ } else if (error instanceof Error) {
84
+ /* istanbul ignore next */
85
+ message = error.stack || '';
86
+ } else {
87
+ if (typeof error === 'string') {
88
+ /* istanbul ignore next */
89
+ message = error + '\n';
90
+ }
91
+ try {
92
+ /* istanbul ignore next */
93
+ new Error(); // eslint-disable-line no-new
94
+ } catch (err) {
95
+ /* istanbul ignore next */
96
+ message += err instanceof Error ? err.stack : '';
97
+ }
98
+ }
99
+
100
+ const output: outputType = {
101
+ html: '',
102
+ text: ''
103
+ };
104
+
105
+ // timestamp
106
+ let header = 'TIMESTAMP';
107
+ output.html += getHeaderHtml(header);
108
+ output.html += getHtml(timestamp.toUTCString());
109
+
110
+ // error
111
+ header = 'ERROR';
112
+ output.html += getHeaderHtml(header);
113
+ output.html += getHtml(message);
114
+ output.text += getHeaderText(header);
115
+ output.text += getText(message);
116
+
117
+ // request
118
+ header = 'REQUEST';
119
+ output.text += getHeaderText(header);
120
+ output.text += getText(Trace.inspectRequest(req));
121
+
122
+ // parameters
123
+ if (typeof sql === 'string' && bind) {
124
+ header = 'PROCEDURE';
125
+ output.html += getHeaderHtml(header);
126
+ output.text += getHeaderText(header);
127
+ getProcedure(output, sql, bind);
128
+ }
129
+
130
+ // environment
131
+ if (environment) {
132
+ header = 'ENVIRONMENT';
133
+ output.html += getHeaderHtml(header);
134
+ output.text += getHeaderText(header);
135
+ getEnvironment(output, environment);
136
+ }
137
+
138
+ return output;
139
+ }
140
+
141
+ /*
142
+ * get procedure
143
+ */
144
+ function getProcedure(output: outputType, sql: string, bind: any) {
145
+ let html = '<table>';
146
+ let text = '';
147
+
148
+ text += 'PROCEDURE: ' + sql + '\n';
149
+ html += `<tr><td>PROCEDURE:</td><td>${sql}</td></tr>`;
150
+
151
+ try {
152
+ /* istanbul ignore else */
153
+ if (sql.indexOf('(:argnames, :argvalues)') < 0) {
154
+ for (const key in bind) {
155
+ const value = inspect(bind[key].val);
156
+
157
+ html += `<tr><td>${key}:</td><td>${value}</td></tr>`;
158
+ text += key + ': ' + value + '\n';
159
+ }
160
+ } else {
161
+ bind.argnames.val.forEach((name: string, index: number) => {
162
+ const value = inspect(bind.argvalues.val[index]);
163
+
164
+ html += `<tr><td>${name}:</td><td>${value}</td></tr>`;
165
+ text += name + ': ' + value + '\n';
166
+ });
167
+ }
168
+ } catch (err) {
169
+ /* istanbul ignore next */
170
+ output.html += err instanceof Error ? err.toString() : 'ERROR';
171
+
172
+ /* istanbul ignore next */
173
+ output.text += err instanceof Error ? err.toString() : 'ERROR';
174
+ /* istanbul ignore next */
175
+ return;
176
+ }
177
+
178
+ html += '</table>';
179
+
180
+ output.html += html;
181
+ output.text += text;
182
+ }
183
+
184
+ /*
185
+ * get evnironment
186
+ */
187
+ function getEnvironment(output: outputType, environment: environmentType) {
188
+ let html = '<table>';
189
+ let text = '';
190
+
191
+ try {
192
+ for (const key in environment) {
193
+ html += `<tr><td>${key}:</td><td>${environment[key]}</td></tr>`;
194
+ text += key + '=' + environment[key] + '\n';
195
+ }
196
+ } catch (err) {
197
+ /* istanbul ignore next */
198
+ output.html += err instanceof Error ? err.toString() : 'ERROR';
199
+
200
+ /* istanbul ignore next */
201
+ output.text += err instanceof Error ? err.toString() : 'ERROR';
202
+
203
+ /* istanbul ignore next */
204
+ return;
205
+ }
206
+
207
+ html += '</table>';
208
+
209
+ output.html += html;
210
+ output.text += text;
211
+ }
212
+
213
+ /*
214
+ * get text header
215
+ */
216
+ function getHeaderText(text: string): string {
217
+ return `\n${text}\n${'='.repeat(text.length)}\n`;
218
+ }
219
+
220
+ /*
221
+ * get html header
222
+ */
223
+ function getHeaderHtml(text: string): string {
224
+ return `<h1>${text}</h1>`;
225
+ }
226
+
227
+ /*
228
+ * get text
229
+ */
230
+ function getText(text: string): string {
231
+ return text + '\n';
232
+ }
233
+
234
+ /*
235
+ * get html
236
+ */
237
+ function getHtml(text: string): string {
238
+ return `<p>${convertToHtml(text)}</p>`;
239
+ }
240
+
241
+ /*
242
+ * convert value to string
243
+ */
244
+ function inspect(value: any): string {
245
+ return util.inspect(value, {showHidden: false, depth: null, colors: false});
246
+ }
247
+
248
+ /*
249
+ * convert LF and/or CR to <br>
250
+ */
251
+ function convertToHtml(text: string): string {
252
+ let html = escape(text);
253
+
254
+ html = html.replace(/(?:\r\n|\r|\n)/g, '<br />');
255
+ html = html.replace(/\t/g, '&nbsp;&nbsp;&nbsp;');
256
+
257
+ return html;
258
+ }
259
+
260
+ /*
261
+ * getHtmlPage
262
+ */
263
+ function getHtmlPage(body: string): string {
264
+ return `<!DOCTYPE html>
265
+ <html lang="en">
266
+ <head>
267
+ <meta charset="utf-8">
268
+ <title>web_plsql error page</title>
269
+ <style type="text/css">
270
+ html {
271
+ font-family: monospace, sans-serif;
272
+ font-size: 12px;
273
+ }
274
+ h1 {
275
+ font-size: 16px;
276
+ padding: 2px;
277
+ background-color: #cc0000;
278
+ }
279
+ </style>
280
+ </head>
281
+ <body>
282
+ ` + body + `
283
+ </body>
284
+ </html>
285
+ `;
286
+ }