Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - download #30

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
}

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
Loading