From 18d326866097d3c39fe4e1db4177d9b6b87e7785 Mon Sep 17 00:00:00 2001 From: youkehai <717407966@qq.com> Date: Fri, 5 Jan 2024 16:10:43 +0800 Subject: [PATCH 01/12] feat: add proxy by tunnel --- README.md | 3 +++ index.js | 19 +++++++++++++++++++ package.json | 3 ++- types/index.d.ts | 7 +++++++ types/index.test-d.ts | 10 ++++++++++ 5 files changed, 41 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a3ca4172..fcbc2e4a 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,9 @@ Creates a new `Shopify` instance. attach to all outgoing requests, like `beforeRetry`, `afterResponse`, etc. Hooks should be provided in the same format that Got expects them and will receive the same arguments Got passes unchanged. +- `proxy` - When your computer or server encounters network errors, timeouts, + etc. while accessing Shopify, if you have a proxy server, you can set the + proxy IP(host) and port(port) to resolve issues related to access timeouts and slowness.. #### Return value diff --git a/index.js b/index.js index 425f6f14..ce020d9f 100644 --- a/index.js +++ b/index.js @@ -5,10 +5,12 @@ const EventEmitter = require('events'); const stopcock = require('stopcock'); const got = require('got'); const url = require('url'); +const tunnel = require('tunnel'); const pkg = require('./package'); const resources = require('./resources'); + const retryableErrorCodes = new Set([ 'ETIMEDOUT', 'ECONNRESET', @@ -100,6 +102,10 @@ function Shopify(options) { 'Basic ' + Buffer.from(`${options.apiKey}:${options.password}`).toString('base64'); } + this.proxy = null; + if (options.proxy) { + this.proxy = {...options.proxy}; + } if (options.autoLimit) { const conf = transform( @@ -157,6 +163,19 @@ Shopify.prototype.request = function request(uri, method, key, data, headers) { method }; + if(this.proxy){ + const agentHttps = tunnel.httpsOverHttp({ + proxy: {...this.proxy} + }); + const agentHttp = tunnel.httpOverHttp({ + proxy: {...this.proxy} + }); + options.agent = { + http:agentHttp, + https:agentHttps + } + } + const afterResponse = (res) => { this.updateLimits(res.headers['x-shopify-shop-api-call-limit']); return res; diff --git a/package.json b/package.json index 712fe366..266414bf 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "got": "^11.1.4", "lodash": "^4.17.10", "qs": "^6.5.2", - "stopcock": "^1.0.0" + "stopcock": "^1.0.0", + "tunnel": "^0.0.6" }, "devDependencies": { "c8": "^7.3.0", diff --git a/types/index.d.ts b/types/index.d.ts index c91eddfd..2be24af5 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -789,6 +789,7 @@ declare namespace Shopify { shopName: string; timeout?: number; hooks?: Hooks; + proxy?:IProxyConfig; } export interface IPrivateShopifyConfig { @@ -801,6 +802,12 @@ declare namespace Shopify { shopName: string; timeout?: number; hooks?: Hooks; + proxy?:IProxyConfig; + } + + export interface IProxyConfig { + host:string; + port:number; } export interface ICallLimits { diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 2ed8f226..f6568db7 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -27,6 +27,16 @@ new Shopify({ } }); +// Can be constructed with Got proxy. +new Shopify({ + shopName: 'my-shopify-store.myshopify.com', + accessToken: '111', + proxy: { + host:'127.0.0.1', + port:7890 + } +}); + expectType(client.callLimits.remaining); expectType(client.callLimits.current); expectType(client.callLimits.max); From 6d8d0eeb51a8b4493a725263c6596f2860be3e1c Mon Sep 17 00:00:00 2001 From: youkehai <717407966@qq.com> Date: Mon, 8 Jan 2024 09:35:47 +0800 Subject: [PATCH 02/12] feat: add agent options for got --- README.md | 7 ++++--- index.js | 22 ++++++---------------- package.json | 3 +-- types/index.d.ts | 11 +++-------- types/index.test-d.ts | 12 +++++++++--- 5 files changed, 23 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index fcbc2e4a..56026ba4 100644 --- a/README.md +++ b/README.md @@ -78,9 +78,10 @@ Creates a new `Shopify` instance. attach to all outgoing requests, like `beforeRetry`, `afterResponse`, etc. Hooks should be provided in the same format that Got expects them and will receive the same arguments Got passes unchanged. -- `proxy` - When your computer or server encounters network errors, timeouts, - etc. while accessing Shopify, if you have a proxy server, you can set the - proxy IP(host) and port(port) to resolve issues related to access timeouts and slowness.. +- `agent` - Optional - A list of `got` + [request agent](https://github.com/sindresorhus/got/tree/v11.8.6?tab=readme-ov-file#agent) + This is necessary because a request to one protocol might redirect to another. + In such a scenario, Got will switch over to the right protocol agent for you. #### Return value diff --git a/index.js b/index.js index ce020d9f..36074faf 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,6 @@ const EventEmitter = require('events'); const stopcock = require('stopcock'); const got = require('got'); const url = require('url'); -const tunnel = require('tunnel'); const pkg = require('./package'); const resources = require('./resources'); @@ -102,10 +101,6 @@ function Shopify(options) { 'Basic ' + Buffer.from(`${options.apiKey}:${options.password}`).toString('base64'); } - this.proxy = null; - if (options.proxy) { - this.proxy = {...options.proxy}; - } if (options.autoLimit) { const conf = transform( @@ -163,17 +158,8 @@ Shopify.prototype.request = function request(uri, method, key, data, headers) { method }; - if(this.proxy){ - const agentHttps = tunnel.httpsOverHttp({ - proxy: {...this.proxy} - }); - const agentHttp = tunnel.httpOverHttp({ - proxy: {...this.proxy} - }); - options.agent = { - http:agentHttp, - https:agentHttps - } + if (this.options.agent) { + options.agent = { ...this.options.agent }; } const afterResponse = (res) => { @@ -304,6 +290,10 @@ Shopify.prototype.graphql = function graphql(data, variables) { body: json ? this.options.stringifyJson({ query: data, variables }) : data }; + if (this.options.agent) { + options.agent = { ...this.options.agent }; + } + const afterResponse = (res) => { if (res.body) { if (res.body.extensions && res.body.extensions.cost) { diff --git a/package.json b/package.json index 266414bf..712fe366 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,7 @@ "got": "^11.1.4", "lodash": "^4.17.10", "qs": "^6.5.2", - "stopcock": "^1.0.0", - "tunnel": "^0.0.6" + "stopcock": "^1.0.0" }, "devDependencies": { "c8": "^7.3.0", diff --git a/types/index.d.ts b/types/index.d.ts index 2be24af5..5252fee8 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,7 +1,7 @@ // Type definitions for shopify-api-node // Project: shopify-api-node // Definitions by: Rich Buggy -import { Hooks } from 'got'; +import { Hooks,Agents } from 'got'; /*~ This is the module template file for class modules. *~ You should rename it to index.d.ts and place it in a folder with the same name as the module. @@ -789,7 +789,7 @@ declare namespace Shopify { shopName: string; timeout?: number; hooks?: Hooks; - proxy?:IProxyConfig; + agent?:Agents; } export interface IPrivateShopifyConfig { @@ -802,12 +802,7 @@ declare namespace Shopify { shopName: string; timeout?: number; hooks?: Hooks; - proxy?:IProxyConfig; - } - - export interface IProxyConfig { - host:string; - port:number; + agent?:Agents; } export interface ICallLimits { diff --git a/types/index.test-d.ts b/types/index.test-d.ts index f6568db7..4081a4e5 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -31,9 +31,15 @@ new Shopify({ new Shopify({ shopName: 'my-shopify-store.myshopify.com', accessToken: '111', - proxy: { - host:'127.0.0.1', - port:7890 + agent: { + // https: new HttpsProxyAgent({ + // keepAlive: true, + // keepAliveMsecs: 1000, + // maxSockets: 256, + // maxFreeSockets: 256, + // scheduling: 'lifo', + // proxy: 'https://localhost:8080' + // }) } }); From 6d5b9b483213f5f036b7e238e30cfe439ed1269b Mon Sep 17 00:00:00 2001 From: Youkehai <42542308+Youkehai@users.noreply.github.com> Date: Thu, 11 Jan 2024 09:04:58 +0800 Subject: [PATCH 03/12] Update README.md Co-authored-by: Luigi Pinca --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 56026ba4..d1e45cc9 100644 --- a/README.md +++ b/README.md @@ -78,10 +78,10 @@ Creates a new `Shopify` instance. attach to all outgoing requests, like `beforeRetry`, `afterResponse`, etc. Hooks should be provided in the same format that Got expects them and will receive the same arguments Got passes unchanged. -- `agent` - Optional - A list of `got` - [request agent](https://github.com/sindresorhus/got/tree/v11.8.6?tab=readme-ov-file#agent) - This is necessary because a request to one protocol might redirect to another. - In such a scenario, Got will switch over to the right protocol agent for you. +- `agent` - Optional - An object that is passed as the `agent` option to `got`. + This allows to use a proxy server. See + [Got documentation](https://github.com/sindresorhus/got/tree/v11.8.6?tab=readme-ov-file#proxies) + for more details. #### Return value From 4fad0493bcedcf27a32b06dfdb582bff23e1c6ab Mon Sep 17 00:00:00 2001 From: Youkehai <42542308+Youkehai@users.noreply.github.com> Date: Thu, 11 Jan 2024 09:05:13 +0800 Subject: [PATCH 04/12] Update index.js Co-authored-by: Luigi Pinca --- index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/index.js b/index.js index 36074faf..85201b25 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,6 @@ const url = require('url'); const pkg = require('./package'); const resources = require('./resources'); - const retryableErrorCodes = new Set([ 'ETIMEDOUT', 'ECONNRESET', From 2b664273701bfda75124da47f7ddabbf6b58ec63 Mon Sep 17 00:00:00 2001 From: Youkehai <42542308+Youkehai@users.noreply.github.com> Date: Thu, 11 Jan 2024 09:05:31 +0800 Subject: [PATCH 05/12] Update index.js Co-authored-by: Luigi Pinca --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 85201b25..c8751201 100644 --- a/index.js +++ b/index.js @@ -154,6 +154,7 @@ Shopify.prototype.request = function request(uri, method, key, data, headers) { parseJson: this.options.parseJson, timeout: this.options.timeout, responseType: 'json', + agent: this.options.agent, method }; From c8e85382046de87c4528685ae328b1a004c323af Mon Sep 17 00:00:00 2001 From: Youkehai <42542308+Youkehai@users.noreply.github.com> Date: Thu, 11 Jan 2024 09:05:41 +0800 Subject: [PATCH 06/12] Update index.js Co-authored-by: Luigi Pinca --- index.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/index.js b/index.js index c8751201..e35f4f51 100644 --- a/index.js +++ b/index.js @@ -158,10 +158,6 @@ Shopify.prototype.request = function request(uri, method, key, data, headers) { method }; - if (this.options.agent) { - options.agent = { ...this.options.agent }; - } - const afterResponse = (res) => { this.updateLimits(res.headers['x-shopify-shop-api-call-limit']); return res; From a6661af04eb32b82b42fdb8f440257f64de7741e Mon Sep 17 00:00:00 2001 From: Youkehai <42542308+Youkehai@users.noreply.github.com> Date: Thu, 11 Jan 2024 09:05:50 +0800 Subject: [PATCH 07/12] Update index.js Co-authored-by: Luigi Pinca --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index e35f4f51..15e4ed5f 100644 --- a/index.js +++ b/index.js @@ -283,6 +283,7 @@ Shopify.prototype.graphql = function graphql(data, variables) { timeout: this.options.timeout, responseType: 'json', method: 'POST', + agent: this.options.agent, body: json ? this.options.stringifyJson({ query: data, variables }) : data }; From 5a32a33a641104ba4109fd767d5e6ab066553aa7 Mon Sep 17 00:00:00 2001 From: Youkehai <42542308+Youkehai@users.noreply.github.com> Date: Thu, 11 Jan 2024 09:05:57 +0800 Subject: [PATCH 08/12] Update index.js Co-authored-by: Luigi Pinca --- index.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/index.js b/index.js index 15e4ed5f..7e2204c2 100644 --- a/index.js +++ b/index.js @@ -287,10 +287,6 @@ Shopify.prototype.graphql = function graphql(data, variables) { body: json ? this.options.stringifyJson({ query: data, variables }) : data }; - if (this.options.agent) { - options.agent = { ...this.options.agent }; - } - const afterResponse = (res) => { if (res.body) { if (res.body.extensions && res.body.extensions.cost) { From 9f7258664c29f9e1211f19436093b73bf8dbb944 Mon Sep 17 00:00:00 2001 From: Youkehai <42542308+Youkehai@users.noreply.github.com> Date: Thu, 11 Jan 2024 09:06:03 +0800 Subject: [PATCH 09/12] Update types/index.d.ts Co-authored-by: Luigi Pinca --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index 5252fee8..b667e05f 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,7 +1,7 @@ // Type definitions for shopify-api-node // Project: shopify-api-node // Definitions by: Rich Buggy -import { Hooks,Agents } from 'got'; +import { Hooks, Agents } from 'got'; /*~ This is the module template file for class modules. *~ You should rename it to index.d.ts and place it in a folder with the same name as the module. From b2f5e1ae4dbf4f619b6a2805d26db8761ed5dfd6 Mon Sep 17 00:00:00 2001 From: Youkehai <42542308+Youkehai@users.noreply.github.com> Date: Thu, 11 Jan 2024 09:06:09 +0800 Subject: [PATCH 10/12] Update types/index.d.ts Co-authored-by: Luigi Pinca --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index b667e05f..25f352da 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -789,7 +789,7 @@ declare namespace Shopify { shopName: string; timeout?: number; hooks?: Hooks; - agent?:Agents; + agent?: Agents; } export interface IPrivateShopifyConfig { From 391387b2ab76d3b83be844bfe46b87b4fcb9ea85 Mon Sep 17 00:00:00 2001 From: Youkehai <42542308+Youkehai@users.noreply.github.com> Date: Thu, 11 Jan 2024 09:06:15 +0800 Subject: [PATCH 11/12] Update types/index.d.ts Co-authored-by: Luigi Pinca --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index 25f352da..92f81a18 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -802,7 +802,7 @@ declare namespace Shopify { shopName: string; timeout?: number; hooks?: Hooks; - agent?:Agents; + agent?: Agents; } export interface ICallLimits { From 814a74acca75f1966ba73a1d821838d52d2cc42e Mon Sep 17 00:00:00 2001 From: Youkehai <42542308+Youkehai@users.noreply.github.com> Date: Thu, 11 Jan 2024 09:06:20 +0800 Subject: [PATCH 12/12] Update types/index.test-d.ts Co-authored-by: Luigi Pinca --- types/index.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 4081a4e5..cb49e076 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -27,7 +27,7 @@ new Shopify({ } }); -// Can be constructed with Got proxy. +// Accepts the `agent` option. new Shopify({ shopName: 'my-shopify-store.myshopify.com', accessToken: '111',