Skip to content

Commit

Permalink
feat(websocket): Enable to run functions in /websocket on the serve…
Browse files Browse the repository at this point in the history
…r side and make the `/websocket` submodule

`/browser/websocket` will be deprecated
  • Loading branch information
takker99 committed Jan 23, 2025
1 parent c6561a0 commit 22d91dd
Show file tree
Hide file tree
Showing 35 changed files with 95 additions and 77 deletions.
2 changes: 1 addition & 1 deletion browser/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./dom/mod.ts";
export * from "./websocket/mod.ts";
export * from "../websocket/mod.ts";
104 changes: 53 additions & 51 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
{
"name": "@cosense/std",
"version": "0.0.0",
"tasks": {
"fix": {
"command": "deno fmt && deno lint --fix && deno publish --dry-run --allow-dirty",
"dependencies": [
"type-check",
"test"
]
},
"check": {
"command": "deno fmt --check && deno lint && deno publish --dry-run",
"dependencies": [
"type-check",
"test"
]
},
"type-check": "deno check --remote **/*.ts",
"test": "deno test --allow-read=./ --doc --parallel --shuffle --no-check",
"coverage": "deno test --allow-read=./ --parallel --shuffle --coverage --no-check && deno coverage --html",
"doc": "deno doc --html mod.ts",
// from https://github.com/jsr-core/unknownutil/blob/v4.2.2/deno.jsonc#L84-L85
"update": "deno outdated --update",
"update:commit": "deno task -q update --commit --prefix deps: --pre-commit=fix"
"compilerOptions": {
"lib": [
"esnext",
"dom",
"dom.iterable",
"deno.ns"
]
},
"exclude": [
"coverage/",
"docs/"
],
"exports": {
".": "./mod.ts",
"./browser": "./browser/mod.ts",
"./browser/dom": "./browser/dom/mod.ts",
"./browser/websocket": "./websocket/mod.ts",
"./parseAbsoluteLink": "./parseAbsoluteLink.ts",
"./rest": "./rest/mod.ts",
"./text": "./text.ts",
"./title": "./title.ts",
"./websocket": "./websocket/mod.ts"
},
"imports": {
"@cosense/std/rest": "./rest/mod.ts",
"@cosense/std/browser/websocket": "./browser/websocket/mod.ts",
"@core/unknownutil": "jsr:@core/unknownutil@^4.0.0",
"@cosense/std/browser/websocket": "./websocket/mod.ts",
"@cosense/std/rest": "./rest/mod.ts",
"@cosense/std/websocket": "./websocket/mod.ts",
"@cosense/types": "jsr:@cosense/types@^0.10.4",
"@cosense/types/rest": "jsr:@cosense/types@0.10/rest",
"@cosense/types/userscript": "jsr:@cosense/types@0.10/userscript",
Expand All @@ -42,44 +41,47 @@
"option-t": "npm:option-t@^51.0.0",
"socket.io-client": "npm:socket.io-client@^4.7.5"
},
"exports": {
".": "./mod.ts",
"./rest": "./rest/mod.ts",
"./browser": "./browser/mod.ts",
"./browser/dom": "./browser/dom/mod.ts",
"./browser/websocket": "./browser/websocket/mod.ts",
"./parseAbsoluteLink": "./parseAbsoluteLink.ts",
"./title": "./title.ts",
"./text": "./text.ts"
},
"compilerOptions": {
"lib": [
"esnext",
"dom",
"dom.iterable",
"deno.ns"
]
},
"exclude": [
"coverage/",
"docs/"
],
"lint": {
"exclude": [
"vendor/"
]
},
"name": "@cosense/std",
"tasks": {
"check": {
"command": "deno fmt --check && deno lint && deno publish --dry-run",
"dependencies": [
"type-check",
"test"
]
},
"coverage": "deno test --allow-read=./ --parallel --shuffle --coverage --no-check && deno coverage --html",
"doc": "deno doc --html mod.ts",
"fix": {
"command": "deno fmt && deno lint --fix && deno publish --dry-run --allow-dirty",
"dependencies": [
"type-check",
"test"
]
},
"test": "deno test --allow-read=./ --doc --parallel --shuffle --no-check",
"type-check": "deno check --remote **/*.ts",
// from https://github.com/jsr-core/unknownutil/blob/v4.2.2/deno.jsonc#L84-L85
"update": "deno outdated --update",
"update:commit": "deno task -q update --commit --prefix deps: --pre-commit=fix"
},
"test": {
"exclude": [
"README.md",
"./browser/websocket/listen.ts",
"./browser/websocket/updateCodeFile.ts",
"./websocket/listen.ts",
"./websocket/updateCodeFile.ts",
"./rest/getCachedAt.ts",
"./rest/getCodeBlocks.ts",
"./rest/getGyazoToken.ts",
"./rest/getTweetInfo.ts",
"./rest/getWebPageTitle.ts",
"./rest/link.ts"
]
}
},
"version": "0.0.0"
}
2 changes: 1 addition & 1 deletion rest/getCodeBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { BaseLine } from "@cosense/types/rest";
import {
type CodeTitle,
extractFromCodeTitle,
} from "../browser/websocket/_codeBlock.ts";
} from "../websocket/_codeBlock.ts";

