isomorphic-git 1.10.1

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.
@@ -0,0 +1,77 @@
1
+ export default index;
2
+ export type GitProgressEvent = {
3
+ phase: string;
4
+ loaded: number;
5
+ total: number;
6
+ };
7
+ export type ProgressCallback = (progress: GitProgressEvent) => void | Promise<void>;
8
+ export type GitHttpRequest = {
9
+ /**
10
+ * - The URL to request
11
+ */
12
+ url: string;
13
+ /**
14
+ * - The HTTP method to use
15
+ */
16
+ method?: string;
17
+ /**
18
+ * - Headers to include in the HTTP request
19
+ */
20
+ headers?: {
21
+ [x: string]: string;
22
+ };
23
+ /**
24
+ * - An async iterator of Uint8Arrays that make up the body of POST requests
25
+ */
26
+ body?: any;
27
+ /**
28
+ * - Reserved for future use (emitting `GitProgressEvent`s)
29
+ */
30
+ onProgress?: ProgressCallback;
31
+ /**
32
+ * - Reserved for future use (canceling a request)
33
+ */
34
+ signal?: any;
35
+ };
36
+ export type GitHttpResponse = {
37
+ /**
38
+ * - The final URL that was fetched after any redirects
39
+ */
40
+ url: string;
41
+ /**
42
+ * - The HTTP method that was used
43
+ */
44
+ method?: string;
45
+ /**
46
+ * - HTTP response headers
47
+ */
48
+ headers?: {
49
+ [x: string]: string;
50
+ };
51
+ /**
52
+ * - An async iterator of Uint8Arrays that make up the body of the response
53
+ */
54
+ body?: any;
55
+ /**
56
+ * - The HTTP status code
57
+ */
58
+ statusCode: number;
59
+ /**
60
+ * - The HTTP status message
61
+ */
62
+ statusMessage: string;
63
+ };
64
+ export type HttpFetch = (request: GitHttpRequest) => Promise<GitHttpResponse>;
65
+ export type HttpClient = {
66
+ request: HttpFetch;
67
+ };
68
+ declare namespace index {
69
+ export { request };
70
+ }
71
+ /**
72
+ * HttpClient
73
+ *
74
+ * @param {GitHttpRequest} request
75
+ * @returns {Promise<GitHttpResponse>}
76
+ */
77
+ export function request({ onProgress, url, method, headers, body, }: GitHttpRequest): Promise<GitHttpResponse>;
@@ -0,0 +1,166 @@
1
+ /**
2
+ * @typedef {Object} GitProgressEvent
3
+ * @property {string} phase
4
+ * @property {number} loaded
5
+ * @property {number} total
6
+ */
7
+
8
+ /**
9
+ * @callback ProgressCallback
10
+ * @param {GitProgressEvent} progress
11
+ * @returns {void | Promise<void>}
12
+ */
13
+
14
+ /**
15
+ * @typedef {Object} GitHttpRequest
16
+ * @property {string} url - The URL to request
17
+ * @property {string} [method='GET'] - The HTTP method to use
18
+ * @property {Object<string, string>} [headers={}] - Headers to include in the HTTP request
19
+ * @property {AsyncIterableIterator<Uint8Array>} [body] - An async iterator of Uint8Arrays that make up the body of POST requests
20
+ * @property {ProgressCallback} [onProgress] - Reserved for future use (emitting `GitProgressEvent`s)
21
+ * @property {object} [signal] - Reserved for future use (canceling a request)
22
+ */
23
+
24
+ /**
25
+ * @typedef {Object} GitHttpResponse
26
+ * @property {string} url - The final URL that was fetched after any redirects
27
+ * @property {string} [method] - The HTTP method that was used
28
+ * @property {Object<string, string>} [headers] - HTTP response headers
29
+ * @property {AsyncIterableIterator<Uint8Array>} [body] - An async iterator of Uint8Arrays that make up the body of the response
30
+ * @property {number} statusCode - The HTTP status code
31
+ * @property {string} statusMessage - The HTTP status message
32
+ */
33
+
34
+ /**
35
+ * @callback HttpFetch
36
+ * @param {GitHttpRequest} request
37
+ * @returns {Promise<GitHttpResponse>}
38
+ */
39
+
40
+ /**
41
+ * @typedef {Object} HttpClient
42
+ * @property {HttpFetch} request
43
+ */
44
+
45
+ // Convert a value to an Async Iterator
46
+ // This will be easier with async generator functions.
47
+ function fromValue(value) {
48
+ let queue = [value];
49
+ return {
50
+ next() {
51
+ return Promise.resolve({ done: queue.length === 0, value: queue.pop() })
52
+ },
53
+ return() {
54
+ queue = [];
55
+ return {}
56
+ },
57
+ [Symbol.asyncIterator]() {
58
+ return this
59
+ },
60
+ }
61
+ }
62
+
63
+ function getIterator(iterable) {
64
+ if (iterable[Symbol.asyncIterator]) {
65
+ return iterable[Symbol.asyncIterator]()
66
+ }
67
+ if (iterable[Symbol.iterator]) {
68
+ return iterable[Symbol.iterator]()
69
+ }
70
+ if (iterable.next) {
71
+ return iterable
72
+ }
73
+ return fromValue(iterable)
74
+ }
75
+
76
+ // Currently 'for await' upsets my linters.
77
+ async function forAwait(iterable, cb) {
78
+ const iter = getIterator(iterable);
79
+ while (true) {
80
+ const { value, done } = await iter.next();
81
+ if (value) await cb(value);
82
+ if (done) break
83
+ }
84
+ if (iter.return) iter.return();
85
+ }
86
+
87
+ async function collect(iterable) {
88
+ let size = 0;
89
+ const buffers = [];
90
+ // This will be easier once `for await ... of` loops are available.
91
+ await forAwait(iterable, value => {
92
+ buffers.push(value);
93
+ size += value.byteLength;
94
+ });
95
+ const result = new Uint8Array(size);
96
+ let nextIndex = 0;
97
+ for (const buffer of buffers) {
98
+ result.set(buffer, nextIndex);
99
+ nextIndex += buffer.byteLength;
100
+ }
101
+ return result
102
+ }
103
+
104
+ // Convert a web ReadableStream (not Node stream!) to an Async Iterator
105
+ // adapted from https://jakearchibald.com/2017/async-iterators-and-generators/
106
+ function fromStream(stream) {
107
+ // Use native async iteration if it's available.
108
+ if (stream[Symbol.asyncIterator]) return stream
109
+ const reader = stream.getReader();
110
+ return {
111
+ next() {
112
+ return reader.read()
113
+ },
114
+ return() {
115
+ reader.releaseLock();
116
+ return {}
117
+ },
118
+ [Symbol.asyncIterator]() {
119
+ return this
120
+ },
121
+ }
122
+ }
123
+
124
+ /* eslint-env browser */
125
+
126
+ /**
127
+ * HttpClient
128
+ *
129
+ * @param {GitHttpRequest} request
130
+ * @returns {Promise<GitHttpResponse>}
131
+ */
132
+ async function request({
133
+ onProgress,
134
+ url,
135
+ method = 'GET',
136
+ headers = {},
137
+ body,
138
+ }) {
139
+ // streaming uploads aren't possible yet in the browser
140
+ if (body) {
141
+ body = await collect(body);
142
+ }
143
+ const res = await fetch(url, { method, headers, body });
144
+ const iter =
145
+ res.body && res.body.getReader
146
+ ? fromStream(res.body)
147
+ : [new Uint8Array(await res.arrayBuffer())];
148
+ // convert Header object to ordinary JSON
149
+ headers = {};
150
+ for (const [key, value] of res.headers.entries()) {
151
+ headers[key] = value;
152
+ }
153
+ return {
154
+ url: res.url,
155
+ method: res.method,
156
+ statusCode: res.status,
157
+ statusMessage: res.statusText,
158
+ body: iter,
159
+ headers: headers,
160
+ }
161
+ }
162
+
163
+ var index = { request };
164
+
165
+ export default index;
166
+ export { request };
@@ -0,0 +1,176 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = global || self, factory(global.GitHttp = {}));
5
+ }(this, (function (exports) { 'use strict';
6
+
7
+ /**
8
+ * @typedef {Object} GitProgressEvent
9
+ * @property {string} phase
10
+ * @property {number} loaded
11
+ * @property {number} total
12
+ */
13
+
14
+ /**
15
+ * @callback ProgressCallback
16
+ * @param {GitProgressEvent} progress
17
+ * @returns {void | Promise<void>}
18
+ */
19
+
20
+ /**
21
+ * @typedef {Object} GitHttpRequest
22
+ * @property {string} url - The URL to request
23
+ * @property {string} [method='GET'] - The HTTP method to use
24
+ * @property {Object<string, string>} [headers={}] - Headers to include in the HTTP request
25
+ * @property {AsyncIterableIterator<Uint8Array>} [body] - An async iterator of Uint8Arrays that make up the body of POST requests
26
+ * @property {ProgressCallback} [onProgress] - Reserved for future use (emitting `GitProgressEvent`s)
27
+ * @property {object} [signal] - Reserved for future use (canceling a request)
28
+ */
29
+
30
+ /**
31
+ * @typedef {Object} GitHttpResponse
32
+ * @property {string} url - The final URL that was fetched after any redirects
33
+ * @property {string} [method] - The HTTP method that was used
34
+ * @property {Object<string, string>} [headers] - HTTP response headers
35
+ * @property {AsyncIterableIterator<Uint8Array>} [body] - An async iterator of Uint8Arrays that make up the body of the response
36
+ * @property {number} statusCode - The HTTP status code
37
+ * @property {string} statusMessage - The HTTP status message
38
+ */
39
+
40
+ /**
41
+ * @callback HttpFetch
42
+ * @param {GitHttpRequest} request
43
+ * @returns {Promise<GitHttpResponse>}
44
+ */
45
+
46
+ /**
47
+ * @typedef {Object} HttpClient
48
+ * @property {HttpFetch} request
49
+ */
50
+
51
+ // Convert a value to an Async Iterator
52
+ // This will be easier with async generator functions.
53
+ function fromValue(value) {
54
+ let queue = [value];
55
+ return {
56
+ next() {
57
+ return Promise.resolve({ done: queue.length === 0, value: queue.pop() })
58
+ },
59
+ return() {
60
+ queue = [];
61
+ return {}
62
+ },
63
+ [Symbol.asyncIterator]() {
64
+ return this
65
+ },
66
+ }
67
+ }
68
+
69
+ function getIterator(iterable) {
70
+ if (iterable[Symbol.asyncIterator]) {
71
+ return iterable[Symbol.asyncIterator]()
72
+ }
73
+ if (iterable[Symbol.iterator]) {
74
+ return iterable[Symbol.iterator]()
75
+ }
76
+ if (iterable.next) {
77
+ return iterable
78
+ }
79
+ return fromValue(iterable)
80
+ }
81
+
82
+ // Currently 'for await' upsets my linters.
83
+ async function forAwait(iterable, cb) {
84
+ const iter = getIterator(iterable);
85
+ while (true) {
86
+ const { value, done } = await iter.next();
87
+ if (value) await cb(value);
88
+ if (done) break
89
+ }
90
+ if (iter.return) iter.return();
91
+ }
92
+
93
+ async function collect(iterable) {
94
+ let size = 0;
95
+ const buffers = [];
96
+ // This will be easier once `for await ... of` loops are available.
97
+ await forAwait(iterable, value => {
98
+ buffers.push(value);
99
+ size += value.byteLength;
100
+ });
101
+ const result = new Uint8Array(size);
102
+ let nextIndex = 0;
103
+ for (const buffer of buffers) {
104
+ result.set(buffer, nextIndex);
105
+ nextIndex += buffer.byteLength;
106
+ }
107
+ return result
108
+ }
109
+
110
+ // Convert a web ReadableStream (not Node stream!) to an Async Iterator
111
+ // adapted from https://jakearchibald.com/2017/async-iterators-and-generators/
112
+ function fromStream(stream) {
113
+ // Use native async iteration if it's available.
114
+ if (stream[Symbol.asyncIterator]) return stream
115
+ const reader = stream.getReader();
116
+ return {
117
+ next() {
118
+ return reader.read()
119
+ },
120
+ return() {
121
+ reader.releaseLock();
122
+ return {}
123
+ },
124
+ [Symbol.asyncIterator]() {
125
+ return this
126
+ },
127
+ }
128
+ }
129
+
130
+ /* eslint-env browser */
131
+
132
+ /**
133
+ * HttpClient
134
+ *
135
+ * @param {GitHttpRequest} request
136
+ * @returns {Promise<GitHttpResponse>}
137
+ */
138
+ async function request({
139
+ onProgress,
140
+ url,
141
+ method = 'GET',
142
+ headers = {},
143
+ body,
144
+ }) {
145
+ // streaming uploads aren't possible yet in the browser
146
+ if (body) {
147
+ body = await collect(body);
148
+ }
149
+ const res = await fetch(url, { method, headers, body });
150
+ const iter =
151
+ res.body && res.body.getReader
152
+ ? fromStream(res.body)
153
+ : [new Uint8Array(await res.arrayBuffer())];
154
+ // convert Header object to ordinary JSON
155
+ headers = {};
156
+ for (const [key, value] of res.headers.entries()) {
157
+ headers[key] = value;
158
+ }
159
+ return {
160
+ url: res.url,
161
+ method: res.method,
162
+ statusCode: res.status,
163
+ statusMessage: res.statusText,
164
+ body: iter,
165
+ headers: headers,
166
+ }
167
+ }
168
+
169
+ var index = { request };
170
+
171
+ exports.default = index;
172
+ exports.request = request;
173
+
174
+ Object.defineProperty(exports, '__esModule', { value: true });
175
+
176
+ })));
@@ -0,0 +1,7 @@
1
+ {
2
+ "type": "module",
3
+ "main": "index.cjs",
4
+ "module": "index.js",
5
+ "typings": "index.d.ts",
6
+ "unpkg": "index.umd.js"
7
+ }