@tylix/core 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tylix/core",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "The Tylix server core: HTTP server and router, logging, metrics and health checks, a shared store (memory, database, Redis), cache, rate limiting, queue, scheduler and channels.",
5
5
  "license": "MIT",
6
6
  "author": "Mbiyrukzz",
@@ -44,9 +44,9 @@
44
44
  "test": "node --test 'src/**/*.test.js'"
45
45
  },
46
46
  "dependencies": {
47
- "@tylix/auth": "^1.2.0",
48
- "@tylix/orm": "^1.2.0",
49
- "@tylix/shared": "^1.2.0",
47
+ "@tylix/auth": "^1.4.0",
48
+ "@tylix/orm": "^1.4.0",
49
+ "@tylix/shared": "^1.4.0",
50
50
  "ws": "^8.18.0"
51
51
  },
52
52
  "devDependencies": {
@@ -16,4 +16,10 @@ export declare function enhanceResponse<T extends import('node:http').ServerResp
16
16
  send(data?: unknown): T
17
17
  cookie(name: string, value: string, options?: CookieOptions): T
18
18
  clearCookie(name: string, options?: CookieOptions): T
19
+ /** Send bytes, a string or a document with toBuffer() (PDF, workbook) as a download. */
20
+ download(
21
+ data: Uint8Array | ArrayBuffer | string | { toBuffer(): Promise<Uint8Array> },
22
+ filename?: string,
23
+ options?: { inline?: boolean; type?: string },
24
+ ): Promise<T>
19
25
  }
@@ -49,6 +49,21 @@ function compress(res, text) {
49
49
  return null
50
50
  }
51
51
 
52
+ const DOWNLOAD_TYPES = {
53
+ pdf: 'application/pdf',
54
+ xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
55
+ csv: 'text/csv; charset=utf-8',
56
+ json: 'application/json',
57
+ txt: 'text/plain; charset=utf-8',
58
+ zip: 'application/zip',
59
+ png: 'image/png',
60
+ jpg: 'image/jpeg',
61
+ jpeg: 'image/jpeg',
62
+ svg: 'image/svg+xml',
63
+ html: 'text/html; charset=utf-8',
64
+ ics: 'text/calendar; charset=utf-8',
65
+ }
66
+
52
67
  const helpers = {
53
68
  status(code) {
54
69
  this.statusCode = code
@@ -108,6 +123,33 @@ const helpers = {
108
123
  return this
109
124
  },
110
125
 
126
+ // Sends a file to download: bytes, a string, or anything with an
127
+ // async toBuffer() (a @tylix/pdf document, a @tylix/excel workbook).
128
+ // The content type comes from the filename unless given.
129
+ // await res.download(createPdf(def), 'invoice.pdf')
130
+ // await res.download(workbook, 'orders.xlsx')
131
+ // await res.download(pdf, 'invoice.pdf', { inline: true }) // view in the browser
132
+ async download(data, filename = 'download', options = {}) {
133
+ let body = data
134
+ if (body && typeof body.toBuffer === 'function') body = await body.toBuffer()
135
+ if (typeof body === 'string') body = Buffer.from(body)
136
+ else if (body instanceof ArrayBuffer) body = Buffer.from(body)
137
+ else if (ArrayBuffer.isView(body) && !Buffer.isBuffer(body)) body = Buffer.from(body.buffer, body.byteOffset, body.byteLength)
138
+ if (!Buffer.isBuffer(body)) throw new TypeError('res.download() needs bytes, a string or an object with toBuffer()')
139
+ const name = String(filename).replace(/["\\\r\n]/g, '_')
140
+ const ascii = name.replace(/[^\x20-\x7e]/g, '_')
141
+ const type = options.type ?? DOWNLOAD_TYPES[name.split('.').pop().toLowerCase()] ?? 'application/octet-stream'
142
+ this.setHeader('Content-Type', type)
143
+ this.setHeader('Content-Length', body.length)
144
+ this.setHeader(
145
+ 'Content-Disposition',
146
+ `${options.inline ? 'inline' : 'attachment'}; filename="${ascii}"; filename*=UTF-8''${encodeURIComponent(name)}`,
147
+ )
148
+ this.setHeader('X-Content-Type-Options', 'nosniff')
149
+ this.end(this.req?.method === 'HEAD' ? undefined : body)
150
+ return this
151
+ },
152
+
111
153
  clearCookie(name, options = {}) {
112
154
  this.cookie(name, '', { ...options, maxAge: 0, expires: new Date(0) })
113
155
  return this
@@ -16,4 +16,10 @@ export interface TylixResponse extends ServerResponse {
16
16
  send(data?: unknown): TylixResponse
17
17
  cookie(name: string, value: string, options?: CookieOptions): TylixResponse
18
18
  clearCookie(name: string, options?: CookieOptions): TylixResponse
19
+ /** Send bytes, a string or a document with toBuffer() (PDF, workbook) as a download. */
20
+ download(
21
+ data: Uint8Array | ArrayBuffer | string | { toBuffer(): Promise<Uint8Array> },
22
+ filename?: string,
23
+ options?: { inline?: boolean; type?: string },
24
+ ): Promise<TylixResponse>
19
25
  }
package/src/types.ts CHANGED
@@ -23,4 +23,5 @@ export interface TylixResponse extends ServerResponse {
23
23
  send(data?: unknown): TylixResponse;
24
24
  cookie(name: string, value: string, options?: Record<string, unknown>): TylixResponse;
25
25
  clearCookie(name: string, options?: Record<string, unknown>): TylixResponse;
26
+ download(data: Uint8Array | ArrayBuffer | string | { toBuffer(): Promise<Uint8Array> }, filename?: string, options?: { inline?: boolean; type?: string }): Promise<TylixResponse>;
26
27
  }