/** Minimal information about a code block that can be extracted from pull() response
*
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TinyCodeBlock } from "../../rest/getCodeBlocks.ts";
import type { TinyCodeBlock } from "../rest/getCodeBlocks.ts";

/** Interface for storing code block title line information
*
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { diff, toExtendedChanges } from "../../deps/onp.ts";
import { diff, toExtendedChanges } from "../deps/onp.ts";
import type { Line } from "@cosense/types/userscript";
import type { DeleteChange, InsertChange, UpdateChange } from "./change.ts";
import { createNewLineId } from "./id.ts";
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Node, parse } from "@progfay/scrapbox-parser";
import type { BaseLine } from "@cosense/types/userscript";
import { toTitleLc } from "../../title.ts";
import { parseYoutube } from "../../parser/youtube.ts";
import { toTitleLc } from "../title.ts";
import { parseYoutube } from "../parser/youtube.ts";

/** Extract metadata from Scrapbox page text
*
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions browser/websocket/listen.ts → websocket/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type {
NotLoggedInError,
NotMemberError,
} from "@cosense/types/rest";
import type { HTTPError } from "../../rest/responseIntoResult.ts";
import type { AbortError, NetworkError } from "../../rest/robustFetch.ts";
import type { HTTPError } from "../rest/responseIntoResult.ts";
import type { AbortError, NetworkError } from "../rest/robustFetch.ts";
import type { ScrapboxSocket } from "./socket.ts";
import type { ListenEvents } from "./listen-events.ts";

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions browser/websocket/pull.ts → websocket/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import {
getPage,
type GetPageOption,
type TooLongURIError,
} from "../../rest/pages.ts";
import { getProfile } from "../../rest/profile.ts";
import { getProject } from "../../rest/project.ts";
import type { HTTPError } from "../../rest/responseIntoResult.ts";
import type { AbortError, NetworkError } from "../../rest/robustFetch.ts";
import type { BaseOptions } from "../../rest/options.ts";
} from "../rest/pages.ts";
import { getProfile } from "../rest/profile.ts";
import { getProject } from "../rest/project.ts";
import type { HTTPError } from "../rest/responseIntoResult.ts";
import type { AbortError, NetworkError } from "../rest/robustFetch.ts";
import type { BaseOptions } from "../rest/options.ts";

/** Extended page metadata required for WebSocket operations
*
Expand Down
15 changes: 8 additions & 7 deletions browser/websocket/push.ts → websocket/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ import {
unwrapErr,
unwrapOk,
} from "option-t/plain_result";
import type { HTTPError } from "../../rest/responseIntoResult.ts";
import type { AbortError, NetworkError } from "../../rest/robustFetch.ts";
import type { TooLongURIError } from "../../rest/pages.ts";
import type { HTTPError } from "../rest/responseIntoResult.ts";
import type { AbortError, NetworkError } from "../rest/robustFetch.ts";
import type { TooLongURIError } from "../rest/pages.ts";
import type {
SocketIOServerDisconnectError,
UnexpectedRequestError,
} from "./error.ts";
import type { BaseOptions } from "../rest/options.ts";

/** Configuration options for the push operation */
export interface PushOptions {
export interface PushOptions extends BaseOptions {
/** Optional Socket instance for external WebSocket connection control
*
* This allows providing an existing Socket instance instead of creating
Expand Down Expand Up @@ -159,15 +160,15 @@ export const push = async (
makeCommit: CommitMakeHandler,
options?: PushOptions,
): Promise<Result<string, PushError>> => {
const result = await connect(options?.socket);
const result = await connect(options?.socket, options?.sid);
if (isErr(result)) {
return createErr({
name: "UnexpectedRequestError",
error: unwrapErr(result),
});
}
const socket = unwrapOk(result);
const pullResult = await pull(project, title);
const pullResult = await pull(project, title, options);
if (isErr(pullResult)) return pullResult;
let metadata = unwrapOk(pullResult);

Expand Down Expand Up @@ -239,7 +240,7 @@ export const push = async (
if (name === "NotFastForwardError") {
await delay(1000); // Brief delay to avoid rapid retries
// Fetch latest page state
const pullResult = await pull(project, title);
const pullResult = await pull(project, title, options);
if (isErr(pullResult)) return pullResult;
metadata = unwrapOk(pullResult);
}
Expand Down
14 changes: 13 additions & 1 deletion browser/websocket/socket.ts → websocket/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,34 @@ import { io, type Socket } from "socket.io-client";
import { createErr, createOk, type Result } from "option-t/plain_result";
import type { ListenEvents } from "./listen-events.ts";
import type { EmitEvents } from "./emit-events.ts";
import { cookie } from "../rest/auth.ts";

/** A pre-configured {@linkcode Socket} type for Scrapbox */
export type ScrapboxSocket = Socket<ListenEvents, EmitEvents>;

/** connect to websocket
*
* @param socket - The {@linkcode Socket} to be connected. If not provided, a new socket will be created
* @param sid - Scrapbox session ID (connect.sid). This is only required in Deno/Node.js environment.
* @returns A {@linkcode Promise}<{@linkcode Socket}> that resolves to a {@linkcode Socket} if connected successfully, or an {@linkcode Error} if failed
*/
export const connect = (socket?: ScrapboxSocket): Promise<
export const connect = (socket?: ScrapboxSocket, sid?: string): Promise<
Result<ScrapboxSocket, Socket.DisconnectReason>
> => {
if (socket?.connected) return Promise.resolve(createOk(socket));
socket ??= io("https://scrapbox.io", {
reconnectionDelay: 5000,
transports: ["websocket"],
...(sid
? {
rejectUnauthorized: false,
extraHeaders: {
Cookie: cookie(sid),
Host: "scrapbox.io",
Referer: "https://scrapbox.io/",
},
}
: {}),
});

const promise = new Promise<
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { BaseLine } from "@cosense/types/rest";
import type { DeleteChange, InsertChange, UpdateChange } from "./change.ts";
import type { TinyCodeBlock } from "../../rest/getCodeBlocks.ts";
import type { TinyCodeBlock } from "../rest/getCodeBlocks.ts";
import { diffToChanges } from "./diffToChanges.ts";
import { isSimpleCodeFile } from "./isSimpleCodeFile.ts";
import type { SimpleCodeFile } from "./updateCodeFile.ts";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { BaseLine } from "@cosense/types/rest";
import type { DeleteChange, InsertChange, UpdateChange } from "./change.ts";
import { getCodeBlocks, type TinyCodeBlock } from "../../rest/getCodeBlocks.ts";
import { getCodeBlocks, type TinyCodeBlock } from "../rest/getCodeBlocks.ts";
import { createNewLineId } from "./id.ts";
import { diff, toExtendedChanges } from "../../deps/onp.ts";
import { diff, toExtendedChanges } from "../deps/onp.ts";
import { countBodyIndent } from "./_codeBlock.ts";
import { push, type PushError, type PushOptions } from "./push.ts";
import type { Result } from "option-t/plain_result";
Expand Down Expand Up @@ -127,7 +127,10 @@ export const updateCodeFile = (
): Promise<Result<string, PushError>> => {
/** Set default values for options here */
const defaultOptions: Required<
Omit<UpdateCodeFileOptions, "maxAttempts" | "socket">
Pick<
UpdateCodeFileOptions,
"insertPositionIfNotExist" | "isInsertEmptyLineInTail" | "debug"
>
> = {
insertPositionIfNotExist: "notInsert",
isInsertEmptyLineInTail: true,
Expand Down

0 comments on commit 22d91dd

Please sign in to comment.