Skip to content

Commit ac29b08

Browse files
committed
Fixed download to multiplatform
1 parent b64d2d4 commit ac29b08

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ The library is designed to be compatible with both module systems, so you can ch
145145
- `getEndpoint(network?: number | string, countriesList?: string | Array<string>): { [key: string]: Array<string> }` — Get an object of SMS endpoints (phone numbers) per country.
146146
- `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.
147147
- `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.
148+
- `downloadMessage(hex: string, optionalFilename?: string): Promise<string>` — Download a file with the encoded content as `.txms.txt` file in your working directory.
148149

149150
### Parameters
150151

@@ -171,7 +172,7 @@ npm i -g txms.js
171172
txms {type} {value} {location}
172173
```
173174

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

bin/txms

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ function run(typ, value, loc) {
5454
const mms = txms.mms(true, value);
5555
process.stdout.write(`${mms}\n`);
5656
} else if (typ === 'download' || typ === 'dl') {
57-
txms.downloadMessage(value, loc);
58-
process.stdout.write(`TxMS downloaded as file\n`);
57+
const filename = txms.downloadMessage(value, loc);
58+
process.stdout.write(`TxMS file was downloaded as "${filename}" in your working directory.\n`);
5959
} else {
6060
throw new Error('Invalid type specified.');
6161
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "txms.js",
3-
"version": "1.2.5",
3+
"version": "1.2.6",
44
"description": "Transaction messaging service protocol",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import fs from 'fs';
2-
import { Buffer } from 'buffer';
3-
41
export interface Transport {
52
encode(hex: string): string;
63
decode(data: string): string;
74
getEndpoint(network?: number | string, countriesList?: string | Array<string>): { [key: string]: Array<string> };
85
sms(number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string;
96
mms(number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string;
107
generateMessageUri(type: 'sms' | 'mms', number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string;
11-
downloadMessage(hex: string, optionalFilename?: string): void;
8+
downloadMessage(hex: string, optionalFilename?: string): Promise<string>;
129
}
1310

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

173-
downloadMessage(hex: string, optionalFilename?: string): void {
170+
async downloadMessage(hex: string, optionalFilename?: string): Promise<string> {
174171
const encodedMessage = this.encode(hex);
175172

176173
let filename: string;
177174
const cleanedHex = hex.startsWith('0x') ? hex.slice(2) : hex;
178-
const first6 = cleanedHex.slice(0, 6);
179-
const last6 = cleanedHex.slice(-6);
180-
filename = `${first6}${last6}.txms.txt`;
175+
if (cleanedHex.length < 12) {
176+
filename = `${cleanedHex}.txms.txt`;
177+
} else {
178+
const first6 = cleanedHex.slice(0, 6);
179+
const last6 = cleanedHex.slice(-6);
180+
filename = `${first6}${last6}.txms.txt`;
181+
}
182+
181183
if (optionalFilename) {
182-
filename = `${slugify(filename)}.txms.txt`;
184+
filename = `${slugify(optionalFilename)}.txms.txt`;
183185
}
184-
const buffer = Buffer.from(encodedMessage, 'utf16le').swap16();
185-
fs.writeFileSync(filename, buffer);
186+
187+
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
188+
const fs = await import('fs');
189+
fs.writeFileSync(filename, encodedMessage);
190+
} else {
191+
const blob = new Blob([encodedMessage], { type: 'text/plain;charset=utf-16' });
192+
193+
const link = document.createElement('a');
194+
link.href = URL.createObjectURL(blob);
195+
link.download = filename;
196+
link.click();
197+
}
198+
199+
return filename;
186200
}
187201
};
188202

0 commit comments

Comments
 (0)