bysquare 1.3.3 → 2.0.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.
package/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # bysquare
2
2
 
3
- ![build][build] ![build][license] ![build][version]
4
-
5
- <!-- Dependency free simple -->
3
+ ![version][version] ![build][build]
6
4
 
7
5
  Simple `Node.js` library to generate "PAY by square" `QR` string.
8
6
 
@@ -19,21 +17,24 @@ your implementation. See [examples](examples).
19
17
 
20
18
  ## How it works
21
19
 
22
- ![diagram](./docs/uml/logic.svg)
23
-
24
- [build]: https://img.shields.io/github/workflow/status/xseman/bysquare/tests
25
- [version]: https://img.shields.io/npm/v/bysquare
26
- [license]: https://img.shields.io/github/license/xseman/bysquare
20
+ ![diagram](./doc/uml/logic.svg)
27
21
 
28
22
  ## Install
29
23
 
30
- Node.js
24
+ GitHub
25
+
26
+ ```sh
27
+ npm install xseman/bysquare#master
28
+ npm install xseman/bysquare#develop
29
+ ```
30
+
31
+ npm registry
31
32
 
32
33
  ```sh
33
34
  npm install bysquare
34
35
  ```
35
36
 
36
- CLI, global
37
+ CLI
37
38
 
38
39
  ```sh
39
40
  npm install --global bysquare
@@ -42,24 +43,29 @@ npm install --global bysquare
42
43
  ## API
43
44
 
44
45
  ```ts
45
- generate(model: Model): Promise<string>
46
- parse(qr: string): Promise<ParsedModel>
46
+ generate(model: DataModel): Promise<string>
47
+ parse(qr: string): Promise<DataModel>
47
48
  detect(qr: string): Boolean
48
49
  ```
49
50
 
50
- **generate(model: Model): Promise\<string>**
51
+ **generate(model: DataModel): Promise\<string>**
51
52
 
52
53
  ```ts
