|
3 | 3 | *
|
4 | 4 | * MIT License
|
5 | 5 | *
|
6 |
| - * Copyright (c) 2019 Richie Bendall |
| 6 | + * Copyright (c) 2016 - 1019 The Node Fetch Team |
7 | 7 | *
|
8 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
9 | 9 | * of this software and associated documentation files (the 'Software'), to deal
|
|
24 | 24 | * SOFTWARE.
|
25 | 25 | */
|
26 | 26 |
|
| 27 | +import { decode as convert } from "iconv-lite" |
| 28 | +import getCharSet from "./utils/getCharSet" |
| 29 | +import { parse as $ } from "cheerio" |
| 30 | +import { isURLSearchParams, isBlob, isArrayBuffer } from "./utils/is" |
| 31 | +import { Stream, Writable } from "stream" |
| 32 | + |
27 | 33 | /**
|
28 |
| - * A quick start template for Typescript. |
| 34 | +* Detect buffer encoding and convert to target encoding |
| 35 | +* ref: http://www.w3.org/TR/2011/WD-html5-20110113/parsing.html#determining-the-character-encoding |
| 36 | +* |
| 37 | +* @param buffer Incoming buffer. |
| 38 | +* @param headers Headers provided with the request. |
| 39 | +*/ |
| 40 | +export function convertBody(buffer: Buffer, headers?: Headers): string { |
| 41 | + const contentType = headers instanceof Headers ? headers.get("content-type") : null |
| 42 | + let charset: string |
| 43 | + |
| 44 | + // Header |
| 45 | + if (contentType) { |
| 46 | + charset = getCharSet(contentType) |
| 47 | + } |
| 48 | + |
| 49 | + // No charset in content type, peek at response body for at most 1024 bytes |
| 50 | + const res = buffer.slice(0, 1024).toString() |
| 51 | + |
| 52 | + // HTML5, HTML4 and XML |
| 53 | + if (!charset && res) { |
| 54 | + charset = getCharSet( |
| 55 | + $(res)("meta[charset]").attr("charset") || // HTML5 |
| 56 | + $(res)("meta[http-equiv][content]").attr("content") || // HTML4 |
| 57 | + $(res.replace(/<\?(.*)\?>/im, "<$1>"), { xmlMode: true }).root().find("xml").attr("encoding") // XML |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + // Prevent decode issues when sites use incorrect encoding |
| 62 | + // ref: https://hsivonen.fi/encoding-menu/ |
| 63 | + if (charset && charset.toLowerCase() in ["gb2312", "gbk"]) { |
| 64 | + charset = "gb18030" |
| 65 | + } |
| 66 | + |
| 67 | + // Turn raw buffers into a single utf-8 buffer |
| 68 | + return convert( |
| 69 | + buffer, |
| 70 | + charset || "utf-8" |
| 71 | + ) |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Performs the operation "extract a `Content-Type` value from |object|" as |
| 76 | + * specified in the specification: |
| 77 | + * https://fetch.spec.whatwg.org/#concept-bodyinit-extract |
| 78 | + * |
| 79 | + * This function assumes that instance.body is present. |
| 80 | + * |
| 81 | + * @param body Any options.body input |
29 | 82 | */
|
30 |
| -export class TypeScriptQuickStart { |
31 |
| - /** |
32 |
| - * Your first method. |
33 |
| - * @param text - The text to return |
34 |
| - */ |
35 |
| - public helloWorld(text: string = ""): string { |
36 |
| - return text |
| 83 | +export function extractContentType(body: any): string | null { |
| 84 | + // Body is string |
| 85 | + if (typeof body === "string") return "text/plain;charset=UTF-8" |
| 86 | + |
| 87 | + // Body is a URLSearchParams |
| 88 | + if (isURLSearchParams(body)) return "application/x-www-form-urlencoded;charset=UTF-8" |
| 89 | + |
| 90 | + // Body is blob |
| 91 | + if (isBlob(body)) return body.type || null |
| 92 | + |
| 93 | + // Body is a Buffer (Buffer, ArrayBuffer or ArrayBufferView) |
| 94 | + if (Buffer.isBuffer(body) || isArrayBuffer(body) || ArrayBuffer.isView(body)) return null |
| 95 | + |
| 96 | + // Detect form data input from form-data module |
| 97 | + if (typeof body.getBoundary === "function") return `multipart/form-data;boundary=${body.getBoundary()}` |
| 98 | + |
| 99 | + // Body is stream - can't really do much about this |
| 100 | + if (body instanceof Stream) return null |
| 101 | + |
| 102 | + // Body constructor defaults other things to string |
| 103 | + return "text/plain;charset=UTF-8" |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * The Fetch Standard treats this as if "total bytes" is a property on the body. |
| 108 | + * For us, we have to explicitly get it with a function. |
| 109 | + * |
| 110 | + * ref: https://fetch.spec.whatwg.org/#concept-body-total-bytes |
| 111 | + * |
| 112 | + * @param body Body object from the Body instance. |
| 113 | + */ |
| 114 | +export function getTotalBytes(body: any): number | null { |
| 115 | + // Body is null or undefined |
| 116 | + if (body == null) return 0 |
| 117 | + |
| 118 | + // Body is Blob |
| 119 | + if (isBlob(body)) return body.size |
| 120 | + |
| 121 | + // Body is Buffer |
| 122 | + if (Buffer.isBuffer(body)) return body.length |
| 123 | + |
| 124 | + // Detect form data input from form-data module |
| 125 | + if (body && typeof body.getLengthSync === "function") return body.hasKnownLength && body.hasKnownLength() ? body.getLengthSync() : null |
| 126 | + |
| 127 | + // Body is stream |
| 128 | + return null |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * Write a Body to a Node.js WritableStream (e.g. http.Request) object. |
| 133 | + * |
| 134 | + * @param body Body object from the Body instance. |
| 135 | + * @param dest The stream to write to. |
| 136 | + */ |
| 137 | +export function writeToStream(body: any, dest: Writable): void { |
| 138 | + // Body is null |
| 139 | + if (body == null) dest.end() |
| 140 | + |
| 141 | + // Body is Blob |
| 142 | + else if (isBlob(body)) body.stream().pipe(dest) |
| 143 | + |
| 144 | + // Body is buffer |
| 145 | + else if (Buffer.isBuffer(body)) { |
| 146 | + dest.write(body) |
| 147 | + dest.end() |
| 148 | + } else { |
| 149 | + // Body is stream |
| 150 | + body.pipe(dest) |
37 | 151 | }
|
38 | 152 | }
|
0 commit comments