Skip to content

Commit 2e59c83

Browse files
Merge pull request #30 from bchainhub/update/feature-download-01
Feature - download
2 parents 1e0d310 + 90dea22 commit 2e59c83

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

bin/txms

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ function run(typ, value, loc) {
5353
} else if (typ === 'mms') {
5454
const mms = txms.mms(true, value);
5555
process.stdout.write(`${mms}\n`);
56+
} else if (typ === 'download' || typ === 'dl') {
57+
txms.downloadMessage(value, loc);
58+
process.stdout.write(`TxMS downloaded as file\n`);
5659
} else {
57-
throw new Error('Invalid type specified. Use "encode", "decode", "getendpoint", "sms", or "mms".');
60+
throw new Error('Invalid type specified.');
5861
}
5962
process.exit(0);
6063
} catch (err) {

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.3",
3+
"version": "1.2.4",
44
"description": "Transaction messaging service protocol",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import fs from 'fs';
2+
13
export interface Transport {
24
encode(hex: string): string;
35
decode(data: string): string;
46
getEndpoint(network?: number | string, countriesList?: string | Array<string>): { [key: string]: Array<string> };
57
sms(number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string;
68
mms(number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string;
79
generateMessageUri(type: 'sms' | 'mms', number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string;
10+
downloadMessage(hex: string, optionalFilename?: string): void;
811
}
912

1013
export interface Error extends globalThis.Error {
@@ -39,6 +42,14 @@ export function addCountry(networkId: number | string, countryCode: string, phon
3942
countries[networkKey][countryCode] = phoneNumbers;
4043
}
4144

45+
function slugify(str: string): string {
46+
return str
47+
.toLowerCase()
48+
.replace(/[^\w\s-]/g, '')
49+
.replace(/[\s_-]+/g, '-')
50+
.replace(/^-+|-+$/g, '');
51+
}
52+
4253
const txms: Transport = {
4354
encode(hex: string): string {
4455
let data = '';
@@ -156,6 +167,21 @@ const txms: Transport = {
156167
}
157168

158169
return endpoint ? `${type}:${endpoint}${encodedMessage ? `${platform === 'ios' ? '&' : '?'}body=${encodedMessage}` : ''}` : `${type}:${platform === 'ios' ? '&' : '?'}body=${encodedMessage}`;
170+
},
171+
172+
downloadMessage(hex: string, optionalFilename?: string): void {
173+
const encodedMessage = this.encode(hex);
174+
175+
let filename: string;
176+
const cleanedHex = hex.startsWith('0x') ? hex.slice(2) : hex;
177+
const first6 = cleanedHex.slice(0, 6);
178+
const last6 = cleanedHex.slice(-6);
179+
filename = `${first6}${last6}.txms.txt`;
180+
if (optionalFilename) {
181+
filename = `${slugify(filename)}.txms.txt`;
182+
}
183+
const buffer = Buffer.from(encodedMessage, 'utf16le').swap16();
184+
fs.writeFileSync(filename, buffer);
159185
}
160186
};
161187

0 commit comments

Comments
 (0)