apollo-link-webextensions-messaging
Advanced tools
Comparing version 1.0.3 to 1.0.4-rc.0
@@ -34,3 +34,6 @@ "use strict"; | ||
if (rpcs_1.isOperationRequestRPC(message)) { | ||
var params = message.params; | ||
var m = rpcs_1.parseRPCNotificationMessage(message); | ||
if (m === null) | ||
return; | ||
var params = m.params; | ||
var operationId_1 = params.operationId; | ||
@@ -46,3 +49,6 @@ var request = { | ||
var operationOnMessageListener_1 = function (message) { | ||
if (rpcs_1.isOperationUnsubscribeRPC(message, operationId_1)) { | ||
var m = rpcs_1.parseRPCNotificationMessage(message); | ||
if (m === null) | ||
return; | ||
if (rpcs_1.isOperationUnsubscribeRPC(m, operationId_1)) { | ||
close_1(); | ||
@@ -88,10 +94,13 @@ } | ||
var onMessageListener = function (message) { | ||
if (rpcs_1.isOperationResultRPC(message, operationId)) { | ||
observer.next(message.params.result); | ||
var m = rpcs_1.parseRPCNotificationMessage(message); | ||
if (m === null) | ||
return; | ||
if (rpcs_1.isOperationResultRPC(m, operationId)) { | ||
observer.next(m.params.result); | ||
} | ||
if (rpcs_1.isOperationCompleteRPC(message, operationId)) { | ||
if (rpcs_1.isOperationCompleteRPC(m, operationId)) { | ||
observer.complete(); | ||
} | ||
if (rpcs_1.isOperationErrorRPC(message, operationId)) { | ||
observer.error(new Error(message.params.errorMessage)); | ||
if (rpcs_1.isOperationErrorRPC(m, operationId)) { | ||
observer.error(new Error(m.params.errorMessage)); | ||
} | ||
@@ -98,0 +107,0 @@ }; |
@@ -10,2 +10,3 @@ import { Operation } from 'apollo-link'; | ||
export declare const isRPCNotificationMessage: <T>(message: Message) => message is RPCNotificationMessage<T>; | ||
export declare const parseRPCNotificationMessage: <T>(message: Message) => RPCNotificationMessage<T> | null; | ||
export declare type OperationRequestRPC = RPCNotificationMessage<{ | ||
@@ -19,3 +20,3 @@ operationId: string; | ||
export declare const OPERATION_REQUEST_METHOD = "operation-request"; | ||
export declare const operationRequestRPC: (operationId: string, operation: Operation) => OperationRequestRPC; | ||
export declare const operationRequestRPC: (operationId: string, operation: Operation) => RPCNotificationMessage<string>; | ||
export declare const isOperationRequestRPC: (message: Message) => message is RPCNotificationMessage<{ | ||
@@ -37,3 +38,3 @@ operationId: string; | ||
export declare const OPERATION_RESULT_METHOD = "operation-result"; | ||
export declare const operationResultRPC: (operationId: string, result: ExecutionResult) => OperationResultRPC; | ||
export declare const operationResultRPC: (operationId: string, result: ExecutionResult) => RPCNotificationMessage<string>; | ||
export declare const isOperationResultRPC: (message: Message, operationId: string) => message is RPCNotificationMessage<{ | ||
@@ -48,3 +49,3 @@ operationId: string; | ||
export declare const OPERATION_ERROR_METHOD = "operation-error"; | ||
export declare const operationErrorRPC: (operationId: string, errorValue: unknown) => OperationErrorRPC; | ||
export declare const operationErrorRPC: (operationId: string, errorValue: unknown) => RPCNotificationMessage<string>; | ||
export declare const isOperationErrorRPC: (message: Message, operationId: string) => message is RPCNotificationMessage<{ | ||
@@ -58,3 +59,3 @@ operationId: string; | ||
export declare const OPERATION_COMPLETE_METHOD = "operation-complete"; | ||
export declare const operationCompleteRPC: (operationId: string) => OperationCompleteRPC; | ||
export declare const operationCompleteRPC: (operationId: string) => RPCNotificationMessage<string>; | ||
export declare const isOperationCompleteRPC: (message: Message, operationId: string) => message is RPCNotificationMessage<{ | ||
@@ -67,5 +68,5 @@ operationId: string; | ||
export declare const OPERATION_UNSUBSCRIBE_METHOD = "operation-unsubscribe"; | ||
export declare const operationUnsubscribeRPC: (operationId: string) => OperationUnsubscribeRPC; | ||
export declare const operationUnsubscribeRPC: (operationId: string) => RPCNotificationMessage<string>; | ||
export declare const isOperationUnsubscribeRPC: (message: Message, operationId: string) => message is RPCNotificationMessage<{ | ||
operationId: string; | ||
}>; |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isOperationUnsubscribeRPC = exports.operationUnsubscribeRPC = exports.OPERATION_UNSUBSCRIBE_METHOD = exports.isOperationCompleteRPC = exports.operationCompleteRPC = exports.OPERATION_COMPLETE_METHOD = exports.isOperationErrorRPC = exports.operationErrorRPC = exports.OPERATION_ERROR_METHOD = exports.isOperationResultRPC = exports.operationResultRPC = exports.OPERATION_RESULT_METHOD = exports.isOperationRequestRPC = exports.operationRequestRPC = exports.OPERATION_REQUEST_METHOD = exports.isRPCNotificationMessage = void 0; | ||
exports.isOperationUnsubscribeRPC = exports.operationUnsubscribeRPC = exports.OPERATION_UNSUBSCRIBE_METHOD = exports.isOperationCompleteRPC = exports.operationCompleteRPC = exports.OPERATION_COMPLETE_METHOD = exports.isOperationErrorRPC = exports.operationErrorRPC = exports.OPERATION_ERROR_METHOD = exports.isOperationResultRPC = exports.operationResultRPC = exports.OPERATION_RESULT_METHOD = exports.isOperationRequestRPC = exports.operationRequestRPC = exports.OPERATION_REQUEST_METHOD = exports.parseRPCNotificationMessage = exports.isRPCNotificationMessage = void 0; | ||
var graphql_1 = require("graphql"); | ||
@@ -8,2 +19,15 @@ exports.isRPCNotificationMessage = function (message) { | ||
}; | ||
exports.parseRPCNotificationMessage = function (message) { | ||
if (!exports.isRPCNotificationMessage(message)) | ||
return null; | ||
if (typeof message.params !== 'string') | ||
return null; | ||
try { | ||
var parsed = JSON.parse(message.params); | ||
return __assign(__assign({}, message), { params: parsed }); | ||
} | ||
catch (e) { | ||
return null; | ||
} | ||
}; | ||
var isRecord = function (r) { return typeof r === 'object' && r !== null; }; | ||
@@ -14,3 +38,3 @@ exports.OPERATION_REQUEST_METHOD = 'operation-request'; | ||
method: exports.OPERATION_REQUEST_METHOD, | ||
params: { | ||
params: JSON.stringify({ | ||
operationId: operationId, | ||
@@ -21,3 +45,3 @@ operationName: operation.operationName, | ||
context: operation.getContext(), | ||
} | ||
}) | ||
}); }; | ||
@@ -31,6 +55,6 @@ exports.isOperationRequestRPC = function (message) { | ||
method: exports.OPERATION_RESULT_METHOD, | ||
params: { | ||
params: JSON.stringify({ | ||
operationId: operationId, | ||
result: result, | ||
} | ||
}) | ||
}); }; | ||
@@ -51,6 +75,6 @@ exports.isOperationResultRPC = function (message, operationId) { | ||
method: exports.OPERATION_ERROR_METHOD, | ||
params: { | ||
params: JSON.stringify({ | ||
operationId: operationId, | ||
errorMessage: errorMessage, | ||
} | ||
}) | ||
}; | ||
@@ -67,5 +91,5 @@ }; | ||
method: exports.OPERATION_COMPLETE_METHOD, | ||
params: { | ||
params: JSON.stringify({ | ||
operationId: operationId, | ||
} | ||
}) | ||
}); }; | ||
@@ -81,5 +105,5 @@ exports.isOperationCompleteRPC = function (message, operationId) { | ||
method: exports.OPERATION_UNSUBSCRIBE_METHOD, | ||
params: { | ||
params: JSON.stringify({ | ||
operationId: operationId, | ||
} | ||
}) | ||
}); }; | ||
@@ -86,0 +110,0 @@ exports.isOperationUnsubscribeRPC = function (message, operationId) { |
{ | ||
"name": "apollo-link-webextensions-messaging", | ||
"version": "1.0.3", | ||
"version": "1.0.4-rc.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -15,3 +15,3 @@ import { ApolloLink, GraphQLRequest, execute, Observable } from 'apollo-link'; | ||
isOperationErrorRPC, | ||
isOperationUnsubscribeRPC, | ||
isOperationUnsubscribeRPC, parseRPCNotificationMessage, OperationRequestRPC, | ||
} from './rpcs'; | ||
@@ -41,5 +41,7 @@ import { MessagingPort, Message } from './types'; | ||
return (port: T): void => { | ||
port.onMessage.addListener(message => { | ||
port.onMessage.addListener((message: Message) => { | ||
if (isOperationRequestRPC(message)) { | ||
const { params } = message; | ||
const m = parseRPCNotificationMessage(message) as OperationRequestRPC; | ||
if (m === null) return; | ||
const { params } = m; | ||
const { operationId } = params; | ||
@@ -58,3 +60,6 @@ const request: GraphQLRequest = { | ||
const operationOnMessageListener = (message: Message): void => { | ||
if (isOperationUnsubscribeRPC(message, operationId)) { | ||
const m = parseRPCNotificationMessage(message); | ||
if (m === null) return; | ||
if (isOperationUnsubscribeRPC(m, operationId)) { | ||
close(); | ||
@@ -108,10 +113,13 @@ } | ||
const onMessageListener = (message: Message): void => { | ||
if (isOperationResultRPC(message, operationId)) { | ||
observer.next(message.params.result); | ||
const m = parseRPCNotificationMessage(message); | ||
if (m === null) return; | ||
if (isOperationResultRPC(m, operationId)) { | ||
observer.next(m.params.result); | ||
} | ||
if (isOperationCompleteRPC(message, operationId)) { | ||
if (isOperationCompleteRPC(m, operationId)) { | ||
observer.complete(); | ||
} | ||
if (isOperationErrorRPC(message, operationId)) { | ||
observer.error(new Error(message.params.errorMessage)); | ||
if (isOperationErrorRPC(m, operationId)) { | ||
observer.error(new Error(m.params.errorMessage)); | ||
} | ||
@@ -118,0 +126,0 @@ }; |
@@ -13,2 +13,18 @@ import { Operation } from 'apollo-link'; | ||
export const parseRPCNotificationMessage = <T>(message: Message): RPCNotificationMessage<T> | null => { | ||
if (!isRPCNotificationMessage(message)) return null; | ||
if (typeof message.params !== 'string') return null; | ||
try { | ||
const parsed = JSON.parse(message.params); | ||
return { | ||
...message, | ||
params: parsed, | ||
} | ||
} catch(e) { | ||
return null; | ||
} | ||
}; | ||
const isRecord = (r: unknown): r is Record<string, unknown> => typeof r === 'object' && r !== null; | ||
@@ -25,6 +41,6 @@ | ||
export const OPERATION_REQUEST_METHOD = 'operation-request'; | ||
export const operationRequestRPC = (operationId: string, operation: Operation): OperationRequestRPC => ({ | ||
export const operationRequestRPC = (operationId: string, operation: Operation): RPCNotificationMessage<string> => ({ | ||
jsonrpc: '2.0', | ||
method: OPERATION_REQUEST_METHOD, | ||
params: { | ||
params: JSON.stringify({ | ||
operationId, | ||
@@ -35,3 +51,3 @@ operationName: operation.operationName, | ||
context: operation.getContext(), | ||
} | ||
}) | ||
}); | ||
@@ -51,9 +67,9 @@ export const isOperationRequestRPC = (message: Message): message is OperationRequestRPC => | ||
export const OPERATION_RESULT_METHOD = 'operation-result'; | ||
export const operationResultRPC = (operationId: string, result: ExecutionResult): OperationResultRPC => ({ | ||
export const operationResultRPC = (operationId: string, result: ExecutionResult): RPCNotificationMessage<string> => ({ | ||
jsonrpc: '2.0', | ||
method: OPERATION_RESULT_METHOD, | ||
params: { | ||
params: JSON.stringify({ | ||
operationId, | ||
result, | ||
} | ||
}) | ||
}); | ||
@@ -71,3 +87,3 @@ export const isOperationResultRPC = (message: Message, operationId: string): message is OperationResultRPC => | ||
export const OPERATION_ERROR_METHOD = 'operation-error'; | ||
export const operationErrorRPC = (operationId: string, errorValue: unknown): OperationErrorRPC => { | ||
export const operationErrorRPC = (operationId: string, errorValue: unknown): RPCNotificationMessage<string> => { | ||
let errorMessage = '<unknow error>'; | ||
@@ -80,6 +96,6 @@ if (errorValue instanceof Error) { | ||
method: OPERATION_ERROR_METHOD, | ||
params: { | ||
params: JSON.stringify({ | ||
operationId, | ||
errorMessage, | ||
} | ||
}) | ||
}; | ||
@@ -96,8 +112,8 @@ }; | ||
export const OPERATION_COMPLETE_METHOD = 'operation-complete'; | ||
export const operationCompleteRPC = (operationId: string): OperationCompleteRPC => ({ | ||
export const operationCompleteRPC = (operationId: string): RPCNotificationMessage<string> => ({ | ||
jsonrpc: '2.0', | ||
method: OPERATION_COMPLETE_METHOD, | ||
params: { | ||
params: JSON.stringify({ | ||
operationId, | ||
} | ||
}) | ||
}); | ||
@@ -114,8 +130,8 @@ export const isOperationCompleteRPC = (message: Message, operationId: string): message is OperationCompleteRPC => | ||
export const OPERATION_UNSUBSCRIBE_METHOD = 'operation-unsubscribe'; | ||
export const operationUnsubscribeRPC = (operationId: string): OperationUnsubscribeRPC => ({ | ||
export const operationUnsubscribeRPC = (operationId: string): RPCNotificationMessage<string> => ({ | ||
jsonrpc: '2.0', | ||
method: OPERATION_UNSUBSCRIBE_METHOD, | ||
params: { | ||
params: JSON.stringify({ | ||
operationId, | ||
} | ||
}) | ||
}); | ||
@@ -122,0 +138,0 @@ export const isOperationUnsubscribeRPC = (message: Message, operationId: string): message is OperationUnsubscribeRPC => |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
46698
1087
2
1