From 84e69e50ab9b4eb5a4ac7972f009dcfc573e3ec9 Mon Sep 17 00:00:00 2001 From: fureweb-com Date: Tue, 16 Mar 2021 22:10:02 +0900 Subject: [PATCH] update nodeFecth implementation resolves #10 --- package.json | 2 +- src/fetch.js | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 5c83556..191a7b6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "public-google-sheets-parser", - "version": "1.2.3", + "version": "1.2.4", "description": "Get JSONArray from public google sheets with using only spreadsheetId", "scripts": { "build:dist": "npx babel ./src/index.js --out-file ./dist/index.js", diff --git a/src/fetch.js b/src/fetch.js index 8b3f9cb..42b36fa 100644 --- a/src/fetch.js +++ b/src/fetch.js @@ -3,10 +3,18 @@ const https = require('https') const nodeFetch = async (url) => { return new Promise((resolve, reject) => { const req = https.request(url, (res) => { - res.on('data', (data) => { - if (!String(data).startsWith('/*O_o*/')) return resolve(null) + const body = [] + let isStarted = false - const response = { ok: true, text: () => String(data) } + res.on('data', (chunk) => { + if (!isStarted && !String(chunk).startsWith('/*O_o*/')) return resolve(null) + isStarted = true + + body.push(chunk) + }) + + res.on('end', () => { + const response = { ok: true, text: () => Buffer.concat(body).toString() } resolve(response) }) })