53
- import { generate, Model, parse, PaymentOptions } from "bysquare"
54
-
55
- const model: Model = {
56
- IBAN: "SK9611000000002918599669",
57
- Amount: 100.0,
58
- CurrencyCode: "EUR",
59
- VariableSymbol: "123",
60
- Payments: 1,
61
- PaymentOptions: PaymentOptions.PaymentOrder,
62
- BankAccounts: 1
54
+ import { generate, DataModel, parse, PaymentOptions } from "bysquare"
55
+
56
+ const model: DataModel = {
57
+ invoiceId: "random-id",
58
+ payments: [
59
+ {
60
+ type: PaymentOptions.PaymentOrder,
61
+ amount: 100.0,
62
+ bankAccounts: [
63
+ { iban: "SK9611000000002918599669" },
64
+ ],
65
+ currencyCode: "EUR",
66
+ variableSymbol: "123",
67
+ }
68
+ ]
63
69
  }
64
70
 
65
71
  generate(model).then((qr: string) => {
@@ -67,15 +73,15 @@ generate(model).then((qr: string) => {
67
73
  })
68
74
  ```
69
75
 
70
- **parse(qr: string): Promise\<ParsedModel>**
76
+ **parse(qr: string): Promise\<DataModel>**
71
77
 
72
78
  ```ts
73
- import { ParsedModel, parse } from "bysquare"
79
+ import { parse, DataModel } from "bysquare"
74
80
 
75
81
  const qr = "0004A00090IFU27IV0J6HGGLIOTIBVHNQQJQ6LAVGNBT363HR13JC6C75G19O246KTT5G8LTLM67HOIATP4OOG8F8FDLJ6T26KFCB1690NEVPQVSG0"
76
- parse(qr).then((model: ParsedModel) => {
82
+ parse(qr).then((model: DataModel) => {
77
83
  // your logic...
78
- })
84
+ });
79
85
  ```
80
86
 
81
87
  **detect(qr: string): Boolean**
@@ -85,8 +91,6 @@ import { detect } from "bysquare"
85
91
 
86
92
  const qr = "0004A00090IFU27IV0J6HGGLIOTIBVHNQQJQ6LAVGNBT363HR13JC6C75G19O246KTT5G8LTLM67HOIATP4OOG8F8FDLJ6T26KFCB1690NEVPQVSG0"
87
93
  const isBysquare = detect(qr)
88
-
89
- // your logic...
90
94
  ```
91
95
 
92
96
  ## CLI
@@ -96,14 +100,18 @@ You can use json file with valid model to generate qr-string.
96
100
  ```sh
97
101
  # example.json
98
102
  # {
99
- # "InvoiceID: "random-id",
100
- # "IBAN": "SK9611000000002918599669",
101
- # "Amount": 100.0,
102
- # "CurrencyCode": "EUR",
103
- # "VariableSymbol": "123",
104
- # "Payments": 1,
105
- # "PaymentOptions": 1,
106
- # "BankAccounts": 1
103
+ # "invoiceId": "random-id",
104
+ # "payments": [
105
+ # {
106
+ # "type": 1,
107
+ # "amount": 100.0,
108
+ # "bankAccounts": [
109
+ # { "iban": "SK9611000000002918599669" }
110
+ # ],
111
+ # "currencyCode": "EUR",
112
+ # "variableSymbol": "123"
113
+ # }
114
+ # ]
107
115
  # }
108
116
 
109
117
  $ npx bysquare ./example.json
@@ -114,14 +122,18 @@ You can also use stdin.
114
122
 
115
123
  ```sh
116
124
  $ bysquare <<< '{
117
- "InvoiceID": "random-id",
118
- "IBAN": "SK9611000000002918599669",
119
- "Amount": 100.0,
120
- "CurrencyCode": "EUR",
121
- "VariableSymbol": "123",
122
- "Payments": 1,
123
- "PaymentOptions": 1,
124
- "BankAccounts": 1
125
+ "invoiceId": "random-id",
126
+ "payments": [
127
+ {
128
+ "type": 1,
129
+ "amount": 100.0,
130
+ "bankAccounts": [
131
+ { "iban": "SK9611000000002918599669" }
132
+ ],
133
+ "currencyCode": "EUR",
134
+ "variableSymbol": "123"
135
+ }
136
+ ]
125
137
  }'
126
138
  $ 0004A00090IFU27IV0J6HGGLIOTIBVHNQQJQ6LAVGNBT363HR13JC6C75G19O246KTT5G8LTLM67HOIATP4OOG8F8FDLJ6T26KFCB1690NEVPQVSG0
127
139
  ```
@@ -142,14 +154,16 @@ Versioning
142
154
  https://github.com/dherges/npm-version-git-flow
143
155
 
144
156
  - Stash unfinished work
145
- - Run tests and build app
146
- - Run the `preversion` script
147
- - Bump version in `package.json` as requested (patch, minor, major, etc)
148
- - Run the `version` script
149
- - Commit and tag
150
- - Run the `postversion` script
157
+ - Run `npm test`
158
+ - Run `npm version <patch, minor, major>`
159
+ - Commit and push
160
+ - Run `npm version`
161
+ - Follow git-flow instructions
151
162
  - Checkout to master
152
163
  - Push commits and tag, git push && git push --tags
153
- - npm publish --dry-run
154
- - Publish to npm, npm publish
164
+ - Validate with `npm publish --dry-run`
165
+ - Publish to npm, `npm publish`
155
166
  -->
167
+
168
+ [build]: https://img.shields.io/github/actions/workflow/status/xseman/bysquare/tests.yml
169
+ [version]: https://img.shields.io/npm/v/bysquare
package/lib/cli.js CHANGED
@@ -1,27 +1,22 @@
1
1
  #!/usr/bin/env node
2
- "use strict";
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- const fs_1 = require("fs");
8
- const path_1 = __importDefault(require("path"));
9
- const readline_1 = require("readline");
10
- const generate_1 = require("./generate");
2
+ import { existsSync, readFileSync } from "node:fs";
3
+ import path from "node:path";
4
+ import { createInterface } from "node:readline";
5
+ import { generate } from "./generate.js";
11
6
  if (process.stdin.isTTY) {
12
- /** bysquare "file" */
7
+ // bysquare "file"
13
8
  handleInput(process.argv[2]);
14
9
  }
15
10
  else {
16
- /** echo "data" | bysquare */
11
+ // echo "data" | bysquare
17
12
  ;
18
13
  (async () => {
19
14
  const stdin = await handleStdin();
20
- const qrString = await jsonStringToQrString(stdin).catch((e) => {
15
+ const qr = await fromJsonString(stdin).catch((e) => {
21
16
  console.error(e);
22
17
  process.exit(1);
23
18
  });
24
- console.log(qrString);
19
+ console.log(qr);
25
20
  process.exit(0);
26
21
  })();
27
22
  }
@@ -30,24 +25,24 @@ async function handleInput(input) {
30
25
  console.log(help());
31
26
  process.exit(0);
32
27
  }
33
- if ((0, fs_1.existsSync)(process.argv[2])) {
34
- const file = (0, fs_1.readFileSync)(process.argv[2], "utf8");
35
- const qrString = await jsonStringToQrString(file).catch((e) => {
28
+ if (existsSync(process.argv[2])) {
29
+ const file = readFileSync(process.argv[2], "utf8");
30
+ const qr = await fromJsonString(file).catch((e) => {
36
31
  console.error(e);
37
32
  process.exit(1);
38
33
  });
39
- console.log(qrString);
34
+ console.log(qr);
40
35
  }
41
36
  else {
42
37
  console.error(`File ${process.argv[2]} doesn't exists`);
43
38
  process.exit(1);
44
39
  }
45
40
  }
