Skip to content

Commit

Permalink
Merge pull request #30 from bchainhub/update/feature-download-01
Browse files Browse the repository at this point in the history
Feature - download
  • Loading branch information
rastislavcore authored Aug 23, 2024
2 parents 1e0d310 + 90dea22 commit 2e59c83
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 4 additions & 1 deletion bin/txms
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ function run(typ, value, loc) {
} else if (typ === 'mms') {
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`);
} else {
throw new Error('Invalid type specified. Use "encode", "decode", "getendpoint", "sms", or "mms".');
throw new Error('Invalid type specified.');
}
process.exit(0);
} catch (err) {
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.3",
"version": "1.2.4",
"description": "Transaction messaging service protocol",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
26 changes: 26 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import fs from 'fs';

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;
}

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

function slugify(str: string): string {
return str
.toLowerCase()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '');
}

const txms: Transport = {
encode(hex: string): string {
let data = '';
Expand Down Expand Up @@ -156,6 +167,21 @@ const txms: Transport = {
}

return endpoint ? `${type}:${endpoint}${encodedMessage ? `${platform === 'ios' ? '&' : '?'}body=${encodedMessage}` : ''}` : `${type}:${platform === 'ios' ? '&' : '?'}body=${encodedMessage}`;
},

downloadMessage(hex: string, optionalFilename?: string): void {
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 (optionalFilename) {
filename = `${slugify(filename)}.txms.txt`;
}
const buffer = Buffer.from(encodedMessage, 'utf16le').swap16();

Check failure on line 183 in src/index.ts

View workflow job for this annotation

GitHub Actions / format

'Buffer' is not defined
fs.writeFileSync(filename, buffer);
}
};

Expand Down

0 comments on commit 2e59c83

Please sign in to comment.