Skip to content

Commit

Permalink
make jwt-simple a peer dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
brianzinn committed Oct 7, 2020
1 parent 968ccff commit 04e38c3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"tslint-config-standard": "^8.0.1",
"typescript": "^3.0.3"
},
"dependencies": {
"jwt-simple": "^0.5.6"
"peerDependencies": {
"jwt-simple": "*"
}
}
2 changes: 1 addition & 1 deletion rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default {
{ file: pkg.module, format: 'es', sourcemap: true },
],
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
external: [],
external: ['jwt-simple'],
watch: {
include: 'src/**',
},
Expand Down
30 changes: 17 additions & 13 deletions src/snowflake-ingest-node.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as crypto from 'crypto';
import { ClientRequest, IncomingMessage } from 'http';
import https, { RequestOptions } from 'https' // chosen over ie: axios to not bring in extra dep.
import jwt from 'jwt-simple';
import * as https from 'https';
import * as jwt from 'jwt-simple';

const USER_AGENT = 'snowpipe-ingest-node/0.0.1/node/npm';

Expand All @@ -16,7 +16,7 @@ export type RecordedCallResponse = {
}

export type RecordedCall = {
request: RequestOptions
request: https.RequestOptions
response: RecordedCallResponse
}

Expand Down Expand Up @@ -70,11 +70,15 @@ export const createSnowpipeAPI = (username: string, privateKey: string, account:
[EXPIRY_TIME]: Math.round(new Date().getTime() / 1000 + 60 * 59)
}

if (jwt === undefined) {
console.error('"jwt-simple" not found (make sure to include this peer dependency in your project).')
}

const bearer = jwt.encode(payload, privateKey, 'RS256');
return bearer;
}

const makeRequest = async (options: RequestOptions, endpointCallHistory: RecordedCall[], postBody?: string): Promise<string> => {
const makeRequest = async (options: https.RequestOptions, endpointCallHistory: RecordedCall[], postBody?: string): Promise<string> => {
return new Promise<string>((resolve, reject) => {
const req: ClientRequest = https.request(
options,
Expand Down Expand Up @@ -145,17 +149,17 @@ export const createSnowpipeAPI = (username: string, privateKey: string, account:

const path = `/v1/data/pipes/${pipeName}/insertFiles?requestId=${getRequestId()}`;

const jwt_token: string = await getBearerToken();
const jwtToken: string = await getBearerToken();

const options = {
const options: https.RequestOptions = {
hostname: config.hostname,
port: 443,
path,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': postBody.length,
'Authorization': `Bearer ${jwt_token}`,
'Authorization': `Bearer ${jwtToken}`,
'User-Agent': USER_AGENT,
Accept: 'application/json'
}
Expand All @@ -173,15 +177,15 @@ export const createSnowpipeAPI = (username: string, privateKey: string, account:
path += `&beginMark=${beginMark}`;
}

const jwt_token: string = await getBearerToken();
const jwtToken: string = await getBearerToken();

const options = {
const options: https.RequestOptions = {
hostname: config.hostname,
port: 443,
path,
method: 'GET',
headers: {
'Authorization': `Bearer ${jwt_token}`,
'Authorization': `Bearer ${jwtToken}`,
'User-Agent': USER_AGENT,
Accept: 'application/json'
}
Expand All @@ -202,15 +206,15 @@ export const createSnowpipeAPI = (username: string, privateKey: string, account:
path += `&endTimeExclusive=${endTimeExclusive}`;
}

const jwt_token: string = await getBearerToken();
const jwtToken: string = await getBearerToken();

const options = {
const options: https.RequestOptions = {
hostname: config.hostname,
port: 443,
path,
method: 'GET',
headers: {
'Authorization': `Bearer ${jwt_token}`,
'Authorization': `Bearer ${jwtToken}`,
'User-Agent': USER_AGENT,
Accept: 'application/json'
}
Expand Down

0 comments on commit 04e38c3

Please sign in to comment.