diff --git a/README.md b/README.md index 5ec5a6d..e241aca 100644 --- a/README.md +++ b/README.md @@ -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): { [key: string]: Array }` — Get an object of SMS endpoints (phone numbers) per country. - `sms(number?: boolean | string | number | Array, 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, 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` — Download a file with the encoded content as `.txms.txt` file in your working directory. ### Parameters @@ -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 diff --git a/bin/txms b/bin/txms index 3dec311..a126d9f 100644 --- a/bin/txms +++ b/bin/txms @@ -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.'); } diff --git a/package.json b/package.json index 1eb3e85..1a5a8ab 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index daddb2c..ec81ef8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,3 @@ -import fs from 'fs'; -import { Buffer } from 'buffer'; - export interface Transport { encode(hex: string): string; decode(data: string): string; @@ -8,7 +5,7 @@ export interface Transport { sms(number?: boolean | string | number | Array, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string; mms(number?: boolean | string | number | Array, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string; generateMessageUri(type: 'sms' | 'mms', number?: boolean | string | number | Array, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string; - downloadMessage(hex: string, optionalFilename?: string): void; + downloadMessage(hex: string, optionalFilename?: string): Promise; } export interface Error extends globalThis.Error { @@ -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 { 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) { + const fs = await import('fs'); + fs.writeFileSync(filename, encodedMessage); + } else { + const blob = new Blob([encodedMessage], { type: 'text/plain;charset=utf-16' }); + + const link = document.createElement('a'); + link.href = URL.createObjectURL(blob); + link.download = filename; + link.click(); + } + + return filename; } };