@metriport/shared 0.9.7 → 0.9.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/common/__tests__/retry.test.js +190 -19
- package/dist/common/__tests__/retry.test.js.map +1 -1
- package/dist/common/array.d.ts +1 -1
- package/dist/common/array.d.ts.map +1 -1
- package/dist/common/array.js +3 -0
- package/dist/common/array.js.map +1 -1
- package/dist/common/file-downloader.d.ts +14 -0
- package/dist/common/file-downloader.d.ts.map +1 -0
- package/dist/common/file-downloader.js +63 -0
- package/dist/common/file-downloader.js.map +1 -0
- package/dist/common/http.d.ts +42 -0
- package/dist/common/http.d.ts.map +1 -0
- package/dist/common/http.js +83 -0
- package/dist/common/http.js.map +1 -0
- package/dist/common/retry-with-backoff.d.ts +27 -0
- package/dist/common/retry-with-backoff.d.ts.map +1 -0
- package/dist/common/retry-with-backoff.js +47 -0
- package/dist/common/retry-with-backoff.js.map +1 -0
- package/dist/common/retry.d.ts +49 -26
- package/dist/common/retry.d.ts.map +1 -1
- package/dist/common/retry.js +85 -34
- package/dist/common/retry.js.map +1 -1
- package/dist/common/stream.d.ts +6 -0
- package/dist/common/stream.d.ts.map +1 -0
- package/dist/common/stream.js +18 -0
- package/dist/common/stream.js.map +1 -0
- package/dist/common/uuid.d.ts +2 -0
- package/dist/common/uuid.d.ts.map +1 -0
- package/dist/common/uuid.js +5 -0
- package/dist/common/uuid.js.map +1 -0
- package/dist/error/shared.d.ts +1 -1
- package/dist/error/shared.d.ts.map +1 -1
- package/dist/error/shared.js +7 -2
- package/dist/error/shared.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/net/__tests__/another.d.ts +1 -0
- package/dist/net/__tests__/another.d.ts.map +1 -0
- package/dist/net/__tests__/another.js +2 -0
- package/dist/net/__tests__/another.js.map +1 -0
- package/dist/net/__tests__/http.test.d.ts +2 -0
- package/dist/net/__tests__/http.test.d.ts.map +1 -0
- package/dist/net/__tests__/http.test.js +102 -0
- package/dist/net/__tests__/http.test.js.map +1 -0
- package/dist/net/__tests__/retry.test.d.ts +2 -0
- package/dist/net/__tests__/retry.test.d.ts.map +1 -0
- package/dist/net/__tests__/retry.test.js +158 -0
- package/dist/net/__tests__/retry.test.js.map +1 -0
- package/dist/net/error.d.ts +19 -0
- package/dist/net/error.d.ts.map +1 -0
- package/dist/net/error.js +49 -0
- package/dist/net/error.js.map +1 -0
- package/dist/net/net.d.ts +2 -0
- package/dist/net/net.d.ts.map +1 -0
- package/dist/net/net.js +16 -0
- package/dist/net/net.js.map +1 -0
- package/dist/net/retry-with-backoff.d.ts +42 -0
- package/dist/net/retry-with-backoff.d.ts.map +1 -0
- package/dist/net/retry-with-backoff.js +83 -0
- package/dist/net/retry-with-backoff.js.map +1 -0
- package/dist/net/retry.d.ts +28 -0
- package/dist/net/retry.d.ts.map +1 -0
- package/dist/net/retry.js +57 -0
- package/dist/net/retry.js.map +1 -0
- package/dist/net/stream.d.ts +6 -0
- package/dist/net/stream.d.ts.map +1 -0
- package/dist/net/stream.js +18 -0
- package/dist/net/stream.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-empty-function */
|
|
4
|
+
const faker_1 = require("@faker-js/faker");
|
|
5
|
+
const axios_1 = require("axios");
|
|
6
|
+
const retry_1 = require("../retry");
|
|
7
|
+
describe("executeWithNetworkRetries", () => {
|
|
8
|
+
const fn = jest.fn();
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
jest.resetAllMocks();
|
|
11
|
+
});
|
|
12
|
+
it("returns when no error", async () => {
|
|
13
|
+
const expectedResult = faker_1.faker.lorem.word();
|
|
14
|
+
fn.mockImplementation(() => expectedResult);
|
|
15
|
+
const resp = await (0, retry_1.executeWithNetworkRetries)(fn, {
|
|
16
|
+
initialDelay: 1,
|
|
17
|
+
maxAttempts: 2,
|
|
18
|
+
});
|
|
19
|
+
expect(resp).toEqual(expectedResult);
|
|
20
|
+
});
|
|
21
|
+
it("retries on ECONNREFUSED", async () => {
|
|
22
|
+
fn.mockImplementation(() => {
|
|
23
|
+
const error = new axios_1.AxiosError("mock error");
|
|
24
|
+
error.code = "ECONNREFUSED";
|
|
25
|
+
throw error;
|
|
26
|
+
});
|
|
27
|
+
await expect(async () => (0, retry_1.executeWithNetworkRetries)(fn, {
|
|
28
|
+
initialDelay: 1,
|
|
29
|
+
maxAttempts: 2,
|
|
30
|
+
})).rejects.toThrow();
|
|
31
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
32
|
+
});
|
|
33
|
+
it("retries on ECONNRESET", async () => {
|
|
34
|
+
fn.mockImplementation(() => {
|
|
35
|
+
const error = new axios_1.AxiosError("mock error");
|
|
36
|
+
error.code = "ECONNRESET";
|
|
37
|
+
throw error;
|
|
38
|
+
});
|
|
39
|
+
await expect(async () => (0, retry_1.executeWithNetworkRetries)(fn, {
|
|
40
|
+
initialDelay: 1,
|
|
41
|
+
maxAttempts: 2,
|
|
42
|
+
})).rejects.toThrow();
|
|
43
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
44
|
+
});
|
|
45
|
+
it("does not retry on AxiosError.ETIMEDOUT", async () => {
|
|
46
|
+
fn.mockImplementation(() => {
|
|
47
|
+
const error = new axios_1.AxiosError("mock error");
|
|
48
|
+
error.code = "ETIMEDOUT";
|
|
49
|
+
throw error;
|
|
50
|
+
});
|
|
51
|
+
await expect(async () => (0, retry_1.executeWithNetworkRetries)(fn, {
|
|
52
|
+
initialDelay: 1,
|
|
53
|
+
maxAttempts: 2,
|
|
54
|
+
})).rejects.toThrow();
|
|
55
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
56
|
+
});
|
|
57
|
+
it("does not retry on AxiosError.ECONNABORTED", async () => {
|
|
58
|
+
fn.mockImplementation(() => {
|
|
59
|
+
const error = new axios_1.AxiosError("mock error");
|
|
60
|
+
error.code = "ECONNABORTED";
|
|
61
|
+
throw error;
|
|
62
|
+
});
|
|
63
|
+
await expect(async () => (0, retry_1.executeWithNetworkRetries)(fn, {
|
|
64
|
+
initialDelay: 1,
|
|
65
|
+
maxAttempts: 2,
|
|
66
|
+
})).rejects.toThrow();
|
|
67
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
68
|
+
});
|
|
69
|
+
it("does not retry on ECONNREFUSED when gets array of status without ECONNREFUSED", async () => {
|
|
70
|
+
fn.mockImplementation(() => {
|
|
71
|
+
const error = new axios_1.AxiosError("mock error");
|
|
72
|
+
error.code = "ECONNREFUSED";
|
|
73
|
+
throw error;
|
|
74
|
+
});
|
|
75
|
+
await expect(async () => (0, retry_1.executeWithNetworkRetries)(fn, {
|
|
76
|
+
initialDelay: 1,
|
|
77
|
+
maxAttempts: 2,
|
|
78
|
+
httpCodesToRetry: ["ECONNRESET"],
|
|
79
|
+
})).rejects.toThrow();
|
|
80
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
81
|
+
});
|
|
82
|
+
it("does not retry on ECONNRESET when gets array of status without ECONNRESET", async () => {
|
|
83
|
+
fn.mockImplementation(() => {
|
|
84
|
+
const error = new axios_1.AxiosError("mock error");
|
|
85
|
+
error.code = "ECONNRESET";
|
|
86
|
+
throw error;
|
|
87
|
+
});
|
|
88
|
+
await expect(async () => (0, retry_1.executeWithNetworkRetries)(fn, {
|
|
89
|
+
initialDelay: 1,
|
|
90
|
+
maxAttempts: 2,
|
|
91
|
+
httpCodesToRetry: ["ECONNREFUSED"],
|
|
92
|
+
})).rejects.toThrow();
|
|
93
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
94
|
+
});
|
|
95
|
+
it("does not retry when error is not AxiosError", async () => {
|
|
96
|
+
fn.mockImplementation(() => {
|
|
97
|
+
throw new Error("mock error");
|
|
98
|
+
});
|
|
99
|
+
await expect(async () => (0, retry_1.executeWithNetworkRetries)(fn, {
|
|
100
|
+
initialDelay: 1,
|
|
101
|
+
maxAttempts: 2,
|
|
102
|
+
})).rejects.toThrow();
|
|
103
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
104
|
+
});
|
|
105
|
+
it("does not retry when retryOnTimeout is false and error is timeout", async () => {
|
|
106
|
+
fn.mockImplementation(() => {
|
|
107
|
+
const error = new axios_1.AxiosError("mock error");
|
|
108
|
+
error.code = axios_1.AxiosError.ETIMEDOUT;
|
|
109
|
+
throw error;
|
|
110
|
+
});
|
|
111
|
+
await expect(async () => (0, retry_1.executeWithNetworkRetries)(fn, {
|
|
112
|
+
initialDelay: 1,
|
|
113
|
+
maxAttempts: 2,
|
|
114
|
+
retryOnTimeout: false,
|
|
115
|
+
})).rejects.toThrow();
|
|
116
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
117
|
+
});
|
|
118
|
+
it("does not retry when retryOnTimeout is empty and error is timeout", async () => {
|
|
119
|
+
fn.mockImplementation(() => {
|
|
120
|
+
const error = new axios_1.AxiosError("mock error");
|
|
121
|
+
error.code = axios_1.AxiosError.ETIMEDOUT;
|
|
122
|
+
throw error;
|
|
123
|
+
});
|
|
124
|
+
await expect(async () => (0, retry_1.executeWithNetworkRetries)(fn, {
|
|
125
|
+
initialDelay: 1,
|
|
126
|
+
maxAttempts: 2,
|
|
127
|
+
})).rejects.toThrow();
|
|
128
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
129
|
+
});
|
|
130
|
+
it("retries when retryOnTimeout is true and error is timeout", async () => {
|
|
131
|
+
fn.mockImplementation(() => {
|
|
132
|
+
const error = new axios_1.AxiosError("mock error");
|
|
133
|
+
error.code = axios_1.AxiosError.ETIMEDOUT;
|
|
134
|
+
throw error;
|
|
135
|
+
});
|
|
136
|
+
await expect(async () => (0, retry_1.executeWithNetworkRetries)(fn, {
|
|
137
|
+
initialDelay: 1,
|
|
138
|
+
maxAttempts: 2,
|
|
139
|
+
retryOnTimeout: true,
|
|
140
|
+
})).rejects.toThrow();
|
|
141
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
142
|
+
});
|
|
143
|
+
it("retries when retryOnTimeout is false and error is timeout and httpCodesToRetry contains timeout code", async () => {
|
|
144
|
+
fn.mockImplementation(() => {
|
|
145
|
+
const error = new axios_1.AxiosError("mock error");
|
|
146
|
+
error.code = axios_1.AxiosError.ETIMEDOUT;
|
|
147
|
+
throw error;
|
|
148
|
+
});
|
|
149
|
+
await expect(async () => (0, retry_1.executeWithNetworkRetries)(fn, {
|
|
150
|
+
initialDelay: 1,
|
|
151
|
+
maxAttempts: 2,
|
|
152
|
+
retryOnTimeout: false,
|
|
153
|
+
httpCodesToRetry: [axios_1.AxiosError.ETIMEDOUT],
|
|
154
|
+
})).rejects.toThrow();
|
|
155
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
//# sourceMappingURL=retry.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry.test.js","sourceRoot":"","sources":["../../../src/net/__tests__/retry.test.ts"],"names":[],"mappings":";;AAAA,yDAAyD;AACzD,2CAAwC;AACxC,iCAAmC;AACnC,oCAAqD;AAErD,QAAQ,CAAC,2BAA2B,EAAE,GAAG,EAAE;IACzC,MAAM,EAAE,GAAG,IAAI,CAAC,EAAE,EAAE,CAAC;IACrB,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,uBAAuB,EAAE,KAAK,IAAI,EAAE;QACrC,MAAM,cAAc,GAAG,aAAK,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;QAC1C,EAAE,CAAC,kBAAkB,CAAC,GAAG,EAAE,CAAC,cAAc,CAAC,CAAC;QAC5C,MAAM,IAAI,GAAG,MAAM,IAAA,iCAAyB,EAAC,EAAE,EAAE;YAC/C,YAAY,EAAE,CA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ErrorCodes)[number];
|
|
3
|
+
export declare const nodeTimeoutErrorCodes: string[];
|
|
4
|
+
export type NodeTimeoutNetworkError = (typeof nodeTimeoutErrorCodes)[number];
|
|
5
|
+
export type NodeNetworkError = NodeTimeoutNetworkError | NodeConnRefusedNetworkError;
|
|
6
|
+
export declare const axiosTimeoutErrorCodes: string[];
|
|
7
|
+
export type AxiosTimeoutError = (typeof axiosTimeoutErrorCodes)[number];
|
|
8
|
+
export declare const axiosResponseErrorCodes: string[];
|
|
9
|
+
export type AxiosResponseError = (typeof axiosResponseErrorCodes)[number];
|
|
10
|
+
export type AxiosNetworkError = AxiosTimeoutError | AxiosResponseError;
|
|
11
|
+
export declare const networkTimeoutErrors: string[];
|
|
12
|
+
export type NetworkTimeoutError = (typeof networkTimeoutErrors)[number];
|
|
13
|
+
export type NetworkError = NodeNetworkError | AxiosNetworkError;
|
|
14
|
+
export declare function getNetworkErrorDetails(error: unknown): {
|
|
15
|
+
details: string;
|
|
16
|
+
code: string | undefined;
|
|
17
|
+
status?: number | undefined;
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/net/error.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,yBAAyB,UAAiC,CAAC;AACxE,MAAM,MAAM,2BAA2B,GAAG,CAAC,OAAO,yBAAyB,CAAC,CAAC,MAAM,CAAC,CAAC;AAErF,eAAO,MAAM,qBAAqB,UAAgB,CAAC;AACnD,MAAM,MAAM,uBAAuB,GAAG,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,CAAC,CAAC;AAE7E,MAAM,MAAM,gBAAgB,GAAG,uBAAuB,GAAG,2BAA2B,CAAC;AAIrF,eAAO,MAAM,sBAAsB,UAAkD,CAAC;AACtF,MAAM,MAAM,iBAAiB,GAAG,CAAC,OAAO,sBAAsB,CAAC,CAAC,MAAM,CAAC,CAAC;AAExE,eAAO,MAAM,uBAAuB,UAAgC,CAAC;AACrE,MAAM,MAAM,kBAAkB,GAAG,CAAC,OAAO,uBAAuB,CAAC,CAAC,MAAM,CAAC,CAAC;AAE1E,MAAM,MAAM,iBAAiB,GAAG,iBAAiB,GAAG,kBAAkB,CAAC;AAIvE,eAAO,MAAM,oBAAoB,UAAwD,CAAC;AAC1F,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,oBAAoB,CAAC,CAAC,MAAM,CAAC,CAAC;AAExE,MAAM,MAAM,YAAY,GAAG,gBAAgB,GAAG,iBAAiB,CAAC;AAEhE,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG;IACtD,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC7B,CAUA"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.getNetworkErrorDetails = exports.networkTimeoutErrors = exports.axiosResponseErrorCodes = exports.axiosTimeoutErrorCodes = exports.nodeTimeoutErrorCodes = exports.nodeConnRefusedErrorCodes = void 0;
|
|
27
|
+
const axios_1 = __importStar(require("axios"));
|
|
28
|
+
const shared_1 = require("../error/shared");
|
|
29
|
+
// https://nodejs.org/docs/latest-v18.x/api/errors.html#common-system-errors
|
|
30
|
+
exports.nodeConnRefusedErrorCodes = ["ECONNREFUSED", "ECONNRESET"];
|
|
31
|
+
exports.nodeTimeoutErrorCodes = ["ETIMEDOUT"];
|
|
32
|
+
// Axios error codes that are timeout errors
|
|
33
|
+
exports.axiosTimeoutErrorCodes = [axios_1.AxiosError.ECONNABORTED, axios_1.AxiosError.ETIMEDOUT];
|
|
34
|
+
exports.axiosResponseErrorCodes = [axios_1.AxiosError.ERR_BAD_RESPONSE];
|
|
35
|
+
// General Network errors
|
|
36
|
+
exports.networkTimeoutErrors = [...exports.nodeTimeoutErrorCodes, ...exports.axiosTimeoutErrorCodes];
|
|
37
|
+
function getNetworkErrorDetails(error) {
|
|
38
|
+
const details = (0, shared_1.errorToString)(error);
|
|
39
|
+
if (axios_1.default.isAxiosError(error)) {
|
|
40
|
+
return {
|
|
41
|
+
details,
|
|
42
|
+
code: error.code,
|
|
43
|
+
status: error.response?.status,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return { details, code: undefined, status: undefined };
|
|
47
|
+
}
|
|
48
|
+
exports.getNetworkErrorDetails = getNetworkErrorDetails;
|
|
49
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../src/net/error.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAA0C;AAC1C,4CAAgD;AAEhD,4EAA4E;AAC/D,QAAA,yBAAyB,GAAG,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;AAG3D,QAAA,qBAAqB,GAAG,CAAC,WAAW,CAAC,CAAC;AAKnD,4CAA4C;AAE/B,QAAA,sBAAsB,GAAG,CAAC,kBAAU,CAAC,YAAY,EAAE,kBAAU,CAAC,SAAS,CAAC,CAAC;AAGzE,QAAA,uBAAuB,GAAG,CAAC,kBAAU,CAAC,gBAAgB,CAAC,CAAC;AAKrE,yBAAyB;AAEZ,QAAA,oBAAoB,GAAG,CAAC,GAAG,6BAAqB,EAAE,GAAG,8BAAsB,CAAC,CAAC;AAK1F,SAAgB,sBAAsB,CAAC,KAAc;IAKnD,MAAM,OAAO,GAAG,IAAA,sBAAa,EAAC,KAAK,CAAC,CAAC;IACrC,IAAI,eAAK,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE;QAC7B,OAAO;YACL,OAAO;YACP,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,MAAM,EAAE,KAAK,CAAC,QAAQ,EAAE,MAAM;SAC/B,CAAC;KACH;IACD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;AACzD,CAAC;AAdD,wDAcC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"net.d.ts","sourceRoot":"","sources":["../../src/net/net.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAQ3D"}
|
package/dist/net/net.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isValidUrl = void 0;
|
|
4
|
+
function isValidUrl(url) {
|
|
5
|
+
if (!url)
|
|
6
|
+
return false;
|
|
7
|
+
try {
|
|
8
|
+
new URL(url);
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
catch (e) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.isValidUrl = isValidUrl;
|
|
16
|
+
//# sourceMappingURL=net.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"net.js","sourceRoot":"","sources":["../../src/net/net.ts"],"names":[],"mappings":";;;AAAA,SAAgB,UAAU,CAAC,GAAuB;IAChD,IAAI,CAAC,GAAG;QAAE,OAAO,KAAK,CAAC;IACvB,IAAI;QACF,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QACb,OAAO,IAAI,CAAC;KACb;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AARD,gCAQC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export type ExecuteWithRetriesOptions = {
|
|
2
|
+
/** The intitial delay in milliseconds. Defaults to 100ms. */
|
|
3
|
+
initialDelay: number;
|
|
4
|
+
/** The maximum delay in milliseconds, optional. Defaults to Infinity. */
|
|
5
|
+
maxDelay: number;
|
|
6
|
+
/**
|
|
7
|
+
* Determines how the backoff is determined.
|
|
8
|
+
* It's unlikely that you would change that.
|
|
9
|
+
* Defaults to 2 (each retry waits double of what the previous one did).
|
|
10
|
+
*/
|
|
11
|
+
backoffMultiplier: number;
|
|
12
|
+
/** The maximum number of retries. Defaults to 10. */
|
|
13
|
+
maxAttempts: number;
|
|
14
|
+
shouldRetry: (error: unknown, attempt: number) => boolean | Promise<boolean>;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* TODO WIP
|
|
18
|
+
* TODO WIP
|
|
19
|
+
* TODO WIP
|
|
20
|
+
* TODO WIP
|
|
21
|
+
*
|
|
22
|
+
* @param fn
|
|
23
|
+
* @param options
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
export declare function executeWithRetries<T>(fn: () => Promise<T>, options?: Partial<ExecuteWithRetriesOptions>): Promise<T>;
|
|
27
|
+
export type ExecuteWithHttpRetriesOptions = ExecuteWithRetriesOptions & {
|
|
28
|
+
/** The HTTP status codes to retry. See `defaultOptions` for defaults. */
|
|
29
|
+
httpStatusToRetry: number[];
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* TODO WIP
|
|
33
|
+
* TODO WIP
|
|
34
|
+
* TODO WIP
|
|
35
|
+
* TODO WIP
|
|
36
|
+
*
|
|
37
|
+
* @param fn
|
|
38
|
+
* @param options
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
41
|
+
export declare function executeWithHttpRetries<T>(fn: () => Promise<T>, options?: Partial<ExecuteWithHttpRetriesOptions>): Promise<T>;
|
|
42
|
+
//# sourceMappingURL=retry-with-backoff.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry-with-backoff.d.ts","sourceRoot":"","sources":["../../src/net/retry-with-backoff.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,yBAAyB,GAAG;IACtC,6DAA6D;IAC7D,YAAY,EAAE,MAAM,CAAC;IACrB,yEAAyE;IACzE,QAAQ,EAAE,MAAM,CAAC;IACjB;;;;OAIG;IACH,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qDAAqD;IACrD,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC9E,CAAC;AAUF;;;;;;;;;GASG;AACH,wBAAsB,kBAAkB,CAAC,CAAC,EACxC,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,GAAE,OAAO,CAAC,yBAAyB,CAAkB,GAC3D,OAAO,CAAC,CAAC,CAAC,CA2BZ;AAED,MAAM,MAAM,6BAA6B,GAAG,yBAAyB,GAAG;IACtE,yEAAyE;IACzE,iBAAiB,EAAE,MAAM,EAAE,CAAC;CAC7B,CAAC;AAYF;;;;;;;;;GASG;AACH,wBAAsB,sBAAsB,CAAC,CAAC,EAC5C,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,GAAE,OAAO,CAAC,6BAA6B,CAAkB,GAC/D,OAAO,CAAC,CAAC,CAAC,CAWZ"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.executeWithHttpRetries = exports.executeWithRetries = void 0;
|
|
7
|
+
const shared_1 = require("@metriport/shared");
|
|
8
|
+
const axios_1 = __importDefault(require("axios"));
|
|
9
|
+
const http_status_1 = __importDefault(require("http-status"));
|
|
10
|
+
const lodash_1 = require("lodash");
|
|
11
|
+
const defaultOptions = {
|
|
12
|
+
initialDelay: 100,
|
|
13
|
+
maxDelay: Infinity,
|
|
14
|
+
backoffMultiplier: 2,
|
|
15
|
+
maxAttempts: 10,
|
|
16
|
+
shouldRetry: (_error, _attempt) => true,
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* TODO WIP
|
|
20
|
+
* TODO WIP
|
|
21
|
+
* TODO WIP
|
|
22
|
+
* TODO WIP
|
|
23
|
+
*
|
|
24
|
+
* @param fn
|
|
25
|
+
* @param options
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
async function executeWithRetries(fn, options = defaultOptions) {
|
|
29
|
+
const actualOptions = { ...defaultOptions, ...options };
|
|
30
|
+
const { initialDelay, maxDelay, backoffMultiplier, maxAttempts: _maxAttempts, shouldRetry, } = actualOptions;
|
|
31
|
+
const maxAttempts = Math.max(_maxAttempts, 1);
|
|
32
|
+
let attempt = 0;
|
|
33
|
+
while (++attempt <= maxAttempts + 1) {
|
|
34
|
+
try {
|
|
35
|
+
return await fn();
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
if (attempt >= maxAttempts) {
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
if (!(await shouldRetry(error, attempt))) {
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
const temp = Math.min(initialDelay * backoffMultiplier * attempt, maxDelay);
|
|
45
|
+
const timeToWait = temp / 2 + (0, lodash_1.random)(0, temp / 2);
|
|
46
|
+
await (0, shared_1.sleep)(timeToWait);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
throw new shared_1.MetriportError("Unreachable code", undefined, { attempt, maxRetries: maxAttempts });
|
|
50
|
+
}
|
|
51
|
+
exports.executeWithRetries = executeWithRetries;
|
|
52
|
+
const defaultHttpRetryOptions = {
|
|
53
|
+
...defaultOptions,
|
|
54
|
+
httpStatusToRetry: [
|
|
55
|
+
http_status_1.default.INTERNAL_SERVER_ERROR,
|
|
56
|
+
http_status_1.default.BAD_GATEWAY,
|
|
57
|
+
http_status_1.default.SERVICE_UNAVAILABLE,
|
|
58
|
+
http_status_1.default.GATEWAY_TIMEOUT,
|
|
59
|
+
],
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* TODO WIP
|
|
63
|
+
* TODO WIP
|
|
64
|
+
* TODO WIP
|
|
65
|
+
* TODO WIP
|
|
66
|
+
*
|
|
67
|
+
* @param fn
|
|
68
|
+
* @param options
|
|
69
|
+
* @returns
|
|
70
|
+
*/
|
|
71
|
+
async function executeWithHttpRetries(fn, options = defaultOptions) {
|
|
72
|
+
const actualOptions = { ...defaultHttpRetryOptions, ...options };
|
|
73
|
+
const { httpStatusToRetry } = actualOptions;
|
|
74
|
+
return executeWithRetries(fn, {
|
|
75
|
+
...actualOptions,
|
|
76
|
+
shouldRetry: (error, _attempt) => {
|
|
77
|
+
const responseStatus = axios_1.default.isAxiosError(error) && error.response?.status ? error.response.status : 500;
|
|
78
|
+
return httpStatusToRetry.includes(responseStatus);
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
exports.executeWithHttpRetries = executeWithHttpRetries;
|
|
83
|
+
//# sourceMappingURL=retry-with-backoff.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry-with-backoff.js","sourceRoot":"","sources":["../../src/net/retry-with-backoff.ts"],"names":[],"mappings":";;;;;;AAAA,8CAA0D;AAC1D,kDAA0B;AAC1B,8DAAqC;AACrC,mCAAgC;AAkBhC,MAAM,cAAc,GAA8B;IAChD,YAAY,EAAE,GAAG;IACjB,QAAQ,EAAE,QAAQ;IAClB,iBAAiB,EAAE,CAAC;IACpB,WAAW,EAAE,EAAE;IACf,WAAW,EAAE,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE,CAAC,IAAI;CACxC,CAAC;AAEF;;;;;;;;;GASG;AACI,KAAK,UAAU,kBAAkB,CACtC,EAAoB,EACpB,UAA8C,cAAc;IAE5D,MAAM,aAAa,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,OAAO,EAAE,CAAC;IACxD,MAAM,EACJ,YAAY,EACZ,QAAQ,EACR,iBAAiB,EACjB,WAAW,EAAE,YAAY,EACzB,WAAW,GACZ,GAAG,aAAa,CAAC;IAClB,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC;IAC9C,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,OAAO,EAAE,OAAO,IAAI,WAAW,GAAG,CAAC,EAAE;QACnC,IAAI;YACF,OAAO,MAAM,EAAE,EAAE,CAAC;SACnB;QAAC,OAAO,KAAU,EAAE;YACnB,IAAI,OAAO,IAAI,WAAW,EAAE;gBAC1B,MAAM,KAAK,CAAC;aACb;YACD,IAAI,CAAC,CAAC,MAAM,WAAW,CAAC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CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAdD,wDAcC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ExecuteWithRetriesOptions } from "../common/retry";
|
|
2
|
+
import { NetworkError } from "./error";
|
|
3
|
+
export type ExecuteWithNetworkRetriesOptions = Omit<ExecuteWithRetriesOptions<unknown>, "shouldRetry"> & {
|
|
4
|
+
/** The network error codes to retry. See `defaultOptions` for defaults. */
|
|
5
|
+
httpCodesToRetry: NetworkError[];
|
|
6
|
+
httpStatusCodesToRetry: number[];
|
|
7
|
+
/** Whether to retry on timeout errors. Default is false. */
|
|
8
|
+
retryOnTimeout?: boolean;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Executes a function with retries and backoff with jitter. If the function throws a network
|
|
12
|
+
* error, it will retry up to maxAttempts-1, waiting between each retry.
|
|
13
|
+
* If the function throws an error on the last attempt, it will throw the error.
|
|
14
|
+
* If the function succeeds, it will return the result.
|
|
15
|
+
* This is a specialization of `executeWithRetries` for network errors.
|
|
16
|
+
* By default it retries on ECONNREFUSED and ECONNRESET (customize the errors to retry
|
|
17
|
+
* setting the option `httpCodesToRetry`).
|
|
18
|
+
* By default it also retries on HTTP status code 429 (Too Many Requests).
|
|
19
|
+
*
|
|
20
|
+
* @param fn the function to be executed
|
|
21
|
+
* @param options the options to be used; see `ExecuteWithHttpRetriesOptions` for components and
|
|
22
|
+
* `defaultOptions` for defaults.
|
|
23
|
+
* @returns the result of calling the `fn` function
|
|
24
|
+
* @see `executeWithRetries()`
|
|
25
|
+
*
|
|
26
|
+
*/
|
|
27
|
+
export declare function executeWithNetworkRetries<T>(fn: () => Promise<T>, options?: Partial<ExecuteWithNetworkRetriesOptions>): Promise<T>;
|
|
28
|
+
//# sourceMappingURL=retry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../src/net/retry.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,yBAAyB,EAC1B,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,YAAY,EAAwB,MAAM,SAAS,CAAC;AAE7D,MAAM,MAAM,gCAAgC,GAAG,IAAI,CACjD,yBAAyB,CAAC,OAAO,CAAC,EAClC,aAAa,CACd,GAAG;IACF,2EAA2E;IAC3E,gBAAgB,EAAE,YAAY,EAAE,CAAC;IACjC,sBAAsB,EAAE,MAAM,EAAE,CAAC;IACjC,4DAA4D;IAC5D,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B,CAAC;AAcF;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,yBAAyB,CAAC,CAAC,EAC/C,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,CAAC,EAAE,OAAO,CAAC,gCAAgC,CAAC,GAClD,OAAO,CAAC,CAAC,CAAC,CAuBZ"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.executeWithNetworkRetries = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const retry_1 = require("../common/retry");
|
|
9
|
+
const error_1 = require("./error");
|
|
10
|
+
const defaultOptions = {
|
|
11
|
+
...retry_1.defaultOptions,
|
|
12
|
+
initialDelay: 1000,
|
|
13
|
+
httpCodesToRetry: [
|
|
14
|
+
// https://nodejs.org/docs/latest-v18.x/api/errors.html#common-system-errors
|
|
15
|
+
"ECONNREFUSED",
|
|
16
|
+
"ECONNRESET", // (Connection reset by peer): A connection was forcibly closed by a peer. This normally results from a loss of the connection on the remote socket due to a timeout or reboot. Commonly encountered via the http and net modules.
|
|
17
|
+
],
|
|
18
|
+
httpStatusCodesToRetry: [429],
|
|
19
|
+
retryOnTimeout: false,
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Executes a function with retries and backoff with jitter. If the function throws a network
|
|
23
|
+
* error, it will retry up to maxAttempts-1, waiting between each retry.
|
|
24
|
+
* If the function throws an error on the last attempt, it will throw the error.
|
|
25
|
+
* If the function succeeds, it will return the result.
|
|
26
|
+
* This is a specialization of `executeWithRetries` for network errors.
|
|
27
|
+
* By default it retries on ECONNREFUSED and ECONNRESET (customize the errors to retry
|
|
28
|
+
* setting the option `httpCodesToRetry`).
|
|
29
|
+
* By default it also retries on HTTP status code 429 (Too Many Requests).
|
|
30
|
+
*
|
|
31
|
+
* @param fn the function to be executed
|
|
32
|
+
* @param options the options to be used; see `ExecuteWithHttpRetriesOptions` for components and
|
|
33
|
+
* `defaultOptions` for defaults.
|
|
34
|
+
* @returns the result of calling the `fn` function
|
|
35
|
+
* @see `executeWithRetries()`
|
|
36
|
+
*
|
|
37
|
+
*/
|
|
38
|
+
async function executeWithNetworkRetries(fn, options) {
|
|
39
|
+
const actualOptions = { ...defaultOptions, ...options };
|
|
40
|
+
const { httpCodesToRetry: httpCodesFromParams, httpStatusCodesToRetry } = actualOptions;
|
|
41
|
+
const httpCodesToRetry = actualOptions.retryOnTimeout
|
|
42
|
+
? [...httpCodesFromParams, ...error_1.networkTimeoutErrors]
|
|
43
|
+
: httpCodesFromParams;
|
|
44
|
+
const codesAsString = httpCodesToRetry.map(String);
|
|
45
|
+
return (0, retry_1.executeWithRetries)(fn, {
|
|
46
|
+
...actualOptions,
|
|
47
|
+
shouldRetry: (_, error) => {
|
|
48
|
+
const networkCode = axios_1.default.isAxiosError(error) ? error.code : undefined;
|
|
49
|
+
const networkStatus = axios_1.default.isAxiosError(error) ? error.response?.status : undefined;
|
|
50
|
+
return ((networkCode && codesAsString.includes(networkCode)) ||
|
|
51
|
+
(networkStatus && httpStatusCodesToRetry.includes(networkStatus)) ||
|
|
52
|
+
false);
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
exports.executeWithNetworkRetries = executeWithNetworkRetries;
|
|
57
|
+
//# sourceMappingURL=retry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry.js","sourceRoot":"","sources":["../../src/net/retry.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA0B;AAC1B,2CAIyB;AACzB,mCAA6D;AAa7D,MAAM,cAAc,GAAqC;IACvD,GAAG,sBAA8B;IACjC,YAAY,EAAE,IAAI;IAClB,gBAAgB,EAAE;QAChB,4EAA4E;QAC5E,cAAc;QACd,YAAY,EAAE,mOAAmO;KAClP;IACD,sBAAsB,EAAE,CAAC,GAAG,CAAC;IAC7B,cAAc,EAAE,KAAK;CACtB,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACI,KAAK,UAAU,yBAAyB,CAC7C,EAAoB,EACpB,OAAmD;IAEnD,MAAM,aAAa,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,OAAO,EAAE,CAAC;IAExD,MAAM,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,GAAG,aAAa,CAAC;IAExF,MAAM,gBAAgB,GAAG,aAAa,CAAC,cAAc;QACnD,CAAC,CAAC,CAAC,GAAG,mBAAmB,EAAE,GAAG,4BAAoB,CAAC;QACnD,CAAC,CAAC,mBAAmB,CAAC;IAExB,MAAM,aAAa,GAAG,gBAAgB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAEnD,OAAO,IAAA,0BAAkB,EAAC,EAAE,EAAE;QAC5B,GAAG,aAAa;QAChB,WAAW,EAAE,CAAC,CAAC,EAAE,KAAc,EAAE,EAAE;YACjC,MAAM,WAAW,GAAG,eAAK,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;YACvE,MAAM,aAAa,GAAG,eAAK,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;YACrF,OAAO,CACL,CAAC,WAAW,IAAI,aAAa,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;gBACpD,CAAC,aAAa,IAAI,sBAAsB,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;gBACjE,KAAK,CACN,CAAC;QACJ,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AA1BD,8DA0BC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import * as stream from "stream";
|
|
4
|
+
export declare function streamToBuffer(stream: stream.Stream): Promise<Buffer>;
|
|
5
|
+
export declare function streamToString(stream: stream.Stream): Promise<String>;
|
|
6
|
+
//# sourceMappingURL=stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/net/stream.ts"],"names":[],"mappings":";;AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAEjC,wBAAsB,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAO3E;AAED,wBAAsB,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAG3E"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.streamToString = exports.streamToBuffer = void 0;
|
|
4
|
+
async function streamToBuffer(stream) {
|
|
5
|
+
const buffers = Array();
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
stream.on("data", chunk => buffers.push(Buffer.from(chunk)));
|
|
8
|
+
stream.on("error", err => reject(err));
|
|
9
|
+
stream.on("end", () => resolve(Buffer.concat(buffers)));
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
exports.streamToBuffer = streamToBuffer;
|
|
13
|
+
async function streamToString(stream) {
|
|
14
|
+
const buffer = await streamToBuffer(stream);
|
|
15
|
+
return buffer.toString();
|
|
16
|
+
}
|
|
17
|
+
exports.streamToString = streamToString;
|
|
18
|
+
//# sourceMappingURL=stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/net/stream.ts"],"names":[],"mappings":";;;AAEO,KAAK,UAAU,cAAc,CAAC,MAAqB;IACxD,MAAM,OAAO,GAAG,KAAK,EAAO,CAAC;IAC7B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAC7D,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;QACvC,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAC1D,CAAC,CAAC,CAAC;AACL,CAAC;AAPD,wCAOC;AAEM,KAAK,UAAU,cAAc,CAAC,MAAqB;IACxD,MAAM,MAAM,GAAG,MAAM,cAAc,CAAC,MAAM,CAAC,CAAC;IAC5C,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAC;AAC3B,CAAC;AAHD,wCAGC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metriport/shared",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.8",
|
|
4
4
|
"description": "Common code shared across packages - by Metriport Inc.",
|
|
5
5
|
"author": "Metriport Inc. <contact@metriport.com>",
|
|
6
6
|
"homepage": "https://metriport.com/",
|
|
@@ -58,5 +58,5 @@
|
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"@faker-js/faker": "^8.0.2"
|
|
60
60
|
},
|
|
61
|
-
"gitHead": "
|
|
61
|
+
"gitHead": "dfddbbe2a55f7deb2793a0031c49b9ab09898596"
|
|
62
62
|
}
|