@web/dev-server-core
Advanced tools
Comparing version 0.2.10 to 0.2.11
# @web/dev-server-core | ||
## 0.2.11 | ||
### Patch Changes | ||
- 2291ca1: export websocket types | ||
## 0.2.10 | ||
@@ -4,0 +10,0 @@ |
@@ -0,9 +1,11 @@ | ||
export { FSWatcher } from 'chokidar'; | ||
export { default as Koa, Context, Middleware } from 'koa'; | ||
export { Server } from 'net'; | ||
import WebSocket from 'ws'; | ||
export { WebSocket }; | ||
export { DevServer } from './server/DevServer'; | ||
export { Plugin } from './Plugin'; | ||
export { Plugin, ServerStartParams } from './Plugin'; | ||
export { DevServerCoreConfig, MimeTypeMappings } from './DevServerCoreConfig'; | ||
export { WebSocketsManager } from './web-sockets/WebSocketsManager'; | ||
export { getRequestBrowserPath, getRequestFilePath, getResponseBody, getHtmlPath, isInlineScriptRequest, } from './utils'; | ||
export { FSWatcher } from 'chokidar'; | ||
export { default as Koa, Context, Middleware } from 'koa'; | ||
export { Server } from 'net'; | ||
export { Logger, ErrorWithLocation } from './logger/Logger'; | ||
@@ -10,0 +12,0 @@ export { PluginSyntaxError } from './logger/PluginSyntaxError'; |
@@ -6,3 +6,12 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PluginError = exports.PluginSyntaxError = exports.Server = exports.Koa = exports.FSWatcher = exports.isInlineScriptRequest = exports.getHtmlPath = exports.getResponseBody = exports.getRequestFilePath = exports.getRequestBrowserPath = exports.WebSocketsManager = exports.DevServer = void 0; | ||
exports.PluginError = exports.PluginSyntaxError = exports.isInlineScriptRequest = exports.getHtmlPath = exports.getResponseBody = exports.getRequestFilePath = exports.getRequestBrowserPath = exports.WebSocketsManager = exports.DevServer = exports.WebSocket = exports.Server = exports.Koa = exports.FSWatcher = void 0; | ||
// reexports of types from internal libraries | ||
var chokidar_1 = require("chokidar"); | ||
Object.defineProperty(exports, "FSWatcher", { enumerable: true, get: function () { return chokidar_1.FSWatcher; } }); | ||
var koa_1 = require("koa"); | ||
Object.defineProperty(exports, "Koa", { enumerable: true, get: function () { return __importDefault(koa_1).default; } }); | ||
var net_1 = require("net"); | ||
Object.defineProperty(exports, "Server", { enumerable: true, get: function () { return net_1.Server; } }); | ||
const ws_1 = __importDefault(require("ws")); | ||
exports.WebSocket = ws_1.default; | ||
var DevServer_1 = require("./server/DevServer"); | ||
@@ -18,8 +27,2 @@ Object.defineProperty(exports, "DevServer", { enumerable: true, get: function () { return DevServer_1.DevServer; } }); | ||
Object.defineProperty(exports, "isInlineScriptRequest", { enumerable: true, get: function () { return utils_1.isInlineScriptRequest; } }); | ||
var chokidar_1 = require("chokidar"); | ||
Object.defineProperty(exports, "FSWatcher", { enumerable: true, get: function () { return chokidar_1.FSWatcher; } }); | ||
var koa_1 = require("koa"); | ||
Object.defineProperty(exports, "Koa", { enumerable: true, get: function () { return __importDefault(koa_1).default; } }); | ||
var net_1 = require("net"); | ||
Object.defineProperty(exports, "Server", { enumerable: true, get: function () { return net_1.Server; } }); | ||
var PluginSyntaxError_1 = require("./logger/PluginSyntaxError"); | ||
@@ -26,0 +29,0 @@ Object.defineProperty(exports, "PluginSyntaxError", { enumerable: true, get: function () { return PluginSyntaxError_1.PluginSyntaxError; } }); |
@@ -24,3 +24,3 @@ /// <reference types="node" /> | ||
}; | ||
export interface ServerArgs { | ||
export interface ServerStartParams { | ||
config: DevServerCoreConfig; | ||
@@ -36,3 +36,3 @@ app: Koa; | ||
injectWebSocket?: boolean; | ||
serverStart?(args: ServerArgs): void | Promise<void>; | ||
serverStart?(args: ServerStartParams): void | Promise<void>; | ||
serverStop?(): void | Promise<void>; | ||
@@ -39,0 +39,0 @@ serve?(context: Context): ServeResult | Promise<ServeResult>; |
@@ -6,6 +6,9 @@ /// <reference types="node" /> | ||
export declare const NAME_WEB_SOCKET_IMPORT = "/__web-dev-server__web-socket.js"; | ||
declare type WebSocketData = { | ||
type: string; | ||
} & Record<string, unknown>; | ||
export interface Events { | ||
message: { | ||
webSocket: WebSocket; | ||
data: WebSocket.Data; | ||
data: WebSocketData; | ||
}; | ||
@@ -46,2 +49,3 @@ } | ||
} | ||
export {}; | ||
//# sourceMappingURL=WebSocketsManager.d.ts.map |
@@ -25,4 +25,14 @@ "use strict"; | ||
}); | ||
webSocket.on('message', data => { | ||
this.emit('message', { webSocket, data }); | ||
webSocket.on('message', rawData => { | ||
try { | ||
const data = JSON.parse(rawData.toString()); | ||
if (!data.type) { | ||
throw new Error('Missing property "type".'); | ||
} | ||
this.emit('message', { webSocket, data }); | ||
} | ||
catch (error) { | ||
console.error('Failed to parse websocket event received from the browser: ', rawData); | ||
console.error(error); | ||
} | ||
}); | ||
@@ -29,0 +39,0 @@ }); |
@@ -15,2 +15,57 @@ "use strict"; | ||
return `export const webSocket = ('WebSocket' in window) ? new WebSocket(\`ws\${location.protocol === 'https:' ? 's': ''}://\${location.host}\`) : null; | ||
export const webSocketOpened = new Promise((resolve) => { | ||
if (!webSocket) { | ||
resolve(); | ||
} else { | ||
webSocket.addEventListener('open', () => { | ||
resolve(); | ||
}); | ||
} | ||
}); | ||
let messageId = 0; | ||
function nextMessageId() { | ||
if (messageId >= Number.MAX_SAFE_INTEGER) { | ||
messageId = 0; | ||
} | ||
messageId += 1; | ||
return messageId; | ||
} | ||
export async function sendMessage(message) { | ||
if (!message.type) { | ||
throw new Error('Missing message type'); | ||
} | ||
await webSocketOpened; | ||
webSocket.send(JSON.stringify(message)); | ||
} | ||
// sends a websocket message and expects a response from the server | ||
export function sendMessageWaitForResponse(message) { | ||
return new Promise(async (resolve, reject) => { | ||
const id = nextMessageId(); | ||
function onResponse(e) { | ||
const message = JSON.parse(e.data); | ||
if (message.type === 'message-response' && message.id === id) { | ||
webSocket.removeEventListener('message', onResponse); | ||
if (message.error) { | ||
reject(new Error(message.error)) | ||
} else { | ||
resolve(message.response); | ||
} | ||
} | ||
} | ||
webSocket.addEventListener('message', onResponse); | ||
const timeoutId = setTimeout(() => { | ||
webSocket.removeEventListener('message', onResponse); | ||
reject(new Error(\`Did not receive a server response for message with type \${message.type} within 20000ms\`)) | ||
}, 20000); | ||
sendMessage({ ...message, id }); | ||
}); | ||
} | ||
if (webSocket) { | ||
@@ -17,0 +72,0 @@ webSocket.addEventListener('message', async (e) => { |
{ | ||
"name": "@web/dev-server-core", | ||
"version": "0.2.10", | ||
"version": "0.2.11", | ||
"publishConfig": { | ||
@@ -5,0 +5,0 @@ "access": "public" |
@@ -0,3 +1,10 @@ | ||
// reexports of types from internal libraries | ||
export { FSWatcher } from 'chokidar'; | ||
export { default as Koa, Context, Middleware } from 'koa'; | ||
export { Server } from 'net'; | ||
import WebSocket from 'ws'; | ||
export { WebSocket }; | ||
export { DevServer } from './server/DevServer'; | ||
export { Plugin } from './Plugin'; | ||
export { Plugin, ServerStartParams } from './Plugin'; | ||
export { DevServerCoreConfig, MimeTypeMappings } from './DevServerCoreConfig'; | ||
@@ -12,7 +19,4 @@ export { WebSocketsManager } from './web-sockets/WebSocketsManager'; | ||
} from './utils'; | ||
export { FSWatcher } from 'chokidar'; | ||
export { default as Koa, Context, Middleware } from 'koa'; | ||
export { Server } from 'net'; | ||
export { Logger, ErrorWithLocation } from './logger/Logger'; | ||
export { PluginSyntaxError } from './logger/PluginSyntaxError'; | ||
export { PluginError } from './logger/PluginError'; |
@@ -20,3 +20,3 @@ import { FSWatcher } from 'chokidar'; | ||
export interface ServerArgs { | ||
export interface ServerStartParams { | ||
config: DevServerCoreConfig; | ||
@@ -33,3 +33,3 @@ app: Koa; | ||
injectWebSocket?: boolean; | ||
serverStart?(args: ServerArgs): void | Promise<void>; | ||
serverStart?(args: ServerStartParams): void | Promise<void>; | ||
serverStop?(): void | Promise<void>; | ||
@@ -36,0 +36,0 @@ serve?(context: Context): ServeResult | Promise<ServeResult>; |
@@ -7,4 +7,6 @@ import { Server } from 'net'; | ||
type WebSocketData = { type: string } & Record<string, unknown>; | ||
export interface Events { | ||
message: { webSocket: WebSocket; data: WebSocket.Data }; | ||
message: { webSocket: WebSocket; data: WebSocketData }; | ||
} | ||
@@ -31,4 +33,13 @@ | ||
webSocket.on('message', data => { | ||
this.emit('message', { webSocket, data }); | ||
webSocket.on('message', rawData => { | ||
try { | ||
const data = JSON.parse(rawData.toString()); | ||
if (!data.type) { | ||
throw new Error('Missing property "type".'); | ||
} | ||
this.emit('message', { webSocket, data }); | ||
} catch (error) { | ||
console.error('Failed to parse websocket event received from the browser: ', rawData); | ||
console.error(error); | ||
} | ||
}); | ||
@@ -35,0 +46,0 @@ }); |
@@ -16,2 +16,57 @@ import { parse as parseHtml } from 'parse5'; | ||
return `export const webSocket = ('WebSocket' in window) ? new WebSocket(\`ws\${location.protocol === 'https:' ? 's': ''}://\${location.host}\`) : null; | ||
export const webSocketOpened = new Promise((resolve) => { | ||
if (!webSocket) { | ||
resolve(); | ||
} else { | ||
webSocket.addEventListener('open', () => { | ||
resolve(); | ||
}); | ||
} | ||
}); | ||
let messageId = 0; | ||
function nextMessageId() { | ||
if (messageId >= Number.MAX_SAFE_INTEGER) { | ||
messageId = 0; | ||
} | ||
messageId += 1; | ||
return messageId; | ||
} | ||
export async function sendMessage(message) { | ||
if (!message.type) { | ||
throw new Error('Missing message type'); | ||
} | ||
await webSocketOpened; | ||
webSocket.send(JSON.stringify(message)); | ||
} | ||
// sends a websocket message and expects a response from the server | ||
export function sendMessageWaitForResponse(message) { | ||
return new Promise(async (resolve, reject) => { | ||
const id = nextMessageId(); | ||
function onResponse(e) { | ||
const message = JSON.parse(e.data); | ||
if (message.type === 'message-response' && message.id === id) { | ||
webSocket.removeEventListener('message', onResponse); | ||
if (message.error) { | ||
reject(new Error(message.error)) | ||
} else { | ||
resolve(message.response); | ||
} | ||
} | ||
} | ||
webSocket.addEventListener('message', onResponse); | ||
const timeoutId = setTimeout(() => { | ||
webSocket.removeEventListener('message', onResponse); | ||
reject(new Error(\`Did not receive a server response for message with type \${message.type} within 20000ms\`)) | ||
}, 20000); | ||
sendMessage({ ...message, id }); | ||
}); | ||
} | ||
if (webSocket) { | ||
@@ -18,0 +73,0 @@ webSocket.addEventListener('message', async (e) => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
261780
4978