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/trace.ts CHANGED
@@ -1,193 +1,194 @@
1
- /*
2
- * Trace utilities
3
- */
4
-
5
- import fs from 'fs';
6
- import path from 'path';
7
- import mkdirp from 'mkdirp';
8
- import util from 'util';
9
- import express from 'express';
10
-
11
- const TRACE_ROOT_DIRECTORY = 'trace';
12
- const SEPARATOR_LINE = '*'.repeat(132);
13
-
14
- export class Trace {
15
- _enabled: boolean;
16
- _directory: string;
17
- _filename: string;
18
- _id: number;
19
-
20
- /**
21
- * Instantiate a new trace object.
22
- *
23
- * @param {'on' | 'off' | 'test'} trace - Tracing.
24
- */
25
- constructor(trace: 'on' | 'off' | 'test') {
26
- this._enabled = trace !== 'off';
27
- this._directory = getTimestampDirectory();
28
- this._filename = '';
29
- this._id = 0;
30
-
31
- // istanbul ignore if
32
- if (!this._enabled) {
33
- return;
34
- }
35
-
36
- // create the trace directory
37
- try {
38
- mkdirp.sync(this._directory);
39
- if (trace === 'on') {
40
- // istanbul ignore next
41
- console.log(`Tracing to the directory "${this._directory}" is enabled.`);
42
- }
43
- } catch (e) {
44
- // istanbul ignore next
45
- console.error(`Unable to create new trace directory "${this._directory}"`, e);
46
- }
47
- }
48
-
49
- /**
50
- * Start a new trace session for a new request.
51
- * This adds a trace line to the trace.log file and creating a new request trace file.
52
- *
53
- * @param {express.Request} req - The req object represents the HTTP request.
54
- */
55
- start(req: express.Request) {
56
- // istanbul ignore if
57
- if (!this._enabled) {
58
- return;
59
- }
60
-
61
- interface TCustomRequest extends express.Request {
62
- uniqueRequestID: number;
63
- }
64
- const customRequest = req as TCustomRequest;
65
-
66
- // Create the next unique request id
67
- customRequest.uniqueRequestID = ++this._id;
68
-
69
- // append to the trace index
70
- appendSync(path.join(this._directory, 'trace.log'), `${customRequest.uniqueRequestID.toString().padEnd(10)} - ${getTimestamp()} - ${customRequest.originalUrl}\n`);
71
-
72
- // compute the trace filename
73
- this._filename = path.join(this._directory, customRequest.uniqueRequestID.toString() + '.log');
74
-
75
- // write the initial request information
76
- this.append(SEPARATOR_LINE +
77
- getSection('TIMESTAMP', getTimestamp()) +
78
- getSection('REQUEST ID', customRequest.uniqueRequestID.toString()) +
79
- getSection('REQUEST', Trace.inspectRequest(req)) +
80
- SEPARATOR_LINE +
81
- '\n');
82
- }
83
-
84
- /**
85
- * Write new message to the trace file.
86
- *
87
- * @param {string} text - Text to append.
88
- */
89
- write(text: string): void {
90
- // istanbul ignore else
91
- if (this._enabled) {
92
- this.append(`${getTimestamp()}:\n${trimRight(text)}\n${SEPARATOR_LINE}\n`);
93
- }
94
- }
95
-
96
- /**
97
- * Append text to the trace file.
98
- *
99
- * @param {string} text - Text to append.
100
- */
101
- append(text: string): void {
102
- // istanbul ignore else
103
- if (this._enabled) {
104
- appendSync(this._filename, text);
105
- }
106
- }
107
-
108
- /**
109
- * Return a string representation of the value.
110
- *
111
- * @param {*} value - Any value.
112
- * @returns {string} - The string representation.
113
- */
114
- static inspect(value: any): string {
115
- return util.inspect(value, {showHidden: false, depth: null, colors: false});
116
- }
117
-
118
- /**
119
- * Return a string representation of the request.
120
- *
121
- * @param {any} req - express.Request.
122
- * @param {boolean} simple - Set to false to see all public properties of the request.
123
- * @returns {string} - The string representation.
124
- */
125
- static inspectRequest(req: any, simple: boolean = true): string {
126
- const simpleRequest: any = {};
127
-
128
- // istanbul ignore else
129
- if (simple) {
130
- ['originalUrl', 'params', 'query', 'url', 'method', 'body', 'files', 'secret', 'cookies'].forEach(key => {
131
- if (req[key]) {
132
- simpleRequest[key] = req[key];
133
- }
134
- });
135
- } else {
136
- Object.keys(req).filter(key => typeof key === 'string' && key.length > 1 && key[0] !== '_').forEach(key => {
137
- simpleRequest[key] = req[key];
138
- });
139
- }
140
-
141
- return util.inspect(simpleRequest, {showHidden: false, depth: null, colors: false});
142
- }
143
- }
144
-
145
- /**
146
- * Append text to the trace file.
147
- *
148
- * @param {string} filename - Trace file name.
149
- * @param {string} text - Text to append.
150
- */
151
- function appendSync(filename: string, text: string): void {
152
- try {
153
- fs.appendFileSync(filename, text);
154
- } catch (e) {
155
- // istanbul ignore next
156
- console.error(`Unable to write to trace file "${filename}"`, e, text);
157
- }
158
- }
159
-
160
- /*
161
- * get a section of a trace message
162
- */
163
- function getSection(section: string, text: string): string {
164
- return `\n${section}\n${'='.repeat(section.length)}\n${text}\n`;
165
- }
166
-
167
- /*
168
- * get a timestamp
169
- */
170
- function getTimestamp(): string {
171
- return new Date().toISOString();
172
- }
173
-
174
- /*
175
- * get a new directory name based on the current timestamp
176
- */
177
- function getTimestampDirectory(): string {
178
- return path.join(TRACE_ROOT_DIRECTORY, getTimestamp().replace(/(-|:)/g, ''));
179
- }
180
-
181
- /*
182
- * trim any cr/lf at the end of the string
183
- */
184
- function trimRight(text: string): string {
185
- let s = text;
186
-
187
- while (s[s.length] === '\n' || s[s.length] === '\r') {
188
- // istanbul ignore next
189
- s = s.slice(0, -1);
190
- }
191
-
192
- return s;
193
- }
1
+ /*
2
+ * Trace utilities
3
+ */
4
+
5
+ import fs from 'fs';
6
+ import path from 'path';
7
+ import mkdirp from 'mkdirp';
8
+ import util from 'util';
9
+ import express from 'express';
10
+
11
+ const TRACE_ROOT_DIRECTORY = 'trace';
12
+ const SEPARATOR_LINE = '*'.repeat(132);
13
+
14
+ export class Trace {
15
+ _enabled: boolean;
16
+ _directory: string;
17
+ _filename: string;
18
+ _id: number;
19
+
20
+ /**
21
+ * Instantiate a new trace object.
22
+ *
23
+ * @param {'on' | 'off' | 'test'} trace - Tracing.
24
+ */
25
+ constructor(trace: 'on' | 'off' | 'test') {
26
+ this._enabled = trace !== 'off';
27
+ this._directory = getTimestampDirectory();
28
+ this._filename = '';
29
+ this._id = 0;
30
+
31
+ // istanbul ignore if
32
+ if (!this._enabled) {
33
+ return;
34
+ }
35
+
36
+ // create the trace directory
37
+ try {
38
+ mkdirp.sync(this._directory);
39
+ if (trace === 'on') {
40
+ // istanbul ignore next
41
+ console.log(`Tracing to the directory "${this._directory}" is enabled.`);
42
+ }
43
+ } catch (e) {
44
+ // istanbul ignore next
45
+ console.error(`Unable to create new trace directory "${this._directory}"`, e);
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Start a new trace session for a new request.
51
+ * This adds a trace line to the trace.log file and creating a new request trace file.
52
+ *
53
+ * @param {express.Request} req - The req object represents the HTTP request.
54
+ */
55
+ start(req: express.Request): void {
56
+ // istanbul ignore if
57
+ if (!this._enabled) {
58
+ return;
59
+ }
60
+
61
+ interface TCustomRequest extends express.Request {
62
+ uniqueRequestID: number;
63
+ }
64
+ const customRequest = req as TCustomRequest;
65
+
66
+ // Create the next unique request id
67
+ customRequest.uniqueRequestID = ++this._id;
68
+
69
+ // append to the trace index
70
+ appendSync(path.join(this._directory, 'trace.log'), `${customRequest.uniqueRequestID.toString().padEnd(10)} - ${getTimestamp()} - ${customRequest.originalUrl}\n`);
71
+
72
+ // compute the trace filename
73
+ this._filename = path.join(this._directory, customRequest.uniqueRequestID.toString() + '.log');
74
+
75
+ // write the initial request information
76
+ this.append(SEPARATOR_LINE +
77
+ getSection('TIMESTAMP', getTimestamp()) +
78
+ getSection('REQUEST ID', customRequest.uniqueRequestID.toString()) +
79
+ getSection('REQUEST', Trace.inspectRequest(req)) +
80
+ SEPARATOR_LINE +
81
+ '\n');
82
+ }
83
+
84
+ /**
85
+ * Write new message to the trace file.
86
+ *
87
+ * @param {string} text - Text to append.
88
+ */
89
+ write(text: string): void {
90
+ // istanbul ignore else
91
+ if (this._enabled) {
92
+ this.append(`${getTimestamp()}:\n${trimRight(text)}\n${SEPARATOR_LINE}\n`);
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Append text to the trace file.
98
+ *
99
+ * @param {string} text - Text to append.
100
+ */
101
+ append(text: string): void {
102
+ // istanbul ignore else
103
+ if (this._enabled) {
104
+ appendSync(this._filename, text);
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Return a string representation of the value.
110
+ *
111
+ * @param {unknown} value - Any value.
112
+ * @returns {string} - The string representation.
113
+ */
114
+ static inspect(value: unknown): string {
115
+ return util.inspect(value, {showHidden: false, depth: null, colors: false});
116
+ }
117
+
118
+ /**
119
+ * Return a string representation of the request.
120
+ *
121
+ * @param {express.Request} req - express.Request.
122
+ * @param {boolean} simple - Set to false to see all public properties of the request.
123
+ * @returns {string} - The string representation.
124
+ */
125
+ static inspectRequest(req: express.Request, simple: boolean = true): string {
126
+ type RequestKeysType = keyof express.Request;
127
+ const simpleRequest: any = {};
128
+
129
+ // istanbul ignore else
130
+ if (simple) {
131
+ ['originalUrl', 'params', 'query', 'url', 'method', 'body', 'files', 'secret', 'cookies'].forEach(key => {
132
+ if (req[key as RequestKeysType]) {
133
+ simpleRequest[key] = req[key as RequestKeysType];
134
+ }
135
+ });
136
+ } else {
137
+ Object.keys(req).filter(key => typeof key === 'string' && key.length > 1 && key[0] !== '_').forEach(key => {
138
+ simpleRequest[key] = req[key as RequestKeysType];
139
+ });
140
+ }
141
+
142
+ return util.inspect(simpleRequest, {showHidden: false, depth: null, colors: false});
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Append text to the trace file.
148
+ *
149
+ * @param {string} filename - Trace file name.
150
+ * @param {string} text - Text to append.
151
+ */
152
+ function appendSync(filename: string, text: string): void {
153
+ try {
154
+ fs.appendFileSync(filename, text);
155
+ } catch (e) {
156
+ // istanbul ignore next
157
+ console.error(`Unable to write to trace file "${filename}"`, e, text);
158
+ }
159
+ }
160
+
161
+ /*
162
+ * get a section of a trace message
163
+ */
164
+ function getSection(section: string, text: string): string {
165
+ return `\n${section}\n${'='.repeat(section.length)}\n${text}\n`;
166
+ }
167
+
168
+ /*
169
+ * get a timestamp
170
+ */
171
+ function getTimestamp(): string {
172
+ return new Date().toISOString();
173
+ }
174
+
175
+ /*
176
+ * get a new directory name based on the current timestamp
177
+ */
178
+ function getTimestampDirectory(): string {
179
+ return path.join(TRACE_ROOT_DIRECTORY, getTimestamp().replace(/(-|:)/g, ''));
180
+ }
181
+
182
+ /*
183
+ * trim any cr/lf at the end of the string
184
+ */
185
+ function trimRight(text: string): string {
186
+ let s = text;
187
+
188
+ while (s[s.length] === '\n' || s[s.length] === '\r') {
189
+ // istanbul ignore next
190
+ s = s.slice(0, -1);
191
+ }
192
+
193
+ return s;
194
+ }
@@ -1,5 +1,5 @@
1
- {
2
- "env": {
3
- "mocha": true
4
- }
5
- }
1
+ {
2
+ "env": {
3
+ "mocha": true
4
+ }
5
+ }
@@ -1,95 +1,96 @@
1
- import {assert} from 'chai';
2
- import {getCGI} from '../src/cgi';
3
- import os from 'os';
4
-
5
- describe('cgi', () => {
6
- it('with a proper configuration object and request', () => {
7
- const PORT = 4711;
8
- const ORIGINAL_URL = '/pls/base/package.procedure?p=1#1';
9
- const DOCUMENT_TABLE_NAME = 'doc-table';
10
- const REMOTE_ADDRESS = '127.0.0.1';
11
-
12
- const req = {
13
- protocol: 'http',
14
- originalUrl: ORIGINAL_URL,
15
- method: 'GET',
16
- params: {
17
- name: 'index.html'
18
- },
19
- httpVersion: '1.1',
20
- ip: REMOTE_ADDRESS,
21
- get(name: string) {
22
- switch (name.toLowerCase()) {
23
- case 'port':
24
- return PORT.toString();
25
- case 'host':
26
- return 'HOST';
27
- case 'user-agent':
28
- return 'USER-AGENT';
29
- case 'accept':
30
- return 'ACCEPT';
31
- case 'accept-encoding':
32
- return 'ACCEPT-ENCODING';
33
- case 'accept-language':
34
- return 'ACCEPT-LANGUAGE';
35
- case 'referer':
36
- case 'referrer':
37
- return 'HTTP-REFERER';
38
- default:
39
- return null;
40
- }
41
- },
42
- cookies: {
43
- cookie1: 'value1',
44
- cookie2: 'value2'
45
- },
46
- connection: {
47
- remoteAddress: REMOTE_ADDRESS
48
- },
49
- socket: {
50
- localPort: PORT
51
- }
52
- };
53
-
54
- const options = {
55
- doctable: DOCUMENT_TABLE_NAME
56
- };
57
-
58
- //@ts-ignore
59
- const cgi = getCGI(req, options);
60
-
61
- assert.strictEqual(Object.keys(cgi).length, 29);
62
-
63
- assert.deepEqual(cgi, {
64
- 'PLSQL_GATEWAY': 'WebDb',
65
- 'GATEWAY_IVERSION': '2',
66
- 'SERVER_SOFTWARE': 'web_plsql',
67
- 'GATEWAY_INTERFACE': 'CGI/1.1',
68
- 'SERVER_PORT': PORT.toString(),
69
- 'SERVER_NAME': os.hostname(),
70
- 'REQUEST_METHOD': 'GET',
71
- 'PATH_INFO': 'index.html',
72
- 'SCRIPT_NAME': '/pls/base',
73
- 'REMOTE_ADDR': REMOTE_ADDRESS,
74
- 'SERVER_PROTOCOL': 'HTTP/1.1',
75
- 'REQUEST_PROTOCOL': 'HTTP',
76
- 'REMOTE_USER': '',
77
- 'HTTP_USER_AGENT': 'USER-AGENT',
78
- 'HTTP_X_FORWARDED_FOR': '',
79
- 'HTTP_HOST': 'HOST',
80
- 'HTTP_ACCEPT': 'ACCEPT',
81
- 'HTTP_ACCEPT_ENCODING': 'ACCEPT-ENCODING',
82
- 'HTTP_ACCEPT_LANGUAGE': 'ACCEPT-LANGUAGE',
83
- 'HTTP_REFERER': 'HTTP-REFERER',
84
- 'WEB_AUTHENT_PREFIX': '',
85
- 'DAD_NAME': 'base',
86
- 'DOC_ACCESS_PATH': 'doc',
87
- 'DOCUMENT_TABLE': DOCUMENT_TABLE_NAME,
88
- 'PATH_ALIAS': '',
89
- 'REQUEST_CHARSET': 'UTF8',
90
- 'REQUEST_IANA_CHARSET': 'UTF-8',
91
- 'SCRIPT_PREFIX': '/pls',
92
- 'HTTP_COOKIE': 'cookie1=value1;cookie2=value2;'
93
- });
94
- });
95
- });
1
+ import {describe, it, expect} from '@jest/globals';
2
+ import {getCGI} from '../../src/cgi';
3
+ import express from 'express';
4
+ import os from 'os';
5
+ import type {oracleExpressMiddleware$options} from '../../src/config';
6
+
7
+ describe('cgi', () => {
8
+ it('with a proper configuration object and request', () => {
9
+ const PORT = 4711;
10
+ const ORIGINAL_URL = '/pls/base/package.procedure?p=1#1';
11
+ const DOCUMENT_TABLE_NAME = 'doc-table';
12
+ const REMOTE_ADDRESS = '127.0.0.1';
13
+
14
+ const req = {
15
+ protocol: 'http',
16
+ originalUrl: ORIGINAL_URL,
17
+ method: 'GET',
18
+ params: {
19
+ name: 'index.html'
20
+ },
21
+ httpVersion: '1.1',
22
+ ip: REMOTE_ADDRESS,
23
+ get(name: string) {
24
+ switch (name.toLowerCase()) {
25
+ case 'port':
26
+ return PORT.toString();
27
+ case 'host':
28
+ return 'HOST';
29
+ case 'user-agent':
30
+ return 'USER-AGENT';
31
+ case 'accept':
32
+ return 'ACCEPT';
33
+ case 'accept-encoding':
34
+ return 'ACCEPT-ENCODING';
35
+ case 'accept-language':
36
+ return 'ACCEPT-LANGUAGE';
37
+ case 'referer':
38
+ case 'referrer':
39
+ return 'HTTP-REFERER';
40
+ default:
41
+ return null;
42
+ }
43
+ },
44
+ cookies: {
45
+ cookie1: 'value1',
46
+ cookie2: 'value2'
47
+ },
48
+ connection: {
49
+ remoteAddress: REMOTE_ADDRESS
50
+ },
51
+ socket: {
52
+ localPort: PORT
53
+ }
54
+ };
55
+
56
+ const options = {
57
+ doctable: DOCUMENT_TABLE_NAME
58
+ };
59
+
60
+ const cgi = getCGI(req as unknown as express.Request, options as unknown as oracleExpressMiddleware$options);
61
+
62
+ expect(Object.keys(cgi)).toHaveLength(29);
63
+
64
+ expect(cgi).toStrictEqual({
65
+ 'PLSQL_GATEWAY': 'WebDb',
66
+ 'GATEWAY_IVERSION': '2',
67
+ 'SERVER_SOFTWARE': 'web_plsql',
68
+ 'GATEWAY_INTERFACE': 'CGI/1.1',
69
+ 'SERVER_PORT': PORT.toString(),
70
+ 'SERVER_NAME': os.hostname(),
71
+ 'REQUEST_METHOD': 'GET',
72
+ 'PATH_INFO': 'index.html',
73
+ 'SCRIPT_NAME': '/pls/base',
74
+ 'REMOTE_ADDR': REMOTE_ADDRESS,
75
+ 'SERVER_PROTOCOL': 'HTTP/1.1',
76
+ 'REQUEST_PROTOCOL': 'HTTP',
77
+ 'REMOTE_USER': '',
78
+ 'HTTP_USER_AGENT': 'USER-AGENT',
79
+ 'HTTP_X_FORWARDED_FOR': '',
80
+ 'HTTP_HOST': 'HOST',
81
+ 'HTTP_ACCEPT': 'ACCEPT',
82
+ 'HTTP_ACCEPT_ENCODING': 'ACCEPT-ENCODING',
83
+ 'HTTP_ACCEPT_LANGUAGE': 'ACCEPT-LANGUAGE',
84
+ 'HTTP_REFERER': 'HTTP-REFERER',
85
+ 'WEB_AUTHENT_PREFIX': '',
86
+ 'DAD_NAME': 'base',
87
+ 'DOC_ACCESS_PATH': 'doc',
88
+ 'DOCUMENT_TABLE': DOCUMENT_TABLE_NAME,
89
+ 'PATH_ALIAS': '',
90
+ 'REQUEST_CHARSET': 'UTF8',
91
+ 'REQUEST_IANA_CHARSET': 'UTF-8',
92
+ 'SCRIPT_PREFIX': '/pls',
93
+ 'HTTP_COOKIE': 'cookie1=value1;cookie2=value2;'
94
+ });
95
+ });
96
+ });