Skip to content

Commit

Permalink
Merge pull request #32 from bchainhub/update/feature-download-03
Browse files Browse the repository at this point in the history
Fixed download to multiplatform
  • Loading branch information
rastislavcore authored Aug 23, 2024
2 parents b64d2d4 + ac29b08 commit e9bb391
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ The library is designed to be compatible with both module systems, so you can ch
- `getEndpoint(network?: number | string, countriesList?: string | Array<string>): { [key: string]: Array<string> }` — Get an object of SMS endpoints (phone numbers) per country.
- `sms(number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string` — Create an SMS URI based on the provided parameters.
- `mms(number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string` — Create an MMS URI based on the provided parameters.
- `downloadMessage(hex: string, optionalFilename?: string): Promise<string>` — Download a file with the encoded content as `.txms.txt` file in your working directory.

### Parameters

Expand All @@ -171,7 +172,7 @@ npm i -g txms.js
txms {type} {value} {location}
```

- type: `encode` (`e`), `decode` (`d`), `getendpoint` (`g`)
- type: `encode` (`e`), `decode` (`d`), `getendpoint` (`g`), `sms`, `mms`, `download` (`dl`)
- value: message; network for getendpoint
- location: ISO 3166 Alpha-2 country/ies code/s for getendpoint

Expand Down
4 changes: 2 additions & 2 deletions bin/txms
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ function run(typ, value, loc) {
const mms = txms.mms(true, value);
process.stdout.write(`${mms}\n`);
} else if (typ === 'download' || typ === 'dl') {
txms.downloadMessage(value, loc);
process.stdout.write(`TxMS downloaded as file\n`);
const filename = txms.downloadMessage(value, loc);
process.stdout.write(`TxMS file was downloaded as "${filename}" in your working directory.\n`);
} else {
throw new Error('Invalid type specified.');
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "txms.js",
"version": "1.2.5",
"version": "1.2.6",
"description": "Transaction messaging service protocol",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
36 changes: 25 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import fs from 'fs';
import { Buffer } from 'buffer';

export interface Transport {
encode(hex: string): string;
decode(data: string): string;
getEndpoint(network?: number | string, countriesList?: string | Array<string>): { [key: string]: Array<string> };
sms(number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string;
mms(number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string;
generateMessageUri(type: 'sms' | 'mms', number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string;
downloadMessage(hex: string, optionalFilename?: string): void;
downloadMessage(hex: string, optionalFilename?: string): Promise<string>;
}

export interface Error extends globalThis.Error {
Expand Down Expand Up @@ -170,19 +167,36 @@ const txms: Transport = {
return endpoint ? `${type}:${endpoint}${encodedMessage ? `${platform === 'ios' ? '&' : '?'}body=${encodedMessage}` : ''}` : `${type}:${platform === 'ios' ? '&' : '?'}body=${encodedMessage}`;
},

downloadMessage(hex: string, optionalFilename?: string): void {
async downloadMessage(hex: string, optionalFilename?: string): Promise<string> {
const encodedMessage = this.encode(hex);

let filename: string;
const cleanedHex = hex.startsWith('0x') ? hex.slice(2) : hex;
const first6 = cleanedHex.slice(0, 6);
const last6 = cleanedHex.slice(-6);
filename = `${first6}${last6}.txms.txt`;
if (cleanedHex.length < 12) {
filename = `${cleanedHex}.txms.txt`;
} else {
const first6 = cleanedHex.slice(0, 6);
const last6 = cleanedHex.slice(-6);
filename = `${first6}${last6}.txms.txt`;
}

if (optionalFilename) {
filename = `${slugify(filename)}.txms.txt`;
filename = `${slugify(optionalFilename)}.txms.txt`;
}
const buffer = Buffer.from(encodedMessage, 'utf16le').swap16();
fs.writeFileSync(filename, buffer);

if (typeof process !== 'undefined' && process.versions && process.versions.node) {

Check failure on line 187 in src/index.ts

View workflow job for this annotation

GitHub Actions / format

'process' is not defined

Check failure on line 187 in src/index.ts

View workflow job for this annotation

GitHub Actions / format

'process' is not defined
const fs = await import('fs');
fs.writeFileSync(filename, encodedMessage);
} else {
const blob = new Blob([encodedMessage], { type: 'text/plain;charset=utf-16' });

Check failure on line 191 in src/index.ts

View workflow job for this annotation

GitHub Actions / format

'Blob' is not defined

const link = document.createElement('a');

Check failure on line 193 in src/index.ts

View workflow job for this annotation

GitHub Actions / format

'document' is not defined
link.href = URL.createObjectURL(blob);

Check failure on line 194 in src/index.ts

View workflow job for this annotation

GitHub Actions / format

'URL' is not defined
link.download = filename;
link.click();
}

return filename;
}
};

Expand Down

0 comments on commit e9bb391

Please sign in to comment.