46
- async function jsonStringToQrString(stdin) {
41
+ async function fromJsonString(stdin) {
47
42
  return new Promise((resolve, reject) => {
48
43
  try {
49
44
  const data = JSON.parse(stdin);
50
- const qrString = (0, generate_1.generate)(data);
45
+ const qrString = generate(data);
51
46
  resolve(qrString);
52
47
  }
53
48
  catch (e) {
@@ -56,7 +51,7 @@ async function jsonStringToQrString(stdin) {
56
51
  });
57
52
  }
58
53
  async function handleStdin() {
59
- const readline = (0, readline_1.createInterface)({
54
+ const readline = createInterface({
60
55
  input: process.stdin,
61
56
  output: process.stdout,
62
57
  terminal: false
@@ -70,11 +65,11 @@ async function handleStdin() {
70
65
  .on("close", () => {
71
66
  resolve(lines.join(""));
72
67
  })
73
- .on("SIGINT" /* CTRL+C */, reject);
68
+ .on("SIGINT", /* CTRL+C */ reject);
74
69
  });
75
70
  }
76
71
  function help() {
77
- const exe = path_1.default.basename(process.argv[1]);
72
+ const exe = path.basename(process.argv[1]);
78
73
  return [
79
74
  "Simple Node.js library to generate 'PAY by square' QR string.",
80
75
  "",
package/lib/generate.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- /// <reference types="node" />
2
- import { Model } from "./index";
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import { DataModel } from "./types.js";
3
3
  /**
4
4
  * Returns a 2 byte buffer that represents the header of the bysquare
5
5
  * specification
@@ -15,7 +15,7 @@ import { Model } from "./index";
15
15
  *
16
16
  * @see 3.5. by square header
17
17
  */
18
- export declare function makeHeaderBysquare(header?: [
18
+ export declare function bysquareHeader(header?: [
19
19
  bySquareType: number,
20
20
  version: number,
21
21
  documentType: number,
@@ -24,24 +24,25 @@ export declare function makeHeaderBysquare(header?: [
24
24
  /**
25
25
  * @see 3.10 Appending CRC32 checksum
26
26
  */
27
- export declare function makeChecksum(tabbedInput: string): Buffer;
27
+ export declare function checksum(intermediate: string): Buffer;
28
28
  /**
29
29
  * Transfer object to a tabbed string and append a CRC32 checksum
30
30
  *
31
31
  * @see 3.10. Appending CRC32 checksum
32
32
  */
33
- export declare function prepareForCompression(model: Model): Buffer;
33
+ export declare function prepareCompression(model: DataModel): Buffer;
34
34
  /**
35
- * Convert object to tab-separated fields according to the sequence specification
35
+ * Transform data to ordered tab-separated intermediate representation ready for
36
+ * encoding
36
37
  *
37
38
  * @see Table 15 PAY by square sequence data model
38
39
  */
39
- export declare function makeTabbed(model: Model): string;
40
+ export declare function toIntermediate(data: DataModel): string;
41
+ type Options = {
42
+ deburr: boolean;
43
+ };
40
44
  /**
41
- * @see 3.13. Alphanumeric conversion using Base32hex
45
+ * Generate QR string ready for encoding into text QR code
42
46
  */
43
- export declare function alphanumericConversion(data: Buffer): string;
44
- /**
45
- * Generate QR string ready for encoding into basic QR code
46
- */
47
- export declare function generate(model: Model): Promise<string>;
47
+ export declare function generate(model: DataModel, options?: Options): Promise<string>;
48
+ export {};
package/lib/generate.js CHANGED
@@ -1,35 +1,7 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.generate = exports.alphanumericConversion = exports.makeTabbed = exports.prepareForCompression = exports.makeChecksum = exports.makeHeaderBysquare = void 0;
30
- const lodash_deburr_1 = __importDefault(require("lodash.deburr"));
31
- const lzma = __importStar(require("lzma-native"));
32
- const index_1 = require("./index");
1
+ import deburr from "lodash.deburr";
2
+ import lzma from "lzma-native";
3
+ import { base32hex } from "rfc4648";
4
+ import { PaymentOptions } from "./types.js";
33
5
  // echo "Hello" | xz --format=raw --lzma1=lc=3,lp=0,pb=2,dict=32KiB --stdout | hexdump -C
34
6
  /**
35
7
  * Returns a 2 byte buffer that represents the header of the bysquare
@@ -46,23 +18,22 @@ const index_1 = require("./index");
46
18
  *
47
19
  * @see 3.5. by square header
48
20
  */
49
- function makeHeaderBysquare(header = [
50
- 0b0000_0000, 0b0000_0000,
51
- 0b0000_0000, 0b0000_0000
21
+ export function bysquareHeader(header = [
22
+ 0, 0,
23
+ 0, 0
52
24
  ]) {
53
25
  const isValid = header.every((nibble) => 0 <= nibble && nibble <= 15);
54
26
  if (!isValid) {
55
- throw new Error("Header range of values must be <0,15>");
27
+ throw new Error(`Invalid header byte value, valid range <0,15>`);
56
28
  }
57
29
  const [bySquareType, version, documentType, reserved] = header;
58
- /** Combine 4-nibbles to 2-bytes */
59
- const headerBuffer = Buffer.from([
30
+ // Combine 4-nibbles to 2-bytes
31
+ const mergedNibbles = Buffer.from([
60
32
  (bySquareType << 4) | (version << 0),
61
- (documentType << 4) | (reserved << 0)
33
+ (documentType << 4) | (reserved << 0),
62
34
  ]);
63
- return headerBuffer;
35
+ return mergedNibbles;
64
36
  }
65
- exports.makeHeaderBysquare = makeHeaderBysquare;
66
37
  /**
67
38
  * Allocates a new buffer of a 2 bytes that represents LZMA header which
68
39
  * contains 16-bit unsigned integer (word, little-endian), which is the size of
@@ -71,7 +42,7 @@ exports.makeHeaderBysquare = makeHeaderBysquare;
71
42
  *
72
43
  * @see 3.11. LZMA Compression
73
44
  */
74
- function makeHeaderLzma(decompressedData) {
45
+ function lzmaHeader(decompressedData) {
75
46
  const bytesCount = decompressedData.length;
76
47
  if (bytesCount >= 2 ** 16) {
77
48
  throw new Error("The maximum compressed data size has been reached");
@@ -83,121 +54,143 @@ function makeHeaderLzma(decompressedData) {
83
54
  /**
84
55
  * @see 3.10 Appending CRC32 checksum
85
56
  */
86
- function makeChecksum(tabbedInput) {
57
+ export function checksum(intermediate) {
87
58
  // @ts-ignore: Wrong return type
88
- const data = lzma.crc32(tabbedInput);
59
+ const data = lzma.crc32(intermediate);
89
60
  const crc32 = Buffer.alloc(4);
90
61
  crc32.writeUInt32LE(data);
91
62
  return crc32;
92
63
  }
93
- exports.makeChecksum = makeChecksum;
94
64
  /**
95
65
  * Transfer object to a tabbed string and append a CRC32 checksum
96
66
  *
97
67
  * @see 3.10. Appending CRC32 checksum
98
68
  */
99
- function prepareForCompression(model) {
100
- const tabbed = makeTabbed(model);
69
+ export function prepareCompression(model) {
70
+ const intermediate = toIntermediate(model);
101
71
  return Buffer.concat([
102
- makeChecksum(tabbed),
103
- Buffer.from(tabbed, "utf-8")
72
+ checksum(intermediate),
73
+ Buffer.from(intermediate, "utf-8")
104
74
  ]);
105
75
  }
106
- exports.prepareForCompression = prepareForCompression;
107
76
  /**
108
- * Convert object to tab-separated fields according to the sequence specification
77
+ * Transform data to ordered tab-separated intermediate representation ready for
78
+ * encoding
109
79
  *
110
80
  * @see Table 15 PAY by square sequence data model
111
81
  */
112
- function makeTabbed(model) {
113
- const tabbed = Object.keys(model).reduce((acc, key) => {
114
- const index = index_1.SequenceOrder[key];
115
- /** Diacritical marks are not allowed */
116
- if (key === "PaymentNote") {
117
- acc[index] = (0, lodash_deburr_1.default)(model[key]);
118
- return acc;
82
+ export function toIntermediate(data) {
83
+ const intermediate = new Array();
84
+ intermediate.push(data.invoiceId?.toString());
85
+ intermediate.push(data.payments.length.toString());
86
+ for (const p of data.payments) {
87
+ intermediate.push(p.type.toString());
88
+ intermediate.push(p.amount?.toString());
89
+ intermediate.push(p.currencyCode);
90
+ intermediate.push(p.paymentDueDate);
91
+ intermediate.push(p.variableSymbol);
92
+ intermediate.push(p.constantSymbol);
93
+ intermediate.push(p.specificSymbol);
94
+ intermediate.push(p.originatorRefInfo);
95
+ intermediate.push(p.paymentNote);
96
+ intermediate.push(p.bankAccounts.length.toString());
97
+ for (const ba of p.bankAccounts) {
98
+ intermediate.push(ba.iban);
99
+ intermediate.push(ba.bic);
100
+ }
101
+ if (p.type === PaymentOptions.StandingOrder) {
102
+ intermediate.push('1');
103
+ intermediate.push(p.day?.toString());
104
+ intermediate.push(p.month?.toString());
105
+ intermediate.push(p.periodicity);
106
+ intermediate.push(p.lastDate);
107
+ }
108
+ else {
109
+ intermediate.push('0');
119
110
  }
120
- acc[index] = String(model[key]);
121
- return acc;
122
- }, Array(33).fill(undefined));
123
- const notStandingOrder = tabbed[14] === undefined;
124
- const notDirectDebit = tabbed[19] === undefined;
125
- if (notStandingOrder) {
126
- const attributesLength = 4;
127
- tabbed[14] = String(0);
128
- tabbed.splice(15, attributesLength);
129
- if (notDirectDebit) {
130
- tabbed[19 - attributesLength] = String(0);
131
- tabbed.splice(20 - attributesLength, 10);
111
+ if (p.type === PaymentOptions.DirectDebit) {
112
+ intermediate.push('1');
113
+ intermediate.push(p.directDebitScheme?.toString());
114
+ intermediate.push(p.directDebitType?.toString());
115
+ intermediate.push(p.variableSymbol?.toString());
116
+ intermediate.push(p.specificSymbol?.toString());
117
+ intermediate.push(p.originatorRefInfo?.toString());
118
+ intermediate.push(p.mandateId?.toString());
119
+ intermediate.push(p.creditorId?.toString());
120
+ intermediate.push(p.contractId?.toString());
121
+ intermediate.push(p.maxAmount?.toString());
122
+ intermediate.push(p.validTillDate?.toString());
123
+ }
124
+ else {
125
+ intermediate.push('0');
132
126
  }
133
- return tabbed.join("\t");
134
127
  }
135
- if (notDirectDebit) {
136
- const attributesLength = 10;
137
- tabbed[19] = String(0);
138
- tabbed.splice(20, attributesLength);
128
+ for (const p of data.payments) {
129
+ intermediate.push(p.beneficiary?.name);
130
+ intermediate.push(p.beneficiary?.street);
131
+ intermediate.push(p.beneficiary?.city);
139
132
  }
140
- return tabbed.join("\t");
133
+ return intermediate.join('\t');
141
134
  }
142
- exports.makeTabbed = makeTabbed;
143
135
  /**
144
- * @see 3.13. Alphanumeric conversion using Base32hex
136
+ * Transfer diacritics to basic latin letters
145
137
  */
146
- function alphanumericConversion(data) {
147
- let paddedBinString = data.reduce((acc, byte) => acc + byte.toString(2).padStart(8, "0"), "");
148
- let paddedLength = paddedBinString.length;
149
- const remainder = paddedLength % 5;
150
- if (remainder) {
151
- paddedBinString += Array(5 - remainder).fill("0").join("");
152
- paddedLength += 5 - remainder;
153
- }
154
- /**
155
- * Map a binary number of 5 bits to a string representation 2^5
156
- * SUBST[0...32] represents char
157
- *
158
- * @see {@link SUBST}
159
- */
160
- let encoded = "";
161
- for (let i = 0; i < paddedLength / 5; i++) {
162
- const binStart = 5 * i;
163
- const binEnd = 5 * i + 5;
164
- const sliced = paddedBinString.slice(binStart, binEnd);
165
- const key = parseInt(sliced, 2);
166
- encoded += index_1.SUBST[key];
138
+ function removeDiacritics(model) {
139
+ for (const payment of model.payments) {
140
+ if (payment.paymentNote) {
141
+ payment.paymentNote = deburr(payment.paymentNote);
142
+ }
143
+ if (payment.beneficiary?.name) {
144
+ payment.beneficiary.name = deburr(payment.beneficiary.name);
145
+ }
146
+ if (payment.beneficiary?.city) {
147
+ payment.beneficiary.city = deburr(payment.beneficiary.city);
148
+ }
149
+ if (payment.beneficiary?.street) {
150
+ payment.beneficiary.street = deburr(payment.beneficiary.street);
151
+ }
167
152
  }
168
- return encoded;
169
153
  }
170
- exports.alphanumericConversion = alphanumericConversion;
171
154
  /**
172
- * Generate QR string ready for encoding into basic QR code
155
+ * Generate QR string ready for encoding into text QR code
173
156
  */
174
- function generate(model) {
175
- const data = prepareForCompression(model);
157
+ export function generate(model, options = { deburr: true }) {
158
+ if (options.deburr) {
159
+ removeDiacritics(model);
160
+ }
161
+ const data = prepareCompression(model);
176
162
  const compressedData = [];
177
163
  return new Promise((resolve, reject) => {
178
164
  const encoder = lzma.createStream("rawEncoder", {
179
165
  synchronous: true,
180
166
  // @ts-ignore: Missing filter types
181
- filters: [{ id: lzma.FILTER_LZMA1 }]
167
+ filters: [
168
+ {
169
+ // @ts-ignore: Missing filter types
170
+ id: lzma.FILTER_LZMA1,
171
+ lc: 3,
172
+ lp: 0,
173
+ pb: 2,
174
+ dict_size: 2 ** 17, // 128 kilobytes
175
+ },
176
+ ],
182
177
  });
183
178
  encoder
184
- .on("data", (chunk) => {
185
- compressedData.push(chunk);
186
- })
187
- .on("error", reject)
188
179
  .on("end", () => {
189
180
  const output = Buffer.concat([
190
- makeHeaderBysquare(),
191
- makeHeaderLzma(data),
181
+ bysquareHeader(),
182
+ lzmaHeader(data),
192
183
  ...compressedData
193
184
  ]);
194
- const qr = alphanumericConversion(output);
195
- resolve(qr);
185
+ resolve(base32hex.stringify(output, { pad: false }));
196
186
  })
187
+ .on("data", (chunk) => {
188
+ compressedData.push(chunk);
189
+ })
190
+ .on("error", reject)
197
191
  .write(data, (error) => {
198
192
  error && reject(error);
199
193
  encoder.end();
200
194
  });
201
195
  });
202
196
  }
203
- exports.generate = generate;
package/lib/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { parse, detect } from "./parse";
2
- export { generate } from "./generate";
3
- export * from "./types";
1
+ export { generate } from "./generate.js";
2
+ export { detect, parse } from "./parse.js";
3
+ export * from "./types.js";
package/lib/index.js CHANGED
@@ -1,24 +1,3 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.generate = exports.detect = exports.parse = void 0;
18
- // Library exposed functions and types
19
- var parse_1 = require("./parse");
20
- Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return parse_1.parse; } });
21
- Object.defineProperty(exports, "detect", { enumerable: true, get: function () { return parse_1.detect; } });
22
- var generate_1 = require("./generate");
23
- Object.defineProperty(exports, "generate", { enumerable: true, get: function () { return generate_1.generate; } });
24
- __exportStar(require("./types"), exports);
1
+ export { generate } from "./generate.js";
2
+ export { detect, parse } from "./parse.js";
3
+ export * from "./types.js";