@textile/grpc-transport
Advanced tools
Comparing version 0.0.7-alpha.1 to 0.1.0
@@ -6,21 +6,8 @@ # Change Log | ||
## [0.0.7-alpha.1](https://github.com/textileio/js-threads/compare/@textile/grpc-transport@0.0.7-alpha.0...@textile/grpc-transport@0.0.7-alpha.1) (2020-11-13) | ||
# [0.1.0](https://github.com/textileio/js-threads/compare/@textile/grpc-transport@0.0.6...@textile/grpc-transport@0.1.0) (2020-11-25) | ||
### Bug Fixes | ||
* **releases:** point to correct module file ([8546aba](https://github.com/textileio/js-threads/commit/8546aba1152bca503baa0de6443a02e47acbf3ca)) | ||
## [0.0.7-alpha.0](https://github.com/textileio/js-threads/compare/@textile/grpc-transport@0.0.6...@textile/grpc-transport@0.0.7-alpha.0) (2020-11-13) | ||
### Features | ||
* **builds:** adds module path and build ([9d029ef](https://github.com/textileio/js-threads/commit/9d029ef44c39d3019773c772bf8d483bcdf3be1a)) | ||
* **builds:** rm umd for now; update build scripts to all use bili ([4757daf](https://github.com/textileio/js-threads/commit/4757dafa316b4e2c84c8ea8d2ad35206ad7737d4)) | ||
* **bundle:** try out bili for packaging ([5df79c4](https://github.com/textileio/js-threads/commit/5df79c4c0dbd1def9b3e5a4c84a21ac787a01663)) | ||
* remove buffers + multiaddr optims ([3c57820](https://github.com/textileio/js-threads/commit/3c578203b8614aad0e892832b8efcc90d6e13fac)) | ||
@@ -27,0 +14,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { grpc } from '@improbable-eng/grpc-web'; | ||
import { grpc } from "@improbable-eng/grpc-web"; | ||
export declare function WebsocketTransport(): grpc.TransportFactory; |
@@ -1,2 +0,114 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("isomorphic-ws"),t=require("loglevel");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r,o=n(e),s=n(t).default.getLogger("grpc-transport").debug;!function(e){e[e.FINISH_SEND=1]="FINISH_SEND"}(r||(r={}));var u=new Uint8Array([1]);function c(e){var t="";return e.forEach((function(e,n){t+="".concat(e,": ").concat(n.join(", "),"\r\n")})),function(e){for(var t,n,r=new Uint8Array(e.length),o=0;o!==e.length;++o){var s=e.charCodeAt(o);if(n=void 0,!(9===(n=t=s)||10===n||13===n||t>=32&&t<=126))throw new Error("Metadata contains invalid ASCII");r[o]=s}return r}(t)}function a(e){e.debug&&s("websocketRequest",e);var t,n=function(e){if("https://"===e.substr(0,8))return"wss://".concat(e.substr(8));if("http://"===e.substr(0,7))return"ws://".concat(e.substr(7));throw new Error("Websocket transport constructed with non-https:// or http:// host.")}(e.url),a=[];function i(e){if(e===r.FINISH_SEND)t.send(u);else{var n=e,o=new Int8Array(n.byteLength+1);o.set(new Uint8Array([0])),o.set(n,1),t.send(o)}}return{sendMessage:function(e){t&&t.readyState!==t.CONNECTING?i(e):a.push(e)},finishSend:function(){t&&t.readyState!==t.CONNECTING?i(r.FINISH_SEND):a.push(r.FINISH_SEND)},start:function(r){(t=new o.default(n,["grpc-websockets"])).binaryType="arraybuffer",t.onopen=function(){e.debug&&s("websocketRequest.onopen"),t.send(c(r)),a.forEach((function(e){i(e)}))},t.onclose=function(t){e.debug&&s("websocketRequest.onclose",t),e.onEnd()},t.onerror=function(t){e.debug&&s("websocketRequest.onerror",t)},t.onmessage=function(t){e.onChunk(new Uint8Array(t.data))}},cancel:function(){e.debug&&s("websocket.abort"),t.close()}}}exports.WebsocketTransport=function(){return function(e){return a(e)}}; | ||
//# sourceMappingURL=index.js.map | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.WebsocketTransport = void 0; | ||
const isomorphic_ws_1 = __importDefault(require("isomorphic-ws")); | ||
const loglevel_1 = __importDefault(require("loglevel")); | ||
const { debug } = loglevel_1.default.getLogger("grpc-transport"); | ||
const isAllowedControlChars = (char) => char === 0x9 || char === 0xa || char === 0xd; | ||
function isValidHeaderAscii(val) { | ||
return isAllowedControlChars(val) || (val >= 0x20 && val <= 0x7e); | ||
} | ||
function encodeASCII(input) { | ||
const encoded = new Uint8Array(input.length); | ||
for (let i = 0; i !== input.length; ++i) { | ||
const charCode = input.charCodeAt(i); | ||
if (!isValidHeaderAscii(charCode)) { | ||
throw new Error("Metadata contains invalid ASCII"); | ||
} | ||
encoded[i] = charCode; | ||
} | ||
return encoded; | ||
} | ||
var WebsocketSignal; | ||
(function (WebsocketSignal) { | ||
WebsocketSignal[WebsocketSignal["FINISH_SEND"] = 1] = "FINISH_SEND"; | ||
})(WebsocketSignal || (WebsocketSignal = {})); | ||
const finishSendFrame = new Uint8Array([1]); | ||
function constructWebSocketAddress(url) { | ||
if (url.substr(0, 8) === "https://") { | ||
return `wss://${url.substr(8)}`; | ||
} | ||
else if (url.substr(0, 7) === "http://") { | ||
return `ws://${url.substr(7)}`; | ||
} | ||
throw new Error("Websocket transport constructed with non-https:// or http:// host."); | ||
} | ||
function headersToBytes(headers) { | ||
let asString = ""; | ||
headers.forEach((key, values) => { | ||
asString += `${key}: ${values.join(", ")}\r\n`; | ||
}); | ||
return encodeASCII(asString); | ||
} | ||
function websocketRequest(options) { | ||
options.debug && debug("websocketRequest", options); | ||
const webSocketAddress = constructWebSocketAddress(options.url); | ||
const sendQueue = []; | ||
let ws; | ||
function sendToWebsocket(toSend) { | ||
if (toSend === WebsocketSignal.FINISH_SEND) { | ||
ws.send(finishSendFrame); | ||
} | ||
else { | ||
const byteArray = toSend; | ||
const c = new Int8Array(byteArray.byteLength + 1); | ||
c.set(new Uint8Array([0])); | ||
c.set(byteArray, 1); | ||
ws.send(c); | ||
} | ||
} | ||
return { | ||
sendMessage: (msgBytes) => { | ||
if (!ws || ws.readyState === ws.CONNECTING) { | ||
sendQueue.push(msgBytes); | ||
} | ||
else { | ||
sendToWebsocket(msgBytes); | ||
} | ||
}, | ||
finishSend: () => { | ||
if (!ws || ws.readyState === ws.CONNECTING) { | ||
sendQueue.push(WebsocketSignal.FINISH_SEND); | ||
} | ||
else { | ||
sendToWebsocket(WebsocketSignal.FINISH_SEND); | ||
} | ||
}, | ||
start: (metadata) => { | ||
ws = new isomorphic_ws_1.default(webSocketAddress, ["grpc-websockets"]); | ||
ws.binaryType = "arraybuffer"; | ||
ws.onopen = function () { | ||
options.debug && debug("websocketRequest.onopen"); | ||
ws.send(headersToBytes(metadata)); | ||
// send any messages that were passed to sendMessage before the connection was ready | ||
sendQueue.forEach((toSend) => { | ||
sendToWebsocket(toSend); | ||
}); | ||
}; | ||
ws.onclose = function (closeEvent) { | ||
options.debug && debug("websocketRequest.onclose", closeEvent); | ||
options.onEnd(); | ||
}; | ||
ws.onerror = function (error) { | ||
options.debug && debug("websocketRequest.onerror", error); | ||
}; | ||
ws.onmessage = function (e) { | ||
options.onChunk(new Uint8Array(e.data)); | ||
}; | ||
}, | ||
cancel: () => { | ||
options.debug && debug("websocket.abort"); | ||
ws.close(); | ||
}, | ||
}; | ||
} | ||
function WebsocketTransport() { | ||
return (opts) => { | ||
return websocketRequest(opts); | ||
}; | ||
} | ||
exports.WebsocketTransport = WebsocketTransport; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@textile/grpc-transport", | ||
"version": "0.0.7-alpha.1", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"module": "dist/index.esm.js", | ||
"version": "0.1.0", | ||
"main": "dist/index", | ||
"types": "dist/index", | ||
"files": [ | ||
@@ -16,6 +15,6 @@ "dist/**/!(*.spec).js?(.map)", | ||
"scripts": { | ||
"build": "bili --config ../../bili.config.js", | ||
"prepublishOnly": "npm run build", | ||
"prepare": "npm run build", | ||
"prebuild": "npm run clean", | ||
"build": "npx tsc -b tsconfig.json", | ||
"clean": "npx rimraf ./dist ./tsconfig.tsbuildinfo" | ||
@@ -31,3 +30,3 @@ }, | ||
}, | ||
"gitHead": "4082f562a628a2012965b1d6629b7613a697c64a" | ||
"gitHead": "e6d84bffbba928d804accc562f1766838479982d" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
115
2
15668
7