braid-http 1.3.51 → 1.3.53
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/braid-http-client.js +264 -255
- package/package.json +1 -1
package/braid-http-client.js
CHANGED
|
@@ -897,8 +897,6 @@ function random_base64url(n) {
|
|
|
897
897
|
// multiplex_fetch provides a fetch-like experience for HTTP requests
|
|
898
898
|
// where the result is actually being sent over a separate multiplexed connection.
|
|
899
899
|
async function multiplex_fetch(url, params, mux_params, aborter) {
|
|
900
|
-
var multiplex_version = '1.0'
|
|
901
|
-
|
|
902
900
|
var origin = new URL(url, typeof document !== 'undefined' ? document.baseURI : undefined).origin
|
|
903
901
|
|
|
904
902
|
// the mux_key is the same as the origin, unless it is being overriden
|
|
@@ -907,272 +905,283 @@ async function multiplex_fetch(url, params, mux_params, aborter) {
|
|
|
907
905
|
|
|
908
906
|
// create a new multiplexer if it doesn't exist for this origin
|
|
909
907
|
if (!multiplex_fetch.multiplexers) multiplex_fetch.multiplexers = {}
|
|
910
|
-
if (!multiplex_fetch.multiplexers[mux_key]) multiplex_fetch.multiplexers[mux_key] =
|
|
911
|
-
|
|
912
|
-
// make up a new multiplexer id (unless it is being overriden)
|
|
913
|
-
var multiplexer = params.headers.get('multiplex-through')?.split('/')[3]
|
|
914
|
-
?? random_base64url(Math.ceil((mux_params?.id_bits ?? 72) / 6))
|
|
915
|
-
|
|
916
|
-
var requests = new Map()
|
|
917
|
-
var mux_error = null
|
|
918
|
-
var try_deleting = new Set()
|
|
919
|
-
|
|
920
|
-
function cleanup(e, stay_dead) {
|
|
921
|
-
// the multiplexer stream has died.. let everyone know..
|
|
922
|
-
mux_error = e
|
|
923
|
-
if (!stay_dead) delete multiplex_fetch.multiplexers[mux_key]
|
|
924
|
-
for (var f of requests.values()) f()
|
|
925
|
-
}
|
|
926
|
-
|
|
927
|
-
async function try_deleting_request(request) {
|
|
928
|
-
if (!try_deleting.has(request)) {
|
|
929
|
-
try_deleting.add(request)
|
|
930
|
-
try {
|
|
931
|
-
var r = await braid_fetch(`${origin}/.well-known/multiplexer/${multiplexer}/${request}`, {
|
|
932
|
-
method: 'DELETE',
|
|
933
|
-
headers: { 'Multiplex-Version': multiplex_version },
|
|
934
|
-
retry: true
|
|
935
|
-
})
|
|
908
|
+
if (!multiplex_fetch.multiplexers[mux_key]) multiplex_fetch.multiplexers[mux_key] =
|
|
909
|
+
create_multiplexer(origin, mux_key, params, mux_params, aborter)
|
|
936
910
|
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
+ r.headers.get('Multiplex-Version')
|
|
941
|
-
+ ', expected ' + multiplex_version)
|
|
942
|
-
} catch (e) {
|
|
943
|
-
e = new Error(`Could not cancel multiplexed request: ${e}`)
|
|
944
|
-
console.error('' + e)
|
|
945
|
-
throw e
|
|
946
|
-
} finally { try_deleting.delete(request) }
|
|
947
|
-
}
|
|
948
|
-
}
|
|
911
|
+
// call the special fetch function for the multiplexer
|
|
912
|
+
return await (await multiplex_fetch.multiplexers[mux_key])(url, params)
|
|
913
|
+
}
|
|
949
914
|
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
if (mux_params?.via === 'POST') throw 'skip multiplex method'
|
|
954
|
-
var r = await braid_fetch(`${origin}/${multiplexer}`, {
|
|
955
|
-
method: 'MULTIPLEX',
|
|
956
|
-
headers: {'Multiplex-Version': multiplex_version},
|
|
957
|
-
retry: true
|
|
958
|
-
})
|
|
959
|
-
if (r.status === 409) {
|
|
960
|
-
var e = await r.json()
|
|
961
|
-
if (e.error === 'Multiplexer already exists')
|
|
962
|
-
return cleanup(create_error(e.error, {dont_wait: true}))
|
|
963
|
-
}
|
|
964
|
-
if (!r.ok || r.headers.get('Multiplex-Version') !== multiplex_version)
|
|
965
|
-
throw 'bad'
|
|
966
|
-
} catch (e) {
|
|
967
|
-
// some servers don't like custom methods,
|
|
968
|
-
// so let's try with a well-known url
|
|
969
|
-
try {
|
|
970
|
-
r = await braid_fetch(`${origin}/.well-known/multiplexer/${multiplexer}`,
|
|
971
|
-
{method: 'POST',
|
|
972
|
-
headers: {'Multiplex-Version': multiplex_version},
|
|
973
|
-
retry: true})
|
|
974
|
-
if (r.status === 409) {
|
|
975
|
-
var e = await r.json()
|
|
976
|
-
if (e.error === 'Multiplexer already exists')
|
|
977
|
-
return cleanup(create_error(e.error, {dont_wait: true}))
|
|
978
|
-
}
|
|
979
|
-
if (!r.ok) throw new Error('status not ok: ' + r.status)
|
|
980
|
-
if (r.headers.get('Multiplex-Version') !== multiplex_version)
|
|
981
|
-
throw new Error('wrong multiplex version: '
|
|
982
|
-
+ r.headers.get('Multiplex-Version')
|
|
983
|
-
+ ', expected ' + multiplex_version)
|
|
984
|
-
} catch (e) {
|
|
985
|
-
// fallback to normal fetch if multiplexed connection fails
|
|
986
|
-
console.error(`Could not establish multiplexer.\n`
|
|
987
|
-
+ `Got error: ${e}.\nFalling back to normal connection.`)
|
|
988
|
-
cleanup(e, true)
|
|
989
|
-
return false
|
|
990
|
-
}
|
|
991
|
-
}
|
|
915
|
+
// returns a function with a fetch-like interface that transparently multiplexes the fetch
|
|
916
|
+
async function create_multiplexer(origin, mux_key, params, mux_params, aborter) {
|
|
917
|
+
var multiplex_version = '1.0'
|
|
992
918
|
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
&& !params.headers.get('multiplex-through'))
|
|
1010
|
-
return await normal_fetch(url, params)
|
|
1011
|
-
|
|
1012
|
-
// make up a new request id (unless it is being overriden)
|
|
1013
|
-
var request = params.headers.get('multiplex-through')?.split('/')[4]
|
|
1014
|
-
?? random_base64url(Math.ceil((mux_params?.id_bits ?? 72) / 6))
|
|
1015
|
-
|
|
1016
|
-
// add the Multiplex-Through header without affecting the underlying params
|
|
1017
|
-
var mux_headers = new Headers(params.headers)
|
|
1018
|
-
mux_headers.set('Multiplex-Through', `/.well-known/multiplexer/${multiplexer}/${request}`)
|
|
1019
|
-
mux_headers.set('Multiplex-Version', multiplex_version)
|
|
1020
|
-
params = {...params, headers: mux_headers}
|
|
1021
|
-
|
|
1022
|
-
// setup a way to receive incoming data from the multiplexer
|
|
1023
|
-
var buffers = []
|
|
1024
|
-
var bytes_available = () => {}
|
|
1025
|
-
var request_error = null
|
|
1026
|
-
|
|
1027
|
-
// this utility calls the callback whenever new data is available to process
|
|
1028
|
-
async function process_buffers(cb) {
|
|
1029
|
-
while (true) {
|
|
1030
|
-
// wait for data if none is available
|
|
1031
|
-
if (!mux_error && !request_error && !buffers.length)
|
|
1032
|
-
await new Promise(done => bytes_available = done)
|
|
1033
|
-
if (mux_error || request_error) throw (mux_error || request_error)
|
|
1034
|
-
|
|
1035
|
-
// process the data
|
|
1036
|
-
let ret = cb()
|
|
1037
|
-
if (ret) return ret
|
|
1038
|
-
}
|
|
1039
|
-
}
|
|
919
|
+
// make up a new multiplexer id (unless it is being overriden)
|
|
920
|
+
var multiplexer = params.headers.get('multiplex-through')?.split('/')[3]
|
|
921
|
+
?? random_base64url(Math.ceil((mux_params?.id_bits ?? 72) / 6))
|
|
922
|
+
|
|
923
|
+
var requests = new Map()
|
|
924
|
+
var mux_error = null
|
|
925
|
+
var try_deleting = new Set()
|
|
926
|
+
var not_used_timeout = null
|
|
927
|
+
var mux_aborter = new AbortController()
|
|
928
|
+
|
|
929
|
+
function cleanup(e, stay_dead) {
|
|
930
|
+
// the multiplexer stream has died.. let everyone know..
|
|
931
|
+
mux_error = e
|
|
932
|
+
if (!stay_dead) delete multiplex_fetch.multiplexers[mux_key]
|
|
933
|
+
for (var f of requests.values()) f()
|
|
934
|
+
}
|
|
1040
935
|
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
936
|
+
async function try_deleting_request(request) {
|
|
937
|
+
if (!try_deleting.has(request)) {
|
|
938
|
+
try_deleting.add(request)
|
|
939
|
+
try {
|
|
940
|
+
var r = await braid_fetch(`${origin}/.well-known/multiplexer/${multiplexer}/${request}`, {
|
|
941
|
+
method: 'DELETE',
|
|
942
|
+
headers: { 'Multiplex-Version': multiplex_version },
|
|
943
|
+
retry: true
|
|
1048
944
|
})
|
|
1049
945
|
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
if (res.status === 409) {
|
|
1064
|
-
var e = await res.json()
|
|
1065
|
-
if (e.error === 'Request already multiplexed') {
|
|
1066
|
-
// the id is already in use,
|
|
1067
|
-
// so we want to retry right away with a different id
|
|
1068
|
-
throw create_error(e.error, {dont_wait: true})
|
|
1069
|
-
}
|
|
1070
|
-
}
|
|
1071
|
-
|
|
1072
|
-
if (res.status === 424) {
|
|
1073
|
-
// the multiplexer isn't there,
|
|
1074
|
-
// could be we arrived before the multiplexer,
|
|
1075
|
-
// or after it was shutdown;
|
|
1076
|
-
// in either case we want to retry right away
|
|
1077
|
-
throw create_error('multiplexer not connected', {dont_wait: true})
|
|
1078
|
-
}
|
|
946
|
+
if (!r.ok) throw new Error('status not ok: ' + r.status)
|
|
947
|
+
if (r.headers.get('Multiplex-Version') !== multiplex_version)
|
|
948
|
+
throw new Error('wrong multiplex version: '
|
|
949
|
+
+ r.headers.get('Multiplex-Version')
|
|
950
|
+
+ ', expected ' + multiplex_version)
|
|
951
|
+
} catch (e) {
|
|
952
|
+
e = new Error(`Could not cancel multiplexed request: ${e}`)
|
|
953
|
+
console.error('' + e)
|
|
954
|
+
throw e
|
|
955
|
+
} finally { try_deleting.delete(request) }
|
|
956
|
+
}
|
|
957
|
+
}
|
|
1079
958
|
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
console.log(`headers_buffer: ` + new TextDecoder().decode(headers_buffer))
|
|
1125
|
-
throw new Error('error parsing headers')
|
|
1126
|
-
} else if (x.result === 'waiting') {
|
|
1127
|
-
if (request_ended)
|
|
1128
|
-
throw new Error('Multiplexed request ended before headers received.')
|
|
1129
|
-
} else return x
|
|
1130
|
-
})
|
|
959
|
+
var mux_promise = (async () => {
|
|
960
|
+
// attempt to establish a multiplexed connection
|
|
961
|
+
try {
|
|
962
|
+
if (mux_params?.via === 'POST') throw 'skip multiplex method'
|
|
963
|
+
var r = await braid_fetch(`${origin}/${multiplexer}`, {
|
|
964
|
+
signal: mux_aborter.signal,
|
|
965
|
+
method: 'MULTIPLEX',
|
|
966
|
+
headers: {'Multiplex-Version': multiplex_version},
|
|
967
|
+
retry: true
|
|
968
|
+
})
|
|
969
|
+
if (r.status === 409) {
|
|
970
|
+
var e = await r.json()
|
|
971
|
+
if (e.error === 'Multiplexer already exists')
|
|
972
|
+
return cleanup(create_error(e.error, {dont_wait: true}))
|
|
973
|
+
}
|
|
974
|
+
if (!r.ok || r.headers.get('Multiplex-Version') !== multiplex_version)
|
|
975
|
+
throw 'bad'
|
|
976
|
+
} catch (e) {
|
|
977
|
+
// some servers don't like custom methods,
|
|
978
|
+
// so let's try with a well-known url
|
|
979
|
+
try {
|
|
980
|
+
r = await braid_fetch(`${origin}/.well-known/multiplexer/${multiplexer}`,
|
|
981
|
+
{method: 'POST',
|
|
982
|
+
signal: mux_aborter.signal,
|
|
983
|
+
headers: {'Multiplex-Version': multiplex_version},
|
|
984
|
+
retry: true})
|
|
985
|
+
if (r.status === 409) {
|
|
986
|
+
var e = await r.json()
|
|
987
|
+
if (e.error === 'Multiplexer already exists')
|
|
988
|
+
return cleanup(create_error(e.error, {dont_wait: true}))
|
|
989
|
+
}
|
|
990
|
+
if (!r.ok) throw new Error('status not ok: ' + r.status)
|
|
991
|
+
if (r.headers.get('Multiplex-Version') !== multiplex_version)
|
|
992
|
+
throw new Error('wrong multiplex version: '
|
|
993
|
+
+ r.headers.get('Multiplex-Version')
|
|
994
|
+
+ ', expected ' + multiplex_version)
|
|
995
|
+
} catch (e) {
|
|
996
|
+
// fallback to normal fetch if multiplexed connection fails
|
|
997
|
+
console.error(`Could not establish multiplexer.\n`
|
|
998
|
+
+ `Got error: ${e}.\nFalling back to normal connection.`)
|
|
999
|
+
cleanup(e, true)
|
|
1000
|
+
return false
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1131
1003
|
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1004
|
+
// parse the multiplexed stream,
|
|
1005
|
+
// and send messages to the appropriate requests
|
|
1006
|
+
parse_multiplex_stream(r.body.getReader(), async (request, bytes) => {
|
|
1007
|
+
if (requests.has(request)) requests.get(request)(bytes)
|
|
1008
|
+
else try_deleting_request(request)
|
|
1009
|
+
}, e => cleanup(e))
|
|
1010
|
+
})()
|
|
1011
|
+
|
|
1012
|
+
// return a "fetch" for this multiplexer
|
|
1013
|
+
return async (url, params) => {
|
|
1014
|
+
|
|
1015
|
+
// if we already know the multiplexer is not working,
|
|
1016
|
+
// then fallback to normal fetch
|
|
1017
|
+
// (unless the user is specifically asking for multiplexing)
|
|
1018
|
+
if ((await promise_done(mux_promise))
|
|
1019
|
+
&& (await mux_promise) === false
|
|
1020
|
+
&& !params.headers.get('multiplex-through'))
|
|
1021
|
+
return await normal_fetch(url, params)
|
|
1022
|
+
|
|
1023
|
+
// make up a new request id (unless it is being overriden)
|
|
1024
|
+
var request = params.headers.get('multiplex-through')?.split('/')[4]
|
|
1025
|
+
?? random_base64url(Math.ceil((mux_params?.id_bits ?? 72) / 6))
|
|
1026
|
+
|
|
1027
|
+
// add the Multiplex-Through header without affecting the underlying params
|
|
1028
|
+
var mux_headers = new Headers(params.headers)
|
|
1029
|
+
mux_headers.set('Multiplex-Through', `/.well-known/multiplexer/${multiplexer}/${request}`)
|
|
1030
|
+
mux_headers.set('Multiplex-Version', multiplex_version)
|
|
1031
|
+
params = {...params, headers: mux_headers}
|
|
1032
|
+
|
|
1033
|
+
// setup a way to receive incoming data from the multiplexer
|
|
1034
|
+
var buffers = []
|
|
1035
|
+
var bytes_available = () => {}
|
|
1036
|
+
var request_error = null
|
|
1037
|
+
|
|
1038
|
+
// this utility calls the callback whenever new data is available to process
|
|
1039
|
+
async function process_buffers(cb) {
|
|
1040
|
+
while (true) {
|
|
1041
|
+
// wait for data if none is available
|
|
1042
|
+
if (!mux_error && !request_error && !buffers.length)
|
|
1043
|
+
await new Promise(done => bytes_available = done)
|
|
1044
|
+
if (mux_error || request_error) throw (mux_error || request_error)
|
|
1045
|
+
|
|
1046
|
+
// process the data
|
|
1047
|
+
let ret = cb()
|
|
1048
|
+
if (ret) return ret
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1159
1051
|
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1052
|
+
// tell the multiplexer to send bytes for this request to us
|
|
1053
|
+
requests.set(request, bytes => {
|
|
1054
|
+
if (!bytes) {
|
|
1055
|
+
buffers.push(bytes)
|
|
1056
|
+
if (mux_error || request_error) aborter.abort()
|
|
1057
|
+
} else if (!mux_error) buffers.push(bytes)
|
|
1058
|
+
bytes_available()
|
|
1059
|
+
})
|
|
1060
|
+
|
|
1061
|
+
// prepare a function that we'll call to cleanly tear things down
|
|
1062
|
+
clearTimeout(not_used_timeout)
|
|
1063
|
+
var unset = async e => {
|
|
1064
|
+
unset = () => {}
|
|
1065
|
+
requests.delete(request)
|
|
1066
|
+
if (!requests.size) not_used_timeout = setTimeout(() => mux_aborter.abort(), mux_params?.not_used_timeout ?? 1000 * 20)
|
|
1067
|
+
request_error = e
|
|
1068
|
+
bytes_available()
|
|
1069
|
+
await try_deleting_request(request)
|
|
1070
|
+
}
|
|
1163
1071
|
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1072
|
+
// do the underlying fetch
|
|
1073
|
+
try {
|
|
1074
|
+
var res = await normal_fetch(url, params)
|
|
1075
|
+
|
|
1076
|
+
if (res.status === 409) {
|
|
1077
|
+
var e = await res.json()
|
|
1078
|
+
if (e.error === 'Request already multiplexed') {
|
|
1079
|
+
// the id is already in use,
|
|
1080
|
+
// so we want to retry right away with a different id
|
|
1081
|
+
throw create_error(e.error, {dont_wait: true})
|
|
1170
1082
|
}
|
|
1171
1083
|
}
|
|
1172
|
-
})()
|
|
1173
1084
|
|
|
1174
|
-
|
|
1175
|
-
|
|
1085
|
+
if (res.status === 424) {
|
|
1086
|
+
// the multiplexer isn't there,
|
|
1087
|
+
// could be we arrived before the multiplexer,
|
|
1088
|
+
// or after it was shutdown;
|
|
1089
|
+
// in either case we want to retry right away
|
|
1090
|
+
throw create_error('multiplexer not connected', {dont_wait: true})
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
// if the response says it's ok,
|
|
1094
|
+
// but it's is not a multiplexed response,
|
|
1095
|
+
// fall back to as if it was a normal fetch
|
|
1096
|
+
if (res.ok && res.status !== 293) return res
|
|
1097
|
+
|
|
1098
|
+
if (res.status !== 293)
|
|
1099
|
+
throw create_error('Could not establish multiplexed request '
|
|
1100
|
+
+ params.headers.get('multiplex-through')
|
|
1101
|
+
+ ', got status: ' + res.status,
|
|
1102
|
+
{ dont_retry: true })
|
|
1103
|
+
|
|
1104
|
+
if (res.headers.get('Multiplex-Version') !== multiplex_version)
|
|
1105
|
+
throw create_error('Could not establish multiplexed request '
|
|
1106
|
+
+ params.headers.get('multiplex-through')
|
|
1107
|
+
+ ', got unknown version: '
|
|
1108
|
+
+ res.headers.get('Multiplex-Version'),
|
|
1109
|
+
{ dont_retry: true })
|
|
1110
|
+
|
|
1111
|
+
// we want to present the illusion that the connection is still open,
|
|
1112
|
+
// and therefor closable with "abort",
|
|
1113
|
+
// so we handle the abort ourselves to close the multiplexed request
|
|
1114
|
+
params.signal.addEventListener('abort', () =>
|
|
1115
|
+
unset(create_error('request aborted', {name: 'AbortError'})))
|
|
1116
|
+
|
|
1117
|
+
// first, we need to listen for the headers..
|
|
1118
|
+
var headers_buffer = new Uint8Array()
|
|
1119
|
+
var parsed_headers = await process_buffers(() => {
|
|
1120
|
+
// check if the request has been closed
|
|
1121
|
+
var request_ended = !buffers[buffers.length - 1]
|
|
1122
|
+
if (request_ended) buffers.pop()
|
|
1123
|
+
|
|
1124
|
+
// aggregate all the new buffers into our big headers_buffer
|
|
1125
|
+
headers_buffer = concat_buffers([headers_buffer, ...buffers])
|
|
1126
|
+
buffers = []
|
|
1127
|
+
|
|
1128
|
+
// and if the request had ended, put that information back
|
|
1129
|
+
if (request_ended) buffers.push(null)
|
|
1130
|
+
|
|
1131
|
+
// try parsing what we got so far as headers..
|
|
1132
|
+
var x = parse_headers(headers_buffer)
|
|
1133
|
+
|
|
1134
|
+
// how did it go?
|
|
1135
|
+
if (x.result === 'error') {
|
|
1136
|
+
// if we got an error, give up
|
|
1137
|
+
console.log(`headers_buffer: ` + new TextDecoder().decode(headers_buffer))
|
|
1138
|
+
throw new Error('error parsing headers')
|
|
1139
|
+
} else if (x.result === 'waiting') {
|
|
1140
|
+
if (request_ended)
|
|
1141
|
+
throw new Error('Multiplexed request ended before headers received.')
|
|
1142
|
+
} else return x
|
|
1143
|
+
})
|
|
1144
|
+
|
|
1145
|
+
// put the bytes left over from the header back
|
|
1146
|
+
if (parsed_headers.input.length) buffers.unshift(parsed_headers.input)
|
|
1147
|
+
|
|
1148
|
+
// these headers will also have the status,
|
|
1149
|
+
// but we want to present the status in a more usual way below
|
|
1150
|
+
var status = parsed_headers.headers[':status']
|
|
1151
|
+
delete parsed_headers.headers[':status']
|
|
1152
|
+
|
|
1153
|
+
// create our own fake response object,
|
|
1154
|
+
// to mimik fetch's response object,
|
|
1155
|
+
// feeding the user our request data from the multiplexer
|
|
1156
|
+
var res = new Response(new ReadableStream({
|
|
1157
|
+
async start(controller) {
|
|
1158
|
+
try {
|
|
1159
|
+
await process_buffers(() => {
|
|
1160
|
+
var b = buffers.shift()
|
|
1161
|
+
if (!b) return true
|
|
1162
|
+
controller.enqueue(b)
|
|
1163
|
+
})
|
|
1164
|
+
} catch (e) {
|
|
1165
|
+
controller.error(e)
|
|
1166
|
+
} finally { controller.close() }
|
|
1167
|
+
}
|
|
1168
|
+
}), {
|
|
1169
|
+
status,
|
|
1170
|
+
headers: parsed_headers.headers
|
|
1171
|
+
})
|
|
1172
|
+
|
|
1173
|
+
// add a convenience property for the user to know if
|
|
1174
|
+
// this response is being multiplexed
|
|
1175
|
+
res.is_multiplexed = true
|
|
1176
|
+
|
|
1177
|
+
// return the fake response object
|
|
1178
|
+
return res
|
|
1179
|
+
} catch (e) {
|
|
1180
|
+
// if we had an error, be sure to unregister ourselves
|
|
1181
|
+
unset(e)
|
|
1182
|
+
throw mux_error || e
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1176
1185
|
}
|
|
1177
1186
|
|
|
1178
1187
|
// waits on reader for chunks like: 123 bytes for request ABC\r\n..123 bytes..
|