Skip to content

Commit

Permalink
fix(crumb): send accept header; cleanup, type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed May 7, 2023
1 parent 32f3042 commit c21f025
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
root: true
env:
node: true
browser: true
commonjs: true
es2021: true
Expand Down
11 changes: 7 additions & 4 deletions src/lib/getCrumb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ export default async function getCrumb(
...fetchOptionsBase,
headers: {
...fetchOptionsBase.headers,
cookie: cookieJar.getCookieStringSync(url),
// NB, we won't get a set-cookie header back without this:
accept: "text/html,application/xhtml+xml,application/xml",
// This request will get our first cookies, so nothing to send.
// cookie: cookieJar.getCookieStringSync(url),
},
};

const response = await fetch(url, fetchOptions);
const setCookieHeader = response.headers.get("set-cookie");
if (setCookieHeader) cookieJar.setFromSetCookieHeaders(setCookieHeader, url);

console.log(response.headers);
console.log(setCookieHeader);
console.log(cookieJar);
// console.log(response.headers);
// console.log(setCookieHeader);
// console.log(cookieJar);

const source = await response.text();

Expand Down
6 changes: 3 additions & 3 deletions src/lib/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export interface QueueOptions {
}

export default class Queue {
concurrency: number = 1;
concurrency = 1;

_running: number = 0;
_running = 0;
_queue: Array<Job> = [];

constructor(opts: QueueOptions = {}) {
Expand All @@ -40,7 +40,7 @@ export default class Queue {
if (this._running < this.concurrency) this.runNext();
}

add(func: () => Promise<void>) {
add(func: () => Promise<any>) {
return new Promise((resolve, reject) => {
this._queue.push({ func, resolve, reject });
this.checkQueue();
Expand Down
14 changes: 8 additions & 6 deletions src/lib/yahooFinanceFetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RequestInit } from "node-fetch";
import type { RequestInfo, RequestInit, Response } from "node-fetch";
import Queue from "./queue.js";

import type { YahooFinanceOptions } from "./options.js";
Expand All @@ -14,8 +14,10 @@ const userAgent = `${pkg.name}/${pkg.version} (+${pkg.repository})`;
interface YahooFinanceFetchThisEnv {
[key: string]: any;
URLSearchParams: (init?: any) => any;
fetch: Function;
fetchDevel: Function;
fetch: (url: RequestInfo, init?: RequestInit) => Promise<Response>;
fetchDevel: () => Promise<
(url: RequestInfo, init?: RequestInit) => Promise<Response>
>;
}

interface YahooFinanceFetchThis {
Expand Down Expand Up @@ -45,7 +47,7 @@ function assertQueueOptions(queue: any, opts: any) {
}

function substituteVariables(this: YahooFinanceFetchThis, urlBase: string) {
return urlBase.replace(/\$\{([^\}]+)\}/g, (match, varName) => {
return urlBase.replace(/\$\{([^}]+)\}/g, (match, varName) => {
if (varName === "YF_QUERY_HOST") {
// const hosts = ["query1.finance.yahoo.com", "query2.finance.yahoo.com"];
// return hosts[Math.floor(Math.random() * hosts.length)];
Expand Down Expand Up @@ -96,7 +98,7 @@ async function yahooFinanceFetch(
const urlSearchParams = new URLSearchParams(params);
const url =
substituteVariables.call(this, urlBase) + "?" + urlSearchParams.toString();
console.log(url);
// console.log(url);

const fetchOptions = {
...fetchOptionsBase,
Expand All @@ -106,7 +108,7 @@ async function yahooFinanceFetch(
},
};

console.log(fetchOptions);
// console.log(fetchOptions);

// used in moduleExec.ts
if (func === "csv") func = "text";
Expand Down

0 comments on commit c21f025

Please sign in to comment.