@devvit/server 0.11.16-next-2025-05-19-1403da2a7.0 → 0.11.16-next-2025-05-20-7f7f5d7b2.0
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/context.d.ts +15 -0
- package/context.d.ts.map +1 -0
- package/context.js +19 -0
- package/create-server.d.ts +13 -0
- package/create-server.d.ts.map +1 -0
- package/create-server.js +39 -0
- package/get-server-port.d.ts +8 -0
- package/get-server-port.d.ts.map +1 -0
- package/get-server-port.js +14 -0
- package/index.d.ts +2 -0
- package/index.d.ts.map +1 -1
- package/index.js +2 -0
- package/package.json +7 -7
- package/webbit-server.d.ts.map +1 -1
- package/webbit-server.js +2 -1
package/context.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type DevvitServerRequestContext = 42;
|
|
2
|
+
/**
|
|
3
|
+
* Gets the current DevvitServerRequestContext. This is set by the server when handling a request.
|
|
4
|
+
* @returns The current DevvitServerRequestContext, or undefined if there is none.
|
|
5
|
+
*/
|
|
6
|
+
export declare function getContext(): DevvitServerRequestContext | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* Runs an async callback with the given DevvitServerRequestContext. Code in the callback can use
|
|
9
|
+
* `getContext()` to get the context.
|
|
10
|
+
* @internal
|
|
11
|
+
* @param context The context to run the callback with
|
|
12
|
+
* @param callback The callback to run
|
|
13
|
+
*/
|
|
14
|
+
export declare function runWithContext<T>(context: DevvitServerRequestContext, callback: () => Promise<T>): Promise<T>;
|
|
15
|
+
//# sourceMappingURL=context.d.ts.map
|
package/context.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,0BAA0B,GAAG,EAAE,CAAC;AAI5C;;;GAGG;AACH,wBAAgB,UAAU,IAAI,0BAA0B,GAAG,SAAS,CAEnE;AAED;;;;;;GAMG;AACH,wBAAsB,cAAc,CAAC,CAAC,EACpC,OAAO,EAAE,0BAA0B,EACnC,QAAQ,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GACzB,OAAO,CAAC,CAAC,CAAC,CAEZ"}
|
package/context.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
const requestContext = new AsyncLocalStorage();
|
|
3
|
+
/**
|
|
4
|
+
* Gets the current DevvitServerRequestContext. This is set by the server when handling a request.
|
|
5
|
+
* @returns The current DevvitServerRequestContext, or undefined if there is none.
|
|
6
|
+
*/
|
|
7
|
+
export function getContext() {
|
|
8
|
+
return requestContext.getStore();
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Runs an async callback with the given DevvitServerRequestContext. Code in the callback can use
|
|
12
|
+
* `getContext()` to get the context.
|
|
13
|
+
* @internal
|
|
14
|
+
* @param context The context to run the callback with
|
|
15
|
+
* @param callback The callback to run
|
|
16
|
+
*/
|
|
17
|
+
export async function runWithContext(context, callback) {
|
|
18
|
+
return requestContext.run(context, callback);
|
|
19
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createServer as nodeCreateServer } from 'node:http';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a new Devvit server. This implements the same API as Node.js's `createServer` function,
|
|
4
|
+
* but we do not guarantee that this actually creates an HTTP server of any kind - it may be any
|
|
5
|
+
* kind of server under the hood. However, it will behave like an HTTP server in terms of the API.
|
|
6
|
+
*/
|
|
7
|
+
export declare const createServer: typeof nodeCreateServer;
|
|
8
|
+
declare global {
|
|
9
|
+
namespace globalThis {
|
|
10
|
+
var enableWebbitBundlingHack: boolean;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=create-server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-server.d.ts","sourceRoot":"","sources":["../src/create-server.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,YAAY,IAAI,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAG7D;;;;GAIG;AACH,eAAO,MAAM,YAAY,EAAE,OAAO,gBAYjC,CAAC;AAuCF,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,UAAU,CAAC;QAEnB,IAAI,wBAAwB,EAAE,OAAO,CAAC;KACvC;CACF"}
|
package/create-server.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { createServer as nodeCreateServer } from 'node:http';
|
|
2
|
+
import { runWithContext } from './context.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a new Devvit server. This implements the same API as Node.js's `createServer` function,
|
|
5
|
+
* but we do not guarantee that this actually creates an HTTP server of any kind - it may be any
|
|
6
|
+
* kind of server under the hood. However, it will behave like an HTTP server in terms of the API.
|
|
7
|
+
*/
|
|
8
|
+
export const createServer = (optionsOrRequestListener, requestListener) => {
|
|
9
|
+
if (typeof optionsOrRequestListener === 'function') {
|
|
10
|
+
return _createServer({}, optionsOrRequestListener);
|
|
11
|
+
}
|
|
12
|
+
return _createServer(optionsOrRequestListener ?? {}, requestListener);
|
|
13
|
+
};
|
|
14
|
+
function _createServer(options, requestListener) {
|
|
15
|
+
const server = nodeCreateServer(options, async (req, res) => {
|
|
16
|
+
// TODO: Implement request context
|
|
17
|
+
return runWithContext(42, async () => {
|
|
18
|
+
return requestListener?.(req, res);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
// Modify the listen callback slightly, to do the Devvit-y bundling hack
|
|
22
|
+
// TODO: When we stop needing to execute code in the bundler, we can remove this. I suspect once
|
|
23
|
+
// devvit.json is fully implemented, we won't need this anymore.
|
|
24
|
+
const originalListen = server.listen.bind(server);
|
|
25
|
+
server.listen = (...args) => {
|
|
26
|
+
// Yes we're using `any` here - we don't care about the types of the arguments, we just want to
|
|
27
|
+
// make sure we call the original listen function with the same arguments, and sadly using
|
|
28
|
+
// unknown here doesn't work.
|
|
29
|
+
if (globalThis.enableWebbitBundlingHack) {
|
|
30
|
+
// This is a hack to get around the fact that we use a side effect import to start the server in
|
|
31
|
+
// prod, but we really don't want to do that when we're just running the code to bundle it.
|
|
32
|
+
// We'll set this global variable in the bundler, and then we can check it here to see if we
|
|
33
|
+
// should actually start the server or not.
|
|
34
|
+
return server;
|
|
35
|
+
}
|
|
36
|
+
return originalListen(...args);
|
|
37
|
+
};
|
|
38
|
+
return server;
|
|
39
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets the port the internal server should run on. Call this when starting the server to get the
|
|
3
|
+
* port to use.
|
|
4
|
+
* @example createServer(myHandler).listen(getServerPort());
|
|
5
|
+
* @returns The port to use for the server.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getServerPort(): number;
|
|
8
|
+
//# sourceMappingURL=get-server-port.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-server-port.d.ts","sourceRoot":"","sources":["../src/get-server-port.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,wBAAgB,aAAa,IAAI,MAAM,CAMtC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import process from 'node:process';
|
|
2
|
+
/**
|
|
3
|
+
* Gets the port the internal server should run on. Call this when starting the server to get the
|
|
4
|
+
* port to use.
|
|
5
|
+
* @example createServer(myHandler).listen(getServerPort());
|
|
6
|
+
* @returns The port to use for the server.
|
|
7
|
+
*/
|
|
8
|
+
export function getServerPort() {
|
|
9
|
+
const envPort = Number(process.env.WEBBIT_PORT);
|
|
10
|
+
if (isNaN(envPort)) {
|
|
11
|
+
return 3000;
|
|
12
|
+
}
|
|
13
|
+
return envPort;
|
|
14
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export { createServer } from './create-server.js';
|
|
2
|
+
export { getServerPort } from './get-server-port.js';
|
|
1
3
|
export { RequestContext, setRequestContext } from './request-context.js';
|
|
2
4
|
export { defineConfig } from './webbit-post.js';
|
|
3
5
|
export { webbitEnable } from './webbit-server.js';
|
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
|
package/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export { createServer } from './create-server.js';
|
|
2
|
+
export { getServerPort } from './get-server-port.js';
|
|
1
3
|
export { RequestContext, setRequestContext } from './request-context.js';
|
|
2
4
|
export { defineConfig } from './webbit-post.js';
|
|
3
5
|
export { webbitEnable } from './webbit-server.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devvit/server",
|
|
3
|
-
"version": "0.11.16-next-2025-05-
|
|
3
|
+
"version": "0.11.16-next-2025-05-20-7f7f5d7b2.0",
|
|
4
4
|
"license": "BSD-3-Clause",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -23,13 +23,13 @@
|
|
|
23
23
|
},
|
|
24
24
|
"types": "./index.d.ts",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@devvit/protos": "0.11.16-next-2025-05-
|
|
27
|
-
"@devvit/public-api": "0.11.16-next-2025-05-
|
|
28
|
-
"@devvit/shared-types": "0.11.16-next-2025-05-
|
|
26
|
+
"@devvit/protos": "0.11.16-next-2025-05-20-7f7f5d7b2.0",
|
|
27
|
+
"@devvit/public-api": "0.11.16-next-2025-05-20-7f7f5d7b2.0",
|
|
28
|
+
"@devvit/shared-types": "0.11.16-next-2025-05-20-7f7f5d7b2.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@devvit/repo-tools": "0.11.16-next-2025-05-
|
|
32
|
-
"@devvit/tsconfig": "0.11.16-next-2025-05-
|
|
31
|
+
"@devvit/repo-tools": "0.11.16-next-2025-05-20-7f7f5d7b2.0",
|
|
32
|
+
"@devvit/tsconfig": "0.11.16-next-2025-05-20-7f7f5d7b2.0",
|
|
33
33
|
"eslint": "9.11.1",
|
|
34
34
|
"typescript": "5.8.3",
|
|
35
35
|
"vitest": "1.6.1"
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"directory": "dist"
|
|
39
39
|
},
|
|
40
40
|
"source": "./src/index.ts",
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "7e1c25ed0a59347c7ae418749d076fa2b3bb81d7"
|
|
42
42
|
}
|
package/webbit-server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webbit-server.d.ts","sourceRoot":"","sources":["../src/webbit-server.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,KAAK,YAAY,EAElB,MAAM,oDAAoD,CAAC;
|
|
1
|
+
{"version":3,"file":"webbit-server.d.ts","sourceRoot":"","sources":["../src/webbit-server.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,KAAK,YAAY,EAElB,MAAM,oDAAoD,CAAC;AAM5D,wBAAgB,YAAY,IAAI,IAAI,CAInC;AAED,wBAAgB,eAAe,IAAI,YAAY,CAsB9C"}
|
package/webbit-server.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { HttpMethod, WebbitServerDefinition, } from '@devvit/protos/types/devvit/actor/webbit/webbit.js';
|
|
2
2
|
import { Devvit } from '@devvit/public-api';
|
|
3
3
|
import { extendDevvitPrototype } from '@devvit/public-api/devvit/internals/helpers/extendDevvitPrototype.js';
|
|
4
|
+
import { getServerPort } from './get-server-port.js';
|
|
4
5
|
export function webbitEnable() {
|
|
5
6
|
const server = newWebbitServer();
|
|
6
7
|
extendDevvitPrototype('Request', server.Request);
|
|
@@ -9,7 +10,7 @@ export function webbitEnable() {
|
|
|
9
10
|
export function newWebbitServer() {
|
|
10
11
|
return {
|
|
11
12
|
async Request(req, meta) {
|
|
12
|
-
const port =
|
|
13
|
+
const port = getServerPort();
|
|
13
14
|
// only set the body if the method is not GET or HEAD
|
|
14
15
|
const body = [HttpMethod.GET, HttpMethod.HEAD].includes(req.method) ? null : req.body;
|
|
15
16
|
const rsp = await fetch(new URL(req.path, `http://webbit.local:${port}`), {
|