@oino-ts/common 0.17.1 → 0.17.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.
@@ -42,7 +42,7 @@ class OINOHttpRequest extends OINORequest {
42
42
  url;
43
43
  method;
44
44
  headers;
45
- body;
45
+ data;
46
46
  requestType;
47
47
  responseType;
48
48
  multipartBoundary;
@@ -59,7 +59,7 @@ class OINOHttpRequest extends OINORequest {
59
59
  this.url = init.url;
60
60
  this.method = init.method ?? "GET";
61
61
  this.headers = init.headers ?? {};
62
- this.body = init.body ?? "";
62
+ this.data = init.data ?? "";
63
63
  this.multipartBoundary = "";
64
64
  this.lastModified = init.lastModified;
65
65
  if (init.multipartBoundary) {
@@ -110,5 +110,14 @@ class OINOHttpRequest extends OINORequest {
110
110
  this.etags = etags;
111
111
  }
112
112
  }
113
+ static async fromRequest(request) {
114
+ const body = await request.arrayBuffer();
115
+ return new OINOHttpRequest({
116
+ url: new URL(request.url),
117
+ method: request.method,
118
+ headers: Object.fromEntries(request.headers),
119
+ data: Buffer.from(body),
120
+ });
121
+ }
113
122
  }
114
123
  exports.OINOHttpRequest = OINOHttpRequest;
@@ -38,7 +38,7 @@ export class OINOHttpRequest extends OINORequest {
38
38
  url;
39
39
  method;
40
40
  headers;
41
- body;
41
+ data;
42
42
  requestType;
43
43
  responseType;
44
44
  multipartBoundary;
@@ -55,7 +55,7 @@ export class OINOHttpRequest extends OINORequest {
55
55
  this.url = init.url;
56
56
  this.method = init.method ?? "GET";
57
57
  this.headers = init.headers ?? {};
58
- this.body = init.body ?? "";
58
+ this.data = init.data ?? "";
59
59
  this.multipartBoundary = "";
60
60
  this.lastModified = init.lastModified;
61
61
  if (init.multipartBoundary) {
@@ -106,4 +106,13 @@ export class OINOHttpRequest extends OINORequest {
106
106
  this.etags = etags;
107
107
  }
108
108
  }
109
+ static async fromRequest(request) {
110
+ const body = await request.arrayBuffer();
111
+ return new OINOHttpRequest({
112
+ url: new URL(request.url),
113
+ method: request.method,
114
+ headers: Object.fromEntries(request.headers),
115
+ data: Buffer.from(body),
116
+ });
117
+ }
109
118
  }
@@ -28,7 +28,7 @@ export interface OINOHttpRequestInit extends OINORequestInit {
28
28
  url?: URL;
29
29
  method?: string;
30
30
  headers?: Record<string, string>;
31
- body?: string;
31
+ data?: string | Buffer | Uint8Array | object | null;
32
32
  requestType?: OINOContentType;
33
33
  responseType?: OINOContentType;
34
34
  multipartBoundary?: string;
@@ -41,7 +41,7 @@ export declare class OINOHttpRequest extends OINORequest {
41
41
  readonly url?: URL;
42
42
  readonly method: string;
43
43
  readonly headers: Record<string, string>;
44
- readonly body: string;
44
+ readonly data: string | Buffer | Uint8Array | object | null;
45
45
  readonly requestType: OINOContentType;
46
46
  readonly responseType: OINOContentType;
47
47
  readonly multipartBoundary?: string;
@@ -54,4 +54,5 @@ export declare class OINOHttpRequest extends OINORequest {
54
54
  *
55
55
  */
56
56
  constructor(init: OINOHttpRequestInit);
57
+ static fromRequest(request: Request): Promise<OINOHttpRequest>;
57
58
  }
package/package.json CHANGED
@@ -1,32 +1,32 @@
1
- {
2
- "name": "@oino-ts/common",
3
- "version": "0.17.1",
4
- "description": "OINO TS package for common classes.",
5
- "author": "Matias Kiviniemi (pragmatta)",
6
- "license": "MPL-2.0",
7
- "repository": {
8
- "type": "git",
9
- "url": "https://github.com/pragmatta/oino-ts.git"
10
- },
11
- "keywords": [
12
- "types",
13
- "typescript",
14
- "library"
15
- ],
16
- "main": "./dist/cjs/index.js",
17
- "module": "./dist/esm/index.js",
18
- "types": "./dist/types/index.d.ts",
19
- "dependencies": {
20
- },
21
- "devDependencies": {
22
- "@oino-ts/types": "0.17.1",
23
- "@types/node": "^22.0.0",
24
- "typescript": "~5.9.0"
25
- },
26
- "files": [
27
- "src/*.ts",
28
- "dist/cjs/*.js",
29
- "dist/esm/*.js",
30
- "dist/types/*.d.ts"
31
- ]
32
- }
1
+ {
2
+ "name": "@oino-ts/common",
3
+ "version": "0.17.2",
4
+ "description": "OINO TS package for common classes.",
5
+ "author": "Matias Kiviniemi (pragmatta)",
6
+ "license": "MPL-2.0",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/pragmatta/oino-ts.git"
10
+ },
11
+ "keywords": [
12
+ "types",
13
+ "typescript",
14
+ "library"
15
+ ],
16
+ "main": "./dist/cjs/index.js",
17
+ "module": "./dist/esm/index.js",
18
+ "types": "./dist/types/index.d.ts",
19
+ "dependencies": {
20
+ },
21
+ "devDependencies": {
22
+ "@oino-ts/types": "0.17.2",
23
+ "@types/node": "^22.0.0",
24
+ "typescript": "~5.9.0"
25
+ },
26
+ "files": [
27
+ "src/*.ts",
28
+ "dist/cjs/*.js",
29
+ "dist/esm/*.js",
30
+ "dist/types/*.d.ts"
31
+ ]
32
+ }
@@ -1,250 +1,250 @@
1
- /*
2
- * This Source Code Form is subject to the terms of the Mozilla Public
3
- * License, v. 2.0. If a copy of the MPL was not distributed with this
4
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
- */
6
-
7
- /**
8
- * Static class for benchmarking functions.
9
- *
10
- */
11
- export abstract class OINOBenchmark {
12
-
13
- protected static _instance:OINOBenchmark
14
- protected static _enabled:Record<string, boolean> = {}
15
-
16
- /**
17
- * Create a new OINOBenchmark instance.
18
- *
19
- * @param enabledModules array of those benchmarks that are enabled
20
- */
21
- constructor(enabledModules:string[] = []) {
22
- OINOBenchmark.setEnabled(enabledModules)
23
- }
24
-
25
- /**
26
- * Set active benchmarking instance.
27
- *
28
- * @param instance OINOBenchmark instance
29
- *
30
- */
31
- static setInstance(instance: OINOBenchmark) {
32
- if (instance) {
33
- OINOBenchmark._instance = instance
34
- }
35
- }
36
-
37
- /**
38
- * Get active benchmarking instance.
39
- *
40
- */
41
- static getInstance(): OINOBenchmark {
42
- return OINOBenchmark._instance
43
- }
44
-
45
- protected abstract _reset():void
46
- /**
47
- * Reset benchmark data (but not what is enabled).
48
- *
49
- */
50
- static reset():void {
51
- OINOBenchmark._instance?._reset()
52
- }
53
-
54
- /**
55
- * Set benchmark names that are enabled.
56
- *
57
- * @param modules array of those benchmarks that are enabled
58
- */
59
- static setEnabled(modules:string[]):void {
60
- OINOBenchmark._enabled = {}
61
- modules.forEach(module_name => {
62
- this._enabled[module_name] = true
63
- });
64
- }
65
-
66
- protected abstract _startMetric(module:string, method:string):void
67
- /**
68
- * Start benchmark timing.
69
- *
70
- * @param module of the benchmark
71
- * @param method of the benchmark
72
- */
73
- static startMetric(module:string, method:string):void {
74
- OINOBenchmark._instance?._startMetric(module, method)
75
- }
76
-
77
- protected abstract _endMetric(module:string, method:string):void
78
- /**
79
- * Complete benchmark timing
80
- *
81
- * @param module of the benchmark
82
- * @param method of the benchmark
83
- */
84
- static endMetric(module:string, method:string):void {
85
- OINOBenchmark._instance?._endMetric(module, method)
86
- }
87
-
88
- protected abstract _getMetric(module:string, method:string):number
89
- /**
90
- * Get given benchmark data.
91
- *
92
- * @param module of the benchmark
93
- * @param method of the benchmark
94
- *
95
- */
96
- static getMetric(module:string, method:string):number {
97
- return OINOBenchmark._instance?._getMetric(module, method)
98
- }
99
-
100
- protected abstract _getMetrics():Record<string, number>
101
- /**
102
- * Get all benchmark data.
103
- *
104
- */
105
- static getMetrics():Record<string, number> {
106
- return OINOBenchmark._instance?._getMetrics()
107
- }
108
-
109
- protected abstract _trackMetric(module:string, method:string, value:number):void
110
- /**
111
- * Track a metric value
112
- *
113
- * @param module of the metric
114
- * @param method of the metric
115
- * @param value of the metric
116
- *
117
- */
118
- static trackMetric(module:string, method:string, value:number):void {
119
- if (OINOBenchmark._enabled[module]) {
120
- OINOBenchmark._instance?._trackMetric(module, method, value)
121
- }
122
- }
123
-
124
- protected abstract _trackException(module:string, method:string, name:string, message:string, stack: string):void
125
- /**
126
- * Track an exception
127
- *
128
- * @param module of the benchmark
129
- * @param method of the benchmark
130
- * @param name of the exception
131
- * @param message of the exception
132
- * @param stack trace of the exception
133
- */
134
- static trackException(module:string, method:string, name:string, message:string, stack:string):void {
135
- if (OINOBenchmark._enabled[module]) {
136
- OINOBenchmark._instance?._trackException(module, method, name, message, stack)
137
- }
138
- }
139
-
140
- protected abstract _getExceptions():any[]
141
- /**
142
- * Get all tracked exceptions.
143
- *
144
- */
145
- static getExceptions():any[] {
146
- return OINOBenchmark._instance?._getExceptions()
147
- }
148
- }
149
-
150
- /**
151
- * OINOMemoryBenchmark is a memory-based benchmark implementation.
152
- * It stores the benchmark data in memory and allows to reset, start, end and get benchmark data.
153
- * In case of recursively/iteratively starting a benchmark, it will honor the first start and ignore the rest. *
154
- */
155
- export class OINOMemoryBenchmark extends OINOBenchmark {
156
-
157
- protected _benchmarkCount:Record<string, number> = {}
158
- protected _benchmarkData:Record<string, number> = {}
159
- protected _benchmarkStart:Record<string, number> = {}
160
-
161
- protected _exceptions:any[] = []
162
-
163
- /**
164
- * Reset benchmark data (but not what is enabled).
165
- *
166
- */
167
- protected _reset():void {
168
- this._benchmarkData = {}
169
- this._benchmarkCount = {}
170
- }
171
-
172
- /**
173
- * Start benchmark timing.
174
- *
175
- * @param module of the benchmark
176
- * @param method of the benchmark
177
- */
178
- protected _startMetric(module:string, method:string):void {
179
- const name:string = module + "." + method
180
- if (OINOBenchmark._enabled[module] && ((this._benchmarkStart[name] === undefined) || (this._benchmarkStart[name] === 0))) { // if benchmark is already started (e.g. loop/recursion), do not start it again
181
- this._benchmarkStart[name] = performance.now()
182
- }
183
- }
184
-
185
- /**
186
- * Complete benchmark timing
187
- *
188
- * @param module of the benchmark
189
- * @param method of the benchmark
190
- */
191
- protected _endMetric(module:string, method:string):void {
192
- const name:string = module + "." + method
193
- let result:number = 0
194
- if (OINOBenchmark._enabled[module] && (this._benchmarkStart[name] > 0)) { // if benchmark is started, end it
195
- const duration = performance.now() - this._benchmarkStart[name]
196
- this._benchmarkStart[name] = 0
197
- this._trackMetric(module, method, duration)
198
- }
199
- return
200
- }
201
-
202
- /**
203
- * Get given benchmark data.
204
- *
205
- * @param module of the benchmark
206
- * @param method of the benchmark
207
- *
208
- */
209
- protected _getMetric(module:string, method:string):number {
210
- const name:string = module + "." + method
211
- if (OINOBenchmark._enabled[module] && (this._benchmarkCount[name] > 0)) {
212
- return this._benchmarkData[module] / this._benchmarkCount[module]
213
- }
214
- return -1
215
- }
216
-
217
- /**
218
- * Get all benchmark data.
219
- *
220
- */
221
- protected _getMetrics():Record<string, number> {
222
- let result:Record<string, number> = {}
223
- for (const name in this._benchmarkData) {
224
- if (this._benchmarkCount[name] > 0) {
225
- result[name] = this._benchmarkData[name] / this._benchmarkCount[name]
226
- }
227
- }
228
- return result
229
- }
230
-
231
- protected _trackMetric(module:string, method:string, value:number):void {
232
- const name:string = module + "." + method
233
- if (this._benchmarkCount[name] == undefined) {
234
- this._benchmarkCount[name] = 1
235
- this._benchmarkData[name] = value
236
- } else {
237
- this._benchmarkCount[name] += 1
238
- this._benchmarkData[name] += value
239
- }
240
- }
241
-
242
- protected _trackException(module:string, method:string, name:string, message:string, stack:string):void {
243
- const exception = { module, method, name, message, stack, timestamp: Date.now() }
244
- this._exceptions.push(exception)
245
- }
246
-
247
- protected _getExceptions():any[] {
248
- return this._exceptions
249
- }
250
- }
1
+ /*
2
+ * This Source Code Form is subject to the terms of the Mozilla Public
3
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
4
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
+ */
6
+
7
+ /**
8
+ * Static class for benchmarking functions.
9
+ *
10
+ */
11
+ export abstract class OINOBenchmark {
12
+
13
+ protected static _instance:OINOBenchmark
14
+ protected static _enabled:Record<string, boolean> = {}
15
+
16
+ /**
17
+ * Create a new OINOBenchmark instance.
18
+ *
19
+ * @param enabledModules array of those benchmarks that are enabled
20
+ */
21
+ constructor(enabledModules:string[] = []) {
22
+ OINOBenchmark.setEnabled(enabledModules)
23
+ }
24
+
25
+ /**
26
+ * Set active benchmarking instance.
27
+ *
28
+ * @param instance OINOBenchmark instance
29
+ *
30
+ */
31
+ static setInstance(instance: OINOBenchmark) {
32
+ if (instance) {
33
+ OINOBenchmark._instance = instance
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Get active benchmarking instance.
39
+ *
40
+ */
41
+ static getInstance(): OINOBenchmark {
42
+ return OINOBenchmark._instance
43
+ }
44
+
45
+ protected abstract _reset():void
46
+ /**
47
+ * Reset benchmark data (but not what is enabled).
48
+ *
49
+ */
50
+ static reset():void {
51
+ OINOBenchmark._instance?._reset()
52
+ }
53
+
54
+ /**
55
+ * Set benchmark names that are enabled.
56
+ *
57
+ * @param modules array of those benchmarks that are enabled
58
+ */
59
+ static setEnabled(modules:string[]):void {
60
+ OINOBenchmark._enabled = {}
61
+ modules.forEach(module_name => {
62
+ this._enabled[module_name] = true
63
+ });
64
+ }
65
+
66
+ protected abstract _startMetric(module:string, method:string):void
67
+ /**
68
+ * Start benchmark timing.
69
+ *
70
+ * @param module of the benchmark
71
+ * @param method of the benchmark
72
+ */
73
+ static startMetric(module:string, method:string):void {
74
+ OINOBenchmark._instance?._startMetric(module, method)
75
+ }
76
+
77
+ protected abstract _endMetric(module:string, method:string):void
78
+ /**
79
+ * Complete benchmark timing
80
+ *
81
+ * @param module of the benchmark
82
+ * @param method of the benchmark
83
+ */
84
+ static endMetric(module:string, method:string):void {
85
+ OINOBenchmark._instance?._endMetric(module, method)
86
+ }
87
+
88
+ protected abstract _getMetric(module:string, method:string):number
89
+ /**
90
+ * Get given benchmark data.
91
+ *
92
+ * @param module of the benchmark
93
+ * @param method of the benchmark
94
+ *
95
+ */
96
+ static getMetric(module:string, method:string):number {
97
+ return OINOBenchmark._instance?._getMetric(module, method)
98
+ }
99
+
100
+ protected abstract _getMetrics():Record<string, number>
101
+ /**
102
+ * Get all benchmark data.
103
+ *
104
+ */
105
+ static getMetrics():Record<string, number> {
106
+ return OINOBenchmark._instance?._getMetrics()
107
+ }
108
+
109
+ protected abstract _trackMetric(module:string, method:string, value:number):void
110
+ /**
111
+ * Track a metric value
112
+ *
113
+ * @param module of the metric
114
+ * @param method of the metric
115
+ * @param value of the metric
116
+ *
117
+ */
118
+ static trackMetric(module:string, method:string, value:number):void {
119
+ if (OINOBenchmark._enabled[module]) {
120
+ OINOBenchmark._instance?._trackMetric(module, method, value)
121
+ }
122
+ }
123
+
124
+ protected abstract _trackException(module:string, method:string, name:string, message:string, stack: string):void
125
+ /**
126
+ * Track an exception
127
+ *
128
+ * @param module of the benchmark
129
+ * @param method of the benchmark
130
+ * @param name of the exception
131
+ * @param message of the exception
132
+ * @param stack trace of the exception
133
+ */
134
+ static trackException(module:string, method:string, name:string, message:string, stack:string):void {
135
+ if (OINOBenchmark._enabled[module]) {
136
+ OINOBenchmark._instance?._trackException(module, method, name, message, stack)
137
+ }
138
+ }
139
+
140
+ protected abstract _getExceptions():any[]
141
+ /**
142
+ * Get all tracked exceptions.
143
+ *
144
+ */
145
+ static getExceptions():any[] {
146
+ return OINOBenchmark._instance?._getExceptions()
147
+ }
148
+ }
149
+
150
+ /**
151
+ * OINOMemoryBenchmark is a memory-based benchmark implementation.
152
+ * It stores the benchmark data in memory and allows to reset, start, end and get benchmark data.
153
+ * In case of recursively/iteratively starting a benchmark, it will honor the first start and ignore the rest. *
154
+ */
155
+ export class OINOMemoryBenchmark extends OINOBenchmark {
156
+
157
+ protected _benchmarkCount:Record<string, number> = {}
158
+ protected _benchmarkData:Record<string, number> = {}
159
+ protected _benchmarkStart:Record<string, number> = {}
160
+
161
+ protected _exceptions:any[] = []
162
+
163
+ /**
164
+ * Reset benchmark data (but not what is enabled).
165
+ *
166
+ */
167
+ protected _reset():void {
168
+ this._benchmarkData = {}
169
+ this._benchmarkCount = {}
170
+ }
171
+
172
+ /**
173
+ * Start benchmark timing.
174
+ *
175
+ * @param module of the benchmark
176
+ * @param method of the benchmark
177
+ */
178
+ protected _startMetric(module:string, method:string):void {
179
+ const name:string = module + "." + method
180
+ if (OINOBenchmark._enabled[module] && ((this._benchmarkStart[name] === undefined) || (this._benchmarkStart[name] === 0))) { // if benchmark is already started (e.g. loop/recursion), do not start it again
181
+ this._benchmarkStart[name] = performance.now()
182
+ }
183
+ }
184
+
185
+ /**
186
+ * Complete benchmark timing
187
+ *
188
+ * @param module of the benchmark
189
+ * @param method of the benchmark
190
+ */
191
+ protected _endMetric(module:string, method:string):void {
192
+ const name:string = module + "." + method
193
+ let result:number = 0
194
+ if (OINOBenchmark._enabled[module] && (this._benchmarkStart[name] > 0)) { // if benchmark is started, end it
195
+ const duration = performance.now() - this._benchmarkStart[name]
196
+ this._benchmarkStart[name] = 0
197
+ this._trackMetric(module, method, duration)
198
+ }
199
+ return
200
+ }
201
+
202
+ /**
203
+ * Get given benchmark data.
204
+ *
205
+ * @param module of the benchmark
206
+ * @param method of the benchmark
207
+ *
208
+ */
209
+ protected _getMetric(module:string, method:string):number {
210
+ const name:string = module + "." + method
211
+ if (OINOBenchmark._enabled[module] && (this._benchmarkCount[name] > 0)) {
212
+ return this._benchmarkData[module] / this._benchmarkCount[module]
213
+ }
214
+ return -1
215
+ }
216
+
217
+ /**
218
+ * Get all benchmark data.
219
+ *
220
+ */
221
+ protected _getMetrics():Record<string, number> {
222
+ let result:Record<string, number> = {}
223
+ for (const name in this._benchmarkData) {
224
+ if (this._benchmarkCount[name] > 0) {
225
+ result[name] = this._benchmarkData[name] / this._benchmarkCount[name]
226
+ }
227
+ }
228
+ return result
229
+ }
230
+
231
+ protected _trackMetric(module:string, method:string, value:number):void {
232
+ const name:string = module + "." + method
233
+ if (this._benchmarkCount[name] == undefined) {
234
+ this._benchmarkCount[name] = 1
235
+ this._benchmarkData[name] = value
236
+ } else {
237
+ this._benchmarkCount[name] += 1
238
+ this._benchmarkData[name] += value
239
+ }
240
+ }
241
+
242
+ protected _trackException(module:string, method:string, name:string, message:string, stack:string):void {
243
+ const exception = { module, method, name, message, stack, timestamp: Date.now() }
244
+ this._exceptions.push(exception)
245
+ }
246
+
247
+ protected _getExceptions():any[] {
248
+ return this._exceptions
249
+ }
250
+ }