@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 2.71 kB
JavaScript
const OPAQUE_PARTITION_KEY="<opaque>";export class Cookie{#t;#e;#i;#r;#s;#n;#a;constructor(t,e,i,r){this.#t=t,this.#e=e,this.#i=i,this.#r={},this.#s=0,this.#n=r||"Medium",this.#a=null}static fromProtocolCookie(t){const e=new Cookie(t.name,t.value,null,t.priority);return e.addAttribute("domain",t.domain),e.addAttribute("path",t.path),t.expires&&e.addAttribute("expires",1e3*t.expires),t.httpOnly&&e.addAttribute("httpOnly"),t.secure&&e.addAttribute("secure"),t.sameSite&&e.addAttribute("sameSite",t.sameSite),"sourcePort"in t&&e.addAttribute("sourcePort",t.sourcePort),"sourceScheme"in t&&e.addAttribute("sourceScheme",t.sourceScheme),"partitionKey"in t&&e.addAttribute("partitionKey",t.partitionKey),"partitionKeyOpaque"in t&&e.addAttribute("partitionKey","<opaque>"),e.setSize(t.size),e}key(){return(this.domain()||"-")+" "+this.name()+" "+(this.path()||"-")}name(){return this.#t}value(){return this.#e}type(){return this.#i}httpOnly(){return"httponly"in this.#r}secure(){return"secure"in this.#r}sameSite(){return this.#r.samesite}partitionKey(){return this.#r.partitionkey}setPartitionKey(t){this.addAttribute("partitionKey",t)}partitionKeyOpaque(){return"<opaque>"===this.#r.partitionkey}setPartitionKeyOpaque(){this.addAttribute("partitionKey","<opaque>")}priority(){return this.#n}session(){return!("expires"in this.#r||"max-age"in this.#r)}path(){return this.#r.path}domain(){return this.#r.domain}expires(){return this.#r.expires}maxAge(){return this.#r["max-age"]}sourcePort(){return this.#r.sourceport}sourceScheme(){return this.#r.sourcescheme}size(){return this.#s}url(){if(!this.domain()||!this.path())return null;let t="";const e=this.sourcePort();return e&&80!==e&&443!==e&&(t=`:${this.sourcePort()}`),(this.secure()?"https://":"http://")+this.domain()+t+this.path()}setSize(t){this.#s=t}expiresDate(t){return this.maxAge()?new Date(t.getTime()+1e3*this.maxAge()):this.expires()?new Date(this.expires()):null}addAttribute(t,e){const i=t.toLowerCase();if("priority"===i)this.#n=e;else this.#r[i]=e}setCookieLine(t){this.#a=t}getCookieLine(){return this.#a}matchesSecurityOrigin(t){const e=new URL(t).hostname;return Cookie.isDomainMatch(this.domain(),e)}static isDomainMatch(t,e){return e===t||!(!t||"."!==t[0])&&(t.substr(1)===e||e.length>t.length&&e.endsWith(t))}}export var Type;!function(t){t[t.Request=0]="Request",t[t.Response=1]="Response"}(Type||(Type={}));export var Attributes;!function(t){t.Name="name",t.Value="value",t.Size="size",t.Domain="domain",t.Path="path",t.Expires="expires",t.HttpOnly="httpOnly",t.Secure="secure",t.SameSite="sameSite",t.SourceScheme="sourceScheme",t.SourcePort="sourcePort",t.Priority="priority",t.PartitionKey="partitionKey"}(Attributes||(Attributes={}));