|
1 | | -import fs from 'fs'; |
2 | | -import { Buffer } from 'buffer'; |
3 | | - |
4 | 1 | export interface Transport { |
5 | 2 | encode(hex: string): string; |
6 | 3 | decode(data: string): string; |
7 | 4 | getEndpoint(network?: number | string, countriesList?: string | Array<string>): { [key: string]: Array<string> }; |
8 | 5 | sms(number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string; |
9 | 6 | mms(number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string; |
10 | 7 | 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>; |
12 | 9 | } |
13 | 10 |
|
14 | 11 | export interface Error extends globalThis.Error { |
@@ -170,19 +167,36 @@ const txms: Transport = { |
170 | 167 | return endpoint ? `${type}:${endpoint}${encodedMessage ? `${platform === 'ios' ? '&' : '?'}body=${encodedMessage}` : ''}` : `${type}:${platform === 'ios' ? '&' : '?'}body=${encodedMessage}`; |
171 | 168 | }, |
172 | 169 |
|
173 | | - downloadMessage(hex: string, optionalFilename?: string): void { |
| 170 | + async downloadMessage(hex: string, optionalFilename?: string): Promise<string> { |
174 | 171 | const encodedMessage = this.encode(hex); |
175 | 172 |
|
176 | 173 | let filename: string; |
177 | 174 | 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 | + |
181 | 183 | if (optionalFilename) { |
182 | | - filename = `${slugify(filename)}.txms.txt`; |
| 184 | + filename = `${slugify(optionalFilename)}.txms.txt`; |
183 | 185 | } |
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; |
186 | 200 | } |
187 | 201 | }; |
188 | 202 |
|
|
0 commit comments