@ac-kit/browser 0.0.0-stage → 0.1.0
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/CHANGELOG.md +44 -0
- package/LICENSE +28 -0
- package/README.md +187 -2
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/indexeddb/_indexed-db-request.d.ts +16 -0
- package/dist/indexeddb/_indexed-db-request.d.ts.map +1 -0
- package/dist/indexeddb/_indexed-db-request.js +45 -0
- package/dist/indexeddb/_indexed-db-request.js.map +1 -0
- package/dist/indexeddb/indexed-db-error.d.ts +9 -0
- package/dist/indexeddb/indexed-db-error.d.ts.map +1 -0
- package/dist/indexeddb/indexed-db-error.js +12 -0
- package/dist/indexeddb/indexed-db-error.js.map +1 -0
- package/dist/indexeddb/indexed-db-records.d.ts +95 -0
- package/dist/indexeddb/indexed-db-records.d.ts.map +1 -0
- package/dist/indexeddb/indexed-db-records.js +121 -0
- package/dist/indexeddb/indexed-db-records.js.map +1 -0
- package/dist/indexeddb/open-indexed-db.d.ts +68 -0
- package/dist/indexeddb/open-indexed-db.d.ts.map +1 -0
- package/dist/indexeddb/open-indexed-db.js +102 -0
- package/dist/indexeddb/open-indexed-db.js.map +1 -0
- package/dist/web-midi/web-midi-access.d.ts +54 -0
- package/dist/web-midi/web-midi-access.d.ts.map +1 -0
- package/dist/web-midi/web-midi-access.js +78 -0
- package/dist/web-midi/web-midi-access.js.map +1 -0
- package/dist/web-midi/web-midi-inputs.d.ts +81 -0
- package/dist/web-midi/web-midi-inputs.d.ts.map +1 -0
- package/dist/web-midi/web-midi-inputs.js +107 -0
- package/dist/web-midi/web-midi-inputs.js.map +1 -0
- package/dist/xhr/send-with-progress.d.ts +96 -0
- package/dist/xhr/send-with-progress.d.ts.map +1 -0
- package/dist/xhr/send-with-progress.js +132 -0
- package/dist/xhr/send-with-progress.js.map +1 -0
- package/package.json +64 -3
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The request failed before a response: the network, CORS, or the browser
|
|
3
|
+
* itself ended it. A `TypeError`, as `fetch`'s own failure.
|
|
4
|
+
*/
|
|
5
|
+
export class HttpNetworkError extends TypeError {
|
|
6
|
+
constructor(options) {
|
|
7
|
+
super("The request did not reach the server", options);
|
|
8
|
+
this.name = "HttpNetworkError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Sends a request from a page through `XMLHttpRequest`, telling how much of its
|
|
13
|
+
* body was sent and of its response received as they go: for a file upload a
|
|
14
|
+
* person watches. `fetch` reports no upload progress, and streams a request
|
|
15
|
+
* body only without its length; prefer it for anything else. The browser
|
|
16
|
+
* follows redirects and keeps its usual cookie and CORS rules.
|
|
17
|
+
*
|
|
18
|
+
* The response body is held whole in memory, read as `responseType` says.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const response = await sendWithProgress(XMLHttpRequest, {
|
|
22
|
+
* method: "POST",
|
|
23
|
+
* url: "/songs",
|
|
24
|
+
* body: file,
|
|
25
|
+
* onUploadProgress: ({ loaded, total }) =>
|
|
26
|
+
* bar.update(loaded / (total ?? loaded)),
|
|
27
|
+
* signal,
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* @param XMLHttpRequestClass The page's `XMLHttpRequest` (a worker's too), or a
|
|
31
|
+
* stand-in in tests.
|
|
32
|
+
* @returns The response, whatever its status.
|
|
33
|
+
* @throws {HttpNetworkError} When the request fails before a response.
|
|
34
|
+
* @throws {TypeError} When `request.headers` holds an invalid field.
|
|
35
|
+
* @throws A `DOMException` the browser throws for a method, URL or field it
|
|
36
|
+
* refuses (`SyntaxError`, `SecurityError`); nothing is sent then.
|
|
37
|
+
* @throws When `request.signal` aborts, its reason; a `DOMException` named
|
|
38
|
+
* `"AbortError"` when the browser aborts it on its own.
|
|
39
|
+
*/
|
|
40
|
+
export function sendWithProgress(XMLHttpRequestClass, request) {
|
|
41
|
+
const { signal, onUploadProgress, onDownloadProgress } = request;
|
|
42
|
+
if (signal?.aborted === true) {
|
|
43
|
+
return Promise.reject(signal.reason);
|
|
44
|
+
}
|
|
45
|
+
let headers;
|
|
46
|
+
try {
|
|
47
|
+
headers = new Headers(request.headers);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
return Promise.reject(error);
|
|
51
|
+
}
|
|
52
|
+
const httpRequest = new XMLHttpRequestClass();
|
|
53
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
54
|
+
const handleAbort = () => {
|
|
55
|
+
reject(signal?.reason);
|
|
56
|
+
httpRequest.abort();
|
|
57
|
+
};
|
|
58
|
+
signal?.addEventListener("abort", handleAbort, { once: true });
|
|
59
|
+
if (onUploadProgress !== undefined) {
|
|
60
|
+
httpRequest.upload.addEventListener("progress", (event) => {
|
|
61
|
+
onUploadProgress(progressOf_(event));
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
if (onDownloadProgress !== undefined) {
|
|
65
|
+
httpRequest.addEventListener("progress", (event) => {
|
|
66
|
+
onDownloadProgress(progressOf_(event));
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
httpRequest.addEventListener("load", () => {
|
|
70
|
+
resolve({
|
|
71
|
+
status: httpRequest.status,
|
|
72
|
+
statusText: httpRequest.statusText,
|
|
73
|
+
headers: parseResponseHeaders_(httpRequest.getAllResponseHeaders()),
|
|
74
|
+
url: httpRequest.responseURL,
|
|
75
|
+
body: responseBody_(httpRequest, request.responseType),
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
httpRequest.addEventListener("error", () => {
|
|
79
|
+
reject(new HttpNetworkError());
|
|
80
|
+
});
|
|
81
|
+
httpRequest.addEventListener("abort", () => {
|
|
82
|
+
reject(new DOMException("The request was aborted", "AbortError"));
|
|
83
|
+
});
|
|
84
|
+
httpRequest.addEventListener("loadend", () => {
|
|
85
|
+
signal?.removeEventListener("abort", handleAbort);
|
|
86
|
+
});
|
|
87
|
+
try {
|
|
88
|
+
httpRequest.open(request.method, String(request.url));
|
|
89
|
+
httpRequest.withCredentials = request.credentials === "include";
|
|
90
|
+
httpRequest.responseType =
|
|
91
|
+
request.responseType === "bytes"
|
|
92
|
+
? "arraybuffer"
|
|
93
|
+
: (request.responseType ?? "text");
|
|
94
|
+
for (const [name, value] of headers) {
|
|
95
|
+
httpRequest.setRequestHeader(name, value);
|
|
96
|
+
}
|
|
97
|
+
httpRequest.send(request.body ?? null);
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
// A method, URL or field the browser refuses: nothing was sent.
|
|
101
|
+
signal?.removeEventListener("abort", handleAbort);
|
|
102
|
+
return Promise.reject(error);
|
|
103
|
+
}
|
|
104
|
+
return promise;
|
|
105
|
+
}
|
|
106
|
+
function progressOf_(event) {
|
|
107
|
+
return {
|
|
108
|
+
loaded: event.loaded,
|
|
109
|
+
total: event.lengthComputable ? event.total : undefined,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function responseBody_(httpRequest, responseType) {
|
|
113
|
+
// `response` is what `responseType` asked for: an `ArrayBuffer` for
|
|
114
|
+
// `"bytes"`, a string or a `Blob` otherwise.
|
|
115
|
+
const body = responseType === "bytes"
|
|
116
|
+
? new Uint8Array(httpRequest.response)
|
|
117
|
+
: httpRequest.response;
|
|
118
|
+
return body; // The branch above follows `responseType`, which the compiler cannot relate to the generic.
|
|
119
|
+
}
|
|
120
|
+
// XMLHttpRequest §getAllResponseHeaders(): `name: value` lines, CRLF-separated,
|
|
121
|
+
// names lowercased and repeated fields combined.
|
|
122
|
+
function parseResponseHeaders_(text) {
|
|
123
|
+
const headers = new Headers();
|
|
124
|
+
for (const line of text.split("\r\n")) {
|
|
125
|
+
const separator = line.indexOf(": ");
|
|
126
|
+
if (separator > 0) {
|
|
127
|
+
headers.append(line.slice(0, separator), line.slice(separator + 2));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return headers;
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=send-with-progress.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"send-with-progress.js","sourceRoot":"","sources":["../../src/xhr/send-with-progress.ts"],"names":[],"mappings":"AAkEA;;;GAGG;AACH,MAAM,OAAO,gBAAiB,SAAQ,SAAS;IAC9C,YAAY,OAAsB;QACjC,KAAK,CAAC,sCAAsC,EAAE,OAAO,CAAC,CAAC;QACvD,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IAChC,CAAC;CACD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,gBAAgB,CAG/B,mBAA8C,EAC9C,OAAsC;IAEtC,MAAM,EAAE,MAAM,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,GAAG,OAAO,CAAC;IACjE,IAAI,MAAM,EAAE,OAAO,KAAK,IAAI,EAAE,CAAC;QAC9B,OAAO,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACtC,CAAC;IACD,IAAI,OAAgB,CAAC;IACrB,IAAI,CAAC;QACJ,OAAO,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACxC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC9B,CAAC;IACD,MAAM,WAAW,GAAG,IAAI,mBAAmB,EAAE,CAAC;IAC9C,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,GACjC,OAAO,CAAC,aAAa,EAElB,CAAC;IACL,MAAM,WAAW,GAAG,GAAS,EAAE;QAC9B,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACvB,WAAW,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC,CAAC;IACF,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,WAAW,EAAE,EAA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C,CAAC,CAAC,4FAA4F;AAClJ,CAAC;AAED,gFAAgF;AAChF,iDAAiD;AACjD,SAAS,qBAAqB,CAAC,IAAY;IAC1C,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC;IAC9B,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;QACvC,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACrC,IAAI,SAAS,GAAG,CAAC,EAAE,CAAC;YACnB,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,SAAS,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,SAAS,GAAG,CAAC,CAAC,CAAC,CAAC;QACrE,CAAC;IACF,CAAC;IAED,OAAO,OAAO,CAAC;AAChB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/browser",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Browser extensions and zero-dependency wrappers over browser APIs: IndexedDB as promises with versioned upgrades, Web MIDI access with input ports followed by name across reloads and hot-plugs, and requests reporting their upload progress over XMLHttpRequest.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"browser",
|
|
7
|
+
"indexeddb",
|
|
8
|
+
"midi",
|
|
9
|
+
"storage",
|
|
10
|
+
"upload-progress",
|
|
11
|
+
"web-midi",
|
|
12
|
+
"xmlhttprequest"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://anthochamp.pages.grenadine.io/node-essentials/api/browser/",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://git.grenadine.io/anthochamp/node-essentials/issues"
|
|
17
|
+
},
|
|
18
|
+
"license": "BSD-3-Clause",
|
|
19
|
+
"author": "Anthony Champagne <dev@anthonychampagne.fr>",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://git.grenadine.io/anthochamp/node-essentials.git",
|
|
23
|
+
"directory": "packages/browser"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"type": "module",
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"main": "./dist/index.js",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": "./dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"main": "./dist/index.js",
|
|
36
|
+
"exports": {
|
|
37
|
+
".": "./dist/index.js"
|
|
38
|
+
},
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"compile": "tsc -p ./tsconfig.build.json -outDir dist",
|
|
43
|
+
"compile:check": "tsc -noEmit -p ./tsconfig.json",
|
|
44
|
+
"clean": "rimraf dist",
|
|
45
|
+
"build": "yarn clean && yarn compile",
|
|
46
|
+
"test": "yarn run -T vitest run",
|
|
47
|
+
"prepack": "yarn build"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@ac-kit/async": "^0.5.0",
|
|
51
|
+
"@ac-kit/core": "^0.9.0",
|
|
52
|
+
"@types/web": "^0.0.367"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@ac-kit/tsconfig": "0.1.0",
|
|
56
|
+
"@ac-kit/typedoc-config": "0.1.0",
|
|
57
|
+
"@ac-kit/vitest-config": "0.1.1",
|
|
58
|
+
"@typescript/native": "typescript@7.0.2",
|
|
59
|
+
"fake-indexeddb": "6.2.8",
|
|
60
|
+
"rimraf": "6.1.4",
|
|
61
|
+
"typedoc": "0.28.20",
|
|
62
|
+
"typescript": "@typescript/typescript6@6.0.2"
|
|
63
|
+
},
|
|
64
|
+
"engines": {
|
|
65
|
+
"node": ">=24"
|
|
66
|
+
}
|
|
6
67
|
}
|