@oino-ts/hashid 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.
package/README.md CHANGED
@@ -1,183 +1,183 @@
1
- # OINO TS
2
- OINO Is Not an ORM but it's trying to solve a similar problem for API development. Instead of mirroring your DB schema in code that needs manual updates, OINO will get the data schema from DBMS using SQL in real time. Every time your app starts, it has an updated data model which enables automatic (de)serialize SQL results to JSON/CSV and back. OINO works on the level below routing where you pass the method, URL ID, body and request parameters to the API-object. OINO will parse and validate the data against the data model and generate proper SQL for your DB. Because OINO knows how data is serialized (e.g. JSON), what column it belongs to (e.g. floating point number) and what the target database is, it knows how to parse, format and escape the value as valid SQL.
3
-
4
- ```
5
- const result:OINOApiResult = await api_orderdetails.doRequest("GET", id, body, params)
6
- return new Response(result.modelset.writeString(OINOContentType.json))
7
- ```
8
-
9
-
10
- # GETTING STARTED
11
-
12
- ### Setup
13
- Install the `@oino-ts/db` npm package and necessary database packages and import them in your code.
14
- ```
15
- bun install @oino-ts/db
16
- bun install @oino-ts/db-bunsqlite
17
- ```
18
-
19
- ```
20
- import { OINODb, OINOApi, OINOFactory } from "@oino-ts/db";
21
- import { OINODbBunSqlite } from "@oino-ts/db-bunsqlite"
22
- ```
23
-
24
- ### Register database and logger
25
- Register your database implementation and logger (see [`OINOConsoleLog`](https://pragmatta.github.io/oino-ts/classes/common_src.OINOConsoleLog.html) how to implement your own)
26
-
27
- ```
28
- OINOLog.setLogger(new OINOConsoleLog())
29
- OINOFactory.registerDb("OINODbBunSqlite", OINODbBunSqlite)
30
- ```
31
-
32
- ### Create a database
33
- Creating a database connection [`OINODb`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODb.html) is done by passing [`OINODbParams`](https://pragmatta.github.io/oino-ts/types/db_src.OINODbParams.html) to the factory method. For [`OINODbBunSqlite`](https://pragmatta.github.io/oino-ts/classes/db_bunsqlite_src.OINODbBunSqlite.html) that means a file url for the database file, for others network host, port, credentials etc.
34
- ```
35
- const db:OINODb = await OINOFactory.createDb( { type: "OINODbBunSqlite", url: "file://../localDb/northwind.sqlite" } )
36
- ```
37
-
38
- ### Create an API
39
- From a database you can create an [`OINOApi`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbApi.html) by passing [`OINOApiParams`](https://pragmatta.github.io/oino-ts/types/db_src.OINODbApiParams.html) with table name and preferences to the factory method.
40
- ```
41
- const api_employees:OINOApi = await OINOFactory.createApi(db, { tableName: "Employees", excludeFields:["BirthDate"] })
42
- ```
43
-
44
- ### Pass HTTP requests to API
45
- When you receive a HTTP request, just pass the method, URL ID, body and params to the correct API, which will parse and validate input and return results.
46
-
47
- ```
48
- const result:OINOApiResult = await api_orderdetails.doRequest("GET", id, body, params)
49
- ```
50
-
51
- ### Write results back to HTTP Response
52
- The results for a GET request will contain [`OINOModelSet`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbModelSet.html) data that can be written out as JSON or CSV as needed. For other requests result is just success or error with messages.
53
- ```
54
- return new Response(result.data.writeString(OINOContentType.json))
55
- ```
56
-
57
-
58
- # MAIN FEATURES
59
-
60
- ## RESTfull
61
- OINO maps HTTP methods GET/POST/PUT/DELETE to SQL operations SELECT/INSERT/UPDATE/DELETE. The GET/POST requests can be made without URL ID to get all rows or insert new ones and others target a single row using URL ID.
62
-
63
- For example HTTP POST
64
- ```
65
- Request and response:
66
- > curl.exe -X POST http://localhost:3001/orderdetails -H "Content-Type: application/json" --data '[{\"OrderID\":11077,\"ProductID\":99,\"UnitPrice\":19,\"Quantity\":1,\"Discount\":0}]'
67
- {"success":true,"statusCode":200,"statusMessage":"OK","messages":[]}
68
-
69
- SQL:
70
- INSERT INTO [OrderDetails] ("OrderID","ProductID","UnitPrice","Quantity","Discount") VALUES (11077,99,19,1,0);
71
- ```
72
-
73
-
74
- ## Universal Serialization
75
- OINO handles serialization of data to JSON/CSV/etc. and back based on the data model. It knows what columns exist, what is their data type and how to convert each to JSON/CSV and back. This allows also partial data to be sent, i.e. you can send only columns that need updating or even send extra columns and have them ignored.
76
-
77
- - Files can be sent to BLOB fields using BASE64 or MIME multipart encoding. Also supports standard HTML form file submission to blob fields and returning them data url images.
78
- - Datetimes are (optionally) normalized to ISO 8601 format.
79
- - Extended JSON-encoding
80
- - Unquoted literal `undefined` can be used to represent non-existent values (leaving property out works too but preserving structure might be easier e.g. when translating data).
81
- - CSV
82
- - Comma-separated, doublequotes.
83
- - Unquoted literal `null` represents null values.
84
- - Unquoted empty string represents undefined values.
85
- - Form data
86
- - Multipart-mixed and binary files not supported.
87
- - Non-existent value line (i.e. nothing after the empty line) treated as a null value.
88
- - Url-encoded
89
- - No null values, missing properties treated as undefined.
90
- - Multiple lines could be used to post multiple rows.
91
-
92
-
93
- ## Database Abstraction
94
- OINO functions as a database abstraction, providing a consistent interface for working with different databases. It abstracts out different conventions in connecting, making queries and formatting data.
95
-
96
- Currently supported databases:
97
- - Bun Sqlite through Bun native implementation
98
- - Postgresql through [pg](https://www.npmjs.com/package/pg)-package
99
- - Mariadb / Mysql-support through [mariadb](https://www.npmjs.com/package/mariadb)-package
100
- - Sql Server through [mssql](https://www.npmjs.com/package/mssql)-package
101
-
102
- ## Composite Keys
103
- To support tables with multipart primary keys OINO generates a composite key `_OINOID_` that is included in the result and can be used as the REST ID. For example in the example above table `OrderDetails` has two primary keys `OrderID` and `ProductID` making the `_OINOID_` of form `11077:99`.
104
-
105
- ## Power Of SQL
106
- Since OINO is just generating SQL, WHERE-conditions can be defined with [`OINODbSqlFilter`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlFilter.html), order with [`OINODbSqlOrder`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlOrder.html), limits/paging with [`OINODbSqlLimit`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlLimit.html) and aggregation with [`OINODbSqlAggregate`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlAggregate.html) that are passed as HTTP request parameters. No more API development where you make unique API endpoints for each filter that fetch all data with original API and filter in backend code. Every API can be filtered when and as needed without unnessecary data tranfer and utilizing SQL indexing when available.
107
-
108
- ## Swagger Support
109
- Swagger is great as long as the definitions are updated and with OINO you can automatically get a Swagger definition including a data model schema.
110
- ```
111
- if (url.pathname == "/swagger.json") {
112
- return new Response(JSON.stringify(OINOSwagger.getApiDefinition(api_array)))
113
- }
114
- ```
115
- ![Swagger definition with a data model schema](img/readme-swagger.png)
116
-
117
- ## Node support
118
- OINO is developped Typescript first but compiles to standard CommonJS and the NPM packages should work on either ESM / CommonJS. Checkout sample apps `readmeApp` (ESM) and `nodeApp` (CommonJS).
119
-
120
- ## HTMX support
121
- OINO is [htmx.org](https://htmx.org) friendly, allowing easy translation of [`OINODataRow`](https://pragmatta.github.io/oino-ts/types/db_src.OINODataRow.html) to HTML output using templates (cf. the [htmx sample app](https://github.com/pragmatta/oino-ts/tree/main/samples/htmxApp)).
122
-
123
- ## Hashids
124
- Autoinc numeric id's are very pragmatic and fit well with OINO (e.g. using a form without primary key fields to insert new rows with database assigned ids). However it's not always sensible to share information about the sequence. Hashids solve this by masking the original values by encrypting the ids using AES-128 and some randomness. Length of the hashid can be chosen from 12-32 characters where longer ids provide more security. However this should not be considereded a cryptographic solution for keeping ids secret but rather making it infeasible to iterate all ids.
125
-
126
-
127
- # STATUS
128
- OINO is currently a side project which should considered in beta status. That means that semantic versioning is the goal but backwards incompatible changes can still happen in point releases if serious architectual issues are discovered.
129
-
130
- ## Roadmap
131
- Major features that are considered in future releases ()
132
-
133
- ### Support for views
134
- Simple cases of views probably work already in some databases but edge cases might get complicated.
135
-
136
- For example issues could be
137
- - How to handle a view which does not have a complete private key?
138
- - What edge cases exist in updating views?
139
- - Can views be handled transparently to the DBMS or is there some fundamentally platform specific behavior?
140
-
141
- ### Batch updates
142
- Supporting batch updates similar to batch inserts is slightly bending the RESTfull principles but would still be a useful optional feature in some situations.
143
-
144
- ### Streaming
145
- One core idea is to be efficient in not making unnecessary copies of the data and minimizing garbage collection debt. This can be taken further by implementing streaming, allowing large dataset to be written to HTTP response as SQL result rows are received.
146
-
147
- ### SQL generation callbacks
148
- It would be useful to allow developer to validate / override SQL generation to cover cases OINO does not support or even workaround issues.
149
-
150
- ### Transactions
151
- Even though the basic case for OINO is executing SQL operations on individual rows, having an option to use SQL transactions could make sense at least for batch operations.
152
-
153
-
154
- # HELP
155
-
156
- ## Bug reports
157
- Fixing bugs is a priority and getting good quality bug reports helps. It's recommended to use the sample Northwind database included with project to replicate issues or make an SQL script export of the relevant table.
158
-
159
- ## Feedback
160
- Understanding and prioritizing the use cases for OINO is also important and feedback about how you'd use OINO is interesting. Feel free to raise issues and feature requests in Github, but understand that short term most of the effort goes towards reaching the beta stage.
161
-
162
- ## Typescript / Javascript architecture
163
- Typescript building with different targets and module-systemts and a ton of configuration is a complex domain and something I have little experience un so help in fixing problems and how thing ought to be done is appreciated.
164
-
165
- # LINKS
166
- - [Github repository](https://github.com/pragmatta/oino-ts)
167
- - [NPM repository](https://www.npmjs.com/org/oino-ts)
168
-
169
-
170
- # ACKNOWLEDGEMENTS
171
-
172
- ## Libraries
173
- OINO uses the following open source libraries and npm packages and I would like to thank everyone for their contributions:
174
- - Postgresql [node-postgres package](https://www.npmjs.com/package/pg)
175
- - Mariadb / Mysql [mariadb package](https://www.npmjs.com/package/mariadb)
176
- - Sql Server [mssql package](https://www.npmjs.com/package/mssql)
177
- - Custom base encoding [base-x package](https://www.npmjs.com/package/base-x)
178
-
179
- ## Bun
180
- OINO has been developed using the Bun runtime, not because of the speed improvements but for the first class Typescript support and integrated developper experience. Kudos on the bun team for making Typescript work more exiting again.
181
-
182
- ## SQL Scripts
183
- The SQL scripts for creating the sample Northwind database are based on [Google Code archive](https://code.google.com/archive/p/northwindextended/downloads) and have been further customized to ensure they would have identical data (in the scope of the automated testing).
1
+ # OINO TS
2
+ OINO Is Not an ORM but it's trying to solve a similar problem for API development. Instead of mirroring your DB schema in code that needs manual updates, OINO will get the data schema from DBMS using SQL in real time. Every time your app starts, it has an updated data model which enables automatic (de)serialize SQL results to JSON/CSV and back. OINO works on the level below routing where you pass the method, URL ID, body and request parameters to the API-object. OINO will parse and validate the data against the data model and generate proper SQL for your DB. Because OINO knows how data is serialized (e.g. JSON), what column it belongs to (e.g. floating point number) and what the target database is, it knows how to parse, format and escape the value as valid SQL.
3
+
4
+ ```
5
+ const result:OINOApiResult = await api_orderdetails.doRequest("GET", id, body, params)
6
+ return new Response(result.modelset.writeString(OINOContentType.json))
7
+ ```
8
+
9
+
10
+ # GETTING STARTED
11
+
12
+ ### Setup
13
+ Install the `@oino-ts/db` npm package and necessary database packages and import them in your code.
14
+ ```
15
+ bun install @oino-ts/db
16
+ bun install @oino-ts/db-bunsqlite
17
+ ```
18
+
19
+ ```
20
+ import { OINODb, OINOApi, OINOFactory } from "@oino-ts/db";
21
+ import { OINODbBunSqlite } from "@oino-ts/db-bunsqlite"
22
+ ```
23
+
24
+ ### Register database and logger
25
+ Register your database implementation and logger (see [`OINOConsoleLog`](https://pragmatta.github.io/oino-ts/classes/common_src.OINOConsoleLog.html) how to implement your own)
26
+
27
+ ```
28
+ OINOLog.setLogger(new OINOConsoleLog())
29
+ OINOFactory.registerDb("OINODbBunSqlite", OINODbBunSqlite)
30
+ ```
31
+
32
+ ### Create a database
33
+ Creating a database connection [`OINODb`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODb.html) is done by passing [`OINODbParams`](https://pragmatta.github.io/oino-ts/types/db_src.OINODbParams.html) to the factory method. For [`OINODbBunSqlite`](https://pragmatta.github.io/oino-ts/classes/db_bunsqlite_src.OINODbBunSqlite.html) that means a file url for the database file, for others network host, port, credentials etc.
34
+ ```
35
+ const db:OINODb = await OINOFactory.createDb( { type: "OINODbBunSqlite", url: "file://../localDb/northwind.sqlite" } )
36
+ ```
37
+
38
+ ### Create an API
39
+ From a database you can create an [`OINOApi`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbApi.html) by passing [`OINOApiParams`](https://pragmatta.github.io/oino-ts/types/db_src.OINODbApiParams.html) with table name and preferences to the factory method.
40
+ ```
41
+ const api_employees:OINOApi = await OINOFactory.createApi(db, { tableName: "Employees", excludeFields:["BirthDate"] })
42
+ ```
43
+
44
+ ### Pass HTTP requests to API
45
+ When you receive a HTTP request, just pass the method, URL ID, body and params to the correct API, which will parse and validate input and return results.
46
+
47
+ ```
48
+ const result:OINOApiResult = await api_orderdetails.doRequest("GET", id, body, params)
49
+ ```
50
+
51
+ ### Write results back to HTTP Response
52
+ The results for a GET request will contain [`OINOModelSet`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbModelSet.html) data that can be written out as JSON or CSV as needed. For other requests result is just success or error with messages.
53
+ ```
54
+ return new Response(result.data.writeString(OINOContentType.json))
55
+ ```
56
+
57
+
58
+ # MAIN FEATURES
59
+
60
+ ## RESTfull
61
+ OINO maps HTTP methods GET/POST/PUT/DELETE to SQL operations SELECT/INSERT/UPDATE/DELETE. The GET/POST requests can be made without URL ID to get all rows or insert new ones and others target a single row using URL ID.
62
+
63
+ For example HTTP POST
64
+ ```
65
+ Request and response:
66
+ > curl.exe -X POST http://localhost:3001/orderdetails -H "Content-Type: application/json" --data '[{\"OrderID\":11077,\"ProductID\":99,\"UnitPrice\":19,\"Quantity\":1,\"Discount\":0}]'
67
+ {"success":true,"statusCode":200,"statusMessage":"OK","messages":[]}
68
+
69
+ SQL:
70
+ INSERT INTO [OrderDetails] ("OrderID","ProductID","UnitPrice","Quantity","Discount") VALUES (11077,99,19,1,0);
71
+ ```
72
+
73
+
74
+ ## Universal Serialization
75
+ OINO handles serialization of data to JSON/CSV/etc. and back based on the data model. It knows what columns exist, what is their data type and how to convert each to JSON/CSV and back. This allows also partial data to be sent, i.e. you can send only columns that need updating or even send extra columns and have them ignored.
76
+
77
+ - Files can be sent to BLOB fields using BASE64 or MIME multipart encoding. Also supports standard HTML form file submission to blob fields and returning them data url images.
78
+ - Datetimes are (optionally) normalized to ISO 8601 format.
79
+ - Extended JSON-encoding
80
+ - Unquoted literal `undefined` can be used to represent non-existent values (leaving property out works too but preserving structure might be easier e.g. when translating data).
81
+ - CSV
82
+ - Comma-separated, doublequotes.
83
+ - Unquoted literal `null` represents null values.
84
+ - Unquoted empty string represents undefined values.
85
+ - Form data
86
+ - Multipart-mixed and binary files not supported.
87
+ - Non-existent value line (i.e. nothing after the empty line) treated as a null value.
88
+ - Url-encoded
89
+ - No null values, missing properties treated as undefined.
90
+ - Multiple lines could be used to post multiple rows.
91
+
92
+
93
+ ## Database Abstraction
94
+ OINO functions as a database abstraction, providing a consistent interface for working with different databases. It abstracts out different conventions in connecting, making queries and formatting data.
95
+
96
+ Currently supported databases:
97
+ - Bun Sqlite through Bun native implementation
98
+ - Postgresql through [pg](https://www.npmjs.com/package/pg)-package
99
+ - Mariadb / Mysql-support through [mariadb](https://www.npmjs.com/package/mariadb)-package
100
+ - Sql Server through [mssql](https://www.npmjs.com/package/mssql)-package
101
+
102
+ ## Composite Keys
103
+ To support tables with multipart primary keys OINO generates a composite key `_OINOID_` that is included in the result and can be used as the REST ID. For example in the example above table `OrderDetails` has two primary keys `OrderID` and `ProductID` making the `_OINOID_` of form `11077:99`.
104
+
105
+ ## Power Of SQL
106
+ Since OINO is just generating SQL, WHERE-conditions can be defined with [`OINODbSqlFilter`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlFilter.html), order with [`OINODbSqlOrder`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlOrder.html), limits/paging with [`OINODbSqlLimit`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlLimit.html) and aggregation with [`OINODbSqlAggregate`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlAggregate.html) that are passed as HTTP request parameters. No more API development where you make unique API endpoints for each filter that fetch all data with original API and filter in backend code. Every API can be filtered when and as needed without unnessecary data tranfer and utilizing SQL indexing when available.
107
+
108
+ ## Swagger Support
109
+ Swagger is great as long as the definitions are updated and with OINO you can automatically get a Swagger definition including a data model schema.
110
+ ```
111
+ if (url.pathname == "/swagger.json") {
112
+ return new Response(JSON.stringify(OINOSwagger.getApiDefinition(api_array)))
113
+ }
114
+ ```
115
+ ![Swagger definition with a data model schema](img/readme-swagger.png)
116
+
117
+ ## Node support
118
+ OINO is developped Typescript first but compiles to standard CommonJS and the NPM packages should work on either ESM / CommonJS. Checkout sample apps `readmeApp` (ESM) and `nodeApp` (CommonJS).
119
+
120
+ ## HTMX support
121
+ OINO is [htmx.org](https://htmx.org) friendly, allowing easy translation of [`OINODataRow`](https://pragmatta.github.io/oino-ts/types/db_src.OINODataRow.html) to HTML output using templates (cf. the [htmx sample app](https://github.com/pragmatta/oino-ts/tree/main/samples/htmxApp)).
122
+
123
+ ## Hashids
124
+ Autoinc numeric id's are very pragmatic and fit well with OINO (e.g. using a form without primary key fields to insert new rows with database assigned ids). However it's not always sensible to share information about the sequence. Hashids solve this by masking the original values by encrypting the ids using AES-128 and some randomness. Length of the hashid can be chosen from 12-32 characters where longer ids provide more security. However this should not be considereded a cryptographic solution for keeping ids secret but rather making it infeasible to iterate all ids.
125
+
126
+
127
+ # STATUS
128
+ OINO is currently a side project which should considered in beta status. That means that semantic versioning is the goal but backwards incompatible changes can still happen in point releases if serious architectual issues are discovered.
129
+
130
+ ## Roadmap
131
+ Major features that are considered in future releases ()
132
+
133
+ ### Support for views
134
+ Simple cases of views probably work already in some databases but edge cases might get complicated.
135
+
136
+ For example issues could be
137
+ - How to handle a view which does not have a complete private key?
138
+ - What edge cases exist in updating views?
139
+ - Can views be handled transparently to the DBMS or is there some fundamentally platform specific behavior?
140
+
141
+ ### Batch updates
142
+ Supporting batch updates similar to batch inserts is slightly bending the RESTfull principles but would still be a useful optional feature in some situations.
143
+
144
+ ### Streaming
145
+ One core idea is to be efficient in not making unnecessary copies of the data and minimizing garbage collection debt. This can be taken further by implementing streaming, allowing large dataset to be written to HTTP response as SQL result rows are received.
146
+
147
+ ### SQL generation callbacks
148
+ It would be useful to allow developer to validate / override SQL generation to cover cases OINO does not support or even workaround issues.
149
+
150
+ ### Transactions
151
+ Even though the basic case for OINO is executing SQL operations on individual rows, having an option to use SQL transactions could make sense at least for batch operations.
152
+
153
+
154
+ # HELP
155
+
156
+ ## Bug reports
157
+ Fixing bugs is a priority and getting good quality bug reports helps. It's recommended to use the sample Northwind database included with project to replicate issues or make an SQL script export of the relevant table.
158
+
159
+ ## Feedback
160
+ Understanding and prioritizing the use cases for OINO is also important and feedback about how you'd use OINO is interesting. Feel free to raise issues and feature requests in Github, but understand that short term most of the effort goes towards reaching the beta stage.
161
+
162
+ ## Typescript / Javascript architecture
163
+ Typescript building with different targets and module-systemts and a ton of configuration is a complex domain and something I have little experience un so help in fixing problems and how thing ought to be done is appreciated.
164
+
165
+ # LINKS
166
+ - [Github repository](https://github.com/pragmatta/oino-ts)
167
+ - [NPM repository](https://www.npmjs.com/org/oino-ts)
168
+
169
+
170
+ # ACKNOWLEDGEMENTS
171
+
172
+ ## Libraries
173
+ OINO uses the following open source libraries and npm packages and I would like to thank everyone for their contributions:
174
+ - Postgresql [node-postgres package](https://www.npmjs.com/package/pg)
175
+ - Mariadb / Mysql [mariadb package](https://www.npmjs.com/package/mariadb)
176
+ - Sql Server [mssql package](https://www.npmjs.com/package/mssql)
177
+ - Custom base encoding [base-x package](https://www.npmjs.com/package/base-x)
178
+
179
+ ## Bun
180
+ OINO has been developed using the Bun runtime, not because of the speed improvements but for the first class Typescript support and integrated developper experience. Kudos on the bun team for making Typescript work more exiting again.
181
+
182
+ ## SQL Scripts
183
+ The SQL scripts for creating the sample Northwind database are based on [Google Code archive](https://code.google.com/archive/p/northwindextended/downloads) and have been further customized to ensure they would have identical data (in the scope of the automated testing).
package/package.json CHANGED
@@ -1,34 +1,34 @@
1
- {
2
- "name": "@oino-ts/hashid",
3
- "version": "0.17.1",
4
- "description": "OINO TS package for hashid's.",
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
- "hashid",
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
- "@types/node": "^20.12.7",
21
- "@oino-ts/common": "0.17.1",
22
- "base-x": "^5.0.0"
23
- },
24
- "devDependencies": {
25
- "typescript": "~5.9.0",
26
- "@oino-ts/types": "0.17.1"
27
- },
28
- "files": [
29
- "src/*.ts",
30
- "dist/cjs/*.js",
31
- "dist/esm/*.js",
32
- "dist/types/*.d.ts"
33
- ]
34
- }
1
+ {
2
+ "name": "@oino-ts/hashid",
3
+ "version": "0.17.2",
4
+ "description": "OINO TS package for hashid's.",
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
+ "hashid",
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
+ "@types/node": "^20.12.7",
21
+ "@oino-ts/common": "0.17.2",
22
+ "base-x": "^5.0.0"
23
+ },
24
+ "devDependencies": {
25
+ "typescript": "~5.9.0",
26
+ "@oino-ts/types": "0.17.2"
27
+ },
28
+ "files": [
29
+ "src/*.ts",
30
+ "dist/cjs/*.js",
31
+ "dist/esm/*.js",
32
+ "dist/types/*.d.ts"
33
+ ]
34
+ }
@@ -1,79 +1,79 @@
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
- import { expect, test } from "bun:test";
8
-
9
- import { OINOHASHID_MAX_LENGTH, OINOHashid } from "./OINOHashid";
10
- import { OINOLog, OINOConsoleLog, OINOLogLevel } from "@oino-ts/common"
11
-
12
- Math.random()
13
-
14
- OINOLog.setInstance(new OINOConsoleLog(OINOLogLevel.error))
15
-
16
- function benchmarkOINOHashId(hashid: OINOHashid, id: string, iterations: number = 1000): number {
17
- const start = performance.now();
18
-
19
- for (let i = 0; i < iterations; i++) {
20
- const h = hashid.encode(id, '');
21
- hashid.decode(h)
22
- }
23
-
24
- const end = performance.now();
25
- const duration = end - start;
26
- return Math.round(iterations / duration)
27
- }
28
-
29
- await test("OINOHashId persistent", async () => {
30
-
31
- let hps_min = Number.MAX_VALUE
32
- let hps_max = 0
33
-
34
- for (let j=12; j<=OINOHASHID_MAX_LENGTH; j++) {
35
- const hashid:OINOHashid = new OINOHashid('c7a87c6a5df870842eb6ef6d7937f0b4', 'OINOHashIdTestApp-persistent', j, true)
36
- let i:number = 1
37
- let id:string = ''
38
- while (i <= j) {
39
- id += i % 10
40
- const hashed_id = hashid.encode(id, '')
41
- const id2 = hashid.decode(hashed_id)
42
- // console.log("j: " + j + ", i: " + i + ", id: " + id + ", hashed_id: " + hashed_id)
43
- expect(id).toMatch(id2)
44
- i++
45
- }
46
- const hps = benchmarkOINOHashId(hashid, id, 2000)
47
- hps_min = Math.min(hps, hps_min)
48
- hps_max = Math.max(hps, hps_max)
49
- expect(hps_min).toBeGreaterThanOrEqual(30)
50
- expect(hps_max).toBeLessThanOrEqual(150)
51
- }
52
- console.log("OINOHashId persistent performance: " + hps_min + "k - " + hps_max + "k hashes per second")
53
- })
54
-
55
- await test("OINOHashId random", async () => {
56
-
57
- let hps_min = Number.MAX_VALUE
58
- let hps_max = 0
59
-
60
- for (let j=12; j<=OINOHASHID_MAX_LENGTH; j++) {
61
- const hashid:OINOHashid = new OINOHashid('c7a87c6a5df870842eb6ef6d7937f0b4', 'OINOHashIdTestApp-random', j, false)
62
- let i:number = 1
63
- let id:string = ''
64
- while (i <= j) {
65
- id += i % 10
66
- const hashed_id = hashid.encode(id, '')
67
- const id2 = hashid.decode(hashed_id)
68
- // console.log("j: " + j + ", i: " + i + ", id: " + id + ", hashed_id: " + hashed_id)
69
- expect(id).toMatch(id2)
70
- i++
71
- }
72
- const hps = benchmarkOINOHashId(hashid, id, 2000)
73
- hps_min = Math.min(hps, hps_min)
74
- hps_max = Math.max(hps, hps_max)
75
- }
76
- console.log("OINOHashId random performance: " + hps_min + "k - " + hps_max + "k hashes per second")
77
- })
78
-
79
-
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
+ import { expect, test } from "bun:test";
8
+
9
+ import { OINOHASHID_MAX_LENGTH, OINOHashid } from "./OINOHashid";
10
+ import { OINOLog, OINOConsoleLog, OINOLogLevel } from "@oino-ts/common"
11
+
12
+ Math.random()
13
+
14
+ OINOLog.setInstance(new OINOConsoleLog(OINOLogLevel.error))
15
+
16
+ function benchmarkOINOHashId(hashid: OINOHashid, id: string, iterations: number = 1000): number {
17
+ const start = performance.now();
18
+
19
+ for (let i = 0; i < iterations; i++) {
20
+ const h = hashid.encode(id, '');
21
+ hashid.decode(h)
22
+ }
23
+
24
+ const end = performance.now();
25
+ const duration = end - start;
26
+ return Math.round(iterations / duration)
27
+ }
28
+
29
+ await test("OINOHashId persistent", async () => {
30
+
31
+ let hps_min = Number.MAX_VALUE
32
+ let hps_max = 0
33
+
34
+ for (let j=12; j<=OINOHASHID_MAX_LENGTH; j++) {
35
+ const hashid:OINOHashid = new OINOHashid('c7a87c6a5df870842eb6ef6d7937f0b4', 'OINOHashIdTestApp-persistent', j, true)
36
+ let i:number = 1
37
+ let id:string = ''
38
+ while (i <= j) {
39
+ id += i % 10
40
+ const hashed_id = hashid.encode(id, '')
41
+ const id2 = hashid.decode(hashed_id)
42
+ // console.log("j: " + j + ", i: " + i + ", id: " + id + ", hashed_id: " + hashed_id)
43
+ expect(id).toMatch(id2)
44
+ i++
45
+ }
46
+ const hps = benchmarkOINOHashId(hashid, id, 2000)
47
+ hps_min = Math.min(hps, hps_min)
48
+ hps_max = Math.max(hps, hps_max)
49
+ expect(hps_min).toBeGreaterThanOrEqual(30)
50
+ expect(hps_max).toBeLessThanOrEqual(150)
51
+ }
52
+ console.log("OINOHashId persistent performance: " + hps_min + "k - " + hps_max + "k hashes per second")
53
+ })
54
+
55
+ await test("OINOHashId random", async () => {
56
+
57
+ let hps_min = Number.MAX_VALUE
58
+ let hps_max = 0
59
+
60
+ for (let j=12; j<=OINOHASHID_MAX_LENGTH; j++) {
61
+ const hashid:OINOHashid = new OINOHashid('c7a87c6a5df870842eb6ef6d7937f0b4', 'OINOHashIdTestApp-random', j, false)
62
+ let i:number = 1
63
+ let id:string = ''
64
+ while (i <= j) {
65
+ id += i % 10
66
+ const hashed_id = hashid.encode(id, '')
67
+ const id2 = hashid.decode(hashed_id)
68
+ // console.log("j: " + j + ", i: " + i + ", id: " + id + ", hashed_id: " + hashed_id)
69
+ expect(id).toMatch(id2)
70
+ i++
71
+ }
72
+ const hps = benchmarkOINOHashId(hashid, id, 2000)
73
+ hps_min = Math.min(hps, hps_min)
74
+ hps_max = Math.max(hps, hps_max)
75
+ }
76
+ console.log("OINOHashId random performance: " + hps_min + "k - " + hps_max + "k hashes per second")
77
+ })
78
+
79
+
package/src/OINOHashid.ts CHANGED
@@ -1,113 +1,113 @@
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
- import { BinaryLike, createCipheriv, createDecipheriv, createHmac, randomFillSync } from 'node:crypto';
8
- import basex from 'base-x'
9
-
10
- export const OINOHASHID_MIN_LENGTH:number = 12
11
- export const OINOHASHID_MAX_LENGTH:number = 42
12
- const OINOHASHID_ALPHABET:string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
13
- const hashidEncoder = basex(OINOHASHID_ALPHABET)
14
-
15
- /**
16
- * Hashid implementation for OINO API:s for the purpose of making it infeasible to scan
17
- * through numeric autoinc keys. It's not a solution to keeping the id secret in insecure
18
- * channels, just making it hard enough to not iterate through the entire key space. Half
19
- * of the the hashid length is nonce and half cryptotext, i.e. 16 char hashid 8 chars of
20
- * base64 encoded nonce ~ 6 bytes or 48 bits of entropy.
21
- *
22
- */
23
- export class OINOHashid {
24
-
25
- private _key:Buffer
26
- private _iv:Buffer
27
- private _domainId:string
28
- private _minLength:number
29
- private _staticIds
30
-
31
- /**
32
- * Hashid constructor
33
- *
34
- * @param key AES128 key (32 char hex-string)
35
- * @param domainId a sufficiently unique domain ID in which row-Id's are unique
36
- * @param minLength minimum length of nonce and crypto
37
- * @param staticIds whether hash values should remain static per row or random values
38
- *
39
- */
40
- constructor (key: string, domainId:string, minLength:number = OINOHASHID_MIN_LENGTH, staticIds:boolean = false) {
41
- this._domainId = domainId
42
- if ((minLength < OINOHASHID_MIN_LENGTH) || (minLength > OINOHASHID_MAX_LENGTH)) {
43
- throw Error("OINOHashid minLength (" + minLength + ")needs to be between " + OINOHASHID_MIN_LENGTH + " and " + OINOHASHID_MAX_LENGTH + "!")
44
- }
45
- this._minLength = Math.ceil(minLength/2)
46
- if (key.length != 32) {
47
- throw Error("OINOHashid key needs to be a 32 character hex-string!")
48
- }
49
- this._staticIds = staticIds
50
- this._key = Buffer.from(key, 'hex')
51
- this._iv = Buffer.alloc(16)
52
- }
53
-
54
- /**
55
- * Encode given id value as a hashid either using random data or given seed value for nonce.
56
- *
57
- * @param id numeric value
58
- * @param cellSeed a sufficiently unique seed for the current cell to keep hashids unique but persistent (e.g. fieldname + primarykey values)
59
- *
60
- */
61
- encode(id:string, cellSeed:string = ""):string {
62
-
63
- // if seed was given use it for pseudorandom chars, otherwise generate them
64
- let random_chars:string = ""
65
- if (this._staticIds) {
66
- const hmac_seed = createHmac('sha256', this._key as BinaryLike)
67
- hmac_seed.update(this._domainId + " " + cellSeed)
68
- random_chars = hashidEncoder.encode(hmac_seed.digest() as Uint8Array)
69
-
70
- } else {
71
- randomFillSync(this._iv as Uint8Array, 0, 16)
72
- random_chars = hashidEncoder.encode(this._iv as Uint8Array)
73
- }
74
- const hmac = createHmac('sha256', this._key as Uint8Array)
75
- let iv_seed:string = random_chars.substring(0, this._minLength)
76
- hmac.update(this._domainId + " " + iv_seed)
77
- const iv_data:Buffer = hmac.digest()
78
- iv_data.copy(this._iv as Uint8Array, 0, 0, 16)
79
-
80
- let plaintext = id.toString()
81
- if (plaintext.length < this._minLength) {
82
- plaintext += " " + random_chars.substring(random_chars.length - (this._minLength - plaintext.length - 1))
83
- }
84
-
85
- const cipher = createCipheriv('aes-128-gcm', this._key as Uint8Array, this._iv as Uint8Array)
86
- const cryptotext = hashidEncoder.encode(cipher.update(plaintext, 'utf8') as Uint8Array) + hashidEncoder.encode(cipher.final() as Uint8Array)
87
- return iv_seed + cryptotext
88
- }
89
-
90
- /**
91
- * Decode given hashid.
92
- *
93
- * @param hashid value
94
- *
95
- */
96
- decode(hashid:string):string {
97
- // reproduce nonce from seed
98
- const hmac = createHmac('sha256', this._key as Uint8Array)
99
- const iv_seed = hashid.substring(0, this._minLength)
100
- hmac.update(this._domainId + " " + iv_seed)
101
- const hash:Buffer = hmac.digest()
102
- hash.copy(this._iv as Uint8Array, 0, 0, 16)
103
-
104
- const cryptotext:string = hashid.substring(this._minLength)
105
- const cryptobytes:Buffer = Buffer.from(hashidEncoder.decode(cryptotext))
106
- const decipher = createDecipheriv('aes-128-gcm', this._key as Uint8Array, this._iv as Uint8Array)
107
- const plaintext = decipher.update(cryptobytes as Uint8Array, undefined, 'utf8')
108
-
109
- return plaintext.split(" ")[0]
110
- }
111
-
112
-
113
- }
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
+ import { BinaryLike, createCipheriv, createDecipheriv, createHmac, randomFillSync } from 'node:crypto';
8
+ import basex from 'base-x'
9
+
10
+ export const OINOHASHID_MIN_LENGTH:number = 12
11
+ export const OINOHASHID_MAX_LENGTH:number = 42
12
+ const OINOHASHID_ALPHABET:string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
13
+ const hashidEncoder = basex(OINOHASHID_ALPHABET)
14
+
15
+ /**
16
+ * Hashid implementation for OINO API:s for the purpose of making it infeasible to scan
17
+ * through numeric autoinc keys. It's not a solution to keeping the id secret in insecure
18
+ * channels, just making it hard enough to not iterate through the entire key space. Half
19
+ * of the the hashid length is nonce and half cryptotext, i.e. 16 char hashid 8 chars of
20
+ * base64 encoded nonce ~ 6 bytes or 48 bits of entropy.
21
+ *
22
+ */
23
+ export class OINOHashid {
24
+
25
+ private _key:Buffer
26
+ private _iv:Buffer
27
+ private _domainId:string
28
+ private _minLength:number
29
+ private _staticIds
30
+
31
+ /**
32
+ * Hashid constructor
33
+ *
34
+ * @param key AES128 key (32 char hex-string)
35
+ * @param domainId a sufficiently unique domain ID in which row-Id's are unique
36
+ * @param minLength minimum length of nonce and crypto
37
+ * @param staticIds whether hash values should remain static per row or random values
38
+ *
39
+ */
40
+ constructor (key: string, domainId:string, minLength:number = OINOHASHID_MIN_LENGTH, staticIds:boolean = false) {
41
+ this._domainId = domainId
42
+ if ((minLength < OINOHASHID_MIN_LENGTH) || (minLength > OINOHASHID_MAX_LENGTH)) {
43
+ throw Error("OINOHashid minLength (" + minLength + ")needs to be between " + OINOHASHID_MIN_LENGTH + " and " + OINOHASHID_MAX_LENGTH + "!")
44
+ }
45
+ this._minLength = Math.ceil(minLength/2)
46
+ if (key.length != 32) {
47
+ throw Error("OINOHashid key needs to be a 32 character hex-string!")
48
+ }
49
+ this._staticIds = staticIds
50
+ this._key = Buffer.from(key, 'hex')
51
+ this._iv = Buffer.alloc(16)
52
+ }
53
+
54
+ /**
55
+ * Encode given id value as a hashid either using random data or given seed value for nonce.
56
+ *
57
+ * @param id numeric value
58
+ * @param cellSeed a sufficiently unique seed for the current cell to keep hashids unique but persistent (e.g. fieldname + primarykey values)
59
+ *
60
+ */
61
+ encode(id:string, cellSeed:string = ""):string {
62
+
63
+ // if seed was given use it for pseudorandom chars, otherwise generate them
64
+ let random_chars:string = ""
65
+ if (this._staticIds) {
66
+ const hmac_seed = createHmac('sha256', this._key as BinaryLike)
67
+ hmac_seed.update(this._domainId + " " + cellSeed)
68
+ random_chars = hashidEncoder.encode(hmac_seed.digest() as Uint8Array)
69
+
70
+ } else {
71
+ randomFillSync(this._iv as Uint8Array, 0, 16)
72
+ random_chars = hashidEncoder.encode(this._iv as Uint8Array)
73
+ }
74
+ const hmac = createHmac('sha256', this._key as Uint8Array)
75
+ let iv_seed:string = random_chars.substring(0, this._minLength)
76
+ hmac.update(this._domainId + " " + iv_seed)
77
+ const iv_data:Buffer = hmac.digest()
78
+ iv_data.copy(this._iv as Uint8Array, 0, 0, 16)
79
+
80
+ let plaintext = id.toString()
81
+ if (plaintext.length < this._minLength) {
82
+ plaintext += " " + random_chars.substring(random_chars.length - (this._minLength - plaintext.length - 1))
83
+ }
84
+
85
+ const cipher = createCipheriv('aes-128-gcm', this._key as Uint8Array, this._iv as Uint8Array)
86
+ const cryptotext = hashidEncoder.encode(cipher.update(plaintext, 'utf8') as Uint8Array) + hashidEncoder.encode(cipher.final() as Uint8Array)
87
+ return iv_seed + cryptotext
88
+ }
89
+
90
+ /**
91
+ * Decode given hashid.
92
+ *
93
+ * @param hashid value
94
+ *
95
+ */
96
+ decode(hashid:string):string {
97
+ // reproduce nonce from seed
98
+ const hmac = createHmac('sha256', this._key as Uint8Array)
99
+ const iv_seed = hashid.substring(0, this._minLength)
100
+ hmac.update(this._domainId + " " + iv_seed)
101
+ const hash:Buffer = hmac.digest()
102
+ hash.copy(this._iv as Uint8Array, 0, 0, 16)
103
+
104
+ const cryptotext:string = hashid.substring(this._minLength)
105
+ const cryptobytes:Buffer = Buffer.from(hashidEncoder.decode(cryptotext))
106
+ const decipher = createDecipheriv('aes-128-gcm', this._key as Uint8Array, this._iv as Uint8Array)
107
+ const plaintext = decipher.update(cryptobytes as Uint8Array, undefined, 'utf8')
108
+
109
+ return plaintext.split(" ")[0]
110
+ }
111
+
112
+
113
+ }
package/src/index.ts CHANGED
@@ -1 +1 @@
1
- export { OINOHashid } from "./OINOHashid.js"
1
+ export { OINOHashid } from "./OINOHashid.js"